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>
246 lines
8.1 KiB
C#
246 lines
8.1 KiB
C#
namespace Febe.Core.Codecs.H264;
|
|
|
|
public sealed partial class H264Decoder
|
|
{
|
|
/// <summary>The picture has more than one slice group (FMO): a slice walks the macroblocks of its group only.</summary>
|
|
private bool fmo;
|
|
|
|
/// <summary>The slice group of each map unit (mapUnitToSliceGroupMap, §8.2.2.1–§8.2.2.7).</summary>
|
|
private int[] mapUnitGroup = [];
|
|
|
|
/// <summary>The slice group of each macroblock address of the picture (mbToSliceGroupMap, §8.2.2.8).</summary>
|
|
private byte[] mbGroup = [];
|
|
|
|
/// <summary>NextMbAddress (§8.2.2): the next address of the same slice group, the picture size after the last one.</summary>
|
|
private int[] nextMb = [];
|
|
|
|
// What the map in the arrays above was computed for.
|
|
private Pps? mapPps;
|
|
private Sps? mapSps;
|
|
private int mapCycle = -1;
|
|
private int mapLayout = -1;
|
|
|
|
/// <summary>The address after <paramref name="addr"/> in the slice (§7.3.4).</summary>
|
|
private int NextAddress(int addr) => fmo ? nextMb[addr] : addr + 1;
|
|
|
|
/// <summary>
|
|
/// The slice group map of the slice about to be decoded (§8.2.2), computed again only when the parameter sets,
|
|
/// slice_group_change_cycle or the kind of picture (frame, field, MBAFF frame) change.
|
|
/// </summary>
|
|
private void PrepareSliceGroups(SliceHeader h, Sps s, Pps p)
|
|
{
|
|
fmo = p.NumSliceGroups > 1;
|
|
if (!fmo)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 0 frame of frame macroblocks only or a field, 1 MBAFF frame, 2 frame of a stream with fields but no MBAFF.
|
|
var layout = s.FrameMbsOnly || h.FieldPic ? 0 : s.MbAdaptiveFrameField ? 1 : 2;
|
|
var cycle = p.SliceGroupMapType is >= 3 and <= 5 ? h.SliceGroupChangeCycle : -1;
|
|
if (ReferenceEquals(mapPps, p) && ReferenceEquals(mapSps, s) && mapCycle == cycle && mapLayout == layout)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var w = s.WidthInMbs;
|
|
var mapHeight = s.HeightInMapUnits;
|
|
var units = w * mapHeight;
|
|
if (mapUnitGroup.Length != units)
|
|
{
|
|
mapUnitGroup = new int[units];
|
|
}
|
|
|
|
BuildMapUnits(p, w, mapHeight, Math.Min(Math.Max(cycle, 0) * p.SliceGroupChangeRate, units));
|
|
|
|
var total = h.FieldPic ? s.WidthInMbs * s.HeightInMbs / 2 : s.WidthInMbs * s.HeightInMbs;
|
|
if (mbGroup.Length != total)
|
|
{
|
|
mbGroup = new byte[total];
|
|
nextMb = new int[total];
|
|
}
|
|
|
|
for (var i = 0; i < total; i++)
|
|
{
|
|
var unit = layout switch
|
|
{
|
|
0 => i,
|
|
1 => i >> 1,
|
|
_ => ((i / (2 * w)) * w) + (i % w),
|
|
};
|
|
mbGroup[i] = (byte)mapUnitGroup[unit];
|
|
}
|
|
|
|
Span<int> last = stackalloc int[8];
|
|
last.Fill(total);
|
|
for (var i = total - 1; i >= 0; i--)
|
|
{
|
|
var g = mbGroup[i];
|
|
nextMb[i] = last[g];
|
|
last[g] = i;
|
|
}
|
|
|
|
mapPps = p;
|
|
mapSps = s;
|
|
mapCycle = cycle;
|
|
mapLayout = layout;
|
|
}
|
|
|
|
/// <summary>mapUnitToSliceGroupMap for the map type of the PPS (§8.2.2.1–§8.2.2.7).</summary>
|
|
private void BuildMapUnits(Pps p, int w, int h, int unitsInGroup0)
|
|
{
|
|
var map = mapUnitGroup;
|
|
var units = map.Length;
|
|
var groups = p.NumSliceGroups;
|
|
var direction = p.SliceGroupChangeDirection ? 1 : 0;
|
|
switch (p.SliceGroupMapType)
|
|
{
|
|
case 0:
|
|
{
|
|
// Interleaved: runs of each group in turn.
|
|
var i = 0;
|
|
do
|
|
{
|
|
for (var g = 0; g < groups && i < units; i += p.RunLength[g++])
|
|
{
|
|
for (var j = 0; j < p.RunLength[g] && i + j < units; j++)
|
|
{
|
|
map[i + j] = g;
|
|
}
|
|
}
|
|
}
|
|
while (i < units);
|
|
|
|
break;
|
|
}
|
|
|
|
case 1:
|
|
// Dispersed.
|
|
for (var i = 0; i < units; i++)
|
|
{
|
|
map[i] = ((i % w) + (((i / w) * groups) / 2)) % groups;
|
|
}
|
|
|
|
break;
|
|
case 2:
|
|
// Foreground rectangles, the last group taking what is left.
|
|
Array.Fill(map, groups - 1);
|
|
for (var g = groups - 2; g >= 0; g--)
|
|
{
|
|
int topLeft = p.TopLeft[g], bottomRight = p.BottomRight[g];
|
|
if (bottomRight >= units || topLeft > bottomRight || topLeft % w > bottomRight % w)
|
|
{
|
|
throw new H264Exception("Rettangolo di un gruppo di slice fuori dall'immagine.");
|
|
}
|
|
|
|
for (var y = topLeft / w; y <= bottomRight / w; y++)
|
|
{
|
|
for (var x = topLeft % w; x <= bottomRight % w; x++)
|
|
{
|
|
map[(y * w) + x] = g;
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
case 3:
|
|
BoxOut(map, w, h, direction, unitsInGroup0);
|
|
break;
|
|
case 4:
|
|
{
|
|
// Raster scan.
|
|
var upperLeft = direction != 0 ? units - unitsInGroup0 : unitsInGroup0;
|
|
for (var i = 0; i < units; i++)
|
|
{
|
|
map[i] = i < upperLeft ? direction : 1 - direction;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 5:
|
|
{
|
|
// Wipe: column by column.
|
|
var upperLeft = direction != 0 ? units - unitsInGroup0 : unitsInGroup0;
|
|
var k = 0;
|
|
for (var x = 0; x < w; x++)
|
|
{
|
|
for (var y = 0; y < h; y++)
|
|
{
|
|
map[(y * w) + x] = k++ < upperLeft ? direction : 1 - direction;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
// Explicit.
|
|
if (p.SliceGroupIds.Length != units)
|
|
{
|
|
throw new H264Exception("Mappa esplicita dei gruppi di slice di dimensione sbagliata.");
|
|
}
|
|
|
|
for (var i = 0; i < units; i++)
|
|
{
|
|
map[i] = p.SliceGroupIds[i];
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>Box-out (§8.2.2.4): group 0 grows as a spiral from the centre, clockwise or counter-clockwise.</summary>
|
|
private static void BoxOut(int[] map, int w, int h, int direction, int unitsInGroup0)
|
|
{
|
|
Array.Fill(map, 1);
|
|
var x = (w - direction) / 2;
|
|
var y = (h - direction) / 2;
|
|
int left = x, top = y, right = x, bottom = y;
|
|
int xDir = direction - 1, yDir = direction;
|
|
for (var k = 0; k < unitsInGroup0;)
|
|
{
|
|
var vacant = map[(y * w) + x] == 1;
|
|
if (vacant)
|
|
{
|
|
map[(y * w) + x] = 0;
|
|
k++;
|
|
}
|
|
|
|
if (xDir == -1 && x == left)
|
|
{
|
|
left = Math.Max(left - 1, 0);
|
|
x = left;
|
|
xDir = 0;
|
|
yDir = (2 * direction) - 1;
|
|
}
|
|
else if (xDir == 1 && x == right)
|
|
{
|
|
right = Math.Min(right + 1, w - 1);
|
|
x = right;
|
|
xDir = 0;
|
|
yDir = 1 - (2 * direction);
|
|
}
|
|
else if (yDir == -1 && y == top)
|
|
{
|
|
top = Math.Max(top - 1, 0);
|
|
y = top;
|
|
xDir = 1 - (2 * direction);
|
|
yDir = 0;
|
|
}
|
|
else if (yDir == 1 && y == bottom)
|
|
{
|
|
bottom = Math.Min(bottom + 1, h - 1);
|
|
y = bottom;
|
|
xDir = (2 * direction) - 1;
|
|
yDir = 0;
|
|
}
|
|
else
|
|
{
|
|
x += xDir;
|
|
y += yDir;
|
|
}
|
|
}
|
|
}
|
|
}
|