vlc.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "Decoder.h"
  2. #include "vlc_table.h"
  3. static int PRED_type_EP[] = {0,0,1,1,1,2,2,2,3,3};
  4. static int QUANT_present_EP[] = {0,1,0,0,1,0,0,1,0,1};
  5. static int CBP_present_EP[] = {1,1,0,1,1,0,1,1,1,1};
  6. static VLCtab MBTYPEtabEP[] = {
  7. {-1,0}, {9,8}, {8,7}, {8,7}, {7,6}, {7,6}, {7,6}, {7,6},
  8. {4,5}, {4,5}, {4,5}, {4,5}, {4,5}, {4,5}, {4,5}, {4,5},
  9. {5,5}, {5,5}, {5,5}, {5,5}, {5,5}, {5,5}, {5,5}, {5,5},
  10. {6,5}, {6,5}, {6,5}, {6,5}, {6,5}, {6,5}, {6,5}, {6,5},
  11. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  12. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  13. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  14. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  15. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  16. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  17. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  18. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  19. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3},
  20. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3},
  21. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3},
  22. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}
  23. };
  24. static int PRED_type_B[] = {0,0,1,1,1,2,2,2,3,3,3,4,4};
  25. static int QUANT_present_B[] = {0,1,0,0,1,0,0,1,0,0,1,0,1};
  26. static int CBPC_pattern_EI[] = {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3};
  27. static int CBP_present_B[] = {1,1,0,1,1,0,1,1,0,1,1,1,1};
  28. static VLCtab MBTYPEtabB[] = {
  29. {-1,0}, {12,7}, {11,6}, {11,6}, {10,5}, {10,5}, {10,5}, {10,5},
  30. {1,4}, {1,4}, {1,4}, {1,4}, {1,4}, {1,4}, {1,4}, {1,4},
  31. {8,5}, {8,5}, {8,5}, {8,5}, {9,5}, {9,5}, {9,5}, {9,5},
  32. {4,5}, {4,5}, {4,5}, {4,5}, {7,5}, {7,5}, {7,5}, {7,5},
  33. {5,3}, {5,3}, {5,3}, {5,3}, {5,3}, {5,3}, {5,3}, {5,3},
  34. {5,3}, {5,3}, {5,3}, {5,3}, {5,3}, {5,3}, {5,3}, {5,3},
  35. {6,3}, {6,3}, {6,3}, {6,3}, {6,3}, {6,3}, {6,3}, {6,3},
  36. {6,3}, {6,3}, {6,3}, {6,3}, {6,3}, {6,3}, {6,3}, {6,3},
  37. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  38. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  39. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3},
  40. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}
  41. };
  42. static int QUANT_present_EI[] = {0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1};
  43. static VLCtab MBTYPEtabEI[] = {
  44. {-1,0}, {8,8}, {5,7}, {5,7}, {6,7}, {6,7}, {7,7}, {7,7},
  45. {-1,0}, {9,8}, {10,8}, {11,8}, {12,8}, {13,8}, {14,8}, {15,8},
  46. {4,4}, {4,4}, {4,4}, {4,4}, {4,4}, {4,4}, {4,4}, {4,4},
  47. {4,4}, {4,4}, {4,4}, {4,4}, {4,4}, {4,4}, {4,4}, {4,4},
  48. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  49. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  50. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  51. {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3}, {1,3},
  52. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  53. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  54. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  55. {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3}, {2,3},
  56. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3},
  57. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3},
  58. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3},
  59. {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}, {3,3}
  60. };
  61. static int PRED_type_EI[] = {1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3};
  62. int Decoder::getTMNMV()
  63. {
  64. int code;
  65. if (buffer.getbits1())
  66. {
  67. return 0;
  68. }
  69. if ((code = buffer.showbits(12))>=512)
  70. {
  71. code = (code>>8) - 2;
  72. buffer.flushbits(TMNMVtab0[code].len);
  73. return TMNMVtab0[code].val;
  74. }
  75. if (code>=128)
  76. {
  77. code = (code>>2) -32;
  78. buffer.flushbits(TMNMVtab1[code].len);
  79. return TMNMVtab1[code].val;
  80. }
  81. if ((code-=5)<0)
  82. {
  83. fault=1;
  84. return 0;
  85. }
  86. buffer.flushbits(TMNMVtab2[code].len);
  87. return TMNMVtab2[code].val;
  88. }
  89. int Decoder::getMCBPC()
  90. {
  91. int code;
  92. code = buffer.showbits (13);
  93. if (code >> 4 == 1)
  94. {
  95. /* macroblock stuffing */
  96. buffer.flushbits (9);
  97. return 255;
  98. }
  99. if (code == 0)
  100. {
  101. fault = 1;
  102. return 0;
  103. }
  104. if (code >= 4096)
  105. {
  106. buffer.flushbits (1);
  107. return 0;
  108. }
  109. if (code >= 16)
  110. {
  111. buffer.flushbits (MCBPCtab0[code >> 4].len);
  112. return MCBPCtab0[code >> 4].val;
  113. }
  114. else
  115. {
  116. buffer.flushbits (MCBPCtab1[code - 8].len);
  117. return MCBPCtab1[code - 8].val;
  118. }
  119. }
  120. int Decoder::getMCBPCintra()
  121. {
  122. int code;
  123. code = buffer.showbits(9);
  124. if (code == 1) {
  125. /* macroblock stuffing */
  126. buffer.flushbits(9);
  127. return 255;
  128. }
  129. if (code < 8) {
  130. fault = 1;
  131. return 0;
  132. }
  133. code >>= 3;
  134. if (code>=32)
  135. {
  136. buffer.flushbits(1);
  137. return 3;
  138. }
  139. buffer.flushbits(MCBPCtabintra[code].len);
  140. return MCBPCtabintra[code].val;
  141. }
  142. int Decoder::getCBPY()
  143. {
  144. int code;
  145. code = buffer.showbits(6);
  146. if (code < 2) {
  147. fault = 1;
  148. return -1;
  149. }
  150. if (code>=48)
  151. {
  152. buffer.flushbits(2);
  153. return 0;
  154. }
  155. buffer.flushbits(CBPYtab[code].len);
  156. return CBPYtab[code].val;
  157. }