Browse code

lavfi/pad: add support to named options

Stefano Sabatini authored on 2013/02/02 08:51:14
Showing 3 changed files
... ...
@@ -3725,14 +3725,50 @@ testsrc=s=100x100, split=4 [in0][in1][in2][in3];
3725 3725
 
3726 3726
 @section pad
3727 3727
 
3728
-Add paddings to the input image, and places the original input at the
3728
+Add paddings to the input image, and place the original input at the
3729 3729
 given coordinates @var{x}, @var{y}.
3730 3730
 
3731
-It accepts the following parameters:
3731
+The filter accepts parameters as a list of @var{key}=@var{value} pairs,
3732
+separated by ":".
3733
+
3734
+If the key of the first options is omitted, the arguments are
3735
+interpreted according to the syntax
3732 3736
 @var{width}:@var{height}:@var{x}:@var{y}:@var{color}.
3733 3737
 
3734
-The parameters @var{width}, @var{height}, @var{x}, and @var{y} are
3735
-expressions containing the following constants:
3738
+A description of the accepted options follows.
3739
+
3740
+@table @option
3741
+@item width, w
3742
+@item height, h
3743
+Specify an expression for the size of the output image with the
3744
+paddings added. If the value for @var{width} or @var{height} is 0, the
3745
+corresponding input size is used for the output.
3746
+
3747
+The @var{width} expression can reference the value set by the
3748
+@var{height} expression, and vice versa.
3749
+
3750
+The default value of @var{width} and @var{height} is 0.
3751
+
3752
+@item x
3753
+@item y
3754
+Specify an expression for the offsets where to place the input image
3755
+in the padded area with respect to the top/left border of the output
3756
+image.
3757
+
3758
+The @var{x} expression can reference the value set by the @var{y}
3759
+expression, and vice versa.
3760
+
3761
+The default value of @var{x} and @var{y} is 0.
3762
+
3763
+@item color
3764
+Specify the color of the padded area, it can be the name of a color
3765
+(case insensitive match) or a 0xRRGGBB[AA] sequence.
3766
+
3767
+The default value of @var{color} is "black".
3768
+@end table
3769
+
3770
+The value for the @var{width}, @var{height}, @var{x}, and @var{y}
3771
+options are expressions containing the following constants:
3736 3772
 
3737 3773
 @table @option
3738 3774
 @item in_w, in_h
... ...
@@ -3766,39 +3802,6 @@ horizontal and vertical chroma subsample values. For example for the
3766 3766
 pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.
3767 3767
 @end table
3768 3768
 
3769
-Follows the description of the accepted parameters.
3770
-
3771
-@table @option
3772
-@item width, height
3773
-
3774
-Specify the size of the output image with the paddings added. If the
3775
-value for @var{width} or @var{height} is 0, the corresponding input size
3776
-is used for the output.
3777
-
3778
-The @var{width} expression can reference the value set by the
3779
-@var{height} expression, and vice versa.
3780
-
3781
-The default value of @var{width} and @var{height} is 0.
3782
-
3783
-@item x, y
3784
-
3785
-Specify the offsets where to place the input image in the padded area
3786
-with respect to the top/left border of the output image.
3787
-
3788
-The @var{x} expression can reference the value set by the @var{y}
3789
-expression, and vice versa.
3790
-
3791
-The default value of @var{x} and @var{y} is 0.
3792
-
3793
-@item color
3794
-
3795
-Specify the color of the padded area, it can be the name of a color
3796
-(case insensitive match) or a 0xRRGGBB[AA] sequence.
3797
-
3798
-The default value of @var{color} is "black".
3799
-
3800
-@end table
3801
-
3802 3769
 @subsection Examples
3803 3770
 
3804 3771
 @itemize
... ...
@@ -3810,6 +3813,11 @@ column 0, row 40:
3810 3810
 pad=640:480:0:40:violet
3811 3811
 @end example
3812 3812
 
3813
+The example above is equivalent to the following command:
3814
+@example
3815
+pad=width=640:height=480:x=0:y=40:color=violet
3816
+@end example
3817
+
3813 3818
 @item
3814 3819
 Pad the input to get an output with dimensions increased by 3/2,
