Browse code

lavfi/drawbox: implement color=invert mode

Based on a libmpcodecs/vf_rectangle.c feature.

Stefano Sabatini authored on 2012/10/31 04:14:07
Showing 3 changed files
... ...
@@ -1741,7 +1741,9 @@ the input width and height. Default to 0.
1741 1741
 
1742 1742
 @item color, c
1743 1743
 Specify the color of the box to write, it can be the name of a color
1744
-(case insensitive match) or a 0xRRGGBB[AA] sequence.
1744
+(case insensitive match) or a 0xRRGGBB[AA] sequence. If the special
1745
+value @code{invert} is used, the box edge color is the same as the
1746
+video with inverted luma.
1745 1747
 @end table
1746 1748
 
1747 1749
 If the key of the first options is omitted, the arguments are
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32 32
 #define LIBAVFILTER_VERSION_MINOR  21
33
-#define LIBAVFILTER_VERSION_MICRO 101
33
+#define LIBAVFILTER_VERSION_MICRO 102
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
... ...
@@ -41,6 +41,7 @@ typedef struct {
41 41
     int x, y, w, h;
42 42
     char *color_str;
43 43
     unsigned char yuv_color[4];
44
+    int invert_color; ///< invert luma color
44 45
     int vsub, hsub;   ///< chroma subsampling
45 46
 } DrawBoxContext;
46 47
 
... ...
@@ -72,7 +73,9 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
72 72
     if ((ret = av_opt_set_from_string(drawbox, args, shorthand, "=", ":")) < 0)
73 73
         return ret;
74 74
 
75
-    if (av_parse_color(rgba_color, drawbox->color_str, -1, ctx) < 0)
75
+    if (!strcmp(drawbox->color_str, "invert"))
76
+        drawbox->invert_color = 1;
77
+    else if (av_parse_color(rgba_color, drawbox->color_str, -1, ctx) < 0)
76 78
         return AVERROR(EINVAL);
77 79
 
78 80
     drawbox->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
... ...
@@ -135,6 +138,12 @@ static int draw_slice(AVFilterLink *inlink, int y0, int h, int slice_dir)
135 135
             row[plane] = picref->data[plane] +
136 136
                 picref->linesize[plane] * (y >> drawbox->vsub);
137 137
 
138
+        if (drawbox->invert_color) {
139
+            for (x = FFMAX(xb, 0); x < (xb + drawbox->w) && x < picref->video->w; x++)
140
+                if ((y - yb < 3) || (yb + drawbox->h - y < 4) ||
141
+                    (x - xb < 3) || (xb + drawbox->w - x < 4))
142
+                    row[0][x] = 0xff - row[0][x];
143
+        } else {
138 144
         for (x = FFMAX(xb, 0); x < (xb + drawbox->w) && x < picref->video->w; x++) {
139 145
             double alpha = (double)drawbox->yuv_color[A] / 255;
140 146
 
... ...
@@ -145,6 +154,7 @@ static int draw_slice(AVFilterLink *inlink, int y0, int h, int slice_dir)
145 145
                 row[2][x >> drawbox->hsub] = (1 - alpha) * row[2][x >> drawbox->hsub] + alpha * drawbox->yuv_color[V];
146 146
             }
147 147
         }
148
+        }
148 149
     }
149 150
 
150 151
     return ff_draw_slice(inlink->dst->outputs[0], y0, h, 1);