swscale: move away x86 specific code from rgb2rgb
[libav.git] / libswscale / rgb2rgb.c
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 Libav.
10 *
11 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 #include <inttypes.h>
26 #include "config.h"
27 #include "libavutil/bswap.h"
28 #include "rgb2rgb.h"
29 #include "swscale.h"
30 #include "swscale_internal.h"
31
32 void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
33 void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
34 void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
35 void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
36 void (*rgb32to16)(const uint8_t *src, uint8_t *dst, long src_size);
37 void (*rgb32to15)(const uint8_t *src, uint8_t *dst, long src_size);
38 void (*rgb15to16)(const uint8_t *src, uint8_t *dst, long src_size);
39 void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
40 void (*rgb15to32)(const uint8_t *src, uint8_t *dst, long src_size);
41 void (*rgb16to15)(const uint8_t *src, uint8_t *dst, long src_size);
42 void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
43 void (*rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size);
44 void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
45 void (*rgb24to16)(const uint8_t *src, uint8_t *dst, long src_size);
46 void (*rgb24to15)(const uint8_t *src, uint8_t *dst, long src_size);
47 void (*shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, long src_size);
48 void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
49 void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
50
51 void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
52 long width, long height,
53 long lumStride, long chromStride, long dstStride);
54 void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
55 long width, long height,
56 long lumStride, long chromStride, long dstStride);
57 void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
58 long width, long height,
59 long lumStride, long chromStride, long dstStride);
60 void (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
61 long width, long height,
62 long lumStride, long chromStride, long dstStride);
63 void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
64 long width, long height,
65 long lumStride, long chromStride, long srcStride);
66 void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
67 long width, long height,
68 long lumStride, long chromStride, long srcStride);
69 void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
70 long srcStride, long dstStride);
71 void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
72 long width, long height, long src1Stride,
73 long src2Stride, long dstStride);
74 void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
75 uint8_t *dst1, uint8_t *dst2,
76 long width, long height,
77 long srcStride1, long srcStride2,
78 long dstStride1, long dstStride2);
79 void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
80 uint8_t *dst,
81 long width, long height,
82 long srcStride1, long srcStride2,
83 long srcStride3, long dstStride);
84 void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
85 long width, long height,
86 long lumStride, long chromStride, long srcStride);
87 void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
88 long width, long height,
89 long lumStride, long chromStride, long srcStride);
90 void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
91 long width, long height,
92 long lumStride, long chromStride, long srcStride);
93 void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
94 long width, long height,
95 long lumStride, long chromStride, long srcStride);
96
97 #define RGB2YUV_SHIFT 8
98 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
99 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
100 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
101 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
102 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
103 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
104 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
105 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
106 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
107
108 //plain C versions
109 #include "rgb2rgb_template.c"
110
111
112 /*
113 RGB15->RGB16 original by Strepto/Astral
114 ported to gcc & bugfixed : A'rpi
115 MMX2, 3DNOW optimization by Nick Kurshev
116 32-bit C version, and and&add trick by Michael Niedermayer
117 */
118
119 void sws_rgb2rgb_init(int flags)
120 {
121 rgb2rgb_init_c();
122 #if HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX
123 rgb2rgb_init_x86(flags);
124 #endif /* HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX */
125 }
126
127 #if LIBSWSCALE_VERSION_MAJOR < 1
128 void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
129 {
130 sws_convertPalette8ToPacked32(src, dst, num_pixels, palette);
131 }
132
133 void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
134 {
135 sws_convertPalette8ToPacked24(src, dst, num_pixels, palette);
136 }
137
138 /**
139 * Palette is assumed to contain BGR16, see rgb32to16 to convert the palette.
140 */
141 void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
142 {
143 long i;
144 for (i=0; i<num_pixels; i++)
145 ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
146 }
147 void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
148 {
149 long i;
150 for (i=0; i<num_pixels; i++)
151 ((uint16_t *)dst)[i] = av_bswap16(((const uint16_t *)palette)[src[i]]);
152 }
153 #endif
154
155 void rgb32to24(const uint8_t *src, uint8_t *dst, long src_size)
156 {
157 long i;
158 long num_pixels = src_size >> 2;
159 for (i=0; i<num_pixels; i++) {
160 #if HAVE_BIGENDIAN
161 /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
162 dst[3*i + 0] = src[4*i + 1];
163 dst[3*i + 1] = src[4*i + 2];
164 dst[3*i + 2] = src[4*i + 3];
165 #else
166 dst[3*i + 0] = src[4*i + 2];
167 dst[3*i + 1] = src[4*i + 1];
168 dst[3*i + 2] = src[4*i + 0];
169 #endif
170 }
171 }
172
173 void rgb24to32(const uint8_t *src, uint8_t *dst, long src_size)
174 {
175 long i;
176 for (i=0; 3*i<src_size; i++) {
177 #if HAVE_BIGENDIAN
178 /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
179 dst[4*i + 0] = 255;
180 dst[4*i + 1] = src[3*i + 0];
181 dst[4*i + 2] = src[3*i + 1];
182 dst[4*i + 3] = src[3*i + 2];
183 #else
184 dst[4*i + 0] = src[3*i + 2];
185 dst[4*i + 1] = src[3*i + 1];
186 dst[4*i + 2] = src[3*i + 0];
187 dst[4*i + 3] = 255;
188 #endif
189 }
190 }
191
192 void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
193 {
194 const uint16_t *end;
195 uint8_t *d = dst;
196 const uint16_t *s = (const uint16_t *)src;
197 end = s + src_size/2;
198 while (s < end) {
199 register uint16_t bgr;
200 bgr = *s++;
201 #if HAVE_BIGENDIAN
202 *d++ = 255;
203 *d++ = (bgr&0x1F)<<3;
204 *d++ = (bgr&0x7E0)>>3;
205 *d++ = (bgr&0xF800)>>8;
206 #else
207 *d++ = (bgr&0xF800)>>8;
208 *d++ = (bgr&0x7E0)>>3;
209 *d++ = (bgr&0x1F)<<3;
210 *d++ = 255;
211 #endif
212 }
213 }
214
215 void rgb16to24(const uint8_t *src, uint8_t *dst, long src_size)
216 {
217 const uint16_t *end;
218 uint8_t *d = dst;
219 const uint16_t *s = (const uint16_t *)src;
220 end = s + src_size/2;
221 while (s < end) {
222 register uint16_t bgr;
223 bgr = *s++;
224 *d++ = (bgr&0xF800)>>8;
225 *d++ = (bgr&0x7E0)>>3;
226 *d++ = (bgr&0x1F)<<3;
227 }
228 }
229
230 void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
231 {
232 long i;
233 long num_pixels = src_size >> 1;
234
235 for (i=0; i<num_pixels; i++) {
236 unsigned rgb = ((const uint16_t*)src)[i];
237 ((uint16_t*)dst)[i] = (rgb>>11) | (rgb&0x7E0) | (rgb<<11);
238 }
239 }
240
241 void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
242 {
243 long i;
244 long num_pixels = src_size >> 1;
245
246 for (i=0; i<num_pixels; i++) {
247 unsigned rgb = ((const uint16_t*)src)[i];
248 ((uint16_t*)dst)[i] = (rgb>>11) | ((rgb&0x7C0)>>1) | ((rgb&0x1F)<<10);
249 }
250 }
251
252 void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
253 {
254 const uint16_t *end;
255 uint8_t *d = dst;
256 const uint16_t *s = (const uint16_t *)src;
257 end = s + src_size/2;
258 while (s < end) {
259 register uint16_t bgr;
260 bgr = *s++;
261 #if HAVE_BIGENDIAN
262 *d++ = 255;
263 *d++ = (bgr&0x1F)<<3;
264 *d++ = (bgr&0x3E0)>>2;
265 *d++ = (bgr&0x7C00)>>7;
266 #else
267 *d++ = (bgr&0x7C00)>>7;
268 *d++ = (bgr&0x3E0)>>2;
269 *d++ = (bgr&0x1F)<<3;
270 *d++ = 255;
271 #endif
272 }
273 }
274
275 void rgb15to24(const uint8_t *src, uint8_t *dst, long src_size)
276 {
277 const uint16_t *end;
278 uint8_t *d = dst;
279 const uint16_t *s = (const uint16_t *)src;
280 end = s + src_size/2;
281 while (s < end) {
282 register uint16_t bgr;
283 bgr = *s++;
284 *d++ = (bgr&0x7C00)>>7;
285 *d++ = (bgr&0x3E0)>>2;
286 *d++ = (bgr&0x1F)<<3;
287 }
288 }
289
290 void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
291 {
292 long i;
293 long num_pixels = src_size >> 1;
294
295 for (i=0; i<num_pixels; i++) {
296 unsigned rgb = ((const uint16_t*)src)[i];
297 ((uint16_t*)dst)[i] = ((rgb&0x7C00)>>10) | ((rgb&0x3E0)<<1) | (rgb<<11);
298 }
299 }
300
301 void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
302 {
303 long i;
304 long num_pixels = src_size >> 1;
305
306 for (i=0; i<num_pixels; i++) {
307 unsigned br;
308 unsigned rgb = ((const uint16_t*)src)[i];
309 br = rgb&0x7c1F;
310 ((uint16_t*)dst)[i] = (br>>10) | (rgb&0x3E0) | (br<<10);
311 }
312 }
313
314 void bgr8torgb8(const uint8_t *src, uint8_t *dst, long src_size)
315 {
316 long i;
317 long num_pixels = src_size;
318 for (i=0; i<num_pixels; i++) {
319 unsigned b,g,r;
320 register uint8_t rgb;
321 rgb = src[i];
322 r = (rgb&0x07);
323 g = (rgb&0x38)>>3;
324 b = (rgb&0xC0)>>6;
325 dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
326 }
327 }
328
329 #define DEFINE_SHUFFLE_BYTES(a, b, c, d) \
330 void shuffle_bytes_##a##b##c##d(const uint8_t *src, uint8_t *dst, long src_size) \
331 { \
332 long i; \
333 \
334 for (i = 0; i < src_size; i+=4) { \
335 dst[i + 0] = src[i + a]; \
336 dst[i + 1] = src[i + b]; \
337 dst[i + 2] = src[i + c]; \
338 dst[i + 3] = src[i + d]; \
339 } \
340 }
341
342 DEFINE_SHUFFLE_BYTES(0, 3, 2, 1);
343 DEFINE_SHUFFLE_BYTES(1, 2, 3, 0);
344 DEFINE_SHUFFLE_BYTES(3, 0, 1, 2);
345 DEFINE_SHUFFLE_BYTES(3, 2, 1, 0);
346