h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_delogo.c
CommitLineData
b157be1f
SS
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
22/**
23 * @file
24 * A very simple tv station logo remover
25 * Ported from MPlayer libmpcodecs/vf_delogo.c.
26 */
27
1d9c2dc8 28#include "libavutil/common.h"
b157be1f
SS
29#include "libavutil/imgutils.h"
30#include "libavutil/opt.h"
31#include "libavutil/pixdesc.h"
32#include "avfilter.h"
b74a1da4 33#include "formats.h"
9d0bfc50 34#include "internal.h"
c04c533f 35#include "video.h"
b157be1f
SS
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 */
55static 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
b157be1f
SS
83 if (!direct)
84 av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
85
7194330b
AK
86 dst += (logo_y1 + 1) * dst_linesize;
87 src += (logo_y1 + 1) * src_linesize;
88
b157be1f
SS
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 = (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
96 + (topright[src_linesize*(y-logo_y-yclipt)] +
97 topright[src_linesize*(y-logo_y-1-yclipt)] +
98 topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
99 + (topleft[x-logo_x-xclipl] +
100 topleft[x-logo_x-1-xclipl] +
101 topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
102 + (botleft[x-logo_x-xclipl] +
103 botleft[x-logo_x-1-xclipl] +
104 botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
105 interp /= 6;
106
107 if (y >= logo_y+band && y < logo_y+logo_h-band &&
108 x >= logo_x+band && x < logo_x+logo_w-band) {
109 *xdst = interp;
110 } else {
111 dist = 0;
112 if (x < logo_x+band)
113 dist = FFMAX(dist, logo_x-x+band);
114 else if (x >= logo_x+logo_w-band)
115 dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
116
117 if (y < logo_y+band)
118 dist = FFMAX(dist, logo_y-y+band);
119 else if (y >= logo_y+logo_h-band)
120 dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
121
122 *xdst = (*xsrc*dist + interp*(band-dist))/band;
123 if (show && (dist == band-1))
124 *xdst = 0;
125 }
126 }
127
128 dst += dst_linesize;
129 src += src_linesize;
130 }
131}
132
58400ac1 133typedef struct DelogoContext {
b157be1f
SS
134 const AVClass *class;
135 int x, y, w, h, band, show;
136} DelogoContext;
137
138#define OFFSET(x) offsetof(DelogoContext, x)
63e58c55 139#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
b157be1f
SS
140
141static const AVOption delogo_options[]= {
63e58c55
AK
142 { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
143 { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
144 { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
145 { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
146 { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
147 { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS },
148 { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
149 { NULL },
b157be1f
SS
150};
151
152static const char *delogo_get_name(void *ctx)
153{
154 return "delogo";
155}
156
157static const AVClass delogo_class = {
158 .class_name = "DelogoContext",
159 .item_name = delogo_get_name,
160 .option = delogo_options,
161};
162
163static int query_formats(AVFilterContext *ctx)
164{
716d413c
AK
165 enum AVPixelFormat pix_fmts[] = {
166 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
167 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
168 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
169 AV_PIX_FMT_NONE
b157be1f
SS
170 };
171
b74a1da4 172 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
b157be1f
SS
173 return 0;
174}
175
c43a7eca 176static av_cold int init(AVFilterContext *ctx)
b157be1f 177{
ba09675f 178 DelogoContext *s = ctx->priv;
b157be1f
SS
179
180#define CHECK_UNSET_OPT(opt) \
ba09675f
AK
181 if (s->opt == -1) { \
182 av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
b157be1f
SS
183 return AVERROR(EINVAL); \
184 }
185 CHECK_UNSET_OPT(x);
186 CHECK_UNSET_OPT(y);
187 CHECK_UNSET_OPT(w);
188 CHECK_UNSET_OPT(h);
189
ba09675f
AK
190 if (s->show)
191 s->band = 4;
b157be1f
SS
192
193 av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
ba09675f 194 s->x, s->y, s->w, s->h, s->band, s->show);
b157be1f 195
ba09675f
AK
196 s->w += s->band*2;
197 s->h += s->band*2;
198 s->x -= s->band;
199 s->y -= s->band;
b157be1f
SS
200
201 return 0;
202}
203
7e350379 204static int filter_frame(AVFilterLink *inlink, AVFrame *in)
b157be1f 205{
ba09675f 206 DelogoContext *s = inlink->dst->priv;
b157be1f 207 AVFilterLink *outlink = inlink->dst->outputs[0];
59ee9f78 208 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
7e350379 209 AVFrame *out;
59ee9f78
AK
210 int hsub0 = desc->log2_chroma_w;
211 int vsub0 = desc->log2_chroma_h;
f81c37e4 212 int direct = 0;
b157be1f
SS
213 int plane;
214
7e350379 215 if (av_frame_is_writable(in)) {
565e4993
AK
216 direct = 1;
217 out = in;
218 } else {
7e350379 219 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 220 if (!out) {
7e350379 221 av_frame_free(&in);
565e4993
AK
222 return AVERROR(ENOMEM);
223 }
224
7e350379
AK
225 av_frame_copy_props(out, in);
226 out->width = outlink->w;
227 out->height = outlink->h;
565e4993
AK
228 }
229
230 for (plane = 0; plane < 4 && in->data[plane]; plane++) {
b157be1f
SS
231 int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
232 int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
233
565e4993
AK
234 apply_delogo(out->data[plane], out->linesize[plane],
235 in ->data[plane], in ->linesize[plane],
b157be1f 236 inlink->w>>hsub, inlink->h>>vsub,
ba09675f
AK
237 s->x>>hsub, s->y>>vsub,
238 s->w>>hsub, s->h>>vsub,
239 s->band>>FFMIN(hsub, vsub),
240 s->show, direct);
b157be1f
SS
241 }
242
565e4993 243 if (!direct)
7e350379 244 av_frame_free(&in);
565e4993
AK
245
246 return ff_filter_frame(outlink, out);
b157be1f
SS
247}
248
568c70e7
MR
249static const AVFilterPad avfilter_vf_delogo_inputs[] = {
250 {
251 .name = "default",
252 .type = AVMEDIA_TYPE_VIDEO,
253 .get_video_buffer = ff_null_get_video_buffer,
565e4993 254 .filter_frame = filter_frame,
568c70e7
MR
255 },
256 { NULL }
257};
258
259static const AVFilterPad avfilter_vf_delogo_outputs[] = {
260 {
261 .name = "default",
262 .type = AVMEDIA_TYPE_VIDEO,
263 },
264 { NULL }
265};
266
cd43ca04 267AVFilter ff_vf_delogo = {
b157be1f
SS
268 .name = "delogo",
269 .description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
270 .priv_size = sizeof(DelogoContext),
63e58c55 271 .priv_class = &delogo_class,
b157be1f
SS
272 .init = init,
273 .query_formats = query_formats,
274
568c70e7
MR
275 .inputs = avfilter_vf_delogo_inputs,
276 .outputs = avfilter_vf_delogo_outputs,
b157be1f 277};