You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

498 lines
19KB

  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 av_pure av_always_inline int pixel_belongs_to_box(DrawBoxContext *s, int x, int y)
  186. {
  187. return (y - s->y < s->thickness) || (s->y + s->h - 1 - y < s->thickness) ||
  188. (x - s->x < s->thickness) || (s->x + s->w - 1 - x < s->thickness);
  189. }
  190. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  191. {
  192. DrawBoxContext *s = inlink->dst->priv;
  193. int plane, x, y, xb = s->x, yb = s->y;
  194. unsigned char *row[4];
  195. if (s->have_alpha && s->replace) {
  196. for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) {
  197. row[0] = frame->data[0] + y * frame->linesize[0];
  198. row[3] = frame->data[3] + y * frame->linesize[3];
  199. for (plane = 1; plane < 3; plane++)
  200. row[plane] = frame->data[plane] +
  201. frame->linesize[plane] * (y >> s->vsub);
  202. if (s->invert_color) {
  203. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++)
  204. if (pixel_belongs_to_box(s, x, y))
  205. row[0][x] = 0xff - row[0][x];
  206. } else {
  207. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) {
  208. if (pixel_belongs_to_box(s, x, y)) {
  209. row[0][x ] = s->yuv_color[Y];
  210. row[1][x >> s->hsub] = s->yuv_color[U];
  211. row[2][x >> s->hsub] = s->yuv_color[V];
  212. row[3][x ] = s->yuv_color[A];
  213. }
  214. }
  215. }
  216. }
  217. } else {
  218. for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) {
  219. row[0] = frame->data[0] + y * frame->linesize[0];
  220. for (plane = 1; plane < 3; plane++)
  221. row[plane] = frame->data[plane] +
  222. frame->linesize[plane] * (y >> s->vsub);
  223. if (s->invert_color) {
  224. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++)
  225. if (pixel_belongs_to_box(s, x, y))
  226. row[0][x] = 0xff - row[0][x];
  227. } else {
  228. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) {
  229. double alpha = (double)s->yuv_color[A] / 255;
  230. if (pixel_belongs_to_box(s, x, y)) {
  231. row[0][x ] = (1 - alpha) * row[0][x ] + alpha * s->yuv_color[Y];
  232. row[1][x >> s->hsub] = (1 - alpha) * row[1][x >> s->hsub] + alpha * s->yuv_color[U];
  233. row[2][x >> s->hsub] = (1 - alpha) * row[2][x >> s->hsub] + alpha * s->yuv_color[V];
  234. }
  235. }
  236. }
  237. }
  238. }
  239. return ff_filter_frame(inlink->dst->outputs[0], frame);
  240. }
  241. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
  242. {
  243. AVFilterLink *inlink = ctx->inputs[0];
  244. DrawBoxContext *s = ctx->priv;
  245. int old_x = s->x;
  246. int old_y = s->y;
  247. int old_w = s->w;
  248. int old_h = s->h;
  249. int old_t = s->thickness;
  250. int old_r = s->replace;
  251. int ret;
  252. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  253. if (ret < 0)
  254. return ret;
  255. ret = init(ctx);
  256. if (ret < 0)
  257. goto end;
  258. ret = config_input(inlink);
  259. end:
  260. if (ret < 0) {
  261. s->x = old_x;
  262. s->y = old_y;
  263. s->w = old_w;
  264. s->h = old_h;
  265. s->thickness = old_t;
  266. s->replace = old_r;
  267. }
  268. return ret;
  269. }
  270. #define OFFSET(x) offsetof(DrawBoxContext, x)
  271. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  272. #if CONFIG_DRAWBOX_FILTER
  273. static const AVOption drawbox_options[] = {
  274. { "x", "set horizontal position of the left box edge", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  275. { "y", "set vertical position of the top box edge", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  276. { "width", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  277. { "w", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  278. { "height", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  279. { "h", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  280. { "color", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  281. { "c", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  282. { "thickness", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, CHAR_MIN, CHAR_MAX, FLAGS },
  283. { "t", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, CHAR_MIN, CHAR_MAX, FLAGS },
  284. { "replace", "replace color & alpha", OFFSET(replace), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, FLAGS },
  285. { NULL }
  286. };
  287. AVFILTER_DEFINE_CLASS(drawbox);
  288. static const AVFilterPad drawbox_inputs[] = {
  289. {
  290. .name = "default",
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. .config_props = config_input,
  293. .filter_frame = filter_frame,
  294. .needs_writable = 1,
  295. },
  296. { NULL }
  297. };
  298. static const AVFilterPad drawbox_outputs[] = {
  299. {
  300. .name = "default",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. },
  303. { NULL }
  304. };
  305. AVFilter ff_vf_drawbox = {
  306. .name = "drawbox",
  307. .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
  308. .priv_size = sizeof(DrawBoxContext),
  309. .priv_class = &drawbox_class,
  310. .init = init,
  311. .query_formats = query_formats,
  312. .inputs = drawbox_inputs,
  313. .outputs = drawbox_outputs,
  314. .process_command = process_command,
  315. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  316. };
  317. #endif /* CONFIG_DRAWBOX_FILTER */
  318. #if CONFIG_DRAWGRID_FILTER
  319. static av_pure av_always_inline int pixel_belongs_to_grid(DrawBoxContext *drawgrid, int x, int y)
  320. {
  321. // x is horizontal (width) coord,
  322. // y is vertical (height) coord
  323. int x_modulo;
  324. int y_modulo;
  325. // Abstract from the offset
  326. x -= drawgrid->x;
  327. y -= drawgrid->y;
  328. x_modulo = x % drawgrid->w;
  329. y_modulo = y % drawgrid->h;
  330. // If x or y got negative, fix values to preserve logics
  331. if (x_modulo < 0)
  332. x_modulo += drawgrid->w;
  333. if (y_modulo < 0)
  334. y_modulo += drawgrid->h;
  335. return x_modulo < drawgrid->thickness // Belongs to vertical line
  336. || y_modulo < drawgrid->thickness; // Belongs to horizontal line
  337. }
  338. static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  339. {
  340. DrawBoxContext *drawgrid = inlink->dst->priv;
  341. int plane, x, y;
  342. uint8_t *row[4];
  343. if (drawgrid->have_alpha && drawgrid->replace) {
  344. for (y = 0; y < frame->height; y++) {
  345. row[0] = frame->data[0] + y * frame->linesize[0];
  346. row[3] = frame->data[3] + y * frame->linesize[3];
  347. for (plane = 1; plane < 3; plane++)
  348. row[plane] = frame->data[plane] +
  349. frame->linesize[plane] * (y >> drawgrid->vsub);
  350. if (drawgrid->invert_color) {
  351. for (x = 0; x < frame->width; x++)
  352. if (pixel_belongs_to_grid(drawgrid, x, y))
  353. row[0][x] = 0xff - row[0][x];
  354. } else {
  355. for (x = 0; x < frame->width; x++) {
  356. if (pixel_belongs_to_grid(drawgrid, x, y)) {
  357. row[0][x ] = drawgrid->yuv_color[Y];
  358. row[1][x >> drawgrid->hsub] = drawgrid->yuv_color[U];
  359. row[2][x >> drawgrid->hsub] = drawgrid->yuv_color[V];
  360. row[3][x ] = drawgrid->yuv_color[A];
  361. }
  362. }
  363. }
  364. }
  365. } else {
  366. for (y = 0; y < frame->height; y++) {
  367. row[0] = frame->data[0] + y * frame->linesize[0];
  368. for (plane = 1; plane < 3; plane++)
  369. row[plane] = frame->data[plane] +
  370. frame->linesize[plane] * (y >> drawgrid->vsub);
  371. if (drawgrid->invert_color) {
  372. for (x = 0; x < frame->width; x++)
  373. if (pixel_belongs_to_grid(drawgrid, x, y))
  374. row[0][x] = 0xff - row[0][x];
  375. } else {
  376. for (x = 0; x < frame->width; x++) {
  377. double alpha = (double)drawgrid->yuv_color[A] / 255;
  378. if (pixel_belongs_to_grid(drawgrid, x, y)) {
  379. row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawgrid->yuv_color[Y];
  380. row[1][x >> drawgrid->hsub] = (1 - alpha) * row[1][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[U];
  381. row[2][x >> drawgrid->hsub] = (1 - alpha) * row[2][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[V];
  382. }
  383. }
  384. }
  385. }
  386. }
  387. return ff_filter_frame(inlink->dst->outputs[0], frame);
  388. }
  389. static const AVOption drawgrid_options[] = {
  390. { "x", "set horizontal offset", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  391. { "y", "set vertical offset", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  392. { "width", "set width of grid cell", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  393. { "w", "set width of grid cell", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  394. { "height", "set height of grid cell", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  395. { "h", "set height of grid cell", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, CHAR_MIN, CHAR_MAX, FLAGS },
  396. { "color", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  397. { "c", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  398. { "thickness", "set grid line thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, {.str="1"}, CHAR_MIN, CHAR_MAX, FLAGS },
  399. { "t", "set grid line thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, {.str="1"}, CHAR_MIN, CHAR_MAX, FLAGS },
  400. { "replace", "replace color & alpha", OFFSET(replace), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, FLAGS },
  401. { NULL }
  402. };
  403. AVFILTER_DEFINE_CLASS(drawgrid);
  404. static const AVFilterPad drawgrid_inputs[] = {
  405. {
  406. .name = "default",
  407. .type = AVMEDIA_TYPE_VIDEO,
  408. .config_props = config_input,
  409. .filter_frame = drawgrid_filter_frame,
  410. .needs_writable = 1,
  411. },
  412. { NULL }
  413. };
  414. static const AVFilterPad drawgrid_outputs[] = {
  415. {
  416. .name = "default",
  417. .type = AVMEDIA_TYPE_VIDEO,
  418. },
  419. { NULL }
  420. };
  421. AVFilter ff_vf_drawgrid = {
  422. .name = "drawgrid",
  423. .description = NULL_IF_CONFIG_SMALL("Draw a colored grid on the input video."),
  424. .priv_size = sizeof(DrawBoxContext),
  425. .priv_class = &drawgrid_class,
  426. .init = init,
  427. .query_formats = query_formats,
  428. .inputs = drawgrid_inputs,
  429. .outputs = drawgrid_outputs,
  430. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  431. .process_command = process_command,
  432. };
  433. #endif /* CONFIG_DRAWGRID_FILTER */