From a86b66e7a6cb0287030b7dffabf868d93f82f4f5 Mon Sep 17 00:00:00 2001 From: Grigory Chudov Date: Mon, 27 May 2013 22:52:26 -0400 Subject: [PATCH] FLACCL: fix incompatibilily with libFLAC on some files with hi-res audio --- CUETools.Codecs.FLACCL/flac.cl | 10 +++++++--- CUETools.Codecs.FLAKE/FlakeReader.cs | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CUETools.Codecs.FLACCL/flac.cl b/CUETools.Codecs.FLACCL/flac.cl index 8375ca5..54cebdf 100644 --- a/CUETools.Codecs.FLACCL/flac.cl +++ b/CUETools.Codecs.FLACCL/flac.cl @@ -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]))); diff --git a/CUETools.Codecs.FLAKE/FlakeReader.cs b/CUETools.Codecs.FLAKE/FlakeReader.cs index c4b4f9d..256d64e 100644 --- a/CUETools.Codecs.FLAKE/FlakeReader.cs +++ b/CUETools.Codecs.FLAKE/FlakeReader.cs @@ -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++)