Commit 0e553f74a9 for ffmpeg

commit 0e553f74a9122085abc3f9ecd975c4cbe06850c1
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Tue Nov 10 21:52:13 2026 -0800

    avfilter/f_select: accept high bit depth gray, YUV and RGB for scene detection

    Avoids a full-frame conversion in front of select for inputs such as
    10-bit 4:2:2 ProRes; the score stays on the same 0 to 1 scale.

    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>

diff --git a/libavfilter/f_select.c b/libavfilter/f_select.c
index 04eef59c37..2548e1bcc9 100644
--- a/libavfilter/f_select.c
+++ b/libavfilter/f_select.c
@@ -272,7 +272,12 @@ static int config_input(AVFilterLink *inlink)
         inlink->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;

     if (CONFIG_SELECT_FILTER && select->do_scene_detect) {
-        select->sad = ff_scene_sad_get_fn(select->bitdepth);
+        int is_be = !!(desc->flags & AV_PIX_FMT_FLAG_BE);
+
+        if (select->bitdepth > 8 && is_be != HAVE_BIGENDIAN)
+            select->sad = ff_scene_sad_bswap_get_fn();
+        else
+            select->sad = ff_scene_sad_get_fn(select->bitdepth);
         if (!select->sad)
             return AVERROR(EINVAL);
     }
@@ -518,6 +523,29 @@ const FFFilter ff_af_aselect = {

 #if CONFIG_SELECT_FILTER

+/**
+ * The scene score of a gray or YUV frame only looks at the luma plane, so
+ * any subsampling works as long as luma is a plane of its own holding
+ * LSB-aligned samples of 8 to 16 bits.
+ */
+static int is_scene_luma_format(const AVPixFmtDescriptor *desc)
+{
+    const AVComponentDescriptor *luma = &desc->comp[0];
+
+    if (desc->flags & (AV_PIX_FMT_FLAG_HWACCEL | AV_PIX_FMT_FLAG_BITSTREAM |
+                       AV_PIX_FMT_FLAG_PAL     | AV_PIX_FMT_FLAG_RGB       |
+                       AV_PIX_FMT_FLAG_FLOAT   | AV_PIX_FMT_FLAG_BAYER))
+        return 0;
+    if (desc->nb_components != 1 &&
+        (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR) || desc->nb_components < 3))
+        return 0;
+    if (luma->depth < 8 || luma->depth > 16)
+        return 0;
+
+    return luma->plane == 0 && luma->offset == 0 && luma->shift == 0 &&
+           luma->step == (luma->depth > 8 ? 2 : 1);
+}
+
 static int query_formats(const AVFilterContext *ctx,
                          AVFilterFormatsConfig **cfg_in,
                          AVFilterFormatsConfig **cfg_out)
@@ -525,15 +553,28 @@ static int query_formats(const AVFilterContext *ctx,
     const SelectContext *select = ctx->priv;

     if (select->do_scene_detect) {
-        static const enum AVPixelFormat pix_fmts[] = {
+        static const enum AVPixelFormat rgb_fmts[] = {
             AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
-            AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, AV_PIX_FMT_GRAY8,
-            AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
-            AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
-            AV_PIX_FMT_YUV420P10,
-            AV_PIX_FMT_NONE
+            AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
+            AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
+            AV_PIX_FMT_BGR48LE, AV_PIX_FMT_BGR48BE,
+            AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
+            AV_PIX_FMT_BGRA64LE, AV_PIX_FMT_BGRA64BE,
         };
-        return ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts);
+        const AVPixFmtDescriptor *desc = NULL;
+        AVFilterFormats *formats = NULL;
+        int ret;
+
+        for (int i = 0; i < FF_ARRAY_ELEMS(rgb_fmts); i++)
+            if ((ret = ff_add_format(&formats, rgb_fmts[i])) < 0)
+                return ret;
+
+        while ((desc = av_pix_fmt_desc_next(desc))) {
+            if (is_scene_luma_format(desc) &&
+                (ret = ff_add_format(&formats, av_pix_fmt_desc_get_id(desc))) < 0)
+                return ret;
+        }
+        return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats);
     }
     return 0;
 }
diff --git a/libavfilter/scene_sad.c b/libavfilter/scene_sad.c
index a6087611b8..0bf32b62f4 100644
--- a/libavfilter/scene_sad.c
+++ b/libavfilter/scene_sad.c
@@ -21,6 +21,7 @@
  * Scene SAD functions
  */

+#include "libavutil/bswap.h"
 #include "scene_sad.h"

 void ff_scene_sad16_c(SCENE_SAD_PARAMS)
@@ -42,6 +43,25 @@ void ff_scene_sad16_c(SCENE_SAD_PARAMS)
     *sum = sad;
 }

