Browse code

vf_pad: switch to an AVOptions-based system.

Anton Khirnov authored on 2013/02/26 05:21:29
Showing 2 changed files
... ...
@@ -1546,8 +1546,38 @@ approach is yet to be tested.
1546 1546
 Add paddings to the input image, and places the original input at the
1547 1547
 given coordinates @var{x}, @var{y}.
1548 1548
 
1549
-It accepts the following parameters:
1550
-@var{width}:@var{height}:@var{x}:@var{y}:@var{color}.
1549
+This filter accepts the following parameters:
1550
+
1551
+@table @option
1552
+@item width, height
1553
+
1554
+Specify the size of the output image with the paddings added. If the
1555
+value for @var{width} or @var{height} is 0, the corresponding input size
1556
+is used for the output.
1557
+
1558
+The @var{width} expression can reference the value set by the
1559
+@var{height} expression, and vice versa.
1560
+
1561
+The default value of @var{width} and @var{height} is 0.
1562
+
1563
+@item x, y
1564
+
1565
+Specify the offsets where to place the input image in the padded area
1566
+with respect to the top/left border of the output image.
1567
+
1568
+The @var{x} expression can reference the value set by the @var{y}
1569
+expression, and vice versa.
1570
+
1571
+The default value of @var{x} and @var{y} is 0.
1572
+
1573
+@item color
1574
+
1575
+Specify the color of the padded area, it can be the name of a color
1576
+(case insensitive match) or a 0xRRGGBB[AA] sequence.
1577
+
1578
+The default value of @var{color} is "black".
1579
+
1580
+@end table
1551 1581
 
1552 1582
 The parameters @var{width}, @var{height}, @var{x}, and @var{y} are
1553 1583
 expressions containing the following constants:
... ...
@@ -1582,46 +1612,13 @@ horizontal and vertical chroma subsample values. For example for the
1582 1582
 pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.
1583 1583
 @end table
1584 1584
 
1585
-Follows the description of the accepted parameters.
1586
-
1587
-@table @option
1588
-@item width, height
1589
-
1590
-Specify the size of the output image with the paddings added. If the
1591
-value for @var{width} or @var{height} is 0, the corresponding input size
1592
-is used for the output.
1593
-
1594
-The @var{width} expression can reference the value set by the
1595
-@var{height} expression, and vice versa.
1596
-
1597
-The default value of @var{width} and @var{height} is 0.
1598
-
1599
-@item x, y
1600
-
1601
-Specify the offsets where to place the input image in the padded area
1602
-with respect to the top/left border of the output image.
1603
-
1604
-The @var{x} expression can reference the value set by the @var{y}
1605
-expression, and vice versa.
1606
-
1607
-The default value of @var{x} and @var{y} is 0.
1608
-
1609
-@item color
1610
-
1611
-Specify the color of the padded area, it can be the name of a color
1612
-(case insensitive match) or a 0xRRGGBB[AA] sequence.
1613
-
1614
-The default value of @var{color} is "black".
1615
-
1616
-@end table
1617
-
1618 1585
 Some examples follow:
1619 1586
 
1620 1587
 @example
1621 1588
 # Add paddings with color "violet" to the input video. Output video
1622 1589
 # size is 640x480, the top-left corner of the input video is placed at
1623 1590
 # column 0, row 40.
1624
-pad=640:480:0:40:violet
1591
+pad=width=640:height=480:x=0:y=40:color=violet
1625 1592
 
1626 1593
 # pad the input to get an output with dimensions increased bt 3/2,
1627 1594
 # and put the input video at the center of the padded area
... ...
@@ -37,6 +37,8 @@
37 37
 #include "libavutil/imgutils.h"
38 38
 #include "libavutil/parseutils.h"
39 39
 #include "libavutil/mathematics.h"
40
+#include "libavutil/opt.h"
41
+
40 42
 #include "drawutils.h"
41 43
 
42 44
 static const char *const var_names[] = {
... ...
@@ -93,14 +95,16 @@ static int query_formats(AVFilterContext *ctx)
93 93
 }
94 94
 
95 95
 typedef struct {
96
+    const AVClass *class;
96 97
     int w, h;               ///< output dimensions, a value of 0 will result in the input size
97 98
     int x, y;               ///< offsets of the input area with respect to the padded area
98 99
     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
99 100
 
100
-    char w_expr[256];       ///< width  expression string
101
-    char h_expr[256];       ///< height expression string
102
-    char x_expr[256];       ///< width  expression string
103
-    char y_expr[256];       ///< height expression string
101
+    char *w_expr;           ///< width  expression string
102
+    char *h_expr;           ///< height expression string
103
+    char *x_expr;           ///< width  expression string
104
+    char *y_expr;           ///< height expression string
105
+    char *color_str;
104 106
 
105 107
     uint8_t color[4];       ///< color expressed either in YUVA or RGBA colorspace for the padding area
106 108
     uint8_t *line[4];
... ...
@@ -111,18 +115,8 @@ typedef struct {
111 111
 static av_cold int init(AVFilterContext *ctx, const char *args)
112 112
 {
113 113
     PadContext *pad = ctx->priv;
114
-    char color_string[128] = "black";
115
-
116
-    av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
117
-    av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
118
-    av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
119
-    av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
120 114
 
121
-    if (args)
122
-        sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s",
123
-               pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
124
-
125
-    if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
115
+    if (av_parse_color(pad->color, pad->color_str, -1, ctx) < 0)
126 116
         return AVERROR(EINVAL);
127 117
 
128 118
     return 0;
... ...
@@ -413,6 +407,26 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
413 413
     return ff_filter_frame(inlink->dst->outputs[0], out);
414 414
 }
415 415
 
416
+#define OFFSET(x) offsetof(PadContext, x)
417
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
418
+static const AVOption options[] = {
419
+    { "width",  "Output video width",       OFFSET(w_expr),    AV_OPT_TYPE_STRING, { .str = "iw" },    .flags = FLAGS },
420
+    { "height", "Output video height",      OFFSET(h_expr),    AV_OPT_TYPE_STRING, { .str = "ih" },    .flags = FLAGS },
421
+    { "x",      "Horizontal position of the left edge of the input video in the "
422
+        "output video",                     OFFSET(x_expr),    AV_OPT_TYPE_STRING, { .str = "0"  },    .flags = FLAGS },
423
+    { "y",      "Vertical position of the top edge of the input video in the "
424
+        "output video",                     OFFSET(y_expr),    AV_OPT_TYPE_STRING, { .str = "0"  },    .flags = FLAGS },
425
+    { "color",  "Color of the padded area", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, .flags = FLAGS },
426
+    { NULL },
427
+};
428
+
429
+static const AVClass pad_class = {
430
+    .class_name = "pad",
431
+    .item_name  = av_default_item_name,
432
+    .option     = options,
433
+    .version    = LIBAVUTIL_VERSION_INT,
434
+};
435
+
416 436
 static const AVFilterPad avfilter_vf_pad_inputs[] = {
417 437
     {
418 438
         .name             = "default",
... ...
@@ -438,6 +452,7 @@ AVFilter avfilter_vf_pad = {
438 438
     .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
439 439
 
440 440
     .priv_size     = sizeof(PadContext),
441
+    .priv_class    = &pad_class,
441 442
     .init          = init,
442 443
     .uninit        = uninit,
443 444
     .query_formats = query_formats,