annotate src/flac/libflac/fixed.c @ 906:16e51fb5908e trunk

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