Commit 60455fff authored by Fiona Glaser's avatar Fiona Glaser

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.
parent 9b10152f
...@@ -112,10 +112,10 @@ SRC2 = $(SRCS) $(SRCCLI) ...@@ -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 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 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 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 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 -m6 -r4 --me esa -8 -t2 -A all --mixed-refs --b-rdo --bime OPT4 = --crf 22 -b3 -m7 -r4 --me esa -8 -t2 -A all --mixed-refs
OPT5 = --frames 50 --crf 24 -b3 -m7 -r3 --me tesa -8 -t1 --mixed-refs --b-rdo --bime OPT5 = --frames 50 --crf 24 -b3 -m9 -r3 --me tesa -8 -t1 --mixed-refs
OPT6 = --frames 50 -q0 -m6 -r2 --me hex -Aall OPT6 = --frames 50 -q0 -m9 -r2 --me hex -Aall
OPT7 = --frames 50 -q0 -m2 -r1 --me hex --no-cabac OPT7 = --frames 50 -q0 -m2 -r1 --me hex --no-cabac
ifeq (,$(VIDS)) ifeq (,$(VIDS))
......
...@@ -487,12 +487,8 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) ...@@ -487,12 +487,8 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value )
p->analyse.f_psy_trellis = 0; p->analyse.f_psy_trellis = 0;
} }
} }
OPT("bime")
p->analyse.b_bidir_me = atobool(value);
OPT("chroma-me") OPT("chroma-me")
p->analyse.b_chroma_me = atobool(value); p->analyse.b_chroma_me = atobool(value);
OPT2("b-rdo", "brdo")
p->analyse.b_bframe_rdo = atobool(value);
OPT("mixed-refs") OPT("mixed-refs")
p->analyse.b_mixed_references = atobool(value); p->analyse.b_mixed_references = atobool(value);
OPT("trellis") OPT("trellis")
...@@ -842,7 +838,6 @@ char *x264_param2string( x264_param_t *p, int b_res ) ...@@ -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, " me=%s", x264_motion_est_names[ p->analyse.i_me_method ] );
s += sprintf( s, " subme=%d", p->analyse.i_subpel_refine ); 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, " 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, " mixed_ref=%d", p->analyse.b_mixed_references );
s += sprintf( s, " me_range=%d", p->analyse.i_me_range ); s += sprintf( s, " me_range=%d", p->analyse.i_me_range );
s += sprintf( s, " chroma_me=%d", p->analyse.b_chroma_me ); 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 ) ...@@ -859,10 +854,9 @@ char *x264_param2string( x264_param_t *p, int b_res )
s += sprintf( s, " bframes=%d", p->i_bframe ); s += sprintf( s, " bframes=%d", p->i_bframe );
if( 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->b_bframe_pyramid, p->i_bframe_adaptive, p->i_bframe_bias,
p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred, p->analyse.i_direct_mv_pred, p->analyse.b_weighted_bipred );
p->analyse.b_bidir_me );
} }
s += sprintf( s, " keyint=%d keyint_min=%d scenecut=%d%s", s += sprintf( s, " keyint=%d keyint_min=%d scenecut=%d%s",
......
...@@ -76,7 +76,7 @@ typedef struct ...@@ -76,7 +76,7 @@ typedef struct
int i_lambda2; int i_lambda2;
int i_qp; int i_qp;
int16_t *p_cost_mv; int16_t *p_cost_mv;
int b_mbrd; int i_mbrd;
/* I: Intra part */ /* I: Intra part */
...@@ -203,19 +203,19 @@ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ) ...@@ -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 ) 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 */ /* conduct the analysis using this lamda and QP */
a->i_qp = h->mb.i_qp = i_qp; a->i_qp = h->mb.i_qp = i_qp;
h->mb.i_chroma_qp = h->chroma_qp_table[i_qp]; h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
a->i_lambda = x264_lambda_tab[i_qp]; a->i_lambda = x264_lambda_tab[i_qp];
a->i_lambda2 = x264_lambda2_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.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.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
&& h->mb.i_subpel_refine >= 5; && 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_transform_8x8 = 0;
h->mb.b_noise_reduction = 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 ) ...@@ -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; a->i_satd_i8x8chroma = COST_MAX;
/* non-RD PCM decision is inaccurate (as is psy-rd), so don't do it */ /* 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; a->b_fast_intra = 0;
h->mb.i_skip_intra = h->mb.i_skip_intra =
h->mb.b_lossless ? 0 : h->mb.b_lossless ? 0 :
a->b_mbrd ? 2 : a->i_mbrd ? 2 :
!h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction; !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
/* II: Inter part P/B frame */ /* 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_ ...@@ -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] ); 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]; 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; int i_cost = 0;
b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[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_ ...@@ -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; a->i_satd_i8x8 = COST_MAX;
i_cost = i_cost * 4/(idx+1); 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; return;
} }
...@@ -738,7 +738,7 @@ static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_ ...@@ -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_cost;
int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 ); 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]; 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_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
i_cost = a->i_lambda * 24; /* from JVT (SATD0) */ 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 ) ...@@ -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 ); assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
h->mb.i_type = P_L0; h->mb.i_type = P_L0;
if( a->b_mbrd ) if( a->i_mbrd )
{ {
x264_mb_cache_fenc_satd( h ); 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 ) 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 ...@@ -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]; const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
int i; int i;
if( IS_INTRA(h->mb.i_type) )
return;
switch( h->mb.i_partition ) switch( h->mb.i_partition )
{ {
case D_16x16: case D_16x16:
if( h->mb.i_type == B_BI_BI ) 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; break;
case D_16x8: case D_16x8:
for( i=0; i<2; i++ ) for( i=0; i<2; i++ )
if( a->i_mb_partition16x8[i] == D_BI_8x8 ) 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; break;
case D_8x16: case D_8x16:
for( i=0; i<2; i++ ) for( i=0; i<2; i++ )
if( a->i_mb_partition8x16[i] == D_BI_8x8 ) 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; break;
case D_8x8: case D_8x8:
for( i=0; i<4; i++ ) for( i=0; i<4; i++ )
if( h->mb.i_sub_partition[i] == D_BI_8x8 ) 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; break;
} }
} }
...@@ -2120,10 +2123,10 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2120,10 +2123,10 @@ void x264_macroblock_analyse( x264_t *h )
/*--------------------------- Do the analysis ---------------------------*/ /*--------------------------- Do the analysis ---------------------------*/
if( h->sh.i_type == SLICE_TYPE_I ) if( h->sh.i_type == SLICE_TYPE_I )
{ {
if( analysis.b_mbrd ) if( analysis.i_mbrd )
x264_mb_cache_fenc_satd( h ); x264_mb_cache_fenc_satd( h );
x264_mb_analyse_intra( h, &analysis, COST_MAX ); x264_mb_analyse_intra( h, &analysis, COST_MAX );
if( analysis.b_mbrd ) if( analysis.i_mbrd )
x264_intra_rd( h, &analysis, COST_MAX ); x264_intra_rd( h, &analysis, COST_MAX );
i_cost = analysis.i_satd_i16x16; i_cost = analysis.i_satd_i16x16;
...@@ -2133,7 +2136,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2133,7 +2136,7 @@ void x264_macroblock_analyse( x264_t *h )
if( analysis.i_satd_pcm < i_cost ) if( analysis.i_satd_pcm < i_cost )
h->mb.i_type = I_PCM; 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 ); x264_intra_rd_refine( h, &analysis );
} }
else if( h->sh.i_type == SLICE_TYPE_P ) else if( h->sh.i_type == SLICE_TYPE_P )
...@@ -2245,7 +2248,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2245,7 +2248,7 @@ void x264_macroblock_analyse( x264_t *h )
/* refine qpel */ /* refine qpel */
//FIXME mb_type costs? //FIXME mb_type costs?
if( analysis.b_mbrd ) if( analysis.i_mbrd )
{ {
/* refine later */ /* refine later */
} }
...@@ -2324,7 +2327,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2324,7 +2327,7 @@ void x264_macroblock_analyse( x264_t *h )
analysis.i_satd_i8x8, analysis.i_satd_i8x8,
analysis.i_satd_i4x4 ); 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) ); x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
i_type = P_L0; i_type = P_L0;
...@@ -2355,7 +2358,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2355,7 +2358,7 @@ void x264_macroblock_analyse( x264_t *h )
h->stat.frame.i_inter_cost += i_cost; h->stat.frame.i_inter_cost += i_cost;
h->stat.frame.i_mbs_analysed++; 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 ) ) if( IS_INTRA( h->mb.i_type ) )
{ {
...@@ -2364,7 +2367,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2364,7 +2367,7 @@ void x264_macroblock_analyse( x264_t *h )
else if( i_partition == D_16x16 ) else if( i_partition == D_16x16 )
{ {
x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref ); 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 ) else if( i_partition == D_16x8 )
{ {
...@@ -2372,8 +2375,8 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -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; 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, 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_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[0], analysis.i_lambda2, 0, 0 );
x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 ); x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2, 0 );
} }
else if( i_partition == D_8x16 ) else if( i_partition == D_8x16 )
{ {
...@@ -2381,8 +2384,8 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -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; 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, 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_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[0], analysis.i_lambda2, 0, 0 );
x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 ); x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1, 0 );
} }
else if( i_partition == D_8x8 ) else if( i_partition == D_8x8 )
{ {
...@@ -2390,7 +2393,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2390,7 +2393,7 @@ void x264_macroblock_analyse( x264_t *h )
x264_analyse_update_cache( h, &analysis ); x264_analyse_update_cache( h, &analysis );
for( i8x8 = 0; i8x8 < 4; i8x8++ ) for( i8x8 = 0; i8x8 < 4; i8x8++ )
if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 ) 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 ) ...@@ -2400,7 +2403,7 @@ void x264_macroblock_analyse( x264_t *h )
int i_bskip_cost = COST_MAX; int i_bskip_cost = COST_MAX;
int b_skip = 0; int b_skip = 0;
if( analysis.b_mbrd ) if( analysis.i_mbrd )
x264_mb_cache_fenc_satd( h ); x264_mb_cache_fenc_satd( h );
h->mb.i_type = B_SKIP; h->mb.i_type = B_SKIP;
...@@ -2436,7 +2439,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2436,7 +2439,7 @@ void x264_macroblock_analyse( x264_t *h )
{ {
/* chance of skip is too small to bother */ /* chance of skip is too small to bother */
} }
else if( analysis.b_mbrd ) else if( analysis.i_mbrd )
{ {
i_bskip_cost = ssd_mb( h ); i_bskip_cost = ssd_mb( h );
/* 6 = minimum cavlc cost of a non-skipped MB */ /* 6 = minimum cavlc cost of a non-skipped MB */
...@@ -2474,7 +2477,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -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_cost16x16bi, i_type, B_BI_BI );
COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT ); 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 ); x264_mb_analyse_b_rd( h, &analysis, i_cost );
if( i_bskip_cost < analysis.i_rd16x16direct && if( i_bskip_cost < analysis.i_rd16x16direct &&
...@@ -2516,7 +2519,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2516,7 +2519,7 @@ void x264_macroblock_analyse( x264_t *h )
} }
} }
if( analysis.b_mbrd ) if( analysis.i_mbrd )
{ {
/* refine later */ /* refine later */
} }
...@@ -2599,7 +2602,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2599,7 +2602,7 @@ void x264_macroblock_analyse( x264_t *h )
} }
} }
if( analysis.b_mbrd ) if( analysis.i_mbrd )
{ {
i_satd_inter = i_cost; i_satd_inter = i_cost;
x264_mb_analyse_b_rd( h, &analysis, i_satd_inter ); x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
...@@ -2620,7 +2623,7 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2620,7 +2623,7 @@ void x264_macroblock_analyse( x264_t *h )
x264_mb_analyse_intra( h, &analysis, i_satd_inter ); 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_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
x264_intra_rd( h, &analysis, i_satd_inter * 17/16 ); x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
...@@ -2634,16 +2637,70 @@ void x264_macroblock_analyse( x264_t *h ) ...@@ -2634,16 +2637,70 @@ void x264_macroblock_analyse( x264_t *h )
h->mb.i_type = i_type; h->mb.i_type = i_type;
h->mb.i_partition = i_partition; 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 ); x264_intra_rd_refine( h, &analysis );
else if( h->param.analyse.b_bidir_me ) if( h->mb.i_subpel_refine >= 5 )
refine_bidir( h, &analysis ); 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 ); x264_analyse_update_cache( h, &analysis );
if( !analysis.b_mbrd ) if( !analysis.i_mbrd )
x264_mb_analyse_transform( h ); x264_mb_analyse_transform( h );
h->mb.b_trellis = h->param.analyse.i_trellis; h->mb.b_trellis = h->param.analyse.i_trellis;
......
...@@ -1026,47 +1026,34 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) ...@@ -1026,47 +1026,34 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb )
/***************************************************************************** /*****************************************************************************
* RD only; doesn't generate a valid bitstream * 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 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 * works on all partition sizes except 16x16
* for sub8x8, call once per 8x8 block * 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 ) 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; const int i_mb_type = h->mb.i_type;
int b_8x16 = h->mb.i_partition == D_8x16;
int j; int j;
if( i_mb_type == P_8x8 ) 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 ); x264_cabac_mb8x8_mvd( h, cb, 0, i8 );
}
else if( i_mb_type == P_L0 ) else if( i_mb_type == P_L0 )
x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4>>b_8x16, 2<<b_8x16 );
else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
{ {
if( h->mb.pic.i_fref[0] > 1 ) 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 );
x264_cabac_mb_ref( h, cb, 0, 4*i8 ); if( x264_mb_type_list1_table[ i_mb_type ][!!i8] ) x264_cabac_mb_mvd( h, cb, 1, 4*i8, 4>>b_8x16, 2<<b_8x16 );
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 );
} }
else if( i_mb_type == B_8x8 ) else if( i_mb_type == B_8x8 )
{ {
x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[i8] ); x264_cabac_mb_sub_b_partition( cb, 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] ] )
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, 0, i8 );
x264_cabac_mb8x8_mvd( h, cb, 1, i8 ); x264_cabac_mb8x8_mvd( h, cb, 1, i8 );
} }
else 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; return;
} }
......
...@@ -639,6 +639,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) ...@@ -639,6 +639,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s )
/***************************************************************************** /*****************************************************************************
* RD only; doesn't generate a valid bitstream * 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 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 * works on all partition sizes except 16x16
* for sub8x8, call once per 8x8 block * 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 ) ...@@ -646,37 +647,22 @@ static int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel )
{ {
bs_t s; bs_t s;
const int i_mb_type = h->mb.i_type; const int i_mb_type = h->mb.i_type;
int b_8x16 = h->mb.i_partition == D_8x16;
int j; int j;
s.i_bits_encoded = 0; s.i_bits_encoded = 0;
if( i_mb_type == P_8x8 ) 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 ); cavlc_mb8x8_mvd( h, &s, 0, i8 );
}
else if( i_mb_type == P_L0 ) 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 ) if( x264_mb_type_list0_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 0, 4*i8, 4>>b_8x16 );
bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); if( x264_mb_type_list1_table[ i_mb_type ][!!i8] ) cavlc_mb_mvd( h, &s, 1, 4*i8, 4>>b_8x16 );
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 );
} }
else if( i_mb_type == B_8x8 ) 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, 0, i8 );
cavlc_mb8x8_mvd( h, &s, 1, i8 ); cavlc_mb8x8_mvd( h, &s, 1, i8 );
} }
......
...@@ -474,8 +474,7 @@ static int x264_validate_parameters( x264_t *h ) ...@@ -474,8 +474,7 @@ static int x264_validate_parameters( x264_t *h )
if( h->param.analyse.i_me_method == X264_ME_TESA && if( h->param.analyse.i_me_method == X264_ME_TESA &&
(h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1) )