[FFmpeg-devel] [PATCH] lavfi/aspect: extend syntax for the setdar and setsar filters

Stefano Sabatini stefasab at gmail.com
Tue Oct 16 21:45:11 CEST 2012


Add support for named options, and deprecate old "num:den" ambiguous
syntax.

TODO: bump micro
---
 doc/filters.texi        |   27 ++++++++++++-----
 libavfilter/vf_aspect.c |   72 +++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 83 insertions(+), 16 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 916655a..df1e0c7 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -3250,16 +3250,27 @@ Keep in mind that the sample aspect ratio set by the @code{setsar}
 filter may be changed by later filters in the filterchain, e.g. if
 another "setsar" or a "setdar" filter is applied.
 
-The @code{setdar} and @code{setsar} filters accept a parameter string
-which represents the wanted aspect ratio.  The parameter can
-be a floating point number string, an expression, or a string of the form
- at var{num}:@var{den}, where @var{num} and @var{den} are the numerator
-and denominator of the aspect ratio. If the parameter is not
-specified, it is assumed the value "0:1".
+The @code{setdar} and @code{setsar} filters accept a string in the
+form @var{num}:@var{den} expressing an aspect ratio, or the following
+named options, expressed as a sequence of @var{key}=@var{value} pairs,
+separated by ":".
+
+ at table @option
+
+ at item r, ratio:
+Set the aspect ratio used by the filter.
+
+The parameter can be a floating point number string, an expression, or
+a string of the form @var{num}:@var{den}, where @var{num} and
+ at var{den} are the numerator and denominator of the aspect ratio. If
+the parameter is not specified, it is assumed the value "0".
+In case the form "@var{num}:@var{den}" the @code{:} character should
+be escaped.
+ at end table
 
 For example to change the display aspect ratio to 16:9, specify:
 @example
-setdar=16:9
+setdar='16:9'
 @end example
 
 The example above is equivalent to:
@@ -3269,7 +3280,7 @@ setdar=1.77777
 
 To change the sample aspect ratio to 10:11, specify:
 @example
-setsar=10:11
+setsar='10:11'
 @end example
 
 @section setfield
diff --git a/libavfilter/vf_aspect.c b/libavfilter/vf_aspect.c
index 4e5ba77..c3e52c8 100644
--- a/libavfilter/vf_aspect.c
+++ b/libavfilter/vf_aspect.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/opt.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
@@ -31,19 +32,44 @@
 #include "video.h"
 
 typedef struct {
+    const AVClass *class;
     AVRational ratio;
+    char *ratio_str;
 } AspectContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+#define OFFSET(x) offsetof(AspectContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption options[] = {
+    {"ratio", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
+    {"r",     "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
+    {NULL}
+};
+
+static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
 {
     AspectContext *aspect = ctx->priv;
-    aspect->ratio = (AVRational) {0, 1};
+    static const char *shorthand[] = { "ratio", NULL };
+    char c;
+    int ret;
+    AVRational q;
+
+    aspect->class = class;
+    av_opt_set_defaults(aspect);
+
+    if (sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
+        aspect->ratio_str = av_strdup(args);
+        av_log(ctx, AV_LOG_WARNING,
+               "num:den syntax is deprecated, please use num/den or named options instead\n");
+    } else if ((ret = av_opt_set_from_string(aspect, args, shorthand, "=", ":")) < 0) {
+        return ret;
+    }
 
-    if (args) {
-        if (av_parse_ratio(&aspect->ratio, args, 100, 0, ctx) < 0 ||
-            aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
+    if (aspect->ratio_str) {
+        ret = av_parse_ratio(&aspect->ratio, aspect->ratio_str, 100, 0, ctx);
+        if (ret < 0 || aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
             av_log(ctx, AV_LOG_ERROR,
-                   "Invalid string '%s' for aspect ratio.\n", args);
+                   "Invalid string '%s' for aspect ratio\n", args);
             return AVERROR(EINVAL);
         }
     }
@@ -61,7 +87,23 @@ static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
     return ff_start_frame(link->dst->outputs[0], picref);
 }
 
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    AspectContext *aspect = ctx->priv;
+
+    av_opt_free(aspect);
+}
+
 #if CONFIG_SETDAR_FILTER
+
+#define setdar_options options
+AVFILTER_DEFINE_CLASS(setdar);
+
+static av_cold int setdar_init(AVFilterContext *ctx, const char *args)
+{
+    return init(ctx, args, &setdar_class);
+}
+
 static int setdar_config_props(AVFilterLink *inlink)
 {
     AspectContext *aspect = inlink->dst->priv;
@@ -103,17 +145,28 @@ AVFilter avfilter_vf_setdar = {
     .name      = "setdar",
     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
 
-    .init      = init,
+    .init      = setdar_init,
 
     .priv_size = sizeof(AspectContext),
 
     .inputs    = avfilter_vf_setdar_inputs,
 
     .outputs   = avfilter_vf_setdar_outputs,
+    .priv_class = &setdar_class,
 };
+
 #endif /* CONFIG_SETDAR_FILTER */
 
 #if CONFIG_SETSAR_FILTER
+
+#define setsar_options options
+AVFILTER_DEFINE_CLASS(setsar);
+
+static av_cold int setsar_init(AVFilterContext *ctx, const char *args)
+{
+    return init(ctx, args, &setsar_class);
+}
+
 static int setsar_config_props(AVFilterLink *inlink)
 {
     AspectContext *aspect = inlink->dst->priv;
@@ -147,12 +200,15 @@ AVFilter avfilter_vf_setsar = {
     .name      = "setsar",
     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
 
-    .init      = init,
+    .init      = setsar_init,
+    .uninit    = uninit,
 
     .priv_size = sizeof(AspectContext),
 
     .inputs    = avfilter_vf_setsar_inputs,
 
     .outputs   = avfilter_vf_setsar_outputs,
+    .priv_class = &setdar_class,
 };
+
 #endif /* CONFIG_SETSAR_FILTER */
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list