OSDN Git Service

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