Libav
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 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 
50 {
51  if (colorspace > 7 || colorspace < 0)
52  colorspace = SWS_CS_DEFAULT;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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;
558 
560 {
561  SwsFunc t = NULL;
562 
563  if (ARCH_PPC)
564  t = ff_yuv2rgb_init_ppc(c);
565  if (ARCH_X86)
566  t = ff_yuv2rgb_init_x86(c);
567 
568  if (t)
569  return t;
570 
572  "No accelerated colorspace conversion found from %s to %s.\n",
573  sws_format_name(c->srcFormat), sws_format_name(c->dstFormat));
574 
575  switch (c->dstFormat) {
576  case AV_PIX_FMT_BGR48BE:
577  case AV_PIX_FMT_BGR48LE:
578  return yuv2rgb_c_bgr48;
579  case AV_PIX_FMT_RGB48BE:
580  case AV_PIX_FMT_RGB48LE:
581  return yuv2rgb_c_48;
582  case AV_PIX_FMT_ARGB:
583  case AV_PIX_FMT_ABGR:
584  if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
585  return yuva2argb_c;
586  case AV_PIX_FMT_RGBA:
587  case AV_PIX_FMT_BGRA:
588  if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
589  return yuva2rgba_c;
590  else
591  return yuv2rgb_c_32;
592  case AV_PIX_FMT_RGB24:
593  return yuv2rgb_c_24_rgb;
594  case AV_PIX_FMT_BGR24:
595  return yuv2rgb_c_24_bgr;
596  case AV_PIX_FMT_RGB565:
597  case AV_PIX_FMT_BGR565:
598  case AV_PIX_FMT_RGB555:
599  case AV_PIX_FMT_BGR555:
600  return yuv2rgb_c_16;
601  case AV_PIX_FMT_RGB444:
602  case AV_PIX_FMT_BGR444:
603  return yuv2rgb_c_12_ordered_dither;
604  case AV_PIX_FMT_RGB8:
605  case AV_PIX_FMT_BGR8:
606  return yuv2rgb_c_8_ordered_dither;
607  case AV_PIX_FMT_RGB4:
608  case AV_PIX_FMT_BGR4:
609  return yuv2rgb_c_4_ordered_dither;
612  return yuv2rgb_c_4b_ordered_dither;
614  return yuv2rgb_c_1_ordered_dither;
615  default:
616  assert(0);
617  }
618  return NULL;
619 }
620 
621 static void fill_table(uint8_t *table[256], const int elemsize,
622  const int inc, void *y_tab)
623 {
624  int i;
625  int64_t cb = 0;
626  uint8_t *y_table = y_tab;
627 
628  y_table -= elemsize * (inc >> 9);
629 
630  for (i = 0; i < 256; i++) {
631  table[i] = y_table + elemsize * (cb >> 16);
632  cb += inc;
633  }
634 }
635 
636 static void fill_gv_table(int table[256], const int elemsize, const int inc)
637 {
638  int i;
639  int64_t cb = 0;
640  int off = -(inc >> 9);
641 
642  for (i = 0; i < 256; i++) {
643  table[i] = elemsize * (off + (cb >> 16));
644  cb += inc;
645  }
646 }
647 
648 static uint16_t roundToInt16(int64_t f)
649 {
650  int r = (f + (1 << 15)) >> 16;
651 
652  if (r < -0x7FFF)
653  return 0x8000;
654  else if (r > 0x7FFF)
655  return 0x7FFF;
656  else
657  return r;
658 }
659 
660 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
661  int fullRange, int brightness,
662  int contrast, int saturation)
663 {
664  const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
666  c->dstFormat == AV_PIX_FMT_BGR24 ||
673  c->dstFormat == AV_PIX_FMT_RGB8 ||
674  c->dstFormat == AV_PIX_FMT_RGB4 ||
677  const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
678  c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
679  c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
680  c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
681  c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
682  c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE);
683  const int bpp = c->dstFormatBpp;
684  uint8_t *y_table;
685  uint16_t *y_table16;
686  uint32_t *y_table32;
687  int i, base, rbase, gbase, bbase, abase, needAlpha;
688  const int yoffs = fullRange ? 384 : 326;
689 
690  int64_t crv = inv_table[0];
691  int64_t cbu = inv_table[1];
692  int64_t cgu = -inv_table[2];
693  int64_t cgv = -inv_table[3];
694  int64_t cy = 1 << 16;
695  int64_t oy = 0;
696  int64_t yb = 0;
697 
698  if (!fullRange) {
699  cy = (cy * 255) / 219;
700  oy = 16 << 16;
701  } else {
702  crv = (crv * 224) / 255;
703  cbu = (cbu * 224) / 255;
704  cgu = (cgu * 224) / 255;
705  cgv = (cgv * 224) / 255;
706  }
707 
708  cy = (cy * contrast) >> 16;
709  crv = (crv * contrast * saturation) >> 32;
710  cbu = (cbu * contrast * saturation) >> 32;
711  cgu = (cgu * contrast * saturation) >> 32;
712  cgv = (cgv * contrast * saturation) >> 32;
713  oy -= 256 * brightness;
714 
715  c->uOffset = 0x0400040004000400LL;
716  c->vOffset = 0x0400040004000400LL;
717  c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
718  c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
719  c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
720  c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
721  c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
722  c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
723 
724  c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
725  c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
726  c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
727  c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
728  c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
729  c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
730 
731  //scale coefficients by cy
732  crv = ((crv << 16) + 0x8000) / cy;
733  cbu = ((cbu << 16) + 0x8000) / cy;
734  cgu = ((cgu << 16) + 0x8000) / cy;
735  cgv = ((cgv << 16) + 0x8000) / cy;
736 
737  av_free(c->yuvTable);
738 
739  switch (bpp) {
740  case 1:
741  c->yuvTable = av_malloc(1024);
742  y_table = c->yuvTable;
743  yb = -(384 << 16) - oy;
744  for (i = 0; i < 1024 - 110; i++) {
745  y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
746  yb += cy;
747  }
748  fill_table(c->table_gU, 1, cgu, y_table + yoffs);
749  fill_gv_table(c->table_gV, 1, cgv);
750  break;
751  case 4:
752  case 4 | 128:
753  rbase = isRgb ? 3 : 0;
754  gbase = 1;
755  bbase = isRgb ? 0 : 3;
756  c->yuvTable = av_malloc(1024 * 3);
757  y_table = c->yuvTable;
758  yb = -(384 << 16) - oy;
759  for (i = 0; i < 1024 - 110; i++) {
760  int yval = av_clip_uint8((yb + 0x8000) >> 16);
761  y_table[i + 110] = (yval >> 7) << rbase;
762  y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
763  y_table[i + 110 + 2048] = (yval >> 7) << bbase;
764  yb += cy;
765  }
766  fill_table(c->table_rV, 1, crv, y_table + yoffs);
767  fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
768  fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
769  fill_gv_table(c->table_gV, 1, cgv);
770  break;
771  case 8:
772  rbase = isRgb ? 5 : 0;
773  gbase = isRgb ? 2 : 3;
774  bbase = isRgb ? 0 : 6;
775  c->yuvTable = av_malloc(1024 * 3);
776  y_table = c->yuvTable;
777  yb = -(384 << 16) - oy;
778  for (i = 0; i < 1024 - 38; i++) {
779  int yval = av_clip_uint8((yb + 0x8000) >> 16);
780  y_table[i + 16] = ((yval + 18) / 36) << rbase;
781  y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
782  y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
783  yb += cy;
784  }
785  fill_table(c->table_rV, 1, crv, y_table + yoffs);
786  fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
787  fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
788  fill_gv_table(c->table_gV, 1, cgv);
789  break;
790  case 12:
791  rbase = isRgb ? 8 : 0;
792  gbase = 4;
793  bbase = isRgb ? 0 : 8;
794  c->yuvTable = av_malloc(1024 * 3 * 2);
795  y_table16 = c->yuvTable;
796  yb = -(384 << 16) - oy;
797  for (i = 0; i < 1024; i++) {
798  uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
799  y_table16[i] = (yval >> 4) << rbase;
800  y_table16[i + 1024] = (yval >> 4) << gbase;
801  y_table16[i + 2048] = (yval >> 4) << bbase;
802  yb += cy;
803  }
804  if (isNotNe)
805  for (i = 0; i < 1024 * 3; i++)
806  y_table16[i] = av_bswap16(y_table16[i]);
807  fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
808  fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
809  fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
810  fill_gv_table(c->table_gV, 2, cgv);
811  break;
812  case 15:
813  case 16:
814  rbase = isRgb ? bpp - 5 : 0;
815  gbase = 5;
816  bbase = isRgb ? 0 : (bpp - 5);
817  c->yuvTable = av_malloc(1024 * 3 * 2);
818  y_table16 = c->yuvTable;
819  yb = -(384 << 16) - oy;
820  for (i = 0; i < 1024; i++) {
821  uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
822  y_table16[i] = (yval >> 3) << rbase;
823  y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
824  y_table16[i + 2048] = (yval >> 3) << bbase;
825  yb += cy;
826  }
827  if (isNotNe)
828  for (i = 0; i < 1024 * 3; i++)
829  y_table16[i] = av_bswap16(y_table16[i]);
830  fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
831  fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
832  fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
833  fill_gv_table(c->table_gV, 2, cgv);
834  break;
835  case 24:
836  case 48:
837  c->yuvTable = av_malloc(1024);
838  y_table = c->yuvTable;
839  yb = -(384 << 16) - oy;
840  for (i = 0; i < 1024; i++) {
841  y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
842  yb += cy;
843  }
844  fill_table(c->table_rV, 1, crv, y_table + yoffs);
845  fill_table(c->table_gU, 1, cgu, y_table + yoffs);
846  fill_table(c->table_bU, 1, cbu, y_table + yoffs);
847  fill_gv_table(c->table_gV, 1, cgv);
848  break;
849  case 32:
850  base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
851  c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
852  rbase = base + (isRgb ? 16 : 0);
853  gbase = base + 8;
854  bbase = base + (isRgb ? 0 : 16);
855  needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
856  if (!needAlpha)
857  abase = (base + 24) & 31;
858  c->yuvTable = av_malloc(1024 * 3 * 4);
859  y_table32 = c->yuvTable;
860  yb = -(384 << 16) - oy;
861  for (i = 0; i < 1024; i++) {
862  unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
863  y_table32[i] = (yval << rbase) +
864  (needAlpha ? 0 : (255u << abase));
865  y_table32[i + 1024] = yval << gbase;
866  y_table32[i + 2048] = yval << bbase;
867  yb += cy;
868  }
869  fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
870  fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
871  fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
872  fill_gv_table(c->table_gV, 4, cgv);
873  break;
874  default:
875  c->yuvTable = NULL;
876  if(!isPlanar(c->dstFormat) || bpp <= 24)
877  av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
878  return -1;
879  }
880  return 0;
881 }
uint64_t vrCoeff
dst_1[0]
Definition: yuv2rgb.c:555
const char * sws_format_name(enum AVPixelFormat format)
Definition: utils.c:205
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:116
char out_2
Definition: yuv2rgb.c:534
const int32_t ff_yuv2rgb_coeffs[8][4]
Definition: yuv2rgb.c:38
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:95
pa_1
Definition: yuv2rgb.c:272
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:98
int acc
Definition: yuv2rgb.c:471
static void fill_table(uint8_t *table[256], const int elemsize, const int inc, void *y_tab)
Definition: yuv2rgb.c:621
av_cold SwsFunc ff_yuv2rgb_init_x86(SwsContext *c)
Definition: yuv2rgb.c:73
#define ARCH_X86
Definition: config.h:33
int dstFormatBpp
Number of bits per pixel of the destination pixel format.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define CONFIG_SWSCALE_ALPHA
Definition: config.h:348
#define AV_PIX_FMT_BGR32_1
Definition: pixfmt.h:225
#define PUTBGR24(dst, src, i)
Definition: yuv2rgb.c:79
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:86
uint64_t ubCoeff
#define PUTRGB(dst, src, i)
Definition: yuv2rgb.c:63
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:112
uint8_t
const int * sws_getCoefficients(int colorspace)
Return a pointer to yuv<->rgb coefficients for the given colorspace suitable for sws_setColorspaceDet...
Definition: yuv2rgb.c:49
uint8_t * table_bU[256]
#define av_bswap16
Definition: bswap.h:31
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:117
uint64_t yOffset
enum AVPixelFormat dstFormat
Destination pixel format.
#define PUTRGBA(dst, ysrc, asrc, i, s)
Definition: yuv2rgb.c:89
#define isALPHA(x)
Definition: swscale-test.c:49
#define r
Definition: input.c:51
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:88
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
packed RGB 1:2:1 bitstream, 4bpp, (msb)1B 2G 1R(lsb), a byte contains two pixels, the first pixel in ...
Definition: pixfmt.h:87
#define PUTBGR48(dst, src, i)
Definition: yuv2rgb.c:105
const uint8_t * d64
Definition: yuv2rgb.c:439
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:104
g
Definition: yuv2rgb.c:535
#define ENDYUV2RGBFUNC()
Definition: yuv2rgb.c:156
#define AV_PIX_FMT_NE(be, le)
Definition: pixfmt.h:219
const uint8_t ff_dither_8x8_32[8][8]
Definition: output.c:56
uint64_t ugCoeff
#define SWS_CS_DEFAULT
Definition: swscale.h:109
#define YUV2RGBFUNC(func_name, dst_type, alpha)
Definition: yuv2rgb.c:115
static uint16_t roundToInt16(int64_t f)
Definition: yuv2rgb.c:648
external api for the swscale stuff
uint64_t vgCoeff
uint64_t uOffset
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:97
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:113
int32_t
int table_gV[256]
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:118
const uint8_t ff_dither_4x4_16[4][8]
Definition: output.c:49
#define PUTRGB4D(dst, src, i, o)
Definition: yuv2rgb.c:473
av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
Definition: yuv2rgb.c:660
int(* SwsFunc)(struct SwsContext *context, const uint8_t *src[], int srcStride[], int srcSliceY, int srcSliceH, uint8_t *dst[], int dstStride[])
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:91
const uint8_t * d16
Definition: yuv2rgb.c:407
#define AV_PIX_FMT_RGB32_1
Definition: pixfmt.h:223
NULL
Definition: eval.c:55
#define CLOSEYUV2RGBFUNC(dst_delta)
Definition: yuv2rgb.c:162
#define av_cold
Definition: attributes.h:66
SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
Definition: yuv2rgb.c:559
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:231
static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
#define PUTRGB12(dst, src, i, o)
Definition: yuv2rgb.c:409
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
uint8_t * table_gU[256]
const uint8_t ff_dither_8x8_220[8][8]
Definition: output.c:79
#define AV_PIX_FMT_RGB444
Definition: pixfmt.h:232
#define ENDYUV2RGBLINE(dst_delta, ss)
Definition: yuv2rgb.c:145
pa_2
Definition: yuv2rgb.c:273
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:89
packed RGB 1:2:1 bitstream, 4bpp, (msb)1R 2G 1B(lsb), a byte contains two pixels, the first pixel in ...
Definition: pixfmt.h:90
av_cold SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c)
#define ARCH_PPC
Definition: config.h:24
const uint8_t ff_dither_8x8_73[8][8]
Definition: output.c:67
#define PUTRGB48(dst, src, i)
Definition: yuv2rgb.c:95
packed RGB 4:4:4, 16bpp, (msb)4A 4R 4G 4B(lsb), big-endian, most significant bits to 0 ...
Definition: pixfmt.h:141
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:230
#define PUTRGB4DB(dst, src, i, o)
Definition: yuv2rgb.c:505
#define PUTRGB8(dst, src, i, o)
Definition: yuv2rgb.c:441
enum AVPixelFormat srcFormat
Source pixel format.
packed RGB 4:4:4, 16bpp, (msb)4A 4R 4G 4B(lsb), little-endian, most significant bits to 0 ...
Definition: pixfmt.h:140
const uint8_t * d128
Definition: yuv2rgb.c:470
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
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:222
#define PUTRGB1(out, src, i, o)
Definition: yuv2rgb.c:537
static void fill_gv_table(int table[256], const int elemsize, const int inc)
Definition: yuv2rgb.c:636
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:96
#define AV_PIX_FMT_BGR444
Definition: pixfmt.h:237
char out_1
Definition: yuv2rgb.c:534
uint64_t yCoeff
#define AV_PIX_FMT_BGR565
Definition: pixfmt.h:235
#define LOADCHROMA(i)
Definition: yuv2rgb.c:56
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:115
dst_2[0]
Definition: yuv2rgb.c:556
const uint8_t * d32
Definition: yuv2rgb.c:438
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
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
enum AVColorSpace colorspace
Definition: dirac.c:100
uint8_t * table_rV[256]
uint64_t vOffset
#define PUTRGB24(dst, src, i)
Definition: yuv2rgb.c:69
#define AV_PIX_FMT_BGR555
Definition: pixfmt.h:236