PageRenderTime 212ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Show/avc/vlc.cpp

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