Libav
|
00001 /* 00002 * software RGB to RGB converter 00003 * pluralize by software PAL8 to RGB converter 00004 * software YUV to YUV converter 00005 * software YUV to RGB converter 00006 * Written by Nick Kurshev. 00007 * palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at) 00008 * 00009 * This file is part of FFmpeg. 00010 * 00011 * FFmpeg is free software; you can redistribute it and/or 00012 * modify it under the terms of the GNU Lesser General Public 00013 * License as published by the Free Software Foundation; either 00014 * version 2.1 of the License, or (at your option) any later version. 00015 * 00016 * FFmpeg is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 * Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with FFmpeg; if not, write to the Free Software 00023 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00024 */ 00025 #include <inttypes.h> 00026 #include "config.h" 00027 #include "libavutil/x86_cpu.h" 00028 #include "libavutil/bswap.h" 00029 #include "rgb2rgb.h" 00030 #include "swscale.h" 00031 #include "swscale_internal.h" 00032 00033 #define FAST_BGR2YV12 // use 7-bit instead of 15-bit coefficients 00034 00035 void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size); 00036 void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size); 00037 void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size); 00038 void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size); 00039 void (*rgb32to16)(const uint8_t *src, uint8_t *dst, long src_size); 00040 void (*rgb32to15)(const uint8_t *src, uint8_t *dst, long src_size); 00041 void (*rgb15to16)(const uint8_t *src, uint8_t *dst, long src_size); 00042 void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, long src_size); 00043 void (*rgb15to32)(const uint8_t *src, uint8_t *dst, long src_size); 00044 void (*rgb16to15)(const uint8_t *src, uint8_t *dst, long src_size); 00045 void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, long src_size); 00046 void (*rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size); 00047 void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size); 00048 void (*rgb24to16)(const uint8_t *src, uint8_t *dst, long src_size); 00049 void (*rgb24to15)(const uint8_t *src, uint8_t *dst, long src_size); 00050 void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size); 00051 void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size); 00052 void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size); 00053 00054 void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst, 00055 long width, long height, 00056 long lumStride, long chromStride, long dstStride); 00057 void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst, 00058 long width, long height, 00059 long lumStride, long chromStride, long dstStride); 00060 void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst, 00061 long width, long height, 00062 long lumStride, long chromStride, long dstStride); 00063 void (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst, 00064 long width, long height, 00065 long lumStride, long chromStride, long dstStride); 00066 void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst, 00067 long width, long height, 00068 long lumStride, long chromStride, long srcStride); 00069 void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst, 00070 long width, long height, 00071 long lumStride, long chromStride, long srcStride); 00072 void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height, 00073 long srcStride, long dstStride); 00074 void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst, 00075 long width, long height, long src1Stride, 00076 long src2Stride, long dstStride); 00077 void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2, 00078 uint8_t *dst1, uint8_t *dst2, 00079 long width, long height, 00080 long srcStride1, long srcStride2, 00081 long dstStride1, long dstStride2); 00082 void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, 00083 uint8_t *dst, 00084 long width, long height, 00085 long srcStride1, long srcStride2, 00086 long srcStride3, long dstStride); 00087 void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src, 00088 long width, long height, 00089 long lumStride, long chromStride, long srcStride); 00090 void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src, 00091 long width, long height, 00092 long lumStride, long chromStride, long srcStride); 00093 void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src, 00094 long width, long height, 00095 long lumStride, long chromStride, long srcStride); 00096 void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src, 00097 long width, long height, 00098 long lumStride, long chromStride, long srcStride); 00099 00100 00101 #if ARCH_X86 00102 DECLARE_ASM_CONST(8, uint64_t, mmx_null) = 0x0000000000000000ULL; 00103 DECLARE_ASM_CONST(8, uint64_t, mmx_one) = 0xFFFFFFFFFFFFFFFFULL; 00104 DECLARE_ASM_CONST(8, uint64_t, mask32b) = 0x000000FF000000FFULL; 00105 DECLARE_ASM_CONST(8, uint64_t, mask32g) = 0x0000FF000000FF00ULL; 00106 DECLARE_ASM_CONST(8, uint64_t, mask32r) = 0x00FF000000FF0000ULL; 00107 DECLARE_ASM_CONST(8, uint64_t, mask32a) = 0xFF000000FF000000ULL; 00108 DECLARE_ASM_CONST(8, uint64_t, mask32) = 0x00FFFFFF00FFFFFFULL; 00109 DECLARE_ASM_CONST(8, uint64_t, mask3216br) = 0x00F800F800F800F8ULL; 00110 DECLARE_ASM_CONST(8, uint64_t, mask3216g) = 0x0000FC000000FC00ULL; 00111 DECLARE_ASM_CONST(8, uint64_t, mask3215g) = 0x0000F8000000F800ULL; 00112 DECLARE_ASM_CONST(8, uint64_t, mul3216) = 0x2000000420000004ULL; 00113 DECLARE_ASM_CONST(8, uint64_t, mul3215) = 0x2000000820000008ULL; 00114 DECLARE_ASM_CONST(8, uint64_t, mask24b) = 0x00FF0000FF0000FFULL; 00115 DECLARE_ASM_CONST(8, uint64_t, mask24g) = 0xFF0000FF0000FF00ULL; 00116 DECLARE_ASM_CONST(8, uint64_t, mask24r) = 0x0000FF0000FF0000ULL; 00117 DECLARE_ASM_CONST(8, uint64_t, mask24l) = 0x0000000000FFFFFFULL; 00118 DECLARE_ASM_CONST(8, uint64_t, mask24h) = 0x0000FFFFFF000000ULL; 00119 DECLARE_ASM_CONST(8, uint64_t, mask24hh) = 0xffff000000000000ULL; 00120 DECLARE_ASM_CONST(8, uint64_t, mask24hhh) = 0xffffffff00000000ULL; 00121 DECLARE_ASM_CONST(8, uint64_t, mask24hhhh) = 0xffffffffffff0000ULL; 00122 DECLARE_ASM_CONST(8, uint64_t, mask15b) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */ 00123 DECLARE_ASM_CONST(8, uint64_t, mask15rg) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */ 00124 DECLARE_ASM_CONST(8, uint64_t, mask15s) = 0xFFE0FFE0FFE0FFE0ULL; 00125 DECLARE_ASM_CONST(8, uint64_t, mask15g) = 0x03E003E003E003E0ULL; 00126 DECLARE_ASM_CONST(8, uint64_t, mask15r) = 0x7C007C007C007C00ULL; 00127 #define mask16b mask15b 00128 DECLARE_ASM_CONST(8, uint64_t, mask16g) = 0x07E007E007E007E0ULL; 00129 DECLARE_ASM_CONST(8, uint64_t, mask16r) = 0xF800F800F800F800ULL; 00130 DECLARE_ASM_CONST(8, uint64_t, red_16mask) = 0x0000f8000000f800ULL; 00131 DECLARE_ASM_CONST(8, uint64_t, green_16mask) = 0x000007e0000007e0ULL; 00132 DECLARE_ASM_CONST(8, uint64_t, blue_16mask) = 0x0000001f0000001fULL; 00133 DECLARE_ASM_CONST(8, uint64_t, red_15mask) = 0x00007c0000007c00ULL; 00134 DECLARE_ASM_CONST(8, uint64_t, green_15mask) = 0x000003e0000003e0ULL; 00135 DECLARE_ASM_CONST(8, uint64_t, blue_15mask) = 0x0000001f0000001fULL; 00136 #endif /* ARCH_X86 */ 00137 00138 #define RGB2YUV_SHIFT 8 00139 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5)) 00140 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5)) 00141 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5)) 00142 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5)) 00143 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5)) 00144 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5)) 00145 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5)) 00146 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5)) 00147 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5)) 00148 00149 //Note: We have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW + MMX2 one. 00150 //plain C versions 00151 #undef HAVE_MMX 00152 #undef HAVE_MMX2 00153 #undef HAVE_AMD3DNOW 00154 #undef HAVE_SSE2 00155 #define HAVE_MMX 0 00156 #define HAVE_MMX2 0 00157 #define HAVE_AMD3DNOW 0 00158 #define HAVE_SSE2 0 00159 #define RENAME(a) a ## _C 00160 #include "rgb2rgb_template.c" 00161 00162 #if ARCH_X86 00163 00164 //MMX versions 00165 #undef RENAME 00166 #undef HAVE_MMX 00167 #define HAVE_MMX 1 00168 #define RENAME(a) a ## _MMX 00169 #include "rgb2rgb_template.c" 00170 00171 //MMX2 versions 00172 #undef RENAME 00173 #undef HAVE_MMX2 00174 #define HAVE_MMX2 1 00175 #define RENAME(a) a ## _MMX2 00176 #include "rgb2rgb_template.c" 00177 00178 //3DNOW versions 00179 #undef RENAME 00180 #undef HAVE_MMX2 00181 #undef HAVE_AMD3DNOW 00182 #define HAVE_MMX2 0 00183 #define HAVE_AMD3DNOW 1 00184 #define RENAME(a) a ## _3DNOW 00185 #include "rgb2rgb_template.c" 00186 00187 #endif //ARCH_X86 || ARCH_X86_64 00188 00189 /* 00190 RGB15->RGB16 original by Strepto/Astral 00191 ported to gcc & bugfixed : A'rpi 00192 MMX2, 3DNOW optimization by Nick Kurshev 00193 32-bit C version, and and&add trick by Michael Niedermayer 00194 */ 00195 00196 void sws_rgb2rgb_init(int flags) 00197 { 00198 #if HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX 00199 if (flags & SWS_CPU_CAPS_MMX2) 00200 rgb2rgb_init_MMX2(); 00201 else if (flags & SWS_CPU_CAPS_3DNOW) 00202 rgb2rgb_init_3DNOW(); 00203 else if (flags & SWS_CPU_CAPS_MMX) 00204 rgb2rgb_init_MMX(); 00205 else 00206 #endif /* HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX */ 00207 rgb2rgb_init_C(); 00208 } 00209 00210 #if LIBSWSCALE_VERSION_MAJOR < 1 00211 void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) 00212 { 00213 sws_convertPalette8ToPacked32(src, dst, num_pixels, palette); 00214 } 00215 00216 void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) 00217 { 00218 sws_convertPalette8ToPacked24(src, dst, num_pixels, palette); 00219 } 00220 00224 void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) 00225 { 00226 long i; 00227 for (i=0; i<num_pixels; i++) 00228 ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]]; 00229 } 00230 void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette) 00231 { 00232 long i; 00233 for (i=0; i<num_pixels; i++) 00234 ((uint16_t *)dst)[i] = bswap_16(((const uint16_t *)palette)[src[i]]); 00235 } 00236 00237 #endif 00238 00239 void rgb32to24(const uint8_t *src, uint8_t *dst, long src_size) 00240 { 00241 long i; 00242 long num_pixels = src_size >> 2; 00243 for (i=0; i<num_pixels; i++) { 00244 #if HAVE_BIGENDIAN 00245 /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */ 00246 dst[3*i + 0] = src[4*i + 1]; 00247 dst[3*i + 1] = src[4*i + 2]; 00248 dst[3*i + 2] = src[4*i + 3]; 00249 #else 00250 dst[3*i + 0] = src[4*i + 2]; 00251 dst[3*i + 1] = src[4*i + 1]; 00252 dst[3*i + 2] = src[4*i + 0]; 00253 #endif 00254 } 00255 } 00256 00257 void rgb24to32(const uint8_t *src, uint8_t *dst, long src_size) 00258 { 00259 long i; 00260 for (i=0; 3*i<src_size; i++) { 00261 #if HAVE_BIGENDIAN 00262 /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */ 00263 dst[4*i + 0] = 255; 00264 dst[4*i + 1] = src[3*i + 0]; 00265 dst[4*i + 2] = src[3*i + 1]; 00266 dst[4*i + 3] = src[3*i + 2]; 00267 #else 00268 dst[4*i + 0] = src[3*i + 2]; 00269 dst[4*i + 1] = src[3*i + 1]; 00270 dst[4*i + 2] = src[3*i + 0]; 00271 dst[4*i + 3] = 255; 00272 #endif 00273 } 00274 } 00275 00276 void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size) 00277 { 00278 const uint16_t *end; 00279 uint8_t *d = dst; 00280 const uint16_t *s = (const uint16_t *)src; 00281 end = s + src_size/2; 00282 while (s < end) { 00283 register uint16_t bgr; 00284 bgr = *s++; 00285 #if HAVE_BIGENDIAN 00286 *d++ = 255; 00287 *d++ = (bgr&0x1F)<<3; 00288 *d++ = (bgr&0x7E0)>>3; 00289 *d++ = (bgr&0xF800)>>8; 00290 #else 00291 *d++ = (bgr&0xF800)>>8; 00292 *d++ = (bgr&0x7E0)>>3; 00293 *d++ = (bgr&0x1F)<<3; 00294 *d++ = 255; 00295 #endif 00296 } 00297 } 00298 00299 void rgb16to24(const uint8_t *src, uint8_t *dst, long src_size) 00300 { 00301 const uint16_t *end; 00302 uint8_t *d = dst; 00303 const uint16_t *s = (const uint16_t *)src; 00304 end = s + src_size/2; 00305 while (s < end) { 00306 register uint16_t bgr; 00307 bgr = *s++; 00308 *d++ = (bgr&0xF800)>>8; 00309 *d++ = (bgr&0x7E0)>>3; 00310 *d++ = (bgr&0x1F)<<3; 00311 } 00312 } 00313 00314 void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size) 00315 { 00316 long i; 00317 long num_pixels = src_size >> 1; 00318 00319 for (i=0; i<num_pixels; i++) { 00320 unsigned rgb = ((const uint16_t*)src)[i]; 00321 ((uint16_t*)dst)[i] = (rgb>>11) | (rgb&0x7E0) | (rgb<<11); 00322 } 00323 } 00324 00325 void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size) 00326 { 00327 long i; 00328 long num_pixels = src_size >> 1; 00329 00330 for (i=0; i<num_pixels; i++) { 00331 unsigned rgb = ((const uint16_t*)src)[i]; 00332 ((uint16_t*)dst)[i] = (rgb>>11) | ((rgb&0x7C0)>>1) | ((rgb&0x1F)<<10); 00333 } 00334 } 00335 00336 void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size) 00337 { 00338 const uint16_t *end; 00339 uint8_t *d = dst; 00340 const uint16_t *s = (const uint16_t *)src; 00341 end = s + src_size/2; 00342 while (s < end) { 00343 register uint16_t bgr; 00344 bgr = *s++; 00345 #if HAVE_BIGENDIAN 00346 *d++ = 255; 00347 *d++ = (bgr&0x1F)<<3; 00348 *d++ = (bgr&0x3E0)>>2; 00349 *d++ = (bgr&0x7C00)>>7; 00350 #else 00351 *d++ = (bgr&0x7C00)>>7; 00352 *d++ = (bgr&0x3E0)>>2; 00353 *d++ = (bgr&0x1F)<<3; 00354 *d++ = 255; 00355 #endif 00356 } 00357 } 00358 00359 void rgb15to24(const uint8_t *src, uint8_t *dst, long src_size) 00360 { 00361 const uint16_t *end; 00362 uint8_t *d = dst; 00363 const uint16_t *s = (const uint16_t *)src; 00364 end = s + src_size/2; 00365 while (s < end) { 00366 register uint16_t bgr; 00367 bgr = *s++; 00368 *d++ = (bgr&0x7C00)>>7; 00369 *d++ = (bgr&0x3E0)>>2; 00370 *d++ = (bgr&0x1F)<<3; 00371 } 00372 } 00373 00374 void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size) 00375 { 00376 long i; 00377 long num_pixels = src_size >> 1; 00378 00379 for (i=0; i<num_pixels; i++) { 00380 unsigned rgb = ((const uint16_t*)src)[i]; 00381 ((uint16_t*)dst)[i] = ((rgb&0x7C00)>>10) | ((rgb&0x3E0)<<1) | (rgb<<11); 00382 } 00383 } 00384 00385 void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size) 00386 { 00387 long i; 00388 long num_pixels = src_size >> 1; 00389 00390 for (i=0; i<num_pixels; i++) { 00391 unsigned br; 00392 unsigned rgb = ((const uint16_t*)src)[i]; 00393 br = rgb&0x7c1F; 00394 ((uint16_t*)dst)[i] = (br>>10) | (rgb&0x3E0) | (br<<10); 00395 } 00396 } 00397 00398 void bgr8torgb8(const uint8_t *src, uint8_t *dst, long src_size) 00399 { 00400 long i; 00401 long num_pixels = src_size; 00402 for (i=0; i<num_pixels; i++) { 00403 unsigned b,g,r; 00404 register uint8_t rgb; 00405 rgb = src[i]; 00406 r = (rgb&0x07); 00407 g = (rgb&0x38)>>3; 00408 b = (rgb&0xC0)>>6; 00409 dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6); 00410 } 00411 } 00412 00413 #define DEFINE_SHUFFLE_BYTES(a, b, c, d) \ 00414 void shuffle_bytes_##a##b##c##d(const uint8_t *src, uint8_t *dst, long src_size) \ 00415 { \ 00416 long i; \ 00417 \ 00418 for (i = 0; i < src_size; i+=4) { \ 00419 dst[i + 0] = src[i + a]; \ 00420 dst[i + 1] = src[i + b]; \ 00421 dst[i + 2] = src[i + c]; \ 00422 dst[i + 3] = src[i + d]; \ 00423 } \ 00424 } 00425 00426 DEFINE_SHUFFLE_BYTES(0, 3, 2, 1); 00427 DEFINE_SHUFFLE_BYTES(1, 2, 3, 0); 00428 DEFINE_SHUFFLE_BYTES(2, 1, 0, 3); 00429 DEFINE_SHUFFLE_BYTES(3, 0, 1, 2); 00430 DEFINE_SHUFFLE_BYTES(3, 2, 1, 0); 00431