+void ff_scene_sad16_bswap_c(SCENE_SAD_PARAMS)
+{
+    uint64_t sad = 0;
+    const uint16_t *src1w = (const uint16_t *)src1;
+    const uint16_t *src2w = (const uint16_t *)src2;
+    int x, y;
+
+    stride1 /= 2;
+    stride2 /= 2;
+
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++)
+            sad += FFABS(av_bswap16(src1w[x]) - av_bswap16(src2w[x]));
+        src1w += stride1;
+        src2w += stride2;
+    }
+    *sum = sad;
+}
+
 void ff_scene_sad_c(SCENE_SAD_PARAMS)
 {
     uint64_t sad = 0;
@@ -73,3 +93,8 @@ ff_scene_sad_fn ff_scene_sad_get_fn(int depth)
     }
     return sad;
 }
+
+ff_scene_sad_fn ff_scene_sad_bswap_get_fn(void)
+{
+    return ff_scene_sad16_bswap_c;
+}
diff --git a/libavfilter/scene_sad.h b/libavfilter/scene_sad.h
index cfa3509afc..06281ffbc5 100644
--- a/libavfilter/scene_sad.h
+++ b/libavfilter/scene_sad.h
@@ -37,10 +37,17 @@ void ff_scene_sad_c(SCENE_SAD_PARAMS);

 void ff_scene_sad16_c(SCENE_SAD_PARAMS);

+/**
+ * SAD of 9 to 16 bit samples stored in the byte order the CPU does not use.
+ */
+void ff_scene_sad16_bswap_c(SCENE_SAD_PARAMS);
+
 ff_scene_sad_fn ff_scene_sad_get_fn_x86(int depth);

 ff_scene_sad_fn ff_scene_sad_get_fn_riscv(int depth);

 ff_scene_sad_fn ff_scene_sad_get_fn(int depth);

+ff_scene_sad_fn ff_scene_sad_bswap_get_fn(void);
+
 #endif /* AVFILTER_SCENE_SAD_H */
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 7862b54981..c2d42067f4 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -917,6 +917,14 @@ FATE_METADATA_FILTER-$(call ALLYES, $(SCENEDETECT_DEPS)) += fate-filter-metadata
 fate-filter-metadata-scenedetect: SRC = $(TARGET_SAMPLES)/svq3/Vertical400kbit.sorenson3.mov
 fate-filter-metadata-scenedetect: CMD = run $(FILTER_METADATA_COMMAND) "sws_flags=+accurate_rnd+bitexact;movie='$(SRC)',select=gt(scene\,.25)"

+define FATE_SCENEDETECT_PIXFMT
+FATE_METADATA_FILTER-$$(call ALLYES, $$(SCENEDETECT_DEPS) FORMAT_FILTER) += fate-filter-metadata-scenedetect-$(1)
+fate-filter-metadata-scenedetect-$(1): SRC = $$(TARGET_SAMPLES)/svq3/Vertical400kbit.sorenson3.mov
+fate-filter-metadata-scenedetect-$(1): CMD = run ffprobe$$(PROGSSUF)$$(EXESUF) -of compact=p=0 -show_entries frame=pts,pix_fmt:frame_tags -bitexact -f lavfi "sws_flags=+accurate_rnd+bitexact;movie='$$(SRC)',format=$(1),select=gt(scene\,.25)"
+endef
+
+$(foreach FMT,gray10le rgb48le rgb48be yuv422p10be yuv422p10le yuv444p16le,$(eval $(call FATE_SCENEDETECT_PIXFMT,$(FMT))))
+
 SCDET_DEPS = LAVFI_INDEV MOVIE_FILTER SCDET_FILTER SCALE_FILTER \
                    MOV_DEMUXER SVQ3_DECODER ZLIB
 FATE_METADATA_FILTER-$(call ALLYES, $(SCDET_DEPS)) += fate-filter-metadata-scdet
