PR #25925 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25925
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25925.patch

Add 9 to 16-bit planar YUV, YUVA and GBR input to mpdecimate, so that a 
`yuv422p10le` source no longer gets an 8-bit `auto_scale` in front of the 
filter. This matters when one decode is split between mpdecimate and analysis 
filters that handle high bit depth natively.

**Thresholds.** `hi` and `lo` keep their 8-bit meaning and are scaled by 
2^(depth-8). A frame whose samples are its 8-bit equivalent shifted left by 
depth-8 has a SAD of exactly SAD8 << (depth-8) per block, so it gets the same 
keep/drop decision. `frac` is a fraction of the block count and is not scaled. 
8-bit formats keep the pixelutils SAD and unscaled thresholds, so their output 
is unchanged.

**Commits**
1. `avfilter/mpdecimate: add high bit depth support` - C 8x8 SAD on 16-bit 
samples, scaled thresholds, new formats, docs, and FATE tests for yuv422p10le 
and yuv444p16le. They output the same frames as the existing 8-bit 
`filter-mpdecimate` and `filter-mpdecimate-mode-1-min-3` tests.
2. `avfilter/x86: add SSE2 16-bit SAD for mpdecimate` - plus a checkasm test. 
It matters when the compiler does not vectorize the C function (configure 
disables the vectorizer for GCC older than 13).

**Testing**
- All mpdecimate FATE tests pass on arm64 (native) and x86_64; the existing 
8-bit refs are unchanged.
- x86_64 was only run under Rosetta 2, not on x86 hardware: checkasm passes, 
and the timings in the commit message come from there.
- With a ProRes 422 `yuv422p10le` source split into blackdetect, cropdetect, 
idet, select and mpdecimate, `-loglevel debug` no longer shows an `auto_scale` 
in front of mpdecimate.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From de0389a93cbe31dcd3b3fd96fc0f6bb65d3e2e35 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Tue, 10 Nov 2026 21:51:31 -0800
Subject: [PATCH 1/2] avfilter/mpdecimate: add high bit depth support

Accept 9 to 16-bit planar YUV, YUVA and GBR formats, so that a
yuv422p10le source no longer needs a conversion to 8-bit in front of
the filter. This matters when one decode is split between mpdecimate
and analysis filters that support high bit depth natively.

For more than 8 bits, the 8x8 SAD reads 16-bit samples with a C
function, since pixelutils only provides 8-bit SAD.

The hi and lo options keep their 8-bit meaning. They are scaled by
2^(depth-8) when the input is configured:

    hi_thr = hi * 2^(depth - 8)
    lo_thr = lo * 2^(depth - 8)

A frame whose samples are its 8-bit equivalent shifted left by
depth - 8 has a SAD of exactly SAD8 << (depth - 8) for every block, so
it is compared against the same thresholds and gives the same keep or
drop decision as the 8-bit frame. frac is a fraction of the number of
blocks and needs no scaling. The scaled thresholds are stored as
int64_t so that large user values do not overflow. 8-bit formats keep
the pixelutils SAD and unscaled thresholds, so their output is
unchanged.

Add FATE tests for yuv422p10le and yuv444p16le. They output the same
frames as the existing 8-bit filter-mpdecimate and
filter-mpdecimate-mode-1-min-3 tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 doc/filters.texi                             |  4 +-
 libavfilter/vf_mpdecimate.c                  | 82 ++++++++++++++------
 tests/fate/filter-video.mak                  |  6 ++
 tests/ref/fate/filter-mpdecimate-yuv422p10le | 25 ++++++
 tests/ref/fate/filter-mpdecimate-yuv444p16le | 12 +++
 5 files changed, 103 insertions(+), 26 deletions(-)
 create mode 100644 tests/ref/fate/filter-mpdecimate-yuv422p10le
 create mode 100644 tests/ref/fate/filter-mpdecimate-yuv444p16le

