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.

1175 lines
43KB

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