pvmp3_huffman_decoding.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* ------------------------------------------------------------------
  2. * Copyright (C) 1998-2009 PacketVideo
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied.
  14. * See the License for the specific language governing permissions
  15. * and limitations under the License.
  16. * -------------------------------------------------------------------
  17. */
  18. /*
  19. ------------------------------------------------------------------------------
  20. PacketVideo Corp.
  21. MP3 Decoder Library
  22. Filename: pvmp3_huffman_decoding.cpp
  23. Funtions:
  24. pvmp3_huffman_quad_decoding
  25. pvmp3_huffman_pair_decoding
  26. pvmp3_huffman_pair_decoding_linbits
  27. Date: 09/21/2007
  28. ------------------------------------------------------------------------------
  29. REVISION HISTORY
  30. Description:
  31. ------------------------------------------------------------------------------
  32. INPUT AND OUTPUT DEFINITIONS
  33. Inputs:
  34. struct huffcodetab *h, pointer to huffman code record
  35. int32 *x, returns decoded x value
  36. int32 *y, returns decoded y value
  37. int32 *v, returns decoded v value (only in quad function)
  38. int32 *w, returns decoded w value (only in quad function)
  39. tbits *pMainData bit stream
  40. Outputs:
  41. ------------------------------------------------------------------------------
  42. FUNCTION DESCRIPTION
  43. These functions are used to decode huffman codewords from the input
  44. bitstream using combined binary search and look-up table approach.
  45. ------------------------------------------------------------------------------
  46. REQUIREMENTS
  47. ------------------------------------------------------------------------------
  48. REFERENCES
  49. [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
  50. ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
  51. ------------------------------------------------------------------------------
  52. PSEUDO-CODE
  53. ------------------------------------------------------------------------------
  54. */
  55. /*----------------------------------------------------------------------------
  56. ; INCLUDES
  57. ----------------------------------------------------------------------------*/
  58. #include "pvmp3_dec_defs.h"
  59. #include "pv_mp3_huffman.h"
  60. #include "pvmp3_getbits.h"
  61. /*----------------------------------------------------------------------------
  62. ; MACROS
  63. ; Define module specific macros here
  64. ----------------------------------------------------------------------------*/
  65. /*----------------------------------------------------------------------------
  66. ; DEFINES
  67. ; Include all pre-processor statements here. Include conditional
  68. ; compile variables also.
  69. ----------------------------------------------------------------------------*/
  70. /*----------------------------------------------------------------------------
  71. ; LOCAL FUNCTION DEFINITIONS
  72. ; Function Prototype declaration
  73. ----------------------------------------------------------------------------*/
  74. /*----------------------------------------------------------------------------
  75. ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
  76. ; Variable declaration - defined here and used outside this module
  77. ----------------------------------------------------------------------------*/
  78. /*----------------------------------------------------------------------------
  79. ; EXTERNAL FUNCTION REFERENCES
  80. ; Declare functions defined elsewhere and referenced in this module
  81. ----------------------------------------------------------------------------*/
  82. /*----------------------------------------------------------------------------
  83. ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
  84. ; Declare variables used in this module but defined elsewhere
  85. ----------------------------------------------------------------------------*/
  86. /*----------------------------------------------------------------------------
  87. ; FUNCTION CODE
  88. ----------------------------------------------------------------------------*/
  89. void pvmp3_huffman_quad_decoding(struct huffcodetab *h,
  90. int32 *is,
  91. tmp3Bits *pMainData)
  92. {
  93. int32 x;
  94. int32 y;
  95. int32 v;
  96. int32 w;
  97. y = (*h->pdec_huff_tab)(pMainData);
  98. if (y)
  99. {
  100. v = (y >> 3);
  101. if (v)
  102. {
  103. if (get1bit(pMainData))
  104. {
  105. v = -v;
  106. }
  107. }
  108. w = (y >> 2) & 1;
  109. if (w)
  110. {
  111. if (get1bit(pMainData))
  112. {
  113. w = -w;
  114. }
  115. }
  116. x = (y >> 1) & 1;
  117. if (x)
  118. {
  119. if (get1bit(pMainData))
  120. {
  121. x = -x;
  122. }
  123. }
  124. y = y & 1;
  125. if (y)
  126. {
  127. if (get1bit(pMainData))
  128. {
  129. y = -y;
  130. }
  131. }
  132. }
  133. else
  134. {
  135. v = 0;
  136. w = 0;
  137. x = 0;
  138. }
  139. *is = v;
  140. *(is + 1) = w;
  141. *(is + 2) = x;
  142. *(is + 3) = y;
  143. }
  144. void pvmp3_huffman_pair_decoding(struct huffcodetab *h, /* pointer to huffman code record */
  145. int32 *is,
  146. tmp3Bits *pMainData)
  147. {
  148. /* Lookup in Huffman table. */
  149. int32 x;
  150. int32 y;
  151. uint16 cw = (*h->pdec_huff_tab)(pMainData);
  152. /* Process sign and escape encodings for dual tables. */
  153. if (cw)
  154. {
  155. x = cw >> 4;
  156. if (x)
  157. {
  158. if (get1bit(pMainData))
  159. {
  160. x = -x;
  161. }
  162. y = cw & 0xf;
  163. if (y && get1bit(pMainData))
  164. {
  165. y = -y;
  166. }
  167. }
  168. else
  169. {
  170. y = cw & 0xf;
  171. if (get1bit(pMainData))
  172. {
  173. y = -y;
  174. }
  175. }
  176. *is = x;
  177. *(is + 1) = y;
  178. }
  179. else
  180. {
  181. *is = 0;
  182. *(is + 1) = 0;
  183. }
  184. }
  185. void pvmp3_huffman_pair_decoding_linbits(struct huffcodetab *h, /* pointer to huffman code record */
  186. int32 *is,
  187. tmp3Bits *pMainData)
  188. {
  189. int32 x;
  190. int32 y;
  191. uint16 cw;
  192. /* Lookup in Huffman table. */
  193. cw = (*h->pdec_huff_tab)(pMainData);
  194. x = cw >> 4;
  195. /* Process sign and escape encodings for dual tables. */
  196. if (15 == (uint32)x)
  197. {
  198. int32 tmp = getUpTo17bits(pMainData, (h->linbits + 1));
  199. x += tmp >> 1;
  200. if (tmp&1)
  201. {
  202. x = -x;
  203. }
  204. }
  205. else if (x)
  206. {
  207. if (get1bit(pMainData))
  208. {
  209. x = -x;
  210. }
  211. }
  212. y = cw & 0xf;
  213. if (15 == (uint32)y)
  214. {
  215. int32 tmp = getUpTo17bits(pMainData, (h->linbits + 1));
  216. y += tmp >> 1;
  217. if (tmp&1)
  218. {
  219. y = -y;
  220. }
  221. }
  222. else if (y)
  223. {
  224. if (get1bit(pMainData))
  225. {
  226. y = -y;
  227. }
  228. }
  229. *is = x;
  230. *(is + 1) = y;
  231. }