vf_eq{,2} buffer alignment
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);
On Sat, Apr 22, 2006 at 05:57:53PM -0400, Alan Curry wrote:
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?
Scale must accept unaligned input. Consider the case where the input comes from vf_crop or similar... Rich
Rich Felker writes the following:
Scale must accept unaligned input. Consider the case where the input comes from vf_crop or similar...
Looking at vf_crop makes it all clear. swscale, my old nemesis, how many more bugs have you? At least 3 more. There are 2 cases of assuming stride==width and 1 case of expecting the Y plane to be aligned at 16 bytes. They're independent bugs, but they all appear in the DEFCSP420_CVT macro which defines the functions altivec_yuv2_{argb,bgra,rgb24,...}. --- postproc/yuv2rgb_altivec.c 2006-04-24 13:34:23.000000000 -0500 +++ postproc/yuv2rgb_altivec.c 2006-04-24 13:34:31.000000000 -0500 @@ -304,7 +304,7 @@ (oplanes[0]+srcSliceY*outstrides[0]+outstrides[0]); \ \ \ - instrides_scl[0] = instrides[0]; \ + instrides_scl[0] = instrides[0]*2-w; /* the loop moves y{1,2}i by w */ \ instrides_scl[1] = instrides[1]-w/2; /* the loop moves ui by w/2 */ \ instrides_scl[2] = instrides[2]-w/2; /* the loop moves vi by w/2 */ \ \ --- postproc/yuv2rgb_altivec.c 2006-04-24 13:34:31.000000000 -0500 +++ postproc/yuv2rgb_altivec.c 2006-04-24 13:34:36.000000000 -0500 @@ -292,7 +292,7 @@ vector unsigned short lCSHIFT = c->CSHIFT; \ \ ubyte *y1i = in[0]; \ - ubyte *y2i = in[0]+w; \ + ubyte *y2i = in[0]+instrides[0]; \ ubyte *ui = in[1]; \ ubyte *vi = in[2]; \ \ --- postproc/yuv2rgb_altivec.c 2006-04-24 13:34:36.000000000 -0500 +++ postproc/yuv2rgb_altivec.c 2006-04-24 13:35:55.000000000 -0500 @@ -278,7 +278,7 @@ vector signed short R1,G1,B1; \ vector unsigned char R,G,B; \ \ - vector unsigned char *uivP, *vivP; \ + vector unsigned char *y1ivP, *y2ivP, *uivP, *vivP; \ vector unsigned char align_perm; \ \ vector signed short \ @@ -315,11 +315,17 @@ \ for (j=0;j<w/16;j++) { \ \ - y0 = vec_ldl (0,y1i); \ - y1 = vec_ldl (0,y2i); \ + y1ivP = (vector unsigned char *)y1i; \ + y2ivP = (vector unsigned char *)y2i; \ uivP = (vector unsigned char *)ui; \ vivP = (vector unsigned char *)vi; \ \ + align_perm = vec_lvsl (0, y1i); \ + y0 = (vector unsigned char)vec_perm (y1ivP[0], y1ivP[1], align_perm);\ + \ + align_perm = vec_lvsl (0, y2i); \ + y1 = (vector unsigned char)vec_perm (y2ivP[0], y2ivP[1], align_perm);\ + \ align_perm = vec_lvsl (0, ui); \ u = (vector signed char)vec_perm (uivP[0], uivP[1], align_perm); \ \
Hi On Mon, Apr 24, 2006 at 03:29:15PM -0400, Alan Curry wrote:
Rich Felker writes the following:
Scale must accept unaligned input. Consider the case where the input comes from vf_crop or similar...
Looking at vf_crop makes it all clear.
swscale, my old nemesis, how many more bugs have you?
swscale_altivec ...
At least 3 more. There are 2 cases of assuming stride==width and 1 case of expecting the Y plane to be aligned at 16 bytes. They're independent bugs, but they all appear in the DEFCSP420_CVT macro which defines the functions altivec_yuv2_{argb,bgra,rgb24,...}.
do we have a swscale-altivec maintainer? if no, then apply it, but with special case(s) for the correctly aligned case so that the code isnt slowed down [...] -- Michael In the past you could go to a library and read, borrow or copy any book Today you'd get arrested for mere telling someone where the library is
On Tue, Apr 25, 2006 at 02:41:23AM +0200, Michael Niedermayer wrote:
do we have a swscale-altivec maintainer?
I think Alan has the post already. He should just write it into DOCS/tech/MAINTAINERS to make it official... Diego
Diego Biurrun writes the following:
On Tue, Apr 25, 2006 at 02:41:23AM +0200, Michael Niedermayer wrote:
do we have a swscale-altivec maintainer?
I think Alan has the post already. He should just write it into DOCS/tech/MAINTAINERS to make it official...
There are still a couple of functions in there I haven't touched...
On Mon, Apr 24, 2006 at 10:01:03PM -0400, Alan Curry wrote:
Diego Biurrun writes the following:
On Tue, Apr 25, 2006 at 02:41:23AM +0200, Michael Niedermayer wrote:
do we have a swscale-altivec maintainer?
I think Alan has the post already. He should just write it into DOCS/tech/MAINTAINERS to make it official...
There are still a couple of functions in there I haven't touched...
But nobody else has touched as many functions as you have. Go for it, accept the maintainership :) Diego
Michael Niedermayer writes the following:
At least 3 more. There are 2 cases of assuming stride==width and 1 case of expecting the Y plane to be aligned at 16 bytes. They're independent bugs, but they all appear in the DEFCSP420_CVT macro which defines the functions altivec_yuv2_{argb,bgra,rgb24,...}.
do we have a swscale-altivec maintainer? if no, then apply it, but with special case(s) for the correctly aligned case so that the code isnt slowed down
Special case as in a whole new set of scaler functions? Or adding an extra copy of the main loop with a single test being done every time the function is called? The current version is neither fully optimized for aligned input nor fully correct for unaligned input, but halfway in between. Only the Y plane is assumed to be aligned; the U and V planes are loaded and shifted. There are 3 independent assumptions that could be made there, so I could make 8 versions of the same loop. I assume nobody would want to see that.
Hi On Mon, Apr 24, 2006 at 10:00:29PM -0400, Alan Curry wrote:
Michael Niedermayer writes the following:
At least 3 more. There are 2 cases of assuming stride==width and 1 case of expecting the Y plane to be aligned at 16 bytes. They're independent bugs, but they all appear in the DEFCSP420_CVT macro which defines the functions altivec_yuv2_{argb,bgra,rgb24,...}.
do we have a swscale-altivec maintainer? if no, then apply it, but with special case(s) for the correctly aligned case so that the code isnt slowed down
Special case as in a whole new set of scaler functions? Or adding an extra copy of the main loop with a single test being done every time the function is called?
The current version is neither fully optimized for aligned input nor fully correct for unaligned input, but halfway in between. Only the Y plane is assumed to be aligned; the U and V planes are loaded and shifted. There are 3 independent assumptions that could be made there, so I could make 8 versions of the same loop. I assume nobody would want to see that.
ok, then your patch is fine and as you are the sws-altivec maintainer, no need to send patches for sws-altivec stuff anyway ... [...] -- Michael In the past you could go to a library and read, borrow or copy any book Today you'd get arrested for mere telling someone where the library is
Michael Niedermayer writes the following:
ok, then your patch is fine and as you are the sws-altivec maintainer, no need to send patches for sws-altivec stuff anyway ...
Scary though that may be... this patch is now committed
participants (4)
-
Alan Curry -
Diego Biurrun -
Michael Niedermayer -
Rich Felker