1 /****************************************************************************** 2 * 3 * Copyright (C) 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 const uint32_t dequant_long_unscaled[17]; 122 123 /** Scales x by y bits to the right, adding a rounding factor. 124 */ 125 #ifndef SCALE 126 #define SCALE(x, y) (((x) + (1 << ((y)-1))) >> (y)) 127 #endif 128 129 #ifdef DEBUG_DEQUANTIZATION 130 131 #include <math.h> 132 133 INLINE float dequant_float(uint32_t raw, OI_UINT scale_factor, OI_UINT bits) { 134 float result = (1 << (scale_factor + 1)) * 135 ((raw * 2.0f + 1.0f) / ((1 << bits) - 1.0f) - 1.0f); 136 137 result /= SBC_DEQUANT_SCALING_FACTOR; 138 139 /* Unless the encoder screwed up, all correct dequantized values should 140 * satisfy this inequality. Non-compliant encoders which generate quantized 141 * values with all 1-bits set can, theoretically, trigger this assert. This 142 * is unlikely, however, and only an issue in debug mode. 143 */ 144 OI_ASSERT(fabs(result) < 32768 * 1.6); 145 146 return result; 147 } 148 149 #endif 150 151 INLINE int32_t OI_SBC_Dequant(uint32_t raw, OI_UINT scale_factor, 152 OI_UINT bits) { 153 uint32_t d; 154 int32_t result; 155 156 OI_ASSERT(scale_factor <= 15); 157 OI_ASSERT(bits <= 16); 158 159 if (bits <= 1) { 160 return 0; 161 } 162 163 d = (raw * 2) + 1; 164 d *= dequant_long_scaled[bits]; 165 result = d - SBC_DEQUANT_LONG_SCALED_OFFSET; 166 167 #ifdef DEBUG_DEQUANTIZATION 168 { 169 int32_t integerized_float_result; 170 float float_result; 171 172 float_result = dequant_float(raw, scale_factor, bits); 173 integerized_float_result = (int32_t)floor(0.5f + float_result * (1 << 15)); 174 175 /* This detects overflow */ 176 OI_ASSERT(((result >= 0) && (integerized_float_result >= 0)) || 177 ((result <= 0) && (integerized_float_result <= 0))); 178 } 179 #endif 180 return result >> (15 - scale_factor); 181 } 182 183 /* This version of Dequant does not incorporate the scaling factor of 1.38. It 184 * is intended for use with implementations of the filterbank which are 185 * hard-coded into a DSP. Output is Q16.4 format, so that after joint stereo 186 * processing (which leaves the most significant bit equal to the sign bit if 187 * the encoder is conformant) the result will fit a 24 bit fixed point signed 188 * value.*/ 189 190 INLINE int32_t OI_SBC_Dequant_Unscaled(uint32_t raw, OI_UINT scale_factor, 191 OI_UINT bits) { 192 uint32_t d; 193 int32_t result; 194 195 OI_ASSERT(scale_factor <= 15); 196 OI_ASSERT(bits <= 16); 197 198 if (bits <= 1) { 199 return 0; 200 } 201 if (bits == 16) { 202 result = (raw << 16) + raw - 0x7fff7fff; 203 return SCALE(result, 24 - scale_factor); 204 } 205 206 d = (raw * 2) + 1; 207 d *= dequant_long_unscaled[bits]; 208 result = d - 0x80000000; 209 210 return SCALE(result, 24 - scale_factor); 211 } 212 213 /** 214 @} 215 */ 216