FLACCL: fix incompatibilily with libFLAC on some files with hi-res audio

This commit is contained in:
Grigory Chudov
2013-05-27 22:52:26 -04:00
parent 5d8f8a775b
commit a86b66e7a6
2 changed files with 10 additions and 4 deletions

View File

@@ -679,7 +679,7 @@ void clQuantizeLPC(
{
float lpc = lpcs[lpcOffs + order * 32 + tid];
// quantize coeffs with given shift
int c = convert_int_rte(clamp(lpc * (1 << shift), (float)(-1 << (cbits - 1)), (float)(1 << (cbits - 1))));
int c = convert_int_rte(clamp(lpc * (1 << shift), (float)((-1 << (cbits - 1)) + 1), (float)((1 << (cbits - 1)) - 1)));
// remove sign bits
tmpi |= c ^ (c >> 31);
tasks[taskNo].coefs[tid] = c;
@@ -825,7 +825,11 @@ void clQuantizeLPC(
#else
int cbits = max(3, min(min(13 - minprecision + (i - ((i >> precisions) << precisions)) - (shared.task.blocksize <= 2304) - (shared.task.blocksize <= 1152) - (shared.task.blocksize <= 576), shared.task.abits), clz(order) + 1 - shared.task.obits));
#endif
// calculate shift based on precision and number of leading zeroes in coeffs
// Calculate shift based on precision and number of leading zeroes in coeffs.
// We know that if shifted by 15, coefs require
// 33 - clz(shared.maxcoef[i]) bits;
// So to get the desired cbits, we need to shift coefs by
// 15 + cbits - (33 - clz(shared.maxcoef[i]));
int shift = max(0,min(15, clz(shared.maxcoef[i]) - 18 + cbits));
//cbits = 13;
@@ -834,7 +838,7 @@ void clQuantizeLPC(
//if (shared.task.abits + 32 - clz(order) < shift
//int shift = max(0,min(15, (shared.task.abits >> 2) - 14 + clz(shared.tmpi[tid & ~31]) + ((32 - clz(order))>>1)));
// quantize coeffs with given shift
coef = convert_int_rte(clamp(lpc * (1 << shift), (float)(-1 << (cbits - 1)), (float)(1 << (cbits - 1))));
coef = convert_int_rte(clamp(lpc * (1 << shift), (float)((-1 << (cbits - 1)) + 1), (float)((1 << (cbits - 1)) - 1)));
// error correction
//shared.tmp[tid] = (tid != 0) * (shared.arp[tid - 1]*(1 << shared.task.shift) - shared.task.coefs[tid - 1]);
//shared.task.coefs[tid] = max(-(1 << (shared.task.cbits - 1)), min((1 << (shared.task.cbits - 1))-1, convert_int_rte((shared.arp[tid]) * (1 << shared.task.shift) + shared.tmp[tid])));

View File

@@ -416,7 +416,9 @@ namespace CUETools.Codecs.FLAKE
// LPC coefficients
frame.subframes[ch].best.cbits = (int)bitreader.readbits(4) + 1; // lpc_precision
frame.subframes[ch].best.shift = bitreader.readbits_signed(5);
if (frame.subframes[ch].best.cbits >= 16)
throw new Exception("cbits >= 16");
frame.subframes[ch].best.shift = bitreader.readbits_signed(5);
if (frame.subframes[ch].best.shift < 0)
throw new Exception("negative shift");
for (int i = 0; i < frame.subframes[ch].best.order; i++)