OLD | NEW |
1 /* libFLAC - Free Lossless Audio Codec library | 1 /* libFLAC - Free Lossless Audio Codec library |
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson | 2 * Copyright (C) 2000-2009 Josh Coalson |
| 3 * Copyright (C) 2011-2014 Xiph.Org Foundation |
3 * | 4 * |
4 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
6 * are met: | 7 * are met: |
7 * | 8 * |
8 * - Redistributions of source code must retain the above copyright | 9 * - Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
10 * | 11 * |
11 * - Redistributions in binary form must reproduce the above copyright | 12 * - Redistributions in binary form must reproduce the above copyright |
12 * notice, this list of conditions and the following disclaimer in the | 13 * notice, this list of conditions and the following disclaimer in the |
13 * documentation and/or other materials provided with the distribution. | 14 * documentation and/or other materials provided with the distribution. |
14 * | 15 * |
15 * - Neither the name of the Xiph.org Foundation nor the names of its | 16 * - 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 * contributors may be used to endorse or promote products derived from |
17 * this software without specific prior written permission. | 18 * this software without specific prior written permission. |
18 * | 19 * |
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR | 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 29 * 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 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ | 31 */ |
31 | 32 |
32 #if HAVE_CONFIG_H | 33 #ifdef HAVE_CONFIG_H |
33 # include <config.h> | 34 # include <config.h> |
34 #endif | 35 #endif |
35 | 36 |
36 #include <math.h> | 37 #include <math.h> |
37 #include <string.h> | 38 #include <string.h> |
| 39 #include "share/compat.h" |
38 #include "private/bitmath.h" | 40 #include "private/bitmath.h" |
39 #include "private/fixed.h" | 41 #include "private/fixed.h" |
| 42 #include "private/macros.h" |
40 #include "FLAC/assert.h" | 43 #include "FLAC/assert.h" |
41 | 44 |
42 #ifndef M_LN2 | |
43 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */ | |
44 #define M_LN2 0.69314718055994530942 | |
45 #endif | |
46 | |
47 #ifdef min | |
48 #undef min | |
49 #endif | |
50 #define min(x,y) ((x) < (y)? (x) : (y)) | |
51 | |
52 #ifdef local_abs | 45 #ifdef local_abs |
53 #undef local_abs | 46 #undef local_abs |
54 #endif | 47 #endif |
55 #define local_abs(x) ((unsigned)((x)<0? -(x) : (x))) | 48 #define local_abs(x) ((unsigned)((x)<0? -(x) : (x))) |
56 | 49 |
57 #ifdef FLAC__INTEGER_ONLY_LIBRARY | 50 #ifdef FLAC__INTEGER_ONLY_LIBRARY |
58 /* rbps stands for residual bits per sample | 51 /* rbps stands for residual bits per sample |
59 * | 52 * |
60 * (ln(2) * err) | 53 * (ln(2) * err) |
61 * rbps = log (-----------) | 54 * rbps = log (-----------) |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 unsigned i, order; | 228 unsigned i, order; |
236 | 229 |
237 for(i = 0; i < data_len; i++) { | 230 for(i = 0; i < data_len; i++) { |
238 error = data[i] ; total_error_0 += local_abs(error);
save = error; | 231 error = data[i] ; total_error_0 += local_abs(error);
save = error; |
239 error -= last_error_0; total_error_1 += local_abs(error); last_e
rror_0 = save; save = error; | 232 error -= last_error_0; total_error_1 += local_abs(error); last_e
rror_0 = save; save = error; |
240 error -= last_error_1; total_error_2 += local_abs(error); last_e
rror_1 = save; save = error; | 233 error -= last_error_1; total_error_2 += local_abs(error); last_e
rror_1 = save; save = error; |
241 error -= last_error_2; total_error_3 += local_abs(error); last_e
rror_2 = save; save = error; | 234 error -= last_error_2; total_error_3 += local_abs(error); last_e
rror_2 = save; save = error; |
242 error -= last_error_3; total_error_4 += local_abs(error); last_e
rror_3 = save; | 235 error -= last_error_3; total_error_4 += local_abs(error); last_e
rror_3 = save; |
243 } | 236 } |
244 | 237 |
245 » if(total_error_0 < min(min(min(total_error_1, total_error_2), total_erro
r_3), total_error_4)) | 238 » if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error
_2), total_error_3), total_error_4)) |
246 order = 0; | 239 order = 0; |
247 » else if(total_error_1 < min(min(total_error_2, total_error_3), total_err
or_4)) | 240 » else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3),
total_error_4)) |
248 order = 1; | 241 order = 1; |
249 » else if(total_error_2 < min(total_error_3, total_error_4)) | 242 » else if(total_error_2 < flac_min(total_error_3, total_error_4)) |
250 order = 2; | 243 order = 2; |
251 else if(total_error_3 < total_error_4) | 244 else if(total_error_3 < total_error_4) |
252 order = 3; | 245 order = 3; |
253 else | 246 else |
254 order = 4; | 247 order = 4; |
255 | 248 |
256 /* Estimate the expected number of bits per residual signal sample. */ | 249 /* Estimate the expected number of bits per residual signal sample. */ |
257 /* 'total_error*' is linearly related to the variance of the residual */ | 250 /* 'total_error*' is linearly related to the variance of the residual */ |
258 /* signal, so we use it directly to compute E(|x|) */ | 251 /* signal, so we use it directly to compute E(|x|) */ |
259 FLAC__ASSERT(data_len > 0 || total_error_0 == 0); | 252 FLAC__ASSERT(data_len > 0 || total_error_0 == 0); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 unsigned i, order; | 290 unsigned i, order; |
298 | 291 |
299 for(i = 0; i < data_len; i++) { | 292 for(i = 0; i < data_len; i++) { |
300 error = data[i] ; total_error_0 += local_abs(error);
save = error; | 293 error = data[i] ; total_error_0 += local_abs(error);
save = error; |
301 error -= last_error_0; total_error_1 += local_abs(error); last_e
rror_0 = save; save = error; | 294 error -= last_error_0; total_error_1 += local_abs(error); last_e
rror_0 = save; save = error; |
302 error -= last_error_1; total_error_2 += local_abs(error); last_e
rror_1 = save; save = error; | 295 error -= last_error_1; total_error_2 += local_abs(error); last_e
rror_1 = save; save = error; |
303 error -= last_error_2; total_error_3 += local_abs(error); last_e
rror_2 = save; save = error; | 296 error -= last_error_2; total_error_3 += local_abs(error); last_e
rror_2 = save; save = error; |
304 error -= last_error_3; total_error_4 += local_abs(error); last_e
rror_3 = save; | 297 error -= last_error_3; total_error_4 += local_abs(error); last_e
rror_3 = save; |
305 } | 298 } |
306 | 299 |
307 » if(total_error_0 < min(min(min(total_error_1, total_error_2), total_erro
r_3), total_error_4)) | 300 » if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error
_2), total_error_3), total_error_4)) |
308 order = 0; | 301 order = 0; |
309 » else if(total_error_1 < min(min(total_error_2, total_error_3), total_err
or_4)) | 302 » else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3),
total_error_4)) |
310 order = 1; | 303 order = 1; |
311 » else if(total_error_2 < min(total_error_3, total_error_4)) | 304 » else if(total_error_2 < flac_min(total_error_3, total_error_4)) |
312 order = 2; | 305 order = 2; |
313 else if(total_error_3 < total_error_4) | 306 else if(total_error_3 < total_error_4) |
314 order = 3; | 307 order = 3; |
315 else | 308 else |
316 order = 4; | 309 order = 4; |
317 | 310 |
318 /* Estimate the expected number of bits per residual signal sample. */ | 311 /* Estimate the expected number of bits per residual signal sample. */ |
319 /* 'total_error*' is linearly related to the variance of the residual */ | 312 /* 'total_error*' is linearly related to the variance of the residual */ |
320 /* signal, so we use it directly to compute E(|x|) */ | 313 /* signal, so we use it directly to compute E(|x|) */ |
321 FLAC__ASSERT(data_len > 0 || total_error_0 == 0); | 314 FLAC__ASSERT(data_len > 0 || total_error_0 == 0); |
322 FLAC__ASSERT(data_len > 0 || total_error_1 == 0); | 315 FLAC__ASSERT(data_len > 0 || total_error_1 == 0); |
323 FLAC__ASSERT(data_len > 0 || total_error_2 == 0); | 316 FLAC__ASSERT(data_len > 0 || total_error_2 == 0); |
324 FLAC__ASSERT(data_len > 0 || total_error_3 == 0); | 317 FLAC__ASSERT(data_len > 0 || total_error_3 == 0); |
325 FLAC__ASSERT(data_len > 0 || total_error_4 == 0); | 318 FLAC__ASSERT(data_len > 0 || total_error_4 == 0); |
326 #ifndef FLAC__INTEGER_ONLY_LIBRARY | 319 #ifndef FLAC__INTEGER_ONLY_LIBRARY |
327 #if defined _MSC_VER || defined __MINGW32__ | |
328 /* with MSVC you have to spoon feed it the casting */ | |
329 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); | |
330 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); | |
331 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); | |
332 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); | |
333 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); | |
334 #else | |
335 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); | 320 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); |
336 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); | 321 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); |
337 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); | 322 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); |
338 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); | 323 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); |
339 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); | 324 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); |
340 #endif | |
341 #else | 325 #else |
342 residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_
wide_integerized(total_error_0, data_len) : 0; | 326 residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_
wide_integerized(total_error_0, data_len) : 0; |
343 residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_
wide_integerized(total_error_1, data_len) : 0; | 327 residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_
wide_integerized(total_error_1, data_len) : 0; |
344 residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_
wide_integerized(total_error_2, data_len) : 0; | 328 residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_
wide_integerized(total_error_2, data_len) : 0; |
345 residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_
wide_integerized(total_error_3, data_len) : 0; | 329 residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_
wide_integerized(total_error_3, data_len) : 0; |
346 residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_
wide_integerized(total_error_4, data_len) : 0; | 330 residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_
wide_integerized(total_error_4, data_len) : 0; |
347 #endif | 331 #endif |
348 | 332 |
349 return order; | 333 return order; |
350 } | 334 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 #if 1 /* OPT: may be faster with some compilers on some systems */ | 410 #if 1 /* OPT: may be faster with some compilers on some systems */ |
427 data[i] = residual[i] + ((data[i-1]+data[i-3])<<
2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4]; | 411 data[i] = residual[i] + ((data[i-1]+data[i-3])<<
2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4]; |
428 #else | 412 #else |
429 data[i] = residual[i] + 4*data[i-1] - 6*data[i-2
] + 4*data[i-3] - data[i-4]; | 413 data[i] = residual[i] + 4*data[i-1] - 6*data[i-2
] + 4*data[i-3] - data[i-4]; |
430 #endif | 414 #endif |
431 break; | 415 break; |
432 default: | 416 default: |
433 FLAC__ASSERT(0); | 417 FLAC__ASSERT(0); |
434 } | 418 } |
435 } | 419 } |
OLD | NEW |