FFmpeg  4.4.9
yuv2rgb.c
Go to the documentation of this file.
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 
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[11][4] = {
50  { 117489, 138438, 13975, 34925 }, /* no sequence_display_extension */
51  { 117489, 138438, 13975, 34925 }, /* 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  { 0 }, /* YCgCo */
59  { 110013, 140363, 12277, 42626 }, /* Bt-2020-NCL */
60  { 110013, 140363, 12277, 42626 }, /* Bt-2020-CL */
61 };
62 
63 const int *sws_getCoefficients(int colorspace)
64 {
65  if (colorspace > 10 || colorspace < 0 || colorspace == 8)
66  colorspace = SWS_CS_DEFAULT;
67  return ff_yuv2rgb_coeffs[colorspace];
68 }
69 
70 #define LOADCHROMA(i) \
71  U = pu[i]; \
72  V = pv[i]; \
73  r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
74  g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
75  b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
76 
77 #define PUTRGB(dst, src, i) \
78  Y = src[2 * i]; \
79  dst[2 * i] = r[Y] + g[Y] + b[Y]; \
80  Y = src[2 * i + 1]; \
81  dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
82 
83 #define PUTRGB_ODD(dst, src, i) \
84  Y = src[2 * i]; \
85  dst[2 * i] = r[Y] + g[Y] + b[Y];
86 
87 #define PUTRGB24(dst, src, i) \
88  Y = src[2 * i]; \
89  dst[6 * i + 0] = r[Y]; \
90  dst[6 * i + 1] = g[Y]; \
91  dst[6 * i + 2] = b[Y]; \
92  Y = src[2 * i + 1]; \
93  dst[6 * i + 3] = r[Y]; \
94  dst[6 * i + 4] = g[Y]; \
95  dst[6 * i + 5] = b[Y];
96 
97 #define PUTRGB24_ODD(dst, src, i) \
98  Y = src[2 * i]; \
99  dst[6 * i + 0] = r[Y]; \
100  dst[6 * i + 1] = g[Y]; \
101  dst[6 * i + 2] = b[Y];
102 
103 #define PUTBGR24(dst, src, i) \
104  Y = src[2 * i]; \
105  dst[6 * i + 0] = b[Y]; \
106  dst[6 * i + 1] = g[Y]; \
107  dst[6 * i + 2] = r[Y]; \
108  Y = src[2 * i + 1]; \
109  dst[6 * i + 3] = b[Y]; \
110  dst[6 * i + 4] = g[Y]; \
111  dst[6 * i + 5] = r[Y];
112 
113 #define PUTBGR24_ODD(dst, src, i) \
114  Y = src[2 * i]; \
115  dst[6 * i + 0] = b[Y]; \
116  dst[6 * i + 1] = g[Y]; \
117  dst[6 * i + 2] = r[Y];
118 
119 #define PUTRGBA(dst, ysrc, asrc, i, s) \
120  Y = ysrc[2 * i]; \
121  dst[2 * i] = r[Y] + g[Y] + b[Y] + ((uint32_t)(asrc[2 * i]) << s); \
122  Y = ysrc[2 * i + 1]; \
123  dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + ((uint32_t)(asrc[2 * i + 1]) << s);
124 
125 #define PUTRGBA_ODD(dst, ysrc, asrc, i, s) \
126  Y = ysrc[2 * i]; \
127  dst[2 * i] = r[Y] + g[Y] + b[Y] + ((uint32_t)(asrc[2 * i]) << s);
128 
129 #define PUTRGB48(dst, src, i) \
130  Y = src[ 2 * i]; \
131  dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
132  dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
133  dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
134  Y = src[ 2 * i + 1]; \
135  dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
136  dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
137  dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
138 
139 #define PUTRGB48_ODD(dst, src, i) \
140  Y = src[ 2 * i]; \
141  dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
142  dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
143  dst[12 * i + 4] = dst[12 * i + 5] = b[Y];
144 
145 #define PUTBGR48(dst, src, i) \
146  Y = src[2 * i]; \
147  dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
148  dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
149  dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
150  Y = src[2 * i + 1]; \
151  dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
152  dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
153  dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
154 
155 #define PUTBGR48_ODD(dst, src, i) \
156  Y = src[2 * i]; \
157  dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
158  dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
159  dst[12 * i + 4] = dst[12 * i + 5] = r[Y];
160 
161 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
162  static int func_name(SwsContext *c, const uint8_t *src[], \
163  int srcStride[], int srcSliceY, int srcSliceH, \
164  uint8_t *dst[], int dstStride[]) \
165  { \
166  int y; \
167  \
168  if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
169  srcStride[1] *= 2; \
170  srcStride[2] *= 2; \
171  } \
172  for (y = 0; y < srcSliceH; y += 2) { \
173  int yd = y + srcSliceY; \
174  dst_type *dst_1 = \
175  (dst_type *)(dst[0] + (yd) * dstStride[0]); \
176  dst_type *dst_2 = \
177  (dst_type *)(dst[0] + (yd + 1) * dstStride[0]); \
178  dst_type av_unused *r, *g, *b; \
179  const uint8_t *py_1 = src[0] + y * srcStride[0]; \
180  const uint8_t *py_2 = py_1 + srcStride[0]; \
181  const uint8_t av_unused *pu = src[1] + (y >> 1) * srcStride[1]; \
182  const uint8_t av_unused *pv = src[2] + (y >> 1) * srcStride[2]; \
183  const uint8_t av_unused *pa_1, *pa_2; \
184  unsigned int h_size = c->dstW >> 3; \
185  if (alpha) { \
186  pa_1 = src[3] + y * srcStride[3]; \
187  pa_2 = pa_1 + srcStride[3]; \
188  } \
189  while (h_size--) { \
190  int av_unused U, V, Y; \
191 
192 #define ENDYUV2RGBLINE(dst_delta, ss) \
193  pu += 4 >> ss; \
194  pv += 4 >> ss; \
195  py_1 += 8 >> ss; \
196  py_2 += 8 >> ss; \
197  dst_1 += dst_delta >> ss; \
198  dst_2 += dst_delta >> ss; \
199  } \
200  if (c->dstW & (4 >> ss)) { \
201  int av_unused Y, U, V; \
202 
203 #define ENDYUV2RGBFUNC() \
204  } \
205  } \
206  return srcSliceH; \
207  }
208 
209 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
213 
217 
221 
225 ENDYUV2RGBLINE(48, 0)
226  LOADCHROMA(0);
227  PUTRGB48(dst_1, py_1, 0);
228  PUTRGB48(dst_2, py_2, 0);
229 
230  LOADCHROMA(1);
231  PUTRGB48(dst_2, py_2, 1);
232  PUTRGB48(dst_1, py_1, 1);
233 ENDYUV2RGBLINE(48, 1)
234  LOADCHROMA(0);
235  PUTRGB48(dst_1, py_1, 0);
236  PUTRGB48(dst_2, py_2, 0);
237 ENDYUV2RGBLINE(48, 2)
238  LOADCHROMA(0);
242 
243 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
244  LOADCHROMA(0);
247 
248  LOADCHROMA(1);
251 
252  LOADCHROMA(2);
255 
256  LOADCHROMA(3);
259 ENDYUV2RGBLINE(48, 0)
260  LOADCHROMA(0);
261  PUTBGR48(dst_1, py_1, 0);
262  PUTBGR48(dst_2, py_2, 0);
263 
264  LOADCHROMA(1);
265  PUTBGR48(dst_2, py_2, 1);
266  PUTBGR48(dst_1, py_1, 1);
267 ENDYUV2RGBLINE(48, 1)
268  LOADCHROMA(0);
269  PUTBGR48(dst_1, py_1, 0);
270  PUTBGR48(dst_2, py_2, 0);
271 ENDYUV2RGBLINE(48, 2)
272  LOADCHROMA(0);
276 
277 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
278  LOADCHROMA(0);
281 
282  LOADCHROMA(1);
285 
286  LOADCHROMA(2);
289 
290  LOADCHROMA(3);
293 ENDYUV2RGBLINE(8, 0)
294  LOADCHROMA(0);
295  PUTRGB(dst_1, py_1, 0);
296  PUTRGB(dst_2, py_2, 0);
297 
298  LOADCHROMA(1);
299  PUTRGB(dst_2, py_2, 1);
300  PUTRGB(dst_1, py_1, 1);
301 ENDYUV2RGBLINE(8, 1)
302  LOADCHROMA(0);
303  PUTRGB(dst_1, py_1, 0);
304  PUTRGB(dst_2, py_2, 0);
305 ENDYUV2RGBLINE(8, 2)
306  LOADCHROMA(0);
310 
311 #if HAVE_BIGENDIAN
312 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
313 #else
314 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
315 #endif
316  LOADCHROMA(0);
317  PUTRGBA(dst_1, py_1, pa_1, 0, 24);
318  PUTRGBA(dst_2, py_2, pa_2, 0, 24);
319 
320  LOADCHROMA(1);
321  PUTRGBA(dst_2, py_2, pa_2, 1, 24);
322  PUTRGBA(dst_1, py_1, pa_1, 1, 24);
323 
324  LOADCHROMA(2);
325  PUTRGBA(dst_1, py_1, pa_1, 2, 24);
326  PUTRGBA(dst_2, py_2, pa_2, 2, 24);
327 
328  LOADCHROMA(3);
329  PUTRGBA(dst_2, py_2, pa_2, 3, 24);
330  PUTRGBA(dst_1, py_1, pa_1, 3, 24);
331  pa_1 += 8;
332  pa_2 += 8;
333 ENDYUV2RGBLINE(8, 0)
334  LOADCHROMA(0);
335  PUTRGBA(dst_1, py_1, pa_1, 0, 24);
336  PUTRGBA(dst_2, py_2, pa_2, 0, 24);
337 
338  LOADCHROMA(1);
339  PUTRGBA(dst_2, py_2, pa_2, 1, 24);
340  PUTRGBA(dst_1, py_1, pa_1, 1, 24);
341  pa_1 += 4;
342  pa_2 += 4;
343 ENDYUV2RGBLINE(8, 1)
344  LOADCHROMA(0);
345  PUTRGBA(dst_1, py_1, pa_1, 0, 24);
346  PUTRGBA(dst_2, py_2, pa_2, 0, 24);
347  pa_1 += 2;
348  pa_2 += 2;
349 ENDYUV2RGBLINE(8, 2)
350  LOADCHROMA(0);
354 
355 #if HAVE_BIGENDIAN
356 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
357 #else
358 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
359 #endif
360  LOADCHROMA(0);
361  PUTRGBA(dst_1, py_1, pa_1, 0, 0);
362  PUTRGBA(dst_2, py_2, pa_2, 0, 0);
363 
364  LOADCHROMA(1);
365  PUTRGBA(dst_2, py_2, pa_2, 1, 0);
366  PUTRGBA(dst_1, py_1, pa_1, 1, 0);
367 
368  LOADCHROMA(2);
369  PUTRGBA(dst_1, py_1, pa_1, 2, 0);
370  PUTRGBA(dst_2, py_2, pa_2, 2, 0);
371 
372  LOADCHROMA(3);
373  PUTRGBA(dst_2, py_2, pa_2, 3, 0);
374  PUTRGBA(dst_1, py_1, pa_1, 3, 0);
375  pa_1 += 8;
376  pa_2 += 8;
377 ENDYUV2RGBLINE(8, 0)
378  LOADCHROMA(0);
379  PUTRGBA(dst_1, py_1, pa_1, 0, 0);
380  PUTRGBA(dst_2, py_2, pa_2, 0, 0);
381 
382  LOADCHROMA(1);
383  PUTRGBA(dst_2, py_2, pa_2, 1, 0);
384  PUTRGBA(dst_1, py_1, pa_1, 1, 0);
385  pa_1 += 4;
386  pa_2 += 4;
387 ENDYUV2RGBLINE(8, 1)
388  LOADCHROMA(0);
389  PUTRGBA(dst_1, py_1, pa_1, 0, 0);
390  PUTRGBA(dst_2, py_2, pa_2, 0, 0);
391  pa_1 += 2;
392  pa_2 += 2;
393 ENDYUV2RGBLINE(8, 2)
394  LOADCHROMA(0);
398 
399 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
400  LOADCHROMA(0);
403 
404  LOADCHROMA(1);
407 
408  LOADCHROMA(2);
411 
412  LOADCHROMA(3);
415 ENDYUV2RGBLINE(24, 0)
416  LOADCHROMA(0);
417  PUTRGB24(dst_1, py_1, 0);
418  PUTRGB24(dst_2, py_2, 0);
419 
420  LOADCHROMA(1);
421  PUTRGB24(dst_2, py_2, 1);
422  PUTRGB24(dst_1, py_1, 1);
423 ENDYUV2RGBLINE(24, 1)
424  LOADCHROMA(0);
425  PUTRGB24(dst_1, py_1, 0);
426  PUTRGB24(dst_2, py_2, 0);
427 ENDYUV2RGBLINE(24, 2)
428  LOADCHROMA(0);
432 
433 // only trivial mods from yuv2rgb_c_24_rgb
434 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
435  LOADCHROMA(0);
438 
439  LOADCHROMA(1);
442 
443  LOADCHROMA(2);
446 
447  LOADCHROMA(3);
450 ENDYUV2RGBLINE(24, 0)
451  LOADCHROMA(0);
452  PUTBGR24(dst_1, py_1, 0);
453  PUTBGR24(dst_2, py_2, 0);
454 
455  LOADCHROMA(1);
456  PUTBGR24(dst_2, py_2, 1);
457  PUTBGR24(dst_1, py_1, 1);
458 ENDYUV2RGBLINE(24, 1)
459  LOADCHROMA(0);
460  PUTBGR24(dst_1, py_1, 0);
461  PUTBGR24(dst_2, py_2, 0);
462 ENDYUV2RGBLINE(24, 2)
463  LOADCHROMA(0);
467 
468 YUV2RGBFUNC(yuv2rgb_c_16_ordered_dither, uint16_t, 0)
469  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
470  const uint8_t *e16 = ff_dither_2x2_4[y & 1];
471  const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
472 
473 #define PUTRGB16(dst, src, i, o) \
474  Y = src[2 * i]; \
475  dst[2 * i] = r[Y + d16[0 + o]] + \
476  g[Y + e16[0 + o]] + \
477  b[Y + f16[0 + o]]; \
478  Y = src[2 * i + 1]; \
479  dst[2 * i + 1] = r[Y + d16[1 + o]] + \
480  g[Y + e16[1 + o]] + \
481  b[Y + f16[1 + o]];
482 
483 #define PUTRGB16_ODD(dst, src, i, o) \
484  Y = src[2 * i]; \
485  dst[2 * i] = r[Y + d16[0 + o]] + \
486  g[Y + e16[0 + o]] + \
487  b[Y + f16[0 + o]];
488  LOADCHROMA(0);
489  PUTRGB16(dst_1, py_1, 0, 0);
490  PUTRGB16(dst_2, py_2, 0, 0 + 8);
491 
492  LOADCHROMA(1);
493  PUTRGB16(dst_2, py_2, 1, 2 + 8);
494  PUTRGB16(dst_1, py_1, 1, 2);
495 
496  LOADCHROMA(2);
497  PUTRGB16(dst_1, py_1, 2, 4);
498  PUTRGB16(dst_2, py_2, 2, 4 + 8);
499 
500  LOADCHROMA(3);
501  PUTRGB16(dst_2, py_2, 3, 6 + 8);
502  PUTRGB16(dst_1, py_1, 3, 6);
503 ENDYUV2RGBLINE(8, 0)
504  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
505  const uint8_t *e16 = ff_dither_2x2_4[y & 1];
506  const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
507 
508  LOADCHROMA(0);
509  PUTRGB16(dst_1, py_1, 0, 0);
510  PUTRGB16(dst_2, py_2, 0, 0 + 8);
511 
512  LOADCHROMA(1);
513  PUTRGB16(dst_2, py_2, 1, 2 + 8);
514  PUTRGB16(dst_1, py_1, 1, 2);
515 ENDYUV2RGBLINE(8, 1)
516  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
517  const uint8_t *e16 = ff_dither_2x2_4[y & 1];
518  const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
519 
520  LOADCHROMA(0);
521  PUTRGB16(dst_1, py_1, 0, 0);
522  PUTRGB16(dst_2, py_2, 0, 0 + 8);
523 ENDYUV2RGBLINE(8, 2)
524  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
525  const uint8_t *e16 = ff_dither_2x2_4[y & 1];
526  const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
527 
528  LOADCHROMA(0);
529  PUTRGB16_ODD(dst_1, py_1, 0, (c->dstW & 6));
530  PUTRGB16_ODD(dst_2, py_2, 0, (c->dstW & 6) + 8);
532 
533 YUV2RGBFUNC(yuv2rgb_c_15_ordered_dither, uint16_t, 0)
534  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
535  const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
536 
537 #define PUTRGB15(dst, src, i, o) \
538  Y = src[2 * i]; \
539  dst[2 * i] = r[Y + d16[0 + o]] + \
540  g[Y + d16[1 + o]] + \
541  b[Y + e16[0 + o]]; \
542  Y = src[2 * i + 1]; \
543  dst[2 * i + 1] = r[Y + d16[1 + o]] + \
544  g[Y + d16[0 + o]] + \
545  b[Y + e16[1 + o]];
546 
547 #define PUTRGB15_ODD(dst, src, i, o) \
548  Y = src[2 * i]; \
549  dst[2 * i] = r[Y + d16[0 + o]] + \
550  g[Y + d16[1 + o]] + \
551  b[Y + e16[0 + o]];
552  LOADCHROMA(0);
553  PUTRGB15(dst_1, py_1, 0, 0);
554  PUTRGB15(dst_2, py_2, 0, 0 + 8);
555 
556  LOADCHROMA(1);
557  PUTRGB15(dst_2, py_2, 1, 2 + 8);
558  PUTRGB15(dst_1, py_1, 1, 2);
559 
560  LOADCHROMA(2);
561  PUTRGB15(dst_1, py_1, 2, 4);
562  PUTRGB15(dst_2, py_2, 2, 4 + 8);
563 
564  LOADCHROMA(3);
565  PUTRGB15(dst_2, py_2, 3, 6 + 8);
566  PUTRGB15(dst_1, py_1, 3, 6);
567 ENDYUV2RGBLINE(8, 0)
568  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
569  const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
570 
571  LOADCHROMA(0);
572  PUTRGB15(dst_1, py_1, 0, 0);
573  PUTRGB15(dst_2, py_2, 0, 0 + 8);
574 
575  LOADCHROMA(1);
576  PUTRGB15(dst_2, py_2, 1, 2 + 8);
577  PUTRGB15(dst_1, py_1, 1, 2);
578 ENDYUV2RGBLINE(8, 1)
579  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
580  const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
581 
582  LOADCHROMA(0);
583  PUTRGB15(dst_1, py_1, 0, 0);
584  PUTRGB15(dst_2, py_2, 0, 0 + 8);
585 ENDYUV2RGBLINE(8, 2)
586  const uint8_t *d16 = ff_dither_2x2_8[y & 1];
587  const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
588 
589  LOADCHROMA(0);
590  PUTRGB15_ODD(dst_1, py_1, 0, (c->dstW & 6));
591  PUTRGB15_ODD(dst_2, py_2, 0, (c->dstW & 6) + 8);
593 
594 // r, g, b, dst_1, dst_2
595 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
596  const uint8_t *d16 = ff_dither_4x4_16[y & 3];
597 
598 #define PUTRGB12(dst, src, i, o) \
599  Y = src[2 * i]; \
600  dst[2 * i] = r[Y + d16[0 + o]] + \
601  g[Y + d16[0 + o]] + \
602  b[Y + d16[0 + o]]; \
603  Y = src[2 * i + 1]; \
604  dst[2 * i + 1] = r[Y + d16[1 + o]] + \
605  g[Y + d16[1 + o]] + \
606  b[Y + d16[1 + o]];
607 
608 #define PUTRGB12_ODD(dst, src, i, o) \
609  Y = src[2 * i]; \
610  dst[2 * i] = r[Y + d16[0 + o]] + \
611  g[Y + d16[0 + o]] + \
612  b[Y + d16[0 + o]];
613 
614  LOADCHROMA(0);
615  PUTRGB12(dst_1, py_1, 0, 0);
616  PUTRGB12(dst_2, py_2, 0, 0 + 8);
617 
618  LOADCHROMA(1);
619  PUTRGB12(dst_2, py_2, 1, 2 + 8);
620  PUTRGB12(dst_1, py_1, 1, 2);
621 
622  LOADCHROMA(2);
623  PUTRGB12(dst_1, py_1, 2, 4);
624  PUTRGB12(dst_2, py_2, 2, 4 + 8);
625 
626  LOADCHROMA(3);
627  PUTRGB12(dst_2, py_2, 3, 6 + 8);
628  PUTRGB12(dst_1, py_1, 3, 6);
629 ENDYUV2RGBLINE(8, 0)
630  const uint8_t *d16 = ff_dither_4x4_16[y & 3];
631 
632  LOADCHROMA(0);
633  PUTRGB12(dst_1, py_1, 0, 0);
634  PUTRGB12(dst_2, py_2, 0, 0 + 8);
635 
636  LOADCHROMA(1);
637  PUTRGB12(dst_2, py_2, 1, 2 + 8);
638  PUTRGB12(dst_1, py_1, 1, 2);
639 ENDYUV2RGBLINE(8, 1)
640  const uint8_t *d16 = ff_dither_4x4_16[y & 3];
641 
642  LOADCHROMA(0);
643  PUTRGB12(dst_1, py_1, 0, 0);
644  PUTRGB12(dst_2, py_2, 0, 0 + 8);
645 ENDYUV2RGBLINE(8, 2)
646  const uint8_t *d16 = ff_dither_4x4_16[y & 3];
647 
648  LOADCHROMA(0);
649  PUTRGB12_ODD(dst_1, py_1, 0, (c->dstW & 6));
650  PUTRGB12_ODD(dst_2, py_2, 0, (c->dstW & 6) + 8);
652 
653 // r, g, b, dst_1, dst_2
654 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
655  const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
656  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
657 
658 #define PUTRGB8(dst, src, i, o) \
659  Y = src[2 * i]; \
660  dst[2 * i] = r[Y + d32[0 + o]] + \
661  g[Y + d32[0 + o]] + \
662  b[Y + d64[0 + o]]; \
663  Y = src[2 * i + 1]; \
664  dst[2 * i + 1] = r[Y + d32[1 + o]] + \
665  g[Y + d32[1 + o]] + \
666  b[Y + d64[1 + o]];
667 
668 #define PUTRGB8_ODD(dst, src, i, o) \
669  Y = src[2 * i]; \
670  dst[2 * i] = r[Y + d32[0 + o]] + \
671  g[Y + d32[0 + o]] + \
672  b[Y + d64[0 + o]];
673 
674  LOADCHROMA(0);
675  PUTRGB8(dst_1, py_1, 0, 0);
676  PUTRGB8(dst_2, py_2, 0, 0 + 8);
677 
678  LOADCHROMA(1);
679  PUTRGB8(dst_2, py_2, 1, 2 + 8);
680  PUTRGB8(dst_1, py_1, 1, 2);
681 
682  LOADCHROMA(2);
683  PUTRGB8(dst_1, py_1, 2, 4);
684  PUTRGB8(dst_2, py_2, 2, 4 + 8);
685 
686  LOADCHROMA(3);
687  PUTRGB8(dst_2, py_2, 3, 6 + 8);
688  PUTRGB8(dst_1, py_1, 3, 6);
689 
690 ENDYUV2RGBLINE(8, 0)
691  const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
692  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
693  LOADCHROMA(0);
694  PUTRGB8(dst_1, py_1, 0, 0);
695  PUTRGB8(dst_2, py_2, 0, 0 + 8);
696 
697  LOADCHROMA(1);
698  PUTRGB8(dst_2, py_2, 1, 2 + 8);
699  PUTRGB8(dst_1, py_1, 1, 2);
700 
701 ENDYUV2RGBLINE(8, 1)
702  const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
703  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
704  LOADCHROMA(0);
705  PUTRGB8(dst_1, py_1, 0, 0);
706  PUTRGB8(dst_2, py_2, 0, 0 + 8);
707 
708 ENDYUV2RGBLINE(8, 2)
709  const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
710  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
711  LOADCHROMA(0);
712  PUTRGB8_ODD(dst_1, py_1, 0, (c->dstW & 6));
713  PUTRGB8_ODD(dst_2, py_2, 0, (c->dstW & 6) + 8);
714 
716 
717 
718 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
719  const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
720  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
721  int acc;
722 
723 #define PUTRGB4D(dst, src, i, o) \
724  Y = src[2 * i]; \
725  acc = r[Y + d128[0 + o]] + \
726  g[Y + d64[0 + o]] + \
727  b[Y + d128[0 + o]]; \
728  Y = src[2 * i + 1]; \
729  acc |= (r[Y + d128[1 + o]] + \
730  g[Y + d64[1 + o]] + \
731  b[Y + d128[1 + o]]) << 4; \
732  dst[i] = acc;
733 
734 #define PUTRGB4D_ODD(dst, src, i, o) \
735  Y = src[2 * i]; \
736  acc = r[Y + d128[0 + o]] + \
737  g[Y + d64[0 + o]] + \
738  b[Y + d128[0 + o]]; \
739  dst[i] = acc << 4;
740 
741  LOADCHROMA(0);
742  PUTRGB4D(dst_1, py_1, 0, 0);
743  PUTRGB4D(dst_2, py_2, 0, 0 + 8);
744 
745  LOADCHROMA(1);
746  PUTRGB4D(dst_2, py_2, 1, 2 + 8);
747  PUTRGB4D(dst_1, py_1, 1, 2);
748 
749  LOADCHROMA(2);
750  PUTRGB4D(dst_1, py_1, 2, 4);
751  PUTRGB4D(dst_2, py_2, 2, 4 + 8);
752 
753  LOADCHROMA(3);
754  PUTRGB4D(dst_2, py_2, 3, 6 + 8);
755  PUTRGB4D(dst_1, py_1, 3, 6);
756 
757 ENDYUV2RGBLINE(4, 0)
758  const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
759  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
760  int acc;
761  LOADCHROMA(0);
762  PUTRGB4D(dst_1, py_1, 0, 0);
763  PUTRGB4D(dst_2, py_2, 0, 0 + 8);
764 
765  LOADCHROMA(1);
766  PUTRGB4D(dst_2, py_2, 1, 2 + 8);
767  PUTRGB4D(dst_1, py_1, 1, 2);
768 
769 ENDYUV2RGBLINE(4, 1)
770  const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
771  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
772  int acc;
773  LOADCHROMA(0);
774  PUTRGB4D(dst_1, py_1, 0, 0);
775  PUTRGB4D(dst_2, py_2, 0, 0 + 8);
776 ENDYUV2RGBLINE(4, 2)
777  const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
778  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
779  int acc;
780  LOADCHROMA(0);
781  PUTRGB4D_ODD(dst_1, py_1, 0, (c->dstW & 6));
782  PUTRGB4D_ODD(dst_2, py_2, 0, (c->dstW & 6) + 8);
784 
785 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
786  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
787  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
788 
789 #define PUTRGB4DB(dst, src, i, o) \
790  Y = src[2 * i]; \
791  dst[2 * i] = r[Y + d128[0 + o]] + \
792  g[Y + d64[0 + o]] + \
793  b[Y + d128[0 + o]]; \
794  Y = src[2 * i + 1]; \
795  dst[2 * i + 1] = r[Y + d128[1 + o]] + \
796  g[Y + d64[1 + o]] + \
797  b[Y + d128[1 + o]];
798 
799 #define PUTRGB4DB_ODD(dst, src, i, o) \
800  Y = src[2 * i]; \
801  dst[2 * i] = r[Y + d128[0 + o]] + \
802  g[Y + d64[0 + o]] + \
803  b[Y + d128[0 + o]];
804 
805  LOADCHROMA(0);
807  PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
808 
809  LOADCHROMA(1);
810  PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
812 
813  LOADCHROMA(2);
815  PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
816 
817  LOADCHROMA(3);
818  PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
820 ENDYUV2RGBLINE(8, 0)
821  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
822  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
823  LOADCHROMA(0);
824  PUTRGB4DB(dst_1, py_1, 0, 0);
825  PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
826 
827  LOADCHROMA(1);
828  PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
829  PUTRGB4DB(dst_1, py_1, 1, 2);
830 ENDYUV2RGBLINE(8, 1)
831  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
832  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
833  LOADCHROMA(0);
834  PUTRGB4DB(dst_1, py_1, 0, 0);
835  PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
836 ENDYUV2RGBLINE(8, 2)
837  const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
838  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
839  LOADCHROMA(0);
840  PUTRGB4DB_ODD(dst_1, py_1, 0, (c->dstW & 6));
841  PUTRGB4DB_ODD(dst_2, py_2, 0, (c->dstW & 6) + 8);
843 
844 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
845  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
846  char out_1 = 0, out_2 = 0;
847  g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
848 
849 #define PUTRGB1(out, src, i, o) \
850  Y = src[2 * i]; \
851  out += out + g[Y + d128[0 + o]]; \
852  Y = src[2 * i + 1]; \
853  out += out + g[Y + d128[1 + o]];
854 
855  PUTRGB1(out_1, py_1, 0, 0);
856  PUTRGB1(out_2, py_2, 0, 0 + 8);
857 
858  PUTRGB1(out_2, py_2, 1, 2 + 8);
859  PUTRGB1(out_1, py_1, 1, 2);
860 
861  PUTRGB1(out_1, py_1, 2, 4);
862  PUTRGB1(out_2, py_2, 2, 4 + 8);
863 
864  PUTRGB1(out_2, py_2, 3, 6 + 8);
865  PUTRGB1(out_1, py_1, 3, 6);
866 
867  dst_1[0] = out_1;
868  dst_2[0] = out_2;
869 
870  py_1 += 8;
871  py_2 += 8;
872  dst_1 += 1;
873  dst_2 += 1;
874  }
875  if (c->dstW & 7) {
876  int av_unused Y, U, V;
877  int pixels_left = c->dstW & 7;
878  const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
879  char out_1 = 0, out_2 = 0;
880  g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
881 
882 #define PUTRGB1_OR00(out, src, i, o) \
883  if (pixels_left) { \
884  PUTRGB1(out, src, i, o) \
885  pixels_left--; \
886  } else { \
887  out <<= 2; \
888  }
889 
890  PUTRGB1_OR00(out_1, py_1, 0, 0);
891  PUTRGB1_OR00(out_2, py_2, 0, 0 + 8);
892 
893  PUTRGB1_OR00(out_2, py_2, 1, 2 + 8);
894  PUTRGB1_OR00(out_1, py_1, 1, 2);
895 
896  PUTRGB1_OR00(out_1, py_1, 2, 4);
897  PUTRGB1_OR00(out_2, py_2, 2, 4 + 8);
898 
899  PUTRGB1_OR00(out_2, py_2, 3, 6 + 8);
900  PUTRGB1_OR00(out_1, py_1, 3, 6);
901 
902  dst_1[0] = out_1;
903  dst_2[0] = out_2;
905 
907 {
908  SwsFunc t = NULL;
909 
910  if (ARCH_PPC)
911  t = ff_yuv2rgb_init_ppc(c);
912  if (ARCH_X86)
913  t = ff_yuv2rgb_init_x86(c);
914 
915  if (t)
916  return t;
917 
919  "No accelerated colorspace conversion found from %s to %s.\n",
920  av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
921 
922  switch (c->dstFormat) {
923  case AV_PIX_FMT_BGR48BE:
924  case AV_PIX_FMT_BGR48LE:
925  return yuv2rgb_c_bgr48;
926  case AV_PIX_FMT_RGB48BE:
927  case AV_PIX_FMT_RGB48LE:
928  return yuv2rgb_c_48;
929  case AV_PIX_FMT_ARGB:
930  case AV_PIX_FMT_ABGR:
931  if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat))
932  return yuva2argb_c;
933  case AV_PIX_FMT_RGBA:
934  case AV_PIX_FMT_BGRA:
935  return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
936  case AV_PIX_FMT_RGB24:
937  return yuv2rgb_c_24_rgb;
938  case AV_PIX_FMT_BGR24:
939  return yuv2rgb_c_24_bgr;
940  case AV_PIX_FMT_RGB565:
941  case AV_PIX_FMT_BGR565:
942  return yuv2rgb_c_16_ordered_dither;
943  case AV_PIX_FMT_RGB555:
944  case AV_PIX_FMT_BGR555:
945  return yuv2rgb_c_15_ordered_dither;
946  case AV_PIX_FMT_RGB444:
947  case AV_PIX_FMT_BGR444:
948  return yuv2rgb_c_12_ordered_dither;
949  case AV_PIX_FMT_RGB8:
950  case AV_PIX_FMT_BGR8:
951  return yuv2rgb_c_8_ordered_dither;
952  case AV_PIX_FMT_RGB4:
953  case AV_PIX_FMT_BGR4:
954  return yuv2rgb_c_4_ordered_dither;
957  return yuv2rgb_c_4b_ordered_dither;
959  return yuv2rgb_c_1_ordered_dither;
960  }
961  return NULL;
962 }
963 
964 static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
965  const int64_t inc, void *y_tab)
966 {
967  int i;
968  uint8_t *y_table = y_tab;
969 
970  y_table -= elemsize * (inc >> 9);
971 
972  for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
974  table[i] = y_table + elemsize * (cb >> 16);
975  }
976 }
977 
978 static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int64_t inc)
979 {
980  int i;
981  int off = -(inc >> 9);
982 
983  for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
985  table[i] = elemsize * (off + (cb >> 16));
986  }
987 }
988 
989 static uint16_t roundToInt16(int64_t f)
990 {
991  int r = (f + (1 << 15)) >> 16;
992 
993  if (r < -0x7FFF)
994  return 0x8000;
995  else if (r > 0x7FFF)
996  return 0x7FFF;
997  else
998  return r;
999 }
1000 
1001 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
1002  int fullRange, int brightness,
1003  int contrast, int saturation)
1004 {
1005  const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
1006  c->dstFormat == AV_PIX_FMT_RGB32_1 ||
1007  c->dstFormat == AV_PIX_FMT_BGR24 ||
1008  c->dstFormat == AV_PIX_FMT_RGB565BE ||
1009  c->dstFormat == AV_PIX_FMT_RGB565LE ||
1010  c->dstFormat == AV_PIX_FMT_RGB555BE ||
1011  c->dstFormat == AV_PIX_FMT_RGB555LE ||
1012  c->dstFormat == AV_PIX_FMT_RGB444BE ||
1013  c->dstFormat == AV_PIX_FMT_RGB444LE ||
1014  c->dstFormat == AV_PIX_FMT_RGB8 ||
1015  c->dstFormat == AV_PIX_FMT_RGB4 ||
1016  c->dstFormat == AV_PIX_FMT_RGB4_BYTE ||
1017  c->dstFormat == AV_PIX_FMT_MONOBLACK;
1018  const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
1019  c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
1020  c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
1021  c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
1022  c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
1023  c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE) ||
1024  c->dstFormat == AV_PIX_FMT_NE(X2RGB10LE, X2RGB10BE);
1025  const int bpp = c->dstFormatBpp;
1026  uint8_t *y_table;
1027  uint16_t *y_table16;
1028  uint32_t *y_table32;
1029  int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
1030  const int yoffs = (fullRange ? 384 : 326) + YUVRGB_TABLE_LUMA_HEADROOM;
1031  const int table_plane_size = 1024 + 2*YUVRGB_TABLE_LUMA_HEADROOM;
1032 
1033  int64_t crv = inv_table[0];
1034  int64_t cbu = inv_table[1];
1035  int64_t cgu = -inv_table[2];
1036  int64_t cgv = -inv_table[3];
1037  int64_t cy = 1 << 16;
1038  int64_t oy = 0;
1039  int64_t yb = 0;
1040 
1041  if (!fullRange) {
1042  cy = (cy * 255) / 219;
1043  oy = 16 << 16;
1044  } else {
1045  crv = (crv * 224) / 255;
1046  cbu = (cbu * 224) / 255;
1047  cgu = (cgu * 224) / 255;
1048  cgv = (cgv * 224) / 255;
1049  }
1050 
1051  cy = (cy * contrast) >> 16;
1052  crv = (crv * contrast * saturation) >> 32;
1053  cbu = (cbu * contrast * saturation) >> 32;
1054  cgu = (cgu * contrast * saturation) >> 32;
1055  cgv = (cgv * contrast * saturation) >> 32;
1056  oy -= 256LL * brightness;
1057 
1058  c->uOffset = 0x0400040004000400LL;
1059  c->vOffset = 0x0400040004000400LL;
1060  c->yCoeff = roundToInt16(cy * (1 << 13)) * 0x0001000100010001ULL;
1061  c->vrCoeff = roundToInt16(crv * (1 << 13)) * 0x0001000100010001ULL;
1062  c->ubCoeff = roundToInt16(cbu * (1 << 13)) * 0x0001000100010001ULL;
1063  c->vgCoeff = roundToInt16(cgv * (1 << 13)) * 0x0001000100010001ULL;
1064  c->ugCoeff = roundToInt16(cgu * (1 << 13)) * 0x0001000100010001ULL;
1065  c->yOffset = roundToInt16(oy * (1 << 3)) * 0x0001000100010001ULL;
1066 
1067  c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy * (1 << 13));
1068  c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy * (1 << 9));
1069  c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv * (1 << 13));
1070  c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv * (1 << 13));
1071  c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu * (1 << 13));
1072  c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu * (1 << 13));
1073 
1074  //scale coefficients by cy
1075  crv = ((crv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
1076  cbu = ((cbu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
1077  cgu = ((cgu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
1078  cgv = ((cgv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
1079 
1080  av_freep(&c->yuvTable);
1081 
1082 #define ALLOC_YUV_TABLE(x) \
1083  c->yuvTable = av_malloc(x); \
1084  if (!c->yuvTable) \
1085  return AVERROR(ENOMEM);
1086  switch (bpp) {
1087  case 1:
1088  ALLOC_YUV_TABLE(table_plane_size);
1089  y_table = c->yuvTable;
1090  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1091  for (i = 0; i < table_plane_size - 110; i++) {
1092  y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
1093  yb += cy;
1094  }
1095  fill_table(c->table_gU, 1, cgu, y_table + yoffs);
1096  fill_gv_table(c->table_gV, 1, cgv);
1097  break;
1098  case 4:
1099  case 4 | 128:
1100  rbase = isRgb ? 3 : 0;
1101  gbase = 1;
1102  bbase = isRgb ? 0 : 3;
1103  ALLOC_YUV_TABLE(table_plane_size * 3);
1104  y_table = c->yuvTable;
1105  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1106  for (i = 0; i < table_plane_size - 110; i++) {
1107  int yval = av_clip_uint8((yb + 0x8000) >> 16);
1108  y_table[i + 110] = (yval >> 7) << rbase;
1109  y_table[i + 37 + table_plane_size] = ((yval + 43) / 85) << gbase;
1110  y_table[i + 110 + 2*table_plane_size] = (yval >> 7) << bbase;
1111  yb += cy;
1112  }
1113  fill_table(c->table_rV, 1, crv, y_table + yoffs);
1114  fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
1115  fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
1116  fill_gv_table(c->table_gV, 1, cgv);
1117  break;
1118  case 8:
1119  rbase = isRgb ? 5 : 0;
1120  gbase = isRgb ? 2 : 3;
1121  bbase = isRgb ? 0 : 6;
1122  ALLOC_YUV_TABLE(table_plane_size * 3);
1123  y_table = c->yuvTable;
1124  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1125  for (i = 0; i < table_plane_size - 38; i++) {
1126  int yval = av_clip_uint8((yb + 0x8000) >> 16);
1127  y_table[i + 16] = ((yval + 18) / 36) << rbase;
1128  y_table[i + 16 + table_plane_size] = ((yval + 18) / 36) << gbase;
1129  y_table[i + 37 + 2*table_plane_size] = ((yval + 43) / 85) << bbase;
1130  yb += cy;
1131  }
1132  fill_table(c->table_rV, 1, crv, y_table + yoffs);
1133  fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
1134  fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
1135  fill_gv_table(c->table_gV, 1, cgv);
1136  break;
1137  case 12:
1138  rbase = isRgb ? 8 : 0;
1139  gbase = 4;
1140  bbase = isRgb ? 0 : 8;
1141  ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
1142  y_table16 = c->yuvTable;
1143  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1144  for (i = 0; i < table_plane_size; i++) {
1145  uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
1146  y_table16[i] = (yval >> 4) << rbase;
1147  y_table16[i + table_plane_size] = (yval >> 4) << gbase;
1148  y_table16[i + 2*table_plane_size] = (yval >> 4) << bbase;
1149  yb += cy;
1150  }
1151  if (isNotNe)
1152  for (i = 0; i < table_plane_size * 3; i++)
1153  y_table16[i] = av_bswap16(y_table16[i]);
1154  fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
1155  fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
1156  fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
1157  fill_gv_table(c->table_gV, 2, cgv);
1158  break;
1159  case 15:
1160  case 16:
1161  rbase = isRgb ? bpp - 5 : 0;
1162  gbase = 5;
1163  bbase = isRgb ? 0 : (bpp - 5);
1164  ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
1165  y_table16 = c->yuvTable;
1166  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1167  for (i = 0; i < table_plane_size; i++) {
1168  uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
1169  y_table16[i] = (yval >> 3) << rbase;
1170  y_table16[i + table_plane_size] = (yval >> (18 - bpp)) << gbase;
1171  y_table16[i + 2*table_plane_size] = (yval >> 3) << bbase;
1172  yb += cy;
1173  }
1174  if (isNotNe)
1175  for (i = 0; i < table_plane_size * 3; i++)
1176  y_table16[i] = av_bswap16(y_table16[i]);
1177  fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
1178  fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
1179  fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
1180  fill_gv_table(c->table_gV, 2, cgv);
1181  break;
1182  case 24:
1183  case 48:
1184  ALLOC_YUV_TABLE(table_plane_size);
1185  y_table = c->yuvTable;
1186  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1187  for (i = 0; i < table_plane_size; i++) {
1188  y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
1189  yb += cy;
1190  }
1191  fill_table(c->table_rV, 1, crv, y_table + yoffs);
1192  fill_table(c->table_gU, 1, cgu, y_table + yoffs);
1193  fill_table(c->table_bU, 1, cbu, y_table + yoffs);
1194  fill_gv_table(c->table_gV, 1, cgv);
1195  break;
1196  case 30:
1197  rbase = 20;
1198  gbase = 10;
1199  bbase = 0;
1200  needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
1201  if (!needAlpha)
1202  abase = 30;
1203  ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
1204  y_table32 = c->yuvTable;
1205  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1206  for (i = 0; i < table_plane_size; i++) {
1207  unsigned yval = av_clip_uintp2((yb + 0x8000) >> 14, 10);
1208  y_table32[i]= (yval << rbase) + (needAlpha ? 0 : (255u << abase));
1209  y_table32[i + table_plane_size] = yval << gbase;
1210  y_table32[i + 2 * table_plane_size] = yval << bbase;
1211  yb += cy;
1212  }
1213  if (isNotNe) {
1214  for (i = 0; i < table_plane_size * 3; i++)
1215  y_table32[i] = av_bswap32(y_table32[i]);
1216  }
1217  fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
1218  fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
1219  fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2 * table_plane_size);
1220  fill_gv_table(c->table_gV, 4, cgv);
1221  break;
1222  case 32:
1223  case 64:
1224  base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
1225  c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
1226  rbase = base + (isRgb ? 16 : 0);
1227  gbase = base + 8;
1228  bbase = base + (isRgb ? 0 : 16);
1229  needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
1230  if (!needAlpha)
1231  abase = (base + 24) & 31;
1232  ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
1233  y_table32 = c->yuvTable;
1234  yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1235  for (i = 0; i < table_plane_size; i++) {
1236  unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
1237  y_table32[i] = (yval << rbase) +
1238  (needAlpha ? 0 : (255u << abase));
1239  y_table32[i + table_plane_size] = yval << gbase;
1240  y_table32[i + 2*table_plane_size] = yval << bbase;
1241  yb += cy;
1242  }
1243  fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
1244  fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
1245  fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2*table_plane_size);
1246  fill_gv_table(c->table_gV, 4, cgv);
1247  break;
1248  default:
1249  if(!isPlanar(c->dstFormat) || bpp <= 24)
1250  av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
1251  return AVERROR(EINVAL);
1252  }
1253  return 0;
1254 }
#define U(x)
Definition: vp56_arith.h:37
#define av_unused
Definition: attributes.h:131
#define av_uninit(x)
Definition: attributes.h:154
#define av_cold
Definition: attributes.h:88
uint8_t
int32_t
#define V
Definition: avdct.c:30
#define av_bswap32
Definition: bswap.h:33
#define Y
Definition: boxblur.h:38
byte swapping routines
#define f(width, name)
Definition: cbs_vp9.c:255
#define FFMAX(a, b)
Definition: common.h:103
#define av_clip_uint8
Definition: common.h:128
#define av_clip_uintp2
Definition: common.h:146
#define ARCH_PPC
Definition: config.h:30
#define ARCH_X86
Definition: config.h:39
#define CONFIG_SWSCALE_ALPHA
Definition: config.h:568
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
#define SWS_CS_DEFAULT
Definition: swscale.h:95
const int * sws_getCoefficients(int colorspace)
Return a pointer to yuv<->rgb coefficients for the given colorspace suitable for sws_setColorspaceDet...
Definition: yuv2rgb.c:63
int i
Definition: input.c:407
const uint8_t ff_dither_8x8_73[][8]
Definition: output.c:72
const uint8_t ff_dither_2x2_4[][8]
Definition: output.c:40
const uint8_t ff_dither_2x2_8[][8]
Definition: output.c:46
const uint8_t ff_dither_4x4_16[][8]
Definition: output.c:52
const uint8_t ff_dither_8x8_220[][8]
Definition: output.c:85
const uint8_t ff_dither_8x8_32[][8]
Definition: output.c:60
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
#define AV_PIX_FMT_BGR444
Definition: pixfmt.h:393
#define AV_PIX_FMT_BGR555
Definition: pixfmt.h:392
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:76
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
@ AV_PIX_FMT_RGB555BE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:107
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_BGR48BE
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:148
@ AV_PIX_FMT_RGB48BE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:102
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
@ AV_PIX_FMT_RGB8
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:86
@ AV_PIX_FMT_BGR8
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:83
@ AV_PIX_FMT_RGB444LE
packed RGB 4:4:4, 16bpp, (msb)4X 4R 4G 4B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:139
@ AV_PIX_FMT_RGB4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:88
@ AV_PIX_FMT_BGR4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:85
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:106
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
@ AV_PIX_FMT_RGB48LE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:103
@ AV_PIX_FMT_RGB444BE
packed RGB 4:4:4, 16bpp, (msb)4X 4R 4G 4B(lsb), big-endian, X=unused/undefined
Definition: pixfmt.h:140
@ AV_PIX_FMT_BGR48LE
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:149
@ AV_PIX_FMT_RGB565BE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:105
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
@ AV_PIX_FMT_RGB4
packed RGB 1:2:1 bitstream, 4bpp, (msb)1R 2G 1B(lsb), a byte contains two pixels, the first pixel in ...
Definition: pixfmt.h:87
@ AV_PIX_FMT_BGR4
packed RGB 1:2:1 bitstream, 4bpp, (msb)1B 2G 1R(lsb), a byte contains two pixels, the first pixel in ...
Definition: pixfmt.h:84
#define AV_PIX_FMT_RGB32_1
Definition: pixfmt.h:373
#define AV_PIX_FMT_BGR32_1
Definition: pixfmt.h:375
#define AV_PIX_FMT_BGR565
Definition: pixfmt.h:391
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:386
#define AV_PIX_FMT_RGB444
Definition: pixfmt.h:388
#define AV_PIX_FMT_NE(be, le)
Definition: pixfmt.h:369
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:372
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:387
static const uint16_t table[]
Definition: prosumer.c:206
#define av_bswap16
Definition: bswap.h:31
external API header
#define YUVRGB_TABLE_HEADROOM
#define YUVRGB_TABLE_LUMA_HEADROOM
int(* SwsFunc)(struct SwsContext *context, const uint8_t *src[], int srcStride[], int srcSliceY, int srcSliceH, uint8_t *dst[], int dstStride[])
static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
#define av_freep(p)
#define av_log(a,...)
#define isALPHA(x)
Definition: swscale.c:51
const char * r
Definition: vf_curves.c:117
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:220
uint8_t base
Definition: vp3data.h:141
static double c[64]
av_cold SwsFunc ff_yuv2rgb_init_x86(SwsContext *c)
Definition: yuv2rgb.c:69
#define PUTRGB24(dst, src, i)
Definition: yuv2rgb.c:87
char out_2
Definition: yuv2rgb.c:846
const uint8_t * d64
Definition: yuv2rgb.c:656
#define PUTRGB1_OR00(out, src, i, o)
pa_1
Definition: yuv2rgb.c:331
#define PUTRGB15(dst, src, i, o)
Definition: yuv2rgb.c:537
py_2
Definition: yuv2rgb.c:871
#define PUTRGB_ODD(dst, src, i)
Definition: yuv2rgb.c:83
#define PUTRGB15_ODD(dst, src, i, o)
Definition: yuv2rgb.c:547
#define PUTBGR24_ODD(dst, src, i)
Definition: yuv2rgb.c:113
#define PUTRGB1(out, src, i, o)
Definition: yuv2rgb.c:849
const uint8_t * d16
Definition: yuv2rgb.c:469
#define ENDYUV2RGBFUNC()
Definition: yuv2rgb.c:203
const uint8_t * e16
Definition: yuv2rgb.c:470
#define PUTRGB(dst, src, i)
Definition: yuv2rgb.c:77
const uint8_t * d128
Definition: yuv2rgb.c:720
#define PUTRGB8(dst, src, i, o)
Definition: yuv2rgb.c:658
#define PUTRGB16_ODD(dst, src, i, o)
Definition: yuv2rgb.c:483
py_1
Definition: yuv2rgb.c:870
#define PUTRGB4DB(dst, src, i, o)
Definition: yuv2rgb.c:789
#define LOADCHROMA(i)
Definition: yuv2rgb.c:70
const uint8_t * d32
Definition: yuv2rgb.c:655
#define PUTRGB4DB_ODD(dst, src, i, o)
Definition: yuv2rgb.c:799
g
Definition: yuv2rgb.c:847
#define PUTRGB24_ODD(dst, src, i)
Definition: yuv2rgb.c:97
#define ENDYUV2RGBLINE(dst_delta, ss)
Definition: yuv2rgb.c:192
#define PUTRGB48_ODD(dst, src, i)
Definition: yuv2rgb.c:139
#define PUTBGR48_ODD(dst, src, i)
Definition: yuv2rgb.c:155
#define PUTRGB4D(dst, src, i, o)
Definition: yuv2rgb.c:723
pa_2
Definition: yuv2rgb.c:332
#define PUTRGB16(dst, src, i, o)
Definition: yuv2rgb.c:473
#define YUV2RGBFUNC(func_name, dst_type, alpha)
Definition: yuv2rgb.c:161
#define PUTRGB48(dst, src, i)
Definition: yuv2rgb.c:129
#define PUTRGB12_ODD(dst, src, i, o)
Definition: yuv2rgb.c:608
#define ALLOC_YUV_TABLE(x)
dst_1[0]
Definition: yuv2rgb.c:867
#define PUTRGBA(dst, ysrc, asrc, i, s)
Definition: yuv2rgb.c:119
char out_1
Definition: yuv2rgb.c:846
#define PUTRGB12(dst, src, i, o)
Definition: yuv2rgb.c:598
#define PUTRGB8_ODD(dst, src, i, o)
Definition: yuv2rgb.c:668
dst_2[0]
Definition: yuv2rgb.c:868
#define PUTRGB4D_ODD(dst, src, i, o)
Definition: yuv2rgb.c:734
int acc
Definition: yuv2rgb.c:721
#define PUTRGBA_ODD(dst, ysrc, asrc, i, s)
Definition: yuv2rgb.c:125
#define PUTBGR24(dst, src, i)
Definition: yuv2rgb.c:103
const uint8_t * f16
Definition: yuv2rgb.c:471
const int32_t ff_yuv2rgb_coeffs[11][4]
Definition: yuv2rgb.c:49
#define PUTBGR48(dst, src, i)
Definition: yuv2rgb.c:145
av_cold SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c)