Statistics
| Branch: | Revision:

ffmpeg / libswscale / rgb2rgb.c @ c0038328

History | View | Annotate | Download (12.1 KB)

1 fcfbc150 Michael Niedermayer
/*
2 8a322796 Diego Biurrun
 * 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 4fadc2b4 Diego Biurrun
 *
9 2912e87a Mans Rullgard
 * This file is part of Libav.
10 d026b45e Diego Biurrun
 *
11 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
12 819ee683 Diego Biurrun
 * 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 4fadc2b4 Diego Biurrun
 *
16 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
17 4fadc2b4 Diego Biurrun
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 819ee683 Diego Biurrun
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 * Lesser General Public License for more details.
20 4fadc2b4 Diego Biurrun
 *
21 819ee683 Diego Biurrun
 * You should have received a copy of the GNU Lesser General Public
22 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
23 b19bcbaa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 a3aece93 Nick Kurshev
 */
25 b234ae81 Nick Kurshev
#include <inttypes.h>
26 b2d374c9 Diego Biurrun
#include "config.h"
27 83da2c6f Diego Biurrun
#include "libavutil/bswap.h"
28 b234ae81 Nick Kurshev
#include "rgb2rgb.h"
29 700490a4 Michael Niedermayer
#include "swscale.h"
30 94c4def2 Luca Abeni
#include "swscale_internal.h"
31 a3aece93 Nick Kurshev
32 6107059c Michael Niedermayer
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 30c48a0a Benoit Fouet
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 6107059c Michael Niedermayer
void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
40 30c48a0a Benoit Fouet
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 6107059c Michael Niedermayer
void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
43 30c48a0a Benoit Fouet
void (*rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size);
44 7f526efd Reimar Döffinger
void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
45 6107059c Michael Niedermayer
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 a51125b5 Ramiro Polla
void (*shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, long src_size);
48 7f526efd Reimar Döffinger
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 700490a4 Michael Niedermayer
51
void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
52 6e42e6c4 Diego Biurrun
                   long width, long height,
53
                   long lumStride, long chromStride, long dstStride);
54 caeaabe7 Alex Beregszaszi
void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
55 6e42e6c4 Diego Biurrun
                   long width, long height,
56
                   long lumStride, long chromStride, long dstStride);
57 700490a4 Michael Niedermayer
void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
58 6e42e6c4 Diego Biurrun
                      long width, long height,
59
                      long lumStride, long chromStride, long dstStride);
60 a6100f39 Baptiste Coudurier
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 700490a4 Michael Niedermayer
void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
64 6e42e6c4 Diego Biurrun
                   long width, long height,
65
                   long lumStride, long chromStride, long srcStride);
66 700490a4 Michael Niedermayer
void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
67 6e42e6c4 Diego Biurrun
                    long width, long height,
68
                    long lumStride, long chromStride, long srcStride);
69 7f526efd Reimar Döffinger
void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
70 6e42e6c4 Diego Biurrun
                 long srcStride, long dstStride);
71 a959e247 Zuxy Meng
void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
72 6e42e6c4 Diego Biurrun
                        long width, long height, long src1Stride,
73
                        long src2Stride, long dstStride);
74 700490a4 Michael Niedermayer
void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
75 6e42e6c4 Diego Biurrun
                    uint8_t *dst1, uint8_t *dst2,
76
                    long width, long height,
77
                    long srcStride1, long srcStride2,
78
                    long dstStride1, long dstStride2);
79 700490a4 Michael Niedermayer
void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
80 6e42e6c4 Diego Biurrun
                     uint8_t *dst,
81
                     long width, long height,
82
                     long srcStride1, long srcStride2,
83
                     long srcStride3, long dstStride);
84 0411072e Michael Niedermayer
void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
85 9b734d44 Ramiro Polla
                     long width, long height,
86
                     long lumStride, long chromStride, long srcStride);
87 0411072e Michael Niedermayer
void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
88 9b734d44 Ramiro Polla
                     long width, long height,
89
                     long lumStride, long chromStride, long srcStride);
90 0411072e Michael Niedermayer
void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
91 9b734d44 Ramiro Polla
                     long width, long height,
92
                     long lumStride, long chromStride, long srcStride);
93 0411072e Michael Niedermayer
void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
94 9b734d44 Ramiro Polla
                     long width, long height,
95
                     long lumStride, long chromStride, long srcStride);
