]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_delogo.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_delogo.c
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/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "video.h"
33
34 /**
35  * Apply a simple delogo algorithm to the image in dst and put the
36  * result in src.
37  *
38  * The algorithm is only applied to the region specified by the logo
39  * parameters.
40  *
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
49  *               parameters tweaking
50  * @param direct if non-zero perform in-place processing
51  */
52 static void apply_delogo(uint8_t *dst, int dst_linesize,
53                          uint8_t *src, int src_linesize,
54                          int w, int h,
55                          int logo_x, int logo_y, int logo_w, int logo_h,
56                          int band, int show, int direct)
57 {
58     int x, y;
59     int interp, dist;
60     uint8_t *xdst, *xsrc;
61
62     uint8_t *topleft, *botleft, *topright;
63     int xclipl, xclipr, yclipt, yclipb;
64     int logo_x1, logo_x2, logo_y1, logo_y2;
65
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);
70
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;
75
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;
79
80     dst += (logo_y1+1)*dst_linesize;
81     src += (logo_y1+1)*src_linesize;
82
83     if (!direct)
84         av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
85
86     for (y = logo_y1+1; y < logo_y2-1; y++) {
87         for (x = logo_x1+1,
88              xdst = dst+logo_x1+1,
89              xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
90             interp =
91                 (topleft[src_linesize*(y-logo_y  -yclipt)]   +
92                  topleft[src_linesize*(y-logo_y-1-yclipt)]   +
93                  topleft[src_linesize*(y-logo_y+1-yclipt)])  * (logo_w-(x-logo_x))/logo_w
94                 +
95                 (topright[src_linesize*(y-logo_y-yclipt)]    +
96                  topright[src_linesize*(y-logo_y-1-yclipt)]  +
97                  topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
98                 +
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                 +
103                 (botleft[x-logo_x-xclipl]    +
104                  botleft[x-logo_x-1-xclipl]  +
105                  botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
106             interp /= 6;
107
108             if (y >= logo_y+band && y < logo_y+logo_h-band &&
109                 x >= logo_x+band && x < logo_x+logo_w-band) {
110                 *xdst = interp;
111             } else {
112                 dist = 0;
113                 if      (x < logo_x+band)
114                     dist = FFMAX(dist, logo_x-x+band);
115                 else if (x >= logo_x+logo_w-band)
116                     dist = FFMAX(dist, x-(logo_x+logo_w-1-band));
117
118                 if      (y < logo_y+band)
119                     dist = FFMAX(dist, logo_y-y+band);
120                 else if (y >= logo_y+logo_h-band)
121                     dist = FFMAX(dist, y-(logo_y+logo_h-1-band));
122
123                 *xdst = (*xsrc*dist + interp*(band-dist))/band;
124                 if (show && (dist == band-1))
125                     *xdst = 0;
126             }
127         }
128
129         dst += dst_linesize;
130         src += src_linesize;
131     }
132 }
133
134 typedef struct {
135     const AVClass *class;
136     int x, y, w, h, band, show;
137 }  DelogoContext;
138
139 #define OFFSET(x) offsetof(DelogoContext, x)
140
141 static const AVOption delogo_options[]= {
142     {"x",    "set logo x position",       OFFSET(x),    AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
143     {"y",    "set logo y position",       OFFSET(y),    AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
144     {"w",    "set logo width",            OFFSET(w),    AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
145     {"h",    "set logo height",           OFFSET(h),    AV_OPT_TYPE_INT, {.dbl=-1}, -1, INT_MAX },
146     {"band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
147     {"t",    "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.dbl= 4}, -1, INT_MAX },
148     {"show", "show delogo area",          OFFSET(show), AV_OPT_TYPE_INT, {.dbl= 0},  0, 1       },
149     {NULL},
150 };
151
152 static const char *delogo_get_name(void *ctx)
153 {
154     return "delogo";
155 }
156
157 static const AVClass delogo_class = {
158     .class_name = "DelogoContext",
159     .item_name  = delogo_get_name,
160     .option     = delogo_options,
161 };
162
163 static int query_formats(AVFilterContext *ctx)
164 {
165     enum PixelFormat pix_fmts[] = {
166         PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
167         PIX_FMT_YUV411P,  PIX_FMT_YUV410P,  PIX_FMT_YUV440P,
168         PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
169         PIX_FMT_NONE
170     };
171
172     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
173     return 0;
174 }
175
176 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
177 {
178     DelogoContext *delogo = ctx->priv;
179     int ret = 0;
180
181     delogo->class = &delogo_class;
182     av_opt_set_defaults(delogo);
183
184     if (args)
185         ret = sscanf(args, "%d:%d:%d:%d:%d",
186                      &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
187     if (ret == 5) {
188         if (delogo->band < 0)
189             delogo->show = 1;
190     } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0) {
191         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
192         return ret;
193     }
194
195 #define CHECK_UNSET_OPT(opt)                                            \
196     if (delogo->opt == -1) {                                            \
197         av_log(delogo, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
198         return AVERROR(EINVAL);                                         \
199     }
200     CHECK_UNSET_OPT(x);
201     CHECK_UNSET_OPT(y);
202     CHECK_UNSET_OPT(w);
203     CHECK_UNSET_OPT(h);
204
205     if (delogo->show)
206         delogo->band = 4;
207
208     av_log(ctx, AV_LOG_INFO, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
209            delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
210
211     delogo->w += delogo->band*2;
212     delogo->h += delogo->band*2;
213     delogo->x -= delogo->band;
214     delogo->y -= delogo->band;
215
216     return 0;
217 }
218
219 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
220 {
221     AVFilterLink *outlink = inlink->dst->outputs[0];
222     AVFilterBufferRef *outpicref;
223
224     if (inpicref->perms & AV_PERM_PRESERVE) {
225         outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
226                                               outlink->w, outlink->h);
227         avfilter_copy_buffer_ref_props(outpicref, inpicref);
228         outpicref->video->w = outlink->w;
229         outpicref->video->h = outlink->h;
230     } else
231         outpicref = inpicref;
232
233     outlink->out_buf = outpicref;
234     avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
235 }
236
237 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
238
239 static void end_frame(AVFilterLink *inlink)
240 {
241     DelogoContext *delogo = inlink->dst->priv;
242     AVFilterLink *outlink = inlink->dst->outputs[0];
243     AVFilterBufferRef *inpicref  = inlink ->cur_buf;
244     AVFilterBufferRef *outpicref = outlink->out_buf;
245     int direct = inpicref == outpicref;
246     int hsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
247     int vsub0 = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
248     int plane;
249
250     for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
251         int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
252         int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
253
254         apply_delogo(outpicref->data[plane], outpicref->linesize[plane],
255                      inpicref ->data[plane], inpicref ->linesize[plane],
256                      inlink->w>>hsub, inlink->h>>vsub,
257                      delogo->x>>hsub, delogo->y>>vsub,
258                      delogo->w>>hsub, delogo->h>>vsub,
259                      delogo->band>>FFMIN(hsub, vsub),
260                      delogo->show, direct);
261     }
262
263     avfilter_draw_slice(outlink, 0, inlink->h, 1);
264     avfilter_end_frame(outlink);
265     avfilter_unref_buffer(inpicref);
266     if (!direct)
267         avfilter_unref_buffer(outpicref);
268 }
269
270 AVFilter avfilter_vf_delogo = {
271     .name          = "delogo",
272     .description   = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
273     .priv_size     = sizeof(DelogoContext),
274     .init          = init,
275     .query_formats = query_formats,
276
277     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
278                                     .type             = AVMEDIA_TYPE_VIDEO,
279                                     .get_video_buffer = ff_null_get_video_buffer,
280                                     .start_frame      = start_frame,
281                                     .draw_slice       = null_draw_slice,
282                                     .end_frame        = end_frame,
283                                     .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
284                                     .rej_perms        = AV_PERM_PRESERVE },
285                                   { .name = NULL}},
286     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
287                                     .type             = AVMEDIA_TYPE_VIDEO, },
288                                   { .name = NULL}},
289 };