Imported Debian version 2.5.2~trusty
[deb_ffmpeg.git] / ffmpeg / libswscale / yuv2rgb.c
CommitLineData
2ba45a60
DM
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 FFmpeg.
10 *
11 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
30#include "libavutil/cpu.h"
31#include "libavutil/bswap.h"
32#include "config.h"
33#include "rgb2rgb.h"
34#include "swscale.h"
35#include "swscale_internal.h"
36#include "libavutil/pixdesc.h"
37
38const 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
49const 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+YUVRGB_TABLE_HEADROOM]; \
60 g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
61 b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
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
166YUV2RGBFUNC(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);
182ENDYUV2RGBLINE(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);
190ENDYUV2RGBLINE(48, 1)
191 LOADCHROMA(0);
192 PUTRGB48(dst_1, py_1, 0);
193 PUTRGB48(dst_2, py_2, 0);
194ENDYUV2RGBFUNC()
195
196YUV2RGBFUNC(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);
212ENDYUV2RGBLINE(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);
220ENDYUV2RGBLINE(48, 1)
221 LOADCHROMA(0);
222 PUTBGR48(dst_1, py_1, 0);
223 PUTBGR48(dst_2, py_2, 0);
224ENDYUV2RGBFUNC()
225
226YUV2RGBFUNC(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);
242ENDYUV2RGBLINE(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);
250ENDYUV2RGBLINE(8, 1)
251 LOADCHROMA(0);
252 PUTRGB(dst_1, py_1, 0);
253 PUTRGB(dst_2, py_2, 0);
254ENDYUV2RGBFUNC()
255
256YUV2RGBFUNC(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;
274ENDYUV2RGBLINE(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;
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);
288ENDYUV2RGBFUNC()
289
290YUV2RGBFUNC(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;
308ENDYUV2RGBLINE(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;
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);
322ENDYUV2RGBFUNC()
323
324YUV2RGBFUNC(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);
340ENDYUV2RGBLINE(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);
348ENDYUV2RGBLINE(24, 1)
349 LOADCHROMA(0);
350 PUTRGB24(dst_1, py_1, 0);
351 PUTRGB24(dst_2, py_2, 0);
352ENDYUV2RGBFUNC()
353
354// only trivial mods from yuv2rgb_c_24_rgb
355YUV2RGBFUNC(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);
371ENDYUV2RGBLINE(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);
379ENDYUV2RGBLINE(24, 1)
380 LOADCHROMA(0);
381 PUTBGR24(dst_1, py_1, 0);
382 PUTBGR24(dst_2, py_2, 0);
383ENDYUV2RGBFUNC()
384
385YUV2RGBFUNC(yuv2rgb_c_16_ordered_dither, uint16_t, 0)
386 const uint8_t *d16 = ff_dither_2x2_8[y & 1];
387 const uint8_t *e16 = ff_dither_2x2_4[y & 1];
388 const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
389
390#define PUTRGB16(dst, src, i, o) \
391 Y = src[2 * i]; \
392 dst[2 * i] = r[Y + d16[0 + o]] + \
393 g[Y + e16[0 + o]] + \
394 b[Y + f16[0 + o]]; \
395 Y = src[2 * i + 1]; \
396 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
397 g[Y + e16[1 + o]] + \
398 b[Y + f16[1 + o]];
399 LOADCHROMA(0);
400 PUTRGB16(dst_1, py_1, 0, 0);
401 PUTRGB16(dst_2, py_2, 0, 0 + 8);
402
403 LOADCHROMA(1);
404 PUTRGB16(dst_2, py_2, 1, 2 + 8);
405 PUTRGB16(dst_1, py_1, 1, 2);
406
407 LOADCHROMA(2);
408 PUTRGB16(dst_1, py_1, 2, 4);
409 PUTRGB16(dst_2, py_2, 2, 4 + 8);
410
411 LOADCHROMA(3);
412 PUTRGB16(dst_2, py_2, 3, 6 + 8);
413 PUTRGB16(dst_1, py_1, 3, 6);
414CLOSEYUV2RGBFUNC(8)
415
416YUV2RGBFUNC(yuv2rgb_c_15_ordered_dither, uint16_t, 0)
417 const uint8_t *d16 = ff_dither_2x2_8[y & 1];
418 const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
419
420#define PUTRGB15(dst, src, i, o) \
421 Y = src[2 * i]; \
422 dst[2 * i] = r[Y + d16[0 + o]] + \
423 g[Y + d16[1 + o]] + \
424 b[Y + e16[0 + o]]; \
425 Y = src[2 * i + 1]; \
426 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
427 g[Y + d16[0 + o]] + \
428 b[Y + e16[1 + o]];
429 LOADCHROMA(0);
430 PUTRGB15(dst_1, py_1, 0, 0);
431 PUTRGB15(dst_2, py_2, 0, 0 + 8);
432
433 LOADCHROMA(1);
434 PUTRGB15(dst_2, py_2, 1, 2 + 8);
435 PUTRGB15(dst_1, py_1, 1, 2);
436
437 LOADCHROMA(2);
438 PUTRGB15(dst_1, py_1, 2, 4);
439 PUTRGB15(dst_2, py_2, 2, 4 + 8);
440
441 LOADCHROMA(3);
442 PUTRGB15(dst_2, py_2, 3, 6 + 8);
443 PUTRGB15(dst_1, py_1, 3, 6);
444CLOSEYUV2RGBFUNC(8)
445
446// r, g, b, dst_1, dst_2
447YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
448 const uint8_t *d16 = ff_dither_4x4_16[y & 3];
449
450#define PUTRGB12(dst, src, i, o) \
451 Y = src[2 * i]; \
452 dst[2 * i] = r[Y + d16[0 + o]] + \
453 g[Y + d16[0 + o]] + \
454 b[Y + d16[0 + o]]; \
455 Y = src[2 * i + 1]; \
456 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
457 g[Y + d16[1 + o]] + \
458 b[Y + d16[1 + o]];
459
460 LOADCHROMA(0);
461 PUTRGB12(dst_1, py_1, 0, 0);
462 PUTRGB12(dst_2, py_2, 0, 0 + 8);
463
464 LOADCHROMA(1);
465 PUTRGB12(dst_2, py_2, 1, 2 + 8);
466 PUTRGB12(dst_1, py_1, 1, 2);
467
468 LOADCHROMA(2);
469 PUTRGB12(dst_1, py_1, 2, 4);
470 PUTRGB12(dst_2, py_2, 2, 4 + 8);
471
472 LOADCHROMA(3);
473 PUTRGB12(dst_2, py_2, 3, 6 + 8);
474 PUTRGB12(dst_1, py_1, 3, 6);
475CLOSEYUV2RGBFUNC(8)
476
477// r, g, b, dst_1, dst_2
478YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
479 const uint8_t *d32 = ff_dither_8x8_32[y & 7];
480 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
481
482#define PUTRGB8(dst, src, i, o) \
483 Y = src[2 * i]; \
484 dst[2 * i] = r[Y + d32[0 + o]] + \
485 g[Y + d32[0 + o]] + \
486 b[Y + d64[0 + o]]; \
487 Y = src[2 * i + 1]; \
488 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
489 g[Y + d32[1 + o]] + \
490 b[Y + d64[1 + o]];
491
492 LOADCHROMA(0);
493 PUTRGB8(dst_1, py_1, 0, 0);
494 PUTRGB8(dst_2, py_2, 0, 0 + 8);
495
496 LOADCHROMA(1);
497 PUTRGB8(dst_2, py_2, 1, 2 + 8);
498 PUTRGB8(dst_1, py_1, 1, 2);
499
500 LOADCHROMA(2);
501 PUTRGB8(dst_1, py_1, 2, 4);
502 PUTRGB8(dst_2, py_2, 2, 4 + 8);
503
504 LOADCHROMA(3);
505 PUTRGB8(dst_2, py_2, 3, 6 + 8);
506 PUTRGB8(dst_1, py_1, 3, 6);
507CLOSEYUV2RGBFUNC(8)
508
509YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
510 const uint8_t * d64 = ff_dither_8x8_73[y & 7];
511 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
512 int acc;
513
514#define PUTRGB4D(dst, src, i, o) \
515 Y = src[2 * i]; \
516 acc = r[Y + d128[0 + o]] + \
517 g[Y + d64[0 + o]] + \
518 b[Y + d128[0 + o]]; \
519 Y = src[2 * i + 1]; \
520 acc |= (r[Y + d128[1 + o]] + \
521 g[Y + d64[1 + o]] + \
522 b[Y + d128[1 + o]]) << 4; \
523 dst[i] = acc;
524
525 LOADCHROMA(0);
526 PUTRGB4D(dst_1, py_1, 0, 0);
527 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
528
529 LOADCHROMA(1);
530 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
531 PUTRGB4D(dst_1, py_1, 1, 2);
532
533 LOADCHROMA(2);
534 PUTRGB4D(dst_1, py_1, 2, 4);
535 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
536
537 LOADCHROMA(3);
538 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
539 PUTRGB4D(dst_1, py_1, 3, 6);
540CLOSEYUV2RGBFUNC(4)
541
542YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
543 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
544 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
545
546#define PUTRGB4DB(dst, src, i, o) \
547 Y = src[2 * i]; \
548 dst[2 * i] = r[Y + d128[0 + o]] + \
549 g[Y + d64[0 + o]] + \
550 b[Y + d128[0 + o]]; \
551 Y = src[2 * i + 1]; \
552 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
553 g[Y + d64[1 + o]] + \
554 b[Y + d128[1 + o]];
555
556 LOADCHROMA(0);
557 PUTRGB4DB(dst_1, py_1, 0, 0);
558 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
559
560 LOADCHROMA(1);
561 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
562 PUTRGB4DB(dst_1, py_1, 1, 2);
563
564 LOADCHROMA(2);
565 PUTRGB4DB(dst_1, py_1, 2, 4);
566 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
567
568 LOADCHROMA(3);
569 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
570 PUTRGB4DB(dst_1, py_1, 3, 6);
571CLOSEYUV2RGBFUNC(8)
572
573YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
574 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
575 char out_1 = 0, out_2 = 0;
576 g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
577
578#define PUTRGB1(out, src, i, o) \
579 Y = src[2 * i]; \
580 out += out + g[Y + d128[0 + o]]; \
581 Y = src[2 * i + 1]; \
582 out += out + g[Y + d128[1 + o]];
583
584 PUTRGB1(out_1, py_1, 0, 0);
585 PUTRGB1(out_2, py_2, 0, 0 + 8);
586
587 PUTRGB1(out_2, py_2, 1, 2 + 8);
588 PUTRGB1(out_1, py_1, 1, 2);
589
590 PUTRGB1(out_1, py_1, 2, 4);
591 PUTRGB1(out_2, py_2, 2, 4 + 8);
592
593 PUTRGB1(out_2, py_2, 3, 6 + 8);
594 PUTRGB1(out_1, py_1, 3, 6);
595
596 dst_1[0] = out_1;
597 dst_2[0] = out_2;
598CLOSEYUV2RGBFUNC(1)
599
600SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
601{
602 SwsFunc t = NULL;
603
604 if (ARCH_PPC)
605 t = ff_yuv2rgb_init_ppc(c);
606 if (ARCH_X86)
607 t = ff_yuv2rgb_init_x86(c);
608
609 if (t)
610 return t;
611
612 av_log(c, AV_LOG_WARNING,
613 "No accelerated colorspace conversion found from %s to %s.\n",
614 av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
615
616 switch (c->dstFormat) {
617 case AV_PIX_FMT_BGR48BE:
618 case AV_PIX_FMT_BGR48LE:
619 return yuv2rgb_c_bgr48;
620 case AV_PIX_FMT_RGB48BE:
621 case AV_PIX_FMT_RGB48LE:
622 return yuv2rgb_c_48;
623 case AV_PIX_FMT_ARGB:
624 case AV_PIX_FMT_ABGR:
625 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat))
626 return yuva2argb_c;
627 case AV_PIX_FMT_RGBA:
628 case AV_PIX_FMT_BGRA:
629 return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
630 case AV_PIX_FMT_RGB24:
631 return yuv2rgb_c_24_rgb;
632 case AV_PIX_FMT_BGR24:
633 return yuv2rgb_c_24_bgr;
634 case AV_PIX_FMT_RGB565:
635 case AV_PIX_FMT_BGR565:
636 return yuv2rgb_c_16_ordered_dither;
637 case AV_PIX_FMT_RGB555:
638 case AV_PIX_FMT_BGR555:
639 return yuv2rgb_c_15_ordered_dither;
640 case AV_PIX_FMT_RGB444:
641 case AV_PIX_FMT_BGR444:
642 return yuv2rgb_c_12_ordered_dither;
643 case AV_PIX_FMT_RGB8:
644 case AV_PIX_FMT_BGR8:
645 return yuv2rgb_c_8_ordered_dither;
646 case AV_PIX_FMT_RGB4:
647 case AV_PIX_FMT_BGR4:
648 return yuv2rgb_c_4_ordered_dither;
649 case AV_PIX_FMT_RGB4_BYTE:
650 case AV_PIX_FMT_BGR4_BYTE:
651 return yuv2rgb_c_4b_ordered_dither;
652 case AV_PIX_FMT_MONOBLACK:
653 return yuv2rgb_c_1_ordered_dither;
654 }
655 return NULL;
656}
657
658static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
659 const int64_t inc, void *y_tab)
660{
661 int i;
662 uint8_t *y_table = y_tab;
663
664 y_table -= elemsize * (inc >> 9);
665
666 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
667 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
668 table[i] = y_table + elemsize * (cb >> 16);
669 }
670}
671
672static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int64_t inc)
673{
674 int i;
675 int off = -(inc >> 9);
676
677 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
678 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
679 table[i] = elemsize * (off + (cb >> 16));
680 }
681}
682
683static uint16_t roundToInt16(int64_t f)
684{
685 int r = (f + (1 << 15)) >> 16;
686
687 if (r < -0x7FFF)
688 return 0x8000;
689 else if (r > 0x7FFF)
690 return 0x7FFF;
691 else
692 return r;
693}
694
695av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
696 int fullRange, int brightness,
697 int contrast, int saturation)
698{
699 const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
700 c->dstFormat == AV_PIX_FMT_RGB32_1 ||
701 c->dstFormat == AV_PIX_FMT_BGR24 ||
702 c->dstFormat == AV_PIX_FMT_RGB565BE ||
703 c->dstFormat == AV_PIX_FMT_RGB565LE ||
704 c->dstFormat == AV_PIX_FMT_RGB555BE ||
705 c->dstFormat == AV_PIX_FMT_RGB555LE ||
706 c->dstFormat == AV_PIX_FMT_RGB444BE ||
707 c->dstFormat == AV_PIX_FMT_RGB444LE ||
708 c->dstFormat == AV_PIX_FMT_RGB8 ||
709 c->dstFormat == AV_PIX_FMT_RGB4 ||
710 c->dstFormat == AV_PIX_FMT_RGB4_BYTE ||
711 c->dstFormat == AV_PIX_FMT_MONOBLACK;
712 const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
713 c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
714 c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
715 c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
716 c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
717 c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE);
718 const int bpp = c->dstFormatBpp;
719 uint8_t *y_table;
720 uint16_t *y_table16;
721 uint32_t *y_table32;
722 int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
723 const int yoffs = fullRange ? 384 : 326;
724
725 int64_t crv = inv_table[0];
726 int64_t cbu = inv_table[1];
727 int64_t cgu = -inv_table[2];
728 int64_t cgv = -inv_table[3];
729 int64_t cy = 1 << 16;
730 int64_t oy = 0;
731 int64_t yb = 0;
732
733 if (!fullRange) {
734 cy = (cy * 255) / 219;
735 oy = 16 << 16;
736 } else {
737 crv = (crv * 224) / 255;
738 cbu = (cbu * 224) / 255;
739 cgu = (cgu * 224) / 255;
740 cgv = (cgv * 224) / 255;
741 }
742
743 cy = (cy * contrast) >> 16;
744 crv = (crv * contrast * saturation) >> 32;
745 cbu = (cbu * contrast * saturation) >> 32;
746 cgu = (cgu * contrast * saturation) >> 32;
747 cgv = (cgv * contrast * saturation) >> 32;
748 oy -= 256 * brightness;
749
750 c->uOffset = 0x0400040004000400LL;
751 c->vOffset = 0x0400040004000400LL;
752 c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
753 c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
754 c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
755 c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
756 c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
757 c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
758
759 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
760 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
761 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
762 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
763 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
764 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
765
766 //scale coefficients by cy
767 crv = ((crv << 16) + 0x8000) / FFMAX(cy, 1);
768 cbu = ((cbu << 16) + 0x8000) / FFMAX(cy, 1);
769 cgu = ((cgu << 16) + 0x8000) / FFMAX(cy, 1);
770 cgv = ((cgv << 16) + 0x8000) / FFMAX(cy, 1);
771
772 av_freep(&c->yuvTable);
773
774 switch (bpp) {
775 case 1:
776 c->yuvTable = av_malloc(1024);
777 y_table = c->yuvTable;
778 yb = -(384 << 16) - oy;
779 for (i = 0; i < 1024 - 110; i++) {
780 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
781 yb += cy;
782 }
783 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
784 fill_gv_table(c->table_gV, 1, cgv);
785 break;
786 case 4:
787 case 4 | 128:
788 rbase = isRgb ? 3 : 0;
789 gbase = 1;
790 bbase = isRgb ? 0 : 3;
791 c->yuvTable = av_malloc(1024 * 3);
792 y_table = c->yuvTable;
793 yb = -(384 << 16) - oy;
794 for (i = 0; i < 1024 - 110; i++) {
795 int yval = av_clip_uint8((yb + 0x8000) >> 16);
796 y_table[i + 110] = (yval >> 7) << rbase;
797 y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
798 y_table[i + 110 + 2048] = (yval >> 7) << bbase;
799 yb += cy;
800 }
801 fill_table(c->table_rV, 1, crv, y_table + yoffs);
802 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
803 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
804 fill_gv_table(c->table_gV, 1, cgv);
805 break;
806 case 8:
807 rbase = isRgb ? 5 : 0;
808 gbase = isRgb ? 2 : 3;
809 bbase = isRgb ? 0 : 6;
810 c->yuvTable = av_malloc(1024 * 3);
811 y_table = c->yuvTable;
812 yb = -(384 << 16) - oy;
813 for (i = 0; i < 1024 - 38; i++) {
814 int yval = av_clip_uint8((yb + 0x8000) >> 16);
815 y_table[i + 16] = ((yval + 18) / 36) << rbase;
816 y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
817 y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
818 yb += cy;
819 }
820 fill_table(c->table_rV, 1, crv, y_table + yoffs);
821 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
822 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
823 fill_gv_table(c->table_gV, 1, cgv);
824 break;
825 case 12:
826 rbase = isRgb ? 8 : 0;
827 gbase = 4;
828 bbase = isRgb ? 0 : 8;
829 c->yuvTable = av_malloc(1024 * 3 * 2);
830 y_table16 = c->yuvTable;
831 yb = -(384 << 16) - oy;
832 for (i = 0; i < 1024; i++) {
833 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
834 y_table16[i] = (yval >> 4) << rbase;
835 y_table16[i + 1024] = (yval >> 4) << gbase;
836 y_table16[i + 2048] = (yval >> 4) << bbase;
837 yb += cy;
838 }
839 if (isNotNe)
840 for (i = 0; i < 1024 * 3; i++)
841 y_table16[i] = av_bswap16(y_table16[i]);
842 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
843 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
844 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
845 fill_gv_table(c->table_gV, 2, cgv);
846 break;
847 case 15:
848 case 16:
849 rbase = isRgb ? bpp - 5 : 0;
850 gbase = 5;
851 bbase = isRgb ? 0 : (bpp - 5);
852 c->yuvTable = av_malloc(1024 * 3 * 2);
853 y_table16 = c->yuvTable;
854 yb = -(384 << 16) - oy;
855 for (i = 0; i < 1024; i++) {
856 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
857 y_table16[i] = (yval >> 3) << rbase;
858 y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
859 y_table16[i + 2048] = (yval >> 3) << bbase;
860 yb += cy;
861 }
862 if (isNotNe)
863 for (i = 0; i < 1024 * 3; i++)
864 y_table16[i] = av_bswap16(y_table16[i]);
865 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
866 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
867 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
868 fill_gv_table(c->table_gV, 2, cgv);
869 break;
870 case 24:
871 case 48:
872 c->yuvTable = av_malloc(1024);
873 y_table = c->yuvTable;
874 yb = -(384 << 16) - oy;
875 for (i = 0; i < 1024; i++) {
876 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
877 yb += cy;
878 }
879 fill_table(c->table_rV, 1, crv, y_table + yoffs);
880 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
881 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
882 fill_gv_table(c->table_gV, 1, cgv);
883 break;
884 case 32:
885 case 64:
886 base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
887 c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
888 rbase = base + (isRgb ? 16 : 0);
889 gbase = base + 8;
890 bbase = base + (isRgb ? 0 : 16);
891 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
892 if (!needAlpha)
893 abase = (base + 24) & 31;
894 c->yuvTable = av_malloc(1024 * 3 * 4);
895 y_table32 = c->yuvTable;
896 yb = -(384 << 16) - oy;
897 for (i = 0; i < 1024; i++) {
898 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
899 y_table32[i] = (yval << rbase) +
900 (needAlpha ? 0 : (255u << abase));
901 y_table32[i + 1024] = yval << gbase;
902 y_table32[i + 2048] = yval << bbase;
903 yb += cy;
904 }
905 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
906 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
907 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
908 fill_gv_table(c->table_gV, 4, cgv);
909 break;
910 default:
911 if(!isPlanar(c->dstFormat) || bpp <= 24)
912 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
913 return -1;
914 }
915 return 0;
916}