vf_drawbox.c 18 KB

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