Files
Febe/tests/Febe.Tests/Media/H264/SyntheticStreams.cs
T
Alby96andClaude Opus 5.5 8d03ca23de Decodificatore H.264 per gli strumenti di Baseline ed Extended: FMO, ASO, slice ridondanti, partizionamento dei dati, slice SP e SI (#58)
Il decodificatore rifiutava i flussi con gruppi di slice e con slice SP o SI, e
non leggeva le partizioni A, B e C: ora li gestisce tutti, perché sono parte di
Baseline ed Extended e un file della libreria può usarli.

- Gruppi di slice dal PPS, tipi da 0 a 6, con il cambio per immagine di
  slice_group_change_cycle; il macroblocco successivo segue il gruppo (§8.2.2).
- Slice in ordine qualunque: la disponibilità dei vicini era già per slice, il
  deblocking a fine immagine.
- Slice SP e SI con QS (§8.6): predizione trasformata e riquantizzata in QS, anche
  per P_Skip; sp_for_switch_flag e macroblocchi SI; nel deblocking contano come
  intra.
- Partizionamento: A con intestazione e dati di categoria 2, B con il residuo
  intra (e gli I_PCM), C con quello inter; la regola del nN a zero con la
  predizione intra vincolata (§9.2.1). Una partizione che manca lascia a zero il
  suo residuo e, da lì in poi, quello della partizione che ne dipende.

I cinque flussi FMO, ASO e SP della serie ITU-T (FM2_SVA_C, FM1_FT_E, FM1_BT_B,
sp1_bt_a, sp2_bt_b) passano fra i verificati e coincidono fotogramma per
fotogramma. Partizionamento, slice ridondanti e ASO si provano riscrivendo flussi
verificati senza toccare i bit dei macroblocchi (StreamRewriter) e chiedendo di
nuovo il riferimento ufficiale; le SI con un flusso sintetico calcolato a mano.
Anche i flussi nuovi e quelli partizionati sono provati rovinati.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 16:42:11 +01:00

212 lines
8.0 KiB
C#

namespace Febe.Tests.Codecs.H264;
/// <summary>
/// Tiny H.264 streams written bit by bit from the syntax of the standard (§7.3): Baseline parameter sets, IDR pictures
/// made of I_PCM macroblocks (their samples are written as they are, so every pixel is known) and P pictures made only
/// of skipped macroblocks. They reach the parts of the decoder the conformance streams in the repository do not: gaps in
/// frame_num, redundant pictures, slices without references, huge parameter sets.
/// </summary>
internal static class SyntheticStreams
{
/// <summary>Settings of the sequence parameter set.</summary>
internal sealed record SequenceSettings(
int WidthInMbs = 1,
int HeightInMbs = 1,
int PocType = 2,
int Log2MaxFrameNum = 4,
int MaxNumRefFrames = 1,
bool GapsAllowed = false,
(int Left, int Right, int Top, int Bottom)? Crop = null);
/// <summary>A sequence parameter set NAL unit with its start code (§7.3.2.1.1).</summary>
internal static byte[] Sps(SequenceSettings s)
{
var bits = new BitWriter();
bits.Bits(66, 8); // profile_idc: Baseline
bits.Bits(0, 8); // constraint flags
bits.Bits(30, 8); // level_idc
bits.Ue(0); // seq_parameter_set_id
bits.Ue((uint)s.Log2MaxFrameNum - 4);
bits.Ue((uint)s.PocType);
if (s.PocType == 0)
{
bits.Ue(4); // log2_max_pic_order_cnt_lsb_minus4
}
bits.Ue((uint)s.MaxNumRefFrames);
bits.Bits(s.GapsAllowed ? 1u : 0u, 1);
bits.Ue((uint)s.WidthInMbs - 1);
bits.Ue((uint)s.HeightInMbs - 1);
bits.Bits(1, 1); // frame_mbs_only_flag
bits.Bits(1, 1); // direct_8x8_inference_flag
bits.Bits(s.Crop is null ? 0u : 1u, 1);
if (s.Crop is { } c)
{
bits.Ue((uint)c.Left);
bits.Ue((uint)c.Right);
bits.Ue((uint)c.Top);
bits.Ue((uint)c.Bottom);
}
bits.Bits(0, 1); // vui_parameters_present_flag
return Nal(0x67, bits);
}
/// <summary>A Baseline picture parameter set (CAVLC, one slice group, deblocking control present, §7.3.2.2).</summary>
internal static byte[] Pps(bool redundantPicCntPresent = false)
{
var bits = new BitWriter();
bits.Ue(0); // pic_parameter_set_id
bits.Ue(0); // seq_parameter_set_id
bits.Bits(0, 1); // entropy_coding_mode_flag: CAVLC
bits.Bits(0, 1); // bottom_field_pic_order_in_frame_present_flag
bits.Ue(0); // num_slice_groups_minus1
bits.Ue(0); // num_ref_idx_l0_default_active_minus1
bits.Ue(0); // num_ref_idx_l1_default_active_minus1
bits.Bits(0, 1); // weighted_pred_flag
bits.Bits(0, 2); // weighted_bipred_idc
bits.Se(0); // pic_init_qp_minus26
bits.Se(0); // pic_init_qs_minus26
bits.Se(0); // chroma_qp_index_offset
bits.Bits(1, 1); // deblocking_filter_control_present_flag
bits.Bits(0, 1); // constrained_intra_pred_flag
bits.Bits(redundantPicCntPresent ? 1u : 0u, 1);
return Nal(0x68, bits);
}
/// <summary>
/// An IDR picture of I_PCM macroblocks whose luma samples are all <paramref name="luma"/> (chroma 128), as one slice.
/// </summary>
internal static byte[] Idr(SequenceSettings s, byte luma, int idrPicId = 0, int? redundantPicCnt = null, int macroblocks = -1)
{
var bits = new BitWriter();
bits.Ue(0); // first_mb_in_slice
bits.Ue(7); // slice_type: I (all slices of the picture)
bits.Ue(0); // pic_parameter_set_id
bits.Bits(0, s.Log2MaxFrameNum); // frame_num
bits.Ue((uint)idrPicId);
if (s.PocType == 0)
{
bits.Bits(0, 8); // pic_order_cnt_lsb
}
if (redundantPicCnt is { } r)
{
bits.Ue((uint)r);
}
bits.Bits(0, 1); // no_output_of_prior_pics_flag
bits.Bits(0, 1); // long_term_reference_flag
bits.Se(0); // slice_qp_delta
bits.Ue(1); // disable_deblocking_filter_idc: off
var count = macroblocks < 0 ? s.WidthInMbs * s.HeightInMbs : macroblocks;
for (var m = 0; m < count; m++)
{
bits.Ue(25); // mb_type: I_PCM
bits.Align(); // pcm_alignment_zero_bit
for (var i = 0; i < 256; i++)
{
bits.Bits(luma, 8);
}
for (var i = 0; i < 128; i++)
{
bits.Bits(128, 8);
}
}
return Nal(0x65, bits);
}
/// <summary>A P picture whose macroblocks are all skipped (a copy of the newest reference), as one slice.</summary>
/// <param name="s">The sequence.</param>
/// <param name="frameNum">frame_num.</param>
/// <param name="reference">False for nal_ref_idc 0.</param>
/// <param name="pocLsb">pic_order_cnt_lsb with POC type 0 (default: twice frame_num).</param>
internal static byte[] SkippedP(SequenceSettings s, int frameNum, bool reference = true, int? pocLsb = null)
{
var bits = new BitWriter();
bits.Ue(0); // first_mb_in_slice
bits.Ue(5); // slice_type: P (all slices of the picture)
bits.Ue(0); // pic_parameter_set_id
bits.Bits((uint)frameNum, s.Log2MaxFrameNum);
if (s.PocType == 0)
{
bits.Bits((uint)(pocLsb ?? (2 * frameNum)) & 0xFF, 8); // pic_order_cnt_lsb
}
bits.Bits(0, 1); // num_ref_idx_active_override_flag
bits.Bits(0, 1); // ref_pic_list_modification_flag_l0
if (reference)
{
bits.Bits(0, 1); // adaptive_ref_pic_marking_mode_flag
}
bits.Se(0); // slice_qp_delta
bits.Ue(1); // disable_deblocking_filter_idc: off
bits.Ue((uint)(s.WidthInMbs * s.HeightInMbs)); // mb_skip_run: the whole picture
return Nal(reference ? (byte)0x41 : (byte)0x01, bits);
}
/// <summary>
/// An IDR picture of one SI slice (§7.3.3, §7.3.5) with QS = 26 + <paramref name="qsDelta"/>: every macroblock is an SI
/// macroblock without residual (Intra_4x4, every block with the predicted mode, chroma DC), except those listed in
/// <paramref name="pcm"/>, which are I_PCM (mb_type 26 in an SI slice) with luma <paramref name="pcmLuma"/> and chroma 128.
/// </summary>
internal static byte[] SiIdr(SequenceSettings s, int qsDelta, int[] pcm, byte pcmLuma)
{
var bits = new BitWriter();
bits.Ue(0); // first_mb_in_slice
bits.Ue(9); // slice_type: SI (all slices of the picture)
bits.Ue(0); // pic_parameter_set_id
bits.Bits(0, s.Log2MaxFrameNum); // frame_num
bits.Ue(0); // idr_pic_id
if (s.PocType == 0)
{
bits.Bits(0, 8); // pic_order_cnt_lsb
}
bits.Bits(0, 1); // no_output_of_prior_pics_flag
bits.Bits(0, 1); // long_term_reference_flag
bits.Se(0); // slice_qp_delta
bits.Se(qsDelta); // slice_qs_delta
bits.Ue(1); // disable_deblocking_filter_idc: off
for (var m = 0; m < s.WidthInMbs * s.HeightInMbs; m++)
{
if (Array.IndexOf(pcm, m) >= 0)
{
bits.Ue(26); // mb_type: I_PCM (25 + 1 in an SI slice)
bits.Align(); // pcm_alignment_zero_bit
for (var i = 0; i < 256; i++)
{
bits.Bits(pcmLuma, 8);
}
for (var i = 0; i < 128; i++)
{
bits.Bits(128, 8);
}
continue;
}
bits.Ue(0); // mb_type: SI
for (var i = 0; i < 16; i++)
{
bits.Bits(1, 1); // prev_intra4x4_pred_mode_flag
}
bits.Ue(0); // intra_chroma_pred_mode: DC
bits.Ue(3); // coded_block_pattern: codeNum 3 is 0 for intra prediction modes (table 9-4)
}
return Nal(0x65, bits);
}
private static byte[] Nal(byte header, BitWriter bits)
{
bits.Bits(1, 1); // rbsp_stop_one_bit
return [0, 0, 0, 1, header, .. bits.ToArray()];
}
}