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)) * ((raw * 2.0f + 1.0f) / ((1 << bits) - 1.0f) - 1.0f);
184 
185   result /= SBC_DEQUANT_SCALING_FACTOR;
186 
187   /* Unless the encoder screwed up, all correct dequantized values should
188    * satisfy this inequality. Non-compliant encoders which generate quantized
189    * values with all 1-bits set can, theoretically, trigger this assert. This
190    * is unlikely, however, and only an issue in debug mode.
191    */
192   OI_ASSERT(fabs(result) < 32768 * 1.6);
193 
194   return result;
195 }
196 
197 #endif
198 
OI_SBC_Dequant(uint32_t raw,OI_UINT scale_factor,OI_UINT bits)199 INLINE int32_t OI_SBC_Dequant(uint32_t raw, OI_UINT scale_factor, OI_UINT bits) {
200   uint32_t d;
201   int32_t result;
202 
203   OI_ASSERT(scale_factor <= 15);
204   OI_ASSERT(bits <= 16);
205 
206   if (bits <= 1) {
207     return 0;
208   }
209 
210   d = (raw * 2) + 1;
211   d *= dequant_long_scaled[bits];
212   result = d - SBC_DEQUANT_LONG_SCALED_OFFSET;
213 
214 #ifdef DEBUG_DEQUANTIZATION
215   {
216     int32_t integerized_float_result;
217     float float_result;
218 
219     float_result = dequant_float(raw, scale_factor, bits);
220     integerized_float_result = (int32_t)floor(0.5f + float_result * (1 << 15));
221 
222     /* This detects overflow */
223     OI_ASSERT(((result >= 0) && (integerized_float_result >= 0)) ||
224               ((result <= 0) && (integerized_float_result <= 0)));
225   }
226 #endif
227   return result >> (15 - scale_factor);
228 }
229 
230 /* This version of Dequant does not incorporate the scaling factor of 1.38. It
231  * is intended for use with implementations of the filterbank which are
232  * hard-coded into a DSP. Output is Q16.4 format, so that after joint stereo
233  * processing (which leaves the most significant bit equal to the sign bit if
234  * the encoder is conformant) the result will fit a 24 bit fixed point signed
235  * value.*/
236 
OI_SBC_Dequant_Unscaled(uint32_t raw,OI_UINT scale_factor,OI_UINT bits)237 INLINE int32_t OI_SBC_Dequant_Unscaled(uint32_t raw, OI_UINT scale_factor, OI_UINT bits) {
238   uint32_t d;
239   int32_t result;
240 
241   OI_ASSERT(scale_factor <= 15);
242   OI_ASSERT(bits <= 16);
243 
244   if (bits <= 1) {
245     return 0;
246   }
247   if (bits == 16) {
248     result = (raw << 16) + raw - 0x7fff7fff;
249     return SCALE(result, 24 - scale_factor);
250   }
251 
252   d = (raw * 2) + 1;
253   d *= dequant_long_unscaled[bits];
254   result = d - 0x80000000;
255 
256   return SCALE(result, 24 - scale_factor);
257 }
258 
259 /**
260 @}
261 */
262