X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fh264_cabac.c;h=485837879d39f0f4bc249c885e2d91f2c03397ec;hb=36d6b545a1a5309b3d9223b0db40ad2879817af5;hp=c0a1332a4d29faf14a27a31adc68c7d2fece0bf6;hpb=2dc380ca8ea9d161c337a47a111f83d2e84ceb9c;p=ffmpeg diff --git a/libavcodec/h264_cabac.c b/libavcodec/h264_cabac.c index c0a1332a4d2..485837879d3 100644 --- a/libavcodec/h264_cabac.c +++ b/libavcodec/h264_cabac.c @@ -20,7 +20,7 @@ */ /** - * @file libavcodec/h264_cabac.c + * @file * H.264 / AVC / MPEG4 part10 cabac decoding. * @author Michael Niedermayer */ @@ -688,30 +688,30 @@ static const int8_t cabac_context_init_PB[3][460][2] = void ff_h264_init_cabac_states(H264Context *h) { MpegEncContext * const s = &h->s; int i; + const int8_t (*tab)[2]; + + if( h->slice_type_nos == FF_I_TYPE ) tab = cabac_context_init_I; + else tab = cabac_context_init_PB[h->cabac_init_idc]; /* calculate pre-state */ for( i= 0; i < 460; i++ ) { - int pre; - if( h->slice_type_nos == FF_I_TYPE ) - pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 ); - else - pre = av_clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 ); + int pre = 2*(((tab[i][0] * s->qscale) >>4 ) + tab[i][1]) - 127; - if( pre <= 63 ) - h->cabac_state[i] = 2 * ( 63 - pre ) + 0; - else - h->cabac_state[i] = 2 * ( pre - 64 ) + 1; + pre^= pre>>31; + if(pre > 124) + pre= 124 + (pre&1); + + h->cabac_state[i] = pre; } } static int decode_cabac_field_decoding_flag(H264Context *h) { MpegEncContext * const s = &h->s; - const long mba_xy = h->mb_xy - 1L; const long mbb_xy = h->mb_xy - 2L*s->mb_stride; unsigned long ctx = 0; - ctx += (s->current_picture.mb_type[mba_xy]>>7)&(h->slice_table[mba_xy] == h->slice_num); + ctx += h->mb_field_decoding_flag & !!s->mb_x; //for FMO:(s->current_picture.mb_type[mba_xy]>>7)&(h->slice_table[mba_xy] == h->slice_num); ctx += (s->current_picture.mb_type[mbb_xy]>>7)&(h->slice_table[mbb_xy] == h->slice_num); return get_cabac_noinline( &h->cabac, &(h->cabac_state+70)[ctx] ); @@ -722,13 +722,10 @@ static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_sl int mb_type; if(intra_slice){ - MpegEncContext * const s = &h->s; - const int mba_xy = h->left_mb_xy[0]; - const int mbb_xy = h->top_mb_xy; int ctx=0; - if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) ) + if( h->left_type[0] & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)) ctx++; - if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) ) + if( h->top_type & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)) ctx++; if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 ) return 0; /* I4x4 */ @@ -750,44 +747,6 @@ static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_sl return mb_type; } -static int decode_cabac_mb_type_b( H264Context *h ) { - MpegEncContext * const s = &h->s; - - const int mba_xy = h->left_mb_xy[0]; - const int mbb_xy = h->top_mb_xy; - int ctx = 0; - int bits; - assert(h->slice_type_nos == FF_B_TYPE); - - if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) ) - ctx++; - if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) ) - ctx++; - - if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) ) - return 0; /* B_Direct_16x16 */ - - if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) { - return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */ - } - - bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3; - bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2; - bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1; - bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); - if( bits < 8 ) - return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */ - else if( bits == 13 ) { - return decode_cabac_intra_mb_type(h, 32, 0) + 23; - } else if( bits == 14 ) - return 11; /* B_L1_L0_8x16 */ - else if( bits == 15 ) - return 22; /* B_8x8 */ - - bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); - return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */ -} - static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) { MpegEncContext * const s = &h->s; int mba_xy, mbb_xy; @@ -834,10 +793,7 @@ static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) { mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] ); mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] ); - if( mode >= pred_mode ) - return mode + 1; - else - return mode; + return mode + ( mode >= pred_mode ); } static int decode_cabac_mb_chroma_pre_mode( H264Context *h) { @@ -847,10 +803,10 @@ static int decode_cabac_mb_chroma_pre_mode( H264Context *h) { int ctx = 0; /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */ - if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 ) + if( h->left_type[0] && h->chroma_pred_mode_table[mba_xy] != 0 ) ctx++; - if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 ) + if( h->top_type && h->chroma_pred_mode_table[mbb_xy] != 0 ) ctx++; if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 ) @@ -871,13 +827,13 @@ static int decode_cabac_mb_cbp_luma( H264Context *h) { cbp_b = h->top_cbp; ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04); - cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]); + cbp += get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]); ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08); - cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1; + cbp += get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1; ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01); - cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2; + cbp += get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2; ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02); - cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3; + cbp += get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3; return cbp; } static int decode_cabac_mb_cbp_chroma( H264Context *h) { @@ -898,22 +854,7 @@ static int decode_cabac_mb_cbp_chroma( H264Context *h) { if( cbp_b == 2 ) ctx += 2; return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ); } -static int decode_cabac_mb_dqp( H264Context *h) { - int ctx= h->last_qscale_diff != 0; - int val = 0; - - while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) { - ctx= 2+(ctx>>1); - val++; - if(val > 102) //prevent infinite loop - return INT_MIN; - } - if( val&0x01 ) - return (val + 1)>>1 ; - else - return -((val + 1)>>1); -} static int decode_cabac_p_mb_sub_type( H264Context *h ) { if( get_cabac( &h->cabac, &h->cabac_state[21] ) ) return 0; /* 8x8 */ @@ -940,10 +881,6 @@ static int decode_cabac_b_mb_sub_type( H264Context *h ) { return type; } -static inline int decode_cabac_mb_transform_size( H264Context *h ) { - return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] ); -} - static int decode_cabac_mb_ref( H264Context *h, int list, int n ) { int refa = h->ref_cache[list][scan8[n] - 1]; int refb = h->ref_cache[list][scan8[n] - 8]; @@ -972,22 +909,21 @@ static int decode_cabac_mb_ref( H264Context *h, int list, int n ) { return ref; } -static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) { - int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) + - abs( h->mvd_cache[list][scan8[n] - 8][l] ); - int ctxbase = (l == 0) ? 40 : 47; +static int decode_cabac_mb_mvd( H264Context *h, int ctxbase, int amvd, int *mvda) { int mvd; - int ctx = (amvd>2) + (amvd>32); - if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx])) + if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+((amvd-3)>>(INT_BIT-1))+((amvd-33)>>(INT_BIT-1))+2])){ +// if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+(amvd>2)+(amvd>32)])){ + *mvda= 0; return 0; + } mvd= 1; - ctx= 3; - while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) { + ctxbase+= 3; + while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase] ) ) { + if( mvd < 4 ) + ctxbase++; mvd++; - if( ctx < 6 ) - ctx++; } if( mvd >= 9 ) { @@ -1001,13 +937,25 @@ static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) { } } while( k-- ) { - if( get_cabac_bypass( &h->cabac ) ) - mvd += 1 << k; + mvd += get_cabac_bypass( &h->cabac )<cabac, -mvd ); } +#define DECODE_CABAC_MB_MVD( h, list, n )\ +{\ + int amvd0 = h->mvd_cache[list][scan8[n] - 1][0] +\ + h->mvd_cache[list][scan8[n] - 8][0];\ + int amvd1 = h->mvd_cache[list][scan8[n] - 1][1] +\ + h->mvd_cache[list][scan8[n] - 8][1];\ +\ + mx += decode_cabac_mb_mvd( h, 40, amvd0, &mpx );\ + my += decode_cabac_mb_mvd( h, 47, amvd1, &mpy );\ +} + static av_always_inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx, int is_dc ) { int nza, nzb; int ctx = 0; @@ -1223,54 +1171,17 @@ static av_always_inline void decode_cabac_residual_internal( H264Context *h, DCT } -#if !CONFIG_SMALL -static void decode_cabac_residual_dc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) { - decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 1); +static void decode_cabac_residual_dc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int max_coeff ) { + decode_cabac_residual_internal(h, block, cat, n, scantable, NULL, max_coeff, 1); } static void decode_cabac_residual_nondc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) { decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0); } -#endif - -static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) { -#if CONFIG_SMALL - decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, cat == 0 || cat == 3); -#else - if( cat == 0 || cat == 3 ) decode_cabac_residual_dc(h, block, cat, n, scantable, qmul, max_coeff); - else decode_cabac_residual_nondc(h, block, cat, n, scantable, qmul, max_coeff); -#endif -} - -static inline void compute_mb_neighbors(H264Context *h) -{ - MpegEncContext * const s = &h->s; - const int mb_xy = h->mb_xy; - h->top_mb_xy = mb_xy - s->mb_stride; - h->left_mb_xy[0] = mb_xy - 1; - if(FRAME_MBAFF){ - const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride; - const int top_pair_xy = pair_xy - s->mb_stride; - const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]); - const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]); - const int curr_mb_field_flag = MB_FIELD; - const int bottom = (s->mb_y & 1); - - if (curr_mb_field_flag && (bottom || top_mb_field_flag)){ - h->top_mb_xy -= s->mb_stride; - } - if (!left_mb_field_flag == curr_mb_field_flag) { - h->left_mb_xy[0] = pair_xy - 1; - } - } else if (FIELD_PICTURE) { - h->top_mb_xy -= s->mb_stride; - } - return; -} /** * decodes a macroblock - * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed + * @return 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed */ int ff_h264_decode_mb_cabac(H264Context *h) { MpegEncContext * const s = &h->s; @@ -1284,8 +1195,6 @@ int ff_h264_decode_mb_cabac(H264Context *h) { if( h->slice_type_nos != FF_I_TYPE ) { int skip; /* a skipped mb needs the aff flag from the following mb */ - if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 ) - predict_field_decoding_flag(h); if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped ) skip = h->next_mb_skipped; else @@ -1317,17 +1226,43 @@ int ff_h264_decode_mb_cabac(H264Context *h) { h->prev_mb_skipped = 0; - compute_mb_neighbors(h); + fill_decode_neighbors(h, -(MB_FIELD)); if( h->slice_type_nos == FF_B_TYPE ) { - mb_type = decode_cabac_mb_type_b( h ); - if( mb_type < 23 ){ - partition_count= b_mb_type_info[mb_type].partition_count; - mb_type= b_mb_type_info[mb_type].type; + int ctx = 0; + assert(h->slice_type_nos == FF_B_TYPE); + + if( !IS_DIRECT( h->left_type[0]-1 ) ) + ctx++; + if( !IS_DIRECT( h->top_type-1 ) ) + ctx++; + + if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) ){ + mb_type= 0; /* B_Direct_16x16 */ + }else if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) { + mb_type= 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */ }else{ - mb_type -= 23; - goto decode_intra_mb; + int bits; + bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3; + bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2; + bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1; + bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); + if( bits < 8 ){ + mb_type= bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */ + }else if( bits == 13 ){ + mb_type= decode_cabac_intra_mb_type(h, 32, 0); + goto decode_intra_mb; + }else if( bits == 14 ){ + mb_type= 11; /* B_L1_L0_8x16 */ + }else if( bits == 15 ){ + mb_type= 22; /* B_8x8 */ + }else{ + bits= ( bits<<1 ) + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); + mb_type= bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */ + } } + partition_count= b_mb_type_info[mb_type].partition_count; + mb_type= b_mb_type_info[mb_type].type; } else if( h->slice_type_nos == FF_P_TYPE ) { if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) { /* P-type */ @@ -1402,7 +1337,7 @@ decode_intra_mb: if( IS_INTRA( mb_type ) ) { int i, pred_mode; if( IS_INTRA4x4( mb_type ) ) { - if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) { + if( dct8x8_allowed && get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] ) ) { mb_type |= MB_TYPE_8x8DCT; for( i = 0; i < 16; i+=4 ) { int pred = pred_intra_mode( h, i ); @@ -1447,11 +1382,8 @@ decode_intra_mb: h->ref_cache[1][scan8[4]] = h->ref_cache[0][scan8[12]] = h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE; - if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) { for( i = 0; i < 4; i++ ) - if( IS_DIRECT(h->sub_mb_type[i]) ) - fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, MB_TYPE_DIRECT2>>1, 1 ); - } + fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, (h->sub_mb_type[i]>>1)&0xFF, 1 ); } } else { for( i = 0; i < 4; i++ ) { @@ -1488,7 +1420,7 @@ decode_intra_mb: for(i=0; i<4; i++){ h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]; if(IS_DIRECT(h->sub_mb_type[i])){ - fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4); + fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 2); continue; } @@ -1500,11 +1432,9 @@ decode_intra_mb: int mx, my; const int index= 4*i + block_width*j; int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ]; - int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ]; - pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy); - - mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 ); - my = mpy + decode_cabac_mb_mvd( h, list, index, 1 ); + uint8_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ]; + pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my); + DECODE_CABAC_MB_MVD( h, list, index) tprintf(s->avctx, "final mv:%d %d\n", mx, my); if(IS_SUB_8X8(sub_mb_type)){ @@ -1514,43 +1444,41 @@ decode_intra_mb: mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my; mvd_cache[ 1 ][0]= - mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx; + mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mpx; mvd_cache[ 1 ][1]= - mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy; + mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= mpy; }else if(IS_SUB_8X4(sub_mb_type)){ mv_cache[ 1 ][0]= mx; mv_cache[ 1 ][1]= my; - mvd_cache[ 1 ][0]= mx - mpx; - mvd_cache[ 1 ][1]= my - mpy; + mvd_cache[ 1 ][0]= mpx; + mvd_cache[ 1 ][1]= mpy; }else if(IS_SUB_4X8(sub_mb_type)){ mv_cache[ 8 ][0]= mx; mv_cache[ 8 ][1]= my; - mvd_cache[ 8 ][0]= mx - mpx; - mvd_cache[ 8 ][1]= my - mpy; + mvd_cache[ 8 ][0]= mpx; + mvd_cache[ 8 ][1]= mpy; } mv_cache[ 0 ][0]= mx; mv_cache[ 0 ][1]= my; - mvd_cache[ 0 ][0]= mx - mpx; - mvd_cache[ 0 ][1]= my - mpy; + mvd_cache[ 0 ][0]= mpx; + mvd_cache[ 0 ][1]= mpy; } }else{ - uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0]; - uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0]; - p[0] = p[1] = p[8] = p[9] = 0; - pd[0]= pd[1]= pd[8]= pd[9]= 0; + fill_rectangle(h->mv_cache [list][ scan8[4*i] ], 2, 2, 8, 0, 4); + fill_rectangle(h->mvd_cache[list][ scan8[4*i] ], 2, 2, 8, 0, 2); } } } } else if( IS_DIRECT(mb_type) ) { ff_h264_pred_direct_motion(h, &mb_type); - fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4); - fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4); + fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 2); + fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 2); dct8x8_allowed &= h->sps.direct_8x8_inference_flag; } else { - int list, mx, my, i, mpx, mpy; + int list, i; if(IS_16X16(mb_type)){ for(list=0; listlist_count; list++){ if(IS_DIR(mb_type, 0, list)){ @@ -1564,21 +1492,18 @@ decode_intra_mb: }else ref=0; fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1); - }else - fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1); //FIXME factorize and the other fill_rect below too + } } for(list=0; listlist_count; list++){ if(IS_DIR(mb_type, 0, list)){ - pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy); - - mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 ); - my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 ); + int mx,my,mpx,mpy; + pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my); + DECODE_CABAC_MB_MVD( h, list, 0) tprintf(s->avctx, "final mv:%d %d\n", mx, my); - fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4); + fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack8to16(mpx,mpy), 2); fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4); - }else - fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4); + } } } else if(IS_16X8(mb_type)){ @@ -1602,15 +1527,15 @@ decode_intra_mb: for(list=0; listlist_count; list++){ for(i=0; i<2; i++){ if(IS_DIR(mb_type, i, list)){ - pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy); - mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 ); - my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 ); + int mx,my,mpx,mpy; + pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my); + DECODE_CABAC_MB_MVD( h, list, 8*i) tprintf(s->avctx, "final mv:%d %d\n", mx, my); - fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4); + fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack8to16(mpx,mpy), 2); fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4); }else{ - fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4); + fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 2); fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4); } } @@ -1637,15 +1562,15 @@ decode_intra_mb: for(list=0; listlist_count; list++){ for(i=0; i<2; i++){ if(IS_DIR(mb_type, i, list)){ - pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy); - mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 ); - my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 ); + int mx,my,mpx,mpy; + pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my); + DECODE_CABAC_MB_MVD( h, list, 4*i) tprintf(s->avctx, "final mv:%d %d\n", mx, my); - fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4); + fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack8to16(mpx,mpy), 2); fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4); }else{ - fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4); + fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 2); fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4); } } @@ -1667,15 +1592,13 @@ decode_intra_mb: h->cbp_table[mb_xy] = h->cbp = cbp; if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) { - if( decode_cabac_mb_transform_size( h ) ) - mb_type |= MB_TYPE_8x8DCT; + mb_type |= MB_TYPE_8x8DCT * get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] ); } s->current_picture.mb_type[mb_xy]= mb_type; if( cbp || IS_INTRA16x16( mb_type ) ) { const uint8_t *scan, *scan8x8, *dc_scan; const uint32_t *qmul; - int dqp; if(IS_INTERLACED(mb_type)){ scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0; @@ -1687,29 +1610,45 @@ decode_intra_mb: dc_scan= luma_dc_zigzag_scan; } - h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h ); - if( dqp == INT_MIN ){ - av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y); - return -1; - } - s->qscale += dqp; - if(((unsigned)s->qscale) > 51){ - if(s->qscale<0) s->qscale+= 52; - else s->qscale-= 52; - } - h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale); - h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale); + // decode_cabac_mb_dqp + if(get_cabac_noinline( &h->cabac, &h->cabac_state[60 + (h->last_qscale_diff != 0)])){ + int val = 1; + int ctx= 2; + + while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) { + ctx= 3; + val++; + if(val > 102){ //prevent infinite loop + av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y); + return -1; + } + } + + if( val&0x01 ) + val= (val + 1)>>1 ; + else + val= -((val + 1)>>1); + h->last_qscale_diff = val; + s->qscale += val; + if(((unsigned)s->qscale) > 51){ + if(s->qscale<0) s->qscale+= 52; + else s->qscale-= 52; + } + h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale); + h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale); + }else + h->last_qscale_diff=0; if( IS_INTRA16x16( mb_type ) ) { int i; //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" ); - decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16); + decode_cabac_residual_dc( h, h->mb, 0, 0, dc_scan, 16); if( cbp&15 ) { qmul = h->dequant4_coeff[0][s->qscale]; for( i = 0; i < 16; i++ ) { //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i ); - decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15); + decode_cabac_residual_nondc(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15); } } else { fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1); @@ -1719,7 +1658,7 @@ decode_intra_mb: for( i8x8 = 0; i8x8 < 4; i8x8++ ) { if( cbp & (1<mb + 64*i8x8, 5, 4*i8x8, + decode_cabac_residual_nondc(h, h->mb + 64*i8x8, 5, 4*i8x8, scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64); } else { qmul = h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale]; @@ -1727,7 +1666,7 @@ decode_intra_mb: const int index = 4*i8x8 + i4x4; //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index ); //START_TIMER - decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, qmul, 16); + decode_cabac_residual_nondc(h, h->mb + 16*index, 2, index, scan, qmul, 16); //STOP_TIMER("decode_residual") } } @@ -1742,7 +1681,7 @@ decode_intra_mb: int c; for( c = 0; c < 2; c++ ) { //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c ); - decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4); + decode_cabac_residual_dc(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, 4); } } @@ -1753,7 +1692,7 @@ decode_intra_mb: for( i = 0; i < 4; i++ ) { const int index = 16 + 4 * c + i; //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 ); - decode_cabac_residual(h, h->mb + 16*index, 4, index, scan + 1, qmul, 15); + decode_cabac_residual_nondc(h, h->mb + 16*index, 4, index, scan + 1, qmul, 15); } } } else {