PR #25914 opened by mindfreeze
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25914
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25914.patch
The entropy shader (`apv_encode_tiles.comp.glsl`) ran one invocation per
transform block of a tile-component,
so a tile-component could hold at most 1024 blocks, the workgroup limit. APV
allows at most 20×20 tiles, so
this capped one `apv_vulkan` stream at about 6400×3600:
```
[apv_vulkan] Tile-component has too many transform blocks (5184 > 1024).
```
7680×4320 and 15360×8640 failed, and 16K needed 16 separate 4K streams stitched
together on playback.
## Change
- The workgroup is sized to the tile-component, capped at 1024 and the device's
`maxComputeWorkGroupInvocations` / `maxComputeWorkGroupSize[0]`.
- Larger tile-components are coded in chunks of one workgroup of blocks.
- Inside a chunk, the existing two scans compute the per-block bit offsets
and the first-AC level context,
and the subgroup rotate hands over the block tails, as before.
- Between chunks, three values carry over in shared memory: the first-AC
level of the last non-empty block,
the running bit offset, and the tail of the chunk's last block. So the
output is the same as one workgroup
covering the whole tile-component would produce.
- Tile-components of up to 1024 blocks take one chunk, so their output is
byte-identical to before.
- The shader addresses the bytestream and the bits within a slot with 32-bit
offsets. Tile sizes that would
overflow them are now rejected with a clear error.
- The 32×16 MB ceiling on the `tile_width`/`tile_height` options is dropped;
values are clamped to the frame.
At 15360×8640 the default is 400 tiles of 48×27 MBs, with 5184 luma blocks per
tile-component (6 chunks). The
coefficient buffer is about 760 MiB and the bytestream buffer about 3 GiB.
## How to reproduce
```
ffmpeg -init_hw_device vulkan=vk -filter_hw_device vk -f lavfi -i
testsrc2=s=15360x8640 -frames:v 1 \
-vf format=yuv422p10le,hwupload -c:v apv_vulkan -qp 32 out.apv
```
Before: `Tile-component has too many transform blocks (5184 > 1024)`.
After: it encodes.
## Testing
I have only tested on an NVIDIA RTX PRO 6000 Blackwell (96GB), driver 580.105.08
1. **Unchanged where it already worked.** Packets are byte-identical to master
for 1920×1080, 3840×2160 and
6400×3600; for 4:2:2 10-bit and 4:4:4 12-bit; and with explicit `-tile_width
32 -tile_height 8`.
2. **The chunked path is exact.** APV reconstruction doesn't depend on the tile
layout, so a 3840×2160 frame
with large tiles must decode to the same pixels as with the default tiles.
It does in all 16 cases tested:
4:2:2 and 4:4:4 at 10 and 12 bits, with tiles of 240×135 MBs (1 tile, 127
chunks), 100×50, 77×61
(remainder tiles, partial last chunks) and 64×16.
3. **16K in one stream.** 10 frames of real 15360×8640 4:4:4 12-bit content
(Unreal Engine render, QP 46,
28 MiB/frame) decode to the same pixels as master's 16 separate 3840×2160
streams of the same frames, one
process per tile, on all 16 tiles. FFmpeg's CPU decoder, FFmpeg's Vulkan
decoder and OpenAPV's
`oapv_app_dec` give identical output for the stream.
4. **Speed.** 3840×2160 4:2:2 10-bit takes about 1.17 s for 60 frames before
and after.
5. **FATE.** The APV tests pass (`fate-apv-*`, `fate-cbs-apv-*`,
`fate-checkasm-apv_dsp`,
`fate-lavf-fate-apv.mp4`).
## Notes
- Most of the shader diff is re-indentation from the new loop. The logic
changes are the three `carry_*`
shared variables, the scan-1 key using the index within the chunk, and lane 0
of subgroup 0 taking its
predecessor's tail from `carry_tail`.
- The default tile-size heuristic still grows tiles only up to 1024 blocks (one
chunk), so streams the encoder
already supported keep their tile layout.
- Only tested on NVIDIA.
Claude helped me to bake parts of the patches
From ebc2e0214950133aac34a32272518f2b3fb8dff7 Mon Sep 17 00:00:00 2001
From: Vibhoothi <[email protected]>
Date: Tue, 10 Nov 2026 16:42:08 +0000
Subject: [PATCH] apv_encode_vulkan: code tile-components of more than 1024
blocks
The entropy shader ran one invocation per transform block of a
tile-component, so a tile-component could hold at most 1024 blocks, the
workgroup size limit. With at most 20x20 tiles, this capped a stream at
about 6400x3600: 7680x4320 and 15360x8640 failed with "Tile-component
has too many transform blocks (5184 > 1024)", and 16K needed 16
separate 4K streams.
Size the workgroup to the tile-component, capped to 1024 and the device
limits, and code larger tile-components in chunks of one workgroup of
blocks. The bit offset, the first-AC level context and the tail of the
last block of a chunk are carried to the next chunk in shared memory,
so the bitstream is the same as with one invocation per block.
Tile-components of up to 1024 blocks are coded in one chunk, as before.
The shader addresses the bytestream and the bits of a slot with 32-bit
offsets, so reject tile sizes that would overflow them, and drop the
32x16 MB ceiling on the tile_width and tile_height options.
Tested on an RTX PRO 6000 (NVIDIA 580.105.08):
- streams up to 6400x3600, 4:2:2 10-bit and 4:4:4 12-bit, and with
explicit tile sizes, are byte-identical to before
- 3840x2160 with tiles of up to 240x135 MBs (127 chunks), including
remainder tiles and partial chunks, in 4:2:2 and 4:4:4 at 10 and 12
bits, decodes to the same pixels as with the default tiles
- 15360x8640 4:4:4 12-bit in one stream (400 tiles of 48x27 MBs, 6
chunks) decodes to the same pixels as 16 separate 4K streams, and the
FFmpeg, Vulkan and OpenAPV decoders agree on it
- 3840x2160 encoding speed is unchanged
Signed-off-by: Vibhoothi <[email protected]>
---
libavcodec/apv_encode_vulkan.c | 31 +--
libavcodec/vulkan/apv_encode_tiles.comp.glsl | 225 ++++++++++---------
2 files changed, 141 insertions(+), 115 deletions(-)
diff --git a/libavcodec/apv_encode_vulkan.c b/libavcodec/apv_encode_vulkan.c
index 3e7ca3a4f56..cb6ae7bbfbb 100644
--- a/libavcodec/apv_encode_vulkan.c
+++ b/libavcodec/apv_encode_vulkan.c
@@ -270,9 +270,14 @@ static int init_entropy_shader(AVCodecContext *avctx, int
blocks_per_mb,
/* One workgroup per tile-component, one invocation per transform block.
* Luma and chroma tile-components hold different block counts under
* chroma sub-sampling, so each gets a pipeline with its own size. */
+ const VkPhysicalDeviceLimits *limits = &ev->s.props.properties.limits;
uint32_t wg = ev->tile_mb_w * ev->tile_mb_h * blocks_per_mb;
uint32_t min_sg = ev->s.subgroup_props.minSubgroupSize;
+ /* Larger tile-components are coded in chunks of wg blocks */
+ wg = FFMIN3(wg, 1024, limits->maxComputeWorkGroupInvocations);
+ wg = FFMIN(wg, limits->maxComputeWorkGroupSize[0]);
+
/* The driver picks the subgroup size; size for the most it may use */
SPEC_LIST_CREATE(sl, 1, sizeof(uint32_t))
SPEC_LIST_ADD(sl, 0, 32, FFALIGN(wg, min_sg) / min_sg);
@@ -818,15 +823,6 @@ static av_cold int vulkan_encode_apv_init(AVCodecContext
*avctx)
return AVERROR(EINVAL);
}
- /* The entropy shader runs one invocation per block in a tile-component
- * and its shared buffers are sized for 1024. */
- if (ev->tile_mb_w * ev->tile_mb_h * ev->blocks_per_mb > 1024) {
- av_log(avctx, AV_LOG_ERROR,
- "Tile-component has too many transform blocks (%d > 1024).\n",
- ev->tile_mb_w * ev->tile_mb_h * ev->blocks_per_mb);
- return AVERROR_PATCHWELCOME;
- }
-
/* qp_chroma left at 0 means "use the luma QP". */
if (ev->qp_c == 0)
ev->qp_c = ev->qp_y;
@@ -852,6 +848,13 @@ static av_cold int vulkan_encode_apv_init(AVCodecContext
*avctx)
ev->bytestream_size = (size_t)ev->tile_count * ev->num_comp *
ev->slot_size;
ev->sizes_size = ((size_t)ev->tile_count * ev->num_comp + 1) *
sizeof(uint32_t);
+ /* The entropy shader uses 32-bit bytestream and bit offsets */
+ if (ev->bytestream_size > UINT32_MAX || ev->slot_size > UINT32_MAX / 8) {
+ av_log(avctx, AV_LOG_ERROR, "Tiles of %dx%d MBs are too large.\n",
+ ev->tile_mb_w, ev->tile_mb_h);
+ return AVERROR(EINVAL);
+ }
+
av_log(avctx, AV_LOG_VERBOSE,
"APV Vulkan encoder: %dx%d, %d tiles (%dx%d MBs each), "
"qp_y=%d qp_c=%d, coeffs=%zu KiB, bytestream=%zu KiB\n",
@@ -940,14 +943,12 @@ static const AVOption vulkan_encode_apv_options[] = {
AV_OPT_TYPE_CONST, { .i64 = APV_QMATRIX_FLAT }, 0, 0, VE,
"qmatrix" },
{ "hevc", "HEVC default intra scaling list (mild perceptual
shaping)", 0,
AV_OPT_TYPE_CONST, { .i64 = APV_QMATRIX_HEVC }, 0, 0, VE,
"qmatrix" },
- /* The minimum legal tile is 16x8 MBs; the maxima are this encoder's
- * ceiling of 1024 transform blocks per tile-component (256 MBs): with
- * the other dimension at its minimum, width <= 32 and height <= 16. A
- * value of 0 is the sentinel for the adaptive per-frame default. */
+ /* The minimum legal tile is 16x8 MBs. A value of 0 is the sentinel for
+ * the adaptive per-frame default. */
{ "tile_width", "Tile width in macroblocks (0 = adaptive, auto-sized per
frame)", OFFSET(tile_w_mbs_opt),
- AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 32, VE },
+ AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
{ "tile_height", "Tile height in macroblocks (0 = adaptive, auto-sized per
frame)", OFFSET(tile_h_mbs_opt),
- AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 16, VE },
+ AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
{ "async_depth", "Internal parallelization depth", OFFSET(async_depth),
AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, VE },
{ NULL }
diff --git a/libavcodec/vulkan/apv_encode_tiles.comp.glsl
b/libavcodec/vulkan/apv_encode_tiles.comp.glsl
index d0b2c5733dc..4147b3e83dc 100644
--- a/libavcodec/vulkan/apv_encode_tiles.comp.glsl
+++ b/libavcodec/vulkan/apv_encode_tiles.comp.glsl
@@ -34,7 +34,8 @@
/*
* One workgroup encodes one tile-component. The workgroup size (set by the
* host via spec constants 253/254/255) equals the number of transform blocks
- * in the tile-component, so there is exactly one invocation per block.
+ * in the tile-component, capped by the host; larger tile-components are coded
+ * in chunks of one workgroup of blocks.
*/
layout (constant_id = 0) const uint max_subgroups = 32; /* at the smallest
subgroup size */
@@ -137,6 +138,9 @@ layout (push_constant, scalar) uniform pushConstants {
shared uint32_t sg_lasttail[max_subgroups]; /* last block's tail, per subgroup
*/
shared uint32_t sg_scan [max_subgroups]; /* per-subgroup scan totals
*/
+shared uint32_t carry_level; /* carried over to the next chunk
*/
+shared uint32_t carry_bits;
+shared uint32_t carry_tail;
/*
* Flush only the complete bytes held by the writer. The trailing partial
@@ -276,7 +280,7 @@ void dc_context(uint coeff_base, uint idx, out int prev_dc,
out int prev_k_dc)
void main(void) [[maximally_reconverges]]
{
- const uint b = gl_LocalInvocationID.x;
+ const uint lid = gl_LocalInvocationID.x;
const uint comp = comp_base + gl_WorkGroupID.z;
const uint tile_idx = gl_WorkGroupID.y * uint(tile_count.x) +
gl_WorkGroupID.x;
const uint tile_comp = tile_idx * uint(num_comp) + comp;
@@ -301,118 +305,139 @@ void main(void) [[maximally_reconverges]]
* tile-component simply reads fewer blocks from its region.
*/
const uint coeff_base = tile_comp * blocks_per_tile * APV_BLK_COEFFS;
- const uint blk = coeff_base + b * APV_BLK_COEFFS;
- int prev_dc = 0, prev_k_dc = 5;
- uint64_t mask = uint64_t(0);
- if (b < nb) {
- dc_context(coeff_base, b, prev_dc, prev_k_dc);
- mask = nz_mask(blk);
+ if (lid == 0u) {
+ carry_level = 0u;
+ carry_bits = 0u;
+ carry_tail = 0u;
}
- /*
- * Scan #1: prev_1st_ac_level is the first-AC level of the most recent
- * *non-empty* block before this one. Pack (blockIndex+1, level) into a
- * key (empty blocks -> 0) so a plain Max picks the highest-index prior
- * non-empty block. Two levels: subgroup scan, then across subgroups.
- */
- int my_first_ac = (b < nb) ? first_ac_level(blk, mask) : 0;
- uint key = (my_first_ac == 0) ? 0u
- : (((b + 1u) << 16) | uint(my_first_ac));
+ for (uint start = 0u; start < nb; start += gl_WorkGroupSize.x) {
+ const uint b = start + lid;
+ const uint blk = coeff_base + b * APV_BLK_COEFFS;
- uint key_excl = subgroupExclusiveMax(key);
- uint key_total = subgroupMax(key);
- if (subgroupElect())
- sg_scan[gl_SubgroupID] = key_total;
- barrier();
+ int prev_dc = 0, prev_k_dc = 5;
+ uint64_t mask = uint64_t(0);
+ if (b < nb) {
+ dc_context(coeff_base, b, prev_dc, prev_k_dc);
+ mask = nz_mask(blk);
+ }
- uint carry = key_excl;
- for (uint i = 0u; i < gl_NumSubgroups; i++) {
- if (i >= gl_SubgroupID)
- break;
- carry = max(carry, sg_scan[i]);
- }
- int prev_1st_ac = (carry == 0u) ? 0 : int(carry & 0xFFFFu);
- barrier(); /* sg_scan is reused by scan #2 */
+ /*
+ * Scan #1: prev_1st_ac_level is the first-AC level of the most recent
+ * *non-empty* block before this one. Pack (lid+1, level) into a
+ * key (empty blocks -> 0) so a plain Max picks the highest-index prior
+ * non-empty block. Two levels: subgroup scan, then across subgroups.
+ */
+ int my_first_ac = (b < nb) ? first_ac_level(blk, mask) : 0;
+ uint key = (my_first_ac == 0) ? 0u
+ : (((lid + 1u) << 16) |
uint(my_first_ac));
- /*
- * Count pass: measure this block's coded length, and -- for free, since
- * we walk every emitted bit anyway -- keep a rolling copy of the last
- * bits in cnt.roll.
- */
- BlockCoder cnt;
- cnt.nbits = 0u;
- cnt.roll = 0u;
- cnt.wr = false;
- if (b < nb)
- process_block(cnt, blk, mask, prev_dc, prev_k_dc, prev_1st_ac);
+ uint key_excl = subgroupExclusiveMax(key);
+ uint key_total = subgroupMax(key);
+ if (subgroupElect())
+ sg_scan[gl_SubgroupID] = key_total;
+ barrier();
- /*
- * Scan #2: exclusive prefix sum of the bit counts gives each block's
- * start offset (in bits) within the tile-component bitstream.
- */
- uint bits_excl = subgroupExclusiveAdd(cnt.nbits);
- uint bits_total = subgroupAdd(cnt.nbits);
- if (subgroupElect())
- sg_scan[gl_SubgroupID] = bits_total;
- barrier();
+ uint carry = key_excl;
+ for (uint i = 0u; i < gl_NumSubgroups; i++) {
+ if (i >= gl_SubgroupID)
+ break;
+ carry = max(carry, sg_scan[i]);
+ }
+ int prev_1st_ac = (carry == 0u) ? int(carry_level) : int(carry &
0xFFFFu);
+ uint key_incl = max(carry, key);
+ barrier(); /* sg_scan is reused by scan #2 */
- uint off = bits_excl;
- for (uint i = 0u; i < gl_NumSubgroups; i++) {
- if (i >= gl_SubgroupID)
- break;
- off += sg_scan[i];
- }
+ /*
+ * Count pass: measure this block's coded length, and -- for free,
since
+ * we walk every emitted bit anyway -- keep a rolling copy of the last
+ * bits in cnt.roll.
+ */
+ BlockCoder cnt;
+ cnt.nbits = 0u;
+ cnt.roll = 0u;
+ cnt.wr = false;
+ if (b < nb)
+ process_block(cnt, blk, mask, prev_dc, prev_k_dc, prev_1st_ac);
- /*
- * This block's tail = its last (end_bit & 7) bits. The next block
- * prepends these so its own writer starts on a byte boundary; that makes
- * every block write a disjoint run of whole bytes -- no atomics, no
- * shared bytes. end_bit is known only now (after scan #2).
- *
- * The predecessor's tail is just the left-neighbour lane, so a subgroup
- * rotate fetches it from a register; only at a subgroup boundary (lane 0)
- * does it fall back to shared memory -- the previous subgroup's last
block.
- */
- uint end_bit = off + cnt.nbits;
- uint tail_n = end_bit & 7u;
- uint my_tail = cnt.roll & ((1u << tail_n) - 1u);
+ /*
+ * Scan #2: exclusive prefix sum of the bit counts gives each block's
+ * start offset (in bits) within the tile-component bitstream.
+ */
+ uint bits_excl = subgroupExclusiveAdd(cnt.nbits);
+ uint bits_total = subgroupAdd(cnt.nbits);
+ if (subgroupElect())
+ sg_scan[gl_SubgroupID] = bits_total;
+ barrier();
- uint prev_tail = subgroupRotate(my_tail, gl_SubgroupSize - 1u);
- if (gl_SubgroupInvocationID == gl_SubgroupSize - 1u)
- sg_lasttail[gl_SubgroupID] = my_tail;
- barrier();
- if (gl_SubgroupInvocationID == 0u && gl_SubgroupID > 0u)
- prev_tail = sg_lasttail[gl_SubgroupID - 1u];
+ uint off = carry_bits + bits_excl;
+ for (uint i = 0u; i < gl_NumSubgroups; i++) {
+ if (i >= gl_SubgroupID)
+ break;
+ off += sg_scan[i];
+ }
- /* Write pass: each block writes bytes [off>>3, end_bit>>3). Inactive
- * remainder-tile invocations write nothing. */
- if (b >= nb)
- return;
+ /*
+ * This block's tail = its last (end_bit & 7) bits. The next block
+ * prepends these so its own writer starts on a byte boundary; that
makes
+ * every block write a disjoint run of whole bytes -- no atomics, no
+ * shared bytes. end_bit is known only now (after scan #2).
+ *
+ * The predecessor's tail is just the left-neighbour lane, so a
subgroup
+ * rotate fetches it from a register; only at a subgroup boundary
(lane 0)
+ * does it fall back to shared memory -- the previous subgroup's last
block.
+ */
+ uint end_bit = off + cnt.nbits;
+ uint tail_n = end_bit & 7u;
+ uint my_tail = cnt.roll & ((1u << tail_n) - 1u);
- BlockCoder enc;
- init_put_bits(enc.pb,
- OFFBUF(u8buf, bytestream, tile_comp * slot_size + (off >>
3u)),
- uint64_t(slot_size));
- enc.nbits = 0u;
- enc.roll = 0u;
- enc.wr = true;
+ uint prev_tail = subgroupRotate(my_tail, gl_SubgroupSize - 1u);
+ if (gl_SubgroupInvocationID == gl_SubgroupSize - 1u)
+ sg_lasttail[gl_SubgroupID] = my_tail;
+ barrier();
+ if (gl_SubgroupInvocationID == 0u && gl_SubgroupID > 0u)
+ prev_tail = sg_lasttail[gl_SubgroupID - 1u];
+ if (lid == 0u)
+ prev_tail = carry_tail;
+ barrier();
- /* Prepend the predecessor's tail so this block's first byte comes out
- * complete. (off & 7 == 0 for block 0, which has no predecessor.) */
- if (b > 0u)
- put_bits(enc.pb, off & 7u, prev_tail);
+ if (lid == gl_WorkGroupSize.x - 1u) {
+ if (key_incl != 0u)
+ carry_level = key_incl & 0xFFFFu;
+ carry_bits = end_bit;
+ carry_tail = my_tail;
+ }
- process_block(enc, blk, mask, prev_dc, prev_k_dc, prev_1st_ac);
+ /* Write pass: each block writes bytes [off>>3, end_bit>>3). Inactive
+ * remainder-tile invocations write nothing. */
+ if (b >= nb)
+ return;
- if (b == nb - 1u) {
- /* Last block: no successor, so flush the trailing partial byte too
- * (the tile-component is byte-aligned, zero-padded), and record the
- * total size. */
- flush_put_bits(enc.pb);
- tile_comp_sizes[tile_comp] = (end_bit + 7u) >> 3u;
- } else {
- /* Leave the trailing partial byte for the next block to prepend. */
- flush_whole_bytes(enc.pb);
+ BlockCoder enc;
+ init_put_bits(enc.pb,
+ OFFBUF(u8buf, bytestream, tile_comp * slot_size + (off
>> 3u)),
+ uint64_t(slot_size));
+ enc.nbits = 0u;
+ enc.roll = 0u;
+ enc.wr = true;
+
+ /* Prepend the predecessor's tail so this block's first byte comes out
+ * complete. (off & 7 == 0 for block 0, which has no predecessor.) */
+ if (b > 0u)
+ put_bits(enc.pb, off & 7u, prev_tail);
+
+ process_block(enc, blk, mask, prev_dc, prev_k_dc, prev_1st_ac);
+
+ if (b == nb - 1u) {
+ /* Last block: no successor, so flush the trailing partial byte too
+ * (the tile-component is byte-aligned, zero-padded), and record
the
+ * total size. */
+ flush_put_bits(enc.pb);
+ tile_comp_sizes[tile_comp] = (end_bit + 7u) >> 3u;
+ } else {
+ /* Leave the trailing partial byte for the next block to prepend.
*/
+ flush_whole_bytes(enc.pb);
+ }
}
}
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]