/H264Dec/source/h264bsd_vlc.c

http://github.com/mbebenita/Broadway · C · 391 lines · 147 code · 49 blank · 195 comment · 40 complexity · 825bbc6bd0eeb2967503610a1a1d7408 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  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 express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*------------------------------------------------------------------------------
  17. Table of contents
  18. 1. Include headers
  19. 2. External compiler flags
  20. 3. Module defines
  21. 4. Local function prototypes
  22. 5. Functions
  23. h264bsdDecodeExpGolombUnsigned
  24. h264bsdDecodeExpGolombSigned
  25. h264bsdDecodeExpGolombMapped
  26. h264bsdDecodeExpGolombTruncated
  27. ------------------------------------------------------------------------------*/
  28. /*------------------------------------------------------------------------------
  29. 1. Include headers
  30. ------------------------------------------------------------------------------*/
  31. #include "h264bsd_vlc.h"
  32. #include "basetype.h"
  33. #include "h264bsd_stream.h"
  34. #include "h264bsd_util.h"
  35. /*------------------------------------------------------------------------------
  36. 2. External compiler flags
  37. --------------------------------------------------------------------------------
  38. --------------------------------------------------------------------------------
  39. 3. Module defines
  40. ------------------------------------------------------------------------------*/
  41. /* definition of special code num, this along with the return value is used
  42. * to handle code num in the range [0, 2^32] in the DecodeExpGolombUnsigned
  43. * function */
  44. #define BIG_CODE_NUM 0xFFFFFFFFU
  45. /* Mapping tables for coded_block_pattern, used for decoding of mapped
  46. * Exp-Golomb codes */
  47. static const u8 codedBlockPatternIntra4x4[48] = {
  48. 47,31,15,0,23,27,29,30,7,11,13,14,39,43,45,46,16,3,5,10,12,19,21,26,28,35,
  49. 37,42,44,1,2,4,8,17,18,20,24,6,9,22,25,32,33,34,36,40,38,41};
  50. static const u8 codedBlockPatternInter[48] = {
  51. 0,16,1,2,4,8,32,3,5,10,12,15,47,7,11,13,14,6,9,31,35,37,42,44,33,34,36,40,
  52. 39,43,45,46,17,18,20,24,19,21,26,28,23,27,29,30,22,25,38,41};
  53. /*------------------------------------------------------------------------------
  54. 4. Local function prototypes
  55. ------------------------------------------------------------------------------*/
  56. /*------------------------------------------------------------------------------
  57. 5.1 Function: h264bsdDecodeExpGolombUnsigned
  58. Functional description:
  59. Decode unsigned Exp-Golomb code. This is the same as codeNum used
  60. in other Exp-Golomb code mappings. Code num (i.e. the decoded
  61. symbol) is determined as
  62. codeNum = 2^leadingZeros - 1 + GetBits(leadingZeros)
  63. Normal decoded symbols are in the range [0, 2^32 - 2]. Symbol
  64. 2^32-1 is indicated by BIG_CODE_NUM with return value HANTRO_OK
  65. while symbol 2^32 is indicated by BIG_CODE_NUM with return value
  66. HANTRO_NOK. These two symbols are special cases with code length
  67. of 65, i.e. 32 '0' bits, a '1' bit, and either 0 or 1 represented
  68. by 32 bits.
  69. Symbol 2^32 is out of unsigned 32-bit range but is needed for
  70. DecodeExpGolombSigned to express value -2^31.
  71. Inputs:
  72. pStrmData pointer to stream data structure
  73. Outputs:
  74. codeNum decoded code word is stored here
  75. Returns:
  76. HANTRO_OK success
  77. HANTRO_NOK failure, no valid code word found, note exception
  78. with BIG_CODE_NUM
  79. ------------------------------------------------------------------------------*/
  80. u32 h264bsdDecodeExpGolombUnsigned(strmData_t *pStrmData, u32 *codeNum)
  81. {
  82. /* Variables */
  83. u32 bits, numZeros;
  84. /* Code */
  85. ASSERT(pStrmData);
  86. ASSERT(codeNum);
  87. bits = h264bsdShowBits32(pStrmData);
  88. /* first bit is 1 -> code length 1 */
  89. if (bits >= 0x80000000)
  90. {
  91. h264bsdFlushBits(pStrmData, 1);
  92. *codeNum = 0;
  93. return(HANTRO_OK);
  94. }
  95. /* second bit is 1 -> code length 3 */
  96. else if (bits >= 0x40000000)
  97. {
  98. if (h264bsdFlushBits(pStrmData, 3) == END_OF_STREAM)
  99. return(HANTRO_NOK);
  100. *codeNum = 1 + ((bits >> 29) & 0x1);
  101. return(HANTRO_OK);
  102. }
  103. /* third bit is 1 -> code length 5 */
  104. else if (bits >= 0x20000000)
  105. {
  106. if (h264bsdFlushBits(pStrmData, 5) == END_OF_STREAM)
  107. return(HANTRO_NOK);
  108. *codeNum = 3 + ((bits >> 27) & 0x3);
  109. return(HANTRO_OK);
  110. }
  111. /* fourth bit is 1 -> code length 7 */
  112. else if (bits >= 0x10000000)
  113. {
  114. if (h264bsdFlushBits(pStrmData, 7) == END_OF_STREAM)
  115. return(HANTRO_NOK);
  116. *codeNum = 7 + ((bits >> 25) & 0x7);
  117. return(HANTRO_OK);
  118. }
  119. /* other code lengths */
  120. else
  121. {
  122. #ifndef H264DEC_NEON
  123. numZeros = 4 + h264bsdCountLeadingZeros(bits, 28);
  124. #else
  125. numZeros = h264bsdCountLeadingZeros(bits);
  126. #endif
  127. /* all 32 bits are zero */
  128. if (numZeros == 32)
  129. {
  130. *codeNum = 0;
  131. h264bsdFlushBits(pStrmData,32);
  132. bits = h264bsdGetBits(pStrmData, 1);
  133. /* check 33rd bit, must be 1 */
  134. if (bits == 1)
  135. {
  136. /* cannot use h264bsdGetBits, limited to 31 bits */
  137. bits = h264bsdShowBits32(pStrmData);
  138. if (h264bsdFlushBits(pStrmData, 32) == END_OF_STREAM)
  139. return(HANTRO_NOK);
  140. /* code num 2^32 - 1, needed for unsigned mapping */
  141. if (bits == 0)
  142. {
  143. *codeNum = BIG_CODE_NUM;
  144. return(HANTRO_OK);
  145. }
  146. /* code num 2^32, needed for unsigned mapping
  147. * (results in -2^31) */
  148. else if (bits == 1)
  149. {
  150. *codeNum = BIG_CODE_NUM;
  151. return(HANTRO_NOK);
  152. }
  153. }
  154. /* if more zeros than 32, it is an error */
  155. return(HANTRO_NOK);
  156. }
  157. else
  158. h264bsdFlushBits(pStrmData,numZeros+1);
  159. bits = h264bsdGetBits(pStrmData, numZeros);
  160. if (bits == END_OF_STREAM)
  161. return(HANTRO_NOK);
  162. *codeNum = (1 << numZeros) - 1 + bits;
  163. }
  164. return(HANTRO_OK);
  165. }
  166. /*------------------------------------------------------------------------------
  167. 5.2 Function: h264bsdDecodeExpGolombSigned
  168. Functional description:
  169. Decode signed Exp-Golomb code. Code num is determined by
  170. h264bsdDecodeExpGolombUnsigned and then mapped to signed
  171. representation as
  172. symbol = (-1)^(codeNum+1) * (codeNum+1)/2
  173. Signed symbols shall be in the range [-2^31, 2^31 - 1]. Symbol
  174. -2^31 is obtained when codeNum is 2^32, which cannot be expressed
  175. by unsigned 32-bit value. This is signaled as a special case from
  176. the h264bsdDecodeExpGolombUnsigned by setting codeNum to
  177. BIG_CODE_NUM and returning HANTRO_NOK status.
  178. Inputs:
  179. pStrmData pointer to stream data structure
  180. Outputs:
  181. value decoded code word is stored here
  182. Returns:
  183. HANTRO_OK success
  184. HANTRO_NOK failure, no valid code word found
  185. ------------------------------------------------------------------------------*/
  186. u32 h264bsdDecodeExpGolombSigned(strmData_t *pStrmData, i32 *value)
  187. {
  188. /* Variables */
  189. u32 status, codeNum = 0;
  190. /* Code */
  191. ASSERT(pStrmData);
  192. ASSERT(value);
  193. status = h264bsdDecodeExpGolombUnsigned(pStrmData, &codeNum);
  194. if (codeNum == BIG_CODE_NUM)
  195. {
  196. /* BIG_CODE_NUM and HANTRO_OK status means codeNum 2^32-1 which would
  197. * result in signed integer valued 2^31 (i.e. out of 32-bit signed
  198. * integer range) */
  199. if (status == HANTRO_OK)
  200. return(HANTRO_NOK);
  201. /* BIG_CODE_NUM and HANTRO_NOK status means codeNum 2^32 which results
  202. * in signed integer valued -2^31 */
  203. else
  204. {
  205. *value = (i32)(2147483648U);
  206. return (HANTRO_OK);
  207. }
  208. }
  209. else if (status == HANTRO_OK)
  210. {
  211. /* (-1)^(codeNum+1) results in positive sign if codeNum is odd,
  212. * negative when it is even. (codeNum+1)/2 is obtained as
  213. * (codeNum+1)>>1 when value is positive and as (-codeNum)>>1 for
  214. * negative value */
  215. /*lint -e702 */
  216. *value = (codeNum & 0x1) ? (i32)((codeNum + 1) >> 1) :
  217. -(i32)((codeNum + 1) >> 1);
  218. /*lint +e702 */
  219. return(HANTRO_OK);
  220. }
  221. return(HANTRO_NOK);
  222. }
  223. /*------------------------------------------------------------------------------
  224. 5.3 Function: h264bsdDecodeExpGolombMapped
  225. Functional description:
  226. Decode mapped Exp-Golomb code. Code num is determined by
  227. h264bsdDecodeExpGolombUnsigned and then mapped to codedBlockPattern
  228. either for intra or inter macroblock. The mapping is implemented by
  229. look-up tables defined in the beginning of the file.
  230. Inputs:
  231. pStrmData pointer to stream data structure
  232. isIntra flag to indicate if intra or inter mapping is to
  233. be used
  234. Outputs:
  235. value decoded code word is stored here
  236. Returns:
  237. HANTRO_OK success
  238. HANTRO_NOK failure, no valid code word found
  239. ------------------------------------------------------------------------------*/
  240. u32 h264bsdDecodeExpGolombMapped(strmData_t *pStrmData, u32 *value,
  241. u32 isIntra)
  242. {
  243. /* Variables */
  244. u32 status, codeNum;
  245. /* Code */
  246. ASSERT(pStrmData);
  247. ASSERT(value);
  248. status = h264bsdDecodeExpGolombUnsigned(pStrmData, &codeNum);
  249. if (status != HANTRO_OK)
  250. return (HANTRO_NOK);
  251. else
  252. {
  253. /* range of valid codeNums [0,47] */
  254. if (codeNum > 47)
  255. return (HANTRO_NOK);
  256. if (isIntra)
  257. *value = codedBlockPatternIntra4x4[codeNum];
  258. else
  259. *value = codedBlockPatternInter[codeNum];
  260. return(HANTRO_OK);
  261. }
  262. }
  263. /*------------------------------------------------------------------------------
  264. 5.4 Function: h264bsdDecodeExpGolombTruncated
  265. Functional description:
  266. Decode truncated Exp-Golomb code. greaterThanOne flag indicates
  267. the range of the symbol to be decoded as follows:
  268. FALSE -> [0,1]
  269. TRUE -> [0,2^32-1]
  270. If flag is false the decoding is performed by reading one bit
  271. from the stream with h264bsdGetBits and mapping this to decoded
  272. symbol as
  273. symbol = bit ? 0 : 1
  274. Otherwise, i.e. when flag is TRUE, code num is determined by
  275. h264bsdDecodeExpGolombUnsigned and this is used as the decoded
  276. symbol.
  277. Inputs:
  278. pStrmData pointer to stream data structure
  279. greaterThanOne flag to indicate if range is wider than [0,1]
  280. Outputs:
  281. value decoded code word is stored here
  282. Returns:
  283. HANTRO_OK success
  284. HANTRO_NOK failure, no valid code word found
  285. ------------------------------------------------------------------------------*/
  286. u32 h264bsdDecodeExpGolombTruncated(
  287. strmData_t *pStrmData,
  288. u32 *value,
  289. u32 greaterThanOne)
  290. {
  291. /* Variables */
  292. /* Code */
  293. ASSERT(pStrmData);
  294. ASSERT(value);
  295. if (greaterThanOne)
  296. {
  297. return(h264bsdDecodeExpGolombUnsigned(pStrmData, value));
  298. }
  299. else
  300. {
  301. *value = h264bsdGetBits(pStrmData,1);
  302. if (*value == END_OF_STREAM)
  303. return (HANTRO_NOK);
  304. *value ^= 0x1;
  305. }
  306. return (HANTRO_OK);
  307. }