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.
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 void FLAC__lpc_compute_autocorrelation(const real data[], unsigned data_len, unsigned lag, real autoc[])
37 assert(lag <= data_len);
40 for(i = lag, d = 0.0; i < data_len; i++)
41 d += data[i] * data[i - lag];
46 void FLAC__lpc_compute_lp_coefficients(const real autoc[], unsigned max_order, real lp_coeff[][FLAC__MAX_LPC_ORDER], real error[])
49 real r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
51 assert(0 < max_order);
52 assert(max_order <= FLAC__MAX_LPC_ORDER);
53 assert(autoc[0] != 0.0);
57 for(i = 0; i < max_order; i++) {
58 /* Sum up this iteration's reflection coefficient. */
60 for(j = 0; j < i; j++)
61 r -= lpc[j] * autoc[i-j];
64 /* Update LPC coefficients and total error. */
66 for(j = 0; j < (i>>1); j++) {
68 lpc[j] += r * lpc[i-1-j];
69 lpc[i-1-j] += r * tmp;
77 for(j = 0; j <= i; j++)
78 lp_coeff[i][j] = -lpc[j]; /* N.B. why do we have to negate here? */
84 int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
87 real d, rprecision = (real)precision, maxlog = -1e99, minlog = 1e99;
89 assert(bits_per_sample > 0);
90 assert(bits_per_sample <= sizeof(int32)*8);
91 assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
92 assert(precision + bits_per_sample < sizeof(int32)*8);
94 (void)bits_per_sample; /* silence compiler warning about unused parameter */
97 for(i = 0; i < order; i++) {
98 if(lp_coeff[i] == 0.0)
100 d = log(fabs(lp_coeff[i])) / M_LN2;
108 else if(maxlog - minlog >= (real)(precision+1))
110 else if((rprecision-1.0) - maxlog >= (real)(precision+1))
111 rprecision = (real)precision + maxlog + 1.0;
113 *shift = (int)floor((rprecision-1.0) - maxlog); /* '-1' because *shift can be negative and the sign bit costs 1 bit */
114 if(*shift > (int)precision || *shift <= -(int)precision) {
115 fprintf(stderr, "@@@ FLAC__lpc_quantize_coefficients(): ERROR: *shift=%d, maxlog=%f, minlog=%f, precision=%u, rprecision=%f\n", *shift, maxlog, minlog, precision, rprecision);
119 if(*shift != 0) { /* just to avoid wasting time... */
120 for(i = 0; i < order; i++)
121 qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
127 int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
130 real d, cmax = -1e99;//@@@, cmin = 1e99;
132 assert(bits_per_sample > 0);
133 assert(bits_per_sample <= sizeof(int32)*8);
134 assert(precision > 0);
135 assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
136 assert(precision + bits_per_sample < sizeof(int32)*8);
138 (void)bits_per_sample; /* silence compiler warning about unused parameter */
141 /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
144 for(i = 0; i < order; i++) {
145 if(lp_coeff[i] == 0.0)
147 d = fabs(lp_coeff[i]);
153 //@@@ if(cmax < cmin)
155 /* => coeffients are all 0, which means our constant-detect didn't work */
156 fprintf(stderr,"@@@ LPCQ ERROR, all lpc_coeffs are 0\n");
160 //@@@ const int minshift = (int)precision - floor(log(cmin) / M_LN2) - 1;
161 const int maxshift = (int)precision - floor(log(cmax) / M_LN2) - 1;
162 //@@@ assert(maxshift >= minshift);
163 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
164 const int min_shiftlimit = -max_shiftlimit - 1;
168 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
169 fprintf(stderr,"@@@ LPCQ ERROR, shift is outside shiftlimit\n");
174 if(*shift != 0) { /* just to avoid wasting time... */
175 for(i = 0; i < order; i++)
176 qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
181 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[])
183 #ifdef FLAC_OVERFLOW_DETECT
188 const int32 *history;
190 #ifdef FLAC_OVERFLOW_DETECT_VERBOSE
191 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
193 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
194 fprintf(stderr,"\n");
198 for(i = 0; i < data_len; i++) {
199 #ifdef FLAC_OVERFLOW_DETECT
204 for(j = 0; j < order; j++) {
205 sum += qlp_coeff[j] * (*(--history));
206 #ifdef FLAC_OVERFLOW_DETECT
207 sumo += (int64)qlp_coeff[j] * (int64)(*history);
208 if(sumo > 2147483647ll || sumo < -2147483648ll) {
209 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);
213 *(residual++) = *(data++) - (sum >> lp_quantization);
216 /* Here's a slower but clearer version:
217 for(i = 0; i < data_len; i++) {
219 for(j = 0; j < order; j++)
220 sum += qlp_coeff[j] * data[i-j-1];
221 residual[i] = data[i] - (sum >> lp_quantization);
226 void FLAC__lpc_restore_signal(const int32 residual[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 data[])
228 #ifdef FLAC_OVERFLOW_DETECT
233 const int32 *history;
235 #ifdef FLAC_OVERFLOW_DETECT_VERBOSE
236 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
238 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
239 fprintf(stderr,"\n");
243 for(i = 0; i < data_len; i++) {
244 #ifdef FLAC_OVERFLOW_DETECT
249 for(j = 0; j < order; j++) {
250 sum += qlp_coeff[j] * (*(--history));
251 #ifdef FLAC_OVERFLOW_DETECT
252 sumo += (int64)qlp_coeff[j] * (int64)(*history);
253 if(sumo > 2147483647ll || sumo < -2147483648ll) {
254 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);
258 *(data++) = *(residual++) + (sum >> lp_quantization);
261 /* Here's a slower but clearer version:
262 for(i = 0; i < data_len; i++) {
264 for(j = 0; j < order; j++)
265 sum += qlp_coeff[j] * data[i-j-1];
266 data[i] = residual[i] + (sum >> lp_quantization);
271 real FLAC__lpc_compute_expected_bits_per_residual_sample(real lpc_error, unsigned total_samples)
275 assert(lpc_error >= 0.0); /* the error can never be negative */
276 assert(total_samples > 0);
278 escale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
280 if(lpc_error > 0.0) {
281 real bps = 0.5 * log(escale * lpc_error) / M_LN2;
292 unsigned FLAC__lpc_compute_best_order(const real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
294 unsigned order, best_order;
295 real best_bits, tmp_bits;
297 assert(max_order > 0);
300 best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[0], total_samples) * (real)total_samples;
302 for(order = 1; order < max_order; order++) {
303 tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[order], total_samples) * (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 */