Browse code

avfilter/vf_pad: Add eval=frame support

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Michael Niedermayer authored on 2016/12/28 10:35:14
Showing 2 changed files
... ...
@@ -10422,6 +10422,24 @@ Specify the color of the padded area. For the syntax of this option,
10422 10422
 check the "Color" section in the ffmpeg-utils manual.
10423 10423
 
10424 10424
 The default value of @var{color} is "black".
10425
+
10426
+@item eval
10427
+Specify when to evaluate  @var{width}, @var{height}, @var{x} and @var{y} expression.
10428
+
10429
+It accepts the following values:
10430
+
10431
+@table @samp
10432
+@item init
10433
+Only evaluate expressions once during the filter initialization or when
10434
+a command is processed.
10435
+
10436
+@item frame
10437
+Evaluate expressions for each incoming frame.
10438
+
10439
+@end table
10440
+
10441
+Default value is @samp{init}.
10442
+
10425 10443
 @end table
10426 10444
 
10427 10445
 The value for the @var{width}, @var{height}, @var{x}, and @var{y}
... ...
@@ -75,11 +75,18 @@ static int query_formats(AVFilterContext *ctx)
75 75
     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
76 76
 }
77 77
 
78
+enum EvalMode {
79
+    EVAL_MODE_INIT,
80
+    EVAL_MODE_FRAME,
81
+    EVAL_MODE_NB
82
+};
83
+
78 84
 typedef struct PadContext {
79 85
     const AVClass *class;
80 86
     int w, h;               ///< output dimensions, a value of 0 will result in the input size
81 87
     int x, y;               ///< offsets of the input area with respect to the padded area
82 88
     int in_w, in_h;         ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
89
+    int inlink_w, inlink_h;
83 90
 
84 91
     char *w_expr;           ///< width  expression string
85 92
     char *h_expr;           ///< height expression string
... ...
@@ -88,6 +95,8 @@ typedef struct PadContext {
88 88
     uint8_t rgba_color[4];  ///< color for the padding area
89 89
     FFDrawContext draw;
90 90
     FFDrawColor color;
91
+
92
+    int eval_mode;          ///< expression evaluation mode
91 93
 } PadContext;
92 94
 
93 95
 static int config_input(AVFilterLink *inlink)
... ...
@@ -163,6 +172,8 @@ static int config_input(AVFilterLink *inlink)
163 163
     s->y    = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
164 164
     s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
165 165
     s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
166
+    s->inlink_w = inlink->w;
167
+    s->inlink_h = inlink->h;
166 168
 
167 169
     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
168 170
            inlink->w, inlink->h, s->w, s->h, s->x, s->y,
... ...
@@ -290,8 +301,35 @@ static int frame_needs_copy(PadContext *s, AVFrame *frame)
290 290
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
291 291
 {
292 292
     PadContext *s = inlink->dst->priv;
293
+    AVFilterLink *outlink = inlink->dst->outputs[0];
293 294
     AVFrame *out;
294
-    int needs_copy = frame_needs_copy(s, in);
295
+    int needs_copy;
296
+    if(s->eval_mode == EVAL_MODE_FRAME && (
297
+           in->width  != s->inlink_w
298
+        || in->height != s->inlink_h
299
+        || in->format != outlink->format
300
+        || in->sample_aspect_ratio.den != outlink->sample_aspect_ratio.den || in->sample_aspect_ratio.num != outlink->sample_aspect_ratio.num)) {
301
+        int ret;
302
+
303
+        inlink->dst->inputs[0]->format = in->format;
304
+        inlink->dst->inputs[0]->w      = in->width;
305
+        inlink->dst->inputs[0]->h      = in->height;
306
+
307
+        inlink->dst->inputs[0]->sample_aspect_ratio.den = in->sample_aspect_ratio.den;
308
+        inlink->dst->inputs[0]->sample_aspect_ratio.num = in->sample_aspect_ratio.num;
309
+
310
+
311
+        if ((ret = config_input(inlink)) < 0) {
312
+            s->inlink_w = -1;
313
+            return ret;
314
+        }
315
+        if ((ret = config_output(outlink)) < 0) {
316
+            s->inlink_w = -1;
317
+            return ret;
318
+        }
319
+    }
320
+
321
+    needs_copy = frame_needs_copy(s, in);
295 322
 
296 323
     if (needs_copy) {
297 324
         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
... ...
@@ -364,6 +402,9 @@ static const AVOption pad_options[] = {
364 364
     { "x",      "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
365 365
     { "y",      "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
366 366
     { "color",  "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
367
+    { "eval",   "specify when to evaluate expressions",    OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
368
+         { "init",  "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT},  .flags = FLAGS, .unit = "eval" },
369
+         { "frame", "eval expressions during initialization and per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
367 370
     { NULL }
368 371
 };
369 372