vf_drawbox.c 18 KB

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