From 38861b90aa958e16743920271f5a4f509d12c6ff Mon Sep 17 00:00:00 2001 From: Oskar Arvidsson Date: Thu, 4 Nov 2010 02:13:43 +0100 Subject: [PATCH] x86 asm for high-bit-depth quant ~3.1-4.2x faster than C. --- common/common.h | 10 +- common/quant.c | 42 +++++- common/quant.h | 4 +- common/set.c | 8 +- common/x86/quant-a.asm | 285 +++++++++++++++++++++++++++++++++++------ common/x86/quant.h | 30 +++-- common/x86/x86util.asm | 8 ++ encoder/rdo.c | 4 +- 8 files changed, 325 insertions(+), 66 deletions(-) diff --git a/common/common.h b/common/common.h index 7d571199..82af6f59 100644 --- a/common/common.h +++ b/common/common.h @@ -114,6 +114,7 @@ typedef union { x264_uint128_t i; uint64_t a[2]; uint32_t b[4]; uint16_t c[8]; u typedef uint16_t pixel; typedef uint64_t pixel4; typedef int32_t dctcoef; + typedef uint32_t udctcoef; # define PIXEL_SPLAT_X4(x) ((x)*0x0001000100010001ULL) # define MPIXEL_X4(src) M64(src) @@ -121,6 +122,7 @@ typedef union { x264_uint128_t i; uint64_t a[2]; uint32_t b[4]; uint16_t c[8]; u typedef uint8_t pixel; typedef uint32_t pixel4; typedef int16_t dctcoef; + typedef uint16_t udctcoef; # define PIXEL_SPLAT_X4(x) ((x)*0x01010101U) # define MPIXEL_X4(src) M32(src) @@ -452,10 +454,10 @@ struct x264_t int (*unquant4_mf[4])[16]; /* [4][52][16] */ int (*unquant8_mf[2])[64]; /* [2][52][64] */ /* quantization matrix for deadzone */ - uint16_t (*quant4_mf[4])[16]; /* [4][52][16] */ - uint16_t (*quant8_mf[2])[64]; /* [2][52][64] */ - uint16_t (*quant4_bias[4])[16]; /* [4][52][16] */ - uint16_t (*quant8_bias[2])[64]; /* [2][52][64] */ + udctcoef (*quant4_mf[4])[16]; /* [4][52][16] */ + udctcoef (*quant8_mf[2])[64]; /* [2][52][64] */ + udctcoef (*quant4_bias[4])[16]; /* [4][52][16] */ + udctcoef (*quant8_bias[2])[64]; /* [2][52][64] */ /* mv/ref cost arrays. Indexed by lambda instead of * qp because, due to rounding, some quantizers share diff --git a/common/quant.c b/common/quant.c index 23bf3f51..430d67a6 100644 --- a/common/quant.c +++ b/common/quant.c @@ -46,7 +46,7 @@ nz |= (coef); \ } -static int quant_8x8( dctcoef dct[64], uint16_t mf[64], uint16_t bias[64] ) +static int quant_8x8( dctcoef dct[64], udctcoef mf[64], udctcoef bias[64] ) { int nz = 0; for( int i = 0; i < 64; i++ ) @@ -54,7 +54,7 @@ static int quant_8x8( dctcoef dct[64], uint16_t mf[64], uint16_t bias[64] ) return !!nz; } -static int quant_4x4( dctcoef dct[16], uint16_t mf[16], uint16_t bias[16] ) +static int quant_4x4( dctcoef dct[16], udctcoef mf[16], udctcoef bias[16] ) { int nz = 0; for( int i = 0; i < 16; i++ ) @@ -285,7 +285,41 @@ void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf ) pf->coeff_level_run[ DCT_LUMA_AC] = x264_coeff_level_run15; pf->coeff_level_run[ DCT_LUMA_4x4] = x264_coeff_level_run16; -#if !X264_HIGH_BIT_DEPTH +#if X264_HIGH_BIT_DEPTH +#if HAVE_MMX + if( cpu&X264_CPU_MMX ) + { + pf->quant_4x4 = x264_quant_4x4_mmx; + pf->quant_8x8 = x264_quant_8x8_mmx; + } + if( cpu&X264_CPU_MMXEXT ) + { + pf->quant_2x2_dc = x264_quant_2x2_dc_mmxext; + pf->quant_4x4_dc = x264_quant_4x4_dc_mmxext; + } + if( cpu&X264_CPU_SSE2 ) + { + pf->quant_4x4 = x264_quant_4x4_sse2; + pf->quant_8x8 = x264_quant_8x8_sse2; + pf->quant_2x2_dc = x264_quant_2x2_dc_sse2; + pf->quant_4x4_dc = x264_quant_4x4_dc_sse2; + } + if( cpu&X264_CPU_SSSE3 ) + { + pf->quant_4x4 = x264_quant_4x4_ssse3; + pf->quant_8x8 = x264_quant_8x8_ssse3; + pf->quant_2x2_dc = x264_quant_2x2_dc_ssse3; + pf->quant_4x4_dc = x264_quant_4x4_dc_ssse3; + } + if( cpu&X264_CPU_SSE4 ) + { + pf->quant_2x2_dc = x264_quant_2x2_dc_sse4; + pf->quant_4x4_dc = x264_quant_4x4_dc_sse4; + pf->quant_4x4 = x264_quant_4x4_sse4; + pf->quant_8x8 = x264_quant_8x8_sse4; + } +#endif // HAVE_MMX +#else // !X264_HIGH_BIT_DEPTH #if HAVE_MMX if( cpu&X264_CPU_MMX ) { @@ -424,7 +458,7 @@ void x264_quant_init( x264_t *h, int cpu, x264_quant_function_t *pf ) pf->coeff_last[DCT_LUMA_8x8] = x264_coeff_last64_neon; } #endif -#endif // !X264_HIGH_BIT_DEPTH +#endif // X264_HIGH_BIT_DEPTH pf->coeff_last[ DCT_LUMA_DC] = pf->coeff_last[DCT_LUMA_4x4]; pf->coeff_last[DCT_CHROMA_AC] = pf->coeff_last[ DCT_LUMA_AC]; pf->coeff_level_run[ DCT_LUMA_DC] = pf->coeff_level_run[DCT_LUMA_4x4]; diff --git a/common/quant.h b/common/quant.h index 97d3e588..059a9663 100644 --- a/common/quant.h +++ b/common/quant.h @@ -29,8 +29,8 @@ typedef struct { - int (*quant_8x8)( dctcoef dct[64], uint16_t mf[64], uint16_t bias[64] ); - int (*quant_4x4)( dctcoef dct[16], uint16_t mf[16], uint16_t bias[16] ); + int (*quant_8x8)( dctcoef dct[64], udctcoef mf[64], udctcoef bias[64] ); + int (*quant_4x4)( dctcoef dct[16], udctcoef mf[16], udctcoef bias[16] ); int (*quant_4x4_dc)( dctcoef dct[16], int mf, int bias ); int (*quant_2x2_dc)( dctcoef dct[4], int mf, int bias ); diff --git a/common/set.c b/common/set.c index c0eaf9bd..92b6b7a6 100644 --- a/common/set.c +++ b/common/set.c @@ -100,7 +100,7 @@ int x264_cqm_init( x264_t *h ) } else { - CHECKED_MALLOC( h-> quant4_mf[i], (QP_MAX+1)*size*sizeof(uint16_t) ); + CHECKED_MALLOC( h-> quant4_mf[i], (QP_MAX+1)*size*sizeof(udctcoef) ); CHECKED_MALLOC( h->dequant4_mf[i], 6*size*sizeof(int) ); CHECKED_MALLOC( h->unquant4_mf[i], (QP_MAX+1)*size*sizeof(int) ); } @@ -112,7 +112,7 @@ int x264_cqm_init( x264_t *h ) if( j < i ) h->quant4_bias[i] = h->quant4_bias[j]; else - CHECKED_MALLOC( h->quant4_bias[i], (QP_MAX+1)*size*sizeof(uint16_t) ); + CHECKED_MALLOC( h->quant4_bias[i], (QP_MAX+1)*size*sizeof(udctcoef) ); } for( int q = 0; q < 6; q++ ) @@ -171,7 +171,9 @@ int x264_cqm_init( x264_t *h ) for( int i = 0; i < 64; i++ ) { h->unquant8_mf[i_list][q][i] = (1ULL << (q/6 + 16 + 8)) / quant8_mf[i_list][q%6][i]; - h->quant8_mf[i_list][q][i] = j = SHIFT(quant8_mf[i_list][q%6][i], q/6); + j = SHIFT(quant8_mf[i_list][q%6][i], q/6); + h->quant8_mf[i_list][q][i] = (uint16_t)j; + if( !j ) { min_qp_err = X264_MIN( min_qp_err, q ); diff --git a/common/x86/quant-a.asm b/common/x86/quant-a.asm index b7affa3d..1a3b002e 100644 --- a/common/x86/quant-a.asm +++ b/common/x86/quant-a.asm @@ -6,6 +6,7 @@ ;* Authors: Loren Merritt ;* Jason Garrett-Glaser ;* Christian Heine +;* Oskar Arvidsson ;* ;* This program is free software; you can redistribute it and/or modify ;* it under the terms of the GNU General Public License as published by @@ -78,15 +79,13 @@ cextern pb_01 %macro QUANT_DC_START_MMX 0 movd m6, r1m ; mf movd m7, r2m ; bias -%ifidn m0, mm0 - pshufw m6, m6, 0 - pshufw m7, m7, 0 +%ifdef X264_HIGH_BIT_DEPTH + SPLATD m6, m6 + SPLATD m7, m7 %else - pshuflw m6, m6, 0 - pshuflw m7, m7, 0 - punpcklqdq m6, m6 - punpcklqdq m7, m7 -%endif + SPLATW m6, m6 + SPLATW m7, m7 +%endif ; X264_HIGH_BIT_DEPTH %endmacro %macro QUANT_DC_START_SSSE3 0 @@ -118,6 +117,246 @@ cextern pb_01 psignw %1, %2 %endmacro +%macro PSIGND_MMX 2 + pxor %1, %2 + psubd %1, %2 +%endmacro + +%macro PSIGND_SSSE3 2 + psignd %1, %2 +%endmacro + +%macro PABSD_MMX 2 + pxor %1, %1 + pcmpgtd %1, %2 + pxor %2, %1 + psubd %2, %1 + SWAP %1, %2 +%endmacro + +%macro PABSD_SSSE3 2 + pabsd %1, %2 +%endmacro + +%macro QUANT_END_MMX 0 + xor eax, eax +%ifdef ARCH_X86_64 +%if mmsize == 16 + packsswb m5, m5 +%endif + movq rcx, m5 + test rcx, rcx +%else +%if mmsize == 16 + pxor m4, m4 + pcmpeqb m5, m4 + pmovmskb ecx, m5 + cmp ecx, (1<