2 * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
3 * Copyright (c) 2011 Stefano Sabatini
5 * This file is part of Libav.
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.
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.
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.
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"
35 * Apply a simple delogo algorithm to the image in dst and put the
38 * The algorithm is only applied to the region specified by the logo
41 * @param w width of the input image
42 * @param h height of the input image
43 * @param logo_x x coordinate of the top left corner of the logo region
44 * @param logo_y y coordinate of the top left corner of the logo region
45 * @param logo_w width of the logo
46 * @param logo_h height of the logo
47 * @param band the size of the band around the processed area
48 * @param show show a rectangle around the processed area, useful for
50 * @param direct if non-zero perform in-place processing
52 static void apply_delogo(uint8_t *dst, int dst_linesize,
53 uint8_t *src, int src_linesize,
55 int logo_x, int logo_y, int logo_w, int logo_h,
56 int band, int show, int direct)
62 uint8_t *topleft, *botleft, *topright;
63 int xclipl, xclipr, yclipt, yclipb;
64 int logo_x1, logo_x2, logo_y1, logo_y2;
66 xclipl = FFMAX(-logo_x, 0);
67 xclipr = FFMAX(logo_x+logo_w-w, 0);
68 yclipt = FFMAX(-logo_y, 0);
69 yclipb = FFMAX(logo_y+logo_h-h, 0);
71 logo_x1 = logo_x + xclipl;
72 logo_x2 = logo_x + logo_w - xclipr;
73 logo_y1 = logo_y + yclipt;
74 logo_y2 = logo_y + logo_h - yclipb;
76 topleft = src+logo_y1 * src_linesize+logo_x1;
77 topright = src+logo_y1 * src_linesize+logo_x2-1;
78 botleft = src+(logo_y2-1) * src_linesize+logo_x1;
80 dst += (logo_y1+1)*dst_linesize;
81 src += (logo_y1+1)*src_linesize;
84 av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
86 for (y = logo_y1+1; y < logo_y2-1; y++) {
89 xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
90 interp = (topleft[src_linesize*(y-logo_y -yclipt)] +
91 topleft[src_linesize*(y-logo_y-1-yclipt)] +
92 topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
93 + (topright[src_linesize*(y-logo_y-yclipt)] +
94 topright[src_linesize*(y-logo_y-1-yclipt)] +
95 topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
96 + (topleft[x-logo_x-xclipl] +
97 topleft[x-logo_x-1-xclipl] +
98 topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
99 + (botleft[x-logo_x-xclipl] +
100 botleft[x-logo_x-1-xclipl] +
101 botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
104 if (y >= logo_y+band && y < logo_y+logo_h-band &&
105 x >= logo_x+band && x < logo_x+logo_w-band) {
110 dist = FFMAX(dist, logo_x-x+band);
111 else if (x >= logo_x+logo_w-band)
112 dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
115 dist = FFMAX(dist, logo_y-y+band);
116 else if (y >= logo_y+logo_h-band)
117 dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
119 *xdst = (*xsrc*dist + interp*(band-dist))/band;
120 if (show && (dist == band-1))
131 const AVClass *class;
132 int x, y, w, h, band, show;
135 #define OFFSET(x) offsetof(DelogoContext, x)
137 static const AVOption delogo_options[]= {
138 {"x", "set logo x position", OFFSET(x), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
139 {"y", "set logo y position", OFFSET(y), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
140 {"w", "set logo width", OFFSET(w), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
141 {"h", "set logo height", OFFSET(h), FF_OPT_TYPE_INT, {-1}, -1, INT_MAX },
142 {"band", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
143 {"t", "set delogo area band size", OFFSET(band), FF_OPT_TYPE_INT, { 4}, -1, INT_MAX },
144 {"show", "show delogo area", OFFSET(show), FF_OPT_TYPE_INT, { 0}, 0, 1 },
148 static const char *delogo_get_name(void *ctx)
153 static const AVClass delogo_class = {
154 .class_name = "DelogoContext",
155 .item_name = delogo_get_name,
156 .option = delogo_options,
159 static int query_formats(AVFilterContext *ctx)
161 enum PixelFormat pix_fmts[] = {
162 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
163 PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P,
164 PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
168 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
172 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
174 DelogoContext *delogo = ctx->priv;
177 delogo->class = &delogo_class;
178 av_opt_set_defaults(delogo);
181 ret = sscanf(args, "%d:%d:%d:%d:%d",
182 &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
184 if (delogo->band < 0)
186 } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0) {
187 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
191 #define CHECK_UNSET_OPT(opt) \
192 if (delogo->opt == -1) { \
193 av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
194 return AVERROR(EINVAL); \
204 av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
205 delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
207 delogo->w += delogo->band*2;
208 delogo->h += delogo->band*2;
209 delogo->x -= delogo->band;
210 delogo->y -= delogo->band;
215 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
217 AVFilterLink *outlink = inlink->dst->outputs[0];
218 AVFilterBufferRef *outpicref;
220 if (inpicref->perms & AV_PERM_PRESERVE) {
221 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
222 outlink->w, outlink->h);
223 avfilter_copy_buffer_ref_props(outpicref, inpicref);
224 outpicref->video->w = outlink->w;
225 outpicref->video->h = outlink->h;
227 outpicref = inpicref;
229 outlink->out_buf = outpicref;
230 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
233 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
235 static void end_frame(AVFilterLink *inlink)
237 DelogoContext *delogo = inlink->dst->priv;
238 AVFilterLink *outlink = inlink->dst->outputs[0];
239 AVFilterBufferRef *inpicref = inlink ->cur_buf;
240 AVFilterBufferRef *outpicref = outlink->out_buf;
241 int direct = inpicref == outpicref;
242 int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
243 int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
246 for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
247 int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
248 int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
250 apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
251 inpicref ->data[plane], inpicref ->linesize[plane],
252 inlink->w>>hsub, inlink->h>>vsub,
253 delogo->x>>hsub, delogo->y>>vsub,
254 delogo->w>>hsub, delogo->h>>vsub,
255 delogo->band>>FFMIN(hsub, vsub),
256 delogo->show, direct);
259 avfilter_draw_slice(outlink, 0, inlink->h, 1);
260 avfilter_end_frame(outlink);
261 avfilter_unref_buffer(inpicref);
263 avfilter_unref_buffer(outpicref);
266 AVFilter avfilter_vf_delogo = {
268 .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
269 .priv_size = sizeof(DelogoContext),
271 .query_formats = query_formats,
273 .inputs = (AVFilterPad[]) {{ .name = "default",
274 .type = AVMEDIA_TYPE_VIDEO,
275 .get_video_buffer = ff_null_get_video_buffer,
276 .start_frame = start_frame,
277 .draw_slice = null_draw_slice,
278 .end_frame = end_frame,
279 .min_perms = AV_PERM_WRITE | AV_PERM_READ,
280 .rej_perms = AV_PERM_PRESERVE },
282 .outputs = (AVFilterPad[]) {{ .name = "default",
283 .type = AVMEDIA_TYPE_VIDEO, },