3 * rgb2rgb.c, Software RGB to RGB convertor
4 * pluralize by Software PAL8 to RGB convertor
5 * Software YUV to YUV convertor
6 * Software YUV to RGB convertor
7 * Written by Nick Kurshev.
8 * palette stuff & yuv stuff by Michael
11 #include "../config.h"
13 #include "../mmx_defs.h"
16 static const uint64_t mask32b __attribute__((aligned(8))) = 0x000000FF000000FFULL;
17 static const uint64_t mask32g __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
18 static const uint64_t mask32r __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
19 static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
20 static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
21 static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
22 static const uint64_t mask24hh __attribute__((aligned(8))) = 0xffff000000000000ULL;
23 static const uint64_t mask24hhh __attribute__((aligned(8))) = 0xffffffff00000000ULL;
24 static const uint64_t mask24hhhh __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
25 static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
26 static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
27 static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
28 static const uint64_t red_16mask __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
29 static const uint64_t green_16mask __attribute__((aligned(8)))= 0x000007e0000007e0ULL;
30 static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
31 static const uint64_t red_15mask __attribute__((aligned(8))) = 0x00007c000000f800ULL;
32 static const uint64_t green_15mask __attribute__((aligned(8)))= 0x000003e0000007e0ULL;
33 static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
35 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
36 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
37 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
38 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
40 static uint64_t __attribute__((aligned(8))) dither4[2]={
42 0x0200020002000200LL,};
44 static uint64_t __attribute__((aligned(8))) dither8[2]={
46 0x0004000400040004LL,};
50 void rgb24to32(const uint8_t *src,uint8_t *dst,unsigned src_size)
53 const uint8_t *s = src;
60 __asm __volatile(PREFETCH" %0"::"m"(*s):"memory");
61 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*4))*(MMREG_SIZE*4));
62 __asm __volatile("movq %0, %%mm7"::"m"(mask32):"memory");
63 if(mm_end == end) mm_end -= MMREG_SIZE*4;
69 "punpckldq 3%1, %%mm0\n\t"
71 "punpckldq 9%1, %%mm1\n\t"
72 "movd 12%1, %%mm2\n\t"
73 "punpckldq 15%1, %%mm2\n\t"
74 "movd 18%1, %%mm3\n\t"
75 "punpckldq 21%1, %%mm3\n\t"
76 "pand %%mm7, %%mm0\n\t"
77 "pand %%mm7, %%mm1\n\t"
78 "pand %%mm7, %%mm2\n\t"
79 "pand %%mm7, %%mm3\n\t"
80 MOVNTQ" %%mm0, %0\n\t"
81 MOVNTQ" %%mm1, 8%0\n\t"
82 MOVNTQ" %%mm2, 16%0\n\t"
90 __asm __volatile(SFENCE:::"memory");
91 __asm __volatile(EMMS:::"memory");
102 void rgb32to24(const uint8_t *src,uint8_t *dst,unsigned src_size)
105 const uint8_t *s = src;
112 __asm __volatile(PREFETCH" %0"::"m"(*s):"memory");
113 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*4))*(MMREG_SIZE*4));
119 "movq 8%1, %%mm1\n\t"
120 "movq 16%1, %%mm4\n\t"
121 "movq 24%1, %%mm5\n\t"
122 "movq %%mm0, %%mm2\n\t"
123 "movq %%mm1, %%mm3\n\t"
124 "movq %%mm4, %%mm6\n\t"
125 "movq %%mm5, %%mm7\n\t"
126 "psrlq $8, %%mm2\n\t"
127 "psrlq $8, %%mm3\n\t"
128 "psrlq $8, %%mm6\n\t"
129 "psrlq $8, %%mm7\n\t"
138 "por %%mm2, %%mm0\n\t"
139 "por %%mm3, %%mm1\n\t"
140 "por %%mm6, %%mm4\n\t"
141 "por %%mm7, %%mm5\n\t"
143 "movq %%mm1, %%mm2\n\t"
144 "movq %%mm4, %%mm3\n\t"
145 "psllq $48, %%mm2\n\t"
146 "psllq $32, %%mm3\n\t"
149 "por %%mm2, %%mm0\n\t"
150 "psrlq $16, %%mm1\n\t"
151 "psrlq $32, %%mm4\n\t"
152 "psllq $16, %%mm5\n\t"
153 "por %%mm3, %%mm1\n\t"
155 "por %%mm5, %%mm4\n\t"
157 MOVNTQ" %%mm0, %0\n\t"
158 MOVNTQ" %%mm1, 8%0\n\t"
161 :"m"(*s),"m"(mask24l),
162 "m"(mask24h),"m"(mask24hh),"m"(mask24hhh),"m"(mask24hhhh)
167 __asm __volatile(SFENCE:::"memory");
168 __asm __volatile(EMMS:::"memory");
180 Original by Strepto/Astral
181 ported to gcc & bugfixed : A'rpi
182 MMX2, 3DNOW optimization by Nick Kurshev
183 32bit c version, and and&add trick by Michael Niedermayer
185 void rgb15to16(const uint8_t *src,uint8_t *dst,unsigned src_size)
188 register const char* s=src+src_size;
189 register char* d=dst+src_size;
190 register int offs=-src_size;
191 __asm __volatile(PREFETCH" %0"::"m"(*(s+offs)));
200 "movq 8%1, %%mm2\n\t"
201 "movq %%mm0, %%mm1\n\t"
202 "movq %%mm2, %%mm3\n\t"
203 "pand %%mm4, %%mm0\n\t"
204 "pand %%mm4, %%mm2\n\t"
205 "paddw %%mm1, %%mm0\n\t"
206 "paddw %%mm3, %%mm2\n\t"
207 MOVNTQ" %%mm0, %0\n\t"
214 __asm __volatile(SFENCE:::"memory");
215 __asm __volatile(EMMS:::"memory");
218 const uint16_t *s1=( uint16_t * )src;
219 uint16_t *d1=( uint16_t * )dst;
220 uint16_t *e=((uint8_t *)s1)+src_size;
222 register int x=*( s1++ );
225 0111 1111 1110 0000=0x7FE0
226 00000000000001 1111=0x001F */
227 *( d1++ )=( x&0x001F )|( ( x&0x7FE0 )<<1 );
230 const unsigned *s1=( unsigned * )src;
231 unsigned *d1=( unsigned * )dst;
233 int size= src_size>>2;
234 for(i=0; i<size; i++)
236 register int x= s1[i];
237 // d1[i] = x + (x&0x7FE07FE0); //faster but need msbit =0 which might not allways be true
238 d1[i] = (x&0x7FFF7FFF) + (x&0x7FE07FE0);
246 * Pallete is assumed to contain bgr32
248 void palette8torgb32(const uint8_t *src, uint8_t *dst, unsigned num_pixels, const uint8_t *palette)
251 for(i=0; i<num_pixels; i++)
252 ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
256 * Pallete is assumed to contain bgr32
258 void palette8torgb24(const uint8_t *src, uint8_t *dst, unsigned num_pixels, const uint8_t *palette)
262 writes 1 byte o much and might cause alignment issues on some architectures?
263 for(i=0; i<num_pixels; i++)
264 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
266 for(i=0; i<num_pixels; i++)
269 dst[0]= palette[ src[i]*4+0 ];
270 dst[1]= palette[ src[i]*4+1 ];
271 dst[2]= palette[ src[i]*4+2 ];
276 void rgb32to16(const uint8_t *src, uint8_t *dst, unsigned src_size)
279 const uint8_t *s = src;
280 const uint8_t *end,*mm_end;
281 uint16_t *d = (uint16_t *)dst;
283 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
284 __asm __volatile(PREFETCH" %0"::"m"(*src):"memory");
288 ::"m"(red_16mask),"m"(green_16mask));
294 "movd 4%1, %%mm3\n\t"
295 "punpckldq 8%1, %%mm0\n\t"
296 "punpckldq 12%1, %%mm3\n\t"
297 "movq %%mm0, %%mm1\n\t"
298 "movq %%mm0, %%mm2\n\t"
299 "movq %%mm3, %%mm4\n\t"
300 "movq %%mm3, %%mm5\n\t"
301 "psrlq $3, %%mm0\n\t"
302 "psrlq $3, %%mm3\n\t"
305 "psrlq $5, %%mm1\n\t"
306 "psrlq $5, %%mm4\n\t"
307 "pand %%mm6, %%mm1\n\t"
308 "pand %%mm6, %%mm4\n\t"
309 "psrlq $8, %%mm2\n\t"
310 "psrlq $8, %%mm5\n\t"
311 "pand %%mm7, %%mm2\n\t"
312 "pand %%mm7, %%mm5\n\t"
313 "por %%mm1, %%mm0\n\t"
314 "por %%mm4, %%mm3\n\t"
315 "por %%mm2, %%mm0\n\t"
316 "por %%mm5, %%mm3\n\t"
317 "psllq $16, %%mm3\n\t"
318 "por %%mm3, %%mm0\n\t"
319 MOVNTQ" %%mm0, %0\n\t"
320 :"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
329 *d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
331 __asm __volatile(SFENCE:::"memory");
332 __asm __volatile(EMMS:::"memory");
334 unsigned j,i,num_pixels=src_size/4;
335 uint16_t *d = (uint16_t *)dst;
336 for(i=0,j=0; j<num_pixels; i+=4,j++)
338 const int b= src[i+0];
339 const int g= src[i+1];
340 const int r= src[i+2];
342 d[j]= (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
347 void rgb32to15(const uint8_t *src, uint8_t *dst, unsigned src_size)
350 const uint8_t *s = src;
351 const uint8_t *end,*mm_end;
352 uint16_t *d = (uint16_t *)dst;
354 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
355 __asm __volatile(PREFETCH" %0"::"m"(*src):"memory");
359 ::"m"(red_15mask),"m"(green_15mask));
365 "movd 4%1, %%mm3\n\t"
366 "punpckldq 8%1, %%mm0\n\t"
367 "punpckldq 12%1, %%mm3\n\t"
368 "movq %%mm0, %%mm1\n\t"
369 "movq %%mm0, %%mm2\n\t"
370 "movq %%mm3, %%mm4\n\t"
371 "movq %%mm3, %%mm5\n\t"
372 "psrlq $3, %%mm0\n\t"
373 "psrlq $3, %%mm3\n\t"
376 "psrlq $6, %%mm1\n\t"
377 "psrlq $6, %%mm4\n\t"
378 "pand %%mm6, %%mm1\n\t"
379 "pand %%mm6, %%mm4\n\t"
380 "psrlq $9, %%mm2\n\t"
381 "psrlq $9, %%mm5\n\t"
382 "pand %%mm7, %%mm2\n\t"
383 "pand %%mm7, %%mm5\n\t"
384 "por %%mm1, %%mm0\n\t"
385 "por %%mm4, %%mm3\n\t"
386 "por %%mm2, %%mm0\n\t"
387 "por %%mm5, %%mm3\n\t"
388 "psllq $16, %%mm3\n\t"
389 "por %%mm3, %%mm0\n\t"
390 MOVNTQ" %%mm0, %0\n\t"
391 :"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
400 *d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
402 __asm __volatile(SFENCE:::"memory");
403 __asm __volatile(EMMS:::"memory");
405 unsigned j,i,num_pixels=src_size/4;
406 uint16_t *d = (uint16_t *)dst;
407 for(i=0,j=0; j<num_pixels; i+=4,j++)
409 const int b= src[i+0];
410 const int g= src[i+1];
411 const int r= src[i+2];
413 d[j]= (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
418 void rgb24to16(const uint8_t *src, uint8_t *dst, unsigned src_size)
421 const uint8_t *s = src;
422 const uint8_t *end,*mm_end;
423 uint16_t *d = (uint16_t *)dst;
425 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
426 __asm __volatile(PREFETCH" %0"::"m"(*src):"memory");
430 ::"m"(red_16mask),"m"(green_16mask));
431 if(mm_end == end) mm_end -= MMREG_SIZE*2;
437 "movd 3%1, %%mm3\n\t"
438 "punpckldq 6%1, %%mm0\n\t"
439 "punpckldq 9%1, %%mm3\n\t"
440 "movq %%mm0, %%mm1\n\t"
441 "movq %%mm0, %%mm2\n\t"
442 "movq %%mm3, %%mm4\n\t"
443 "movq %%mm3, %%mm5\n\t"
444 "psrlq $3, %%mm0\n\t"
445 "psrlq $3, %%mm3\n\t"
448 "psrlq $5, %%mm1\n\t"
449 "psrlq $5, %%mm4\n\t"
450 "pand %%mm6, %%mm1\n\t"
451 "pand %%mm6, %%mm4\n\t"
452 "psrlq $8, %%mm2\n\t"
453 "psrlq $8, %%mm5\n\t"
454 "pand %%mm7, %%mm2\n\t"
455 "pand %%mm7, %%mm5\n\t"
456 "por %%mm1, %%mm0\n\t"
457 "por %%mm4, %%mm3\n\t"
458 "por %%mm2, %%mm0\n\t"
459 "por %%mm5, %%mm3\n\t"
460 "psllq $16, %%mm3\n\t"
461 "por %%mm3, %%mm0\n\t"
462 MOVNTQ" %%mm0, %0\n\t"
463 :"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
472 *d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
474 __asm __volatile(SFENCE:::"memory");
475 __asm __volatile(EMMS:::"memory");
477 unsigned j,i,num_pixels=src_size/3;
478 uint16_t *d = (uint16_t *)dst;
479 for(i=0,j=0; j<num_pixels; i+=3,j++)
481 const int b= src[i+0];
482 const int g= src[i+1];
483 const int r= src[i+2];
485 d[j]= (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
490 void rgb24to15(const uint8_t *src, uint8_t *dst, unsigned src_size)
493 const uint8_t *s = src;
494 const uint8_t *end,*mm_end;
495 uint16_t *d = (uint16_t *)dst;
497 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
498 __asm __volatile(PREFETCH" %0"::"m"(*src):"memory");
502 ::"m"(red_15mask),"m"(green_15mask));
503 if(mm_end == end) mm_end -= MMREG_SIZE*2;
509 "movd 3%1, %%mm3\n\t"
510 "punpckldq 6%1, %%mm0\n\t"
511 "punpckldq 9%1, %%mm3\n\t"
512 "movq %%mm0, %%mm1\n\t"
513 "movq %%mm0, %%mm2\n\t"
514 "movq %%mm3, %%mm4\n\t"
515 "movq %%mm3, %%mm5\n\t"
516 "psrlq $3, %%mm0\n\t"
517 "psrlq $3, %%mm3\n\t"
520 "psrlq $6, %%mm1\n\t"
521 "psrlq $6, %%mm4\n\t"
522 "pand %%mm6, %%mm1\n\t"
523 "pand %%mm6, %%mm4\n\t"
524 "psrlq $9, %%mm2\n\t"
525 "psrlq $9, %%mm5\n\t"
526 "pand %%mm7, %%mm2\n\t"
527 "pand %%mm7, %%mm5\n\t"
528 "por %%mm1, %%mm0\n\t"
529 "por %%mm4, %%mm3\n\t"
530 "por %%mm2, %%mm0\n\t"
531 "por %%mm5, %%mm3\n\t"
532 "psllq $16, %%mm3\n\t"
533 "por %%mm3, %%mm0\n\t"
534 MOVNTQ" %%mm0, %0\n\t"
535 :"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
544 *d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
546 __asm __volatile(SFENCE:::"memory");
547 __asm __volatile(EMMS:::"memory");
549 unsigned j,i,num_pixels=src_size/3;
550 uint16_t *d = (uint16_t *)dst;
551 for(i=0,j=0; j<num_pixels; i+=3,j++)
553 const int b= src[i+0];
554 const int g= src[i+1];
555 const int r= src[i+2];
557 d[j]= (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
563 * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
565 void palette8torgb16(const uint8_t *src, uint8_t *dst, unsigned num_pixels, const uint8_t *palette)
568 for(i=0; i<num_pixels; i++)
569 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
573 * Pallete is assumed to contain bgr15, see rgb32to15 to convert the palette
575 void palette8torgb15(const uint8_t *src, uint8_t *dst, unsigned num_pixels, const uint8_t *palette)
578 for(i=0; i<num_pixels; i++)
579 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
582 void rgb32tobgr32(const uint8_t *src, uint8_t *dst, unsigned int src_size)
584 int num_pixels= src_size >> 2;
587 "xorl %%eax, %%eax \n\t"
590 PREFETCH" 32(%0, %%eax) \n\t"
591 "movq (%0, %%eax), %%mm0 \n\t"
592 "movq %%mm0, %%mm1 \n\t"
593 "movq %%mm0, %%mm2 \n\t"
594 "pslld $16, %%mm0 \n\t"
595 "psrld $16, %%mm1 \n\t"
596 "pand mask32r, %%mm0 \n\t"
597 "pand mask32g, %%mm2 \n\t"
598 "pand mask32b, %%mm1 \n\t"
599 "por %%mm0, %%mm2 \n\t"
600 "por %%mm1, %%mm2 \n\t"
601 MOVNTQ" %%mm2, (%1, %%eax) \n\t"
602 "addl $2, %%eax \n\t"
603 "cmpl %2, %%eax \n\t"
605 :: "r" (src), "r"(dst), "r" (num_pixels)
609 __asm __volatile(SFENCE:::"memory");
610 __asm __volatile(EMMS:::"memory");
613 for(i=0; i<num_pixels; i++)
615 dst[4*i + 0] = src[4*i + 2];
616 dst[4*i + 1] = src[4*i + 1];
617 dst[4*i + 2] = src[4*i + 0];
624 * height should be a multiple of 2 and width should be a multiple of 16 (if this is a
625 * problem for anyone then tell me, and ill fix it)
627 void yv12toyuy2(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
628 unsigned int width, unsigned int height,
629 unsigned int lumStride, unsigned int chromStride, unsigned int dstStride)
632 const int chromWidth= width>>1;
633 for(y=0; y<height; y++)
636 //FIXME handle 2 lines a once (fewer prefetch, reuse some chrom, but very likely limited by mem anyway)
638 "xorl %%eax, %%eax \n\t"
641 PREFETCH" 32(%1, %%eax, 2) \n\t"
642 PREFETCH" 32(%2, %%eax) \n\t"
643 PREFETCH" 32(%3, %%eax) \n\t"
644 "movq (%2, %%eax), %%mm0 \n\t" // U(0)
645 "movq %%mm0, %%mm2 \n\t" // U(0)
646 "movq (%3, %%eax), %%mm1 \n\t" // V(0)
647 "punpcklbw %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
648 "punpckhbw %%mm1, %%mm2 \n\t" // UVUV UVUV(8)
650 "movq (%1, %%eax,2), %%mm3 \n\t" // Y(0)
651 "movq 8(%1, %%eax,2), %%mm5 \n\t" // Y(8)
652 "movq %%mm3, %%mm4 \n\t" // Y(0)
653 "movq %%mm5, %%mm6 \n\t" // Y(8)
654 "punpcklbw %%mm0, %%mm3 \n\t" // YUYV YUYV(0)
655 "punpckhbw %%mm0, %%mm4 \n\t" // YUYV YUYV(4)
656 "punpcklbw %%mm2, %%mm5 \n\t" // YUYV YUYV(8)
657 "punpckhbw %%mm2, %%mm6 \n\t" // YUYV YUYV(12)
659 MOVNTQ" %%mm3, (%0, %%eax, 4) \n\t"
660 MOVNTQ" %%mm4, 8(%0, %%eax, 4) \n\t"
661 MOVNTQ" %%mm5, 16(%0, %%eax, 4) \n\t"
662 MOVNTQ" %%mm6, 24(%0, %%eax, 4) \n\t"
664 "addl $8, %%eax \n\t"
665 "cmpl %4, %%eax \n\t"
667 ::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "r" (chromWidth)
672 for(i=0; i<chromWidth; i++)
674 dst[4*i+0] = ysrc[2*i+0];
675 dst[4*i+1] = usrc[i];
676 dst[4*i+2] = ysrc[2*i+1];
677 dst[4*i+3] = vsrc[i];
697 * height should be a multiple of 2 and width should be a multiple of 16 (if this is a
698 * problem for anyone then tell me, and ill fix it)
700 void yuy2toyv12(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
701 unsigned int width, unsigned int height,
702 unsigned int lumStride, unsigned int chromStride, unsigned int srcStride)
705 const int chromWidth= width>>1;
706 for(y=0; y<height; y+=2)
710 "xorl %%eax, %%eax \n\t"
711 "pcmpeqw %%mm7, %%mm7 \n\t"
712 "psrlw $8, %%mm7 \n\t" // FF,00,FF,00...
715 PREFETCH" 64(%0, %%eax, 4) \n\t"
716 "movq (%0, %%eax, 4), %%mm0 \n\t" // YUYV YUYV(0)
717 "movq 8(%0, %%eax, 4), %%mm1 \n\t" // YUYV YUYV(4)
718 "movq %%mm0, %%mm2 \n\t" // YUYV YUYV(0)
719 "movq %%mm1, %%mm3 \n\t" // YUYV YUYV(4)
720 "psrlw $8, %%mm0 \n\t" // U0V0 U0V0(0)
721 "psrlw $8, %%mm1 \n\t" // U0V0 U0V0(4)
722 "pand %%mm7, %%mm2 \n\t" // Y0Y0 Y0Y0(0)
723 "pand %%mm7, %%mm3 \n\t" // Y0Y0 Y0Y0(4)
724 "packuswb %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
725 "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(0)
727 MOVNTQ" %%mm2, (%1, %%eax, 2) \n\t"
729 "movq 16(%0, %%eax, 4), %%mm1 \n\t" // YUYV YUYV(8)
730 "movq 24(%0, %%eax, 4), %%mm2 \n\t" // YUYV YUYV(12)
731 "movq %%mm1, %%mm3 \n\t" // YUYV YUYV(8)
732 "movq %%mm2, %%mm4 \n\t" // YUYV YUYV(12)
733 "psrlw $8, %%mm1 \n\t" // U0V0 U0V0(8)
734 "psrlw $8, %%mm2 \n\t" // U0V0 U0V0(12)
735 "pand %%mm7, %%mm3 \n\t" // Y0Y0 Y0Y0(8)
736 "pand %%mm7, %%mm4 \n\t" // Y0Y0 Y0Y0(12)
737 "packuswb %%mm2, %%mm1 \n\t" // UVUV UVUV(8)
738 "packuswb %%mm4, %%mm3 \n\t" // YYYY YYYY(8)
740 MOVNTQ" %%mm3, 8(%1, %%eax, 2) \n\t"
742 "movq %%mm0, %%mm2 \n\t" // UVUV UVUV(0)
743 "movq %%mm1, %%mm3 \n\t" // UVUV UVUV(8)
744 "psrlw $8, %%mm0 \n\t" // V0V0 V0V0(0)
745 "psrlw $8, %%mm1 \n\t" // V0V0 V0V0(8)
746 "pand %%mm7, %%mm2 \n\t" // U0U0 U0U0(0)
747 "pand %%mm7, %%mm3 \n\t" // U0U0 U0U0(8)
748 "packuswb %%mm1, %%mm0 \n\t" // VVVV VVVV(0)
749 "packuswb %%mm3, %%mm2 \n\t" // UUUU UUUU(0)
751 MOVNTQ" %%mm0, (%3, %%eax) \n\t"
752 MOVNTQ" %%mm2, (%2, %%eax) \n\t"
754 "addl $8, %%eax \n\t"
755 "cmpl %4, %%eax \n\t"
757 ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "r" (chromWidth)
765 "xorl %%eax, %%eax \n\t"
768 PREFETCH" 64(%0, %%eax, 4) \n\t"
769 "movq (%0, %%eax, 4), %%mm0 \n\t" // YUYV YUYV(0)
770 "movq 8(%0, %%eax, 4), %%mm1 \n\t" // YUYV YUYV(4)
771 "movq 16(%0, %%eax, 4), %%mm2 \n\t" // YUYV YUYV(8)
772 "movq 24(%0, %%eax, 4), %%mm3 \n\t" // YUYV YUYV(12)
773 "pand %%mm7, %%mm0 \n\t" // Y0Y0 Y0Y0(0)
774 "pand %%mm7, %%mm1 \n\t" // Y0Y0 Y0Y0(4)
775 "pand %%mm7, %%mm2 \n\t" // Y0Y0 Y0Y0(8)
776 "pand %%mm7, %%mm3 \n\t" // Y0Y0 Y0Y0(12)
777 "packuswb %%mm1, %%mm0 \n\t" // YYYY YYYY(0)
778 "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(8)
780 MOVNTQ" %%mm0, (%1, %%eax, 2) \n\t"
781 MOVNTQ" %%mm2, 8(%1, %%eax, 2) \n\t"
783 "addl $8, %%eax \n\t"
784 "cmpl %4, %%eax \n\t"
787 ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "r" (chromWidth)
792 for(i=0; i<chromWidth; i++)
794 ydst[2*i+0] = src[4*i+0];
795 udst[i] = src[4*i+1];
796 ydst[2*i+1] = src[4*i+2];
797 vdst[i] = src[4*i+3];
802 for(i=0; i<chromWidth; i++)
804 ydst[2*i+0] = src[4*i+0];
805 ydst[2*i+1] = src[4*i+2];
814 asm volatile( EMMS" \n\t"
822 * height should be a multiple of 2 and width should be a multiple of 16 (if this is a
823 * problem for anyone then tell me, and ill fix it)
825 void uyvytoyv12(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
826 unsigned int width, unsigned int height,
827 unsigned int lumStride, unsigned int chromStride, unsigned int srcStride)
830 const int chromWidth= width>>1;
831 for(y=0; y<height; y+=2)
835 "xorl %%eax, %%eax \n\t"
836 "pcmpeqw %%mm7, %%mm7 \n\t"
837 "psrlw $8, %%mm7 \n\t" // FF,00,FF,00...
840 PREFETCH" 64(%0, %%eax, 4) \n\t"
841 "movq (%0, %%eax, 4), %%mm0 \n\t" // UYVY UYVY(0)
842 "movq 8(%0, %%eax, 4), %%mm1 \n\t" // UYVY UYVY(4)
843 "movq %%mm0, %%mm2 \n\t" // UYVY UYVY(0)
844 "movq %%mm1, %%mm3 \n\t" // UYVY UYVY(4)
845 "pand %%mm7, %%mm0 \n\t" // U0V0 U0V0(0)
846 "pand %%mm7, %%mm1 \n\t" // U0V0 U0V0(4)
847 "psrlw $8, %%mm2 \n\t" // Y0Y0 Y0Y0(0)
848 "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(4)
849 "packuswb %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
850 "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(0)
852 MOVNTQ" %%mm2, (%1, %%eax, 2) \n\t"
854 "movq 16(%0, %%eax, 4), %%mm1 \n\t" // UYVY UYVY(8)
855 "movq 24(%0, %%eax, 4), %%mm2 \n\t" // UYVY UYVY(12)
856 "movq %%mm1, %%mm3 \n\t" // UYVY UYVY(8)
857 "movq %%mm2, %%mm4 \n\t" // UYVY UYVY(12)
858 "pand %%mm7, %%mm1 \n\t" // U0V0 U0V0(8)
859 "pand %%mm7, %%mm2 \n\t" // U0V0 U0V0(12)
860 "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(8)
861 "psrlw $8, %%mm4 \n\t" // Y0Y0 Y0Y0(12)
862 "packuswb %%mm2, %%mm1 \n\t" // UVUV UVUV(8)
863 "packuswb %%mm4, %%mm3 \n\t" // YYYY YYYY(8)
865 MOVNTQ" %%mm3, 8(%1, %%eax, 2) \n\t"
867 "movq %%mm0, %%mm2 \n\t" // UVUV UVUV(0)
868 "movq %%mm1, %%mm3 \n\t" // UVUV UVUV(8)
869 "psrlw $8, %%mm0 \n\t" // V0V0 V0V0(0)
870 "psrlw $8, %%mm1 \n\t" // V0V0 V0V0(8)
871 "pand %%mm7, %%mm2 \n\t" // U0U0 U0U0(0)
872 "pand %%mm7, %%mm3 \n\t" // U0U0 U0U0(8)
873 "packuswb %%mm1, %%mm0 \n\t" // VVVV VVVV(0)
874 "packuswb %%mm3, %%mm2 \n\t" // UUUU UUUU(0)
876 MOVNTQ" %%mm0, (%3, %%eax) \n\t"
877 MOVNTQ" %%mm2, (%2, %%eax) \n\t"
879 "addl $8, %%eax \n\t"
880 "cmpl %4, %%eax \n\t"
882 ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "r" (chromWidth)
890 "xorl %%eax, %%eax \n\t"
893 PREFETCH" 64(%0, %%eax, 4) \n\t"
894 "movq (%0, %%eax, 4), %%mm0 \n\t" // YUYV YUYV(0)
895 "movq 8(%0, %%eax, 4), %%mm1 \n\t" // YUYV YUYV(4)
896 "movq 16(%0, %%eax, 4), %%mm2 \n\t" // YUYV YUYV(8)
897 "movq 24(%0, %%eax, 4), %%mm3 \n\t" // YUYV YUYV(12)
898 "psrlw $8, %%mm0 \n\t" // Y0Y0 Y0Y0(0)
899 "psrlw $8, %%mm1 \n\t" // Y0Y0 Y0Y0(4)
900 "psrlw $8, %%mm2 \n\t" // Y0Y0 Y0Y0(8)
901 "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(12)
902 "packuswb %%mm1, %%mm0 \n\t" // YYYY YYYY(0)
903 "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(8)
905 MOVNTQ" %%mm0, (%1, %%eax, 2) \n\t"
906 MOVNTQ" %%mm2, 8(%1, %%eax, 2) \n\t"
908 "addl $8, %%eax \n\t"
909 "cmpl %4, %%eax \n\t"
912 ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "r" (chromWidth)
917 for(i=0; i<chromWidth; i++)
919 udst[i] = src[4*i+0];
920 ydst[2*i+0] = src[4*i+1];
921 vdst[i] = src[4*i+2];
922 ydst[2*i+1] = src[4*i+3];
927 for(i=0; i<chromWidth; i++)
929 ydst[2*i+0] = src[4*i+1];
930 ydst[2*i+1] = src[4*i+3];
939 asm volatile( EMMS" \n\t"