Commit 49d7f2b61d for ffmpeg
commit 49d7f2b61d455d3676060fe996824c2fc91fd199
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Tue Nov 10 21:51:31 2026 -0800
avfilter/mpdecimate: add high bit depth support
hi and lo are scaled by 2^(depth-8), so samples shifted left from 8-bit
get the same keep/drop decisions.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/doc/filters.texi b/doc/filters.texi
index 98d3e9f640..84993c59f3 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -19179,7 +19179,11 @@ 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. For otherwise
+identical input, this preserves keep/drop decisions when the high bit
+depth samples are the corresponding 8-bit samples shifted left by
+depth-8.
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 f3ea47c5c2..37cd8cb017 100644
--- a/libavfilter/vf_mpdecimate.c
+++ b/libavfilter/vf_mpdecimate.c
@@ -61,6 +61,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
+ int 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;
@@ -84,32 +86,48 @@ 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.
* The reason is appended to bp, which the caller prints with the frame's
* keep/drop decision in a single av_log() call.
*/
-static int diff_planes(AVFilterContext *ctx, AVBPrint *bp,
- uint8_t *cur, int cur_linesize,
- uint8_t *ref, int ref_linesize,
- int w, int h)
+static av_always_inline int diff_planes(AVFilterContext *ctx, AVBPrint *bp,
+ uint8_t *cur, int cur_linesize,
+ uint8_t *ref, int ref_linesize,
+ int w, int h, int bpp)
{
DecimateContext *decimate = ctx->priv;
int x, y;
int d, c = 0;
int t = (w/16)*(h/16)*decimate->frac;
+ int hi_thr = decimate->hi_thr, lo_thr = decimate->lo_thr;
- /* 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 > hi_thr) {
av_bprintf(bp, "%d>=hi ", d);
return 1;
}
- if (d > decimate->lo) {
+ if (d > lo_thr) {
c++;
if (c > t) {
av_bprintf(bp, "lo:%d>=%d ", c, t);
@@ -123,6 +141,20 @@ static int diff_planes(AVFilterContext *ctx, AVBPrint *bp,
return 0;
}
+static int diff_planes_8(AVFilterContext *ctx, AVBPrint *bp,
+ uint8_t *cur, int cur_linesize,
+ uint8_t *ref, int ref_linesize, int w, int h)
+{
+ return diff_planes(ctx, bp, cur, cur_linesize, ref, ref_linesize, w, h, 1);
+}
+
+static int diff_planes_16(AVFilterContext *ctx, AVBPrint *bp,
+ uint8_t *cur, int cur_linesize,
+ uint8_t *ref, int ref_linesize, int w, int h)
+{
+ return diff_planes(ctx, bp, cur, cur_linesize, ref, ref_linesize, w, h, 2);
+}
+
/**
* Tell if the frame is different with respect to the reference frame ref.
*/
@@ -130,6 +162,10 @@ static int is_frame_different(AVFilterContext *ctx, AVBPrint *bp,
AVFrame *cur, AVFrame *ref)
{
DecimateContext *decimate = ctx->priv;
+ int (*diff)(AVFilterContext *ctx, AVBPrint *bp,
+ uint8_t *cur, int cur_linesize,
+ uint8_t *ref, int ref_linesize, int w, int h) =
+ decimate->bpp == 2 ? diff_planes_16 : diff_planes_8;
int plane;
for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
@@ -140,11 +176,11 @@ static int is_frame_different(AVFilterContext *ctx, AVBPrint *bp,
*/
int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
- if (diff_planes(ctx, bp,
- cur->data[plane], cur->linesize[plane],
- ref->data[plane], ref->linesize[plane],
- AV_CEIL_RSHIFT(ref->width, hsub),
- AV_CEIL_RSHIFT(ref->height, vsub)))
+ if (diff(ctx, bp,
+ cur->data[plane], cur->linesize[plane],
+ ref->data[plane], ref->linesize[plane],
+ AV_CEIL_RSHIFT(ref->width, hsub),
+ AV_CEIL_RSHIFT(ref->height, vsub)))
return 1;
}
@@ -181,25 +217,6 @@ static DecimateResult decimate_frame(AVFilterContext *ctx, AVBPrint *bp,
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;
@@ -219,6 +236,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
};
@@ -227,8 +259,28 @@ 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;
+ /* an 8x8 SAD is at most 64 * 65535, so clipping to int keeps the decisions */
+ decimate->hi_thr = av_clip64(decimate->hi * scale, INT_MIN, INT_MAX);
+ decimate->lo_thr = av_clip64(decimate->lo * scale, INT_MIN, INT_MAX);
+
+ 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 * scale, decimate->lo * scale, decimate->frac);
return 0;
}
@@ -355,7 +407,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 5ffb4bb341..8091ee8ab2 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -508,6 +508,18 @@ 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 SCALE MPDECIMATE) += fate-filter-mpdecimate-yuv422p10le
+fate-filter-mpdecimate-yuv422p10le: CMD = framecrc -lavfi testsrc2=r=2:d=10,fps=3,format=yuv422p10le,scale,mpdecimate,scale -pix_fmt yuv422p10le
+
+FATE_FILTER-$(call FILTERFRAMECRC, TESTSRC2 FPS FORMAT SCALE MPDECIMATE) += fate-filter-mpdecimate-yuv444p16le
+fate-filter-mpdecimate-yuv444p16le: CMD = framecrc -lavfi testsrc2=r=2:d=7,fps=7,format=yuv444p16le,scale,mpdecimate=mode=1:min=3,scale -pix_fmt yuv444p16le
+
+FATE_FILTER-$(call FILTERFRAMECRC, NULLSRC SCALE FORMAT GEQ MPDECIMATE) += fate-filter-mpdecimate-hi-10bit
+fate-filter-mpdecimate-hi-10bit: CMD = framecrc -lavfi nullsrc=s=32x16:r=1:d=6,scale,format=yuv444p10,geq=lum=400+24*N:cb=512:cr=512,mpdecimate=hi=768:lo=2147483647,scale -pix_fmt yuv444p10le
+
+FATE_FILTER-$(call FILTERFRAMECRC, NULLSRC SCALE FORMAT GEQ MPDECIMATE) += fate-filter-mpdecimate-lo-10bit
+fate-filter-mpdecimate-lo-10bit: CMD = framecrc -lavfi nullsrc=s=32x16:r=1:d=6,scale,format=yuv444p10,geq=lum=400+20*N:cb=512:cr=512,mpdecimate=hi=2147483647:lo=320,scale -pix_fmt yuv444p10le
+
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-hi-10bit b/tests/ref/fate/filter-mpdecimate-hi-10bit
new file mode 100644
index 0000000000..4bb9470efe
--- /dev/null
+++ b/tests/ref/fate/filter-mpdecimate-hi-10bit
@@ -0,0 +1,7 @@
+#tb 0: 1/1
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 32x16
+#sar 0: 1/1
+0, 0, 0, 1, 3072, 0xcbdb2a0f
+0, 3, 3, 1, 3072, 0xb04aba0f
diff --git a/tests/ref/fate/filter-mpdecimate-lo-10bit b/tests/ref/fate/filter-mpdecimate-lo-10bit
new file mode 100644
index 0000000000..def75be207
--- /dev/null
+++ b/tests/ref/fate/filter-mpdecimate-lo-10bit
@@ -0,0 +1,8 @@
+#tb 0: 1/1
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 32x16
+#sar 0: 1/1
+0, 0, 0, 1, 3072, 0xcbdb2a0f
+0, 2, 2, 1, 3072, 0x4aca7a0f
+0, 4, 4, 1, 3072, 0xc9aaca0f
diff --git a/tests/ref/fate/filter-mpdecimate-yuv422p10le b/tests/ref/fate/filter-mpdecimate-yuv422p10le
new file mode 100644
index 0000000000..b9a6dcf76d
--- /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 0000000000..1e7242c16b
--- /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