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.

207 lines
7.6KB

  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. * Grid drawing filter.
  24. * Code derived from drawbox filter.
  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. } DrawGridContext;
  44. static av_cold int init(AVFilterContext *ctx)
  45. {
  46. DrawGridContext *drawgrid = ctx->priv;
  47. uint8_t rgba_color[4];
  48. if (!strcmp(drawgrid->color_str, "invert"))
  49. drawgrid->invert_color = 1;
  50. else if (av_parse_color(rgba_color, drawgrid->color_str, -1, ctx) < 0)
  51. return AVERROR(EINVAL);
  52. if (!drawgrid->invert_color) {
  53. drawgrid->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
  54. drawgrid->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  55. drawgrid->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  56. drawgrid->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. DrawGridContext *drawgrid = inlink->dst->priv;
  75. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  76. drawgrid->hsub = desc->log2_chroma_w;
  77. drawgrid->vsub = desc->log2_chroma_h;
  78. if (drawgrid->w == 0) drawgrid->w = inlink->w - drawgrid->thickness;
  79. if (drawgrid->h == 0) drawgrid->h = inlink->h - drawgrid->thickness;
  80. av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
  81. drawgrid->x, drawgrid->y, drawgrid->w, drawgrid->h,
  82. drawgrid->yuv_color[Y], drawgrid->yuv_color[U], drawgrid->yuv_color[V], drawgrid->yuv_color[A]);
  83. return 0;
  84. }
  85. static av_pure av_always_inline int pixel_belongs_to_grid(DrawGridContext *drawgrid, int x, int y)
  86. {
  87. // x is horizontal (width) coord,
  88. // y is vertical (height) coord
  89. int x_modulo;
  90. int y_modulo;
  91. // Abstract from the offset
  92. x -= drawgrid->x;
  93. y -= drawgrid->y;
  94. x_modulo = x % drawgrid->w;
  95. y_modulo = y % drawgrid->h;
  96. // If x or y got negative, fix values to preserve logics
  97. if (x_modulo < 0)
  98. x_modulo += drawgrid->w;
  99. if (y_modulo < 0)
  100. y_modulo += drawgrid->h;
  101. return x_modulo < drawgrid->thickness // Belongs to vertical line
  102. || y_modulo < drawgrid->thickness; // Belongs to horizontal line
  103. }
  104. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  105. {
  106. DrawGridContext *drawgrid = inlink->dst->priv;
  107. int plane, x, y;
  108. uint8_t *row[4];
  109. for (y = 0; y < frame->height; y++) {
  110. row[0] = frame->data[0] + y * frame->linesize[0];
  111. for (plane = 1; plane < 3; plane++)
  112. row[plane] = frame->data[plane] +
  113. frame->linesize[plane] * (y >> drawgrid->vsub);
  114. if (drawgrid->invert_color) {
  115. for (x = 0; x < frame->width; x++)
  116. if (pixel_belongs_to_grid(drawgrid, x, y))
  117. row[0][x] = 0xff - row[0][x];
  118. } else {
  119. for (x = 0; x < frame->width; x++) {
  120. double alpha = (double)drawgrid->yuv_color[A] / 255;
  121. if (pixel_belongs_to_grid(drawgrid, x, y)) {
  122. row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawgrid->yuv_color[Y];
  123. row[1][x >> drawgrid->hsub] = (1 - alpha) * row[1][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[U];
  124. row[2][x >> drawgrid->hsub] = (1 - alpha) * row[2][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[V];
  125. }
  126. }
  127. }
  128. }
  129. return ff_filter_frame(inlink->dst->outputs[0], frame);
  130. }
  131. #define OFFSET(x) offsetof(DrawGridContext, x)
  132. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  133. static const AVOption drawgrid_options[] = {
  134. { "x", "set horizontal offset", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
  135. { "y", "set vertical offset", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
  136. { "width", "set width of grid cell", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  137. { "w", "set width of grid cell", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  138. { "height", "set height of grid cell", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  139. { "h", "set height of grid cell", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  140. { "color", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  141. { "c", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, FLAGS },
  142. { "thickness", "set grid line thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
  143. { "t", "set grid line thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS },
  144. { NULL }
  145. };
  146. AVFILTER_DEFINE_CLASS(drawgrid);
  147. static const AVFilterPad avfilter_vf_drawgrid_inputs[] = {
  148. {
  149. .name = "default",
  150. .type = AVMEDIA_TYPE_VIDEO,
  151. .config_props = config_input,
  152. .filter_frame = filter_frame,
  153. .needs_writable = 1,
  154. },
  155. { NULL }
  156. };
  157. static const AVFilterPad avfilter_vf_drawgrid_outputs[] = {
  158. {
  159. .name = "default",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. },
  162. { NULL }
  163. };
  164. AVFilter avfilter_vf_drawgrid = {
  165. .name = "drawgrid",
  166. .description = NULL_IF_CONFIG_SMALL("Draw a colored grid on the input video."),
  167. .priv_size = sizeof(DrawGridContext),
  168. .priv_class = &drawgrid_class,
  169. .init = init,
  170. .query_formats = query_formats,
  171. .inputs = avfilter_vf_drawgrid_inputs,
  172. .outputs = avfilter_vf_drawgrid_outputs,
  173. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  174. };