namespace Febe.Core.Codecs.H264;
public sealed partial class H264Decoder
{
/// The picture has more than one slice group (FMO): a slice walks the macroblocks of its group only.
private bool fmo;
/// The slice group of each map unit (mapUnitToSliceGroupMap, §8.2.2.1–§8.2.2.7).
private int[] mapUnitGroup = [];
/// The slice group of each macroblock address of the picture (mbToSliceGroupMap, §8.2.2.8).
private byte[] mbGroup = [];
/// NextMbAddress (§8.2.2): the next address of the same slice group, the picture size after the last one.
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;
/// The address after in the slice (§7.3.4).
private int NextAddress(int addr) => fmo ? nextMb[addr] : addr + 1;
///
/// 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.
///
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 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;
}
/// mapUnitToSliceGroupMap for the map type of the PPS (§8.2.2.1–§8.2.2.7).
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;
}
}
/// Box-out (§8.2.2.4): group 0 grows as a spiral from the centre, clockwise or counter-clockwise.
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;
}
}
}
}