1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 The Android Open Source Project
4  *  Copyright 2003 - 2004 Open Interface North America, Inc. All rights reserved.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 /**********************************************************************************
21   $Revision: #1 $
22 ***********************************************************************************/
23 
24 /**
25  @file
26 
27  Dequantizer for SBC decoder; reconstructs quantized representation of subband samples.
28 
29  @ingroup codec_internal
30  */
31 
32 /**
33 @addtogroup codec_internal
34 @{
35 */
36 
37 /**
38  This function is a fixed-point approximation of a modification of the following
39  dequantization operation defined in the spec, as inferred from section 12.6.4:
40 
41  @code
42  dequant = 2^(scale_factor+1) * ((raw * 2.0 + 1.0) / ((2^bits) - 1) - 1)
43 
44  2 <= bits <= 16
45  0 <= raw < (2^bits)-1   (the -1 is because quantized values with all 1's are forbidden)
46 
47  -65535 < dequant < 65535
48  @endcode
49 
50  The code below computes the dequantized value divided by a scaling constant
51  equal to about 1.38. This constant is chosen to ensure that the entry in the
52  dequant_long_scaled table for 16 bits is as accurate as possible, since it has
53  the least relative precision available to it due to its small magnitude.
54 
55  This routine outputs in Q16.15 format.
56 
57  The helper array dequant_long is defined as follows:
58 
59  @code
60  dequant_long_long[bits] = round(2^31 * 1/((2^bits - 1) / 1.38...)  for 2 <= bits <= 16
61  @endcode
62 
63 
64  Additionally, the table entries have the following property:
65 
66  @code
67  dequant_long_scaled[bits] <= 2^31 / ((2^bits - 1))  for 2 <= bits <= 16
68  @endcode
69 
70  Therefore
71 
72  @code
73  d = 2 * raw + 1              1 <= d <= 2^bits - 2
74 
75  d' = d * dequant_long[bits]
76 
77                   d * dequant_long_scaled[bits] <= (2^bits - 2) * (2^31 / (2^bits - 1))
78                   d * dequant_long_scaled[bits] <= 2^31 * (2^bits - 2)/(2^bits - 1) < 2^31
79  @endcode
80 
81  Therefore, d' doesn't overflow a signed 32-bit value.
82 
83  @code
84 
85  d' =~ 2^31 * (raw * 2.0 + 1.0) / (2^bits - 1) / 1.38...
86 
87  result = d' - 2^31/1.38... =~ 2^31 * ((raw * 2.0 + 1.0) / (2^bits - 1) - 1) / 1.38...
88 
89  result is therefore a scaled approximation to dequant. It remains only to
90  turn 2^31 into 2^(scale_factor+1). Since we're aiming for Q16.15 format,
91  this is achieved by shifting right by (15-scale_factor):
92 
93   (2^31 * x) >> (15-scale_factor) =~ 2^(31-15+scale_factor) * x = 2^15 * 2^(1+scale_factor) * x
94  @endcode
95 
96  */
97 
98 #include "common/bt_target.h"
99 #include <oi_codec_sbc_private.h>
100 
101 #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE)
102 
103 #ifndef SBC_DEQUANT_LONG_SCALED_OFFSET
104 #define SBC_DEQUANT_LONG_SCALED_OFFSET 1555931970
105 #endif
106 
107 #ifndef SBC_DEQUANT_LONG_UNSCALED_OFFSET
108 #define SBC_DEQUANT_LONG_UNSCALED_OFFSET 2147483648
109 #endif
110 
111 #ifndef SBC_DEQUANT_SCALING_FACTOR
112 #define SBC_DEQUANT_SCALING_FACTOR 1.38019122262781f
113 #endif
114 
115 extern const OI_UINT32 dequant_long_scaled[17];
116 extern const OI_UINT32 dequant_long_unscaled[17];
117 
118 /** Scales x by y bits to the right, adding a rounding factor.
119  */
120 #ifndef SCALE
121 #define SCALE(x, y) (((x) + (1 <<((y)-1))) >> (y))
122 #endif
123 
124 #ifdef DEBUG_DEQUANTIZATION
125 
126 #include <math.h>
127 
dequant_float(OI_UINT32 raw,OI_UINT scale_factor,OI_UINT bits)128 static INLINE float dequant_float(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits)
129 {
130     float result = (1 << (scale_factor + 1)) * ((raw * 2.0f + 1.0f) / ((1 << bits) - 1.0f) - 1.0f);
131 
132     result /= SBC_DEQUANT_SCALING_FACTOR;
133 
134     /* Unless the encoder screwed up, all correct dequantized values should
135      * satisfy this inequality. Non-compliant encoders which generate quantized
136      * values with all 1-bits set can, theoretically, trigger this assert. This
137      * is unlikely, however, and only an issue in debug mode.
138      */
139     OI_ASSERT(fabs(result) < 32768 * 1.6);
140 
141     return result;
142 }
143 
144 #endif
145 
146 
OI_SBC_Dequant(OI_UINT32 raw,OI_UINT scale_factor,OI_UINT bits)147 INLINE OI_INT32 OI_SBC_Dequant(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits)
148 {
149     OI_UINT32 d;
150     OI_INT32 result;
151 
152     OI_ASSERT(scale_factor <= 15);
153     OI_ASSERT(bits <= 16);
154 
155     if (bits <= 1) {
156         return 0;
157     }
158 
159     d = (raw * 2) + 1;
160     d *= dequant_long_scaled[bits];
161     result = d - SBC_DEQUANT_LONG_SCALED_OFFSET;
162 
163 #ifdef DEBUG_DEQUANTIZATION
164     {
165         OI_INT32 integerized_float_result;
166         float float_result;
167 
168         float_result = dequant_float(raw, scale_factor, bits);
169         integerized_float_result = (OI_INT32)floor(0.5f + float_result * (1 << 15));
170 
171         /* This detects overflow */
172         OI_ASSERT(((result >= 0) && (integerized_float_result >= 0)) ||
173                   ((result <= 0) && (integerized_float_result <= 0)));
174     }
175 #endif
176     return result >> (15 - scale_factor);
177 }
178 
179 /* This version of Dequant does not incorporate the scaling factor of 1.38. It
180  * is intended for use with implementations of the filterbank which are
181  * hard-coded into a DSP. Output is Q16.4 format, so that after joint stereo
182  * processing (which leaves the most significant bit equal to the sign bit if
183  * the encoder is conformant) the result will fit a 24 bit fixed point signed
184  * value.*/
185 
OI_SBC_Dequant_Unscaled(OI_UINT32 raw,OI_UINT scale_factor,OI_UINT bits)186 INLINE OI_INT32 OI_SBC_Dequant_Unscaled(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits)
187 {
188     OI_UINT32 d;
189     OI_INT32 result;
190 
191     OI_ASSERT(scale_factor <= 15);
192     OI_ASSERT(bits <= 16);
193 
194 
195     if (bits <= 1) {
196         return 0;
197     }
198     if (bits == 16) {
199         result = (raw << 16) + raw - 0x7fff7fff;
200         return SCALE(result, 24 - scale_factor);
201     }
202 
203 
204     d = (raw * 2) + 1;
205     d *= dequant_long_unscaled[bits];
206     result = d - 0x80000000;
207 
208     return SCALE(result, 24 - scale_factor);
209 }
210 
211 /**
212 @}
213 */
214 
215 #endif /* #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE) */
216