96 0411072e Michael Niedermayer
97 1de97d84 Michael Niedermayer
#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 8a322796 Diego Biurrun
//plain C versions
109 1de97d84 Michael Niedermayer
#include "rgb2rgb_template.c"
110
111 b238eb2e Nick Kurshev
112 a3aece93 Nick Kurshev
/*
113 8a322796 Diego Biurrun
 RGB15->RGB16 original by Strepto/Astral
114 a3aece93 Nick Kurshev
 ported to gcc & bugfixed : A'rpi
115 51da31f1 Nick Kurshev
 MMX2, 3DNOW optimization by Nick Kurshev
116 8a322796 Diego Biurrun
 32-bit C version, and and&add trick by Michael Niedermayer
117 a3aece93 Nick Kurshev
*/
118 fcfbc150 Michael Niedermayer
119 dd68318c Ramiro Polla
void sws_rgb2rgb_init(int flags)
120
{
121 c0038328 Luca Barbato
    rgb2rgb_init_c();
122 819ee683 Diego Biurrun
#if HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX
123 c0038328 Luca Barbato
    rgb2rgb_init_x86(flags);
124 f4406ec1 Diego Biurrun
#endif /* HAVE_MMX2 || HAVE_AMD3DNOW || HAVE_MMX */
125 ac4d0aea Michael Niedermayer
}
126 700490a4 Michael Niedermayer
127 2b991422 Reinhard Tartler
#if LIBSWSCALE_VERSION_MAJOR < 1
128 522ce957 Vitor Sessak
void palette8topacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
129 fcfbc150 Michael Niedermayer
{
130 2b991422 Reinhard Tartler
    sws_convertPalette8ToPacked32(src, dst, num_pixels, palette);
131 6c1baeb0 Alex Beregszaszi
}
132
133 522ce957 Vitor Sessak
void palette8topacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
134 9ce6584e Michael Niedermayer
{
135 2b991422 Reinhard Tartler
    sws_convertPalette8ToPacked24(src, dst, num_pixels, palette);
136 6c1baeb0 Alex Beregszaszi
}
137
138 fcfbc150 Michael Niedermayer
/**
139 8a322796 Diego Biurrun
 * Palette is assumed to contain BGR16, see rgb32to16 to convert the palette.
140 fcfbc150 Michael Niedermayer
 */
