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