From fd9ab6cee508192fe53ffaa52d3d7586004808fa Mon Sep 17 00:00:00 2001 From: Loren Merritt Date: Sun, 6 Jan 2008 08:15:04 +0000 Subject: [PATCH] convert absolute difference of sums from mmx to sse2 convert mv bits cost and ads threshold from C to sse2 convert bytemask-to-list from C to scalar asm 1.6x faster me=esa (x86_64) or 1.3x faster (x86_32). (times consider only motion estimation. overall encode speedup may vary.) git-svn-id: svn://svn.videolan.org/x264/trunk@717 df754926-b1dd-0310-bc7b-ec298dee348c --- common/amd64/pixel-a.asm | 228 +++++++++++++++++++++++++++++++----- common/i386/pixel-a.asm | 246 ++++++++++++++++++++++++++++++++------- common/i386/pixel.h | 19 ++- common/pixel.c | 67 +++++++---- common/pixel.h | 6 +- encoder/analyse.c | 17 ++- encoder/me.c | 54 ++++----- encoder/me.h | 2 + tools/checkasm.c | 21 ++-- 9 files changed, 519 insertions(+), 141 deletions(-) diff --git a/common/amd64/pixel-a.asm b/common/amd64/pixel-a.asm index 3b07a989..6de7ffeb 100644 --- a/common/amd64/pixel-a.asm +++ b/common/amd64/pixel-a.asm @@ -1045,9 +1045,82 @@ cglobal x264_intra_satd_x3_8x8c_mmxext +; int x264_pixel_ads_mvs( int16_t *mvs, uint8_t *masks, int width ) +; { +; int nmv=0, i, j; +; *(uint32_t*)(masks+width) = 0; +; for( i=0; iname[PIXEL_4x8] = x264_pixel_##name##_4x8##cpu;\ pixf->name[PIXEL_4x4] = x264_pixel_##name##_4x4##cpu; +#define INIT_ADS( cpu ) \ + pixf->ads[PIXEL_16x16] = x264_pixel_ads4##cpu;\ + pixf->ads[PIXEL_16x8] = x264_pixel_ads2##cpu;\ + pixf->ads[PIXEL_8x8] = x264_pixel_ads1##cpu; + INIT7( sad, ); INIT7( sad_x3, ); INIT7( sad_x4, ); INIT7( ssd, ); INIT7( satd, ); INIT4( sa8d, ); + INIT_ADS( ); pixf->ssim_4x4x2_core = ssim_4x4x2_core; pixf->ssim_end4 = ssim_end4; - pixf->ads[PIXEL_16x16] = pixel_ads4; - pixf->ads[PIXEL_16x8] = pixel_ads2; - pixf->ads[PIXEL_8x8] = pixel_ads1; - #ifdef HAVE_MMX if( cpu&X264_CPU_MMX ) { @@ -485,10 +505,7 @@ void x264_pixel_init( int cpu, x264_pixel_function_t *pixf ) INIT7( sad_x3, _mmxext ); INIT7( sad_x4, _mmxext ); INIT7( satd, _mmxext ); - - pixf->ads[PIXEL_16x16] = x264_pixel_ads4_mmxext; - pixf->ads[PIXEL_16x8 ] = x264_pixel_ads2_mmxext; - pixf->ads[PIXEL_8x8 ] = x264_pixel_ads1_mmxext; + INIT_ADS( _mmxext ); #ifdef ARCH_X86 pixf->sa8d[PIXEL_16x16] = x264_pixel_sa8d_16x16_mmxext; @@ -535,6 +552,7 @@ void x264_pixel_init( int cpu, x264_pixel_function_t *pixf ) INIT2( sad_x3, _sse2 ); INIT2( sad_x4, _sse2 ); INIT5( satd, _sse2 ); + INIT_ADS( _sse2 ); #ifdef ARCH_X86 if( cpu&X264_CPU_CACHELINE_SPLIT ) @@ -570,6 +588,7 @@ void x264_pixel_init( int cpu, x264_pixel_function_t *pixf ) if( cpu&X264_CPU_SSSE3 ) { INIT5( satd, _ssse3 ); + INIT_ADS( _ssse3 ); #ifdef ARCH_X86_64 pixf->sa8d[PIXEL_16x16]= x264_pixel_sa8d_16x16_ssse3; pixf->sa8d[PIXEL_8x8] = x264_pixel_sa8d_8x8_ssse3; diff --git a/common/pixel.h b/common/pixel.h index d35d71f4..1a8d0ac2 100644 --- a/common/pixel.h +++ b/common/pixel.h @@ -80,9 +80,9 @@ typedef struct x264_pixel_cmp_x4_t sad_x4[7]; /* abs-diff-sum for successive elimination. - * may round width up to a multiple of 8. */ - void (*ads[7])( int enc_dc[4], uint16_t *sums, int delta, - uint16_t *res, int width ); + * may round width up to a multiple of 16. */ + int (*ads[7])( int enc_dc[4], uint16_t *sums, int delta, + uint16_t *cost_mvx, int16_t *mvs, int width, int thresh ); /* calculate satd of V, H, and DC modes. * may be NULL, in which case just use pred+satd instead. */ diff --git a/encoder/analyse.c b/encoder/analyse.c index 4532bf22..84074a45 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -167,16 +167,18 @@ static const int i_sub_mb_p_cost_table[4] = { static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a ); +uint16_t *x264_cost_mv_fpel[52][4]; + /* initialize an array of lambda*nbits for all possible mvs */ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ) { static int16_t *p_cost_mv[52]; + int i, j; if( !p_cost_mv[a->i_qp] ) { /* could be faster, but isn't called many times */ /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */ - int i; p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) ); p_cost_mv[a->i_qp] += 2*4*2048; for( i = 0; i <= 2*4*2048; i++ ) @@ -185,8 +187,19 @@ static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a ) p_cost_mv[a->i_qp][i] = a->i_lambda * bs_size_se( i ); } } - a->p_cost_mv = p_cost_mv[a->i_qp]; + + /* FIXME is this useful for all me methods? */ + if( h->param.analyse.i_me_method == X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] ) + { + for( j=0; j<4; j++ ) + { + x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) ); + x264_cost_mv_fpel[a->i_qp][j] += 2*2048; + for( i = -2*2048; i <= 2*2048; i++ ) + x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j]; + } + } } static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) diff --git a/encoder/me.c b/encoder/me.c index 66017c76..52982a41 100644 --- a/encoder/me.c +++ b/encoder/me.c @@ -101,22 +101,19 @@ static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_ite COPY3_IF_LT( bcost, costs[3], bmx, omx+(m3x), bmy, omy+(m3y) );\ } -#define COST_MV_X4_ABS( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )\ +#define COST_MV_X3_ABS( m0x, m0y, m1x, m1y, m2x, m2y )\ {\ - h->pixf.sad_x4[i_pixel]( m->p_fenc[0],\ + h->pixf.sad_x3[i_pixel]( m->p_fenc[0],\ p_fref + (m0x) + (m0y)*m->i_stride[0],\ p_fref + (m1x) + (m1y)*m->i_stride[0],\ p_fref + (m2x) + (m2y)*m->i_stride[0],\ - p_fref + (m3x) + (m3y)*m->i_stride[0],\ m->i_stride[0], costs );\ - costs[0] += p_cost_mvx[m0x<<2]; /* no cost_mvy */\ - costs[1] += p_cost_mvx[m1x<<2];\ - costs[2] += p_cost_mvx[m2x<<2];\ - costs[3] += p_cost_mvx[m3x<<2];\ + costs[0] += p_cost_mvx[(m0x)<<2]; /* no cost_mvy */\ + costs[1] += p_cost_mvx[(m1x)<<2];\ + costs[2] += p_cost_mvx[(m2x)<<2];\ COPY3_IF_LT( bcost, costs[0], bmx, m0x, bmy, m0y );\ COPY3_IF_LT( bcost, costs[1], bmx, m1x, bmy, m1y );\ COPY3_IF_LT( bcost, costs[2], bmx, m2x, bmy, m2y );\ - COPY3_IF_LT( bcost, costs[3], bmx, m3x, bmy, m3y );\ } /* 1 */ @@ -456,11 +453,14 @@ me_hex2: { const int min_x = X264_MAX( bmx - i_me_range, mv_x_min ); const int min_y = X264_MAX( bmy - i_me_range, mv_y_min ); - const int max_x = ((X264_MIN( bmx + i_me_range, mv_x_max ) - min_x + 3) & ~3) + min_x - 1; + const int max_x = X264_MIN( bmx + i_me_range, mv_x_max ); const int max_y = X264_MIN( bmy + i_me_range, mv_y_max ); - int mx, my; + /* SEA is fastest in multiples of 4 */ + const int width = (max_x - min_x + 3) & ~3; + int my; #if 0 /* plain old exhaustive search */ + int mx; for( my = min_y; my <= max_y; my++ ) for( mx = min_x; mx <= max_x; mx++ ) COST_MV( mx, my ); @@ -470,10 +470,13 @@ me_hex2: const int stride = m->i_stride[0]; static uint8_t zero[16*16] = {0,}; uint16_t *sums_base = m->integral; - int enc_dc[4]; + DECLARE_ALIGNED( int, enc_dc[4], 16 ); int sad_size = i_pixel <= PIXEL_8x8 ? PIXEL_8x8 : PIXEL_4x4; int delta = x264_pixel_size[sad_size].w; - uint16_t *ads = x264_malloc((max_x-min_x+8) * sizeof(uint16_t)); + int16_t xs_buf[64]; + int16_t *xs = width<=64 ? xs_buf : x264_malloc( width*sizeof(int16_t) ); + int xn; + uint16_t *cost_fpel_mvx = x264_cost_mv_fpel[h->mb.i_qp][-m->mvp[0]&3] + (-m->mvp[0]>>2); h->pixf.sad_x4[sad_size]( zero, m->p_fenc[0], m->p_fenc[0]+delta, m->p_fenc[0]+delta*FENC_STRIDE, m->p_fenc[0]+delta+delta*FENC_STRIDE, @@ -487,29 +490,18 @@ me_hex2: for( my = min_y; my <= max_y; my++ ) { - int mvs[3], i_mvs=0; bcost -= p_cost_mvy[my<<2]; - h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta, - ads, max_x-min_x+1 ); - for( mx = min_x; mx <= max_x; mx++ ) - { - if( ads[mx-min_x] < bcost - p_cost_mvx[mx<<2] ) - { - if( i_mvs == 3 ) - { - COST_MV_X4_ABS( mvs[0],my, mvs[1],my, mvs[2],my, mx,my ); - i_mvs = 0; - } - else - mvs[i_mvs++] = mx; - } - } + xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta, + cost_fpel_mvx+min_x, xs, width, bcost ); + for( i=0; i