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 & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
10 * This file is part of FFmpeg.
12 * FFmpeg is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 * the C code (not assembly, mmx, ...) of this file can be used
27 * under the LGPL license too
33 #include "swscale_internal.h"
37 #include "libvo/fastmemcpy.h"
40 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
42 void (*rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size);
43 void (*rgb24to16)(const uint8_t *src,uint8_t *dst,long src_size);
44 void (*rgb24to15)(const uint8_t *src,uint8_t *dst,long src_size);
45 void (*rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size);
46 void (*rgb32to16)(const uint8_t *src,uint8_t *dst,long src_size);
47 void (*rgb32to15)(const uint8_t *src,uint8_t *dst,long src_size);
48 void (*rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size);
49 void (*rgb15to24)(const uint8_t *src,uint8_t *dst,long src_size);
50 void (*rgb15to32)(const uint8_t *src,uint8_t *dst,long src_size);
51 void (*rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size);
52 void (*rgb16to24)(const uint8_t *src,uint8_t *dst,long src_size);
53 void (*rgb16to32)(const uint8_t *src,uint8_t *dst,long src_size);
54 //void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
55 void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
56 void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
57 void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
58 void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
59 //void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
60 void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
61 void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
63 void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
64 long width, long height,
65 long lumStride, long chromStride, long dstStride);
66 void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
67 long width, long height,
68 long lumStride, long chromStride, long dstStride);
69 void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
70 long width, long height,
71 long lumStride, long chromStride, long dstStride);
72 void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
73 long width, long height,
74 long lumStride, long chromStride, long srcStride);
75 void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
76 long width, long height,
77 long lumStride, long chromStride, long srcStride);
78 void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
79 long srcStride, long dstStride);
80 void (*interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dst,
81 long width, long height, long src1Stride,
82 long src2Stride, long dstStride);
83 void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
84 uint8_t *dst1, uint8_t *dst2,
85 long width, long height,
86 long srcStride1, long srcStride2,
87 long dstStride1, long dstStride2);
88 void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
90 long width, long height,
91 long srcStride1, long srcStride2,
92 long srcStride3, long dstStride);
94 #if defined(ARCH_X86) && defined(CONFIG_GPL)
95 static const uint64_t mmx_null __attribute__((aligned(8))) = 0x0000000000000000ULL;
96 static const uint64_t mmx_one __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFFULL;
97 static const uint64_t mask32b attribute_used __attribute__((aligned(8))) = 0x000000FF000000FFULL;
98 static const uint64_t mask32g attribute_used __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
99 static const uint64_t mask32r attribute_used __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
100 static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
101 static const uint64_t mask3216br __attribute__((aligned(8)))=0x00F800F800F800F8ULL;
102 static const uint64_t mask3216g __attribute__((aligned(8)))=0x0000FC000000FC00ULL;
103 static const uint64_t mask3215g __attribute__((aligned(8)))=0x0000F8000000F800ULL;
104 static const uint64_t mul3216 __attribute__((aligned(8))) = 0x2000000420000004ULL;
105 static const uint64_t mul3215 __attribute__((aligned(8))) = 0x2000000820000008ULL;
106 static const uint64_t mask24b attribute_used __attribute__((aligned(8))) = 0x00FF0000FF0000FFULL;
107 static const uint64_t mask24g attribute_used __attribute__((aligned(8))) = 0xFF0000FF0000FF00ULL;
108 static const uint64_t mask24r attribute_used __attribute__((aligned(8))) = 0x0000FF0000FF0000ULL;
109 static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
110 static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
111 static const uint64_t mask24hh __attribute__((aligned(8))) = 0xffff000000000000ULL;
112 static const uint64_t mask24hhh __attribute__((aligned(8))) = 0xffffffff00000000ULL;
113 static const uint64_t mask24hhhh __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
114 static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
115 static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
116 static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
117 static const uint64_t mask15g __attribute__((aligned(8))) = 0x03E003E003E003E0ULL;
118 static const uint64_t mask15r __attribute__((aligned(8))) = 0x7C007C007C007C00ULL;
119 #define mask16b mask15b
120 static const uint64_t mask16g __attribute__((aligned(8))) = 0x07E007E007E007E0ULL;
121 static const uint64_t mask16r __attribute__((aligned(8))) = 0xF800F800F800F800ULL;
122 static const uint64_t red_16mask __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
123 static const uint64_t green_16mask __attribute__((aligned(8)))= 0x000007e0000007e0ULL;
124 static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
125 static const uint64_t red_15mask __attribute__((aligned(8))) = 0x00007c000000f800ULL;
126 static const uint64_t green_15mask __attribute__((aligned(8)))= 0x000003e0000007e0ULL;
127 static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
130 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
131 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
132 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
134 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
135 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
136 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
138 static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
139 static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
140 static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
143 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
144 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
145 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
146 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
148 static uint64_t __attribute__((aligned(8))) dither4[2]={
149 0x0103010301030103LL,
150 0x0200020002000200LL,};
152 static uint64_t __attribute__((aligned(8))) dither8[2]={
153 0x0602060206020602LL,
154 0x0004000400040004LL,};
156 #endif /* defined(ARCH_X86) */
158 #define RGB2YUV_SHIFT 8
159 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
160 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
161 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
162 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
163 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
164 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
165 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
166 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
167 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
169 //Note: we have C, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
175 #define RENAME(a) a ## _C
176 #include "rgb2rgb_template.c"
178 #if defined(ARCH_X86) && defined(CONFIG_GPL)
186 #define RENAME(a) a ## _MMX
187 #include "rgb2rgb_template.c"
195 #define RENAME(a) a ## _MMX2
196 #include "rgb2rgb_template.c"
204 #define RENAME(a) a ## _3DNOW
205 #include "rgb2rgb_template.c"
207 #endif //ARCH_X86 || ARCH_X86_64
210 rgb15->rgb16 Original by Strepto/Astral
211 ported to gcc & bugfixed : A'rpi
212 MMX2, 3DNOW optimization by Nick Kurshev
213 32bit c version, and and&add trick by Michael Niedermayer
216 void sws_rgb2rgb_init(int flags){
217 #if (defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)) && defined(CONFIG_GPL)
218 if(flags & SWS_CPU_CAPS_MMX2)
220 else if(flags & SWS_CPU_CAPS_3DNOW)
221 rgb2rgb_init_3DNOW();
222 else if(flags & SWS_CPU_CAPS_MMX)
225 #endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
230 * Palette is assumed to contain BGR32.
232 void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
237 for(i=0; i<num_pixels; i++)
238 ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
241 for(i=0; i<num_pixels; i++)
243 #ifdef WORDS_BIGENDIAN
244 dst[3]= palette[ src[i]*4+2 ];
245 dst[2]= palette[ src[i]*4+1 ];
246 dst[1]= palette[ src[i]*4+0 ];
249 dst[0]= palette[ src[i]*4+2 ];
250 dst[1]= palette[ src[i]*4+1 ];
251 dst[2]= palette[ src[i]*4+0 ];
252 //dst[3]= 0; /* do we need this cleansing? */
258 void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
261 for(i=0; i<num_pixels; i++)
263 #ifdef WORDS_BIGENDIAN
264 dst[3]= palette[ src[i]*4+0 ];
265 dst[2]= palette[ src[i]*4+1 ];
266 dst[1]= palette[ src[i]*4+2 ];
269 dst[0]= palette[ src[i]*4+0 ];
270 dst[1]= palette[ src[i]*4+1 ];
271 dst[2]= palette[ src[i]*4+2 ];
272 //dst[3]= 0; /* do we need this cleansing? */
280 * Palette is assumed to contain BGR32.
282 void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
286 writes 1 byte o much and might cause alignment issues on some architectures?
287 for(i=0; i<num_pixels; i++)
288 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
290 for(i=0; i<num_pixels; i++)
293 dst[0]= palette[ src[i]*4+2 ];
294 dst[1]= palette[ src[i]*4+1 ];
295 dst[2]= palette[ src[i]*4+0 ];
300 void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
304 writes 1 byte o much and might cause alignment issues on some architectures?
305 for(i=0; i<num_pixels; i++)
306 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
308 for(i=0; i<num_pixels; i++)
311 dst[0]= palette[ src[i]*4+0 ];
312 dst[1]= palette[ src[i]*4+1 ];
313 dst[2]= palette[ src[i]*4+2 ];
319 * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
321 void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
324 for(i=0; i<num_pixels; i++)
325 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
327 void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
330 for(i=0; i<num_pixels; i++)
331 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
335 * Palette is assumed to contain BGR15, see rgb32to15 to convert the palette.
337 void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
340 for(i=0; i<num_pixels; i++)
341 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
343 void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
346 for(i=0; i<num_pixels; i++)
347 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
350 void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
353 long num_pixels = src_size >> 2;
354 for(i=0; i<num_pixels; i++)
356 #ifdef WORDS_BIGENDIAN
357 /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
358 dst[3*i + 0] = src[4*i + 1];
359 dst[3*i + 1] = src[4*i + 2];
360 dst[3*i + 2] = src[4*i + 3];
362 dst[3*i + 0] = src[4*i + 2];
363 dst[3*i + 1] = src[4*i + 1];
364 dst[3*i + 2] = src[4*i + 0];
369 void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
372 for(i=0; 3*i<src_size; i++)
374 #ifdef WORDS_BIGENDIAN
375 /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
377 dst[4*i + 1] = src[3*i + 0];
378 dst[4*i + 2] = src[3*i + 1];
379 dst[4*i + 3] = src[3*i + 2];
381 dst[4*i + 0] = src[3*i + 2];
382 dst[4*i + 1] = src[3*i + 1];
383 dst[4*i + 2] = src[3*i + 0];
389 void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
392 uint8_t *d = (uint8_t *)dst;
393 const uint16_t *s = (uint16_t *)src;
394 end = s + src_size/2;
397 register uint16_t bgr;
399 #ifdef WORDS_BIGENDIAN
401 *d++ = (bgr&0x1F)<<3;
402 *d++ = (bgr&0x7E0)>>3;
403 *d++ = (bgr&0xF800)>>8;
405 *d++ = (bgr&0xF800)>>8;
406 *d++ = (bgr&0x7E0)>>3;
407 *d++ = (bgr&0x1F)<<3;
413 void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
416 uint8_t *d = (uint8_t *)dst;
417 const uint16_t *s = (const uint16_t *)src;
418 end = s + src_size/2;
421 register uint16_t bgr;
423 *d++ = (bgr&0xF800)>>8;
424 *d++ = (bgr&0x7E0)>>3;
425 *d++ = (bgr&0x1F)<<3;
429 void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
432 long num_pixels = src_size >> 1;
434 for(i=0; i<num_pixels; i++)
437 register uint16_t rgb;
441 b = (rgb&0xF800)>>11;
442 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
446 void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
449 long num_pixels = src_size >> 1;
451 for(i=0; i<num_pixels; i++)
454 register uint16_t rgb;
458 b = (rgb&0xF800)>>11;
459 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
463 void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
466 uint8_t *d = (uint8_t *)dst;
467 const uint16_t *s = (const uint16_t *)src;
468 end = s + src_size/2;
471 register uint16_t bgr;
473 #ifdef WORDS_BIGENDIAN
475 *d++ = (bgr&0x1F)<<3;
476 *d++ = (bgr&0x3E0)>>2;
477 *d++ = (bgr&0x7C00)>>7;
479 *d++ = (bgr&0x7C00)>>7;
480 *d++ = (bgr&0x3E0)>>2;
481 *d++ = (bgr&0x1F)<<3;
487 void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
490 uint8_t *d = (uint8_t *)dst;
491 const uint16_t *s = (uint16_t *)src;
492 end = s + src_size/2;
495 register uint16_t bgr;
497 *d++ = (bgr&0x7C00)>>7;
498 *d++ = (bgr&0x3E0)>>2;
499 *d++ = (bgr&0x1F)<<3;
503 void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
506 long num_pixels = src_size >> 1;
508 for(i=0; i<num_pixels; i++)
511 register uint16_t rgb;
515 b = (rgb&0x7C00)>>10;
516 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
520 void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
523 long num_pixels = src_size >> 1;
525 for(i=0; i<num_pixels; i++)
528 register uint16_t rgb;
532 b = (rgb&0x7C00)>>10;
533 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
537 void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
540 long num_pixels = src_size;
541 for(i=0; i<num_pixels; i++)
544 register uint8_t rgb;
549 dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);