Decodificatore H.264 per i formati dei profili High: 4:2:2, 4:4:4, piani separati, 4:0:0, da 8 a 14 bit, senza perdita (#58)

Il decodificatore rifiutava tutto ciò che non era 4:2:0 a 8 bit, così i video
4:2:2, monocromatici o a 10 bit restavano senza miniatura. Ora i campioni stanno
in piani di uno o due byte e i nuclei di predizione, trasformata e filtro sono
generici su byte e ushort, specializzati dal JIT: a 8 bit i tempi restano quelli
di prima (misurati sui flussi di conformità, 100,4%).

- 4:2:2: DC della crominanza 2x4 con qP + 3, tabelle CAVLC di nC -2, contesti
  CABAC per NumC8x8, predizione intra 8x16, vettore verticale a piena
  risoluzione, bordi della crominanza alle righe 4, 8 e 12.
- 4:4:4: Cb e Cr codificati come la luminanza in CAVLC, con le loro matrici
  e il filtro della luminanza; separate_colour_plane_flag come tre immagini
  monocromatiche con i propri dati di macroblocco e di moto.
- 4:0:0 senza sintassi della crominanza; in uscita la crominanza 4:2:0 al
  valore medio, come il file ufficiale.
- Profondità da 8 a 14 bit: QP da -QpBdOffset, soglie del deblocking e scarti
  dei pesi scalati; uscita a due byte little-endian.
- Senza perdita: qpprime_y_zero_transform_bypass_flag a QP'Y 0, con la DPCM
  della predizione intra orizzontale e verticale.
- VideoFrameDecoder converte in RGB alla profondità e al campionamento del
  fotogramma.

I dieci flussi 4:2:2 (8 e 10 bit) e 4:0:0 della serie ITU-T coincidono con la
ricostruzione ufficiale e passano fra i verificati. 4:4:4, piani separati,
senza perdita e 11-14 bit si provano con flussi sintetici (H264FormatTests).
Il CABAC in 4:4:4 con i piani insieme si rifiuta: mancano i contesti 460-1023.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 17:31:12 +01:00
1 parent 8d03ca23de
commit da84c79f7e
23 files changed
+2016 -607

No files matched your search

@@ -203,6 +203,183 @@ internal static class SyntheticStreams
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.
}
private static byte[] Nal(byte header, BitWriter bits)
{
bits.Bits(1, 1); // rbsp_stop_one_bit