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.

298 lines
11KB

  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/pixdesc.h"
  30. #include "libavutil/parseutils.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. enum { Y, U, V, A };
  36. typedef struct {
  37. const AVClass *class;
  38. int x, y, w_opt, h_opt, w, h;
  39. int thickness;
  40. char *color_str;
  41. unsigned char yuv_color[4];
  42. int invert_color; ///< invert luma color
  43. int vsub, hsub; ///< chroma subsampling
  44. } DrawBoxContext;
  45. static av_cold int init(AVFilterContext *ctx)
  46. {
  47. DrawBoxContext *s = ctx->priv;
  48. uint8_t rgba_color[4];
  49. if (!strcmp(s->color_str, "invert"))
  50. s->invert_color = 1;
  51. else if (av_parse_color(rgba_color, s->color_str, -1, ctx) < 0)
  52. return AVERROR(EINVAL);
  53. if (!s->invert_color) {
  54. s->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
  55. s->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  56. s->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  57. s->yuv_color[A] = rgba_color[3];
  58. }
  59. return 0;
  60. }
  61. static int query_formats(AVFilterContext *ctx)
  62. {
  63. static const enum AVPixelFormat pix_fmts[] = {
  64. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  65. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  66. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  67. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  68. AV_PIX_FMT_NONE
  69. };
  70. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  71. return 0;
  72. }
  73. static int config_input(AVFilterLink *inlink)
  74. {
  75. DrawBoxContext *s = inlink->dst->priv;
  76. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  77. s->hsub = desc->log2_chroma_w;
  78. s->vsub = desc->log2_chroma_h;
  79. s->w = (s->w_opt > 0) ? s->w_opt : inlink->w;
  80. s->h = (s->h_opt > 0) ? s->h_opt : inlink->h;
  81. av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
  82. s->x, s->y, s->w, s->h,
  83. s->yuv_color[Y], s->yuv_color[U], s->yuv_color[V], s->yuv_color[A]);
  84. return 0;
  85. }
  86. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  87. {
  88. DrawBoxContext *s = inlink->dst->priv;
  89. int plane, x, y, xb = s->x, yb = s->y;
  90. unsigned char *row[4];
  91. for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) {
  92. row[0] = frame->data[0] + y * frame->linesize[0];
  93. for (plane = 1; plane < 3; plane++)
  94. row[plane] = frame->data[plane] +
  95. frame->linesize[plane] * (y >> s->vsub);
  96. if (s->invert_color) {
  97. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++)
  98. if ((y - yb < s->thickness-1) || (yb + s->h - y < s->thickness) ||
  99. (x - xb < s->thickness-1) || (xb + s->w - x < s->thickness))
  100. row[0][x] = 0xff - row[0][x];
  101. } else {
  102. for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) {
  103. double alpha = (double)s->yuv_color[A] / 255;
  104. if ((y - yb < s->thickness-1) || (yb + s->h - y < s->thickness) ||
  105. (x - xb < s->thickness-1) || (xb + s->w - x < s->thickness)) {
  106. row[0][x ] = (1 - alpha) * row[0][x ] + alpha * s->yuv_color[Y];
  107. row[1][x >> s->hsub] = (1 - alpha) * row[1][x >> s->hsub] + alpha * s->yuv_color[U];
  108. row[2][x >> s->hsub] = (1 - alpha) * row[2][x >> s->hsub] + alpha * s->yuv_color[V];
  109. }
  110. }
  111. }
  112. }
  113. return ff_filter_frame(inlink->dst->outputs[0], frame);
  114. }
  115. #define OFFSET(x) offsetof(DrawBoxContext, x)
  116. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  117. #if CONFIG_DRAWBOX_FILTER
  118. static const AVOption drawbox_options[] = {
  119. { "x", "set horizontal position of the left box edge", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
  120. { "y", "set vertical position of the top box edge", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
  121. { "width", "set width of the box", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  122. { "w", "set width of the box", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  123. { "height", "set height of the box", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  124. { "h", "set height of the box", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  125. { "color", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  126. { "c", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  127. { "thickness", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
  128. { "t", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
  129. { NULL }
  130. };
  131. AVFILTER_DEFINE_CLASS(drawbox);
  132. static const AVFilterPad drawbox_inputs[] = {
  133. {
  134. .name = "default",
  135. .type = AVMEDIA_TYPE_VIDEO,
  136. .config_props = config_input,
  137. .get_video_buffer = ff_null_get_video_buffer,
  138. .filter_frame = filter_frame,
  139. .needs_writable = 1,
  140. },
  141. { NULL }
  142. };
  143. static const AVFilterPad drawbox_outputs[] = {
  144. {
  145. .name = "default",
  146. .type = AVMEDIA_TYPE_VIDEO,
  147. },
  148. { NULL }
  149. };
  150. AVFilter avfilter_vf_drawbox = {
  151. .name = "drawbox",
  152. .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
  153. .priv_size = sizeof(DrawBoxContext),
  154. .priv_class = &drawbox_class,
  155. .init = init,
  156. .query_formats = query_formats,
  157. .inputs = drawbox_inputs,
  158. .outputs = drawbox_outputs,
  159. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  160. };
  161. #endif /* CONFIG_DRAWBOX_FILTER */
  162. #if CONFIG_DRAWGRID_FILTER
  163. static av_pure av_always_inline int pixel_belongs_to_grid(DrawBoxContext *drawgrid, int x, int y)
  164. {
  165. // x is horizontal (width) coord,
  166. // y is vertical (height) coord
  167. int x_modulo;
  168. int y_modulo;
  169. // Abstract from the offset
  170. x -= drawgrid->x;
  171. y -= drawgrid->y;
  172. x_modulo = x % drawgrid->w;
  173. y_modulo = y % drawgrid->h;
  174. // If x or y got negative, fix values to preserve logics
  175. if (x_modulo < 0)
  176. x_modulo += drawgrid->w;
  177. if (y_modulo < 0)
  178. y_modulo += drawgrid->h;
  179. return x_modulo < drawgrid->thickness // Belongs to vertical line
  180. || y_modulo < drawgrid->thickness; // Belongs to horizontal line
  181. }
  182. static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  183. {
  184. DrawBoxContext *drawgrid = inlink->dst->priv;
  185. int plane, x, y;
  186. uint8_t *row[4];
  187. for (y = 0; y < frame->height; y++) {
  188. row[0] = frame->data[0] + y * frame->linesize[0];
  189. for (plane = 1; plane < 3; plane++)
  190. row[plane] = frame->data[plane] +
  191. frame->linesize[plane] * (y >> drawgrid->vsub);
  192. if (drawgrid->invert_color) {
  193. for (x = 0; x < frame->width; x++)
  194. if (pixel_belongs_to_grid(drawgrid, x, y))
  195. row[0][x] = 0xff - row[0][x];
  196. } else {
  197. for (x = 0; x < frame->width; x++) {
  198. double alpha = (double)drawgrid->yuv_color[A] / 255;
  199. if (pixel_belongs_to_grid(drawgrid, x, y)) {
  200. row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawgrid->yuv_color[Y];
  201. row[1][x >> drawgrid->hsub] = (1 - alpha) * row[1][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[U];
  202. row[2][x >> drawgrid->hsub] = (1 - alpha) * row[2][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[V];
  203. }
  204. }
  205. }
  206. }
  207. return ff_filter_frame(inlink->dst->outputs[0], frame);
  208. }
  209. static const AVOption drawgrid_options[] = {
  210. { "x", "set horizontal offset", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
  211. { "y", "set vertical offset", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
  212. { "width", "set width of grid cell", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  213. { "w", "set width of grid cell", OFFSET(w_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  214. { "height", "set height of grid cell", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  215. { "h", "set height of grid cell", OFFSET(h_opt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  216. { "color", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  217. { "c", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  218. { "thickness", "set grid line thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
  219. { "t", "set grid line thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
  220. { NULL }
  221. };
  222. AVFILTER_DEFINE_CLASS(drawgrid);
  223. static const AVFilterPad drawgrid_inputs[] = {
  224. {
  225. .name = "default",
  226. .type = AVMEDIA_TYPE_VIDEO,
  227. .config_props = config_input,
  228. .filter_frame = drawgrid_filter_frame,
  229. .needs_writable = 1,
  230. },
  231. { NULL }
  232. };
  233. static const AVFilterPad drawgrid_outputs[] = {
  234. {
  235. .name = "default",
  236. .type = AVMEDIA_TYPE_VIDEO,
  237. },
  238. { NULL }
  239. };
  240. AVFilter avfilter_vf_drawgrid = {
  241. .name = "drawgrid",
  242. .description = NULL_IF_CONFIG_SMALL("Draw a colored grid on the input video."),
  243. .priv_size = sizeof(DrawBoxContext),
  244. .priv_class = &drawgrid_class,
  245. .init = init,
  246. .query_formats = query_formats,
  247. .inputs = drawgrid_inputs,
  248. .outputs = drawgrid_outputs,
  249. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  250. };
  251. #endif /* CONFIG_DRAWGRID_FILTER */