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/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 
51 static void apply_delogo(uint8_t *dst, int dst_linesize,
52  uint8_t *src, int src_linesize,
53  int w, int h,
54  int logo_x, int logo_y, int logo_w, int logo_h,
55  int band, int show, int direct)
56 {
57  int x, y;
58  int interp, dist;
59  uint8_t *xdst, *xsrc;
60 
61  uint8_t *topleft, *botleft, *topright;
62  int xclipl, xclipr, yclipt, yclipb;
63  int logo_x1, logo_x2, logo_y1, logo_y2;
64 
65  xclipl = FFMAX(-logo_x, 0);
66  xclipr = FFMAX(logo_x+logo_w-w, 0);
67  yclipt = FFMAX(-logo_y, 0);
68  yclipb = FFMAX(logo_y+logo_h-h, 0);
69 
70  logo_x1 = logo_x + xclipl;
71  logo_x2 = logo_x + logo_w - xclipr;
72  logo_y1 = logo_y + yclipt;
73  logo_y2 = logo_y + logo_h - yclipb;
74 
75  topleft = src+logo_y1 * src_linesize+logo_x1;
76  topright = src+logo_y1 * src_linesize+logo_x2-1;
77  botleft = src+(logo_y2-1) * src_linesize+logo_x1;
78 
79  dst += (logo_y1+1)*dst_linesize;
80  src += (logo_y1+1)*src_linesize;
81 
82  if (!direct)
83  av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
84 
85  for (y = logo_y1+1; y < logo_y2-1; y++) {
86  for (x = logo_x1+1,
87  xdst = dst+logo_x1+1,
88  xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
89  interp = (topleft[src_linesize*(y-logo_y -yclipt)] +
90  topleft[src_linesize*(y-logo_y-1-yclipt)] +
91  topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
92  + (topright[src_linesize*(y-logo_y-yclipt)] +
93  topright[src_linesize*(y-logo_y-1-yclipt)] +
94  topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
95  + (topleft[x-logo_x-xclipl] +
96  topleft[x-logo_x-1-xclipl] +
97  topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
98  + (botleft[x-logo_x-xclipl] +
99  botleft[x-logo_x-1-xclipl] +
100  botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
101  interp /= 6;
102 
103  if (y >= logo_y+band && y < logo_y+logo_h-band &&
104  x >= logo_x+band && x < logo_x+logo_w-band) {
105  *xdst = interp;
106  } else {
107  dist = 0;
108  if (x < logo_x+band)
109  dist = FFMAX(dist, logo_x-x+band);
110  else if (x >= logo_x+logo_w-band)
111  dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
112 
113  if (y < logo_y+band)
114  dist = FFMAX(dist, logo_y-y+band);
115  else if (y >= logo_y+logo_h-band)
116  dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
117 
118  *xdst = (*xsrc*dist + interp*(band-dist))/band;
119  if (show && (dist == band-1))
120  *xdst = 0;
121  }
122  }
123 
124  dst += dst_linesize;
125  src += src_linesize;
126  }
127 }
128 
129 typedef struct {
130  const AVClass *class;
131  int x, y, w, h, band, show;
132 } DelogoContext;
133 
134 #define OFFSET(x) offsetof(DelogoContext, x)
135 
136 static const AVOption delogo_options[]= {
137  {"x", "set logo x position", OFFSET(x), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
138  {"y", "set logo y position", OFFSET(y), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
139  {"w", "set logo width", OFFSET(w), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
140  {"h", "set logo height", OFFSET(h), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
141  {"band", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
142  {"t", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
143  {"show", "show delogo area", OFFSET(show), FF_OPT_TYPE_INT, { 0}, 0, 1 },
144  {NULL},
145 };
146 
147 static const char *delogo_get_name(void *ctx)
148 {
149  return "delogo";
150 }
151 
152 static const AVClass delogo_class = {
153  .class_name = "DelogoContext",
154  .item_name = delogo_get_name,
155  .option = delogo_options,
156 };
157 
159 {
160  enum PixelFormat pix_fmts[] = {
165  };
166 
168  return 0;
169 }
170 
171 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
172 {
173  DelogoContext *delogo = ctx->priv;
174  int ret = 0;
175 
176  delogo->class = &delogo_class;
177  av_opt_set_defaults(delogo);
178 
179  if (args)
180  ret = sscanf(args, "%d:%d:%d:%d:%d",
181  &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
182  if (ret == 5) {
183  if (delogo->band < 0)
184  delogo->show = 1;
185  } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0) {
186  av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
187  return ret;
188  }
189 
190 #define CHECK_UNSET_OPT(opt) \
191  if (delogo->opt == -1) { \
192  av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
193  return AVERROR(EINVAL); \
194  }
195  CHECK_UNSET_OPT(x);
196  CHECK_UNSET_OPT(y);
197  CHECK_UNSET_OPT(w);
198  CHECK_UNSET_OPT(h);
199 
200  if (delogo->show)
201  delogo->band = 4;
202 
203  av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
204  delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
205 
206  delogo->w += delogo->band*2;
207  delogo->h += delogo->band*2;
208  delogo->x -= delogo->band;
209  delogo->y -= delogo->band;
210 
211  return 0;
212 }
213 
214 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
215 {
216  AVFilterLink *outlink = inlink->dst->outputs[0];
217  AVFilterBufferRef *outpicref;
218 
219  if (inpicref->perms & AV_PERM_PRESERVE) {
220  outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
221  outlink->w, outlink->h);
222  avfilter_copy_buffer_ref_props(outpicref, inpicref);
223  outpicref->video->w = outlink->w;
224  outpicref->video->h = outlink->h;
225  } else
226  outpicref = inpicref;
227 
228  outlink->out_buf = outpicref;
229  avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
230 }
231 
232 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
233 
234 static void end_frame(AVFilterLink *inlink)
235 {
236  DelogoContext *delogo = inlink->dst->priv;
237  AVFilterLink *outlink = inlink->dst->outputs[0];
238  AVFilterBufferRef *inpicref = inlink ->cur_buf;
239  AVFilterBufferRef *outpicref = outlink->out_buf;
240  int direct = inpicref == outpicref;
241  int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
242  int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
243  int plane;
244 
245  for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
246  int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
247  int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
248 
249  apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
250  inpicref ->data[plane], inpicref ->linesize[plane],
251  inlink->w>>hsub, inlink->h>>vsub,
252  delogo->x>>hsub, delogo->y>>vsub,
253  delogo->w>>hsub, delogo->h>>vsub,
254  delogo->band>>FFMIN(hsub, vsub),
255  delogo->show, direct);
256  }
257 
258  avfilter_draw_slice(outlink, 0, inlink->h, 1);
259  avfilter_end_frame(outlink);
260  avfilter_unref_buffer(inpicref);
261  if (!direct)
262  avfilter_unref_buffer(outpicref);
263 }
264 
266  .name = "delogo",
267  .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
268  .priv_size = sizeof(DelogoContext),
269  .init = init,
271 
272  .inputs = (AVFilterPad[]) {{ .name = "default",
273  .type = AVMEDIA_TYPE_VIDEO,
274  .get_video_buffer = avfilter_null_get_video_buffer,
275  .start_frame = start_frame,
276  .draw_slice = null_draw_slice,
277  .end_frame = end_frame,
278  .min_perms = AV_PERM_WRITE | AV_PERM_READ,
279  .rej_perms = AV_PERM_PRESERVE },
280  { .name = NULL}},
281  .outputs = (AVFilterPad[]) {{ .name = "default",
282  .type = AVMEDIA_TYPE_VIDEO, },
283  { .name = NULL}},
284 };
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:68
AVFilterBufferRef * avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: avfilter.c:289
AVOption.
Definition: opt.h:244
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:141
int linesize[8]
number of bytes per line
Definition: avfilter.h:127
misc image utilities
static void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
Copy properties of src to dst, without copying the actual data.
Definition: avfilter.h:148
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:598
static int query_formats(AVFilterContext *ctx)
Definition: vf_delogo.c:158
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:698
AVOptions.
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:71
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:81
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:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
#define av_cold
Definition: attributes.h:71
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:70
static const AVClass delogo_class
Definition: vf_delogo.c:152
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
Definition: vf_delogo.c:171
void avfilter_end_frame(AVFilterLink *link)
Notifie the next filter that the current frame has finished.
Definition: avfilter.c:430
AVFilter avfilter_vf_delogo
Definition: vf_delogo.c:265
A filter pad used for either input or output.
Definition: avfilter.h:312
static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Definition: vf_delogo.c:232
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: defaults.c:234
int h
image height
Definition: avfilter.h:108
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. ...
Definition: internal.h:191
void * priv
private data for use by the filter
Definition: avfilter.h:553
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVFilterFormats * avfilter_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:90
const AVPixFmtDescriptor av_pix_fmt_descriptors[PIX_FMT_NB]
The array of all the pixel format descriptors.
Definition: pixdesc.c:119
#define OFFSET(x)
Definition: vf_delogo.c:134
#define FFMAX(a, b)
Definition: common.h:53
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:69
#define FFMIN(a, b)
Definition: common.h:55
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
Definition: vf_delogo.c:214
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Send a slice to the next filter.
Definition: avfilter.c:447
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: avfilter.c:73
A reference to an AVFilterBuffer.
Definition: avfilter.h:124
NULL
Definition: eval.c:50
static void end_frame(AVFilterLink *inlink)
Definition: vf_delogo.c:234
int perms
permissions, see the AV_PERM_* flags
Definition: avfilter.h:138
static const char * delogo_get_name(void *ctx)
Definition: vf_delogo.c:147
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static const AVOption delogo_options[]
Definition: vf_delogo.c:136
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:51
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:497
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: avfilter.c:47
const AVClass * class
Definition: vf_delogo.c:130
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
Definition: avfilter.c:400
const char * name
filter name
Definition: avfilter.h:498
AVFilterBufferRef * avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
get_video_buffer() handler for filters which simply pass video along
Definition: defaults.c:288
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:83
#define CHECK_UNSET_OPT(opt)
PixelFormat
Pixel format.
Definition: pixfmt.h:62
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:125
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:82
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:126
An instance of a filter.
Definition: avfilter.h:538
Y , 8bpp.
Definition: pixfmt.h:72
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:224
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101