/* marc.hoffman@analog.com March 8, 2004 Altivec Acceleration for Color Space Conversion revision 0.1 convert I420 YV12 to RGB in various formats, it rejects images that are not in 420 formats it rejects images that don't have widths of multiples of 16 it rejects images that don't have heights of multiples of 2 reject defers to C simulation codes. lots of optimizations to be done here 1. need to fix saturation code, I just couldn't get it to fly with packs and adds. so we currently use max min to clip 2. the inefficient use of chroma loading needs a bit of brushing up 3. analysis of pipeline stalls needs to be done */ #include #include #include #include #include "config.h" #include "rgb2rgb.h" #include "swscale.h" #include "swscale_internal.h" #include "../mangle.h" #include "../libvo/img_format.h" //FIXME try to reduce dependency of such stuff typedef unsigned char ubyte; typedef signed char sbyte; /* RGB interleaver, 16 planar pels 8-bit samples per channel in homogeneous vector registers x0,x1,x2 are interleaved with the following technique: o0 = vec_mergeh (x0,x1); o1 = vec_perm (o0, x2, perm_rgb_0); o2 = vec_perm (o0, x2, perm_rgb_1); o3 = vec_mergel (x0,x1); o4 = vec_perm (o3,o2,perm_rgb_2); o5 = vec_perm (o3,o2,perm_rgb_3); perm_rgb_0: o0(RG).h v1(B) --> o1* 0 1 2 3 4 rgbr|gbrg|brgb|rgbr 0010 0100 1001 0010 0102 3145 2673 894A perm_rgb_1: o0(RG).h v1(B) --> o2 0 1 2 3 4 gbrg|brgb|bbbb|bbbb 0100 1001 1111 1111 B5CD 6EF7 89AB CDEF perm_rgb_2: o3(RG).l o2(rgbB.l) --> o4* 0 1 2 3 4 gbrg|brgb|rgbr|gbrg 1111 1111 0010 0100 89AB CDEF 0182 3945 perm_rgb_2: o3(RG).l o2(rgbB.l) ---> o5* 0 1 2 3 4 brgb|rgbr|gbrg|brgb 1001 0010 0100 1001 a67b 89cA BdCD eEFf */ static const vector unsigned char perm_rgb_0 = (vector unsigned char)(0x00,0x01,0x10,0x02,0x03,0x11,0x04,0x05, 0x12,0x06,0x07,0x13,0x08,0x09,0x14,0x0a), perm_rgb_1 = (vector unsigned char)(0x0b,0x15,0x0c,0x0d,0x16,0x0e,0x0f,0x17, 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f), perm_rgb_2 = (vector unsigned char)(0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x00,0x01,0x18,0x02,0x03,0x19,0x04,0x05), perm_rgb_3 = (vector unsigned char)(0x1a,0x06,0x07,0x1b,0x08,0x09,0x1c,0x0a, 0x0b,0x1d,0x0c,0x0d,0x1e,0x0e,0x0f,0x1f); #define vec_merge3(x2,x1,x0,y0,y1,y2) \ do { \ typeof(x0) o0,o2,o3; \ o0 = vec_mergeh (x0,x1); \ y0 = vec_perm (o0, x2, perm_rgb_0);\ o2 = vec_perm (o0, x2, perm_rgb_1);\ o3 = vec_mergel (x0,x1); \ y1 = vec_perm (o3,o2,perm_rgb_2); \ y2 = vec_perm (o3,o2,perm_rgb_3); \ } while(0) #define vec_mstrgb24(x0,x1,x2,ptr) \ do { \ typeof(x0) _0,_1,_2; \ vec_merge3 (x0,x1,x2,_0,_1,_2); \ vec_st (_0, 0, ptr++); \ vec_st (_1, 0, ptr++); \ vec_st (_2, 0, ptr++); \ } while (0); #define vec_mstbgr24(x0,x1,x2,ptr) \ do { \ typeof(x0) _0,_1,_2; \ vec_merge3 (x2,x1,x0,_0,_1,_2); \ vec_st (_0, 0, ptr++); \ vec_st (_1, 0, ptr++); \ vec_st (_2, 0, ptr++); \ } while (0); /* pack the pixels in rgb0 format msb R lsb 0 */ #define vec_mstrgb32(T,x0,x1,x2,x3,ptr) \ do { \ T _0,_1,_2,_3; \ _0 = vec_mergeh (x0,x1); \ _1 = vec_mergeh (x2,x3); \ _2 = (T)vec_mergeh ((vector unsigned short)_0,(vector unsigned short)_1); \ _3 = (T)vec_mergel ((vector unsigned short)_0,(vector unsigned short)_1); \ vec_st (_2, 0*16, (T *)ptr); \ vec_st (_3, 1*16, (T *)ptr); \ _0 = vec_mergel (x0,x1); \ _1 = vec_mergel (x2,x3); \ _2 = (T)vec_mergeh ((vector unsigned short)_0,(vector unsigned short)_1); \ _3 = (T)vec_mergel ((vector unsigned short)_0,(vector unsigned short)_1); \ vec_st (_2, 2*16, (T *)ptr); \ vec_st (_3, 3*16, (T *)ptr); \ ptr += 4; \ } while (0); /* | 1 0 1.4021 | | Y | | 1 -0.3441 -0.7142 |x| Cb| | 1 1.7718 0 | | Cr| Y: [-128 127] Cb/Cr : [-128 127] typical yuv conversion work on Y: 0-255 this version has been optimized for jpeg decode. */ #define vec_unh(x) \ (vector signed short) \ vec_perm(x,(typeof(x))(0),\ (vector unsigned char)(0x10,0x00,0x10,0x01,0x10,0x02,0x10,0x03,\ 0x10,0x04,0x10,0x05,0x10,0x06,0x10,0x07)) #define vec_unl(x) \ (vector signed short) \ vec_perm(x,(typeof(x))(0),\ (vector unsigned char)(0x10,0x08,0x10,0x09,0x10,0x0A,0x10,0x0B,\ 0x10,0x0C,0x10,0x0D,0x10,0x0E,0x10,0x0F)) #define vec_clip(x) \ vec_max (vec_min (x, (typeof(x))(255)), (typeof(x))(0)) #define vec_packs(x,y) \ (vector unsigned char)vec_pack (vec_clip (x), vec_clip (y)) //#define out_pixels(a,b,c,ptr) vec_mstrgb32(typeof(a),((typeof (a))(0)),a,a,a,ptr) #undef out_pixels #define out_pixels(a,b,c,ptr) vec_mstrgb32(typeof(a),((typeof (a))(0)),a,b,c,ptr) static int altivec_yuv2_abgr32 (SwsContext *c, unsigned char **in, int *instrides, int srcSliceY, int srcSliceH, unsigned char **oplanes, int *outstrides) { int w = c->srcW; int h = srcSliceH; int i,j; int instrides_scl[3]; int adjout; vector unsigned char y0,y1; vector signed char u,v; vector signed short Y0,Y1,Y2,Y3; vector signed short U,V; vector signed short vx,ux,uvx; vector signed short vx0,ux0,uvx0; vector signed short vx1,ux1,uvx1; vector signed short R0,G0,B0; vector signed short R1,G1,B1; vector unsigned char R,G,B; vector unsigned char *uivP, *vivP; vector unsigned char align_perm; ubyte *y1i = in[0]; ubyte *y2i = in[0]+w; ubyte *ui = in[1]; ubyte *vi = in[2]; vector unsigned char *oute = (vector unsigned char *)(oplanes[0]+srcSliceY*outstrides[0]); vector unsigned char *outo = (vector unsigned char *)(oplanes[0]+srcSliceY*outstrides[0]+outstrides[0]); instrides_scl[0] = instrides[0]; 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 */ adjout = outstrides[0] - w*3; for (i=0;i>15; // 1.7718*u ux = vec_sl (U, (vector unsigned short)(1)); ux = vec_mradds (ux, (vector signed short)(29029), (vector signed short)(0)); ux0 = vec_mergeh (ux,ux); ux1 = vec_mergel (ux,ux); // vx = (22972*(v<<1)+0x4000)>>15; // 1.4021*v vx = vec_sl (V, (vector unsigned short)(1)); vx = vec_mradds (vx, (vector signed short)(22972), (vector signed short)(0)); vx0 = vec_mergeh (vx,vx); vx1 = vec_mergel (vx,vx); // uvx = ((-11276*u) + (-23400*v))>>15; // -(0.34414*u+0.71414*v) uvx = vec_mradds (U, (vector signed short)(-11276), (vector signed short)(0)); uvx = vec_mradds (V, (vector signed short)(-23400), uvx); uvx0 = vec_mergeh (uvx,uvx); uvx1 = vec_mergel (uvx,uvx); R0 = vec_add (Y0,vx0); G0 = vec_add (Y0,uvx0); B0 = vec_add (Y0,ux0); R1 = vec_add (Y1,vx1); G1 = vec_add (Y1,uvx1); B1 = vec_add (Y1,ux1); R = vec_packs (R0,R1); G = vec_packs (G0,G1); B = vec_packs (B0,B1); out_pixels(R,G,B,oute); R0 = vec_add (Y2,vx0); G0 = vec_add (Y2,uvx0); B0 = vec_add (Y2,ux0); R1 = vec_add (Y3,vx1); G1 = vec_add (Y3,uvx1); B1 = vec_add (Y3,ux1); R = vec_packs (R0,R1); G = vec_packs (G0,G1); B = vec_packs (B0,B1); out_pixels(R,G,B,outo); y1i += 16; y2i += 16; ui += 8; vi += 8; } outo += (outstrides[0])>>4; oute += (outstrides[0])>>4; ui += instrides_scl[1]; vi += instrides_scl[2]; y1i += instrides_scl[0]; y2i += instrides_scl[0]; } return srcSliceH; } #undef out_pixels #define out_pixels(a,b,c,ptr) vec_mstrgb32(typeof(a),a,b,c,((typeof (a))(0)),ptr) static int altivec_yuv2_argb32 (SwsContext *c, unsigned char **in, int *instrides, int srcSliceY, int srcSliceH, unsigned char **oplanes, int *outstrides) { int w = c->srcW; int h = srcSliceH; int i,j; int instrides_scl[3]; int adjout; vector unsigned char y0,y1; vector signed char u,v; vector signed short Y0,Y1,Y2,Y3; vector signed short U,V; vector signed short vx,ux,uvx; vector signed short vx0,ux0,uvx0; vector signed short vx1,ux1,uvx1; vector signed short R0,G0,B0; vector signed short R1,G1,B1; vector unsigned char R,G,B; vector unsigned char *uivP, *vivP; vector unsigned char align_perm; ubyte *y1i = in[0]; ubyte *y2i = in[0]+w; ubyte *ui = in[1]; ubyte *vi = in[2]; vector unsigned char *oute = (vector unsigned char *)(oplanes[0]+srcSliceY*outstrides[0]); vector unsigned char *outo = (vector unsigned char *)(oplanes[0]+srcSliceY*outstrides[0]+outstrides[0]); instrides_scl[0] = instrides[0]; 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 */ adjout = outstrides[0] - w*3; for (i=0;i>15; // 1.7718*u ux = vec_sl (U, (vector unsigned short)(1)); ux = vec_mradds (ux, (vector signed short)(29029), (vector signed short)(0)); ux0 = vec_mergeh (ux,ux); ux1 = vec_mergel (ux,ux); // vx = (22972*(v<<1)+0x4000)>>15; // 1.4021*v vx = vec_sl (V, (vector unsigned short)(1)); vx = vec_mradds (vx, (vector signed short)(22972), (vector signed short)(0)); vx0 = vec_mergeh (vx,vx); vx1 = vec_mergel (vx,vx); // uvx = ((-11276*u) + (-23400*v))>>15; // -(0.34414*u+0.71414*v) uvx = vec_mradds (U, (vector signed short)(-11276), (vector signed short)(0)); uvx = vec_mradds (V, (vector signed short)(-23400), uvx); uvx0 = vec_mergeh (uvx,uvx); uvx1 = vec_mergel (uvx,uvx); R0 = vec_add (Y0,vx0); G0 = vec_add (Y0,uvx0); B0 = vec_add (Y0,ux0); R1 = vec_add (Y1,vx1); G1 = vec_add (Y1,uvx1); B1 = vec_add (Y1,ux1); R = vec_packs (R0,R1); G = vec_packs (G0,G1); B = vec_packs (B0,B1); out_pixels(R,G,B,oute); R0 = vec_add (Y2,vx0); G0 = vec_add (Y2,uvx0); B0 = vec_add (Y2,ux0); R1 = vec_add (Y3,vx1); G1 = vec_add (Y3,uvx1); B1 = vec_add (Y3,ux1); R = vec_packs (R0,R1); G = vec_packs (G0,G1); B = vec_packs (B0,B1); out_pixels(R,G,B,outo); y1i += 16; y2i += 16; ui += 8; vi += 8; } outo += (outstrides[0])>>4; oute += (outstrides[0])>>4; ui += instrides_scl[1]; vi += instrides_scl[2]; y1i += instrides_scl[0]; y2i += instrides_scl[0]; } return srcSliceH; } #undef out_pixels #define out_pixels(a,b,c,ptr) vec_mstrgb24(a,b,c,ptr) static int altivec_yuv2_rgb24 (SwsContext *c, unsigned char **in, int *instrides, int srcSliceY, int srcSliceH, unsigned char **oplanes, int *outstrides) { int w = c->srcW; int h = srcSliceH; int i,j; int instrides_scl[3]; int adjout; vector unsigned char y0,y1; vector signed char u,v; vector signed short Y0,Y1,Y2,Y3; vector signed short U,V; vector signed short vx,ux,uvx; vector signed short vx0,ux0,uvx0; vector signed short vx1,ux1,uvx1; vector signed short R0,G0,B0; vector signed short R1,G1,B1; vector unsigned char R,G,B; vector unsigned char *uivP, *vivP; vector unsigned char align_perm; ubyte *y1i = in[0]; ubyte *y2i = in[0]+w; ubyte *ui = in[1]; ubyte *vi = in[2]; vector unsigned char *oute = (vector unsigned char *)(oplanes[0]+srcSliceY*outstrides[0]); vector unsigned char *outo = (vector unsigned char *)(oplanes[0]+srcSliceY*outstrides[0]+outstrides[0]); instrides_scl[0] = instrides[0]; 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 */ adjout = outstrides[0] - w*3; for (i=0;i>15; // 1.7718*u ux = vec_sl (U, (vector unsigned short)(1)); ux = vec_mradds (ux, (vector signed short)(29029), (vector signed short)(0)); ux0 = vec_mergeh (ux,ux); ux1 = vec_mergel (ux,ux); // vx = (22972*(v<<1)+0x4000)>>15; // 1.4021*v vx = vec_sl (V, (vector unsigned short)(1)); vx = vec_mradds (vx, (vector signed short)(22972), (vector signed short)(0)); vx0 = vec_mergeh (vx,vx); vx1 = vec_mergel (vx,vx); // uvx = ((-11276*u) + (-23400*v))>>15; // -(0.34414*u+0.71414*v) uvx = vec_mradds (U, (vector signed short)(-11276), (vector signed short)(0)); uvx = vec_mradds (V, (vector signed short)(-23400), uvx); uvx0 = vec_mergeh (uvx,uvx); uvx1 = vec_mergel (uvx,uvx); R0 = vec_add (Y0,vx0); G0 = vec_add (Y0,uvx0); B0 = vec_add (Y0,ux0); R1 = vec_add (Y1,vx1); G1 = vec_add (Y1,uvx1); B1 = vec_add (Y1,ux1); R = vec_packs (R0,R1); G = vec_packs (G0,G1); B = vec_packs (B0,B1); out_pixels(R,G,B,oute); R0 = vec_add (Y2,vx0); G0 = vec_add (Y2,uvx0); B0 = vec_add (Y2,ux0); R1 = vec_add (Y3,vx1); G1 = vec_add (Y3,uvx1); B1 = vec_add (Y3,ux1); R = vec_packs (R0,R1); G = vec_packs (G0,G1); B = vec_packs (B0,B1); out_pixels(R,G,B,outo); y1i += 16; y2i += 16; ui += 8; vi += 8; } outo += (outstrides[0])>>4; oute += (outstrides[0])>>4; ui += instrides_scl[1]; vi += instrides_scl[2]; y1i += instrides_scl[0]; y2i += instrides_scl[0]; } return srcSliceH; } /* Ok currently the acceleration routine only supports inputs of widths a multiple of 16 and heights a multiple 2 So we just fall back to the C codes for this. */ SwsFunc yuv2rgb_init_altivec (SwsContext *c) { if (c->srcFormat == IMGFMT_422P) return NULL; if ((c->srcW & 0xf) != 0) return NULL; if ((c->srcH & 0x1) != 0) return NULL; switch(c->dstFormat){ case IMGFMT_RGB24: MSG_WARN("ALTIVEC: Color Space RGB24\n"); return altivec_yuv2_rgb24; case IMGFMT_RGB32: MSG_WARN("ALTIVEC: Color Space RGB32\n"); return altivec_yuv2_argb32; case IMGFMT_BGR32: MSG_WARN("ALTIVEC: Color Space BGR32\n"); return altivec_yuv2_abgr32; default: return NULL; } }