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