PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Avc/vlc.cpp

http://github.com/mbebenita/Broadway
C++ | 832 lines | 667 code | 125 blank | 40 comment | 123 complexity | ea0529ce77fe28b62591c9ab7fd48494 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. #include "avcdec_lib.h"
  19. #include "avcdec_bitstream.h"
  20. //#define PV_ARM_V5
  21. #ifdef PV_ARM_V5
  22. #define PV_CLZ(A,B) __asm{CLZ (A),(B)} \
  23. A -= 16;
  24. #else
  25. // #define PV_CLZ(A,B) while (((B) & 0x8000) == 0) {(B) <<=1; A++;}
  26. // A Leading Zeros, B tmp (number, but has one bit set)
  27. #define PV_CLZ(A,B) \
  28. A = 1; \
  29. if (B >> 8 == 0) { \
  30. A += 8; \
  31. B <<= 8; \
  32. } \
  33. if (B >> 12 == 0) { \
  34. A += 4; \
  35. B <<= 4; \
  36. } \
  37. if (B >> 14 == 0) { \
  38. A += 2; \
  39. B <<= 2; \
  40. } \
  41. A -= B >> 15;
  42. #endif
  43. #define PV_NO_CLZ
  44. #ifndef PV_NO_CLZ
  45. typedef struct tagVLCNumCoeffTrail
  46. {
  47. int trailing;
  48. int total_coeff;
  49. int length;
  50. } VLCNumCoeffTrail;
  51. typedef struct tagShiftOffset
  52. {
  53. int shift;
  54. int offset;
  55. } ShiftOffset;
  56. const VLCNumCoeffTrail NumCoeffTrailOnes[3][67] =
  57. {
  58. {{0, 0, 1}, {1, 1, 2}, {2, 2, 3}, {1, 2, 6}, {0, 1, 6}, {3, 3, 5}, {3, 3, 5}, {3, 5, 7},
  59. {2, 3, 7}, {3, 4, 6}, {3, 4, 6}, {3, 6, 8}, {2, 4, 8}, {1, 3, 8}, {0, 2, 8}, {3, 7, 9},
  60. {2, 5, 9}, {1, 4, 9}, {0, 3, 9}, {3, 8, 10}, {2, 6, 10}, {1, 5, 10}, {0, 4, 10}, {3, 9, 11},
  61. {2, 7, 11}, {1, 6, 11}, {0, 5, 11}, {0, 8, 13}, {2, 9, 13}, {1, 8, 13}, {0, 7, 13}, {3, 10, 13},
  62. {2, 8, 13}, {1, 7, 13}, {0, 6, 13}, {3, 12, 14}, {2, 11, 14}, {1, 10, 14}, {0, 10, 14}, {3, 11, 14},
  63. {2, 10, 14}, {1, 9, 14}, {0, 9, 14}, {3, 14, 15}, {2, 13, 15}, {1, 12, 15}, {0, 12, 15}, {3, 13, 15},
  64. {2, 12, 15}, {1, 11, 15}, {0, 11, 15}, {3, 16, 16}, {2, 15, 16}, {1, 15, 16}, {0, 14, 16}, {3, 15, 16},
  65. {2, 14, 16}, {1, 14, 16}, {0, 13, 16}, {0, 16, 16}, {2, 16, 16}, {1, 16, 16}, {0, 15, 16}, {1, 13, 15},
  66. { -1, -1, -1}, { -1, -1, -1}, { -1, -1, -1}},
  67. {{1, 1, 2}, {0, 0, 2}, {3, 4, 4}, {3, 3, 4}, {2, 2, 3}, {2, 2, 3}, {3, 6, 6}, {2, 3, 6},
  68. {1, 3, 6}, {0, 1, 6}, {3, 5, 5}, {3, 5, 5}, {1, 2, 5}, {1, 2, 5}, {3, 7, 6}, {2, 4, 6},
  69. {1, 4, 6}, {0, 2, 6}, {3, 8, 7}, {2, 5, 7}, {1, 5, 7}, {0, 3, 7}, {0, 5, 8}, {2, 6, 8},
  70. {1, 6, 8}, {0, 4, 8}, {3, 9, 9}, {2, 7, 9}, {1, 7, 9}, {0, 6, 9}, {3, 11, 11}, {2, 9, 11},
  71. {1, 9, 11}, {0, 8, 11}, {3, 10, 11}, {2, 8, 11}, {1, 8, 11}, {0, 7, 11}, {0, 11, 12}, {2, 11, 12},
  72. {1, 11, 12}, {0, 10, 12}, {3, 12, 12}, {2, 10, 12}, {1, 10, 12}, {0, 9, 12}, {3, 14, 13}, {2, 13, 13},
  73. {1, 13, 13}, {0, 13, 13}, {3, 13, 13}, {2, 12, 13}, {1, 12, 13}, {0, 12, 13}, {1, 15, 14}, {0, 15, 14},
  74. {2, 15, 14}, {1, 14, 14}, {2, 14, 13}, {2, 14, 13}, {0, 14, 13}, {0, 14, 13}, {3, 16, 14}, {2, 16, 14},
  75. {1, 16, 14}, {0, 16, 14}, {3, 15, 13}},
  76. {{3, 7, 4}, {3, 6, 4}, {3, 5, 4}, {3, 4, 4}, {3, 3, 4}, {2, 2, 4}, {1, 1, 4}, {0, 0, 4},
  77. {1, 5, 5}, {2, 5, 5}, {1, 4, 5}, {2, 4, 5}, {1, 3, 5}, {3, 8, 5}, {2, 3, 5}, {1, 2, 5},
  78. {0, 3, 6}, {2, 7, 6}, {1, 7, 6}, {0, 2, 6}, {3, 9, 6}, {2, 6, 6}, {1, 6, 6}, {0, 1, 6},
  79. {0, 7, 7}, {0, 6, 7}, {2, 9, 7}, {0, 5, 7}, {3, 10, 7}, {2, 8, 7}, {1, 8, 7}, {0, 4, 7},
  80. {3, 12, 8}, {2, 11, 8}, {1, 10, 8}, {0, 9, 8}, {3, 11, 8}, {2, 10, 8}, {1, 9, 8}, {0, 8, 8},
  81. {0, 12, 9}, {2, 13, 9}, {1, 12, 9}, {0, 11, 9}, {3, 13, 9}, {2, 12, 9}, {1, 11, 9}, {0, 10, 9},
  82. {1, 15, 10}, {0, 14, 10}, {3, 14, 10}, {2, 14, 10}, {1, 14, 10}, {0, 13, 10}, {1, 13, 9}, {1, 13, 9},
  83. {1, 16, 10}, {0, 15, 10}, {3, 15, 10}, {2, 15, 10}, {3, 16, 10}, {2, 16, 10}, {0, 16, 10}, { -1, -1, -1},
  84. { -1, -1, -1}, { -1, -1, -1}, { -1, -1, -1}}
  85. };
  86. const ShiftOffset NumCoeffTrailOnes_indx[3][15] =
  87. {
  88. {{15, -1}, {14, 0}, {13, 1}, {10, -1}, {9, 3}, {8, 7}, {7, 11}, {6, 15},
  89. {5, 19}, {3, 19}, {2, 27}, {1, 35}, {0, 43}, {0, 55}, {1, 62}},
  90. {{14, -2}, {12, -2}, {10, -2}, {10, 10}, {9, 14}, {8, 18}, {7, 22}, {5, 22},
  91. {4, 30}, {3, 38}, {2, 46}, {2, 58}, {3, 65}, {16, 0}, {16, 0}},
  92. {{12, -8}, {11, 0}, {10, 8}, {9, 16}, {8, 24}, {7, 32}, {6, 40}, {6, 52},
  93. {6, 58}, {6, 61}, {16, 0}, {16, 0}, {16, 0}, {16, 0}, {16, 0}}
  94. };
  95. const static int nC_table[8] = {0, 0, 1, 1, 2, 2, 2, 2};
  96. #endif
  97. /**
  98. See algorithm in subclause 9.1, Table 9-1, Table 9-2. */
  99. AVCDec_Status ue_v(AVCDecBitstream *bitstream, uint *codeNum)
  100. {
  101. uint temp, tmp_cnt;
  102. int leading_zeros = 0;
  103. BitstreamShowBits(bitstream, 16, &temp);
  104. tmp_cnt = temp | 0x1;
  105. PV_CLZ(leading_zeros, tmp_cnt)
  106. if (leading_zeros < 8)
  107. {
  108. *codeNum = (temp >> (15 - (leading_zeros << 1))) - 1;
  109. BitstreamFlushBits(bitstream, (leading_zeros << 1) + 1);
  110. }
  111. else
  112. {
  113. BitstreamReadBits(bitstream, (leading_zeros << 1) + 1, &temp);
  114. *codeNum = temp - 1;
  115. }
  116. return AVCDEC_SUCCESS;
  117. }
  118. /**
  119. See subclause 9.1.1, Table 9-3 */
  120. AVCDec_Status se_v(AVCDecBitstream *bitstream, int *value)
  121. {
  122. uint temp, tmp_cnt;
  123. int leading_zeros = 0;
  124. BitstreamShowBits(bitstream, 16, &temp);
  125. tmp_cnt = temp | 0x1;
  126. PV_CLZ(leading_zeros, tmp_cnt)
  127. if (leading_zeros < 8)
  128. {
  129. temp >>= (15 - (leading_zeros << 1));
  130. BitstreamFlushBits(bitstream, (leading_zeros << 1) + 1);
  131. }
  132. else
  133. {
  134. BitstreamReadBits(bitstream, (leading_zeros << 1) + 1, &temp);
  135. }
  136. *value = temp >> 1;
  137. if (temp & 0x01) // lsb is signed bit
  138. *value = -(*value);
  139. // leading_zeros = temp >> 1;
  140. // *value = leading_zeros - (leading_zeros*2*(temp&1));
  141. return AVCDEC_SUCCESS;
  142. }
  143. AVCDec_Status se_v32bit(AVCDecBitstream *bitstream, int32 *value)
  144. {
  145. int leadingZeros;
  146. uint32 infobits;
  147. uint32 codeNum;
  148. if (AVCDEC_SUCCESS != GetEGBitstring32bit(bitstream, &leadingZeros, &infobits))
  149. return AVCDEC_FAIL;
  150. codeNum = (1 << leadingZeros) - 1 + infobits;
  151. *value = (codeNum + 1) / 2;
  152. if ((codeNum & 0x01) == 0) // lsb is signed bit
  153. *value = -(*value);
  154. return AVCDEC_SUCCESS;
  155. }
  156. AVCDec_Status te_v(AVCDecBitstream *bitstream, uint *value, uint range)
  157. {
  158. if (range > 1)
  159. {
  160. ue_v(bitstream, value);
  161. }
  162. else
  163. {
  164. BitstreamRead1Bit(bitstream, value);
  165. *value = 1 - (*value);
  166. }
  167. return AVCDEC_SUCCESS;
  168. }
  169. /* This function is only used for syntax with range from -2^31 to 2^31-1 */
  170. /* only a few of them in the SPS and PPS */
  171. AVCDec_Status GetEGBitstring32bit(AVCDecBitstream *bitstream, int *leadingZeros, uint32 *infobits)
  172. {
  173. int bit_value;
  174. uint info_temp;
  175. *leadingZeros = 0;
  176. BitstreamRead1Bit(bitstream, (uint*)&bit_value);
  177. while (!bit_value)
  178. {
  179. (*leadingZeros)++;
  180. BitstreamRead1Bit(bitstream, (uint*)&bit_value);
  181. }
  182. if (*leadingZeros > 0)
  183. {
  184. if (sizeof(uint) == 4) /* 32 bit machine */
  185. {
  186. BitstreamReadBits(bitstream, *leadingZeros, (uint*)&info_temp);
  187. *infobits = (uint32)info_temp;
  188. }
  189. else if (sizeof(uint) == 2) /* 16 bit machine */
  190. {
  191. *infobits = 0;
  192. if (*leadingZeros > 16)
  193. {
  194. BitstreamReadBits(bitstream, 16, (uint*)&info_temp);
  195. (*leadingZeros) -= 16;
  196. *infobits = ((uint32)info_temp) << (*leadingZeros);
  197. }
  198. BitstreamReadBits(bitstream, *leadingZeros, (uint*)&info_temp);
  199. *infobits |= (uint32)info_temp ;
  200. }
  201. }
  202. else
  203. *infobits = 0;
  204. return AVCDEC_SUCCESS;
  205. }
  206. /* see Table 9-4 assignment of codeNum to values of coded_block_pattern. */
  207. const static uint8 MapCBP[48][2] =
  208. {
  209. {47, 0}, {31, 16}, {15, 1}, { 0, 2}, {23, 4}, {27, 8}, {29, 32}, {30, 3}, { 7, 5}, {11, 10}, {13, 12}, {14, 15},
  210. {39, 47}, {43, 7}, {45, 11}, {46, 13}, {16, 14}, { 3, 6}, { 5, 9}, {10, 31}, {12, 35}, {19, 37}, {21, 42}, {26, 44},
  211. {28, 33}, {35, 34}, {37, 36}, {42, 40}, {44, 39}, { 1, 43}, { 2, 45}, { 4, 46}, { 8, 17}, {17, 18}, {18, 20}, {20, 24},
  212. {24, 19}, { 6, 21}, { 9, 26}, {22, 28}, {25, 23}, {32, 27}, {33, 29}, {34, 30}, {36, 22}, {40, 25}, {38, 38}, {41, 41},
  213. };
  214. AVCDec_Status DecodeCBP(AVCMacroblock *currMB, AVCDecBitstream *stream)
  215. {
  216. uint codeNum;
  217. uint coded_block_pattern;
  218. ue_v(stream, &codeNum);
  219. if (codeNum > 47)
  220. {
  221. return AVCDEC_FAIL;
  222. }
  223. /* can get rid of the if _OPTIMIZE */
  224. if (currMB->mbMode == AVC_I4)
  225. {
  226. coded_block_pattern = MapCBP[codeNum][0];
  227. }
  228. else
  229. {
  230. coded_block_pattern = MapCBP[codeNum][1];
  231. }
  232. // currMB->cbpL = coded_block_pattern&0xF; /* modulo 16 */
  233. // currMB->cbpC = coded_block_pattern>>4; /* divide 16 */
  234. currMB->CBP = coded_block_pattern;
  235. return AVCDEC_SUCCESS;
  236. }
  237. /* TO BE OPTIMIZED !!!!! */
  238. AVCDec_Status ce_TotalCoeffTrailingOnes(AVCDecBitstream *stream, int *TrailingOnes, int *TotalCoeff, int nC)
  239. {
  240. #ifdef PV_NO_CLZ
  241. const static uint8 TotCofNTrail1[75][3] = {{0, 0, 16}/*error */, {0, 0, 16}/*error */, {1, 13, 15}, {1, 13, 15}, {0, 16, 16}, {2, 16, 16}, {1, 16, 16}, {0, 15, 16},
  242. {3, 16, 16}, {2, 15, 16}, {1, 15, 16}, {0, 14, 16}, {3, 15, 16}, {2, 14, 16}, {1, 14, 16}, {0, 13, 16},
  243. {3, 14, 15}, {2, 13, 15}, {1, 12, 15}, {0, 12, 15}, {3, 13, 15}, {2, 12, 15}, {1, 11, 15}, {0, 11, 15},
  244. {3, 12, 14}, {2, 11, 14}, {1, 10, 14}, {0, 10, 14}, {3, 11, 14}, {2, 10, 14}, {1, 9, 14}, {0, 9, 14},
  245. {0, 8, 13}, {2, 9, 13}, {1, 8, 13}, {0, 7, 13}, {3, 10, 13}, {2, 8, 13}, {1, 7, 13}, {0, 6, 13},
  246. {3, 9, 11}, {2, 7, 11}, {1, 6, 11}, {0, 5, 11}, {3, 8, 10},
  247. {2, 6, 10}, {1, 5, 10}, {0, 4, 10}, {3, 7, 9}, {2, 5, 9}, {1, 4, 9}, {0, 3, 9}, {3, 6, 8},
  248. {2, 4, 8}, {1, 3, 8}, {0, 2, 8}, {3, 5, 7}, {2, 3, 7}, {3, 4, 6}, {3, 4, 6}, {1, 2, 6},
  249. {1, 2, 6}, {0, 1, 6}, {0, 1, 6}, {3, 3, 5}, {3, 3, 5}, {3, 3, 5}, {3, 3, 5}, {2, 2, 3},
  250. {1, 1, 2}, {1, 1, 2}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}
  251. };
  252. const static uint8 TotCofNTrail2[84][3] = {{0, 0, 14 /* error */}, {0, 0, 14/*error */}, {3, 15, 13}, {3, 15, 13}, {3, 16, 14}, {2, 16, 14}, {1, 16, 14}, {0, 16, 14},
  253. {1, 15, 14}, {0, 15, 14}, {2, 15, 14}, {1, 14, 14}, {2, 14, 13}, {2, 14, 13}, {0, 14, 13}, {0, 14, 13},
  254. {3, 14, 13}, {2, 13, 13}, {1, 13, 13}, {0, 13, 13}, {3, 13, 13}, {2, 12, 13}, {1, 12, 13}, {0, 12, 13},
  255. {0, 11, 12}, {2, 11, 12}, {1, 11, 12}, {0, 10, 12}, {3, 12, 12}, {2, 10, 12}, {1, 10, 12}, {0, 9, 12},
  256. {3, 11, 11}, {2, 9, 11}, {1, 9, 11}, {0, 8, 11}, {3, 10, 11}, {2, 8, 11}, {1, 8, 11}, {0, 7, 11},
  257. {3, 9, 9}, {2, 7, 9}, {1, 7, 9}, {0, 6, 9}, {0, 5, 8}, {0, 5, 8}, {2, 6, 8}, {2, 6, 8},
  258. {1, 6, 8}, {1, 6, 8}, {0, 4, 8}, {0, 4, 8}, {3, 8, 7}, {2, 5, 7}, {1, 5, 7}, {0, 3, 7},
  259. {3, 7, 6}, {3, 7, 6}, {2, 4, 6}, {2, 4, 6}, {1, 4, 6}, {1, 4, 6}, {0, 2, 6}, {0, 2, 6},
  260. {3, 6, 6}, {2, 3, 6}, {1, 3, 6}, {0, 1, 6}, {3, 5, 5}, {3, 5, 5}, {1, 2, 5}, {1, 2, 5},
  261. {3, 4, 4}, {3, 3, 4}, {2, 2, 3}, {2, 2, 3}, {1, 1, 2}, {1, 1, 2}, {1, 1, 2}, {1, 1, 2},
  262. {0, 0, 2}, {0, 0, 2}, {0, 0, 2}, {0, 0, 2}
  263. };
  264. const static uint8 TotCofNTrail3[64][3] = {{0, 0, 10/*error*/}, {0, 16, 10}, {3, 16, 10}, {2, 16, 10}, {1, 16, 10}, {0, 15, 10}, {3, 15, 10},
  265. {2, 15, 10}, {1, 15, 10}, {0, 14, 10}, {3, 14, 10}, {2, 14, 10}, {1, 14, 10}, {0, 13, 10}, {1, 13, 9},
  266. {1, 13, 9}, {0, 12, 9}, {2, 13, 9}, {1, 12, 9}, {0, 11, 9}, {3, 13, 9}, {2, 12, 9}, {1, 11, 9},
  267. {0, 10, 9}, {3, 12, 8}, {2, 11, 8}, {1, 10, 8}, {0, 9, 8}, {3, 11, 8}, {2, 10, 8}, {1, 9, 8},
  268. {0, 8, 8}, {0, 7, 7}, {0, 6, 7}, {2, 9, 7}, {0, 5, 7}, {3, 10, 7}, {2, 8, 7}, {1, 8, 7},
  269. {0, 4, 7}, {0, 3, 6}, {2, 7, 6}, {1, 7, 6}, {0, 2, 6}, {3, 9, 6}, {2, 6, 6}, {1, 6, 6},
  270. {0, 1, 6}, {1, 5, 5}, {2, 5, 5}, {1, 4, 5}, {2, 4, 5}, {1, 3, 5}, {3, 8, 5}, {2, 3, 5},
  271. {1, 2, 5}, {3, 7, 4}, {3, 6, 4}, {3, 5, 4}, {3, 4, 4}, {3, 3, 4}, {2, 2, 4}, {1, 1, 4},
  272. {0, 0, 4}
  273. };
  274. #endif
  275. uint code;
  276. #ifdef PV_NO_CLZ
  277. uint8 *pcode;
  278. if (nC < 2)
  279. {
  280. BitstreamShowBits(stream, 16, &code);
  281. if (code >= 8192)
  282. {
  283. pcode = (uint8*) & (TotCofNTrail1[(code>>13)+65+2][0]);
  284. }
  285. else if (code >= 2048)
  286. {
  287. pcode = (uint8*) & (TotCofNTrail1[(code>>9)+50+2][0]);
  288. }
  289. else if (code >= 1024)
  290. {
  291. pcode = (uint8*) & (TotCofNTrail1[(code>>8)+46+2][0]);
  292. }
  293. else if (code >= 512)
  294. {
  295. pcode = (uint8*) & (TotCofNTrail1[(code>>7)+42+2][0]);
  296. }
  297. else if (code >= 256)
  298. {
  299. pcode = (uint8*) & (TotCofNTrail1[(code>>6)+38+2][0]);
  300. }
  301. else if (code >= 128)
  302. {
  303. pcode = (uint8*) & (TotCofNTrail1[(code>>5)+34+2][0]);
  304. }
  305. else if (code >= 64)
  306. {
  307. pcode = (uint8*) & (TotCofNTrail1[(code>>3)+22+2][0]);
  308. }
  309. else if (code >= 32)
  310. {
  311. pcode = (uint8*) & (TotCofNTrail1[(code>>2)+14+2][0]);
  312. }
  313. else if (code >= 16)
  314. {
  315. pcode = (uint8*) & (TotCofNTrail1[(code>>1)+6+2][0]);
  316. }
  317. else
  318. {
  319. pcode = (uint8*) & (TotCofNTrail1[(code-2)+2][0]);
  320. }
  321. *TrailingOnes = pcode[0];
  322. *TotalCoeff = pcode[1];
  323. BitstreamFlushBits(stream, pcode[2]);
  324. }
  325. else if (nC < 4)
  326. {
  327. BitstreamShowBits(stream, 14, &code);
  328. if (code >= 4096)
  329. {
  330. pcode = (uint8*) & (TotCofNTrail2[(code>>10)+66+2][0]);
  331. }
  332. else if (code >= 2048)
  333. {
  334. pcode = (uint8*) & (TotCofNTrail2[(code>>8)+54+2][0]);
  335. }
  336. else if (code >= 512)
  337. {
  338. pcode = (uint8*) & (TotCofNTrail2[(code>>7)+46+2][0]);
  339. }
  340. else if (code >= 128)
  341. {
  342. pcode = (uint8*) & (TotCofNTrail2[(code>>5)+34+2][0]);
  343. }
  344. else if (code >= 64)
  345. {
  346. pcode = (uint8*) & (TotCofNTrail2[(code>>3)+22+2][0]);
  347. }
  348. else if (code >= 32)
  349. {
  350. pcode = (uint8*) & (TotCofNTrail2[(code>>2)+14+2][0]);
  351. }
  352. else if (code >= 16)
  353. {
  354. pcode = (uint8*) & (TotCofNTrail2[(code>>1)+6+2][0]);
  355. }
  356. else
  357. {
  358. pcode = (uint8*) & (TotCofNTrail2[code-2+2][0]);
  359. }
  360. *TrailingOnes = pcode[0];
  361. *TotalCoeff = pcode[1];
  362. BitstreamFlushBits(stream, pcode[2]);
  363. }
  364. else if (nC < 8)
  365. {
  366. BitstreamShowBits(stream, 10, &code);
  367. if (code >= 512)
  368. {
  369. pcode = (uint8*) & (TotCofNTrail3[(code>>6)+47+1][0]);
  370. }
  371. else if (code >= 256)
  372. {
  373. pcode = (uint8*) & (TotCofNTrail3[(code>>5)+39+1][0]);
  374. }
  375. else if (code >= 128)
  376. {
  377. pcode = (uint8*) & (TotCofNTrail3[(code>>4)+31+1][0]);
  378. }
  379. else if (code >= 64)
  380. {
  381. pcode = (uint8*) & (TotCofNTrail3[(code>>3)+23+1][0]);
  382. }
  383. else if (code >= 32)
  384. {
  385. pcode = (uint8*) & (TotCofNTrail3[(code>>2)+15+1][0]);
  386. }
  387. else if (code >= 16)
  388. {
  389. pcode = (uint8*) & (TotCofNTrail3[(code>>1)+7+1][0]);
  390. }
  391. else
  392. {
  393. pcode = (uint8*) & (TotCofNTrail3[code-1+1][0]);
  394. }
  395. *TrailingOnes = pcode[0];
  396. *TotalCoeff = pcode[1];
  397. BitstreamFlushBits(stream, pcode[2]);
  398. }
  399. else
  400. {
  401. /* read 6 bit FLC */
  402. BitstreamReadBits(stream, 6, &code);
  403. *TrailingOnes = code & 3;
  404. *TotalCoeff = (code >> 2) + 1;
  405. if (*TotalCoeff > 16)
  406. {
  407. *TotalCoeff = 16; // _ERROR
  408. }
  409. if (code == 3)
  410. {
  411. *TrailingOnes = 0;
  412. (*TotalCoeff)--;
  413. }
  414. }
  415. #else
  416. const VLCNumCoeffTrail *ptr;
  417. const ShiftOffset *ptr_indx;
  418. uint temp, leading_zeros = 0;
  419. if (nC < 8)
  420. {
  421. BitstreamShowBits(stream, 16, &code);
  422. temp = code | 1;
  423. PV_CLZ(leading_zeros, temp)
  424. temp = nC_table[nC];
  425. ptr_indx = &NumCoeffTrailOnes_indx[temp][leading_zeros];
  426. ptr = &NumCoeffTrailOnes[temp][(code >> ptr_indx->shift) + ptr_indx->offset];
  427. *TrailingOnes = ptr->trailing;
  428. *TotalCoeff = ptr->total_coeff;
  429. BitstreamFlushBits(stream, ptr->length);
  430. }
  431. else
  432. {
  433. /* read 6 bit FLC */
  434. BitstreamReadBits(stream, 6, &code);
  435. *TrailingOnes = code & 3;
  436. *TotalCoeff = (code >> 2) + 1;
  437. if (*TotalCoeff > 16)
  438. {
  439. *TotalCoeff = 16; // _ERROR
  440. }
  441. if (code == 3)
  442. {
  443. *TrailingOnes = 0;
  444. (*TotalCoeff)--;
  445. }
  446. }
  447. #endif
  448. return AVCDEC_SUCCESS;
  449. }
  450. /* TO BE OPTIMIZED !!!!! */
  451. AVCDec_Status ce_TotalCoeffTrailingOnesChromaDC(AVCDecBitstream *stream, int *TrailingOnes, int *TotalCoeff)
  452. {
  453. AVCDec_Status status;
  454. const static uint8 TotCofNTrail5[21][3] =
  455. {
  456. {3, 4, 7}, {3, 4, 7}, {2, 4, 8}, {1, 4, 8}, {2, 3, 7}, {2, 3, 7}, {1, 3, 7},
  457. {1, 3, 7}, {0, 4, 6}, {0, 3, 6}, {0, 2, 6}, {3, 3, 6}, {1, 2, 6}, {0, 1, 6},
  458. {2, 2, 3}, {0, 0, 2}, {0, 0, 2}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}
  459. };
  460. uint code;
  461. uint8 *pcode;
  462. status = BitstreamShowBits(stream, 8, &code);
  463. if (code >= 32)
  464. {
  465. pcode = (uint8*) & (TotCofNTrail5[(code>>5)+13][0]);
  466. }
  467. else if (code >= 8)
  468. {
  469. pcode = (uint8*) & (TotCofNTrail5[(code>>2)+6][0]);
  470. }
  471. else
  472. {
  473. pcode = (uint8*) & (TotCofNTrail5[code][0]);
  474. }
  475. *TrailingOnes = pcode[0];
  476. *TotalCoeff = pcode[1];
  477. BitstreamFlushBits(stream, pcode[2]);
  478. return status;
  479. }
  480. /* see Table 9-6 */
  481. AVCDec_Status ce_LevelPrefix(AVCDecBitstream *stream, uint *code)
  482. {
  483. uint temp;
  484. uint leading_zeros = 0;
  485. BitstreamShowBits(stream, 16, &temp);
  486. temp |= 1 ;
  487. PV_CLZ(leading_zeros, temp)
  488. BitstreamFlushBits(stream, leading_zeros + 1);
  489. *code = leading_zeros;
  490. return AVCDEC_SUCCESS;
  491. }
  492. /* see Table 9-7 and 9-8 */
  493. AVCDec_Status ce_TotalZeros(AVCDecBitstream *stream, int *code, int TotalCoeff)
  494. {
  495. const static uint8 TotZero1[28][2] = {{15, 9}, {14, 9}, {13, 9}, {12, 8},
  496. {12, 8}, {11, 8}, {11, 8}, {10, 7}, {9, 7}, {8, 6}, {8, 6}, {7, 6}, {7, 6}, {6, 5}, {6, 5},
  497. {6, 5}, {6, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {4, 4}, {3, 4},
  498. {2, 3}, {2, 3}, {1, 3}, {1, 3}, {0, 1}
  499. };
  500. const static uint8 TotZero2n3[2][18][2] = {{{14, 6}, {13, 6}, {12, 6}, {11, 6},
  501. {10, 5}, {10, 5}, {9, 5}, {9, 5}, {8, 4}, {7, 4}, {6, 4}, {5, 4}, {4, 3}, {4, 3},
  502. {3, 3}, {2, 3}, {1, 3}, {0, 3}},
  503. /*const static uint8 TotZero3[18][2]=*/{{13, 6}, {11, 6}, {12, 5}, {12, 5}, {10, 5},
  504. {10, 5}, {9, 5}, {9, 5}, {8, 4}, {5, 4}, {4, 4}, {0, 4}, {7, 3}, {7, 3}, {6, 3}, {3, 3},
  505. {2, 3}, {1, 3}}
  506. };
  507. const static uint8 TotZero4[17][2] = {{12, 5}, {11, 5}, {10, 5}, {0, 5}, {9, 4},
  508. {9, 4}, {7, 4}, {7, 4}, {3, 4}, {3, 4}, {2, 4}, {2, 4}, {8, 3}, {6, 3}, {5, 3}, {4, 3}, {1, 3}
  509. };
  510. const static uint8 TotZero5[13][2] = {{11, 5}, {9, 5}, {10, 4}, {8, 4}, {2, 4},
  511. {1, 4}, {0, 4}, {7, 3}, {7, 3}, {6, 3}, {5, 3}, {4, 3}, {3, 3}
  512. };
  513. const static uint8 TotZero6to10[5][15][2] = {{{10, 6}, {0, 6}, {1, 5}, {1, 5}, {8, 4},
  514. {8, 4}, {8, 4}, {8, 4}, {9, 3}, {7, 3}, {6, 3}, {5, 3}, {4, 3}, {3, 3}, {2, 3}},
  515. /*const static uint8 TotZero7[15][2]=*/{{9, 6}, {0, 6}, {1, 5}, {1, 5}, {7, 4},
  516. {7, 4}, {7, 4}, {7, 4}, {8, 3}, {6, 3}, {4, 3}, {3, 3}, {2, 3}, {5, 2}, {5, 2}},
  517. /*const static uint8 TotZero8[15][2]=*/{{8, 6}, {0, 6}, {2, 5}, {2, 5}, {1, 4},
  518. {1, 4}, {1, 4}, {1, 4}, {7, 3}, {6, 3}, {3, 3}, {5, 2}, {5, 2}, {4, 2}, {4, 2}},
  519. /*const static uint8 TotZero9[15][2]=*/{{1, 6}, {0, 6}, {7, 5}, {7, 5}, {2, 4},
  520. {2, 4}, {2, 4}, {2, 4}, {5, 3}, {6, 2}, {6, 2}, {4, 2}, {4, 2}, {3, 2}, {3, 2}},
  521. /*const static uint8 TotZero10[11][2]=*/{{1, 5}, {0, 5}, {6, 4}, {6, 4}, {2, 3},
  522. {2, 3}, {2, 3}, {2, 3}, {5, 2}, {4, 2}, {3, 2}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}
  523. };
  524. const static uint8 TotZero11[7][2] = {{0, 4}, {1, 4}, {2, 3}, {2, 3}, {3, 3}, {5, 3}, {4, 1}};
  525. const static uint8 TotZero12to15[4][5][2] =
  526. {
  527. {{3, 1}, {2, 2}, {4, 3}, {1, 4}, {0, 4}},
  528. {{2, 1}, {3, 2}, {1, 3}, {0, 3}, {0, 0}},
  529. {{2, 1}, {1, 2}, {0, 2}, {0, 0}, {0, 0}},
  530. {{1, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}}
  531. };
  532. uint temp, mask;
  533. int indx;
  534. uint8 *pcode;
  535. if (TotalCoeff == 1)
  536. {
  537. BitstreamShowBits(stream, 9, &temp);
  538. if (temp >= 256)
  539. {
  540. pcode = (uint8*) & (TotZero1[27][0]);
  541. }
  542. else if (temp >= 64)
  543. {
  544. pcode = (uint8*) & (TotZero1[(temp>>5)+19][0]);
  545. }
  546. else if (temp >= 8)
  547. {
  548. pcode = (uint8*) & (TotZero1[(temp>>2)+5][0]);
  549. }
  550. else
  551. {
  552. pcode = (uint8*) & (TotZero1[temp-1][0]);
  553. }
  554. }
  555. else if (TotalCoeff == 2 || TotalCoeff == 3)
  556. {
  557. BitstreamShowBits(stream, 6, &temp);
  558. if (temp >= 32)
  559. {
  560. pcode = (uint8*) & (TotZero2n3[TotalCoeff-2][(temp>>3)+10][0]);
  561. }
  562. else if (temp >= 8)
  563. {
  564. pcode = (uint8*) & (TotZero2n3[TotalCoeff-2][(temp>>2)+6][0]);
  565. }
  566. else
  567. {
  568. pcode = (uint8*) & (TotZero2n3[TotalCoeff-2][temp][0]);
  569. }
  570. }
  571. else if (TotalCoeff == 4)
  572. {
  573. BitstreamShowBits(stream, 5, &temp);
  574. if (temp >= 12)
  575. {
  576. pcode = (uint8*) & (TotZero4[(temp>>2)+9][0]);
  577. }
  578. else
  579. {
  580. pcode = (uint8*) & (TotZero4[temp][0]);
  581. }
  582. }
  583. else if (TotalCoeff == 5)
  584. {
  585. BitstreamShowBits(stream, 5, &temp);
  586. if (temp >= 16)
  587. {
  588. pcode = (uint8*) & (TotZero5[(temp>>2)+5][0]);
  589. }
  590. else if (temp >= 2)
  591. {
  592. pcode = (uint8*) & (TotZero5[(temp>>1)+1][0]);
  593. }
  594. else
  595. {
  596. pcode = (uint8*) & (TotZero5[temp][0]);
  597. }
  598. }
  599. else if (TotalCoeff >= 6 && TotalCoeff <= 10)
  600. {
  601. if (TotalCoeff == 10)
  602. {
  603. BitstreamShowBits(stream, 5, &temp);
  604. }
  605. else
  606. {
  607. BitstreamShowBits(stream, 6, &temp);
  608. }
  609. if (temp >= 8)
  610. {
  611. pcode = (uint8*) & (TotZero6to10[TotalCoeff-6][(temp>>3)+7][0]);
  612. }
  613. else
  614. {
  615. pcode = (uint8*) & (TotZero6to10[TotalCoeff-6][temp][0]);
  616. }
  617. }
  618. else if (TotalCoeff == 11)
  619. {
  620. BitstreamShowBits(stream, 4, &temp);
  621. if (temp >= 8)
  622. {
  623. pcode = (uint8*) & (TotZero11[6][0]);
  624. }
  625. else if (temp >= 4)
  626. {
  627. pcode = (uint8*) & (TotZero11[(temp>>1)+2][0]);
  628. }
  629. else
  630. {
  631. pcode = (uint8*) & (TotZero11[temp][0]);
  632. }
  633. }
  634. else
  635. {
  636. BitstreamShowBits(stream, (16 - TotalCoeff), &temp);
  637. mask = 1 << (15 - TotalCoeff);
  638. indx = 0;
  639. while ((temp&mask) == 0 && indx < (16 - TotalCoeff)) /* search location of 1 bit */
  640. {
  641. mask >>= 1;
  642. indx++;
  643. }
  644. pcode = (uint8*) & (TotZero12to15[TotalCoeff-12][indx]);
  645. }
  646. *code = pcode[0];
  647. BitstreamFlushBits(stream, pcode[1]);
  648. return AVCDEC_SUCCESS;
  649. }
  650. /* see Table 9-9 */
  651. AVCDec_Status ce_TotalZerosChromaDC(AVCDecBitstream *stream, int *code, int TotalCoeff)
  652. {
  653. const static uint8 TotZeroChrom1to3[3][8][2] =
  654. {
  655. {{3, 3}, {2, 3}, {1, 2}, {1, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}},
  656. {{2, 2}, {2, 2}, {1, 2}, {1, 2}, {0, 1}, {0, 1}, {0, 1}, {0, 1}},
  657. {{1, 1}, {1, 1}, {1, 1}, {1, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}},
  658. };
  659. uint temp;
  660. uint8 *pcode;
  661. BitstreamShowBits(stream, 3, &temp);
  662. pcode = (uint8*) & (TotZeroChrom1to3[TotalCoeff-1][temp]);
  663. *code = pcode[0];
  664. BitstreamFlushBits(stream, pcode[1]);
  665. return AVCDEC_SUCCESS;
  666. }
  667. /* see Table 9-10 */
  668. AVCDec_Status ce_RunBefore(AVCDecBitstream *stream, int *code, int zerosLeft)
  669. {
  670. const static int codlen[6] = {1, 2, 2, 3, 3, 3}; /* num bits to read */
  671. const static uint8 RunBeforeTab[6][8][2] = {{{1, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
  672. /*const static int RunBefore2[4][2]=*/{{2, 2}, {1, 2}, {0, 1}, {0, 1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
  673. /*const static int RunBefore3[4][2]=*/{{3, 2}, {2, 2}, {1, 2}, {0, 2}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
  674. /*const static int RunBefore4[7][2]=*/{{4, 3}, {3, 3}, {2, 2}, {2, 2}, {1, 2}, {1, 2}, {0, 2}, {0, 2}},
  675. /*const static int RunBefore5[7][2]=*/{{5, 3}, {4, 3}, {3, 3}, {2, 3}, {1, 2}, {1, 2}, {0, 2}, {0, 2}},
  676. /*const static int RunBefore6[7][2]=*/{{1, 3}, {2, 3}, {4, 3}, {3, 3}, {6, 3}, {5, 3}, {0, 2}, {0, 2}}
  677. };
  678. uint temp;
  679. uint8 *pcode;
  680. int indx;
  681. if (zerosLeft <= 6)
  682. {
  683. BitstreamShowBits(stream, codlen[zerosLeft-1], &temp);
  684. pcode = (uint8*) & (RunBeforeTab[zerosLeft-1][temp][0]);
  685. *code = pcode[0];
  686. BitstreamFlushBits(stream, pcode[1]);
  687. }
  688. else
  689. {
  690. BitstreamReadBits(stream, 3, &temp);
  691. if (temp)
  692. {
  693. *code = 7 - temp;
  694. }
  695. else
  696. {
  697. BitstreamShowBits(stream, 9, &temp);
  698. temp <<= 7;
  699. temp |= 1;
  700. indx = 0;
  701. PV_CLZ(indx, temp)
  702. *code = 7 + indx;
  703. BitstreamFlushBits(stream, indx + 1);
  704. }
  705. }
  706. return AVCDEC_SUCCESS;
  707. }