PR #26364 opened by sjpotter URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26364 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26364.patch
In P pictures and advanced profile I/BI pictures, VC-1 reconstructs every block in `v->block`, a ring of one MB row plus two MBs of blocks (about 94 KB at 1080p). Intra blocks are put into the picture later by `vc1_put_blocks_clamped()`: one MB row and one MB column behind decoding in progressive and field pictures, one MB column behind in interlaced frame pictures. That delay exists only so that overlap smoothing can filter the neighbouring blocks first. B pictures already work differently: they reconstruct into the small `v->blocks` buffer and put intra blocks at once. This change does the same for P and I pictures where it can: - Coded inter blocks are added to the prediction straight away and never read back, so they always use `v->blocks`. - When overlap smoothing is off for the picture, intra blocks also use `v->blocks` and are put immediately (with `fieldtx` honoured in interlaced frame pictures), and `vc1_put_blocks_clamped()` is not called. - When overlap smoothing is on, intra blocks keep using the ring and the delayed put, as before. - Simple/main profile I pictures (`vc1_decode_i_blocks()`) are not changed. It is plain C in `vc1_block.c`, so it applies to all architectures. The point is the memory footprint: block data stays in L1 instead of streaming through the 94 KB ring, intra pixels are written while those picture lines are still in cache instead of 16 lines later, and there is one fewer pass over the blocks per MB. The output is unchanged for streams that decode without errors. One behaviour changes on damaged streams: when a slice stops at a decoding error, the intra MBs decoded just before the error are now in the picture. Before, the MBs still waiting for the delayed put were never written. Error concealment then starts from different pixels, so the output of damaged streams can differ from before. The code was written with AI assistance and tested as described below. ### Why These changes came out of testing the VC-1 frame-threading PR ([#26273](https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26273)) on a low-power ARM TV. Even with 3 or 4 frame threads that TV can't decode some VC-1 Blu-ray content in real time (1080i interlaced reached about 0.83-0.91x real time with 4 threads). Threading alone isn't enough there, so the per-thread cost of the decoder has to come down too. This change doesn't depend on #26273 and applies to master on its own. All numbers below except the last table were measured on master. The test builds (both master and patched) also had the VC-1 parser fix from the separate parser PR applied, because without it some of the interlaced clips don't decode correctly on master. The parser fix doesn't touch block decoding. The single-thread profiling that found this overhead was done on an Apple M3 Max, and the change was then tested on the TV to see whether the gain carries over. The two are very different machines: a wide out-of-order core with large caches, against an in-order Cortex-A53 with 32 KB of L1 data cache per core and a 512 KB L2 shared by all four cores. A change that looks good on the Mac can gain less on the TV, or more. Both sets of numbers are given below. For this change the TV gained more than the Mac: on the Mac the cycle difference is within noise, while the TV gets a small but consistent gain even on one thread. ### Apple M3 Max (aarch64), single thread Decode-only (`-threads 1`, `-f null`, first 800 frames), CPU cycles and instructions from `/usr/bin/time -l`, best of 5 interleaved runs, both builds in one session. Speedup is master cycles / patched cycles; instructions are relative to master. | clip | master Gcycles | patched Gcycles | speedup | instructions | |---|---:|---:|---:|---:| | progressive 1080p (DarkKnight) | 14.58 | 14.50 | 1.006x | 0.992 | | progressive 1080p (Contact) | 12.85 | 12.77 | 1.006x | 0.991 | | progressive 1080p (HP1) | 12.20 | 12.13 | 1.006x | 0.993 | | progressive 1080p (HumanPlanet) | 14.70 | 14.71 | 1.000x | 0.994 | | interlaced 1080i (Diana) | 19.98 | 20.07 | 0.995x | 0.996 | | interlaced 1080i (NIN peak) | 15.09 | 15.04 | 1.003x | 0.996 | | interlaced 1080i (NIN mid) | 9.38 | 9.50 | 0.988x | 0.995 | | interlaced 1080i (PlanetEarth peak) | 25.17 | 25.09 | 1.003x | 0.997 | | interlaced 1080i (PlanetEarth mid) | 20.19 | 19.96 | 1.011x | 0.996 | On the Mac this change makes no measurable difference: instructions drop by 0.3-0.9%, and the cycle differences (-1.2% to +1.1%) are inside this machine's session-to-session noise of about ±2%. An earlier session with 3 runs gave 0.983x to 1.021x. With its large caches, the M3 Max doesn't feel the ring traffic. ### Sony BRAVIA TV (Cortex-A53, 32-bit), single thread MediaTek MT5891, 4x Cortex-A53 @ 1.0 GHz, 32-bit Android 9, NDK clang, armv7-a with NEON. Decode-only (`-threads 1`, `-f null`, whole clip, 2156-2696 frames), median of 3 runs. CPU time is user+sys. | clip | master fps | patched fps | fps | CPU time | |---|---:|---:|---:|---:| | progressive 1080p (DarkKnight) | 11.38 | 11.77 | +3.4% | -3.2% | | progressive 1080p (Contact) | 11.78 | 12.18 | +3.4% | -3.2% | | progressive 1080p (HP1) | 12.05 | 12.29 | +2.0% | -1.9% | | progressive 1080p (HumanPlanet) | 10.58 | 11.03 | +4.3% | -4.0% | | interlaced 1080i (Diana) | 8.46 | 8.61 | +1.8% | -1.7% | | interlaced 1080i (NIN peak) | 9.82 | 9.95 | +1.3% | -1.3% | | interlaced 1080i (NIN mid) | 9.52 | 9.64 | +1.3% | -1.2% | | interlaced 1080i (PlanetEarth peak) | 6.72 | 6.88 | +2.4% | -2.3% | | interlaced 1080i (PlanetEarth mid) | 7.11 | 7.29 | +2.5% | -2.4% | Run-to-run spread on the TV is about ±1%, and here every patched run was faster than every master run except one Diana run. Progressive gains +2% to +4.3%, interlaced +1.3% to +2.5%; the NIN clips are close to the noise level. Progressive gains more because there the old code put intra blocks a full MB row late, so it re-read each ring slot about 94 KB later and wrote pixels 16 lines above the current row. Interlaced frame pictures already put intra blocks only one MB column late, and most interlaced pictures are B pictures, which never used the ring. ### Sony BRAVIA TV with frame threading (not master) For context only: the same change was first measured on the TV on top of the frame-threading PR (#26273) and other local changes, not on master. There, with 3 or 4 decoding threads sharing the 512 KB L2, the progressive clips gained +3.7% to +8.9% fps at 4 threads and +3% to +5.6% at 3 threads, and the interlaced clips about 0-2%. Those were single runs per configuration, so they are noisier than the tables above. Master has no VC-1 frame threading, so this part of the gain can't be shown on master today. The clips are commercial and can't be shared. ### Testing - `make fate-vc1 fate-wmv3-drm fate-mss2 fate-checkasm-vc1dsp` with a full macOS (aarch64) build, all passing. With overlap smoothing on, the FATE samples only exercise advanced profile I pictures (SA00040, SA00050); none of their P pictures use overlap smoothing. - To cover P pictures with overlap smoothing on, decoded 34 public WMV3/VC-1 samples from samples.ffmpeg.org (`V-codecs/WMV9/` and `V-codecs/WVC1/`) with `-threads 1 -f framemd5`, master against patched: all bit-exact. Eight of them (b52crash2, bloodyback, guitar, nokia_n90, psychonauts and the three `sb_wmv9_samples/*-q60`) have P pictures with overlap smoothing on, five of those mixed with P pictures where it is off. The set also covers simple, main and advanced profile, and interlaced frame and field pictures. - Decoded the 9 benchmark VC-1 Blu-ray clips above on macOS with `-threads 1 -f framemd5`: bit-exact against reference decodes from an unpatched decoder. This content never enables overlap smoothing, so it covers the immediate put in progressive, interlaced field and interlaced frame (`fieldtx`) pictures. - Decoded the same 9 clips on the TV with `-threads 1 -f framemd5` using an armv7-a build of this branch: bit-exact against the same reference decodes. - Damaged streams: decoded copies of 7 samples (FATE, public and Blu-ray) with the `noise` bitstream filter applied at two strengths. On 3 of the 7 the output differs from master. In the case I checked, the first difference is in the MB rows just above where a slice stopped at an error, as described above. Both builds decode all of them to the end without crashing. - Builds cleanly for macOS (aarch64) and with the Android NDK for armv7-a and aarch64. From a2b99545cedc3f25552a0c9164c3b7a35e344a8c Mon Sep 17 00:00:00 2001 From: Shaya Potter <[email protected]> Date: Thu, 26 Nov 2026 03:08:19 +0200 Subject: [PATCH] avcodec/vc1_block: decode P and I blocks into v->blocks when possible P picture blocks and advanced profile I/BI picture blocks were always reconstructed in the v->block ring, which holds one MB row plus two MBs of blocks (about 94 KB at 1080p), and intra blocks were put into the picture by vc1_put_blocks_clamped() one MB row and one MB column later (one MB column later in interlaced frame pictures). The delay is only needed so that overlap smoothing can filter the neighbouring blocks first. Inter blocks are added to the prediction at once and never read back, so reconstruct them in the small v->blocks buffer instead, as the B picture code already does. When overlap smoothing is off for the picture, do the same for intra blocks and put them immediately, honouring fieldtx in interlaced frame pictures. With overlap smoothing on, intra blocks keep using the ring and the delayed put. Simple/main profile I pictures are not changed. This keeps the block data in L1 instead of streaming it through the ring, and drops the per-MB vc1_put_blocks_clamped() call when overlap smoothing is off. The output is unchanged for streams that decode without errors. When decoding of a slice stops at an error, the intra MBs decoded just before it are now in the picture; before, the ones still waiting for the delayed put were never written. --- libavcodec/vc1_block.c | 97 +++++++++++++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 25 deletions(-) diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c index a84c853b6f1..7c621862cf8 100644 --- a/libavcodec/vc1_block.c +++ b/libavcodec/vc1_block.c @@ -172,6 +172,25 @@ static void vc1_put_blocks_clamped(VC1Context *v, int put_signed) idx = 0; \ } while (0) +/* Without overlap smoothing, intra blocks can be put as soon as they are + * reconstructed instead of going through vc1_put_blocks_clamped(). */ +static av_always_inline void vc1_put_intra_block(VC1Context *v, int16_t block[64], + int n, int fieldtx) +{ + MpegEncContext *s = &v->s; + + if (n > 3) + s->idsp.put_signed_pixels_clamped(block, s->dest[n - 3], s->uvlinesize); + else if (fieldtx) + s->idsp.put_signed_pixels_clamped(block, + s->dest[0] + ((n & 2) >> 1) * s->linesize + (n & 1) * 8, + s->linesize << 1); + else + s->idsp.put_signed_pixels_clamped(block, + s->dest[0] + (n & 2) * 4 * s->linesize + (n & 1) * 8, + s->linesize); +} + /***********************************************************************/ /** * @name VC-1 Block-level functions @@ -1252,6 +1271,7 @@ static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 }; static int vc1_decode_p_mb(VC1Context *v) { MpegEncContext *s = &v->s; + const int overlap = v->overlap && v->pq >= 9; GetBitContext *const gb = &v->gb; int i, j; int mb_pos = s->mb_x + s->mb_y * s->mb_stride; @@ -1267,6 +1287,7 @@ static int vc1_decode_p_mb(VC1Context *v) int dst_idx, off; int skipped, fourmv; int block_cbp = 0, pat, block_tt = 0, block_intra = 0; + int16_t *blk; int ret; mquant = v->pq; /* lossy initialization */ @@ -1326,20 +1347,23 @@ static int vc1_decode_p_mb(VC1Context *v) if (i == 1 || i == 3 || s->mb_x) v->c_avail = v->mb_type[s->block_index[i] - 1]; - ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant, + blk = overlap ? v->block[v->cur_blk_idx][block_map[i]] : v->blocks[i]; + ret = vc1_decode_intra_block(v, blk, i, val, mquant, (i & 4) ? v->codingset2 : v->codingset); if (ret < 0) return ret; if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY)) continue; - v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]); + v->vc1dsp.vc1_inv_trans_8x8(blk); if (v->rangeredfrm) for (j = 0; j < 64; j++) - v->block[v->cur_blk_idx][block_map[i]][j] *= 2; + blk[j] *= 2; + if (!overlap) + vc1_put_intra_block(v, blk, i, 0); block_cbp |= 0xF << (i << 2); block_intra |= 1 << i; } else if (val) { - pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb, first_block, + pat = vc1_decode_p_block(v, v->blocks[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize, CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt); if (pat < 0) @@ -1431,20 +1455,23 @@ static int vc1_decode_p_mb(VC1Context *v) if (i == 1 || i == 3 || s->mb_x) v->c_avail = v->mb_type[s->block_index[i] - 1]; - ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, is_coded[i], mquant, + blk = overlap ? v->block[v->cur_blk_idx][block_map[i]] : v->blocks[i]; + ret = vc1_decode_intra_block(v, blk, i, is_coded[i], mquant, (i & 4) ? v->codingset2 : v->codingset); if (ret < 0) return ret; if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY)) continue; - v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]); + v->vc1dsp.vc1_inv_trans_8x8(blk); if (v->rangeredfrm) for (j = 0; j < 64; j++) - v->block[v->cur_blk_idx][block_map[i]][j] *= 2; + blk[j] *= 2; + if (!overlap) + vc1_put_intra_block(v, blk, i, 0); block_cbp |= 0xF << (i << 2); block_intra |= 1 << i; } else if (is_coded[i]) { - pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb, + pat = vc1_decode_p_block(v, v->blocks[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize, CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), @@ -1473,9 +1500,10 @@ static int vc1_decode_p_mb(VC1Context *v) } } end: - if (v->overlap && v->pq >= 9) + if (overlap) { ff_vc1_p_overlap_filter(v); - vc1_put_blocks_clamped(v, 1); + vc1_put_blocks_clamped(v, 1); + } v->cbp[s->mb_x] = block_cbp; v->ttblk[s->mb_x] = block_tt; @@ -1489,6 +1517,7 @@ end: static int vc1_decode_p_mb_intfr(VC1Context *v) { MpegEncContext *s = &v->s; + const int overlap = v->overlap && v->pq >= 9; GetBitContext *const gb = &v->gb; int i; int mb_pos = s->mb_x + s->mb_y * s->mb_stride; @@ -1505,6 +1534,7 @@ static int vc1_decode_p_mb_intfr(VC1Context *v) int block_cbp = 0, pat, block_tt = 0; int idx_mbmode = 0, mvbp; int fieldtx; + int16_t *blk; int ret; mquant = v->pq; /* Lossy initialization */ @@ -1577,13 +1607,16 @@ static int vc1_decode_p_mb_intfr(VC1Context *v) if (i == 1 || i == 3 || s->mb_x) v->c_avail = v->mb_type[s->block_index[i] - 1]; - ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant, + blk = overlap ? v->block[v->cur_blk_idx][block_map[i]] : v->blocks[i]; + ret = vc1_decode_intra_block(v, blk, i, val, mquant, (i & 4) ? v->codingset2 : v->codingset); if (ret < 0) return ret; if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY)) continue; - v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]); + v->vc1dsp.vc1_inv_trans_8x8(blk); + if (!overlap) + vc1_put_intra_block(v, blk, i, fieldtx); block_cbp |= 0xf << (i << 2); } @@ -1655,7 +1688,7 @@ static int vc1_decode_p_mb_intfr(VC1Context *v) else off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize)); if (val) { - pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb, + pat = vc1_decode_p_block(v, v->blocks[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : (s->linesize << fieldtx), CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt); @@ -1684,9 +1717,10 @@ static int vc1_decode_p_mb_intfr(VC1Context *v) ff_vc1_mc_1mv(v, 0); v->fieldtx_plane[mb_pos] = 0; } - if (v->overlap && v->pq >= 9) + if (overlap) { ff_vc1_p_overlap_filter(v); - vc1_put_blocks_clamped(v, 1); + vc1_put_blocks_clamped(v, 1); + } v->cbp[s->mb_x] = block_cbp; v->ttblk[s->mb_x] = block_tt; @@ -1697,6 +1731,7 @@ static int vc1_decode_p_mb_intfr(VC1Context *v) static int vc1_decode_p_mb_intfi(VC1Context *v) { MpegEncContext *s = &v->s; + const int overlap = v->overlap && v->pq >= 9; GetBitContext *const gb = &v->gb; int i; int mb_pos = s->mb_x + s->mb_y * s->mb_stride; @@ -1712,6 +1747,7 @@ static int vc1_decode_p_mb_intfi(VC1Context *v) int pred_flag = 0; int block_cbp = 0, pat, block_tt = 0; int idx_mbmode = 0; + int16_t *blk; int ret; mquant = v->pq; /* Lossy initialization */ @@ -1743,13 +1779,16 @@ static int vc1_decode_p_mb_intfi(VC1Context *v) if (i == 1 || i == 3 || s->mb_x) v->c_avail = v->mb_type[s->block_index[i] - 1]; - ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant, + blk = overlap ? v->block[v->cur_blk_idx][block_map[i]] : v->blocks[i]; + ret = vc1_decode_intra_block(v, blk, i, val, mquant, (i & 4) ? v->codingset2 : v->codingset); if (ret < 0) return ret; if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY)) continue; - v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[i]]); + v->vc1dsp.vc1_inv_trans_8x8(blk); + if (!overlap) + vc1_put_intra_block(v, blk, i, 0); block_cbp |= 0xf << (i << 2); } } else { @@ -1793,7 +1832,7 @@ static int vc1_decode_p_mb_intfi(VC1Context *v) val = ((cbp >> (5 - i)) & 1); off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize; if (val) { - pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb, + pat = vc1_decode_p_block(v, v->blocks[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize, CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), @@ -1807,9 +1846,10 @@ static int vc1_decode_p_mb_intfi(VC1Context *v) } } } - if (v->overlap && v->pq >= 9) + if (overlap) { ff_vc1_p_overlap_filter(v); - vc1_put_blocks_clamped(v, 1); + vc1_put_blocks_clamped(v, 1); + } v->cbp[s->mb_x] = block_cbp; v->ttblk[s->mb_x] = block_tt; @@ -2625,6 +2665,8 @@ static int vc1_decode_i_blocks_adv(VC1Context *v) int mb_pos; int mquant; int mqdiff; + const int overlap = v->overlap && (v->pq >= 9 || v->condover != CONDOVER_NONE); + int16_t (*blk)[64]; if (get_bits_left(gb) <= 1) return AVERROR_INVALIDDATA; @@ -2669,7 +2711,8 @@ static int vc1_decode_i_blocks_adv(VC1Context *v) for (;s->mb_x < s->mb_width; s->mb_x++) { mquant = v->pq; update_block_index(s); - s->bdsp.clear_blocks(v->block[v->cur_blk_idx][0]); + blk = overlap ? v->block[v->cur_blk_idx] : v->blocks; + s->bdsp.clear_blocks(blk[0]); mb_pos = s->mb_x + s->mb_y * s->mb_stride; s->cur_pic.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA; for (int i = 0; i < 4; i++) { @@ -2713,17 +2756,21 @@ static int vc1_decode_i_blocks_adv(VC1Context *v) v->a_avail = !s->first_slice_line || (k == 2 || k == 3); v->c_avail = !!s->mb_x || (k == 1 || k == 3); - vc1_decode_i_block_adv(v, v->block[v->cur_blk_idx][block_map[k]], k, val, + vc1_decode_i_block_adv(v, blk[overlap ? block_map[k] : k], k, val, (k < 4) ? v->codingset : v->codingset2, mquant); if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY)) continue; - v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][block_map[k]]); + v->vc1dsp.vc1_inv_trans_8x8(blk[overlap ? block_map[k] : k]); + if (!overlap) + vc1_put_intra_block(v, blk[k], k, + v->fcm == ILACE_FRAME && v->fieldtx_plane[mb_pos]); } - if (v->overlap && (v->pq >= 9 || v->condover != CONDOVER_NONE)) + if (overlap) { ff_vc1_i_overlap_filter(v); - vc1_put_blocks_clamped(v, 1); + vc1_put_blocks_clamped(v, 1); + } if (v->loop_filter) ff_vc1_i_loop_filter(v); -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
