From: Loren Merritt Date: Thu, 23 Dec 2004 04:33:36 +0000 (+0000) Subject: implement macroblock types B_16x8, B_8x16 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4e5e3770a6b2366676232fe5f335f572f6cdefcb;p=libx264 implement macroblock types B_16x8, B_8x16 tweak thresholds for comparing B mb types git-svn-id: svn://svn.videolan.org/x264/trunk@72 df754926-b1dd-0310-bc7b-ec298dee348c --- diff --git a/encoder/analyse.c b/encoder/analyse.c index 04fd6ffd..41b4bb2c 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -94,6 +94,13 @@ typedef struct int i_cost16x16direct; int i_cost8x8bi; int i_cost8x8direct[4]; + int i_cost16x8bi; + int i_cost8x16bi; + + int i_mb_partition16x8[2]; /* mb_partition_e */ + int i_mb_partition8x16[2]; + int i_mb_type16x8; /* mb_class_e */ + int i_mb_type8x16; int b_direct_available; @@ -116,6 +123,17 @@ static const uint8_t block_idx_y[16] = { 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }; +/* TODO: calculate CABAC costs */ +static const int i_mb_b_cost_table[18] = { + 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0 +}; +static const int i_mb_b16x8_cost_table[16] = { + 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9 +}; +static const int i_sub_mb_b_cost_table[13] = { + 7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1 +}; + static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) { memset( a, 0, sizeof( x264_mb_analysis_t ) ); @@ -177,6 +195,8 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) a->i_cost16x16bi = -1; a->i_cost16x16direct = -1; a->i_cost8x8bi = -1; + a->i_cost16x8bi = -1; + a->i_cost8x16bi = -1; } } } @@ -746,7 +766,12 @@ static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a ) a->i_cost16x16direct += a->i_cost8x8direct[i] = h->pixf.satd[PIXEL_8x8]( &p_fenc[off], i_stride, &p_fdec[off], i_stride ); + + /* mb type cost */ + a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8]; } + + a->i_cost16x16direct += a->i_lambda * i_mb_b_cost_table[B_DIRECT]; } static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a ) @@ -781,6 +806,8 @@ static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a ) a->l0.me16x16 = m; } } + /* subtract ref cost, so we don't have to add it for the other MB types */ + a->l0.me16x16.cost -= m.lm * bs_size_te( h->sh.i_num_ref_idx_l0_active - 1, a->l0.i_ref ); /* ME for list 1 */ a->l1.me16x16.cost = INT_MAX; @@ -800,8 +827,10 @@ static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a ) a->l1.me16x16 = m; } } + /* subtract ref cost, so we don't have to add it for the other MB types */ + a->l1.me16x16.cost -= m.lm * bs_size_te( h->sh.i_num_ref_idx_l1_active - 1, a->l1.i_ref ); - /* Set global ref, needed for all others modes FIXME some work for mixed block mode */ + /* Set global ref, needed for other modes? */ x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref ); x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref ); @@ -823,29 +852,81 @@ static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a ) bs_size_se( a->l0.me16x16.mv[1] - a->l0.me16x16.mvp[1] ) + bs_size_se( a->l1.me16x16.mv[0] - a->l1.me16x16.mvp[0] ) + bs_size_se( a->l1.me16x16.mv[1] - a->l1.me16x16.mvp[1] ) ); + + /* mb type cost */ + a->i_cost16x16bi += a->i_lambda * i_mb_b_cost_table[B_BI_BI]; + a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0]; + a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1]; } +#define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) \ + if( x264_mb_partition_listX_table[0][part] ) \ + { \ + x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); \ + x264_macroblock_cache_mv( h, x,y,dx,dy, 0, me0.mv[0], me0.mv[1] ); \ + } \ + else \ + { \ + x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); \ + x264_macroblock_cache_mv( h, x,y,dx,dy, 0, 0, 0 ); \ + if( b_mvd ) \ + x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0, 0 ); \ + } \ + if( x264_mb_partition_listX_table[1][part] ) \ + { \ + x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); \ + x264_macroblock_cache_mv( h, x,y,dx,dy, 1, me1.mv[0], me1.mv[1] ); \ + } \ + else \ + { \ + x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); \ + x264_macroblock_cache_mv( h, x,y,dx,dy, 1, 0, 0 ); \ + if( b_mvd ) \ + x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0, 0 ); \ + } + +static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd ) +{ + int x = (i%2)*2; + int y = (i/2)*2; + if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) + { + x264_mb_load_mv_direct8x8( h, i ); + if( b_mvd ) + { + x264_macroblock_cache_mvd( h, x, y, 2, 2, 0, 0, 0 ); + x264_macroblock_cache_mvd( h, x, y, 2, 2, 1, 0, 0 ); + x264_macroblock_cache_skip( h, x, y, 2, 2, 1 ); + } + } + else + { + CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] ); + } +} +static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd ) +{ + CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] ); +} +static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd ) +{ + CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] ); +} +#undef CACHE_MV_BI + static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a ) { - uint8_t pix[2][8*8]; uint8_t *p_fref[2] = { h->mb.pic.p_fref[0][a->l0.i_ref][0], h->mb.pic.p_fref[1][a->l1.i_ref][0] }; uint8_t *p_fenc = h->mb.pic.p_fenc[0]; - int mvc[2][5][2], i_mvc[2]; - int i, j; + uint8_t pix[2][8*8]; + int i, l; /* XXX Needed for x264_mb_predict_mv */ h->mb.i_partition = D_8x8; a->i_cost8x8bi = 0; - i_mvc[0] = i_mvc[1] = 1; - mvc[0][0][0] = a->l0.me16x16.mv[0]; - mvc[0][0][1] = a->l0.me16x16.mv[1]; - mvc[1][0][0] = a->l1.me16x16.mv[0]; - mvc[1][0][1] = a->l1.me16x16.mv[1]; - - for( i = 0; i < 4; i++ ) { const int x8 = i%2; @@ -854,35 +935,37 @@ static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a ) int i_part_cost; int i_part_cost_bi = 0; - for( j = 0; j < 2; j++ ) + for( l = 0; l < 2; l++ ) { - x264_mb_analysis_list_t *l = j ? &a->l1 : &a->l0; - x264_me_t *m = &l->me8x8[i]; + x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0; + x264_me_t *m = &lX->me8x8[i]; m->i_pixel = PIXEL_8x8; m->lm = a->i_lambda; m->p_fenc = p_fenc_i; - m->p_fref = &p_fref[j][8*(y8*h->mb.pic.i_stride[0]+x8)]; + m->p_fref = &p_fref[l][8*(y8*h->mb.pic.i_stride[0]+x8)]; m->i_stride = h->mb.pic.i_stride[0]; m->i_mv_range = a->i_mv_range; - x264_mb_predict_mv( h, j, 4*i, 2, m->mvp ); - x264_me_search( h, m, mvc[j], i_mvc[j] ); + x264_mb_predict_mv( h, l, 4*i, 2, m->mvp ); + x264_me_search( h, m, &lX->me16x16.mv, 1 ); - x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, j, m->mv[0], m->mv[1] ); - l->i_cost8x8 += m->cost; + x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, l, m->mv[0], m->mv[1] ); + lX->i_cost8x8 += m->cost; /* BI mode */ - h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[j], 8, + h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[l], 8, m->mv[0], m->mv[1], 8, 8 ); - /* FIXME: add ref cost */ + /* FIXME: ref cost */ i_part_cost_bi += a->i_lambda * ( bs_size_se( m->mv[0] - m->mvp[0] ) + - bs_size_se( m->mv[1] - m->mvp[1] ) ); + bs_size_se( m->mv[1] - m->mvp[1] ) + + i_sub_mb_b_cost_table[D_L0_8x8] ); } h->pixf.avg[PIXEL_8x8]( pix[0], 8, pix[1], 8 ); - i_part_cost_bi += h->pixf.satd[PIXEL_8x8]( p_fenc_i, h->mb.pic.i_stride[0], pix[0], 8 ); + i_part_cost_bi += h->pixf.satd[PIXEL_8x8]( p_fenc_i, h->mb.pic.i_stride[0], pix[0], 8 ) + + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8]; i_part_cost = a->l0.me8x8[i].cost; h->mb.i_sub_partition[i] = D_L0_8x8; @@ -904,40 +987,163 @@ static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a ) a->i_cost8x8bi += i_part_cost; /* XXX Needed for x264_mb_predict_mv */ - if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) + x264_mb_cache_mv_b8x8( h, a, i, 0 ); + } + + /* mb type cost */ + a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8]; +} + +static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a ) +{ + uint8_t *p_fref[2] = { h->mb.pic.p_fref[0][a->l0.i_ref][0], + h->mb.pic.p_fref[1][a->l1.i_ref][0] }; + uint8_t *p_fenc = h->mb.pic.p_fenc[0]; + uint8_t pix[2][8*8]; + int i_ref_stride = h->mb.pic.i_stride[0]; + int mvc[2][2]; + int i, l; + + h->mb.i_partition = D_16x8; + a->i_cost16x8bi = 0; + + for( i = 0; i < 2; i++ ) + { + uint8_t *p_fenc_i = &p_fenc[8*i*i_ref_stride]; + int i_part_cost; + int i_part_cost_bi = 0; + + /* TODO: check only the list(s) that were used in b8x8? */ + for( l = 0; l < 2; l++ ) { - x264_mb_load_mv_direct8x8( h, i ); - x264_macroblock_cache_mvd( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 ); - x264_macroblock_cache_mvd( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 ); - x264_macroblock_cache_skip( h, 2*x8, 2*y8, 2, 2, 1 ); + x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0; + x264_me_t *m = &lX->me16x8[i]; + + m->i_pixel = PIXEL_16x8; + m->lm = a->i_lambda; + + m->p_fenc = p_fenc_i; + m->i_stride= i_ref_stride; + m->p_fref = &p_fref[l][8*i*i_ref_stride]; + m->i_mv_range = a->i_mv_range; + + mvc[0][0] = lX->me8x8[2*i].mv[0]; + mvc[0][1] = lX->me8x8[2*i].mv[1]; + mvc[1][0] = lX->me8x8[2*i+1].mv[0]; + mvc[1][1] = lX->me8x8[2*i+1].mv[1]; + + x264_mb_predict_mv( h, 0, 8*i, 2, m->mvp ); + x264_me_search( h, m, mvc, 2 ); + + /* BI mode */ + h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[l], 8, + m->mv[0], m->mv[1], 8, 8 ); + /* FIXME: ref cost */ + i_part_cost_bi += a->i_lambda * ( bs_size_se( m->mv[0] - m->mvp[0] ) + + bs_size_se( m->mv[1] - m->mvp[1] ) ); } - else + + h->pixf.avg[PIXEL_16x8]( pix[0], 8, pix[1], 8 ); + i_part_cost_bi += h->pixf.satd[PIXEL_16x8]( p_fenc_i, h->mb.pic.i_stride[0], pix[0], 8 ); + + i_part_cost = a->l0.me16x8[i].cost; + a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */ + if( a->l1.me16x8[i].cost < i_part_cost ) { - if( h->mb.i_sub_partition[i] == D_L1_8x8 ) - { - x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, -1 ); - x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 ); - x264_macroblock_cache_mvd( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 ); - } - else - { - x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, a->l0.i_ref ); - x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, a->l0.me8x8[i].mv[0], a->l0.me8x8[i].mv[1] ); - } + i_part_cost = a->l1.me16x8[i].cost; + a->i_mb_partition16x8[i] = D_L1_8x8; + } + if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost ) + { + i_part_cost = i_part_cost_bi; + a->i_mb_partition16x8[i] = D_BI_8x8; + } + a->i_cost16x8bi += i_part_cost; - if( h->mb.i_sub_partition[i] == D_L0_8x8 ) - { - x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 1, -1 ); - x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 ); - x264_macroblock_cache_mvd( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 ); - } - else - { - x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 1, a->l1.i_ref ); - x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, a->l1.me8x8[i].mv[0], a->l1.me8x8[i].mv[1] ); - } + if( i == 0 ) + x264_mb_cache_mv_b16x8( h, a, i, 0 ); + } + + /* mb type cost */ + a->i_mb_type16x8 = B_L0_L0 + + (a->i_mb_partition16x8[0]>>2) * 3 + + (a->i_mb_partition16x8[1]>>2); + a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8]; +} +static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a ) +{ + uint8_t *p_fref[2] = { h->mb.pic.p_fref[0][a->l0.i_ref][0], + h->mb.pic.p_fref[1][a->l1.i_ref][0] }; + uint8_t *p_fenc = h->mb.pic.p_fenc[0]; + uint8_t pix[2][8*8]; + int i_ref_stride = h->mb.pic.i_stride[0]; + int mvc[2][2]; + int i, l; + + h->mb.i_partition = D_8x16; + a->i_cost8x16bi = 0; + + for( i = 0; i < 2; i++ ) + { + uint8_t *p_fenc_i = &p_fenc[8*i]; + int i_part_cost; + int i_part_cost_bi = 0; + + for( l = 0; l < 2; l++ ) + { + x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0; + x264_me_t *m = &lX->me8x16[i]; + + m->i_pixel = PIXEL_8x16; + m->lm = a->i_lambda; + + m->p_fenc = p_fenc_i; + m->p_fref = &p_fref[l][8*i]; + m->i_stride= i_ref_stride; + m->i_mv_range = a->i_mv_range; + + mvc[0][0] = lX->me8x8[i].mv[0]; + mvc[0][1] = lX->me8x8[i].mv[1]; + mvc[1][0] = lX->me8x8[i+2].mv[0]; + mvc[1][1] = lX->me8x8[i+2].mv[1]; + + x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp ); + x264_me_search( h, m, mvc, 2 ); + + /* BI mode */ + h->mc[MC_LUMA]( m->p_fref, m->i_stride, pix[l], 8, + m->mv[0], m->mv[1], 8, 8 ); + /* FIXME: ref cost */ + i_part_cost_bi += a->i_lambda * ( bs_size_se( m->mv[0] - m->mvp[0] ) + + bs_size_se( m->mv[1] - m->mvp[1] ) ); + } + + h->pixf.avg[PIXEL_8x16]( pix[0], 8, pix[1], 8 ); + i_part_cost_bi += h->pixf.satd[PIXEL_8x16]( p_fenc_i, h->mb.pic.i_stride[0], pix[0], 8 ); + + i_part_cost = a->l0.me8x16[i].cost; + a->i_mb_partition8x16[i] = D_L0_8x8; + if( a->l1.me8x16[i].cost < i_part_cost ) + { + i_part_cost = a->l1.me8x16[i].cost; + a->i_mb_partition8x16[i] = D_L1_8x8; } + if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost ) + { + i_part_cost = i_part_cost_bi; + a->i_mb_partition8x16[i] = D_BI_8x8; + } + a->i_cost8x16bi += i_part_cost; + + if( i == 0 ) + x264_mb_cache_mv_b8x16( h, a, i, 0 ); } + + /* mb type cost */ + a->i_mb_type8x16 = B_L0_L0 + + (a->i_mb_partition8x16[0]>>2) * 3 + + (a->i_mb_partition8x16[1]>>2); + a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16]; } /***************************************************************************** @@ -1155,10 +1361,7 @@ void x264_macroblock_analyse( x264_t *h ) } else if( h->sh.i_type == SLICE_TYPE_B ) { - const unsigned int i_neighbour = h->mb.i_neighbour; - const unsigned int flags = h->param.analyse.inter; int b_skip = 0; - int i_cost; analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h ); if( analysis.b_direct_available ) @@ -1173,19 +1376,19 @@ void x264_macroblock_analyse( x264_t *h ) if( !b_skip ) { - /* best inter mode */ + const unsigned int flags = h->param.analyse.inter; + int i_partition; + int i_cost; + + /* select best inter mode */ /* direct must be first */ if( analysis.b_direct_available ) x264_mb_analyse_inter_direct( h, &analysis ); x264_mb_analyse_inter_b16x16( h, &analysis ); - /* 8x8 must be last */ - if( flags & X264_ANALYSE_BSUB16x16 ) - x264_mb_analyse_inter_b8x8( h, &analysis ); - h->mb.i_type = B_L0_L0; - h->mb.i_partition = D_16x16; + i_partition = D_16x16; i_cost = analysis.l0.me16x16.cost; if( analysis.l1.me16x16.cost < i_cost ) { @@ -1202,24 +1405,58 @@ void x264_macroblock_analyse( x264_t *h ) h->mb.i_type = B_DIRECT; i_cost = analysis.i_cost16x16direct; } - if( analysis.i_cost8x8bi < i_cost && analysis.i_cost8x8bi >= 0 ) + + if( flags & X264_ANALYSE_BSUB16x16 ) { - h->mb.i_type = B_8x8; - h->mb.i_partition = D_8x8; - i_cost = analysis.i_cost8x8bi; + x264_mb_analyse_inter_b8x8( h, &analysis ); + if( analysis.i_cost8x8bi < i_cost ) + { + h->mb.i_type = B_8x8; + i_partition = D_8x8; + i_cost = analysis.i_cost8x8bi; + + if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] || + h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] ) + { + x264_mb_analyse_inter_b16x8( h, &analysis ); + if( analysis.i_cost16x8bi < i_cost ) + { + i_partition = D_16x8; + i_cost = analysis.i_cost16x8bi; + h->mb.i_type = analysis.i_mb_type16x8; + } + } + if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] || + h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] ) + { + x264_mb_analyse_inter_b8x16( h, &analysis ); + if( analysis.i_cost8x16bi < i_cost ) + { + i_partition = D_8x16; + i_cost = analysis.i_cost8x16bi; + h->mb.i_type = analysis.i_mb_type8x16; + } + } + } } + h->mb.i_partition = i_partition; + /* refine qpel */ - if( h->mb.i_partition == D_16x16 ) + if( i_partition == D_16x16 ) { if( h->mb.i_type == B_L0_L0 ) { + analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0]; x264_me_refine_qpel( h, &analysis.l0.me16x16 ); + analysis.l0.me16x16.cost += analysis.i_lambda * i_mb_b_cost_table[B_L0_L0]; i_cost = analysis.l0.me16x16.cost; } else if( h->mb.i_type == B_L1_L1 ) { + analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1]; x264_me_refine_qpel( h, &analysis.l1.me16x16 ); + analysis.l1.me16x16.cost += analysis.i_lambda * i_mb_b_cost_table[B_L1_L1]; i_cost = analysis.l1.me16x16.cost; } } @@ -1227,6 +1464,10 @@ void x264_macroblock_analyse( x264_t *h ) /* best intra mode */ x264_mb_analyse_intra( h, &analysis ); + /* mb type cost */ + analysis.i_sad_i16x16 += analysis.i_lambda * i_mb_b_cost_table[I_16x16]; + analysis.i_sad_i4x4 += analysis.i_lambda * i_mb_b_cost_table[I_4x4]; + if( analysis.i_sad_i16x16 >= 0 && analysis.i_sad_i16x16 < i_cost ) { h->mb.i_type = I_16x16; @@ -1330,18 +1571,28 @@ void x264_macroblock_analyse( x264_t *h ) } case B_SKIP: + /* nothing has changed since x264_macroblock_probe_bskip */ + break; case B_DIRECT: - /* probably unnecessary for B_SKIP */ x264_mb_load_mv_direct8x8( h, 0 ); x264_mb_load_mv_direct8x8( h, 1 ); x264_mb_load_mv_direct8x8( h, 2 ); x264_mb_load_mv_direct8x8( h, 3 ); break; - case B_L0_L0: + case B_8x8: + /* optimize: cache might not need to be rewritten */ + for( i = 0; i < 4; i++ ) + x264_mb_cache_mv_b8x8( h, &analysis, i, 1 ); + break; + + default: /* the rest of the B types */ switch( h->mb.i_partition ) { - case D_16x16: + case D_16x16: + switch( h->mb.i_type ) + { + case B_L0_L0: x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref ); x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] ); @@ -1349,15 +1600,7 @@ void x264_macroblock_analyse( x264_t *h ) x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0, 0 ); x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0, 0 ); break; - default: - fprintf( stderr, "internal error\n" ); - break; - } - break; - case B_L1_L1: - switch( h->mb.i_partition ) - { - case D_16x16: + case B_L1_L1: x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 ); x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0, 0 ); x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0, 0 ); @@ -1365,35 +1608,27 @@ void x264_macroblock_analyse( x264_t *h ) x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, analysis.l1.i_ref ); x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, analysis.l1.me16x16.mv[0], analysis.l1.me16x16.mv[1] ); break; - - default: - fprintf( stderr, "internal error\n" ); - break; - } - break; - case B_BI_BI: - switch( h->mb.i_partition ) - { - case D_16x16: + case B_BI_BI: x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.i_ref ); x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.mv[0], analysis.l0.me16x16.mv[1] ); x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, analysis.l1.i_ref ); x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, analysis.l1.me16x16.mv[0], analysis.l1.me16x16.mv[1] ); break; - - default: - fprintf( stderr, "internal error\n" ); - break; + } + break; + case D_16x8: + x264_mb_cache_mv_b16x8( h, &analysis, 0, 1 ); + x264_mb_cache_mv_b16x8( h, &analysis, 1, 1 ); + break; + case D_8x16: + x264_mb_cache_mv_b8x16( h, &analysis, 0, 1 ); + x264_mb_cache_mv_b8x16( h, &analysis, 1, 1 ); + break; + default: + fprintf( stderr, "internal error (invalid MB type)\n" ); + break; } - break; - case B_8x8: - /* nothing to do: caches were updated during analysis */ - break; - - default: - fprintf( stderr, "internal error (invalid MB type)\n" ); - break; } }