PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/H264Dec/source/h264bsd_cavlc.c

http://github.com/mbebenita/Broadway
C | 916 lines | 578 code | 137 blank | 201 comment | 134 complexity | 23feef226a18322eebbf45d3d4e9072e MD5 | raw file
Possible License(s): BSD-3-Clause
  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. DecodeCoeffToken
  24. DecodeLevelPrefix
  25. DecodeTotalZeros
  26. DecodeRunBefore
  27. DecodeResidualBlockCavlc
  28. ------------------------------------------------------------------------------*/
  29. /*------------------------------------------------------------------------------
  30. 1. Include headers
  31. ------------------------------------------------------------------------------*/
  32. #include "h264bsd_cavlc.h"
  33. #include "h264bsd_util.h"
  34. /*------------------------------------------------------------------------------
  35. 2. External compiler flags
  36. --------------------------------------------------------------------------------
  37. --------------------------------------------------------------------------------
  38. 3. Module defines
  39. ------------------------------------------------------------------------------*/
  40. /* Following descriptions use term "information field" to represent combination
  41. * of certain decoded symbol value and the length of the corresponding variable
  42. * length code word. For example, total_zeros information field consists of
  43. * 4 bits symbol value (bits [4,7]) along with four bits to represent length
  44. * of the VLC code word (bits [0,3]) */
  45. /* macro to obtain length of the coeff token information field, bits [0,4] */
  46. #define LENGTH_TC(vlc) ((vlc) & 0x1F)
  47. /* macro to obtain length of the other information fields, bits [0,3] */
  48. #define LENGTH(vlc) ((vlc) & 0xF)
  49. /* macro to obtain code word from the information fields, bits [4,7] */
  50. #define INFO(vlc) (((vlc) >> 4) & 0xF) /* 4 MSB bits contain information */
  51. /* macro to obtain trailing ones from the coeff token information word,
  52. * bits [5,10] */
  53. #define TRAILING_ONES(coeffToken) ((coeffToken>>5) & 0x3F)
  54. /* macro to obtain total coeff from the coeff token information word,
  55. * bits [11,15] */
  56. #define TOTAL_COEFF(coeffToken) (((coeffToken) >> 11) & 0x1F)
  57. #define VLC_NOT_FOUND 0xFFFFFFFEU
  58. /* VLC tables for coeff_token. Because of long codes (max. 16 bits) some of the
  59. * tables have been splitted into multiple separate tables. Each array/table
  60. * element has the following structure:
  61. * [5 bits for tot.coeff.] [6 bits for tr.ones] [5 bits for VLC length]
  62. * If there is a 0x0000 value, it means that there is not corresponding VLC
  63. * codeword for that index. */
  64. /* VLC lengths up to 6 bits, 0 <= nC < 2 */
  65. static const u16 coeffToken0_0[32] = {
  66. 0x0000,0x0000,0x0000,0x2066,0x1026,0x0806,0x1865,0x1865,
  67. 0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,
  68. 0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,
  69. 0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822,0x0822};
  70. /* VLC lengths up to 10 bits, 0 <= nC < 2 */
  71. static const u16 coeffToken0_1[48] = {
  72. 0x0000,0x0000,0x0000,0x0000,0x406a,0x304a,0x282a,0x200a,
  73. 0x3869,0x3869,0x2849,0x2849,0x2029,0x2029,0x1809,0x1809,
  74. 0x3068,0x3068,0x3068,0x3068,0x2048,0x2048,0x2048,0x2048,
  75. 0x1828,0x1828,0x1828,0x1828,0x1008,0x1008,0x1008,0x1008,
  76. 0x2867,0x2867,0x2867,0x2867,0x2867,0x2867,0x2867,0x2867,
  77. 0x1847,0x1847,0x1847,0x1847,0x1847,0x1847,0x1847,0x1847};
  78. /* VLC lengths up to 14 bits, 0 <= nC < 2 */
  79. static const u16 coeffToken0_2[56] = {
  80. 0x606e,0x584e,0x502e,0x500e,0x586e,0x504e,0x482e,0x480e,
  81. 0x400d,0x400d,0x484d,0x484d,0x402d,0x402d,0x380d,0x380d,
  82. 0x506d,0x506d,0x404d,0x404d,0x382d,0x382d,0x300d,0x300d,
  83. 0x486b,0x486b,0x486b,0x486b,0x486b,0x486b,0x486b,0x486b,
  84. 0x384b,0x384b,0x384b,0x384b,0x384b,0x384b,0x384b,0x384b,
  85. 0x302b,0x302b,0x302b,0x302b,0x302b,0x302b,0x302b,0x302b,
  86. 0x280b,0x280b,0x280b,0x280b,0x280b,0x280b,0x280b,0x280b};
  87. /* VLC lengths up to 16 bits, 0 <= nC < 2 */
  88. static const u16 coeffToken0_3[32] = {
  89. 0x0000,0x0000,0x682f,0x682f,0x8010,0x8050,0x8030,0x7810,
  90. 0x8070,0x7850,0x7830,0x7010,0x7870,0x7050,0x7030,0x6810,
  91. 0x706f,0x706f,0x684f,0x684f,0x602f,0x602f,0x600f,0x600f,
  92. 0x686f,0x686f,0x604f,0x604f,0x582f,0x582f,0x580f,0x580f};
  93. /* VLC lengths up to 6 bits, 2 <= nC < 4 */
  94. static const u16 coeffToken2_0[32] = {
  95. 0x0000,0x0000,0x0000,0x0000,0x3866,0x2046,0x2026,0x1006,
  96. 0x3066,0x1846,0x1826,0x0806,0x2865,0x2865,0x1025,0x1025,
  97. 0x2064,0x2064,0x2064,0x2064,0x1864,0x1864,0x1864,0x1864,
  98. 0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043,0x1043};
  99. /* VLC lengths up to 9 bits, 2 <= nC < 4 */
  100. static const u16 coeffToken2_1[32] = {
  101. 0x0000,0x0000,0x0000,0x0000,0x4869,0x3849,0x3829,0x3009,
  102. 0x2808,0x2808,0x3048,0x3048,0x3028,0x3028,0x2008,0x2008,
  103. 0x4067,0x4067,0x4067,0x4067,0x2847,0x2847,0x2847,0x2847,
  104. 0x2827,0x2827,0x2827,0x2827,0x1807,0x1807,0x1807,0x1807};
  105. /* VLC lengths up to 14 bits, 2 <= nC < 4 */
  106. static const u16 coeffToken2_2[128] = {
  107. 0x0000,0x0000,0x786d,0x786d,0x806e,0x804e,0x802e,0x800e,
  108. 0x782e,0x780e,0x784e,0x702e,0x704d,0x704d,0x700d,0x700d,
  109. 0x706d,0x706d,0x684d,0x684d,0x682d,0x682d,0x680d,0x680d,
  110. 0x686d,0x686d,0x604d,0x604d,0x602d,0x602d,0x600d,0x600d,
  111. 0x580c,0x580c,0x580c,0x580c,0x584c,0x584c,0x584c,0x584c,
  112. 0x582c,0x582c,0x582c,0x582c,0x500c,0x500c,0x500c,0x500c,
  113. 0x606c,0x606c,0x606c,0x606c,0x504c,0x504c,0x504c,0x504c,
  114. 0x502c,0x502c,0x502c,0x502c,0x480c,0x480c,0x480c,0x480c,
  115. 0x586b,0x586b,0x586b,0x586b,0x586b,0x586b,0x586b,0x586b,
  116. 0x484b,0x484b,0x484b,0x484b,0x484b,0x484b,0x484b,0x484b,
  117. 0x482b,0x482b,0x482b,0x482b,0x482b,0x482b,0x482b,0x482b,
  118. 0x400b,0x400b,0x400b,0x400b,0x400b,0x400b,0x400b,0x400b,
  119. 0x506b,0x506b,0x506b,0x506b,0x506b,0x506b,0x506b,0x506b,
  120. 0x404b,0x404b,0x404b,0x404b,0x404b,0x404b,0x404b,0x404b,
  121. 0x402b,0x402b,0x402b,0x402b,0x402b,0x402b,0x402b,0x402b,
  122. 0x380b,0x380b,0x380b,0x380b,0x380b,0x380b,0x380b,0x380b};
  123. /* VLC lengths up to 6 bits, 4 <= nC < 8 */
  124. static const u16 coeffToken4_0[64] = {
  125. 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  126. 0x1806,0x3846,0x3826,0x1006,0x4866,0x3046,0x3026,0x0806,
  127. 0x2825,0x2825,0x2845,0x2845,0x2025,0x2025,0x2045,0x2045,
  128. 0x1825,0x1825,0x4065,0x4065,0x1845,0x1845,0x1025,0x1025,
  129. 0x3864,0x3864,0x3864,0x3864,0x3064,0x3064,0x3064,0x3064,
  130. 0x2864,0x2864,0x2864,0x2864,0x2064,0x2064,0x2064,0x2064,
  131. 0x1864,0x1864,0x1864,0x1864,0x1044,0x1044,0x1044,0x1044,
  132. 0x0824,0x0824,0x0824,0x0824,0x0004,0x0004,0x0004,0x0004};
  133. /* VLC lengths up to 10 bits, 4 <= nC < 8 */
  134. static const u16 coeffToken4_1[128] = {
  135. 0x0000,0x800a,0x806a,0x804a,0x802a,0x780a,0x786a,0x784a,
  136. 0x782a,0x700a,0x706a,0x704a,0x702a,0x680a,0x6829,0x6829,
  137. 0x6009,0x6009,0x6849,0x6849,0x6029,0x6029,0x5809,0x5809,
  138. 0x6869,0x6869,0x6049,0x6049,0x5829,0x5829,0x5009,0x5009,
  139. 0x6068,0x6068,0x6068,0x6068,0x5848,0x5848,0x5848,0x5848,
  140. 0x5028,0x5028,0x5028,0x5028,0x4808,0x4808,0x4808,0x4808,
  141. 0x5868,0x5868,0x5868,0x5868,0x5048,0x5048,0x5048,0x5048,
  142. 0x4828,0x4828,0x4828,0x4828,0x4008,0x4008,0x4008,0x4008,
  143. 0x3807,0x3807,0x3807,0x3807,0x3807,0x3807,0x3807,0x3807,
  144. 0x3007,0x3007,0x3007,0x3007,0x3007,0x3007,0x3007,0x3007,
  145. 0x4847,0x4847,0x4847,0x4847,0x4847,0x4847,0x4847,0x4847,
  146. 0x2807,0x2807,0x2807,0x2807,0x2807,0x2807,0x2807,0x2807,
  147. 0x5067,0x5067,0x5067,0x5067,0x5067,0x5067,0x5067,0x5067,
  148. 0x4047,0x4047,0x4047,0x4047,0x4047,0x4047,0x4047,0x4047,
  149. 0x4027,0x4027,0x4027,0x4027,0x4027,0x4027,0x4027,0x4027,
  150. 0x2007,0x2007,0x2007,0x2007,0x2007,0x2007,0x2007,0x2007};
  151. /* fixed 6 bit length VLC, nC <= 8 */
  152. static const u16 coeffToken8[64] = {
  153. 0x0806,0x0826,0x0000,0x0006,0x1006,0x1026,0x1046,0x0000,
  154. 0x1806,0x1826,0x1846,0x1866,0x2006,0x2026,0x2046,0x2066,
  155. 0x2806,0x2826,0x2846,0x2866,0x3006,0x3026,0x3046,0x3066,
  156. 0x3806,0x3826,0x3846,0x3866,0x4006,0x4026,0x4046,0x4066,
  157. 0x4806,0x4826,0x4846,0x4866,0x5006,0x5026,0x5046,0x5066,
  158. 0x5806,0x5826,0x5846,0x5866,0x6006,0x6026,0x6046,0x6066,
  159. 0x6806,0x6826,0x6846,0x6866,0x7006,0x7026,0x7046,0x7066,
  160. 0x7806,0x7826,0x7846,0x7866,0x8006,0x8026,0x8046,0x8066};
  161. /* VLC lengths up to 3 bits, nC == -1 */
  162. static const u16 coeffTokenMinus1_0[8] = {
  163. 0x0000,0x1043,0x0002,0x0002,0x0821,0x0821,0x0821,0x0821};
  164. /* VLC lengths up to 8 bits, nC == -1 */
  165. static const u16 coeffTokenMinus1_1[32] = {
  166. 0x2067,0x2067,0x2048,0x2028,0x1847,0x1847,0x1827,0x1827,
  167. 0x2006,0x2006,0x2006,0x2006,0x1806,0x1806,0x1806,0x1806,
  168. 0x1006,0x1006,0x1006,0x1006,0x1866,0x1866,0x1866,0x1866,
  169. 0x1026,0x1026,0x1026,0x1026,0x0806,0x0806,0x0806,0x0806};
  170. /* VLC tables for total_zeros. One table containing longer code, totalZeros_1,
  171. * has been broken into two separate tables. Table elements have the
  172. * following structure:
  173. * [4 bits for info] [4 bits for VLC length] */
  174. /* VLC lengths up to 5 bits */
  175. static const u8 totalZeros_1_0[32] = {
  176. 0x00,0x00,0x65,0x55,0x44,0x44,0x34,0x34,
  177. 0x23,0x23,0x23,0x23,0x13,0x13,0x13,0x13,
  178. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  179. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};
  180. /* VLC lengths up to 9 bits */
  181. static const u8 totalZeros_1_1[32] = {
  182. 0x00,0xf9,0xe9,0xd9,0xc8,0xc8,0xb8,0xb8,
  183. 0xa7,0xa7,0xa7,0xa7,0x97,0x97,0x97,0x97,
  184. 0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,
  185. 0x76,0x76,0x76,0x76,0x76,0x76,0x76,0x76};
  186. static const u8 totalZeros_2[64] = {
  187. 0xe6,0xd6,0xc6,0xb6,0xa5,0xa5,0x95,0x95,
  188. 0x84,0x84,0x84,0x84,0x74,0x74,0x74,0x74,
  189. 0x64,0x64,0x64,0x64,0x54,0x54,0x54,0x54,
  190. 0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,
  191. 0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
  192. 0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
  193. 0x13,0x13,0x13,0x13,0x13,0x13,0x13,0x13,
  194. 0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03};
  195. static const u8 totalZeros_3[64] = {
  196. 0xd6,0xb6,0xc5,0xc5,0xa5,0xa5,0x95,0x95,
  197. 0x84,0x84,0x84,0x84,0x54,0x54,0x54,0x54,
  198. 0x44,0x44,0x44,0x44,0x04,0x04,0x04,0x04,
  199. 0x73,0x73,0x73,0x73,0x73,0x73,0x73,0x73,
  200. 0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
  201. 0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
  202. 0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
  203. 0x13,0x13,0x13,0x13,0x13,0x13,0x13,0x13};
  204. static const u8 totalZeros_4[32] = {
  205. 0xc5,0xb5,0xa5,0x05,0x94,0x94,0x74,0x74,
  206. 0x34,0x34,0x24,0x24,0x83,0x83,0x83,0x83,
  207. 0x63,0x63,0x63,0x63,0x53,0x53,0x53,0x53,
  208. 0x43,0x43,0x43,0x43,0x13,0x13,0x13,0x13};
  209. static const u8 totalZeros_5[32] = {
  210. 0xb5,0x95,0xa4,0xa4,0x84,0x84,0x24,0x24,
  211. 0x14,0x14,0x04,0x04,0x73,0x73,0x73,0x73,
  212. 0x63,0x63,0x63,0x63,0x53,0x53,0x53,0x53,
  213. 0x43,0x43,0x43,0x43,0x33,0x33,0x33,0x33};
  214. static const u8 totalZeros_6[64] = {
  215. 0xa6,0x06,0x15,0x15,0x84,0x84,0x84,0x84,
  216. 0x93,0x93,0x93,0x93,0x93,0x93,0x93,0x93,
  217. 0x73,0x73,0x73,0x73,0x73,0x73,0x73,0x73,
  218. 0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
  219. 0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x53,
  220. 0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,
  221. 0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
  222. 0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23};
  223. static const u8 totalZeros_7[64] = {
  224. 0x96,0x06,0x15,0x15,0x74,0x74,0x74,0x74,
  225. 0x83,0x83,0x83,0x83,0x83,0x83,0x83,0x83,
  226. 0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
  227. 0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,
  228. 0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
  229. 0x23,0x23,0x23,0x23,0x23,0x23,0x23,0x23,
  230. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
  231. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52};
  232. static const u8 totalZeros_8[64] = {
  233. 0x86,0x06,0x25,0x25,0x14,0x14,0x14,0x14,
  234. 0x73,0x73,0x73,0x73,0x73,0x73,0x73,0x73,
  235. 0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
  236. 0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,
  237. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
  238. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
  239. 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
  240. 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42};
  241. static const u8 totalZeros_9[64] = {
  242. 0x16,0x06,0x75,0x75,0x24,0x24,0x24,0x24,
  243. 0x53,0x53,0x53,0x53,0x53,0x53,0x53,0x53,
  244. 0x62,0x62,0x62,0x62,0x62,0x62,0x62,0x62,
  245. 0x62,0x62,0x62,0x62,0x62,0x62,0x62,0x62,
  246. 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
  247. 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
  248. 0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32,
  249. 0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32};
  250. static const u8 totalZeros_10[32] = {
  251. 0x15,0x05,0x64,0x64,0x23,0x23,0x23,0x23,
  252. 0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x52,
  253. 0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
  254. 0x32,0x32,0x32,0x32,0x32,0x32,0x32,0x32};
  255. static const u8 totalZeros_11[16] = {
  256. 0x04,0x14,0x23,0x23,0x33,0x33,0x53,0x53,
  257. 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41};
  258. static const u8 totalZeros_12[16] = {
  259. 0x04,0x14,0x43,0x43,0x22,0x22,0x22,0x22,
  260. 0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31};
  261. static const u8 totalZeros_13[8] = {0x03,0x13,0x32,0x32,0x21,0x21,0x21,0x21};
  262. static const u8 totalZeros_14[4] = {0x02,0x12,0x21,0x21};
  263. /* VLC tables for run_before. Table elements have the following structure:
  264. * [4 bits for info] [4bits for VLC length]
  265. */
  266. static const u8 runBefore_6[8] = {0x13,0x23,0x43,0x33,0x63,0x53,0x02,0x02};
  267. static const u8 runBefore_5[8] = {0x53,0x43,0x33,0x23,0x12,0x12,0x02,0x02};
  268. static const u8 runBefore_4[8] = {0x43,0x33,0x22,0x22,0x12,0x12,0x02,0x02};
  269. static const u8 runBefore_3[4] = {0x32,0x22,0x12,0x02};
  270. static const u8 runBefore_2[4] = {0x22,0x12,0x01,0x01};
  271. static const u8 runBefore_1[2] = {0x11,0x01};
  272. /* following four macros are used to handle stream buffer "cache" in the CAVLC
  273. * decoding function */
  274. /* macro to initialize stream buffer cache, fills the buffer (32 bits) */
  275. #define BUFFER_INIT(value, bits) \
  276. { \
  277. bits = 32; \
  278. value = h264bsdShowBits32(pStrmData); \
  279. }
  280. /* macro to read numBits bits from the buffer, bits will be written to
  281. * outVal. Refills the buffer if not enough bits left */
  282. #define BUFFER_SHOW(value, bits, outVal, numBits) \
  283. { \
  284. if (bits < (numBits)) \
  285. { \
  286. if(h264bsdFlushBits(pStrmData,32-bits) == END_OF_STREAM) \
  287. return(HANTRO_NOK); \
  288. value = h264bsdShowBits32(pStrmData); \
  289. bits = 32; \
  290. } \
  291. (outVal) = value >> (32 - (numBits)); \
  292. }
  293. /* macro to flush numBits bits from the buffer */
  294. #define BUFFER_FLUSH(value, bits, numBits) \
  295. { \
  296. value <<= (numBits); \
  297. bits -= (numBits); \
  298. }
  299. /* macro to read and flush numBits bits from the buffer, bits will be written
  300. * to outVal. Refills the buffer if not enough bits left */
  301. #define BUFFER_GET(value, bits, outVal, numBits) \
  302. { \
  303. if (bits < (numBits)) \
  304. { \
  305. if(h264bsdFlushBits(pStrmData,32-bits) == END_OF_STREAM) \
  306. return(HANTRO_NOK); \
  307. value = h264bsdShowBits32(pStrmData); \
  308. bits = 32; \
  309. } \
  310. (outVal) = value >> (32 - (numBits)); \
  311. value <<= (numBits); \
  312. bits -= (numBits); \
  313. }
  314. /*------------------------------------------------------------------------------
  315. 4. Local function prototypes
  316. ------------------------------------------------------------------------------*/
  317. static u32 DecodeCoeffToken(u32 bits, u32 nc);
  318. static u32 DecodeLevelPrefix(u32 bits);
  319. static u32 DecodeTotalZeros(u32 bits, u32 totalCoeff, u32 isChromaDC);
  320. static u32 DecodeRunBefore(u32 bits,u32 zerosLeft);
  321. /*------------------------------------------------------------------------------
  322. Function: DecodeCoeffToken
  323. Functional description:
  324. Function to decode coeff_token information field from the stream.
  325. Inputs:
  326. u32 bits next 16 stream bits
  327. u32 nc nC, see standard for details
  328. Outputs:
  329. u32 information field (11 bits for value, 5 bits for length)
  330. ------------------------------------------------------------------------------*/
  331. u32 DecodeCoeffToken(u32 bits, u32 nc)
  332. {
  333. /* Variables */
  334. u32 value;
  335. /* Code */
  336. /* standard defines that nc for decoding of chroma dc coefficients is -1,
  337. * represented by u32 here -> -1 maps to 2^32 - 1 */
  338. ASSERT(nc <= 16 || nc == (u32)(-1));
  339. if (nc < 2)
  340. {
  341. if (bits >= 0x8000)
  342. {
  343. value = 0x0001;
  344. }
  345. else if (bits >= 0x0C00)
  346. value = coeffToken0_0[bits >> 10];
  347. else if (bits >= 0x0100)
  348. value = coeffToken0_1[bits >> 6];
  349. else if (bits >= 0x0020)
  350. value = coeffToken0_2[(bits>>2)-8];
  351. else
  352. value = coeffToken0_3[bits];
  353. }
  354. else if (nc < 4)
  355. {
  356. if (bits >= 0x8000)
  357. {
  358. value = bits & 0x4000 ? 0x0002 : 0x0822;
  359. }
  360. else if (bits >= 0x1000)
  361. value = coeffToken2_0[bits >> 10];
  362. else if (bits >= 0x0200)
  363. value = coeffToken2_1[bits >> 7];
  364. else
  365. value = coeffToken2_2[bits>>2];
  366. }
  367. else if (nc < 8)
  368. {
  369. value = coeffToken4_0[bits >> 10];
  370. if (!value)
  371. value = coeffToken4_1[bits>>6];
  372. }
  373. else if (nc <= 16)
  374. {
  375. value = coeffToken8[bits>>10];
  376. }
  377. else
  378. {
  379. value = coeffTokenMinus1_0[bits >> 13];
  380. if (!value)
  381. value = coeffTokenMinus1_1[bits>>8];
  382. }
  383. return(value);
  384. }
  385. /*------------------------------------------------------------------------------
  386. Function: DecodeLevelPrefix
  387. Functional description:
  388. Function to decode level_prefix information field from the stream
  389. Inputs:
  390. u32 bits next 16 stream bits
  391. Outputs:
  392. u32 level_prefix information field or VLC_NOT_FOUND
  393. ------------------------------------------------------------------------------*/
  394. u32 DecodeLevelPrefix(u32 bits)
  395. {
  396. /* Variables */
  397. u32 numZeros;
  398. /* Code */
  399. if (bits >= 0x8000)
  400. numZeros = 0;
  401. else if (bits >= 0x4000)
  402. numZeros = 1;
  403. else if (bits >= 0x2000)
  404. numZeros = 2;
  405. else if (bits >= 0x1000)
  406. numZeros = 3;
  407. else if (bits >= 0x0800)
  408. numZeros = 4;
  409. else if (bits >= 0x0400)
  410. numZeros = 5;
  411. else if (bits >= 0x0200)
  412. numZeros = 6;
  413. else if (bits >= 0x0100)
  414. numZeros = 7;
  415. else if (bits >= 0x0080)
  416. numZeros = 8;
  417. else if (bits >= 0x0040)
  418. numZeros = 9;
  419. else if (bits >= 0x0020)
  420. numZeros = 10;
  421. else if (bits >= 0x0010)
  422. numZeros = 11;
  423. else if (bits >= 0x0008)
  424. numZeros = 12;
  425. else if (bits >= 0x0004)
  426. numZeros = 13;
  427. else if (bits >= 0x0002)
  428. numZeros = 14;
  429. else if (bits >= 0x0001)
  430. numZeros = 15;
  431. else /* more than 15 zeros encountered which is an error */
  432. return(VLC_NOT_FOUND);
  433. return(numZeros);
  434. }
  435. /*------------------------------------------------------------------------------
  436. Function: DecodeTotalZeros
  437. Functional description:
  438. Function to decode total_zeros information field from the stream
  439. Inputs:
  440. u32 bits next 9 stream bits
  441. u32 totalCoeff total number of coefficients for the block
  442. being decoded
  443. u32 isChromaDC flag to indicate chroma DC block
  444. Outputs:
  445. u32 information field (4 bits value, 4 bits length)
  446. ------------------------------------------------------------------------------*/
  447. u32 DecodeTotalZeros(u32 bits, u32 totalCoeff, u32 isChromaDC)
  448. {
  449. /* Variables */
  450. u32 value = 0x0;
  451. /* Code */
  452. ASSERT(totalCoeff);
  453. if (!isChromaDC)
  454. {
  455. ASSERT(totalCoeff < 16);
  456. switch (totalCoeff)
  457. {
  458. case 1:
  459. value = totalZeros_1_0[bits >> 4];
  460. if (!value)
  461. value = totalZeros_1_1[bits];
  462. break;
  463. case 2:
  464. value = totalZeros_2[bits >> 3];
  465. break;
  466. case 3:
  467. value = totalZeros_3[bits >> 3];
  468. break;
  469. case 4:
  470. value = totalZeros_4[bits >> 4];
  471. break;
  472. case 5:
  473. value = totalZeros_5[bits >> 4];
  474. break;
  475. case 6:
  476. value = totalZeros_6[bits >> 3];
  477. break;
  478. case 7:
  479. value = totalZeros_7[bits >> 3];
  480. break;
  481. case 8:
  482. value = totalZeros_8[bits >> 3];
  483. break;
  484. case 9:
  485. value = totalZeros_9[bits >> 3];
  486. break;
  487. case 10:
  488. value = totalZeros_10[bits >> 4];
  489. break;
  490. case 11:
  491. value = totalZeros_11[bits >> 5];
  492. break;
  493. case 12:
  494. value = totalZeros_12[bits >> 5];
  495. break;
  496. case 13:
  497. value = totalZeros_13[bits >> 6];
  498. break;
  499. case 14:
  500. value = totalZeros_14[bits >> 7];
  501. break;
  502. default: /* case 15 */
  503. value = (bits >> 8) ? 0x11 : 0x01;
  504. break;
  505. }
  506. }
  507. else
  508. {
  509. ASSERT(totalCoeff < 4);
  510. bits >>= 6;
  511. if (bits > 3)
  512. value = 0x01;
  513. else
  514. {
  515. if (totalCoeff == 3)
  516. value = 0x11;
  517. else if (bits > 1)
  518. {
  519. value = 0x12;
  520. }
  521. else if (totalCoeff == 2)
  522. value = 0x22;
  523. else if (bits)
  524. value = 0x23;
  525. else
  526. value = 0x33;
  527. }
  528. }
  529. return(value);
  530. }
  531. /*------------------------------------------------------------------------------
  532. Function: DecodeRunBefore
  533. Functional description:
  534. Function to decode run_before information field from the stream
  535. Inputs:
  536. u32 bits next 11 stream bits
  537. u32 zerosLeft number of zeros left for the current block
  538. Outputs:
  539. u32 information field (4 bits value, 4 bits length)
  540. ------------------------------------------------------------------------------*/
  541. u32 DecodeRunBefore(u32 bits, u32 zerosLeft)
  542. {
  543. /* Variables */
  544. u32 value = 0x0;
  545. /* Code */
  546. switch (zerosLeft)
  547. {
  548. case 1:
  549. value = runBefore_1[bits>>10];
  550. break;
  551. case 2:
  552. value = runBefore_2[bits>>9];
  553. break;
  554. case 3:
  555. value = runBefore_3[bits>>9];
  556. break;
  557. case 4:
  558. value = runBefore_4[bits>>8];
  559. break;
  560. case 5:
  561. value = runBefore_5[bits>>8];
  562. break;
  563. case 6:
  564. value = runBefore_6[bits>>8];
  565. break;
  566. default:
  567. if (bits >= 0x100)
  568. value = ((7-(bits>>8))<<4)+0x3;
  569. else if (bits >= 0x80)
  570. value = 0x74;
  571. else if (bits >= 0x40)
  572. value = 0x85;
  573. else if (bits >= 0x20)
  574. value = 0x96;
  575. else if (bits >= 0x10)
  576. value = 0xa7;
  577. else if (bits >= 0x8)
  578. value = 0xb8;
  579. else if (bits >= 0x4)
  580. value = 0xc9;
  581. else if (bits >= 0x2)
  582. value = 0xdA;
  583. else if (bits)
  584. value = 0xeB;
  585. if (INFO(value) > zerosLeft)
  586. value = 0;
  587. break;
  588. }
  589. return(value);
  590. }
  591. /*------------------------------------------------------------------------------
  592. Function: DecodeResidualBlockCavlc
  593. Functional description:
  594. Function to decode one CAVLC coded block. This corresponds to
  595. syntax elements residual_block_cavlc() in the standard.
  596. Inputs:
  597. pStrmData pointer to stream data structure
  598. nc nC value
  599. maxNumCoeff maximum number of residual coefficients
  600. Outputs:
  601. coeffLevel stores decoded coefficient levels
  602. Returns:
  603. numCoeffs on bits [4,11] if successful
  604. coeffMap on bits [16,31] if successful, this is bit map
  605. where each bit indicates if the corresponding
  606. coefficient was zero (0) or non-zero (1)
  607. HANTRO_NOK end of stream or error in stream
  608. ------------------------------------------------------------------------------*/
  609. u32 h264bsdDecodeResidualBlockCavlc(
  610. strmData_t *pStrmData,
  611. i32 *coeffLevel,
  612. i32 nc,
  613. u32 maxNumCoeff)
  614. {
  615. /* Variables */
  616. u32 i, tmp, totalCoeff, trailingOnes, suffixLength, levelPrefix;
  617. u32 levelSuffix, zerosLeft, bit;
  618. i32 level[16];
  619. u32 run[16];
  620. /* stream "cache" */
  621. u32 bufferValue;
  622. u32 bufferBits;
  623. /* Code */
  624. ASSERT(pStrmData);
  625. ASSERT(coeffLevel);
  626. ASSERT(nc > -2);
  627. ASSERT(maxNumCoeff == 4 || maxNumCoeff == 15 || maxNumCoeff == 16);
  628. ASSERT(VLC_NOT_FOUND != END_OF_STREAM);
  629. /* assume that coeffLevel array has been "cleaned" by caller */
  630. BUFFER_INIT(bufferValue, bufferBits);
  631. /*lint -e774 disable lint warning on always false comparison */
  632. BUFFER_SHOW(bufferValue, bufferBits, bit, 16);
  633. /*lint +e774 */
  634. tmp = DecodeCoeffToken(bit, (u32)nc);
  635. if (!tmp)
  636. return(HANTRO_NOK);
  637. BUFFER_FLUSH(bufferValue, bufferBits, LENGTH_TC(tmp));
  638. totalCoeff = TOTAL_COEFF(tmp);
  639. if (totalCoeff > maxNumCoeff)
  640. return(HANTRO_NOK);
  641. trailingOnes = TRAILING_ONES(tmp);
  642. if (totalCoeff != 0)
  643. {
  644. i = 0;
  645. /* nonzero coefficients: +/- 1 */
  646. if (trailingOnes)
  647. {
  648. BUFFER_GET(bufferValue, bufferBits, bit, trailingOnes);
  649. tmp = 1 << (trailingOnes - 1);
  650. for (; tmp; i++)
  651. {
  652. level[i] = bit & tmp ? -1 : 1;
  653. tmp >>= 1;
  654. }
  655. }
  656. /* other levels */
  657. if (totalCoeff > 10 && trailingOnes < 3)
  658. suffixLength = 1;
  659. else
  660. suffixLength = 0;
  661. for (; i < totalCoeff; i++)
  662. {
  663. BUFFER_SHOW(bufferValue, bufferBits, bit, 16);
  664. levelPrefix = DecodeLevelPrefix(bit);
  665. if (levelPrefix == VLC_NOT_FOUND)
  666. return(HANTRO_NOK);
  667. BUFFER_FLUSH(bufferValue, bufferBits, levelPrefix+1);
  668. if (levelPrefix < 14)
  669. tmp = suffixLength;
  670. else if (levelPrefix == 14)
  671. {
  672. tmp = suffixLength ? suffixLength : 4;
  673. }
  674. else
  675. {
  676. /* setting suffixLength to 1 here corresponds to adding 15
  677. * to levelCode value if levelPrefix == 15 and
  678. * suffixLength == 0 */
  679. if (!suffixLength)
  680. suffixLength = 1;
  681. tmp = 12;
  682. }
  683. if (suffixLength)
  684. levelPrefix <<= suffixLength;
  685. if (tmp)
  686. {
  687. BUFFER_GET(bufferValue, bufferBits, levelSuffix, tmp);
  688. levelPrefix += levelSuffix;
  689. }
  690. tmp = levelPrefix;
  691. if (i == trailingOnes && trailingOnes < 3)
  692. tmp += 2;
  693. level[i] = (tmp+2)>>1;
  694. if (suffixLength == 0)
  695. suffixLength = 1;
  696. if ((level[i] > (3 << (suffixLength - 1))) && suffixLength < 6)
  697. suffixLength++;
  698. if (tmp & 0x1)
  699. level[i] = -level[i];
  700. }
  701. /* zero runs */
  702. if (totalCoeff < maxNumCoeff)
  703. {
  704. BUFFER_SHOW(bufferValue, bufferBits, bit,9);
  705. zerosLeft = DecodeTotalZeros(bit, totalCoeff,
  706. (u32)(maxNumCoeff == 4));
  707. if (!zerosLeft)
  708. return(HANTRO_NOK);
  709. BUFFER_FLUSH(bufferValue, bufferBits, LENGTH(zerosLeft));
  710. zerosLeft = INFO(zerosLeft);
  711. }
  712. else
  713. zerosLeft = 0;
  714. for (i = 0; i < totalCoeff - 1; i++)
  715. {
  716. if (zerosLeft > 0)
  717. {
  718. BUFFER_SHOW(bufferValue, bufferBits, bit,11);
  719. tmp = DecodeRunBefore(bit, zerosLeft);
  720. if (!tmp)
  721. return(HANTRO_NOK);
  722. BUFFER_FLUSH(bufferValue, bufferBits, LENGTH(tmp));
  723. run[i] = INFO(tmp);
  724. zerosLeft -= run[i]++;
  725. }
  726. else
  727. {
  728. run[i] = 1;
  729. }
  730. }
  731. /* combining level and run, levelSuffix variable used to hold coeffMap,
  732. * i.e. bit map indicating which coefficients had non-zero value. */
  733. /*lint -esym(771,level,run) level and run are always initialized */
  734. tmp = zerosLeft;
  735. coeffLevel[tmp] = level[totalCoeff-1];
  736. levelSuffix = 1 << tmp;
  737. for (i = totalCoeff-1; i--;)
  738. {
  739. tmp += run[i];
  740. levelSuffix |= 1 << tmp;
  741. coeffLevel[tmp] = level[i];
  742. }
  743. }
  744. else
  745. levelSuffix = 0;
  746. if (h264bsdFlushBits(pStrmData, 32-bufferBits) != HANTRO_OK)
  747. return(HANTRO_NOK);
  748. return((totalCoeff << 4) | (levelSuffix << 16));
  749. }