• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libswscale/rgb2rgb.c

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

Generated on Sat Feb 16 2013 09:23:15 for ffmpeg by  doxygen 1.7.1