vf_delogo.c 9.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
 * Copyright (c) 2011 Stefano Sabatini
 *
 * This file is part of Libav.
 *
 * Libav 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,
 * 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.,
 * 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.
 */

28
#include "libavutil/common.h"
29 30 31 32
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
33
#include "formats.h"
34
#include "internal.h"
35
#include "video.h"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

/**
 * Apply a simple delogo algorithm to the image in dst and put the
 * result in src.
 *
 * The algorithm is only applied to the region specified by the logo
 * parameters.
 *
 * @param w      width of the input image
 * @param h      height of the input image
 * @param logo_x x coordinate of the top left corner of the logo region
 * @param logo_y y coordinate of the top left corner of the logo region
 * @param logo_w width of the logo
 * @param logo_h height of the logo
 * @param band   the size of the band around the processed area
 * @param show   show a rectangle around the processed area, useful for
 *               parameters tweaking
 * @param direct if non-zero perform in-place processing
 */
static void apply_delogo(uint8_t *dst, int dst_linesize,
                         uint8_t *src, int src_linesize,
                         int w, int h,
                         int logo_x, int logo_y, int logo_w, int logo_h,
                         int band, int show, int direct)
{
    int x, y;
    int interp, dist;
    uint8_t *xdst, *xsrc;

    uint8_t *topleft, *botleft, *topright;
    int xclipl, xclipr, yclipt, yclipb;
    int logo_x1, logo_x2, logo_y1, logo_y2;

    xclipl = FFMAX(-logo_x, 0);
    xclipr = FFMAX(logo_x+logo_w-w, 0);
    yclipt = FFMAX(-logo_y, 0);
    yclipb = FFMAX(logo_y+logo_h-h, 0);

    logo_x1 = logo_x + xclipl;
    logo_x2 = logo_x + logo_w - xclipr;
    logo_y1 = logo_y + yclipt;
    logo_y2 = logo_y + logo_h - yclipb;

    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;

    if (!direct)
        av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);

86 87 88
    dst += (logo_y1 + 1) * dst_linesize;
    src += (logo_y1 + 1) * src_linesize;

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    for (y = logo_y1+1; y < logo_y2-1; y++) {
        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;

            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;
                if      (x < logo_x+band)
                    dist = FFMAX(dist, logo_x-x+band);
                else if (x >= logo_x+logo_w-band)
                    dist = FFMAX(dist, x-(logo_x+logo_w-1-band));

                if      (y < logo_y+band)
                    dist = FFMAX(dist, logo_y-y+band);
                else if (y >= logo_y+logo_h-band)
                    dist = FFMAX(dist, y-(logo_y+logo_h-1-band));

                *xdst = (*xsrc*dist + interp*(band-dist))/band;
                if (show && (dist == band-1))
                    *xdst = 0;
            }
        }

        dst += dst_linesize;
        src += src_linesize;
    }
}

133
typedef struct DelogoContext {
134 135 136 137 138
    const AVClass *class;
    int x, y, w, h, band, show;
}  DelogoContext;

#define OFFSET(x) offsetof(DelogoContext, x)
139
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
140 141

static const AVOption delogo_options[]= {
142 143 144 145 146 147 148 149
    { "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 },
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
};

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,
};

static int query_formats(AVFilterContext *ctx)
{
165 166 167 168 169
    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
170 171
    };

172
    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
173 174 175
    return 0;
}

176
static av_cold int init(AVFilterContext *ctx)
177
{
178
    DelogoContext *s = ctx->priv;
179 180

#define CHECK_UNSET_OPT(opt)                                            \
181 182
    if (s->opt == -1) {                                            \
        av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \
183 184 185 186 187 188 189
        return AVERROR(EINVAL);                                         \
    }
    CHECK_UNSET_OPT(x);
    CHECK_UNSET_OPT(y);
    CHECK_UNSET_OPT(w);
    CHECK_UNSET_OPT(h);

190 191
    if (s->show)
        s->band = 4;
192 193

    av_log(ctx, AV_LOG_DEBUG, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
194
           s->x, s->y, s->w, s->h, s->band, s->show);
195

196 197 198 199
    s->w += s->band*2;
    s->h += s->band*2;
    s->x -= s->band;
    s->y -= s->band;
200 201 202 203

    return 0;
}

204
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
205
{
206
    DelogoContext *s = inlink->dst->priv;
207
    AVFilterLink *outlink = inlink->dst->outputs[0];
208
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
209
    AVFrame *out;
210 211
    int hsub0 = desc->log2_chroma_w;
    int vsub0 = desc->log2_chroma_h;
212
    int direct = 0;
213 214
    int plane;

215
    if (av_frame_is_writable(in)) {
216 217 218
        direct = 1;
        out = in;
    } else {
219
        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
220
        if (!out) {
221
            av_frame_free(&in);
222 223 224
            return AVERROR(ENOMEM);
        }

225 226 227
        av_frame_copy_props(out, in);
        out->width  = outlink->w;
        out->height = outlink->h;
228 229 230
    }

    for (plane = 0; plane < 4 && in->data[plane]; plane++) {
231 232 233
        int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
        int vsub = plane == 1 || plane == 2 ? vsub0 : 0;

234 235
        apply_delogo(out->data[plane], out->linesize[plane],
                     in ->data[plane], in ->linesize[plane],
236
                     inlink->w>>hsub, inlink->h>>vsub,
237 238 239 240
                     s->x>>hsub, s->y>>vsub,
                     s->w>>hsub, s->h>>vsub,
                     s->band>>FFMIN(hsub, vsub),
                     s->show, direct);
241 242
    }

243
    if (!direct)
244
        av_frame_free(&in);
245 246

    return ff_filter_frame(outlink, out);
247 248
}

249 250 251 252 253
static const AVFilterPad avfilter_vf_delogo_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .get_video_buffer = ff_null_get_video_buffer,
254
        .filter_frame     = filter_frame,
255 256 257 258 259 260 261 262 263 264 265 266
    },
    { NULL }
};

static const AVFilterPad avfilter_vf_delogo_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

267
AVFilter ff_vf_delogo = {
268 269 270
    .name          = "delogo",
    .description   = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
    .priv_size     = sizeof(DelogoContext),
271
    .priv_class    = &delogo_class,
272 273 274
    .init          = init,
    .query_formats = query_formats,

275 276
    .inputs    = avfilter_vf_delogo_inputs,
    .outputs   = avfilter_vf_delogo_outputs,
277
};