configure: Do not check for the __builtin_vec_vsx_ld
[libav.git] / libswscale / yuv2rgb.c
CommitLineData
e76709d8
KS
1/*
2 * software YUV to RGB converter
3 *
4 * Copyright (C) 2009 Konstantin Shishkov
5 *
e76709d8
KS
6 * 1,4,8bpp support and context / deglobalize stuff
7 * by Michael Niedermayer (michaelni@gmx.at)
8 *
2912e87a 9 * This file is part of Libav.
e76709d8 10 *
2912e87a 11 * Libav is free software; you can redistribute it and/or
e76709d8
KS
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 *
2912e87a 16 * Libav is distributed in the hope that it will be useful,
e76709d8
KS
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
2912e87a 22 * License along with Libav; if not, write to the Free Software
e76709d8
KS
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <inttypes.h>
29#include <assert.h>
30
89d94b21
DB
31#include "libavutil/cpu.h"
32#include "libavutil/bswap.h"
e76709d8
KS
33#include "config.h"
34#include "rgb2rgb.h"
35#include "swscale.h"
36#include "swscale_internal.h"
37
e9175634
VG
38/* Color space conversion coefficients for YCbCr -> RGB mapping.
39 *
40 * Entries are {crv, cbu, cgu, cgv}
41 *
42 * crv = (255 / 224) * 65536 * (1 - cr) / 0.5
43 * cbu = (255 / 224) * 65536 * (1 - cb) / 0.5
44 * cgu = (255 / 224) * 65536 * (cb / cg) * (1 - cb) / 0.5
45 * cgv = (255 / 224) * 65536 * (cr / cg) * (1 - cr) / 0.5
46 *
47 * where Y = cr * R + cg * G + cb * B and cr + cg + cb = 1.
48 */
e76709d8 49const int32_t ff_yuv2rgb_coeffs[8][4] = {
89d94b21
DB
50 { 117504, 138453, 13954, 34903 }, /* no sequence_display_extension */
51 { 117504, 138453, 13954, 34903 }, /* ITU-R Rec. 709 (1990) */
52 { 104597, 132201, 25675, 53279 }, /* unspecified */
53 { 104597, 132201, 25675, 53279 }, /* reserved */
54 { 104448, 132798, 24759, 53109 }, /* FCC */
55 { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
56 { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
57 { 117579, 136230, 16907, 35559 } /* SMPTE 240M (1987) */
e76709d8
KS
58};
59
8ebed38e 60const int *sws_getCoefficients(int colorspace)
3f3dc76c
DC
61{
62 if (colorspace > 7 || colorspace < 0)
63 colorspace = SWS_CS_DEFAULT;
64 return ff_yuv2rgb_coeffs[colorspace];
65}
66
e76709d8
KS
67#define LOADCHROMA(i) \
68 U = pu[i]; \
69 V = pv[i]; \
70 r = (void *)c->table_rV[V]; \
71 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
72 b = (void *)c->table_bU[U];
73
89d94b21
DB
74#define PUTRGB(dst, src, i) \
75 Y = src[2 * i]; \
76 dst[2 * i] = r[Y] + g[Y] + b[Y]; \
77 Y = src[2 * i + 1]; \
78 dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
79
80#define PUTRGB24(dst, src, i) \
81 Y = src[2 * i]; \
82 dst[6 * i + 0] = r[Y]; \
83 dst[6 * i + 1] = g[Y]; \
84 dst[6 * i + 2] = b[Y]; \
85 Y = src[2 * i + 1]; \
86 dst[6 * i + 3] = r[Y]; \
87 dst[6 * i + 4] = g[Y]; \
88 dst[6 * i + 5] = b[Y];
89
90#define PUTBGR24(dst, src, i) \
91 Y = src[2 * i]; \
92 dst[6 * i + 0] = b[Y]; \
93 dst[6 * i + 1] = g[Y]; \
94 dst[6 * i + 2] = r[Y]; \
95 Y = src[2 * i + 1]; \
96 dst[6 * i + 3] = b[Y]; \
97 dst[6 * i + 4] = g[Y]; \
98 dst[6 * i + 5] = r[Y];
99
100#define PUTRGBA(dst, ysrc, asrc, i, s) \
101 Y = ysrc[2 * i]; \
102 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
103 Y = ysrc[2 * i + 1]; \
104 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
105
106#define PUTRGB48(dst, src, i) \
107 Y = src[ 2 * i]; \
108 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
109 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
110 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
111 Y = src[ 2 * i + 1]; \
112 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
113 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
114 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
115
116#define PUTBGR48(dst, src, i) \
117 Y = src[2 * i]; \
118 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
119 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
120 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
121 Y = src[2 * i + 1]; \
122 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
123 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
124 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
125
126#define YUV2RGBFUNC(func_name, dst_type, alpha) \
127 static int func_name(SwsContext *c, const uint8_t *src[], \
128 int srcStride[], int srcSliceY, int srcSliceH, \
129 uint8_t *dst[], int dstStride[]) \
130 { \
131 int y; \
132 \
716d413c 133 if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
89d94b21
DB
134 srcStride[1] *= 2; \
135 srcStride[2] *= 2; \
136 } \
137 for (y = 0; y < srcSliceH; y += 2) { \
138 dst_type *dst_1 = \
139 (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
140 dst_type *dst_2 = \
141 (dst_type *)(dst[0] + (y + srcSliceY + 1) * dstStride[0]); \
142 dst_type av_unused *r, *g, *b; \
143 const uint8_t *py_1 = src[0] + y * srcStride[0]; \
144 const uint8_t *py_2 = py_1 + srcStride[0]; \
145 const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
146 const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
147 const uint8_t av_unused *pa_1, *pa_2; \
148 unsigned int h_size = c->dstW >> 3; \
149 if (alpha) { \
150 pa_1 = src[3] + y * srcStride[3]; \
151 pa_2 = pa_1 + srcStride[3]; \
152 } \
153 while (h_size--) { \
154 int av_unused U, V, Y; \
155
b64a72e1
RB
156#define ENDYUV2RGBLINE(dst_delta, ss) \
157 pu += 4 >> ss; \
158 pv += 4 >> ss; \
159 py_1 += 8 >> ss; \
160 py_2 += 8 >> ss; \
161 dst_1 += dst_delta >> ss; \
162 dst_2 += dst_delta >> ss; \
89d94b21 163 } \
b64a72e1 164 if (c->dstW & (4 >> ss)) { \
89d94b21
DB
165 int av_unused Y, U, V; \
166
167#define ENDYUV2RGBFUNC() \
168 } \
169 } \
170 return srcSliceH; \
171 }
e76709d8 172
89d94b21 173#define CLOSEYUV2RGBFUNC(dst_delta) \
b64a72e1 174 ENDYUV2RGBLINE(dst_delta, 0) \
e76709d8
KS
175 ENDYUV2RGBFUNC()
176
68e7f482
KS
177YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
178 LOADCHROMA(0);
89d94b21
DB
179 PUTRGB48(dst_1, py_1, 0);
180 PUTRGB48(dst_2, py_2, 0);
68e7f482
KS
181
182 LOADCHROMA(1);
89d94b21
DB
183 PUTRGB48(dst_2, py_2, 1);
184 PUTRGB48(dst_1, py_1, 1);
68e7f482
KS
185
186 LOADCHROMA(2);
89d94b21
DB
187 PUTRGB48(dst_1, py_1, 2);
188 PUTRGB48(dst_2, py_2, 2);
68e7f482
KS
189
190 LOADCHROMA(3);
89d94b21
DB
191 PUTRGB48(dst_2, py_2, 3);
192 PUTRGB48(dst_1, py_1, 3);
b64a72e1 193ENDYUV2RGBLINE(48, 0)
68e7f482 194 LOADCHROMA(0);
89d94b21
DB
195 PUTRGB48(dst_1, py_1, 0);
196 PUTRGB48(dst_2, py_2, 0);
68e7f482
KS
197
198 LOADCHROMA(1);
89d94b21
DB
199 PUTRGB48(dst_2, py_2, 1);
200 PUTRGB48(dst_1, py_1, 1);
b64a72e1
RB
201ENDYUV2RGBLINE(48, 1)
202 LOADCHROMA(0);
203 PUTRGB48(dst_1, py_1, 0);
204 PUTRGB48(dst_2, py_2, 0);
68e7f482
KS
205ENDYUV2RGBFUNC()
206
1afbae10
PR
207YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
208 LOADCHROMA(0);
89d94b21
DB
209 PUTBGR48(dst_1, py_1, 0);
210 PUTBGR48(dst_2, py_2, 0);
1afbae10
PR
211
212 LOADCHROMA(1);
89d94b21
DB
213 PUTBGR48(dst_2, py_2, 1);
214 PUTBGR48(dst_1, py_1, 1);
1afbae10
PR
215
216 LOADCHROMA(2);
89d94b21
DB
217 PUTBGR48(dst_1, py_1, 2);
218 PUTBGR48(dst_2, py_2, 2);
1afbae10
PR
219
220 LOADCHROMA(3);
89d94b21
DB
221 PUTBGR48(dst_2, py_2, 3);
222 PUTBGR48(dst_1, py_1, 3);
b64a72e1 223ENDYUV2RGBLINE(48, 0)
1afbae10 224 LOADCHROMA(0);
89d94b21
DB
225 PUTBGR48(dst_1, py_1, 0);
226 PUTBGR48(dst_2, py_2, 0);
1afbae10
PR
227
228 LOADCHROMA(1);
89d94b21
DB
229 PUTBGR48(dst_2, py_2, 1);
230 PUTBGR48(dst_1, py_1, 1);
b64a72e1
RB
231ENDYUV2RGBLINE(48, 1)
232 LOADCHROMA(0);
233 PUTBGR48(dst_1, py_1, 0);
234 PUTBGR48(dst_2, py_2, 0);
1afbae10
PR
235ENDYUV2RGBFUNC()
236
9a10a076 237YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
e76709d8 238 LOADCHROMA(0);
89d94b21
DB
239 PUTRGB(dst_1, py_1, 0);
240 PUTRGB(dst_2, py_2, 0);
e76709d8
KS
241
242 LOADCHROMA(1);
89d94b21
DB
243 PUTRGB(dst_2, py_2, 1);
244 PUTRGB(dst_1, py_1, 1);
e76709d8
KS
245
246 LOADCHROMA(2);
89d94b21
DB
247 PUTRGB(dst_1, py_1, 2);
248 PUTRGB(dst_2, py_2, 2);
e76709d8
KS
249
250 LOADCHROMA(3);
89d94b21
DB
251 PUTRGB(dst_2, py_2, 3);
252 PUTRGB(dst_1, py_1, 3);
b64a72e1 253ENDYUV2RGBLINE(8, 0)
e76709d8 254 LOADCHROMA(0);
89d94b21
DB
255 PUTRGB(dst_1, py_1, 0);
256 PUTRGB(dst_2, py_2, 0);
e76709d8
KS
257
258 LOADCHROMA(1);
89d94b21
DB
259 PUTRGB(dst_2, py_2, 1);
260 PUTRGB(dst_1, py_1, 1);
b64a72e1
RB
261ENDYUV2RGBLINE(8, 1)
262 LOADCHROMA(0);
263 PUTRGB(dst_1, py_1, 0);
264 PUTRGB(dst_2, py_2, 0);
e76709d8
KS
265ENDYUV2RGBFUNC()
266
3acd545f
CS
267YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
268 LOADCHROMA(0);
89d94b21
DB
269 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
270 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
3acd545f
CS
271
272 LOADCHROMA(1);
6e293d11
MS
273 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
274 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
3acd545f
CS
275
276 LOADCHROMA(2);
89d94b21
DB
277 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
278 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
3acd545f
CS
279
280 LOADCHROMA(3);
6e293d11
MS
281 PUTRGBA(dst_2, py_2, pa_2, 3, 24);
282 PUTRGBA(dst_1, py_1, pa_1, 3, 24);
6c5eac9a
MS
283 pa_1 += 8;
284 pa_2 += 8;
b64a72e1 285ENDYUV2RGBLINE(8, 0)
3acd545f 286 LOADCHROMA(0);
89d94b21
DB
287 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
288 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
3acd545f
CS
289
290 LOADCHROMA(1);
6e293d11
MS
291 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
292 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
6c5eac9a
MS
293 pa_1 += 4;
294 pa_2 += 4;
b64a72e1
RB
295ENDYUV2RGBLINE(8, 1)
296 LOADCHROMA(0);
297 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
298 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
3acd545f
CS
299ENDYUV2RGBFUNC()
300
301YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
302 LOADCHROMA(0);
89d94b21
DB
303 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
304 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
3acd545f
CS
305
306 LOADCHROMA(1);
89d94b21
DB
307 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
308 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
3acd545f
CS
309
310 LOADCHROMA(2);
89d94b21
DB
311 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
312 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
3acd545f
CS
313
314 LOADCHROMA(3);
89d94b21
DB
315 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
316 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
6c5eac9a
MS
317 pa_1 += 8;
318 pa_2 += 8;
b64a72e1 319ENDYUV2RGBLINE(8, 0)
3acd545f 320 LOADCHROMA(0);
89d94b21
DB
321 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
322 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
3acd545f
CS
323
324 LOADCHROMA(1);
89d94b21
DB
325 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
326 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
6c5eac9a
MS
327 pa_1 += 4;
328 pa_2 += 4;
b64a72e1
RB
329ENDYUV2RGBLINE(8, 1)
330 LOADCHROMA(0);
331 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
332 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
3acd545f
CS
333ENDYUV2RGBFUNC()
334
9a10a076 335YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
e76709d8 336 LOADCHROMA(0);
89d94b21
DB
337 PUTRGB24(dst_1, py_1, 0);
338 PUTRGB24(dst_2, py_2, 0);
e76709d8
KS
339
340 LOADCHROMA(1);
89d94b21
DB
341 PUTRGB24(dst_2, py_2, 1);
342 PUTRGB24(dst_1, py_1, 1);
e76709d8
KS
343
344 LOADCHROMA(2);
89d94b21
DB
345 PUTRGB24(dst_1, py_1, 2);
346 PUTRGB24(dst_2, py_2, 2);
e76709d8
KS
347
348 LOADCHROMA(3);
89d94b21
DB
349 PUTRGB24(dst_2, py_2, 3);
350 PUTRGB24(dst_1, py_1, 3);
b64a72e1 351ENDYUV2RGBLINE(24, 0)
e76709d8 352 LOADCHROMA(0);
89d94b21
DB
353 PUTRGB24(dst_1, py_1, 0);
354 PUTRGB24(dst_2, py_2, 0);
e76709d8
KS
355
356 LOADCHROMA(1);
89d94b21
DB
357 PUTRGB24(dst_2, py_2, 1);
358 PUTRGB24(dst_1, py_1, 1);
b64a72e1
RB
359ENDYUV2RGBLINE(24, 1)
360 LOADCHROMA(0);
361 PUTRGB24(dst_1, py_1, 0);
362 PUTRGB24(dst_2, py_2, 0);
e76709d8
KS
363ENDYUV2RGBFUNC()
364
365// only trivial mods from yuv2rgb_c_24_rgb
9a10a076 366YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
e76709d8 367 LOADCHROMA(0);
89d94b21
DB
368 PUTBGR24(dst_1, py_1, 0);
369 PUTBGR24(dst_2, py_2, 0);
e76709d8
KS
370
371 LOADCHROMA(1);
89d94b21
DB
372 PUTBGR24(dst_2, py_2, 1);
373 PUTBGR24(dst_1, py_1, 1);
e76709d8
KS
374
375 LOADCHROMA(2);
89d94b21
DB
376 PUTBGR24(dst_1, py_1, 2);
377 PUTBGR24(dst_2, py_2, 2);
e76709d8
KS
378
379 LOADCHROMA(3);
89d94b21
DB
380 PUTBGR24(dst_2, py_2, 3);
381 PUTBGR24(dst_1, py_1, 3);
b64a72e1 382ENDYUV2RGBLINE(24, 0)
e76709d8 383 LOADCHROMA(0);
89d94b21
DB
384 PUTBGR24(dst_1, py_1, 0);
385 PUTBGR24(dst_2, py_2, 0);
e76709d8
KS
386
387 LOADCHROMA(1);
89d94b21
DB
388 PUTBGR24(dst_2, py_2, 1);
389 PUTBGR24(dst_1, py_1, 1);
b64a72e1
RB
390ENDYUV2RGBLINE(24, 1)
391 LOADCHROMA(0);
392 PUTBGR24(dst_1, py_1, 0);
393 PUTBGR24(dst_2, py_2, 0);
e76709d8
KS
394ENDYUV2RGBFUNC()
395
396// This is exactly the same code as yuv2rgb_c_32 except for the types of
397// r, g, b, dst_1, dst_2
9a10a076 398YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
e76709d8 399 LOADCHROMA(0);
89d94b21
DB
400 PUTRGB(dst_1, py_1, 0);
401 PUTRGB(dst_2, py_2, 0);
e76709d8
KS
402
403 LOADCHROMA(1);
89d94b21
DB
404 PUTRGB(dst_2, py_2, 1);
405 PUTRGB(dst_1, py_1, 1);
e76709d8
KS
406
407 LOADCHROMA(2);
89d94b21
DB
408 PUTRGB(dst_1, py_1, 2);
409 PUTRGB(dst_2, py_2, 2);
e76709d8
KS
410
411 LOADCHROMA(3);
89d94b21
DB
412 PUTRGB(dst_2, py_2, 3);
413 PUTRGB(dst_1, py_1, 3);
e76709d8
KS
414CLOSEYUV2RGBFUNC(8)
415
e76709d8 416// r, g, b, dst_1, dst_2
601949b8 417YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
aa2ba8c9 418 const uint8_t *d16 = ff_dither_4x4_16[y & 3];
89d94b21
DB
419
420#define PUTRGB12(dst, src, i, o) \
421 Y = src[2 * i]; \
422 dst[2 * i] = r[Y + d16[0 + o]] + \
423 g[Y + d16[0 + o]] + \
424 b[Y + d16[0 + o]]; \
425 Y = src[2 * i + 1]; \
426 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
427 g[Y + d16[1 + o]] + \
428 b[Y + d16[1 + o]];
601949b8
JK
429
430 LOADCHROMA(0);
89d94b21
DB
431 PUTRGB12(dst_1, py_1, 0, 0);
432 PUTRGB12(dst_2, py_2, 0, 0 + 8);
601949b8
JK
433
434 LOADCHROMA(1);
89d94b21
DB
435 PUTRGB12(dst_2, py_2, 1, 2 + 8);
436 PUTRGB12(dst_1, py_1, 1, 2);
601949b8
JK
437
438 LOADCHROMA(2);
89d94b21
DB
439 PUTRGB12(dst_1, py_1, 2, 4);
440 PUTRGB12(dst_2, py_2, 2, 4 + 8);
601949b8
JK
441
442 LOADCHROMA(3);
89d94b21
DB
443 PUTRGB12(dst_2, py_2, 3, 6 + 8);
444 PUTRGB12(dst_1, py_1, 3, 6);
601949b8
JK
445CLOSEYUV2RGBFUNC(8)
446
447// r, g, b, dst_1, dst_2
9a10a076 448YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
aa2ba8c9
DB
449 const uint8_t *d32 = ff_dither_8x8_32[y & 7];
450 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
89d94b21
DB
451
452#define PUTRGB8(dst, src, i, o) \
453 Y = src[2 * i]; \
454 dst[2 * i] = r[Y + d32[0 + o]] + \
455 g[Y + d32[0 + o]] + \
456 b[Y + d64[0 + o]]; \
457 Y = src[2 * i + 1]; \
458 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
459 g[Y + d32[1 + o]] + \
460 b[Y + d64[1 + o]];
e76709d8
KS
461
462 LOADCHROMA(0);
89d94b21
DB
463 PUTRGB8(dst_1, py_1, 0, 0);
464 PUTRGB8(dst_2, py_2, 0, 0 + 8);
e76709d8
KS
465
466 LOADCHROMA(1);
89d94b21
DB
467 PUTRGB8(dst_2, py_2, 1, 2 + 8);
468 PUTRGB8(dst_1, py_1, 1, 2);
e76709d8
KS
469
470 LOADCHROMA(2);
89d94b21
DB
471 PUTRGB8(dst_1, py_1, 2, 4);
472 PUTRGB8(dst_2, py_2, 2, 4 + 8);
e76709d8
KS
473
474 LOADCHROMA(3);
89d94b21
DB
475 PUTRGB8(dst_2, py_2, 3, 6 + 8);
476 PUTRGB8(dst_1, py_1, 3, 6);
e76709d8
KS
477CLOSEYUV2RGBFUNC(8)
478
9a10a076 479YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
aa2ba8c9
DB
480 const uint8_t * d64 = ff_dither_8x8_73[y & 7];
481 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
e76709d8
KS
482 int acc;
483
89d94b21
DB
484#define PUTRGB4D(dst, src, i, o) \
485 Y = src[2 * i]; \
486 acc = r[Y + d128[0 + o]] + \
487 g[Y + d64[0 + o]] + \
488 b[Y + d128[0 + o]]; \
489 Y = src[2 * i + 1]; \
490 acc |= (r[Y + d128[1 + o]] + \
491 g[Y + d64[1 + o]] + \
492 b[Y + d128[1 + o]]) << 4; \
493 dst[i] = acc;
e76709d8
KS
494
495 LOADCHROMA(0);
89d94b21
DB
496 PUTRGB4D(dst_1, py_1, 0, 0);
497 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
e76709d8
KS
498
499 LOADCHROMA(1);
89d94b21
DB
500 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
501 PUTRGB4D(dst_1, py_1, 1, 2);
e76709d8
KS
502
503 LOADCHROMA(2);
89d94b21
DB
504 PUTRGB4D(dst_1, py_1, 2, 4);
505 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
e76709d8
KS
506
507 LOADCHROMA(3);
89d94b21
DB
508 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
509 PUTRGB4D(dst_1, py_1, 3, 6);
e76709d8
KS
510CLOSEYUV2RGBFUNC(4)
511
9a10a076 512YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
aa2ba8c9
DB
513 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
514 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
89d94b21
DB
515
516#define PUTRGB4DB(dst, src, i, o) \
517 Y = src[2 * i]; \
518 dst[2 * i] = r[Y + d128[0 + o]] + \
519 g[Y + d64[0 + o]] + \
520 b[Y + d128[0 + o]]; \
521 Y = src[2 * i + 1]; \
522 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
523 g[Y + d64[1 + o]] + \
524 b[Y + d128[1 + o]];
e76709d8
KS
525
526 LOADCHROMA(0);
89d94b21
DB
527 PUTRGB4DB(dst_1, py_1, 0, 0);
528 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
e76709d8
KS
529
530 LOADCHROMA(1);
89d94b21
DB
531 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
532 PUTRGB4DB(dst_1, py_1, 1, 2);
e76709d8
KS
533
534 LOADCHROMA(2);
89d94b21
DB
535 PUTRGB4DB(dst_1, py_1, 2, 4);
536 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
e76709d8
KS
537
538 LOADCHROMA(3);
89d94b21
DB
539 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
540 PUTRGB4DB(dst_1, py_1, 3, 6);
e76709d8
KS
541CLOSEYUV2RGBFUNC(8)
542
9a10a076 543YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
aa2ba8c9 544 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
89d94b21
DB
545 char out_1 = 0, out_2 = 0;
546 g = c->table_gU[128] + c->table_gV[128];
e76709d8 547
89d94b21
DB
548#define PUTRGB1(out, src, i, o) \
549 Y = src[2 * i]; \
550 out += out + g[Y + d128[0 + o]]; \
551 Y = src[2 * i + 1]; \
552 out += out + g[Y + d128[1 + o]];
e76709d8 553
89d94b21
DB
554 PUTRGB1(out_1, py_1, 0, 0);
555 PUTRGB1(out_2, py_2, 0, 0 + 8);
e76709d8 556
89d94b21
DB
557 PUTRGB1(out_2, py_2, 1, 2 + 8);
558 PUTRGB1(out_1, py_1, 1, 2);
e76709d8 559
89d94b21
DB
560 PUTRGB1(out_1, py_1, 2, 4);
561 PUTRGB1(out_2, py_2, 2, 4 + 8);
e76709d8 562
89d94b21
DB
563 PUTRGB1(out_2, py_2, 3, 6 + 8);
564 PUTRGB1(out_1, py_1, 3, 6);
e76709d8 565
89d94b21
DB
566 dst_1[0] = out_1;
567 dst_2[0] = out_2;
e76709d8
KS
568CLOSEYUV2RGBFUNC(1)
569
780daf2b 570SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
e76709d8
KS
571{
572 SwsFunc t = NULL;
e76709d8 573
67e6a9f5 574 if (ARCH_PPC)
c2503d9c 575 t = ff_yuv2rgb_init_ppc(c);
67e6a9f5
DB
576 if (ARCH_X86)
577 t = ff_yuv2rgb_init_x86(c);
e76709d8
KS
578
579 if (t)
580 return t;
581
89d94b21
DB
582 av_log(c, AV_LOG_WARNING,
583 "No accelerated colorspace conversion found from %s to %s.\n",
584 sws_format_name(c->srcFormat), sws_format_name(c->dstFormat));
e76709d8
KS
585
586 switch (c->dstFormat) {
716d413c
AK
587 case AV_PIX_FMT_BGR48BE:
588 case AV_PIX_FMT_BGR48LE:
89d94b21 589 return yuv2rgb_c_bgr48;
716d413c
AK
590 case AV_PIX_FMT_RGB48BE:
591 case AV_PIX_FMT_RGB48LE:
89d94b21 592 return yuv2rgb_c_48;
716d413c
AK
593 case AV_PIX_FMT_ARGB:
594 case AV_PIX_FMT_ABGR:
595 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
89d94b21 596 return yuva2argb_c;
716d413c
AK
597 case AV_PIX_FMT_RGBA:
598 case AV_PIX_FMT_BGRA:
599 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
89d94b21
DB
600 return yuva2rgba_c;
601 else
602 return yuv2rgb_c_32;
716d413c 603 case AV_PIX_FMT_RGB24:
89d94b21 604 return yuv2rgb_c_24_rgb;
716d413c 605 case AV_PIX_FMT_BGR24:
89d94b21 606 return yuv2rgb_c_24_bgr;
716d413c
AK
607 case AV_PIX_FMT_RGB565:
608 case AV_PIX_FMT_BGR565:
609 case AV_PIX_FMT_RGB555:
610 case AV_PIX_FMT_BGR555:
89d94b21 611 return yuv2rgb_c_16;
716d413c
AK
612 case AV_PIX_FMT_RGB444:
613 case AV_PIX_FMT_BGR444:
89d94b21 614 return yuv2rgb_c_12_ordered_dither;
716d413c
AK
615 case AV_PIX_FMT_RGB8:
616 case AV_PIX_FMT_BGR8:
89d94b21 617 return yuv2rgb_c_8_ordered_dither;
716d413c
AK
618 case AV_PIX_FMT_RGB4:
619 case AV_PIX_FMT_BGR4:
89d94b21 620 return yuv2rgb_c_4_ordered_dither;
716d413c
AK
621 case AV_PIX_FMT_RGB4_BYTE:
622 case AV_PIX_FMT_BGR4_BYTE:
89d94b21 623 return yuv2rgb_c_4b_ordered_dither;
716d413c 624 case AV_PIX_FMT_MONOBLACK:
89d94b21 625 return yuv2rgb_c_1_ordered_dither;
e76709d8
KS
626 default:
627 assert(0);
628 }
629 return NULL;
630}
631
89d94b21
DB
632static void fill_table(uint8_t *table[256], const int elemsize,
633 const int inc, void *y_tab)
e76709d8
KS
634{
635 int i;
89d94b21 636 int64_t cb = 0;
453d28af 637 uint8_t *y_table = y_tab;
e76709d8
KS
638
639 y_table -= elemsize * (inc >> 9);
640
641 for (i = 0; i < 256; i++) {
642 table[i] = y_table + elemsize * (cb >> 16);
89d94b21 643 cb += inc;
e76709d8
KS
644 }
645}
646
647static void fill_gv_table(int table[256], const int elemsize, const int inc)
648{
649 int i;
650 int64_t cb = 0;
89d94b21 651 int off = -(inc >> 9);
e76709d8
KS
652
653 for (i = 0; i < 256; i++) {
654 table[i] = elemsize * (off + (cb >> 16));
89d94b21 655 cb += inc;
e76709d8
KS
656 }
657}
658
0093ac5b
SS
659static uint16_t roundToInt16(int64_t f)
660{
89d94b21
DB
661 int r = (f + (1 << 15)) >> 16;
662
663 if (r < -0x7FFF)
664 return 0x8000;
665 else if (r > 0x7FFF)
666 return 0x7FFF;
667 else
668 return r;
0093ac5b
SS
669}
670
89d94b21
DB
671av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
672 int fullRange, int brightness,
673 int contrast, int saturation)
e76709d8 674{
716d413c
AK
675 const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
676 c->dstFormat == AV_PIX_FMT_RGB32_1 ||
677 c->dstFormat == AV_PIX_FMT_BGR24 ||
678 c->dstFormat == AV_PIX_FMT_RGB565BE ||
679 c->dstFormat == AV_PIX_FMT_RGB565LE ||
680 c->dstFormat == AV_PIX_FMT_RGB555BE ||
681 c->dstFormat == AV_PIX_FMT_RGB555LE ||
682 c->dstFormat == AV_PIX_FMT_RGB444BE ||
683 c->dstFormat == AV_PIX_FMT_RGB444LE ||
684 c->dstFormat == AV_PIX_FMT_RGB8 ||
685 c->dstFormat == AV_PIX_FMT_RGB4 ||
686 c->dstFormat == AV_PIX_FMT_RGB4_BYTE ||
687 c->dstFormat == AV_PIX_FMT_MONOBLACK;
688 const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
689 c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
690 c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
691 c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
692 c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
693 c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE);
30b61475 694 const int bpp = c->dstFormatBpp;
e76709d8
KS
695 uint8_t *y_table;
696 uint16_t *y_table16;
697 uint32_t *y_table32;
3acd545f 698 int i, base, rbase, gbase, bbase, abase, needAlpha;
e76709d8
KS
699 const int yoffs = fullRange ? 384 : 326;
700
701 int64_t crv = inv_table[0];
702 int64_t cbu = inv_table[1];
703 int64_t cgu = -inv_table[2];
704 int64_t cgv = -inv_table[3];
89d94b21 705 int64_t cy = 1 << 16;
e76709d8 706 int64_t oy = 0;
89d94b21 707 int64_t yb = 0;
e76709d8
KS
708
709 if (!fullRange) {
89d94b21
DB
710 cy = (cy * 255) / 219;
711 oy = 16 << 16;
e76709d8 712 } else {
89d94b21
DB
713 crv = (crv * 224) / 255;
714 cbu = (cbu * 224) / 255;
715 cgu = (cgu * 224) / 255;
716 cgv = (cgv * 224) / 255;
e76709d8
KS
717 }
718
89d94b21
DB
719 cy = (cy * contrast) >> 16;
720 crv = (crv * contrast * saturation) >> 32;
721 cbu = (cbu * contrast * saturation) >> 32;
722 cgu = (cgu * contrast * saturation) >> 32;
723 cgv = (cgv * contrast * saturation) >> 32;
724 oy -= 256 * brightness;
725
726 c->uOffset = 0x0400040004000400LL;
727 c->vOffset = 0x0400040004000400LL;
728 c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
729 c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
730 c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
731 c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
732 c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
733 c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
734
735 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
736 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
737 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
738 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
739 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
740 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
0093ac5b 741
e76709d8
KS
742 //scale coefficients by cy
743 crv = ((crv << 16) + 0x8000) / cy;
744 cbu = ((cbu << 16) + 0x8000) / cy;
745 cgu = ((cgu << 16) + 0x8000) / cy;
746 cgv = ((cgv << 16) + 0x8000) / cy;
747
748 av_free(c->yuvTable);
749
1dd797e3
VG
750#define ALLOC_YUV_TABLE(x) \
751 c->yuvTable = av_malloc(x); \
752 if (!c->yuvTable) \
753 return AVERROR(ENOMEM);
e76709d8
KS
754 switch (bpp) {
755 case 1:
1dd797e3 756 ALLOC_YUV_TABLE(1024);
89d94b21
DB
757 y_table = c->yuvTable;
758 yb = -(384 << 16) - oy;
759 for (i = 0; i < 1024 - 110; i++) {
760 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
761 yb += cy;
e76709d8
KS
762 }
763 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
764 fill_gv_table(c->table_gV, 1, cgv);
765 break;
766 case 4:
89d94b21
DB
767 case 4 | 128:
768 rbase = isRgb ? 3 : 0;
769 gbase = 1;
770 bbase = isRgb ? 0 : 3;
1dd797e3 771 ALLOC_YUV_TABLE(1024 * 3);
89d94b21
DB
772 y_table = c->yuvTable;
773 yb = -(384 << 16) - oy;
774 for (i = 0; i < 1024 - 110; i++) {
775 int yval = av_clip_uint8((yb + 0x8000) >> 16);
776 y_table[i + 110] = (yval >> 7) << rbase;
777 y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
778 y_table[i + 110 + 2048] = (yval >> 7) << bbase;
e76709d8
KS
779 yb += cy;
780 }
781 fill_table(c->table_rV, 1, crv, y_table + yoffs);
782 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
783 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
784 fill_gv_table(c->table_gV, 1, cgv);
785 break;
786 case 8:
89d94b21
DB
787 rbase = isRgb ? 5 : 0;
788 gbase = isRgb ? 2 : 3;
789 bbase = isRgb ? 0 : 6;
1dd797e3 790 ALLOC_YUV_TABLE(1024 * 3);
89d94b21
DB
791 y_table = c->yuvTable;
792 yb = -(384 << 16) - oy;
793 for (i = 0; i < 1024 - 38; i++) {
794 int yval = av_clip_uint8((yb + 0x8000) >> 16);
795 y_table[i + 16] = ((yval + 18) / 36) << rbase;
796 y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
797 y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
e76709d8
KS
798 yb += cy;
799 }
800 fill_table(c->table_rV, 1, crv, y_table + yoffs);
801 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
802 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
803 fill_gv_table(c->table_gV, 1, cgv);
804 break;
601949b8 805 case 12:
89d94b21
DB
806 rbase = isRgb ? 8 : 0;
807 gbase = 4;
808 bbase = isRgb ? 0 : 8;
1dd797e3 809 ALLOC_YUV_TABLE(1024 * 3 * 2);
89d94b21
DB
810 y_table16 = c->yuvTable;
811 yb = -(384 << 16) - oy;
601949b8 812 for (i = 0; i < 1024; i++) {
89d94b21
DB
813 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
814 y_table16[i] = (yval >> 4) << rbase;
815 y_table16[i + 1024] = (yval >> 4) << gbase;
816 y_table16[i + 2048] = (yval >> 4) << bbase;
601949b8
JK
817 yb += cy;
818 }
a4e44130 819 if (isNotNe)
89d94b21 820 for (i = 0; i < 1024 * 3; i++)
6a01eab7 821 y_table16[i] = av_bswap16(y_table16[i]);
601949b8
JK
822 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
823 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
824 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
825 fill_gv_table(c->table_gV, 2, cgv);
826 break;
e76709d8
KS
827 case 15:
828 case 16:
89d94b21
DB
829 rbase = isRgb ? bpp - 5 : 0;
830 gbase = 5;
831 bbase = isRgb ? 0 : (bpp - 5);
1dd797e3 832 ALLOC_YUV_TABLE(1024 * 3 * 2);
89d94b21
DB
833 y_table16 = c->yuvTable;
834 yb = -(384 << 16) - oy;
e76709d8 835 for (i = 0; i < 1024; i++) {
89d94b21
DB
836 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
837 y_table16[i] = (yval >> 3) << rbase;
838 y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
839 y_table16[i + 2048] = (yval >> 3) << bbase;
e76709d8
KS
840 yb += cy;
841 }
89d94b21
DB
842 if (isNotNe)
843 for (i = 0; i < 1024 * 3; i++)
6a01eab7 844 y_table16[i] = av_bswap16(y_table16[i]);
e76709d8
KS
845 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
846 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
847 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
848 fill_gv_table(c->table_gV, 2, cgv);
849 break;
850 case 24:
68e7f482 851 case 48:
1dd797e3 852 ALLOC_YUV_TABLE(1024);
89d94b21
DB
853 y_table = c->yuvTable;
854 yb = -(384 << 16) - oy;
e76709d8 855 for (i = 0; i < 1024; i++) {
89d94b21
DB
856 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
857 yb += cy;
e76709d8
KS
858 }
859 fill_table(c->table_rV, 1, crv, y_table + yoffs);
860 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
861 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
862 fill_gv_table(c->table_gV, 1, cgv);
863 break;
864 case 32:
716d413c
AK
865 base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
866 c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
89d94b21
DB
867 rbase = base + (isRgb ? 16 : 0);
868 gbase = base + 8;
869 bbase = base + (isRgb ? 0 : 16);
3acd545f
CS
870 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
871 if (!needAlpha)
872 abase = (base + 24) & 31;
1dd797e3 873 ALLOC_YUV_TABLE(1024 * 3 * 4);
89d94b21
DB
874 y_table32 = c->yuvTable;
875 yb = -(384 << 16) - oy;
e76709d8 876 for (i = 0; i < 1024; i++) {
89d94b21
DB
877 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
878 y_table32[i] = (yval << rbase) +
879 (needAlpha ? 0 : (255u << abase));
880 y_table32[i + 1024] = yval << gbase;
881 y_table32[i + 2048] = yval << bbase;
e76709d8
KS
882 yb += cy;
883 }
884 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
885 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
886 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
887 fill_gv_table(c->table_gV, 4, cgv);
888 break;
889 default:
890 c->yuvTable = NULL;
813784a8
MN
891 if(!isPlanar(c->dstFormat) || bpp <= 24)
892 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
e76709d8
KS
893 return -1;
894 }
895 return 0;
896}