remove reference to obsolete Makefile.vc
[platform/upstream/flac.git] / src / libFLAC / lpc.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <math.h>
21 #include "FLAC/assert.h"
22 #include "FLAC/format.h"
23 #include "private/bitmath.h"
24 #include "private/lpc.h"
25 #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
26 #include <stdio.h>
27 #endif
28
29 #ifndef M_LN2
30 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
31 #define M_LN2 0.69314718055994530942
32 #endif
33
34 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
35 {
36         /* a readable, but slower, version */
37 #if 0
38         FLAC__real d;
39         unsigned i;
40
41         FLAC__ASSERT(lag > 0);
42         FLAC__ASSERT(lag <= data_len);
43
44         while(lag--) {
45                 for(i = lag, d = 0.0; i < data_len; i++)
46                         d += data[i] * data[i - lag];
47                 autoc[lag] = d;
48         }
49 #endif
50
51         /*
52          * this version tends to run faster because of better data locality
53          * ('data_len' is usually much larger than 'lag')
54          */
55         FLAC__real d;
56         unsigned sample, coeff;
57         const unsigned limit = data_len - lag;
58
59         FLAC__ASSERT(lag > 0);
60         FLAC__ASSERT(lag <= data_len);
61
62         for(coeff = 0; coeff < lag; coeff++)
63                 autoc[coeff] = 0.0;
64         for(sample = 0; sample <= limit; sample++) {
65                 d = data[sample];
66                 for(coeff = 0; coeff < lag; coeff++)
67                         autoc[coeff] += d * data[sample+coeff];
68         }
69         for(; sample < data_len; sample++) {
70                 d = data[sample];
71                 for(coeff = 0; coeff < data_len - sample; coeff++)
72                         autoc[coeff] += d * data[sample+coeff];
73         }
74 }
75
76 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__real error[])
77 {
78         unsigned i, j;
79         double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
80
81         FLAC__ASSERT(0 < max_order);
82         FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
83         FLAC__ASSERT(autoc[0] != 0.0);
84
85         err = autoc[0];
86
87         for(i = 0; i < max_order; i++) {
88                 /* Sum up this iteration's reflection coefficient. */
89                 r = -autoc[i+1];
90                 for(j = 0; j < i; j++)
91                         r -= lpc[j] * autoc[i-j];
92                 ref[i] = (r/=err);
93
94                 /* Update LPC coefficients and total error. */
95                 lpc[i]=r;
96                 for(j = 0; j < (i>>1); j++) {
97                         double tmp = lpc[j];
98                         lpc[j] += r * lpc[i-1-j];
99                         lpc[i-1-j] += r * tmp;
100                 }
101                 if(i & 1)
102                         lpc[j] += lpc[j] * r;
103
104                 err *= (1.0 - r * r);
105
106                 /* save this order */
107                 for(j = 0; j <= i; j++)
108                         lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
109                 error[i] = (FLAC__real)err;
110         }
111 }
112
113 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
114 {
115         unsigned i;
116         double d, cmax = -1e32;
117         FLAC__int32 qmax, qmin;
118         const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
119         const int min_shiftlimit = -max_shiftlimit - 1;
120
121         FLAC__ASSERT(precision > 0);
122         FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
123
124         /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
125         precision--;
126         qmax = 1 << precision;
127         qmin = -qmax;
128         qmax--;
129
130         for(i = 0; i < order; i++) {
131                 if(lp_coeff[i] == 0.0)
132                         continue;
133                 d = fabs(lp_coeff[i]);
134                 if(d > cmax)
135                         cmax = d;
136         }
137 redo_it:
138         if(cmax <= 0.0) {
139                 /* => coefficients are all 0, which means our constant-detect didn't work */
140                 return 2;
141         }
142         else {
143                 int log2cmax;
144
145                 (void)frexp(cmax, &log2cmax);
146                 log2cmax--;
147                 *shift = (int)precision - log2cmax - 1;
148
149                 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
150 #if 0
151                         /*@@@ this does not seem to help at all, but was not extensively tested either: */
152                         if(*shift > max_shiftlimit)
153                                 *shift = max_shiftlimit;
154                         else
155 #endif
156                                 return 1;
157                 }
158         }
159
160         if(*shift >= 0) {
161                 for(i = 0; i < order; i++) {
162                         qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] * (double)(1 << *shift));
163
164                         /* double-check the result */
165                         if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
166 #ifdef FLAC__OVERFLOW_DETECT
167                                 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, (double)lp_coeff[i] * (double)(1 << *shift), floor((double)lp_coeff[i] * (double)(1 << *shift)));
168 #endif
169                                 cmax *= 2.0;
170                                 goto redo_it;
171                         }
172                 }
173         }
174         else { /* (*shift < 0) */
175                 const int nshift = -(*shift);
176 #ifdef DEBUG
177                 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
178 #endif
179                 for(i = 0; i < order; i++) {
180                         qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] / (double)(1 << nshift));
181
182                         /* double-check the result */
183                         if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
184 #ifdef FLAC__OVERFLOW_DETECT
185                                 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, (double)lp_coeff[i] / (double)(1 << nshift), floor((double)lp_coeff[i] / (double)(1 << nshift)));
186 #endif
187                                 cmax *= 2.0;
188                                 goto redo_it;
189                         }
190                 }
191         }
192
193         return 0;
194 }
195
196 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[])
197 {
198 #ifdef FLAC__OVERFLOW_DETECT
199         FLAC__int64 sumo;
200 #endif
201         unsigned i, j;
202         FLAC__int32 sum;
203         const FLAC__int32 *history;
204
205 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
206         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
207         for(i=0;i<order;i++)
208                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
209         fprintf(stderr,"\n");
210 #endif
211         FLAC__ASSERT(order > 0);
212
213         for(i = 0; i < data_len; i++) {
214 #ifdef FLAC__OVERFLOW_DETECT
215                 sumo = 0;
216 #endif
217                 sum = 0;
218                 history = data;
219                 for(j = 0; j < order; j++) {
220                         sum += qlp_coeff[j] * (*(--history));
221 #ifdef FLAC__OVERFLOW_DETECT
222                         sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
223 #if defined _MSC_VER
224                         if(sumo > 2147483647I64 || sumo < -2147483648I64)
225 #else
226                         if(sumo > 2147483647ll || sumo < -2147483648ll)
227 #endif
228                         {
229                                 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);
230                         }
231 #endif
232                 }
233                 *(residual++) = *(data++) - (sum >> lp_quantization);
234         }
235
236         /* Here's a slower but clearer version:
237         for(i = 0; i < data_len; i++) {
238                 sum = 0;
239                 for(j = 0; j < order; j++)
240                         sum += qlp_coeff[j] * data[i-j-1];
241                 residual[i] = data[i] - (sum >> lp_quantization);
242         }
243         */
244 }
245
246 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[])
247 {
248         unsigned i, j;
249         FLAC__int64 sum;
250         const FLAC__int32 *history;
251
252 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
253         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
254         for(i=0;i<order;i++)
255                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
256         fprintf(stderr,"\n");
257 #endif
258         FLAC__ASSERT(order > 0);
259
260         for(i = 0; i < data_len; i++) {
261                 sum = 0;
262                 history = data;
263                 for(j = 0; j < order; j++)
264                         sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
265 #ifdef FLAC__OVERFLOW_DETECT
266                 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
267                         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
268                         break;
269                 }
270                 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
271                         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));
272                         break;
273                 }
274 #endif
275                 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
276         }
277 }
278
279 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[])
280 {
281 #ifdef FLAC__OVERFLOW_DETECT
282         FLAC__int64 sumo;
283 #endif
284         unsigned i, j;
285         FLAC__int32 sum;
286         const FLAC__int32 *history;
287
288 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
289         fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
290         for(i=0;i<order;i++)
291                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
292         fprintf(stderr,"\n");
293 #endif
294         FLAC__ASSERT(order > 0);
295
296         for(i = 0; i < data_len; i++) {
297 #ifdef FLAC__OVERFLOW_DETECT
298                 sumo = 0;
299 #endif
300                 sum = 0;
301                 history = data;
302                 for(j = 0; j < order; j++) {
303                         sum += qlp_coeff[j] * (*(--history));
304 #ifdef FLAC__OVERFLOW_DETECT
305                         sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
306 #if defined _MSC_VER
307                         if(sumo > 2147483647I64 || sumo < -2147483648I64)
308 #else
309                         if(sumo > 2147483647ll || sumo < -2147483648ll)
310 #endif
311                         {
312                                 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);
313                         }
314 #endif
315                 }
316                 *(data++) = *(residual++) + (sum >> lp_quantization);
317         }
318
319         /* Here's a slower but clearer version:
320         for(i = 0; i < data_len; i++) {
321                 sum = 0;
322                 for(j = 0; j < order; j++)
323                         sum += qlp_coeff[j] * data[i-j-1];
324                 data[i] = residual[i] + (sum >> lp_quantization);
325         }
326         */
327 }
328
329 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[])
330 {
331         unsigned i, j;
332         FLAC__int64 sum;
333         const FLAC__int32 *history;
334
335 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
336         fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
337         for(i=0;i<order;i++)
338                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
339         fprintf(stderr,"\n");
340 #endif
341         FLAC__ASSERT(order > 0);
342
343         for(i = 0; i < data_len; i++) {
344                 sum = 0;
345                 history = data;
346                 for(j = 0; j < order; j++)
347                         sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
348 #ifdef FLAC__OVERFLOW_DETECT
349                 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
350                         fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
351                         break;
352                 }
353                 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*residual) + (sum >> lp_quantization)) > 32) {
354                         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));
355                         break;
356                 }
357 #endif
358                 *(data++) = *(residual++) + (FLAC__int32)(sum >> lp_quantization);
359         }
360 }
361
362 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__real lpc_error, unsigned total_samples)
363 {
364         double error_scale;
365
366         FLAC__ASSERT(total_samples > 0);
367
368         error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
369
370         return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
371 }
372
373 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__real lpc_error, double error_scale)
374 {
375         if(lpc_error > 0.0) {
376                 FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
377                 if(bps >= 0.0)
378                         return bps;
379                 else
380                         return 0.0;
381         }
382         else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
383                 return (FLAC__real)1e32;
384         }
385         else {
386                 return 0.0;
387         }
388 }
389
390 unsigned FLAC__lpc_compute_best_order(const FLAC__real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
391 {
392         unsigned order, best_order;
393         FLAC__real best_bits, tmp_bits;
394         double error_scale;
395
396         FLAC__ASSERT(max_order > 0);
397         FLAC__ASSERT(total_samples > 0);
398
399         error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
400
401         best_order = 0;
402         best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__real)total_samples;
403
404         for(order = 1; order < max_order; order++) {
405                 tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (FLAC__real)(total_samples - order) + (FLAC__real)(order * bits_per_signal_sample);
406                 if(tmp_bits < best_bits) {
407                         best_order = order;
408                         best_bits = tmp_bits;
409                 }
410         }
411
412         return best_order+1; /* +1 since index of lpc_error[] is order-1 */
413 }