Exemplo n.º 1
0
        public unsafe CDRepairFix VerifyParity(ushort[,] syn2, uint crc, int actualOffset)
		{
            int npar2 = syn2.GetLength(1);
            int npar = Math.Min(AccurateRipVerify.maxNpar, npar2);
            var erroff = new int[stride * npar / 2];
            var forney = new ushort[stride * npar / 2];
            var syn1 = ar.GetSyndrome(npar, -1, -actualOffset);
            var rs = new RsDecode16(npar, this.galois);
            CDRepairFix fix = new CDRepairFix(this, npar);

            fix.actualOffset = actualOffset;
            fix.correctableErrors = 0;
            fix.hasErrors = false;
            fix.canRecover = true;

			fixed (ushort *psyn2 = syn2, psyn1 = syn1)
			{
                int sfLen = npar / 2 + 2;
                int ofLen = npar / 2 + 1;
                int efLen = npar / 2;
                int* _sigma = stackalloc int[npar / 2 + 2];
                int* _omega = stackalloc int[npar / 2 + 1];
                int* _errpos = stackalloc int[npar / 2];
                int* syn = stackalloc int[npar];
				int offset = fix.actualOffset;

				for (int part2 = 0; part2 < stride; part2++)
				{
					ushort* syn1part = psyn1 + part2 * npar;
                    ushort* syn2part = psyn2 + part2 * npar;
					int err = 0;

					for (int i = 0; i < npar; i++)
					{
                        var synI = syn1part[i] ^ syn2part[i];
                        syn[i] = synI;
                        err |= synI;
					}

                    if (err != 0)
                    {
                        int errcount = rs.calcSigmaMBM(_sigma, syn);
                        fix.hasErrors = true;
                        if (errcount <= 0 || errcount > efLen || !rs.chienSearch(_errpos, stridecount, errcount, _sigma))
                        {
                            fix.canRecover = false;
                            return fix;
                        }

                        galois.mulPoly(_omega, _sigma, syn, ofLen, sfLen, npar);

                        for (int i = 0; i < errcount; i++)
                        {
                            int pos = galois.toPos(stridecount, _errpos[i]) * stride + part2;
                            int erroffi = stride + pos + pregap * 2 - actualOffset * 2;
                            ushort diff = (ushort)this.galois.doForney(errcount, _errpos[i], _sigma, _omega);
                            if (erroffi < pregap * 2 || erroffi >= finalSampleCount * 2)
                            {
                                fix.canRecover = false;
                                return fix;
                            }
                            crc ^= Crc32.Combine(Crc32.ComputeChecksum(Crc32.ComputeChecksum(0, (byte)diff), (byte)(diff >> 8)), 0, (stridecount * stride - pos - 1) * 2);
                            erroff[fix.correctableErrors] = erroffi;
                            forney[fix.correctableErrors] = diff;
                            fix.correctableErrors++;
                        }
                    }
				}

                crc ^= ar.CTDBCRC(-actualOffset);
                if (crc != 0)
                {
                    fix.canRecover = false;
                    return fix;
                }
			}

            fix.erroffsorted = new int[fix.correctableErrors];
            fix.forneysorted = new ushort[fix.correctableErrors];
            for (int i = 0; i < fix.correctableErrors; i++)
            {
                fix.erroffsorted[i] = erroff[i];
                fix.forneysorted[i] = forney[i];
            }
            Array.Sort<int, ushort>(fix.erroffsorted, fix.forneysorted, 0, fix.correctableErrors);
            return fix;
		}
Exemplo n.º 2
0
        public unsafe bool FindOffset(ushort[,] syn2, uint expectedCRC, out int actualOffset, out bool hasErrors)
        {
            int npar2 = syn2.GetLength(1);
            int npar = Math.Min(AccurateRipVerify.maxNpar, npar2);

            if (npar2 != npar)
                throw new Exception("npar mismatch");
            if (ar.Position != ar.FinalSampleCount)
                throw new Exception("ar.Position != ar.FinalSampleCount");

            var rs = new RsDecode16(npar, this.galois);
            fixed (ushort* syn2part = syn2)
            {
                int* _sigma = stackalloc int[npar];
                int* _errpos = stackalloc int[npar];
                int* syn = stackalloc int[npar];
                int bestOffset = 0;
                int bestOffsetErrors = npar / 2;

                // fast search
                for (int offset = 1 - stride / 2; offset < stride / 2; offset++)
                {
                    var syn1 = ar.GetSyndrome(npar, 1, -offset);
                    int err = 0;
                    for (int i = 0; i < npar; i++)
                    {
                        int synI = syn1[0, i] ^ syn2part[i];
                        syn[i] = synI;
                        err |= synI;
                    }
                    if (err == 0)
                    {
                        actualOffset = offset;
                        hasErrors = ar.CTDBCRC(-offset) != expectedCRC;
                        return true;
                    }
                    int err_count = rs.calcSigmaMBM(_sigma, syn);
                    if (err_count > 0 && err_count < bestOffsetErrors && rs.chienSearch(_errpos, stridecount, err_count, _sigma))
                    {
                        bestOffset = offset;
                        bestOffsetErrors = err_count;
                    }
                }

                if (bestOffsetErrors < npar / 2)
                {
                    actualOffset = bestOffset;
                    hasErrors = true;
                    return true;
                }
            }
            actualOffset = 0;
            hasErrors = true;
            return false;
        }