00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <inttypes.h>
00029 #include "config.h"
00030 #include "rgb2rgb.h"
00031 #include "swscale.h"
00032 #include "swscale_internal.h"
00033 #include "x86_cpu.h"
00034 #include "bswap.h"
00035
00036 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
00037
00038 void (*rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size);
00039 void (*rgb24to16)(const uint8_t *src,uint8_t *dst,long src_size);
00040 void (*rgb24to15)(const uint8_t *src,uint8_t *dst,long src_size);
00041 void (*rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size);
00042 void (*rgb32to16)(const uint8_t *src,uint8_t *dst,long src_size);
00043 void (*rgb32to15)(const uint8_t *src,uint8_t *dst,long src_size);
00044 void (*rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size);
00045 void (*rgb15to24)(const uint8_t *src,uint8_t *dst,long src_size);
00046 void (*rgb15to32)(const uint8_t *src,uint8_t *dst,long src_size);
00047 void (*rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size);
00048 void (*rgb16to24)(const uint8_t *src,uint8_t *dst,long src_size);
00049 void (*rgb16to32)(const uint8_t *src,uint8_t *dst,long src_size);
00050
00051 void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
00052 void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
00053 void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
00054 void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
00055
00056 void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
00057 void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
00058
00059 void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
00060 long width, long height,
00061 long lumStride, long chromStride, long dstStride);
00062 void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
00063 long width, long height,
00064 long lumStride, long chromStride, long dstStride);
00065 void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
00066 long width, long height,
00067 long lumStride, long chromStride, long dstStride);
00068 void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
00069 long width, long height,
00070 long lumStride, long chromStride, long srcStride);
00071 void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
00072 long width, long height,
00073 long lumStride, long chromStride, long srcStride);
00074 void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
00075 long srcStride, long dstStride);
00076 void (*interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dst,
00077 long width, long height, long src1Stride,
00078 long src2Stride, long dstStride);
00079 void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
00080 uint8_t *dst1, uint8_t *dst2,
00081 long width, long height,
00082 long srcStride1, long srcStride2,
00083 long dstStride1, long dstStride2);
00084 void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
00085 uint8_t *dst,
00086 long width, long height,
00087 long srcStride1, long srcStride2,
00088 long srcStride3, long dstStride);
00089
00090 #if defined(ARCH_X86) && defined(CONFIG_GPL)
00091 static const uint64_t mmx_null __attribute__((aligned(8))) = 0x0000000000000000ULL;
00092 static const uint64_t mmx_one __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFFULL;
00093 static const uint64_t mask32b attribute_used __attribute__((aligned(8))) = 0x000000FF000000FFULL;
00094 static const uint64_t mask32g attribute_used __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
00095 static const uint64_t mask32r attribute_used __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
00096 static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
00097 static const uint64_t mask3216br __attribute__((aligned(8))) = 0x00F800F800F800F8ULL;
00098 static const uint64_t mask3216g __attribute__((aligned(8))) = 0x0000FC000000FC00ULL;
00099 static const uint64_t mask3215g __attribute__((aligned(8))) = 0x0000F8000000F800ULL;
00100 static const uint64_t mul3216 __attribute__((aligned(8))) = 0x2000000420000004ULL;
00101 static const uint64_t mul3215 __attribute__((aligned(8))) = 0x2000000820000008ULL;
00102 static const uint64_t mask24b attribute_used __attribute__((aligned(8))) = 0x00FF0000FF0000FFULL;
00103 static const uint64_t mask24g attribute_used __attribute__((aligned(8))) = 0xFF0000FF0000FF00ULL;
00104 static const uint64_t mask24r attribute_used __attribute__((aligned(8))) = 0x0000FF0000FF0000ULL;
00105 static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
00106 static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
00107 static const uint64_t mask24hh __attribute__((aligned(8))) = 0xffff000000000000ULL;
00108 static const uint64_t mask24hhh __attribute__((aligned(8))) = 0xffffffff00000000ULL;
00109 static const uint64_t mask24hhhh __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
00110 static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL;
00111 static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL;
00112 static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
00113 static const uint64_t mask15g __attribute__((aligned(8))) = 0x03E003E003E003E0ULL;
00114 static const uint64_t mask15r __attribute__((aligned(8))) = 0x7C007C007C007C00ULL;
00115 #define mask16b mask15b
00116 static const uint64_t mask16g __attribute__((aligned(8))) = 0x07E007E007E007E0ULL;
00117 static const uint64_t mask16r __attribute__((aligned(8))) = 0xF800F800F800F800ULL;
00118 static const uint64_t red_16mask __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
00119 static const uint64_t green_16mask __attribute__((aligned(8))) = 0x000007e0000007e0ULL;
00120 static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
00121 static const uint64_t red_15mask __attribute__((aligned(8))) = 0x00007c0000007c00ULL;
00122 static const uint64_t green_15mask __attribute__((aligned(8))) = 0x000003e0000003e0ULL;
00123 static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
00124
00125 #ifdef FAST_BGR2YV12
00126 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
00127 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
00128 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
00129 #else
00130 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
00131 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
00132 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
00133 #endif
00134 static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
00135 static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8))) = 0x8080808080808080ULL;
00136 static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
00137
00138 #if 0
00139 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
00140 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
00141 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
00142 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
00143
00144 static uint64_t __attribute__((aligned(8))) dither4[2]={
00145 0x0103010301030103LL,
00146 0x0200020002000200LL,};
00147
00148 static uint64_t __attribute__((aligned(8))) dither8[2]={
00149 0x0602060206020602LL,
00150 0x0004000400040004LL,};
00151 #endif
00152 #endif
00153
00154 #define RGB2YUV_SHIFT 8
00155 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
00156 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
00157 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
00158 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
00159 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
00160 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
00161 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
00162 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
00163 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
00164
00165
00166
00167 #undef HAVE_MMX
00168 #undef HAVE_MMX2
00169 #undef HAVE_3DNOW
00170 #undef HAVE_SSE2
00171 #define RENAME(a) a ## _C
00172 #include "rgb2rgb_template.c"
00173
00174 #if defined(ARCH_X86) && defined(CONFIG_GPL)
00175
00176
00177 #undef RENAME
00178 #define HAVE_MMX
00179 #undef HAVE_MMX2
00180 #undef HAVE_3DNOW
00181 #undef HAVE_SSE2
00182 #define RENAME(a) a ## _MMX
00183 #include "rgb2rgb_template.c"
00184
00185
00186 #undef RENAME
00187 #define HAVE_MMX
00188 #define HAVE_MMX2
00189 #undef HAVE_3DNOW
00190 #undef HAVE_SSE2
00191 #define RENAME(a) a ## _MMX2
00192 #include "rgb2rgb_template.c"
00193
00194
00195 #undef RENAME
00196 #define HAVE_MMX
00197 #undef HAVE_MMX2
00198 #define HAVE_3DNOW
00199 #undef HAVE_SSE2
00200 #define RENAME(a) a ## _3DNOW
00201 #include "rgb2rgb_template.c"
00202
00203 #endif //ARCH_X86 || ARCH_X86_64
00204
00205
00206
00207
00208
00209
00210
00211
00212 void sws_rgb2rgb_init(int flags){
00213 #if (defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)) && defined(CONFIG_GPL)
00214 if (flags & SWS_CPU_CAPS_MMX2)
00215 rgb2rgb_init_MMX2();
00216 else if (flags & SWS_CPU_CAPS_3DNOW)
00217 rgb2rgb_init_3DNOW();
00218 else if (flags & SWS_CPU_CAPS_MMX)
00219 rgb2rgb_init_MMX();
00220 else
00221 #endif
00222 rgb2rgb_init_C();
00223 }
00224
00228 void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00229 {
00230 long i;
00231
00232
00233
00234
00235
00236
00237 for (i=0; i<num_pixels; i++)
00238 {
00239 #ifdef WORDS_BIGENDIAN
00240 dst[3]= palette[ src[i]*4+2 ];
00241 dst[2]= palette[ src[i]*4+1 ];
00242 dst[1]= palette[ src[i]*4+0 ];
00243 #else
00244
00245 dst[0]= palette[ src[i]*4+2 ];
00246 dst[1]= palette[ src[i]*4+1 ];
00247 dst[2]= palette[ src[i]*4+0 ];
00248
00249 #endif
00250 dst+= 4;
00251 }
00252 }
00253
00254 void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00255 {
00256 long i;
00257 for (i=0; i<num_pixels; i++)
00258 {
00259 #ifdef WORDS_BIGENDIAN
00260 dst[3]= palette[ src[i]*4+0 ];
00261 dst[2]= palette[ src[i]*4+1 ];
00262 dst[1]= palette[ src[i]*4+2 ];
00263 #else
00264
00265 dst[0]= palette[ src[i]*4+0 ];
00266 dst[1]= palette[ src[i]*4+1 ];
00267 dst[2]= palette[ src[i]*4+2 ];
00268
00269 #endif
00270
00271 dst+= 4;
00272 }
00273 }
00274
00278 void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00279 {
00280 long i;
00281
00282
00283
00284
00285
00286 for (i=0; i<num_pixels; i++)
00287 {
00288
00289 dst[0]= palette[ src[i]*4+2 ];
00290 dst[1]= palette[ src[i]*4+1 ];
00291 dst[2]= palette[ src[i]*4+0 ];
00292 dst+= 3;
00293 }
00294 }
00295
00296 void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00297 {
00298 long i;
00299
00300
00301
00302
00303
00304 for (i=0; i<num_pixels; i++)
00305 {
00306
00307 dst[0]= palette[ src[i]*4+0 ];
00308 dst[1]= palette[ src[i]*4+1 ];
00309 dst[2]= palette[ src[i]*4+2 ];
00310 dst+= 3;
00311 }
00312 }
00313
00317 void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00318 {
00319 long i;
00320 for (i=0; i<num_pixels; i++)
00321 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
00322 }
00323 void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00324 {
00325 long i;
00326 for (i=0; i<num_pixels; i++)
00327 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
00328 }
00329
00333 void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00334 {
00335 long i;
00336 for (i=0; i<num_pixels; i++)
00337 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
00338 }
00339 void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00340 {
00341 long i;
00342 for (i=0; i<num_pixels; i++)
00343 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
00344 }
00345
00346 void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
00347 {
00348 long i;
00349 long num_pixels = src_size >> 2;
00350 for (i=0; i<num_pixels; i++)
00351 {
00352 #ifdef WORDS_BIGENDIAN
00353
00354 dst[3*i + 0] = src[4*i + 1];
00355 dst[3*i + 1] = src[4*i + 2];
00356 dst[3*i + 2] = src[4*i + 3];
00357 #else
00358 dst[3*i + 0] = src[4*i + 2];
00359 dst[3*i + 1] = src[4*i + 1];
00360 dst[3*i + 2] = src[4*i + 0];
00361 #endif
00362 }
00363 }
00364
00365 void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
00366 {
00367 long i;
00368 for (i=0; 3*i<src_size; i++)
00369 {
00370 #ifdef WORDS_BIGENDIAN
00371
00372 dst[4*i + 0] = 0;
00373 dst[4*i + 1] = src[3*i + 0];
00374 dst[4*i + 2] = src[3*i + 1];
00375 dst[4*i + 3] = src[3*i + 2];
00376 #else
00377 dst[4*i + 0] = src[3*i + 2];
00378 dst[4*i + 1] = src[3*i + 1];
00379 dst[4*i + 2] = src[3*i + 0];
00380 dst[4*i + 3] = 0;
00381 #endif
00382 }
00383 }
00384
00385 void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
00386 {
00387 const uint16_t *end;
00388 uint8_t *d = (uint8_t *)dst;
00389 const uint16_t *s = (uint16_t *)src;
00390 end = s + src_size/2;
00391 while (s < end)
00392 {
00393 register uint16_t bgr;
00394 bgr = *s++;
00395 #ifdef WORDS_BIGENDIAN
00396 *d++ = 0;
00397 *d++ = (bgr&0x1F)<<3;
00398 *d++ = (bgr&0x7E0)>>3;
00399 *d++ = (bgr&0xF800)>>8;
00400 #else
00401 *d++ = (bgr&0xF800)>>8;
00402 *d++ = (bgr&0x7E0)>>3;
00403 *d++ = (bgr&0x1F)<<3;
00404 *d++ = 0;
00405 #endif
00406 }
00407 }
00408
00409 void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
00410 {
00411 const uint16_t *end;
00412 uint8_t *d = (uint8_t *)dst;
00413 const uint16_t *s = (const uint16_t *)src;
00414 end = s + src_size/2;
00415 while (s < end)
00416 {
00417 register uint16_t bgr;
00418 bgr = *s++;
00419 *d++ = (bgr&0xF800)>>8;
00420 *d++ = (bgr&0x7E0)>>3;
00421 *d++ = (bgr&0x1F)<<3;
00422 }
00423 }
00424
00425 void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
00426 {
00427 long i;
00428 long num_pixels = src_size >> 1;
00429
00430 for (i=0; i<num_pixels; i++)
00431 {
00432 unsigned b,g,r;
00433 register uint16_t rgb;
00434 rgb = src[2*i];
00435 r = rgb&0x1F;
00436 g = (rgb&0x7E0)>>5;
00437 b = (rgb&0xF800)>>11;
00438 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
00439 }
00440 }
00441
00442 void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
00443 {
00444 long i;
00445 long num_pixels = src_size >> 1;
00446
00447 for (i=0; i<num_pixels; i++)
00448 {
00449 unsigned b,g,r;
00450 register uint16_t rgb;
00451 rgb = src[2*i];
00452 r = rgb&0x1F;
00453 g = (rgb&0x7E0)>>5;
00454 b = (rgb&0xF800)>>11;
00455 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
00456 }
00457 }
00458
00459 void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
00460 {
00461 const uint16_t *end;
00462 uint8_t *d = (uint8_t *)dst;
00463 const uint16_t *s = (const uint16_t *)src;
00464 end = s + src_size/2;
00465 while (s < end)
00466 {
00467 register uint16_t bgr;
00468 bgr = *s++;
00469 #ifdef WORDS_BIGENDIAN
00470 *d++ = 0;
00471 *d++ = (bgr&0x1F)<<3;
00472 *d++ = (bgr&0x3E0)>>2;
00473 *d++ = (bgr&0x7C00)>>7;
00474 #else
00475 *d++ = (bgr&0x7C00)>>7;
00476 *d++ = (bgr&0x3E0)>>2;
00477 *d++ = (bgr&0x1F)<<3;
00478 *d++ = 0;
00479 #endif
00480 }
00481 }
00482
00483 void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
00484 {
00485 const uint16_t *end;
00486 uint8_t *d = (uint8_t *)dst;
00487 const uint16_t *s = (uint16_t *)src;
00488 end = s + src_size/2;
00489 while (s < end)
00490 {
00491 register uint16_t bgr;
00492 bgr = *s++;
00493 *d++ = (bgr&0x7C00)>>7;
00494 *d++ = (bgr&0x3E0)>>2;
00495 *d++ = (bgr&0x1F)<<3;
00496 }
00497 }
00498
00499 void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
00500 {
00501 long i;
00502 long num_pixels = src_size >> 1;
00503
00504 for (i=0; i<num_pixels; i++)
00505 {
00506 unsigned b,g,r;
00507 register uint16_t rgb;
00508 rgb = src[2*i];
00509 r = rgb&0x1F;
00510 g = (rgb&0x3E0)>>5;
00511 b = (rgb&0x7C00)>>10;
00512 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
00513 }
00514 }
00515
00516 void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
00517 {
00518 long i;
00519 long num_pixels = src_size >> 1;
00520
00521 for (i=0; i<num_pixels; i++)
00522 {
00523 unsigned b,g,r;
00524 register uint16_t rgb;
00525 rgb = src[2*i];
00526 r = rgb&0x1F;
00527 g = (rgb&0x3E0)>>5;
00528 b = (rgb&0x7C00)>>10;
00529 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
00530 }
00531 }
00532
00533 void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
00534 {
00535 long i;
00536 long num_pixels = src_size;
00537 for (i=0; i<num_pixels; i++)
00538 {
00539 unsigned b,g,r;
00540 register uint8_t rgb;
00541 rgb = src[i];
00542 r = (rgb&0x07);
00543 g = (rgb&0x38)>>3;
00544 b = (rgb&0xC0)>>6;
00545 dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
00546 }
00547 }