1 /******************************************************************************
2 *
3 * Copyright 2014 The Android Open Source Project
4 * Copyright 2003 - 2004 Open Interface North America, Inc. All rights
5 * reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************************/
20
21 /*******************************************************************************
22 $Revision: #1 $
23 ******************************************************************************/
24
25 /**
26 @file
27
28 Dequantizer for SBC decoder; reconstructs quantized representation of subband
29 samples.
30
31 @ingroup codec_internal
32 */
33
34 /**
35 @addtogroup codec_internal
36 @{
37 */
38
39 /**
40 This function is a fixed-point approximation of a modification of the following
41 dequantization operation defined in the spec, as inferred from section 12.6.4:
42
43 @code
44 dequant = 2^(scale_factor+1) * ((raw * 2.0 + 1.0) / ((2^bits) - 1) - 1)
45
46 2 <= bits <= 16
47 0 <= raw < (2^bits)-1 (the -1 is because quantized values with all 1's are
48 forbidden)
49
50 -65535 < dequant < 65535
51 @endcode
52
53 The code below computes the dequantized value divided by a scaling constant
54 equal to about 1.38. This constant is chosen to ensure that the entry in the
55 dequant_long_scaled table for 16 bits is as accurate as possible, since it has
56 the least relative precision available to it due to its small magnitude.
57
58 This routine outputs in Q16.15 format.
59
60 The helper array dequant_long is defined as follows:
61
62 @code
63 dequant_long_long[bits] = round(2^31 * 1/((2^bits - 1) / 1.38...) for 2 <=
64 bits <= 16
65 @endcode
66
67
68 Additionally, the table entries have the following property:
69
70 @code
71 dequant_long_scaled[bits] <= 2^31 / ((2^bits - 1)) for 2 <= bits <= 16
72 @endcode
73
74 Therefore
75
76 @code
77 d = 2 * raw + 1 1 <= d <= 2^bits - 2
78
79 d' = d * dequant_long[bits]
80
81 d * dequant_long_scaled[bits] <= (2^bits - 2) * (2^31 /
82 (2^bits - 1))
83 d * dequant_long_scaled[bits] <= 2^31 * (2^bits - 2)/(2^bits -
84 1) < 2^31
85 @endcode
86
87 Therefore, d' doesn't overflow a signed 32-bit value.
88
89 @code
90
91 d' =~ 2^31 * (raw * 2.0 + 1.0) / (2^bits - 1) / 1.38...
92
93 result = d' - 2^31/1.38... =~ 2^31 * ((raw * 2.0 + 1.0) / (2^bits - 1) - 1) /
94 1.38...
95
96 result is therefore a scaled approximation to dequant. It remains only to
97 turn 2^31 into 2^(scale_factor+1). Since we're aiming for Q16.15 format,
98 this is achieved by shifting right by (15-scale_factor):
99
100 (2^31 * x) >> (15-scale_factor) =~ 2^(31-15+scale_factor) * x = 2^15 *
101 2^(1+scale_factor) * x
102 @endcode
103
104 */
105
106 #include <oi_codec_sbc_private.h>
107
108 #ifndef SBC_DEQUANT_LONG_SCALED_OFFSET
109 #define SBC_DEQUANT_LONG_SCALED_OFFSET 1555931970
110 #endif
111
112 #ifndef SBC_DEQUANT_LONG_UNSCALED_OFFSET
113 #define SBC_DEQUANT_LONG_UNSCALED_OFFSET 2147483648
114 #endif
115
116 #ifndef SBC_DEQUANT_SCALING_FACTOR
117 #define SBC_DEQUANT_SCALING_FACTOR 1.38019122262781f
118 #endif
119
120 const uint32_t dequant_long_scaled[17] = {
121 0, 0,
122 0x1ee9e116, /* bits=2 0.24151243 1/3 * (1/1.38019122262781)
123 (0x00000008)*/
124 0x0d3fa99c, /* bits=3 0.10350533 1/7 * (1/1.38019122262781)
125 (0x00000013)*/
126 0x062ec69e, /* bits=4 0.04830249 1/15 * (1/1.38019122262781)
127 (0x00000029)*/
128 0x02fddbfa, /* bits=5 0.02337217 1/31 * (1/1.38019122262781)
129 (0x00000055)*/
130 0x0178d9f5, /* bits=6 0.01150059 1/63 * (1/1.38019122262781)
131 (0x000000ad)*/
132 0x00baf129, /* bits=7 0.00570502 1/127 * (1/1.38019122262781)
133 (0x0000015e)*/
134 0x005d1abe, /* bits=8 0.00284132 1/255 * (1/1.38019122262781)
135 (0x000002bf)*/
136 0x002e760d, /* bits=9 0.00141788 1/511 * (1/1.38019122262781)
137 (0x00000582)*/
138 0x00173536, /* bits=10 0.00070825 1/1023 * (1/1.38019122262781)
139 (0x00000b07)*/
140 0x000b9928, /* bits=11 0.00035395 1/2047 * (1/1.38019122262781)
141 (0x00001612)*/
142 0x0005cc37, /* bits=12 0.00017693 1/4095 * (1/1.38019122262781)
143 (0x00002c27)*/
144 0x0002e604, /* bits=13 0.00008846 1/8191 * (1/1.38019122262781)
145 (0x00005852)*/
146 0x000172fc, /* bits=14 0.00004422 1/16383 * (1/1.38019122262781)
147 (0x0000b0a7)*/
148 0x0000b97d, /* bits=15 0.00002211 1/32767 * (1/1.38019122262781)
149 (0x00016150)*/
150 0x00005cbe, /* bits=16 0.00001106 1/65535 * (1/1.38019122262781)
151 (0x0002c2a5)*/
152 };
153
154 const uint32_t dequant_long_unscaled[17] = {
155 0, 0, 0x2aaaaaab, /* bits=2 0.33333333 1/3 (0x00000005)*/
156 0x12492492, /* bits=3 0.14285714 1/7 (0x0000000e)*/
157 0x08888889, /* bits=4 0.06666667 1/15 (0x0000001d)*/
158 0x04210842, /* bits=5 0.03225806 1/31 (0x0000003e)*/
159 0x02082082, /* bits=6 0.01587302 1/63 (0x0000007e)*/
160 0x01020408, /* bits=7 0.00787402 1/127 (0x000000fe)*/
161 0x00808081, /* bits=8 0.00392157 1/255 (0x000001fd)*/
162 0x00402010, /* bits=9 0.00195695 1/511 (0x000003fe)*/
163 0x00200802, /* bits=10 0.00097752 1/1023 (0x000007fe)*/
164 0x00100200, /* bits=11 0.00048852 1/2047 (0x00000ffe)*/
165 0x00080080, /* bits=12 0.00024420 1/4095 (0x00001ffe)*/
166 0x00040020, /* bits=13 0.00012209 1/8191 (0x00003ffe)*/
167 0x00020008, /* bits=14 0.00006104 1/16383 (0x00007ffe)*/
168 0x00010002, /* bits=15 0.00003052 1/32767 (0x0000fffe)*/
169 0x00008001, /* bits=16 0.00001526 1/65535 (0x0001fffc)*/
170 };
171
172 /** Scales x by y bits to the right, adding a rounding factor.
173 */
174 #ifndef SCALE
175 #define SCALE(x, y) (((x) + (1 << ((y)-1))) >> (y))
176 #endif
177
178 #ifdef DEBUG_DEQUANTIZATION
179
180 #include <math.h>
181
dequant_float(uint32_t raw,OI_UINT scale_factor,OI_UINT bits)182 INLINE float dequant_float(uint32_t raw, OI_UINT scale_factor, OI_UINT bits) {
183 float result = (1 << (scale_factor + 1)) *
184 ((raw * 2.0f + 1.0f) / ((1 << bits) - 1.0f) - 1.0f);
185
186 result /= SBC_DEQUANT_SCALING_FACTOR;
187
188 /* Unless the encoder screwed up, all correct dequantized values should
189 * satisfy this inequality. Non-compliant encoders which generate quantized
190 * values with all 1-bits set can, theoretically, trigger this assert. This
191 * is unlikely, however, and only an issue in debug mode.
192 */
193 OI_ASSERT(fabs(result) < 32768 * 1.6);
194
195 return result;
196 }
197
198 #endif
199
OI_SBC_Dequant(uint32_t raw,OI_UINT scale_factor,OI_UINT bits)200 INLINE int32_t OI_SBC_Dequant(uint32_t raw, OI_UINT scale_factor,
201 OI_UINT bits) {
202 uint32_t d;
203 int32_t result;
204
205 OI_ASSERT(scale_factor <= 15);
206 OI_ASSERT(bits <= 16);
207
208 if (bits <= 1) {
209 return 0;
210 }
211
212 d = (raw * 2) + 1;
213 d *= dequant_long_scaled[bits];
214 result = d - SBC_DEQUANT_LONG_SCALED_OFFSET;
215
216 #ifdef DEBUG_DEQUANTIZATION
217 {
218 int32_t integerized_float_result;
219 float float_result;
220
221 float_result = dequant_float(raw, scale_factor, bits);
222 integerized_float_result = (int32_t)floor(0.5f + float_result * (1 << 15));
223
224 /* This detects overflow */
225 OI_ASSERT(((result >= 0) && (integerized_float_result >= 0)) ||
226 ((result <= 0) && (integerized_float_result <= 0)));
227 }
228 #endif
229 return result >> (15 - scale_factor);
230 }
231
232 /* This version of Dequant does not incorporate the scaling factor of 1.38. It
233 * is intended for use with implementations of the filterbank which are
234 * hard-coded into a DSP. Output is Q16.4 format, so that after joint stereo
235 * processing (which leaves the most significant bit equal to the sign bit if
236 * the encoder is conformant) the result will fit a 24 bit fixed point signed
237 * value.*/
238
OI_SBC_Dequant_Unscaled(uint32_t raw,OI_UINT scale_factor,OI_UINT bits)239 INLINE int32_t OI_SBC_Dequant_Unscaled(uint32_t raw, OI_UINT scale_factor,
240 OI_UINT bits) {
241 uint32_t d;
242 int32_t result;
243
244 OI_ASSERT(scale_factor <= 15);
245 OI_ASSERT(bits <= 16);
246
247 if (bits <= 1) {
248 return 0;
249 }
250 if (bits == 16) {
251 result = (raw << 16) + raw - 0x7fff7fff;
252 return SCALE(result, 24 - scale_factor);
253 }
254
255 d = (raw * 2) + 1;
256 d *= dequant_long_unscaled[bits];
257 result = d - 0x80000000;
258
259 return SCALE(result, 24 - scale_factor);
260 }
261
262 /**
263 @}
264 */
265