Browse code

buffersrc: accept key=value arguments.

The current flat arguments syntax is not easily extensible
due to sws_param possibly containing commas.
This is also consistent with abuffersrc.

Nicolas George authored on 2012/06/05 19:33:35
Showing 2 changed files
... ...
@@ -3162,33 +3162,26 @@ Buffer video frames, and make them available to the filter chain.
3162 3162
 This source is mainly intended for a programmatic use, in particular
3163 3163
 through the interface defined in @file{libavfilter/vsrc_buffer.h}.
3164 3164
 
3165
-It accepts the following parameters:
3166
-@var{width}:@var{height}:@var{pix_fmt_string}:@var{timebase_num}:@var{timebase_den}:@var{sample_aspect_ratio_num}:@var{sample_aspect_ratio.den}:@var{scale_params}
3167
-
3168
-All the parameters but @var{scale_params} need to be explicitly
3169
-defined.
3170
-
3171
-Follows the list of the accepted parameters.
3165
+It accepts a list of options in the form of @var{key}=@var{value} pairs
3166
+separated by ":". A descroption of the accepted options follows.
3172 3167
 
3173 3168
 @table @option
3174 3169
 
3175
-@item width, height
3176
-Specify the width and height of the buffered video frames.
3170
+@item video_size
3171
+Specify the size (width and height) of the buffered video frames.
3177 3172
 
3178
-@item pix_fmt_string
3173
+@item pix_fmt
3179 3174
 A string representing the pixel format of the buffered video frames.
3180 3175
 It may be a number corresponding to a pixel format, or a pixel format
3181 3176
 name.
3182 3177
 
3183
-@item timebase_num, timebase_den
3184
-Specify numerator and denomitor of the timebase assumed by the
3185
-timestamps of the buffered frames.
3178
+@item time_base
3179
+Specify the timebase assumed by the timestamps of the buffered frames.
3186 3180
 
3187
-@item sample_aspect_ratio.num, sample_aspect_ratio.den
3188
-Specify numerator and denominator of the sample aspect ratio assumed
3189
-by the video frames.
3181
+@item pixel_aspect
3182
+Specify the sample aspect ratio assumed by the video frames.
3190 3183
 
3191
-@item scale_params
3184
+@item sws_param
3192 3185
 Specify the optional parameters to be used for the scale filter which
3193 3186
 is automatically inserted when an input change is detected in the
3194 3187
 input size or format.
... ...
@@ -3196,7 +3189,7 @@ input size or format.
3196 3196
 
3197 3197
 For example:
3198 3198
 @example
3199
-buffer=320:240:yuv410p:1:24:1:1
3199
+buffer=size=320x240:pix_fmt=yuv410p:time_base=1/24:pixel_aspect=1/1
3200 3200
 @end example
3201 3201
 
3202 3202
 will instruct the source to accept video frames with size 320x240 and
... ...
@@ -3206,9 +3199,14 @@ Since the pixel format with name "yuv410p" corresponds to the number 6
3206 3206
 (check the enum PixelFormat definition in @file{libavutil/pixfmt.h}),
3207 3207
 this example corresponds to:
3208 3208
 @example
3209
-buffer=320:240:6:1:24:1:1
3209
+buffer=size=320x240:pixfmt=6:time_base=1/24:pixel_aspect=1/1
3210 3210
 @end example
3211 3211
 
3212
+Alternatively, the options can be specified as a flat string, but this
3213
+syntax is deprecated:
3214
+
3215
+@var{width}:@var{height}:@var{pix_fmt}:@var{time_base.num}:@var{time_base.den}:@var{pixel_aspect.num}:@var{pixel_aspect.den}[:@var{sws_param}]
3216
+
3212 3217
 @section cellauto
3213 3218
 
3214 3219
 Create a pattern generated by an elementary cellular automaton.
... ...
@@ -45,10 +45,10 @@ typedef struct {
45 45
     unsigned          nb_failed_requests;
46 46
 
47 47
     /* video only */
48
-    int               h, w;
48
+    int               w, h;
49 49
     enum PixelFormat  pix_fmt;
50 50
     AVRational        pixel_aspect;
51
-    char              sws_param[256];
51
+    char              *sws_param;
52 52
 
53 53
     /* audio only */
54 54
     int sample_rate;
... ...
@@ -216,35 +216,81 @@ unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
216 216
     return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
217 217
 }
