Decodificatore H.264 scritto in casa, provato sui flussi di conformità (#58)
Decodifica Baseline, Main e High a fotogrammi progressivi 4:2:0 a 8 bit (CAVLC e CABAC, slice I/P/B, trasformata 8x8 e matrici di quantizzazione, predizione pesata, diretta spaziale e temporale, gestione dei riferimenti, filtro di deblocco), scritta dallo standard ITU-T H.264 senza codice di terzi. I dodici flussi della suite ITU-T H.264.1 in tests/Febe.Tests/Media/H264/Conformita danno, fotogramma per fotogramma, gli MD5 della ricostruzione ufficiale. Le tabelle di inizializzazione CABAC sono trascritte dal testo della Raccomandazione (03/2005): quelle a memoria per cabac_init_idc 1 e 2 erano sbagliate e le prove le hanno scoperte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9958745e53
commit
697de97ec3
55 files changed
+7072
No files matched your search
@@ -0,0 +1,113 @@
|
||||
namespace Febe.Core.Codecs.H264;
|
||||
|
||||
/// <summary>
|
||||
/// The arithmetic decoding engine of CABAC (§9.3.1.2 and §9.3.3.2), reading the slice data bit by bit, with the
|
||||
/// context variables of the slice.
|
||||
/// </summary>
|
||||
internal sealed class CabacEngine
|
||||
{
|
||||
private readonly byte[] state = new byte[CabacTables.ContextCount];
|
||||
private readonly byte[] mps = new byte[CabacTables.ContextCount];
|
||||
private RbspReader reader = null!;
|
||||
private int range;
|
||||
private int offset;
|
||||
|
||||
/// <summary>Initialises the context variables (§9.3.1.1).</summary>
|
||||
internal void InitContexts(SliceHeader header, int sliceQp)
|
||||
{
|
||||
var table = header.IsIntra ? CabacTables.InitI : CabacTables.InitPB[header.CabacInitIdc];
|
||||
var qp = Math.Clamp(sliceQp, 0, 51);
|
||||
for (var i = 0; i < CabacTables.ContextCount; i++)
|
||||
{
|
||||
int m = table[i * 2], n = table[(i * 2) + 1];
|
||||
var pre = Math.Clamp(((m * qp) >> 4) + n, 1, 126);
|
||||
if (pre <= 63)
|
||||
{
|
||||
state[i] = (byte)(63 - pre);
|
||||
mps[i] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
state[i] = (byte)(pre - 64);
|
||||
mps[i] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Initialises the decoding engine at the current, byte-aligned position (§9.3.1.2).</summary>
|
||||
internal void Start(RbspReader source)
|
||||
{
|
||||
reader = source;
|
||||
range = 510;
|
||||
offset = (int)source.Read(9);
|
||||
if (offset >= range)
|
||||
{
|
||||
throw new H264Exception("Dati CABAC non validi.");
|
||||
}
|
||||
}
|
||||
|
||||
internal int Decision(int ctx)
|
||||
{
|
||||
var s = state[ctx];
|
||||
var lps = CabacTables.RangeLps[(s << 2) + ((range >> 6) & 3)];
|
||||
range -= lps;
|
||||
int bin;
|
||||
if (offset >= range)
|
||||
{
|
||||
bin = mps[ctx] ^ 1;
|
||||
offset -= range;
|
||||
range = lps;
|
||||
if (s == 0)
|
||||
{
|
||||
mps[ctx] ^= 1;
|
||||
}
|
||||
|
||||
state[ctx] = CabacTables.NextStateLps[s];
|
||||
}
|
||||
else
|
||||
{
|
||||
bin = mps[ctx];
|
||||
if (s < 62)
|
||||
{
|
||||
state[ctx] = (byte)(s + 1);
|
||||
}
|
||||
}
|
||||
|
||||
while (range < 256)
|
||||
{
|
||||
range <<= 1;
|
||||
offset = (offset << 1) | reader.ReadBit();
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
internal int Bypass()
|
||||
{
|
||||
offset = (offset << 1) | reader.ReadBit();
|
||||
if (offset >= range)
|
||||
{
|
||||
offset -= range;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
internal int Terminate()
|
||||
{
|
||||
range -= 2;
|
||||
if (offset >= range)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (range < 256)
|
||||
{
|
||||
range <<= 1;
|
||||
offset = (offset << 1) | reader.ReadBit();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user