mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
CD parity information database
This commit is contained in:
@@ -29,7 +29,7 @@ namespace CUETools.CDRepair
|
||||
galois = Galois16.instance;
|
||||
rs = new RsDecode16(npar, galois);
|
||||
crc32 = new Crc32();
|
||||
crc = 0xffffffff;
|
||||
//crc = 0xffffffff;
|
||||
encodeGx = galois.makeEncodeGxLog(npar);
|
||||
laststride = stride + (finalSampleCount * 2) % stride;
|
||||
stridecount = (finalSampleCount * 2) / stride - 2; // minus one for leadin and one for leadout
|
||||
@@ -97,11 +97,19 @@ namespace CUETools.CDRepair
|
||||
get { throw new Exception("unsupported"); }
|
||||
}
|
||||
|
||||
public int NPAR
|
||||
{
|
||||
get
|
||||
{
|
||||
return npar;
|
||||
}
|
||||
}
|
||||
|
||||
public uint CRC
|
||||
{
|
||||
get
|
||||
{
|
||||
return crc ^ 0xffffffff;
|
||||
return crc32.Combine(0xffffffff, crc, stride * 2 * stridecount) ^ 0xffffffff;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,19 +121,14 @@ namespace CUETools.CDRepair
|
||||
protected ushort[] leadin;
|
||||
protected ushort[] leadout;
|
||||
protected bool verify;
|
||||
protected bool hasErrors = false, canRecover = true;
|
||||
protected int actualOffset = 0;
|
||||
protected bool encode;
|
||||
protected uint crcA, crcB;
|
||||
|
||||
internal int[,] sigma;
|
||||
internal int[,] omega;
|
||||
internal int[,] errpos;
|
||||
internal int[,] erroff;
|
||||
internal int[] errors;
|
||||
|
||||
public CDRepairEncode(int finalSampleCount, int stride, int npar, bool verify)
|
||||
public CDRepairEncode(int finalSampleCount, int stride, int npar, bool verify, bool encode)
|
||||
: base (finalSampleCount, stride, npar)
|
||||
{
|
||||
this.verify = verify;
|
||||
this.encode = encode;
|
||||
parity = new byte[stride * npar * 2];
|
||||
if (verify)
|
||||
{
|
||||
@@ -136,45 +139,64 @@ namespace CUETools.CDRepair
|
||||
syndrome = new ushort[1, npar];
|
||||
}
|
||||
|
||||
new public unsafe void Write(AudioBuffer sampleBuffer)
|
||||
private unsafe void ProcessStride(int currentStride, int currentPart, int count, ushort* data)
|
||||
{
|
||||
sampleBuffer.Prepare(this);
|
||||
|
||||
if ((sampleBuffer.ByteLength & 1) != 0)
|
||||
throw new Exception("never happens");
|
||||
|
||||
int firstPos = Math.Max(0, stride - sampleCount * 2);
|
||||
int lastPos = Math.Min(sampleBuffer.ByteLength >> 1, (finalSampleCount - sampleCount) * 2 - laststride);
|
||||
|
||||
fixed (byte* bytes = sampleBuffer.Bytes, par = parity)
|
||||
fixed (int* gx = encodeGx)
|
||||
fixed (uint* t = crc32.table)
|
||||
fixed (uint* crct = crc32.table)
|
||||
fixed (byte* bpar = parity)
|
||||
fixed (ushort* exp = galois.ExpTbl, log = galois.LogTbl, synptr = syndrome)
|
||||
{
|
||||
ushort* data = (ushort*)bytes;
|
||||
fixed (int* gx = encodeGx)
|
||||
for (int pos = 0; pos < count; pos++)
|
||||
{
|
||||
ushort* par = (ushort*)bpar;
|
||||
int part = currentPart + pos;
|
||||
ushort* wr = ((ushort*)par) + part * npar;
|
||||
ushort dd = data[pos];
|
||||
|
||||
if (verify)
|
||||
for (int pos = 0; pos < (sampleBuffer.ByteLength >> 1); pos++)
|
||||
crc = (crc >> 8) ^ crct[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ crct[(byte)(crc ^ (dd >> 8))];
|
||||
|
||||
if (verify)
|
||||
{
|
||||
ushort dd = data[pos];
|
||||
if (sampleCount * 2 + pos < 2 * stride)
|
||||
leadin[sampleCount * 2 + pos] = dd;
|
||||
int remaining = (finalSampleCount - sampleCount) * 2 - pos - 1;
|
||||
if (remaining < stride + laststride)
|
||||
leadout[remaining] = dd;
|
||||
ushort* syn = synptr + part * npar;
|
||||
syn[0] ^= dd; // wk += data
|
||||
for (int i = 1; i < npar; i++)
|
||||
syn[i] = (ushort)(dd ^ galois.mulExp(syn[i], i)); // wk = data + wk * α^i
|
||||
}
|
||||
|
||||
if (npar == 8)
|
||||
{
|
||||
for (int pos = firstPos; pos < lastPos; pos++)
|
||||
int ib = wr[0] ^ dd;
|
||||
if (ib != 0)
|
||||
{
|
||||
int part = (sampleCount * 2 + pos) % stride;
|
||||
ushort* wr = ((ushort*)par) + part * 8;
|
||||
ushort dd = data[pos];
|
||||
ushort* myexp = exp + log[ib];
|
||||
for (int i = 0; i < npar - 1; i++)
|
||||
wr[i] = (ushort)(wr[i + 1] ^ myexp[gx[i]]);
|
||||
wr[npar - 1] = myexp[gx[npar - 1]];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < npar - 1; i++)
|
||||
wr[i] = wr[i + 1];
|
||||
wr[npar - 1] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ (dd >> 8))];
|
||||
private unsafe void ProcessStride8(int currentStride, int currentPart, int count, ushort* data)
|
||||
{
|
||||
fixed (uint* crct = crc32.table)
|
||||
fixed (byte* bpar = parity)
|
||||
fixed (ushort* exp = galois.ExpTbl, log = galois.LogTbl, synptr = syndrome)
|
||||
for (int pos = 0; pos < count; pos++)
|
||||
{
|
||||
ushort* par = (ushort*)bpar;
|
||||
int part = currentPart + pos;
|
||||
ushort* wr = par + part * 8;
|
||||
ushort dd = data[pos];
|
||||
|
||||
crc = (crc >> 8) ^ crct[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ crct[(byte)(crc ^ (dd >> 8))];
|
||||
|
||||
if (encode)
|
||||
{
|
||||
int ib = wr[0] ^ dd;
|
||||
if (ib != 0)
|
||||
{
|
||||
@@ -199,161 +221,299 @@ namespace CUETools.CDRepair
|
||||
wr[6] = wr[7];
|
||||
wr[7] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// syn[i] += data[pos] * α^(pos*i)
|
||||
if (verify && dd != 0)
|
||||
{
|
||||
ushort* syn = synptr + part * 8;
|
||||
ushort* myexp = exp + log[dd];
|
||||
int offs = stridecount - (sampleCount * 2 + pos) / stride;
|
||||
syn[0] ^= dd;
|
||||
syn[1] ^= myexp[offs];
|
||||
syn[2] ^= myexp[(offs * 2) % 65535];
|
||||
syn[3] ^= myexp[(offs * 3) % 65535];
|
||||
syn[4] ^= myexp[(offs * 4) % 65535];
|
||||
syn[5] ^= myexp[(offs * 5) % 65535];
|
||||
syn[6] ^= myexp[(offs * 6) % 65535];
|
||||
syn[7] ^= myexp[(offs * 7) % 65535];
|
||||
//ushort logdd = log[dd];
|
||||
//syn[1] ^= exp[(logdd + offs) % 65535];
|
||||
//syn[2] ^= exp[(logdd + offs * 2) % 65535];
|
||||
//syn[3] ^= exp[(logdd + offs * 3) % 65535];
|
||||
//syn[4] ^= exp[(logdd + offs * 4) % 65535];
|
||||
//syn[5] ^= exp[(logdd + offs * 5) % 65535];
|
||||
//syn[6] ^= exp[(logdd + offs * 6) % 65535];
|
||||
//syn[7] ^= exp[(logdd + offs * 7) % 65535];
|
||||
}
|
||||
// syn[i] += data[pos] * α^(n*i)
|
||||
if (verify && dd != 0)
|
||||
{
|
||||
int n = stridecount - currentStride;
|
||||
ushort* syn = synptr + part * 8;
|
||||
syn[0] ^= dd;
|
||||
int idx = log[dd];
|
||||
idx += n; syn[1] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[2] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[3] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[4] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[5] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[6] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[7] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
}
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
private unsafe void ProcessStride16(int currentStride, int currentPart, int count, ushort* data)
|
||||
{
|
||||
fixed (uint* crct = crc32.table)
|
||||
fixed (byte* bpar = parity)
|
||||
fixed (ushort* exp = galois.ExpTbl, log = galois.LogTbl, synptr = syndrome)
|
||||
for (int pos = 0; pos < count; pos++)
|
||||
{
|
||||
for (int pos = firstPos; pos < lastPos; pos++)
|
||||
ushort* par = (ushort*)bpar;
|
||||
int part = currentPart + pos;
|
||||
ushort* wr = par + part * 16;
|
||||
ushort dd = data[pos];
|
||||
|
||||
crc = (crc >> 8) ^ crct[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ crct[(byte)(crc ^ (dd >> 8))];
|
||||
|
||||
int ib = wr[0] ^ dd;
|
||||
if (ib != 0)
|
||||
{
|
||||
int part = (sampleCount * 2 + pos) % stride;
|
||||
ushort* wr = ((ushort*)par) + part * npar;
|
||||
ushort dd = data[pos];
|
||||
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ (dd >> 8))];
|
||||
|
||||
if (verify)
|
||||
{
|
||||
ushort* syn = synptr + part * npar;
|
||||
syn[0] ^= dd; // wk += data
|
||||
for (int i = 1; i < npar; i++)
|
||||
syn[i] = (ushort)(dd ^ galois.mulExp(syn[i], i)); // wk = data + wk * α^i
|
||||
}
|
||||
|
||||
int ib = wr[0] ^ dd;
|
||||
if (ib != 0)
|
||||
{
|
||||
ushort* myexp = exp + log[ib];
|
||||
for (int i = 0; i < npar - 1; i++)
|
||||
wr[i] = (ushort)(wr[i + 1] ^ myexp[gx[i]]);
|
||||
wr[npar - 1] = myexp[gx[npar - 1]];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < npar - 1; i++)
|
||||
wr[i] = wr[i + 1];
|
||||
wr[npar - 1] = 0;
|
||||
}
|
||||
ushort* myexp = exp + log[ib];
|
||||
wr[0] = (ushort)(wr[1] ^ myexp[0x000059f1]);
|
||||
wr[1] = (ushort)(wr[2] ^ myexp[0x0000608f]);
|
||||
wr[2] = (ushort)(wr[3] ^ myexp[0x0000918b]);
|
||||
wr[3] = (ushort)(wr[4] ^ myexp[0x00004487]);
|
||||
wr[4] = (ushort)(wr[5] ^ myexp[0x0000a151]);
|
||||
wr[5] = (ushort)(wr[6] ^ myexp[0x0000c074]);
|
||||
wr[6] = (ushort)(wr[7] ^ myexp[0x00004178]);
|
||||
wr[7] = (ushort)(wr[8] ^ myexp[0x00004730]);
|
||||
wr[8] = (ushort)(wr[9] ^ myexp[0x00004187]);
|
||||
wr[9] = (ushort)(wr[10] ^ myexp[0x0000c092]);
|
||||
wr[10] = (ushort)(wr[11] ^ myexp[0x0000a17e]);
|
||||
wr[11] = (ushort)(wr[12] ^ myexp[0x000044c3]);
|
||||
wr[12] = (ushort)(wr[13] ^ myexp[0x000091d6]);
|
||||
wr[13] = (ushort)(wr[14] ^ myexp[0x000060e9]);
|
||||
wr[14] = (ushort)(wr[15] ^ myexp[0x00005a5a]);
|
||||
wr[15] = myexp[0x00000078];
|
||||
}
|
||||
else
|
||||
{
|
||||
wr[0] = wr[1];
|
||||
wr[1] = wr[2];
|
||||
wr[2] = wr[3];
|
||||
wr[3] = wr[4];
|
||||
wr[4] = wr[5];
|
||||
wr[5] = wr[6];
|
||||
wr[6] = wr[7];
|
||||
wr[7] = wr[8];
|
||||
wr[8] = wr[9];
|
||||
wr[9] = wr[10];
|
||||
wr[10] = wr[11];
|
||||
wr[11] = wr[12];
|
||||
wr[12] = wr[13];
|
||||
wr[13] = wr[14];
|
||||
wr[14] = wr[15];
|
||||
wr[15] = 0;
|
||||
}
|
||||
|
||||
// syn[i] += data[pos] * α^(n*i)
|
||||
if (verify && dd != 0)
|
||||
{
|
||||
int n = stridecount - currentStride;
|
||||
ushort* syn = synptr + part * 16;
|
||||
syn[0] ^= dd;
|
||||
int idx = log[dd];
|
||||
idx += n; syn[1] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[2] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[3] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[4] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[5] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[6] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[7] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[8] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[9] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[10] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[11] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[12] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[13] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[14] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
idx += n; syn[15] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new public unsafe void Write(AudioBuffer sampleBuffer)
|
||||
{
|
||||
sampleBuffer.Prepare(this);
|
||||
|
||||
if ((sampleBuffer.ByteLength & 1) != 0)
|
||||
throw new Exception("never happens");
|
||||
|
||||
fixed (byte* bytes = sampleBuffer.Bytes)
|
||||
{
|
||||
int offs = 0;
|
||||
while (offs < sampleBuffer.Length)
|
||||
{
|
||||
int currentPart = (sampleCount * 2) % stride;
|
||||
int currentStride = (sampleCount * 2) / stride;
|
||||
// Process no more than there is in the buffer, and no more than up to a stride boundary.
|
||||
int copyCount = Math.Min((sampleBuffer.Length - offs) * 2, stride - currentPart);
|
||||
ushort* data = ((ushort*)bytes) + offs * 2;
|
||||
|
||||
if (verify)
|
||||
{
|
||||
// remember CRC after leadin
|
||||
if (sampleCount * 2 == stride * 2)
|
||||
crcA = crc;
|
||||
|
||||
// remember CRC before leadout
|
||||
if ((finalSampleCount - sampleCount) * 2 == stride + laststride)
|
||||
crcB = crc;
|
||||
|
||||
if (currentStride < 2)
|
||||
for (int pos = 0; pos < copyCount; pos++)
|
||||
leadin[sampleCount * 2 + pos] = data[pos];
|
||||
|
||||
if (currentStride >= stridecount)
|
||||
for (int pos = 0; pos < copyCount; pos++)
|
||||
{
|
||||
int remaining = (finalSampleCount - sampleCount) * 2 - pos - 1;
|
||||
if (remaining < stride + laststride)
|
||||
leadout[remaining] = data[pos];
|
||||
}
|
||||
}
|
||||
|
||||
if (currentStride >= 1 && currentStride <= stridecount)
|
||||
{
|
||||
if (npar == 8)
|
||||
ProcessStride8(currentStride, currentPart, copyCount, data);
|
||||
else if (npar == 16)
|
||||
ProcessStride16(currentStride, currentPart, copyCount, data);
|
||||
else
|
||||
ProcessStride(currentStride, currentPart, copyCount, data);
|
||||
}
|
||||
|
||||
sampleCount += copyCount >> 1;
|
||||
offs += copyCount >> 1;
|
||||
}
|
||||
}
|
||||
sampleCount += sampleBuffer.Length;
|
||||
}
|
||||
|
||||
public unsafe bool VerifyParity(byte[] parity2)
|
||||
public unsafe CDRepairFix VerifyParity(byte[] parity2, int actualOffset)
|
||||
{
|
||||
return VerifyParity(parity2, 0, parity2.Length);
|
||||
return VerifyParity(npar, parity2, 0, parity2.Length, actualOffset);
|
||||
}
|
||||
|
||||
public unsafe bool VerifyParity(byte[] parity2, int pos, int len)
|
||||
private unsafe uint OffsettedCRC(int actualOffset)
|
||||
{
|
||||
fixed (uint* crct = crc32.table)
|
||||
{
|
||||
// calculate leadin CRC
|
||||
uint crc0 = 0;
|
||||
for (int off = stride - 2 * actualOffset; off < 2 * stride; off++)
|
||||
{
|
||||
ushort dd = leadin[off];
|
||||
crc0 = (crc0 >> 8) ^ crct[(byte)(crc0 ^ dd)];
|
||||
crc0 = (crc0 >> 8) ^ crct[(byte)(crc0 ^ (dd >> 8))];
|
||||
}
|
||||
// calculate leadout CRC
|
||||
uint crc2 = 0;
|
||||
for (int off = laststride + stride - 1; off >= laststride + 2 * actualOffset; off--)
|
||||
{
|
||||
ushort dd = leadout[off];
|
||||
crc2 = (crc2 >> 8) ^ crct[(byte)(crc2 ^ dd)];
|
||||
crc2 = (crc2 >> 8) ^ crct[(byte)(crc2 ^ (dd >> 8))];
|
||||
}
|
||||
|
||||
// calculate middle CRC
|
||||
uint crc1 = crc32.Combine(crcA, crcB, (stridecount - 2) * stride * 2);
|
||||
// calculate offsettedCRC as sum of 0xffffffff, crc0, crc1, crc2;
|
||||
return crc32.Combine(
|
||||
0xffffffff,
|
||||
crc32.Combine(
|
||||
crc32.Combine(
|
||||
crc0,
|
||||
crc1,
|
||||
(stridecount - 2) * stride * 2),
|
||||
crc2,
|
||||
(stride - 2 * actualOffset) * 2),
|
||||
stridecount * stride * 2) ^ 0xffffffff;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe bool FindOffset(int npar2, byte[] parity2, int pos, uint expectedCRC, out int actualOffset, out bool hasErrors)
|
||||
{
|
||||
if (npar2 != npar)
|
||||
throw new Exception("npar mismatch");
|
||||
if (!verify)
|
||||
throw new Exception("verify was not enabled");
|
||||
if (sampleCount != finalSampleCount)
|
||||
throw new Exception("sampleCount != finalSampleCount");
|
||||
if (len != stride * npar * 2)
|
||||
throw new Exception("wrong size");
|
||||
|
||||
sigma = new int[stride, npar / 2 + 2];
|
||||
omega = new int[stride, npar / 2 + 1];
|
||||
errpos = new int[stride, npar / 2];
|
||||
erroff = new int[stride, npar / 2];
|
||||
errors = new int[stride];
|
||||
|
||||
actualOffset = 0;
|
||||
|
||||
// find offset
|
||||
fixed (byte* par2ptr = &parity2[pos])
|
||||
{
|
||||
ushort* par2 = (ushort*)par2ptr;
|
||||
int* syn = stackalloc int[npar];
|
||||
int* _sigma = stackalloc int[npar];
|
||||
int* _omega = stackalloc int[npar];
|
||||
int* _errpos = stackalloc int[npar];
|
||||
int bestErrors = npar;
|
||||
bool foundOffset = false;
|
||||
|
||||
// We can only use offset if Abs(offset * 2) < stride,
|
||||
// else we might need to add/remove more than one sample
|
||||
// from syndrome calculations, and that would be too difficult
|
||||
// and will probably require longer leadin/leadout.
|
||||
for (int offset = 1 - stride / 2; offset < stride / 2; offset++)
|
||||
for (int allowed_errors = 0; allowed_errors < npar / 2 && !foundOffset; allowed_errors++)
|
||||
{
|
||||
int err = 0;
|
||||
int part2 = 0;
|
||||
ushort* wr = par2 + part2 * npar;
|
||||
|
||||
for (int i = 0; i < npar; i++)
|
||||
// We can only use offset if Abs(offset * 2) < stride,
|
||||
// else we might need to add/remove more than one sample
|
||||
// from syndrome calculations, and that would be too difficult
|
||||
// and will probably require longer leadin/leadout.
|
||||
for (int offset = 1 - stride / 2; offset < stride / 2; offset++)
|
||||
{
|
||||
int part = (stride - 1) % stride;
|
||||
int part2 = (part + offset * 2 + stride) % stride;
|
||||
ushort* wr = par2 + part2 * npar;
|
||||
int err = 0;
|
||||
int part = (part2 + stride - offset * 2) % stride;
|
||||
int* syn = stackalloc int[npar];
|
||||
|
||||
syn[i] = syndrome[part, i];
|
||||
|
||||
// offset < 0
|
||||
if (part < -offset * 2)
|
||||
for (int i = 0; i < npar; i++)
|
||||
{
|
||||
syn[i] ^= galois.mulExp(leadin[stride + part], (i * (stridecount - 1)) % galois.Max);
|
||||
syn[i] = leadout[laststride - part - 1] ^ galois.mulExp(syn[i], i);
|
||||
}
|
||||
// offset > 0
|
||||
if (part >= stride - offset * 2)
|
||||
{
|
||||
syn[i] = galois.divExp(syn[i] ^ leadout[laststride + stride - part - 1], i);
|
||||
syn[i] ^= galois.mulExp(leadin[part], (i * (stridecount - 1)) % galois.Max);
|
||||
}
|
||||
int synI = syndrome[part, i];
|
||||
|
||||
for (int j = 0; j < npar; j++)
|
||||
syn[i] = wr[j] ^ galois.mulExp(syn[i], i);
|
||||
// offset < 0
|
||||
if (part < -offset * 2)
|
||||
{
|
||||
synI ^= galois.mulExp(leadin[stride + part], (i * (stridecount - 1)) % galois.Max);
|
||||
synI = leadout[laststride - part - 1] ^ galois.mulExp(synI, i);
|
||||
}
|
||||
// offset > 0
|
||||
if (part >= stride - offset * 2)
|
||||
{
|
||||
synI = galois.divExp(synI ^ leadout[laststride + stride - part - 1], i);
|
||||
synI ^= galois.mulExp(leadin[part], (i * (stridecount - 1)) % galois.Max);
|
||||
}
|
||||
|
||||
err |= syn[i];
|
||||
}
|
||||
if (err == 0)
|
||||
{
|
||||
actualOffset = offset;
|
||||
bestErrors = 0;
|
||||
break;
|
||||
}
|
||||
int err_count = rs.calcSigmaMBM(_sigma, _omega, syn);
|
||||
if (err_count > 0 && rs.chienSearch(_errpos, stridecount + npar, err_count, _sigma))
|
||||
{
|
||||
if (err_count < bestErrors)
|
||||
for (int j = 0; j < npar; j++)
|
||||
synI = wr[j] ^ galois.mulExp(synI, i);
|
||||
|
||||
syn[i] = synI;
|
||||
err |= synI;
|
||||
}
|
||||
int err_count = err == 0 ? 0 : rs.calcSigmaMBM(_sigma, syn);
|
||||
if (err_count == allowed_errors && (err_count == 0 || rs.chienSearch(_errpos, stridecount + npar, err_count, _sigma)))
|
||||
{
|
||||
actualOffset = offset;
|
||||
bestErrors = err_count;
|
||||
hasErrors = err_count != 0 || OffsettedCRC(offset) != expectedCRC;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
actualOffset = 0;
|
||||
hasErrors = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public unsafe CDRepairFix VerifyParity(int npar2, byte[] parity2, int pos, int len, int actualOffset)
|
||||
{
|
||||
if (len != stride * npar * 2)
|
||||
throw new Exception("wrong size");
|
||||
|
||||
CDRepairFix fix = new CDRepairFix(this);
|
||||
fix.actualOffset = actualOffset;
|
||||
fix.correctableErrors = 0;
|
||||
fix.hasErrors = false;
|
||||
fix.canRecover = true;
|
||||
|
||||
fix.sigma = new int[stride, npar / 2 + 2];
|
||||
fix.omega = new int[stride, npar / 2 + 1];
|
||||
fix.errpos = new int[stride, npar / 2];
|
||||
fix.erroff = new int[stride, npar / 2];
|
||||
fix.errors = new int[stride];
|
||||
|
||||
hasErrors = false;
|
||||
fixed (byte* par = &parity2[pos])
|
||||
fixed (ushort* exp = galois.ExpTbl, log = galois.LogTbl)
|
||||
{
|
||||
int* syn = stackalloc int[npar];
|
||||
int offset = actualOffset;
|
||||
int offset = fix.actualOffset;
|
||||
|
||||
for (int part = 0; part < stride; part++)
|
||||
{
|
||||
@@ -400,25 +560,27 @@ namespace CUETools.CDRepair
|
||||
|
||||
if (err != 0)
|
||||
{
|
||||
hasErrors = true;
|
||||
fixed (int* s = &sigma[part, 0], o = &omega[part, 0], e = &errpos[part, 0], f = &erroff[part, 0])
|
||||
fixed (int* s = &fix.sigma[part, 0], o = &fix.omega[part, 0], e = &fix.errpos[part, 0], f = &fix.erroff[part, 0])
|
||||
{
|
||||
errors[part] = rs.calcSigmaMBM(s, o, syn);
|
||||
if (errors[part] <= 0 || !rs.chienSearch(e, stridecount + npar, errors[part], s))
|
||||
canRecover = false;
|
||||
fix.errors[part] = rs.calcSigmaMBM(s, syn);
|
||||
fix.hasErrors = true;
|
||||
fix.correctableErrors += fix.errors[part];
|
||||
if (fix.errors[part] <= 0 || !rs.chienSearch(e, stridecount + npar, fix.errors[part], s))
|
||||
fix.canRecover = false;
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < errors[part]; i++)
|
||||
galois.mulPoly(o, s, syn, npar / 2 + 1, npar, npar);
|
||||
for (int i = 0; i < fix.errors[part]; i++)
|
||||
f[i] = galois.toPos(stridecount + npar, e[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
errors[part] = 0;
|
||||
fix.errors[part] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return !hasErrors;
|
||||
return fix;
|
||||
}
|
||||
|
||||
public byte[] Parity
|
||||
@@ -428,6 +590,55 @@ namespace CUETools.CDRepair
|
||||
return parity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CDRepairFix : CDRepair
|
||||
{
|
||||
internal bool hasErrors = false, canRecover = true;
|
||||
internal int actualOffset = 0;
|
||||
internal int correctableErrors = 0;
|
||||
internal int[,] sigma;
|
||||
internal int[,] omega;
|
||||
internal int[,] errpos;
|
||||
internal int[,] erroff;
|
||||
internal int[] errors;
|
||||
|
||||
public CDRepairFix(CDRepairEncode decode)
|
||||
: base(decode)
|
||||
{
|
||||
}
|
||||
|
||||
new public unsafe void Write(AudioBuffer sampleBuffer)
|
||||
{
|
||||
sampleBuffer.Prepare(this);
|
||||
|
||||
if ((sampleBuffer.ByteLength & 1) != 0)
|
||||
throw new Exception("never happens");
|
||||
|
||||
int firstPos = Math.Max(0, stride - sampleCount * 2 - ActualOffset * 2);
|
||||
int lastPos = Math.Min(sampleBuffer.ByteLength >> 1, (finalSampleCount - sampleCount) * 2 - laststride - ActualOffset * 2);
|
||||
|
||||
fixed (byte* bytes = sampleBuffer.Bytes)
|
||||
fixed (uint* t = crc32.table)
|
||||
{
|
||||
ushort* data = (ushort*)bytes;
|
||||
for (int pos = firstPos; pos < lastPos; pos++)
|
||||
{
|
||||
int part = (sampleCount * 2 + pos) % stride;
|
||||
int nerrors = errors[part];
|
||||
fixed (int* s = &sigma[part, 0], o = &omega[part, 0], f = &erroff[part, 0])
|
||||
for (int i = 0; i < nerrors; i++)
|
||||
if (f[i] == (sampleCount * 2 + ActualOffset * 2 + pos) / stride - 1)
|
||||
data[pos] ^= (ushort)rs.doForney(nerrors, errpos[part, i], s, o);
|
||||
|
||||
ushort dd = data[pos];
|
||||
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ (dd >> 8))];
|
||||
}
|
||||
}
|
||||
sampleCount += sampleBuffer.Length;
|
||||
}
|
||||
|
||||
public bool HasErrors
|
||||
{
|
||||
@@ -445,6 +656,14 @@ namespace CUETools.CDRepair
|
||||
}
|
||||
}
|
||||
|
||||
public int CorrectableErrors
|
||||
{
|
||||
get
|
||||
{
|
||||
return correctableErrors;
|
||||
}
|
||||
}
|
||||
|
||||
public int ActualOffset
|
||||
{
|
||||
get
|
||||
@@ -453,47 +672,4 @@ namespace CUETools.CDRepair
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CDRepairFix : CDRepair
|
||||
{
|
||||
CDRepairEncode decode;
|
||||
|
||||
public CDRepairFix(CDRepairEncode decode)
|
||||
: base(decode)
|
||||
{
|
||||
this.decode = decode;
|
||||
}
|
||||
|
||||
new public unsafe void Write(AudioBuffer sampleBuffer)
|
||||
{
|
||||
sampleBuffer.Prepare(this);
|
||||
|
||||
if ((sampleBuffer.ByteLength & 1) != 0)
|
||||
throw new Exception("never happens");
|
||||
|
||||
int firstPos = Math.Max(0, stride - sampleCount * 2 - decode.ActualOffset * 2);
|
||||
int lastPos = Math.Min(sampleBuffer.ByteLength >> 1, (finalSampleCount - sampleCount) * 2 - laststride - decode.ActualOffset * 2);
|
||||
|
||||
fixed (byte* bytes = sampleBuffer.Bytes)
|
||||
fixed (uint* t = crc32.table)
|
||||
{
|
||||
ushort* data = (ushort*)bytes;
|
||||
for (int pos = firstPos; pos < lastPos; pos++)
|
||||
{
|
||||
int part = (sampleCount * 2 + pos) % stride;
|
||||
int errors = decode.errors[part];
|
||||
fixed (int* s = &decode.sigma[part, 0], o = &decode.omega[part, 0], f = &decode.erroff[part, 0])
|
||||
for (int i = 0; i < errors; i++)
|
||||
if (f[i] == (sampleCount * 2 + decode.ActualOffset * 2 + pos) / stride - 1)
|
||||
data[pos] ^= (ushort)rs.doForney(errors, decode.errpos[part, i], s, o);
|
||||
|
||||
ushort dd = data[pos];
|
||||
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ (dd >> 8))];
|
||||
}
|
||||
}
|
||||
sampleCount += sampleBuffer.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user