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.

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