mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
Version bump to 2.0.8
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using CUETools.Parity;
|
||||
using CUETools.CDImage;
|
||||
using CUETools.Codecs;
|
||||
|
||||
@@ -18,9 +19,10 @@ namespace CUETools.AccurateRip
|
||||
_accDisks = new List<AccDisk>();
|
||||
//_crc32 = new Crc32();
|
||||
_hasLogCRC = false;
|
||||
_finalSampleCount = _toc.AudioLength * 588;
|
||||
_CRCLOG = new uint[_toc.AudioTracks + 1];
|
||||
_CRCMASK = new uint[_toc.AudioTracks + 1];
|
||||
_CRCMASK[0] = 0xffffffff ^ Crc32.Combine(0xffffffff, 0, (int)_toc.AudioLength * 588 * 4);
|
||||
_CRCMASK[0] = 0xffffffff ^ Crc32.Combine(0xffffffff, 0, (int)_finalSampleCount * 4);
|
||||
for (int iTrack = 1; iTrack <= _toc.AudioTracks; iTrack++)
|
||||
_CRCMASK[iTrack] = 0xffffffff ^ Crc32.Combine(0xffffffff, 0, (int)_toc[iTrack + _toc.FirstAudio - 1].Length * 588 * 4);
|
||||
Init();
|
||||
@@ -316,30 +318,53 @@ namespace CUETools.AccurateRip
|
||||
_CRCLOG[iTrack] = value;
|
||||
}
|
||||
|
||||
private ushort[,] syndrome = new ushort[1,1];
|
||||
private byte[] parity = new byte[1];
|
||||
private ushort[] expTbl = new ushort[1];
|
||||
private ushort[] logTbl = new ushort[1];
|
||||
private int stride = 1, stridecount, npar;
|
||||
internal ushort[,] syndrome;
|
||||
internal byte[] parity;
|
||||
internal ushort[] leadin;
|
||||
internal ushort[] leadout;
|
||||
private int stride = 1, laststride = 1, stridecount = 1, npar = 1;
|
||||
private bool calcSyn = false;
|
||||
private bool calcParity = false;
|
||||
|
||||
public void CalcSyndrome(ushort[] expTbl, ushort[] logTbl, ushort[,] syndrome, byte[] parity, int stride, int stridecount, int npar, bool calcParity, bool calcSyn)
|
||||
internal void InitCDRepair(int stride, int laststride, int stridecount, int npar, bool calcSyn, bool calcParity)
|
||||
{
|
||||
if (npar != 8)
|
||||
throw new NotSupportedException();
|
||||
this.syndrome = syndrome;
|
||||
this.parity = parity;
|
||||
this.logTbl = logTbl;
|
||||
this.expTbl = expTbl;
|
||||
throw new NotSupportedException("npar != 8");
|
||||
this.stride = stride;
|
||||
this.laststride = laststride;
|
||||
this.stridecount = stridecount;
|
||||
this.npar = npar;
|
||||
this.calcSyn = calcSyn;
|
||||
this.calcParity = calcParity;
|
||||
Init();
|
||||
}
|
||||
|
||||
private unsafe static void CalcSyn8(ushort* exp, ushort* log, ushort* syn, uint lo, uint n)
|
||||
internal unsafe uint CTDBCRC(int actualOffset)
|
||||
{
|
||||
fixed (uint* crct = Crc32.table)
|
||||
{
|
||||
// calculate leadin CRC
|
||||
uint crc0 = 0;
|
||||
for (int off = 0; off < stride - 2 * actualOffset; 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 + 2 * actualOffset - 1; off >= 0; off--)
|
||||
{
|
||||
ushort dd = leadout[off];
|
||||
crc2 = (crc2 >> 8) ^ crct[(byte)(crc2 ^ dd)];
|
||||
crc2 = (crc2 >> 8) ^ crct[(byte)(crc2 ^ (dd >> 8))];
|
||||
}
|
||||
|
||||
return GetCRC32(crc0, (stride - 2 * actualOffset) * 2, crc2, (laststride + 2 * actualOffset) * 2);
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe static void CalcSyn8(ushort* exp, ushort* log, ushort* syn, uint lo, uint n, int npar)
|
||||
{
|
||||
syn[0] ^= (ushort)lo;
|
||||
uint idx = log[lo] + n; syn[1] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
@@ -349,6 +374,17 @@ namespace CUETools.AccurateRip
|
||||
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)];
|
||||
//for (int i = 8; i < npar; i += 8)
|
||||
//{
|
||||
// idx += n; syn[i] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
// idx += n; syn[i + 1] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
// idx += n; syn[i + 2] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
// idx += n; syn[i + 3] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
// idx += n; syn[i + 4] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
// idx += n; syn[i + 5] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
// idx += n; syn[i + 6] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
// idx += n; syn[i + 7] ^= exp[(idx & 0xffff) + (idx >> 16)];
|
||||
//}
|
||||
}
|
||||
|
||||
#if alternateSynCalc
|
||||
@@ -452,7 +488,7 @@ namespace CUETools.AccurateRip
|
||||
int pk = ((int)(lo << 16)) >> 16;
|
||||
peak = Math.Max(peak, (pk << 1) ^ (pk >> 31));
|
||||
|
||||
if (doSyn && lo != 0) CalcSyn8(exp, log, syn + i * 16, lo, n);
|
||||
if (doSyn && lo != 0) CalcSyn8(exp, log, syn + i * 16, lo, n, npar);
|
||||
if (doPar) CalcPar8(exp, log, wr + i * 16, lo);
|
||||
|
||||
uint hi = sample >> 16;
|
||||
@@ -468,7 +504,7 @@ namespace CUETools.AccurateRip
|
||||
pk = ((int)(hi << 16)) >> 16;
|
||||
peak = Math.Max(peak, (pk << 1) ^ (pk >> 31));
|
||||
|
||||
if (doSyn && hi != 0) CalcSyn8(exp, log, syn + i * 16 + 8, hi, n);
|
||||
if (doSyn && hi != 0) CalcSyn8(exp, log, syn + i * 16 + 8, hi, n, npar);
|
||||
if (doPar) CalcPar8(exp, log, wr + i * 16 + 8, hi);
|
||||
}
|
||||
|
||||
@@ -513,7 +549,7 @@ namespace CUETools.AccurateRip
|
||||
|
||||
int pos = 0;
|
||||
fixed (uint* t = Crc32.table)
|
||||
fixed (ushort* exp = expTbl, log = logTbl, synptr1 = syndrome)
|
||||
fixed (ushort* exp = Galois16.instance.ExpTbl, log = Galois16.instance.LogTbl, synptr1 = syndrome)
|
||||
fixed (byte* pSampleBuff = &sampleBuffer.Bytes[0], bpar = parity)
|
||||
while (pos < sampleBuffer.Length)
|
||||
{
|
||||
@@ -527,6 +563,19 @@ namespace CUETools.AccurateRip
|
||||
int currentPart = ((int)_sampleCount * 2) % stride;
|
||||
ushort* synptr = synptr1 + npar * currentPart;
|
||||
ushort* wr = ((ushort*)bpar) + npar * currentPart;
|
||||
int currentStride = ((int)_sampleCount * 2) / stride;
|
||||
|
||||
if (currentStride < 2 && leadin != null)
|
||||
for (int i = 0; i < copyCount * 2; i++)
|
||||
leadin[_sampleCount * 2 + i] = ((ushort*)samples)[i];
|
||||
|
||||
if (currentStride >= stridecount && leadout != null)
|
||||
for (int i = 0; i < copyCount * 2; i++)
|
||||
{
|
||||
int remaining = (int)(_finalSampleCount - _sampleCount) * 2 - i - 1;
|
||||
if (remaining < stride + laststride)
|
||||
leadout[remaining] = ((ushort*)samples)[i];
|
||||
}
|
||||
|
||||
if (currentSector < 10)
|
||||
CalculateCRCs(t, exp, log, synptr, wr, samples, copyCount, currentOffset, currentOffset);
|
||||
@@ -609,6 +658,18 @@ namespace CUETools.AccurateRip
|
||||
_CacheCRCWN = new uint[_toc.AudioTracks + 1, 31 * 588];
|
||||
_CRCNL = new int[_toc.AudioTracks + 1, 31 * 588];
|
||||
_Peak = new int[_toc.AudioTracks + 1];
|
||||
syndrome = new ushort[calcSyn ? stride : 1, npar];
|
||||
parity = new byte[stride * npar * 2];
|
||||
if (calcSyn || calcParity)
|
||||
{
|
||||
leadin = new ushort[stride * 2];
|
||||
leadout = new ushort[stride + laststride];
|
||||
}
|
||||
else
|
||||
{
|
||||
leadin = null;
|
||||
leadout = null;
|
||||
}
|
||||
_currentTrack = 0;
|
||||
Position = _toc[_toc.FirstAudio][0].Start * 588;
|
||||
}
|
||||
@@ -741,11 +802,14 @@ namespace CUETools.AccurateRip
|
||||
|
||||
public long FinalSampleCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return _finalSampleCount;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 0) // != _toc.Length?
|
||||
if (value != _finalSampleCount)
|
||||
throw new Exception("invalid FinalSampleCount");
|
||||
_finalSampleCount = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
642
CUETools.AccurateRip/CDRepair.cs
Normal file
642
CUETools.AccurateRip/CDRepair.cs
Normal file
@@ -0,0 +1,642 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CUETools.CDImage;
|
||||
using CUETools.Codecs;
|
||||
using CUETools.Parity;
|
||||
using CUETools.AccurateRip;
|
||||
|
||||
namespace CUETools.AccurateRip
|
||||
{
|
||||
public class CDRepair
|
||||
{
|
||||
protected int sampleCount;
|
||||
protected int finalSampleCount;
|
||||
internal Galois galois;
|
||||
protected RsDecode rs;
|
||||
//protected uint crc;
|
||||
protected int[] encodeGx;
|
||||
internal int stride;
|
||||
internal int laststride;
|
||||
internal int stridecount;
|
||||
internal int npar;
|
||||
|
||||
public CDRepair(int finalSampleCount, int stride, int npar)
|
||||
{
|
||||
this.npar = npar;
|
||||
this.stride = stride;
|
||||
this.finalSampleCount = finalSampleCount;
|
||||
sampleCount = 0;
|
||||
galois = Galois16.instance;
|
||||
rs = new RsDecode16(npar, galois);
|
||||
//crc32 = new Crc32();
|
||||
//crc = 0xffffffff;
|
||||
encodeGx = galois.makeEncodeGxLog(npar);
|
||||
laststride = stride + (finalSampleCount * 2) % stride;
|
||||
stridecount = (finalSampleCount * 2) / stride - 2; // minus one for leadin and one for leadout
|
||||
if ((finalSampleCount * 2 + stride - 1) / stride + npar > galois.Max)
|
||||
throw new Exception("invalid stride");
|
||||
}
|
||||
|
||||
public CDRepair(CDRepair src)
|
||||
: this(src.finalSampleCount, src.stride, src.npar)
|
||||
{
|
||||
}
|
||||
|
||||
//public unsafe void Write(AudioBuffer sampleBuffer)
|
||||
//{
|
||||
// throw new Exception("unsupported");
|
||||
//}
|
||||
|
||||
//public unsafe void Close()
|
||||
//{
|
||||
// if (sampleCount != finalSampleCount)
|
||||
// throw new Exception("sampleCount != finalSampleCount");
|
||||
//}
|
||||
|
||||
public long FinalSampleCount
|
||||
{
|
||||
set
|
||||
{
|
||||
if (value < 0) // != _toc.Length?
|
||||
throw new Exception("invalid FinalSampleCount");
|
||||
finalSampleCount = (int)value;
|
||||
}
|
||||
}
|
||||
|
||||
public int NPAR
|
||||
{
|
||||
get
|
||||
{
|
||||
return npar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CDRepairEncode : CDRepair
|
||||
{
|
||||
internal bool verify;
|
||||
internal bool encode;
|
||||
protected AccurateRipVerify ar;
|
||||
|
||||
public CDRepairEncode(AccurateRipVerify ar, int stride, int npar, bool verify, bool encode)
|
||||
: base ((int)ar.FinalSampleCount, stride, npar)
|
||||
{
|
||||
this.ar = ar;
|
||||
this.verify = verify;
|
||||
this.encode = encode;
|
||||
|
||||
ar.InitCDRepair(stride, laststride, stridecount, npar, verify, encode);
|
||||
}
|
||||
|
||||
//private unsafe void ProcessStride(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)
|
||||
// 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];
|
||||
|
||||
// //crc = (crc >> 8) ^ crct[(byte)(crc ^ dd)];
|
||||
// //crc = (crc >> 8) ^ crct[(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;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//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++)
|
||||
// {
|
||||
// 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)
|
||||
// {
|
||||
// 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)
|
||||
//{
|
||||
// if (!verify && !encode)
|
||||
// return;
|
||||
|
||||
// sampleBuffer.Prepare(this);
|
||||
|
||||
// if ((sampleBuffer.ByteLength & 1) != 0 || sampleCount + sampleBuffer.Length > finalSampleCount)
|
||||
// throw new Exception("sampleCount > finalSampleCount");
|
||||
|
||||
// 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 (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 == 16)
|
||||
// ProcessStride16(currentStride, currentPart, copyCount, data);
|
||||
// else if (npar != 8)
|
||||
// ProcessStride(currentStride, currentPart, copyCount, data);
|
||||
// }
|
||||
|
||||
// sampleCount += copyCount >> 1;
|
||||
// offs += copyCount >> 1;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
public unsafe CDRepairFix VerifyParity(byte[] parity2, int actualOffset)
|
||||
{
|
||||
return VerifyParity(npar, parity2, 0, parity2.Length, actualOffset);
|
||||
}
|
||||
|
||||
public uint CRC
|
||||
{
|
||||
get
|
||||
{
|
||||
return ar.CTDBCRC(0);
|
||||
}
|
||||
}
|
||||
|
||||
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 (ar.Position != ar.FinalSampleCount)
|
||||
throw new Exception("ar.Position != ar.FinalSampleCount");
|
||||
|
||||
// find offset
|
||||
fixed (byte* par2ptr = &parity2[pos])
|
||||
{
|
||||
ushort* par2 = (ushort*)par2ptr;
|
||||
int* _sigma = stackalloc int[npar];
|
||||
int* _errpos = stackalloc int[npar];
|
||||
int* syn = stackalloc int[npar];
|
||||
bool foundOffset = false;
|
||||
|
||||
for (int allowed_errors = 0; allowed_errors < npar / 2 && !foundOffset; allowed_errors++)
|
||||
{
|
||||
int part2 = 0;
|
||||
ushort* wr = par2 + part2 * npar;
|
||||
|
||||
// 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 err = 0;
|
||||
int part = (part2 + stride - offset * 2) % stride;
|
||||
|
||||
for (int i = 0; i < npar; i++)
|
||||
{
|
||||
int synI = ar.syndrome[part, i];
|
||||
|
||||
// offset < 0
|
||||
if (part < -offset * 2)
|
||||
{
|
||||
synI ^= galois.mulExp(ar.leadin[stride + part], (i * (stridecount - 1)) % galois.Max);
|
||||
synI = ar.leadout[laststride - part - 1] ^ galois.mulExp(synI, i);
|
||||
}
|
||||
// offset > 0
|
||||
if (part >= stride - offset * 2)
|
||||
{
|
||||
synI = galois.divExp(synI ^ ar.leadout[laststride + stride - part - 1], i);
|
||||
synI ^= galois.mulExp(ar.leadin[part], (i * (stridecount - 1)) % galois.Max);
|
||||
}
|
||||
|
||||
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;
|
||||
hasErrors = err_count != 0 || ar.CTDBCRC(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];
|
||||
|
||||
fixed (byte* par = &parity2[pos])
|
||||
fixed (ushort* exp = galois.ExpTbl, log = galois.LogTbl)
|
||||
{
|
||||
int* syn = stackalloc int[npar];
|
||||
int offset = fix.actualOffset;
|
||||
|
||||
for (int part = 0; part < stride; part++)
|
||||
{
|
||||
int part2 = (part + offset * 2 + stride) % stride;
|
||||
ushort* wr = (ushort*)par + part2 * npar;
|
||||
int err = 0;
|
||||
|
||||
for (int i = 0; i < npar; i++)
|
||||
{
|
||||
syn[i] = ar.syndrome[part, i];
|
||||
|
||||
// offset < 0
|
||||
if (part < -offset * 2)
|
||||
{
|
||||
syn[i] ^= galois.mulExp(ar.leadin[stride + part], (i * (stridecount - 1)) % galois.Max);
|
||||
syn[i] = ar.leadout[laststride - part - 1] ^ galois.mulExp(syn[i], i);
|
||||
}
|
||||
// offset > 0
|
||||
if (part >= stride - offset * 2)
|
||||
{
|
||||
syn[i] = galois.divExp(syn[i] ^ ar.leadout[laststride + stride - part - 1], i);
|
||||
syn[i] ^= galois.mulExp(ar.leadin[part], (i * (stridecount - 1)) % galois.Max);
|
||||
}
|
||||
|
||||
//syn[i] = galois.mulExp(syn[i], i * npar);
|
||||
|
||||
for (int j = 0; j < npar; j++)
|
||||
syn[i] = wr[j] ^ galois.mulExp(syn[i], i); // wk = data + wk * α^i
|
||||
|
||||
err |= syn[i];
|
||||
}
|
||||
|
||||
//for (int j = 0; j < npar; j++)
|
||||
// if (wr[j] != 0)
|
||||
// {
|
||||
// ushort* myexp = exp + log[wr[j]];
|
||||
// syn[0] ^= wr[j];
|
||||
// for (int i = 1; i < npar; i++)
|
||||
// syn[i] ^= myexp[(npar - j - 1) * i];
|
||||
// }
|
||||
|
||||
//for (int i = 0; i < npar; i++)
|
||||
// err |= syn[i];
|
||||
|
||||
if (err != 0)
|
||||
{
|
||||
fixed (int* s = &fix.sigma[part, 0], o = &fix.omega[part, 0], e = &fix.errpos[part, 0])
|
||||
{
|
||||
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
|
||||
galois.mulPoly(o, s, syn, npar / 2 + 1, npar, npar);
|
||||
}
|
||||
}
|
||||
else
|
||||
fix.errors[part] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return fix;
|
||||
}
|
||||
|
||||
public byte[] Parity
|
||||
{
|
||||
get
|
||||
{
|
||||
return ar.parity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CDRepairFix : CDRepair, IAudioDest
|
||||
{
|
||||
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[] erroffsorted;
|
||||
internal ushort[] forneysorted;
|
||||
internal int erroffcount;
|
||||
internal int[] errors;
|
||||
private BitArray affectedSectorArray;
|
||||
private int nexterroff;
|
||||
uint crc = 0;
|
||||
|
||||
internal CDRepairFix(CDRepairEncode decode)
|
||||
: base(decode)
|
||||
{
|
||||
}
|
||||
|
||||
public string AffectedSectors
|
||||
{
|
||||
get
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
SortErrors();
|
||||
for (int i = 0; i < erroffcount; i++)
|
||||
{
|
||||
int j;
|
||||
for (j = i + 1; j < erroffcount; j++)
|
||||
if (erroffsorted[j] - erroffsorted[j - 1] > 2 * 588 * 5)
|
||||
break;
|
||||
uint sec1 = (uint)erroffsorted[i] / 2 / 588;
|
||||
uint sec2 = (uint)erroffsorted[j - 1] / 2 / 588;
|
||||
if (sb.Length != 0) sb.Append(",");
|
||||
sb.Append(CDImageLayout.TimeToString(sec1));
|
||||
if (sec1 != sec2) sb.Append("-");
|
||||
if (sec1 != sec2) sb.Append(CDImageLayout.TimeToString(sec2));
|
||||
i = j - 1;
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public BitArray AffectedSectorArray
|
||||
{
|
||||
get
|
||||
{
|
||||
if (affectedSectorArray == null)
|
||||
{
|
||||
affectedSectorArray = new BitArray(finalSampleCount / 588 + 1);
|
||||
SortErrors();
|
||||
for (int i = 0; i < erroffcount; i++)
|
||||
affectedSectorArray[erroffsorted[i] / 2 / 588] = true;
|
||||
}
|
||||
return affectedSectorArray;
|
||||
}
|
||||
}
|
||||
|
||||
private int GetErrOff(int part, int i)
|
||||
{
|
||||
return (2 + galois.toPos(stridecount + npar, errpos[part, i]) - (stride + part + ActualOffset * 2) / stride) * stride + part;
|
||||
}
|
||||
|
||||
private unsafe void SortErrors()
|
||||
{
|
||||
if (erroffsorted != null)
|
||||
return;
|
||||
erroffcount = 0;
|
||||
erroffsorted = new int[errpos.GetLength(0) * errpos.GetLength(1)];
|
||||
forneysorted = new ushort[errpos.GetLength(0) * errpos.GetLength(1)];
|
||||
for (int part = 0; part < stride; part++)
|
||||
{
|
||||
fixed (int* s = &sigma[part, 0], o = &omega[part, 0])
|
||||
for (int i = 0; i < errors[part]; i++)
|
||||
{
|
||||
erroffsorted[erroffcount] = GetErrOff(part, i);
|
||||
if (erroffsorted[erroffcount] >= 0 && erroffsorted[erroffcount] < finalSampleCount * 2)
|
||||
{
|
||||
forneysorted[erroffcount] = (ushort)rs.doForney(errors[part], errpos[part, i], s, o);
|
||||
erroffcount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Array.Sort<int, ushort>(erroffsorted, forneysorted, 0, erroffcount);
|
||||
// assert erroffcount == CorrectableErrors
|
||||
nexterroff = 0;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
SortErrors();
|
||||
|
||||
fixed (byte* bytes = sampleBuffer.Bytes)
|
||||
fixed (uint* t = Crc32.table)
|
||||
{
|
||||
ushort* data = (ushort*)bytes;
|
||||
for (int pos = firstPos; pos < lastPos; pos++)
|
||||
{
|
||||
if (sampleCount * 2 + pos == erroffsorted[nexterroff] && nexterroff < erroffsorted.Length)
|
||||
data[pos] ^= forneysorted[nexterroff++];
|
||||
|
||||
ushort dd = data[pos];
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ dd)];
|
||||
crc = (crc >> 8) ^ t[(byte)(crc ^ (dd >> 8))];
|
||||
}
|
||||
}
|
||||
sampleCount += sampleBuffer.Length;
|
||||
}
|
||||
|
||||
public unsafe void Close()
|
||||
{
|
||||
if (sampleCount != finalSampleCount)
|
||||
throw new Exception("sampleCount != finalSampleCount");
|
||||
}
|
||||
|
||||
public bool HasErrors
|
||||
{
|
||||
get
|
||||
{
|
||||
return hasErrors;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanRecover
|
||||
{
|
||||
get
|
||||
{
|
||||
return canRecover;
|
||||
}
|
||||
}
|
||||
|
||||
public int CorrectableErrors
|
||||
{
|
||||
get
|
||||
{
|
||||
return correctableErrors;
|
||||
}
|
||||
}
|
||||
|
||||
public int ActualOffset
|
||||
{
|
||||
get
|
||||
{
|
||||
return actualOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public uint CRC
|
||||
{
|
||||
get
|
||||
{
|
||||
return 0xffffffff ^ Crc32.Combine(0xffffffff, crc, stride * stridecount * 2);
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
throw new Exception("unsupported");
|
||||
}
|
||||
|
||||
public int CompressionLevel
|
||||
{
|
||||
get { return 0; }
|
||||
set { }
|
||||
}
|
||||
|
||||
public string Options
|
||||
{
|
||||
set
|
||||
{
|
||||
if (value == null || value == "") return;
|
||||
throw new Exception("Unsupported options " + value);
|
||||
}
|
||||
}
|
||||
|
||||
public AudioPCMConfig PCM
|
||||
{
|
||||
get { return AudioPCMConfig.RedBook; }
|
||||
}
|
||||
|
||||
public long BlockSize
|
||||
{
|
||||
set { throw new Exception("unsupported"); }
|
||||
}
|
||||
|
||||
public string Path
|
||||
{
|
||||
get { throw new Exception("unsupported"); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AccurateRip.cs" />
|
||||
<Compile Include="CDRepair.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -55,6 +56,10 @@
|
||||
<Project>{1DD41038-D885-46C5-8DDE-E0B82F066584}</Project>
|
||||
<Name>CUETools.CDImage</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\CUETools.Parity\CUETools.Parity.csproj">
|
||||
<Project>{ECEB839C-171B-4535-958F-9899310A0342}</Project>
|
||||
<Name>CUETools.Parity</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
|
||||
//
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("2.0.7.0")]
|
||||
[assembly: AssemblyFileVersion("2.0.7.0")]
|
||||
[assembly: AssemblyVersion("2.0.8.0")]
|
||||
[assembly: AssemblyFileVersion("2.0.8.0")]
|
||||
|
||||
Reference in New Issue
Block a user