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