diff --git a/tests/ref/fate/filter-metadata-scenedetect-gray10le b/tests/ref/fate/filter-metadata-scenedetect-gray10le
new file mode 100644
index 0000000000..645b6a4051
--- /dev/null
+++ b/tests/ref/fate/filter-metadata-scenedetect-gray10le
@@ -0,0 +1,11 @@
+pts=1620|pix_fmt=gray10|tag:lavfi.scene_score=1.000000
+pts=4140|pix_fmt=gray10|tag:lavfi.scene_score=0.926262
+pts=5800|pix_fmt=gray10|tag:lavfi.scene_score=1.000000
+pts=6720|pix_fmt=gray10|tag:lavfi.scene_score=0.477555
+pts=8160|pix_fmt=gray10|tag:lavfi.scene_score=1.000000
+pts=9760|pix_fmt=gray10|tag:lavfi.scene_score=1.000000
+pts=14080|pix_fmt=gray10|tag:lavfi.scene_score=0.878129
+pts=15700|pix_fmt=gray10|tag:lavfi.scene_score=1.000000
+pts=18500|pix_fmt=gray10|tag:lavfi.scene_score=0.424530
+pts=20040|pix_fmt=gray10|tag:lavfi.scene_score=0.353944
+pts=21760|pix_fmt=gray10|tag:lavfi.scene_score=1.000000
diff --git a/tests/ref/fate/filter-metadata-scenedetect-rgb48be b/tests/ref/fate/filter-metadata-scenedetect-rgb48be
new file mode 100644
index 0000000000..e0c68a7b78
--- /dev/null
+++ b/tests/ref/fate/filter-metadata-scenedetect-rgb48be
@@ -0,0 +1,11 @@
+pts=1620|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=4140|pix_fmt=rgb48|tag:lavfi.scene_score=0.872199
+pts=5800|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=6720|pix_fmt=rgb48|tag:lavfi.scene_score=0.461832
+pts=8160|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=9760|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=14080|pix_fmt=rgb48|tag:lavfi.scene_score=0.841219
+pts=15700|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=18500|pix_fmt=rgb48|tag:lavfi.scene_score=0.474949
+pts=20040|pix_fmt=rgb48|tag:lavfi.scene_score=0.380118
+pts=21760|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
diff --git a/tests/ref/fate/filter-metadata-scenedetect-rgb48le b/tests/ref/fate/filter-metadata-scenedetect-rgb48le
new file mode 100644
index 0000000000..e0c68a7b78
--- /dev/null
+++ b/tests/ref/fate/filter-metadata-scenedetect-rgb48le
@@ -0,0 +1,11 @@
+pts=1620|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=4140|pix_fmt=rgb48|tag:lavfi.scene_score=0.872199
+pts=5800|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=6720|pix_fmt=rgb48|tag:lavfi.scene_score=0.461832
+pts=8160|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=9760|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=14080|pix_fmt=rgb48|tag:lavfi.scene_score=0.841219
+pts=15700|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
+pts=18500|pix_fmt=rgb48|tag:lavfi.scene_score=0.474949
+pts=20040|pix_fmt=rgb48|tag:lavfi.scene_score=0.380118
+pts=21760|pix_fmt=rgb48|tag:lavfi.scene_score=1.000000
diff --git a/tests/ref/fate/filter-metadata-scenedetect-yuv422p10be b/tests/ref/fate/filter-metadata-scenedetect-yuv422p10be
new file mode 100644
index 0000000000..0f1734c1ab
--- /dev/null
+++ b/tests/ref/fate/filter-metadata-scenedetect-yuv422p10be
@@ -0,0 +1,11 @@
+pts=1620|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=4140|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.923403
+pts=5800|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=6720|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.475643
+pts=8160|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=9760|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=14080|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.874623
+pts=15700|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=18500|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.422509
+pts=20040|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.352360
+pts=21760|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
diff --git a/tests/ref/fate/filter-metadata-scenedetect-yuv422p10le b/tests/ref/fate/filter-metadata-scenedetect-yuv422p10le
new file mode 100644
index 0000000000..0f1734c1ab
--- /dev/null
+++ b/tests/ref/fate/filter-metadata-scenedetect-yuv422p10le
@@ -0,0 +1,11 @@
+pts=1620|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=4140|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.923403
+pts=5800|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=6720|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.475643
+pts=8160|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=9760|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=14080|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.874623
+pts=15700|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
+pts=18500|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.422509
+pts=20040|pix_fmt=yuv422p10|tag:lavfi.scene_score=0.352360
+pts=21760|pix_fmt=yuv422p10|tag:lavfi.scene_score=1.000000
diff --git a/tests/ref/fate/filter-metadata-scenedetect-yuv444p16le b/tests/ref/fate/filter-metadata-scenedetect-yuv444p16le
new file mode 100644
index 0000000000..0d16a990a0
--- /dev/null
+++ b/tests/ref/fate/filter-metadata-scenedetect-yuv444p16le
@@ -0,0 +1,11 @@
+pts=1620|pix_fmt=yuv444p16|tag:lavfi.scene_score=1.000000
+pts=4140|pix_fmt=yuv444p16|tag:lavfi.scene_score=0.923403
+pts=5800|pix_fmt=yuv444p16|tag:lavfi.scene_score=1.000000
+pts=6720|pix_fmt=yuv444p16|tag:lavfi.scene_score=0.475643
+pts=8160|pix_fmt=yuv444p16|tag:lavfi.scene_score=1.000000
+pts=9760|pix_fmt=yuv444p16|tag:lavfi.scene_score=1.000000
+pts=14080|pix_fmt=yuv444p16|tag:lavfi.scene_score=0.874623
+pts=15700|pix_fmt=yuv444p16|tag:lavfi.scene_score=1.000000
+pts=18500|pix_fmt=yuv444p16|tag:lavfi.scene_score=0.422509
+pts=20040|pix_fmt=yuv444p16|tag:lavfi.scene_score=0.352360
+pts=21760|pix_fmt=yuv444p16|tag:lavfi.scene_score=1.000000