FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_drawbox.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Affine Systems, Inc (Michael Sullivan, Bobby Impollonia)
3  * Copyright (c) 2013 Andrey Utkin <andrey.krieger.utkin gmail com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Box and grid drawing filters. Also a nice template for a filter
25  * that needs to write in the input frame.
26  */
27 
28 #include "libavutil/colorspace.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/parseutils.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37 
38 enum { Y, U, V, A };
39 
40 typedef struct {
41  const AVClass *class;
42  int x, y, w_opt, h_opt, w, h;
43  int thickness;
44  char *color_str;
45  unsigned char yuv_color[4];
46  int invert_color; ///< invert luma color
47  int vsub, hsub; ///< chroma subsampling
49 
50 static av_cold int init(AVFilterContext *ctx)
51 {
52  DrawBoxContext *s = ctx->priv;
53  uint8_t rgba_color[4];
54 
55  if (!strcmp(s->color_str, "invert"))
56  s->invert_color = 1;
57  else if (av_parse_color(rgba_color, s->color_str, -1, ctx) < 0)
58  return AVERROR(EINVAL);
59 
60  if (!s->invert_color) {
61  s->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
62  s->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
63  s->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
64  s->yuv_color[A] = rgba_color[3];
65  }
66 
67  return 0;
68 }
69 
71 {
72  static const enum AVPixelFormat pix_fmts[] = {
78  };
79 
81  return 0;
82 }
83 
84 static int config_input(AVFilterLink *inlink)
85 {
86  DrawBoxContext *s = inlink->dst->priv;
87  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
88 
89  s->hsub = desc->log2_chroma_w;
90  s->vsub = desc->log2_chroma_h;
91 
92  s->w = (s->w_opt > 0) ? s->w_opt : inlink->w;
93  s->h = (s->h_opt > 0) ? s->h_opt : inlink->h;
94 
95  av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
96  s->x, s->y, s->w, s->h,
97  s->yuv_color[Y], s->yuv_color[U], s->yuv_color[V], s->yuv_color[A]);
98 
99  return 0;
100 }
101 
102 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
103 {
104  DrawBoxContext *s = inlink->dst->priv;
105  int plane, x, y, xb = s->x, yb = s->y;
106  unsigned char *row[4];
107 
108  for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) {
109  row[0] = frame->data[0] + y * frame->linesize[0];
110 
111  for (plane = 1; plane < 3; plane++)
112  row[plane] = frame->data[plane] +
113  frame->linesize[plane] * (y >> s->vsub);
114 
115  if (s->invert_color) {
116  for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++)
117  if ((y - yb < s->thickness) || (yb + s->h - 1 - y < s->thickness) ||
118  (x - xb < s->thickness) || (xb + s->w - 1 - x < s->thickness))
119  row[0][x] = 0xff - row[0][x];
120  } else {
121  for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) {
122  double alpha = (double)s->yuv_color[A] / 255;
123 
124  if ((y - yb < s->thickness) || (yb + s->h - 1 - y < s->thickness) ||
125  (x - xb < s->thickness) || (xb + s->w - 1 - x < s->thickness)) {
126  row[0][x ] = (1 - alpha) * row[0][x ] + alpha * s->yuv_color[Y];
127  row[1][x >> s->hsub] = (1 - alpha) * row[1][x >> s->hsub] + alpha * s->yuv_color[U];
128  row[2][x >> s->hsub] = (1 - alpha) * row[2][x >> s->hsub] + alpha * s->yuv_color[V];
129  }
130  }
131  }
132  }
133 
134  return ff_filter_frame(inlink->dst->outputs[0], frame);
135 }
136 
137 #define OFFSET(x) offsetof(DrawBoxContext, x)
138 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
139 
140 #if CONFIG_DRAWBOX_FILTER
141 
142 static const AVOption drawbox_options[] = {
143  { "x", "set horizontal position of the left box edge", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
144  { "y", "set vertical position of the top box edge", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
145  { "width", "set width of the box", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
146  { "w", "set width of the box", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
147  { "height", "set height of the box", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
148  { "h", "set height of the box", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
149  { "color", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
150  { "c", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
151  { "thickness", "set the box thickness", OFFSET(thickness), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, INT_MAX, FLAGS },
152  { "t", "set the box thickness", OFFSET(thickness), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, INT_MAX, FLAGS },
153  { NULL }
154 };
155 
156 AVFILTER_DEFINE_CLASS(drawbox);
157 
158 static const AVFilterPad drawbox_inputs[] = {
159  {
160  .name = "default",
161  .type = AVMEDIA_TYPE_VIDEO,
162  .config_props = config_input,
163  .get_video_buffer = ff_null_get_video_buffer,
164  .filter_frame = filter_frame,
165  .needs_writable = 1,
166  },
167  { NULL }
168 };
169 
170 static const AVFilterPad drawbox_outputs[] = {
171  {
172  .name = "default",
173  .type = AVMEDIA_TYPE_VIDEO,
174  },
175  { NULL }
176 };
177 
178 AVFilter avfilter_vf_drawbox = {
179  .name = "drawbox",
180  .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
181  .priv_size = sizeof(DrawBoxContext),
182  .priv_class = &drawbox_class,
183  .init = init,
184 
186  .inputs = drawbox_inputs,
187  .outputs = drawbox_outputs,
189 };
190 #endif /* CONFIG_DRAWBOX_FILTER */
191 
192 #if CONFIG_DRAWGRID_FILTER
193 static av_pure av_always_inline int pixel_belongs_to_grid(DrawBoxContext *drawgrid, int x, int y)
194 {
195  // x is horizontal (width) coord,
196  // y is vertical (height) coord
197  int x_modulo;
198  int y_modulo;
199 
200  // Abstract from the offset
201  x -= drawgrid->x;
202  y -= drawgrid->y;
203 
204  x_modulo = x % drawgrid->w;
205  y_modulo = y % drawgrid->h;
206 
207  // If x or y got negative, fix values to preserve logics
208  if (x_modulo < 0)
209  x_modulo += drawgrid->w;
210  if (y_modulo < 0)
211  y_modulo += drawgrid->h;
212 
213  return x_modulo < drawgrid->thickness // Belongs to vertical line
214  || y_modulo < drawgrid->thickness; // Belongs to horizontal line
215 }
216 
217 static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame)
218 {
219  DrawBoxContext *drawgrid = inlink->dst->priv;
220  int plane, x, y;
221  uint8_t *row[4];
222 
223  for (y = 0; y < frame->height; y++) {
224  row[0] = frame->data[0] + y * frame->linesize[0];
225 
226  for (plane = 1; plane < 3; plane++)
227  row[plane] = frame->data[plane] +
228  frame->linesize[plane] * (y >> drawgrid->vsub);
229 
230  if (drawgrid->invert_color) {
231  for (x = 0; x < frame->width; x++)
232  if (pixel_belongs_to_grid(drawgrid, x, y))
233  row[0][x] = 0xff - row[0][x];
234  } else {
235  for (x = 0; x < frame->width; x++) {
236  double alpha = (double)drawgrid->yuv_color[A] / 255;
237 
238  if (pixel_belongs_to_grid(drawgrid, x, y)) {
239  row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawgrid->yuv_color[Y];
240  row[1][x >> drawgrid->hsub] = (1 - alpha) * row[1][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[U];
241  row[2][x >> drawgrid->hsub] = (1 - alpha) * row[2][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[V];
242  }
243  }
244  }
245  }
246 
247  return ff_filter_frame(inlink->dst->outputs[0], frame);
248 }
249 
250 static const AVOption drawgrid_options[] = {
251  { "x", "set horizontal offset", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
252  { "y", "set vertical offset", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
253  { "width", "set width of grid cell", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
254  { "w", "set width of grid cell", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
255  { "height", "set height of grid cell", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
256  { "h", "set height of grid cell", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
257  { "color", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
258  { "c", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
259  { "thickness", "set grid line thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
260  { "t", "set grid line thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
261  { NULL }
262 };
263 
264 AVFILTER_DEFINE_CLASS(drawgrid);
265 
266 static const AVFilterPad drawgrid_inputs[] = {
267  {
268  .name = "default",
269  .type = AVMEDIA_TYPE_VIDEO,
270  .config_props = config_input,
271  .filter_frame = drawgrid_filter_frame,
272  .needs_writable = 1,
273  },
274  { NULL }
275 };
276 
277 static const AVFilterPad drawgrid_outputs[] = {
278  {
279  .name = "default",
280  .type = AVMEDIA_TYPE_VIDEO,
281  },
282  { NULL }
283 };
284 
285 AVFilter avfilter_vf_drawgrid = {
286  .name = "drawgrid",
287  .description = NULL_IF_CONFIG_SMALL("Draw a colored grid on the input video."),
288  .priv_size = sizeof(DrawBoxContext),
289  .priv_class = &drawgrid_class,
290  .init = init,
292  .inputs = drawgrid_inputs,
293  .outputs = drawgrid_outputs,
295 };
296 
297 #endif /* CONFIG_DRAWGRID_FILTER */