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#include <inttypes.h>
26#include "config.h"
27#include "libavutil/x86_cpu.h"
28#include "libavutil/bswap.h"
29#include "rgb2rgb.h"
30#include "swscale.h"
31#include "swscale_internal.h"
32
33#define FAST_BGR2YV12 // use 7-bit instead of 15-bit coefficients
34
35void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
36void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
37void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
38void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
39void (*rgb32to16)(const uint8_t *src, uint8_t *dst, long src_size);
40void (*rgb32to15)(const uint8_t *src, uint8_t *dst, long src_size);
41void (*rgb15to16)(const uint8_t *src, uint8_t *dst, long src_size);
42void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
43void (*rgb15to32)(const uint8_t *src, uint8_t *dst, long src_size);
44void (*rgb16to15)(const uint8_t *src, uint8_t *dst, long src_size);
45void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
46void (*rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size);
47void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
48void (*rgb24to16)(const uint8_t *src, uint8_t *dst, long src_size);
49void (*rgb24to15)(const uint8_t *src, uint8_t *dst, long src_size);
50void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
51void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
52void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
53
54void (*yv12toyuy2)(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);
57void (*yv12touyvy)(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);
60void (*yuv422ptoyuy2)(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);
63void (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
64                      long width, long height,
65                      long lumStride, long chromStride, long dstStride);
66void (*yuy2toyv12)(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);
69void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
70                    long width, long height,
71                    long lumStride, long chromStride, long srcStride);
72void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
73                 long srcStride, long dstStride);
74void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
75                        long width, long height, long src1Stride,
76                        long src2Stride, long dstStride);
77void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
78                    uint8_t *dst1, uint8_t *dst2,
79                    long width, long height,
80                    long srcStride1, long srcStride2,
81                    long dstStride1, long dstStride2);
82void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
83                     uint8_t *dst,
84                     long width, long height,
85                     long srcStride1, long srcStride2,
86                     long srcStride3, long dstStride);
87void (*uyvytoyuv420)(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);
90void (*uyvytoyuv422)(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);
93void (*yuyvtoyuv420)(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);
96void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
97                     long width, long height,
98                     long lumStride, long chromStride, long srcStride);
99
100
101#if ARCH_X86
102DECLARE_ASM_CONST(8, uint64_t, mmx_null)     = 0x0000000000000000ULL;
103DECLARE_ASM_CONST(8, uint64_t, mmx_one)      = 0xFFFFFFFFFFFFFFFFULL;
104DECLARE_ASM_CONST(8, uint64_t, mask32b)      = 0x000000FF000000FFULL;
105DECLARE_ASM_CONST(8, uint64_t, mask32g)      = 0x0000FF000000FF00ULL;
106DECLARE_ASM_CONST(8, uint64_t, mask32r)      = 0x00FF000000FF0000ULL;
107DECLARE_ASM_CONST(8, uint64_t, mask32a)      = 0xFF000000FF000000ULL;
108DECLARE_ASM_CONST(8, uint64_t, mask32)       = 0x00FFFFFF00FFFFFFULL;
109DECLARE_ASM_CONST(8, uint64_t, mask3216br)   = 0x00F800F800F800F8ULL;
110DECLARE_ASM_CONST(8, uint64_t, mask3216g)    = 0x0000FC000000FC00ULL;
111DECLARE_ASM_CONST(8, uint64_t, mask3215g)    = 0x0000F8000000F800ULL;
112DECLARE_ASM_CONST(8, uint64_t, mul3216)      = 0x2000000420000004ULL;
113DECLARE_ASM_CONST(8, uint64_t, mul3215)      = 0x2000000820000008ULL;
114DECLARE_ASM_CONST(8, uint64_t, mask24b)      = 0x00FF0000FF0000FFULL;
115DECLARE_ASM_CONST(8, uint64_t, mask24g)      = 0xFF0000FF0000FF00ULL;
116DECLARE_ASM_CONST(8, uint64_t, mask24r)      = 0x0000FF0000FF0000ULL;
117DECLARE_ASM_CONST(8, uint64_t, mask24l)      = 0x0000000000FFFFFFULL;
118DECLARE_ASM_CONST(8, uint64_t, mask24h)      = 0x0000FFFFFF000000ULL;
119DECLARE_ASM_CONST(8, uint64_t, mask24hh)     = 0xffff000000000000ULL;
120DECLARE_ASM_CONST(8, uint64_t, mask24hhh)    = 0xffffffff00000000ULL;
121DECLARE_ASM_CONST(8, uint64_t, mask24hhhh)   = 0xffffffffffff0000ULL;
122DECLARE_ASM_CONST(8, uint64_t, mask15b)      = 0x001F001F001F001FULL; /* 00000000 00011111  xxB */
123DECLARE_ASM_CONST(8, uint64_t, mask15rg)     = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000  RGx */
124DECLARE_ASM_CONST(8, uint64_t, mask15s)      = 0xFFE0FFE0FFE0FFE0ULL;
125DECLARE_ASM_CONST(8, uint64_t, mask15g)      = 0x03E003E003E003E0ULL;
126DECLARE_ASM_CONST(8, uint64_t, mask15r)      = 0x7C007C007C007C00ULL;
127#define mask16b mask15b
128DECLARE_ASM_CONST(8, uint64_t, mask16g)      = 0x07E007E007E007E0ULL;
129DECLARE_ASM_CONST(8, uint64_t, mask16r)      = 0xF800F800F800F800ULL;
130DECLARE_ASM_CONST(8, uint64_t, red_16mask)   = 0x0000f8000000f800ULL;
131DECLARE_ASM_CONST(8, uint64_t, green_16mask) = 0x000007e0000007e0ULL;
132DECLARE_ASM_CONST(8, uint64_t, blue_16mask)  = 0x0000001f0000001fULL;
133DECLARE_ASM_CONST(8, uint64_t, red_15mask)   = 0x00007c0000007c00ULL;
134DECLARE_ASM_CONST(8, uint64_t, green_15mask) = 0x000003e0000003e0ULL;
135DECLARE_ASM_CONST(8, uint64_t, blue_15mask)  = 0x0000001f0000001fULL;
136#endif /* ARCH_X86 */
137
138#define RGB2YUV_SHIFT 8
139#define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
140#define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
141#define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
142#define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
143#define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
144#define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
145#define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
146#define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
147#define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
148
149//Note: We have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW + MMX2 one.
150//plain C versions
151#undef HAVE_MMX
152#undef HAVE_MMX2
153#undef HAVE_AMD3DNOW
154#undef HAVE_SSE2
155#define HAVE_MMX 0
156#define HAVE_MMX2 0
157#define HAVE_AMD3DNOW 0
158#define HAVE_SSE2 0
159#define RENAME(a) a ## _C
160#include "rgb2rgb_template.c"
161
162#if ARCH_X86
163
164//MMX versions
165#undef RENAME
166#undef HAVE_MMX
167#define HAVE_MMX 1
168#define RENAME(a) a ## _MMX
169#include "rgb2rgb_template.c"
170
171//MMX2 versions
172#undef RENAME
173#undef HAVE_MMX2
174#define HAVE_MMX2 1
175#define RENAME(a) a ## _MMX2
176#include "rgb2rgb_template.c"
177
178//3DNOW versions
179#undef RENAME
180#undef HAVE_MMX2
181#undef HAVE_AMD3DNOW
182#define HAVE_MMX2 0
183#define HAVE_AMD3DNOW 1
184#define RENAME(a) a ## _3DNOW
185#include "rgb2rgb_template.c"
186
187#endif //ARCH_X86 || ARCH_X86_64
188
189/*
190 RGB15->RGB16 original by Strepto/Astral
191 ported to gcc & bugfixed : A'rpi
192 MMX2, 3DNOW optimization by Nick Kurshev
193 32-bit C version, and and&add trick by Michael Niedermayer
194*/
195
196void sws_rgb2rgb_init(int flags)
197{
198#if HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX
199    if (flags & SWS_CPU_CAPS_MMX2)
200        rgb2rgb_init_MMX2();
201    else if (flags & SWS_CPU_CAPS_3DNOW)
202        rgb2rgb_init_3DNOW();
203    else if (flags & SWS_CPU_CAPS_MMX)
204        rgb2rgb_init_MMX();
205    else
206#endif /* HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX */
207        rgb2rgb_init_C();
208}
209
210#if LIBSWSCALE_VERSION_MAJOR < 1
211void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
212{
213    sws_convertPalette8ToPacked32(src, dst, num_pixels, palette);
214}
215
216void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
217{
218    sws_convertPalette8ToPacked24(src, dst, num_pixels, palette);
219}
220
221/**
222 * Palette is assumed to contain BGR16, see rgb32to16 to convert the palette.
223 */
224void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
225{
226    long i;
227    for (i=0; i<num_pixels; i++)
228        ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
229}
230void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
231{
232    long i;
233    for (i=0; i<num_pixels; i++)
234        ((uint16_t *)dst)[i] = bswap_16(((const uint16_t *)palette)[src[i]]);
235}
236
237#endif
238
239void rgb32to24(const uint8_t *src, uint8_t *dst, long src_size)
240{
241    long i;
242    long num_pixels = src_size >> 2;
243    for (i=0; i<num_pixels; i++) {
244#if HAVE_BIGENDIAN
245        /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
246        dst[3*i + 0] = src[4*i + 1];
247        dst[3*i + 1] = src[4*i + 2];
248        dst[3*i + 2] = src[4*i + 3];
249#else
250        dst[3*i + 0] = src[4*i + 2];
251        dst[3*i + 1] = src[4*i + 1];
252        dst[3*i + 2] = src[4*i + 0];
253#endif
254    }
255}
256
257void rgb24to32(const uint8_t *src, uint8_t *dst, long src_size)
258{
259    long i;
260    for (i=0; 3*i<src_size; i++) {
261#if HAVE_BIGENDIAN
262        /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
263        dst[4*i + 0] = 255;
264        dst[4*i + 1] = src[3*i + 0];
265        dst[4*i + 2] = src[3*i + 1];
266        dst[4*i + 3] = src[3*i + 2];
267#else
268        dst[4*i + 0] = src[3*i + 2];
269        dst[4*i + 1] = src[3*i + 1];
270        dst[4*i + 2] = src[3*i + 0];
271        dst[4*i + 3] = 255;
272#endif
273    }
274}
275
276void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
277{
278    const uint16_t *end;
279    uint8_t *d = dst;
280    const uint16_t *s = (const uint16_t *)src;
281    end = s + src_size/2;
282    while (s < end) {
283        register uint16_t bgr;
284        bgr = *s++;
285#if HAVE_BIGENDIAN
286        *d++ = 255;
287        *d++ = (bgr&0x1F)<<3;
288        *d++ = (bgr&0x7E0)>>3;
289        *d++ = (bgr&0xF800)>>8;
290#else
291        *d++ = (bgr&0xF800)>>8;
292        *d++ = (bgr&0x7E0)>>3;
293        *d++ = (bgr&0x1F)<<3;
294        *d++ = 255;
295#endif
296    }
297}
298
299void rgb16to24(const uint8_t *src, uint8_t *dst, long src_size)
300{
301    const uint16_t *end;
302    uint8_t *d = dst;
303    const uint16_t *s = (const uint16_t *)src;
304    end = s + src_size/2;
305    while (s < end) {
306        register uint16_t bgr;
307        bgr = *s++;
308        *d++ = (bgr&0xF800)>>8;
309        *d++ = (bgr&0x7E0)>>3;
310        *d++ = (bgr&0x1F)<<3;
311    }
312}
313
314void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
315{
316    long i;
317    long num_pixels = src_size >> 1;
318
319    for (i=0; i<num_pixels; i++) {
320        unsigned rgb = ((const uint16_t*)src)[i];
321        ((uint16_t*)dst)[i] = (rgb>>11) | (rgb&0x7E0) | (rgb<<11);
322    }
323}
324
325void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
326{
327    long i;
328    long num_pixels = src_size >> 1;
329
330    for (i=0; i<num_pixels; i++) {
331        unsigned rgb = ((const uint16_t*)src)[i];
332        ((uint16_t*)dst)[i] = (rgb>>11) | ((rgb&0x7C0)>>1) | ((rgb&0x1F)<<10);
333    }
334}
335
336void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
337{
338    const uint16_t *end;
339    uint8_t *d = dst;
340    const uint16_t *s = (const uint16_t *)src;
341    end = s + src_size/2;
342    while (s < end) {
343        register uint16_t bgr;
344        bgr = *s++;
345#if HAVE_BIGENDIAN
346        *d++ = 255;
347        *d++ = (bgr&0x1F)<<3;
348        *d++ = (bgr&0x3E0)>>2;
349        *d++ = (bgr&0x7C00)>>7;
350#else
351        *d++ = (bgr&0x7C00)>>7;
352        *d++ = (bgr&0x3E0)>>2;
353        *d++ = (bgr&0x1F)<<3;
354        *d++ = 255;
355#endif
356    }
357}
358
359void rgb15to24(const uint8_t *src, uint8_t *dst, long src_size)
360{
361    const uint16_t *end;
362    uint8_t *d = dst;
363    const uint16_t *s = (const uint16_t *)src;
364    end = s + src_size/2;
365    while (s < end) {
366        register uint16_t bgr;
367        bgr = *s++;
368        *d++ = (bgr&0x7C00)>>7;
369        *d++ = (bgr&0x3E0)>>2;
370        *d++ = (bgr&0x1F)<<3;
371    }
372}
373
374void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
375{
376    long i;
377    long num_pixels = src_size >> 1;
378
379    for (i=0; i<num_pixels; i++) {
380        unsigned rgb = ((const uint16_t*)src)[i];
381        ((uint16_t*)dst)[i] = ((rgb&0x7C00)>>10) | ((rgb&0x3E0)<<1) | (rgb<<11);
382    }
383}
384
385void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
386{
387    long i;
388    long num_pixels = src_size >> 1;
389
390    for (i=0; i<num_pixels; i++) {
391        unsigned br;
392        unsigned rgb = ((const uint16_t*)src)[i];
393        br = rgb&0x7c1F;
394        ((uint16_t*)dst)[i] = (br>>10) | (rgb&0x3E0) | (br<<10);
395    }
396}
397
398void bgr8torgb8(const uint8_t *src, uint8_t *dst, long src_size)
399{
400    long i;
401    long num_pixels = src_size;
402    for (i=0; i<num_pixels; i++) {
403        unsigned b,g,r;
404        register uint8_t rgb;
405        rgb = src[i];
406        r = (rgb&0x07);
407        g = (rgb&0x38)>>3;
408        b = (rgb&0xC0)>>6;
409        dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
410    }
411}
412
413#define DEFINE_SHUFFLE_BYTES(a, b, c, d)                                \
414void shuffle_bytes_##a##b##c##d(const uint8_t *src, uint8_t *dst, long src_size) \
415{                                                                       \
416    long i;                                                             \
417                                                                        \
418    for (i = 0; i < src_size; i+=4) {                                   \
419        dst[i + 0] = src[i + a];                                        \
420        dst[i + 1] = src[i + b];                                        \
421        dst[i + 2] = src[i + c];                                        \
422        dst[i + 3] = src[i + d];                                        \
423    }                                                                   \
424}
425
426DEFINE_SHUFFLE_BYTES(0, 3, 2, 1);
427DEFINE_SHUFFLE_BYTES(1, 2, 3, 0);
428DEFINE_SHUFFLE_BYTES(2, 1, 0, 3);
429DEFINE_SHUFFLE_BYTES(3, 0, 1, 2);
430DEFINE_SHUFFLE_BYTES(3, 2, 1, 0);
431
432