From 8dfa0bd878e06e1571ddc24763ee73aee06a9570 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 2 Sep 2026 16:02:24 +1000 Subject: [PATCH] Persist AV1 derived transform sizes --- HEIF_IMPLEMENTATION_PLAN.md | 2 +- .../Formats/Heif/Av1/Tiling/Av1TileWriter.cs | 6 +++--- .../Heif/Av1/Av1CoefficientsEntropyTests.cs | 16 ++++++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/HEIF_IMPLEMENTATION_PLAN.md b/HEIF_IMPLEMENTATION_PLAN.md index 9f10ba394f..7f03b54e33 100644 --- a/HEIF_IMPLEMENTATION_PLAN.md +++ b/HEIF_IMPLEMENTATION_PLAN.md @@ -831,7 +831,7 @@ Encoder verification contract: - [~] Encoder mode information now uses a frame-owned integer alias grid over a packed 8-byte value allocation, matching current libaom's `mi_grid_base` and `mi_alloc` relationship without a managed object or reference per 4x4 entry. Every coded 4x4 cell covered by square, rectangular, or clipped edge blocks maps to its owning allocation entry before context-dependent symbols are written. At 4K, mode values occupy about 4.0 MiB and the alias grid about 2.0 MiB. Packed syntax, relative neighbor lookup, full block mapping, writer traversal, entropy, and OBU coverage pass 1,947 of 1,947 direct net11 VSTest cases in Release; complete mode decision still remains. - [~] The final-block decision workspace uses one reusable 10.3 KiB ImageSharp allocator owner. It contains 1,024 explicitly packed 10-byte final-block entries and the 341 preorder partition bytes required by a complete 128x128-through-8x8 quadtree, replacing separate managed arrays. Construction and the explicit per-superblock reset initialize every syntax field, including the nonzero sentinel that disables filter-intra prediction; pooled palette, quantizer, prediction, and partition bytes cannot leak into the next decision pass. Exact allocation, size, initialization, reset, return, repeated-run, writer, entropy, and OBU coverage pass 1,957 of 1,957 direct net11 VSTest cases in Release; complete mode decision still remains. - [~] Finalized transform coefficients and packed EOB/type state now use raster-ordered, per-superblock plane segments matching current libaom's coefficient-pool geometry. One ImageSharp allocator owner replaces libaom's separate coefficient, EOB, and entropy-context allocations while preserving the full 1024 luma and 256-per-chroma 4x4 state capacity of a 128x128 4:2:0 superblock. The transform-block boundary can populate the owner's quantized coefficient and state slices directly; mode-decision traversal still needs to select and invoke it. -- [~] Tile partition writing now follows current libaom's recursive `write_modes_sb` preorder traversal and `update_ext_partition_context` edge updates directly. Bottom-edge blocks use the horizontal-alike partition CDF and right-edge blocks use the vertical-alike CDF; byte-exact regressions cover both paths after the previous calls were found reversed. Lossless chroma-from-luma availability now uses the subsampled plane block size shared with the decoder instead of the lossy 32x32 limit, preserving the correct UV-mode alphabet for each segment. The obsolete SVT-derived global geometry catalog and its unimplemented lookup are removed; transform geometry is derived in libaom's bounded 64x64 residual order, fixed intra transform-size symbols use the reference depth and neighbor contexts, frame-edge and segmentation syntax use mode-information units, and 128x128 CDEF units use libaom's 0-to-3 indexing and first-block strength ownership. Writer, entropy, and OBU coverage passes 1,957 of 1,957 direct net11 VSTest cases in Release, with 20 of 20 focused encoder and decoder chroma-from-luma cases. Partition and mode analysis still need to populate these retained decisions; variable inter-transform syntax remains part of later inter-frame support. +- [~] Tile partition writing now follows current libaom's recursive `write_modes_sb` preorder traversal and `update_ext_partition_context` edge updates directly. Bottom-edge blocks use the horizontal-alike partition CDF and right-edge blocks use the vertical-alike CDF; byte-exact regressions cover both paths after the previous calls were found reversed. Lossless chroma-from-luma availability now uses the subsampled plane block size shared with the decoder instead of the lossy 32x32 limit, preserving the correct UV-mode alphabet for each segment. The obsolete SVT-derived global geometry catalog and its unimplemented lookup are removed; transform geometry is derived in libaom's bounded 64x64 residual order, fixed intra transform-size symbols use the reference depth and neighbor contexts, and each derived transform size is persisted to the frame-owned mode information before the entropy snapshot and coefficient traversal consume it. Frame-edge and segmentation syntax use mode-information units, and 128x128 CDEF units use libaom's 0-to-3 indexing and first-block strength ownership. The focused transform-state regression passes 3 of 3 direct net11 VSTest cases in Release. Writer, entropy, and OBU coverage passes 1,957 of 1,957 direct net11 VSTest cases in Release, with 20 of 20 focused encoder and decoder chroma-from-luma cases. Partition and mode analysis still need to populate these retained decisions; variable inter-transform syntax remains part of later inter-frame support. - [ ] Implement legal deblocking, CDEF, restoration, super-resolution, and film-grain signaling decisions. - [~] The coefficient symbol encoder now reuses tile-lifetime level and context workspaces instead of allocating per transform, defers both coefficient rents until the first nonzero transform block, and disposes all tile scratch independently from the detached encoded bytes. Its range coder matches current libaom's 64-bit coding window, bulk big-endian byte flush, and backward carry propagation while using one byte of allocator scratch per estimated output byte instead of the former 16-bit pre-carry storage. The reference-type symbol encoder is passed normally through tile traversal, and the operation boundary owns the allocator-backed item payload stream for exactly one synchronous encode. Every remaining encoder fragment must be audited before it becomes active. - [~] The planar conversion, residual construction, forward transform, and forward quantizer use descending SIMD dispatch: Vector512, Vector256, Vector128, then scalar. Residual construction matches current libaom's exact source-minus-prediction arithmetic for 8-bit and high-bit-depth planes, preserves independent row strides and unaligned starts, and writes directly into caller-owned signed-short storage without allocation. Apply the same rule to every later hot-path family. diff --git a/src/ImageSharp/Formats/Heif/Av1/Tiling/Av1TileWriter.cs b/src/ImageSharp/Formats/Heif/Av1/Tiling/Av1TileWriter.cs index 5e8e73653e..0d5fe6b827 100644 --- a/src/ImageSharp/Formats/Heif/Av1/Tiling/Av1TileWriter.cs +++ b/src/ImageSharp/Formats/Heif/Av1/Tiling/Av1TileWriter.cs @@ -637,7 +637,6 @@ internal partial class Av1TileWriter Av1BlockSize blockSize = macroBlockModeInfo.Block.BlockSize; bool skipWritingCoefficients = macroBlockModeInfo.Block.Skip; pcs.MapModeInfoBlock(modeInfoPosition, blockSize); - entropyCodingContext.MacroBlockModeInfo = macroBlockModeInfo; Av1MacroBlockD macroBlock = entropyCodingContext.MacroBlock; Guard.MustBeLessThan((int)blockSize, (int)Av1BlockSize.AllSizes, nameof(blockSize)); @@ -753,12 +752,13 @@ internal partial class Av1TileWriter WriteTransformSize( pcs, writer, - macroBlockModeInfo, + ref macroBlockModeInfo, macroBlock, blockSize, blockOrigin, tile_idx); + entropyCodingContext.MacroBlockModeInfo = macroBlockModeInfo; if (!skipWritingCoefficients) { EncodeCoefficients1d( @@ -794,7 +794,7 @@ internal partial class Av1TileWriter internal static void WriteTransformSize( Av1PictureControlSet pcs, Av1SymbolEncoder writer, - Av1MacroBlockModeInfo macroBlockModeInfo, + ref Av1MacroBlockModeInfo macroBlockModeInfo, Av1MacroBlockD macroBlock, Av1BlockSize blockSize, Point blockOrigin, diff --git a/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1CoefficientsEntropyTests.cs b/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1CoefficientsEntropyTests.cs index 644c58ae1c..b144c852af 100644 --- a/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1CoefficientsEntropyTests.cs +++ b/tests/ImageSharp.Tests/Formats/Heif/Av1/Av1CoefficientsEntropyTests.cs @@ -458,7 +458,7 @@ public class Av1CoefficientsEntropyTests Av1TileWriter.WriteTransformSize( picture, writer, - modeInfo, + ref modeInfo, macroBlock, modeInfo.Block.BlockSize, blockOrigin, @@ -680,11 +680,11 @@ public class Av1CoefficientsEntropyTests picture.CbDcSignLevelCoefficientNeighbors = [blue]; picture.TransformFunctionContexts = [transforms]; Av1TileInfo tile = new(0, 0, picture.Parent.FrameHeader); - Point[] blockPositions = [new(16, 0), new(24, 0), new(16, 8), new(24, 8)]; + Point[] modeInfoPositions = [new(16, 0), new(24, 0), new(16, 8), new(24, 8)]; using Av1EncoderSuperblockWorkspace workspace = new(Configuration.Default); - for (int index = 0; index < blockPositions.Length; index++) + for (int index = 0; index < modeInfoPositions.Length; index++) { - Point position = blockPositions[index]; + Point position = modeInfoPositions[index]; ref Av1EncoderBlockModeInfo blockMode = ref picture.ModeInfoAllocation.Span[ (position.Y * picture.ModeInfoStride) + position.X].Block; @@ -733,6 +733,14 @@ public class Av1CoefficientsEntropyTests coefficients, tileIndex: 0); + Assert.Equal(Av1TransformSize.Size32x32, context.MacroBlockModeInfo.Block.TransformSize); + foreach (Point position in modeInfoPositions) + { + Assert.Equal( + Av1TransformSize.Size32x32, + picture.GetMacroBlockModeInfo(position).Block.TransformSize); + } + writer.Dispose(); Assert.Equal(4096, context.CodedAreaSuperblock);