dequant.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $Revision: #1 $
  21. ***********************************************************************************/
  22. /**
  23. @file
  24. Dequantizer for SBC decoder; reconstructs quantized representation of subband samples.
  25. @ingroup codec_internal
  26. */
  27. /**
  28. @addtogroup codec_internal
  29. @{
  30. */
  31. /**
  32. This function is a fixed-point approximation of a modification of the following
  33. dequantization operation defined in the spec, as inferred from section 12.6.4:
  34. @code
  35. dequant = 2^(scale_factor+1) * ((raw * 2.0 + 1.0) / ((2^bits) - 1) - 1)
  36. 2 <= bits <= 16
  37. 0 <= raw < (2^bits)-1 (the -1 is because quantized values with all 1's are forbidden)
  38. -65535 < dequant < 65535
  39. @endcode
  40. The code below computes the dequantized value divided by a scaling constant
  41. equal to about 1.38. This constant is chosen to ensure that the entry in the
  42. dequant_long_scaled table for 16 bits is as accurate as possible, since it has
  43. the least relative precision available to it due to its small magnitude.
  44. This routine outputs in Q16.15 format.
  45. The helper array dequant_long is defined as follows:
  46. @code
  47. dequant_long_long[bits] = round(2^31 * 1/((2^bits - 1) / 1.38...) for 2 <= bits <= 16
  48. @endcode
  49. Additionally, the table entries have the following property:
  50. @code
  51. dequant_long_scaled[bits] <= 2^31 / ((2^bits - 1)) for 2 <= bits <= 16
  52. @endcode
  53. Therefore
  54. @code
  55. d = 2 * raw + 1 1 <= d <= 2^bits - 2
  56. d' = d * dequant_long[bits]
  57. d * dequant_long_scaled[bits] <= (2^bits - 2) * (2^31 / (2^bits - 1))
  58. d * dequant_long_scaled[bits] <= 2^31 * (2^bits - 2)/(2^bits - 1) < 2^31
  59. @endcode
  60. Therefore, d' doesn't overflow a signed 32-bit value.
  61. @code
  62. d' =~ 2^31 * (raw * 2.0 + 1.0) / (2^bits - 1) / 1.38...
  63. result = d' - 2^31/1.38... =~ 2^31 * ((raw * 2.0 + 1.0) / (2^bits - 1) - 1) / 1.38...
  64. result is therefore a scaled approximation to dequant. It remains only to
  65. turn 2^31 into 2^(scale_factor+1). Since we're aiming for Q16.15 format,
  66. this is achieved by shifting right by (15-scale_factor):
  67. (2^31 * x) >> (15-scale_factor) =~ 2^(31-15+scale_factor) * x = 2^15 * 2^(1+scale_factor) * x
  68. @endcode
  69. */
  70. #include "common/bt_target.h"
  71. #include <oi_codec_sbc_private.h>
  72. #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE)
  73. #ifndef SBC_DEQUANT_LONG_SCALED_OFFSET
  74. #define SBC_DEQUANT_LONG_SCALED_OFFSET 1555931970
  75. #endif
  76. #ifndef SBC_DEQUANT_LONG_UNSCALED_OFFSET
  77. #define SBC_DEQUANT_LONG_UNSCALED_OFFSET 2147483648
  78. #endif
  79. #ifndef SBC_DEQUANT_SCALING_FACTOR
  80. #define SBC_DEQUANT_SCALING_FACTOR 1.38019122262781f
  81. #endif
  82. const OI_UINT32 dequant_long_scaled[17];
  83. const OI_UINT32 dequant_long_unscaled[17];
  84. /** Scales x by y bits to the right, adding a rounding factor.
  85. */
  86. #ifndef SCALE
  87. #define SCALE(x, y) (((x) + (1 <<((y)-1))) >> (y))
  88. #endif
  89. #ifdef DEBUG_DEQUANTIZATION
  90. #include <math.h>
  91. static INLINE float dequant_float(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits)
  92. {
  93. float result = (1 << (scale_factor + 1)) * ((raw * 2.0f + 1.0f) / ((1 << bits) - 1.0f) - 1.0f);
  94. result /= SBC_DEQUANT_SCALING_FACTOR;
  95. /* Unless the encoder screwed up, all correct dequantized values should
  96. * satisfy this inequality. Non-compliant encoders which generate quantized
  97. * values with all 1-bits set can, theoretically, trigger this assert. This
  98. * is unlikely, however, and only an issue in debug mode.
  99. */
  100. OI_ASSERT(fabs(result) < 32768 * 1.6);
  101. return result;
  102. }
  103. #endif
  104. INLINE OI_INT32 OI_SBC_Dequant(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits)
  105. {
  106. OI_UINT32 d;
  107. OI_INT32 result;
  108. OI_ASSERT(scale_factor <= 15);
  109. OI_ASSERT(bits <= 16);
  110. if (bits <= 1) {
  111. return 0;
  112. }
  113. d = (raw * 2) + 1;
  114. d *= dequant_long_scaled[bits];
  115. result = d - SBC_DEQUANT_LONG_SCALED_OFFSET;
  116. #ifdef DEBUG_DEQUANTIZATION
  117. {
  118. OI_INT32 integerized_float_result;
  119. float float_result;
  120. float_result = dequant_float(raw, scale_factor, bits);
  121. integerized_float_result = (OI_INT32)floor(0.5f + float_result * (1 << 15));
  122. /* This detects overflow */
  123. OI_ASSERT(((result >= 0) && (integerized_float_result >= 0)) ||
  124. ((result <= 0) && (integerized_float_result <= 0)));
  125. }
  126. #endif
  127. return result >> (15 - scale_factor);
  128. }
  129. /* This version of Dequant does not incorporate the scaling factor of 1.38. It
  130. * is intended for use with implementations of the filterbank which are
  131. * hard-coded into a DSP. Output is Q16.4 format, so that after joint stereo
  132. * processing (which leaves the most significant bit equal to the sign bit if
  133. * the encoder is conformant) the result will fit a 24 bit fixed point signed
  134. * value.*/
  135. INLINE OI_INT32 OI_SBC_Dequant_Unscaled(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits)
  136. {
  137. OI_UINT32 d;
  138. OI_INT32 result;
  139. OI_ASSERT(scale_factor <= 15);
  140. OI_ASSERT(bits <= 16);
  141. if (bits <= 1) {
  142. return 0;
  143. }
  144. if (bits == 16) {
  145. result = (raw << 16) + raw - 0x7fff7fff;
  146. return SCALE(result, 24 - scale_factor);
  147. }
  148. d = (raw * 2) + 1;
  149. d *= dequant_long_unscaled[bits];
  150. result = d - 0x80000000;
  151. return SCALE(result, 24 - scale_factor);
  152. }
  153. /**
  154. @}
  155. */
  156. #endif /* #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE) */