84e3e9c50e0b48528a8429531af9c99818b70c09
[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 const int32_t ff_yuv2rgb_coeffs[8][4] = {
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) */
47 };
48
49 const int *sws_getCoefficients(int colorspace)
50 {
51 if (colorspace > 7 || colorspace < 0)
52 colorspace = SWS_CS_DEFAULT;
53 return ff_yuv2rgb_coeffs[colorspace];
54 }
55
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
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 \
122 if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
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
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; \
152 } \
153 if (c->dstW & (4 >> ss)) { \
154 int av_unused Y, U, V; \
155
156 #define ENDYUV2RGBFUNC() \
157 } \
158 } \
159 return srcSliceH; \
160 }
161
162 #define CLOSEYUV2RGBFUNC(dst_delta) \
163 ENDYUV2RGBLINE(dst_delta, 0) \
164 ENDYUV2RGBFUNC()
165
166 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
167 LOADCHROMA(0);
168 PUTRGB48(dst_1, py_1, 0);
169 PUTRGB48(dst_2, py_2, 0);
170
171 LOADCHROMA(1);
172 PUTRGB48(dst_2, py_2, 1);
173 PUTRGB48(dst_1, py_1, 1);
174
175 LOADCHROMA(2);
176 PUTRGB48(dst_1, py_1, 2);
177 PUTRGB48(dst_2, py_2, 2);
178
179 LOADCHROMA(3);
180 PUTRGB48(dst_2, py_2, 3);
181 PUTRGB48(dst_1, py_1, 3);
182 ENDYUV2RGBLINE(48, 0)
183 LOADCHROMA(0);
184 PUTRGB48(dst_1, py_1, 0);
185 PUTRGB48(dst_2, py_2, 0);
186
187 LOADCHROMA(1);
188 PUTRGB48(dst_2, py_2, 1);
189 PUTRGB48(dst_1, py_1, 1);
190 ENDYUV2RGBLINE(48, 1)
191 LOADCHROMA(0);
192 PUTRGB48(dst_1, py_1, 0);
193 PUTRGB48(dst_2, py_2, 0);
194 ENDYUV2RGBFUNC()
195
196 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
197 LOADCHROMA(0);
198 PUTBGR48(dst_1, py_1, 0);
199 PUTBGR48(dst_2, py_2, 0);
200
201 LOADCHROMA(1);
202 PUTBGR48(dst_2, py_2, 1);
203 PUTBGR48(dst_1, py_1, 1);
204
205 LOADCHROMA(2);
206 PUTBGR48(dst_1, py_1, 2);
207 PUTBGR48(dst_2, py_2, 2);
208
209 LOADCHROMA(3);
210 PUTBGR48(dst_2, py_2, 3);
211 PUTBGR48(dst_1, py_1, 3);
212 ENDYUV2RGBLINE(48, 0)
213 LOADCHROMA(0);
214 PUTBGR48(dst_1, py_1, 0);
215 PUTBGR48(dst_2, py_2, 0);
216
217 LOADCHROMA(1);
218 PUTBGR48(dst_2, py_2, 1);
219 PUTBGR48(dst_1, py_1, 1);
220 ENDYUV2RGBLINE(48, 1)
221 LOADCHROMA(0);
222 PUTBGR48(dst_1, py_1, 0);
223 PUTBGR48(dst_2, py_2, 0);
224 ENDYUV2RGBFUNC()
225
226 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
227 LOADCHROMA(0);
228 PUTRGB(dst_1, py_1, 0);
229 PUTRGB(dst_2, py_2, 0);
230
231 LOADCHROMA(1);
232 PUTRGB(dst_2, py_2, 1);
233 PUTRGB(dst_1, py_1, 1);
234
235 LOADCHROMA(2);
236 PUTRGB(dst_1, py_1, 2);
237 PUTRGB(dst_2, py_2, 2);
238
239 LOADCHROMA(3);
240 PUTRGB(dst_2, py_2, 3);
241 PUTRGB(dst_1, py_1, 3);
242 ENDYUV2RGBLINE(8, 0)
243 LOADCHROMA(0);
244 PUTRGB(dst_1, py_1, 0);
245 PUTRGB(dst_2, py_2, 0);
246
247 LOADCHROMA(1);
248 PUTRGB(dst_2, py_2, 1);
249 PUTRGB(dst_1, py_1, 1);
250 ENDYUV2RGBLINE(8, 1)
251 LOADCHROMA(0);
252 PUTRGB(dst_1, py_1, 0);
253 PUTRGB(dst_2, py_2, 0);
254 ENDYUV2RGBFUNC()
255
256 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
257 LOADCHROMA(0);
258 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
259 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
260
261 LOADCHROMA(1);
262 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
263 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
264
265 LOADCHROMA(2);
266 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
267 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
268
269 LOADCHROMA(3);
270 PUTRGBA(dst_2, py_2, pa_2, 3, 24);
271 PUTRGBA(dst_1, py_1, pa_1, 3, 24);
272 pa_1 += 8;
273 pa_2 += 8;
274 ENDYUV2RGBLINE(8, 0)
275 LOADCHROMA(0);
276 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
277 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
278
279 LOADCHROMA(1);
280 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
281 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
282 pa_1 += 4;
283 pa_2 += 4;
284 ENDYUV2RGBLINE(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);
288 ENDYUV2RGBFUNC()
289
290 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
291 LOADCHROMA(0);
292 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
293 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
294
295 LOADCHROMA(1);
296 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
297 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
298
299 LOADCHROMA(2);
300 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
301 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
302
303 LOADCHROMA(3);
304 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
305 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
306 pa_1 += 8;
307 pa_2 += 8;
308 ENDYUV2RGBLINE(8, 0)
309 LOADCHROMA(0);
310 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
311 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
312
313 LOADCHROMA(1);
314 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
315 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
316 pa_1 += 4;
317 pa_2 += 4;
318 ENDYUV2RGBLINE(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);
322 ENDYUV2RGBFUNC()
323
324 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
325 LOADCHROMA(0);
326 PUTRGB24(dst_1, py_1, 0);
327 PUTRGB24(dst_2, py_2, 0);
328
329 LOADCHROMA(1);
330 PUTRGB24(dst_2, py_2, 1);
331 PUTRGB24(dst_1, py_1, 1);
332
333 LOADCHROMA(2);
334 PUTRGB24(dst_1, py_1, 2);
335 PUTRGB24(dst_2, py_2, 2);
336
337 LOADCHROMA(3);
338 PUTRGB24(dst_2, py_2, 3);
339 PUTRGB24(dst_1, py_1, 3);
340 ENDYUV2RGBLINE(24, 0)
341 LOADCHROMA(0);
342 PUTRGB24(dst_1, py_1, 0);
343 PUTRGB24(dst_2, py_2, 0);
344
345 LOADCHROMA(1);
346 PUTRGB24(dst_2, py_2, 1);
347 PUTRGB24(dst_1, py_1, 1);
348 ENDYUV2RGBLINE(24, 1)
349 LOADCHROMA(0);
350 PUTRGB24(dst_1, py_1, 0);
351 PUTRGB24(dst_2, py_2, 0);
352 ENDYUV2RGBFUNC()
353
354 // only trivial mods from yuv2rgb_c_24_rgb
355 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
356 LOADCHROMA(0);
357 PUTBGR24(dst_1, py_1, 0);
358 PUTBGR24(dst_2, py_2, 0);
359
360 LOADCHROMA(1);
361 PUTBGR24(dst_2, py_2, 1);
362 PUTBGR24(dst_1, py_1, 1);
363
364 LOADCHROMA(2);
365 PUTBGR24(dst_1, py_1, 2);
366 PUTBGR24(dst_2, py_2, 2);
367
368 LOADCHROMA(3);
369 PUTBGR24(dst_2, py_2, 3);
370 PUTBGR24(dst_1, py_1, 3);
371 ENDYUV2RGBLINE(24, 0)
372 LOADCHROMA(0);
373 PUTBGR24(dst_1, py_1, 0);
374 PUTBGR24(dst_2, py_2, 0);
375
376 LOADCHROMA(1);
377 PUTBGR24(dst_2, py_2, 1);
378 PUTBGR24(dst_1, py_1, 1);
379 ENDYUV2RGBLINE(24, 1)
380 LOADCHROMA(0);
381 PUTBGR24(dst_1, py_1, 0);
382 PUTBGR24(dst_2, py_2, 0);
383 ENDYUV2RGBFUNC()
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
387 YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
388 LOADCHROMA(0);
389 PUTRGB(dst_1, py_1, 0);
390 PUTRGB(dst_2, py_2, 0);
391
392 LOADCHROMA(1);
393 PUTRGB(dst_2, py_2, 1);
394 PUTRGB(dst_1, py_1, 1);
395
396 LOADCHROMA(2);
397 PUTRGB(dst_1, py_1, 2);
398 PUTRGB(dst_2, py_2, 2);
399
400 LOADCHROMA(3);
401 PUTRGB(dst_2, py_2, 3);
402 PUTRGB(dst_1, py_1, 3);
403 CLOSEYUV2RGBFUNC(8)
404
405 // r, g, b, dst_1, dst_2
406 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
407 const uint8_t *d16 = ff_dither_4x4_16[y & 3];
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]];
418
419 LOADCHROMA(0);
420 PUTRGB12(dst_1, py_1, 0, 0);
421 PUTRGB12(dst_2, py_2, 0, 0 + 8);
422
423 LOADCHROMA(1);
424 PUTRGB12(dst_2, py_2, 1, 2 + 8);
425 PUTRGB12(dst_1, py_1, 1, 2);
426
427 LOADCHROMA(2);
428 PUTRGB12(dst_1, py_1, 2, 4);
429 PUTRGB12(dst_2, py_2, 2, 4 + 8);
430
431 LOADCHROMA(3);
432 PUTRGB12(dst_2, py_2, 3, 6 + 8);
433 PUTRGB12(dst_1, py_1, 3, 6);
434 CLOSEYUV2RGBFUNC(8)
435
436 // r, g, b, dst_1, dst_2
437 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
438 const uint8_t *d32 = ff_dither_8x8_32[y & 7];
439 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
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]];
450
451 LOADCHROMA(0);
452 PUTRGB8(dst_1, py_1, 0, 0);
453 PUTRGB8(dst_2, py_2, 0, 0 + 8);
454
455 LOADCHROMA(1);
456 PUTRGB8(dst_2, py_2, 1, 2 + 8);
457 PUTRGB8(dst_1, py_1, 1, 2);
458
459 LOADCHROMA(2);
460 PUTRGB8(dst_1, py_1, 2, 4);
461 PUTRGB8(dst_2, py_2, 2, 4 + 8);
462
463 LOADCHROMA(3);
464 PUTRGB8(dst_2, py_2, 3, 6 + 8);
465 PUTRGB8(dst_1, py_1, 3, 6);
466 CLOSEYUV2RGBFUNC(8)
467
468 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
469 const uint8_t * d64 = ff_dither_8x8_73[y & 7];
470 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
471 int acc;
472
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;
483
484 LOADCHROMA(0);
485 PUTRGB4D(dst_1, py_1, 0, 0);
486 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
487
488 LOADCHROMA(1);
489 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
490 PUTRGB4D(dst_1, py_1, 1, 2);
491
492 LOADCHROMA(2);
493 PUTRGB4D(dst_1, py_1, 2, 4);
494 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
495
496 LOADCHROMA(3);
497 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
498 PUTRGB4D(dst_1, py_1, 3, 6);
499 CLOSEYUV2RGBFUNC(4)
500
501 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
502 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
503 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
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]];
514
515 LOADCHROMA(0);
516 PUTRGB4DB(dst_1, py_1, 0, 0);
517 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
518
519 LOADCHROMA(1);
520 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
521 PUTRGB4DB(dst_1, py_1, 1, 2);
522
523 LOADCHROMA(2);
524 PUTRGB4DB(dst_1, py_1, 2, 4);
525 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
526
527 LOADCHROMA(3);
528 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
529 PUTRGB4DB(dst_1, py_1, 3, 6);
530 CLOSEYUV2RGBFUNC(8)
531
532 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
533 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
534 char out_1 = 0, out_2 = 0;
535 g = c->table_gU[128] + c->table_gV[128];
536
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]];
542
543 PUTRGB1(out_1, py_1, 0, 0);
544 PUTRGB1(out_2, py_2, 0, 0 + 8);
545
546 PUTRGB1(out_2, py_2, 1, 2 + 8);
547 PUTRGB1(out_1, py_1, 1, 2);
548
549 PUTRGB1(out_1, py_1, 2, 4);
550 PUTRGB1(out_2, py_2, 2, 4 + 8);
551
552 PUTRGB1(out_2, py_2, 3, 6 + 8);
553 PUTRGB1(out_1, py_1, 3, 6);
554
555 dst_1[0] = out_1;
556 dst_2[0] = out_2;
557 CLOSEYUV2RGBFUNC(1)
558
559 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
560 {
561 SwsFunc t = NULL;
562
563 if (ARCH_X86)
564 t = ff_yuv2rgb_init_x86(c);
565 else if (HAVE_VIS)
566 t = ff_yuv2rgb_init_vis(c);
567 else if (ARCH_BFIN)
568 t = ff_yuv2rgb_get_func_ptr_bfin(c);
569 else if (ARCH_PPC)
570 t = ff_yuv2rgb_init_ppc(c);
571
572 if (t)
573 return t;
574
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));
578
579 switch (c->dstFormat) {
580 case AV_PIX_FMT_BGR48BE:
581 case AV_PIX_FMT_BGR48LE:
582 return yuv2rgb_c_bgr48;
583 case AV_PIX_FMT_RGB48BE:
584 case AV_PIX_FMT_RGB48LE:
585 return yuv2rgb_c_48;
586 case AV_PIX_FMT_ARGB:
587 case AV_PIX_FMT_ABGR:
588 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
589 return yuva2argb_c;
590 case AV_PIX_FMT_RGBA:
591 case AV_PIX_FMT_BGRA:
592 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
593 return yuva2rgba_c;
594 else
595 return yuv2rgb_c_32;
596 case AV_PIX_FMT_RGB24:
597 return yuv2rgb_c_24_rgb;
598 case AV_PIX_FMT_BGR24:
599 return yuv2rgb_c_24_bgr;
600 case AV_PIX_FMT_RGB565:
601 case AV_PIX_FMT_BGR565:
602 case AV_PIX_FMT_RGB555:
603 case AV_PIX_FMT_BGR555:
604 return yuv2rgb_c_16;
605 case AV_PIX_FMT_RGB444:
606 case AV_PIX_FMT_BGR444:
607 return yuv2rgb_c_12_ordered_dither;
608 case AV_PIX_FMT_RGB8:
609 case AV_PIX_FMT_BGR8:
610 return yuv2rgb_c_8_ordered_dither;
611 case AV_PIX_FMT_RGB4:
612 case AV_PIX_FMT_BGR4:
613 return yuv2rgb_c_4_ordered_dither;
614 case AV_PIX_FMT_RGB4_BYTE:
615 case AV_PIX_FMT_BGR4_BYTE:
616 return yuv2rgb_c_4b_ordered_dither;
617 case AV_PIX_FMT_MONOBLACK:
618 return yuv2rgb_c_1_ordered_dither;
619 default:
620 assert(0);
621 }
622 return NULL;
623 }
624
625 static void fill_table(uint8_t *table[256], const int elemsize,
626 const int inc, void *y_tab)
627 {
628 int i;
629 int64_t cb = 0;
630 uint8_t *y_table = y_tab;
631
632 y_table -= elemsize * (inc >> 9);
633
634 for (i = 0; i < 256; i++) {
635 table[i] = y_table + elemsize * (cb >> 16);
636 cb += inc;
637 }
638 }
639
640 static void fill_gv_table(int table[256], const int elemsize, const int inc)
641 {
642 int i;
643 int64_t cb = 0;
644 int off = -(inc >> 9);
645
646 for (i = 0; i < 256; i++) {
647 table[i] = elemsize * (off + (cb >> 16));
648 cb += inc;
649 }
650 }
651
652 static uint16_t roundToInt16(int64_t f)
653 {
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;
662 }
663
664 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
665 int fullRange, int brightness,
666 int contrast, int saturation)
667 {
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);
687 const int bpp = c->dstFormatBpp;
688 uint8_t *y_table;
689 uint16_t *y_table16;
690 uint32_t *y_table32;
691 int i, base, rbase, gbase, bbase, abase, needAlpha;
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];
698 int64_t cy = 1 << 16;
699 int64_t oy = 0;
700 int64_t yb = 0;
701
702 if (!fullRange) {
703 cy = (cy * 255) / 219;
704 oy = 16 << 16;
705 } else {
706 crv = (crv * 224) / 255;
707 cbu = (cbu * 224) / 255;
708 cgu = (cgu * 224) / 255;
709 cgv = (cgv * 224) / 255;
710 }
711
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);
734
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);
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;
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:
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;
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:
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;
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;
794 case 12:
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;
801 for (i = 0; i < 1024; i++) {
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;
806 yb += cy;
807 }
808 if (isNotNe)
809 for (i = 0; i < 1024 * 3; i++)
810 y_table16[i] = av_bswap16(y_table16[i]);
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;
816 case 15:
817 case 16:
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;
824 for (i = 0; i < 1024; i++) {
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;
829 yb += cy;
830 }
831 if (isNotNe)
832 for (i = 0; i < 1024 * 3; i++)
833 y_table16[i] = av_bswap16(y_table16[i]);
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:
840 case 48:
841 c->yuvTable = av_malloc(1024);
842 y_table = c->yuvTable;
843 yb = -(384 << 16) - oy;
844 for (i = 0; i < 1024; i++) {
845 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
846 yb += cy;
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:
854 base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
855 c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
856 rbase = base + (isRgb ? 16 : 0);
857 gbase = base + 8;
858 bbase = base + (isRgb ? 0 : 16);
859 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
860 if (!needAlpha)
861 abase = (base + 24) & 31;
862 c->yuvTable = av_malloc(1024 * 3 * 4);
863 y_table32 = c->yuvTable;
864 yb = -(384 << 16) - oy;
865 for (i = 0; i < 1024; i++) {
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;
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;
880 if(!isPlanar(c->dstFormat) || bpp <= 24)
881 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
882 return -1;
883 }
884 return 0;
885 }