Files
Chronos/tests/Titano.Core.Tests/Video/SyntheticStreams.cs
T

324 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}