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