ffmpeg-devel
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
November 2026
- 11 participants
- 290 discussions
[PR] avcodec/d3d12va_encode_h264: enable 8x8 transform for High profiles (PR #25934)
by witaly-iwanow 11 Nov '26
by witaly-iwanow 11 Nov '26
11 Nov '26
PR #25934 opened by witaly-iwanow
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25934
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25934.patch
Fixes #25928.
From 52aa4d67561ad2c25bf8c8f7f230c7f6828db5c6 Mon Sep 17 00:00:00 2001
From: Vitaly Ivanov <vivanov(a)networkoptix.com>
Date: Wed, 11 Nov 2026 15:48:44 +0700
Subject: [PATCH] avcodec/d3d12va_encode_h264: enable 8x8 transform for High
profiles
ff_hw_base_encode_init_params_h264() sets transform_8x8_mode_flag in the
PPS for High profiles, but the encoder was never configured with
USE_ADAPTIVE_8x8_TRANSFORM, so drivers encoded without it and the output
did not match its PPS.
Enable it when the driver supports it and derive the PPS flag from the
configuration.
Fixes #25928.
Signed-off-by: Vitaly Ivanov <vivanov(a)networkoptix.com>
---
libavcodec/d3d12va_encode_h264.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libavcodec/d3d12va_encode_h264.c b/libavcodec/d3d12va_encode_h264.c
index 55ed2490647..c65c18863a3 100644
--- a/libavcodec/d3d12va_encode_h264.c
+++ b/libavcodec/d3d12va_encode_h264.c
@@ -260,6 +260,8 @@ static int d3d12va_encode_h264_init_sequence_params(AVCodecContext *avctx)
ctx->gop.pH264GroupOfPictures->log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4;
pps->deblocking_filter_control_present_flag = 1;
pps->constrained_intra_pred_flag = priv->constrained_intra_pred;
+ pps->transform_8x8_mode_flag = !!(ctx->codec_conf.pH264Config->ConfigurationFlags &
+ D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_ADAPTIVE_8x8_TRANSFORM);
return 0;
}
@@ -333,6 +335,16 @@ static int d3d12va_encode_h264_get_encoder_caps(AVCodecContext *avctx)
}
}
+ // 8x8 transform, which the PPS signals for High profiles
+ if (ctx->profile->av_profile == AV_PROFILE_H264_HIGH ||
+ ctx->profile->av_profile == AV_PROFILE_H264_HIGH_10) {
+ if (h264_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_ADAPTIVE_8x8_TRANSFORM_ENCODING_SUPPORT) {
+ config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_ADAPTIVE_8x8_TRANSFORM;
+ } else {
+ av_log(avctx, AV_LOG_WARNING, "Adaptive 8x8 transform is not supported by the driver, disabling.\n");
+ }
+ }
+
base_ctx->surface_width = FFALIGN(avctx->width, 16);
base_ctx->surface_height = FFALIGN(avctx->height, 16);
--
2.54.0
1
0
[PR] avcodec/d3d12va_encode: keep the sequence header out of the bitstream buffer (PR #25933)
by witaly-iwanow 11 Nov '26
by witaly-iwanow 11 Nov '26
11 Nov '26
PR #25933 opened by witaly-iwanow
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25933
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25933.patch
Fixes #25923: on Intel, `h264_d3d12va` IDR packets lost their SPS/PPS and part of the slice, because the driver writes the frame at offset 0 regardless of `FrameStartOffset`. The sequence header now stays in the picture and is prepended to the packet, and the frame is encoded at offset 0.
From a84e0081bc0465fb6b2fbbc5adbf44b5e99265f2 Mon Sep 17 00:00:00 2001
From: Vitaly Ivanov <vivanov(a)networkoptix.com>
Date: Wed, 11 Nov 2026 14:47:11 +0700
Subject: [PATCH] avcodec/d3d12va_encode: keep the sequence header out of the
bitstream buffer
The header was written to the start of the output buffer and the frame
placed after it with FrameStartOffset. The Intel H.264 encoder ignores
that offset and writes the frame at 0, over the header, so every IDR
lost its SPS/PPS and part of its slice data.
Keep the header in the picture and prepend it to the packet instead,
encoding at offset 0. This also removes a read of aligned_header_size
bytes from a MAX_PARAM_BUFFER_SIZE stack buffer.
Fixes #25923.
Signed-off-by: Vitaly Ivanov <vivanov(a)networkoptix.com>
---
libavcodec/d3d12va_encode.c | 29 +++++++----------------------
libavcodec/d3d12va_encode.h | 3 ++-
libavcodec/d3d12va_encode_av1.c | 4 ++--
3 files changed, 11 insertions(+), 25 deletions(-)
diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
index 19a6901c357..2243c6db742 100644
--- a/libavcodec/d3d12va_encode.c
+++ b/libavcodec/d3d12va_encode.c
@@ -325,8 +325,6 @@ static int d3d12va_encode_issue(AVCodecContext *avctx,
AVD3D12VAFramesContext *frames_hwctx = base_ctx->input_frames->hwctx;
int err, i, j;
HRESULT hr;
- char data[MAX_PARAM_BUFFER_SIZE];
- void *ptr;
size_t bit_len;
ID3D12CommandAllocator *command_allocator = NULL;
ID3D12VideoEncodeCommandList2 *cmd_list = ctx->command_list;
@@ -367,8 +365,6 @@ static int d3d12va_encode_issue(AVCodecContext *avctx,
D3D12_VIDEO_ENCODER_RESOLVE_METADATA_OUTPUT_ARGUMENTS output_metadata = { 0 };
- memset(data, 0, sizeof(data));
-
av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
"as type %s.\n", base_pic->display_order, base_pic->encode_order,
ff_hw_base_encode_get_pictype_name(base_pic->type));
@@ -446,28 +442,18 @@ static int d3d12va_encode_issue(AVCodecContext *avctx,
}
}
+ // The header stays out of the output buffer: some drivers (Intel H.264)
+ // write the frame at offset 0 regardless of FrameStartOffset.
if (base_pic->type == FF_HW_PICTURE_TYPE_IDR) {
if (ctx->codec->write_sequence_header) {
- bit_len = 8 * sizeof(data);
- err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
+ bit_len = 8 * sizeof(pic->header_data);
+ err = ctx->codec->write_sequence_header(avctx, pic->header_data, &bit_len);
if (err < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
"header: %d.\n", err);
goto fail;
}
pic->header_size = (int)bit_len / 8;
- pic->aligned_header_size = pic->header_size % ctx->req.CompressedBitstreamBufferAccessAlignment ?
- FFALIGN(pic->header_size, ctx->req.CompressedBitstreamBufferAccessAlignment) :
- pic->header_size;
-
- hr = ID3D12Resource_Map(pic->output_buffer, 0, NULL, (void **)&ptr);
- if (FAILED(hr)) {
- err = AVERROR_UNKNOWN;
- goto fail;
- }
-
- memcpy(ptr, data, pic->aligned_header_size);
- ID3D12Resource_Unmap(pic->output_buffer, 0, NULL);
}
}
@@ -510,10 +496,10 @@ static int d3d12va_encode_issue(AVCodecContext *avctx,
input_args.PictureControlDesc.PictureControlCodecData = pic->pic_ctl;
input_args.PictureControlDesc.ReferenceFrames = d3d12_refs;
- input_args.CurrentFrameBitstreamMetadataSize = pic->aligned_header_size;
+ input_args.CurrentFrameBitstreamMetadataSize = pic->header_size;
output_args.Bitstream.pBuffer = pic->output_buffer;
- output_args.Bitstream.FrameStartOffset = pic->aligned_header_size;
+ output_args.Bitstream.FrameStartOffset = 0;
output_args.ReconstructedPicture.pReconstructedPicture = pic->recon_surface->texture;
output_args.ReconstructedPicture.ReconstructedPictureSubresource = ctx->is_texture_array ? pic->subresource_index : 0;
output_args.EncoderOutputMetadata.pBuffer = pic->encoded_metadata;
@@ -874,10 +860,9 @@ static int d3d12va_encode_get_coded_data(AVCodecContext *avctx,
goto end;
ptr = pkt->data;
- memcpy(ptr, mapped_data, pic->header_size);
+ memcpy(ptr, pic->header_data, pic->header_size);
ptr += pic->header_size;
- mapped_data += pic->aligned_header_size;
total_size -= pic->header_size;
memcpy(ptr, mapped_data, total_size);
diff --git a/libavcodec/d3d12va_encode.h b/libavcodec/d3d12va_encode.h
index bfc5dd55786..41d6aaafb78 100644
--- a/libavcodec/d3d12va_encode.h
+++ b/libavcodec/d3d12va_encode.h
@@ -40,8 +40,9 @@ extern const AVCodecHWConfigInternal *const ff_d3d12va_encode_hw_configs[];
#define D3D12VA_VIDEO_ENC_ASYNC_DEPTH 8
typedef struct D3D12VAEncodePicture {
+ // Sequence header of an IDR frame, prepended to the coded data on output.
+ char header_data[MAX_PARAM_BUFFER_SIZE];
int header_size;
- int aligned_header_size;
AVD3D12VAFrame *input_surface;
AVD3D12VAFrame *recon_surface;
diff --git a/libavcodec/d3d12va_encode_av1.c b/libavcodec/d3d12va_encode_av1.c
index 0b89c02bf21..96a5be0ca67 100644
--- a/libavcodec/d3d12va_encode_av1.c
+++ b/libavcodec/d3d12va_encode_av1.c
@@ -546,7 +546,7 @@ static int d3d12va_encode_av1_get_coded_data(AVCodecContext *avctx,
}
uint8_t *dst = tile_group_buf;
- uint8_t *tiles_base = mapped_data + pic->aligned_header_size;
+ uint8_t *tiles_base = mapped_data;
uint64_t src_off = 0;
for (uint64_t i = 0; i < nb_subregions; i++) {
/* Tiles are laid out contiguously by their full size; the actual
@@ -614,7 +614,7 @@ static int d3d12va_encode_av1_get_coded_data(AVCodecContext *avctx,
ptr = pkt->data + prev_stash_sz;
}
- memcpy(ptr, mapped_data, pic->header_size);
+ memcpy(ptr, pic->header_data, pic->header_size);
ptr += pic->header_size;
memcpy(ptr, pic_hd_data, av1_pic_hd_size);
--
2.54.0
1
0
11 Nov '26
PR #25932 opened by Kilian (lusoris)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25932
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25932.patch
This fixes an out-of-bounds read in the libx264 wrapper: with interlaced or fake-interlaced encoding, an odd macroblock row count, `mb_info=1` and video hint side data, x264 reads one row of `mb_info` past the map that `setup_mb_info()` allocates.
## Cause
`setup_mb_info()` allocates `mb_width * mb_height` entries, with `mb_height` the frame's own macroblock row count. x264 rounds its row count up to an even number when interlaced or fake-interlaced encoding is on (`x264_sps_init()` in `encoder/set.c` sets `i_mb_height` that way when `b_frame_mbs_only` is off) and reads `mb_info` over that grid, for example in `x264_frame_deblock_row()` (`common/deblock.c`). With an odd count it reads one row past the allocation.
Pull request 25909 fixes the same sizing for the ROI offset map in `setup_roi()`; its review pointed out this one.
## Change
`setup_mb_info()` now allocates the map for an even row count, as the review of 25909 suggested. The fill keeps its extent, so it leaves the added row at 0, which x264 reads as not constant, the conservative choice.
## Testing
The reproducer is a small API program that encodes four 4:2:0 frames with libx264, `mb_info=1` and an `AV_VIDEO_HINT_TYPE_CHANGED` hint with no rectangles on each frame, and prints the MD5 of all packets. FFmpeg master 08d6ed2100 and x264 0480cb05 are both built with AddressSanitizer.
- master: with `-x264-params interlaced=1` or `fake-interlaced=1` at 64x48 and 48x80, and with `interlaced=1` and one 32x32 hint rectangle at 64x48, AddressSanitizer reports a heap-buffer-overflow read of 1 byte in `x264_frame_deblock_row()` (`common/deblock.c:580`), just past the map allocated in `setup_mb_info()`. 64x64, 96x32 and progressive encoding run clean.
- With this change, all 14 runs (three modes at four sizes, plus the rectangle case progressive and interlaced) exit 0 without an AddressSanitizer report.
- The MD5 is identical between master and this change for the nine runs whose grid does not round: progressive at all four sizes, both interlaced modes at 64x64 and 96x32, and the progressive rectangle case.
This touches the same file as pull requests 25875 and 25909, in a different function; whichever lands later will be rebased.
From 4d7de8b464458cd24f2029a6b05f15cc45c3a9d9 Mon Sep 17 00:00:00 2001
From: lusoris <lusoris(a)proton.me>
Date: Wed, 11 Nov 2026 03:42:22 +0100
Subject: [PATCH] avcodec/libx264: size the mb_info map for interlaced encoding
x264 rounds its macroblock row count up to an even number for
interlaced and fake-interlaced encoding and reads mb_info over that
grid. setup_mb_info() allocated the map for the frame's own row count,
so with an odd count x264 read one row past the allocation: at 64x48
with -x264-params interlaced=1 or fake-interlaced=1 and video hint
side data, AddressSanitizer reports a heap-buffer-overflow in
x264_frame_deblock_row().
Allocate the map for an even row count. The extra row stays 0, so x264
treats those macroblocks as not constant, and the output for sizes and
modes that do not round is unchanged.
Found in review of pull request 25909 (ROI offset map).
Signed-off-by: lusoris <lusoris(a)proton.me>
---
libavcodec/libx264.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index a432c0b50e5..fa90e00232e 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -362,6 +362,10 @@ static int setup_mb_info(AVCodecContext *ctx, x264_picture_t *pic,
{
int mb_width = (frame->width + MB_SIZE - 1) / MB_SIZE;
int mb_height = (frame->height + MB_SIZE - 1) / MB_SIZE;
+ /* x264 rounds the macroblock row count up to an even number for
+ * interlaced and fake-interlaced encoding and reads mb_info
+ * over that grid, so size the map for it. */
+ int mb_height_alloc = FFALIGN(mb_height, 2);
const AVVideoRect *mbinfo_rects;
int nb_rects;
@@ -370,7 +374,7 @@ static int setup_mb_info(AVCodecContext *ctx, x264_picture_t *pic,
mbinfo_rects = (const AVVideoRect *)av_video_hint_rects(info);
nb_rects = info->nb_rects;
- mbinfo = av_calloc(mb_width * mb_height, sizeof(*mbinfo));
+ mbinfo = av_calloc(mb_width * mb_height_alloc, sizeof(*mbinfo));
if (!mbinfo)
return AVERROR(ENOMEM);
--
2.54.0
1
0
[PR] avformat/movenc: allow negative CTS offsets with edit lists in CMAF output (PR #25931)
by Baptiste Coudurier 11 Nov '26
by Baptiste Coudurier 11 Nov '26
11 Nov '26
PR #25931 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25931
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25931.patch
Since cf0244aa38 (issue #23417), enabling edit lists with `-movflags cmaf` (e.g. via `+delay_moov`, or `-use_editlist 1`) disables negative CTS offsets for all tracks, even when `negative_cts_offsets` is explicitly set. That is correct for the case reported there: a CMAF video track shall not use both signed composition offsets and an edit list (ISO/IEC 23000-19, 9.2.5).
It however leaves no way to write video tracks with version 1 `trun` boxes and no edit list, which is the form 9.2.5 requires outside of CMAF track files and that the `cmf2` brand mandates (7.7.2, 7.7.3), while still writing the offset edit list needed for audio priming (10.2.6, 10.3.3.2).
### Changes
- **allow negative CTS offsets with edit lists in CMAF output**: when `negative_cts_offsets` is explicitly set together with `cmaf`, keep the signed offsets and don't write an edit list for the video tracks; the other tracks keep theirs. If such a video track doesn't start at pts 0, a warning is printed, as this can't be signaled without an edit list.
- **signal the cmf2 brand**: write `cmf2` next to `cmfc` when CMAF output uses negative CTS offsets, as 7.2 recommends listing all the structural brands a track conforms to.
The behavior of plain `-movflags cmaf`, with or without edit lists, and of an explicit `-use_editlist 1`, is unchanged, apart from the additional compatible brand when negative CTS offsets are already in use.
### Result
`-movflags cmaf+delay_moov+negative_cts_offsets+frag_keyframe`, H.264 with B-frames + AAC:
| | before | after |
|---|---|---|
| video `trun` | version 0, unsigned offsets | version 1, signed offsets, earliest pts of each fragment == `baseMediaDecodeTime` (7.5.17) |
| video edit list | `media_time` = reorder delay | none |
| audio edit list | single entry, `media_time` = priming | same |
| brands | `cmfc` | `cmfc`, `cmf2` |
### Testing
- New `cmaf-negative-cts-elst` case in `fate-api-movenc`. The `cmf2` brand grows the `ftyp` by 4 bytes, which changes the `api-movenc` and `mov-mp4-fragmented-ttml-stpp` references.
- Full `make fate` passes locally, except tests whose samples are missing from my local sample set.
- Apple's `avmediainfo` reads the output with video starting at 0 and the audio priming trimmed, and `mediastreamvalidator` 2.0.284 reports no media issues on single-track files referenced by byte-range HLS playlists.
The clause numbers are from the ISO/IEC 23000-19 4th edition CD text (WG 03 N1847).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
From 28f1af224bd94f95b05ec4a2856362da0ef708ba Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <baptiste.coudurier(a)gmail.com>
Date: Tue, 10 Nov 2026 23:19:25 -0800
Subject: [PATCH 1/2] avformat/movenc: allow negative CTS offsets with edit
lists in CMAF output
Since cf0244aa38, enabling edit lists with the cmaf flag disables negative
CTS offsets for all the tracks, even when the negative_cts_offsets flag was
explicitly set, as a CMAF video track shall not use both signed composition
offsets and an edit list (ISO/IEC 23000-19, 9.2.5).
This made it impossible to use version 1 trun boxes for the video tracks,
as required outside of CMAF track files and by the cmf2 brand, while still
writing the offset edit list needed to signal audio priming.
When negative_cts_offsets is explicitly set together with cmaf, keep the
signed offsets, and don't write an edit list for the video tracks instead.
The default behavior is unchanged.
Co-Authored-By: Claude Fable 5.2 <noreply(a)anthropic.com>
---
doc/muxers.texi | 7 +++++++
libavformat/movenc.c | 23 +++++++++++++++++++++--
tests/api/api-movenc-test.c | 10 ++++++++++
tests/ref/fate/api-movenc | 5 +++++
4 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi
index a2489760fcd..3b3bc18a13d 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -491,6 +491,13 @@ Set various muxing switches. The following flags can be used:
write CMAF (Common Media Application Format) compatible fragmented
MP4 output
+This implicitly sets @samp{negative_cts_offsets}, unless edit lists are
+written (see @option{use_editlist}), in which case all the tracks get an
+edit list and unsigned CTS offsets are used, which CMAF only allows in
+CMAF track files. Setting @samp{negative_cts_offsets} explicitly together
+with this flag keeps the negative CTS offsets and writes edit lists only
+for the non-video tracks, e.g. to signal audio priming.
+
@item dash
write DASH (Dynamic Adaptive Streaming over HTTP) compatible fragmented
MP4 output
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index ea71acde42a..d6fe7491540 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4542,7 +4542,20 @@ static int mov_write_trak_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext
av_assert2(mov->use_editlist >= 0);
if (track->start_dts != AV_NOPTS_VALUE) {
- if (mov->use_editlist)
+ /* A CMAF video track shall not use both signed composition offsets
+ * and an edit list (ISO/IEC 23000-19, 9.2.5). The signed offsets
+ * already make the first sample start at its decode time. */
+ if (mov->use_editlist && mov->flags & FF_MOV_FLAG_CMAF &&
+ mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS &&
+ track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
+ if (track->start_cts != AV_NOPTS_VALUE &&
+ track->start_dts + track->start_cts)
+ av_log(mov->fc, AV_LOG_WARNING,
+ "Track %d starts at pts %"PRId64", which can't be "
+ "signaled without an edit list in a CMAF video track "
+ "using negative CTS offsets\n",
+ track->track_id, track->start_dts + track->start_cts);
+ } else if (mov->use_editlist)
mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
av_log(mov->fc, AV_LOG_WARNING,
@@ -8335,6 +8348,7 @@ static int mov_init_iamf_track(AVFormatContext *s)
static int mov_init(AVFormatContext *s)
{
MOVMuxContext *mov = s->priv_data;
+ int negative_cts_offsets = mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS;
int has_iamf = 0;
int i, ret;
@@ -8420,7 +8434,12 @@ static int mov_init(AVFormatContext *s)
!(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
- if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist)
+ /* CMAF video tracks either use signed composition offsets or an edit
+ * list, never both. When edit lists are enabled, default to writing
+ * them for all the tracks, with unsigned offsets (CMAF track file).
+ * If negative CTS offsets were explicitly requested, keep them and
+ * don't write an edit list for the video tracks instead. */
+ if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist && !negative_cts_offsets)
mov->flags &= ~FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS;
if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO &&
diff --git a/tests/api/api-movenc-test.c b/tests/api/api-movenc-test.c
index 39494748fd3..3ee4e83fbe3 100644
--- a/tests/api/api-movenc-test.c
+++ b/tests/api/api-movenc-test.c
@@ -526,6 +526,16 @@ int main(int argc, char **argv)
finish();
close_out();
+ // Test writing CMAF with both edit lists and negative CTS offsets;
+ // the video track uses the signed offsets and has no edit list,
+ // while the audio track has an edit list for its preroll.
+ init_out("cmaf-negative-cts-elst");
+ av_dict_set(&opts, "movflags", "+cmaf+frag_keyframe+delay_moov+negative_cts_offsets", 0);
+ init(1, 1);
+ mux_gops(2);
+ finish();
+ close_out();
+
// Test writing a file with one track lacking packets, with delay_moov.
skip_write_audio = 1;
init_out("delay-moov-empty-track");
diff --git a/tests/ref/fate/api-movenc b/tests/ref/fate/api-movenc
index 20996b89101..0638c653d50 100644
--- a/tests/ref/fate/api-movenc
+++ b/tests/ref/fate/api-movenc
@@ -53,6 +53,11 @@ write_data len 916, time -33333, type sync atom moof
write_data len 908, time 966667, type sync atom moof
write_data len 148, time nopts, type trailer atom -
b3e114f4c1f8d090ab4411b1845e89f8 3203 delay-moov-elst
+write_data len 1187, time nopts, type header atom ftyp
+write_data len 908, time 0, type sync atom moof
+write_data len 900, time 1000000, type sync atom moof
+write_data len 148, time nopts, type trailer atom -
+1282090463af3213f43b3acdba23df94 3143 cmaf-negative-cts-elst
write_data len 1195, time nopts, type header atom ftyp
write_data len 836, time 0, type sync atom moof
write_data len 67, time nopts, type trailer atom -
--
2.54.0
From 253eede4744d13654740b5aa2f0f53bc247692d9 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <baptiste.coudurier(a)gmail.com>
Date: Tue, 10 Nov 2026 23:21:23 -0800
Subject: [PATCH 2/2] avformat/movenc: signal the cmf2 brand in CMAF output
using negative CTS offsets
ISO/IEC 23000-19, 7.2 recommends listing all the structural CMAF brands
a track conforms to. On top of the cmfc constraints, cmf2 requires video
tracks to use version 1 trun boxes and to not have an edit list, and the
sample flags, durations and sizes to be set in the tfhd or trun boxes
(7.7), all of which are fulfilled when negative CTS offsets are used.
Co-Authored-By: Claude Fable 5.2 <noreply(a)anthropic.com>
---
libavformat/movenc.c | 7 ++++-
tests/ref/fate/api-movenc | 8 +++---
tests/ref/fate/mov-mp4-fragmented-ttml-stpp | 30 ++++++++++-----------
3 files changed, 25 insertions(+), 20 deletions(-)
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index d6fe7491540..d9c89dcf12e 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6477,8 +6477,13 @@ static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
// brand, if not already the major brand. This is compatible with users that
// don't understand tfdt.
if (mov->mode == MODE_MP4) {
- if (mov->flags & FF_MOV_FLAG_CMAF)
+ if (mov->flags & FF_MOV_FLAG_CMAF) {
ffio_wfourcc(pb, "cmfc");
+ // cmf2 additionally requires video tracks to use version 1
+ // trun boxes and to not have an edit list.
+ if (mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
+ ffio_wfourcc(pb, "cmf2");
+ }
if (mov->flags & FF_MOV_FLAG_FRAGMENT && !(mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS))
ffio_wfourcc(pb, "iso6");
if (has_av1)
diff --git a/tests/ref/fate/api-movenc b/tests/ref/fate/api-movenc
index 0638c653d50..7dcc533e965 100644
--- a/tests/ref/fate/api-movenc
+++ b/tests/ref/fate/api-movenc
@@ -53,11 +53,11 @@ write_data len 916, time -33333, type sync atom moof
write_data len 908, time 966667, type sync atom moof
write_data len 148, time nopts, type trailer atom -
b3e114f4c1f8d090ab4411b1845e89f8 3203 delay-moov-elst
-write_data len 1187, time nopts, type header atom ftyp
+write_data len 1191, time nopts, type header atom ftyp
write_data len 908, time 0, type sync atom moof
write_data len 900, time 1000000, type sync atom moof
write_data len 148, time nopts, type trailer atom -
-1282090463af3213f43b3acdba23df94 3143 cmaf-negative-cts-elst
+72905d37240956dfb856d5e1f4aca4cb 3147 cmaf-negative-cts-elst
write_data len 1195, time nopts, type header atom ftyp
write_data len 836, time 0, type sync atom moof
write_data len 67, time nopts, type trailer atom -
@@ -160,11 +160,11 @@ write_data len 900, time 0, type sync atom moof
write_data len 908, time 1000000, type sync atom moof
write_data len 148, time nopts, type trailer atom -
47ea28db2ccd18d9c3b080a64e5420de 3115 empty-moov-neg-cts
-write_data len 28, time nopts, type header atom ftyp
+write_data len 32, time nopts, type header atom ftyp
write_data len 1123, time nopts, type header atom -
write_data len 70, time 0, type boundary atom emsg
write_data len 1832, time 0, type sync atom moof
write_data len 70, time 2000000, type boundary atom emsg
write_data len 1840, time 2000000, type sync atom moof
write_data len 148, time nopts, type trailer atom -
-43f9d447aa759ec494e20d9c8d149488 5111 emsg
+782d5eb4c4bccaf442cf3a928b510409 5115 emsg
diff --git a/tests/ref/fate/mov-mp4-fragmented-ttml-stpp b/tests/ref/fate/mov-mp4-fragmented-ttml-stpp
index f1316833206..95f9d5bbfd7 100644
--- a/tests/ref/fate/mov-mp4-fragmented-ttml-stpp
+++ b/tests/ref/fate/mov-mp4-fragmented-ttml-stpp
@@ -1,5 +1,5 @@
-aceb3be7068511fe52ccb953fede8ecd *tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
-183872 tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
+2696266b1506f9537b59ac4aa490e596 *tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
+183876 tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
<?xml version="1.0" encoding="utf-8"?>
<tt
xmlns="http://www.w3.org/ns/ttml"
@@ -415,16 +415,16 @@ aceb3be7068511fe52ccb953fede8ecd *tests/data/fate/mov-mp4-fragmented-ttml-stpp.m
</div>
</body>
</tt>
-packet,subtitle,1,0,0.000000,0,0.000000,2002,2.002000,1172,14302,K__,CRC32:bde7afa1
-packet,subtitle,1,2002,2.002000,2002,2.002000,2002,2.002000,1015,28648,K__,CRC32:bd3bd4bc
-packet,subtitle,1,4004,4.004000,4004,4.004000,2002,2.002000,1305,42837,K__,CRC32:c8ff5017
-packet,subtitle,1,6006,6.006000,6006,6.006000,2002,2.002000,1111,57316,K__,CRC32:f0b10da6
-packet,subtitle,1,8008,8.008000,8008,8.008000,2002,2.002000,927,71601,K__,CRC32:55a7bb1b
-packet,subtitle,1,10010,10.010000,10010,10.010000,2002,2.002000,1194,85702,K__,CRC32:03dc4753
-packet,subtitle,1,12012,12.012000,12012,12.012000,2002,2.002000,874,100070,K__,CRC32:c7a092d4
-packet,subtitle,1,14014,14.014000,14014,14.014000,2002,2.002000,1154,114118,K__,CRC32:b476fe9c
-packet,subtitle,1,16016,16.016000,16016,16.016000,2002,2.002000,1240,128446,K__,CRC32:912e694c
-packet,subtitle,1,18018,18.018000,18018,18.018000,2002,2.002000,960,142860,K__,CRC32:bba73912
-packet,subtitle,1,20020,20.020000,20020,20.020000,2002,2.002000,1251,156994,K__,CRC32:4c7b87d8
-packet,subtitle,1,22022,22.022000,22022,22.022000,2002,2.002000,1285,171419,K__,CRC32:c0368927
-packet,subtitle,1,24024,24.024000,24024,24.024000,501,0.501000,1676,181630,K__,CRC32:812a103f
+packet,subtitle,1,0,0.000000,0,0.000000,2002,2.002000,1172,14306,K__,CRC32:bde7afa1
+packet,subtitle,1,2002,2.002000,2002,2.002000,2002,2.002000,1015,28652,K__,CRC32:bd3bd4bc
+packet,subtitle,1,4004,4.004000,4004,4.004000,2002,2.002000,1305,42841,K__,CRC32:c8ff5017
+packet,subtitle,1,6006,6.006000,6006,6.006000,2002,2.002000,1111,57320,K__,CRC32:f0b10da6
+packet,subtitle,1,8008,8.008000,8008,8.008000,2002,2.002000,927,71605,K__,CRC32:55a7bb1b
+packet,subtitle,1,10010,10.010000,10010,10.010000,2002,2.002000,1194,85706,K__,CRC32:03dc4753
+packet,subtitle,1,12012,12.012000,12012,12.012000,2002,2.002000,874,100074,K__,CRC32:c7a092d4
+packet,subtitle,1,14014,14.014000,14014,14.014000,2002,2.002000,1154,114122,K__,CRC32:b476fe9c
+packet,subtitle,1,16016,16.016000,16016,16.016000,2002,2.002000,1240,128450,K__,CRC32:912e694c
+packet,subtitle,1,18018,18.018000,18018,18.018000,2002,2.002000,960,142864,K__,CRC32:bba73912
+packet,subtitle,1,20020,20.020000,20020,20.020000,2002,2.002000,1251,156998,K__,CRC32:4c7b87d8
+packet,subtitle,1,22022,22.022000,22022,22.022000,2002,2.002000,1285,171423,K__,CRC32:c0368927
+packet,subtitle,1,24024,24.024000,24024,24.024000,501,0.501000,1676,181634,K__,CRC32:812a103f
--
2.54.0
1
0
[PR] avcodec/libx264: fix the mb_info and ROI maps for interlaced encoding (PR #25930)
by Baptiste Coudurier 11 Nov '26
by Baptiste Coudurier 11 Nov '26
11 Nov '26
PR #25930 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25930
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25930.patch
Two fixes for the per-macroblock maps the libx264 wrapper hands to x264 when interlaced encoding is enabled. Both were noted as pre-existing in the review of #25909.
## mb_info map size
`setup_mb_info()` allocates `mb_width * mb_height` bytes for the frame's own macroblock row count. x264 rounds its row count up to an even number for interlaced and fake-interlaced encoding and reads `mb_info` over that grid, so with an odd count it reads one row past the allocation. The map is now allocated for an even row count; the extra row stays 0, that is not marked constant.
## ROI mapping with interlaced encoding
`setup_roi()` maps regions to macroblock rows in frame raster order. With `interlaced=1` x264 codes macroblock pairs, each frame or field coded, and in a field coded pair each of the two map rows covers every other line of the pair's 32 lines. A region mapped to one row of a pair therefore missed half of its lines. The choice is made per pair after adaptive quantization, so the offset is now applied to both rows of every pair a region touches. Fake-interlaced encoding keeps the raster mapping, since x264 codes those frames progressively.
This commit also allocates the offset map for an even row count, because the pair rounding writes into that row. That part is the same change as #25909; whichever lands second needs a rebase.
## Testing
AddressSanitizer build of FFmpeg with libx264 (x264 itself not instrumented), `testsrc2` with `addroi`, at 64x48, 64x64 and 64x80, progressive, `interlaced=1` and `fake-interlaced=1`, three region positions each (27 cases), compared with master:
- all 27 run without an AddressSanitizer report;
- progressive and fake-interlaced output is bit-identical to master;
- `interlaced=1` output changes in 7 of 9 cases. The two unchanged ones are the bottom-row region at 64x48 and 64x80, where the only newly set row is the added one.
Not tested: the mb_info change at runtime (no command line path produces video hint side data), and the overreads themselves, which happen inside x264 and need an instrumented x264 to observe.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
From 8c95d60049a7be75bf1ed928f0e8ad74a8d7147e Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <baptiste.coudurier(a)gmail.com>
Date: Tue, 10 Nov 2026 23:38:24 -0800
Subject: [PATCH 1/2] avcodec/libx264: size the mb_info map for interlaced
encoding
x264 rounds its macroblock row count up to an even number for
interlaced and fake-interlaced encoding and reads mb_info over that
grid. setup_mb_info() allocated the map for the frame's own row
count, so with an odd count x264 read one row past the allocation.
Allocate the map for an even row count. The extra row stays 0, that
is not marked constant.
Co-Authored-By: Claude Fable 5.2 <noreply(a)anthropic.com>
---
libavcodec/libx264.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index a432c0b50e5..e5a853c10a9 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -370,7 +370,10 @@ static int setup_mb_info(AVCodecContext *ctx, x264_picture_t *pic,
mbinfo_rects = (const AVVideoRect *)av_video_hint_rects(info);
nb_rects = info->nb_rects;
- mbinfo = av_calloc(mb_width * mb_height, sizeof(*mbinfo));
+ /* x264 rounds the macroblock row count up to an even number for
+ * interlaced and fake-interlaced encoding and reads mb_info over
+ * that grid, so size the map for it. The extra row stays 0. */
+ mbinfo = av_calloc(mb_width * FFALIGN(mb_height, 2), sizeof(*mbinfo));
if (!mbinfo)
return AVERROR(ENOMEM);
--
2.54.0
From 18d102b35e0b827e8a963086e097bb28206fb94c Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <baptiste.coudurier(a)gmail.com>
Date: Tue, 10 Nov 2026 23:39:28 -0800
Subject: [PATCH 2/2] avcodec/libx264: apply ROI offsets per macroblock pair
when interlaced
With interlaced encoding x264 codes macroblock pairs, each either
frame or field coded. In a field coded pair each of the two map rows
covers every other line of the pair's 32 lines, so a region mapped in
frame raster order to one row of a pair missed half of its lines.
The choice is made per pair after adaptive quantization, so apply
the offset to both rows of every pair a region touches.
x264 also rounds its macroblock row count up to an even number for
interlaced and fake-interlaced encoding and reads quant_offsets over
that grid, so allocate the map for an even row count. With an odd
count it read one row past the allocation.
Co-Authored-By: Claude Fable 5.2 <noreply(a)anthropic.com>
---
libavcodec/libx264.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index e5a853c10a9..ffd70b83701 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -410,6 +410,10 @@ static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic,
int mbx = (frame->width + MB_SIZE - 1) / MB_SIZE;
int mby = (frame->height + MB_SIZE - 1) / MB_SIZE;
+ /* x264 rounds the macroblock row count up to an even number for
+ * interlaced and fake-interlaced encoding and reads quant_offsets
+ * over that grid, so size the map for it. */
+ int mby_alloc = FFALIGN(mby, 2);
int qp_range = 51 + 6 * (x4->params.i_bitdepth - 8);
int nb_rois;
const AVRegionOfInterest *roi;
@@ -438,7 +442,7 @@ static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic,
}
nb_rois = size / roi_size;
- qoffsets = av_calloc(mbx * mby, sizeof(*qoffsets));
+ qoffsets = av_calloc(mbx * mby_alloc, sizeof(*qoffsets));
if (!qoffsets)
return AVERROR(ENOMEM);
@@ -452,6 +456,14 @@ static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic,
starty = FFMIN(mby, roi->top / MB_SIZE);
endy = FFMIN(mby, (roi->bottom + MB_SIZE - 1)/ MB_SIZE);
+ if (x4->params.b_interlaced) {
+ /* A macroblock pair may be field coded, in which case each of
+ * its two rows covers every other line of the pair's 32 lines.
+ * Apply the offset to both rows of each pair the region
+ * touches. */
+ starty &= ~1;
+ endy = FFALIGN(endy, 2);
+ }
startx = FFMIN(mbx, roi->left / MB_SIZE);
endx = FFMIN(mbx, (roi->right + MB_SIZE - 1)/ MB_SIZE);
--
2.54.0
1
0
[PR] avformat/mov: fix timestamps of fragments using negative CTS offsets (PR #25929)
by Baptiste Coudurier 11 Nov '26
by Baptiste Coudurier 11 Nov '26
11 Nov '26
PR #25929 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25929
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25929.patch
## Problem
In the mov demuxer a sample's pts is computed as index timestamp + `sc->dts_shift` + CTS offset, where `dts_shift` is the largest negative CTS offset seen in the track. For samples described in the `moov`, `mov_build_index()` subtracts `dts_shift` from the index timestamps, so the result is correct.
For samples described by `trun` boxes in fragments, `mov_read_trun()` stored the raw decode time (from `tfdt`, the previous trun or the track end) as the index timestamp. Only the path deriving dts from a known pts subtracted `dts_shift`. As a result every pts of the track, and the stream `start_time`, came out too large by `dts_shift`.
This affects fragmented MP4 using signed (version 1) composition offsets and no edit list, such as what the muxer writes with `-movflags cmaf` or `+negative_cts_offsets`: video is demuxed delayed by the reorder delay relative to the other streams (0.04 s in the test below).
## Fix
In `mov_read_trun()`:
- keep the local `dts` as the real decode time and store `dts - sc->dts_shift` as the index timestamp;
- compare `prev_dts` against that shifted value;
- when `dts_shift` grows while parsing a trun, subtract the increase from the index entries already filled in (skipping the not yet filled hole of the current trun) and from `prev_dts`.
## Testing
New test `fate-mov-mp4-frag-negative-cts`: muxes `tests/data/vsynth1.yuv` with `-c:v mpeg4 -bf 2 -g 12 -movflags +frag_keyframe+empty_moov+negative_cts_offsets`, then checks the demuxed packets and `stream=start_time`.
- With `libavformat/mov.c` from master the test fails: `start_time=0.040000` instead of `start_time=0.000000`.
- With the fix it passes: first packet dts -512 / pts 0, `start_time=0.000000`.
Full `make -k fate` on macOS arm64 (`--disable-doc --disable-autodetect --enable-gpl`): no existing reference changes. 14 tests fail here only because my local sample set is out of date and their sample file is missing ("No such file or directory"), unrelated to this change: `hevc-tile-row-order-nokey`, `hevc-atc-sei-reset`, `notchlc-alpha-ramp`, `notchlc-alpha-blocks`, `notchlc-alpha-macroblocks`, `notchlc-alpha-ramp-oddsize`, `notchlc-chroma-alpha`, `sub-webvtt-timestamp-map`, `vvc-rpr-weighted-pred`, `mov-vpcc-reserved-color-tags`, `mpeg4-reserved-color-tags`, `probe-format-jpeg-not-cavsvideo`, `probe-format-wav-not-mpegts`, `prores-color-tags-fallback`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
From e603e5bc13123868a191bd7e5c2c92bcfb45a980 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <baptiste.coudurier(a)gmail.com>
Date: Tue, 10 Nov 2026 23:23:27 -0800
Subject: [PATCH] avformat/mov: fix timestamps of fragments using negative CTS
offsets
The pts of a sample is computed as its index timestamp + dts_shift + CTS
offset, dts_shift being the largest negative CTS offset of the track. For
this to work, the index timestamps have to be offset by dts_shift, which
is done for the samples described in the moov, but not for the ones from
trun boxes, unless their time was derived from a pts.
As a result, in fragmented files using signed CTS offsets and no edit
list, e.g. as written by the muxer with the cmaf or negative_cts_offsets
flags, all the video timestamps were off by dts_shift, typically delaying
the video by the reorder delay relative to the other streams.
Offset the index timestamps of the samples from trun boxes as well, and
update the entries already added when dts_shift changes.
Co-Authored-By: Claude Fable 5.2 <noreply(a)anthropic.com>
---
libavformat/mov.c | 25 ++++++++++++++---
tests/fate/mov.mak | 8 ++++++
tests/ref/fate/mov-mp4-frag-negative-cts | 35 ++++++++++++++++++++++++
3 files changed, 64 insertions(+), 4 deletions(-)
create mode 100644 tests/ref/fate/mov-mp4-frag-negative-cts
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 0c2f592e7f3..26d57434f2f 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -6242,7 +6242,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
unsigned entries, first_sample_flags = frag->flags;
int flags, distance, i;
int64_t prev_dts = AV_NOPTS_VALUE;
- int next_frag_index = -1, index_entry_pos;
+ int next_frag_index = -1, index_entry_pos, first_entry_pos;
size_t requested_size;
AVIndexEntry *new_entries;
MOVFragmentStreamInfo * frag_stream_info;
@@ -6438,6 +6438,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
if (index_entry_pos > 0)
prev_dts = sti->index_entries[index_entry_pos-1].timestamp;
+ first_entry_pos = index_entry_pos;
for (i = 0; i < entries && !pb->eof_reached; i++) {
unsigned sample_size = frag->size;
@@ -6446,6 +6447,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
unsigned ctts_duration = 0;
int keyframe = 0;
int index_entry_flags = 0;
+ int dts_shift = sc->dts_shift;
if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
@@ -6460,8 +6462,23 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
}
mov_update_dts_shift(sc, ctts_duration, c->fc);
+ if (sc->dts_shift != dts_shift) {
+ // The index timestamps are offset by dts_shift, as the pts is
+ // computed as timestamp + dts_shift + ctts. Update the entries
+ // already added, skipping the ones not yet filled in.
+ dts_shift = sc->dts_shift - dts_shift;
+ for (int j = 0; j < sti->nb_index_entries; j++) {
+ if (j == index_entry_pos)
+ j = first_entry_pos + entries;
+ if (j < sti->nb_index_entries)
+ sti->index_entries[j].timestamp =
+ av_sat_sub64(sti->index_entries[j].timestamp, dts_shift);
+ }
+ if (prev_dts != AV_NOPTS_VALUE)
+ prev_dts = av_sat_sub64(prev_dts, dts_shift);
+ }
if (pts != AV_NOPTS_VALUE) {
- dts = av_sat_sub64(pts, sc->dts_shift);
+ dts = pts;
if (flags & MOV_TRUN_SAMPLE_CTS) {
dts = av_sat_sub64(dts, ctts_duration);
} else {
@@ -6487,11 +6504,11 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
}
// Fragments can overlap in time. Discard overlapping frames after
// decoding.
- if (prev_dts >= dts)
+ if (prev_dts >= av_sat_sub64(dts, sc->dts_shift))
index_entry_flags |= AVINDEX_DISCARD_FRAME;
sti->index_entries[index_entry_pos].pos = offset;
- sti->index_entries[index_entry_pos].timestamp = dts;
+ sti->index_entries[index_entry_pos].timestamp = av_sat_sub64(dts, sc->dts_shift);
sti->index_entries[index_entry_pos].size = sample_size;
sti->index_entries[index_entry_pos].min_distance = distance;
sti->index_entries[index_entry_pos].flags = index_entry_flags;
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 7aabe980d3d..645bea31c7f 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -615,6 +615,14 @@ fate-mov-stereo3d-sbs-mp4: CMD = transcode rawvideo $(TARGET_PATH)/tests/data/vs
"-c:v mpeg4 -qscale:v 10 -stereo3d:v sbsl -strict unofficial -frames:v 2" \
"-c:v copy" "-show_entries stream_side_data_list" "" "" "-s 352x288 -pix_fmt yuv420p"
+# Fragments using negative CTS offsets, without any edit list.
+FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, MPEG4, MP4 MOV, RAWVIDEO_DEMUXER) \
+ += fate-mov-mp4-frag-negative-cts
+fate-mov-mp4-frag-negative-cts: tests/data/vsynth1.yuv
+fate-mov-mp4-frag-negative-cts: CMD = transcode rawvideo $(TARGET_PATH)/tests/data/vsynth1.yuv mp4 \
+ "-c:v mpeg4 -qscale:v 10 -bf 2 -g 12 -frames:v 24 -movflags +frag_keyframe+empty_moov+negative_cts_offsets" \
+ "-c:v copy" "-show_entries stream=start_time" "" "" "-s 352x288 -pix_fmt yuv420p"
+
FATE_FFMPEG += $(FATE_MOV_FFMPEG-yes)
FATE_FFMPEG_FFPROBE += $(FATE_MOV_FFMPEG_FFPROBE-yes)
diff --git a/tests/ref/fate/mov-mp4-frag-negative-cts b/tests/ref/fate/mov-mp4-frag-negative-cts
new file mode 100644
index 00000000000..437add04470
--- /dev/null
+++ b/tests/ref/fate/mov-mp4-frag-negative-cts
@@ -0,0 +1,35 @@
+90b9cfb016ba0e338c212aeb359e6356 *tests/data/fate/mov-mp4-frag-negative-cts.mp4
+315067 tests/data/fate/mov-mp4-frag-negative-cts.mp4
+#extradata 0: 31, 0x656a0612
+#tb 0: 1/12800
+#media_type 0: video
+#codec_id 0: mpeg4
+#dimensions 0: 352x288
+#sar 0: 1/1
+0, -512, 0, 512, 27837, 0xd9809b60
+0, 0, 1536, 512, 11808, 0xe8a80469, F=0x0
+0, 512, 512, 512, 7843, 0x69a26bfc, F=0x0
+0, 1024, 1024, 512, 8815, 0x33504ac2, F=0x0
+0, 1536, 3072, 512, 12344, 0x6b82b0b2, F=0x0
+0, 2048, 2048, 512, 10270, 0x9e881379, F=0x0
+0, 2560, 2560, 512, 8594, 0x9d6adec4, F=0x0
+0, 3072, 4608, 512, 18506, 0x716ac152, F=0x0
+0, 3584, 3584, 512, 9925, 0x844c49d5, F=0x0
+0, 4096, 4096, 512, 10041, 0x4d3d56b6, F=0x0
+0, 4608, 6144, 512, 27925, 0xc719d5f6
+0, 5120, 5120, 512, 8028, 0xe7ae65af, F=0x0
+0, 5632, 5632, 512, 8488, 0x7e95b975, F=0x0
+0, 6144, 7680, 512, 18538, 0x923c2579, F=0x0
+0, 6656, 6656, 512, 9665, 0x7bf9d41d, F=0x0
+0, 7168, 7168, 512, 9793, 0x68872956, F=0x0
+0, 7680, 9216, 512, 19023, 0x1006d3ff, F=0x0
+0, 8192, 8192, 512, 9614, 0x88dba0f9, F=0x0
+0, 8704, 8704, 512, 10769, 0x679a7b1a, F=0x0
+0, 9216, 10752, 512, 14375, 0x8ed53e93, F=0x0
+0, 9728, 9728, 512, 7604, 0x45f7f80d, F=0x0
+0, 10240, 10240, 512, 7926, 0xc22da563, F=0x0
+0, 10752, 11776, 512, 28215, 0x8dff5c71
+0, 11264, 11264, 512, 7688, 0x40575c5e, F=0x0
+[STREAM]
+start_time=0.000000
+[/STREAM]
--
2.54.0
1
0
[PR] avformat/movenc: keep the tracks' start offsets with negative_cts_offsets and no edit lists (PR #25927)
by Baptiste Coudurier 11 Nov '26
by Baptiste Coudurier 11 Nov '26
11 Nov '26
PR #25927 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25927
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25927.patch
Fixes #25775.
Since cc929cee76, the muxer no longer selects `avoid_negative_ts=make_zero` when edit lists are disabled and `negative_cts_offsets` is set. Each track then starts at 0 on its own and the difference between the tracks' start times is lost: AAC priming leaves the audio 1024 samples late, and a start offset such as one set with `-itsoffset` disappears.
Select `make_zero` again in that case, so that the first sample of the tracks starting later is stretched to cover the difference, as is done without `negative_cts_offsets`. Since the flag makes each track's first sample have dts == pts, the shift is based on pts instead of dts, otherwise the video B-frame delay would needlessly delay every track.
With the commands from the issue (audio marker minus video marker):
| Options | Before | After |
|---|---|---|
| `-use_editlist 0 -movflags +negative_cts_offsets` | +21.333 ms | -0.070 ms |
| same, plus `-itsoffset 0.5` on the video input | +21.333 ms | -500.070 ms |
| default, and `-use_editlist 0` alone | unchanged | unchanged |
The 0.070 ms is the rounding of the 1024 sample lead to the video timescale. AAC priming is still played, only an edit list can trim it.
This also applies to fragmented output without edit lists (CMAF, ISMV). The api-movenc ismv case, with B-frames and audio preroll, changes accordingly: the video is delayed by the audio preroll.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
From 6721704c939bd851316d287c948eb6cda0b130b9 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <baptiste.coudurier(a)gmail.com>
Date: Tue, 10 Nov 2026 23:01:38 -0800
Subject: [PATCH] avformat/movenc: keep the tracks' start offsets with
negative_cts_offsets and no edit lists
Since cc929cee76, the muxer no longer selects avoid_negative_ts=make_zero
when edit lists are disabled and negative_cts_offsets is set. Each track
then starts at 0 on its own and the difference between the tracks' start
times is lost: AAC priming leaves the audio 1024 samples late, and a
start offset such as one set with -itsoffset disappears.
Select make_zero again in that case, so that the first sample of the
tracks starting later is stretched to cover the difference, as is done
without negative_cts_offsets. Since the flag makes each track's first
sample have dts == pts, base the shift on pts instead of dts, otherwise
the video B-frame delay would needlessly delay every track.
This also applies to fragmented output without edit lists (CMAF, ISMV).
The api-movenc ismv case, with B-frames and audio preroll, changes
accordingly: the video is delayed by the audio preroll.
Fixes #25775.
Co-Authored-By: Claude Fable 5.2 <noreply(a)anthropic.com>
---
libavformat/movenc.c | 12 +++++++++---
tests/ref/fate/api-movenc | 4 ++--
2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index ea71acde42a..8ddd6c2d438 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -8423,9 +8423,15 @@ static int mov_init(AVFormatContext *s)
if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist)
mov->flags &= ~FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS;
- if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO &&
- !(mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS))
- s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO;
+ if (!mov->use_editlist) {
+ if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO)
+ s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO;
+ /* With negative CTS offsets, each track's dts is shifted so that
+ * its first sample has dts == pts; the pts are thus what need to be
+ * non-negative, and what the tracks' start offsets are based on. */
+ if (mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
+ ffformatcontext(s)->avoid_negative_ts_use_pts = 1;
+ }
/* Clear the omit_tfhd_offset flag if default_base_moof is set;
* if the latter is set that's enough and omit_tfhd_offset doesn't
diff --git a/tests/ref/fate/api-movenc b/tests/ref/fate/api-movenc
index 20996b89101..55bbc65c377 100644
--- a/tests/ref/fate/api-movenc
+++ b/tests/ref/fate/api-movenc
@@ -21,10 +21,10 @@ write_data len 24, time nopts, type header atom ftyp
write_data len 1171, time nopts, type header atom -
write_data len 728, time 0, type sync atom moof
write_data len 828, time nopts, type unknown atom -
-write_data len 728, time 999999, type sync atom moof
+write_data len 728, time 1046439, type sync atom moof
write_data len 812, time nopts, type unknown atom -
write_data len 148, time nopts, type trailer atom -
-824f4461ac74f8a5dc54186f86c98ac2 4439 ismv
+5d57e49a4324a9ead98eb554b4f9caed 4439 ismv
write_data len 36, time nopts, type header atom ftyp
write_data len 1123, time nopts, type header atom -
write_data len 796, time 0, type sync atom moof
--
2.54.0
1
0
[PR] avfilter/f_select: accept high bit depth gray, YUV and RGB for scene detection (PR #25926)
by Baptiste Coudurier 11 Nov '26
by Baptiste Coudurier 11 Nov '26
11 Nov '26
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 <baptiste.coudurier(a)gmail.com>
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 <noreply(a)anthropic.com>
---
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 <baptiste.coudurier(a)gmail.com>
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 <noreply(a)anthropic.com>
---
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
1
0
11 Nov '26
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 <baptiste.coudurier(a)gmail.com>
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 <noreply(a)anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply(a)anthropic.com>
---
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 <baptiste.coudurier(a)gmail.com>
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 <noreply(a)anthropic.com>
---
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
1
0
PR #25924 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25924
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25924.patch
Split the per-row sums in `filter()` across slice jobs. Each job sums its share of the rows of every plane into per-job storage, and the caller adds up the partial sums. The sums are integers, so the totals, and therefore the output, do not depend on the number of jobs. The type decision, history, repeated field detection and metadata stay sequential.
**Output is unchanged.** `idet,metadata=mode=print` followed by `showinfo` was compared against master for `-filter_threads` 1, auto, 3 and 16, in default mode, with `half_life=3` and with `analyze_interlaced_flag=20`: 8-bit and 10-bit input in 420/422/444, yuva420p and gray16, progressive, TFF and BFF interlaced, telecined, odd heights, and frames only 3 and 5 lines high. Per-frame metadata, frame flags and the summary lines are identical.
**Timing**, wall time for 120 s of 4096x2160 ProRes 422 (yuv422p10le, 23.976 fps) on a c7g.4xlarge (Graviton3, 16 vCPU), `-vf idet -f null -`:
| | wall time |
|---|---|
| decode only | 15.8 s |
| idet, master | 47.2 s |
| idet, 1 filter thread | 47.5 s |
| idet, 4 filter threads | 18.9 s |
| idet, 8 filter threads | 19.5 s |
| idet, 16 filter threads | 18.3 s |
With 4 or more filter threads the decoder is the limit. On an Apple M3 Max, idet alone on 4096x2160 yuv422p10le (300 frames) goes from 1.21 s to 0.39 s with 4 threads and 0.32 s with 8.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
From 9aa98fb12ad00fe198841209eea242fc63a7a994 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <baptiste.coudurier(a)gmail.com>
Date: Tue, 10 Nov 2026 21:42:21 -0800
Subject: [PATCH] avfilter/vf_idet: add slice threading
Split the per-row sums in filter() across slice jobs. Each job sums
its share of the rows of every plane into per-job storage, and the
caller adds up the partial sums. The sums are integers, so the totals,
and therefore the output, do not depend on the number of jobs.
The type decision, history, repeated field detection and metadata
stay sequential.
4096x2160 yuv422p10le, 300 frames, Apple M3 Max, wall time:
1 thread: 1.21 s
4 threads: 0.39 s
8 threads: 0.32 s
16 threads: 0.33 s
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
---
libavfilter/vf_idet.c | 84 ++++++++++++++++++++++++++++++++++++-------
1 file changed, 72 insertions(+), 12 deletions(-)
diff --git a/libavfilter/vf_idet.c b/libavfilter/vf_idet.c
index 02dc14fb7a1..1e4db2ed7aa 100644
--- a/libavfilter/vf_idet.c
+++ b/libavfilter/vf_idet.c
@@ -21,6 +21,7 @@
#include <float.h> /* FLT_MAX */
#include "libavutil/common.h"
+#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
@@ -40,6 +41,12 @@ typedef enum {
REPEAT_BOTTOM,
} RepeatedField;
+typedef struct IDETSums {
+ int64_t alpha[2];
+ int64_t delta;
+ int64_t gamma[2];
+} IDETSums;
+
typedef struct IDETContext {
const AVClass *class;
IDETDSPContext dsp;
@@ -72,6 +79,9 @@ typedef struct IDETContext {
const AVPixFmtDescriptor *csp;
int eof;
+
+ int nb_threads;
+ IDETSums *sums;
} IDETContext;
#define OFFSET(x) offsetof(IDETContext, x)
@@ -132,30 +142,32 @@ static const char *rep2str(RepeatedField repeated_field)
return NULL;
}
-static void filter(AVFilterContext *ctx)
+static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
{
IDETContext *idet = ctx->priv;
- int y, i;
- int64_t alpha[2]={0};
- int64_t delta=0;
- int64_t gamma[2]={0};
- Type type, best_type;
- RepeatedField repeat;
- int match = 0;
- AVDictionary **metadata = &idet->cur->metadata;
+ IDETSums *sums = &idet->sums[jobnr];
+ int64_t alpha[2] = { 0 };
+ int64_t delta = 0;
+ int64_t gamma[2] = { 0 };
ff_idet_filter_func filter_line = idet->dsp.filter_line;
- for (i = 0; i < idet->csp->nb_components; i++) {
+ for (int i = 0; i < idet->csp->nb_components; i++) {
int w = idet->cur->width;
int h = idet->cur->height;
int refs = idet->cur->linesize[i];
+ int rows, start, end;
if (i && i<3) {
w = AV_CEIL_RSHIFT(w, idet->csp->log2_chroma_w);
h = AV_CEIL_RSHIFT(h, idet->csp->log2_chroma_h);
}
- for (y = 2; y < h - 2; y++) {
+ /* rows 2 to h - 3 are analyzed, split across the jobs */
+ rows = FFMAX(h - 4, 0);
+ start = 2 + ff_slice_pos(rows, jobnr, nb_jobs);
+ end = 2 + ff_slice_pos(rows, jobnr + 1, nb_jobs);
+
+ for (int y = start; y < end; y++) {
uint8_t *prev = &idet->prev->data[i][y*idet->prev->linesize[i]];
uint8_t *cur = &idet->cur ->data[i][y*refs];
uint8_t *next = &idet->next->data[i][y*idet->next->linesize[i]];
@@ -166,6 +178,38 @@ static void filter(AVFilterContext *ctx)
}
}
+ sums->alpha[0] = alpha[0];
+ sums->alpha[1] = alpha[1];
+ sums->delta = delta;
+ sums->gamma[0] = gamma[0];
+ sums->gamma[1] = gamma[1];
+
+ return 0;
+}
+
+static void filter(AVFilterContext *ctx)
+{
+ IDETContext *idet = ctx->priv;
+ int i;
+ int64_t alpha[2]={0};
+ int64_t delta=0;
+ int64_t gamma[2]={0};
+ Type type, best_type;
+ RepeatedField repeat;
+ int match = 0;
+ AVDictionary **metadata = &idet->cur->metadata;
+ const int nb_jobs = FFMIN(idet->cur->height, idet->nb_threads);
+
+ ff_filter_execute(ctx, filter_slice, NULL, NULL, nb_jobs);
+
+ for (i = 0; i < nb_jobs; i++) {
+ alpha[0] += idet->sums[i].alpha[0];
+ alpha[1] += idet->sums[i].alpha[1];
+ delta += idet->sums[i].delta;
+ gamma[0] += idet->sums[i].gamma[0];
+ gamma[1] += idet->sums[i].gamma[1];
+ }
+
if (alpha[0] > idet->interlace_threshold * alpha[1]){
type = TFF;
}else if(alpha[1] > idet->interlace_threshold * alpha[0]){
@@ -380,6 +424,7 @@ static av_cold void uninit(AVFilterContext *ctx)
av_frame_free(&idet->prev);
av_frame_free(&idet->cur );
av_frame_free(&idet->next);
+ av_freep(&idet->sums);
}
static const enum AVPixelFormat pix_fmts[] = {
@@ -434,11 +479,26 @@ static av_cold int init(AVFilterContext *ctx)
return 0;
}
+static int config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ IDETContext *idet = ctx->priv;
+
+ idet->nb_threads = FFMAX(1, ff_filter_get_nb_threads(ctx));
+ av_freep(&idet->sums);
+ idet->sums = av_calloc(idet->nb_threads, sizeof(*idet->sums));
+ if (!idet->sums)
+ return AVERROR(ENOMEM);
+
+ return 0;
+}
+
static const AVFilterPad idet_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = filter_frame,
+ .config_props = config_input,
},
};
@@ -453,7 +513,7 @@ static const AVFilterPad idet_outputs[] = {
const FFFilter ff_vf_idet = {
.p.name = "idet",
.p.description = NULL_IF_CONFIG_SMALL("Interlace detect Filter."),
- .p.flags = AVFILTER_FLAG_METADATA_ONLY,
+ .p.flags = AVFILTER_FLAG_METADATA_ONLY | AVFILTER_FLAG_SLICE_THREADS,
.p.priv_class = &idet_class,
.priv_size = sizeof(IDETContext),
.init = init,
--
2.54.0
1
0