[FFmpeg-devel] [PATCH] lavfi: port decimate libmpcodecs filter
Nicolas George
nicolas.george at normalesup.org
Sun Aug 19 20:22:20 CEST 2012
Le tridi 3 fructidor, an CCXX, Stefano Sabatini a écrit :
> >From 7379459869bb671282e7f50d4bf7ba44c05ef0a5 Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefasab at gmail.com>
> Date: Sat, 17 Mar 2012 15:49:51 +0100
> Subject: [PATCH] lavfi: port decimate libmpcodecs filter
>
> ---
> configure | 1 +
> doc/filters.texi | 34 +++++++
> libavfilter/Makefile | 1 +
> libavfilter/allfilters.c | 1 +
> libavfilter/diff.c | 35 +++++++
> libavfilter/diff.h | 27 +++++
> libavfilter/vf_decimate.c | 243 +++++++++++++++++++++++++++++++++++++++++++++
> libavfilter/x86/Makefile | 2 +
> libavfilter/x86/diff.c | 63 ++++++++++++
> 9 files changed, 407 insertions(+), 0 deletions(-)
> create mode 100644 libavfilter/diff.c
> create mode 100644 libavfilter/diff.h
> create mode 100644 libavfilter/vf_decimate.c
> create mode 100644 libavfilter/x86/diff.c
>
> diff --git a/configure b/configure
> index 679fae0..e67278c 100755
> --- a/configure
> +++ b/configure
> @@ -1830,6 +1830,7 @@ blackframe_filter_deps="gpl"
> boxblur_filter_deps="gpl"
> colormatrix_filter_deps="gpl"
> cropdetect_filter_deps="gpl"
> +decimate_filter_deps="gpl"
> delogo_filter_deps="gpl"
> deshake_filter_deps="avcodec"
> drawtext_filter_deps="libfreetype"
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 8847990..1ac88c2 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -1539,6 +1539,40 @@ indicates never reset and return the largest area encountered during
> playback.
> @end table
>
> + at section decimate
> +
> +This filter drops frames that do not differ greatly from the previous
> +frame in order to reduce framerate. The main use of this filter is
> +for very-low-bitrate encoding (e.g. streaming over dialup modem), but
> +it could in theory be used for fixing movies that were
> +inverse-telecined incorrectly.
> +
> +It accepts the following parameters:
> + at var{max}:@var{hi}:@var{lo}:@var{frac}.
> +
> + at table @option
> +
> + at item max
> +Set the maximum number of consecutive frames which can be dropped (if
> +positive), or the minimum interval between dropped frames (if
> +negative). Default value is 0.
> +
> + at item hi, lo, frac
> +Set the dropping threshold values.
> +
> +Values for @var{hi} and @var{lo} are for 8x8 pixel blocks and
> +represent actual pixel value differences, so a threshold of 64
> +corresponds to 1 unit of difference for each pixel, or the same spread
> +out differently over the block.
> +
> +A frame is a candidate for dropping if no 8x8 blocks differ by more
> +than a threshold of @var{hi}, and if no more than @var{frac} blocks (1
> +meaning the whole image) differ by more than a threshold of @var{lo}.
> +
> +Default value for @var{hi} is 64*12, default value for @var{lo} is
> +64*5, and default value for @var{frac} is 0.33.
> + at end table
> +
> @section delogo
>
> Suppress a TV station logo by a simple interpolation of the surrounding
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index af4fde6..4cae706 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -87,6 +87,7 @@ OBJS-$(CONFIG_COLORMATRIX_FILTER) += vf_colormatrix.o
> OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o
> OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o
> OBJS-$(CONFIG_CROPDETECT_FILTER) += vf_cropdetect.o
> +OBJS-$(CONFIG_DECIMATE_FILTER) += vf_decimate.o diff.o
> OBJS-$(CONFIG_DELOGO_FILTER) += vf_delogo.o
> OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o
> OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 6defed4..be49e23 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -78,6 +78,7 @@ void avfilter_register_all(void)
> REGISTER_FILTER (COPY, copy, vf);
> REGISTER_FILTER (CROP, crop, vf);
> REGISTER_FILTER (CROPDETECT, cropdetect, vf);
> + REGISTER_FILTER (DECIMATE, decimate, vf);
> REGISTER_FILTER (DELOGO, delogo, vf);
> REGISTER_FILTER (DESHAKE, deshake, vf);
> REGISTER_FILTER (DRAWBOX, drawbox, vf);
> diff --git a/libavfilter/diff.c b/libavfilter/diff.c
> new file mode 100644
> index 0000000..4e748ec
> --- /dev/null
> +++ b/libavfilter/diff.c
> @@ -0,0 +1,35 @@
> +/*
> + * Copyright (c) 2003 Rich Felker
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
The original code is under GPL, but you clearly reimplemented it. Probably
alter the copyright notice.
> + *
> + * 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
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser 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
> + */
> +
> +#include "diff.h"
> +
> +int ff_diff_8x8_c(const uint8_t *cur, int cur_linesize,
> + const uint8_t *ref, int ref_linesize)
> +{
> + int x, y, d = 0;
> +
> + for (y = 8; y; y--) {
> + for (x = 8; x; x--)
> + d += abs(cur[x] - ref[x]);
Looks strange: the data will be accessed at indices 1..8, instead of 0..7.
And in reverse order, which may be not good for the cache. Maybe switch to a
more natural loop?
> + cur += cur_linesize;
> + ref += ref_linesize;
> + }
> + return d;
> +}
> diff --git a/libavfilter/diff.h b/libavfilter/diff.h
> new file mode 100644
> index 0000000..c27128d
> --- /dev/null
> +++ b/libavfilter/diff.h
> @@ -0,0 +1,27 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 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
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser 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
> + */
> +
> +#ifndef AVFILTER_DIFF_H
> +#define AVFILTER_DIFF_H
> +
> +#include "avfilter.h"
> +
> +int ff_diff_8x8_c (const uint8_t *cur, int cur_linesize, const uint8_t *ref, int ref_linesize);
> +int ff_diff_8x8_mmx(const uint8_t *cur, int cur_linesize, const uint8_t *ref, int ref_linesize);
> +
> +#endif /* AVFILTER_DIFF_H */
> diff --git a/libavfilter/vf_decimate.c b/libavfilter/vf_decimate.c
> new file mode 100644
> index 0000000..d94eed6
> --- /dev/null
> +++ b/libavfilter/vf_decimate.c
> @@ -0,0 +1,243 @@
> +/*
> + * Copyright (c) 2003 Rich Felker
> + * Copyright (c) 2012 Stefano Sabatini
> + *
> + * 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.
> + */
> +
> +#include "libavutil/pixdesc.h"
> +#include "libavutil/timestamp.h"
> +#include "avfilter.h"
> +#include "diff.h"
> +#include "internal.h"
> +#include "formats.h"
> +#include "video.h"
> +
> +typedef struct {
> + int lo, hi; ///< lower and higher threshold number of differences
> + /// values for 8x8 blocks
> + float frac; ///< threshold of changed pixels over the total fraction
Maybe store it as an integer? That would help making the filter bit-exact.
> +
> + int max_drop_count; ///< if positive: maximum number of sequential frames to drop
> + /// if negative: minimum number of frames between two drops
> + int drop_count; ///< if positive: number of frames sequentially dropped
> + /// if negative: number of sequential frame which were not dropped
> +
> + int hsub, vsub; ///< chroma subsampling values
> + AVFilterBufferRef *ref; ///< reference picture
> + /* diff function used to compute difference between lines */
> + int (* diff)(const uint8_t *cur, int cur_linesize,
> + const uint8_t *ref, int ref_linesize);
> +} DecimateContext;
> +
> +/**
> + * Return 1 in case the two frames are different, 0 otherwise.
> + */
> +static int diff_planes(AVFilterContext *ctx,
> + uint8_t *cur, int cur_linesize,
> + uint8_t *ref, int ref_linesize,
> + int w, int h)
> +{
> + DecimateContext *decimate = ctx->priv;
> +
> + int x, y;
> + int d, c = 0;
> + int t = (w/16)*(h/16)*decimate->frac;
> +
> + /* compute difference for blocks of 8x8 bytes */
> + for (y = 0; y < h-7; y += 4) {
> + for (x = 8; x < w-7; x += 4) {
> + d = decimate->diff(cur+x+y*cur_linesize, cur_linesize,
> + ref+x+y*ref_linesize, ref_linesize);
I wonder if the benefit of the MMX optimization of the diff function is not
completely wasted by the fact that all computations are done four times.
> + if (d > decimate->hi)
> + return 1;
> + if (d > decimate->lo) {
> + c++;
> + if (c > t)
> + return 1;
> + }
> + }
> + }
> + return 0;
> +}
> +
> +/**
> + * Tell if the frame should be decimated, that is if it is no much
> + * different with respect to the reference frame ref.
> + */
> +static int decimate_frame(AVFilterContext *ctx,
> + AVFilterBufferRef *cur, AVFilterBufferRef *ref)
> +{
> + DecimateContext *decimate = ctx->priv;
> + int plane;
> +
> + if (decimate->max_drop_count == 0 ||
> + decimate->drop_count >= decimate->max_drop_count)
> + return 0;
> +
> + for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
> + int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
> + int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
> + if (diff_planes(ctx,
> + cur->data[plane], cur->linesize[plane],
> + ref->data[plane], ref->linesize[plane],
> + ref->video->w>>hsub, ref->video->h>>vsub))
> + return 0;
> + }
> +
> + return 1;
> +}
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> + DecimateContext *decimate = ctx->priv;
> + int cpu_flags = av_get_cpu_flags();
> +
> + /* set default values */
> + decimate->drop_count = decimate->max_drop_count = 0;
> + decimate->lo = 64*5;
> + decimate->hi = 64*12;
> + decimate->frac = 0.33;
> +
> + if (args)
> + sscanf(args, "%d:%d:%d:%f",
> + &decimate->max_drop_count,
> + &decimate->hi, &decimate->lo, &decimate->frac);
> +
> + av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
> + decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
> +
> + if (HAVE_MMX && cpu_flags&AV_CPU_FLAG_MMX2 && HAVE_EBX_AVAILABLE)
> + decimate->diff = ff_diff_8x8_mmx;
> + else
> + decimate->diff = ff_diff_8x8_c;
> +
> + return 0;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> + DecimateContext *decimate = ctx->priv;
> + avfilter_unref_bufferp(&decimate->ref);
> +}
> +
> +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_YUVJ444P, PIX_FMT_YUVJ422P,
> + PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
> + PIX_FMT_YUVA420P,
> + PIX_FMT_NONE
> + };
> +
> + ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> +
> + return 0;
> +}
> +
> +static int config_input(AVFilterLink *inlink)
> +{
> + AVFilterContext *ctx = inlink->dst;
> + DecimateContext *decimate = ctx->priv;
> + const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
> + decimate->hsub = pix_desc->log2_chroma_w;
> + decimate->vsub = pix_desc->log2_chroma_h;
> + return 0;
> +}
> +
> +static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) { return 0; }
> +
> +static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { return 0; }
> +
> +static int end_frame(AVFilterLink *inlink)
> +{
> + DecimateContext *decimate = inlink->dst->priv;
> + AVFilterBufferRef *cur = inlink->cur_buf;
> + AVFilterLink *outlink = inlink->dst->outputs[0];
> + int ret;
> +
> + if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
> + decimate->drop_count = FFMAX(1, decimate->drop_count+1);
> + } else {
> + avfilter_unref_buffer(decimate->ref);
> + decimate->ref = cur;
> + inlink->cur_buf = NULL;
> + decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
> +
> + if ((ret = ff_start_frame(outlink,
> + avfilter_ref_buffer(cur, ~AV_PERM_WRITE)) < 0) ||
> + (ret = ff_draw_slice(outlink, 0, inlink->h, 1)) < 0 ||
> + (ret = ff_end_frame(outlink)) < 0)
> + return ret;
> + }
> +
> + av_log(inlink->dst, AV_LOG_DEBUG,
> + "%s pts:%s pts_time:%s drop_count:%d\n",
> + decimate->drop_count > 0 ? "drop" : "keep",
> + av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
> + decimate->drop_count);
> +
> + return 0;
> +}
> +
> +static int request_frame(AVFilterLink *outlink)
> +{
> + DecimateContext *decimate = outlink->src->priv;
> + AVFilterLink *inlink = outlink->src->inputs[0];
> + int ret;
> +
> + do {
> + ret = ff_request_frame(inlink);
> + } while (decimate->drop_count < 0 && ret >= 0);
> +
> + return ret;
> +}
> +
> +AVFilter avfilter_vf_decimate = {
> + .name = "decimate",
> + .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
> + .init = init,
> + .uninit = uninit,
> +
> + .priv_size = sizeof(DecimateContext),
> + .query_formats = query_formats,
> +
> + .inputs = (const AVFilterPad[]) {
> + {
> + .name = "default",
> + .type = AVMEDIA_TYPE_VIDEO,
> + .get_video_buffer = ff_null_get_video_buffer,
> + .config_props = config_input,
> + .start_frame = start_frame,
> + .draw_slice = draw_slice,
> + .end_frame = end_frame,
> + .min_perms = AV_PERM_READ,
You need PRESERVE.
> + },
> + { .name = NULL }
> + },
> + .outputs = (const AVFilterPad[]) {
> + {
> + .name = "default",
> + .type = AVMEDIA_TYPE_VIDEO,
> + .request_frame = request_frame,
> + },
> + { .name = NULL }
> + },
> +};
> diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile
> index e98693d..1bbfe68 100644
> --- a/libavfilter/x86/Makefile
> +++ b/libavfilter/x86/Makefile
> @@ -1,2 +1,4 @@
> MMX-OBJS-$(CONFIG_YADIF_FILTER) += x86/yadif.o
> MMX-OBJS-$(CONFIG_GRADFUN_FILTER) += x86/gradfun.o
> +
> +MMX-OBJS-$(HAVE_MMX) += x86/diff.o
This looks strange.
> \ No newline at end of file
> diff --git a/libavfilter/x86/diff.c b/libavfilter/x86/diff.c
> new file mode 100644
> index 0000000..b4ad088
> --- /dev/null
> +++ b/libavfilter/x86/diff.c
> @@ -0,0 +1,63 @@
> +/*
> + * Copyright (c) 2003 Rich Felker
> + *
> + * 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.
> + */
> +
> +#include "libavutil/cpu.h"
> +#include "libavfilter/diff.h"
> +
> +int ff_diff_8x8_mmx(const uint8_t *cur, int cur_linesize,
> + const uint8_t *ref, int ref_linesize)
> +{
> + volatile short out[4];
> + __asm__ (
> + "movl $8, %%ecx \n\t"
> + "pxor %%mm4, %%mm4 \n\t"
> + "pxor %%mm7, %%mm7 \n\t"
> +
> + ".p2align 4 \n\t"
> + "1: \n\t"
> +
> + "movq (%%"REG_S"), %%mm0 \n\t"
> + "movq (%%"REG_S"), %%mm2 \n\t"
> + "add %%"REG_a", %%"REG_S" \n\t"
> + "movq (%%"REG_D"), %%mm1 \n\t"
> + "add %%"REG_b", %%"REG_D" \n\t"
> + "psubusb %%mm1, %%mm2 \n\t"
> + "psubusb %%mm0, %%mm1 \n\t"
> + "movq %%mm2, %%mm0 \n\t"
> + "movq %%mm1, %%mm3 \n\t"
> + "punpcklbw %%mm7, %%mm0 \n\t"
> + "punpcklbw %%mm7, %%mm1 \n\t"
> + "punpckhbw %%mm7, %%mm2 \n\t"
> + "punpckhbw %%mm7, %%mm3 \n\t"
> + "paddw %%mm0, %%mm4 \n\t"
> + "paddw %%mm1, %%mm4 \n\t"
> + "paddw %%mm2, %%mm4 \n\t"
> + "paddw %%mm3, %%mm4 \n\t"
> +
> + "decl %%ecx \n\t"
> + "jnz 1b \n\t"
> + "movq %%mm4, (%%"REG_d") \n\t"
> + "emms \n\t"
> + :
> + : "S" (cur), "D" (ref), "a" ((long)cur_linesize), "b" ((long)ref_linesize), "d" (out)
> + : "%ecx", "memory"
> + );
> + return out[0]+out[1]+out[2]+out[3];
> +}
Regards,
--
Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120819/3d6ab172/attachment.asc>
More information about the ffmpeg-devel
mailing list