From c89bc900a3bf0d4c4c728ad378703970b4f14e18 Mon Sep 17 00:00:00 2001 From: Jason Garrett-Glaser Date: Tue, 30 Sep 2008 18:34:56 -0700 Subject: [PATCH] Rework subme system, add RD refinement in B-frames The new system is as follows: subme6 is RD in I/P frames, subme7 is RD in all frames, subme8 is RD refinement in I/P frames, and subme9 is RD refinement in all frames. subme6 == old subme6, subme7 == old subme6+brdo, subme8 == old subme7+brdo, subme9 == no equivalent --b-rdo has, accordingly, been removed. --bime has also been removed, and instead enabled automatically at subme >= 5. RD refinement in B-frames (subme9) includes both qpel-RD and an RD version of bime. --- Makefile | 8 +-- common/common.c | 10 +--- encoder/analyse.c | 135 ++++++++++++++++++++++++++++++++-------------- encoder/cabac.c | 27 +++------- encoder/cavlc.c | 26 +++------ encoder/encoder.c | 6 +-- encoder/me.c | 71 ++++++++++++++++++------ encoder/me.h | 5 +- x264.c | 17 +++--- x264.h | 4 +- 10 files changed, 185 insertions(+), 124 deletions(-) diff --git a/Makefile b/Makefile index 607d1c71..2d185099 100644 --- a/Makefile +++ b/Makefile @@ -112,10 +112,10 @@ SRC2 = $(SRCS) $(SRCCLI) OPT0 = --crf 30 -b1 -m1 -r1 --me dia --no-cabac --pre-scenecut --direct temporal --no-ssim --no-psnr OPT1 = --crf 16 -b2 -m3 -r3 --me hex -8 --direct spatial --no-dct-decimate OPT2 = --crf 26 -b2 -m5 -r2 --me hex -8 -w --cqm jvt --nr 100 -OPT3 = --crf 18 -b3 -m7 -r5 --me umh -8 -t1 -A all --mixed-refs --b-rdo -w --b-pyramid --direct auto --bime --no-fast-pskip -OPT4 = --crf 22 -b3 -m6 -r4 --me esa -8 -t2 -A all --mixed-refs --b-rdo --bime -OPT5 = --frames 50 --crf 24 -b3 -m7 -r3 --me tesa -8 -t1 --mixed-refs --b-rdo --bime -OPT6 = --frames 50 -q0 -m6 -r2 --me hex -Aall +OPT3 = --crf 18 -b3 -m9 -r5 --me umh -8 -t1 -A all --mixed-refs -w --b-pyramid --direct auto --no-fast-pskip +OPT4 = --crf 22 -b3 -m7 -r4 --me esa -8 -t2 -A all --mixed-refs +OPT5 = --frames 50 --crf 24 -b3 -m9 -r3 --me tesa -8 -t1 --mixed-refs +OPT6 = --frames 50 -q0 -m9 -r2 --me hex -Aall OPT7 = --frames 50 -q0 -m2 -r1 --me hex --no-cabac ifeq (,$(VIDS)) diff --git a/common/common.c b/common/common.c index 9d84bfaa..5e08c688 100644 --- a/common/common.c +++ b/common/common.c @@ -487,12 +487,8 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) p->analyse.f_psy_trellis = 0; } } - OPT("bime") - p->analyse.b_bidir_me = atobool(value); OPT("chroma-me") p->analyse.b_chroma_me = atobool(value); - OPT2("b-rdo", "brdo") - p->analyse.b_bframe_rdo = atobool(value); OPT("mixed-refs") p->analyse.b_mixed_references = atobool(value); OPT("trellis") @@ -842,7 +838,6 @@ char *x264_param2string( x264_param_t *p, int b_res ) s += sprintf( s, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] ); s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine ); s += sprintf( s, " psy_rd=%.1f:%.1f", p->analyse.f_psy_rd, p->analyse.f_psy_trellis ); - s += sprintf( s, " brdo=%d", p->analyse.b_bframe_rdo ); s += sprintf( s, " mixed_ref=%d", p->analyse.b_mixed_references ); s += sprintf( s, " me_range=%d", p->analyse.i_me_range ); s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me ); @@ -859,10 +854,9 @@ char *x264_param2string( x264_param_t *p, int b_res ) s += sprintf( s, " bframes=%d", p->i_bframe ); if( p->i_bframe ) { - s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d wpredb=%d bime=%d", + s += sprintf( s, " b_pyramid=%d b_adapt=%d b_bias=%d direct=%d wpredb=%d", p->b_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias, - p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, - p->analyse.b_bidir_me ); + p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred ); } s += sprintf( s, " keyint=%d keyint_min=%d scenecut=%d%s", diff --git a/encoder/analyse.c b/encoder/analyse.c index ebc14d60..db4917bc 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -76,7 +76,7 @@ typedef struct int i_lambda2; int i_qp; int16_t *p_cost_mv; - int b_mbrd; + int i_mbrd; /* I: Intra part */ @@ -203,19 +203,19 @@ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ) static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) { + int i = h->param.analyse.i_subpel_refine - (h->sh.i_type == SLICE_TYPE_B); + /* mbrd == 1 -> RD mode decision */ + /* mbrd == 2 -> RD refinement */ + a->i_mbrd = (i>=6) + (i>=8); /* conduct the analysis using this lamda and QP */ a->i_qp = h->mb.i_qp = i_qp; h->mb.i_chroma_qp = h->chroma_qp_table[i_qp]; a->i_lambda = x264_lambda_tab[i_qp]; a->i_lambda2 = x264_lambda2_tab[i_qp]; - a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 && - ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo ); - - h->mb.i_me_method = h->param.analyse.i_me_method; h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine; h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P && h->mb.i_subpel_refine >= 5; - h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd; + h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->i_mbrd; h->mb.b_transform_8x8 = 0; h->mb.b_noise_reduction = 0; @@ -226,12 +226,12 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) a->i_satd_i8x8chroma = COST_MAX; /* non-RD PCM decision is inaccurate (as is psy-rd), so don't do it */ - a->i_satd_pcm = !h->mb.i_psy_rd && a->b_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX; + a->i_satd_pcm = !h->mb.i_psy_rd && a->i_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX; a->b_fast_intra = 0; h->mb.i_skip_intra = h->mb.b_lossless ? 0 : - a->b_mbrd ? 2 : + a->i_mbrd ? 2 : !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction; /* II: Inter part P/B frame */ @@ -650,7 +650,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_ { DECLARE_ALIGNED_16( uint8_t edge[33] ); x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8]; - int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 ); + int i_satd_thresh = a->i_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 ); int i_cost = 0; b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0]; @@ -728,7 +728,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_ a->i_satd_i8x8 = COST_MAX; i_cost = i_cost * 4/(idx+1); } - if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 ) + if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+!!a->i_mbrd)/4 ) return; } @@ -738,7 +738,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_ int i_cost; int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 ); b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0]; - if( a->b_mbrd ) + if( a->i_mbrd ) i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8; i_cost = a->i_lambda * 24; /* from JVT (SATD0) */ @@ -1094,7 +1094,7 @@ static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a ) assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 ); h->mb.i_type = P_L0; - if( a->b_mbrd ) + if( a->i_mbrd ) { x264_mb_cache_fenc_satd( h ); if( a->l0.me16x16.i_ref == 0 && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv ) @@ -2029,31 +2029,34 @@ static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_i } } -static void refine_bidir( x264_t *h, x264_mb_analysis_t *a ) +static void x264_refine_bidir( x264_t *h, x264_mb_analysis_t *a ) { const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref]; int i; + if( IS_INTRA(h->mb.i_type) ) + return; + switch( h->mb.i_partition ) { case D_16x16: if( h->mb.i_type == B_BI_BI ) - x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight ); + x264_me_refine_bidir_satd( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight ); break; case D_16x8: for( i=0; i<2; i++ ) if( a->i_mb_partition16x8[i] == D_BI_8x8 ) - x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight ); + x264_me_refine_bidir_satd( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight ); break; case D_8x16: for( i=0; i<2; i++ ) if( a->i_mb_partition8x16[i] == D_BI_8x8 ) - x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight ); + x264_me_refine_bidir_satd( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight ); break; case D_8x8: for( i=0; i<4; i++ ) if( h->mb.i_sub_partition[i] == D_BI_8x8 ) - x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight ); + x264_me_refine_bidir_satd( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight ); break; } } @@ -2120,10 +2123,10 @@ void x264_macroblock_analyse( x264_t *h ) /*--------------------------- Do the analysis ---------------------------*/ if( h->sh.i_type == SLICE_TYPE_I ) { - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) x264_mb_cache_fenc_satd( h ); x264_mb_analyse_intra( h, &analysis, COST_MAX ); - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) x264_intra_rd( h, &analysis, COST_MAX ); i_cost = analysis.i_satd_i16x16; @@ -2133,7 +2136,7 @@ void x264_macroblock_analyse( x264_t *h ) if( analysis.i_satd_pcm < i_cost ) h->mb.i_type = I_PCM; - else if( h->mb.i_subpel_refine >= 7 ) + else if( analysis.i_mbrd >= 2 ) x264_intra_rd_refine( h, &analysis ); } else if( h->sh.i_type == SLICE_TYPE_P ) @@ -2245,7 +2248,7 @@ void x264_macroblock_analyse( x264_t *h ) /* refine qpel */ //FIXME mb_type costs? - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) { /* refine later */ } @@ -2324,7 +2327,7 @@ void x264_macroblock_analyse( x264_t *h ) analysis.i_satd_i8x8, analysis.i_satd_i4x4 ); - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) { x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) ); i_type = P_L0; @@ -2355,7 +2358,7 @@ void x264_macroblock_analyse( x264_t *h ) h->stat.frame.i_inter_cost += i_cost; h->stat.frame.i_mbs_analysed++; - if( h->mb.i_subpel_refine >= 7 && h->mb.i_type != I_PCM ) + if( analysis.i_mbrd >= 2 && h->mb.i_type != I_PCM ) { if( IS_INTRA( h->mb.i_type ) ) { @@ -2364,7 +2367,7 @@ void x264_macroblock_analyse( x264_t *h ) else if( i_partition == D_16x16 ) { x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref ); - x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 ); + x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 ); } else if( i_partition == D_16x8 ) { @@ -2372,8 +2375,8 @@ void x264_macroblock_analyse( x264_t *h ) h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8; x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref ); x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref ); - x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 ); - x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 ); + x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0, 0 ); + x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2, 0 ); } else if( i_partition == D_8x16 ) { @@ -2381,8 +2384,8 @@ void x264_macroblock_analyse( x264_t *h ) h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8; x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref ); x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref ); - x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 ); - x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 ); + x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0, 0 ); + x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1, 0 ); } else if( i_partition == D_8x8 ) { @@ -2390,7 +2393,7 @@ void x264_macroblock_analyse( x264_t *h ) x264_analyse_update_cache( h, &analysis ); for( i8x8 = 0; i8x8 < 4; i8x8++ ) if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 ) - x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 ); + x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8, 0 ); } } } @@ -2400,7 +2403,7 @@ void x264_macroblock_analyse( x264_t *h ) int i_bskip_cost = COST_MAX; int b_skip = 0; - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) x264_mb_cache_fenc_satd( h ); h->mb.i_type = B_SKIP; @@ -2436,7 +2439,7 @@ void x264_macroblock_analyse( x264_t *h ) { /* chance of skip is too small to bother */ } - else if( analysis.b_mbrd ) + else if( analysis.i_mbrd ) { i_bskip_cost = ssd_mb( h ); /* 6 = minimum cavlc cost of a non-skipped MB */ @@ -2474,7 +2477,7 @@ void x264_macroblock_analyse( x264_t *h ) COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI ); COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT ); - if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 ) + if( analysis.i_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 ) { x264_mb_analyse_b_rd( h, &analysis, i_cost ); if( i_bskip_cost < analysis.i_rd16x16direct && @@ -2516,7 +2519,7 @@ void x264_macroblock_analyse( x264_t *h ) } } - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) { /* refine later */ } @@ -2599,7 +2602,7 @@ void x264_macroblock_analyse( x264_t *h ) } } - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) { i_satd_inter = i_cost; x264_mb_analyse_b_rd( h, &analysis, i_satd_inter ); @@ -2620,7 +2623,7 @@ void x264_macroblock_analyse( x264_t *h ) x264_mb_analyse_intra( h, &analysis, i_satd_inter ); - if( analysis.b_mbrd ) + if( analysis.i_mbrd ) { x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost ); x264_intra_rd( h, &analysis, i_satd_inter * 17/16 ); @@ -2634,16 +2637,70 @@ void x264_macroblock_analyse( x264_t *h ) h->mb.i_type = i_type; h->mb.i_partition = i_partition; - if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) && i_type != I_PCM ) + if( analysis.i_mbrd >= 2 && IS_INTRA( i_type ) && i_type != I_PCM ) x264_intra_rd_refine( h, &analysis ); - else if( h->param.analyse.b_bidir_me ) - refine_bidir( h, &analysis ); + if( h->mb.i_subpel_refine >= 5 ) + x264_refine_bidir( h, &analysis ); + + if( analysis.i_mbrd >= 2 && i_type > B_DIRECT && i_type < B_SKIP ) + { + const int i_biweight = h->mb.bipred_weight[analysis.l0.i_ref][analysis.l1.i_ref]; + x264_analyse_update_cache( h, &analysis ); + + if( i_partition == D_16x16 ) + { + if( i_type == B_L0_L0 ) + x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 ); + else if( i_type == B_L1_L1 ) + x264_me_refine_qpel_rd( h, &analysis.l1.me16x16, analysis.i_lambda2, 0, 1 ); + else if( i_type == B_BI_BI ) + x264_me_refine_bidir_rd( h, &analysis.l0.me16x16, &analysis.l1.me16x16, i_biweight, 0, analysis.i_lambda2 ); + } + else if( i_partition == D_16x8 ) + { + for( i = 0; i < 2; i++ ) + { + h->mb.i_sub_partition[i*2] = h->mb.i_sub_partition[i*2+1] = analysis.i_mb_partition16x8[i]; + if( analysis.i_mb_partition16x8[i] == D_L0_8x8 ) + x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[i], analysis.i_lambda2, i*2, 0 ); + else if( analysis.i_mb_partition16x8[i] == D_L1_8x8 ) + x264_me_refine_qpel_rd( h, &analysis.l1.me16x8[i], analysis.i_lambda2, i*2, 1 ); + else if( analysis.i_mb_partition16x8[i] == D_BI_8x8 ) + x264_me_refine_bidir_rd( h, &analysis.l0.me16x8[i], &analysis.l1.me16x8[i], i_biweight, i*2, analysis.i_lambda2 ); + } + } + else if( i_partition == D_8x16 ) + { + for( i = 0; i < 2; i++ ) + { + h->mb.i_sub_partition[i] = h->mb.i_sub_partition[i+2] = analysis.i_mb_partition8x16[i]; + if( analysis.i_mb_partition8x16[i] == D_L0_8x8 ) + x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[i], analysis.i_lambda2, i, 0 ); + else if( analysis.i_mb_partition8x16[i] == D_L1_8x8 ) + x264_me_refine_qpel_rd( h, &analysis.l1.me8x16[i], analysis.i_lambda2, i, 1 ); + else if( analysis.i_mb_partition8x16[i] == D_BI_8x8 ) + x264_me_refine_bidir_rd( h, &analysis.l0.me8x16[i], &analysis.l1.me8x16[i], i_biweight, i, analysis.i_lambda2 ); + } + } + else if( i_partition == D_8x8 ) + { + for( i = 0; i < 4; i++ ) + { + if( h->mb.i_sub_partition[i] == D_L0_8x8 ) + x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i], analysis.i_lambda2, i, 0 ); + else if( h->mb.i_sub_partition[i] == D_L1_8x8 ) + x264_me_refine_qpel_rd( h, &analysis.l1.me8x8[i], analysis.i_lambda2, i, 1 ); + else if( h->mb.i_sub_partition[i] == D_BI_8x8 ) + x264_me_refine_bidir_rd( h, &analysis.l0.me8x8[i], &analysis.l1.me8x8[i], i_biweight, i, analysis.i_lambda2 ); + } + } + } } } x264_analyse_update_cache( h, &analysis ); - if( !analysis.b_mbrd ) + if( !analysis.i_mbrd ) x264_mb_analyse_transform( h ); h->mb.b_trellis = h->param.analyse.i_trellis; diff --git a/encoder/cabac.c b/encoder/cabac.c index 8187438c..2f0f02a9 100644 --- a/encoder/cabac.c +++ b/encoder/cabac.c @@ -1026,47 +1026,34 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) /***************************************************************************** * RD only; doesn't generate a valid bitstream * doesn't write cbp or chroma dc (I don't know how much this matters) + * doesn't write ref or subpartition (never varies between calls, so no point in doing so) * works on all partition sizes except 16x16 * for sub8x8, call once per 8x8 block *****************************************************************************/ static void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel ) { const int i_mb_type = h->mb.i_type; + int b_8x16 = h->mb.i_partition == D_8x16; int j; if( i_mb_type == P_8x8 ) - { - x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[i8] ); - if( h->mb.pic.i_fref[0] > 1 ) - x264_cabac_mb_ref( h, cb, 0, 4*i8 ); x264_cabac_mb8x8_mvd( h, cb, 0, i8 ); - } else if( i_mb_type == P_L0 ) + x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4>>b_8x16, 2< B_DIRECT && i_mb_type < B_8x8 ) { - if( h->mb.pic.i_fref[0] > 1 ) - x264_cabac_mb_ref( h, cb, 0, 4*i8 ); - if( h->mb.i_partition == D_16x8 ) - x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4, 2 ); - else //8x16 - x264_cabac_mb_mvd( h, cb, 0, 4*i8, 2, 4 ); + if( x264_mb_type_list0_table[ i_mb_type ][!!i8] ) x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4>>b_8x16, 2<>b_8x16, 2<mb.i_sub_partition[i8] ); - - if( h->mb.pic.i_fref[0] > 1 - && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] ) - x264_cabac_mb_ref( h, cb, 0, 4*i8 ); - if( h->mb.pic.i_fref[1] > 1 - && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] ) - x264_cabac_mb_ref( h, cb, 1, 4*i8 ); - x264_cabac_mb8x8_mvd( h, cb, 0, i8 ); x264_cabac_mb8x8_mvd( h, cb, 1, i8 ); } else { - x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type\n" ); + x264_log(h, X264_LOG_ERROR, "invalid/unhandled mb_type %d\n",i_mb_type ); return; } diff --git a/encoder/cavlc.c b/encoder/cavlc.c index b6f4c9a2..8e27aa23 100644 --- a/encoder/cavlc.c +++ b/encoder/cavlc.c @@ -639,6 +639,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) /***************************************************************************** * RD only; doesn't generate a valid bitstream * doesn't write cbp or chroma dc (I don't know how much this matters) + * doesn't write ref or subpartition (never varies between calls, so no point in doing so) * works on all partition sizes except 16x16 * for sub8x8, call once per 8x8 block *****************************************************************************/ @@ -646,37 +647,22 @@ static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel ) { bs_t s; const int i_mb_type = h->mb.i_type; + int b_8x16 = h->mb.i_partition == D_8x16; int j; s.i_bits_encoded = 0; if( i_mb_type == P_8x8 ) - { - bs_write_ue( &s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] ); - 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[4*i8]] ); cavlc_mb8x8_mvd( h, &s, 0, i8 ); - } else if( i_mb_type == P_L0 ) + cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 ); + else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 ) { - 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[4*i8]] ); - if( h->mb.i_partition == D_16x8 ) - cavlc_mb_mvd( h, &s, 0, 4*i8, 4 ); - else //8x16 - cavlc_mb_mvd( h, &s, 0, 4*i8, 2 ); + if( x264_mb_type_list0_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 ); + if( x264_mb_type_list1_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 1, 4*i8, 4>>b_8x16 ); } else if( i_mb_type == B_8x8 ) { - bs_write_ue( &s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i8] ] ); - - if( h->mb.pic.i_fref[0] > 1 - && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] ) - bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); - if( h->mb.pic.i_fref[1] > 1 - && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] ) - bs_write_te( &s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[4*i8]] ); - cavlc_mb8x8_mvd( h, &s, 0, i8 ); cavlc_mb8x8_mvd( h, &s, 1, i8 ); } diff --git a/encoder/encoder.c b/encoder/encoder.c index 5103f090..f1c7457f 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -474,8 +474,7 @@ static int x264_validate_parameters( x264_t *h ) if( h->param.analyse.i_me_method == X264_ME_TESA && (h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1) ) h->param.analyse.i_me_method = X264_ME_ESA; - h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 7 ); - h->param.analyse.b_bframe_rdo = h->param.analyse.b_bframe_rdo && h->param.analyse.i_subpel_refine >= 6; + h->param.analyse.i_subpel_refine = x264_clip3( h->param.analyse.i_subpel_refine, 1, 9 ); h->param.analyse.b_mixed_references = h->param.analyse.b_mixed_references && h->param.i_frame_reference > 1; h->param.analyse.inter &= X264_ANALYSE_PSUB16x16|X264_ANALYSE_PSUB8x8|X264_ANALYSE_BSUB16x16| X264_ANALYSE_I4x4|X264_ANALYSE_I8x8; @@ -591,7 +590,6 @@ static int x264_validate_parameters( x264_t *h ) BOOLIFY( b_interlaced ); BOOLIFY( analyse.b_transform_8x8 ); BOOLIFY( analyse.i_direct_8x8_inference ); - BOOLIFY( analyse.b_bidir_me ); BOOLIFY( analyse.b_chroma_me ); BOOLIFY( analyse.b_fast_pskip ); BOOLIFY( rc.b_stat_write ); @@ -816,8 +814,6 @@ int x264_encoder_reconfig( x264_t *h, x264_param_t *param ) COPY( analyse.i_noise_reduction ); COPY( analyse.i_subpel_refine ); COPY( analyse.i_trellis ); - COPY( analyse.b_bidir_me ); - COPY( analyse.b_bframe_rdo ); COPY( analyse.b_chroma_me ); COPY( analyse.b_dct_decimate ); COPY( analyse.b_fast_pskip ); diff --git a/encoder/me.c b/encoder/me.c index 9eb108ae..25e1195a 100644 --- a/encoder/me.c +++ b/encoder/me.c @@ -29,8 +29,8 @@ * subpel_iters[i_subpel_refine] = { refine_hpel, refine_qpel, me_hpel, me_qpel } * where me_* are the number of EPZS iterations run on all candidate block types, * and refine_* are run only on the winner. - * the subme=7 values are much higher because any amount of satd search makes - * up its time by reducing the number of rd iterations. */ + * the subme=8,9 values are much higher because any amount of satd search makes + * up its time by reducing the number of qpel-rd iterations. */ static const int subpel_iterations[][4] = {{1,0,0,0}, {1,1,0,0}, @@ -39,6 +39,8 @@ static const int subpel_iterations[][4] = {0,2,1,1}, {0,2,1,2}, {0,0,2,2}, + {0,0,2,2}, + {0,0,4,10}, {0,0,4,10}}; /* (x-1)%6 */ @@ -797,6 +799,8 @@ static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_ite BIME_CACHE(a,b) \ BIME_CACHE(-(a),-(b)) +#define SATD_THRESH 17/16 + #define COST_BIMV_SATD( m0x, m0y, m1x, m1y ) \ if( pass == 0 || !((visited[(m0x)&7][(m0y)&7][(m1x)&7] & (1<<((m1y)&7)))) ) \ { \ @@ -808,7 +812,27 @@ if( pass == 0 || !((visited[(m0x)&7][(m0y)&7][(m1x)&7] & (1<<((m1y)&7)))) ) \ cost = h->pixf.mbcmp[i_pixel]( m0->p_fenc[0], FENC_STRIDE, pix, bw ) \ + p_cost_m0x[ m0x ] + p_cost_m0y[ m0y ] \ + p_cost_m1x[ m1x ] + p_cost_m1y[ m1y ]; \ - if( cost < bcost ) \ + if( rd ) \ + { \ + if( cost < bcost * SATD_THRESH ) \ + { \ + uint64_t costrd; \ + if( cost < bcost ) \ + bcost = cost; \ + *(uint32_t*)cache0_mv = *(uint32_t*)cache0_mv2 = pack16to32_mask(m0x,m0y); \ + *(uint32_t*)cache1_mv = *(uint32_t*)cache1_mv2 = pack16to32_mask(m1x,m1y); \ + costrd = x264_rd_cost_part( h, i_lambda2, i8, m0->i_pixel ); \ + if( costrd < bcostrd ) \ + {\ + bcostrd = costrd;\ + bm0x = m0x; \ + bm0y = m0y; \ + bm1x = m1x; \ + bm1y = m1y; \ + }\ + } \ + } \ + else if( cost < bcost ) \ { \ bcost = cost; \ bm0x = m0x; \ @@ -831,8 +855,13 @@ if( pass == 0 || !((visited[(m0x)&7][(m0y)&7][(m1x)&7] & (1<<((m1y)&7)))) ) \ CHECK_BIDIR2(c,d,a,b) \ CHECK_BIDIR2(d,a,b,c) -int x264_me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight ) +static void ALWAYS_INLINE x264_me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2, int rd ) { + static const int pixel_mv_offs[] = { 0, 4, 4*8, 0 }; + int16_t *cache0_mv = h->mb.cache.mv[0][x264_scan8[i8*4]]; + int16_t *cache0_mv2 = cache0_mv + pixel_mv_offs[m0->i_pixel]; + int16_t *cache1_mv = h->mb.cache.mv[1][x264_scan8[i8*4]]; + int16_t *cache1_mv2 = cache1_mv + pixel_mv_offs[m0->i_pixel]; const int i_pixel = m0->i_pixel; const int bw = x264_pixel_size[i_pixel].w; const int bh = x264_pixel_size[i_pixel].h; @@ -853,17 +882,20 @@ int x264_me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight int bm1y = m1->mv[1], om1y = bm1y; int bcost = COST_MAX; int pass = 0; + uint64_t bcostrd = COST_MAX64; + /* each byte of visited represents 8 possible m1y positions, so a 4D array isn't needed */ DECLARE_ALIGNED_16( uint8_t visited[8][8][8] ); + + if( bm0y > h->mb.mv_max_spel[1] - 8 || + bm1y > h->mb.mv_max_spel[1] - 8 ) + return; + h->mc.memzero_aligned( visited, sizeof(visited) ); BIME_CACHE( 0, 0 ); CHECK_BIDIR( 0, 0, 0, 0 ); - if( bm0y > h->mb.mv_max_spel[1] - 8 || - bm1y > h->mb.mv_max_spel[1] - 8 ) - return bcost; - for( pass = 0; pass < 8; pass++ ) { /* check all mv pairs that differ in at most 2 components from the current mvs. */ @@ -897,7 +929,16 @@ int x264_me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight m0->mv[1] = bm0y; m1->mv[0] = bm1x; m1->mv[1] = bm1y; - return bcost; +} + +void x264_me_refine_bidir_satd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight ) +{ + x264_me_refine_bidir( h, m0, m1, i_weight, 0, 0, 0 ); +} + +void x264_me_refine_bidir_rd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2 ) +{ + x264_me_refine_bidir( h, m0, m1, i_weight, i8, i_lambda2, 1 ); } #undef COST_MV_SATD @@ -921,13 +962,11 @@ int x264_me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight } \ } -#define SATD_THRESH 17/16 - -void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i8 ) +void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i8, int i_list ) { // don't have to fill the whole mv cache rectangle static const int pixel_mv_offs[] = { 0, 4, 4*8, 0 }; - int16_t *cache_mv = h->mb.cache.mv[0][x264_scan8[i8*4]]; + int16_t *cache_mv = h->mb.cache.mv[i_list][x264_scan8[i8*4]]; int16_t *cache_mv2 = cache_mv + pixel_mv_offs[m->i_pixel]; const int16_t *p_cost_mvx, *p_cost_mvy; const int bw = x264_pixel_size[m->i_pixel].w>>2; @@ -947,7 +986,7 @@ void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i8 ) int satds[8]; if( m->i_pixel != PIXEL_16x16 && i8 != 0 ) - x264_mb_predict_mv( h, 0, i8*4, bw, m->mvp ); + x264_mb_predict_mv( h, i_list, i8*4, bw, m->mvp ); pmx = m->mvp[0]; pmy = m->mvp[1]; p_cost_mvx = m->p_cost_mv - pmx; @@ -999,7 +1038,7 @@ void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i8 ) m->cost = bcost; m->mv[0] = bmx; m->mv[1] = bmy; - x264_macroblock_cache_mv ( h, 2*(i8&1), i8&2, bw, bh, 0, pack16to32_mask(bmx, bmy) ); - x264_macroblock_cache_mvd( h, 2*(i8&1), i8&2, bw, bh, 0, pack16to32_mask(bmx - pmx, bmy - pmy) ); + x264_macroblock_cache_mv ( h, 2*(i8&1), i8&2, bw, bh, i_list, pack16to32_mask(bmx, bmy) ); + x264_macroblock_cache_mvd( h, 2*(i8&1), i8&2, bw, bh, i_list, pack16to32_mask(bmx - pmx, bmy - pmy) ); } diff --git a/encoder/me.h b/encoder/me.h index 655c2a14..7b94a197 100644 --- a/encoder/me.h +++ b/encoder/me.h @@ -53,8 +53,9 @@ static inline void x264_me_search( x264_t *h, x264_me_t *m, int16_t (*mvc)[2], i { x264_me_search_ref( h, m, mvc, i_mvc, NULL ); } void x264_me_refine_qpel( x264_t *h, x264_me_t *m ); -void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i8 ); -int x264_me_refine_bidir( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight ); +void x264_me_refine_qpel_rd( x264_t *h, x264_me_t *m, int i_lambda2, int i8, int i_list ); +void x264_me_refine_bidir_rd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight, int i8, int i_lambda2 ); +void x264_me_refine_bidir_satd( x264_t *h, x264_me_t *m0, x264_me_t *m1, int i_weight ); uint64_t x264_rd_cost_part( x264_t *h, int i_lambda2, int i8, int i_pixel ); extern uint16_t *x264_cost_mv_fpel[52][4]; diff --git a/x264.c b/x264.c index efca7024..3bc4028a 100644 --- a/x264.c +++ b/x264.c @@ -248,16 +248,21 @@ static void Help( x264_param_t *defaults, int b_longhelp ) H0( " --merange Maximum motion vector search range [%d]\n", defaults->analyse.i_me_range ); H1( " --mvrange Maximum motion vector length [-1 (auto)]\n" ); H1( " --mvrange-thread Minimum buffer between threads [-1 (auto)]\n" ); - H0( " -m, --subme Subpixel motion estimation and partition\n" - " decision quality: 1=fast, 7=best. [%d]\n", defaults->analyse.i_subpel_refine ); - H0( " --b-rdo RD based mode decision for B-frames. Requires subme 6.\n" ); + H0( " -m, --subme Subpixel motion estimation and mode decision [%d]\n", defaults->analyse.i_subpel_refine ); + H1( " - 1: SAD mode decision, one qpel iteration\n" + " - 2: SATD mode decision\n" + " - 3-5: Progressively more qpel\n" + " - 6: RD mode decision for I/P-frames\n" + " - 7: RD mode decision for all frames\n" + " - 8: RD refinement for I/P-frames\n" + " - 9: RD refinement for all frames\n" ); + else H0( " decision quality: 1=fast, 9=best.\n" ); H0( " --psy-rd Strength of psychovisual optimization [\"%.1f:%.1f\"]\n" - " #1: RDO (requires subme>=6)\n" + " #1: RD (requires subme>=6)\n" " #2: Trellis (requires trellis, experimental)\n", defaults->analyse.f_psy_rd, defaults->analyse.f_psy_trellis ); H0( " --mixed-refs Decide references on a per partition basis\n" ); H1( " --no-chroma-me Ignore chroma in motion estimation\n" ); - H1( " --bime Jointly optimize both MVs in B-frames\n" ); H0( " -8, --8x8dct Adaptive spatial transform size\n" ); H0( " -t, --trellis Trellis RD quantization. Requires CABAC. [%d]\n" " - 0: disabled\n" @@ -425,10 +430,8 @@ static int Parse( int argc, char **argv, { "mvrange-thread", required_argument, NULL, 0 }, { "subme", required_argument, NULL, 'm' }, { "psy-rd", required_argument, NULL, 0 }, - { "b-rdo", no_argument, NULL, 0 }, { "mixed-refs", no_argument, NULL, 0 }, { "no-chroma-me", no_argument, NULL, 0 }, - { "bime", no_argument, NULL, 0 }, { "8x8dct", no_argument, NULL, '8' }, { "trellis", required_argument, NULL, 't' }, { "no-fast-pskip", no_argument, NULL, 0 }, diff --git a/x264.h b/x264.h index 4d81bced..afb8a419 100644 --- a/x264.h +++ b/x264.h @@ -35,7 +35,7 @@ #include -#define X264_BUILD 64 +#define X264_BUILD 65 /* x264_t: * opaque handler for encoder */ @@ -233,9 +233,7 @@ typedef struct x264_param_t int i_mv_range; /* maximum length of a mv (in pixels). -1 = auto, based on level */ int i_mv_range_thread; /* minimum space between threads. -1 = auto, based on number of threads. */ int i_subpel_refine; /* subpixel motion estimation quality */ - int b_bidir_me; /* jointly optimize both MVs in B-frames */ int b_chroma_me; /* chroma ME for subpel and mode decision in P-frames */ - int b_bframe_rdo; /* RD based mode decision for B-frames */ int b_mixed_references; /* allow each mb partition in P-frames to have it's own reference number */ int i_trellis; /* trellis RD quantization */ int b_fast_pskip; /* early SKIP detection on P-frames */ -- 2.47.3