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 Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
27 #include "libavutil/colorspace.h"
28 #include "libavutil/common.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 
36 enum { Y, U, V, A };
37 
38 typedef struct {
39  int x, y, w, h;
40  unsigned char yuv_color[4];
41  int vsub, hsub;
43 
44 static av_cold int init(AVFilterContext *ctx, const char *args)
45 {
47  char color_str[1024] = "black";
48  uint8_t rgba_color[4];
49 
50  drawbox->x = drawbox->y = drawbox->w = drawbox->h = 0;
51 
52  if (args)
53  sscanf(args, "%d:%d:%d:%d:%s",
54  &drawbox->x, &drawbox->y, &drawbox->w, &drawbox->h, color_str);
55 
56  if (av_parse_color(rgba_color, color_str, -1, ctx) < 0)
57  return AVERROR(EINVAL);
58 
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  return 0;
65 }
66 
68 {
69  enum AVPixelFormat pix_fmts[] = {
75  };
76 
78  return 0;
79 }
80 
81 static int config_input(AVFilterLink *inlink)
82 {
83  DrawBoxContext *drawbox = inlink->dst->priv;
84  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
85 
86  drawbox->hsub = desc->log2_chroma_w;
87  drawbox->vsub = desc->log2_chroma_h;
88 
89  if (drawbox->w == 0) drawbox->w = inlink->w;
90  if (drawbox->h == 0) drawbox->h = inlink->h;
91 
92  av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
93  drawbox->w, drawbox->y, drawbox->w, drawbox->h,
94  drawbox->yuv_color[Y], drawbox->yuv_color[U], drawbox->yuv_color[V], drawbox->yuv_color[A]);
95 
96  return 0;
97 }
98 
99 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
100 {
101  DrawBoxContext *drawbox = inlink->dst->priv;
102  int plane, x, y, xb = drawbox->x, yb = drawbox->y;
103  unsigned char *row[4];
104 
105  for (y = FFMAX(yb, 0); y < frame->video->h && y < (yb + drawbox->h); y++) {
106  row[0] = frame->data[0] + y * frame->linesize[0];
107 
108  for (plane = 1; plane < 3; plane++)
109  row[plane] = frame->data[plane] +
110  frame->linesize[plane] * (y >> drawbox->vsub);
111 
112  for (x = FFMAX(xb, 0); x < (xb + drawbox->w) && x < frame->video->w; x++) {
113  double alpha = (double)drawbox->yuv_color[A] / 255;
114 
115  if ((y - yb < 3) || (yb + drawbox->h - y < 4) ||
116  (x - xb < 3) || (xb + drawbox->w - x < 4)) {
117  row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawbox->yuv_color[Y];
118  row[1][x >> drawbox->hsub] = (1 - alpha) * row[1][x >> drawbox->hsub] + alpha * drawbox->yuv_color[U];
119  row[2][x >> drawbox->hsub] = (1 - alpha) * row[2][x >> drawbox->hsub] + alpha * drawbox->yuv_color[V];
120  }
121  }
122  }
123 
124  return ff_filter_frame(inlink->dst->outputs[0], frame);
125 }
126 
128  {
129  .name = "default",
130  .type = AVMEDIA_TYPE_VIDEO,
131  .config_props = config_input,
132  .get_video_buffer = ff_null_get_video_buffer,
133  .filter_frame = filter_frame,
134  .min_perms = AV_PERM_WRITE | AV_PERM_READ,
135  .rej_perms = AV_PERM_PRESERVE
136  },
137  { NULL }
138 };
139 
141  {
142  .name = "default",
143  .type = AVMEDIA_TYPE_VIDEO,
144  },
145  { NULL }
146 };
147 
149  .name = "drawbox",
150  .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
151  .priv_size = sizeof(DrawBoxContext),
152  .init = init,
153 
155  .inputs = avfilter_vf_drawbox_inputs,
156  .outputs = avfilter_vf_drawbox_outputs,
157 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
Various defines for YUV<->RGB conversion.
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
const char * name
Pad name.
Definition: internal.h:39
uint8_t
static int query_formats(AVFilterContext *ctx)
Definition: vf_drawbox.c:67
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:101
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
static const AVFilterPad avfilter_vf_drawbox_outputs[]
Definition: vf_drawbox.c:140
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:375
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:300
A filter pad used for either input or output.
Definition: internal.h:33
static int config_input(AVFilterLink *inlink)
Definition: vf_drawbox.c:81
int h
image height
Definition: avfilter.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
Definition: vf_drawbox.c:99
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
Definition: vf_drawbox.c:36
static const AVFilterPad avfilter_vf_drawbox_inputs[]
Definition: vf_drawbox.c:127
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
Definition: vf_drawbox.c:36
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
static void drawbox(AVFilterBufferRef *picref, unsigned int x, unsigned int y, unsigned int width, unsigned int height, uint8_t *line[4], int pixel_step[4], uint8_t color[4], int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
Definition: vf_drawtext.c:695
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:41
AVFilter avfilter_vf_drawbox
Definition: vf_drawbox.c:148
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
const char * name
filter name
Definition: avfilter.h:372
misc parsing utilities
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
#define RGB_TO_U_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:103
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:99
Definition: vf_drawbox.c:36
#define RGB_TO_V_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:107
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_drawbox.c:44
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
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:79
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
#define RGB_TO_Y_CCIR(r, g, b)
Definition: colorspace.h:99
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
An instance of a filter.
Definition: avfilter.h:418
unsigned char yuv_color[4]
Definition: vf_drawbox.c:40
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
Definition: vf_drawbox.c:36
internal API functions
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:72
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)