2701f6cc |
/*
* Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
* Copyright (c) 2011 Stefano Sabatini |
3e5b02bd |
* Copyright (c) 2013, 2015 Jean Delvare <jdelvare@suse.com> |
2701f6cc |
*
* This file is part of FFmpeg.
*
* 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.
*
* 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 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 |
16fd75ce |
* Originally imported from MPlayer libmpcodecs/vf_delogo.c,
* the algorithm was later improved. |
2701f6cc |
*/
|
1d9c2dc8 |
#include "libavutil/common.h" |
2701f6cc |
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h" |
b74a1da4 |
#include "formats.h" |
9d0bfc50 |
#include "internal.h" |
c04c533f |
#include "video.h" |
2701f6cc |
/** |
b08cd2fb |
* Apply a simple delogo algorithm to the image in src and put the
* result in dst. |
2701f6cc |
*
* 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, |
99f1d749 |
int w, int h, AVRational sar, |
2701f6cc |
int logo_x, int logo_y, int logo_w, int logo_h, |
b6d0bb60 |
unsigned int band, int show, int direct) |
2701f6cc |
{ |
b6d0bb60 |
int x, y; |
e74f1a12 |
uint64_t interp, weightl, weightr, weightt, weightb, weight; |
2701f6cc |
uint8_t *xdst, *xsrc;
uint8_t *topleft, *botleft, *topright; |
4e8d6b31 |
unsigned int left_sample, right_sample; |
2701f6cc |
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; |
47b2ba98 |
logo_x2 = logo_x + logo_w - xclipr - 1; |
2701f6cc |
logo_y1 = logo_y + yclipt; |
47b2ba98 |
logo_y2 = logo_y + logo_h - yclipb - 1; |
2701f6cc |
|
47b2ba98 |
topleft = src+logo_y1 * src_linesize+logo_x1;
topright = src+logo_y1 * src_linesize+logo_x2;
botleft = src+logo_y2 * src_linesize+logo_x1; |
2701f6cc |
if (!direct)
av_image_copy_plane(dst, dst_linesize, src, src_linesize, w, h);
|
7194330b |
dst += (logo_y1 + 1) * dst_linesize;
src += (logo_y1 + 1) * src_linesize; |
46a4e72b |
|
47b2ba98 |
for (y = logo_y1+1; y < logo_y2; y++) { |
4e8d6b31 |
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)];
|
2701f6cc |
for (x = logo_x1+1,
xdst = dst+logo_x1+1, |
47b2ba98 |
xsrc = src+logo_x1+1; x < logo_x2; x++, xdst++, xsrc++) { |
16fd75ce |
|
47b2ba98 |
if (show && (y == logo_y1+1 || y == logo_y2-1 ||
x == logo_x1+1 || x == logo_x2-1)) { |
3e5b02bd |
*xdst = 0;
continue;
}
|
99f1d749 |
/* Weighted interpolation based on relative distances, taking SAR into account */ |
47b2ba98 |
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; |
16fd75ce |
|
2701f6cc |
interp = |
4e8d6b31 |
left_sample * weightl |
2701f6cc |
+ |
4e8d6b31 |
right_sample * weightr |
2701f6cc |
+ |
6fc8c4cc |
(topleft[x-logo_x1] +
topleft[x-logo_x1-1] +
topleft[x-logo_x1+1]) * weightt |
2701f6cc |
+ |
6fc8c4cc |
(botleft[x-logo_x1] +
botleft[x-logo_x1-1] +
botleft[x-logo_x1+1]) * weightb; |
e74f1a12 |
weight = (weightl + weightr + weightt + weightb) * 3U;
interp = ROUNDED_DIV(interp, weight); |
2701f6cc |
if (y >= logo_y+band && y < logo_y+logo_h-band &&
x >= logo_x+band && x < logo_x+logo_w-band) {
*xdst = interp;
} else { |
b6d0bb60 |
unsigned dist = 0;
|
2701f6cc |
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;
}
}
dst += dst_linesize;
src += src_linesize;
}
}
|
58400ac1 |
typedef struct DelogoContext { |
2701f6cc |
const AVClass *class;
int x, y, w, h, band, show;
} DelogoContext;
#define OFFSET(x) offsetof(DelogoContext, x) |
42d621d1 |
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
2701f6cc |
static const AVOption delogo_options[]= { |
63e58c55 |
{ "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 }, |
8bc708fc |
#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 |
ee4f0ec0 |
{ "show", "show delogo area", OFFSET(show), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, FLAGS }, |
b211607b |
{ NULL } |
2701f6cc |
};
|
c17808ce |
AVFILTER_DEFINE_CLASS(delogo); |
2701f6cc |
static int query_formats(AVFilterContext *ctx)
{ |
185d1f3b |
static const enum AVPixelFormat pix_fmts[] = { |
716d413c |
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 |
2701f6cc |
}; |
fd682b18 |
AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
if (!fmts_list)
return AVERROR(ENOMEM);
return ff_set_common_formats(ctx, fmts_list); |
2701f6cc |
}
|
d69a4177 |
static av_cold int init(AVFilterContext *ctx) |
2701f6cc |
{ |
ba09675f |
DelogoContext *s = ctx->priv; |
2701f6cc |
#define CHECK_UNSET_OPT(opt) \ |
ba09675f |
if (s->opt == -1) { \
av_log(s, AV_LOG_ERROR, "Option %s was not set.\n", #opt); \ |
2701f6cc |
return AVERROR(EINVAL); \
}
CHECK_UNSET_OPT(x);
CHECK_UNSET_OPT(y);
CHECK_UNSET_OPT(w);
CHECK_UNSET_OPT(h);
|
8bc708fc |
#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 |
fda968aa |
av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n", |
ba09675f |
s->x, s->y, s->w, s->h, s->band, s->show); |
2701f6cc |
|
ba09675f |
s->w += s->band*2;
s->h += s->band*2;
s->x -= s->band;
s->y -= s->band; |
2701f6cc |
return 0;
}
|
aeefe018 |
static int config_input(AVFilterLink *inlink)
{
DelogoContext *s = inlink->dst->priv;
/* Check whether the logo area fits in the frame */
if (s->x + (s->band - 1) < 0 || s->x + s->w - (s->band*2 - 2) > inlink->w ||
s->y + (s->band - 1) < 0 || s->y + s->h - (s->band*2 - 2) > inlink->h) {
av_log(s, AV_LOG_ERROR, "Logo area is outside of the frame.\n");
return AVERROR(EINVAL);
}
return 0;
}
|
7e350379 |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
2701f6cc |
{ |
ba09675f |
DelogoContext *s = inlink->dst->priv; |
2701f6cc |
AVFilterLink *outlink = inlink->dst->outputs[0]; |
59ee9f78 |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
7e350379 |
AVFrame *out; |
59ee9f78 |
int hsub0 = desc->log2_chroma_w;
int vsub0 = desc->log2_chroma_h; |
38464146 |
int direct = 0; |
2701f6cc |
int plane; |
99f1d749 |
AVRational sar; |
2701f6cc |
|
7e350379 |
if (av_frame_is_writable(in)) { |
38464146 |
direct = 1;
out = in;
} else { |
7e350379 |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
38464146 |
if (!out) { |
7e350379 |
av_frame_free(&in); |
38464146 |
return AVERROR(ENOMEM);
} |
565e4993 |
|
7e350379 |
av_frame_copy_props(out, in); |
38464146 |
}
|
99f1d749 |
sar = in->sample_aspect_ratio;
/* Assume square pixels if SAR is unknown */
if (!sar.num)
sar.num = sar.den = 1;
|
ec11cfdf |
for (plane = 0; plane < desc->nb_components; plane++) { |
2701f6cc |
int hsub = plane == 1 || plane == 2 ? hsub0 : 0;
int vsub = plane == 1 || plane == 2 ? vsub0 : 0;
|
38464146 |
apply_delogo(out->data[plane], out->linesize[plane],
in ->data[plane], in ->linesize[plane], |
21f94684 |
AV_CEIL_RSHIFT(inlink->w, hsub),
AV_CEIL_RSHIFT(inlink->h, vsub), |
99f1d749 |
sar, s->x>>hsub, s->y>>vsub, |
f0bcb13a |
/* Up and left borders were rounded down, inject lost bits
* into width and height to avoid error accumulation */ |
21f94684 |
AV_CEIL_RSHIFT(s->w + (s->x & ((1<<hsub)-1)), hsub),
AV_CEIL_RSHIFT(s->h + (s->y & ((1<<vsub)-1)), vsub), |
ba09675f |
s->band>>FFMIN(hsub, vsub),
s->show, direct); |
2701f6cc |
}
|
38464146 |
if (!direct) |
7e350379 |
av_frame_free(&in); |
38464146 |
return ff_filter_frame(outlink, out); |
2701f6cc |
}
|
568c70e7 |
static const AVFilterPad avfilter_vf_delogo_inputs[] = {
{ |
b211607b |
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = filter_frame, |
aeefe018 |
.config_props = config_input, |
568c70e7 |
},
{ NULL }
};
static const AVFilterPad avfilter_vf_delogo_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
},
{ NULL }
};
|
cd43ca04 |
AVFilter ff_vf_delogo = { |
2701f6cc |
.name = "delogo",
.description = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
.priv_size = sizeof(DelogoContext), |
63e58c55 |
.priv_class = &delogo_class, |
2701f6cc |
.init = init,
.query_formats = query_formats, |
b211607b |
.inputs = avfilter_vf_delogo_inputs,
.outputs = avfilter_vf_delogo_outputs,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, |
5af7daab |
}; |