3815 3820
 and put the input video at the center of the padded area:
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32 32
 #define LIBAVFILTER_VERSION_MINOR  35
33
-#define LIBAVFILTER_VERSION_MICRO 100
33
+#define LIBAVFILTER_VERSION_MICRO 101
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/colorspace.h"
36 36
 #include "libavutil/avassert.h"
37 37
 #include "libavutil/imgutils.h"
38
+#include "libavutil/opt.h"
38 39
 #include "libavutil/parseutils.h"
39 40
 #include "libavutil/mathematics.h"
40 41
 #include "drawutils.h"
... ...
@@ -76,40 +77,61 @@ static int query_formats(AVFilterContext *ctx)
76 76
 }
77 77
 
78 78
 typedef struct {
79
+    const AVClass *class;
79 80
     int w, h;               ///< output dimensions, a value of 0 will result in the input size
80 81
     int x, y;               ///< offsets of the input area with respect to the padded area
81 82
     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
82 83
 
83
-    char w_expr[256];       ///< width  expression string
84
-    char h_expr[256];       ///< height expression string
85
-    char x_expr[256];       ///< width  expression string
86
-    char y_expr[256];       ///< height expression string
87
-
84
+    char *w_expr;       ///< width  expression string
85
+    char *h_expr;       ///< height expression string
86
+    char *x_expr;       ///< width  expression string
87
+    char *y_expr;       ///< height expression string
88
+    char *color_str;
88 89
     uint8_t rgba_color[4];  ///< color for the padding area
89 90
     FFDrawContext draw;
90 91
     FFDrawColor color;
91 92
 } PadContext;
92 93
 
94
+#define OFFSET(x) offsetof(PadContext, x)
95
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
96
+
97
+static const AVOption pad_options[] = {
98
+    { "width",  "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
99
+    { "w",      "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
100
+    { "height", "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
101
+    { "h",      "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
102
+    { "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 },
103
+    { "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 },
104
+    { "color",  "set the color of the padded area border", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, .flags = FLAGS },
105
+    {NULL}
106
+};
107
+
108
+AVFILTER_DEFINE_CLASS(pad);
109
+
93 110
 static av_cold int init(AVFilterContext *ctx, const char *args)
94 111
 {
95 112
     PadContext *pad = ctx->priv;
96
-    char color_string[128] = "black";
113
+    static const char *shorthand[] = { "width", "height", "x", "y", "color", NULL };
114
+    int ret;
97 115
 
98
-    av_strlcpy(pad->w_expr, "iw", sizeof(pad->w_expr));
99
-    av_strlcpy(pad->h_expr, "ih", sizeof(pad->h_expr));
100
-    av_strlcpy(pad->x_expr, "0" , sizeof(pad->w_expr));
101
-    av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr));
116
+    pad->class = &pad_class;
117
+    av_opt_set_defaults(pad);
102 118
 
103
-    if (args)
104
-        sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s",
105
-               pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string);
119
+    if ((ret = av_opt_set_from_string(pad, args, shorthand, "=", ":")) < 0)
120
+        return ret;
106 121
 
107
-    if (av_parse_color(pad->rgba_color, color_string, -1, ctx) < 0)
122
+    if (av_parse_color(pad->rgba_color, pad->color_str, -1, ctx) < 0)
108 123
         return AVERROR(EINVAL);
109 124
 
110 125
     return 0;
111 126
 }
112 127
 
128
+static av_cold void uninit(AVFilterContext *ctx)
129
+{
130
+    PadContext *pad = ctx->priv;
131
+    av_opt_free(pad);
132
+}
133
+
113 134
 static int config_input(AVFilterLink *inlink)
114 135
 {
115 136
     AVFilterContext *ctx = inlink->dst;
... ...
@@ -368,9 +390,11 @@ AVFilter avfilter_vf_pad = {
368 368
 
369 369
     .priv_size     = sizeof(PadContext),
370 370
     .init          = init,
371
+    .uninit        = uninit,
371 372
     .query_formats = query_formats,
372 373
 
373 374
     .inputs    = avfilter_vf_pad_inputs,
374 375
 
375 376
     .outputs   = avfilter_vf_pad_outputs,
377
+    .priv_class = &pad_class,
376 378
 };