[MPlayer-dev-eng] vf_eq{,2} buffer alignment

Alan Curry pacman at theworld.com
Sat Apr 22 23:57:53 CEST 2006


vf_eq and vf_eq2 both pass on a malloc()ed buffer to the next filter in the
chain as part of an mp_image_t. When the next filter is vf_scale using
altivec operations, it expects the image plans to begin at 16-byte boundaries
so it ends up putting the pixels in the wrong place.

If you're altivec-capable, the effect is observable with

  mplayer -vf eq2=1.0:1.0:0.1,scale -vo x11 yourmovie

The misalignment depends on the whims of malloc, but in my case I usually get
everything shifted 8 pixels to the right, with what should be the rightmost 8
pixels appearing instead on the left.

The following patch changes eq and eq2 to align their buffers at 16-byte
boundaries. Is this OK, or should I instead try to make scale more tolerant
of unaligned input?

Index: libmpcodecs/vf_eq.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpcodecs/vf_eq.c,v
retrieving revision 1.12
diff -u -r1.12 vf_eq.c
--- libmpcodecs/vf_eq.c	15 Apr 2006 20:46:54 -0000	1.12
+++ libmpcodecs/vf_eq.c	22 Apr 2006 21:45:03 -0000
@@ -4,6 +4,9 @@
 #include <inttypes.h>
 
 #include "config.h"
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
 #include "mp_msg.h"
 #include "cpudetect.h"
 #include "asmalign.h"
@@ -131,7 +134,7 @@
 	dmpi->stride[1] = mpi->stride[1];
 	dmpi->stride[2] = mpi->stride[2];
 
-	if (!vf->priv->buf) vf->priv->buf = malloc(mpi->stride[0]*mpi->h);
+	if (!vf->priv->buf) vf->priv->buf = memalign(16, mpi->stride[0]*mpi->h);
 	
 	if ((vf->priv->brightness == 0) && (vf->priv->contrast == 0))
 		dmpi->planes[0] = mpi->planes[0];
Index: libmpcodecs/vf_eq2.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpcodecs/vf_eq2.c,v
retrieving revision 1.12
diff -u -r1.12 vf_eq2.c
--- libmpcodecs/vf_eq2.c	15 Apr 2006 20:46:54 -0000	1.12
+++ libmpcodecs/vf_eq2.c	22 Apr 2006 21:45:03 -0000
@@ -16,6 +16,9 @@
 #include <inttypes.h>
 
 #include "config.h"
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
 #include "mp_msg.h"
 #include "cpudetect.h"
 #include "asmalign.h"
@@ -240,13 +243,14 @@
       eq2->buf_w[1] = eq2->buf_w[2] = src->w >> src->chroma_x_shift;
       eq2->buf_h[1] = eq2->buf_h[2] = src->h >> src->chroma_y_shift;
     img_n = eq2->buf_w[0]*eq2->buf_h[0];
+    free(eq2->buf[0]);
     if(src->num_planes>1){
       img_c = eq2->buf_w[1]*eq2->buf_h[1];
-      eq2->buf[0] = (unsigned char *) realloc (eq2->buf[0], img_n + 2*img_c);
+      eq2->buf[0] = memalign (16, img_n + 2*img_c);
       eq2->buf[1] = eq2->buf[0] + img_n;
       eq2->buf[2] = eq2->buf[1] + img_c;
     } else
-      eq2->buf[0] = (unsigned char *) realloc (eq2->buf[0], img_n);
+      eq2->buf[0] = memalign (16, img_n);
   }
 
   dst = vf_get_image (vf->next, src->imgfmt, MP_IMGTYPE_EXPORT, 0, src->w, src->h);




More information about the MPlayer-dev-eng mailing list