1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "FLAC/assert.h"
38 #include "FLAC/format.h"
39 #include "private/bitmath.h"
40 #include "private/lpc.h"
41 #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
45 #ifndef FLAC__INTEGER_ONLY_LIBRARY
48 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
49 #define M_LN2 0.69314718055994530942
52 void FLAC__lpc_window_data(const FLAC__real in[], const FLAC__real window[], FLAC__real out[], unsigned data_len)
55 for(i = 0; i < data_len; i++)
56 out[i] = in[i] * window[i];
59 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
61 /* a readable, but slower, version */
66 FLAC__ASSERT(lag > 0);
67 FLAC__ASSERT(lag <= data_len);
70 * Technically we should subtract the mean first like so:
71 * for(i = 0; i < data_len; i++)
73 * but it appears not to make enough of a difference to matter, and
74 * most signals are already closely centered around zero
77 for(i = lag, d = 0.0; i < data_len; i++)
78 d += data[i] * data[i - lag];
84 * this version tends to run faster because of better data locality
85 * ('data_len' is usually much larger than 'lag')
88 unsigned sample, coeff;
89 const unsigned limit = data_len - lag;
91 FLAC__ASSERT(lag > 0);
92 FLAC__ASSERT(lag <= data_len);
94 for(coeff = 0; coeff < lag; coeff++)
96 for(sample = 0; sample <= limit; sample++) {
98 for(coeff = 0; coeff < lag; coeff++)
99 autoc[coeff] += d * data[sample+coeff];
101 for(; sample < data_len; sample++) {
103 for(coeff = 0; coeff < data_len - sample; coeff++)
104 autoc[coeff] += d * data[sample+coeff];
108 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
111 FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
113 FLAC__ASSERT(0 != max_order);
114 FLAC__ASSERT(0 < *max_order);
115 FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER);
116 FLAC__ASSERT(autoc[0] != 0.0);
120 for(i = 0; i < *max_order; i++) {
121 /* Sum up this iteration's reflection coefficient. */
123 for(j = 0; j < i; j++)
124 r -= lpc[j] * autoc[i-j];
127 /* Update LPC coefficients and total error. */
129 for(j = 0; j < (i>>1); j++) {
130 FLAC__double tmp = lpc[j];
131 lpc[j] += r * lpc[i-1-j];
132 lpc[i-1-j] += r * tmp;
135 lpc[j] += lpc[j] * r;
137 err *= (1.0 - r * r);
139 /* save this order */
140 for(j = 0; j <= i; j++)
141 lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
144 /* see SF bug #1601812 http://sourceforge.net/tracker/index.php?func=detail&aid=1601812&group_id=13478&atid=113478 */
152 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
156 FLAC__int32 qmax, qmin;
158 FLAC__ASSERT(precision > 0);
159 FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
161 /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
163 qmax = 1 << precision;
167 /* calc cmax = max( |lp_coeff[i]| ) */
169 for(i = 0; i < order; i++) {
170 const FLAC__double d = fabs(lp_coeff[i]);
176 /* => coefficients are all 0, which means our constant-detect didn't work */
180 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
181 const int min_shiftlimit = -max_shiftlimit - 1;
184 (void)frexp(cmax, &log2cmax);
186 *shift = (int)precision - log2cmax - 1;
188 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
189 #ifdef FLAC__OVERFLOW_DETECT
190 fprintf(stderr,"FLAC__lpc_quantize_coefficients: shift out of limit: shift=%d cmax=%f precision=%u\n",q,qmax,*shift,cmax,precision+1);
193 /*@@@ this does not seem to help at all, but was not extensively tested either: */
194 if(*shift > max_shiftlimit)
195 *shift = max_shiftlimit;
203 FLAC__double error = 0.0;
205 for(i = 0; i < order; i++) {
206 error += lp_coeff[i] * (1 << *shift);
207 q = lround(error); /* round() is also suitable */
208 #ifdef FLAC__OVERFLOW_DETECT
210 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
212 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
222 /* negative shift is very rare but due to design flaw, negative shift is
223 * a NOP in the decoder, so it must be handled specially by scaling down
227 const int nshift = -(*shift);
228 FLAC__double error = 0.0;
231 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
233 for(i = 0; i < order; i++) {
234 error += lp_coeff[i] / (1 << nshift);
235 q = lround(error); /* round() is also suitable */
236 #ifdef FLAC__OVERFLOW_DETECT
238 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
240 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
255 void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
257 #ifdef FLAC__OVERFLOW_DETECT
262 const FLAC__int32 *history;
264 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
265 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
267 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
268 fprintf(stderr,"\n");
270 FLAC__ASSERT(order > 0);
272 for(i = 0; i < data_len; i++) {
273 #ifdef FLAC__OVERFLOW_DETECT
278 for(j = 0; j < order; j++) {
279 sum += qlp_coeff[j] * (*(--history));
280 #ifdef FLAC__OVERFLOW_DETECT
281 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
283 if(sumo > 2147483647I64 || sumo < -2147483648I64)
284 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
286 if(sumo > 2147483647ll || sumo < -2147483648ll)
287 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
291 *(residual++) = *(data++) - (sum >> lp_quantization);
294 /* Here's a slower but clearer version:
295 for(i = 0; i < data_len; i++) {
297 for(j = 0; j < order; j++)
298 sum += qlp_coeff[j] * data[i-j-1];
299 residual[i] = data[i] - (sum >> lp_quantization);
304 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
308 const FLAC__int32 *history;
310 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
311 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
313 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
314 fprintf(stderr,"\n");
316 FLAC__ASSERT(order > 0);
318 for(i = 0; i < data_len; i++) {
321 for(j = 0; j < order; j++)
322 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
323 #ifdef FLAC__OVERFLOW_DETECT
324 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
325 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
328 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
329 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization)));
333 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
337 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
339 void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
341 #ifdef FLAC__OVERFLOW_DETECT
346 const FLAC__int32 *r = residual, *history;
348 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
349 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
351 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
352 fprintf(stderr,"\n");
354 FLAC__ASSERT(order > 0);
356 for(i = 0; i < data_len; i++) {
357 #ifdef FLAC__OVERFLOW_DETECT
362 for(j = 0; j < order; j++) {
363 sum += qlp_coeff[j] * (*(--history));
364 #ifdef FLAC__OVERFLOW_DETECT
365 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
367 if(sumo > 2147483647I64 || sumo < -2147483648I64)
368 fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
370 if(sumo > 2147483647ll || sumo < -2147483648ll)
371 fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
375 *(data++) = *(r++) + (sum >> lp_quantization);
378 /* Here's a slower but clearer version:
379 for(i = 0; i < data_len; i++) {
381 for(j = 0; j < order; j++)
382 sum += qlp_coeff[j] * data[i-j-1];
383 data[i] = residual[i] + (sum >> lp_quantization);
388 void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
392 const FLAC__int32 *r = residual, *history;
394 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
395 fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
397 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
398 fprintf(stderr,"\n");
400 FLAC__ASSERT(order > 0);
402 for(i = 0; i < data_len; i++) {
405 for(j = 0; j < order; j++)
406 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
407 #ifdef FLAC__OVERFLOW_DETECT
408 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
409 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
412 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
413 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization)));
417 *(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization);
421 #ifndef FLAC__INTEGER_ONLY_LIBRARY
423 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples)
425 FLAC__double error_scale;
427 FLAC__ASSERT(total_samples > 0);
429 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
431 return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
434 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale)
436 if(lpc_error > 0.0) {
437 FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2;
443 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */
451 unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
453 unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */
454 FLAC__double bits, best_bits, error_scale;
456 FLAC__ASSERT(max_order > 0);
457 FLAC__ASSERT(total_samples > 0);
459 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
462 best_bits = (unsigned)(-1);
464 for(index = 0, order = 1; index < max_order; index++, order++) {
465 bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order);
466 if(bits < best_bits) {
472 return best_index+1; /* +1 since index of lpc_error[] is order-1 */
475 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */