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