diff --git a/doc/filters.texi b/doc/filters.texi
index ec23628067a..9f1bf100465 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -19167,7 +19167,9 @@ Set the dropping threshold values.
 Values for @option{hi} and @option{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.
+out differently over the block. They are expressed for 8-bit samples
+and are scaled by 2^(depth-8) for higher bit depth formats, so that a
+high bit depth frame gives the same result as its 8-bit equivalent.
 
 A frame is a candidate for dropping if no 8x8 blocks differ by more
 than a threshold of @option{hi}, and if no more than @option{frac} blocks (1
diff --git a/libavfilter/vf_mpdecimate.c b/libavfilter/vf_mpdecimate.c
index 7523af2e86f..211093530e2 100644
--- a/libavfilter/vf_mpdecimate.c
+++ b/libavfilter/vf_mpdecimate.c
@@ -60,6 +60,8 @@ typedef struct DecimateContext {
     int dup_count;                 ///< for mode 1: number of duplicated frames
 
     int hsub, vsub;                ///< chroma subsampling values
+    int bpp;                       ///< bytes per sample
+    int64_t hi_thr, lo_thr;        ///< hi and lo scaled to the input bit depth
     AVFrame *ref;                  ///< reference picture
     av_pixelutils_sad_fn sad;      ///< sum of absolute difference function
 } DecimateContext;
@@ -83,6 +85,21 @@ static const AVOption mpdecimate_options[] = {
 
 AVFILTER_DEFINE_CLASS(mpdecimate);
 
+static int sad_8x8_16(const uint8_t *src1, ptrdiff_t stride1,
+                      const uint8_t *src2, ptrdiff_t stride2)
+{
+    int sum = 0;
+
+    for (int y = 0; y < 8; y++) {
+        const uint16_t *s1 = (const uint16_t *)(src1 + y * stride1);
+        const uint16_t *s2 = (const uint16_t *)(src2 + y * stride2);
+        for (int x = 0; x < 8; x++)
+            sum += FFABS(s1[x] - s2[x]);
+    }
+
+    return sum;
+}
+
 /**
  * Return 1 if the two planes are different, 0 otherwise.
  */
@@ -96,17 +113,18 @@ static int diff_planes(AVFilterContext *ctx,
     int x, y;
     int d, c = 0;
     int t = (w/16)*(h/16)*decimate->frac;
+    int bpp = decimate->bpp;
 
-    /* compute difference for blocks of 8x8 bytes */
+    /* compute difference for blocks of 8x8 samples */
     for (y = 0; y < h-7; y += 4) {
         for (x = 8; x < w-7; x += 4) {
-            d = decimate->sad(cur + y*cur_linesize + x, cur_linesize,
-                              ref + y*ref_linesize + x, ref_linesize);
-            if (d > decimate->hi) {
+            d = decimate->sad(cur + y*cur_linesize + x*bpp, cur_linesize,
+                              ref + y*ref_linesize + x*bpp, ref_linesize);
+            if (d > decimate->hi_thr) {
                 av_log(ctx, AV_LOG_DEBUG, "%d>=hi ", d);
                 return 1;
             }
-            if (d > decimate->lo) {
+            if (d > decimate->lo_thr) {
                 c++;
                 if (c > t) {
                     av_log(ctx, AV_LOG_DEBUG, "lo:%d>=%d ", c, t);
@@ -177,25 +195,6 @@ static DecimateResult decimate_frame(AVFilterContext *ctx, 
AVFrame *cur, AVFrame
     return DECIMATE_DROP;
 }
 
-static av_cold int init(AVFilterContext *ctx)
-{
-    DecimateContext *decimate = ctx->priv;
-
-    decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not 
aligned on blocksize
-    if (!decimate->sad)
-        return AVERROR(EINVAL);
-
-    if (decimate->mode == 1) {
-        av_log(ctx, AV_LOG_VERBOSE, "min_dup_count:%d hi:%d lo:%d frac:%f\n",
-           decimate->min_dup_count, decimate->hi, decimate->lo,
-           decimate->frac);
-    } else {
-        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);
-    }
-    return 0;
-}
-
 static av_cold void uninit(AVFilterContext *ctx)
 {
     DecimateContext *decimate = ctx->priv;
@@ -215,6 +214,21 @@ static const enum AVPixelFormat pix_fmts[] = {
     AV_PIX_FMT_YUVA444P,
     AV_PIX_FMT_YUVA422P,
 
+    AV_PIX_FMT_YUV420P9,     AV_PIX_FMT_YUV422P9,     AV_PIX_FMT_YUV444P9,
+    AV_PIX_FMT_YUV420P10,    AV_PIX_FMT_YUV422P10,    AV_PIX_FMT_YUV444P10,
+    AV_PIX_FMT_YUV420P12,    AV_PIX_FMT_YUV422P12,    AV_PIX_FMT_YUV444P12,
+    AV_PIX_FMT_YUV420P14,    AV_PIX_FMT_YUV422P14,    AV_PIX_FMT_YUV444P14,
+    AV_PIX_FMT_YUV420P16,    AV_PIX_FMT_YUV422P16,    AV_PIX_FMT_YUV444P16,
+    AV_PIX_FMT_YUV440P10,    AV_PIX_FMT_YUV440P12,
+
+    AV_PIX_FMT_YUVA420P9,    AV_PIX_FMT_YUVA422P9,    AV_PIX_FMT_YUVA444P9,
+    AV_PIX_FMT_YUVA420P10,   AV_PIX_FMT_YUVA422P10,   AV_PIX_FMT_YUVA444P10,
+                             AV_PIX_FMT_YUVA422P12,   AV_PIX_FMT_YUVA444P12,
+    AV_PIX_FMT_YUVA420P16,   AV_PIX_FMT_YUVA422P16,   AV_PIX_FMT_YUVA444P16,
+
+    AV_PIX_FMT_GBRP9,        AV_PIX_FMT_GBRP10,       AV_PIX_FMT_GBRP12,
+    AV_PIX_FMT_GBRP14,       AV_PIX_FMT_GBRP16,
+
     AV_PIX_FMT_NONE
 };
 
@@ -223,8 +237,27 @@ static int config_input(AVFilterLink *inlink)
     AVFilterContext *ctx = inlink->dst;
     DecimateContext *decimate = ctx->priv;
     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
+    int depth = pix_desc->comp[0].depth;
+    /* hi and lo are expressed in 8-bit units */
+    int64_t scale = 1LL << (depth - 8);
+
     decimate->hsub = pix_desc->log2_chroma_w;
     decimate->vsub = pix_desc->log2_chroma_h;
+    decimate->bpp  = depth > 8 ? 2 : 1;
+    decimate->hi_thr = decimate->hi * scale;
+    decimate->lo_thr = decimate->lo * scale;
+
+    if (depth > 8)
+        decimate->sad = sad_8x8_16;
+    else
+        decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not 
aligned on blocksize
+    if (!decimate->sad)
+        return AVERROR(EINVAL);
+
+    av_log(ctx, AV_LOG_VERBOSE, "%s:%d depth:%d hi:%"PRId64" lo:%"PRId64" 
frac:%f\n",
+           decimate->mode == 1 ? "min_dup_count" : "max_drop_count",
+           decimate->mode == 1 ? decimate->min_dup_count : 
decimate->max_drop_count,
+           depth, decimate->hi_thr, decimate->lo_thr, decimate->frac);
 
     return 0;
 }
@@ -341,7 +374,6 @@ const FFFilter ff_vf_mpdecimate = {
     .p.name        = "mpdecimate",
     .p.description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
     .p.priv_class  = &mpdecimate_class,
-    .init          = init,
     .uninit        = uninit,
     .priv_size     = sizeof(DecimateContext),
     FILTER_INPUTS(mpdecimate_inputs),
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 82ab48dc68d..bd3f8458bf9 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -508,6 +508,12 @@ fate-filter-mpdecimate-mode-1: CMD = framecrc -lavfi 
testsrc2=r=4:d=5,fps=5,mpde
 FATE_FILTER-$(call FILTERFRAMECRC, TESTSRC2 FPS MPDECIMATE) += 
fate-filter-mpdecimate-mode-1-min-3
 fate-filter-mpdecimate-mode-1-min-3: CMD = framecrc -lavfi 
testsrc2=r=2:d=7,fps=7,mpdecimate=mode=1:min=3 -pix_fmt yuv420p
 
+FATE_FILTER-$(call FILTERFRAMECRC, TESTSRC2 FPS FORMAT MPDECIMATE) += 
fate-filter-mpdecimate-yuv422p10le
+fate-filter-mpdecimate-yuv422p10le: CMD = framecrc -lavfi 
testsrc2=r=2:d=10,fps=3,format=yuv422p10le,mpdecimate -pix_fmt yuv422p10le
+
+FATE_FILTER-$(call FILTERFRAMECRC, TESTSRC2 FPS FORMAT MPDECIMATE) += 
fate-filter-mpdecimate-yuv444p16le
+fate-filter-mpdecimate-yuv444p16le: CMD = framecrc -lavfi 
testsrc2=r=2:d=7,fps=7,format=yuv444p16le,mpdecimate=mode=1:min=3 -pix_fmt 
yuv444p16le
+
 FATE_FILTER-$(call FILTERFRAMECRC, FPS TESTSRC2) += $(addprefix 
fate-filter-fps-, up up-round-down up-round-up down down-round-down 
down-round-up down-eof-pass start-drop start-fill single)
 fate-filter-fps-up: CMD = framecrc -lavfi testsrc2=r=3:d=2,fps=7
 fate-filter-fps-up-round-down: CMD = framecrc -lavfi 
testsrc2=r=3:d=2,fps=7:round=down
diff --git a/tests/ref/fate/filter-mpdecimate-yuv422p10le 
b/tests/ref/fate/filter-mpdecimate-yuv422p10le
new file mode 100644
index 00000000000..b9a6dcf76dd
--- /dev/null
+++ b/tests/ref/fate/filter-mpdecimate-yuv422p10le
@@ -0,0 +1,25 @@
+#tb 0: 1/3
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 1/1
+0,          0,          0,        1,   307200, 0xb937e930
+0,          2,          2,        1,   307200, 0xc0c24d0f
+0,          3,          3,        1,   307200, 0x67ed4f61
+0,          5,          5,        1,   307200, 0xcdd6dcc5
+0,          6,          6,        1,   307200, 0xb7f417b4
+0,          8,          8,        1,   307200, 0x99a4867f
+0,          9,          9,        1,   307200, 0x613313cc
+0,         11,         11,        1,   307200, 0x860273b4
+0,         12,         12,        1,   307200, 0x476b3048
+0,         14,         14,        1,   307200, 0xf5c5d0c4
+0,         15,         15,        1,   307200, 0x496be7d7
+0,         17,         17,        1,   307200, 0x6e1a2536
+0,         18,         18,        1,   307200, 0xd08b3c0b
+0,         20,         20,        1,   307200, 0x9fd2aab6
+0,         21,         21,        1,   307200, 0x1764308b
+0,         23,         23,        1,   307200, 0xb21ca3fd
+0,         24,         24,        1,   307200, 0xbff185d2
+0,         26,         26,        1,   307200, 0x6809989e
+0,         27,         27,        1,   307200, 0xad470a25
+0,         29,         29,        1,   307200, 0xa81d8d0f
diff --git a/tests/ref/fate/filter-mpdecimate-yuv444p16le 
b/tests/ref/fate/filter-mpdecimate-yuv444p16le
new file mode 100644
index 00000000000..1e7242c16b8
--- /dev/null
+++ b/tests/ref/fate/filter-mpdecimate-yuv444p16le
@@ -0,0 +1,12 @@
+#tb 0: 1/7
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 1/1
+0,          0,          0,        1,   460800, 0xc78e7cff
+0,          7,          7,        1,   460800, 0x60d46cff
+0,         14,         14,        1,   460800, 0x2ffcb265
+0,         21,         21,        1,   460800, 0x45989596
+0,         28,         28,        1,   460800, 0x08dba3c0
+0,         35,         35,        1,   460800, 0x34387ec3
+0,         42,         42,        1,   460800, 0x86347054
-- 
2.54.0


From a68c7c0f24734a05ab4b3ff47cec2ea96b627cc5 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Tue, 10 Nov 2026 22:08:57 -0800
Subject: [PATCH 2/2] avfilter/x86: add SSE2 16-bit SAD for mpdecimate

The high bit depth path of mpdecimate uses a C 8x8 SAD on 16-bit
samples. Its speed depends on the compiler vectorizing it, which
configure disables for GCC older than 13.

Add an SSE2 version handling the full 16-bit range, and a checkasm
test for the function.

x86_64 clang build run under Rosetta 2, 300 static 3840x2160
yuv422p10le frames through mpdecimate:

    C, not vectorized (-fno-vectorize -fno-slp-vectorize): 6.1s
    C, vectorized by clang:                                2.4s
    SSE2:                                                  2.3s

checkasm --bench, same setup, against the non-vectorized C:

    sad_8x8_16_10_c:      19.3
    sad_8x8_16_10_sse2:    6.0 ( 3.13x)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 libavfilter/mpdecimate.h             | 32 +++++++++++++
 libavfilter/vf_mpdecimate.c          | 16 ++++++-
 libavfilter/x86/Makefile             |  2 +
 libavfilter/x86/vf_mpdecimate.asm    | 54 +++++++++++++++++++++
 libavfilter/x86/vf_mpdecimate_init.c | 35 ++++++++++++++
 tests/checkasm/Makefile              |  1 +
 tests/checkasm/checkasm.c            |  3 ++
 tests/checkasm/checkasm.h            |  1 +
 tests/checkasm/vf_mpdecimate.c       | 71 ++++++++++++++++++++++++++++
 tests/fate/checkasm.mak              |  1 +
 10 files changed, 215 insertions(+), 1 deletion(-)
 create mode 100644 libavfilter/mpdecimate.h
 create mode 100644 libavfilter/x86/vf_mpdecimate.asm
 create mode 100644 libavfilter/x86/vf_mpdecimate_init.c
 create mode 100644 tests/checkasm/vf_mpdecimate.c

diff --git a/libavfilter/mpdecimate.h b/libavfilter/mpdecimate.h
new file mode 100644
index 00000000000..d8728792ab3
--- /dev/null
+++ b/libavfilter/mpdecimate.h
@@ -0,0 +1,32 @@
+/*
+ * 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_MPDECIMATE_H
+#define AVFILTER_MPDECIMATE_H
+
+#include "libavutil/pixelutils.h"
+
+/**
+ * Get the SAD function for 8x8 blocks of 16-bit samples.
+ * Strides are in bytes.
+ */
+av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn(void);
+
+av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn_x86(void);
+
+#endif /* AVFILTER_MPDECIMATE_H */
diff --git a/libavfilter/vf_mpdecimate.c b/libavfilter/vf_mpdecimate.c
index 211093530e2..a43a58fdec8 100644
--- a/libavfilter/vf_mpdecimate.c
+++ b/libavfilter/vf_mpdecimate.c
@@ -25,12 +25,15 @@
  * Rich Felker.
  */
 
+#include "config.h"
+
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixelutils.h"
 #include "libavutil/timestamp.h"
 #include "avfilter.h"
 #include "filters.h"
+#include "mpdecimate.h"
 #include "video.h"
 
 typedef enum {
@@ -100,6 +103,17 @@ static int sad_8x8_16(const uint8_t *src1, ptrdiff_t 
stride1,
     return sum;
 }
 
+av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn(void)
+{
+    av_pixelutils_sad_fn sad = NULL;
+
+#if ARCH_X86 && HAVE_X86ASM
+    sad = ff_mpdecimate_get_sad16_fn_x86();
+#endif
+
+    return sad ? sad : sad_8x8_16;
+}
+
 /**
  * Return 1 if the two planes are different, 0 otherwise.
  */
@@ -248,7 +262,7 @@ static int config_input(AVFilterLink *inlink)
     decimate->lo_thr = decimate->lo * scale;
 
     if (depth > 8)
-        decimate->sad = sad_8x8_16;
+        decimate->sad = ff_mpdecimate_get_sad16_fn();
     else
         decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not 
aligned on blocksize
     if (!decimate->sad)
diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile
index cfbed4317c8..895b33f483f 100644
--- a/libavfilter/x86/Makefile
+++ b/libavfilter/x86/Makefile
@@ -43,6 +43,8 @@ X86ASM-OBJS-$(CONFIG_MASKEDCLAMP_FILTER)     += 
x86/vf_maskedclamp.o          \
                                                 x86/vf_maskedclamp_init.o
 X86ASM-OBJS-$(CONFIG_MASKEDMERGE_FILTER)     += x86/vf_maskedmerge.o          \
                                                 x86/vf_maskedmerge_init.o
+X86ASM-OBJS-$(CONFIG_MPDECIMATE_FILTER)      += x86/vf_mpdecimate.o           \
+                                                x86/vf_mpdecimate_init.o
 X86ASM-OBJS-$(CONFIG_NLMEANS_FILTER)         += x86/vf_nlmeans.o              \
                                                 x86/vf_nlmeans_init.o
 X86ASM-OBJS-$(CONFIG_OVERLAY_FILTER)         += x86/vf_overlay.o              \
diff --git a/libavfilter/x86/vf_mpdecimate.asm 
b/libavfilter/x86/vf_mpdecimate.asm
new file mode 100644
index 00000000000..1780585aa9b
--- /dev/null
+++ b/libavfilter/x86/vf_mpdecimate.asm
@@ -0,0 +1,54 @@
+;*****************************************************************************
+;* x86-optimized functions for mpdecimate filter
+;*
+;* 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
+;******************************************************************************
+
+%include "libavutil/x86/x86util.asm"
+
+SECTION .text
+
+;------------------------------------------------------------------------------
+; int ff_mpdecimate_sad_8x8_16(const uint8_t *src1, ptrdiff_t stride1,
+;                              const uint8_t *src2, ptrdiff_t stride2);
+;
+; SAD of an 8x8 block of unaligned 16-bit samples, strides in bytes.
+;------------------------------------------------------------------------------
+INIT_XMM sse2
+cglobal mpdecimate_sad_8x8_16, 4, 5, 5, src1, stride1, src2, stride2, cnt
+    pxor            m0, m0
+    pxor            m4, m4
+    mov           cntd, 8
+.loop:
+    movu            m1, [src1q]
+    movu            m2, [src2q]
+    mova            m3, m1
+    psubusw         m1, m2
+    psubusw         m2, m3
+    por             m1, m2          ; |src1 - src2|, full 16-bit range
+    mova            m2, m1
+    punpcklwd       m1, m4
+    punpckhwd       m2, m4
+    paddd           m0, m1
+    paddd           m0, m2
+    add          src1q, stride1q
+    add          src2q, stride2q
+    dec           cntd
+    jg .loop
+    HADDD           m0, m1
+    movd           eax, m0
+    RET
diff --git a/libavfilter/x86/vf_mpdecimate_init.c 
b/libavfilter/x86/vf_mpdecimate_init.c
new file mode 100644
index 00000000000..583277e1b26
--- /dev/null
+++ b/libavfilter/x86/vf_mpdecimate_init.c
@@ -0,0 +1,35 @@
+/*
+ * 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
+ */
+
+#include "libavutil/attributes.h"
+#include "libavutil/cpu.h"
+#include "libavutil/x86/cpu.h"
+#include "libavfilter/mpdecimate.h"
+
+int ff_mpdecimate_sad_8x8_16_sse2(const uint8_t *src1, ptrdiff_t stride1,
+                                  const uint8_t *src2, ptrdiff_t stride2);
+
+av_cold av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn_x86(void)
+{
+    int cpu_flags = av_get_cpu_flags();
+
+    if (EXTERNAL_SSE2(cpu_flags))
+        return ff_mpdecimate_sad_8x8_16_sse2;
+
+    return NULL;
+}
diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile
index ab1cc17b56c..28d7d8400c0 100644
--- a/tests/checkasm/Makefile
+++ b/tests/checkasm/Makefile
@@ -82,6 +82,7 @@ AVFILTEROBJS-$(CONFIG_GBLUR_FILTER)      += vf_gblur.o
 AVFILTEROBJS-$(CONFIG_HFLIP_FILTER)      += vf_hflip.o
 AVFILTEROBJS-$(CONFIG_IDET_FILTER)       += vf_idet.o
 AVFILTEROBJS-$(CONFIG_LIMITER_FILTER)    += vf_limiter.o
+AVFILTEROBJS-$(CONFIG_MPDECIMATE_FILTER) += vf_mpdecimate.o
 AVFILTEROBJS-$(CONFIG_PP7_FILTER)        += vf_pp7.o
 AVFILTEROBJS-$(CONFIG_THRESHOLD_FILTER)  += vf_threshold.o
 AVFILTEROBJS-$(CONFIG_NLMEANS_FILTER)    += vf_nlmeans.o
diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index e50d2885a3a..6e60cba2410 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -285,6 +285,9 @@ static const CheckasmTest tests[] = {
     #if CONFIG_IDET_FILTER
         { "vf_idet", checkasm_check_idet },
     #endif
+    #if CONFIG_MPDECIMATE_FILTER
+        { "vf_mpdecimate", checkasm_check_mpdecimate },
+    #endif
     #if CONFIG_NLMEANS_FILTER
         { "vf_nlmeans", checkasm_check_nlmeans },
     #endif
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index 0fc6823c4fb..e00e5c63ea9 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -82,6 +82,7 @@ void checkasm_check_llvidencdsp(void);
 void checkasm_check_lpc(void);
 void checkasm_check_motion(void);
 void checkasm_check_mpeg4videodsp(void);
+void checkasm_check_mpdecimate(void);
 void checkasm_check_mpegvideo_unquantize(void);
 void checkasm_check_mpegvideoencdsp(void);
 void checkasm_check_nlmeans(void);
diff --git a/tests/checkasm/vf_mpdecimate.c b/tests/checkasm/vf_mpdecimate.c
new file mode 100644
index 00000000000..1a74cc5ca56
--- /dev/null
+++ b/tests/checkasm/vf_mpdecimate.c
@@ -0,0 +1,71 @@
+/*
+ * 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 "checkasm.h"
+
+#include "libavfilter/mpdecimate.h"
+#include "libavutil/mem_internal.h"
+
+#define STRIDE 32
+
+static void check_sad16(int depth)
+{
+    LOCAL_ALIGNED_32(uint16_t, src1, [STRIDE * 9]);
+    LOCAL_ALIGNED_32(uint16_t, src2, [STRIDE * 9]);
+    const int mask = (1 << depth) - 1;
+
+    declare_func(int, const uint8_t *src1, ptrdiff_t stride1,
+                      const uint8_t *src2, ptrdiff_t stride2);
+
+    for (int i = 0; i < STRIDE * 9; i++) {
+        src1[i] = rnd() & mask;
+        src2[i] = rnd() & mask;
+    }
+    if (depth == 16) {
+        /* largest possible SAD */
+        for (int i = 0; i < STRIDE * 9; i++) {
+            src1[i] = 0;
+            src2[i] = 0xffff;
+        }
+    }
+
+    if (check_func(ff_mpdecimate_get_sad16_fn(), "sad_8x8_16_%d", depth)) {
+        /* blocks are not aligned in the filter */
+        for (int off = 0; off < 2; off++) {
+            const uint8_t *p1 = (const uint8_t *)(src1 + off * 3);
+            const uint8_t *p2 = (const uint8_t *)(src2 + off * 5);
+            int ref = call_ref(p1, STRIDE * 2, p2, STRIDE * 2);
+            int new = call_new(p1, STRIDE * 2, p2, STRIDE * 2);
+            if (ref != new) {
+                fprintf(stderr, "sad_8x8_16_%d: %d != %d\n", depth, ref, new);
+                fail();
+            }
+        }
+        bench_new((const uint8_t *)src1, STRIDE * 2,
+                  (const uint8_t *)src2, STRIDE * 2);
+    }
+}
+
+void checkasm_check_mpdecimate(void)
+{
+    static const int depths[] = { 10, 12, 15, 16 };
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(depths); i++)
+        check_sad16(depths[i]);
+    report("sad_8x8_16");
+}
diff --git a/tests/fate/checkasm.mak b/tests/fate/checkasm.mak
index 19bbe7e571a..df996cccecc 100644
--- a/tests/fate/checkasm.mak
+++ b/tests/fate/checkasm.mak
@@ -88,6 +88,7 @@ FATE_CHECKASM = fate-checkasm-aacencdsp                       
          \
                 fate-checkasm-vf_hflip                                  \
                 fate-checkasm-vf_idet                                   \
                                fate-checkasm-vf_limiter                        
        \
+                fate-checkasm-vf_mpdecimate                             \
                 fate-checkasm-vf_nlmeans                                \
                 fate-checkasm-vf_pp7                                    \
                 fate-checkasm-vf_threshold                              \
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to