diff options
Diffstat (limited to 'libavfilter/vf_delogo.c')
-rw-r--r-- | libavfilter/vf_delogo.c | 170 |
1 files changed, 101 insertions, 69 deletions
diff --git a/libavfilter/vf_delogo.c b/libavfilter/vf_delogo.c index dc58078d04..63c3539467 100644 --- a/libavfilter/vf_delogo.c +++ b/libavfilter/vf_delogo.c @@ -1,28 +1,30 @@ /* * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com> * Copyright (c) 2011 Stefano Sabatini + * Copyright (c) 2013, 2015 Jean Delvare <jdelvare@suse.com> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or modify + * FFmpeg is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along - * with Libav; if not, write to the Free Software Foundation, Inc., + * with FFmpeg; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /** * @file * A very simple tv station logo remover - * Ported from MPlayer libmpcodecs/vf_delogo.c. + * Originally imported from MPlayer libmpcodecs/vf_delogo.c, + * the algorithm was later improved. */ #include "libavutil/common.h" @@ -35,8 +37,8 @@ #include "video.h" /** - * Apply a simple delogo algorithm to the image in dst and put the - * result in src. + * Apply a simple delogo algorithm to the image in src and put the + * result in dst. * * The algorithm is only applied to the region specified by the logo * parameters. @@ -54,15 +56,16 @@ */ static void apply_delogo(uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize, - int w, int h, + int w, int h, AVRational sar, int logo_x, int logo_y, int logo_w, int logo_h, - int band, int show, int direct) + unsigned int band, int show, int direct) { int x, y; - int interp, dist; + uint64_t interp, weightl, weightr, weightt, weightb, weight; uint8_t *xdst, *xsrc; uint8_t *topleft, *botleft, *topright; + unsigned int left_sample, right_sample; int xclipl, xclipr, yclipt, yclipb; int logo_x1, logo_x2, logo_y1, logo_y2; @@ -72,13 +75,13 @@ static void apply_delogo(uint8_t *dst, int dst_linesize, yclipb = FFMAX(logo_y+logo_h-h, 0); logo_x1 = logo_x + xclipl; - logo_x2 = logo_x + logo_w - xclipr; + logo_x2 = logo_x + logo_w - xclipr - 1; logo_y1 = logo_y + yclipt; - logo_y2 = logo_y + logo_h - yclipb; + logo_y2 = logo_y + logo_h - yclipb - 1; - topleft = src+logo_y1 * src_linesize+logo_x1; - topright = src+logo_y1 * src_linesize+logo_x2-1; - botleft = src+(logo_y2-1) * src_linesize+logo_x1; + topleft = src+logo_y1 * src_linesize+logo_x1; + topright = src+logo_y1 * src_linesize+logo_x2; + botleft = src+logo_y2 * src_linesize+logo_x1; if (!direct) av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h); @@ -86,29 +89,51 @@ static void apply_delogo(uint8_t *dst, int dst_linesize, dst += (logo_y1 + 1) * dst_linesize; src += (logo_y1 + 1) * src_linesize; - for (y = logo_y1+1; y < logo_y2-1; y++) { + for (y = logo_y1+1; y < logo_y2; y++) { + left_sample = topleft[src_linesize*(y-logo_y1)] + + topleft[src_linesize*(y-logo_y1-1)] + + topleft[src_linesize*(y-logo_y1+1)]; + right_sample = topright[src_linesize*(y-logo_y1)] + + topright[src_linesize*(y-logo_y1-1)] + + topright[src_linesize*(y-logo_y1+1)]; + for (x = logo_x1+1, xdst = dst+logo_x1+1, - xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) { - interp = (topleft[src_linesize*(y-logo_y -yclipt)] + - topleft[src_linesize*(y-logo_y-1-yclipt)] + - topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w - + (topright[src_linesize*(y-logo_y-yclipt)] + - topright[src_linesize*(y-logo_y-1-yclipt)] + - topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w - + (topleft[x-logo_x-xclipl] + - topleft[x-logo_x-1-xclipl] + - topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h - + (botleft[x-logo_x-xclipl] + - botleft[x-logo_x-1-xclipl] + - botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h; - interp /= 6; + xsrc = src+logo_x1+1; x < logo_x2; x++, xdst++, xsrc++) { + + if (show && (y == logo_y1+1 || y == logo_y2-1 || + x == logo_x1+1 || x == logo_x2-1)) { + *xdst = 0; + continue; + } + + /* Weighted interpolation based on relative distances, taking SAR into account */ + weightl = (uint64_t) (logo_x2-x) * (y-logo_y1) * (logo_y2-y) * sar.den; + weightr = (uint64_t)(x-logo_x1) * (y-logo_y1) * (logo_y2-y) * sar.den; + weightt = (uint64_t)(x-logo_x1) * (logo_x2-x) * (logo_y2-y) * sar.num; + weightb = (uint64_t)(x-logo_x1) * (logo_x2-x) * (y-logo_y1) * sar.num; + + interp = + left_sample * weightl + + + right_sample * weightr + + + (topleft[x-logo_x1] + + topleft[x-logo_x1-1] + + topleft[x-logo_x1+1]) * weightt + + + (botleft[x-logo_x1] + + botleft[x-logo_x1-1] + + botleft[x-logo_x1+1]) * weightb; + weight = (weightl + weightr + weightt + weightb) * 3U; + interp = ROUNDED_DIV(interp, weight); if (y >= logo_y+band && y < logo_y+logo_h-band && x >= logo_x+band && x < logo_x+logo_w-band) { *xdst = interp; } else { - dist = 0; + unsigned dist = 0; + if (x < logo_x+band) dist = FFMAX(dist, logo_x-x+band); else if (x >= logo_x+logo_w-band) @@ -120,8 +145,6 @@ static void apply_delogo(uint8_t *dst, int dst_linesize, dist = FFMAX(dist, y-(logo_y+logo_h-1-band)); *xdst = (*xsrc*dist + interp*(band-dist))/band; - if (show && (dist == band-1)) - *xdst = 0; } } @@ -136,41 +159,36 @@ typedef struct DelogoContext { } DelogoContext; #define OFFSET(x) offsetof(DelogoContext, x) -#define FLAGS AV_OPT_FLAG_VIDEO_PARAM +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM static const AVOption delogo_options[]= { { "x", "set logo x position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS }, { "y", "set logo y position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS }, { "w", "set logo width", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS }, { "h", "set logo height", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS }, - { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS }, - { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 4 }, -1, INT_MAX, FLAGS }, - { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS }, - { NULL }, +#if LIBAVFILTER_VERSION_MAJOR < 7 + /* Actual default value for band/t is 1, set in init */ + { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, + { "t", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, +#endif + { "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, FLAGS }, + { NULL } }; -static const char *delogo_get_name(void *ctx) -{ - return "delogo"; -} - -static const AVClass delogo_class = { - .class_name = "DelogoContext", - .item_name = delogo_get_name, - .option = delogo_options, -}; +AVFILTER_DEFINE_CLASS(delogo); static int query_formats(AVFilterContext *ctx) { - enum AVPixelFormat pix_fmts[] = { + static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE }; - - ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); - return 0; + AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); + if (!fmts_list) + return AVERROR(ENOMEM); + return ff_set_common_formats(ctx, fmts_list); } static av_cold int init(AVFilterContext *ctx) @@ -187,10 +205,17 @@ static av_cold int init(AVFilterContext *ctx) CHECK_UNSET_OPT(w); CHECK_UNSET_OPT(h); - if (s->show) - s->band = 4; - - av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n", +#if LIBAVFILTER_VERSION_MAJOR < 7 + if (s->band == 0) { /* Unset, use default */ + av_log(ctx, AV_LOG_WARNING, "Note: default band value was changed from 4 to 1.\n"); + s->band = 1; + } else if (s->band != 1) { + av_log(ctx, AV_LOG_WARNING, "Option band is deprecated.\n"); + } +#else + s->band = 1; +#endif + av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n", s->x, s->y, s->w, s->h, s->band, s->show); s->w += s->band*2; @@ -211,6 +236,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) int vsub0 = desc->log2_chroma_h; int direct = 0; int plane; + AVRational sar; if (av_frame_is_writable(in)) { direct = 1; @@ -223,19 +249,26 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) } av_frame_copy_props(out, in); - out->width = outlink->w; - out->height = outlink->h; } - for (plane = 0; plane < 4 && in->data[plane]; plane++) { + sar = in->sample_aspect_ratio; + /* Assume square pixels if SAR is unknown */ + if (!sar.num) + sar.num = sar.den = 1; + + for (plane = 0; plane < desc->nb_components; plane++) { int hsub = plane == 1 || plane == 2 ? hsub0 : 0; int vsub = plane == 1 || plane == 2 ? vsub0 : 0; apply_delogo(out->data[plane], out->linesize[plane], in ->data[plane], in ->linesize[plane], - inlink->w>>hsub, inlink->h>>vsub, - s->x>>hsub, s->y>>vsub, - s->w>>hsub, s->h>>vsub, + AV_CEIL_RSHIFT(inlink->w, hsub), + AV_CEIL_RSHIFT(inlink->h, vsub), + sar, s->x>>hsub, s->y>>vsub, + /* Up and left borders were rounded down, inject lost bits + * into width and height to avoid error accumulation */ + AV_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub), + AV_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub), s->band>>FFMIN(hsub, vsub), s->show, direct); } @@ -248,10 +281,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) static const AVFilterPad avfilter_vf_delogo_inputs[] = { { - .name = "default", - .type = AVMEDIA_TYPE_VIDEO, - .get_video_buffer = ff_null_get_video_buffer, - .filter_frame = filter_frame, + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, }, { NULL } }; @@ -271,7 +303,7 @@ AVFilter ff_vf_delogo = { .priv_class = &delogo_class, .init = init, .query_formats = query_formats, - - .inputs = avfilter_vf_delogo_inputs, - .outputs = avfilter_vf_delogo_outputs, + .inputs = avfilter_vf_delogo_inputs, + .outputs = avfilter_vf_delogo_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, }; |