1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "FLAC/assert.h"
23 #include "FLAC/format.h"
24 #include "private/lpc.h"
27 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
28 #define M_LN2 0.69314718055994530942
31 #define LOCAL_FABS(x) ((x)<0.0? -(x):(x))
33 void FLAC__lpc_compute_autocorrelation(const real data[], unsigned data_len, unsigned lag, real autoc[])
35 /* a readable, but slower, version */
40 FLAC__ASSERT(lag > 0);
41 FLAC__ASSERT(lag <= data_len);
44 for(i = lag, d = 0.0; i < data_len; i++)
45 d += data[i] * data[i - lag];
51 * this version tends to run faster because of better data locality
52 * ('data_len' is usually much larger than 'lag')
55 unsigned sample, coeff;
56 const unsigned limit = data_len - lag;
58 FLAC__ASSERT(lag > 0);
59 FLAC__ASSERT(lag <= data_len);
61 for(coeff = 0; coeff < lag; coeff++)
63 for(sample = 0; sample <= limit; sample++) {
65 for(coeff = 0; coeff < lag; coeff++)
66 autoc[coeff] += d * data[sample+coeff];
68 for(; sample < data_len; sample++) {
70 for(coeff = 0; coeff < data_len - sample; coeff++)
71 autoc[coeff] += d * data[sample+coeff];
75 void FLAC__lpc_compute_lp_coefficients(const real autoc[], unsigned max_order, real lp_coeff[][FLAC__MAX_LPC_ORDER], real error[])
78 real r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
80 FLAC__ASSERT(0 < max_order);
81 FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
82 FLAC__ASSERT(autoc[0] != 0.0);
86 for(i = 0; i < max_order; i++) {
87 /* Sum up this iteration's reflection coefficient. */
89 for(j = 0; j < i; j++)
90 r -= lpc[j] * autoc[i-j];
93 /* Update LPC coefficients and total error. */
95 for(j = 0; j < (i>>1); j++) {
97 lpc[j] += r * lpc[i-1-j];
98 lpc[i-1-j] += r * tmp;
101 lpc[j] += lpc[j] * r;
103 err *= (1.0 - r * r);
105 /* save this order */
106 for(j = 0; j <= i; j++)
107 lp_coeff[i][j] = -lpc[j]; /* negate FIR filter coeff to get predictor coeff */
112 int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
115 real d, cmax = -1e32;
117 FLAC__ASSERT(bits_per_sample > 0);
118 FLAC__ASSERT(bits_per_sample <= sizeof(int32)*8);
119 FLAC__ASSERT(precision > 0);
120 FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
121 FLAC__ASSERT(precision + bits_per_sample < sizeof(int32)*8);
123 (void)bits_per_sample; /* silence compiler warning about unused parameter */
126 /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
129 for(i = 0; i < order; i++) {
130 if(lp_coeff[i] == 0.0)
132 d = LOCAL_FABS(lp_coeff[i]);
137 /* => coefficients are all 0, which means our constant-detect didn't work */
141 const int maxshift = (int)precision - (int)floor(log(cmax) / M_LN2) - 1;
142 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
143 const int min_shiftlimit = -max_shiftlimit - 1;
147 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
152 if(*shift != 0) { /* just to avoid wasting time... */
153 for(i = 0; i < order; i++)
154 qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
159 void FLAC__lpc_compute_residual_from_qlp_coefficients(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[])
161 #ifdef FLAC__OVERFLOW_DETECT
166 const int32 *history;
168 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
169 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
171 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
172 fprintf(stderr,"\n");
174 FLAC__ASSERT(order > 0);
176 for(i = 0; i < data_len; i++) {
177 #ifdef FLAC__OVERFLOW_DETECT
182 for(j = 0; j < order; j++) {
183 sum += qlp_coeff[j] * (*(--history));
184 #ifdef FLAC__OVERFLOW_DETECT
185 sumo += (int64)qlp_coeff[j] * (int64)(*history);
186 if(sumo > 2147483647ll || sumo < -2147483648ll) {
187 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);
191 *(residual++) = *(data++) - (sum >> lp_quantization);
194 /* Here's a slower but clearer version:
195 for(i = 0; i < data_len; i++) {
197 for(j = 0; j < order; j++)
198 sum += qlp_coeff[j] * data[i-j-1];
199 residual[i] = data[i] - (sum >> lp_quantization);
204 void FLAC__lpc_restore_signal(const int32 residual[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 data[])
206 #ifdef FLAC__OVERFLOW_DETECT
211 const int32 *history;
213 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
214 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
216 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
217 fprintf(stderr,"\n");
219 FLAC__ASSERT(order > 0);
221 for(i = 0; i < data_len; i++) {
222 #ifdef FLAC__OVERFLOW_DETECT
227 for(j = 0; j < order; j++) {
228 sum += qlp_coeff[j] * (*(--history));
229 #ifdef FLAC__OVERFLOW_DETECT
230 sumo += (int64)qlp_coeff[j] * (int64)(*history);
231 if(sumo > 2147483647ll || sumo < -2147483648ll) {
232 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);
236 *(data++) = *(residual++) + (sum >> lp_quantization);
239 /* Here's a slower but clearer version:
240 for(i = 0; i < data_len; i++) {
242 for(j = 0; j < order; j++)
243 sum += qlp_coeff[j] * data[i-j-1];
244 data[i] = residual[i] + (sum >> lp_quantization);
249 real FLAC__lpc_compute_expected_bits_per_residual_sample(real lpc_error, unsigned total_samples)
253 FLAC__ASSERT(total_samples > 0);
255 error_scale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
257 if(lpc_error > 0.0) {
258 real bps = 0.5 * log(error_scale * lpc_error) / M_LN2;
264 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
272 real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(real lpc_error, real error_scale)
274 if(lpc_error > 0.0) {
275 real bps = 0.5 * log(error_scale * lpc_error) / M_LN2;
281 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
289 unsigned FLAC__lpc_compute_best_order(const real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
291 unsigned order, best_order;
292 real best_bits, tmp_bits, error_scale;
294 FLAC__ASSERT(max_order > 0);
295 FLAC__ASSERT(total_samples > 0);
297 error_scale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
300 best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (real)total_samples;
302 for(order = 1; order < max_order; order++) {
303 tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (real)(total_samples - order) + (real)(order * bits_per_signal_sample);
304 if(tmp_bits < best_bits) {
306 best_bits = tmp_bits;
310 return best_order+1; /* +1 since index of lpc_error[] is order-1 */