2 * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
3 * Copyright (c) 2011 Stefano Sabatini
5 * This file is part of FFmpeg.
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.
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.
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.
24 * A very simple tv station logo remover
25 * Ported from MPlayer libmpcodecs/vf_delogo.c.
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
37 * Apply a simple delogo algorithm to the image in dst and put the
40 * The algorithm is only applied to the region specified by the logo
43 * @param w width of the input image
44 * @param h height of the input image
45 * @param logo_x x coordinate of the top left corner of the logo region
46 * @param logo_y y coordinate of the top left corner of the logo region
47 * @param logo_w width of the logo
48 * @param logo_h height of the logo
49 * @param band the size of the band around the processed area
50 * @param show show a rectangle around the processed area, useful for
52 * @param direct if non-zero perform in-place processing
54 static void apply_delogo(uint8_t *dst, int dst_linesize,
55 uint8_t *src, int src_linesize,
57 int logo_x, int logo_y, int logo_w, int logo_h,
58 int band, int show, int direct)
64 uint8_t *topleft, *botleft, *topright;
65 int xclipl, xclipr, yclipt, yclipb;
66 int logo_x1, logo_x2, logo_y1, logo_y2;
68 xclipl = FFMAX(-logo_x, 0);
69 xclipr = FFMAX(logo_x+logo_w-w, 0);
70 yclipt = FFMAX(-logo_y, 0);
71 yclipb = FFMAX(logo_y+logo_h-h, 0);
73 logo_x1 = logo_x + xclipl;
74 logo_x2 = logo_x + logo_w - xclipr;
75 logo_y1 = logo_y + yclipt;
76 logo_y2 = logo_y + logo_h - yclipb;
78 topleft = src+logo_y1 * src_linesize+logo_x1;
79 topright = src+logo_y1 * src_linesize+logo_x2-1;
80 botleft = src+(logo_y2-1) * src_linesize+logo_x1;
82 dst += (logo_y1+1)*dst_linesize;
83 src += (logo_y1+1)*src_linesize;
86 av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
88 for (y = logo_y1+1; y < logo_y2-1; y++) {
91 xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
93 (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
97 (topright[src_linesize*(y-logo_y-yclipt)] +
98 topright[src_linesize*(y-logo_y-1-yclipt)] +
99 topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
101 (topleft[x-logo_x-xclipl] +
102 topleft[x-logo_x-1-xclipl] +
103 topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
105 (botleft[x-logo_x-xclipl] +
106 botleft[x-logo_x-1-xclipl] +
107 botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
110 if (y >= logo_y+band && y < logo_y+logo_h-band &&
111 x >= logo_x+band && x < logo_x+logo_w-band) {
116 dist = FFMAX(dist, logo_x-x+band);
117 else if (x >= logo_x+logo_w-band)
118 dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
121 dist = FFMAX(dist, logo_y-y+band);
122 else if (y >= logo_y+logo_h-band)
123 dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
125 *xdst = (*xsrc*dist + interp*(band-dist))/band;
126 if (show && (dist == band-1))
137 const AVClass *class;
138 int x, y, w, h, band, show;
141 #define OFFSET(x) offsetof(DelogoContext, x)
143 static const AVOption delogo_options[]= {
144 {"x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
145 {"y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
146 {"w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
147 {"h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
148 {"band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
149 {"t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
150 {"show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, {.dbl= 0}, 0, 1 },
154 AVFILTER_DEFINE_CLASS(delogo);
156 static int query_formats(AVFilterContext *ctx)
158 enum PixelFormat pix_fmts[] = {
159 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
160 PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P,
161 PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
165 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
169 static av_cold int init(AVFilterContext *ctx, const char *args)
171 DelogoContext *delogo = ctx->priv;
174 delogo->class = &delogo_class;
175 av_opt_set_defaults(delogo);
178 ret = sscanf(args, "%d:%d:%d:%d:%d",
179 &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
181 if (delogo->band < 0)
183 } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0) {
184 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
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); \
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);
204 delogo->w += delogo->band*2;
205 delogo->h += delogo->band*2;
206 delogo->x -= delogo->band;
207 delogo->y -= delogo->band;
212 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
217 static int end_frame(AVFilterLink *inlink)
219 DelogoContext *delogo = inlink->dst->priv;
220 AVFilterLink *outlink = inlink->dst->outputs[0];
221 AVFilterBufferRef *inpicref = inlink ->cur_buf;
222 AVFilterBufferRef *outpicref = outlink->out_buf;
223 int direct = inpicref->buf == outpicref->buf;
224 int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
225 int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
229 for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
230 int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
231 int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
233 apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
234 inpicref ->data[plane], inpicref ->linesize[plane],
235 inlink->w>>hsub, inlink->h>>vsub,
236 delogo->x>>hsub, delogo->y>>vsub,
237 delogo->w>>hsub, delogo->h>>vsub,
238 delogo->band>>FFMIN(hsub, vsub),
239 delogo->show, direct);
242 if ((ret = ff_draw_slice(outlink, 0, inlink->h, 1)) < 0 ||
243 (ret = ff_end_frame(outlink)) < 0)
248 AVFilter avfilter_vf_delogo = {
250 .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
251 .priv_size = sizeof(DelogoContext),
253 .query_formats = query_formats,
255 .inputs = (const AVFilterPad[]) {{ .name = "default",
256 .type = AVMEDIA_TYPE_VIDEO,
257 .get_video_buffer = ff_null_get_video_buffer,
258 .start_frame = ff_inplace_start_frame,
259 .draw_slice = null_draw_slice,
260 .end_frame = end_frame,
261 .min_perms = AV_PERM_WRITE | AV_PERM_READ,
262 .rej_perms = AV_PERM_PRESERVE },
264 .outputs = (const AVFilterPad[]) {{ .name = "default",
265 .type = AVMEDIA_TYPE_VIDEO, },