You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1078 lines
39KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... cavlc bitstream decoding
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 cavlc bitstream decoding.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #define CABAC 0
  27. #include "internal.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h264.h"
  31. #include "h264data.h" // FIXME FIXME FIXME
  32. #include "h264_mvpred.h"
  33. #include "golomb.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. static const uint8_t golomb_to_inter_cbp_gray[16]={
  37. 0, 1, 2, 4, 8, 3, 5,10,12,15, 7,11,13,14, 6, 9,
  38. };
  39. static const uint8_t golomb_to_intra4x4_cbp_gray[16]={
  40. 15, 0, 7,11,13,14, 3, 5,10,12, 1, 2, 4, 8, 6, 9,
  41. };
  42. static const uint8_t chroma_dc_coeff_token_len[4*5]={
  43. 2, 0, 0, 0,
  44. 6, 1, 0, 0,
  45. 6, 6, 3, 0,
  46. 6, 7, 7, 6,
  47. 6, 8, 8, 7,
  48. };
  49. static const uint8_t chroma_dc_coeff_token_bits[4*5]={
  50. 1, 0, 0, 0,
  51. 7, 1, 0, 0,
  52. 4, 6, 1, 0,
  53. 3, 3, 2, 5,
  54. 2, 3, 2, 0,
  55. };
  56. static const uint8_t coeff_token_len[4][4*17]={
  57. {
  58. 1, 0, 0, 0,
  59. 6, 2, 0, 0, 8, 6, 3, 0, 9, 8, 7, 5, 10, 9, 8, 6,
  60. 11,10, 9, 7, 13,11,10, 8, 13,13,11, 9, 13,13,13,10,
  61. 14,14,13,11, 14,14,14,13, 15,15,14,14, 15,15,15,14,
  62. 16,15,15,15, 16,16,16,15, 16,16,16,16, 16,16,16,16,
  63. },
  64. {
  65. 2, 0, 0, 0,
  66. 6, 2, 0, 0, 6, 5, 3, 0, 7, 6, 6, 4, 8, 6, 6, 4,
  67. 8, 7, 7, 5, 9, 8, 8, 6, 11, 9, 9, 6, 11,11,11, 7,
  68. 12,11,11, 9, 12,12,12,11, 12,12,12,11, 13,13,13,12,
  69. 13,13,13,13, 13,14,13,13, 14,14,14,13, 14,14,14,14,
  70. },
  71. {
  72. 4, 0, 0, 0,
  73. 6, 4, 0, 0, 6, 5, 4, 0, 6, 5, 5, 4, 7, 5, 5, 4,
  74. 7, 5, 5, 4, 7, 6, 6, 4, 7, 6, 6, 4, 8, 7, 7, 5,
  75. 8, 8, 7, 6, 9, 8, 8, 7, 9, 9, 8, 8, 9, 9, 9, 8,
  76. 10, 9, 9, 9, 10,10,10,10, 10,10,10,10, 10,10,10,10,
  77. },
  78. {
  79. 6, 0, 0, 0,
  80. 6, 6, 0, 0, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6,
  81. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  82. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  83. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  84. }
  85. };
  86. static const uint8_t coeff_token_bits[4][4*17]={
  87. {
  88. 1, 0, 0, 0,
  89. 5, 1, 0, 0, 7, 4, 1, 0, 7, 6, 5, 3, 7, 6, 5, 3,
  90. 7, 6, 5, 4, 15, 6, 5, 4, 11,14, 5, 4, 8,10,13, 4,
  91. 15,14, 9, 4, 11,10,13,12, 15,14, 9,12, 11,10,13, 8,
  92. 15, 1, 9,12, 11,14,13, 8, 7,10, 9,12, 4, 6, 5, 8,
  93. },
  94. {
  95. 3, 0, 0, 0,
  96. 11, 2, 0, 0, 7, 7, 3, 0, 7,10, 9, 5, 7, 6, 5, 4,
  97. 4, 6, 5, 6, 7, 6, 5, 8, 15, 6, 5, 4, 11,14,13, 4,
  98. 15,10, 9, 4, 11,14,13,12, 8,10, 9, 8, 15,14,13,12,
  99. 11,10, 9,12, 7,11, 6, 8, 9, 8,10, 1, 7, 6, 5, 4,
  100. },
  101. {
  102. 15, 0, 0, 0,
  103. 15,14, 0, 0, 11,15,13, 0, 8,12,14,12, 15,10,11,11,
  104. 11, 8, 9,10, 9,14,13, 9, 8,10, 9, 8, 15,14,13,13,
  105. 11,14,10,12, 15,10,13,12, 11,14, 9,12, 8,10,13, 8,
  106. 13, 7, 9,12, 9,12,11,10, 5, 8, 7, 6, 1, 4, 3, 2,
  107. },
  108. {
  109. 3, 0, 0, 0,
  110. 0, 1, 0, 0, 4, 5, 6, 0, 8, 9,10,11, 12,13,14,15,
  111. 16,17,18,19, 20,21,22,23, 24,25,26,27, 28,29,30,31,
  112. 32,33,34,35, 36,37,38,39, 40,41,42,43, 44,45,46,47,
  113. 48,49,50,51, 52,53,54,55, 56,57,58,59, 60,61,62,63,
  114. }
  115. };
  116. static const uint8_t total_zeros_len[16][16]= {
  117. {1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
  118. {3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
  119. {4,3,3,3,4,4,3,3,4,5,5,6,5,6},
  120. {5,3,4,4,3,3,3,4,3,4,5,5,5},
  121. {4,4,4,3,3,3,3,3,4,5,4,5},
  122. {6,5,3,3,3,3,3,3,4,3,6},
  123. {6,5,3,3,3,2,3,4,3,6},
  124. {6,4,5,3,2,2,3,3,6},
  125. {6,6,4,2,2,3,2,5},
  126. {5,5,3,2,2,2,4},
  127. {4,4,3,3,1,3},
  128. {4,4,2,1,3},
  129. {3,3,1,2},
  130. {2,2,1},
  131. {1,1},
  132. };
  133. static const uint8_t total_zeros_bits[16][16]= {
  134. {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
  135. {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
  136. {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
  137. {3,7,5,4,6,5,4,3,3,2,2,1,0},
  138. {5,4,3,7,6,5,4,3,2,1,1,0},
  139. {1,1,7,6,5,4,3,2,1,1,0},
  140. {1,1,5,4,3,3,2,1,1,0},
  141. {1,1,1,3,3,2,2,1,0},
  142. {1,0,1,3,2,1,1,1},
  143. {1,0,1,3,2,1,1},
  144. {0,1,1,2,1,3},
  145. {0,1,1,1,1},
  146. {0,1,1,1},
  147. {0,1,1},
  148. {0,1},
  149. };
  150. static const uint8_t chroma_dc_total_zeros_len[3][4]= {
  151. { 1, 2, 3, 3,},
  152. { 1, 2, 2, 0,},
  153. { 1, 1, 0, 0,},
  154. };
  155. static const uint8_t chroma_dc_total_zeros_bits[3][4]= {
  156. { 1, 1, 1, 0,},
  157. { 1, 1, 0, 0,},
  158. { 1, 0, 0, 0,},
  159. };
  160. static const uint8_t run_len[7][16]={
  161. {1,1},
  162. {1,2,2},
  163. {2,2,2,2},
  164. {2,2,2,3,3},
  165. {2,2,3,3,3,3},
  166. {2,3,3,3,3,3,3},
  167. {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
  168. };
  169. static const uint8_t run_bits[7][16]={
  170. {1,0},
  171. {1,1,0},
  172. {3,2,1,0},
  173. {3,2,1,1,0},
  174. {3,2,3,2,1,0},
  175. {3,0,1,3,2,5,4},
  176. {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
  177. };
  178. static VLC coeff_token_vlc[4];
  179. static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2];
  180. static const int coeff_token_vlc_tables_size[4]={520,332,280,256};
  181. static VLC chroma_dc_coeff_token_vlc;
  182. static VLC_TYPE chroma_dc_coeff_token_vlc_table[256][2];
  183. static const int chroma_dc_coeff_token_vlc_table_size = 256;
  184. static VLC total_zeros_vlc[15];
  185. static VLC_TYPE total_zeros_vlc_tables[15][512][2];
  186. static const int total_zeros_vlc_tables_size = 512;
  187. static VLC chroma_dc_total_zeros_vlc[3];
  188. static VLC_TYPE chroma_dc_total_zeros_vlc_tables[3][8][2];
  189. static const int chroma_dc_total_zeros_vlc_tables_size = 8;
  190. static VLC run_vlc[6];
  191. static VLC_TYPE run_vlc_tables[6][8][2];
  192. static const int run_vlc_tables_size = 8;
  193. static VLC run7_vlc;
  194. static VLC_TYPE run7_vlc_table[96][2];
  195. static const int run7_vlc_table_size = 96;
  196. #define LEVEL_TAB_BITS 8
  197. static int8_t cavlc_level_tab[7][1<<LEVEL_TAB_BITS][2];
  198. /**
  199. * gets the predicted number of non-zero coefficients.
  200. * @param n block index
  201. */
  202. static inline int pred_non_zero_count(H264Context *h, int n){
  203. const int index8= scan8[n];
  204. const int left= h->non_zero_count_cache[index8 - 1];
  205. const int top = h->non_zero_count_cache[index8 - 8];
  206. int i= left + top;
  207. if(i<64) i= (i+1)>>1;
  208. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  209. return i&31;
  210. }
  211. static av_cold void init_cavlc_level_tab(void){
  212. int suffix_length;
  213. unsigned int i;
  214. for(suffix_length=0; suffix_length<7; suffix_length++){
  215. for(i=0; i<(1<<LEVEL_TAB_BITS); i++){
  216. int prefix= LEVEL_TAB_BITS - av_log2(2*i);
  217. if(prefix + 1 + suffix_length <= LEVEL_TAB_BITS){
  218. int level_code = (prefix << suffix_length) +
  219. (i >> (av_log2(i) - suffix_length)) - (1 << suffix_length);
  220. int mask = -(level_code&1);
  221. level_code = (((2 + level_code) >> 1) ^ mask) - mask;
  222. cavlc_level_tab[suffix_length][i][0]= level_code;
  223. cavlc_level_tab[suffix_length][i][1]= prefix + 1 + suffix_length;
  224. }else if(prefix + 1 <= LEVEL_TAB_BITS){
  225. cavlc_level_tab[suffix_length][i][0]= prefix+100;
  226. cavlc_level_tab[suffix_length][i][1]= prefix + 1;
  227. }else{
  228. cavlc_level_tab[suffix_length][i][0]= LEVEL_TAB_BITS+100;
  229. cavlc_level_tab[suffix_length][i][1]= LEVEL_TAB_BITS;
  230. }
  231. }
  232. }
  233. }
  234. av_cold void ff_h264_decode_init_vlc(void){
  235. static int done = 0;
  236. if (!done) {
  237. int i;
  238. int offset;
  239. done = 1;
  240. chroma_dc_coeff_token_vlc.table = chroma_dc_coeff_token_vlc_table;
  241. chroma_dc_coeff_token_vlc.table_allocated = chroma_dc_coeff_token_vlc_table_size;
  242. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  243. &chroma_dc_coeff_token_len [0], 1, 1,
  244. &chroma_dc_coeff_token_bits[0], 1, 1,
  245. INIT_VLC_USE_NEW_STATIC);
  246. offset = 0;
  247. for(i=0; i<4; i++){
  248. coeff_token_vlc[i].table = coeff_token_vlc_tables+offset;
  249. coeff_token_vlc[i].table_allocated = coeff_token_vlc_tables_size[i];
  250. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  251. &coeff_token_len [i][0], 1, 1,
  252. &coeff_token_bits[i][0], 1, 1,
  253. INIT_VLC_USE_NEW_STATIC);
  254. offset += coeff_token_vlc_tables_size[i];
  255. }
  256. /*
  257. * This is a one time safety check to make sure that
  258. * the packed static coeff_token_vlc table sizes
  259. * were initialized correctly.
  260. */
  261. assert(offset == FF_ARRAY_ELEMS(coeff_token_vlc_tables));
  262. for(i=0; i<3; i++){
  263. chroma_dc_total_zeros_vlc[i].table = chroma_dc_total_zeros_vlc_tables[i];
  264. chroma_dc_total_zeros_vlc[i].table_allocated = chroma_dc_total_zeros_vlc_tables_size;
  265. init_vlc(&chroma_dc_total_zeros_vlc[i],
  266. CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  267. &chroma_dc_total_zeros_len [i][0], 1, 1,
  268. &chroma_dc_total_zeros_bits[i][0], 1, 1,
  269. INIT_VLC_USE_NEW_STATIC);
  270. }
  271. for(i=0; i<15; i++){
  272. total_zeros_vlc[i].table = total_zeros_vlc_tables[i];
  273. total_zeros_vlc[i].table_allocated = total_zeros_vlc_tables_size;
  274. init_vlc(&total_zeros_vlc[i],
  275. TOTAL_ZEROS_VLC_BITS, 16,
  276. &total_zeros_len [i][0], 1, 1,
  277. &total_zeros_bits[i][0], 1, 1,
  278. INIT_VLC_USE_NEW_STATIC);
  279. }
  280. for(i=0; i<6; i++){
  281. run_vlc[i].table = run_vlc_tables[i];
  282. run_vlc[i].table_allocated = run_vlc_tables_size;
  283. init_vlc(&run_vlc[i],
  284. RUN_VLC_BITS, 7,
  285. &run_len [i][0], 1, 1,
  286. &run_bits[i][0], 1, 1,
  287. INIT_VLC_USE_NEW_STATIC);
  288. }
  289. run7_vlc.table = run7_vlc_table,
  290. run7_vlc.table_allocated = run7_vlc_table_size;
  291. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  292. &run_len [6][0], 1, 1,
  293. &run_bits[6][0], 1, 1,
  294. INIT_VLC_USE_NEW_STATIC);
  295. init_cavlc_level_tab();
  296. }
  297. }
  298. /**
  299. *
  300. */
  301. static inline int get_level_prefix(GetBitContext *gb){
  302. unsigned int buf;
  303. int log;
  304. OPEN_READER(re, gb);
  305. UPDATE_CACHE(re, gb);
  306. buf=GET_CACHE(re, gb);
  307. log= 32 - av_log2(buf);
  308. #ifdef TRACE
  309. print_bin(buf>>(32-log), log);
  310. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  311. #endif
  312. LAST_SKIP_BITS(re, gb, log);
  313. CLOSE_READER(re, gb);
  314. return log-1;
  315. }
  316. /**
  317. * decodes a residual block.
  318. * @param n block index
  319. * @param scantable scantable
  320. * @param max_coeff number of coefficients in the block
  321. * @return <0 if an error occurred
  322. */
  323. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  324. MpegEncContext * const s = &h->s;
  325. static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  326. int level[16];
  327. int zeros_left, coeff_token, total_coeff, i, trailing_ones, run_before;
  328. //FIXME put trailing_onex into the context
  329. if(max_coeff <= 8){
  330. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  331. total_coeff= coeff_token>>2;
  332. }else{
  333. if(n >= LUMA_DC_BLOCK_INDEX){
  334. total_coeff= pred_non_zero_count(h, (n - LUMA_DC_BLOCK_INDEX)*16);
  335. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  336. total_coeff= coeff_token>>2;
  337. }else{
  338. total_coeff= pred_non_zero_count(h, n);
  339. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  340. total_coeff= coeff_token>>2;
  341. }
  342. }
  343. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  344. //FIXME set last_non_zero?
  345. if(total_coeff==0)
  346. return 0;
  347. if(total_coeff > (unsigned)max_coeff) {
  348. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  349. return -1;
  350. }
  351. trailing_ones= coeff_token&3;
  352. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  353. assert(total_coeff<=16);
  354. i = show_bits(gb, 3);
  355. skip_bits(gb, trailing_ones);
  356. level[0] = 1-((i&4)>>1);
  357. level[1] = 1-((i&2) );
  358. level[2] = 1-((i&1)<<1);
  359. if(trailing_ones<total_coeff) {
  360. int mask, prefix;
  361. int suffix_length = total_coeff > 10 & trailing_ones < 3;
  362. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  363. int level_code= cavlc_level_tab[suffix_length][bitsi][0];
  364. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  365. if(level_code >= 100){
  366. prefix= level_code - 100;
  367. if(prefix == LEVEL_TAB_BITS)
  368. prefix += get_level_prefix(gb);
  369. //first coefficient has suffix_length equal to 0 or 1
  370. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  371. if(suffix_length)
  372. level_code= (prefix<<1) + get_bits1(gb); //part
  373. else
  374. level_code= prefix; //part
  375. }else if(prefix==14){
  376. if(suffix_length)
  377. level_code= (prefix<<1) + get_bits1(gb); //part
  378. else
  379. level_code= prefix + get_bits(gb, 4); //part
  380. }else{
  381. level_code= 30 + get_bits(gb, prefix-3); //part
  382. if(prefix>=16){
  383. if(prefix > 25+3){
  384. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid level prefix\n");
  385. return -1;
  386. }
  387. level_code += (1<<(prefix-3))-4096;
  388. }
  389. }
  390. if(trailing_ones < 3) level_code += 2;
  391. suffix_length = 2;
  392. mask= -(level_code&1);
  393. level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask;
  394. }else{
  395. level_code += ((level_code>>31)|1) & -(trailing_ones < 3);
  396. suffix_length = 1 + (level_code + 3U > 6U);
  397. level[trailing_ones]= level_code;
  398. }
  399. //remaining coefficients have suffix_length > 0
  400. for(i=trailing_ones+1;i<total_coeff;i++) {
  401. static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX };
  402. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  403. level_code= cavlc_level_tab[suffix_length][bitsi][0];
  404. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  405. if(level_code >= 100){
  406. prefix= level_code - 100;
  407. if(prefix == LEVEL_TAB_BITS){
  408. prefix += get_level_prefix(gb);
  409. }
  410. if(prefix<15){
  411. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  412. }else{
  413. level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
  414. if(prefix>=16)
  415. level_code += (1<<(prefix-3))-4096;
  416. }
  417. mask= -(level_code&1);
  418. level_code= (((2+level_code)>>1) ^ mask) - mask;
  419. }
  420. level[i]= level_code;
  421. suffix_length+= suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length];
  422. }
  423. }
  424. if(total_coeff == max_coeff)
  425. zeros_left=0;
  426. else{
  427. /* FIXME: we don't actually support 4:2:2 yet. */
  428. if(max_coeff <= 8)
  429. zeros_left= get_vlc2(gb, (chroma_dc_total_zeros_vlc-1)[ total_coeff ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  430. else
  431. zeros_left= get_vlc2(gb, (total_zeros_vlc-1)[ total_coeff ].table, TOTAL_ZEROS_VLC_BITS, 1);
  432. }
  433. #define STORE_BLOCK(type) \
  434. scantable += zeros_left + total_coeff - 1; \
  435. if(n >= LUMA_DC_BLOCK_INDEX){ \
  436. ((type*)block)[*scantable] = level[0]; \
  437. for(i=1;i<total_coeff && zeros_left > 0;i++) { \
  438. if(zeros_left < 7) \
  439. run_before= get_vlc2(gb, (run_vlc-1)[zeros_left].table, RUN_VLC_BITS, 1); \
  440. else \
  441. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \
  442. zeros_left -= run_before; \
  443. scantable -= 1 + run_before; \
  444. ((type*)block)[*scantable]= level[i]; \
  445. } \
  446. for(;i<total_coeff;i++) { \
  447. scantable--; \
  448. ((type*)block)[*scantable]= level[i]; \
  449. } \
  450. }else{ \
  451. ((type*)block)[*scantable] = ((int)(level[0] * qmul[*scantable] + 32))>>6; \
  452. for(i=1;i<total_coeff && zeros_left > 0;i++) { \
  453. if(zeros_left < 7) \
  454. run_before= get_vlc2(gb, (run_vlc-1)[zeros_left].table, RUN_VLC_BITS, 1); \
  455. else \
  456. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \
  457. zeros_left -= run_before; \
  458. scantable -= 1 + run_before; \
  459. ((type*)block)[*scantable]= ((int)(level[i] * qmul[*scantable] + 32))>>6; \
  460. } \
  461. for(;i<total_coeff;i++) { \
  462. scantable--; \
  463. ((type*)block)[*scantable]= ((int)(level[i] * qmul[*scantable] + 32))>>6; \
  464. } \
  465. }
  466. if (h->pixel_shift) {
  467. STORE_BLOCK(int32_t)
  468. } else {
  469. STORE_BLOCK(int16_t)
  470. }
  471. if(zeros_left<0){
  472. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  473. return -1;
  474. }
  475. return 0;
  476. }
  477. static av_always_inline int decode_luma_residual(H264Context *h, GetBitContext *gb, const uint8_t *scan, const uint8_t *scan8x8, int pixel_shift, int mb_type, int cbp, int p){
  478. int i4x4, i8x8;
  479. MpegEncContext * const s = &h->s;
  480. int qscale = p == 0 ? s->qscale : h->chroma_qp[p-1];
  481. if(IS_INTRA16x16(mb_type)){
  482. AV_ZERO128(h->mb_luma_dc[p]+0);
  483. AV_ZERO128(h->mb_luma_dc[p]+8);
  484. AV_ZERO128(h->mb_luma_dc[p]+16);
  485. AV_ZERO128(h->mb_luma_dc[p]+24);
  486. if( decode_residual(h, h->intra_gb_ptr, h->mb_luma_dc[p], LUMA_DC_BLOCK_INDEX+p, scan, NULL, 16) < 0){
  487. return -1; //FIXME continue if partitioned and other return -1 too
  488. }
  489. assert((cbp&15) == 0 || (cbp&15) == 15);
  490. if(cbp&15){
  491. for(i8x8=0; i8x8<4; i8x8++){
  492. for(i4x4=0; i4x4<4; i4x4++){
  493. const int index= i4x4 + 4*i8x8 + p*16;
  494. if( decode_residual(h, h->intra_gb_ptr, h->mb + (16*index << pixel_shift),
  495. index, scan + 1, h->dequant4_coeff[p][qscale], 15) < 0 ){
  496. return -1;
  497. }
  498. }
  499. }
  500. return 0xf;
  501. }else{
  502. fill_rectangle(&h->non_zero_count_cache[scan8[p*16]], 4, 4, 8, 0, 1);
  503. return 0;
  504. }
  505. }else{
  506. int cqm = (IS_INTRA( mb_type ) ? 0:3)+p;
  507. /* For CAVLC 4:4:4, we need to keep track of the luma 8x8 CBP for deblocking nnz purposes. */
  508. int new_cbp = 0;
  509. for(i8x8=0; i8x8<4; i8x8++){
  510. if(cbp & (1<<i8x8)){
  511. if(IS_8x8DCT(mb_type)){
  512. DCTELEM *buf = &h->mb[64*i8x8+256*p << pixel_shift];
  513. uint8_t *nnz;
  514. for(i4x4=0; i4x4<4; i4x4++){
  515. const int index= i4x4 + 4*i8x8 + p*16;
  516. if( decode_residual(h, gb, buf, index, scan8x8+16*i4x4,
  517. h->dequant8_coeff[cqm][qscale], 16) < 0 )
  518. return -1;
  519. }
  520. nnz= &h->non_zero_count_cache[ scan8[4*i8x8+p*16] ];
  521. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  522. new_cbp |= !!nnz[0] << i8x8;
  523. }else{
  524. for(i4x4=0; i4x4<4; i4x4++){
  525. const int index= i4x4 + 4*i8x8 + p*16;
  526. if( decode_residual(h, gb, h->mb + (16*index << pixel_shift), index,
  527. scan, h->dequant4_coeff[cqm][qscale], 16) < 0 ){
  528. return -1;
  529. }
  530. new_cbp |= h->non_zero_count_cache[ scan8[index] ] << i8x8;
  531. }
  532. }
  533. }else{
  534. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8+p*16] ];
  535. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  536. }
  537. }
  538. return new_cbp;
  539. }
  540. }
  541. int ff_h264_decode_mb_cavlc(H264Context *h){
  542. MpegEncContext * const s = &h->s;
  543. int mb_xy;
  544. int partition_count;
  545. unsigned int mb_type, cbp;
  546. int dct8x8_allowed= h->pps.transform_8x8_mode;
  547. int decode_chroma = h->sps.chroma_format_idc == 1 || h->sps.chroma_format_idc == 2;
  548. const int pixel_shift = h->pixel_shift;
  549. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  550. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  551. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  552. down the code */
  553. if(h->slice_type_nos != AV_PICTURE_TYPE_I){
  554. if(s->mb_skip_run==-1)
  555. s->mb_skip_run= get_ue_golomb(&s->gb);
  556. if (s->mb_skip_run--) {
  557. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  558. if(s->mb_skip_run==0)
  559. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  560. }
  561. decode_mb_skip(h);
  562. return 0;
  563. }
  564. }
  565. if(FRAME_MBAFF){
  566. if( (s->mb_y&1) == 0 )
  567. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  568. }
  569. h->prev_mb_skipped= 0;
  570. mb_type= get_ue_golomb(&s->gb);
  571. if(h->slice_type_nos == AV_PICTURE_TYPE_B){
  572. if(mb_type < 23){
  573. partition_count= b_mb_type_info[mb_type].partition_count;
  574. mb_type= b_mb_type_info[mb_type].type;
  575. }else{
  576. mb_type -= 23;
  577. goto decode_intra_mb;
  578. }
  579. }else if(h->slice_type_nos == AV_PICTURE_TYPE_P){
  580. if(mb_type < 5){
  581. partition_count= p_mb_type_info[mb_type].partition_count;
  582. mb_type= p_mb_type_info[mb_type].type;
  583. }else{
  584. mb_type -= 5;
  585. goto decode_intra_mb;
  586. }
  587. }else{
  588. assert(h->slice_type_nos == AV_PICTURE_TYPE_I);
  589. if(h->slice_type == AV_PICTURE_TYPE_SI && mb_type)
  590. mb_type--;
  591. decode_intra_mb:
  592. if(mb_type > 25){
  593. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_picture_type_char(h->slice_type), s->mb_x, s->mb_y);
  594. return -1;
  595. }
  596. partition_count=0;
  597. cbp= i_mb_type_info[mb_type].cbp;
  598. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  599. mb_type= i_mb_type_info[mb_type].type;
  600. }
  601. if(MB_FIELD)
  602. mb_type |= MB_TYPE_INTERLACED;
  603. h->slice_table[ mb_xy ]= h->slice_num;
  604. if(IS_INTRA_PCM(mb_type)){
  605. unsigned int x;
  606. static const uint16_t mb_sizes[4] = {256,384,512,768};
  607. const int mb_size = mb_sizes[h->sps.chroma_format_idc]*h->sps.bit_depth_luma >> 3;
  608. // We assume these blocks are very rare so we do not optimize it.
  609. align_get_bits(&s->gb);
  610. // The pixels are stored in the same order as levels in h->mb array.
  611. for(x=0; x < mb_size; x++){
  612. ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
  613. }
  614. // In deblocking, the quantizer is 0
  615. s->current_picture.f.qscale_table[mb_xy] = 0;
  616. // All coeffs are present
  617. memset(h->non_zero_count[mb_xy], 16, 48);
  618. s->current_picture.f.mb_type[mb_xy] = mb_type;
  619. return 0;
  620. }
  621. if(MB_MBAFF){
  622. h->ref_count[0] <<= 1;
  623. h->ref_count[1] <<= 1;
  624. }
  625. fill_decode_neighbors(h, mb_type);
  626. fill_decode_caches(h, mb_type);
  627. //mb_pred
  628. if(IS_INTRA(mb_type)){
  629. int pred_mode;
  630. // init_top_left_availability(h);
  631. if(IS_INTRA4x4(mb_type)){
  632. int i;
  633. int di = 1;
  634. if(dct8x8_allowed && get_bits1(&s->gb)){
  635. mb_type |= MB_TYPE_8x8DCT;
  636. di = 4;
  637. }
  638. // fill_intra4x4_pred_table(h);
  639. for(i=0; i<16; i+=di){
  640. int mode= pred_intra_mode(h, i);
  641. if(!get_bits1(&s->gb)){
  642. const int rem_mode= get_bits(&s->gb, 3);
  643. mode = rem_mode + (rem_mode >= mode);
  644. }
  645. if(di==4)
  646. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  647. else
  648. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  649. }
  650. write_back_intra_pred_mode(h);
  651. if( ff_h264_check_intra4x4_pred_mode(h) < 0)
  652. return -1;
  653. }else{
  654. h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode(h, h->intra16x16_pred_mode);
  655. if(h->intra16x16_pred_mode < 0)
  656. return -1;
  657. }
  658. if(decode_chroma){
  659. pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb));
  660. if(pred_mode < 0)
  661. return -1;
  662. h->chroma_pred_mode= pred_mode;
  663. } else {
  664. h->chroma_pred_mode = DC_128_PRED8x8;
  665. }
  666. }else if(partition_count==4){
  667. int i, j, sub_partition_count[4], list, ref[2][4];
  668. if(h->slice_type_nos == AV_PICTURE_TYPE_B){
  669. for(i=0; i<4; i++){
  670. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  671. if(h->sub_mb_type[i] >=13){
  672. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  673. return -1;
  674. }
  675. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  676. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  677. }
  678. if( IS_DIRECT(h->sub_mb_type[0]|h->sub_mb_type[1]|h->sub_mb_type[2]|h->sub_mb_type[3])) {
  679. ff_h264_pred_direct_motion(h, &mb_type);
  680. h->ref_cache[0][scan8[4]] =
  681. h->ref_cache[1][scan8[4]] =
  682. h->ref_cache[0][scan8[12]] =
  683. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  684. }
  685. }else{
  686. assert(h->slice_type_nos == AV_PICTURE_TYPE_P); //FIXME SP correct ?
  687. for(i=0; i<4; i++){
  688. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  689. if(h->sub_mb_type[i] >=4){
  690. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  691. return -1;
  692. }
  693. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  694. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  695. }
  696. }
  697. for(list=0; list<h->list_count; list++){
  698. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  699. for(i=0; i<4; i++){
  700. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  701. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  702. unsigned int tmp;
  703. if(ref_count == 1){
  704. tmp= 0;
  705. }else if(ref_count == 2){
  706. tmp= get_bits1(&s->gb)^1;
  707. }else{
  708. tmp= get_ue_golomb_31(&s->gb);
  709. if(tmp>=ref_count){
  710. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  711. return -1;
  712. }
  713. }
  714. ref[list][i]= tmp;
  715. }else{
  716. //FIXME
  717. ref[list][i] = -1;
  718. }
  719. }
  720. }
  721. if(dct8x8_allowed)
  722. dct8x8_allowed = get_dct8x8_allowed(h);
  723. for(list=0; list<h->list_count; list++){
  724. for(i=0; i<4; i++){
  725. if(IS_DIRECT(h->sub_mb_type[i])) {
  726. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  727. continue;
  728. }
  729. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  730. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  731. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  732. const int sub_mb_type= h->sub_mb_type[i];
  733. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  734. for(j=0; j<sub_partition_count[i]; j++){
  735. int mx, my;
  736. const int index= 4*i + block_width*j;
  737. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  738. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  739. mx += get_se_golomb(&s->gb);
  740. my += get_se_golomb(&s->gb);
  741. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  742. if(IS_SUB_8X8(sub_mb_type)){
  743. mv_cache[ 1 ][0]=
  744. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  745. mv_cache[ 1 ][1]=
  746. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  747. }else if(IS_SUB_8X4(sub_mb_type)){
  748. mv_cache[ 1 ][0]= mx;
  749. mv_cache[ 1 ][1]= my;
  750. }else if(IS_SUB_4X8(sub_mb_type)){
  751. mv_cache[ 8 ][0]= mx;
  752. mv_cache[ 8 ][1]= my;
  753. }
  754. mv_cache[ 0 ][0]= mx;
  755. mv_cache[ 0 ][1]= my;
  756. }
  757. }else{
  758. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  759. p[0] = p[1]=
  760. p[8] = p[9]= 0;
  761. }
  762. }
  763. }
  764. }else if(IS_DIRECT(mb_type)){
  765. ff_h264_pred_direct_motion(h, &mb_type);
  766. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  767. }else{
  768. int list, mx, my, i;
  769. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  770. if(IS_16X16(mb_type)){
  771. for(list=0; list<h->list_count; list++){
  772. unsigned int val;
  773. if(IS_DIR(mb_type, 0, list)){
  774. if(h->ref_count[list]==1){
  775. val= 0;
  776. }else if(h->ref_count[list]==2){
  777. val= get_bits1(&s->gb)^1;
  778. }else{
  779. val= get_ue_golomb_31(&s->gb);
  780. if(val >= h->ref_count[list]){
  781. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  782. return -1;
  783. }
  784. }
  785. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  786. }
  787. }
  788. for(list=0; list<h->list_count; list++){
  789. if(IS_DIR(mb_type, 0, list)){
  790. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  791. mx += get_se_golomb(&s->gb);
  792. my += get_se_golomb(&s->gb);
  793. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  794. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  795. }
  796. }
  797. }
  798. else if(IS_16X8(mb_type)){
  799. for(list=0; list<h->list_count; list++){
  800. for(i=0; i<2; i++){
  801. unsigned int val;
  802. if(IS_DIR(mb_type, i, list)){
  803. if(h->ref_count[list] == 1){
  804. val= 0;
  805. }else if(h->ref_count[list] == 2){
  806. val= get_bits1(&s->gb)^1;
  807. }else{
  808. val= get_ue_golomb_31(&s->gb);
  809. if(val >= h->ref_count[list]){
  810. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  811. return -1;
  812. }
  813. }
  814. }else
  815. val= LIST_NOT_USED&0xFF;
  816. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  817. }
  818. }
  819. for(list=0; list<h->list_count; list++){
  820. for(i=0; i<2; i++){
  821. unsigned int val;
  822. if(IS_DIR(mb_type, i, list)){
  823. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  824. mx += get_se_golomb(&s->gb);
  825. my += get_se_golomb(&s->gb);
  826. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  827. val= pack16to32(mx,my);
  828. }else
  829. val=0;
  830. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  831. }
  832. }
  833. }else{
  834. assert(IS_8X16(mb_type));
  835. for(list=0; list<h->list_count; list++){
  836. for(i=0; i<2; i++){
  837. unsigned int val;
  838. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  839. if(h->ref_count[list]==1){
  840. val= 0;
  841. }else if(h->ref_count[list]==2){
  842. val= get_bits1(&s->gb)^1;
  843. }else{
  844. val= get_ue_golomb_31(&s->gb);
  845. if(val >= h->ref_count[list]){
  846. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  847. return -1;
  848. }
  849. }
  850. }else
  851. val= LIST_NOT_USED&0xFF;
  852. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  853. }
  854. }
  855. for(list=0; list<h->list_count; list++){
  856. for(i=0; i<2; i++){
  857. unsigned int val;
  858. if(IS_DIR(mb_type, i, list)){
  859. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  860. mx += get_se_golomb(&s->gb);
  861. my += get_se_golomb(&s->gb);
  862. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  863. val= pack16to32(mx,my);
  864. }else
  865. val=0;
  866. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  867. }
  868. }
  869. }
  870. }
  871. if(IS_INTER(mb_type))
  872. write_back_motion(h, mb_type);
  873. if(!IS_INTRA16x16(mb_type)){
  874. cbp= get_ue_golomb(&s->gb);
  875. if(decode_chroma){
  876. if(cbp > 47){
  877. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  878. return -1;
  879. }
  880. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
  881. else cbp= golomb_to_inter_cbp [cbp];
  882. }else{
  883. if(cbp > 15){
  884. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  885. return -1;
  886. }
  887. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
  888. else cbp= golomb_to_inter_cbp_gray[cbp];
  889. }
  890. }
  891. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  892. mb_type |= MB_TYPE_8x8DCT*get_bits1(&s->gb);
  893. }
  894. h->cbp=
  895. h->cbp_table[mb_xy]= cbp;
  896. s->current_picture.f.mb_type[mb_xy] = mb_type;
  897. if(cbp || IS_INTRA16x16(mb_type)){
  898. int i4x4, chroma_idx;
  899. int dquant;
  900. int ret;
  901. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  902. const uint8_t *scan, *scan8x8;
  903. const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
  904. if(IS_INTERLACED(mb_type)){
  905. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  906. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  907. }else{
  908. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  909. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  910. }
  911. dquant= get_se_golomb(&s->gb);
  912. s->qscale += dquant;
  913. if(((unsigned)s->qscale) > max_qp){
  914. if(s->qscale<0) s->qscale+= max_qp+1;
  915. else s->qscale-= max_qp+1;
  916. if(((unsigned)s->qscale) > max_qp){
  917. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  918. return -1;
  919. }
  920. }
  921. h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
  922. h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
  923. if( (ret = decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 0)) < 0 ){
  924. return -1;
  925. }
  926. h->cbp_table[mb_xy] |= ret << 12;
  927. if(CHROMA444){
  928. if( decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 1) < 0 ){
  929. return -1;
  930. }
  931. if( decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 2) < 0 ){
  932. return -1;
  933. }
  934. } else {
  935. if(cbp&0x30){
  936. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  937. if( decode_residual(h, gb, h->mb + ((256 + 16*16*chroma_idx) << pixel_shift), CHROMA_DC_BLOCK_INDEX+chroma_idx, chroma_dc_scan, NULL, 4) < 0){
  938. return -1;
  939. }
  940. }
  941. if(cbp&0x20){
  942. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  943. const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
  944. for(i4x4=0; i4x4<4; i4x4++){
  945. const int index= 16 + 16*chroma_idx + i4x4;
  946. if( decode_residual(h, gb, h->mb + (16*index << pixel_shift), index, scan + 1, qmul, 15) < 0){
  947. return -1;
  948. }
  949. }
  950. }
  951. }else{
  952. fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);
  953. fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);
  954. }
  955. }
  956. }else{
  957. fill_rectangle(&h->non_zero_count_cache[scan8[ 0]], 4, 4, 8, 0, 1);
  958. fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);
  959. fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);
  960. }
  961. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  962. write_back_non_zero_count(h);
  963. if(MB_MBAFF){
  964. h->ref_count[0] >>= 1;
  965. h->ref_count[1] >>= 1;
  966. }
  967. return 0;
  968. }