[FFmpeg-devel] [PATCH] lavfi: port decimate libmpcodecs filter
Stefano Sabatini
stefasab at gmail.com
Sun Mar 18 01:01:31 CET 2012
---
doc/filters.texi | 29 +++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/diff.h | 27 +++
libavfilter/libmpcodecs/vf_decimate.c | 2 +-
libavfilter/vf_decimate.c | 308 +++++++++++++++++++++++++++++++++
libavfilter/x86/Makefile | 2 +
libavfilter/x86/diff.c | 62 +++++++
8 files changed, 431 insertions(+), 1 deletions(-)
create mode 100644 libavfilter/diff.h
create mode 100644 libavfilter/vf_decimate.c
create mode 100644 libavfilter/x86/diff.c
diff --git a/doc/filters.texi b/doc/filters.texi
index 0a6af05..c5dcf61 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -1058,6 +1058,35 @@ 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
+Values of @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 region differs by more
+than a threshold of @var{hi}, and if not more than @var{frac} portion
+(1 meaning the whole image) differs by more than a threshold of @var{lo}.
+ 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 6a9a3ad..d21fee7 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -55,6 +55,7 @@ OBJS-$(CONFIG_BOXBLUR_FILTER) += vf_boxblur.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
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 2fedf7d..f7a1efb 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -63,6 +63,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.h b/libavfilter/diff.h
new file mode 100644
index 0000000..14e1028
--- /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 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.
+ */
+
+#ifndef AVFILTER_DIFF_H
+#define AVFILTER_DIFF_H
+
+#include "avfilter.h"
+
+int ff_diff_8x8_c(unsigned char *old, unsigned char *new, int os, int ns);
+int ff_diff_8x8_mmx(unsigned char *old, unsigned char *new, int os, int ns);
+
+#endif /* AVFILTER_DIFF_H */
diff --git a/libavfilter/libmpcodecs/vf_decimate.c b/libavfilter/libmpcodecs/vf_decimate.c
index 1fd7bce..6c089ea 100644
--- a/libavfilter/libmpcodecs/vf_decimate.c
+++ b/libavfilter/libmpcodecs/vf_decimate.c
@@ -46,7 +46,7 @@ static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
"pxor %%mm4, %%mm4 \n\t"
"pxor %%mm7, %%mm7 \n\t"
- ASMALIGN(4)
+ ".p2align 4 \n\t"
"1: \n\t"
"movq (%%"REG_S"), %%mm0 \n\t"
diff --git a/libavfilter/vf_decimate.c b/libavfilter/vf_decimate.c
new file mode 100644
index 0000000..182136e
--- /dev/null
+++ b/libavfilter/vf_decimate.c
@@ -0,0 +1,308 @@
+/*
+ * 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/fifo.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/timestamp.h"
+#include "avfilter.h"
+#include "diff.h"
+
+#define FIFO_SIZE 8
+
+typedef struct {
+ int lo, hi; ///< lower and higher values
+ float frac; ///< fraction of changed pixels over the total
+ int max, last, count;
+ int (* diff)(uint8_t *prev, uint8_t *curr, int prev_linesize, int curr_linesize);
+ int hsub, vsub; ///< chroma subsampling values
+ int cache_frames;
+ AVFilterBufferRef *prev_picref;
+ AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
+ int select;
+} DecimateContext;
+
+int ff_diff_8x8_c(uint8_t *prev, uint8_t *curr, int prev_linesize, int curr_linesize)
+{
+ int x, y, d = 0;
+ for (y = 8; y; y--) {
+ for (x = 8; x; x--) {
+ d += abs(curr[x] - prev[x]);
+ }
+ curr += curr_linesize;
+ prev += prev_linesize;
+ }
+ return d;
+}
+
+/**
+ * Return 1 in case the two frames are different, 0 otherwise.
+ */
+static int compute_plane_diff(AVFilterContext *ctx,
+ uint8_t *prev, uint8_t *curr,
+ int w, int h, int prev_linesize, int curr_linesize)
+{
+ 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(prev+x+y*prev_linesize, curr+x+y*curr_linesize,
+ prev_linesize, curr_linesize);
+ if (d > decimate->hi)
+ return 1;
+ if (d > decimate->lo) {
+ c++;
+ if (c > t)
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+static int decimate_frame(AVFilterContext *ctx,
+ AVFilterBufferRef *prev, AVFilterBufferRef *curr)
+{
+ DecimateContext *decimate = ctx->priv;
+ int plane, diff;
+
+ for (plane = 0; prev->data[plane] && prev->linesize[plane]; plane++) {
+ int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
+ int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
+ diff = compute_plane_diff(ctx, prev->data[plane], curr->data[plane],
+ prev->video->w>>hsub, prev->video->h>>vsub,
+ prev->linesize[plane], curr->linesize[plane]);
+ if (diff)
+ break;
+ }
+
+ if (diff) {
+ if (decimate->max == 0)
+ return 0;
+ else if (decimate->max > 0 && decimate->count++ < decimate->max)
+ return 0;
+ else if (decimate->max < 0 && (decimate->last+1) >= -decimate->max) {
+ decimate->last = 0;
+ return 0;
+ }
+ }
+ decimate->last++;
+ decimate->count = 0;
+
+ return 1;
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ DecimateContext *decimate = ctx->priv;
+ int cpu_flags = av_get_cpu_flags();
+
+ /* set default values */
+ decimate->max = 0;
+ decimate->lo = 64*5;
+ decimate->hi = 64*12;
+ decimate->frac = 0.33;
+
+ decimate->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
+ if (!decimate->pending_frames) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ if (args)
+ sscanf(args, "%d:%d:%d:%f",
+ &decimate->max, &decimate->hi, &decimate->lo, &decimate->frac);
+
+ av_log(ctx, AV_LOG_INFO, "max:%d hi:%d lo:%d frac:%f\n",
+ decimate->max, 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;
+ AVFilterBufferRef *picref;
+
+ while (decimate->pending_frames &&
+ av_fifo_generic_read(decimate->pending_frames, &picref,
+ sizeof(picref), NULL) == sizeof(picref))
+ avfilter_unref_buffer(picref);
+ av_fifo_free(decimate->pending_frames);
+ decimate->pending_frames = NULL;
+
+ if (decimate->prev_picref)
+ avfilter_unref_buffer(decimate->prev_picref);
+ decimate->prev_picref = NULL;
+}
+
+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
+ };
+
+ avfilter_set_common_pixel_formats(ctx, avfilter_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 void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) { }
+
+static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
+
+static void end_frame(AVFilterLink *inlink)
+{
+ DecimateContext *decimate = inlink->dst->priv;
+ AVFilterBufferRef *picref = inlink->cur_buf;
+ AVFilterLink *outlink = inlink->dst->outputs[0];
+
+ if (!decimate->prev_picref) {
+ decimate->prev_picref = avfilter_ref_buffer(picref, ~0);
+ return;
+ }
+
+ decimate->select = !decimate_frame(inlink->dst, picref, decimate->prev_picref);
+ if (decimate->select) {
+ if (decimate->cache_frames) {
+ if (!av_fifo_space(decimate->pending_frames))
+ av_log(inlink->dst, AV_LOG_ERROR,
+ "Buffering limit reached, cannot cache more frames\n");
+ else
+ av_fifo_generic_write(decimate->pending_frames, &picref,
+ sizeof(picref), NULL);
+ return;
+ }
+
+ avfilter_start_frame(outlink, avfilter_ref_buffer(decimate->prev_picref, ~0));
+ avfilter_draw_slice(outlink, 0, outlink->h, 1);
+ avfilter_end_frame(outlink);
+
+ avfilter_unref_buffer(decimate->prev_picref);
+ decimate->prev_picref = avfilter_ref_buffer(picref, ~0);
+ } else {
+ av_log(inlink->dst, AV_LOG_DEBUG,
+ "drop pts:%s pts_time:%s\n",
+ av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base));
+ }
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ DecimateContext *decimate = ctx->priv;
+ AVFilterLink *inlink = outlink->src->inputs[0];
+ decimate->select = 0;
+
+ if (av_fifo_size(decimate->pending_frames)) {
+ AVFilterBufferRef *picref;
+ av_fifo_generic_read(decimate->pending_frames, &picref, sizeof(picref), NULL);
+ avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
+ avfilter_draw_slice(outlink, 0, outlink->h, 1);
+ avfilter_end_frame(outlink);
+ avfilter_unref_buffer(picref);
+ return 0;
+ }
+
+ while (!decimate->select) {
+ int ret = avfilter_request_frame(inlink);
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int poll_frame(AVFilterLink *outlink)
+{
+ DecimateContext *decimate = outlink->src->priv;
+ AVFilterLink *inlink = outlink->src->inputs[0];
+ int count, ret;
+
+ if (!av_fifo_size(decimate->pending_frames)) {
+ if ((count = avfilter_poll_frame(inlink)) <= 0)
+ return count;
+ /* request frame from input, and apply decimate condition to it */
+ decimate->cache_frames = 1;
+ while (count-- && av_fifo_space(decimate->pending_frames)) {
+ ret = avfilter_request_frame(inlink);
+ if (ret < 0)
+ break;
+ }
+ decimate->cache_frames = 0;
+ }
+
+ return av_fifo_size(decimate->pending_frames)/sizeof(AVFilterBufferRef *);
+}
+
+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 = avfilter_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, },
+ { .name = NULL }
+ },
+ .outputs = (const AVFilterPad[]) {
+ { .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .poll_frame = poll_frame,
+ .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
\ No newline at end of file
diff --git a/libavfilter/x86/diff.c b/libavfilter/x86/diff.c
new file mode 100644
index 0000000..19b1b42
--- /dev/null
+++ b/libavfilter/x86/diff.c
@@ -0,0 +1,62 @@
+/*
+ * 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 "libavutil/x86_cpu.h"
+#include "libavfilter/diff.h"
+
+int ff_diff_8x8_mmx(uint8_t *prev, uint8_t *curr, int prev_linesize, int curr_linesize)
+{
+ volatile short out[4];
+ __asm__ (
+ "movl $8, %%ecx \n\t"
+ "pxor %%mm4, %%mm4 \n\t"
+ "pxor %%mm7, %%mm7 \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" (prev), "D" (curr), "a" ((long)prev_linesize), "b" ((long)curr_linesize), "d" (out)
+ : "%ecx", "memory"
+ );
+ return out[0]+out[1]+out[2]+out[3];
+}
--
1.7.5.4
More information about the ffmpeg-devel
mailing list