218 218
 
219
+#define OFFSET(x) offsetof(BufferSourceContext, x)
220
+#define V AV_OPT_FLAG_VIDEO_PARAM
221
+static const AVOption video_options[] = {
222
+    { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL,   { 0 }, 0, INT_MAX, V },
223
+    { "video_size",     NULL, OFFSET(w),                   AV_OPT_TYPE_IMAGE_SIZE,           .flags = V },
224
+    { "pix_fmt",        NULL, OFFSET(pix_fmt),             AV_OPT_TYPE_PIXEL_FMT,            .flags = V },
225
+    { "pixel_aspect",   NULL, OFFSET(pixel_aspect),        AV_OPT_TYPE_RATIONAL,   { 0 }, 0, INT_MAX, V },
226
+    { "sws_param",      NULL, OFFSET(sws_param),           AV_OPT_TYPE_STRING,               .flags = V },
227
+    { NULL },
228
+};
229
+#undef V
230
+
231
+static const AVClass vbuffer_class = {
232
+    .class_name = "vbuffer source",
233
+    .item_name  = av_default_item_name,
234
+    .option     = video_options,
235
+    .version    = LIBAVUTIL_VERSION_INT,
236
+    .category   = AV_CLASS_CATEGORY_FILTER,
237
+};
238
+
219 239
 static av_cold int init_video(AVFilterContext *ctx, const char *args, void *opaque)
220 240
 {
221 241
     BufferSourceContext *c = ctx->priv;
222
-    char pix_fmt_str[128];
242
+    char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
223 243
     int ret, n = 0;
224
-    *c->sws_param = 0;
225 244
 
226
-    if (!args ||
227
-        (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
245
+    c->class = &vbuffer_class;
246
+
247
+    if (!args) {
248
+        av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
249
+        return AVERROR(EINVAL);
250
+    }
251
+    colon = strchr(args, ':');
252
+    equal = strchr(args, '=');
253
+    if (equal && (!colon || equal < colon)) {
254
+        av_opt_set_defaults(c);
255
+        ret = av_set_options_string(c, args, "=", ":");
256
+        if (ret < 0) {
257
+            av_log(ctx, AV_LOG_ERROR, "Error parsing options string: %s.\n", args);
258
+            goto fail;
259
+        }
260
+    } else {
261
+    if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
228 262
                     &c->time_base.num, &c->time_base.den,
229
-                    &c->pixel_aspect.num, &c->pixel_aspect.den, c->sws_param)) < 7) {
263
+                    &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
230 264
         av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
231
-        return AVERROR(EINVAL);
265
+        ret = AVERROR(EINVAL);
266
+        goto fail;
232 267
     }
233 268
 
234 269
     if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
235
-        return ret;
270
+        goto fail;
271
+    c->sws_param = av_strdup(sws_param);
272
+    if (!c->sws_param) {
273
+        ret = AVERROR(ENOMEM);
274
+        goto fail;
275
+    }
276
+    }
236 277
 
237
-    if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
238
-        return AVERROR(ENOMEM);
278
+    if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
279
+        ret = AVERROR(ENOMEM);
280
+        goto fail;
281
+    }
239 282
 
240 283
     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
241 284
            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
242 285
            c->time_base.num, c->time_base.den,
243
-           c->pixel_aspect.num, c->pixel_aspect.den, c->sws_param);
286
+           c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
244 287
     return 0;
288
+
289
+fail:
290
+    av_opt_free(c);
291
+    return ret;
245 292
 }
246 293
 
247
-#define OFFSET(x) offsetof(BufferSourceContext, x)
248 294
 #define A AV_OPT_FLAG_AUDIO_PARAM
249 295
 static const AVOption audio_options[] = {
250 296
     { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL, { 0 }, 0, INT_MAX, A },
... ...
@@ -318,6 +364,7 @@ static av_cold void uninit(AVFilterContext *ctx)
318 318
     }
319 319
     av_fifo_free(s->fifo);
320 320
     s->fifo = NULL;
321
+    av_freep(&s->sws_param);
321 322
 }
322 323
 
323 324
 static int query_formats(AVFilterContext *ctx)