FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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, const char *args)
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  return ret;
187 
188 #define CHECK_UNSET_OPT(opt) \
189  if (delogo->opt == -1) { \
190  av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
191  return AVERROR(EINVAL); \
192  }
193  CHECK_UNSET_OPT(x);
195  CHECK_UNSET_OPT(w);
196  CHECK_UNSET_OPT(h);
197 
198  if (delogo->show)
199  delogo->band = 4;
200 
201  av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
202  delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
203 
204  delogo->w += delogo->band*2;
205  delogo->h += delogo->band*2;
206  delogo->x -= delogo->band;
207  delogo->y -= delogo->band;
208 
209  return 0;
210 }
211 
213 {
214  DelogoContext *delogo = inlink->dst->priv;
215  AVFilterLink *outlink = inlink->dst->outputs[0];
216  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
218  int hsub0 = desc->log2_chroma_w;
219  int vsub0 = desc->log2_chroma_h;
220  int direct = 0;
221  int plane;
222 
223  if (in->perms & AV_PERM_WRITE) {
224  direct = 1;
225  out = in;
226  } else {
227  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
228  if (!out) {
230  return AVERROR(ENOMEM);
231  }
233  }
234 
235  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
236  int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
237  int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
238 
239  apply_delogo(out->data[plane], out->linesize[plane],
240  in ->data[plane], in ->linesize[plane],
241  inlink->w>>hsub, inlink->h>>vsub,
242  delogo->x>>hsub, delogo->y>>vsub,
243  delogo->w>>hsub, delogo->h>>vsub,
244  delogo->band>>FFMIN(hsub, vsub),
245  delogo->show, direct);
246  }
247 
248  if (!direct)
250 
251  return ff_filter_frame(outlink, out);
252 }
253 
255  {
256  .name = "default",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .get_video_buffer = ff_null_get_video_buffer,
259  .filter_frame = filter_frame,
260  .min_perms = AV_PERM_WRITE | AV_PERM_READ,
261  },
262  { NULL }
263 };
264 
266  {
267  .name = "default",
268  .type = AVMEDIA_TYPE_VIDEO,
269  },
270  { NULL }
271 };
272 
274  .name = "delogo",
275  .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
276  .priv_size = sizeof(DelogoContext),
277  .init = init,
279 
280  .inputs = avfilter_vf_delogo_inputs,
281  .outputs = avfilter_vf_delogo_outputs,
282  .priv_class = &delogo_class,
283 };