From 70ffda3217c58bbbfb8a7e7c58824b8ca6c56128 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini Date: Tue, 17 Jan 2012 15:25:14 +0100 Subject: lavu: introduce av_parse_ratio() and use it in ffmpeg and lavfi/aspect Factorize code and provide ratio parsing consistency. --- libavutil/avutil.h | 2 +- libavutil/log.h | 2 ++ libavutil/parseutils.c | 31 +++++++++++++++++++++++++++---- libavutil/parseutils.h | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) (limited to 'libavutil') diff --git a/libavutil/avutil.h b/libavutil/avutil.h index 5d574e8851..c8427fd0bf 100644 --- a/libavutil/avutil.h +++ b/libavutil/avutil.h @@ -154,7 +154,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 51 -#define LIBAVUTIL_VERSION_MINOR 37 +#define LIBAVUTIL_VERSION_MINOR 38 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ diff --git a/libavutil/log.h b/libavutil/log.h index 02a0e1ac8c..26c806f644 100644 --- a/libavutil/log.h +++ b/libavutil/log.h @@ -124,6 +124,8 @@ typedef struct AVClass { */ #define AV_LOG_DEBUG 48 +#define AV_LOG_MAX_OFFSET (AV_LOG_DEBUG - AV_LOG_QUIET) + /** * Send the specified message to the log if the level is less than or equal * to the current av_log_level. By default, all logging messages are sent to diff --git a/libavutil/parseutils.c b/libavutil/parseutils.c index 2649e3b2bc..802931dd2e 100644 --- a/libavutil/parseutils.c +++ b/libavutil/parseutils.c @@ -31,6 +31,32 @@ #include "random_seed.h" #include "parseutils.h" +int av_parse_ratio(AVRational *q, const char *str, int max, + int log_offset, void *log_ctx) +{ + char c; + int ret; + int64_t gcd; + + if (sscanf(str, "%d:%d%c", &q->num, &q->den, &c) != 2) { + double d; + ret = av_expr_parse_and_eval(&d, str, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, log_offset, log_ctx); + if (ret < 0) + return ret; + *q = av_d2q(d, max); + } + + gcd = av_gcd(FFABS(q->num), FFABS(q->den)); + if (gcd) { + q->num /= gcd; + q->den /= gcd; + } + + return 0; +} + typedef struct { const char *abbr; int width, height; @@ -124,7 +150,6 @@ int av_parse_video_rate(AVRational *rate, const char *arg) { int i, ret; int n = FF_ARRAY_ELEMS(video_rate_abbrs); - double res; /* First, we check our abbreviation table */ for (i = 0; i < n; ++i) @@ -134,10 +159,8 @@ int av_parse_video_rate(AVRational *rate, const char *arg) } /* Then, we try to parse it as fraction */ - if ((ret = av_expr_parse_and_eval(&res, arg, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, 0, NULL)) < 0) + if ((ret = av_parse_ratio_quiet(rate, arg, 1001000)) < 0) return ret; - *rate = av_d2q(res, 1001000); if (rate->num <= 0 || rate->den <= 0) return AVERROR(EINVAL); return 0; diff --git a/libavutil/parseutils.h b/libavutil/parseutils.h index 2a74a060f2..a545652d9e 100644 --- a/libavutil/parseutils.h +++ b/libavutil/parseutils.h @@ -28,6 +28,30 @@ * misc parsing utilities */ +/** + * Parse str and store the parsed ratio in q. + * + * Note that a ratio with infinite (1/0) or negative value is + * considered valid, so you should check on the returned value if you + * want to exclude those values. + * + * The undefined value can be expressed using the "0:0" string. + * + * @param[in,out] q pointer to the AVRational which will contain the ratio + * @param[in] str the string to parse: it has to be a string in the format + * num:den, a float number or an expression + * @param[in] max the maximum allowed numerator and denominator + * @param[in] log_offset log level offset which is applied to the log + * level of log_ctx + * @param[in] log_ctx parent logging context + * @return >= 0 on success, a negative error code otherwise + */ +int av_parse_ratio(AVRational *q, const char *str, int max, + int log_offset, void *log_ctx); + +#define av_parse_ratio_quiet(rate, str, max) \ + av_parse_ratio(rate, str, max, AV_LOG_MAX_OFFSET, NULL) + /** * Parse str and put in width_ptr and height_ptr the detected values. * -- cgit v1.2.3