vf_drawbox.c 15 KB

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