141 7f526efd Reimar Döffinger
void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
142 fcfbc150 Michael Niedermayer
{
143 6e42e6c4 Diego Biurrun
    long i;
144
    for (i=0; i<num_pixels; i++)
145 774c386a Baptiste Coudurier
        ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
146 fcfbc150 Michael Niedermayer
}
147 7f526efd Reimar Döffinger
void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
148 6c1baeb0 Alex Beregszaszi
{
149 6e42e6c4 Diego Biurrun
    long i;
150
    for (i=0; i<num_pixels; i++)
151 6a01eab7 Måns Rullgård
        ((uint16_t *)dst)[i] = av_bswap16(((const uint16_t *)palette)[src[i]]);
152 6c1baeb0 Alex Beregszaszi
}
153 2b991422 Reinhard Tartler
#endif
154 fcfbc150 Michael Niedermayer
155 6107059c Michael Niedermayer
void rgb32to24(const uint8_t *src, uint8_t *dst, long src_size)
156 ac4d0aea Michael Niedermayer
{
157 6e42e6c4 Diego Biurrun
    long i;
158
    long num_pixels = src_size >> 2;
159 dd68318c Ramiro Polla
    for (i=0; i<num_pixels; i++) {
160 7d73d1c3 Ramiro Polla
#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 6e42e6c4 Diego Biurrun
    }
171 ac4d0aea Michael Niedermayer
}
172
173 6107059c Michael Niedermayer
void rgb24to32(const uint8_t *src, uint8_t *dst, long src_size)
174 ac4d0aea Michael Niedermayer
{
175 6e42e6c4 Diego Biurrun
    long i;
176 dd68318c Ramiro Polla
    for (i=0; 3*i<src_size; i++) {
177 7d73d1c3 Ramiro Polla
#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 6e42e6c4 Diego Biurrun
    }
190 ac4d0aea Michael Niedermayer
}
191
192 7f526efd Reimar Döffinger
void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
193 ac4d0aea Michael Niedermayer
{
194 6e42e6c4 Diego Biurrun
    const uint16_t *end;
195 72d6b610 Baptiste Coudurier
    uint8_t *d = dst;
196 774c386a Baptiste Coudurier
    const uint16_t *s = (const uint16_t *)src;
197 6e42e6c4 Diego Biurrun
    end = s + src_size/2;
198 dd68318c Ramiro Polla
    while (s < end) {
199 6e42e6c4 Diego Biurrun
        register uint16_t bgr;
200
        bgr = *s++;
201 7d73d1c3 Ramiro Polla
#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 6e42e6c4 Diego Biurrun
    }
213 ac4d0aea Michael Niedermayer
}
214
215 6107059c Michael Niedermayer
void rgb16to24(const uint8_t *src, uint8_t *dst, long src_size)
216 ac4d0aea Michael Niedermayer
{
217 6e42e6c4 Diego Biurrun
    const uint16_t *end;
218 72d6b610 Baptiste Coudurier
    uint8_t *d = dst;
219 6e42e6c4 Diego Biurrun
    const uint16_t *s = (const uint16_t *)src;
220
    end = s + src_size/2;
221 dd68318c Ramiro Polla
    while (s < end) {
222 6e42e6c4 Diego Biurrun
        register uint16_t bgr;
223
        bgr = *s++;
224
        *d++ = (bgr&0xF800)>>8;
225
        *d++ = (bgr&0x7E0)>>3;
226
        *d++ = (bgr&0x1F)<<3;
227
    }
228 ac4d0aea Michael Niedermayer
}
229
230 7f526efd Reimar Döffinger
void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
231 ac4d0aea Michael Niedermayer
{
232 6e42e6c4 Diego Biurrun
    long i;
233
    long num_pixels = src_size >> 1;
234
235 dd68318c Ramiro Polla
    for (i=0; i<num_pixels; i++) {
236 7372e9bb Michael Niedermayer
        unsigned rgb = ((const uint16_t*)src)[i];
237
        ((uint16_t*)dst)[i] = (rgb>>11) | (rgb&0x7E0) | (rgb<<11);
238 6e42e6c4 Diego Biurrun
    }
239 ac4d0aea Michael Niedermayer
}
240
241 7f526efd Reimar Döffinger
void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
242 ac4d0aea Michael Niedermayer
{
243 6e42e6c4 Diego Biurrun
    long i;
244
    long num_pixels = src_size >> 1;
245
246 dd68318c Ramiro Polla
    for (i=0; i<num_pixels; i++) {
247 7372e9bb Michael Niedermayer
        unsigned rgb = ((const uint16_t*)src)[i];
248
        ((uint16_t*)dst)[i] = (rgb>>11) | ((rgb&0x7C0)>>1) | ((rgb&0x1F)<<10);
249 6e42e6c4 Diego Biurrun
    }
250 ac4d0aea Michael Niedermayer
}
251
252 7f526efd Reimar Döffinger
void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
253 ac4d0aea Michael Niedermayer
{
254 6e42e6c4 Diego Biurrun
    const uint16_t *end;
255 72d6b610 Baptiste Coudurier
    uint8_t *d = dst;
256 6e42e6c4 Diego Biurrun
    const uint16_t *s = (const uint16_t *)src;
257
    end = s + src_size/2;
258 dd68318c Ramiro Polla
    while (s < end) {
259 6e42e6c4 Diego Biurrun
        register uint16_t bgr;
260
        bgr = *s++;
261 7d73d1c3 Ramiro Polla
#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 6e42e6c4 Diego Biurrun
    }
273 ac4d0aea Michael Niedermayer
}
274
275 6107059c Michael Niedermayer
void rgb15to24(const uint8_t *src, uint8_t *dst, long src_size)
276 ac4d0aea Michael Niedermayer
{
277 6e42e6c4 Diego Biurrun
    const uint16_t *end;
278 72d6b610 Baptiste Coudurier
    uint8_t *d = dst;
279 774c386a Baptiste Coudurier
    const uint16_t *s = (const uint16_t *)src;
280 6e42e6c4 Diego Biurrun
    end = s + src_size/2;
281 dd68318c Ramiro Polla
    while (s < end) {
282 6e42e6c4 Diego Biurrun
        register uint16_t bgr;
283
        bgr = *s++;
284
        *d++ = (bgr&0x7C00)>>7;
285
        *d++ = (bgr&0x3E0)>>2;
286
        *d++ = (bgr&0x1F)<<3;
287
    }
288 ac4d0aea Michael Niedermayer
}
289
290 7f526efd Reimar Döffinger
void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
291 ac4d0aea Michael Niedermayer
{
292 6e42e6c4 Diego Biurrun
    long i;
293
    long num_pixels = src_size >> 1;
294
295 dd68318c Ramiro Polla
    for (i=0; i<num_pixels; i++) {
296 7372e9bb Michael Niedermayer
        unsigned rgb = ((const uint16_t*)src)[i];
297
        ((uint16_t*)dst)[i] = ((rgb&0x7C00)>>10) | ((rgb&0x3E0)<<1) | (rgb<<11);
298 6e42e6c4 Diego Biurrun
    }
299 ac4d0aea Michael Niedermayer
}
300
301 7f526efd Reimar Döffinger
void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
302 ac4d0aea Michael Niedermayer
{
303 6e42e6c4 Diego Biurrun
    long i;
304
    long num_pixels = src_size >> 1;
305
306 dd68318c Ramiro Polla
    for (i=0; i<num_pixels; i++) {
307 7372e9bb Michael Niedermayer
        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 6e42e6c4 Diego Biurrun
    }
312 ac4d0aea Michael Niedermayer
}
313
314 6107059c Michael Niedermayer
void bgr8torgb8(const uint8_t *src, uint8_t *dst, long src_size)
315 ac4d0aea Michael Niedermayer
{
316 6e42e6c4 Diego Biurrun
    long i;
317
    long num_pixels = src_size;
318 dd68318c Ramiro Polla
    for (i=0; i<num_pixels; i++) {
319 6e42e6c4 Diego Biurrun
        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 ac4d0aea Michael Niedermayer
}
328 82e5f86b Stefano Sabatini
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);