[Mplayer-cvslog] CVS: main/libavcodec/i386 mpegvideo.c,1.1,1.2

Nick Kurshev nick at mplayer.dev.hu
Fri Jul 20 12:55:46 CEST 2001


Update of /cvsroot/mplayer/main/libavcodec/i386
In directory mplayer:/var/tmp.root/cvs-serv10402/main/libavcodec/i386

Modified Files:
	mpegvideo.c 
Log Message:
MMX optimization of dct_unquantize. Another 10% on K7

Index: mpegvideo.c
===================================================================
RCS file: /cvsroot/mplayer/main/libavcodec/i386/mpegvideo.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- mpegvideo.c	16 Jul 2001 09:16:20 -0000	1.1
+++ mpegvideo.c	20 Jul 2001 10:55:43 -0000	1.2
@@ -18,6 +18,164 @@
  *
  * Optimized for ia32 cpus by Nick Kurshev <nickols_k at mail.ru>
  */
+#ifdef HAVE_MMX 
+static const unsigned short mm_wabs[4] __attribute__ ((aligned(8))) = { 0xffff, 0xffff, 0xffff, 0xffff };
+static const unsigned short mm_wone[4] __attribute__ ((aligned(8))) = { 0x1, 0x1, 0x1, 0x1 };
+
+/*
+  NK:
+  Note: looking at PARANOID:
+  "enable all paranoid tests for rounding, overflows, etc..."
+
+#ifdef PARANOID
+                if (level < -2048 || level > 2047)
+                    fprintf(stderr, "unquant error %d %d\n", i, level);
+#endif
+  We can suppose that result of two multiplications can't be greate of 0xFFFF
+  i.e. is 16-bit, so we use here only PMULLW instruction and can avoid
+  a complex multiplication.
+=====================================================
+ Full formula for multiplication of 2 integer numbers
+ which are represent as high:low words:
+ input: value1 = high1:low1
+        value2 = high2:low2
+ output: value3 = value1*value2
+ value3=high3:low3 (on overflow: modulus 2^32 wrap-around)
+ this mean that for 0x123456 * 0x123456 correct result is 0x766cb0ce4
+ but this algorithm will compute only 0x66cb0ce4
+ this limited by 16-bit size of operands
+ ---------------------------------
+ tlow1 = high1*low2
+ tlow2 = high2*low1
+ tlow1 = tlow1 + tlow2
+ high3:low3 = low1*low2
+ high3 += tlow1
+*/
+static void dct_unquantize(MpegEncContext *s, 
+                           DCTELEM *block, int n, int qscale)
+{
+    int i, level;
+    const UINT16 *quant_matrix;
+    if (s->mb_intra) {
+        if (n < 4) 
+            block[0] = block[0] * s->y_dc_scale;
+        else
+            block[0] = block[0] * s->c_dc_scale;
+        if (s->out_format == FMT_H263) {
+            i = 1;
+            goto unquant_even;
+        }
+        /* XXX: only mpeg1 */
+        quant_matrix = s->intra_matrix;
+	i=1;
+	/* Align on 4 elements boundary */
+	while(i&3)
+	{
+            level = block[i];
+            if (level) {
+                if (level < 0) level = -level;
+                    level = (int)(level * qscale * quant_matrix[i]) >> 3;
+                    level = (level - 1) | 1;
+                if (block[i] < 0) level = -level;
+                block[i] = level;
+            }
+	    i++;
+	}
+	__asm __volatile(
+	"movd	%0, %%mm6\n\t"       /* mm6 = qscale | 0  */
+	"punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
+	"movq	%2, %%mm4\n\t"
+	"movq	%%mm6, %%mm7\n\t"
+	"movq	%1, %%mm5\n\t"
+	"packssdw %%mm6, %%mm7\n\t" /* mm7 = qscale | qscale | qscale | qscale */
+	"pxor	%%mm6, %%mm6\n\t"
+	::"g"(qscale),"m"(mm_wone[0]),"m"(mm_wabs[0]):"memory");
+        for(;i<64;i+=4) {
+		__asm __volatile(
+			"movq	%1, %%mm0\n\t"
+			"movq	%1, %%mm2\n\t"
+			"movq	%%mm7, %%mm1\n\t"
+			"pcmpgtw %%mm6, %%mm2\n\t"
+			"pmullw	%2, %%mm1\n\t"
+			"pandn	%%mm4, %%mm2\n\t"
+			"por	%%mm5, %%mm2\n\t"  
+			"pmullw	%%mm2, %%mm0\n\t" /* mm0 = abs(block[i]). */
+			
+			"pmullw	%%mm0, %%mm1\n\t"
+			"psraw	$3, %%mm1\n\t"
+			"psubw	%%mm5, %%mm1\n\t"   /* block[i] --; */
+			"por	%%mm5, %%mm1\n\t"   /* block[i] |= 1 */
+			"pmullw %%mm2, %%mm1\n\t"   /* change signs again */
+
+			"movq	%1, %%mm3\n\t"
+			"pcmpeqw %%mm6, %%mm3\n\t"
+			"pandn	%%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
+			"pand	%%mm3, %%mm1\n\t" /* nullify if was zero */
+			"movq	%%mm1, %0"
+			:"=m"(block[i])
+			:"m"(block[i]), "m"(quant_matrix[i])
+			:"memory");
+        }
+    } else {
+        i = 0;
+    unquant_even:
+        quant_matrix = s->non_intra_matrix;
+	/* Align on 4 elements boundary */
+	while(i&3)
+	{
+	    level = block[i];
+            if (level) {
+                if (level < 0) level = -level;
+                    level = (((level << 1) + 1) * qscale *
+                             ((int) quant_matrix[i])) >> 4;
+                    level = (level - 1) | 1;
+                if(block[i] < 0) level = -level;
+                block[i] = level;
+	    }
+	    i++;
+	}
+	__asm __volatile(
+	"movd	%0, %%mm6\n\t"       /* mm6 = qscale | 0  */
+	"punpckldq %%mm6, %%mm6\n\t" /* mm6 = qscale | qscale */
+	"movq	%2, %%mm4\n\t"
+	"movq	%%mm6, %%mm7\n\t"
+	"movq	%1, %%mm5\n\t"
+	"packssdw %%mm6, %%mm7\n\t" /* mm7 = qscale | qscale | qscale | qscale */
+	"pxor	%%mm6, %%mm6\n\t"
+	::"g"(qscale),"m"(mm_wone[0]),"m"(mm_wabs[0]):"memory");
+        for(;i<64;i+=4) {
+		__asm __volatile(
+			"movq	%1, %%mm0\n\t"
+			"movq	%1, %%mm2\n\t"
+			"movq	%%mm7, %%mm1\n\t"
+			"pcmpgtw %%mm6, %%mm2\n\t"
+			"pmullw	%2, %%mm1\n\t"
+			"pandn	%%mm4, %%mm2\n\t"
+			"por	%%mm5, %%mm2\n\t"  
+			"pmullw	%%mm2, %%mm0\n\t" /* mm0 = abs(block[i]). */
+			"psllw	$1, %%mm0\n\t" /* block[i] <<= 1 */
+			"paddw	%%mm5, %%mm0\n\t" /* block[i] ++ */
+			
+			"pmullw	%%mm0, %%mm1\n\t"
+			"psraw	$4, %%mm1\n\t"
+			"psubw	%%mm5, %%mm1\n\t"   /* block[i] --; */
+			"por	%%mm5, %%mm1\n\t"   /* block[i] |= 1 */
+			"pmullw %%mm2, %%mm1\n\t"   /* change signs again */
+
+			"movq	%1, %%mm3\n\t"
+			"pcmpeqw %%mm6, %%mm3\n\t"
+			"pandn	%%mm4, %%mm3\n\t"  /* fake of pcmpneqw : mm0 != 0 then mm1 = -1 */
+			"pand	%%mm3, %%mm1\n\t" /* nullify if was zero */
+			"movq	%%mm1, %0"
+			:"=m"(block[i])
+			:"m"(block[i]), "m"(quant_matrix[i])
+			:"memory");
+        }
+    }
+/* Fix me: if decore will not be modified in the future this EMMS can be omitted */
+emms();
+}
+#endif
 
 void MPV_frame_start(MpegEncContext *s)
 {




More information about the MPlayer-cvslog mailing list