changeset 10020:9829b7e61b55

new mmx/mmx2/3dnow code for improved performance
author rfelker
date Tue, 29 Apr 2003 03:13:43 +0000
parents 0ea7b9c6c27f
children a7ae63354098
files libmpcodecs/vf_tfields.c
diffstat 1 files changed, 152 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/libmpcodecs/vf_tfields.c	Tue Apr 29 02:23:47 2003 +0000
+++ b/libmpcodecs/vf_tfields.c	Tue Apr 29 03:13:43 2003 +0000
@@ -4,6 +4,7 @@
 
 #include "../config.h"
 #include "../mp_msg.h"
+#include "../cpudetect.h"
 
 #include "img_format.h"
 #include "mp_image.h"
@@ -56,30 +57,149 @@
 	}
 }
 
-static void qpelup(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss)
+#ifdef HAVE_3DNOW
+static void qpel_3DNOW(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
 {
-	int i, j;
-	memcpy(d, s, w);
+	int i, j, ssd=ss;
+	int crap1, crap2;
+	if (up) {
+		ssd = -ss;
+		memcpy(d, s, w);
+		d += ds;
+		s += ss;
+	}
 	for (i=h-1; i; i--) {
+		asm(
+			"pxor %%mm7, %%mm7 \n\t"
+			"1: \n\t"
+			"movq (%%esi), %%mm0 \n\t"
+			"movq (%%esi,%%eax), %%mm1 \n\t"
+			"pavgusb %%mm0, %%mm1 \n\t"
+			"addl $8, %%esi \n\t"
+			"pavgusb %%mm0, %%mm1 \n\t"
+			"movq %%mm1, (%%edi) \n\t"
+			"addl $8, %%edi \n\t"
+			"decl %%ecx \n\t"
+			"jnz 1b \n\t"
+			: "=S"(crap1), "=D"(crap2)
+			: "c"(w>>3), "S"(s), "D"(d), "a"(ssd)
+		);
+		for (j=(w&7); j<w; j++)
+			d[j] = (s[j+ssd] + 3*s[j])>>2;
 		d += ds;
 		s += ss;
-		for (j=0; j<w; j++)
-			d[j] = (s[j-ss] + 3*s[j])>>2;
 	}
+	if (!up) memcpy(d, s, w);
+	asm volatile("emms \n\t" : : : "memory");
 }
+#endif
 
-static void qpeldown(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss)
+#ifdef HAVE_MMX2
+static void qpel_MMX2(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
 {
-	int i, j;
+	int i, j, ssd=ss;
+	int crap1, crap2;
+	if (up) {
+		ssd = -ss;
+		memcpy(d, s, w);
+		d += ds;
+		s += ss;
+	}
 	for (i=h-1; i; i--) {
-		for (j=0; j<w; j++)
-			d[j] = (3*s[j] + s[j+ss])>>2;
+		asm(
+			"pxor %%mm7, %%mm7 \n\t"
+			"1: \n\t"
+			"movq (%%esi), %%mm0 \n\t"
+			"movq (%%esi,%%eax), %%mm1 \n\t"
+			"pavgb %%mm0, %%mm1 \n\t"
+			"addl $8, %%esi \n\t"
+			"pavgb %%mm0, %%mm1 \n\t"
+			"movq %%mm1, (%%edi) \n\t"
+			"addl $8, %%edi \n\t"
+			"decl %%ecx \n\t"
+			"jnz 1b \n\t"
+			: "=S"(crap1), "=D"(crap2)
+			: "c"(w>>3), "S"(s), "D"(d), "a"(ssd)
+		);
+		for (j=(w&7); j<w; j++)
+			d[j] = (s[j+ssd] + 3*s[j])>>2;
 		d += ds;
 		s += ss;
 	}
-	memcpy(d, s, w);
+	if (!up) memcpy(d, s, w);
+	asm volatile("emms \n\t" : : : "memory");
+}
+#endif
+
+#ifdef HAVE_MMX
+static void qpel_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
+{
+	int i, j, ssd=ss;
+	int crap1, crap2;
+	if (up) {
+		ssd = -ss;
+		memcpy(d, s, w);
+		d += ds;
+		s += ss;
+	}
+	for (i=h-1; i; i--) {
+		asm(
+			"pxor %%mm7, %%mm7 \n\t"
+			"1: \n\t"
+			"movq (%%esi), %%mm0 \n\t"
+			"movq (%%esi), %%mm1 \n\t"
+			"movq (%%esi,%%eax), %%mm2 \n\t"
+			"movq (%%esi,%%eax), %%mm3 \n\t"
+			"addl $8, %%esi \n\t"
+			"punpcklbw %%mm7, %%mm0 \n\t"
+			"punpckhbw %%mm7, %%mm1 \n\t"
+			"punpcklbw %%mm7, %%mm2 \n\t"
+			"punpckhbw %%mm7, %%mm3 \n\t"
+			"paddw %%mm0, %%mm2 \n\t"
+			"paddw %%mm1, %%mm3 \n\t"
+			"paddw %%mm0, %%mm2 \n\t"
+			"paddw %%mm1, %%mm3 \n\t"
+			"paddw %%mm0, %%mm2 \n\t"
+			"paddw %%mm1, %%mm3 \n\t"
+			"psrlw $2, %%mm2 \n\t"
+			"psrlw $2, %%mm3 \n\t"
+			"packsswb %%mm3, %%mm2 \n\t"
+			"movq %%mm2, (%%edi) \n\t"
+			"addl $8, %%edi \n\t"
+			"decl %%ecx \n\t"
+			"jnz 1b \n\t"
+			: "=S"(crap1), "=D"(crap2)
+			: "c"(w>>3), "S"(s), "D"(d), "a"(ssd)
+		);
+		for (j=(w&7); j<w; j++)
+			d[j] = (s[j+ssd] + 3*s[j])>>2;
+		d += ds;
+		s += ss;
+	}
+	if (!up) memcpy(d, s, w);
+	asm volatile("emms \n\t" : : : "memory");
+}
+#endif
+
+static void qpel_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
+{
+	int i, j, ssd=ss;
+	if (up) {
+		ssd = -ss;
+		memcpy(d, s, w);
+		d += ds;
+		s += ss;
+	}
+	for (i=h-1; i; i--) {
+		for (j=0; j<w; j++)
+			d[j] = (s[j+ssd] + 3*s[j])>>2;
+		d += ds;
+		s += ss;
+	}
+	if (!up) memcpy(d, s, w);
 }
 
+static void (*qpel)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
 
 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi)
 {
@@ -155,27 +275,27 @@
 		dmpi = vf_get_image(vf->next, mpi->imgfmt,
 			MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
 			mpi->width, mpi->height/2);
-		qpeldown(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
-			dmpi->stride[0], mpi->stride[0]*2);
+		qpel(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
+			dmpi->stride[0], mpi->stride[0]*2, 0);
 		if (mpi->flags & MP_IMGFLAG_PLANAR) {
-			qpeldown(dmpi->planes[1], mpi->planes[1],
+			qpel(dmpi->planes[1], mpi->planes[1],
 				mpi->chroma_width, mpi->chroma_height/2,
-				dmpi->stride[1], mpi->stride[1]*2);
-			qpeldown(dmpi->planes[2], mpi->planes[2],
+				dmpi->stride[1], mpi->stride[1]*2, 0);
+			qpel(dmpi->planes[2], mpi->planes[2],
 				mpi->chroma_width, mpi->chroma_height/2,
-				dmpi->stride[2], mpi->stride[2]*2);
+				dmpi->stride[2], mpi->stride[2]*2, 0);
 		}
 		ret = vf_next_put_image(vf, dmpi);
 		
-		qpelup(dmpi->planes[0], mpi->planes[0] + mpi->stride[0],
-			mpi->w, mpi->h/2, dmpi->stride[0], mpi->stride[0]*2);
+		qpel(dmpi->planes[0], mpi->planes[0] + mpi->stride[0],
+			mpi->w, mpi->h/2, dmpi->stride[0], mpi->stride[0]*2, 1);
 		if (mpi->flags & MP_IMGFLAG_PLANAR) {
-			qpelup(dmpi->planes[1], mpi->planes[1] + mpi->stride[1],
+			qpel(dmpi->planes[1], mpi->planes[1] + mpi->stride[1],
 				mpi->chroma_width, mpi->chroma_height/2,
-				dmpi->stride[1], mpi->stride[1]*2);
-			qpelup(dmpi->planes[2], mpi->planes[2] + mpi->stride[2],
+				dmpi->stride[1], mpi->stride[1]*2, 1);
+			qpel(dmpi->planes[2], mpi->planes[2] + mpi->stride[2],
 				mpi->chroma_width, mpi->chroma_height/2,
-				dmpi->stride[2], mpi->stride[2]*2);
+				dmpi->stride[2], mpi->stride[2]*2, 1);
 		}
 		return vf_next_put_image(vf, dmpi) || ret;
 	}
@@ -224,6 +344,16 @@
 	vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
 	vf->priv->mode = 0;
 	if (args) sscanf(args, "%d", &vf->priv->mode);
+	qpel = qpel_C;
+#ifdef HAVE_MMX
+	if(gCpuCaps.hasMMX) qpel = qpel_MMX;
+#endif
+#ifdef HAVE_MMX2
+	if(gCpuCaps.hasMMX2) qpel = qpel_MMX2;
+#endif
+#ifdef HAVE_3DNOW
+	if(gCpuCaps.has3DNow) qpel = qpel_3DNOW;
+#endif
 	return 1;
 }