Libav 0.7.1
|
00001 /* 00002 * H.26L/H.264/AVC/JVT/14496-10/... cavlc bitstream decoding 00003 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #define CABAC 0 00029 00030 #include "internal.h" 00031 #include "avcodec.h" 00032 #include "mpegvideo.h" 00033 #include "h264.h" 00034 #include "h264data.h" // FIXME FIXME FIXME 00035 #include "h264_mvpred.h" 00036 #include "golomb.h" 00037 00038 //#undef NDEBUG 00039 #include <assert.h> 00040 00041 static const uint8_t golomb_to_inter_cbp_gray[16]={ 00042 0, 1, 2, 4, 8, 3, 5,10,12,15, 7,11,13,14, 6, 9, 00043 }; 00044 00045 static const uint8_t golomb_to_intra4x4_cbp_gray[16]={ 00046 15, 0, 7,11,13,14, 3, 5,10,12, 1, 2, 4, 8, 6, 9, 00047 }; 00048 00049 static const uint8_t chroma_dc_coeff_token_len[4*5]={ 00050 2, 0, 0, 0, 00051 6, 1, 0, 0, 00052 6, 6, 3, 0, 00053 6, 7, 7, 6, 00054 6, 8, 8, 7, 00055 }; 00056 00057 static const uint8_t chroma_dc_coeff_token_bits[4*5]={ 00058 1, 0, 0, 0, 00059 7, 1, 0, 0, 00060 4, 6, 1, 0, 00061 3, 3, 2, 5, 00062 2, 3, 2, 0, 00063 }; 00064 00065 static const uint8_t coeff_token_len[4][4*17]={ 00066 { 00067 1, 0, 0, 0, 00068 6, 2, 0, 0, 8, 6, 3, 0, 9, 8, 7, 5, 10, 9, 8, 6, 00069 11,10, 9, 7, 13,11,10, 8, 13,13,11, 9, 13,13,13,10, 00070 14,14,13,11, 14,14,14,13, 15,15,14,14, 15,15,15,14, 00071 16,15,15,15, 16,16,16,15, 16,16,16,16, 16,16,16,16, 00072 }, 00073 { 00074 2, 0, 0, 0, 00075 6, 2, 0, 0, 6, 5, 3, 0, 7, 6, 6, 4, 8, 6, 6, 4, 00076 8, 7, 7, 5, 9, 8, 8, 6, 11, 9, 9, 6, 11,11,11, 7, 00077 12,11,11, 9, 12,12,12,11, 12,12,12,11, 13,13,13,12, 00078 13,13,13,13, 13,14,13,13, 14,14,14,13, 14,14,14,14, 00079 }, 00080 { 00081 4, 0, 0, 0, 00082 6, 4, 0, 0, 6, 5, 4, 0, 6, 5, 5, 4, 7, 5, 5, 4, 00083 7, 5, 5, 4, 7, 6, 6, 4, 7, 6, 6, 4, 8, 7, 7, 5, 00084 8, 8, 7, 6, 9, 8, 8, 7, 9, 9, 8, 8, 9, 9, 9, 8, 00085 10, 9, 9, 9, 10,10,10,10, 10,10,10,10, 10,10,10,10, 00086 }, 00087 { 00088 6, 0, 0, 0, 00089 6, 6, 0, 0, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 00090 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 00091 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 00092 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 00093 } 00094 }; 00095 00096 static const uint8_t coeff_token_bits[4][4*17]={ 00097 { 00098 1, 0, 0, 0, 00099 5, 1, 0, 0, 7, 4, 1, 0, 7, 6, 5, 3, 7, 6, 5, 3, 00100 7, 6, 5, 4, 15, 6, 5, 4, 11,14, 5, 4, 8,10,13, 4, 00101 15,14, 9, 4, 11,10,13,12, 15,14, 9,12, 11,10,13, 8, 00102 15, 1, 9,12, 11,14,13, 8, 7,10, 9,12, 4, 6, 5, 8, 00103 }, 00104 { 00105 3, 0, 0, 0, 00106 11, 2, 0, 0, 7, 7, 3, 0, 7,10, 9, 5, 7, 6, 5, 4, 00107 4, 6, 5, 6, 7, 6, 5, 8, 15, 6, 5, 4, 11,14,13, 4, 00108 15,10, 9, 4, 11,14,13,12, 8,10, 9, 8, 15,14,13,12, 00109 11,10, 9,12, 7,11, 6, 8, 9, 8,10, 1, 7, 6, 5, 4, 00110 }, 00111 { 00112 15, 0, 0, 0, 00113 15,14, 0, 0, 11,15,13, 0, 8,12,14,12, 15,10,11,11, 00114 11, 8, 9,10, 9,14,13, 9, 8,10, 9, 8, 15,14,13,13, 00115 11,14,10,12, 15,10,13,12, 11,14, 9,12, 8,10,13, 8, 00116 13, 7, 9,12, 9,12,11,10, 5, 8, 7, 6, 1, 4, 3, 2, 00117 }, 00118 { 00119 3, 0, 0, 0, 00120 0, 1, 0, 0, 4, 5, 6, 0, 8, 9,10,11, 12,13,14,15, 00121 16,17,18,19, 20,21,22,23, 24,25,26,27, 28,29,30,31, 00122 32,33,34,35, 36,37,38,39, 40,41,42,43, 44,45,46,47, 00123 48,49,50,51, 52,53,54,55, 56,57,58,59, 60,61,62,63, 00124 } 00125 }; 00126 00127 static const uint8_t total_zeros_len[16][16]= { 00128 {1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9}, 00129 {3,3,3,3,3,4,4,4,4,5,5,6,6,6,6}, 00130 {4,3,3,3,4,4,3,3,4,5,5,6,5,6}, 00131 {5,3,4,4,3,3,3,4,3,4,5,5,5}, 00132 {4,4,4,3,3,3,3,3,4,5,4,5}, 00133 {6,5,3,3,3,3,3,3,4,3,6}, 00134 {6,5,3,3,3,2,3,4,3,6}, 00135 {6,4,5,3,2,2,3,3,6}, 00136 {6,6,4,2,2,3,2,5}, 00137 {5,5,3,2,2,2,4}, 00138 {4,4,3,3,1,3}, 00139 {4,4,2,1,3}, 00140 {3,3,1,2}, 00141 {2,2,1}, 00142 {1,1}, 00143 }; 00144 00145 static const uint8_t total_zeros_bits[16][16]= { 00146 {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1}, 00147 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0}, 00148 {5,7,6,5,4,3,4,3,2,3,2,1,1,0}, 00149 {3,7,5,4,6,5,4,3,3,2,2,1,0}, 00150 {5,4,3,7,6,5,4,3,2,1,1,0}, 00151 {1,1,7,6,5,4,3,2,1,1,0}, 00152 {1,1,5,4,3,3,2,1,1,0}, 00153 {1,1,1,3,3,2,2,1,0}, 00154 {1,0,1,3,2,1,1,1}, 00155 {1,0,1,3,2,1,1}, 00156 {0,1,1,2,1,3}, 00157 {0,1,1,1,1}, 00158 {0,1,1,1}, 00159 {0,1,1}, 00160 {0,1}, 00161 }; 00162 00163 static const uint8_t chroma_dc_total_zeros_len[3][4]= { 00164 { 1, 2, 3, 3,}, 00165 { 1, 2, 2, 0,}, 00166 { 1, 1, 0, 0,}, 00167 }; 00168 00169 static const uint8_t chroma_dc_total_zeros_bits[3][4]= { 00170 { 1, 1, 1, 0,}, 00171 { 1, 1, 0, 0,}, 00172 { 1, 0, 0, 0,}, 00173 }; 00174 00175 static const uint8_t run_len[7][16]={ 00176 {1,1}, 00177 {1,2,2}, 00178 {2,2,2,2}, 00179 {2,2,2,3,3}, 00180 {2,2,3,3,3,3}, 00181 {2,3,3,3,3,3,3}, 00182 {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11}, 00183 }; 00184 00185 static const uint8_t run_bits[7][16]={ 00186 {1,0}, 00187 {1,1,0}, 00188 {3,2,1,0}, 00189 {3,2,1,1,0}, 00190 {3,2,3,2,1,0}, 00191 {3,0,1,3,2,5,4}, 00192 {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1}, 00193 }; 00194 00195 static VLC coeff_token_vlc[4]; 00196 static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2]; 00197 static const int coeff_token_vlc_tables_size[4]={520,332,280,256}; 00198 00199 static VLC chroma_dc_coeff_token_vlc; 00200 static VLC_TYPE chroma_dc_coeff_token_vlc_table[256][2]; 00201 static const int chroma_dc_coeff_token_vlc_table_size = 256; 00202 00203 static VLC total_zeros_vlc[15]; 00204 static VLC_TYPE total_zeros_vlc_tables[15][512][2]; 00205 static const int total_zeros_vlc_tables_size = 512; 00206 00207 static VLC chroma_dc_total_zeros_vlc[3]; 00208 static VLC_TYPE chroma_dc_total_zeros_vlc_tables[3][8][2]; 00209 static const int chroma_dc_total_zeros_vlc_tables_size = 8; 00210 00211 static VLC run_vlc[6]; 00212 static VLC_TYPE run_vlc_tables[6][8][2]; 00213 static const int run_vlc_tables_size = 8; 00214 00215 static VLC run7_vlc; 00216 static VLC_TYPE run7_vlc_table[96][2]; 00217 static const int run7_vlc_table_size = 96; 00218 00219 #define LEVEL_TAB_BITS 8 00220 static int8_t cavlc_level_tab[7][1<<LEVEL_TAB_BITS][2]; 00221 00222 00227 static inline int pred_non_zero_count(H264Context *h, int n){ 00228 const int index8= scan8[n]; 00229 const int left= h->non_zero_count_cache[index8 - 1]; 00230 const int top = h->non_zero_count_cache[index8 - 8]; 00231 int i= left + top; 00232 00233 if(i<64) i= (i+1)>>1; 00234 00235 tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31); 00236 00237 return i&31; 00238 } 00239 00240 static av_cold void init_cavlc_level_tab(void){ 00241 int suffix_length, mask; 00242 unsigned int i; 00243 00244 for(suffix_length=0; suffix_length<7; suffix_length++){ 00245 for(i=0; i<(1<<LEVEL_TAB_BITS); i++){ 00246 int prefix= LEVEL_TAB_BITS - av_log2(2*i); 00247 int level_code= (prefix<<suffix_length) + (i>>(LEVEL_TAB_BITS-prefix-1-suffix_length)) - (1<<suffix_length); 00248 00249 mask= -(level_code&1); 00250 level_code= (((2+level_code)>>1) ^ mask) - mask; 00251 if(prefix + 1 + suffix_length <= LEVEL_TAB_BITS){ 00252 cavlc_level_tab[suffix_length][i][0]= level_code; 00253 cavlc_level_tab[suffix_length][i][1]= prefix + 1 + suffix_length; 00254 }else if(prefix + 1 <= LEVEL_TAB_BITS){ 00255 cavlc_level_tab[suffix_length][i][0]= prefix+100; 00256 cavlc_level_tab[suffix_length][i][1]= prefix + 1; 00257 }else{ 00258 cavlc_level_tab[suffix_length][i][0]= LEVEL_TAB_BITS+100; 00259 cavlc_level_tab[suffix_length][i][1]= LEVEL_TAB_BITS; 00260 } 00261 } 00262 } 00263 } 00264 00265 av_cold void ff_h264_decode_init_vlc(void){ 00266 static int done = 0; 00267 00268 if (!done) { 00269 int i; 00270 int offset; 00271 done = 1; 00272 00273 chroma_dc_coeff_token_vlc.table = chroma_dc_coeff_token_vlc_table; 00274 chroma_dc_coeff_token_vlc.table_allocated = chroma_dc_coeff_token_vlc_table_size; 00275 init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5, 00276 &chroma_dc_coeff_token_len [0], 1, 1, 00277 &chroma_dc_coeff_token_bits[0], 1, 1, 00278 INIT_VLC_USE_NEW_STATIC); 00279 00280 offset = 0; 00281 for(i=0; i<4; i++){ 00282 coeff_token_vlc[i].table = coeff_token_vlc_tables+offset; 00283 coeff_token_vlc[i].table_allocated = coeff_token_vlc_tables_size[i]; 00284 init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17, 00285 &coeff_token_len [i][0], 1, 1, 00286 &coeff_token_bits[i][0], 1, 1, 00287 INIT_VLC_USE_NEW_STATIC); 00288 offset += coeff_token_vlc_tables_size[i]; 00289 } 00290 /* 00291 * This is a one time safety check to make sure that 00292 * the packed static coeff_token_vlc table sizes 00293 * were initialized correctly. 00294 */ 00295 assert(offset == FF_ARRAY_ELEMS(coeff_token_vlc_tables)); 00296 00297 for(i=0; i<3; i++){ 00298 chroma_dc_total_zeros_vlc[i].table = chroma_dc_total_zeros_vlc_tables[i]; 00299 chroma_dc_total_zeros_vlc[i].table_allocated = chroma_dc_total_zeros_vlc_tables_size; 00300 init_vlc(&chroma_dc_total_zeros_vlc[i], 00301 CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4, 00302 &chroma_dc_total_zeros_len [i][0], 1, 1, 00303 &chroma_dc_total_zeros_bits[i][0], 1, 1, 00304 INIT_VLC_USE_NEW_STATIC); 00305 } 00306 for(i=0; i<15; i++){ 00307 total_zeros_vlc[i].table = total_zeros_vlc_tables[i]; 00308 total_zeros_vlc[i].table_allocated = total_zeros_vlc_tables_size; 00309 init_vlc(&total_zeros_vlc[i], 00310 TOTAL_ZEROS_VLC_BITS, 16, 00311 &total_zeros_len [i][0], 1, 1, 00312 &total_zeros_bits[i][0], 1, 1, 00313 INIT_VLC_USE_NEW_STATIC); 00314 } 00315 00316 for(i=0; i<6; i++){ 00317 run_vlc[i].table = run_vlc_tables[i]; 00318 run_vlc[i].table_allocated = run_vlc_tables_size; 00319 init_vlc(&run_vlc[i], 00320 RUN_VLC_BITS, 7, 00321 &run_len [i][0], 1, 1, 00322 &run_bits[i][0], 1, 1, 00323 INIT_VLC_USE_NEW_STATIC); 00324 } 00325 run7_vlc.table = run7_vlc_table, 00326 run7_vlc.table_allocated = run7_vlc_table_size; 00327 init_vlc(&run7_vlc, RUN7_VLC_BITS, 16, 00328 &run_len [6][0], 1, 1, 00329 &run_bits[6][0], 1, 1, 00330 INIT_VLC_USE_NEW_STATIC); 00331 00332 init_cavlc_level_tab(); 00333 } 00334 } 00335 00339 static inline int get_level_prefix(GetBitContext *gb){ 00340 unsigned int buf; 00341 int log; 00342 00343 OPEN_READER(re, gb); 00344 UPDATE_CACHE(re, gb); 00345 buf=GET_CACHE(re, gb); 00346 00347 log= 32 - av_log2(buf); 00348 #ifdef TRACE 00349 print_bin(buf>>(32-log), log); 00350 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__); 00351 #endif 00352 00353 LAST_SKIP_BITS(re, gb, log); 00354 CLOSE_READER(re, gb); 00355 00356 return log-1; 00357 } 00358 00366 static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){ 00367 MpegEncContext * const s = &h->s; 00368 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}; 00369 int level[16]; 00370 int zeros_left, coeff_token, total_coeff, i, trailing_ones, run_before; 00371 00372 //FIXME put trailing_onex into the context 00373 00374 if(max_coeff <= 8){ 00375 coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1); 00376 total_coeff= coeff_token>>2; 00377 }else{ 00378 if(n >= LUMA_DC_BLOCK_INDEX){ 00379 total_coeff= pred_non_zero_count(h, (n - LUMA_DC_BLOCK_INDEX)*16); 00380 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2); 00381 total_coeff= coeff_token>>2; 00382 }else{ 00383 total_coeff= pred_non_zero_count(h, n); 00384 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2); 00385 total_coeff= coeff_token>>2; 00386 } 00387 } 00388 h->non_zero_count_cache[ scan8[n] ]= total_coeff; 00389 00390 //FIXME set last_non_zero? 00391 00392 if(total_coeff==0) 00393 return 0; 00394 if(total_coeff > (unsigned)max_coeff) { 00395 av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff); 00396 return -1; 00397 } 00398 00399 trailing_ones= coeff_token&3; 00400 tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff); 00401 assert(total_coeff<=16); 00402 00403 i = show_bits(gb, 3); 00404 skip_bits(gb, trailing_ones); 00405 level[0] = 1-((i&4)>>1); 00406 level[1] = 1-((i&2) ); 00407 level[2] = 1-((i&1)<<1); 00408 00409 if(trailing_ones<total_coeff) { 00410 int mask, prefix; 00411 int suffix_length = total_coeff > 10 & trailing_ones < 3; 00412 int bitsi= show_bits(gb, LEVEL_TAB_BITS); 00413 int level_code= cavlc_level_tab[suffix_length][bitsi][0]; 00414 00415 skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]); 00416 if(level_code >= 100){ 00417 prefix= level_code - 100; 00418 if(prefix == LEVEL_TAB_BITS) 00419 prefix += get_level_prefix(gb); 00420 00421 //first coefficient has suffix_length equal to 0 or 1 00422 if(prefix<14){ //FIXME try to build a large unified VLC table for all this 00423 if(suffix_length) 00424 level_code= (prefix<<1) + get_bits1(gb); //part 00425 else 00426 level_code= prefix; //part 00427 }else if(prefix==14){ 00428 if(suffix_length) 00429 level_code= (prefix<<1) + get_bits1(gb); //part 00430 else 00431 level_code= prefix + get_bits(gb, 4); //part 00432 }else{ 00433 level_code= 30 + get_bits(gb, prefix-3); //part 00434 if(prefix>=16){ 00435 if(prefix > 25+3){ 00436 av_log(h->s.avctx, AV_LOG_ERROR, "Invalid level prefix\n"); 00437 return -1; 00438 } 00439 level_code += (1<<(prefix-3))-4096; 00440 } 00441 } 00442 00443 if(trailing_ones < 3) level_code += 2; 00444 00445 suffix_length = 2; 00446 mask= -(level_code&1); 00447 level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask; 00448 }else{ 00449 level_code += ((level_code>>31)|1) & -(trailing_ones < 3); 00450 00451 suffix_length = 1 + (level_code + 3U > 6U); 00452 level[trailing_ones]= level_code; 00453 } 00454 00455 //remaining coefficients have suffix_length > 0 00456 for(i=trailing_ones+1;i<total_coeff;i++) { 00457 static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX }; 00458 int bitsi= show_bits(gb, LEVEL_TAB_BITS); 00459 level_code= cavlc_level_tab[suffix_length][bitsi][0]; 00460 00461 skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]); 00462 if(level_code >= 100){ 00463 prefix= level_code - 100; 00464 if(prefix == LEVEL_TAB_BITS){ 00465 prefix += get_level_prefix(gb); 00466 } 00467 if(prefix<15){ 00468 level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length); 00469 }else{ 00470 level_code = (15<<suffix_length) + get_bits(gb, prefix-3); 00471 if(prefix>=16) 00472 level_code += (1<<(prefix-3))-4096; 00473 } 00474 mask= -(level_code&1); 00475 level_code= (((2+level_code)>>1) ^ mask) - mask; 00476 } 00477 level[i]= level_code; 00478 suffix_length+= suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length]; 00479 } 00480 } 00481 00482 if(total_coeff == max_coeff) 00483 zeros_left=0; 00484 else{ 00485 /* FIXME: we don't actually support 4:2:2 yet. */ 00486 if(max_coeff <= 8) 00487 zeros_left= get_vlc2(gb, (chroma_dc_total_zeros_vlc-1)[ total_coeff ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1); 00488 else 00489 zeros_left= get_vlc2(gb, (total_zeros_vlc-1)[ total_coeff ].table, TOTAL_ZEROS_VLC_BITS, 1); 00490 } 00491 00492 #define STORE_BLOCK(type) \ 00493 scantable += zeros_left + total_coeff - 1; \ 00494 if(n >= LUMA_DC_BLOCK_INDEX){ \ 00495 ((type*)block)[*scantable] = level[0]; \ 00496 for(i=1;i<total_coeff && zeros_left > 0;i++) { \ 00497 if(zeros_left < 7) \ 00498 run_before= get_vlc2(gb, (run_vlc-1)[zeros_left].table, RUN_VLC_BITS, 1); \ 00499 else \ 00500 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \ 00501 zeros_left -= run_before; \ 00502 scantable -= 1 + run_before; \ 00503 ((type*)block)[*scantable]= level[i]; \ 00504 } \ 00505 for(;i<total_coeff;i++) { \ 00506 scantable--; \ 00507 ((type*)block)[*scantable]= level[i]; \ 00508 } \ 00509 }else{ \ 00510 ((type*)block)[*scantable] = ((int)(level[0] * qmul[*scantable] + 32))>>6; \ 00511 for(i=1;i<total_coeff && zeros_left > 0;i++) { \ 00512 if(zeros_left < 7) \ 00513 run_before= get_vlc2(gb, (run_vlc-1)[zeros_left].table, RUN_VLC_BITS, 1); \ 00514 else \ 00515 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); \ 00516 zeros_left -= run_before; \ 00517 scantable -= 1 + run_before; \ 00518 ((type*)block)[*scantable]= ((int)(level[i] * qmul[*scantable] + 32))>>6; \ 00519 } \ 00520 for(;i<total_coeff;i++) { \ 00521 scantable--; \ 00522 ((type*)block)[*scantable]= ((int)(level[i] * qmul[*scantable] + 32))>>6; \ 00523 } \ 00524 } 00525 00526 if (h->pixel_shift) { 00527 STORE_BLOCK(int32_t) 00528 } else { 00529 STORE_BLOCK(int16_t) 00530 } 00531 00532 if(zeros_left<0){ 00533 av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y); 00534 return -1; 00535 } 00536 00537 return 0; 00538 } 00539 00540 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){ 00541 int i4x4, i8x8; 00542 MpegEncContext * const s = &h->s; 00543 int qscale = p == 0 ? s->qscale : h->chroma_qp[p-1]; 00544 if(IS_INTRA16x16(mb_type)){ 00545 AV_ZERO128(h->mb_luma_dc[p]+0); 00546 AV_ZERO128(h->mb_luma_dc[p]+8); 00547 AV_ZERO128(h->mb_luma_dc[p]+16); 00548 AV_ZERO128(h->mb_luma_dc[p]+24); 00549 if( decode_residual(h, h->intra_gb_ptr, h->mb_luma_dc[p], LUMA_DC_BLOCK_INDEX+p, scan, NULL, 16) < 0){ 00550 return -1; //FIXME continue if partitioned and other return -1 too 00551 } 00552 00553 assert((cbp&15) == 0 || (cbp&15) == 15); 00554 00555 if(cbp&15){ 00556 for(i8x8=0; i8x8<4; i8x8++){ 00557 for(i4x4=0; i4x4<4; i4x4++){ 00558 const int index= i4x4 + 4*i8x8 + p*16; 00559 if( decode_residual(h, h->intra_gb_ptr, h->mb + (16*index << pixel_shift), 00560 index, scan + 1, h->dequant4_coeff[p][qscale], 15) < 0 ){ 00561 return -1; 00562 } 00563 } 00564 } 00565 return 0xf; 00566 }else{ 00567 fill_rectangle(&h->non_zero_count_cache[scan8[p*16]], 4, 4, 8, 0, 1); 00568 return 0; 00569 } 00570 }else{ 00571 int cqm = (IS_INTRA( mb_type ) ? 0:3)+p; 00572 /* For CAVLC 4:4:4, we need to keep track of the luma 8x8 CBP for deblocking nnz purposes. */ 00573 int new_cbp = 0; 00574 for(i8x8=0; i8x8<4; i8x8++){ 00575 if(cbp & (1<<i8x8)){ 00576 if(IS_8x8DCT(mb_type)){ 00577 DCTELEM *buf = &h->mb[64*i8x8+256*p << pixel_shift]; 00578 uint8_t *nnz; 00579 for(i4x4=0; i4x4<4; i4x4++){ 00580 const int index= i4x4 + 4*i8x8 + p*16; 00581 if( decode_residual(h, gb, buf, index, scan8x8+16*i4x4, 00582 h->dequant8_coeff[cqm][qscale], 16) < 0 ) 00583 return -1; 00584 } 00585 nnz= &h->non_zero_count_cache[ scan8[4*i8x8+p*16] ]; 00586 nnz[0] += nnz[1] + nnz[8] + nnz[9]; 00587 new_cbp |= !!nnz[0] << i8x8; 00588 }else{ 00589 for(i4x4=0; i4x4<4; i4x4++){ 00590 const int index= i4x4 + 4*i8x8 + p*16; 00591 if( decode_residual(h, gb, h->mb + (16*index << pixel_shift), index, 00592 scan, h->dequant4_coeff[cqm][qscale], 16) < 0 ){ 00593 return -1; 00594 } 00595 new_cbp |= h->non_zero_count_cache[ scan8[index] ] << i8x8; 00596 } 00597 } 00598 }else{ 00599 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8+p*16] ]; 00600 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0; 00601 } 00602 } 00603 return new_cbp; 00604 } 00605 } 00606 00607 int ff_h264_decode_mb_cavlc(H264Context *h){ 00608 MpegEncContext * const s = &h->s; 00609 int mb_xy; 00610 int partition_count; 00611 unsigned int mb_type, cbp; 00612 int dct8x8_allowed= h->pps.transform_8x8_mode; 00613 int decode_chroma = h->sps.chroma_format_idc == 1 || h->sps.chroma_format_idc == 2; 00614 const int pixel_shift = h->pixel_shift; 00615 00616 mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride; 00617 00618 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y); 00619 cbp = 0; /* avoid warning. FIXME: find a solution without slowing 00620 down the code */ 00621 if(h->slice_type_nos != AV_PICTURE_TYPE_I){ 00622 if(s->mb_skip_run==-1) 00623 s->mb_skip_run= get_ue_golomb(&s->gb); 00624 00625 if (s->mb_skip_run--) { 00626 if(FRAME_MBAFF && (s->mb_y&1) == 0){ 00627 if(s->mb_skip_run==0) 00628 h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb); 00629 } 00630 decode_mb_skip(h); 00631 return 0; 00632 } 00633 } 00634 if(FRAME_MBAFF){ 00635 if( (s->mb_y&1) == 0 ) 00636 h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb); 00637 } 00638 00639 h->prev_mb_skipped= 0; 00640 00641 mb_type= get_ue_golomb(&s->gb); 00642 if(h->slice_type_nos == AV_PICTURE_TYPE_B){ 00643 if(mb_type < 23){ 00644 partition_count= b_mb_type_info[mb_type].partition_count; 00645 mb_type= b_mb_type_info[mb_type].type; 00646 }else{ 00647 mb_type -= 23; 00648 goto decode_intra_mb; 00649 } 00650 }else if(h->slice_type_nos == AV_PICTURE_TYPE_P){ 00651 if(mb_type < 5){ 00652 partition_count= p_mb_type_info[mb_type].partition_count; 00653 mb_type= p_mb_type_info[mb_type].type; 00654 }else{ 00655 mb_type -= 5; 00656 goto decode_intra_mb; 00657 } 00658 }else{ 00659 assert(h->slice_type_nos == AV_PICTURE_TYPE_I); 00660 if(h->slice_type == AV_PICTURE_TYPE_SI && mb_type) 00661 mb_type--; 00662 decode_intra_mb: 00663 if(mb_type > 25){ 00664 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); 00665 return -1; 00666 } 00667 partition_count=0; 00668 cbp= i_mb_type_info[mb_type].cbp; 00669 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode; 00670 mb_type= i_mb_type_info[mb_type].type; 00671 } 00672 00673 if(MB_FIELD) 00674 mb_type |= MB_TYPE_INTERLACED; 00675 00676 h->slice_table[ mb_xy ]= h->slice_num; 00677 00678 if(IS_INTRA_PCM(mb_type)){ 00679 unsigned int x; 00680 static const uint16_t mb_sizes[4] = {256,384,512,768}; 00681 const int mb_size = mb_sizes[h->sps.chroma_format_idc]*h->sps.bit_depth_luma >> 3; 00682 00683 // We assume these blocks are very rare so we do not optimize it. 00684 align_get_bits(&s->gb); 00685 00686 // The pixels are stored in the same order as levels in h->mb array. 00687 for(x=0; x < mb_size; x++){ 00688 ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8); 00689 } 00690 00691 // In deblocking, the quantizer is 0 00692 s->current_picture.qscale_table[mb_xy]= 0; 00693 // All coeffs are present 00694 memset(h->non_zero_count[mb_xy], 16, 48); 00695 00696 s->current_picture.mb_type[mb_xy]= mb_type; 00697 return 0; 00698 } 00699 00700 if(MB_MBAFF){ 00701 h->ref_count[0] <<= 1; 00702 h->ref_count[1] <<= 1; 00703 } 00704 00705 fill_decode_neighbors(h, mb_type); 00706 fill_decode_caches(h, mb_type); 00707 00708 //mb_pred 00709 if(IS_INTRA(mb_type)){ 00710 int pred_mode; 00711 // init_top_left_availability(h); 00712 if(IS_INTRA4x4(mb_type)){ 00713 int i; 00714 int di = 1; 00715 if(dct8x8_allowed && get_bits1(&s->gb)){ 00716 mb_type |= MB_TYPE_8x8DCT; 00717 di = 4; 00718 } 00719 00720 // fill_intra4x4_pred_table(h); 00721 for(i=0; i<16; i+=di){ 00722 int mode= pred_intra_mode(h, i); 00723 00724 if(!get_bits1(&s->gb)){ 00725 const int rem_mode= get_bits(&s->gb, 3); 00726 mode = rem_mode + (rem_mode >= mode); 00727 } 00728 00729 if(di==4) 00730 fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 ); 00731 else 00732 h->intra4x4_pred_mode_cache[ scan8[i] ] = mode; 00733 } 00734 ff_h264_write_back_intra_pred_mode(h); 00735 if( ff_h264_check_intra4x4_pred_mode(h) < 0) 00736 return -1; 00737 }else{ 00738 h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode(h, h->intra16x16_pred_mode); 00739 if(h->intra16x16_pred_mode < 0) 00740 return -1; 00741 } 00742 if(decode_chroma){ 00743 pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb)); 00744 if(pred_mode < 0) 00745 return -1; 00746 h->chroma_pred_mode= pred_mode; 00747 } else { 00748 h->chroma_pred_mode = DC_128_PRED8x8; 00749 } 00750 }else if(partition_count==4){ 00751 int i, j, sub_partition_count[4], list, ref[2][4]; 00752 00753 if(h->slice_type_nos == AV_PICTURE_TYPE_B){ 00754 for(i=0; i<4; i++){ 00755 h->sub_mb_type[i]= get_ue_golomb_31(&s->gb); 00756 if(h->sub_mb_type[i] >=13){ 00757 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); 00758 return -1; 00759 } 00760 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; 00761 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type; 00762 } 00763 if( IS_DIRECT(h->sub_mb_type[0]|h->sub_mb_type[1]|h->sub_mb_type[2]|h->sub_mb_type[3])) { 00764 ff_h264_pred_direct_motion(h, &mb_type); 00765 h->ref_cache[0][scan8[4]] = 00766 h->ref_cache[1][scan8[4]] = 00767 h->ref_cache[0][scan8[12]] = 00768 h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE; 00769 } 00770 }else{ 00771 assert(h->slice_type_nos == AV_PICTURE_TYPE_P); //FIXME SP correct ? 00772 for(i=0; i<4; i++){ 00773 h->sub_mb_type[i]= get_ue_golomb_31(&s->gb); 00774 if(h->sub_mb_type[i] >=4){ 00775 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); 00776 return -1; 00777 } 00778 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; 00779 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type; 00780 } 00781 } 00782 00783 for(list=0; list<h->list_count; list++){ 00784 int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list]; 00785 for(i=0; i<4; i++){ 00786 if(IS_DIRECT(h->sub_mb_type[i])) continue; 00787 if(IS_DIR(h->sub_mb_type[i], 0, list)){ 00788 unsigned int tmp; 00789 if(ref_count == 1){ 00790 tmp= 0; 00791 }else if(ref_count == 2){ 00792 tmp= get_bits1(&s->gb)^1; 00793 }else{ 00794 tmp= get_ue_golomb_31(&s->gb); 00795 if(tmp>=ref_count){ 00796 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp); 00797 return -1; 00798 } 00799 } 00800 ref[list][i]= tmp; 00801 }else{ 00802 //FIXME 00803 ref[list][i] = -1; 00804 } 00805 } 00806 } 00807 00808 if(dct8x8_allowed) 00809 dct8x8_allowed = get_dct8x8_allowed(h); 00810 00811 for(list=0; list<h->list_count; list++){ 00812 for(i=0; i<4; i++){ 00813 if(IS_DIRECT(h->sub_mb_type[i])) { 00814 h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ]; 00815 continue; 00816 } 00817 h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]= 00818 h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i]; 00819 00820 if(IS_DIR(h->sub_mb_type[i], 0, list)){ 00821 const int sub_mb_type= h->sub_mb_type[i]; 00822 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1; 00823 for(j=0; j<sub_partition_count[i]; j++){ 00824 int mx, my; 00825 const int index= 4*i + block_width*j; 00826 int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ]; 00827 pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my); 00828 mx += get_se_golomb(&s->gb); 00829 my += get_se_golomb(&s->gb); 00830 tprintf(s->avctx, "final mv:%d %d\n", mx, my); 00831 00832 if(IS_SUB_8X8(sub_mb_type)){ 00833 mv_cache[ 1 ][0]= 00834 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx; 00835 mv_cache[ 1 ][1]= 00836 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my; 00837 }else if(IS_SUB_8X4(sub_mb_type)){ 00838 mv_cache[ 1 ][0]= mx; 00839 mv_cache[ 1 ][1]= my; 00840 }else if(IS_SUB_4X8(sub_mb_type)){ 00841 mv_cache[ 8 ][0]= mx; 00842 mv_cache[ 8 ][1]= my; 00843 } 00844 mv_cache[ 0 ][0]= mx; 00845 mv_cache[ 0 ][1]= my; 00846 } 00847 }else{ 00848 uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0]; 00849 p[0] = p[1]= 00850 p[8] = p[9]= 0; 00851 } 00852 } 00853 } 00854 }else if(IS_DIRECT(mb_type)){ 00855 ff_h264_pred_direct_motion(h, &mb_type); 00856 dct8x8_allowed &= h->sps.direct_8x8_inference_flag; 00857 }else{ 00858 int list, mx, my, i; 00859 //FIXME we should set ref_idx_l? to 0 if we use that later ... 00860 if(IS_16X16(mb_type)){ 00861 for(list=0; list<h->list_count; list++){ 00862 unsigned int val; 00863 if(IS_DIR(mb_type, 0, list)){ 00864 if(h->ref_count[list]==1){ 00865 val= 0; 00866 }else if(h->ref_count[list]==2){ 00867 val= get_bits1(&s->gb)^1; 00868 }else{ 00869 val= get_ue_golomb_31(&s->gb); 00870 if(val >= h->ref_count[list]){ 00871 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val); 00872 return -1; 00873 } 00874 } 00875 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1); 00876 } 00877 } 00878 for(list=0; list<h->list_count; list++){ 00879 if(IS_DIR(mb_type, 0, list)){ 00880 pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my); 00881 mx += get_se_golomb(&s->gb); 00882 my += get_se_golomb(&s->gb); 00883 tprintf(s->avctx, "final mv:%d %d\n", mx, my); 00884 00885 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4); 00886 } 00887 } 00888 } 00889 else if(IS_16X8(mb_type)){ 00890 for(list=0; list<h->list_count; list++){ 00891 for(i=0; i<2; i++){ 00892 unsigned int val; 00893 if(IS_DIR(mb_type, i, list)){ 00894 if(h->ref_count[list] == 1){ 00895 val= 0; 00896 }else if(h->ref_count[list] == 2){ 00897 val= get_bits1(&s->gb)^1; 00898 }else{ 00899 val= get_ue_golomb_31(&s->gb); 00900 if(val >= h->ref_count[list]){ 00901 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val); 00902 return -1; 00903 } 00904 } 00905 }else 00906 val= LIST_NOT_USED&0xFF; 00907 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1); 00908 } 00909 } 00910 for(list=0; list<h->list_count; list++){ 00911 for(i=0; i<2; i++){ 00912 unsigned int val; 00913 if(IS_DIR(mb_type, i, list)){ 00914 pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my); 00915 mx += get_se_golomb(&s->gb); 00916 my += get_se_golomb(&s->gb); 00917 tprintf(s->avctx, "final mv:%d %d\n", mx, my); 00918 00919 val= pack16to32(mx,my); 00920 }else 00921 val=0; 00922 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4); 00923 } 00924 } 00925 }else{ 00926 assert(IS_8X16(mb_type)); 00927 for(list=0; list<h->list_count; list++){ 00928 for(i=0; i<2; i++){ 00929 unsigned int val; 00930 if(IS_DIR(mb_type, i, list)){ //FIXME optimize 00931 if(h->ref_count[list]==1){ 00932 val= 0; 00933 }else if(h->ref_count[list]==2){ 00934 val= get_bits1(&s->gb)^1; 00935 }else{ 00936 val= get_ue_golomb_31(&s->gb); 00937 if(val >= h->ref_count[list]){ 00938 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val); 00939 return -1; 00940 } 00941 } 00942 }else 00943 val= LIST_NOT_USED&0xFF; 00944 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1); 00945 } 00946 } 00947 for(list=0; list<h->list_count; list++){ 00948 for(i=0; i<2; i++){ 00949 unsigned int val; 00950 if(IS_DIR(mb_type, i, list)){ 00951 pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my); 00952 mx += get_se_golomb(&s->gb); 00953 my += get_se_golomb(&s->gb); 00954 tprintf(s->avctx, "final mv:%d %d\n", mx, my); 00955 00956 val= pack16to32(mx,my); 00957 }else 00958 val=0; 00959 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4); 00960 } 00961 } 00962 } 00963 } 00964 00965 if(IS_INTER(mb_type)) 00966 write_back_motion(h, mb_type); 00967 00968 if(!IS_INTRA16x16(mb_type)){ 00969 cbp= get_ue_golomb(&s->gb); 00970 00971 if(decode_chroma){ 00972 if(cbp > 47){ 00973 av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y); 00974 return -1; 00975 } 00976 if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp]; 00977 else cbp= golomb_to_inter_cbp [cbp]; 00978 }else{ 00979 if(cbp > 15){ 00980 av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y); 00981 return -1; 00982 } 00983 if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp]; 00984 else cbp= golomb_to_inter_cbp_gray[cbp]; 00985 } 00986 } 00987 00988 if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){ 00989 mb_type |= MB_TYPE_8x8DCT*get_bits1(&s->gb); 00990 } 00991 h->cbp= 00992 h->cbp_table[mb_xy]= cbp; 00993 s->current_picture.mb_type[mb_xy]= mb_type; 00994 00995 if(cbp || IS_INTRA16x16(mb_type)){ 00996 int i4x4, chroma_idx; 00997 int dquant; 00998 int ret; 00999 GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr; 01000 const uint8_t *scan, *scan8x8; 01001 const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8); 01002 01003 if(IS_INTERLACED(mb_type)){ 01004 scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0; 01005 scan= s->qscale ? h->field_scan : h->field_scan_q0; 01006 }else{ 01007 scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0; 01008 scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0; 01009 } 01010 01011 dquant= get_se_golomb(&s->gb); 01012 01013 s->qscale += dquant; 01014 01015 if(((unsigned)s->qscale) > max_qp){ 01016 if(s->qscale<0) s->qscale+= max_qp+1; 01017 else s->qscale-= max_qp+1; 01018 if(((unsigned)s->qscale) > max_qp){ 01019 av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y); 01020 return -1; 01021 } 01022 } 01023 01024 h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale); 01025 h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale); 01026 01027 if( (ret = decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 0)) < 0 ){ 01028 return -1; 01029 } 01030 h->cbp_table[mb_xy] |= ret << 12; 01031 if(CHROMA444){ 01032 if( decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 1) < 0 ){ 01033 return -1; 01034 } 01035 if( decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 2) < 0 ){ 01036 return -1; 01037 } 01038 } else { 01039 if(cbp&0x30){ 01040 for(chroma_idx=0; chroma_idx<2; chroma_idx++) 01041 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){ 01042 return -1; 01043 } 01044 } 01045 01046 if(cbp&0x20){ 01047 for(chroma_idx=0; chroma_idx<2; chroma_idx++){ 01048 const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]]; 01049 for(i4x4=0; i4x4<4; i4x4++){ 01050 const int index= 16 + 16*chroma_idx + i4x4; 01051 if( decode_residual(h, gb, h->mb + (16*index << pixel_shift), index, scan + 1, qmul, 15) < 0){ 01052 return -1; 01053 } 01054 } 01055 } 01056 }else{ 01057 fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1); 01058 fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1); 01059 } 01060 } 01061 }else{ 01062 fill_rectangle(&h->non_zero_count_cache[scan8[ 0]], 4, 4, 8, 0, 1); 01063 fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1); 01064 fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1); 01065 } 01066 s->current_picture.qscale_table[mb_xy]= s->qscale; 01067 write_back_non_zero_count(h); 01068 01069 if(MB_MBAFF){ 01070 h->ref_count[0] >>= 1; 01071 h->ref_count[1] >>= 1; 01072 } 01073 01074 return 0; 01075 } 01076