Files
Febe/tests/Febe.Tests/Media/H264/H264ExtendedToolsTests.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

94 lines
4.3 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 Febe.Core.Codecs.H264;
namespace Febe.Tests.Codecs.H264;
/// <summary>
/// Tools of the Baseline and Extended profiles that no stream of the ITU-T series uses — slice data partitioning,
/// redundant slices — and slices in another order than the one of the series, on conformance streams rewritten by
/// <see cref="StreamRewriter"/>: the macroblocks keep their bits, so every frame must still be the official one.
/// </summary>
[TestClass]
public sealed class H264ExtendedToolsTests
{
[TestMethod]
[DataRow("BA3_SVA_C")] // Extended, B slices
[DataRow("sp1_bt_a")] // SP slices
[DataRow("FM1_FT_E")] // slice groups of types 1–4
[DataRow("MR6_BT_B")] // Extended, field pictures
[DataRow("CVMAQP3_Sony_D")] // MBAFF
[DataRow("SVA_BA1_B")] // non-IDR I slices: residual in partition B only
public void DataPartitionsGiveTheOfficialFrames(string name)
{
var stream = StreamRewriter.Partition(Original(name));
var types = NalTypes(stream);
Assert.IsFalse(types.Contains(1), "restano slice non partizionate");
Assert.IsTrue(types.Contains(2), "manca la partizione A");
Assert.IsTrue(types.Contains(3) || types.Contains(4), "mancano le partizioni B e C");
H264ConformanceTests.AssertStreamMatches(name, stream);
}
[TestMethod]
[DataRow(false, true)]
[DataRow(true, false)]
[DataRow(false, false)]
public void MissingPartitionsLeaveTheirResidualOut(bool keepB, bool keepC)
{
// Without partition B or C a slice still decodes, without the residual of its intra or inter macroblocks: every
// picture comes out, and the IDR picture (never partitioned) is still the official one.
var name = "BA3_SVA_C";
var complete = H264ConformanceTests.FrameMd5s(StreamRewriter.Partition(Original(name)));
var partial = H264ConformanceTests.FrameMd5s(StreamRewriter.Partition(Original(name), keepB, keepC));
var expected = H264ConformanceTests.ReferenceFrames(name);
Assert.HasCount(expected.Count, partial);
Assert.AreEqual(expected[0], partial[0], "l'immagine IDR");
Assert.IsTrue(partial.Where((md5, i) => md5 != complete[i]).Any(), "togliere una partizione cambia qualcosa");
}
[TestMethod]
[DataRow("BA3_SVA_C", 4101)] // CAVLC
[DataRow("CABA3_SVA_B", 4102)] // CABAC
[DataRow("MR6_BT_B", 4103)] // fields, POC type 1
[DataRow("CAPAMA3_Sand_F", 4104)] // CABAC, MBAFF and fields
[DataRow("FM1_FT_E", 4105)] // slice groups, several PPS
[DataRow("sp1_bt_a", 4106)] // SP slices
public void RedundantSlicesAreSkipped(string name, int seed)
{
var stream = StreamRewriter.AddRedundantSlices(Original(name), seed);
Assert.IsGreaterThan(Original(name).Length, stream.Length);
H264ConformanceTests.AssertStreamMatches(name, stream);
}
[TestMethod]
public void RedundantSlicesAreSkippedAlsoWhenPartitioned()
{
// The redundant slices become partitions A (header and random bits) with slice_id and redundant_pic_cnt.
var name = "BA3_SVA_C";
H264ConformanceTests.AssertStreamMatches(name, StreamRewriter.Partition(StreamRewriter.AddRedundantSlices(Original(name), 4107)));
}
[TestMethod]
[DataRow("BASQP1_Sony_C")] // twenty slices per picture
[DataRow("FM1_FT_E")] // slice groups
[DataRow("CAMASL3_Sony_B")] // CABAC, MBAFF, four slices per picture
public void SlicesInReverseOrderGiveTheOfficialFrames(string name)
{
var original = Original(name);
var stream = StreamRewriter.ReverseSlices(original);
CollectionAssert.AreNotEqual(original, stream, "il flusso ha più slice per immagine");
H264ConformanceTests.AssertStreamMatches(name, stream);
}
[TestMethod]
[DataRow("FM1_FT_E", 4201)]
[DataRow("sp2_bt_b", 4202)]
[DataRow("BA3_SVA_C", 4203)]
public void DamagedPartitionedStreamsGiveFramesOrAnH264Exception(string name, int seed)
{
H264RobustnessTests.AssertDamagedCopiesDecode(name, StreamRewriter.Partition(Original(name)), seed, trials: 30);
}
private static byte[] Original(string name) => File.ReadAllBytes(H264ConformanceTests.StreamFile(H264ConformanceTests.Folder(name)));
private static HashSet<int> NalTypes(byte[] stream) => StreamRewriter.Units(stream).Select(u => u[0] & 0x1F).ToHashSet();
}