vf_delogo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
3  * Copyright (c) 2011 Stefano Sabatini
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Libav 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with Libav; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
28 #include "libavutil/common.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 
55 static void apply_delogo(uint8_t *dst, int dst_linesize,
56  uint8_t *src, int src_linesize,
57  int w, int h,
58  int logo_x, int logo_y, int logo_w, int logo_h,
59  int band, int show, int direct)
60 {
61  int x, y;
62  int interp, dist;
63  uint8_t *xdst, *xsrc;
64 
65  uint8_t *topleft, *botleft, *topright;
66  int xclipl, xclipr, yclipt, yclipb;
67  int logo_x1, logo_x2, logo_y1, logo_y2;
68 
69  xclipl = FFMAX(-logo_x, 0);
70  xclipr = FFMAX(logo_x+logo_w-w, 0);
71  yclipt = FFMAX(-logo_y, 0);
72  yclipb = FFMAX(logo_y+logo_h-h, 0);
73 
74  logo_x1 = logo_x + xclipl;
75  logo_x2 = logo_x + logo_w - xclipr;
76  logo_y1 = logo_y + yclipt;
77  logo_y2 = logo_y + logo_h - yclipb;
78 
79  topleft = src+logo_y1 * src_linesize+logo_x1;
80  topright = src+logo_y1 * src_linesize+logo_x2-1;
81  botleft = src+(logo_y2-1) * src_linesize+logo_x1;
82 
83  if (!direct)
84  av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
85 
86  dst += (logo_y1 + 1) * dst_linesize;
87  src += (logo_y1 + 1) * src_linesize;
88 
89  for (y = logo_y1+1; y < logo_y2-1; y++) {
90  for (x = logo_x1+1,
91  xdst = dst+logo_x1+1,
92  xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
93  interp = (topleft[src_linesize*(y-logo_y -yclipt)] +
94  topleft[src_linesize*(y-logo_y-1-yclipt)] +
95  topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
96  + (topright[src_linesize*(y-logo_y-yclipt)] +
97  topright[src_linesize*(y-logo_y-1-yclipt)] +
98  topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
99  + (topleft[x-logo_x-xclipl] +
100  topleft[x-logo_x-1-xclipl] +
101  topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
102  + (botleft[x-logo_x-xclipl] +
103  botleft[x-logo_x-1-xclipl] +
104  botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
105  interp /= 6;
106 
107  if (y >= logo_y+band && y < logo_y+logo_h-band &&
108  x >= logo_x+band && x < logo_x+logo_w-band) {
109  *xdst = interp;
110  } else {
111  dist = 0;
112  if (x < logo_x+band)
113  dist = FFMAX(dist, logo_x-x+band);
114  else if (x >= logo_x+logo_w-band)
115  dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
116 
117  if (y < logo_y+band)
118  dist = FFMAX(dist, logo_y-y+band);
119  else if (y >= logo_y+logo_h-band)
120  dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
121 
122  *xdst = (*xsrc*dist + interp*(band-dist))/band;
123  if (show && (dist == band-1))
124  *xdst = 0;
125  }
126  }
127 
128  dst += dst_linesize;
129  src += src_linesize;
130  }
131 }
132 
133 typedef struct {
134  const AVClass *class;
135  int x, y, w, h, band, show;
136 } DelogoContext;
137 
138 #define OFFSET(x) offsetof(DelogoContext, x)
139 
140 static const AVOption delogo_options[]= {
141  {"x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX },
142  {"y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX },
143  {"w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX },
144  {"h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX },
145  {"band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 = 4}, -1, INT_MAX },
146  {"t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 = 4}, -1, INT_MAX },
147  {"show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1 },
148  {NULL},
149 };
150 
151 static const char *delogo_get_name(void *ctx)
152 {
153  return "delogo";
154 }
155 
156 static const AVClass delogo_class = {
157  .class_name = "DelogoContext",
158  .item_name = delogo_get_name,
159  .option = delogo_options,
160 };
161 
163 {
164  enum AVPixelFormat pix_fmts[] = {
169  };
170 
172  return 0;
173 }
174 
175 static av_cold int init(AVFilterContext *ctx, const char *args)
176 {
177  DelogoContext *delogo = ctx->priv;
178  int ret = 0;
179 
180  delogo->class = &delogo_class;
181  av_opt_set_defaults(delogo);
182 
183  if (args)
184  ret = sscanf(args, "%d:%d:%d:%d:%d",
185  &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
186  if (ret == 5) {
187  if (delogo->band < 0)
188  delogo->show = 1;
189  } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0) {
190  av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
191  return ret;
192  }
193 
194 #define CHECK_UNSET_OPT(opt) \
195  if (delogo->opt == -1) { \
196  av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
197  return AVERROR(EINVAL); \
198  }
199  CHECK_UNSET_OPT(x);
200  CHECK_UNSET_OPT(y);
201  CHECK_UNSET_OPT(w);
202  CHECK_UNSET_OPT(h);
203 
204  if (delogo->show)
205  delogo->band = 4;
206 
207  av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
208  delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
209 
210  delogo->w += delogo->band*2;
211  delogo->h += delogo->band*2;
212  delogo->x -= delogo->band;
213  delogo->y -= delogo->band;
214 
215  return 0;
216 }
217 
219 {
220  DelogoContext *delogo = inlink->dst->priv;
221  AVFilterLink *outlink = inlink->dst->outputs[0];
222  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
223  AVFilterBufferRef *out;
224  int hsub0 = desc->log2_chroma_w;
225  int vsub0 = desc->log2_chroma_h;
226  int direct = 0;
227  int plane;
228 
229  if ((in->perms & AV_PERM_WRITE) && !(in->perms & AV_PERM_PRESERVE)) {
230  direct = 1;
231  out = in;
232  } else {
233  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
234  if (!out) {
236  return AVERROR(ENOMEM);
237  }
238 
240  out->video->w = outlink->w;
241  out->video->h = outlink->h;
242  }
243 
244  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
245  int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
246  int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
247 
248  apply_delogo(out->data[plane], out->linesize[plane],
249  in ->data[plane], in ->linesize[plane],
250  inlink->w>>hsub, inlink->h>>vsub,
251  delogo->x>>hsub, delogo->y>>vsub,
252  delogo->w>>hsub, delogo->h>>vsub,
253  delogo->band>>FFMIN(hsub, vsub),
254  delogo->show, direct);
255  }
256 
257  if (!direct)
259 
260  return ff_filter_frame(outlink, out);
261 }
262 
264  {
265  .name = "default",
266  .type = AVMEDIA_TYPE_VIDEO,
267  .get_video_buffer = ff_null_get_video_buffer,
268  .filter_frame = filter_frame,
269  .min_perms = AV_PERM_WRITE | AV_PERM_READ,
270  .rej_perms = AV_PERM_PRESERVE
271  },
272  { NULL }
273 };
274 
276  {
277  .name = "default",
278  .type = AVMEDIA_TYPE_VIDEO,
279  },
280  { NULL }
281 };
282 
284  .name = "delogo",
285  .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
286  .priv_size = sizeof(DelogoContext),
287  .init = init,
289 
290  .inputs = avfilter_vf_delogo_inputs,
291  .outputs = avfilter_vf_delogo_outputs,
292 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
AVOption.
Definition: opt.h:233
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
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:505
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
static int query_formats(AVFilterContext *ctx)
Definition: vf_delogo.c:162
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:587
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
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
AVOptions.
static const AVClass delogo_class
Definition: vf_delogo.c:156
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
Definition: buffer.c:88
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
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_delogo.c:175
AVFilter avfilter_vf_delogo
Definition: vf_delogo.c:283
A filter pad used for either input or output.
Definition: internal.h:33
AVFilterBufferRef * ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:145
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 const AVFilterPad avfilter_vf_delogo_outputs[]
Definition: vf_delogo.c:275
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
Definition: vf_delogo.c:218
#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
#define OFFSET(x)
Definition: vf_delogo.c:138
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
int perms
permissions, see the AV_PERM_* flags
Definition: avfilter.h:172
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
static const char * delogo_get_name(void *ctx)
Definition: vf_delogo.c:151
static const AVOption delogo_options[]
Definition: vf_delogo.c:140
static void apply_delogo(uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize, int w, int h, int logo_x, int logo_y, int logo_w, int logo_h, int band, int show, int direct)
Apply a simple delogo algorithm to the image in dst and put the result in src.
Definition: vf_delogo.c:55
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
const AVClass * class
Definition: vf_delogo.c:134
const char * name
filter name
Definition: avfilter.h:372
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:99
#define CHECK_UNSET_OPT(opt)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
common internal and external API header
void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
Copy properties of src to dst, without copying the actual data.
Definition: buffer.c:164
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
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
An instance of a filter.
Definition: avfilter.h:418
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:231
internal API functions
static const AVFilterPad avfilter_vf_delogo_inputs[]
Definition: vf_delogo.c:263
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