ppc: Centralize compiler-specific altivec.h #include handling in one place
[libav.git] / libswscale / rgb2rgb.c
CommitLineData
fcfbc150 1/*
8a322796
DB
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)
4fadc2b4 8 *
2912e87a 9 * This file is part of Libav.
d026b45e 10 *
2912e87a 11 * Libav is free software; you can redistribute it and/or
819ee683
DB
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.
4fadc2b4 15 *
2912e87a 16 * Libav is distributed in the hope that it will be useful,
4fadc2b4 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
819ee683
DB
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
4fadc2b4 20 *
819ee683 21 * You should have received a copy of the GNU Lesser General Public
2912e87a 22 * License along with Libav; if not, write to the Free Software
b19bcbaa 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a3aece93 24 */
89d94b21 25
b234ae81 26#include <inttypes.h>
89d94b21 27
5a6e3c03 28#include "libavutil/attributes.h"
83da2c6f 29#include "libavutil/bswap.h"
89d94b21 30#include "config.h"
b234ae81 31#include "rgb2rgb.h"
700490a4 32#include "swscale.h"
94c4def2 33#include "swscale_internal.h"
a3aece93 34
89d94b21
DB
35void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
36void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, int src_size);
37void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, int src_size);
b8e89339 38void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, int src_size);
89d94b21 39void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
b8e89339
AK
40void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, int src_size);
41void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, int src_size);
89d94b21
DB
42void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
43void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, int src_size);
44
b8e89339
AK
45void (*rgb32to16)(const uint8_t *src, uint8_t *dst, int src_size);
46void (*rgb32to15)(const uint8_t *src, uint8_t *dst, int src_size);
b8e89339
AK
47void (*rgb24to16)(const uint8_t *src, uint8_t *dst, int src_size);
48void (*rgb24to15)(const uint8_t *src, uint8_t *dst, int src_size);
89d94b21
DB
49void (*rgb16to32)(const uint8_t *src, uint8_t *dst, int src_size);
50void (*rgb16to15)(const uint8_t *src, uint8_t *dst, int src_size);
51void (*rgb15to16)(const uint8_t *src, uint8_t *dst, int src_size);
52void (*rgb15to32)(const uint8_t *src, uint8_t *dst, int src_size);
53
b8e89339 54void (*shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, int src_size);
700490a4 55
89d94b21
DB
56void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc,
57 const uint8_t *vsrc, uint8_t *dst,
b8e89339
AK
58 int width, int height,
59 int lumStride, int chromStride, int dstStride);
89d94b21
DB
60void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc,
61 const uint8_t *vsrc, uint8_t *dst,
b8e89339
AK
62 int width, int height,
63 int lumStride, int chromStride, int dstStride);
89d94b21
DB
64void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc,
65 const uint8_t *vsrc, uint8_t *dst,
b8e89339
AK
66 int width, int height,
67 int lumStride, int chromStride, int dstStride);
89d94b21
DB
68void (*yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc,
69 const uint8_t *vsrc, uint8_t *dst,
b8e89339
AK
70 int width, int height,
71 int lumStride, int chromStride, int dstStride);
89d94b21
DB
72void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst,
73 uint8_t *udst, uint8_t *vdst,
b8e89339
AK
74 int width, int height,
75 int lumStride, int chromStride, int srcStride);
89d94b21
DB
76void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst,
77 uint8_t *udst, uint8_t *vdst,
b8e89339
AK
78 int width, int height,
79 int lumStride, int chromStride, int srcStride);
80void (*planar2x)(const uint8_t *src, uint8_t *dst, int width, int height,
81 int srcStride, int dstStride);
a959e247 82void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
b8e89339
AK
83 int width, int height, int src1Stride,
84 int src2Stride, int dstStride);
9047491f
MN
85void (*deinterleaveBytes)(const uint8_t *src, uint8_t *dst1, uint8_t *dst2,
86 int width, int height, int srcStride,
87 int dst1Stride, int dst2Stride);
700490a4 88void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
6e42e6c4 89 uint8_t *dst1, uint8_t *dst2,
b8e89339
AK
90 int width, int height,
91 int srcStride1, int srcStride2,
92 int dstStride1, int dstStride2);
89d94b21
DB
93void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2,
94 const uint8_t *src3, uint8_t *dst,
b8e89339
AK
95 int width, int height,
96 int srcStride1, int srcStride2,
97 int srcStride3, int dstStride);
89d94b21
DB
98void (*uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
99 const uint8_t *src, int width, int height,
b8e89339 100 int lumStride, int chromStride, int srcStride);
89d94b21
DB
101void (*uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
102 const uint8_t *src, int width, int height,
b8e89339 103 int lumStride, int chromStride, int srcStride);
89d94b21
DB
104void (*yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
105 const uint8_t *src, int width, int height,
b8e89339 106 int lumStride, int chromStride, int srcStride);
89d94b21
DB
107void (*yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
108 const uint8_t *src, int width, int height,
b8e89339 109 int lumStride, int chromStride, int srcStride);
0411072e 110
1de97d84 111#define RGB2YUV_SHIFT 8
89d94b21
DB
112#define BY ((int)( 0.098 * (1 << RGB2YUV_SHIFT) + 0.5))
113#define BV ((int)(-0.071 * (1 << RGB2YUV_SHIFT) + 0.5))
114#define BU ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
115#define GY ((int)( 0.504 * (1 << RGB2YUV_SHIFT) + 0.5))
116#define GV ((int)(-0.368 * (1 << RGB2YUV_SHIFT) + 0.5))
117#define GU ((int)(-0.291 * (1 << RGB2YUV_SHIFT) + 0.5))
118#define RY ((int)( 0.257 * (1 << RGB2YUV_SHIFT) + 0.5))
119#define RV ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
120#define RU ((int)(-0.148 * (1 << RGB2YUV_SHIFT) + 0.5))
1de97d84 121
8a322796 122//plain C versions
1de97d84
MN
123#include "rgb2rgb_template.c"
124
a3aece93 125/*
89d94b21
DB
126 * RGB15->RGB16 original by Strepto/Astral
127 * ported to gcc & bugfixed : A'rpi
652f5185 128 * MMXEXT, 3DNOW optimization by Nick Kurshev
89d94b21
DB
129 * 32-bit C version, and and&add trick by Michael Niedermayer
130 */
fcfbc150 131
facdfe40 132av_cold void ff_rgb2rgb_init(void)
dd68318c 133{
c0038328 134 rgb2rgb_init_c();
a5195839 135 if (ARCH_X86)
facdfe40 136 ff_rgb2rgb_init_x86();
ac4d0aea 137}
700490a4 138
b8e89339 139void rgb32to24(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 140{
72b95764
DB
141 int i, num_pixels = src_size >> 2;
142
89d94b21 143 for (i = 0; i < num_pixels; i++) {
7d73d1c3
RP
144#if HAVE_BIGENDIAN
145 /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
89d94b21
DB
146 dst[3 * i + 0] = src[4 * i + 1];
147 dst[3 * i + 1] = src[4 * i + 2];
148 dst[3 * i + 2] = src[4 * i + 3];
7d73d1c3 149#else
89d94b21
DB
150 dst[3 * i + 0] = src[4 * i + 2];
151 dst[3 * i + 1] = src[4 * i + 1];
152 dst[3 * i + 2] = src[4 * i + 0];
7d73d1c3 153#endif
6e42e6c4 154 }
ac4d0aea
MN
155}
156
b8e89339 157void rgb24to32(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 158{
b8e89339 159 int i;
89d94b21
DB
160
161 for (i = 0; 3 * i < src_size; i++) {
7d73d1c3 162#if HAVE_BIGENDIAN
89d94b21
DB
163 /* RGB24 (= R, G, B) -> BGR32 (= A, R, G, B) */
164 dst[4 * i + 0] = 255;
165 dst[4 * i + 1] = src[3 * i + 0];
166 dst[4 * i + 2] = src[3 * i + 1];
167 dst[4 * i + 3] = src[3 * i + 2];
7d73d1c3 168#else
89d94b21
DB
169 dst[4 * i + 0] = src[3 * i + 2];
170 dst[4 * i + 1] = src[3 * i + 1];
171 dst[4 * i + 2] = src[3 * i + 0];
172 dst[4 * i + 3] = 255;
7d73d1c3 173#endif
6e42e6c4 174 }
ac4d0aea
MN
175}
176
b8e89339 177void rgb16tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 178{
89d94b21
DB
179 uint8_t *d = dst;
180 const uint16_t *s = (const uint16_t *)src;
72b95764
DB
181 const uint16_t *end = s + src_size / 2;
182
dd68318c 183 while (s < end) {
72b95764 184 register uint16_t bgr = *s++;
7d73d1c3
RP
185#if HAVE_BIGENDIAN
186 *d++ = 255;
89d94b21
DB
187 *d++ = (bgr & 0x1F) << 3;
188 *d++ = (bgr & 0x7E0) >> 3;
189 *d++ = (bgr & 0xF800) >> 8;
7d73d1c3 190#else
89d94b21
DB
191 *d++ = (bgr & 0xF800) >> 8;
192 *d++ = (bgr & 0x7E0) >> 3;
193 *d++ = (bgr & 0x1F) << 3;
7d73d1c3
RP
194 *d++ = 255;
195#endif
6e42e6c4 196 }
ac4d0aea
MN
197}
198
0cc1a86d
PM
199void rgb12to15(const uint8_t *src, uint8_t *dst, int src_size)
200{
0cc1a86d 201 uint16_t rgb, r, g, b;
89d94b21
DB
202 uint16_t *d = (uint16_t *)dst;
203 const uint16_t *s = (const uint16_t *)src;
72b95764
DB
204 const uint16_t *end = s + src_size / 2;
205
0cc1a86d 206 while (s < end) {
89d94b21
DB
207 rgb = *s++;
208 r = rgb & 0xF00;
209 g = rgb & 0x0F0;
210 b = rgb & 0x00F;
211 r = (r << 3) | ((r & 0x800) >> 1);
212 g = (g << 2) | ((g & 0x080) >> 2);
213 b = (b << 1) | ( b >> 3);
0cc1a86d
PM
214 *d++ = r | g | b;
215 }
216}
217
b8e89339 218void rgb16to24(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 219{
89d94b21
DB
220 uint8_t *d = dst;
221 const uint16_t *s = (const uint16_t *)src;
72b95764
DB
222 const uint16_t *end = s + src_size / 2;
223
dd68318c 224 while (s < end) {
72b95764 225 register uint16_t bgr = *s++;
89d94b21
DB
226 *d++ = (bgr & 0xF800) >> 8;
227 *d++ = (bgr & 0x7E0) >> 3;
228 *d++ = (bgr & 0x1F) << 3;
6e42e6c4 229 }
ac4d0aea
MN
230}
231
b8e89339 232void rgb16tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 233{
72b95764 234 int i, num_pixels = src_size >> 1;
6e42e6c4 235
89d94b21
DB
236 for (i = 0; i < num_pixels; i++) {
237 unsigned rgb = ((const uint16_t *)src)[i];
238 ((uint16_t *)dst)[i] = (rgb >> 11) | (rgb & 0x7E0) | (rgb << 11);
6e42e6c4 239 }
ac4d0aea
MN
240}
241
b8e89339 242void rgb16tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 243{
72b95764 244 int i, num_pixels = src_size >> 1;
6e42e6c4 245
89d94b21
DB
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);
6e42e6c4 249 }
ac4d0aea
MN
250}
251
b8e89339 252void rgb15tobgr32(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 253{
89d94b21
DB
254 uint8_t *d = dst;
255 const uint16_t *s = (const uint16_t *)src;
72b95764
DB
256 const uint16_t *end = s + src_size / 2;
257
dd68318c 258 while (s < end) {
72b95764 259 register uint16_t bgr = *s++;
7d73d1c3
RP
260#if HAVE_BIGENDIAN
261 *d++ = 255;
89d94b21
DB
262 *d++ = (bgr & 0x1F) << 3;
263 *d++ = (bgr & 0x3E0) >> 2;
264 *d++ = (bgr & 0x7C00) >> 7;
7d73d1c3 265#else
89d94b21
DB
266 *d++ = (bgr & 0x7C00) >> 7;
267 *d++ = (bgr & 0x3E0) >> 2;
268 *d++ = (bgr & 0x1F) << 3;
7d73d1c3
RP
269 *d++ = 255;
270#endif
6e42e6c4 271 }
ac4d0aea
MN
272}
273
b8e89339 274void rgb15to24(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 275{
89d94b21
DB
276 uint8_t *d = dst;
277 const uint16_t *s = (const uint16_t *)src;
72b95764
DB
278 const uint16_t *end = s + src_size / 2;
279
dd68318c 280 while (s < end) {
72b95764 281 register uint16_t bgr = *s++;
89d94b21
DB
282 *d++ = (bgr & 0x7C00) >> 7;
283 *d++ = (bgr & 0x3E0) >> 2;
284 *d++ = (bgr & 0x1F) << 3;
6e42e6c4 285 }
ac4d0aea
MN
286}
287
b8e89339 288void rgb15tobgr16(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 289{
72b95764 290 int i, num_pixels = src_size >> 1;
6e42e6c4 291
89d94b21
DB
292 for (i = 0; i < num_pixels; i++) {
293 unsigned rgb = ((const uint16_t *)src)[i];
294 ((uint16_t *)dst)[i] = ((rgb & 0x7C00) >> 10) | ((rgb & 0x3E0) << 1) | (rgb << 11);
6e42e6c4 295 }
ac4d0aea
MN
296}
297
b8e89339 298void rgb15tobgr15(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 299{
72b95764 300 int i, num_pixels = src_size >> 1;
6e42e6c4 301
89d94b21
DB
302 for (i = 0; i < num_pixels; i++) {
303 unsigned rgb = ((const uint16_t *)src)[i];
304 unsigned br = rgb & 0x7C1F;
305 ((uint16_t *)dst)[i] = (br >> 10) | (rgb & 0x3E0) | (br << 10);
6e42e6c4 306 }
ac4d0aea
MN
307}
308
f7f35632
PM
309void rgb12tobgr12(const uint8_t *src, uint8_t *dst, int src_size)
310{
89d94b21
DB
311 uint16_t *d = (uint16_t *)dst;
312 uint16_t *s = (uint16_t *)src;
72b95764 313 int i, num_pixels = src_size >> 1;
f7f35632
PM
314
315 for (i = 0; i < num_pixels; i++) {
316 unsigned rgb = s[i];
89d94b21 317 d[i] = (rgb << 8 | rgb & 0xF0 | rgb >> 8) & 0xFFF;
f7f35632
PM
318 }
319}
320
b8e89339 321void bgr8torgb8(const uint8_t *src, uint8_t *dst, int src_size)
ac4d0aea 322{
72b95764
DB
323 int i, num_pixels = src_size;
324
89d94b21 325 for (i = 0; i < num_pixels; i++) {
72b95764
DB
326 register uint8_t rgb = src[i];
327 unsigned r = (rgb & 0x07);
328 unsigned g = (rgb & 0x38) >> 3;
329 unsigned b = (rgb & 0xC0) >> 6;
89d94b21 330 dst[i] = ((b << 1) & 0x07) | ((g & 0x07) << 3) | ((r & 0x03) << 6);
6e42e6c4 331 }
ac4d0aea 332}
82e5f86b
SS
333
334#define DEFINE_SHUFFLE_BYTES(a, b, c, d) \
89d94b21
DB
335void shuffle_bytes_ ## a ## b ## c ## d(const uint8_t *src, \
336 uint8_t *dst, int src_size) \
82e5f86b 337{ \
89d94b21 338 int i; \
82e5f86b 339 \
89d94b21 340 for (i = 0; i < src_size; i += 4) { \
82e5f86b
SS
341 dst[i + 0] = src[i + a]; \
342 dst[i + 1] = src[i + b]; \
343 dst[i + 2] = src[i + c]; \
344 dst[i + 3] = src[i + d]; \
345 } \
346}
347
373211d8
MR
348DEFINE_SHUFFLE_BYTES(0, 3, 2, 1)
349DEFINE_SHUFFLE_BYTES(1, 2, 3, 0)
350DEFINE_SHUFFLE_BYTES(3, 0, 1, 2)
351DEFINE_SHUFFLE_BYTES(3, 2, 1, 0)