PR #25926 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25926 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25926.patch
`select` with a `scene` expression only accepted gray8, a few 8-bit YUV layouts, yuv420p10 and 8-bit packed RGB. Any other input, for example 10-bit 4:2:2 ProRes, got a full-frame swscale conversion in front of the filter just to compute the score. **Commit 1, avfilter/f_select** - Builds the format list from the pixel format descriptors: any non-hwaccel gray or YUV format whose luma is a separate plane of LSB-aligned 8 to 16 bit samples (planar 4:1:0 to 4:4:4, alpha and full-range variants, NV and P016-style semi-planar). The score only reads the luma plane, so nothing else was needed. - Adds rgb48, bgr48, rgba64 and bgra64. - Accepts both byte orders, through a new byte-swapping SAD in `scene_sad` (C). - The score normalisation is unchanged (SAD / 2^(depth - 8)), so thresholds keep their meaning. Formats that were accepted before negotiate and score exactly as they did. Inputs that still need a conversion may be converted to a closer format than before; in particular high bit depth RGB is now scored on RGB samples instead of on luma after a conversion to yuv420p10. - MSB-aligned formats such as p010 are still converted. - New FATE tests: gray10le, rgb48le, rgb48be, yuv422p10le, yuv422p10be, yuv444p16le. **Commit 2, avfilter/x86/scene_sad** - AVX2 and AVX-512 versions of the byte-swapping SAD, built by giving the existing macros a bswap argument. Safe for the full 16-bit range. - checkasm test added. **Testing** - FATE scenedetect, select, scdet and checkasm-scene_sad pass on aarch64 (macOS). - `checkasm --test=scene_sad` passes on a Xeon Platinum 8488C for AVX2 and AVX-512, and scene scores match C with `-cpuflags 0`. ``` checkasm --bench, Xeon Platinum 8488C, GCC 15.2 scene_sad16_bswap_c 54485.0 scene_sad16_bswap_avx2 12851.7 ( 4.20x) scene_sad16_bswap_avx512 8883.0 ( 6.10x) ``` With a 4K ProRes 422 HQ source on an Apple M3 Max, a graph splitting the decoder output to `select='gt(scene,0.4)'`, `idet`, `cropdetect` and `blackdetect` goes from 1.65s to 1.27s for 240 frames. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 130e1c14a5314d6113bc62a0d38d964db4249b68 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 10 Nov 2026 21:52:13 -0800 Subject: [PATCH 1/2] avfilter/f_select: accept high bit depth gray, YUV and RGB for scene detection The scene score of a gray or YUV frame is computed on the luma plane only, and both the SAD functions and the score normalisation already handle any depth up to 16 bits. The format list was nevertheless limited to gray8, a few 8-bit YUV layouts and yuv420p10, so every other input, such as 10-bit 4:2:2 ProRes masters, got a full-frame swscale conversion in front of the filter just to compute the score. Build the list from the pixel format descriptors instead and accept every non-hwaccel gray or YUV format whose luma is a separate plane of LSB-aligned 8 to 16 bit samples. That covers gray, the planar 4:1:0 to 4:4:4 formats, their alpha and full-range variants, and the NV and P016-style semi-planar formats. MSB-aligned formats such as p010 are still converted. Also add the 16-bit counterparts of the packed RGB entries, rgb48, bgr48, rgba64 and bgra64, which the existing packed path handles as is. Samples in the byte order the CPU does not use, such as the rgb48be frames output by the PNG decoder, go through a new byte-swapping SAD in scene_sad instead of being converted first. The SAD is divided by 2^(depth - 8), so a high bit depth frame scores on the same 0 to 1 scale as its 8-bit equivalent and thresholds such as gt(scene,0.4) keep their meaning. Formats that were accepted before negotiate and score exactly as they did. Inputs that still need a conversion may now be converted to a closer format, so their scores can change slightly. In particular, high bit depth RGB inputs used to be converted to yuv420p10 and scored on luma, and are now scored on their RGB samples like 8-bit RGB. With a 4K ProRes 422 HQ source on an Apple M3 Max, a graph splitting the decoder output to select='gt(scene,0.4)', idet, cropdetect and blackdetect goes from 1.65s to 1.27s for 240 frames. Add FATE tests for gray10le, rgb48le, rgb48be, yuv422p10le, yuv422p10be and yuv444p16le, which also print the negotiated pixel format. Co-Authored-By: Claude Fable 5.2 <[email protected]> --- libavfilter/f_select.c | 57 ++++++++++++++++--- libavfilter/scene_sad.c | 25 ++++++++ libavfilter/scene_sad.h | 7 +++ tests/fate/filter-video.mak | 8 +++ .../fate/filter-metadata-scenedetect-gray10le | 11 ++++ .../fate/filter-metadata-scenedetect-rgb48be | 11 ++++ .../fate/filter-metadata-scenedetect-rgb48le | 11 ++++ .../filter-metadata-scenedetect-yuv422p10be | 11 ++++ .../filter-metadata-scenedetect-yuv422p10le | 11 ++++ .../filter-metadata-scenedetect-yuv444p16le | 11 ++++ 10 files changed, 155 insertions(+), 8 deletions(-) create mode 100644 tests/ref/fate/filter-metadata-scenedetect-gray10le create mode 100644 tests/ref/fate/filter-metadata-scenedetect-rgb48be create mode 100644 tests/ref/fate/filter-metadata-scenedetect-rgb48le create mode 100644 tests/ref/fate/filter-metadata-scenedetect-yuv422p10be create mode 100644 tests/ref/fate/filter-metadata-scenedetect-yuv422p10le create mode 100644 tests/ref/fate/filter-metadata-scenedetect-yuv444p16le diff --git a/libavfilter/f_select.c b/libavfilter/f_select.c index c1c93fd9732..c38e9dcc8f2 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); } @@ -517,6 +522,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) @@ -524,15 +552,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 a6087611b8b..0bf32b62f49 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 cfa3509afcf..06281ffbc59 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 82ab48dc68d..8f353e0157a 100644 --- a/tests/fate/filter-video.mak +++ b/tests/fate/filter-video.mak @@ -893,6 +893,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 00000000000..645b6a40514 --- /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 00000000000..e0c68a7b785 --- /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 00000000000..e0c68a7b785 --- /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 00000000000..0f1734c1ab3 --- /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 00000000000..0f1734c1ab3 --- /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 00000000000..0d16a990a0e --- /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 -- 2.54.0 From 3ef8ee52cd9078e6ae171472e03410bdc7b0b13b Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 10 Nov 2026 22:25:54 -0800 Subject: [PATCH 2/2] avfilter/x86/scene_sad: add AVX2 and AVX-512 byte-swapped 16-bit SAD Scene detection in the select filter uses ff_scene_sad16_bswap_c for samples in the byte order the CPU does not use, such as the rgb48be frames output by the PNG decoder. Add AVX2 and AVX-512 versions of it by giving the existing SAD macros a bswap argument. The loop, the row handling and the widening of the sums are shared with the 9 to 15 bit functions. Only the byte swap and the absolute difference are new, and the latter uses unsigned min and max so that the function is safe for the full 16-bit range. Add a checkasm test for the new function. checkasm --bench on a Xeon Platinum 8488C with GCC 15.2 scene_sad16_bswap_c 54485.0 scene_sad16_bswap_avx2 12851.7 ( 4.20x) scene_sad16_bswap_avx512 8883.0 ( 6.10x) Co-Authored-By: Claude Fable 5.2 <[email protected]> --- libavfilter/scene_sad.c | 6 +++++- libavfilter/scene_sad.h | 2 ++ libavfilter/x86/scene_sad.asm | 36 +++++++++++++++++++++++++------- libavfilter/x86/scene_sad_init.c | 30 ++++++++++++++++++++------ tests/checkasm/scene_sad.c | 14 ++++++++----- 5 files changed, 69 insertions(+), 19 deletions(-) diff --git a/libavfilter/scene_sad.c b/libavfilter/scene_sad.c index 0bf32b62f49..82259a3a615 100644 --- a/libavfilter/scene_sad.c +++ b/libavfilter/scene_sad.c @@ -96,5 +96,9 @@ ff_scene_sad_fn ff_scene_sad_get_fn(int depth) ff_scene_sad_fn ff_scene_sad_bswap_get_fn(void) { - return ff_scene_sad16_bswap_c; + ff_scene_sad_fn sad = NULL; +#if ARCH_X86 && HAVE_X86ASM + sad = ff_scene_sad_bswap_get_fn_x86(); +#endif + return sad ? sad : ff_scene_sad16_bswap_c; } diff --git a/libavfilter/scene_sad.h b/libavfilter/scene_sad.h index 06281ffbc59..2495a080996 100644 --- a/libavfilter/scene_sad.h +++ b/libavfilter/scene_sad.h @@ -48,6 +48,8 @@ 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_x86(void); + ff_scene_sad_fn ff_scene_sad_bswap_get_fn(void); #endif /* AVFILTER_SCENE_SAD_H */ diff --git a/libavfilter/x86/scene_sad.asm b/libavfilter/x86/scene_sad.asm index 181c2908935..c38d35da3e2 100644 --- a/libavfilter/x86/scene_sad.asm +++ b/libavfilter/x86/scene_sad.asm @@ -27,25 +27,45 @@ SECTION_RODATA pw_1: times 32 dw 1 +pb_bswap16: times 4 db 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14 SECTION .text -%macro SAD_INIT 1 ; depth +%macro SAD_INIT 1-2 0 ; depth, bswap +%if %2 +cglobal scene_sad%1_bswap, 6, 7, 6, src1, stride1, src2, stride2, width, end, x + movu m3, [pb_bswap16] + pxor m5, m5 +%else cglobal scene_sad%1, 6, 7, 3, src1, stride1, src2, stride2, width, end, x +%endif add src1q, widthq add src2q, widthq neg widthq pxor m1, m1 %endmacro -%macro PSADQ 4 ; depth, dst, [src2], tmp +%macro PSADQ 4-5 0 ; depth, dst, [src2], tmp, bswap %if %1 == 8 psadbw %2, %3 %else + %if %5 + ; byte-swapped samples, safe for the full 16-bit range + movu %4, %3 + pshufb %2, m3 + pshufb %4, m3 + pminuw m4, %2, %4 + pmaxuw %2, %4 + psubw %2, m4 + punpckhwd %4, %2, m5 + punpcklwd %2, m5 + paddd %2, %4 + %else psubw %2, %3 pabsw %2, %2 pmaddwd %2, [pw_1] + %endif %if mmsize == 64 vextracti32x8 ymm%4, %2, 1 paddd ymm%2, ymm%4 @@ -58,13 +78,13 @@ cglobal scene_sad%1, 6, 7, 3, src1, stride1, src2, stride2, width, end, x %endif %endmacro -%macro SAD_LOOP 1 ; depth +%macro SAD_LOOP 1-2 0 ; depth, bswap .nextrow: mov xq, widthq .loop: movu m0, [src1q + xq] - PSADQ %1, m0, [src2q + xq], m2 + PSADQ %1, m0, [src2q + xq], m2, %2 paddq m1, m0 add xq, mmsize jl .loop @@ -79,9 +99,9 @@ RET %endmacro -%macro SAD_FRAMES 1 ; depth - SAD_INIT %1 - SAD_LOOP %1 +%macro SAD_FRAMES 1-2 0 ; depth, bswap + SAD_INIT %1, %2 + SAD_LOOP %1, %2 %endmacro @@ -93,6 +113,7 @@ SAD_FRAMES 8 INIT_YMM avx2 SAD_FRAMES 8 SAD_FRAMES 16 +SAD_FRAMES 16, 1 %endif @@ -101,5 +122,6 @@ SAD_FRAMES 16 INIT_ZMM avx512 SAD_FRAMES 8 SAD_FRAMES 16 +SAD_FRAMES 16, 1 %endif diff --git a/libavfilter/x86/scene_sad_init.c b/libavfilter/x86/scene_sad_init.c index a35f02c5600..2b80b33e89b 100644 --- a/libavfilter/x86/scene_sad_init.c +++ b/libavfilter/x86/scene_sad_init.c @@ -36,7 +36,7 @@ static void FUNC_NAME(SCENE_SAD_PARAMS) { \ *sum += sad[0]; \ } -#define SCENE_SAD16_FUNC(FUNC_NAME, ASM_FUNC_NAME, MMSIZE) \ +#define SCENE_SAD16_FUNC(FUNC_NAME, ASM_FUNC_NAME, C_FUNC_NAME, MMSIZE) \ void ASM_FUNC_NAME(SCENE_SAD_PARAMS); \ \ static void FUNC_NAME(SCENE_SAD_PARAMS) { \ @@ -46,20 +46,24 @@ static void FUNC_NAME(SCENE_SAD_PARAMS) { \ ASM_FUNC_NAME(src1, stride1, src2, stride2, bytes, height, sad); \ for (int i = 0; i < MMSIZE / 8; i++) \ *sum += sad[i]; \ - ff_scene_sad16_c(src1 + bytes, stride1, \ - src2 + bytes, stride2, \ - width - (bytes >> 1), height, sad); \ + C_FUNC_NAME(src1 + bytes, stride1, \ + src2 + bytes, stride2, \ + width - (bytes >> 1), height, sad); \ *sum += sad[0]; \ } SCENE_SAD_FUNC(scene_sad_sse2, ff_scene_sad8_sse2, 16) #if HAVE_AVX2_EXTERNAL SCENE_SAD_FUNC(scene_sad_avx2, ff_scene_sad8_avx2, 32) -SCENE_SAD16_FUNC(scene_sad16_avx2, ff_scene_sad16_avx2, 32) +SCENE_SAD16_FUNC(scene_sad16_avx2, ff_scene_sad16_avx2, ff_scene_sad16_c, 32) +SCENE_SAD16_FUNC(scene_sad16_bswap_avx2, ff_scene_sad16_bswap_avx2, + ff_scene_sad16_bswap_c, 32) #endif #if HAVE_AVX512_EXTERNAL SCENE_SAD_FUNC(scene_sad_avx512, ff_scene_sad8_avx512, 64) -SCENE_SAD16_FUNC(scene_sad16_avx512, ff_scene_sad16_avx512, 64) +SCENE_SAD16_FUNC(scene_sad16_avx512, ff_scene_sad16_avx512, ff_scene_sad16_c, 64) +SCENE_SAD16_FUNC(scene_sad16_bswap_avx512, ff_scene_sad16_bswap_avx512, + ff_scene_sad16_bswap_c, 64) #endif ff_scene_sad_fn ff_scene_sad_get_fn_x86(int depth) @@ -88,3 +92,17 @@ ff_scene_sad_fn ff_scene_sad_get_fn_x86(int depth) } return NULL; } + +ff_scene_sad_fn ff_scene_sad_bswap_get_fn_x86(void) +{ + int cpu_flags = av_get_cpu_flags(); +#if HAVE_AVX512_EXTERNAL + if (EXTERNAL_AVX512(cpu_flags)) + return scene_sad16_bswap_avx512; +#endif +#if HAVE_AVX2_EXTERNAL + if (EXTERNAL_AVX2_FAST(cpu_flags)) + return scene_sad16_bswap_avx2; +#endif + return NULL; +} diff --git a/tests/checkasm/scene_sad.c b/tests/checkasm/scene_sad.c index bc33c428c63..fd3e265e1ab 100644 --- a/tests/checkasm/scene_sad.c +++ b/tests/checkasm/scene_sad.c @@ -32,7 +32,7 @@ tmp_buf[j] = rnd() & mask; \ } while (0) -static void check_scene_sad(int depth) +static void check_scene_sad(int depth, int bswap) { LOCAL_ALIGNED_32(uint8_t, src1, [WIDTH * HEIGHT * 2]); LOCAL_ALIGNED_32(uint8_t, src2, [WIDTH * HEIGHT * 2]); @@ -50,13 +50,15 @@ static void check_scene_sad(int depth) randomize_buffers(uint16_t, src2, width * HEIGHT, mask); } - if (check_func(ff_scene_sad_get_fn(depth), "scene_sad%d", depth)) { + if (check_func(bswap ? ff_scene_sad_bswap_get_fn() : ff_scene_sad_get_fn(depth), + "scene_sad%d%s", depth, bswap ? "_bswap" : "")) { uint64_t sum1, sum2; call_ref(src1, STRIDE, src2, STRIDE, width, HEIGHT, &sum1); call_new(src1, STRIDE, src2, STRIDE, width, HEIGHT, &sum2); if (sum1 != sum2) { - fprintf(stderr, "scene_sad%d: sum mismatch: %llu != %llu\n", - depth, (unsigned long long) sum1, (unsigned long long) sum2); + fprintf(stderr, "scene_sad%d%s: sum mismatch: %llu != %llu\n", + depth, bswap ? "_bswap" : "", + (unsigned long long) sum1, (unsigned long long) sum2); fail(); } bench_new(src1, STRIDE, src2, STRIDE, WIDTH, HEIGHT, &sum2); @@ -67,7 +69,9 @@ void checkasm_check_scene_sad(void) { const int depths[] = { 8, 10, 12, 14, 15, 16 }; for (int i = 0; i < FF_ARRAY_ELEMS(depths); i++) { - check_scene_sad(depths[i]); + check_scene_sad(depths[i], 0); report("scene_sad%d", depths[i]); } + check_scene_sad(16, 1); + report("scene_sad16_bswap"); } -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
