From 364a17974979dcddf41b7e88b2f34e721ca8855a Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Fri, 6 Feb 2004 02:12:37 +0000 Subject: [PATCH] quantizer noise shaping optimization Originally committed as revision 2750 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/dsputil.c | 26 ++++++++++ libavcodec/dsputil.h | 5 ++ libavcodec/i386/dsputil_mmx.c | 94 ++++++++++++++++++++++++++++++++++- libavcodec/mpegvideo.c | 40 +++------------ 4 files changed, 132 insertions(+), 33 deletions(-) diff --git a/libavcodec/dsputil.c b/libavcodec/dsputil.c index 114d67b501..c49ff76beb 100644 --- a/libavcodec/dsputil.c +++ b/libavcodec/dsputil.c @@ -2532,6 +2532,29 @@ static int pix_abs8_xy2_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, return s; } +static int try_8x8basis_c(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale){ + int i; + unsigned int sum=0; + + for(i=0; i<8*8; i++){ + int b= rem[i] + ((basis[i]*scale + (1<<(BASIS_SHIFT - RECON_SHIFT-1)))>>(BASIS_SHIFT - RECON_SHIFT)); + int w= weight[i]; + b>>= RECON_SHIFT; + assert(-512>4; + } + return sum>>2; +} + +static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale){ + int i; + + for(i=0; i<8*8; i++){ + rem[i] += (basis[i]*scale + (1<<(BASIS_SHIFT - RECON_SHIFT-1)))>>(BASIS_SHIFT - RECON_SHIFT); + } +} + /** * permutes an 8x8 block. * @param block the block which will be permuted according to the given permutation vector @@ -3235,6 +3258,9 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx) c->h263_h_loop_filter= h263_h_loop_filter_c; c->h263_v_loop_filter= h263_v_loop_filter_c; + + c->try_8x8basis= try_8x8basis_c; + c->add_8x8basis= add_8x8basis_c; #ifdef HAVE_MMX dsputil_init_mmx(c, avctx); diff --git a/libavcodec/dsputil.h b/libavcodec/dsputil.h index fbe2c684e1..0a77c7f4fc 100644 --- a/libavcodec/dsputil.h +++ b/libavcodec/dsputil.h @@ -285,6 +285,11 @@ typedef struct DSPContext { #define FF_SIMPLE_IDCT_PERM 3 #define FF_TRANSPOSE_IDCT_PERM 4 + int (*try_8x8basis)(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale); + void (*add_8x8basis)(int16_t rem[64], int16_t basis[64], int scale); +#define BASIS_SHIFT 16 +#define RECON_SHIFT 6 + } DSPContext; void dsputil_static_init(void); diff --git a/libavcodec/i386/dsputil_mmx.c b/libavcodec/i386/dsputil_mmx.c index efa0225573..e2aee75c23 100644 --- a/libavcodec/i386/dsputil_mmx.c +++ b/libavcodec/i386/dsputil_mmx.c @@ -1973,6 +1973,92 @@ static void just_return() { return; } c->put_no_rnd_ ## postfix1 = put_no_rnd_ ## postfix2;\ c->avg_ ## postfix1 = avg_ ## postfix2; +static int try_8x8basis_mmx(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale){ + int i=0; + + assert(ABS(scale) < 256); + scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT; + + asm volatile( + "pcmpeqw %%mm6, %%mm6 \n\t" // -1w + "psrlw $15, %%mm6 \n\t" // 1w + "pxor %%mm7, %%mm7 \n\t" + "movd %4, %%mm5 \n\t" + "punpcklwd %%mm5, %%mm5 \n\t" + "punpcklwd %%mm5, %%mm5 \n\t" + "1: \n\t" + "movq (%1, %0), %%mm0 \n\t" + "movq 8(%1, %0), %%mm1 \n\t" + "pmulhw %%mm5, %%mm0 \n\t" + "pmulhw %%mm5, %%mm1 \n\t" + "paddw %%mm6, %%mm0 \n\t" + "paddw %%mm6, %%mm1 \n\t" + "psraw $1, %%mm0 \n\t" + "psraw $1, %%mm1 \n\t" + "paddw (%2, %0), %%mm0 \n\t" + "paddw 8(%2, %0), %%mm1 \n\t" + "psraw $6, %%mm0 \n\t" + "psraw $6, %%mm1 \n\t" + "pmullw (%3, %0), %%mm0 \n\t" + "pmullw 8(%3, %0), %%mm1 \n\t" + "pmaddwd %%mm0, %%mm0 \n\t" + "pmaddwd %%mm1, %%mm1 \n\t" + "paddd %%mm1, %%mm0 \n\t" + "psrld $4, %%mm0 \n\t" + "paddd %%mm0, %%mm7 \n\t" + "addl $16, %0 \n\t" + "cmpl $128, %0 \n\t" //FIXME optimize & bench + " jb 1b \n\t" + "movq %%mm7, %%mm6 \n\t" + "psrlq $32, %%mm7 \n\t" + "paddd %%mm6, %%mm7 \n\t" + "psrld $2, %%mm7 \n\t" + "movd %%mm7, %0 \n\t" + + : "+r" (i) + : "r"(basis), "r"(rem), "r"(weight), "g"(scale) + ); + return i; +} + +static void add_8x8basis_mmx(int16_t rem[64], int16_t basis[64], int scale){ + int i=0; + + if(ABS(scale) < 256){ + scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT; + asm volatile( + "pcmpeqw %%mm6, %%mm6 \n\t" // -1w + "psrlw $15, %%mm6 \n\t" // 1w + "movd %3, %%mm5 \n\t" + "punpcklwd %%mm5, %%mm5 \n\t" + "punpcklwd %%mm5, %%mm5 \n\t" + "1: \n\t" + "movq (%1, %0), %%mm0 \n\t" + "movq 8(%1, %0), %%mm1 \n\t" + "pmulhw %%mm5, %%mm0 \n\t" + "pmulhw %%mm5, %%mm1 \n\t" + "paddw %%mm6, %%mm0 \n\t" + "paddw %%mm6, %%mm1 \n\t" + "psraw $1, %%mm0 \n\t" + "psraw $1, %%mm1 \n\t" + "paddw (%2, %0), %%mm0 \n\t" + "paddw 8(%2, %0), %%mm1 \n\t" + "movq %%mm0, (%2, %0) \n\t" + "movq %%mm1, 8(%2, %0) \n\t" + "addl $16, %0 \n\t" + "cmpl $128, %0 \n\t" //FIXME optimize & bench + " jb 1b \n\t" + + : "+r" (i) + : "r"(basis), "r"(rem), "g"(scale) + ); + }else{ + for(i=0; i<8*8; i++){ + rem[i] += (basis[i]*scale + (1<<(BASIS_SHIFT - RECON_SHIFT-1)))>>(BASIS_SHIFT - RECON_SHIFT); + } + } +} + /* external functions, from idct_mmx.c */ void ff_mmx_idct(DCTELEM *block); void ff_mmxext_idct(DCTELEM *block); @@ -2125,10 +2211,16 @@ void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx) if(!(avctx->flags & CODEC_FLAG_BITEXACT)){ c->vsad[0] = vsad16_mmx; } + + if(!(avctx->flags & CODEC_FLAG_BITEXACT)){ + c->try_8x8basis= try_8x8basis_mmx; + } + c->add_8x8basis= add_8x8basis_mmx; + #endif //CONFIG_ENCODERS c->h263_v_loop_filter= h263_v_loop_filter_mmx; - c->h263_h_loop_filter= h263_h_loop_filter_mmx; + c->h263_h_loop_filter= h263_h_loop_filter_mmx; if (mm_flags & MM_MMXEXT) { c->put_pixels_tab[0][1] = put_pixels16_x2_mmx2; diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index ab64d3d6a3..e792f3c852 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -4915,8 +4915,6 @@ static int dct_quantize_trellis_c(MpegEncContext *s, return last_non_zero; } -#define BASIS_SHIFT 16 -#define RECON_SHIFT 6 //#define REFINE_STATS 1 static int16_t basis[64][64]; @@ -4939,29 +4937,6 @@ static void build_basis(uint8_t *perm){ } } -static int try_basis(int16_t rem[64], int16_t weight[64], int index, int scale){ - int i; - unsigned int sum=0; - - for(i=0; i<8*8; i++){ - int b= rem[i] - ((basis[index][i]*scale + (1<<(BASIS_SHIFT - RECON_SHIFT-1)))>>(BASIS_SHIFT - RECON_SHIFT)); - int w= weight[i]; - b= (b + (1<<(RECON_SHIFT-1))) >> RECON_SHIFT; - assert(-512>4; - } - return sum>>2; -} - -static void add_basis(int16_t rem[64], int index, int scale){ - int i; - - for(i=0; i<8*8; i++){ - rem[i] -= (basis[index][i]*scale + (1<<(BASIS_SHIFT - RECON_SHIFT-1)))>>(BASIS_SHIFT - RECON_SHIFT); - } -} - static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise? DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale){ @@ -5028,8 +5003,9 @@ static int messed_sign=0; #ifdef REFINE_STATS {START_TIMER #endif - for(i=0; i<64; i++){ //FIXME memsetw or similar - rem[i]= (orig[i]<dsp.add_8x8basis(rem, basis[j], coeff); }else{ run++; } @@ -5081,7 +5057,7 @@ STOP_TIMER("init rem[]") {START_TIMER #endif for(;;){ - int best_score=try_basis(rem, weight, 0, 0); + int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0); int nochange_score= best_score; int best_coeff=0; int best_change=0; @@ -5105,7 +5081,7 @@ STOP_TIMER("init rem[]") if(new_coeff >= 2048 || new_coeff < 0) continue; - score= try_basis(rem, weight, 0, new_coeff - old_coeff); + score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff); if(score -100*lambda) || lambda==0); - score+= try_basis(rem, weight, j, unquant_change); + score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change); if(scoredsp.add_8x8basis(rem, basis[j], best_unquant_change); }else{ break; }