// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Formats.Heif.Hevc;
namespace SixLabors.ImageSharp.Benchmarks.Codecs.Heif;
///
/// Measures complete coded-frame traversal through HEVC transform-skip and residual differential reconstruction.
///
[MemoryDiagnoser(displayGenColumns: false)]
public class HevcResidualReconstructionBenchmarks
{
///
/// The coded frame width, which is an exact multiple of the maximum transform-block side.
///
private const int Width = 1920;
///
/// The coded frame height including the final padded coding-tree row for a 1080-line presentation.
///
private const int Height = 1088;
///
/// The benchmark transform-block side in samples.
///
private const int BlockSize = 32;
///
/// The deterministic dequantized coefficients reused by each benchmark block.
///
private readonly int[] coefficients = new int[BlockSize * BlockSize];
///
/// The reusable packed residual block.
///
private readonly int[] residual = new int[BlockSize * BlockSize];
///
/// Populates a dense, deterministic twelve-bit transform-skip workload outside the measured frame traversal.
///
[GlobalSetup]
public void Setup()
{
for (int i = 0; i < this.coefficients.Length; i++)
{
this.coefficients[i] = (((i * 104729) + 4099) & 8191) - 4096;
}
}
///
/// Measures frame-wide transform-skip normalization.
///
/// The final residual, keeping the block output observable.
[Benchmark(Baseline = true)]
public int TransformSkipFrame() => this.ReconstructFrame(HevcResidualDpcmMode.None);
///
/// Measures frame-wide transform-skip normalization followed by horizontal residual differential reconstruction.
///
/// The final residual, keeping the block output observable.
[Benchmark]
public int HorizontalResidualDpcmFrame() => this.ReconstructFrame(HevcResidualDpcmMode.Horizontal);
///
/// Measures frame-wide transform-skip normalization followed by vertical residual differential reconstruction.
///
/// The final residual, keeping the block output observable.
[Benchmark]
public int VerticalResidualDpcmFrame() => this.ReconstructFrame(HevcResidualDpcmMode.Vertical);
///
/// Reconstructs every maximum-size transform block in the coded benchmark frame.
///
/// The residual differential mode applied after transform-skip normalization.
/// The final reconstructed residual.
private int ReconstructFrame(HevcResidualDpcmMode mode)
{
for (int y = 0; y < Height; y += BlockSize)
{
for (int x = 0; x < Width; x += BlockSize)
{
HevcResidualReconstructor.ApplyTransformSkip(this.coefficients, this.residual, BlockSize, BlockSize, 12, 18, 5, true, false);
HevcResidualReconstructor.ApplyResidualDpcm(this.residual, BlockSize, BlockSize, mode);
}
}
return this.residual[^1];
}
}