// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats.Heif.Hevc;
namespace SixLabors.ImageSharp.Tests.Formats.Heif.Hevc;
///
/// Verifies HEVC arithmetic-decoder suspension and restart around pulse-code-modulated coding units.
///
[Trait("Format", "Heic")]
public class HevcCabacDecoderTests
{
///
/// Verifies that PCM samples begin after the terminating arithmetic bytes and that arithmetic decoding resumes after the raw payload.
///
[Fact]
public void PcmPayloadSuspendsAndRestartsArithmeticDecoding()
{
ReadOnlySpan data = [0xFF, 0xFF, 0xAB, 0xFF, 0xFF];
HevcCabacDecoder decoder = new(data);
Assert.True(decoder.ReadPcmFlag());
Assert.Equal((ushort)0xA, decoder.ReadPcmSample(4));
Assert.Equal((ushort)0xB, decoder.ReadPcmSample(4));
decoder.RestartAfterPcm();
Assert.True(decoder.ReadTerminate());
}
///
/// Verifies that a PCM sample cannot read beyond its bounded entropy substream.
///
[Fact]
public void PcmPayloadRejectsTruncatedSample()
{
Assert.Throws(ReadTruncatedPcmSample);
}
///
/// Attempts to read a sample wider than the remaining raw PCM payload.
///
private static void ReadTruncatedPcmSample()
{
ReadOnlySpan data = [0xFF, 0xFF, 0x80];
HevcCabacDecoder decoder = new(data);
Assert.True(decoder.ReadPcmFlag());
decoder.ReadPcmSample(16);
}
}