Files
Febe/tests/Febe.Tests/Media/H264/SyntheticStreams.cs
T
Alby96andClaude Opus 5.5 cdc3cc11b9 Correzioni dopo la revisione del decodificatore H.264, dei lettori FLV e della CI (#58)
- CI: le due prove MVC di FeaturesStillToDo finivano con «da verificare: …», che
  riepilogo-prove.sh contava come saltate: ogni verifica del ramo sarebbe fallita.
  Ora cominciano con «da fare: », e lo script esenta solo le inconclusive di
  FeaturesStillToDo il cui motivo comincia con «da fare: » subito dopo la cornice
  di MSTest (prima bastava la parola in qualunque punto di qualunque prova).
  PipelineTests prova lo script su .trx con le cornici inglese e italiana;
  processo.md, Sviluppo.md e verifica.yml descrivono la categoria.
- Partizionamento dei dati: una partizione B o C sperduta (partizione A persa)
  sovrascriveva il buffer delle partizioni della slice in attesa. Ora slice_id e
  redundant_pic_cnt si leggono prima di copiare, e contano l'ordine A, B, C.
- MVC: le viste tengono insieme al massimo 2 × MaxDpbMbs di immagini, e il
  decodificatore di una vista nuova si tiene solo dopo il controllo dell'SPS di
  sottoinsieme; Wanted vale per ogni vista.
- FLV: senza durata nei metadati la durata arriva alla fine dell'ultimo
  fotogramma, come per MPEG; «solo audio» solo con una prova vera nella testa
  (un onMetaData grande non fa più scartare un video).
- Flussi di conformità: tolti i 48 readme e schede di terzi (decisione di
  Alberto: nel repository solo flussi e MD5).
- Prove nuove: partizioni A perse, limite delle viste e Wanted in MVC, unità di
  estensione troncate, flussi rovinati 4:2:2, 10 bit, 4:0:0, SVC e MVC.
- Commenti sulla provenienza che citano i file di ricostruzione ufficiali e
  §8.7 invece del decodificatore di riferimento; commento di
  ImportEnvironment.VideoProbe aggiornato; righe nuove in decisioni.md.

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

475 lines
19 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.
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);
}
/// <summary>
/// Settings of a High 4:4:4 Predictive sequence (profile_idc 244): chroma format, bit depths, separate colour planes,
/// lossless macroblocks (qpprime_y_zero_transform_bypass_flag, with the slices at QP′Y 0), CABAC.
/// </summary>
internal sealed record HighSettings(
int ChromaFormat = 1,
int BitDepthLuma = 8,
int BitDepthChroma = 8,
bool SeparatePlanes = false,
bool Lossless = false,
int WidthInMbs = 1,
int HeightInMbs = 1,
bool Cabac = false)
{
/// <summary>ChromaArrayType: no chroma syntax for monochrome and separate planes.</summary>
internal int ChromaArrayType => SeparatePlanes ? 0 : ChromaFormat;
/// <summary>Chroma samples of each component in a macroblock.</summary>
internal int ChromaSamples => ChromaArrayType switch
{
0 => 0,
1 => 64,
2 => 128,
_ => 256,
};
}
/// <summary>The sequence parameter set of <paramref name="h"/> (§7.3.2.1.1), POC type 2, no VUI.</summary>
internal static byte[] HighSps(HighSettings h)
{
var bits = new BitWriter();
bits.Bits(244, 8); // profile_idc: High 4:4:4 Predictive
bits.Bits(0, 8); // constraint flags
bits.Bits(30, 8); // level_idc
bits.Ue(0); // seq_parameter_set_id
bits.Ue((uint)h.ChromaFormat);
if (h.ChromaFormat == 3)
{
bits.Bits(h.SeparatePlanes ? 1u : 0u, 1);
}
bits.Ue((uint)h.BitDepthLuma - 8);
bits.Ue((uint)h.BitDepthChroma - 8);
bits.Bits(h.Lossless ? 1u : 0u, 1); // qpprime_y_zero_transform_bypass_flag
bits.Bits(0, 1); // seq_scaling_matrix_present_flag
bits.Ue(0); // log2_max_frame_num_minus4
bits.Ue(2); // pic_order_cnt_type
bits.Ue(1); // max_num_ref_frames
bits.Bits(0, 1); // gaps_in_frame_num_value_allowed_flag
bits.Ue((uint)h.WidthInMbs - 1);
bits.Ue((uint)h.HeightInMbs - 1);
bits.Bits(1, 1); // frame_mbs_only_flag
bits.Bits(1, 1); // direct_8x8_inference_flag
bits.Bits(0, 1); // frame_cropping_flag
bits.Bits(0, 1); // vui_parameters_present_flag
return Nal(0x67, bits);
}
/// <summary>The picture parameter set of <paramref name="h"/>: QP′Y 0 (QPY −QpBdOffsetY) when lossless, else 26.</summary>
internal static byte[] HighPps(HighSettings h)
{
var bits = new BitWriter();
bits.Ue(0); // pic_parameter_set_id
bits.Ue(0); // seq_parameter_set_id
bits.Bits(h.Cabac ? 1u : 0u, 1); // entropy_coding_mode_flag
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(h.Lossless ? -26 - (6 * (h.BitDepthLuma - 8)) : 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(0, 1); // redundant_pic_cnt_present_flag
return Nal(0x68, bits);
}
/// <summary>
/// One I slice of an IDR picture of <paramref name="h"/> (for separate planes, the slice of <paramref name="colourPlane"/>),
/// deblocking off, its macroblocks written by <paramref name="macroblocks"/>.
/// </summary>
internal static byte[] HighIdr(HighSettings h, Action<BitWriter> macroblocks, int colourPlane = 0)
{
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
if (h.SeparatePlanes)
{
bits.Bits((uint)colourPlane, 2);
}
bits.Bits(0, 4); // frame_num
bits.Ue(0); // idr_pic_id
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
macroblocks(bits);
return Nal(0x65, bits);
}
/// <summary>
/// An I_PCM macroblock (CAVLC): its luma samples all <paramref name="luma"/>, then (with chroma syntax) Cb and Cr all
/// <paramref name="cb"/> and <paramref name="cr"/>, at the bit depths of <paramref name="h"/>.
/// </summary>
internal static void Pcm(BitWriter bits, HighSettings h, int luma, int cb, int cr)
{
bits.Ue(25); // mb_type: I_PCM
bits.Align(); // pcm_alignment_zero_bit
for (var i = 0; i < 256; i++)
{
bits.Bits((uint)luma, h.BitDepthLuma);
}
for (var c = 0; c < 2; c++)
{
for (var i = 0; i < h.ChromaSamples; i++)
{
bits.Bits((uint)(c == 0 ? cb : cr), h.BitDepthChroma);
}
}
}
/// <summary>
/// An Intra_16x16 macroblock (CAVLC) without AC residual, prediction mode <paramref name="mode"/> (0 vertical,
/// 1 horizontal, 2 DC; chroma DC if the chroma has syntax), and in each luma-like plane (Y; Cb and Cr too in 4:4:4) one
/// DC level of <paramref name="dc"/> on the first block. The neighbour it predicts from must be I_PCM: its blocks
/// count 16 coefficients, so nC is 16 and coeff_token the 6-bit code of §9.2.1.
/// </summary>
internal static void Intra16x16Dc(BitWriter bits, HighSettings h, int mode, int[] dc)
{
bits.Ue((uint)(1 + mode)); // mb_type: I_16x16_<mode>_0_0
if (h.ChromaArrayType is 1 or 2)
{
bits.Ue(0); // intra_chroma_pred_mode: DC
}
bits.Se(0); // mb_qp_delta
var planes = h.ChromaArrayType == 3 ? 3 : 1;
for (var p = 0; p < planes; p++)
{
var level = dc[p];
if (level == 0)
{
bits.Bits(0b000011, 6); // coeff_token: TotalCoeff 0
continue;
}
if (Math.Abs(level) == 1)
{
bits.Bits(0b000001, 6); // coeff_token: TotalCoeff 1, TrailingOnes 1
bits.Bits(level < 0 ? 1u : 0u, 1); // trailing_ones_sign_flag
}
else
{
// coeff_token: TotalCoeff 1, TrailingOnes 0; level_prefix with suffixLength 0, the first level after
// fewer than three trailing ones counting from 2 (levelCode = 2 |level| − 4, + 1 when negative).
bits.Bits(0b000000, 6);
var levelCode = (2 * Math.Abs(level)) - 4 + (level < 0 ? 1 : 0);
if (levelCode >= 14)
{
throw new ArgumentOutOfRangeException(nameof(dc), "Livello troppo grande per questo flusso di prova.");
}
bits.Bits(1, levelCode + 1); // level_prefix: levelCode zeros then a one
}
bits.Bits(1, 1); // total_zeros: 0
}
// No chroma residual: the mb_type says coded_block_pattern chroma 0.
}
/// <summary>
/// A subset sequence parameter set (NAL unit 15, §7.3.2.1.3) of the Multiview High profile: the sequence of
/// <paramref name="s"/> in 8-bit 4:2:0, then seq_parameter_set_mvc_extension() (§H.7.3.2.1.4) with
/// <paramref name="views"/> views, view_id 0 to views − 1, none of them referring to another.
/// </summary>
internal static byte[] MvcSubsetSps(SequenceSettings s, int views)
{
var bits = new BitWriter();
bits.Bits(118, 8); // profile_idc: Multiview High
bits.Bits(0, 8); // constraint flags
bits.Bits(51, 8); // level_idc
bits.Ue(0); // seq_parameter_set_id
bits.Ue(1); // chroma_format_idc: 4:2:0
bits.Ue(0); // bit_depth_luma_minus8
bits.Ue(0); // bit_depth_chroma_minus8
bits.Bits(0, 1); // qpprime_y_zero_transform_bypass_flag
bits.Bits(0, 1); // seq_scaling_matrix_present_flag
bits.Ue((uint)s.Log2MaxFrameNum - 4);
bits.Ue(2); // pic_order_cnt_type
bits.Ue((uint)s.MaxNumRefFrames);
bits.Bits(0, 1); // gaps_in_frame_num_value_allowed_flag
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(0, 1); // frame_cropping_flag
bits.Bits(0, 1); // vui_parameters_present_flag
bits.Bits(1, 1); // bit_equal_to_one
bits.Ue((uint)views - 1); // num_views_minus1
for (var i = 0; i < views; i++)
{
bits.Ue((uint)i); // view_id
}
for (var pass = 0; pass < 2; pass++)
{
for (var i = 1; i < views; i++)
{
bits.Ue(0); // num_anchor_refs_l0 (then num_non_anchor_refs_l0)
bits.Ue(0); // num_anchor_refs_l1 (then num_non_anchor_refs_l1)
}
}
bits.Ue(0); // num_level_values_signalled_minus1
bits.Bits(51, 8); // level_idc
bits.Ue(0); // num_applicable_ops_minus1
bits.Bits(0, 3); // applicable_op_temporal_id
bits.Ue(0); // applicable_op_num_target_views_minus1
bits.Ue(0); // applicable_op_target_view_id
bits.Ue((uint)views - 1); // applicable_op_num_views_minus1
bits.Bits(0, 1); // mvc_vui_parameters_present_flag
bits.Bits(0, 1); // additional_extension2_flag
return Nal(0x6F, bits);
}
/// <summary>
/// An IDR view component of view <paramref name="viewId"/> (NAL unit 20 with the MVC header, §H.7.3.1.1), as one I
/// slice whose macroblocks are all Intra_16x16 DC without residual: one byte each.
/// </summary>
internal static byte[] ViewIdr(SequenceSettings s, int viewId)
{
var bits = new BitWriter();
bits.Ue(0); // first_mb_in_slice
bits.Ue(7); // slice_type: I
bits.Ue(0); // pic_parameter_set_id
bits.Bits(0, s.Log2MaxFrameNum); // frame_num
bits.Ue(0); // idr_pic_id
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
for (var m = 0; m < s.WidthInMbs * s.HeightInMbs; m++)
{
bits.Ue(3); // mb_type: I_16x16_2_0_0 (DC, no coded block)
bits.Ue(0); // intra_chroma_pred_mode: DC
bits.Se(0); // mb_qp_delta
bits.Bits(1, 1); // coeff_token of Intra16x16DCLevel, nC 0: TotalCoeff 0
}
bits.Bits(1, 1); // rbsp_stop_one_bit
// nal_unit_header_mvc_extension(), outside emulation prevention: svc_extension_flag 0, non_idr_flag 0,
// priority_id 1, view_id, temporal_id 0, anchor_pic_flag 1, inter_view_flag 1, reserved_one_bit.
return [0, 0, 0, 1, 0x74, 0x01, (byte)(viewId >> 2), (byte)(((viewId & 3) << 6) | 0x07), .. bits.ToArray()];
}
private static byte[] Nal(byte header, BitWriter bits)
{
bits.Bits(1, 1); // rbsp_stop_one_bit
return [0, 0, 0, 1, header, .. bits.ToArray()];
}
}