3 * rgb2rgb.c, Software RGB to RGB convertor
4 * Written by Nick Kurshev.
5 * palette stuff & yuv stuff by Michael
10 #include "../mmx_defs.h"
13 static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
14 static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
15 static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
16 static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
17 static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
18 static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
21 void rgb24to32(const uint8_t *src,uint8_t *dst,uint32_t src_size)
24 const uint8_t *s = src;
31 __asm __volatile(PREFETCH" %0"::"m"(*s):"memory");
32 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
33 __asm __volatile("movq %0, %%mm7"::"m"(mask32):"memory");
34 if(mm_end == end) mm_end -= MMREG_SIZE*2;
43 "punpckldq %%mm1, %%mm0\n\t"
44 "punpckldq %%mm3, %%mm2\n\t"
45 "pand %%mm7, %%mm0\n\t"
46 "pand %%mm7, %%mm2\n\t"
47 MOVNTQ" %%mm0, %0\n\t"
55 __asm __volatile(SFENCE:::"memory");
56 __asm __volatile(EMMS:::"memory");
67 void rgb32to24(const uint8_t *src,uint8_t *dst,uint32_t src_size)
70 const uint8_t *s = src;
77 __asm __volatile(PREFETCH" %0"::"m"(*s):"memory");
78 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
82 ::"m"(mask24l),"m"(mask24h):"memory");
83 if(mm_end == end) mm_end -= MMREG_SIZE*2;
90 "movq %%mm0, %%mm2\n\t"
91 "movq %%mm1, %%mm3\n\t"
94 "pand %%mm7, %%mm0\n\t"
95 "pand %%mm7, %%mm1\n\t"
96 "pand %%mm6, %%mm2\n\t"
97 "pand %%mm6, %%mm3\n\t"
98 "por %%mm2, %%mm0\n\t"
99 "por %%mm3, %%mm1\n\t"
100 MOVNTQ" %%mm0, %0\n\t"
108 __asm __volatile(SFENCE:::"memory");
109 __asm __volatile(EMMS:::"memory");
121 Original by Strepto/Astral
122 ported to gcc & bugfixed : A'rpi
123 MMX2, 3DNOW optimization by Nick Kurshev
124 32bit c version, and and&add trick by Michael Niedermayer
126 void rgb15to16(const uint8_t *src,uint8_t *dst,uint32_t src_size)
129 register const char* s=src+src_size;
130 register char* d=dst+src_size;
131 register int offs=-src_size;
132 __asm __volatile(PREFETCH" %0"::"m"(*(s+offs)));
141 "movq 8%1, %%mm2\n\t"
142 "movq %%mm0, %%mm1\n\t"
143 "movq %%mm2, %%mm3\n\t"
144 "pand %%mm4, %%mm0\n\t"
145 "pand %%mm4, %%mm2\n\t"
146 "paddw %%mm1, %%mm0\n\t"
147 "paddw %%mm3, %%mm2\n\t"
148 MOVNTQ" %%mm0, %0\n\t"
155 __asm __volatile(SFENCE:::"memory");
156 __asm __volatile(EMMS:::"memory");
159 const uint16_t *s1=( uint16_t * )src;
160 uint16_t *d1=( uint16_t * )dst;
161 uint16_t *e=((uint8_t *)s1)+src_size;
163 register int x=*( s1++ );
166 0111 1111 1110 0000=0x7FE0
167 00000000000001 1111=0x001F */
168 *( d1++ )=( x&0x001F )|( ( x&0x7FE0 )<<1 );
171 const uint32_t *s1=( uint32_t * )src;
172 uint32_t *d1=( uint32_t * )dst;
174 int size= src_size>>2;
175 for(i=0; i<size; i++)
177 register int x= s1[i];
178 // d1[i] = x + (x&0x7FE07FE0); //faster but need msbit =0 which might not allways be true
179 d1[i] = (x&0x7FFF7FFF) + (x&0x7FE07FE0);
187 * Pallete is assumed to contain bgr32
189 void palette8torgb32(uint8_t *src, uint8_t *dst, int num_pixels, uint8_t *palette)
192 for(i=0; i<num_pixels; i++)
193 ((uint32_t *)dst)[i] = ((uint32_t *)palette)[ src[i] ];
197 * Pallete is assumed to contain bgr32
199 void palette8torgb24(uint8_t *src, uint8_t *dst, int num_pixels, uint8_t *palette)
203 writes 1 byte o much and might cause alignment issues on some architectures?
204 for(i=0; i<num_pixels; i++)
205 ((uint32_t *)(&dst[i*3])) = ((uint32_t *)palette)[ src[i] ];
207 for(i=0; i<num_pixels; i++)
210 dst[0]= palette[ src[i]*4+0 ];
211 dst[1]= palette[ src[i]*4+1 ];
212 dst[2]= palette[ src[i]*4+2 ];
217 void rgb32to16(uint8_t *src, uint8_t *dst, int num_pixels)
220 for(i=0; i<num_pixels; i+=4)
222 const int b= src[i+0];
223 const int g= src[i+1];
224 const int r= src[i+2];
226 ((uint16_t *)dst)[i]= (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
230 void rgb32to15(uint8_t *src, uint8_t *dst, int num_pixels)
233 for(i=0; i<num_pixels; i+=4)
235 const int b= src[i+0];
236 const int g= src[i+1];
237 const int r= src[i+2];
239 ((uint16_t *)dst)[i]= (b>>3) | ((g&0xF8)<<3) | ((r&0xF8)<<7);
245 * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
247 void palette8torgb16(uint8_t *src, uint8_t *dst, int num_pixels, uint8_t *palette)
250 for(i=0; i<num_pixels; i++)
251 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
255 * Pallete is assumed to contain bgr15, see rgb32to15 to convert the palette
257 void palette8torgb15(uint8_t *src, uint8_t *dst, int num_pixels, uint8_t *palette)
260 for(i=0; i<num_pixels; i++)
261 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
265 * num_pixels must be a multiple of 16 for the MMX version
267 void yv12toyuy2(uint8_t *ysrc, uint8_t *usrc, uint8_t *vsrc, uint8_t *dst, int num_pixels)
271 "xorl %%eax, %%eax \n\t"
273 PREFETCH" 32(%1, %%eax, 2) \n\t"
274 PREFETCH" 32(%2, %%eax) \n\t"
275 PREFETCH" 32(%3, %%eax) \n\t"
276 "movq (%2, %%eax), %%mm0 \n\t" // U(0)
277 "movq %%mm0, %%mm2 \n\t" // U(0)
278 "movq (%3, %%eax), %%mm1 \n\t" // V(0)
279 "punpcklbw %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
280 "punpckhbw %%mm1, %%mm2 \n\t" // UVUV UVUV(8)
282 "movq (%1, %%eax,2), %%mm3 \n\t" // Y(0)
283 "movq 8(%1, %%eax,2), %%mm5 \n\t" // Y(8)
284 "movq %%mm3, %%mm4 \n\t" // Y(0)
285 "movq %%mm5, %%mm6 \n\t" // Y(8)
286 "punpcklbw %%mm0, %%mm3 \n\t" // YUYV YUYV(0)
287 "punpckhbw %%mm0, %%mm4 \n\t" // YUYV YUYV(4)
288 "punpcklbw %%mm2, %%mm5 \n\t" // YUYV YUYV(8)
289 "punpckhbw %%mm2, %%mm6 \n\t" // YUYV YUYV(12)
291 MOVNTQ" %%mm3, (%0, %%eax, 4) \n\t"
292 MOVNTQ" %%mm4, 8(%0, %%eax, 4) \n\t"
293 MOVNTQ" %%mm5, 16(%0, %%eax, 4) \n\t"
294 MOVNTQ" %%mm6, 24(%0, %%eax, 4) \n\t"
296 "addl $8, %%eax \n\t"
297 "cmpl %4, %%eax \n\t"
301 ::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "r" (num_pixels>>1)
308 for(i=0; i<num_pixels; i++)
310 dst[4*i+0] = ysrc[2*i+0];
311 dst[4*i+1] = usrc[i];
312 dst[4*i+2] = ysrc[2*i+1];
313 dst[4*i+3] = vsrc[i];
318 void yuy2toyv12(uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst, int num_pixels)
322 for(i=0; i<num_pixels; i++)
324 ydst[2*i+0] = src[4*i+0];
325 udst[i] = src[4*i+1];
326 ydst[2*i+1] = src[4*i+2];
327 vdst[i] = src[4*i+3];