More elegant solution
[libav.git] / postproc / rgb2rgb.c
CommitLineData
b234ae81
NK
1#include <inttypes.h>
2#include "../config.h"
3#include "rgb2rgb.h"
b238eb2e 4#include "mmx.h"
90f8a479 5#include "../mmx_defs.h"
79811694 6
b234ae81
NK
7void rgb24to32(uint8_t *src,uint8_t *dst,uint32_t src_size)
8{
fde33ab5 9 uint8_t *dest = dst;
b234ae81
NK
10 uint8_t *s = src;
11 uint8_t *end;
49a0c6ee
NK
12#ifdef HAVE_MMX
13 const uint64_t mask32 = 0x00FFFFFF00FFFFFFULL;
14 uint8_t *mm_end;
15#endif
b234ae81 16 end = s + src_size;
49a0c6ee 17#ifdef HAVE_MMX
90f8a479
NK
18 __asm __volatile(PREFETCH" %0\n\t"::"m"(*s):"memory");
19 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
49a0c6ee 20 __asm __volatile("movq %0, %%mm7"::"m"(mask32):"memory");
90f8a479 21 if(mm_end == end) mm_end -= MMREG_SIZE*2;
49a0c6ee
NK
22 while(s < mm_end)
23 {
24 __asm __volatile(
90f8a479 25 PREFETCH" 32%1\n\t"
49a0c6ee
NK
26 "movd %1, %%mm0\n\t"
27 "movd 3%1, %%mm1\n\t"
28 "movd 6%1, %%mm2\n\t"
29 "movd 9%1, %%mm3\n\t"
30 "punpckldq %%mm1, %%mm0\n\t"
31 "punpckldq %%mm3, %%mm2\n\t"
32 "pand %%mm7, %%mm0\n\t"
33 "pand %%mm7, %%mm2\n\t"
96b956cc
NK
34 MOVNTQ" %%mm0, %0\n\t"
35 MOVNTQ" %%mm2, 8%0"
49a0c6ee
NK
36 :"=m"(*dest)
37 :"m"(*s)
38 :"memory");
39 dest += 16;
40 s += 12;
41 }
79811694 42 __asm __volatile(SFENCE:::"memory");
96b956cc 43 __asm __volatile(EMMS:::"memory");
49a0c6ee 44#endif
b234ae81
NK
45 while(s < end)
46 {
fde33ab5
NK
47 *dest++ = *s++;
48 *dest++ = *s++;
49 *dest++ = *s++;
50 *dest++ = 0;
b234ae81
NK
51 }
52}
59ac5a93 53
49a0c6ee 54/* TODO: MMX optimization */
59ac5a93
NK
55void rgb32to24(uint8_t *src,uint8_t *dst,uint32_t src_size)
56{
57 uint8_t *dest = dst;
58 uint8_t *s = src;
59 uint8_t *end;
60 end = s + src_size;
61 while(s < end)
62 {
63 *dest++ = *s++;
64 *dest++ = *s++;
65 *dest++ = *s++;
66 s++;
67 }
68}
b238eb2e
NK
69
70/* Original by Strepto/Astral
71 ported to gcc & bugfixed : A'rpi */
72void rgb15to16(uint8_t *src,uint8_t *dst,uint32_t src_size)
73{
74#ifdef HAVE_MMX
75 static uint64_t mask_b = 0x001F001F001F001FLL; // 00000000 00011111 xxB
76 static uint64_t mask_rg = 0x7FE07FE07FE07FE0LL; // 01111111 11100000 RGx
77 register char* s=src+src_size;
78 register char* d=dst+src_size;
79 register int offs=-src_size;
80 movq_m2r (mask_b, mm4);
81 movq_m2r (mask_rg, mm5);
82 while(offs<0){
83 movq_m2r (*(s+offs), mm0);
84 movq_r2r (mm0, mm1);
85
86 movq_m2r (*(s+8+offs), mm2);
87 movq_r2r (mm2, mm3);
88
89 pand_r2r (mm4, mm0);
90 pand_r2r (mm5, mm1);
91
92 psllq_i2r(1,mm1);
93 pand_r2r (mm4, mm2);
94
95 pand_r2r (mm5, mm3);
96 por_r2r (mm1, mm0);
97
98 psllq_i2r(1,mm3);
99 movq_r2m (mm0,*(d+offs));
100
101 por_r2r (mm3,mm2);
102 movq_r2m (mm2,*(d+8+offs));
103
104 offs+=16;
105 }
106 emms();
107#else
108 uint16_t *s1=( uint16_t * )src;
109 uint16_t *d1=( uint16_t * )dst;
110 uint16_t *e=((uint8_t *)s1)+src_size;
111 while( s1<e ){
112 register int x=*( s1++ );
113 /* rrrrrggggggbbbbb
114 0rrrrrgggggbbbbb
115 0111 1111 1110 0000=0x7FE0
116 00000000000001 1111=0x001F */
117 *( d1++ )=( x&0x001F )|( ( x&0x7FE0 )<<1 );
118 }
119#endif
120}