[FFmpeg-devel] [PATCH] lavfi: add kerndeint filter
Stefano Sabatini
stefasab at gmail.com
Tue Sep 18 16:36:03 CEST 2012
On date Tuesday 2012-09-18 07:01:44 +0200, Jérémy Tran encoded:
> This is a port of the vf_kerndeint filter (libmpcodecs/vf_kerndeint) by
> Donal A. Graft (original avisynth plugin author).
>
> In the MPlayer filter, the do_interlace attribute can only be set through
> the control panel and not through the command line, should we drop it ?
It could be useful when scripting (you autodetect if the frames are
interleaved, and add a switch accordingly, otherwise the filter will
act in passthrough mode).
> Also, the MPlayer version uses a fast_memcpy(), is it okay to replace it
> with memcpy() ?
No need to add it if it is adding a dependency on libavcodec.
We may need to move that to libavutil if we regard that as useful.
> ---
> configure | 1 +
> doc/filters.texi | 21 +++
> libavfilter/Makefile | 1 +
> libavfilter/allfilters.c | 1 +
> libavfilter/vf_kerndeint.c | 404 +++++++++++++++++++++++++++++++++++++++++++++
> 5 files changed, 428 insertions(+)
> create mode 100644 libavfilter/vf_kerndeint.c
>
> diff --git a/configure b/configure
> index 4ecb116..3b6b532 100755
> --- a/configure
> +++ b/configure
> @@ -1902,6 +1902,7 @@ frei0r_filter_extralibs='$ldl'
> frei0r_src_filter_deps="frei0r dlopen"
> frei0r_src_filter_extralibs='$ldl'
> hqdn3d_filter_deps="gpl"
> +kerndeint_filter_deps="gpl"
> movie_filter_deps="avcodec avformat"
> mp_filter_deps="gpl avcodec swscale postproc inline_asm"
> mptestsrc_filter_deps="gpl"
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 60b240d..155a173 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -2420,6 +2420,27 @@ If a parameter is omitted, it is kept at its current value.
> Interlaceing detect filter. This filter tries to detect if the input is
> interlaced or progressive. Top or bottom field first.
>
> + at section kerndeint
> +
> +Donald Graft's adaptive kernel deinterlacer. Deinterlaces parts of a video if
> +a configurable threshold is exceeded.
Nit: Deinterlace
Also this description is quite vaguous.
> +
> +This filter accepts the following optional parameters:
> +[@var{threshold}[:@var{map}[:@var{order}[:@var{sharp}[:@var{twoway}]]]]]
Note: we may support named option at almost no cost, once we get
av_opt_set_options_string() committed.
> +
> + at var{threshold} must be an integer in the range [0,255] and defaults to 10.
Which affects what?
> +
> + at var{map} must be 0 (ignore pixels exceeding the threshold) or 1 (paint pixels
> +exceeding the threshold white) and defaults to 0.
> +
> + at var{order} must be 0 (leave fields alone) or 1 (swap fields) and defaults to 0.
> +
> + at var{sharp} must be 0 (disable additional sharpening) or 1 (enable additional
> +sharpening) and defaults to 0.
> +
> + at var{twoway} must be 0 (disable twoway sharpening) or 1 (enable twoway
> +sharpening) and defaults to 0.
Examples?
> +
> @section lut, lutrgb, lutyuv
>
> Compute a look-up table for binding each pixel component input value
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 82d39e4..d80b579 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -108,6 +108,7 @@ OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
> OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o
> OBJS-$(CONFIG_HUE_FILTER) += vf_hue.o
> OBJS-$(CONFIG_IDET_FILTER) += vf_idet.o
> +OBJS-$(CONFIG_KERNDEINT_FILTER) += vf_kerndeint.o
> OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o
> OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o
> OBJS-$(CONFIG_LUTYUV_FILTER) += vf_lut.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 6842ec9..e2980e5 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -99,6 +99,7 @@ void avfilter_register_all(void)
> REGISTER_FILTER (HQDN3D, hqdn3d, vf);
> REGISTER_FILTER (HUE, hue, vf);
> REGISTER_FILTER (IDET, idet, vf);
> + REGISTER_FILTER (KERNDEINT, kerndeint, vf);
> REGISTER_FILTER (LUT, lut, vf);
> REGISTER_FILTER (LUTRGB, lutrgb, vf);
> REGISTER_FILTER (LUTYUV, lutyuv, vf);
> diff --git a/libavfilter/vf_kerndeint.c b/libavfilter/vf_kerndeint.c
> new file mode 100644
> index 0000000..ef6ffa9
> --- /dev/null
> +++ b/libavfilter/vf_kerndeint.c
> @@ -0,0 +1,404 @@
> +/*
> + * Copyright (c) 2012 Jeremy Tran
> + * Copyright (c) 2004 Tobias Diedrich
> + * Copyright (c) 2003 Donald A. Graft
> + *
> + * 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
> + * Kernel Deinterlacer
> + * Ported from MPlayer libmpcodecs/vf_kerndeint.c.
> + */
> +
> +#include "libavutil/pixdesc.h"
> +
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "internal.h"
> +
> +#define PLANAR_Y 0
> +#define PLANAR_U 1
> +#define PLANAR_V 2
> +
> +#define RGB 0
> +#define YUY 1
> +#define YUV 2
> +
> +typedef struct {
> + int frame;
> + int map;
> + int order;
> + int thresh;
> + int sharp;
> + int twoway;
> + int do_deinterlace;
> + int hsub;
> + int vsub;
> + int format;
> + uint8_t *temp_data[4];
> +} KerndeintContext;
doxygen?
> +
> +#define CHECK_PARAM(param, min, max) \
> + do { \
> + if (param < min || param > max) { \
> + av_log(ctx, AV_LOG_ERROR, \
> + "Invalid " #param " value : must be an integer " \
> + "between %d and %d\n", \
> + min, max); \
> + ret = AVERROR(EINVAL); \
> + } \
> + } while (0);
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> + KerndeintContext *kerndeint = ctx->priv;
> + int ret = 0, i;
> + int threshold = 10, map = 0, order = 0, sharp = 0, twoway = 0;
> + char c1 = ':', c2 = ':', c3 = ':', c4 = ':';
> +
> + if (args)
> + sscanf(args, "%d%c%d%c%d%c%d%c%d",
> + &threshold, &c1, &map, &c2,
> + &order, &c3, &sharp, &c4, &twoway);
> +
> + if (c1 != ':' || c2 != ':' || c3 != ':' || c4 != ':') {
> + av_log(ctx, AV_LOG_ERROR,
> + "Invalid syntax, must be: "
> + "kerndeint[=threshold[:map[:order[:sharp[:twoway]]]]]\n");
> + return AVERROR(EINVAL);
> + }
> +
> + kerndeint->thresh = threshold;
> + kerndeint->map = map;
> + kerndeint->order = order;
> + kerndeint->sharp = sharp;
> + kerndeint->twoway = twoway;
> + kerndeint->do_deinterlace = 1;
why don't you directly set the values in the struct?
> +
> + for (i = 0; i < 4; ++i)
> + kerndeint->temp_data[i] = NULL;
> +
> + CHECK_PARAM(threshold, 0, 255);
> + CHECK_PARAM(map, 0, 1);
> + CHECK_PARAM(order, 0, 1);
> + CHECK_PARAM(sharp, 0, 1);
> + CHECK_PARAM(twoway, 0, 1);
> +
> + return ret;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> + KerndeintContext *kerndeint = ctx->priv;
> + int i;
> +
> + for (i = 0; i < 4; ++i)
> + av_free(kerndeint->temp_data[i]);
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> + static const enum PixelFormat pix_fmts[] = {
> + PIX_FMT_YUV444P, PIX_FMT_YUV422P,
> + PIX_FMT_YUV420P, PIX_FMT_YUV411P,
> + PIX_FMT_YUV410P, PIX_FMT_YUV440P,
> + PIX_FMT_YUVA420P,
> + PIX_FMT_YUYV422,
> + PIX_FMT_RGB24, PIX_FMT_BGR24,
> + PIX_FMT_ARGB, PIX_FMT_ABGR,
> + PIX_FMT_RGBA, PIX_FMT_BGRA,
> + PIX_FMT_NONE
> + };
> +
> + ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> +
> + return 0;
> +}
> +
> +static int config_props(AVFilterLink *inlink)
> +{
> + KerndeintContext *kerndeint = inlink->dst->priv;
> + const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
> + int i;
> +
> + kerndeint->hsub = desc->log2_chroma_w;
> + kerndeint->vsub = desc->log2_chroma_h;
> +
> + for (i = 0; i < 4; ++i)
> + kerndeint->temp_data[i] = av_mallocz(inlink->h * inlink->w);
> +
> + switch (inlink->format) {
> + case PIX_FMT_YUV444P:
> + case PIX_FMT_YUV422P:
> + case PIX_FMT_YUV420P:
> + case PIX_FMT_YUV411P:
> + case PIX_FMT_YUV410P:
> + case PIX_FMT_YUV440P:
> + case PIX_FMT_YUVA420P:
> + kerndeint->format = YUV;
> + break;
> + case PIX_FMT_YUYV422:
> + kerndeint->format = YUY;
Nit: YUYV?
> + break;
> + case PIX_FMT_RGB24:
> + case PIX_FMT_BGR24:
> + case PIX_FMT_ARGB:
> + case PIX_FMT_ABGR:
> + case PIX_FMT_RGBA:
> + case PIX_FMT_BGRA:
> + kerndeint->format = RGB;
> + break;
> + }
> +
> + return 0;
> +}
> +
> +static int end_frame(AVFilterLink *inlink)
> +{
> + KerndeintContext *kerndeint = inlink->dst->priv;
> + AVFilterBufferRef *inpic = inlink->cur_buf;
> + AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
> + int cw = inlink->w >> kerndeint->hsub;
> + int ch = inlink->h >> kerndeint->vsub;
> + const unsigned char *prvp, *prvpp, *prvpn, *prvpnn, *prvppp, *prvp4p, *prvp4n;
> + const unsigned char *srcp_saved;
> + const unsigned char *srcp, *srcpp, *srcpn, *srcpnn, *srcppp, *srcp3p, *srcp3n, *srcp4p, *srcp4n;
> + unsigned char *dstp, *dstp_saved;
> + int src_linesize;
> + int psrc_linesize;
> + int dst_linesize;
> + int x, y, z, g;
> + int n = kerndeint->frame++;
> + int val, hi, lo, w, h;
> + double valf;
> + int plane;
> + int threshold = kerndeint->thresh;
> + int order = kerndeint->order;
> + int map = kerndeint->map;
> + int sharp = kerndeint->sharp;
> + int twoway = kerndeint->twoway;
const while at it
> +
> + if (kerndeint->do_deinterlace) {
> + for (z = 0; inpic->data[z] && z < 4; z++) {
> + if (!z)
> + plane = PLANAR_Y;
> + else if (z == 1)
> + plane = PLANAR_U;
> + else
> + plane = PLANAR_V;
Why the intermediary z variable assuming that plane == z?
Also, does it work with alpha planes?
> +
> + h = plane == PLANAR_Y ? inlink->h : ch;
> + w = plane == PLANAR_Y ? inlink->w : cw;
> +
> + srcp = srcp_saved = inpic->data[z];
> + src_linesize = inpic->linesize[z];
> + psrc_linesize = outpic->linesize[z];
> + dstp = dstp_saved = outpic->data[z];
> + dst_linesize = outpic->linesize[z];
> + srcp = srcp_saved + (1 - order) * src_linesize;
> + dstp = dstp_saved + (1 - order) * dst_linesize;
> +
> + for (y = 0; y < h; y += 2) {
> + memcpy(dstp, srcp, w);
> + srcp += 2 * src_linesize;
> + dstp += 2 * dst_linesize;
> + }
> +
> + // Copy through the lines that will be missed below.
> + memcpy(dstp_saved + order * dst_linesize, srcp_saved + (1 - order) * src_linesize, w);
> + memcpy(dstp_saved + (2 + order) * dst_linesize, srcp_saved + (3 - order) * src_linesize, w);
> + memcpy(dstp_saved + (h - 2 + order) * dst_linesize, srcp_saved + (h - 1 - order) * src_linesize, w);
> + memcpy(dstp_saved + (h - 4 + order) * dst_linesize, srcp_saved + (h - 3 - order) * src_linesize, w);
> +
> + /* For the other field choose adaptively between using the previous field
> + or the interpolant from the current field. */
> + prvp = kerndeint->temp_data[z] + 5 * psrc_linesize - (1 - order) * psrc_linesize;
> + prvpp = prvp - psrc_linesize;
> + prvppp = prvp - 2 * psrc_linesize;
> + prvp4p = prvp - 4 * psrc_linesize;
> + prvpn = prvp + psrc_linesize;
> + prvpnn = prvp + 2 * psrc_linesize;
> + prvp4n = prvp + 4 * psrc_linesize;
> +
> + srcp = srcp_saved + 5 * src_linesize - (1 - order) * src_linesize;
> + srcpp = srcp - src_linesize;
> + srcppp = srcp - 2 * src_linesize;
> + srcp3p = srcp - 3 * src_linesize;
> + srcp4p = srcp - 4 * src_linesize;
> +
> + srcpn = srcp + src_linesize;
> + srcpnn = srcp + 2 * src_linesize;
> + srcp3n = srcp + 3 * src_linesize;
> + srcp4n = srcp + 4 * src_linesize;
> +
> + dstp = dstp_saved + 5 * dst_linesize - (1 - order) * dst_linesize;
> +
> + for (y = 5 - (1 - order); y <= h - 5 - (1 - order); y += 2) {
> + for (x = 0; x < w; x++) {
> + if ((threshold == 0) || (n == 0) ||
> + (abs((int)prvp[x] - (int)srcp[x]) > threshold) ||
> + (abs((int)prvpp[x] - (int)srcpp[x]) > threshold) ||
> + (abs((int)prvpn[x] - (int)srcpn[x]) > threshold)) {
> + if (map == 1) {
> + g = x & ~3;
> +
> + if (kerndeint->format == RGB) {
> + dstp[g++] = 255;
> + dstp[g++] = 255;
> + dstp[g++] = 255;
> + dstp[g] = 255;
> + x = g;
> + } else if (kerndeint->format == YUY) {
> + dstp[g++] = 235;
> + dstp[g++] = 128;
> + dstp[g++] = 235;
> + dstp[g] = 128;
> + x = g;
> + } else {
> + if (plane == PLANAR_Y)
> + dstp[x] = 235;
> + else
> + dstp[x] = 128;
> + }
> + } else {
> + if (kerndeint->format == RGB) {
> + hi = 255;
> + lo = 0;
> + } else if (kerndeint->format == YUY) {
> + hi = (x & 1) ? 240 : 235;
> + lo = 16;
> + } else {
> + hi = (plane == PLANAR_Y) ? 235 : 240;
> + lo = 16;
> + }
> +
> + if (sharp == 1) {
> + if (twoway == 1) {
> + valf = 0.526 * ((int)srcpp[x] + (int)srcpn[x])
> + + 0.170 * ((int)srcp[x] + (int)prvp[x])
> + - 0.116 * ((int)srcppp[x]
> + + (int)srcpnn[x]
> + + (int)prvppp[x]
> + + (int)prvpnn[x])
> + - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
> + + 0.031 * ((int)srcp4p[x]
> + + (int)srcp4n[x]
> + + (int)prvp4p[x]
> + + (int)prvp4n[x]);
> + } else {
> + valf = 0.526 * ((int)srcpp[x] + (int)srcpn[x])
> + + 0.170 * ((int)prvp[x])
> + - 0.116 * ((int)prvppp[x] + (int)prvpnn[x])
> + - 0.026 * ((int)srcp3p[x] + (int)srcp3n[x])
> + + 0.031 * ((int)prvp4p[x] + (int)prvp4p[x]);
> + }
> +
> + if (valf > hi)
> + valf = hi;
> + else if (valf < lo)
> + valf = lo;
av_clip
> +
> + dstp[x] = (int) valf;
> + } else {
> + if (twoway == 1) {
> + val = (8*((int)srcpp[x] + (int)srcpn[x])
> + + 2 * ((int)srcp[x] + (int)prvp[x])
> + - (int)(srcppp[x]) - (int)(srcpnn[x])
> + - (int)(prvppp[x]) - (int)(prvpnn[x]))
> + >> 4;
> + } else {
> + val = (8 * ((int)srcpp[x]
> + + (int)srcpn[x])
> + + 2 * ((int)prvp[x])
> + - (int)(prvppp[x]) - (int)(prvpnn[x]))
> + >> 4;
> + }
> +
> + if (val > hi)
> + val = hi;
> + else if (val < lo)
> + val = lo;
same
> +
> + dstp[x] = (int) val;
> + }
> + }
> + } else {
> + dstp[x] = srcp[x];
> + }
> + }
> + prvp += 2 * psrc_linesize;
> + prvpp += 2 * psrc_linesize;
> + prvppp += 2 * psrc_linesize;
> + prvpn += 2 * psrc_linesize;
> + prvpnn += 2 * psrc_linesize;
> + prvp4p += 2 * psrc_linesize;
> + prvp4n += 2 * psrc_linesize;
> + srcp += 2 * src_linesize;
> + srcpp += 2 * src_linesize;
> + srcppp += 2 * src_linesize;
> + srcp3p += 2 * src_linesize;
> + srcp4p += 2 * src_linesize;
> + srcpn += 2 * src_linesize;
> + srcpnn += 2 * src_linesize;
> + srcp3n += 2 * src_linesize;
> + srcp4n += 2 * src_linesize;
> + dstp += 2 * dst_linesize;
> + }
> +
> + srcp = inpic->data[z];
> + dstp = kerndeint->temp_data[z];
> + for (y = 0; y < h; y++) {
> + memcpy(dstp, srcp, w);
> + srcp += src_linesize;
> + dstp += psrc_linesize;
av_image_copy_plane?
[...]
--
FFmpeg = Faithless and Frenzy Magical Philosofic Excellent Gorilla
More information about the ffmpeg-devel
mailing list