yuv2rgb: Document the color space coefficients
[libav.git] / libswscale / yuv2rgb.c
1 /*
2 * software YUV to RGB converter
3 *
4 * Copyright (C) 2009 Konstantin Shishkov
5 *
6 * 1,4,8bpp support and context / deglobalize stuff
7 * by Michael Niedermayer (michaelni@gmx.at)
8 *
9 * This file is part of Libav.
10 *
11 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
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
31 #include "libavutil/cpu.h"
32 #include "libavutil/bswap.h"
33 #include "config.h"
34 #include "rgb2rgb.h"
35 #include "swscale.h"
36 #include "swscale_internal.h"
37
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 */
49 const int32_t ff_yuv2rgb_coeffs[8][4] = {
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) */
58 };
59
60 const int *sws_getCoefficients(int colorspace)
61 {
62 if (colorspace > 7 || colorspace < 0)
63 colorspace = SWS_CS_DEFAULT;
64 return ff_yuv2rgb_coeffs[colorspace];
65 }
66
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
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 \
133 if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
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
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; \
163 } \
164 if (c->dstW & (4 >> ss)) { \
165 int av_unused Y, U, V; \
166
167 #define ENDYUV2RGBFUNC() \
168 } \
169 } \
170 return srcSliceH; \
171 }
172
173 #define CLOSEYUV2RGBFUNC(dst_delta) \
174 ENDYUV2RGBLINE(dst_delta, 0) \
175 ENDYUV2RGBFUNC()
176
177 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
178 LOADCHROMA(0);
179 PUTRGB48(dst_1, py_1, 0);
180 PUTRGB48(dst_2, py_2, 0);
181
182 LOADCHROMA(1);
183 PUTRGB48(dst_2, py_2, 1);
184 PUTRGB48(dst_1, py_1, 1);
185
186 LOADCHROMA(2);
187 PUTRGB48(dst_1, py_1, 2);
188 PUTRGB48(dst_2, py_2, 2);
189
190 LOADCHROMA(3);
191 PUTRGB48(dst_2, py_2, 3);
192 PUTRGB48(dst_1, py_1, 3);
193 ENDYUV2RGBLINE(48, 0)
194 LOADCHROMA(0);
195 PUTRGB48(dst_1, py_1, 0);
196 PUTRGB48(dst_2, py_2, 0);
197
198 LOADCHROMA(1);
199 PUTRGB48(dst_2, py_2, 1);
200 PUTRGB48(dst_1, py_1, 1);
201 ENDYUV2RGBLINE(48, 1)
202 LOADCHROMA(0);
203 PUTRGB48(dst_1, py_1, 0);
204 PUTRGB48(dst_2, py_2, 0);
205 ENDYUV2RGBFUNC()
206
207 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
208 LOADCHROMA(0);
209 PUTBGR48(dst_1, py_1, 0);
210 PUTBGR48(dst_2, py_2, 0);
211
212 LOADCHROMA(1);
213 PUTBGR48(dst_2, py_2, 1);
214 PUTBGR48(dst_1, py_1, 1);
215
216 LOADCHROMA(2);
217 PUTBGR48(dst_1, py_1, 2);
218 PUTBGR48(dst_2, py_2, 2);
219
220 LOADCHROMA(3);
221 PUTBGR48(dst_2, py_2, 3);
222 PUTBGR48(dst_1, py_1, 3);
223 ENDYUV2RGBLINE(48, 0)
224 LOADCHROMA(0);
225 PUTBGR48(dst_1, py_1, 0);
226 PUTBGR48(dst_2, py_2, 0);
227
228 LOADCHROMA(1);
229 PUTBGR48(dst_2, py_2, 1);
230 PUTBGR48(dst_1, py_1, 1);
231 ENDYUV2RGBLINE(48, 1)
232 LOADCHROMA(0);
233 PUTBGR48(dst_1, py_1, 0);
234 PUTBGR48(dst_2, py_2, 0);
235 ENDYUV2RGBFUNC()
236
237 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
238 LOADCHROMA(0);
239 PUTRGB(dst_1, py_1, 0);
240 PUTRGB(dst_2, py_2, 0);
241
242 LOADCHROMA(1);
243 PUTRGB(dst_2, py_2, 1);
244 PUTRGB(dst_1, py_1, 1);
245
246 LOADCHROMA(2);
247 PUTRGB(dst_1, py_1, 2);
248 PUTRGB(dst_2, py_2, 2);
249
250 LOADCHROMA(3);
251 PUTRGB(dst_2, py_2, 3);
252 PUTRGB(dst_1, py_1, 3);
253 ENDYUV2RGBLINE(8, 0)
254 LOADCHROMA(0);
255 PUTRGB(dst_1, py_1, 0);
256 PUTRGB(dst_2, py_2, 0);
257
258 LOADCHROMA(1);
259 PUTRGB(dst_2, py_2, 1);
260 PUTRGB(dst_1, py_1, 1);
261 ENDYUV2RGBLINE(8, 1)
262 LOADCHROMA(0);
263 PUTRGB(dst_1, py_1, 0);
264 PUTRGB(dst_2, py_2, 0);
265 ENDYUV2RGBFUNC()
266
267 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
268 LOADCHROMA(0);
269 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
270 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
271
272 LOADCHROMA(1);
273 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
274 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
275
276 LOADCHROMA(2);
277 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
278 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
279
280 LOADCHROMA(3);
281 PUTRGBA(dst_2, py_2, pa_2, 3, 24);
282 PUTRGBA(dst_1, py_1, pa_1, 3, 24);
283 pa_1 += 8;
284 pa_2 += 8;
285 ENDYUV2RGBLINE(8, 0)
286 LOADCHROMA(0);
287 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
288 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
289
290 LOADCHROMA(1);
291 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
292 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
293 pa_1 += 4;
294 pa_2 += 4;
295 ENDYUV2RGBLINE(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);
299 ENDYUV2RGBFUNC()
300
301 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
302 LOADCHROMA(0);
303 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
304 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
305
306 LOADCHROMA(1);
307 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
308 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
309
310 LOADCHROMA(2);
311 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
312 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
313
314 LOADCHROMA(3);
315 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
316 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
317 pa_1 += 8;
318 pa_2 += 8;
319 ENDYUV2RGBLINE(8, 0)
320 LOADCHROMA(0);
321 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
322 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
323
324 LOADCHROMA(1);
325 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
326 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
327 pa_1 += 4;
328 pa_2 += 4;
329 ENDYUV2RGBLINE(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);
333 ENDYUV2RGBFUNC()
334
335 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
336 LOADCHROMA(0);
337 PUTRGB24(dst_1, py_1, 0);
338 PUTRGB24(dst_2, py_2, 0);
339
340 LOADCHROMA(1);
341 PUTRGB24(dst_2, py_2, 1);
342 PUTRGB24(dst_1, py_1, 1);
343
344 LOADCHROMA(2);
345 PUTRGB24(dst_1, py_1, 2);
346 PUTRGB24(dst_2, py_2, 2);
347
348 LOADCHROMA(3);
349 PUTRGB24(dst_2, py_2, 3);
350 PUTRGB24(dst_1, py_1, 3);
351 ENDYUV2RGBLINE(24, 0)
352 LOADCHROMA(0);
353 PUTRGB24(dst_1, py_1, 0);
354 PUTRGB24(dst_2, py_2, 0);
355
356 LOADCHROMA(1);
357 PUTRGB24(dst_2, py_2, 1);
358 PUTRGB24(dst_1, py_1, 1);
359 ENDYUV2RGBLINE(24, 1)
360 LOADCHROMA(0);
361 PUTRGB24(dst_1, py_1, 0);
362 PUTRGB24(dst_2, py_2, 0);
363 ENDYUV2RGBFUNC()
364
365 // only trivial mods from yuv2rgb_c_24_rgb
366 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
367 LOADCHROMA(0);
368 PUTBGR24(dst_1, py_1, 0);
369 PUTBGR24(dst_2, py_2, 0);
370
371 LOADCHROMA(1);
372 PUTBGR24(dst_2, py_2, 1);
373 PUTBGR24(dst_1, py_1, 1);
374
375 LOADCHROMA(2);
376 PUTBGR24(dst_1, py_1, 2);
377 PUTBGR24(dst_2, py_2, 2);
378
379 LOADCHROMA(3);
380 PUTBGR24(dst_2, py_2, 3);
381 PUTBGR24(dst_1, py_1, 3);
382 ENDYUV2RGBLINE(24, 0)
383 LOADCHROMA(0);
384 PUTBGR24(dst_1, py_1, 0);
385 PUTBGR24(dst_2, py_2, 0);
386
387 LOADCHROMA(1);
388 PUTBGR24(dst_2, py_2, 1);
389 PUTBGR24(dst_1, py_1, 1);
390 ENDYUV2RGBLINE(24, 1)
391 LOADCHROMA(0);
392 PUTBGR24(dst_1, py_1, 0);
393 PUTBGR24(dst_2, py_2, 0);
394 ENDYUV2RGBFUNC()
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
398 YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
399 LOADCHROMA(0);
400 PUTRGB(dst_1, py_1, 0);
401 PUTRGB(dst_2, py_2, 0);
402
403 LOADCHROMA(1);
404 PUTRGB(dst_2, py_2, 1);
405 PUTRGB(dst_1, py_1, 1);
406
407 LOADCHROMA(2);
408 PUTRGB(dst_1, py_1, 2);
409 PUTRGB(dst_2, py_2, 2);
410
411 LOADCHROMA(3);
412 PUTRGB(dst_2, py_2, 3);
413 PUTRGB(dst_1, py_1, 3);
414 CLOSEYUV2RGBFUNC(8)
415
416 // r, g, b, dst_1, dst_2
417 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
418 const uint8_t *d16 = ff_dither_4x4_16[y & 3];
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]];
429
430 LOADCHROMA(0);
431 PUTRGB12(dst_1, py_1, 0, 0);
432 PUTRGB12(dst_2, py_2, 0, 0 + 8);
433
434 LOADCHROMA(1);
435 PUTRGB12(dst_2, py_2, 1, 2 + 8);
436 PUTRGB12(dst_1, py_1, 1, 2);
437
438 LOADCHROMA(2);
439 PUTRGB12(dst_1, py_1, 2, 4);
440 PUTRGB12(dst_2, py_2, 2, 4 + 8);
441
442 LOADCHROMA(3);
443 PUTRGB12(dst_2, py_2, 3, 6 + 8);
444 PUTRGB12(dst_1, py_1, 3, 6);
445 CLOSEYUV2RGBFUNC(8)
446
447 // r, g, b, dst_1, dst_2
448 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
449 const uint8_t *d32 = ff_dither_8x8_32[y & 7];
450 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
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]];
461
462 LOADCHROMA(0);
463 PUTRGB8(dst_1, py_1, 0, 0);
464 PUTRGB8(dst_2, py_2, 0, 0 + 8);
465
466 LOADCHROMA(1);
467 PUTRGB8(dst_2, py_2, 1, 2 + 8);
468 PUTRGB8(dst_1, py_1, 1, 2);
469
470 LOADCHROMA(2);
471 PUTRGB8(dst_1, py_1, 2, 4);
472 PUTRGB8(dst_2, py_2, 2, 4 + 8);
473
474 LOADCHROMA(3);
475 PUTRGB8(dst_2, py_2, 3, 6 + 8);
476 PUTRGB8(dst_1, py_1, 3, 6);
477 CLOSEYUV2RGBFUNC(8)
478
479 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
480 const uint8_t * d64 = ff_dither_8x8_73[y & 7];
481 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
482 int acc;
483
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;
494
495 LOADCHROMA(0);
496 PUTRGB4D(dst_1, py_1, 0, 0);
497 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
498
499 LOADCHROMA(1);
500 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
501 PUTRGB4D(dst_1, py_1, 1, 2);
502
503 LOADCHROMA(2);
504 PUTRGB4D(dst_1, py_1, 2, 4);
505 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
506
507 LOADCHROMA(3);
508 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
509 PUTRGB4D(dst_1, py_1, 3, 6);
510 CLOSEYUV2RGBFUNC(4)
511
512 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
513 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
514 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
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]];
525
526 LOADCHROMA(0);
527 PUTRGB4DB(dst_1, py_1, 0, 0);
528 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
529
530 LOADCHROMA(1);
531 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
532 PUTRGB4DB(dst_1, py_1, 1, 2);
533
534 LOADCHROMA(2);
535 PUTRGB4DB(dst_1, py_1, 2, 4);
536 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
537
538 LOADCHROMA(3);
539 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
540 PUTRGB4DB(dst_1, py_1, 3, 6);
541 CLOSEYUV2RGBFUNC(8)
542
543 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
544 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
545 char out_1 = 0, out_2 = 0;
546 g = c->table_gU[128] + c->table_gV[128];
547
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]];
553
554 PUTRGB1(out_1, py_1, 0, 0);
555 PUTRGB1(out_2, py_2, 0, 0 + 8);
556
557 PUTRGB1(out_2, py_2, 1, 2 + 8);
558 PUTRGB1(out_1, py_1, 1, 2);
559
560 PUTRGB1(out_1, py_1, 2, 4);
561 PUTRGB1(out_2, py_2, 2, 4 + 8);
562
563 PUTRGB1(out_2, py_2, 3, 6 + 8);
564 PUTRGB1(out_1, py_1, 3, 6);
565
566 dst_1[0] = out_1;
567 dst_2[0] = out_2;
568 CLOSEYUV2RGBFUNC(1)
569
570 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
571 {
572 SwsFunc t = NULL;
573
574 if (ARCH_PPC)
575 t = ff_yuv2rgb_init_ppc(c);
576 if (ARCH_X86)
577 t = ff_yuv2rgb_init_x86(c);
578
579 if (t)
580 return t;
581
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));
585
586 switch (c->dstFormat) {
587 case AV_PIX_FMT_BGR48BE:
588 case AV_PIX_FMT_BGR48LE:
589 return yuv2rgb_c_bgr48;
590 case AV_PIX_FMT_RGB48BE:
591 case AV_PIX_FMT_RGB48LE:
592 return yuv2rgb_c_48;
593 case AV_PIX_FMT_ARGB:
594 case AV_PIX_FMT_ABGR:
595 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
596 return yuva2argb_c;
597 case AV_PIX_FMT_RGBA:
598 case AV_PIX_FMT_BGRA:
599 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
600 return yuva2rgba_c;
601 else
602 return yuv2rgb_c_32;
603 case AV_PIX_FMT_RGB24:
604 return yuv2rgb_c_24_rgb;
605 case AV_PIX_FMT_BGR24:
606 return yuv2rgb_c_24_bgr;
607 case AV_PIX_FMT_RGB565:
608 case AV_PIX_FMT_BGR565:
609 case AV_PIX_FMT_RGB555:
610 case AV_PIX_FMT_BGR555:
611 return yuv2rgb_c_16;
612 case AV_PIX_FMT_RGB444:
613 case AV_PIX_FMT_BGR444:
614 return yuv2rgb_c_12_ordered_dither;
615 case AV_PIX_FMT_RGB8:
616 case AV_PIX_FMT_BGR8:
617 return yuv2rgb_c_8_ordered_dither;
618 case AV_PIX_FMT_RGB4:
619 case AV_PIX_FMT_BGR4:
620 return yuv2rgb_c_4_ordered_dither;
621 case AV_PIX_FMT_RGB4_BYTE:
622 case AV_PIX_FMT_BGR4_BYTE:
623 return yuv2rgb_c_4b_ordered_dither;
624 case AV_PIX_FMT_MONOBLACK:
625 return yuv2rgb_c_1_ordered_dither;
626 default:
627 assert(0);
628 }
629 return NULL;
630 }
631
632 static void fill_table(uint8_t *table[256], const int elemsize,
633 const int inc, void *y_tab)
634 {
635 int i;
636 int64_t cb = 0;
637 uint8_t *y_table = y_tab;
638
639 y_table -= elemsize * (inc >> 9);
640
641 for (i = 0; i < 256; i++) {
642 table[i] = y_table + elemsize * (cb >> 16);
643 cb += inc;
644 }
645 }
646
647 static void fill_gv_table(int table[256], const int elemsize, const int inc)
648 {
649 int i;
650 int64_t cb = 0;
651 int off = -(inc >> 9);
652
653 for (i = 0; i < 256; i++) {
654 table[i] = elemsize * (off + (cb >> 16));
655 cb += inc;
656 }
657 }
658
659 static uint16_t roundToInt16(int64_t f)
660 {
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;
669 }
670
671 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
672 int fullRange, int brightness,
673 int contrast, int saturation)
674 {
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);
694 const int bpp = c->dstFormatBpp;
695 uint8_t *y_table;
696 uint16_t *y_table16;
697 uint32_t *y_table32;
698 int i, base, rbase, gbase, bbase, abase, needAlpha;
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];
705 int64_t cy = 1 << 16;
706 int64_t oy = 0;
707 int64_t yb = 0;
708
709 if (!fullRange) {
710 cy = (cy * 255) / 219;
711 oy = 16 << 16;
712 } else {
713 crv = (crv * 224) / 255;
714 cbu = (cbu * 224) / 255;
715 cgu = (cgu * 224) / 255;
716 cgv = (cgv * 224) / 255;
717 }
718
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);
741
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
750 #define ALLOC_YUV_TABLE(x) \
751 c->yuvTable = av_malloc(x); \
752 if (!c->yuvTable) \
753 return AVERROR(ENOMEM);
754 switch (bpp) {
755 case 1:
756 ALLOC_YUV_TABLE(1024);
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;
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:
767 case 4 | 128:
768 rbase = isRgb ? 3 : 0;
769 gbase = 1;
770 bbase = isRgb ? 0 : 3;
771 ALLOC_YUV_TABLE(1024 * 3);
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;
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:
787 rbase = isRgb ? 5 : 0;
788 gbase = isRgb ? 2 : 3;
789 bbase = isRgb ? 0 : 6;
790 ALLOC_YUV_TABLE(1024 * 3);
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;
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;
805 case 12:
806 rbase = isRgb ? 8 : 0;
807 gbase = 4;
808 bbase = isRgb ? 0 : 8;
809 ALLOC_YUV_TABLE(1024 * 3 * 2);
810 y_table16 = c->yuvTable;
811 yb = -(384 << 16) - oy;
812 for (i = 0; i < 1024; i++) {
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;
817 yb += cy;
818 }
819 if (isNotNe)
820 for (i = 0; i < 1024 * 3; i++)
821 y_table16[i] = av_bswap16(y_table16[i]);
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;
827 case 15:
828 case 16:
829 rbase = isRgb ? bpp - 5 : 0;
830 gbase = 5;
831 bbase = isRgb ? 0 : (bpp - 5);
832 ALLOC_YUV_TABLE(1024 * 3 * 2);
833 y_table16 = c->yuvTable;
834 yb = -(384 << 16) - oy;
835 for (i = 0; i < 1024; i++) {
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;
840 yb += cy;
841 }
842 if (isNotNe)
843 for (i = 0; i < 1024 * 3; i++)
844 y_table16[i] = av_bswap16(y_table16[i]);
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:
851 case 48:
852 ALLOC_YUV_TABLE(1024);
853 y_table = c->yuvTable;
854 yb = -(384 << 16) - oy;
855 for (i = 0; i < 1024; i++) {
856 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
857 yb += cy;
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:
865 base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
866 c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
867 rbase = base + (isRgb ? 16 : 0);
868 gbase = base + 8;
869 bbase = base + (isRgb ? 0 : 16);
870 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
871 if (!needAlpha)
872 abase = (base + 24) & 31;
873 ALLOC_YUV_TABLE(1024 * 3 * 4);
874 y_table32 = c->yuvTable;
875 yb = -(384 << 16) - oy;
876 for (i = 0; i < 1024; i++) {
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;
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;
891 if(!isPlanar(c->dstFormat) || bpp <= 24)
892 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
893 return -1;
894 }
895 return 0;
896 }