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 FFmpeg.
6  *
7  * FFmpeg 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  * 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
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 FFmpeg; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 /**
23  * @file
24  * A very simple tv station logo remover
25  * Ported from MPlayer libmpcodecs/vf_delogo.c.
26  */
27 
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 
37 /**
38  * Apply a simple delogo algorithm to the image in dst and put the
39  * result in src.
40  *
41  * The algorithm is only applied to the region specified by the logo
42  * parameters.
43  *
44  * @param w width of the input image
45  * @param h height of the input image
46  * @param logo_x x coordinate of the top left corner of the logo region
47  * @param logo_y y coordinate of the top left corner of the logo region
48  * @param logo_w width of the logo
49  * @param logo_h height of the logo
50  * @param band the size of the band around the processed area
51  * @param show show a rectangle around the processed area, useful for
52  * parameters tweaking
53  * @param direct if non-zero perform in-place processing
54  */
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 =
94  (topleft[src_linesize*(y-logo_y -yclipt)] +
95  topleft[src_linesize*(y-logo_y-1-yclipt)] +
96  topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
97  +
98  (topright[src_linesize*(y-logo_y-yclipt)] +
99  topright[src_linesize*(y-logo_y-1-yclipt)] +
100  topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
101  +
102  (topleft[x-logo_x-xclipl] +
103  topleft[x-logo_x-1-xclipl] +
104  topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
105  +
106  (botleft[x-logo_x-xclipl] +
107  botleft[x-logo_x-1-xclipl] +
108  botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
109  interp /= 6;
110 
111  if (y >= logo_y+band && y < logo_y+logo_h-band &&
112  x >= logo_x+band && x < logo_x+logo_w-band) {
113  *xdst = interp;
114  } else {
115  dist = 0;
116  if (x < logo_x+band)
117  dist = FFMAX(dist, logo_x-x+band);
118  else if (x >= logo_x+logo_w-band)
119  dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
120 
121  if (y < logo_y+band)
122  dist = FFMAX(dist, logo_y-y+band);
123  else if (y >= logo_y+logo_h-band)
124  dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
125 
126  *xdst = (*xsrc*dist + interp*(band-dist))/band;
127  if (show && (dist == band-1))
128  *xdst = 0;
129  }
130  }
131 
132  dst += dst_linesize;
133  src += src_linesize;
134  }
135 }
136 
137 typedef struct {
138  const AVClass *class;
139  int x, y, w, h, band, show;
140 } DelogoContext;
141 
142 #define OFFSET(x) offsetof(DelogoContext, x)
143 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
144 
145 static const AVOption delogo_options[]= {
146  { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
147  { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
148  { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
149  { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
150  { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
151  { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
152  { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
153  { NULL },
154 };
155 
156 AVFILTER_DEFINE_CLASS(delogo);
157 
159 {
160  static const enum AVPixelFormat pix_fmts[] = {
165  };
166 
168  return 0;
169 }
170 
171 static av_cold int init(AVFilterContext *ctx)
172 {
173  DelogoContext *delogo = ctx->priv;
174 
175 #define CHECK_UNSET_OPT(opt) \
176  if (delogo->opt == -1) { \
177  av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
178  return AVERROR(EINVAL); \
179  }
183  CHECK_UNSET_OPT(h);
184 
185  if (delogo->band < 0 || delogo->show) {
186  delogo->show = 1;
187  delogo->band = 4;
188  }
189 
190  av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
191  delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
192 
193  delogo->w += delogo->band*2;
194  delogo->h += delogo->band*2;
195  delogo->x -= delogo->band;
196  delogo->y -= delogo->band;
197 
198  return 0;
199 }
200 
201 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
202 {
203  DelogoContext *delogo = inlink->dst->priv;
204  AVFilterLink *outlink = inlink->dst->outputs[0];
205  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
206  AVFrame *out;
207  int hsub0 = desc->log2_chroma_w;
208  int vsub0 = desc->log2_chroma_h;
209  int direct = 0;
210  int plane;
211 
212  if (av_frame_is_writable(in)) {
213  direct = 1;
214  out = in;
215  } else {
216  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
217  if (!out) {
218  av_frame_free(&in);
219  return AVERROR(ENOMEM);
220  }
221 
222  av_frame_copy_props(out, in);
223  }
224 
225  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
226  int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
227  int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
228 
229  apply_delogo(out->data[plane], out->linesize[plane],
230  in ->data[plane], in ->linesize[plane],
231  inlink->w>>hsub, inlink->h>>vsub,
232  delogo->x>>hsub, delogo->y>>vsub,
233  delogo->w>>hsub, delogo->h>>vsub,
234  delogo->band>>FFMIN(hsub, vsub),
235  delogo->show, direct);
236  }
237 
238  if (!direct)
239  av_frame_free(&in);
240 
241  return ff_filter_frame(outlink, out);
242 }
243 
245  {
246  .name = "default",
247  .type = AVMEDIA_TYPE_VIDEO,
248  .get_video_buffer = ff_null_get_video_buffer,
249  .filter_frame = filter_frame,
250  },
251  { NULL }
252 };
253 
255  {
256  .name = "default",
257  .type = AVMEDIA_TYPE_VIDEO,
258  },
259  { NULL }
260 };
261 
263  .name = "delogo",
264  .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
265  .priv_size = sizeof(DelogoContext),
266  .priv_class = &delogo_class,
267  .init = init,
269 
270  .inputs = avfilter_vf_delogo_inputs,
271  .outputs = avfilter_vf_delogo_outputs,
272 };
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
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
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
#define FLAGS
Definition: vf_delogo.c:143
static int query_formats(AVFilterContext *ctx)
Definition: vf_delogo.c:158
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
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
const char * name
Pad name.
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:105
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.
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
AVFilter avfilter_vf_delogo
Definition: vf_delogo.c:262
A filter pad used for either input or output.
Discrete Time axis x
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:254
#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
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:361
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static av_cold int init(AVFilterContext *ctx)
Definition: vf_delogo.c:171
#define OFFSET(x)
Definition: vf_delogo.c:142
#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
#define FFMIN(a, b)
Definition: common.h:58
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_delogo.c:201
NULL
Definition: eval.c:55
AVFILTER_DEFINE_CLASS(delogo)
AVS_Value src
Definition: avisynth_c.h:523
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
static const AVOption delogo_options[]
Definition: vf_delogo.c:145
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:74
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
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
#define CHECK_UNSET_OPT(opt)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
Y , 8bpp.
Definition: pixfmt.h:76
common internal and external API header
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
function y
Definition: D.m:1
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
An instance of a filter.
Definition: avfilter.h:524
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:103
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
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:242
internal API functions
static const AVFilterPad avfilter_vf_delogo_inputs[]
Definition: vf_delogo.c:244
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