feat(video): add bitstream readers for Annex-B, H.264/HEVC SPS and AV1 OBUs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
8 files changed
+1630
No files matched your search
@@ -0,0 +1,229 @@
|
||||
using Titano.Core.Video.Bitstream;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Video;
|
||||
|
||||
public sealed class BitstreamTests
|
||||
{
|
||||
[Test]
|
||||
public void ExpGolombRoundTrip()
|
||||
{
|
||||
uint[] unsigned = [0, 1, 2, 3, 7, 8, 254, 255, 256, 65535, 1_000_000, uint.MaxValue - 1];
|
||||
int[] signed = [0, 1, -1, 2, -2, 100, -100, int.MaxValue, -int.MaxValue];
|
||||
var w = new BitWriter();
|
||||
foreach (var v in unsigned) w.WriteUe(v);
|
||||
foreach (var v in signed) w.WriteSe(v);
|
||||
w.WriteBits(0b101, 3);
|
||||
var r = new BitReader(w.ToArray());
|
||||
foreach (var v in unsigned) Assert.Equal(v, r.ReadUe());
|
||||
foreach (var v in signed) Assert.Equal(v, r.ReadSe());
|
||||
Assert.Equal(0b101u, r.ReadBits(3));
|
||||
Assert.False(r.IsOverrun);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadingPastTheEndSetsOverrunWithoutThrowing()
|
||||
{
|
||||
var r = new BitReader([0xFF]);
|
||||
Assert.Equal(0xFFu, r.ReadBits(8));
|
||||
Assert.Equal(0u, r.ReadBits(16));
|
||||
Assert.True(r.IsOverrun);
|
||||
var zeros = new BitReader(new byte[8]);
|
||||
Assert.Equal(0u, zeros.ReadUe());
|
||||
Assert.True(zeros.IsOverrun);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Leb128RoundTrip()
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[8];
|
||||
foreach (var v in new ulong[] { 0, 1, 127, 128, 16383, 16384, uint.MaxValue })
|
||||
{
|
||||
var n = Av1ObuParser.WriteLeb128(buffer, v);
|
||||
Assert.Equal(Av1ObuParser.Leb128Size(v), n);
|
||||
Assert.True(Av1ObuParser.TryReadLeb128(buffer[..n], out var back, out var length));
|
||||
Assert.Equal(v, back);
|
||||
Assert.Equal(n, length);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SplitsThreeAndFourByteStartCodes()
|
||||
{
|
||||
byte[] stream = [0xAA, 0, 0, 0, 1, 0x67, 1, 2, 0, 0, 1, 0x68, 3, 0, 0, 0, 0, 1, 0x65, 4, 5, 6, 0, 0];
|
||||
var units = AnnexBParser.Split(stream);
|
||||
Assert.Count(3, units);
|
||||
Assert.BytesEqual([0x67, 1, 2], units[0].Slice(stream));
|
||||
Assert.BytesEqual([0x68, 3], units[1].Slice(stream));
|
||||
Assert.BytesEqual([0x65, 4, 5, 6], units[2].Slice(stream));
|
||||
Assert.Equal(H264NalUnitType.Sps, AnnexBParser.H264Type(stream[units[0].Offset]));
|
||||
Assert.Equal(H264NalUnitType.IdrSlice, AnnexBParser.H264Type(stream[units[2].Offset]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertsAnnexBToLengthPrefixedAndDetectsBoth()
|
||||
{
|
||||
var annexB = AnnexBParser.ToAnnexB([SyntheticStreams.H264Sps(), SyntheticStreams.H264Pps(), SyntheticStreams.H264Slice(true, 300, 1)]);
|
||||
var prefixed = AnnexBParser.ToLengthPrefixed(annexB);
|
||||
var fromAnnexB = AnnexBParser.SplitAccessUnit(annexB, out var wasAnnexB);
|
||||
var fromPrefixed = AnnexBParser.SplitAccessUnit(prefixed, out var wasAnnexB2);
|
||||
Assert.True(wasAnnexB);
|
||||
Assert.False(wasAnnexB2);
|
||||
Assert.Equal(3, fromAnnexB.Count);
|
||||
Assert.Equal(3, fromPrefixed.Count);
|
||||
for (var i = 0; i < 3; i++)
|
||||
Assert.BytesEqual(fromAnnexB[i].Slice(annexB), fromPrefixed[i].Slice(prefixed));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LengthPrefixedSplitRejectsInconsistentLengths()
|
||||
{
|
||||
Assert.False(AnnexBParser.TrySplitLengthPrefixed([0, 0, 0, 9, 1, 2], 4, out _));
|
||||
Assert.False(AnnexBParser.TrySplitLengthPrefixed([0, 0, 0, 0], 4, out _));
|
||||
Assert.True(AnnexBParser.TrySplitLengthPrefixed([0, 2, 0x41, 9, 0, 1, 0x41], 2, out var units));
|
||||
Assert.Count(2, units);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmulationPreventionRoundTrip()
|
||||
{
|
||||
var rng = new Random(7);
|
||||
for (var trial = 0; trial < 200; trial++)
|
||||
{
|
||||
var data = new byte[rng.Next(1, 200)];
|
||||
for (var i = 0; i < data.Length; i++) data[i] = rng.Next(4) == 0 ? (byte)rng.Next(4) : (byte)0;
|
||||
var escaped = AnnexBParser.AddEmulationPrevention(data);
|
||||
for (var i = 2; i < escaped.Length; i++)
|
||||
Assert.False(escaped[i - 2] == 0 && escaped[i - 1] == 0 && escaped[i] <= 2, "start code prefix survived escaping");
|
||||
var back = AnnexBParser.RemoveEmulationPrevention(escaped);
|
||||
// After a lone trailing 0x00 the appended 0x03 is not preceded by two zeros, so it stays.
|
||||
if (back.Length == data.Length + 1) back = back[..^1];
|
||||
Assert.BytesEqual(data, back);
|
||||
}
|
||||
Assert.BytesEqual([0, 0, 3, 1, 0, 0, 3, 0], AnnexBParser.AddEmulationPrevention([0, 0, 1, 0, 0, 0]).AsSpan(0, 8));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParsesH264HighProfileSps()
|
||||
{
|
||||
var nal = SyntheticStreams.H264Sps(1920, 1080, 100, 41);
|
||||
Assert.True(H264SpsParser.TryParse(nal, out var sps));
|
||||
Assert.Equal(100, sps.ProfileIdc);
|
||||
Assert.Equal(41, sps.LevelIdc);
|
||||
Assert.Equal(1, sps.ChromaFormatIdc);
|
||||
Assert.Equal(8, sps.BitDepthLuma);
|
||||
Assert.Equal(1920, sps.Width);
|
||||
Assert.Equal(1080, sps.Height);
|
||||
Assert.Equal(1088, sps.CodedHeight);
|
||||
Assert.Equal(1, sps.ColourPrimaries);
|
||||
Assert.Equal(false, sps.FullRange);
|
||||
Assert.Approximately(29.97, sps.FrameRate!.Value, 0.01);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case(1280, 720, 77, 31)]
|
||||
[Case(3840, 2160, 100, 51)]
|
||||
[Case(642, 482, 66, 30)]
|
||||
public void ParsesH264SizesWithCropping(int width, int height, int profile, int level)
|
||||
{
|
||||
Assert.True(H264SpsParser.TryParse(SyntheticStreams.H264Sps(width, height, profile, level), out var sps));
|
||||
Assert.Equal(width, sps.Width);
|
||||
Assert.Equal(height, sps.Height);
|
||||
Assert.Equal(profile, sps.ProfileIdc);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParsesHevcMain10Sps()
|
||||
{
|
||||
Assert.True(HevcSpsParser.TryParse(SyntheticStreams.HevcSps(1920, 1080, 10, 153), out var sps));
|
||||
Assert.Equal(1920, sps.Width);
|
||||
Assert.Equal(1080, sps.Height);
|
||||
Assert.Equal(1080, sps.CodedHeight);
|
||||
Assert.Equal(10, sps.BitDepthLuma);
|
||||
Assert.Equal(10, sps.BitDepthChroma);
|
||||
Assert.Equal(1, sps.ChromaFormatIdc);
|
||||
Assert.Equal(2, sps.ProfileTierLevel.ProfileIdc);
|
||||
Assert.Equal(153, sps.ProfileTierLevel.LevelIdc);
|
||||
Assert.Equal(0x2000_0000u, sps.ProfileTierLevel.ProfileCompatibilityFlags);
|
||||
Assert.Equal(0x9000_0000_0000UL, sps.ProfileTierLevel.ConstraintIndicatorFlags);
|
||||
Assert.False(sps.ProfileTierLevel.HighTier);
|
||||
Assert.True(sps.TemporalIdNesting);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AppliesHevcConformanceWindow()
|
||||
{
|
||||
Assert.True(HevcSpsParser.TryParse(SyntheticStreams.HevcSps(1916, 1076, 8, 120), out var sps));
|
||||
Assert.Equal(1920, sps.CodedWidth);
|
||||
Assert.Equal(1080, sps.CodedHeight);
|
||||
Assert.Equal(1916, sps.Width);
|
||||
Assert.Equal(1076, sps.Height);
|
||||
Assert.Equal(1, sps.ProfileTierLevel.ProfileIdc);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParsesAv1SequenceHeader()
|
||||
{
|
||||
var obuBytes = SyntheticStreams.Av1SequenceHeaderObu(3840, 2160, 12, tenBit: true);
|
||||
Assert.True(Av1ObuParser.TryReadObu(obuBytes, 0, out var obu));
|
||||
Assert.Equal(Av1ObuType.SequenceHeader, obu.Type);
|
||||
Assert.True(obu.HasSizeField);
|
||||
Assert.Equal(obuBytes.Length, obu.TotalLength);
|
||||
Assert.True(Av1ObuParser.TryParseSequenceHeader(obu.Payload(obuBytes), out var header));
|
||||
Assert.Equal(3840, header.MaxFrameWidth);
|
||||
Assert.Equal(2160, header.MaxFrameHeight);
|
||||
Assert.Equal(12, header.SeqLevelIdx);
|
||||
Assert.Equal(10, header.BitDepth);
|
||||
Assert.True(header.ChromaSubsamplingX && header.ChromaSubsamplingY);
|
||||
Assert.Equal(1, header.ColorPrimaries);
|
||||
Assert.False(header.FullRange);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadsObuWithoutSizeFieldAndSplitsTemporalUnits()
|
||||
{
|
||||
byte[] lastWithoutSize = [(byte)Av1ObuType.Frame << 3, 9, 8, 7];
|
||||
Assert.True(Av1ObuParser.TryReadObu(lastWithoutSize, 0, out var obu));
|
||||
Assert.False(obu.HasSizeField);
|
||||
Assert.Equal(3, obu.PayloadLength);
|
||||
|
||||
var stream = SyntheticStreams.Concat(
|
||||
SyntheticStreams.Av1TemporalDelimiter(), SyntheticStreams.Av1SequenceHeaderObu(), SyntheticStreams.Av1Frame(50, 1),
|
||||
SyntheticStreams.Av1TemporalDelimiter(), SyntheticStreams.Av1Frame(40, 2),
|
||||
SyntheticStreams.Av1TemporalDelimiter(), SyntheticStreams.Av1Frame(30, 3));
|
||||
var units = Av1ObuParser.SplitTemporalUnits(stream);
|
||||
Assert.Count(3, units);
|
||||
Assert.Equal(0, units[0].Offset);
|
||||
Assert.Equal(stream.Length, units[2].Offset + units[2].Length);
|
||||
Assert.Equal(units[0].Length, units[1].Offset);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParsersSurviveTruncationAndGarbage()
|
||||
{
|
||||
byte[][] inputs = [SyntheticStreams.H264Sps(), SyntheticStreams.HevcSps(), SyntheticStreams.Av1SequenceHeaderObu()];
|
||||
var rng = new Random(3);
|
||||
foreach (var input in inputs)
|
||||
{
|
||||
for (var length = 0; length < input.Length; length++)
|
||||
{
|
||||
var slice = input.AsSpan(0, length);
|
||||
H264SpsParser.TryParse(slice, out _);
|
||||
HevcSpsParser.TryParse(slice, out _);
|
||||
Av1ObuParser.Split(slice);
|
||||
if (length > 2) Av1ObuParser.TryParseSequenceHeader(slice[2..], out _);
|
||||
}
|
||||
for (var trial = 0; trial < 300; trial++)
|
||||
{
|
||||
var corrupt = (byte[])input.Clone();
|
||||
corrupt[rng.Next(corrupt.Length)] ^= (byte)(1 << rng.Next(8));
|
||||
H264SpsParser.TryParse(corrupt, out _);
|
||||
HevcSpsParser.TryParse(corrupt, out _);
|
||||
Av1ObuParser.SplitTemporalUnits(corrupt);
|
||||
Av1ObuParser.TryParseSequenceHeader(corrupt, out _);
|
||||
AnnexBParser.SplitAccessUnit(corrupt, out _);
|
||||
}
|
||||
}
|
||||
Assert.False(H264SpsParser.TryParse(SyntheticStreams.H264Sps().AsSpan(0, 5), out _));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
using Titano.Core.Video.Bitstream;
|
||||
|
||||
namespace Titano.Core.Tests.Video;
|
||||
|
||||
/// <summary>Builds syntactically valid H.264/HEVC parameter sets, AV1 sequence headers and slice stubs.</summary>
|
||||
internal static class SyntheticStreams
|
||||
{
|
||||
/// <summary>H.264 SPS NAL (header included) for 4:2:0 8-bit with cropping to the requested size and VUI timing.</summary>
|
||||
public static byte[] H264Sps(int width = 1920, int height = 1080, int profileIdc = 100, int levelIdc = 41, uint timeScale = 60000, uint unitsInTick = 1001)
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteBits((ulong)profileIdc, 8);
|
||||
w.WriteBits(0, 8); // constraint flags
|
||||
w.WriteBits((ulong)levelIdc, 8);
|
||||
w.WriteUe(0); // seq_parameter_set_id
|
||||
if (profileIdc == 100)
|
||||
{
|
||||
w.WriteUe(1); // chroma_format_idc
|
||||
w.WriteUe(0); // bit_depth_luma_minus8
|
||||
w.WriteUe(0); // bit_depth_chroma_minus8
|
||||
w.WriteBit(false); // qpprime_y_zero_transform_bypass_flag
|
||||
w.WriteBit(false); // seq_scaling_matrix_present_flag
|
||||
}
|
||||
w.WriteUe(0); // log2_max_frame_num_minus4
|
||||
w.WriteUe(0); // pic_order_cnt_type
|
||||
w.WriteUe(2); // log2_max_pic_order_cnt_lsb_minus4
|
||||
w.WriteUe(4); // max_num_ref_frames
|
||||
w.WriteBit(false); // gaps_in_frame_num_value_allowed_flag
|
||||
var mbW = (width + 15) / 16;
|
||||
var mbH = (height + 15) / 16;
|
||||
w.WriteUe((uint)mbW - 1);
|
||||
w.WriteUe((uint)mbH - 1);
|
||||
w.WriteBit(true); // frame_mbs_only_flag
|
||||
w.WriteBit(true); // direct_8x8_inference_flag
|
||||
var cropRight = (mbW * 16 - width) / 2;
|
||||
var cropBottom = (mbH * 16 - height) / 2;
|
||||
var crop = cropRight > 0 || cropBottom > 0;
|
||||
w.WriteBit(crop);
|
||||
if (crop)
|
||||
{
|
||||
w.WriteUe(0);
|
||||
w.WriteUe((uint)cropRight);
|
||||
w.WriteUe(0);
|
||||
w.WriteUe((uint)cropBottom);
|
||||
}
|
||||
w.WriteBit(true); // vui_parameters_present_flag
|
||||
w.WriteBit(true); // aspect_ratio_info_present_flag
|
||||
w.WriteBits(1, 8); // aspect_ratio_idc 1:1
|
||||
w.WriteBit(false); // overscan_info_present_flag
|
||||
w.WriteBit(true); // video_signal_type_present_flag
|
||||
w.WriteBits(5, 3); // video_format
|
||||
w.WriteBit(false); // video_full_range_flag
|
||||
w.WriteBit(true); // colour_description_present_flag
|
||||
w.WriteBits(1, 8);
|
||||
w.WriteBits(1, 8);
|
||||
w.WriteBits(1, 8);
|
||||
w.WriteBit(false); // chroma_loc_info_present_flag
|
||||
w.WriteBit(true); // timing_info_present_flag
|
||||
w.WriteBits(unitsInTick, 32);
|
||||
w.WriteBits(timeScale, 32);
|
||||
w.WriteBit(true); // fixed_frame_rate_flag
|
||||
w.WriteBit(false); // nal_hrd_parameters_present_flag
|
||||
w.WriteBit(false); // vcl_hrd_parameters_present_flag
|
||||
w.WriteBit(false); // pic_struct_present_flag
|
||||
w.WriteBit(false); // bitstream_restriction_flag
|
||||
w.WriteTrailingBits();
|
||||
return Nal([0x67], w.ToArray());
|
||||
}
|
||||
|
||||
public static byte[] H264Pps()
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteUe(0); // pic_parameter_set_id
|
||||
w.WriteUe(0); // seq_parameter_set_id
|
||||
w.WriteBit(true); // entropy_coding_mode_flag
|
||||
w.WriteBit(false); // bottom_field_pic_order_in_frame_present_flag
|
||||
w.WriteUe(0); // num_slice_groups_minus1
|
||||
w.WriteUe(0); // num_ref_idx_l0_default_active_minus1
|
||||
w.WriteUe(0); // num_ref_idx_l1_default_active_minus1
|
||||
w.WriteBit(false); // weighted_pred_flag
|
||||
w.WriteBits(0, 2); // weighted_bipred_idc
|
||||
w.WriteSe(0); // pic_init_qp_minus26
|
||||
w.WriteSe(0); // pic_init_qs_minus26
|
||||
w.WriteSe(0); // chroma_qp_index_offset
|
||||
w.WriteBit(true); // deblocking_filter_control_present_flag
|
||||
w.WriteBit(false); // constrained_intra_pred_flag
|
||||
w.WriteBit(false); // redundant_pic_cnt_present_flag
|
||||
w.WriteBit(true); // transform_8x8_mode_flag
|
||||
w.WriteBit(false); // pic_scaling_matrix_present_flag
|
||||
w.WriteSe(0); // second_chroma_qp_index_offset
|
||||
w.WriteTrailingBits();
|
||||
return Nal([0x68], w.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>A slice NAL with a plausible header and pseudo-random payload.</summary>
|
||||
public static byte[] H264Slice(bool idr, int payloadBytes, int seed)
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteUe(0); // first_mb_in_slice
|
||||
w.WriteUe(idr ? 7u : 5u); // slice_type I / P
|
||||
w.WriteUe(0); // pic_parameter_set_id
|
||||
w.WriteBytes(Random(payloadBytes, seed));
|
||||
w.WriteTrailingBits();
|
||||
return Nal([idr ? (byte)0x65 : (byte)0x41], w.ToArray());
|
||||
}
|
||||
|
||||
public static byte[] H264AccessUnitDelimiter() => [0x09, 0xF0];
|
||||
|
||||
public static byte[] HevcVps()
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteBits(0, 4); // vps_video_parameter_set_id
|
||||
w.WriteBit(true); // vps_base_layer_internal_flag
|
||||
w.WriteBit(true); // vps_base_layer_available_flag
|
||||
w.WriteBits(0, 6); // vps_max_layers_minus1
|
||||
w.WriteBits(0, 3); // vps_max_sub_layers_minus1
|
||||
w.WriteBit(true); // vps_temporal_id_nesting_flag
|
||||
w.WriteBits(0xFFFF, 16);
|
||||
WriteHevcProfileTierLevel(w, 2, 153);
|
||||
w.WriteBit(true); // vps_sub_layer_ordering_info_present_flag
|
||||
w.WriteUe(4);
|
||||
w.WriteUe(0);
|
||||
w.WriteUe(0);
|
||||
w.WriteBits(0, 6); // vps_max_layer_id
|
||||
w.WriteUe(0); // vps_num_layer_sets_minus1
|
||||
w.WriteBit(false); // vps_timing_info_present_flag
|
||||
w.WriteBit(false); // vps_extension_flag
|
||||
w.WriteTrailingBits();
|
||||
return Nal([0x40, 0x01], w.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>HEVC SPS for 4:2:0 with the given bit depth, coded in 8×8 units with a conformance window.</summary>
|
||||
public static byte[] HevcSps(int width = 1920, int height = 1080, int bitDepth = 10, int levelIdc = 153)
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteBits(0, 4); // sps_video_parameter_set_id
|
||||
w.WriteBits(0, 3); // sps_max_sub_layers_minus1
|
||||
w.WriteBit(true); // sps_temporal_id_nesting_flag
|
||||
WriteHevcProfileTierLevel(w, bitDepth == 10 ? 2 : 1, levelIdc);
|
||||
w.WriteUe(0); // sps_seq_parameter_set_id
|
||||
w.WriteUe(1); // chroma_format_idc
|
||||
var codedW = (width + 7) / 8 * 8;
|
||||
var codedH = (height + 7) / 8 * 8;
|
||||
w.WriteUe((uint)codedW);
|
||||
w.WriteUe((uint)codedH);
|
||||
var window = codedW != width || codedH != height;
|
||||
w.WriteBit(window);
|
||||
if (window)
|
||||
{
|
||||
w.WriteUe(0);
|
||||
w.WriteUe((uint)(codedW - width) / 2);
|
||||
w.WriteUe(0);
|
||||
w.WriteUe((uint)(codedH - height) / 2);
|
||||
}
|
||||
w.WriteUe((uint)bitDepth - 8);
|
||||
w.WriteUe((uint)bitDepth - 8);
|
||||
w.WriteUe(4); // log2_max_pic_order_cnt_lsb_minus4
|
||||
w.WriteBit(true); // sps_sub_layer_ordering_info_present_flag
|
||||
w.WriteUe(4);
|
||||
w.WriteUe(0);
|
||||
w.WriteUe(0);
|
||||
w.WriteUe(0); // log2_min_luma_coding_block_size_minus3
|
||||
w.WriteUe(3); // log2_diff_max_min_luma_coding_block_size
|
||||
w.WriteUe(0); // log2_min_luma_transform_block_size_minus2
|
||||
w.WriteUe(3); // log2_diff_max_min_luma_transform_block_size
|
||||
w.WriteUe(1); // max_transform_hierarchy_depth_inter
|
||||
w.WriteUe(1); // max_transform_hierarchy_depth_intra
|
||||
w.WriteBit(false); // scaling_list_enabled_flag
|
||||
w.WriteBit(true); // amp_enabled_flag
|
||||
w.WriteBit(true); // sample_adaptive_offset_enabled_flag
|
||||
w.WriteBit(false); // pcm_enabled_flag
|
||||
w.WriteUe(0); // num_short_term_ref_pic_sets
|
||||
w.WriteBit(false); // long_term_ref_pics_present_flag
|
||||
w.WriteBit(true); // sps_temporal_mvp_enabled_flag
|
||||
w.WriteBit(true); // strong_intra_smoothing_enabled_flag
|
||||
w.WriteBit(false); // vui_parameters_present_flag
|
||||
w.WriteBit(false); // sps_extension_present_flag
|
||||
w.WriteTrailingBits();
|
||||
return Nal([0x42, 0x01], w.ToArray());
|
||||
}
|
||||
|
||||
public static byte[] HevcPps()
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteUe(0); // pps_pic_parameter_set_id
|
||||
w.WriteUe(0); // pps_seq_parameter_set_id
|
||||
w.WriteBit(false); // dependent_slice_segments_enabled_flag
|
||||
w.WriteBit(false); // output_flag_present_flag
|
||||
w.WriteBits(0, 3); // num_extra_slice_header_bits
|
||||
w.WriteBit(false); // sign_data_hiding_enabled_flag
|
||||
w.WriteBit(true); // cabac_init_present_flag
|
||||
w.WriteUe(0);
|
||||
w.WriteUe(0);
|
||||
w.WriteSe(0); // init_qp_minus26
|
||||
w.WriteBit(false); // constrained_intra_pred_flag
|
||||
w.WriteBit(false); // transform_skip_enabled_flag
|
||||
w.WriteBit(false); // cu_qp_delta_enabled_flag
|
||||
w.WriteSe(0);
|
||||
w.WriteSe(0);
|
||||
w.WriteBit(false); // pps_slice_chroma_qp_offsets_present_flag
|
||||
w.WriteBit(false); // weighted_pred_flag
|
||||
w.WriteBit(false); // weighted_bipred_flag
|
||||
w.WriteBit(false); // transquant_bypass_enabled_flag
|
||||
w.WriteBit(false); // tiles_enabled_flag
|
||||
w.WriteBit(false); // entropy_coding_sync_enabled_flag
|
||||
w.WriteBit(true); // pps_loop_filter_across_slices_enabled_flag
|
||||
w.WriteBit(false); // deblocking_filter_control_present_flag
|
||||
w.WriteBit(false); // pps_scaling_list_data_present_flag
|
||||
w.WriteBit(false); // lists_modification_present_flag
|
||||
w.WriteUe(0); // log2_parallel_merge_level_minus2
|
||||
w.WriteBit(false); // slice_segment_header_extension_present_flag
|
||||
w.WriteBit(false); // pps_extension_present_flag
|
||||
w.WriteTrailingBits();
|
||||
return Nal([0x44, 0x01], w.ToArray());
|
||||
}
|
||||
|
||||
public static byte[] HevcSlice(bool idr, int payloadBytes, int seed)
|
||||
{
|
||||
var type = idr ? HevcNalUnitType.IdrWRadl : HevcNalUnitType.TrailR;
|
||||
var w = new BitWriter();
|
||||
w.WriteBit(true); // first_slice_segment_in_pic_flag
|
||||
w.WriteBytes(Random(payloadBytes, seed));
|
||||
w.WriteTrailingBits();
|
||||
return Nal([(byte)((byte)type << 1), 0x01], w.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>AV1 sequence header OBU (with size field) for profile 0 4:2:0.</summary>
|
||||
public static byte[] Av1SequenceHeaderObu(int width = 1920, int height = 1080, int levelIdx = 8, bool tenBit = false)
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteBits(0, 3); // seq_profile
|
||||
w.WriteBit(false); // still_picture
|
||||
w.WriteBit(false); // reduced_still_picture_header
|
||||
w.WriteBit(false); // timing_info_present_flag
|
||||
w.WriteBit(false); // initial_display_delay_present_flag
|
||||
w.WriteBits(0, 5); // operating_points_cnt_minus_1
|
||||
w.WriteBits(0, 12); // operating_point_idc[0]
|
||||
w.WriteBits((ulong)levelIdx, 5);
|
||||
if (levelIdx > 7) w.WriteBit(false); // seq_tier[0]
|
||||
const int bits = 16;
|
||||
w.WriteBits(bits - 1, 4);
|
||||
w.WriteBits(bits - 1, 4);
|
||||
w.WriteBits((ulong)width - 1, bits);
|
||||
w.WriteBits((ulong)height - 1, bits);
|
||||
w.WriteBit(false); // frame_id_numbers_present_flag
|
||||
w.WriteBit(false); // use_128x128_superblock
|
||||
w.WriteBit(true); // enable_filter_intra
|
||||
w.WriteBit(true); // enable_intra_edge_filter
|
||||
w.WriteBits(0, 4); // interintra, masked, warped, dual filter
|
||||
w.WriteBit(true); // enable_order_hint
|
||||
w.WriteBits(0, 2); // enable_jnt_comp, enable_ref_frame_mvs
|
||||
w.WriteBit(true); // seq_choose_screen_content_tools
|
||||
w.WriteBit(true); // seq_choose_integer_mv
|
||||
w.WriteBits(6, 3); // order_hint_bits_minus_1
|
||||
w.WriteBits(0, 3); // superres, cdef, restoration
|
||||
w.WriteBit(tenBit); // high_bitdepth
|
||||
w.WriteBit(false); // mono_chrome
|
||||
w.WriteBit(true); // color_description_present_flag
|
||||
w.WriteBits(1, 8);
|
||||
w.WriteBits(1, 8);
|
||||
w.WriteBits(1, 8);
|
||||
w.WriteBit(false); // color_range
|
||||
w.WriteBits(0, 2); // chroma_sample_position
|
||||
w.WriteBit(false); // separate_uv_delta_q
|
||||
w.WriteBit(false); // film_grain_params_present
|
||||
w.WriteTrailingBits();
|
||||
return Obu(Av1ObuType.SequenceHeader, w.ToArray());
|
||||
}
|
||||
|
||||
public static byte[] Av1TemporalDelimiter() => Obu(Av1ObuType.TemporalDelimiter, []);
|
||||
|
||||
public static byte[] Av1Frame(int payloadBytes, int seed) => Obu(Av1ObuType.Frame, Random(payloadBytes, seed));
|
||||
|
||||
/// <summary>An OBU with obu_has_size_field set.</summary>
|
||||
public static byte[] Obu(Av1ObuType type, ReadOnlySpan<byte> payload)
|
||||
{
|
||||
var w = new BitWriter();
|
||||
w.WriteBits((ulong)((byte)type << 3 | 0x02), 8);
|
||||
w.WriteLeb128((ulong)payload.Length);
|
||||
w.WriteBytes(payload);
|
||||
return w.ToArray();
|
||||
}
|
||||
|
||||
public static byte[] Concat(params byte[][] parts)
|
||||
{
|
||||
var total = 0;
|
||||
foreach (var p in parts) total += p.Length;
|
||||
var result = new byte[total];
|
||||
var pos = 0;
|
||||
foreach (var p in parts)
|
||||
{
|
||||
p.CopyTo(result, pos);
|
||||
pos += p.Length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] Random(int length, int seed)
|
||||
{
|
||||
var bytes = new byte[length];
|
||||
new Random(seed).NextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static void WriteHevcProfileTierLevel(BitWriter w, int profileIdc, int levelIdc)
|
||||
{
|
||||
w.WriteBits(0, 2); // general_profile_space
|
||||
w.WriteBit(false); // general_tier_flag
|
||||
w.WriteBits((ulong)profileIdc, 5);
|
||||
w.WriteBits(1UL << (31 - profileIdc), 32); // general_profile_compatibility_flag[profileIdc]
|
||||
w.WriteBits(0x9000_0000_0000UL, 48); // progressive_source + frame_only_constraint
|
||||
w.WriteBits((ulong)levelIdc, 8);
|
||||
}
|
||||
|
||||
private static byte[] Nal(ReadOnlySpan<byte> header, byte[] rbsp)
|
||||
{
|
||||
var escaped = AnnexBParser.AddEmulationPrevention(rbsp);
|
||||
var nal = new byte[header.Length + escaped.Length];
|
||||
header.CopyTo(nal);
|
||||
escaped.CopyTo(nal, header.Length);
|
||||
return nal;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user