1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004 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.
33 #include "FLAC/assert.h"
34 #include "FLAC/format.h"
35 #include "private/bitmath.h"
36 #include "private/lpc.h"
37 #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
42 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
43 #define M_LN2 0.69314718055994530942
46 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
48 /* a readable, but slower, version */
53 FLAC__ASSERT(lag > 0);
54 FLAC__ASSERT(lag <= data_len);
57 for(i = lag, d = 0.0; i < data_len; i++)
58 d += data[i] * data[i - lag];
64 * this version tends to run faster because of better data locality
65 * ('data_len' is usually much larger than 'lag')
68 unsigned sample, coeff;
69 const unsigned limit = data_len - lag;
71 FLAC__ASSERT(lag > 0);
72 FLAC__ASSERT(lag <= data_len);
74 for(coeff = 0; coeff < lag; coeff++)
76 for(sample = 0; sample <= limit; sample++) {
78 for(coeff = 0; coeff < lag; coeff++)
79 autoc[coeff] += d * data[sample+coeff];
81 for(; sample < data_len; sample++) {
83 for(coeff = 0; coeff < data_len - sample; coeff++)
84 autoc[coeff] += d * data[sample+coeff];
88 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
91 FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
93 FLAC__ASSERT(0 < max_order);
94 FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
95 FLAC__ASSERT(autoc[0] != 0.0);
99 for(i = 0; i < max_order; i++) {
100 /* Sum up this iteration's reflection coefficient. */
102 for(j = 0; j < i; j++)
103 r -= lpc[j] * autoc[i-j];
106 /* Update LPC coefficients and total error. */
108 for(j = 0; j < (i>>1); j++) {
109 FLAC__double tmp = lpc[j];
110 lpc[j] += r * lpc[i-1-j];
111 lpc[i-1-j] += r * tmp;
114 lpc[j] += lpc[j] * r;
116 err *= (1.0 - r * r);
118 /* save this order */
119 for(j = 0; j <= i; j++)
120 lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
125 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
128 FLAC__double d, cmax = -1e32;
129 FLAC__int32 qmax, qmin;
130 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
131 const int min_shiftlimit = -max_shiftlimit - 1;
133 FLAC__ASSERT(precision > 0);
134 FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
136 /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
138 qmax = 1 << precision;
142 for(i = 0; i < order; i++) {
143 if(lp_coeff[i] == 0.0)
145 d = fabs(lp_coeff[i]);
151 /* => coefficients are all 0, which means our constant-detect didn't work */
157 (void)frexp(cmax, &log2cmax);
159 *shift = (int)precision - log2cmax - 1;
161 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
163 /*@@@ this does not seem to help at all, but was not extensively tested either: */
164 if(*shift > max_shiftlimit)
165 *shift = max_shiftlimit;
173 for(i = 0; i < order; i++) {
174 qlp_coeff[i] = (FLAC__int32)floor((FLAC__double)lp_coeff[i] * (FLAC__double)(1 << *shift));
176 /* double-check the result */
177 if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
178 #ifdef FLAC__OVERFLOW_DETECT
179 fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (FLAC__double)lp_coeff[i] * (FLAC__double)(1 << *shift), floor((FLAC__double)lp_coeff[i] * (FLAC__double)(1 << *shift)));
186 else { /* (*shift < 0) */
187 const int nshift = -(*shift);
189 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
191 for(i = 0; i < order; i++) {
192 qlp_coeff[i] = (FLAC__int32)floor((FLAC__double)lp_coeff[i] / (FLAC__double)(1 << nshift));
194 /* double-check the result */
195 if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
196 #ifdef FLAC__OVERFLOW_DETECT
197 fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (FLAC__double)lp_coeff[i] / (FLAC__double)(1 << nshift), floor((FLAC__double)lp_coeff[i] / (FLAC__double)(1 << nshift)));
208 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[])
210 #ifdef FLAC__OVERFLOW_DETECT
215 const FLAC__int32 *history;
217 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
218 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
220 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
221 fprintf(stderr,"\n");
223 FLAC__ASSERT(order > 0);
225 for(i = 0; i < data_len; i++) {
226 #ifdef FLAC__OVERFLOW_DETECT
231 for(j = 0; j < order; j++) {
232 sum += qlp_coeff[j] * (*(--history));
233 #ifdef FLAC__OVERFLOW_DETECT
234 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
236 if(sumo > 2147483647I64 || sumo < -2147483648I64)
237 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);
239 if(sumo > 2147483647ll || sumo < -2147483648ll)
240 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,sumo);
244 *(residual++) = *(data++) - (sum >> lp_quantization);
247 /* Here's a slower but clearer version:
248 for(i = 0; i < data_len; i++) {
250 for(j = 0; j < order; j++)
251 sum += qlp_coeff[j] * data[i-j-1];
252 residual[i] = data[i] - (sum >> lp_quantization);
257 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[])
261 const FLAC__int32 *history;
263 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
264 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
266 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
267 fprintf(stderr,"\n");
269 FLAC__ASSERT(order > 0);
271 for(i = 0; i < data_len; i++) {
274 for(j = 0; j < order; j++)
275 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
276 #ifdef FLAC__OVERFLOW_DETECT
277 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
278 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
281 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
282 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization));
286 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
290 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[])
292 #ifdef FLAC__OVERFLOW_DETECT
297 const FLAC__int32 *history;
299 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
300 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
302 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
303 fprintf(stderr,"\n");
305 FLAC__ASSERT(order > 0);
307 for(i = 0; i < data_len; i++) {
308 #ifdef FLAC__OVERFLOW_DETECT
313 for(j = 0; j < order; j++) {
314 sum += qlp_coeff[j] * (*(--history));
315 #ifdef FLAC__OVERFLOW_DETECT
316 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
318 if(sumo > 2147483647I64 || sumo < -2147483648I64)
319 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);
321 if(sumo > 2147483647ll || sumo < -2147483648ll)
322 fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,sumo);
326 *(data++) = *(residual++) + (sum >> lp_quantization);
329 /* Here's a slower but clearer version:
330 for(i = 0; i < data_len; i++) {
332 for(j = 0; j < order; j++)
333 sum += qlp_coeff[j] * data[i-j-1];
334 data[i] = residual[i] + (sum >> lp_quantization);
339 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[])
343 const FLAC__int32 *history;
345 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
346 fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
348 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
349 fprintf(stderr,"\n");
351 FLAC__ASSERT(order > 0);
353 for(i = 0; i < data_len; i++) {
356 for(j = 0; j < order; j++)
357 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
358 #ifdef FLAC__OVERFLOW_DETECT
359 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
360 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
363 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*residual) + (sum >> lp_quantization)) > 32) {
364 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *residual, sum >> lp_quantization, (FLAC__int64)(*residual) + (sum >> lp_quantization));
368 *(data++) = *(residual++) + (FLAC__int32)(sum >> lp_quantization);
372 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples)
374 FLAC__double error_scale;
376 FLAC__ASSERT(total_samples > 0);
378 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
380 return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
383 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale)
385 if(lpc_error > 0.0) {
386 FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2;
392 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */
400 unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
402 unsigned order, best_order;
403 FLAC__double best_bits, tmp_bits, error_scale;
405 FLAC__ASSERT(max_order > 0);
406 FLAC__ASSERT(total_samples > 0);
408 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
411 best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__double)total_samples;
413 for(order = 1; order < max_order; order++) {
414 tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * bits_per_signal_sample);
415 if(tmp_bits < best_bits) {
417 best_bits = tmp_bits;
421 return best_order+1; /* +1 since index of lpc_error[] is order-1 */