Browse code

lavfi/scale: implement clever/insane parsing heuristic, and add a size option

If the first argument can be read as a video size, set that output size
in the scale. This allows to specify in a filtergraph scale=qcif or
scale=320x240.

This is not completely safe, for example in case of a typo in the video
size string the first argument will be read as the input width
expression, giving rise to a confusing comment.

Stefano Sabatini authored on 2012/10/29 19:48:53
Showing 3 changed files
... ...
@@ -3017,7 +3017,9 @@ of the input, by changing the output sample aspect ratio.
3017 3017
 This filter accepts a list of named options in the form of
3018 3018
 @var{key}=@var{value} pairs separated by ":". If the key for the first
3019 3019
 two options is not specified, the assumed keys for the first two
3020
-values are @code{w} and @code{h}.
3020
+values are @code{w} and @code{h}. If the first option has no key and
3021
+can be interpreted like a video size specification, it will be used
3022
+to set the video size.
3021 3023
 
3022 3024
 A description of the accepted options follows.
3023 3025
 
... ...
@@ -3050,6 +3052,10 @@ Default value is @code{0}.
3050 3050
 @item flags
3051 3051
 Set libswscale scaling flags. If not explictly specified the filter
3052 3052
 applies a bilinear scaling algorithm.
3053
+
3054
+@item size, s
3055
+Set the video size, the value must be a valid abbreviation or in the
3056
+form @var{width}x@var{height}.
3053 3057
 @end table
3054 3058
 
3055 3059
 The values of the @var{w} and @var{h} options are expressions
... ...
@@ -3102,12 +3108,27 @@ Scale the input video to a size of 200x100:
3102 3102
 scale=200:100
3103 3103
 @end example
3104 3104
 
3105
-@item
3106
-The above example is the same as:
3105
+This is equivalent to:
3107 3106
 @example
3108 3107
 scale=w=200:h=100
3109 3108
 @end example
3110 3109
 
3110
+or:
3111
+@example
3112
+scale=200x100
3113
+@end example
3114
+
3115
+@item
3116
+Specify a size abbreviation for the output size:
3117
+@example
3118
+scale=qcif
3119
+@end example
3120
+
3121
+which can also be written as:
3122
+@example
3123
+scale=size=qcif
3124
+@end example
3125
+
3111 3126
 @item
3112 3127
 Scale the input to 2x:
3113 3128
 @example
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32 32
 #define LIBAVFILTER_VERSION_MINOR  20
33
-#define LIBAVFILTER_VERSION_MICRO 107
33
+#define LIBAVFILTER_VERSION_MICRO 108
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
... ...
@@ -35,6 +35,7 @@
35 35
 #include "libavutil/internal.h"
36 36
 #include "libavutil/mathematics.h"
37 37
 #include "libavutil/opt.h"
38
+#include "libavutil/parseutils.h"
38 39
 #include "libavutil/pixdesc.h"
39 40
 #include "libavutil/imgutils.h"
40 41
 #include "libavutil/avassert.h"
... ...
@@ -78,6 +79,7 @@ typedef struct {
78 78
      */
79 79
     int w, h;
80 80
     char *flags_str;            ///sws flags string
81
+    char *size_str;
81 82
     unsigned int flags;         ///sws flags
82 83
 
83 84
     int hsub, vsub;             ///< chroma subsampling
... ...
@@ -94,12 +96,14 @@ typedef struct {
94 94
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
95 95
 
96 96
 static const AVOption scale_options[] = {
97
-    { "w",      "set width expression",    OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS },
98
-    { "width",  "set width expression",    OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS },
99
-    { "h",      "set height expression",   OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS },
100
-    { "height", "set height expression",   OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS },
97
+    { "w",      "set width expression",    OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
98
+    { "width",  "set width expression",    OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
99
+    { "h",      "set height expression",   OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
100
+    { "height", "set height expression",   OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
101 101
     { "flags",  "set libswscale flags",    OFFSET(flags_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, INT_MAX, FLAGS },
102 102
     { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
103
+    { "size",   "set video size",          OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
104
+    { "s",      "set video size",          OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
103 105
     { NULL },
104 106
 };
105 107
 
... ...
@@ -110,13 +114,45 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
110 110
     ScaleContext *scale = ctx->priv;
111 111
     static const char *shorthand[] = { "w", "h", NULL };
112 112
     int ret;
113
+    const char *args0 = args;
113 114
 
114 115
     scale->class = &scale_class;
115 116
     av_opt_set_defaults(scale);
116 117
 
118
+    if (args && (scale->size_str = av_get_token(&args, ":"))) {
119
+        if (av_parse_video_size(&scale->w, &scale->h, scale->size_str) < 0) {
120
+            av_freep(&scale->size_str);
121
+            args = args0;
122
+        } else if (*args)
123
+            args++;
124
+    }
125
+
117 126
     if ((ret = av_opt_set_from_string(scale, args, shorthand, "=", ":")) < 0)
118 127
         return ret;
119 128
 
129
+    if (scale->size_str && (scale->w_expr || scale->h_expr)) {
130
+        av_log(ctx, AV_LOG_ERROR,
131
+               "Size and width/height expressions cannot be set at the same time.\n");
132
+            return AVERROR(EINVAL);
133
+    }
134
+
135
+    if (scale->size_str) {
136
+        char buf[32];
137
+        if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
138
+            av_log(ctx, AV_LOG_ERROR,
139
+                   "Invalid size '%s'\n", scale->size_str);
140
+            return ret;
141
+        }
142
+        snprintf(buf, sizeof(buf)-1, "%d", scale->w);
143
+        av_opt_set(scale, "w", buf, 0);
144
+        snprintf(buf, sizeof(buf)-1, "%d", scale->h);
145
+        av_opt_set(scale, "h", buf, 0);
146
+    }
147
+    if (!scale->w_expr)
148
+        av_opt_set(scale, "w", "iw", 0);
149
+    if (!scale->h_expr)
150
+        av_opt_set(scale, "h", "ih", 0);
151
+
120 152
     av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:%s interl:%d\n",
121 153
            scale->w_expr, scale->h_expr, scale->flags_str, scale->interlaced);
122 154