FLAC__ize all the #defines
[flac.git] / src / libFLAC / lpc.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  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 <assert.h>
21 #include <math.h>
22 #include <stdio.h>
23 #include "FLAC/format.h"
24 #include "private/lpc.h"
25
26 #ifndef M_LN2
27 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
28 #define M_LN2 0.69314718055994530942
29 #endif
30
31 void FLAC__lpc_compute_autocorrelation(const real data[], unsigned data_len, unsigned lag, real autoc[])
32 {
33         real d;
34         unsigned i;
35
36         assert(lag > 0);
37         assert(lag <= data_len);
38
39         while(lag--) {
40                 for(i = lag, d = 0.0; i < data_len; i++)
41                         d += data[i] * data[i - lag];
42                 autoc[lag] = d;
43         }
44 }
45
46 void FLAC__lpc_compute_lp_coefficients(const real autoc[], unsigned max_order, real lp_coeff[][FLAC__MAX_LPC_ORDER], real error[])
47 {
48         unsigned i, j;
49         real r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
50
51         assert(0 < max_order);
52         assert(max_order <= FLAC__MAX_LPC_ORDER);
53         assert(autoc[0] != 0.0);
54
55         err = autoc[0];
56
57         for(i = 0; i < max_order; i++) {
58                 /* Sum up this iteration's reflection coefficient. */
59                 r =- autoc[i+1];
60                 for(j = 0; j < i; j++)
61                         r -= lpc[j] * autoc[i-j];
62                 ref[i] = (r/=err);
63
64                 /* Update LPC coefficients and total error. */
65                 lpc[i]=r;
66                 for(j = 0; j < (i>>1); j++) {
67                         real tmp = lpc[j];
68                         lpc[j] += r * lpc[i-1-j];
69                         lpc[i-1-j] += r * tmp;
70                 }
71                 if(i & 1)
72                         lpc[j] += lpc[j] * r;
73
74                 err *= (1.0 - r * r);
75
76                 /* save this order */
77                 for(j = 0; j <= i; j++)
78                         lp_coeff[i][j] = -lpc[j]; /* negate to get FIR filter coeffs */
79                 error[i] = err;
80         }
81 }
82
83 int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
84 {
85         unsigned i;
86         real d, cmax = -1e99;
87
88         assert(bits_per_sample > 0);
89         assert(bits_per_sample <= sizeof(int32)*8);
90         assert(precision > 0);
91         assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
92         assert(precision + bits_per_sample < sizeof(int32)*8);
93 #ifdef NDEBUG
94         (void)bits_per_sample; /* silence compiler warning about unused parameter */
95 #endif
96
97         /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
98         precision--;
99
100         for(i = 0; i < order; i++) {
101                 if(lp_coeff[i] == 0.0)
102                         continue;
103                 d = fabs(lp_coeff[i]);
104                 if(d > cmax)
105                         cmax = d;
106         }
107         if(cmax < 0.0) {
108                 /* => coefficients are all 0, which means our constant-detect didn't work */
109                 return 2;
110         }
111         else {
112                 const int maxshift = (int)precision - (int)floor(log(cmax) / M_LN2) - 1;
113                 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
114                 const int min_shiftlimit = -max_shiftlimit - 1;
115
116                 *shift = maxshift;
117
118                 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
119                         return 1;
120                 }
121         }
122
123         if(*shift != 0) { /* just to avoid wasting time... */
124                 for(i = 0; i < order; i++)
125                         qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
126         }
127         return 0;
128 }
129
130 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[])
131 {
132 #ifdef FLAC__OVERFLOW_DETECT
133         int64 sumo;
134 #endif
135         unsigned i, j;
136         int32 sum;
137         const int32 *history;
138
139 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
140         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
141         for(i=0;i<order;i++)
142                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
143         fprintf(stderr,"\n");
144 #endif
145         assert(order > 0);
146
147         for(i = 0; i < data_len; i++) {
148 #ifdef FLAC__OVERFLOW_DETECT
149                 sumo = 0;
150 #endif
151                 sum = 0;
152                 history = data;
153                 for(j = 0; j < order; j++) {
154                         sum += qlp_coeff[j] * (*(--history));
155 #ifdef FLAC__OVERFLOW_DETECT
156                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
157                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
158                                 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);
159                         }
160 #endif
161                 }
162                 *(residual++) = *(data++) - (sum >> lp_quantization);
163         }
164
165         /* Here's a slower but clearer version:
166         for(i = 0; i < data_len; i++) {
167                 sum = 0;
168                 for(j = 0; j < order; j++)
169                         sum += qlp_coeff[j] * data[i-j-1];
170                 residual[i] = data[i] - (sum >> lp_quantization);
171         }
172         */
173 }
174
175 void FLAC__lpc_restore_signal(const int32 residual[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 data[])
176 {
177 #ifdef FLAC__OVERFLOW_DETECT
178         int64 sumo;
179 #endif
180         unsigned i, j;
181         int32 sum;
182         const int32 *history;
183
184 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
185         fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
186         for(i=0;i<order;i++)
187                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
188         fprintf(stderr,"\n");
189 #endif
190         assert(order > 0);
191
192         for(i = 0; i < data_len; i++) {
193 #ifdef FLAC__OVERFLOW_DETECT
194                 sumo = 0;
195 #endif
196                 sum = 0;
197                 history = data;
198                 for(j = 0; j < order; j++) {
199                         sum += qlp_coeff[j] * (*(--history));
200 #ifdef FLAC__OVERFLOW_DETECT
201                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
202                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
203                                 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);
204                         }
205 #endif
206                 }
207                 *(data++) = *(residual++) + (sum >> lp_quantization);
208         }
209
210         /* Here's a slower but clearer version:
211         for(i = 0; i < data_len; i++) {
212                 sum = 0;
213                 for(j = 0; j < order; j++)
214                         sum += qlp_coeff[j] * data[i-j-1];
215                 data[i] = residual[i] + (sum >> lp_quantization);
216         }
217         */
218 }
219
220 real FLAC__lpc_compute_expected_bits_per_residual_sample(real lpc_error, unsigned total_samples)
221 {
222         real escale;
223
224         assert(lpc_error >= 0.0); /* the error can never be negative */
225         assert(total_samples > 0);
226
227         escale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
228
229         if(lpc_error > 0.0) {
230                 real bps = 0.5 * log(escale * lpc_error) / M_LN2;
231                 if(bps >= 0.0)
232                         return bps;
233                 else
234                         return 0.0;
235         }
236         else {
237                 return 0.0;
238         }
239 }
240
241 unsigned FLAC__lpc_compute_best_order(const real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
242 {
243         unsigned order, best_order;
244         real best_bits, tmp_bits;
245
246         assert(max_order > 0);
247
248         best_order = 0;
249         best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[0], total_samples) * (real)total_samples;
250
251         for(order = 1; order < max_order; order++) {
252                 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);
253                 if(tmp_bits < best_bits) {
254                         best_order = order;
255                         best_bits = tmp_bits;
256                 }
257         }
258
259         return best_order+1; /* +1 since index of lpc_error[] is order-1 */
260 }