FFmpeg
rgb2rgb.c
Go to the documentation of this file.
1 /*
2  * software RGB to RGB converter
3  * pluralize by software PAL8 to RGB converter
4  * software YUV to YUV converter
5  * software YUV to RGB converter
6  * Written by Nick Kurshev.
7  * palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
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 GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include <stdint.h>
27 
28 #include "config.h"
29 #include "libavutil/attributes.h"
30 #include "libavutil/x86/cpu.h"
31 #include "libavutil/cpu.h"
32 #include "libavutil/bswap.h"
33 #include "libavutil/mem_internal.h"
34 
35 #include "libswscale/rgb2rgb.h"
36 #include "libswscale/swscale.h"
38 
39 #if HAVE_INLINE_ASM
40 
41 DECLARE_ASM_CONST(8, uint64_t, mmx_ff) = 0x00000000000000FFULL;
42 DECLARE_ASM_CONST(8, uint64_t, mmx_null) = 0x0000000000000000ULL;
43 DECLARE_ASM_CONST(8, uint64_t, mask32a) = 0xFF000000FF000000ULL;
44 DECLARE_ASM_CONST(8, uint64_t, mask3216br) = 0x00F800F800F800F8ULL;
45 DECLARE_ASM_CONST(8, uint64_t, mask3216g) = 0x0000FC000000FC00ULL;
46 DECLARE_ASM_CONST(8, uint64_t, mask3215g) = 0x0000F8000000F800ULL;
47 DECLARE_ASM_CONST(8, uint64_t, mul3216) = 0x2000000420000004ULL;
48 DECLARE_ASM_CONST(8, uint64_t, mul3215) = 0x2000000820000008ULL;
49 DECLARE_ASM_CONST(8, uint64_t, mask24b) = 0x00FF0000FF0000FFULL;
50 DECLARE_ASM_CONST(8, uint64_t, mask24g) = 0xFF0000FF0000FF00ULL;
51 DECLARE_ASM_CONST(8, uint64_t, mask24r) = 0x0000FF0000FF0000ULL;
52 DECLARE_ASM_CONST(8, uint64_t, mask24l) = 0x0000000000FFFFFFULL;
53 DECLARE_ASM_CONST(8, uint64_t, mask24h) = 0x0000FFFFFF000000ULL;
54 DECLARE_ASM_CONST(8, uint64_t, mask15b) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
55 DECLARE_ASM_CONST(8, uint64_t, mask15rg) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
56 DECLARE_ASM_CONST(8, uint64_t, mask15s) = 0xFFE0FFE0FFE0FFE0ULL;
57 DECLARE_ASM_CONST(8, uint64_t, mask15g) = 0x03E003E003E003E0ULL;
58 DECLARE_ASM_CONST(8, uint64_t, mask15r) = 0x7C007C007C007C00ULL;
59 #define mask16b mask15b
60 DECLARE_ASM_CONST(8, uint64_t, mask16g) = 0x07E007E007E007E0ULL;
61 DECLARE_ASM_CONST(8, uint64_t, mask16r) = 0xF800F800F800F800ULL;
62 DECLARE_ASM_CONST(8, uint64_t, red_16mask) = 0x0000f8000000f800ULL;
63 DECLARE_ASM_CONST(8, uint64_t, green_16mask) = 0x000007e0000007e0ULL;
64 DECLARE_ASM_CONST(8, uint64_t, blue_16mask) = 0x0000001f0000001fULL;
65 DECLARE_ASM_CONST(8, uint64_t, red_15mask) = 0x00007c0000007c00ULL;
66 DECLARE_ASM_CONST(8, uint64_t, green_15mask) = 0x000003e0000003e0ULL;
67 DECLARE_ASM_CONST(8, uint64_t, blue_15mask) = 0x0000001f0000001fULL;
68 DECLARE_ASM_CONST(8, uint64_t, mul15_mid) = 0x4200420042004200ULL;
69 DECLARE_ASM_CONST(8, uint64_t, mul15_hi) = 0x0210021002100210ULL;
70 DECLARE_ASM_CONST(8, uint64_t, mul16_mid) = 0x2080208020802080ULL;
71 
72 DECLARE_ALIGNED(8, extern const uint64_t, ff_bgr2YOffset);
73 DECLARE_ALIGNED(8, extern const uint64_t, ff_w1111);
74 DECLARE_ALIGNED(8, extern const uint64_t, ff_bgr2UVOffset);
75 
76 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
77 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
78 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
79 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
80 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
81 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
82 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
83 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
84 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
85 
86 // Note: We have C, MMX, MMXEXT, 3DNOW versions, there is no 3DNOW + MMXEXT one.
87 
88 #define COMPILE_TEMPLATE_MMXEXT 0
89 #define COMPILE_TEMPLATE_AMD3DNOW 0
90 #define COMPILE_TEMPLATE_SSE2 0
91 #define COMPILE_TEMPLATE_AVX 0
92 
93 //MMX versions
94 #undef RENAME
95 #define RENAME(a) a ## _mmx
96 #include "rgb2rgb_template.c"
97 
98 // MMXEXT versions
99 #undef RENAME
100 #undef COMPILE_TEMPLATE_MMXEXT
101 #define COMPILE_TEMPLATE_MMXEXT 1
102 #define RENAME(a) a ## _mmxext
103 #include "rgb2rgb_template.c"
104 
105 //SSE2 versions
106 #undef RENAME
107 #undef COMPILE_TEMPLATE_SSE2
108 #define COMPILE_TEMPLATE_SSE2 1
109 #define RENAME(a) a ## _sse2
110 #include "rgb2rgb_template.c"
111 
112 //AVX versions
113 #undef RENAME
114 #undef COMPILE_TEMPLATE_AVX
115 #define COMPILE_TEMPLATE_AVX 1
116 #define RENAME(a) a ## _avx
117 #include "rgb2rgb_template.c"
118 
119 //3DNOW versions
120 #undef RENAME
121 #undef COMPILE_TEMPLATE_MMXEXT
122 #undef COMPILE_TEMPLATE_SSE2
123 #undef COMPILE_TEMPLATE_AVX
124 #undef COMPILE_TEMPLATE_AMD3DNOW
125 #define COMPILE_TEMPLATE_MMXEXT 0
126 #define COMPILE_TEMPLATE_SSE2 0
127 #define COMPILE_TEMPLATE_AVX 0
128 #define COMPILE_TEMPLATE_AMD3DNOW 1
129 #define RENAME(a) a ## _3dnow
130 #include "rgb2rgb_template.c"
131 
132 /*
133  RGB15->RGB16 original by Strepto/Astral
134  ported to gcc & bugfixed : A'rpi
135  MMXEXT, 3DNOW optimization by Nick Kurshev
136  32-bit C version, and and&add trick by Michael Niedermayer
137 */
138 
139 #endif /* HAVE_INLINE_ASM */
140 
141 void ff_shuffle_bytes_2103_mmxext(const uint8_t *src, uint8_t *dst, int src_size);
142 void ff_shuffle_bytes_2103_ssse3(const uint8_t *src, uint8_t *dst, int src_size);
143 void ff_shuffle_bytes_0321_ssse3(const uint8_t *src, uint8_t *dst, int src_size);
144 void ff_shuffle_bytes_1230_ssse3(const uint8_t *src, uint8_t *dst, int src_size);
145 void ff_shuffle_bytes_3012_ssse3(const uint8_t *src, uint8_t *dst, int src_size);
146 void ff_shuffle_bytes_3210_ssse3(const uint8_t *src, uint8_t *dst, int src_size);
147 
148 #if ARCH_X86_64
149 void ff_shuffle_bytes_2103_avx2(const uint8_t *src, uint8_t *dst, int src_size);
150 void ff_shuffle_bytes_0321_avx2(const uint8_t *src, uint8_t *dst, int src_size);
151 void ff_shuffle_bytes_1230_avx2(const uint8_t *src, uint8_t *dst, int src_size);
152 void ff_shuffle_bytes_3012_avx2(const uint8_t *src, uint8_t *dst, int src_size);
153 void ff_shuffle_bytes_3210_avx2(const uint8_t *src, uint8_t *dst, int src_size);
154 
155 void ff_uyvytoyuv422_sse2(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
156  const uint8_t *src, int width, int height,
157  int lumStride, int chromStride, int srcStride);
158 void ff_uyvytoyuv422_avx(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
159  const uint8_t *src, int width, int height,
160  int lumStride, int chromStride, int srcStride);
161 #endif
162 
164 {
165  int cpu_flags = av_get_cpu_flags();
166 
167 #if HAVE_INLINE_ASM
168  if (INLINE_MMX(cpu_flags))
169  rgb2rgb_init_mmx();
171  rgb2rgb_init_3dnow();
173  rgb2rgb_init_mmxext();
174  if (INLINE_SSE2(cpu_flags))
175  rgb2rgb_init_sse2();
176  if (INLINE_AVX(cpu_flags))
177  rgb2rgb_init_avx();
178 #endif /* HAVE_INLINE_ASM */
179 
180  if (EXTERNAL_MMXEXT(cpu_flags)) {
182  }
183  if (EXTERNAL_SSE2(cpu_flags)) {
184 #if ARCH_X86_64
185  uyvytoyuv422 = ff_uyvytoyuv422_sse2;
186 #endif
187  }
188  if (EXTERNAL_SSSE3(cpu_flags)) {
194  }
195 #if ARCH_X86_64
197  shuffle_bytes_0321 = ff_shuffle_bytes_0321_avx2;
198  shuffle_bytes_2103 = ff_shuffle_bytes_2103_avx2;
199  shuffle_bytes_1230 = ff_shuffle_bytes_1230_avx2;
200  shuffle_bytes_3012 = ff_shuffle_bytes_3012_avx2;
201  shuffle_bytes_3210 = ff_shuffle_bytes_3210_avx2;
202  }
203  if (EXTERNAL_AVX(cpu_flags)) {
204  uyvytoyuv422 = ff_uyvytoyuv422_avx;
205  }
206 #endif
207 }
INLINE_MMX
#define INLINE_MMX(flags)
Definition: cpu.h:86
shuffle_bytes_3012
void(* shuffle_bytes_3012)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:57
cpu.h
mem_internal.h
DECLARE_ASM_CONST
#define DECLARE_ASM_CONST(n, t, v)
Definition: mem.h:118
ff_shuffle_bytes_3210_ssse3
void ff_shuffle_bytes_3210_ssse3(const uint8_t *src, uint8_t *dst, int src_size)
EXTERNAL_AVX2_FAST
#define EXTERNAL_AVX2_FAST(flags)
Definition: cpu.h:79
shuffle_bytes_3210
void(* shuffle_bytes_3210)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:58
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:98
cpu_flags
static atomic_int cpu_flags
Definition: cpu.c:50
ff_shuffle_bytes_0321_ssse3
void ff_shuffle_bytes_0321_ssse3(const uint8_t *src, uint8_t *dst, int src_size)
av_cold
#define av_cold
Definition: attributes.h:90
ff_shuffle_bytes_3012_ssse3
void ff_shuffle_bytes_3012_ssse3(const uint8_t *src, uint8_t *dst, int src_size)
width
#define width
INLINE_SSE2
#define INLINE_SSE2(flags)
Definition: cpu.h:89
shuffle_bytes_1230
void(* shuffle_bytes_1230)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:56
shuffle_bytes_2103
void(* shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:55
ff_shuffle_bytes_2103_mmxext
void ff_shuffle_bytes_2103_mmxext(const uint8_t *src, uint8_t *dst, int src_size)
INLINE_AVX
#define INLINE_AVX(flags)
Definition: cpu.h:100
src
#define src
Definition: vp8dsp.c:255
cpu.h
rgb2rgb_init_x86
av_cold void rgb2rgb_init_x86(void)
Definition: rgb2rgb.c:163
shuffle_bytes_0321
void(* shuffle_bytes_0321)(const uint8_t *src, uint8_t *dst, int src_size)
Definition: rgb2rgb.c:54
height
#define height
attributes.h
EXTERNAL_SSE2
#define EXTERNAL_SSE2(flags)
Definition: cpu.h:59
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:116
uyvytoyuv422
void(* uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src, int width, int height, int lumStride, int chromStride, int srcStride)
Definition: rgb2rgb.c:107
swscale_internal.h
bswap.h
EXTERNAL_AVX
#define EXTERNAL_AVX(flags)
Definition: cpu.h:70
INLINE_MMXEXT
#define INLINE_MMXEXT(flags)
Definition: cpu.h:87
ff_shuffle_bytes_1230_ssse3
void ff_shuffle_bytes_1230_ssse3(const uint8_t *src, uint8_t *dst, int src_size)
ff_shuffle_bytes_2103_ssse3
void ff_shuffle_bytes_2103_ssse3(const uint8_t *src, uint8_t *dst, int src_size)
EXTERNAL_SSSE3
#define EXTERNAL_SSSE3(flags)
Definition: cpu.h:65
INLINE_AMD3DNOW
#define INLINE_AMD3DNOW(flags)
Definition: cpu.h:84
EXTERNAL_MMXEXT
#define EXTERNAL_MMXEXT(flags)
Definition: cpu.h:57
rgb2rgb.h
swscale.h