vf_drawbox.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Affine Systems, Inc (Michael Sullivan, Bobby Impollonia)
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Box drawing filter. Also a nice template for a filter that needs to
24  * write in the input frame.
25  */
26 
27 #include "libavutil/colorspace.h"
28 #include "libavutil/common.h"
29 #include "libavutil/opt.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 
37 enum { Y, U, V, A };
38 
39 typedef struct {
40  const AVClass *class;
41  int x, y, w, h, thickness;
42  char *color_str;
43  unsigned char yuv_color[4];
44  int invert_color; ///< invert luma color
45  int vsub, hsub; ///< chroma subsampling
47 
48 static av_cold int init(AVFilterContext *ctx)
49 {
50  DrawBoxContext *drawbox = ctx->priv;
51  uint8_t rgba_color[4];
52 
53  if (!strcmp(drawbox->color_str, "invert"))
54  drawbox->invert_color = 1;
55  else if (av_parse_color(rgba_color, drawbox->color_str, -1, ctx) < 0)
56  return AVERROR(EINVAL);
57 
58  if (!drawbox->invert_color) {
59  drawbox->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
60  drawbox->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
61  drawbox->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
62  drawbox->yuv_color[A] = rgba_color[3];
63  }
64 
65  return 0;
66 }
67 
69 {
70  static const enum AVPixelFormat pix_fmts[] = {
76  };
77 
79  return 0;
80 }
81 
82 static int config_input(AVFilterLink *inlink)
83 {
84  DrawBoxContext *drawbox = inlink->dst->priv;
85  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
86 
87  drawbox->hsub = desc->log2_chroma_w;
88  drawbox->vsub = desc->log2_chroma_h;
89 
90  if (drawbox->w == 0) drawbox->w = inlink->w;
91  if (drawbox->h == 0) drawbox->h = inlink->h;
92 
93  av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
94  drawbox->x, drawbox->y, drawbox->w, drawbox->h,
95  drawbox->yuv_color[Y], drawbox->yuv_color[U], drawbox->yuv_color[V], drawbox->yuv_color[A]);
96 
97  return 0;
98 }
99 
100 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
101 {
102  DrawBoxContext *drawbox = inlink->dst->priv;
103  int plane, x, y, xb = drawbox->x, yb = drawbox->y;
104  unsigned char *row[4];
105 
106  for (y = FFMAX(yb, 0); y < frame->height && y < (yb + drawbox->h); y++) {
107  row[0] = frame->data[0] + y * frame->linesize[0];
108 
109  for (plane = 1; plane < 3; plane++)
110  row[plane] = frame->data[plane] +
111  frame->linesize[plane] * (y >> drawbox->vsub);
112 
113  if (drawbox->invert_color) {
114  for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->width; x++)
115  if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
116  (x - xb < drawbox->thickness-1) || (xb + drawbox->w - x < drawbox->thickness))
117  row[0][x] = 0xff - row[0][x];
118  } else {
119  for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->width; x++) {
120  double alpha = (double)drawbox->yuv_color[A] / 255;
121 
122  if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
123  (x - xb < drawbox->thickness-1) || (xb + drawbox->w - x < drawbox->thickness)) {
124  row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawbox->yuv_color[Y];
125  row[1][x >> drawbox->hsub] = (1 - alpha) * row[1][x >> drawbox->hsub] + alpha * drawbox->yuv_color[U];
126  row[2][x >> drawbox->hsub] = (1 - alpha) * row[2][x >> drawbox->hsub] + alpha * drawbox->yuv_color[V];
127  }
128  }
129  }
130  }
131 
132  return ff_filter_frame(inlink->dst->outputs[0], frame);
133 }
134 
135 #define OFFSET(x) offsetof(DrawBoxContext, x)
136 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
137 
138 static const AVOption drawbox_options[] = {
139  { "x", "Horizontal position of the left box edge", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
140  { "y", "Vertical position of the top box edge", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
141  { "width", "Width of the box", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
142  { "w", "Width of the box", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
143  { "height", "Height of the box", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
144  { "h", "Height of the box", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
145  { "color", "Color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, .flags = FLAGS },
146  { "c", "Color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, .flags = FLAGS },
147  { "thickness", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
148  { "t", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
149  { NULL },
150 };
151 
152 AVFILTER_DEFINE_CLASS(drawbox);
153 
155  {
156  .name = "default",
157  .type = AVMEDIA_TYPE_VIDEO,
158  .config_props = config_input,
159  .get_video_buffer = ff_null_get_video_buffer,
160  .filter_frame = filter_frame,
161  .needs_writable = 1,
162  },
163  { NULL }
164 };
165 
167  {
168  .name = "default",
169  .type = AVMEDIA_TYPE_VIDEO,
170  },
171  { NULL }
172 };
173 
175  .name = "drawbox",
176  .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
177  .priv_size = sizeof(DrawBoxContext),
178  .priv_class = &drawbox_class,
179  .init = init,
180 
182  .inputs = avfilter_vf_drawbox_inputs,
183  .outputs = avfilter_vf_drawbox_outputs,
184 };
Definition: vf_drawbox.c:37
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
Definition: vf_drawbox.c:37
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
char * color_str
Definition: vf_drawbox.c:42
Various defines for YUV<->RGB conversion.
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
output residual component w
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
const char * name
Pad name.
#define OFFSET(x)
Definition: vf_drawbox.c:135
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
Definition: vf_drawbox.c:37
static int query_formats(AVFilterContext *ctx)
Definition: vf_drawbox.c:68
#define FLAGS
Definition: vf_drawbox.c:136
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:104
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:81
static const AVFilterPad avfilter_vf_drawbox_outputs[]
Definition: vf_drawbox.c:166
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:545
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
Definition: parseutils.c:337
frame
Definition: stft.m:14
int invert_color
invert luma color
Definition: vf_drawbox.c:44
A filter pad used for either input or output.
Discrete Time axis x
static int config_input(AVFilterLink *inlink)
Definition: vf_drawbox.c:82
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:86
int width
width and height of the video frame
Definition: frame.h:122
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
AVFILTER_DEFINE_CLASS(drawbox)
#define FFMAX(a, b)
Definition: common.h:56
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
#define AV_LOG_VERBOSE
Definition: log.h:157
static const AVFilterPad avfilter_vf_drawbox_inputs[]
Definition: vf_drawbox.c:154
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:80
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
int hsub
chroma subsampling
Definition: vf_drawbox.c:45
AVFilter avfilter_vf_drawbox
Definition: vf_drawbox.c:174
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:74
Definition: vf_drawbox.c:37
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
#define RGB_TO_U_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:103
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
#define RGB_TO_V_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:107
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
common internal and external API header
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:82
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
function y
Definition: D.m:1
static av_cold int init(AVFilterContext *ctx)
Definition: vf_drawbox.c:48
#define RGB_TO_Y_CCIR(r, g, b)
Definition: colorspace.h:99
static const AVOption drawbox_options[]
Definition: vf_drawbox.c:138
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_drawbox.c:100
An instance of a filter.
Definition: avfilter.h:524
int height
Definition: frame.h:122
unsigned char yuv_color[4]
Definition: vf_drawbox.c:43
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:103
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs