Browse code

vf_drawbox: switch to an AVOptions-based system.

Anton Khirnov authored on 2013/02/26 05:21:29
Showing 2 changed files
... ...
@@ -811,10 +811,7 @@ delogo=x=0:y=0:w=100:h=77:band=10
811 811
 
812 812
 Draw a colored box on the input image.
813 813
 
814
-It accepts the syntax:
815
-@example
816
-drawbox=@var{x}:@var{y}:@var{width}:@var{height}:@var{color}
817
-@end example
814
+This filter accepts the following options:
818 815
 
819 816
 @table @option
820 817
 
... ...
@@ -836,7 +833,7 @@ Follow some examples:
836 836
 drawbox
837 837
 
838 838
 # draw a box with color red and an opacity of 50%
839
-drawbox=10:20:200:60:red@@0.5"
839
+drawbox=x=10:y=20:width=200:height=60:color=red@@0.5"
840 840
 @end example
841 841
 
842 842
 @section drawtext
... ...
@@ -26,6 +26,7 @@
26 26
 
27 27
 #include "libavutil/colorspace.h"
28 28
 #include "libavutil/common.h"
29
+#include "libavutil/opt.h"
29 30
 #include "libavutil/pixdesc.h"
30 31
 #include "libavutil/parseutils.h"
31 32
 #include "avfilter.h"
... ...
@@ -36,7 +37,9 @@
36 36
 enum { Y, U, V, A };
37 37
 
38 38
 typedef struct {
39
+    const AVClass *class;
39 40
     int x, y, w, h;
41
+    char *color_str;
40 42
     unsigned char yuv_color[4];
41 43
     int vsub, hsub;   ///< chroma subsampling
42 44
 } DrawBoxContext;
... ...
@@ -44,16 +47,9 @@ typedef struct {
44 44
 static av_cold int init(AVFilterContext *ctx, const char *args)
45 45
 {
46 46
     DrawBoxContext *drawbox= ctx->priv;
47
-    char color_str[1024] = "black";
48 47
     uint8_t rgba_color[4];
49 48
 
50
-    drawbox->x = drawbox->y = drawbox->w = drawbox->h = 0;
51
-
52
-    if (args)
53
-        sscanf(args, "%d:%d:%d:%d:%s",
54
-               &drawbox->x, &drawbox->y, &drawbox->w, &drawbox->h, color_str);
55
-
56
-    if (av_parse_color(rgba_color, color_str, -1, ctx) < 0)
49
+    if (av_parse_color(rgba_color, drawbox->color_str, -1, ctx) < 0)
57 50
         return AVERROR(EINVAL);
58 51
 
59 52
     drawbox->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
... ...
@@ -124,6 +120,24 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
124 124
     return ff_filter_frame(inlink->dst->outputs[0], frame);
125 125
 }
126 126
 
127
+#define OFFSET(x) offsetof(DrawBoxContext, x)
128
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
129
+static const AVOption options[] = {
130
+    { "x",      "Horizontal position of the left box edge", OFFSET(x),         AV_OPT_TYPE_INT,    { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
131
+    { "y",      "Vertical position of the top box edge",    OFFSET(y),         AV_OPT_TYPE_INT,    { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
132
+    { "width",  "Width of the box",                         OFFSET(w),         AV_OPT_TYPE_INT,    { .i64 = 0 }, 0,       INT_MAX, FLAGS },
133
+    { "height", "Height of the box",                        OFFSET(h),         AV_OPT_TYPE_INT,    { .i64 = 0 }, 0,       INT_MAX, FLAGS },
134
+    { "color",  "Color of the box",                         OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" },    .flags = FLAGS },
135
+    { NULL },
136
+};
137
+
138
+static const AVClass drawbox_class = {
139
+    .class_name = "drawbox",
140
+    .item_name  = av_default_item_name,
141
+    .option     = options,
142
+    .version    = LIBAVUTIL_VERSION_INT,
143
+};
144
+
127 145
 static const AVFilterPad avfilter_vf_drawbox_inputs[] = {
128 146
     {
129 147
         .name             = "default",
... ...
@@ -148,6 +162,7 @@ AVFilter avfilter_vf_drawbox = {
148 148
     .name      = "drawbox",
149 149
     .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
150 150
     .priv_size = sizeof(DrawBoxContext),
151
+    .priv_class = &drawbox_class,
151 152
     .init      = init,
152 153
 
153 154
     .query_formats   = query_formats,