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