2 * rgb2rgb.c, Software RGB to RGB convertor
3 * pluralize by Software PAL8 to RGB convertor
4 * Software YUV to YUV convertor
5 * Software YUV to RGB convertor
6 * Written by Nick Kurshev.
7 * palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
9 * This file is part of FFmpeg.
11 * FFmpeg is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 * the C code (not assembly, mmx, ...) of this file can be used
26 * under the LGPL license too
32 #include "swscale_internal.h"
36 #include "libvo/fastmemcpy.h"
39 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
41 void (*rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size);
42 void (*rgb24to16)(const uint8_t *src,uint8_t *dst,long src_size);
43 void (*rgb24to15)(const uint8_t *src,uint8_t *dst,long src_size);
44 void (*rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size);
45 void (*rgb32to16)(const uint8_t *src,uint8_t *dst,long src_size);
46 void (*rgb32to15)(const uint8_t *src,uint8_t *dst,long src_size);
47 void (*rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size);
48 void (*rgb15to24)(const uint8_t *src,uint8_t *dst,long src_size);
49 void (*rgb15to32)(const uint8_t *src,uint8_t *dst,long src_size);
50 void (*rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size);
51 void (*rgb16to24)(const uint8_t *src,uint8_t *dst,long src_size);
52 void (*rgb16to32)(const uint8_t *src,uint8_t *dst,long src_size);
53 //void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
54 void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
55 void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
56 void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
57 void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
58 //void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
59 void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
60 void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
62 void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
63 long width, long height,
64 long lumStride, long chromStride, long dstStride);
65 void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
66 long width, long height,
67 long lumStride, long chromStride, long dstStride);
68 void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
69 long width, long height,
70 long lumStride, long chromStride, long dstStride);
71 void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
72 long width, long height,
73 long lumStride, long chromStride, long srcStride);
74 void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
75 long width, long height,
76 long lumStride, long chromStride, long srcStride);
77 void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
78 long srcStride, long dstStride);
79 void (*interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dst,
80 long width, long height, long src1Stride,
81 long src2Stride, long dstStride);
82 void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
83 uint8_t *dst1, uint8_t *dst2,
84 long width, long height,
85 long srcStride1, long srcStride2,
86 long dstStride1, long dstStride2);
87 void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
89 long width, long height,
90 long srcStride1, long srcStride2,
91 long srcStride3, long dstStride);
93 #if defined(ARCH_X86) && defined(CONFIG_GPL)
94 static const uint64_t mmx_null __attribute__((aligned(8))) = 0x0000000000000000ULL;
95 static const uint64_t mmx_one __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFFULL;
96 static const uint64_t mask32b attribute_used __attribute__((aligned(8))) = 0x000000FF000000FFULL;
97 static const uint64_t mask32g attribute_used __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
98 static const uint64_t mask32r attribute_used __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
99 static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
100 static const uint64_t mask3216br __attribute__((aligned(8))) = 0x00F800F800F800F8ULL;
101 static const uint64_t mask3216g __attribute__((aligned(8))) = 0x0000FC000000FC00ULL;
102 static const uint64_t mask3215g __attribute__((aligned(8))) = 0x0000F8000000F800ULL;
103 static const uint64_t mul3216 __attribute__((aligned(8))) = 0x2000000420000004ULL;
104 static const uint64_t mul3215 __attribute__((aligned(8))) = 0x2000000820000008ULL;
105 static const uint64_t mask24b attribute_used __attribute__((aligned(8))) = 0x00FF0000FF0000FFULL;
106 static const uint64_t mask24g attribute_used __attribute__((aligned(8))) = 0xFF0000FF0000FF00ULL;
107 static const uint64_t mask24r attribute_used __attribute__((aligned(8))) = 0x0000FF0000FF0000ULL;
108 static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
109 static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
110 static const uint64_t mask24hh __attribute__((aligned(8))) = 0xffff000000000000ULL;
111 static const uint64_t mask24hhh __attribute__((aligned(8))) = 0xffffffff00000000ULL;
112 static const uint64_t mask24hhhh __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
113 static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
114 static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
115 static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
116 static const uint64_t mask15g __attribute__((aligned(8))) = 0x03E003E003E003E0ULL;
117 static const uint64_t mask15r __attribute__((aligned(8))) = 0x7C007C007C007C00ULL;
118 #define mask16b mask15b
119 static const uint64_t mask16g __attribute__((aligned(8))) = 0x07E007E007E007E0ULL;
120 static const uint64_t mask16r __attribute__((aligned(8))) = 0xF800F800F800F800ULL;
121 static const uint64_t red_16mask __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
122 static const uint64_t green_16mask __attribute__((aligned(8))) = 0x000007e0000007e0ULL;
123 static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
124 static const uint64_t red_15mask __attribute__((aligned(8))) = 0x00007c0000007c00ULL;
125 static const uint64_t green_15mask __attribute__((aligned(8))) = 0x000003e0000003e0ULL;
126 static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
129 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
130 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
131 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
133 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
134 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
135 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
137 static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
138 static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8))) = 0x8080808080808080ULL;
139 static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
142 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
143 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
144 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
145 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
147 static uint64_t __attribute__((aligned(8))) dither4[2]={
148 0x0103010301030103LL,
149 0x0200020002000200LL,};
151 static uint64_t __attribute__((aligned(8))) dither8[2]={
152 0x0602060206020602LL,
153 0x0004000400040004LL,};
155 #endif /* defined(ARCH_X86) */
157 #define RGB2YUV_SHIFT 8
158 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
159 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
160 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
161 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
162 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
163 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
164 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
165 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
166 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
168 //Note: we have C, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
174 #define RENAME(a) a ## _C
175 #include "rgb2rgb_template.c"
177 #if defined(ARCH_X86) && defined(CONFIG_GPL)
185 #define RENAME(a) a ## _MMX
186 #include "rgb2rgb_template.c"
194 #define RENAME(a) a ## _MMX2
195 #include "rgb2rgb_template.c"
203 #define RENAME(a) a ## _3DNOW
204 #include "rgb2rgb_template.c"
206 #endif //ARCH_X86 || ARCH_X86_64
209 rgb15->rgb16 Original by Strepto/Astral
210 ported to gcc & bugfixed : A'rpi
211 MMX2, 3DNOW optimization by Nick Kurshev
212 32bit c version, and and&add trick by Michael Niedermayer
215 void sws_rgb2rgb_init(int flags){
216 #if (defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)) && defined(CONFIG_GPL)
217 if (flags & SWS_CPU_CAPS_MMX2)
219 else if (flags & SWS_CPU_CAPS_3DNOW)
220 rgb2rgb_init_3DNOW();
221 else if (flags & SWS_CPU_CAPS_MMX)
224 #endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
229 * Palette is assumed to contain BGR32.
231 void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
236 for (i=0; i<num_pixels; i++)
237 ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
240 for (i=0; i<num_pixels; i++)
242 #ifdef WORDS_BIGENDIAN
243 dst[3]= palette[ src[i]*4+2 ];
244 dst[2]= palette[ src[i]*4+1 ];
245 dst[1]= palette[ src[i]*4+0 ];
248 dst[0]= palette[ src[i]*4+2 ];
249 dst[1]= palette[ src[i]*4+1 ];
250 dst[2]= palette[ src[i]*4+0 ];
251 //dst[3]= 0; /* do we need this cleansing? */
257 void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
260 for (i=0; i<num_pixels; i++)
262 #ifdef WORDS_BIGENDIAN
263 dst[3]= palette[ src[i]*4+0 ];
264 dst[2]= palette[ src[i]*4+1 ];
265 dst[1]= palette[ src[i]*4+2 ];
268 dst[0]= palette[ src[i]*4+0 ];
269 dst[1]= palette[ src[i]*4+1 ];
270 dst[2]= palette[ src[i]*4+2 ];
271 //dst[3]= 0; /* do we need this cleansing? */
279 * Palette is assumed to contain BGR32.
281 void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
285 writes 1 byte o much and might cause alignment issues on some architectures?
286 for (i=0; i<num_pixels; i++)
287 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
289 for (i=0; i<num_pixels; i++)
292 dst[0]= palette[ src[i]*4+2 ];
293 dst[1]= palette[ src[i]*4+1 ];
294 dst[2]= palette[ src[i]*4+0 ];
299 void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
303 writes 1 byte o much and might cause alignment issues on some architectures?
304 for (i=0; i<num_pixels; i++)
305 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
307 for (i=0; i<num_pixels; i++)
310 dst[0]= palette[ src[i]*4+0 ];
311 dst[1]= palette[ src[i]*4+1 ];
312 dst[2]= palette[ src[i]*4+2 ];
318 * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
320 void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
323 for (i=0; i<num_pixels; i++)
324 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
326 void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
329 for (i=0; i<num_pixels; i++)
330 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
334 * Palette is assumed to contain BGR15, see rgb32to15 to convert the palette.
336 void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
339 for (i=0; i<num_pixels; i++)
340 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
342 void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
345 for (i=0; i<num_pixels; i++)
346 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
349 void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
352 long num_pixels = src_size >> 2;
353 for (i=0; i<num_pixels; i++)
355 #ifdef WORDS_BIGENDIAN
356 /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
357 dst[3*i + 0] = src[4*i + 1];
358 dst[3*i + 1] = src[4*i + 2];
359 dst[3*i + 2] = src[4*i + 3];
361 dst[3*i + 0] = src[4*i + 2];
362 dst[3*i + 1] = src[4*i + 1];
363 dst[3*i + 2] = src[4*i + 0];
368 void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
371 for (i=0; 3*i<src_size; i++)
373 #ifdef WORDS_BIGENDIAN
374 /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
376 dst[4*i + 1] = src[3*i + 0];
377 dst[4*i + 2] = src[3*i + 1];
378 dst[4*i + 3] = src[3*i + 2];
380 dst[4*i + 0] = src[3*i + 2];
381 dst[4*i + 1] = src[3*i + 1];
382 dst[4*i + 2] = src[3*i + 0];
388 void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
391 uint8_t *d = (uint8_t *)dst;
392 const uint16_t *s = (uint16_t *)src;
393 end = s + src_size/2;
396 register uint16_t bgr;
398 #ifdef WORDS_BIGENDIAN
400 *d++ = (bgr&0x1F)<<3;
401 *d++ = (bgr&0x7E0)>>3;
402 *d++ = (bgr&0xF800)>>8;
404 *d++ = (bgr&0xF800)>>8;
405 *d++ = (bgr&0x7E0)>>3;
406 *d++ = (bgr&0x1F)<<3;
412 void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
415 uint8_t *d = (uint8_t *)dst;
416 const uint16_t *s = (const uint16_t *)src;
417 end = s + src_size/2;
420 register uint16_t bgr;
422 *d++ = (bgr&0xF800)>>8;
423 *d++ = (bgr&0x7E0)>>3;
424 *d++ = (bgr&0x1F)<<3;
428 void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
431 long num_pixels = src_size >> 1;
433 for (i=0; i<num_pixels; i++)
436 register uint16_t rgb;
440 b = (rgb&0xF800)>>11;
441 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
445 void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
448 long num_pixels = src_size >> 1;
450 for (i=0; i<num_pixels; i++)
453 register uint16_t rgb;
457 b = (rgb&0xF800)>>11;
458 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
462 void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
465 uint8_t *d = (uint8_t *)dst;
466 const uint16_t *s = (const uint16_t *)src;
467 end = s + src_size/2;
470 register uint16_t bgr;
472 #ifdef WORDS_BIGENDIAN
474 *d++ = (bgr&0x1F)<<3;
475 *d++ = (bgr&0x3E0)>>2;
476 *d++ = (bgr&0x7C00)>>7;
478 *d++ = (bgr&0x7C00)>>7;
479 *d++ = (bgr&0x3E0)>>2;
480 *d++ = (bgr&0x1F)<<3;
486 void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
489 uint8_t *d = (uint8_t *)dst;
490 const uint16_t *s = (uint16_t *)src;
491 end = s + src_size/2;
494 register uint16_t bgr;
496 *d++ = (bgr&0x7C00)>>7;
497 *d++ = (bgr&0x3E0)>>2;
498 *d++ = (bgr&0x1F)<<3;
502 void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
505 long num_pixels = src_size >> 1;
507 for (i=0; i<num_pixels; i++)
510 register uint16_t rgb;
514 b = (rgb&0x7C00)>>10;
515 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
519 void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
522 long num_pixels = src_size >> 1;
524 for (i=0; i<num_pixels; i++)
527 register uint16_t rgb;
531 b = (rgb&0x7C00)>>10;
532 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
536 void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
539 long num_pixels = src_size;
540 for (i=0; i<num_pixels; i++)
543 register uint8_t rgb;
548 dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);