minor comments
[platform/upstream/flac.git] / src / libFLAC / fixed.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
30  */
31
32 #include <math.h>
33 #include "private/bitmath.h"
34 #include "private/fixed.h"
35 #include "FLAC/assert.h"
36
37 #ifndef M_LN2
38 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
39 #define M_LN2 0.69314718055994530942
40 #endif
41
42 #ifdef min
43 #undef min
44 #endif
45 #define min(x,y) ((x) < (y)? (x) : (y))
46
47 #ifdef local_abs
48 #undef local_abs
49 #endif
50 #define local_abs(x) ((unsigned)((x)<0? -(x) : (x)))
51
52 #ifdef FLAC__INTEGER_ONLY_LIBRARY
53 /* rbps stands for residual bits per sample
54  *
55  *             (ln(2) * err)
56  * rbps = log  (-----------)
57  *           2 (     n     )
58  */
59 static FLAC__fixedpoint local__compute_rbps_integerized(FLAC__uint32 err, FLAC__uint32 n)
60 {
61         FLAC__uint32 rbps;
62         unsigned bits; /* the number of bits required to represent a number */
63         int fracbits; /* the number of bits of rbps that comprise the fractional part */
64
65         FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
66         FLAC__ASSERT(err > 0);
67         FLAC__ASSERT(n > 0);
68
69         FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
70         if(err <= n)
71                 return 0;
72         /*
73          * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
74          * These allow us later to know we won't lose too much precision in the
75          * fixed-point division (err<<fracbits)/n.
76          */
77
78         fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2(err)+1);
79
80         err <<= fracbits;
81         err /= n;
82         /* err now holds err/n with fracbits fractional bits */
83
84         /*
85          * Whittle err down to 16 bits max.  16 significant bits is enough for
86          * our purposes.
87          */
88         FLAC__ASSERT(err > 0);
89         bits = FLAC__bitmath_ilog2(err)+1;
90         if(bits > 16) {
91                 err >>= (bits-16);
92                 fracbits -= (bits-16);
93         }
94         rbps = (FLAC__uint32)err;
95
96         /* Multiply by fixed-point version of ln(2), with 16 fractional bits */
97         rbps *= FLAC__FP_LN2;
98         fracbits += 16;
99         FLAC__ASSERT(fracbits >= 0);
100
101         /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
102         {
103                 const int f = fracbits & 3;
104                 if(f) {
105                         rbps >>= f;
106                         fracbits -= f;
107                 }
108         }
109
110         rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));
111
112         if(rbps == 0)
113                 return 0;
114
115         /*
116          * The return value must have 16 fractional bits.  Since the whole part
117          * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
118          * must be >= -3, these assertion allows us to be able to shift rbps
119          * left if necessary to get 16 fracbits without losing any bits of the
120          * whole part of rbps.
121          *
122          * There is a slight chance due to accumulated error that the whole part
123          * will require 6 bits, so we use 6 in the assertion.  Really though as
124          * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
125          */
126         FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
127         FLAC__ASSERT(fracbits >= -3);
128
129         /* now shift the decimal point into place */
130         if(fracbits < 16)
131                 return rbps << (16-fracbits);
132         else if(fracbits > 16)
133                 return rbps >> (fracbits-16);
134         else
135                 return rbps;
136 }
137
138 static FLAC__fixedpoint local__compute_rbps_wide_integerized(FLAC__uint64 err, FLAC__uint32 n)
139 {
140         FLAC__uint32 rbps;
141         unsigned bits; /* the number of bits required to represent a number */
142         int fracbits; /* the number of bits of rbps that comprise the fractional part */
143
144         FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
145         FLAC__ASSERT(err > 0);
146         FLAC__ASSERT(n > 0);
147
148         FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
149         if(err <= n)
150                 return 0;
151         /*
152          * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
153          * These allow us later to know we won't lose too much precision in the
154          * fixed-point division (err<<fracbits)/n.
155          */
156
157         fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2_wide(err)+1);
158
159         err <<= fracbits;
160         err /= n;
161         /* err now holds err/n with fracbits fractional bits */
162
163         /*
164          * Whittle err down to 16 bits max.  16 significant bits is enough for
165          * our purposes.
166          */
167         FLAC__ASSERT(err > 0);
168         bits = FLAC__bitmath_ilog2_wide(err)+1;
169         if(bits > 16) {
170                 err >>= (bits-16);
171                 fracbits -= (bits-16);
172         }
173         rbps = (FLAC__uint32)err;
174
175         /* Multiply by fixed-point version of ln(2), with 16 fractional bits */
176         rbps *= FLAC__FP_LN2;
177         fracbits += 16;
178         FLAC__ASSERT(fracbits >= 0);
179
180         /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
181         {
182                 const int f = fracbits & 3;
183                 if(f) {
184                         rbps >>= f;
185                         fracbits -= f;
186                 }
187         }
188
189         rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));
190
191         if(rbps == 0)
192                 return 0;
193
194         /*
195          * The return value must have 16 fractional bits.  Since the whole part
196          * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
197          * must be >= -3, these assertion allows us to be able to shift rbps
198          * left if necessary to get 16 fracbits without losing any bits of the
199          * whole part of rbps.
200          *
201          * There is a slight chance due to accumulated error that the whole part
202          * will require 6 bits, so we use 6 in the assertion.  Really though as
203          * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
204          */
205         FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
206         FLAC__ASSERT(fracbits >= -3);
207
208         /* now shift the decimal point into place */
209         if(fracbits < 16)
210                 return rbps << (16-fracbits);
211         else if(fracbits > 16)
212                 return rbps >> (fracbits-16);
213         else
214                 return rbps;
215 }
216 #endif
217
218 #ifndef FLAC__INTEGER_ONLY_LIBRARY
219 unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
220 #else
221 unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
222 #endif
223 {
224         FLAC__int32 last_error_0 = data[-1];
225         FLAC__int32 last_error_1 = data[-1] - data[-2];
226         FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
227         FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
228         FLAC__int32 error, save;
229         FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
230         unsigned i, order;
231
232         for(i = 0; i < data_len; i++) {
233                 error  = data[i]     ; total_error_0 += local_abs(error);                      save = error;
234                 error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
235                 error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
236                 error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
237                 error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
238         }
239
240         if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
241                 order = 0;
242         else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
243                 order = 1;
244         else if(total_error_2 < min(total_error_3, total_error_4))
245                 order = 2;
246         else if(total_error_3 < total_error_4)
247                 order = 3;
248         else
249                 order = 4;
250
251         /* Estimate the expected number of bits per residual signal sample. */
252         /* 'total_error*' is linearly related to the variance of the residual */
253         /* signal, so we use it directly to compute E(|x|) */
254         FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
255         FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
256         FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
257         FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
258         FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
259 #ifndef FLAC__INTEGER_ONLY_LIBRARY
260         residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
261         residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
262         residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
263         residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
264         residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
265 #else
266         residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_integerized(total_error_0, data_len) : 0;
267         residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_integerized(total_error_1, data_len) : 0;
268         residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_integerized(total_error_2, data_len) : 0;
269         residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_integerized(total_error_3, data_len) : 0;
270         residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_integerized(total_error_4, data_len) : 0;
271 #endif
272
273         return order;
274 }
275
276 #ifndef FLAC__INTEGER_ONLY_LIBRARY
277 unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
278 #else
279 unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
280 #endif
281 {
282         FLAC__int32 last_error_0 = data[-1];
283         FLAC__int32 last_error_1 = data[-1] - data[-2];
284         FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
285         FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
286         FLAC__int32 error, save;
287         /* total_error_* are 64-bits to avoid overflow when encoding
288          * erratic signals when the bits-per-sample and blocksize are
289          * large.
290          */
291         FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
292         unsigned i, order;
293
294         for(i = 0; i < data_len; i++) {
295                 error  = data[i]     ; total_error_0 += local_abs(error);                      save = error;
296                 error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
297                 error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
298                 error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
299                 error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
300         }
301
302         if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
303                 order = 0;
304         else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
305                 order = 1;
306         else if(total_error_2 < min(total_error_3, total_error_4))
307                 order = 2;
308         else if(total_error_3 < total_error_4)
309                 order = 3;
310         else
311                 order = 4;
312
313         /* Estimate the expected number of bits per residual signal sample. */
314         /* 'total_error*' is linearly related to the variance of the residual */
315         /* signal, so we use it directly to compute E(|x|) */
316         FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
317         FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
318         FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
319         FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
320         FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
321 #ifndef FLAC__INTEGER_ONLY_LIBRARY
322 #if defined _MSC_VER || defined __MINGW32__
323         /* with MSVC you have to spoon feed it the casting */
324         residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
325         residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
326         residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
327         residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
328         residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
329 #else
330         residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
331         residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
332         residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
333         residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
334         residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
335 #endif
336 #else
337         residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_wide_integerized(total_error_0, data_len) : 0;
338         residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_wide_integerized(total_error_1, data_len) : 0;
339         residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_wide_integerized(total_error_2, data_len) : 0;
340         residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_wide_integerized(total_error_3, data_len) : 0;
341         residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_wide_integerized(total_error_4, data_len) : 0;
342 #endif
343
344         return order;
345 }
346
347 void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
348 {
349         const int idata_len = (int)data_len;
350         int i;
351
352         switch(order) {
353                 case 0:
354                         for(i = 0; i < idata_len; i++) {
355                                 residual[i] = data[i];
356                         }
357                         break;
358                 case 1:
359                         for(i = 0; i < idata_len; i++) {
360                                 residual[i] = data[i] - data[i-1];
361                         }
362                         break;
363                 case 2:
364                         for(i = 0; i < idata_len; i++) {
365                                 /* == data[i] - 2*data[i-1] + data[i-2] */
366                                 residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
367                         }
368                         break;
369                 case 3:
370                         for(i = 0; i < idata_len; i++) {
371                                 /* == data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3] */
372                                 residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
373                         }
374                         break;
375                 case 4:
376                         for(i = 0; i < idata_len; i++) {
377                                 /* == data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4] */
378                                 residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
379                         }
380                         break;
381                 default:
382                         FLAC__ASSERT(0);
383         }
384 }
385
386 void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[])
387 {
388         int i, idata_len = (int)data_len;
389
390         switch(order) {
391                 case 0:
392                         for(i = 0; i < idata_len; i++) {
393                                 data[i] = residual[i];
394                         }
395                         break;
396                 case 1:
397                         for(i = 0; i < idata_len; i++) {
398                                 data[i] = residual[i] + data[i-1];
399                         }
400                         break;
401                 case 2:
402                         for(i = 0; i < idata_len; i++) {
403                                 /* == residual[i] + 2*data[i-1] - data[i-2] */
404                                 data[i] = residual[i] + (data[i-1]<<1) - data[i-2];
405                         }
406                         break;
407                 case 3:
408                         for(i = 0; i < idata_len; i++) {
409                                 /* residual[i] + 3*data[i-1] - 3*data[i-2]) + data[i-3] */
410                                 data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3];
411                         }
412                         break;
413                 case 4:
414                         for(i = 0; i < idata_len; i++) {
415                                 /* == residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4] */
416                                 data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4];
417                         }
418                         break;
419                 default:
420                         FLAC__ASSERT(0);
421         }
422 }