Block.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "Decoder.h"
  2. #include "indices.h"
  3. #include "vlc_table.h"
  4. static unsigned char zig_zag_scan[64]={
  5. 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
  6. 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
  7. 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
  8. 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
  9. };
  10. #define ESCAPE 7167
  11. // mode 1 = Inter (or Intra in advanced intra coding mode)
  12. // mode 0 = Intra
  13. void Decoder::getblock(int comp, int mode)
  14. {
  15. int val, i, j, sign;
  16. unsigned int code;
  17. VLCtab *tab;
  18. short *bp;
  19. int run, last, level, QP;
  20. short *qval;
  21. /* TODO: benski>
  22. i think this whole function can get replaced with
  23. ippiDecodeCoeffsIntra_H263_1u16s (mode==0)
  24. or
  25. ippiDecodeCoeffsInter_H263_1u16s (mode == 1)
  26. with pCoef = bp
  27. modQuantFlag = escapemode>=1
  28. scan = IPPVC_SCAN_ZIGZAG
  29. */
  30. bp = block[comp];
  31. /* decode AC coefficients (or all coefficients in advanced intra coding
  32. * mode) */
  33. for (i = (mode == 0);; i++)
  34. {
  35. code = buffer.showbits(12);
  36. if (code >= 512)
  37. tab = &DCT3Dtab0[(code >> 5) - 16];
  38. else if (code >= 128)
  39. tab = &DCT3Dtab1[(code >> 2) - 32];
  40. else if (code >= 8)
  41. tab = &DCT3Dtab2[(code >> 0) - 8];
  42. else
  43. {
  44. fault = 1;
  45. return;
  46. }
  47. run = (tab->val >> 4) & 255;
  48. level = tab->val & 15;
  49. last = (tab->val >> 12) & 1;
  50. buffer.flushbits(tab->len);
  51. if (tab->val == ESCAPE)
  52. {
  53. /* escape */
  54. if (escapemode >= 1)
  55. {
  56. int is11 = buffer.getbits1();
  57. sign=0;
  58. last = buffer.getbits1();
  59. i+=run = buffer.getbits(6);
  60. if (is11)
  61. {
  62. level = buffer.getbits(11);
  63. if ((sign = (level>=1024)))
  64. val = 2048 - level;
  65. else
  66. val = level;
  67. }
  68. else
  69. {
  70. level = buffer.getbits(7);
  71. if ((sign = (level>=64)))
  72. val = 128 - level;
  73. else
  74. val = level;
  75. }
  76. }
  77. else
  78. {
  79. last = buffer.getbits1();
  80. i += run = buffer.getbits(6);
  81. level = buffer.getbits(8);
  82. if ((sign = (level >= 128)))
  83. val = 256 - level;
  84. else
  85. val = level;
  86. }
  87. }
  88. else
  89. {
  90. i += run;
  91. val = level;
  92. sign = buffer.getbits(1);
  93. }
  94. if (i >= 64)
  95. {
  96. fault = 1;
  97. return;
  98. }
  99. /* Descan in the proper order in advanced intra coding mode */
  100. j = zig_zag_scan[i];
  101. qval = &bp[j];
  102. QP = quant;
  103. /* TODO: benski>
  104. ippiQuantInvIntra_H263_16s_C1I
  105. or
  106. ippiQuantInvInter_H263_16s_C1I (mode == 1)
  107. but outside the loop
  108. pSrcDst = bp
  109. QP = quant
  110. modQuantFlag = escapemode >= 1
  111. */
  112. /* TMN3 dequantization */
  113. if ((QP % 2) == 1)
  114. *qval = (sign ? -(QP * (2 * val + 1)) : QP * (2 * val + 1));
  115. else
  116. *qval = (sign ? -(QP * (2 * val + 1) - 1) : QP * (2 * val + 1) - 1);
  117. if (last)
  118. {
  119. /* That's it */
  120. return;
  121. }
  122. }}