]> git.sesse.net Git - ffmpeg/blob - postproc/rgb2rgb.c
preprocessor cleanup
[ffmpeg] / postproc / rgb2rgb.c
1 #include <inttypes.h>
2 #include "../config.h"
3 #include "rgb2rgb.h"
4 #include "mmx.h"
5
6 #ifdef HAVE_3DNOW
7 #define PREFETCH "prefetch"
8 #define PREFETCHW "prefetchw"
9 #elif defined ( HAVE_MMX2 )
10 #define PREFETCH "prefetchnta"
11 #define PREFETCHW "prefetcht0"
12 #endif
13
14 #ifdef HAVE_3DNOW
15 #define EMMS "femms"
16 #else
17 #define EMMS "emms"
18 #endif
19
20 #ifdef HAVE_MMX2
21 #define MOVNTQ "movntq"
22 #else
23 #define MOVNTQ "movq"
24 #endif
25
26 #ifdef HAVE_MMX2
27 #define SFENCE "sfence"
28 #endif
29
30 void rgb24to32(uint8_t *src,uint8_t *dst,uint32_t src_size)
31 {
32   uint8_t *dest = dst;
33   uint8_t *s = src;
34   uint8_t *end;
35 #ifdef HAVE_MMX
36   const uint64_t mask32 = 0x00FFFFFF00FFFFFFULL;
37   uint8_t *mm_end;
38 #endif
39   end = s + src_size;
40 #ifdef HAVE_MMX
41 #ifdef PREFETCH
42   __asm __volatile(
43     PREFETCH" %0\n\t"
44     ::"m"(*s):"memory");
45 #endif
46   mm_end = (uint8_t*)((((unsigned long)end)/16)*16);
47   __asm __volatile("movq %0, %%mm7"::"m"(mask32):"memory");
48   while(s < mm_end)
49   {
50 #ifdef PREFETCH
51     __asm __volatile(
52         PREFETCH" 32%0\n\t"
53         ::"m"(*s):"memory");
54 #endif
55     __asm __volatile(
56         "movd   %1, %%mm0\n\t"
57         "movd   3%1, %%mm1\n\t"
58         "movd   6%1, %%mm2\n\t"
59         "movd   9%1, %%mm3\n\t"
60         "punpckldq %%mm1, %%mm0\n\t"
61         "punpckldq %%mm3, %%mm2\n\t"
62         "pand   %%mm7, %%mm0\n\t"
63         "pand   %%mm7, %%mm2\n\t"
64         MOVNTQ" %%mm0, %0\n\t"
65         MOVNTQ" %%mm2, 8%0"
66         :"=m"(*dest)
67         :"m"(*s)
68         :"memory");
69     dest += 16;
70     s += 12;
71   }
72 #ifdef SFENCE
73   __asm __volatile(SFENCE:::"memory");
74 #endif
75   __asm __volatile(EMMS:::"memory");
76 #endif
77   while(s < end)
78   {
79     *dest++ = *s++;
80     *dest++ = *s++;
81     *dest++ = *s++;
82     *dest++ = 0;
83   }
84 }
85
86 /* TODO: MMX optimization */
87 void rgb32to24(uint8_t *src,uint8_t *dst,uint32_t src_size)
88 {
89   uint8_t *dest = dst;
90   uint8_t *s = src;
91   uint8_t *end;
92   end = s + src_size;
93   while(s < end)
94   {
95     *dest++ = *s++;
96     *dest++ = *s++;
97     *dest++ = *s++;
98     s++;
99   }
100 }
101
102 /* Original by Strepto/Astral
103  ported to gcc & bugfixed : A'rpi */
104 void rgb15to16(uint8_t *src,uint8_t *dst,uint32_t src_size)
105 {
106 #ifdef HAVE_MMX
107   static uint64_t mask_b  = 0x001F001F001F001FLL; // 00000000 00011111  xxB
108   static uint64_t mask_rg = 0x7FE07FE07FE07FE0LL; // 01111111 11100000  RGx
109   register char* s=src+src_size;
110   register char* d=dst+src_size;
111   register int offs=-src_size;
112   movq_m2r (mask_b,  mm4);
113   movq_m2r (mask_rg, mm5);
114   while(offs<0){
115     movq_m2r (*(s+offs), mm0);
116     movq_r2r (mm0, mm1);
117
118     movq_m2r (*(s+8+offs), mm2);
119     movq_r2r (mm2, mm3);
120     
121     pand_r2r (mm4, mm0);
122     pand_r2r (mm5, mm1);
123     
124     psllq_i2r(1,mm1);
125     pand_r2r (mm4, mm2);
126
127     pand_r2r (mm5, mm3);
128     por_r2r  (mm1, mm0);
129
130     psllq_i2r(1,mm3);
131     movq_r2m (mm0,*(d+offs));
132
133     por_r2r  (mm3,mm2);
134     movq_r2m (mm2,*(d+8+offs));
135
136     offs+=16;
137   }
138   emms();
139 #else
140    uint16_t *s1=( uint16_t * )src;
141    uint16_t *d1=( uint16_t * )dst;
142    uint16_t *e=((uint8_t *)s1)+src_size;
143    while( s1<e ){
144      register int x=*( s1++ );
145      /* rrrrrggggggbbbbb
146         0rrrrrgggggbbbbb
147         0111 1111 1110 0000=0x7FE0
148         00000000000001 1111=0x001F */
149      *( d1++ )=( x&0x001F )|( ( x&0x7FE0 )<<1 );
150    }
151 #endif
152 }