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.

401 lines
15KB

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