vf_drawbox.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. * @file
  23. * Box and grid drawing filters. Also a nice template for a filter
  24. * that needs to write in the input frame.
  25. */
  26. #include "libavutil/colorspace.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/eval.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/parseutils.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. static const char *const var_names[] = {
  37. "dar",
  38. "hsub", "vsub",
  39. "in_h", "ih", ///< height of the input video
  40. "in_w", "iw", ///< width of the input video
  41. "sar",
  42. "x",
  43. "y",
  44. "h", ///< height of the rendered box
  45. "w", ///< width of the rendered box
  46. "t",
  47. NULL
  48. };
  49. enum { Y, U, V, A };
  50. enum var_name {
  51. VAR_DAR,
  52. VAR_HSUB, VAR_VSUB,
  53. VAR_IN_H, VAR_IH,
  54. VAR_IN_W, VAR_IW,
  55. VAR_SAR,
  56. VAR_X,
  57. VAR_Y,
  58. VAR_H,
  59. VAR_W,
  60. VAR_T,
  61. VARS_NB
  62. };
  63. typedef struct {
  64. const AVClass *class;
  65. int x, y, w, h;
  66. int thickness;
  67. char *color_str;
  68. unsigned char yuv_color[4];
  69. int invert_color; ///< invert luma color
  70. int vsub, hsub; ///< chroma subsampling
  71. char *x_expr, *y_expr; ///< expression for x and y
  72. char *w_expr, *h_expr; ///< expression for width and height
  73. char *t_expr; ///< expression for thickness
  74. } DrawBoxContext;
  75. static const int NUM_EXPR_EVALS = 5;
  76. static av_cold int init(AVFilterContext *ctx)
  77. {
  78. DrawBoxContext *s = ctx->priv;
  79. uint8_t rgba_color[4];
  80. if (!strcmp(s->color_str, "invert"))
  81. s->invert_color = 1;
  82. else if (av_parse_color(rgba_color, s->color_str, -1, ctx) < 0)
  83. return AVERROR(EINVAL);
  84. if (!s->invert_color) {
  85. s->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
  86. s->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  87. s->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  88. s->yuv_color[A] = rgba_color[3];
  89. }
  90. return 0;
  91. }
  92. static int query_formats(AVFilterContext *ctx)
  93. {
  94. static const enum AVPixelFormat pix_fmts[] = {
  95. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  96. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  97. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  98. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  99. AV_PIX_FMT_NONE
  100. };
  101. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  102. return 0;
  103. }
  104. static int config_input(AVFilterLink *inlink)
  105. {
  106. AVFilterContext *ctx = inlink->dst;
  107. DrawBoxContext *s = ctx->priv;
  108. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  109. double var_values[VARS_NB], res;
  110. char *expr;
  111. int ret;
  112. int i;
  113. s->hsub = desc->log2_chroma_w;
  114. s->vsub = desc->log2_chroma_h;
  115. var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
  116. var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
  117. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
  118. var_values[VAR_DAR] = (double)inlink->w / inlink->h * var_values[VAR_SAR];
  119. var_values[VAR_HSUB] = s->hsub;
  120. var_values[VAR_VSUB] = s->vsub;
  121. var_values[VAR_X] = NAN;
  122. var_values[VAR_Y] = NAN;
  123. var_values[VAR_H] = NAN;
  124. var_values[VAR_W] = NAN;
  125. var_values[VAR_T] = NAN;
  126. for (i = 0; i <= NUM_EXPR_EVALS; i++) {
  127. /* evaluate expressions, fail on last iteration */
  128. if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
  129. var_names, var_values,
  130. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
  131. goto fail;
  132. s->x = var_values[VAR_X] = res;
  133. if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
  134. var_names, var_values,
  135. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
  136. goto fail;
  137. s->y = var_values[VAR_Y] = res;
  138. if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
  139. var_names, var_values,
  140. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
  141. goto fail;
  142. s->w = var_values[VAR_W] = res;
  143. if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
  144. var_names, var_values,
  145. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
  146. goto fail;
  147. s->h = var_values[VAR_H] = res;
  148. if ((ret = av_expr_parse_and_eval(&res, (expr = s->t_expr),
  149. var_names, var_values,
  150. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
  151. goto fail;
  152. s->thickness = var_values[VAR_T] = res;
  153. }
  154. /* if w or h are zero, use the input w/h */
  155. s->w = (s->w > 0) ? s->w : inlink->w;
  156. s->h = (s->h > 0) ? s->h : inlink->h;
  157. /* sanity check width and height */
  158. if (s->w < 0 || s->h < 0) {
  159. av_log(ctx, AV_LOG_ERROR, "Size values less than 0 are not acceptable.\n");
  160. return AVERROR(EINVAL);
  161. }
  162. av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
  163. s->x, s->y, s->w, s->h,
  164. s->yuv_color[Y], s->yuv_color[U], s->yuv_color[V], s->yuv_color[A]);
  165. return 0;
  166. fail:
  167. av_log(ctx, AV_LOG_ERROR,
  168. "Error when evaluating the expression '%s'.\n",
  169. expr);
  170. return ret;
  171. }
  172. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  173. {
  174. DrawBoxContext *s = inlink->dst->priv;
  175. int plane, x, y, xb = s->x, yb = s->y;
  176. unsigned char *row[4];
  177. for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) {
  178. row[0] = frame->data[0] + y * frame->linesize[0];
  179. for (plane = 1; plane < 3; plane++)
  180. row[plane] = frame->data[plane] +
  181. frame->linesize[plane] * (y >> s->vsub);
  182. if (s->invert_color) {
  183. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++)
  184. if ((y - yb < s->thickness) || (yb + s->h - 1 - y < s->thickness) ||
  185. (x - xb < s->thickness) || (xb + s->w - 1 - x < s->thickness))
  186. row[0][x] = 0xff - row[0][x];
  187. } else {
  188. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) {
  189. double alpha = (double)s->yuv_color[A] / 255;
  190. if ((y - yb < s->thickness) || (yb + s->h - 1 - y < s->thickness) ||
  191. (x - xb < s->thickness) || (xb + s->w - 1 - x < s->thickness)) {
  192. row[0][x ] = (1 - alpha) * row[0][x ] + alpha * s->yuv_color[Y];
  193. row[1][x >> s->hsub] = (1 - alpha) * row[1][x >> s->hsub] + alpha * s->yuv_color[U];
  194. row[2][x >> s->hsub] = (1 - alpha) * row[2][x >> s->hsub] + alpha * s->yuv_color[V];
  195. }
  196. }
  197. }
  198. }
  199. return ff_filter_frame(inlink->dst->outputs[0], frame);
  200. }
  201. #define OFFSET(x) offsetof(DrawBoxContext, x)
  202. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  203. #if CONFIG_DRAWBOX_FILTER
  204. static const AVOption drawbox_options[] = {
  205. { "x", "set horizontal position of the left box edge", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  206. { "y", "set vertical position of the top box edge", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  207. { "width", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  208. { "w", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  209. { "height", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  210. { "h", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  211. { "color", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  212. { "c", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  213. { "thickness", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, CHAR_MIN, CHAR_MAX, FLAGS },
  214. { "t", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, CHAR_MIN, CHAR_MAX, FLAGS },
  215. { NULL }
  216. };
  217. AVFILTER_DEFINE_CLASS(drawbox);
  218. static const AVFilterPad drawbox_inputs[] = {
  219. {
  220. .name = "default",
  221. .type = AVMEDIA_TYPE_VIDEO,
  222. .config_props = config_input,
  223. .filter_frame = filter_frame,
  224. .needs_writable = 1,
  225. },
  226. { NULL }
  227. };
  228. static const AVFilterPad drawbox_outputs[] = {
  229. {
  230. .name = "default",
  231. .type = AVMEDIA_TYPE_VIDEO,
  232. },
  233. { NULL }
  234. };
  235. AVFilter ff_vf_drawbox = {
  236. .name = "drawbox",
  237. .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
  238. .priv_size = sizeof(DrawBoxContext),
  239. .priv_class = &drawbox_class,
  240. .init = init,
  241. .query_formats = query_formats,
  242. .inputs = drawbox_inputs,
  243. .outputs = drawbox_outputs,
  244. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  245. };
  246. #endif /* CONFIG_DRAWBOX_FILTER */
  247. #if CONFIG_DRAWGRID_FILTER
  248. static av_pure av_always_inline int pixel_belongs_to_grid(DrawBoxContext *drawgrid, int x, int y)
  249. {
  250. // x is horizontal (width) coord,
  251. // y is vertical (height) coord
  252. int x_modulo;
  253. int y_modulo;
  254. // Abstract from the offset
  255. x -= drawgrid->x;
  256. y -= drawgrid->y;
  257. x_modulo = x % drawgrid->w;
  258. y_modulo = y % drawgrid->h;
  259. // If x or y got negative, fix values to preserve logics
  260. if (x_modulo < 0)
  261. x_modulo += drawgrid->w;
  262. if (y_modulo < 0)
  263. y_modulo += drawgrid->h;
  264. return x_modulo < drawgrid->thickness // Belongs to vertical line
  265. || y_modulo < drawgrid->thickness; // Belongs to horizontal line
  266. }
  267. static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  268. {
  269. DrawBoxContext *drawgrid = inlink->dst->priv;
  270. int plane, x, y;
  271. uint8_t *row[4];
  272. for (y = 0; y < frame->height; y++) {
  273. row[0] = frame->data[0] + y * frame->linesize[0];
  274. for (plane = 1; plane < 3; plane++)
  275. row[plane] = frame->data[plane] +
  276. frame->linesize[plane] * (y >> drawgrid->vsub);
  277. if (drawgrid->invert_color) {
  278. for (x = 0; x < frame->width; x++)
  279. if (pixel_belongs_to_grid(drawgrid, x, y))
  280. row[0][x] = 0xff - row[0][x];
  281. } else {
  282. for (x = 0; x < frame->width; x++) {
  283. double alpha = (double)drawgrid->yuv_color[A] / 255;
  284. if (pixel_belongs_to_grid(drawgrid, x, y)) {
  285. row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawgrid->yuv_color[Y];
  286. row[1][x >> drawgrid->hsub] = (1 - alpha) * row[1][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[U];
  287. row[2][x >> drawgrid->hsub] = (1 - alpha) * row[2][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[V];
  288. }
  289. }
  290. }
  291. }
  292. return ff_filter_frame(inlink->dst->outputs[0], frame);
  293. }
  294. static const AVOption drawgrid_options[] = {
  295. { "x", "set horizontal offset", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  296. { "y", "set vertical offset", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  297. { "width", "set width of grid cell", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  298. { "w", "set width of grid cell", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  299. { "height", "set height of grid cell", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  300. { "h", "set height of grid cell", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  301. { "color", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  302. { "c", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  303. { "thickness", "set grid line thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, {.str="1"}, CHAR_MIN, CHAR_MAX, FLAGS },
  304. { "t", "set grid line thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, {.str="1"}, CHAR_MIN, CHAR_MAX, FLAGS },
  305. { NULL }
  306. };
  307. AVFILTER_DEFINE_CLASS(drawgrid);
  308. static const AVFilterPad drawgrid_inputs[] = {
  309. {
  310. .name = "default",
  311. .type = AVMEDIA_TYPE_VIDEO,
  312. .config_props = config_input,
  313. .filter_frame = drawgrid_filter_frame,
  314. .needs_writable = 1,
  315. },
  316. { NULL }
  317. };
  318. static const AVFilterPad drawgrid_outputs[] = {
  319. {
  320. .name = "default",
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. },
  323. { NULL }
  324. };
  325. AVFilter ff_vf_drawgrid = {
  326. .name = "drawgrid",
  327. .description = NULL_IF_CONFIG_SMALL("Draw a colored grid on the input video."),
  328. .priv_size = sizeof(DrawBoxContext),
  329. .priv_class = &drawgrid_class,
  330. .init = init,
  331. .query_formats = query_formats,
  332. .inputs = drawgrid_inputs,
  333. .outputs = drawgrid_outputs,
  334. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  335. };
  336. #endif /* CONFIG_DRAWGRID_FILTER */