PR #26273 opened by superuser404 URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26273 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26273.patch
The vc1 and wmv3 decoders declare no threading capability, so they decode on one thread whatever the caller asks for. This series makes both frame-threaded on the pattern of the MPEG-4 Part 2 / H.263 decoder. vc1image and wmv3image are unchanged. We need it for a player that sends VC-1 to the software decoder (there is no VideoToolbox VC-1). On 1080 Blu-ray VC-1 a single thread keeps up, but a seek that lands on a non-keyframe decodes forward on one core, and that is what the user waits for. Four commits, each builds and passes FATE on its own: 1. `avcodec/vc1dec: give each reference field picture its own field flags`. B field pictures read the field flags (`mv_f`) of the last reference field picture in direct mode. They lived in two context buffers swapped after each reference field picture. Each reference field picture now writes them into a buffer from a RefStruct pool. Output unchanged. 2. `avcodec/vc1dec: create the picture pool when the decoder opens`. `ff_thread_sync_ref()` only hands the pool to thread copies during init, and VC-1 created it on the first picture. 3. `avcodec/vc1: report decoding progress and wait for it`. Row progress for reference pictures, and waits in motion compensation, skipped P pictures and B direct mode. Rows are reported three behind the decoded row, because the loop filter of row y - 2 still changes the last line of row y - 3. The commit also makes a reference picture that fails after `ff_mpv_frame_start()` report itself complete, otherwise a waiting thread hangs. A no-op without frame threads. 4. `avcodec/vc1dec: add frame threading`. `update_thread_context()`, the points where setup finishes (listed in the message), and the capability. Testing, macOS arm64: - `fate-vc1`, `fate-wmv3-drm` and `fate-mss2` (17 tests) pass with `THREAD_TYPE=frame` and `THREADS=1, 2, 3, 4, 8, 16`. `fate-source` passes. - Frame hashes after repeated seeks and `avcodec_flush_buffers()` match between 1, 3 and 8 threads on the nine FATE VC-1/WMV3 samples remuxed to Matroska (a small libavformat program; I can attach it). - 600 randomly damaged copies of the FATE samples: no hang and no crash at 1, 3 and 8 threads. AddressSanitizer is clean on 300 of them at 1 and 8 threads. - ThreadSanitizer is clean on the intact samples, with and without seeks. One thing to look at. On 26 of 300 damaged copies ThreadSanitizer reports races, and all of them are in `ff_er_frame_end()`. A picture reports rows while it has no error, the error turns up later in the slice, and error concealment then rewrites rows another thread is already reading. The H.263 decoder reports progress the same way (`report_decode_progress()` checks `error_occurred` only at the time it reports), and the same TSan run over damaged H.264 conformance streams reports the same class in `ff_er_frame_end()` too. The visible effect is that the concealment of a damaged picture depends on timing. I left it like this to stay consistent with those decoders. A fix would mean keeping error concealment away from rows the picture has already reported, and that changes the error resilience code every decoder shares. I'm happy to try that separately if you want it. Open question: hwaccel. None of the VC-1 hwaccels is thread-safe, so their threads serialize on the hwaccel mutex. But a reference field picture and a sliced reference picture call into the hwaccel before `ff_thread_finish_setup()`, and DXVA2, D3D11VA and NVDEC are not async-safe either, while `ff_thread_finish_setup()` assumes no hwaccel call before it. Finishing setup before `start_frame()` under hwaccel is a two-line change, but then `update_thread_context()` reads the intensity compensation state while the second field header still writes it (unused under hwaccel, still a race). Should I do that, or keep the hwaccel path as it is? I could not test any VC-1 hwaccel here. Known limit: a reference picture with slices finishes setup only when its decode returns, because a slice header may repeat the picture header. Sliced streams therefore gain only from their B pictures (SA20021: 1.23 s to 1.19 s). Checking the slices' header flags up front could lift that, as a follow-up. Speed on an Apple M1, 6 threads against 1: | Sample | 1 thread | 6 threads | |---|---|---| | `vc1/ilaced_twomv.vc1` (1080, interlaced) | 6.48 s | 2.56 s | | `vc1/SA10143.vc1` (480, field pictures) | 1.54 s | 0.80 s | | `vc1/SMM0005.rcv` remuxed to Matroska (WMV3 Main) | 0.89 s | 0.30 s | | `vc1/SA20021.vc1` (480, slices) | 1.23 s | 1.19 s | (`-stream_loop`, best of three runs; SMM0005 one run, because `-stream_loop` cannot rewind the vc1t demuxer.) classicjazz, who wrote the first version of this patch, measured 1080 Blu-ray VC-1 on an M1 Max at 6 threads: 4.0 to 4.5 times as fast on progressive titles, 2.2 to 2.4 times on titles with field pictures only. >From 32f9742a6f6e5bbb0a1aed92637871640f8c2340 Mon Sep 17 00:00:00 2001 From: Vincent Herbst <[email protected]> Date: Sun, 22 Nov 2026 05:54:37 +0100 Subject: [PATCH 1/4] avcodec/vc1dec: give each reference field picture its own field flags In direct mode a B field picture reads the field flags (mv_f) of the last reference field picture. They lived in two buffers of the decoder context that were swapped after each reference field picture, so only the context that decoded that picture could read them. Each reference field picture now writes them into a buffer from a RefStruct pool, and mv_f_next[0] points into the last one. Other pictures still write theirs into the scratch buffer of the context. This prepares frame threading; the output does not change. Co-authored-by: classicjazz <[email protected]> Signed-off-by: Vincent Herbst <[email protected]> --- libavcodec/vc1.h | 8 ++++++++ libavcodec/vc1dec.c | 34 +++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/libavcodec/vc1.h b/libavcodec/vc1.h index 006cefd58e2..bb75b13ca12 100644 --- a/libavcodec/vc1.h +++ b/libavcodec/vc1.h @@ -353,6 +353,14 @@ typedef struct VC1Context{ uint8_t *blk_mv_type_base, *blk_mv_type; ///< 0: frame MV, 1: field MV (interlaced frame) uint8_t *mv_f_base, *mv_f[2]; ///< 0: MV obtained from same field, 1: opposite field uint8_t *mv_f_next_base, *mv_f_next[2]; + size_t mv_f_size; ///< bytes in each of mv_f_base and mv_f_next_base + /** + * Field flags of reference field pictures. Each one writes mv_f[0] into a + * buffer of its own, which B field pictures then read as mv_f_next[0], + * so a later frame thread can read it while it is still being written. + */ + struct AVRefStructPool *mv_f_pool; + uint8_t *mv_f_next_ref; ///< RefStruct reference: the buffer mv_f_next[0] points into, if any int field_mode; ///< 1 for interlaced field pictures int fptype; int second_field; diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index a4d465dd0b3..b2b981f5883 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -49,6 +49,7 @@ #include "libavutil/avassert.h" #include "libavutil/imgutils.h" #include "libavutil/mem.h" +#include "libavutil/refstruct.h" #include "libavutil/thread.h" @@ -403,16 +404,22 @@ static av_cold int vc1_decode_init_alloc_tables(VC1Context *v) if (!v->blk_mv_type_base) return AVERROR(ENOMEM); v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1; - v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2)); + v->mv_f_size = 2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2); + v->mv_f_base = av_mallocz(v->mv_f_size); if (!v->mv_f_base) return AVERROR(ENOMEM); v->mv_f[0] = v->mv_f_base + s->b8_stride + 1; v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2); - v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2)); + v->mv_f_next_base = av_mallocz(v->mv_f_size); if (!v->mv_f_next_base) return AVERROR(ENOMEM); v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1; v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2); + /* the same layout as the first half of mv_f_base */ + v->mv_f_pool = av_refstruct_pool_alloc(v->mv_f_size / 2 + s->b8_stride + 1, + AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME); + if (!v->mv_f_pool) + return AVERROR(ENOMEM); if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) { for (i = 0; i < 4; i++) @@ -755,6 +762,8 @@ static av_cold void vc1_decode_reset(AVCodecContext *avctx) av_freep(&v->blk_mv_type_base); av_freep(&v->mv_f_base); av_freep(&v->mv_f_next_base); + av_refstruct_unref(&v->mv_f_next_ref); + av_refstruct_pool_uninit(&v->mv_f_pool); av_freep(&v->block); av_freep(&v->cbp_base); av_freep(&v->ttblk_base); @@ -1059,6 +1068,23 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, s->cur_pic.ptr->f->repeat_pict = v->rptfrm * 2; } + /* A reference field picture writes its field flags into a buffer of its + * own, which B field pictures read as mv_f_next until the next one; any + * other picture writes them into scratch space. */ + if (v->field_mode && s->pict_type != AV_PICTURE_TYPE_B && !avctx->hwaccel) { + uint8_t *mv_f = av_refstruct_pool_get(v->mv_f_pool); + if (!mv_f) { + ret = AVERROR(ENOMEM); + goto err; + } + av_refstruct_unref(&v->mv_f_next_ref); + v->mv_f_next_ref = mv_f; + v->mv_f[0] = mv_f + s->b8_stride + 1; + v->mv_f_next[0] = v->mv_f[0]; + } else { + v->mv_f[0] = v->mv_f_base + s->b8_stride + 1; + } + if (avctx->hwaccel) { const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel); s->mb_y = 0; @@ -1295,10 +1321,8 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, s->cur_pic.linesize[2] >>= 1; s->linesize >>= 1; s->uvlinesize >>= 1; - if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) { - FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]); + if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]); - } } ff_dlog(s->avctx, "Consumed %i/%i bits\n", get_bits_count(&v->gb), v->gb.size_in_bits); -- 2.54.1 >From 7c510894f7b23d8a1fc0277cfe15be3846469c80 Mon Sep 17 00:00:00 2001 From: Vincent Herbst <[email protected]> Date: Sun, 22 Nov 2026 05:54:42 +0100 Subject: [PATCH 2/4] avcodec/vc1dec: create the picture pool when the decoder opens ff_mpv_decode_init() hands the picture pool to frame thread copies through ff_thread_sync_ref(), which only works while the codec opens. The VC-1 decoder called it on the first picture. Call it from vc1_decode_init() as well; the call on the first picture then keeps the pool, as it already does after a size change. Co-authored-by: classicjazz <[email protected]> Signed-off-by: Vincent Herbst <[email protected]> --- libavcodec/vc1dec.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index b2b981f5883..5586b77adb2 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -636,6 +636,13 @@ static av_cold int vc1_decode_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; v->s.avctx = avctx; + /* The picture pool is shared by all frame threads and is only handed to + * a thread copy while the codec opens, so create it here rather than + * with the rest of the context on the first picture. */ + ret = ff_mpv_decode_init(s, avctx); + if (ret < 0) + return ret; + ff_vc1_init_common(v); if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) { -- 2.54.1 >From 5433043848a41a4d18b4690ef6dc0e795f738c3c Mon Sep 17 00:00:00 2001 From: Vincent Herbst <[email protected]> Date: Sun, 22 Nov 2026 05:54:48 +0100 Subject: [PATCH 3/4] avcodec/vc1: report decoding progress and wait for it A reference picture now reports the macroblock rows that are final, and every read of a reference picture waits for the rows it touches: motion compensation, skipped P pictures, and the co-located motion vectors and field flags of B direct mode. A row is reported three rows behind the one being decoded. Pixels are put one row late, after the overlap filter, and while row y decodes the loop filter runs on row y - 2, whose top edge changes the last line of row y - 3. A field picture reports while it decodes its second field, in frame rows. A damaged picture is only reported when it ends, as in the H.263 decoder. A reference picture that fails after ff_mpv_frame_start() is never finished by ff_mpv_frame_end(), so the error path now reports it complete; a thread waiting for its rows would hang otherwise. A flush drops the reference to the picture that wrote the field flags. Without frame threads the progress of every picture is INT_MAX, so none of this changes anything yet. Co-authored-by: classicjazz <[email protected]> Signed-off-by: Vincent Herbst <[email protected]> --- libavcodec/vc1.h | 53 ++++++++++++++++++++++++++++++++++++++++++ libavcodec/vc1_block.c | 12 ++++++++++ libavcodec/vc1_mc.c | 27 +++++++++++++++++++++ libavcodec/vc1dec.c | 27 ++++++++++++++++++--- 4 files changed, 116 insertions(+), 3 deletions(-) diff --git a/libavcodec/vc1.h b/libavcodec/vc1.h index bb75b13ca12..2f0079e4a9c 100644 --- a/libavcodec/vc1.h +++ b/libavcodec/vc1.h @@ -27,6 +27,7 @@ #include "get_bits.h" #include "h264chroma.h" #include "mpegvideo.h" +#include "threadprogress.h" #include "intrax8.h" #include "vc1_common.h" #include "vc1dsp.h" @@ -361,6 +362,7 @@ typedef struct VC1Context{ */ struct AVRefStructPool *mv_f_pool; uint8_t *mv_f_next_ref; ///< RefStruct reference: the buffer mv_f_next[0] points into, if any + MPVPicture *mv_f_next_pic; ///< RefStruct reference: the picture that writes mv_f_next_ref int field_mode; ///< 1 for interlaced field pictures int fptype; int second_field; @@ -464,4 +466,55 @@ void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg); void ff_vc1_interp_mc(VC1Context *v); +/** + * Frame threading. A reference picture reports its progress in macroblock + * rows of the frame (16 luma lines); a picture that reads it waits for the + * rows it is about to touch. A field picture reports while it decodes its + * second field, when the rows of both fields are there. + */ + +/** + * Rows behind the row being decoded that a picture still changes: its pixels + * are put one row behind, once the overlap filter has run, and the loop + * filter runs behind that. In a field picture these are field rows. + */ +#define VC1_PROGRESS_LAG 3 + +/** Report the rows the macroblock row just decoded has made final. */ +static av_always_inline void ff_vc1_report_row(const VC1Context *v) +{ + const MpegEncContext *s = &v->s; + + int row; + + /* Error concealment rewrites the picture at its end, so a damaged + * picture is only reported then, as in the H.263 decoder. */ + if (!s->cur_pic.reference || s->er.error_occurred || + s->mb_y < VC1_PROGRESS_LAG) + return; + if (!v->field_mode) + row = s->mb_y - VC1_PROGRESS_LAG; + else if (v->second_field) // field row y covers frame rows 2y and 2y + 1 + row = 2 * (s->mb_y - VC1_PROGRESS_LAG) + 1; + else + return; + ff_thread_progress_report(&s->cur_pic.ptr->progress, row); +} + +/** + * Wait until a reference picture is final down to a line. + * @param last_line lowest luma line about to be read, as the current picture + * addresses it (a field line in a field picture) + */ +static av_always_inline void ff_vc1_await_ref(const VC1Context *v, + const MPVWorkPicture *ref, + int last_line) +{ + if (ref->ptr == v->s.cur_pic.ptr) // second field reading the first one + return; + if (v->field_mode) + last_line = 2 * last_line + 1; + ff_thread_progress_await(&ref->ptr->progress, last_line >> 4); +} + #endif /* AVCODEC_VC1_H */ diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c index a84c853b6f1..00c6b41f3a2 100644 --- a/libavcodec/vc1_block.c +++ b/libavcodec/vc1_block.c @@ -2607,6 +2607,7 @@ static void vc1_decode_i_blocks(VC1Context *v) } s->first_slice_line = 0; + ff_vc1_report_row(v); } /* This is intentionally mb_height and not end_mb_y - unlike in advanced @@ -2740,6 +2741,7 @@ static int vc1_decode_i_blocks_adv(VC1Context *v) inc_blk_idx(v->cur_blk_idx); } s->first_slice_line = 0; + ff_vc1_report_row(v); } ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1, @@ -2831,6 +2833,7 @@ static void vc1_decode_p_blocks(VC1Context *v) v->luma_mv - s->mb_stride, sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride); s->first_slice_line = 0; + ff_vc1_report_row(v); } ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1, (s->end_mb_y << v->field_mode) - 1, ER_MB_END); @@ -2869,6 +2872,13 @@ static void vc1_decode_b_blocks(VC1Context *v) for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) { s->mb_x = 0; init_block_index(v); + /* direct mode reads the co-located motion vectors of the next picture, + * and in a field picture the field flags of the last reference field + * picture, which need not be the next picture */ + if (s->next_pic.ptr) + ff_vc1_await_ref(v, &s->next_pic, s->mb_y * 16 + 15); + if (v->field_mode && v->mv_f_next_pic) + ff_thread_progress_await(&v->mv_f_next_pic->progress, 2 * s->mb_y + 1); for (; s->mb_x < s->mb_width; s->mb_x++) { update_block_index(s); @@ -2927,10 +2937,12 @@ static void vc1_decode_skip_blocks(VC1Context *v) s->mb_x = 0; init_block_index(v); update_block_index(s); + ff_vc1_await_ref(v, &s->last_pic, s->mb_y * 16 + 15); memcpy(s->dest[0], s->last_pic.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16); memcpy(s->dest[1], s->last_pic.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8); memcpy(s->dest[2], s->last_pic.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8); s->first_slice_line = 0; + ff_vc1_report_row(v); } } diff --git a/libavcodec/vc1_mc.c b/libavcodec/vc1_mc.c index db19d95ef93..dda25c01863 100644 --- a/libavcodec/vc1_mc.c +++ b/libavcodec/vc1_mc.c @@ -178,6 +178,7 @@ void ff_vc1_mc_1mv(VC1Context *v, int dir) int v_edge_pos = s->v_edge_pos >> v->field_mode; int i; const uint8_t (*luty)[256], (*lutuv)[256]; + const MPVWorkPicture *ref; int use_ic; int interlace; int linesize, uvlinesize; @@ -219,6 +220,7 @@ void ff_vc1_mc_1mv(VC1Context *v, int dir) } if (!dir) { if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) { + ref = &s->cur_pic; srcY = s->cur_pic.data[0]; srcU = s->cur_pic.data[1]; srcV = s->cur_pic.data[2]; @@ -227,6 +229,7 @@ void ff_vc1_mc_1mv(VC1Context *v, int dir) use_ic = *v->curr_use_ic; interlace = 1; } else { + ref = &s->last_pic; srcY = s->last_pic.data[0]; srcU = s->last_pic.data[1]; srcV = s->last_pic.data[2]; @@ -236,6 +239,7 @@ void ff_vc1_mc_1mv(VC1Context *v, int dir) interlace = v->last_interlaced; } } else { + ref = &s->next_pic; srcY = s->next_pic.data[0]; srcU = s->next_pic.data[1]; srcV = s->next_pic.data[2]; @@ -272,6 +276,9 @@ void ff_vc1_mc_1mv(VC1Context *v, int dir) } } + /* the 16 luma lines plus filter taps, the 8 chroma lines plus one */ + ff_vc1_await_ref(v, ref, FFMAX(src_y + 20, 2 * uvsrc_y + 22)); + srcY += src_y * s->linesize + src_x; srcU += uvsrc_y * s->uvlinesize + uvsrc_x; srcV += uvsrc_y * s->uvlinesize + uvsrc_x; @@ -458,6 +465,7 @@ void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg) int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0; int v_edge_pos = s->v_edge_pos >> v->field_mode; const uint8_t (*luty)[256]; + const MPVWorkPicture *ref; int use_ic; int interlace; int linesize; @@ -474,17 +482,20 @@ void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg) if (!dir) { if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) { + ref = &s->cur_pic; srcY = s->cur_pic.data[0]; luty = v->curr_luty; use_ic = *v->curr_use_ic; interlace = 1; } else { + ref = &s->last_pic; srcY = s->last_pic.data[0]; luty = v->last_luty; use_ic = v->last_use_ic; interlace = v->last_interlaced; } } else { + ref = &s->next_pic; srcY = s->next_pic.data[0]; luty = v->next_luty; use_ic = v->next_use_ic; @@ -553,6 +564,9 @@ void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg) src_y = av_clip(src_y, -18, s->avctx->coded_height + 1); } + /* 8 lines plus filter taps, every other line for a field MV */ + ff_vc1_await_ref(v, ref, src_y + (13 << fieldmv) + 2); + srcY += src_y * s->linesize + src_x; if (v->field_mode && v->ref_field_type[dir]) srcY += linesize; @@ -641,6 +655,7 @@ void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir) int chroma_ref_type; int v_edge_pos = s->v_edge_pos >> v->field_mode; const uint8_t (*lutuv)[256]; + const MPVWorkPicture *ref; int use_ic; int interlace; int uvlinesize; @@ -698,12 +713,14 @@ void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir) if (!dir) { if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) { + ref = &s->cur_pic; srcU = s->cur_pic.data[1]; srcV = s->cur_pic.data[2]; lutuv = v->curr_lutuv; use_ic = *v->curr_use_ic; interlace = 1; } else { + ref = &s->last_pic; srcU = s->last_pic.data[1]; srcV = s->last_pic.data[2]; lutuv = v->last_lutuv; @@ -711,6 +728,7 @@ void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir) interlace = v->last_interlaced; } } else { + ref = &s->next_pic; srcU = s->next_pic.data[1]; srcV = s->next_pic.data[2]; lutuv = v->next_lutuv; @@ -723,6 +741,8 @@ void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir) return; } + ff_vc1_await_ref(v, ref, 2 * uvsrc_y + 22); + srcU += uvsrc_y * s->uvlinesize + uvsrc_x; srcV += uvsrc_y * s->uvlinesize + uvsrc_x; @@ -852,6 +872,7 @@ void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg) int interlace; int uvlinesize; const uint8_t (*lutuv)[256]; + const MPVWorkPicture *ref; if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) return; @@ -880,12 +901,14 @@ void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg) else uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1); if (i < 2 ? dir : dir2) { + ref = &s->next_pic; srcU = s->next_pic.data[1]; srcV = s->next_pic.data[2]; lutuv = v->next_lutuv; use_ic = v->next_use_ic; interlace = v->next_interlaced; } else { + ref = &s->last_pic; srcU = s->last_pic.data[1]; srcV = s->last_pic.data[2]; lutuv = v->last_lutuv; @@ -894,6 +917,8 @@ void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg) } if (!srcU) return; + /* 4 chroma lines plus one, every other line for a field MV */ + ff_vc1_await_ref(v, ref, 2 * uvsrc_y + (12 << fieldmv) + 2); srcU += uvsrc_y * s->uvlinesize + uvsrc_x; srcV += uvsrc_y * s->uvlinesize + uvsrc_x; uvmx_field[i] = (uvmx_field[i] & 3) << 1; @@ -1056,6 +1081,8 @@ void ff_vc1_interp_mc(VC1Context *v) } } + ff_vc1_await_ref(v, &s->next_pic, FFMAX(src_y + 20, 2 * uvsrc_y + 22)); + srcY += src_y * s->linesize + src_x; srcU += uvsrc_y * s->uvlinesize + uvsrc_x; srcV += uvsrc_y * s->uvlinesize + uvsrc_x; diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index 5586b77adb2..14778d65c74 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -770,6 +770,7 @@ static av_cold void vc1_decode_reset(AVCodecContext *avctx) av_freep(&v->mv_f_base); av_freep(&v->mv_f_next_base); av_refstruct_unref(&v->mv_f_next_ref); + av_refstruct_unref(&v->mv_f_next_pic); av_refstruct_pool_uninit(&v->mv_f_pool); av_freep(&v->block); av_freep(&v->cbp_base); @@ -788,6 +789,15 @@ av_cold int ff_vc1_decode_end(AVCodecContext *avctx) return ff_mpv_decode_close(avctx); } +static av_cold void vc1_decode_flush(AVCodecContext *avctx) +{ + VC1Context *const v = avctx->priv_data; + + ff_mpeg_flush(avctx); + /* The picture is done by now; its field flags stay as they are. */ + av_refstruct_unref(&v->mv_f_next_pic); +} + /** Decode a VC1/WMV3 frame * @todo TODO: Handle VC-1 IDUs (Transport level?) */ @@ -809,6 +819,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, int raw_size; } *slices = NULL, *tmp; unsigned slices_allocated = 0; + int frame_started = 0; v->second_field = 0; @@ -1057,6 +1068,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, if ((ret = ff_mpv_frame_start(s, avctx)) < 0) { goto err; } + frame_started = 1; v->s.cur_pic.ptr->field_picture = v->field_mode; v->s.cur_pic.ptr->f->flags |= AV_FRAME_FLAG_INTERLACED * (v->fcm != PROGRESSIVE); @@ -1077,7 +1089,9 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, /* A reference field picture writes its field flags into a buffer of its * own, which B field pictures read as mv_f_next until the next one; any - * other picture writes them into scratch space. */ + * other picture writes them into scratch space. A frame thread reading + * them waits for the picture that writes them, so only frame threading + * keeps a reference to that picture. */ if (v->field_mode && s->pict_type != AV_PICTURE_TYPE_B && !avctx->hwaccel) { uint8_t *mv_f = av_refstruct_pool_get(v->mv_f_pool); if (!mv_f) { @@ -1088,6 +1102,8 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, v->mv_f_next_ref = mv_f; v->mv_f[0] = mv_f + s->b8_stride + 1; v->mv_f_next[0] = v->mv_f[0]; + if (avctx->active_thread_type & FF_THREAD_FRAME) + av_refstruct_replace(&v->mv_f_next_pic, s->cur_pic.ptr); } else { v->mv_f[0] = v->mv_f_base + s->b8_stride + 1; } @@ -1382,6 +1398,11 @@ image: end: ret = buf_size; err: + /* A reference picture that fails after it started is never finished by + * ff_mpv_frame_end(), so a frame thread waiting for its rows would wait + * forever. Let it read what is there. */ + if (ret < 0 && frame_started && s->cur_pic.reference) + ff_thread_progress_report(&s->cur_pic.ptr->progress, INT_MAX); av_free(buf2); for (i = 0; i < n_slices; i++) av_free(slices[i].buf); @@ -1399,7 +1420,7 @@ const FFCodec ff_vc1_decoder = { .init = vc1_decode_init, .close = ff_vc1_decode_end, FF_CODEC_DECODE_CB(vc1_decode_frame), - .flush = ff_mpeg_flush, + .flush = vc1_decode_flush, .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, .hw_configs = (const AVCodecHWConfigInternal *const []) { #if CONFIG_VC1_DXVA2_HWACCEL @@ -1441,7 +1462,7 @@ const FFCodec ff_wmv3_decoder = { .init = vc1_decode_init, .close = ff_vc1_decode_end, FF_CODEC_DECODE_CB(vc1_decode_frame), - .flush = ff_mpeg_flush, + .flush = vc1_decode_flush, .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, .hw_configs = (const AVCodecHWConfigInternal *const []) { #if CONFIG_WMV3_DXVA2_HWACCEL -- 2.54.1 >From 0d16fbb82c8bf97347709f4993b2483df4714d69 Mon Sep 17 00:00:00 2001 From: Vincent Herbst <[email protected]> Date: Sun, 22 Nov 2026 06:04:29 +0100 Subject: [PATCH 4/4] avcodec/vc1dec: add frame threading The vc1 and wmv3 decoders now decode with frame threads. vc1image and wmv3image stay single-threaded. update_thread_context() copies the sequence header and entry point, the reference pictures, and what one picture hands the next: the intensity compensation tables, refdist for B field pictures, the rounding toggle of Simple and Main profile, and the field flags of the last reference field picture. A thread copy that has to resize keeps the pixel format of the thread before it instead of negotiating one again. The next thread starts once nothing it reads can change: - a B picture after its header, since no later picture reads from it; - a frame or interlaced frame reference picture without slices after its header, since later pictures wait for its rows; - a reference field picture after its second field header, which can change the intensity compensation tables; - any other picture when it is done, since a slice header may repeat the picture header. fate-vc1, fate-wmv3-drm and fate-mss2 pass with THREAD_TYPE=frame and THREADS=1, 2, 3, 4, 8 and 16. Frame hashes after repeated seeks and flushes match between 1, 3 and 8 threads on the FATE samples remuxed to Matroska. 600 damaged copies of the FATE samples decode without a hang or a crash at 1, 3 and 8 threads, and AddressSanitizer is clean on 300 of them. ThreadSanitizer is clean on the intact samples, with and without seeks. On 26 of 300 damaged copies it reports races in ff_er_frame_end(): error concealment rewrites rows that the picture reported before the error was found, while another thread reads them. The H.264 decoder shows the same on damaged streams, and the H.263 decoder reports its progress the same way. The concealment of a damaged picture then depends on timing. On an Apple M1, 6 threads against 1: ilaced_twomv.vc1 (1080, interlaced) 6.48 s -> 2.56 s SA10143.vc1 (480, field pictures) 1.54 s -> 0.80 s SMM0005.rcv (WMV3 Main) 0.89 s -> 0.30 s SA20021.vc1 (480, sliced) 1.23 s -> 1.19 s In a stream with slices only the B pictures run ahead. classicjazz measured 1080 Blu-ray VC-1 on an M1 Max at 6 threads: progressive titles decode 4.0 to 4.5 times as fast, titles with field pictures only 2.2 to 2.4 times. Co-authored-by: classicjazz <[email protected]> Signed-off-by: Vincent Herbst <[email protected]> --- libavcodec/vc1dec.c | 125 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 5 deletions(-) diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c index 14778d65c74..7423a658e20 100644 --- a/libavcodec/vc1dec.c +++ b/libavcodec/vc1dec.c @@ -42,6 +42,7 @@ #include "msmpeg4_vc1_data.h" #include "profiles.h" #include "simple_idct.h" +#include "thread.h" #include "vc1.h" #include "vc1data.h" #include "vc1_vlc_data.h" @@ -455,7 +456,11 @@ static enum AVPixelFormat vc1_get_format(AVCodecContext *avctx) static void vc1_decode_reset(AVCodecContext *avctx); -av_cold int ff_vc1_decode_init(AVCodecContext *avctx) +/** + * @param negotiate_format 0 keeps avctx->pix_fmt: a frame thread copy takes + * it from the thread before it + */ +static av_cold int vc1_init_context(AVCodecContext *avctx, int negotiate_format) { VC1Context *const v = avctx->priv_data; MpegEncContext *const s = &v->s; @@ -469,7 +474,8 @@ av_cold int ff_vc1_decode_init(AVCodecContext *avctx) if (ret < 0) return ret; - avctx->pix_fmt = vc1_get_format(avctx); + if (negotiate_format) + avctx->pix_fmt = vc1_get_format(avctx); ret = ff_mpv_common_init(s); if (ret < 0) @@ -483,6 +489,11 @@ av_cold int ff_vc1_decode_init(AVCodecContext *avctx) return 0; } +av_cold int ff_vc1_decode_init(AVCodecContext *avctx) +{ + return vc1_init_context(avctx, 1); +} + av_cold void ff_vc1_init_transposed_scantables(VC1Context *v) { int i; @@ -798,6 +809,89 @@ static av_cold void vc1_decode_flush(AVCodecContext *avctx) av_refstruct_unref(&v->mv_f_next_pic); } +#if HAVE_THREADS +static int vc1_update_thread_context(AVCodecContext *dst, + const AVCodecContext *src) +{ + VC1Context *const v = dst->priv_data; + const VC1Context *const v1 = src->priv_data; + MpegEncContext *const s = &v->s; + const MpegEncContext *const s1 = &v1->s; + int ret; + + if (dst == src) + return 0; + + /* Only state the source no longer changes is read here: the source may + * still be decoding (see ff_thread_finish_setup() in vc1_decode_frame()). + * After setup, a B picture's second field header, its blocks and the + * doubled field linesize keep changing its context, and so do the blocks + * of a frame or interlaced frame reference picture. */ + + /* sequence header and entry point */ + memcpy(&v->res_sprite, &v1->res_sprite, + (const char *)&v1->finterpflag + sizeof(v1->finterpflag) - + (const char *)&v1->res_sprite); + v->zz_8x4 = v1->zz_8x4; + v->zz_4x8 = v1->zz_4x8; + v->hrd_num_leaky_buckets = v1->hrd_num_leaky_buckets; + v->resync_marker = v1->resync_marker; + v->broken_link = v1->broken_link; + v->closed_entry = v1->closed_entry; + v->range_mapy_flag = v1->range_mapy_flag; + v->range_mapuv_flag = v1->range_mapuv_flag; + v->range_mapy = v1->range_mapy; + v->range_mapuv = v1->range_mapuv; + + if (!s1->context_initialized) + return 0; + + if (s->context_initialized && + (s->width != s1->width || s->height != s1->height)) + vc1_decode_reset(dst); + if (!s->context_initialized) { + ret = vc1_init_context(dst, 0); + if (ret < 0) + return ret; + } + s->h_edge_pos = s1->h_edge_pos; + s->v_edge_pos = s1->v_edge_pos; + s->low_delay = s1->low_delay; + s->workaround_bugs = s1->workaround_bugs; + + ff_mpv_replace_picture(&s->last_pic, &s1->last_pic); + ff_mpv_replace_picture(&s->next_pic, &s1->next_pic); + + /* carried from picture to picture */ + if (v1->profile < PROFILE_ADVANCED) // Advanced codes it in every header + v->rnd = v1->rnd; + v->refdist = v1->refdist; // a B field picture reads its anchor's + v->last_use_ic = v1->last_use_ic; + v->next_use_ic = v1->next_use_ic; + v->aux_use_ic = v1->aux_use_ic; + memcpy(v->last_luty, v1->last_luty, sizeof(v->last_luty)); + memcpy(v->last_lutuv, v1->last_lutuv, sizeof(v->last_lutuv)); + memcpy(v->aux_luty, v1->aux_luty, sizeof(v->aux_luty)); + memcpy(v->aux_lutuv, v1->aux_lutuv, sizeof(v->aux_lutuv)); + memcpy(v->next_luty, v1->next_luty, sizeof(v->next_luty)); + memcpy(v->next_lutuv, v1->next_lutuv, sizeof(v->next_lutuv)); + v->curr_use_ic = !v1->curr_use_ic ? NULL : + v1->curr_use_ic == &v1->aux_use_ic ? &v->aux_use_ic : &v->next_use_ic; + v->curr_luty = !v1->curr_luty ? NULL : + v1->curr_luty == v1->aux_luty ? v->aux_luty : v->next_luty; + v->curr_lutuv = !v1->curr_lutuv ? NULL : + v1->curr_lutuv == v1->aux_lutuv ? v->aux_lutuv : v->next_lutuv; + + /* field flags of the last reference field picture (B field direct mode) */ + av_refstruct_replace(&v->mv_f_next_ref, v1->mv_f_next_ref); + av_refstruct_replace(&v->mv_f_next_pic, v1->mv_f_next_pic); + v->mv_f_next[0] = (v->mv_f_next_ref ? v->mv_f_next_ref : v->mv_f_next_base) + + s->b8_stride + 1; + + return 0; +} +#endif + /** Decode a VC1/WMV3 frame * @todo TODO: Handle VC-1 IDUs (Transport level?) */ @@ -819,7 +913,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, int raw_size; } *slices = NULL, *tmp; unsigned slices_allocated = 0; - int frame_started = 0; + int setup_finished = 0, frame_started = 0; v->second_field = 0; @@ -1108,6 +1202,19 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, v->mv_f[0] = v->mv_f_base + s->b8_stride + 1; } + /* Let the next frame thread start once everything it reads is final. + * A B picture changes nothing a later picture reads. A frame or + * interlaced frame reference picture without slices has nothing left + * to change either: later pictures wait for its rows as they need them. + * A reference field picture finishes setup once its second field header + * is parsed, which can change the intensity compensation tables. With + * slices, the frame thread code does it when the decode has returned, + * since a slice may repeat the picture header. */ + if (s->pict_type == AV_PICTURE_TYPE_B || (!v->field_mode && !n_slices)) { + ff_thread_finish_setup(avctx); + setup_finished = 1; + } + if (avctx->hwaccel) { const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel); s->mb_y = 0; @@ -1299,6 +1406,10 @@ static int vc1_decode_frame(AVCodecContext *avctx, AVFrame *pict, goto err; continue; } + if (!setup_finished && n_slices == 1) { + ff_thread_finish_setup(avctx); + setup_finished = 1; + } } else if (get_bits1(&v->gb)) { v->pic_header_flag = 1; if ((header_ret = ff_vc1_parse_frame_header_adv(v, &v->gb)) < 0) { @@ -1420,8 +1531,10 @@ const FFCodec ff_vc1_decoder = { .init = vc1_decode_init, .close = ff_vc1_decode_end, FF_CODEC_DECODE_CB(vc1_decode_frame), + UPDATE_THREAD_CONTEXT(vc1_update_thread_context), .flush = vc1_decode_flush, - .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, + .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | + AV_CODEC_CAP_FRAME_THREADS, .hw_configs = (const AVCodecHWConfigInternal *const []) { #if CONFIG_VC1_DXVA2_HWACCEL HWACCEL_DXVA2(vc1), @@ -1462,8 +1575,10 @@ const FFCodec ff_wmv3_decoder = { .init = vc1_decode_init, .close = ff_vc1_decode_end, FF_CODEC_DECODE_CB(vc1_decode_frame), + UPDATE_THREAD_CONTEXT(vc1_update_thread_context), .flush = vc1_decode_flush, - .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY, + .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | + AV_CODEC_CAP_FRAME_THREADS, .hw_configs = (const AVCodecHWConfigInternal *const []) { #if CONFIG_WMV3_DXVA2_HWACCEL HWACCEL_DXVA2(wmv3), -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
