From: Fiona Glaser Date: Wed, 8 Apr 2009 12:45:03 +0000 (-0700) Subject: CAVLC optimizations X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bf749f764aa24ad77502907eaeb1ba9e0d82d035;p=libx264 CAVLC optimizations faster bs_write_te, port CABAC context selection optimization to CAVLC. --- diff --git a/common/bs.h b/common/bs.h index 0765f50a..254dbce0 100644 --- a/common/bs.h +++ b/common/bs.h @@ -233,7 +233,7 @@ static inline void bs_write_te( bs_t *s, int x, int val ) { if( x == 1 ) bs_write1( s, 1^val ); - else if( x > 1 ) + else //if( x > 1 ) bs_write_ue( s, val ); } @@ -265,10 +265,8 @@ static inline int bs_size_te( int x, int val ) { if( x == 1 ) return 1; - else if( x > 1 ) + else //if( x > 1 ) return x264_ue_size_tab[val+1]; - else - return 0; } #endif diff --git a/encoder/analyse.c b/encoder/analyse.c index 50178e8c..c0791b82 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -195,7 +195,7 @@ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ) } for( i = 0; i < 3; i++ ) for( j = 0; j < 33; j++ ) - x264_cost_ref[a->i_qp][i][j] = a->i_lambda * bs_size_te( i, j ); + x264_cost_ref[a->i_qp][i][j] = i ? a->i_lambda * bs_size_te( i, j ) : 0; } a->p_cost_mv = p_cost_mv[a->i_qp]; a->p_cost_ref0 = x264_cost_ref[a->i_qp][x264_clip3(h->sh.i_num_ref_idx_l0_active-1,0,2)]; diff --git a/encoder/cavlc.c b/encoder/cavlc.c index 50eb5a1a..cc5964e8 100644 --- a/encoder/cavlc.c +++ b/encoder/cavlc.c @@ -112,16 +112,13 @@ static inline int block_residual_write_cavlc_escape( x264_t *h, bs_t *s, int i_s return i_suffix_length; } -static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count ) +static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, int i_idx, int16_t *l, int i_count, int nC ) { - static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3}; static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0}; x264_run_level_t runlevel; int i_trailing, i_total_zero, i_suffix_length, i; int i_total = 0; unsigned int i_sign; - /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */ - int nC = i_idx >= 25 ? 4 : ct_index[x264_mb_predict_non_zero_code( h, i_idx == 24 ? 0 : i_idx )]; if( !h->mb.cache.non_zero_count[x264_scan8[i_idx]] ) { @@ -198,6 +195,14 @@ static void block_residual_write_cavlc( x264_t *h, bs_t *s, int i_ctxBlockCat, i } } +static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3}; + +#define block_residual_write_cavlc(h,s,cat,idx,l,count)\ +{\ + int nC = cat == DCT_CHROMA_DC ? 4 : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? 0 : idx )];\ + block_residual_write_cavlc(h,s,cat,idx,l,count,nC);\ +} + static void cavlc_qp_delta( x264_t *h, bs_t *s ) { int i_dqp = h->mb.i_qp - h->mb.i_last_qp; @@ -319,7 +324,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) } #if !RDO_SKIP_BS - if( i_mb_type == I_PCM) + if( i_mb_type == I_PCM ) { bs_write_ue( s, i_mb_i_offset + 25 ); i_mb_pos_tex = bs_pos( s ); @@ -382,7 +387,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) if( h->mb.i_partition == D_16x16 ) { - bs_write_ue( s, 0 ); + bs_write1( s, 1 ); if( h->mb.pic.i_fref[0] > 1 ) bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] ); @@ -437,7 +442,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) else { bs_write_ue( s, 3 ); - b_sub_ref = h->mb.pic.i_fref[0] > 1; + b_sub_ref = 1; } /* sub mb type */ @@ -468,12 +473,14 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) bs_write_ue( s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i] ] ); /* ref */ - for( i = 0; i < 4; i++ ) - if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] ) - bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] ); - for( i = 0; i < 4; i++ ) - if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] ) - bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] ); + if( h->mb.pic.i_fref[0] > 1 ) + for( i = 0; i < 4; i++ ) + if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] ) + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] ); + if( h->mb.pic.i_fref[1] > 1 ) + for( i = 0; i < 4; i++ ) + if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] ) + bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] ); /* mvd */ for( i = 0; i < 4; i++ ) @@ -555,7 +562,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) } } else if( i_mb_type == B_DIRECT ) - bs_write_ue( s, 0 ); + bs_write1( s, 1 ); else { x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" );