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 extern const uint8_t dither_4x4_16[4][8];
39 extern const uint8_t dither_8x8_32[8][8];
40 extern const uint8_t dither_8x8_73[8][8];
41 extern const uint8_t dither_8x8_220[8][8];
42 
43 const int32_t ff_yuv2rgb_coeffs[8][4] = {
44  { 117504, 138453, 13954, 34903 }, /* no sequence_display_extension */
45  { 117504, 138453, 13954, 34903 }, /* ITU-R Rec. 709 (1990) */
46  { 104597, 132201, 25675, 53279 }, /* unspecified */
47  { 104597, 132201, 25675, 53279 }, /* reserved */
48  { 104448, 132798, 24759, 53109 }, /* FCC */
49  { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
50  { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
51  { 117579, 136230, 16907, 35559 } /* SMPTE 240M (1987) */
52 };
53 
55 {
56  if (colorspace > 7 || colorspace < 0)
57  colorspace = SWS_CS_DEFAULT;
59 }
60 
61 #define LOADCHROMA(i) \
62  U = pu[i]; \
63  V = pv[i]; \
64  r = (void *)c->table_rV[V]; \
65  g = (void *)(c->table_gU[U] + c->table_gV[V]); \
66  b = (void *)c->table_bU[U];
67 
68 #define PUTRGB(dst, src, i) \
69  Y = src[2 * i]; \
70  dst[2 * i] = r[Y] + g[Y] + b[Y]; \
71  Y = src[2 * i + 1]; \
72  dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
73 
74 #define PUTRGB24(dst, src, i) \
75  Y = src[2 * i]; \
76  dst[6 * i + 0] = r[Y]; \
77  dst[6 * i + 1] = g[Y]; \
78  dst[6 * i + 2] = b[Y]; \
79  Y = src[2 * i + 1]; \
80  dst[6 * i + 3] = r[Y]; \
81  dst[6 * i + 4] = g[Y]; \
82  dst[6 * i + 5] = b[Y];
83 
84 #define PUTBGR24(dst, src, i) \
85  Y = src[2 * i]; \
86  dst[6 * i + 0] = b[Y]; \
87  dst[6 * i + 1] = g[Y]; \
88  dst[6 * i + 2] = r[Y]; \
89  Y = src[2 * i + 1]; \
90  dst[6 * i + 3] = b[Y]; \
91  dst[6 * i + 4] = g[Y]; \
92  dst[6 * i + 5] = r[Y];
93 
94 #define PUTRGBA(dst, ysrc, asrc, i, s) \
95  Y = ysrc[2 * i]; \
96  dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
97  Y = ysrc[2 * i + 1]; \
98  dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
99 
100 #define PUTRGB48(dst, src, i) \
101  Y = src[ 2 * i]; \
102  dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
103  dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
104  dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
105  Y = src[ 2 * i + 1]; \
106  dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
107  dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
108  dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
109 
110 #define PUTBGR48(dst, src, i) \
111  Y = src[2 * i]; \
112  dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
113  dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
114  dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
115  Y = src[2 * i + 1]; \
116  dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
117  dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
118  dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
119 
120 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
121  static int func_name(SwsContext *c, const uint8_t *src[], \
122  int srcStride[], int srcSliceY, int srcSliceH, \
123  uint8_t *dst[], int dstStride[]) \
124  { \
125  int y; \
126  \
127  if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
128  srcStride[1] *= 2; \
129  srcStride[2] *= 2; \
130  } \
131  for (y = 0; y < srcSliceH; y += 2) { \
132  dst_type *dst_1 = \
133  (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
134  dst_type *dst_2 = \
135  (dst_type *)(dst[0] + (y + srcSliceY + 1) * dstStride[0]); \
136  dst_type av_unused *r, *g, *b; \
137  const uint8_t *py_1 = src[0] + y * srcStride[0]; \
138  const uint8_t *py_2 = py_1 + srcStride[0]; \
139  const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
140  const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
141  const uint8_t av_unused *pa_1, *pa_2; \
142  unsigned int h_size = c->dstW >> 3; \
143  if (alpha) { \
144  pa_1 = src[3] + y * srcStride[3]; \
145  pa_2 = pa_1 + srcStride[3]; \
146  } \
147  while (h_size--) { \
148  int av_unused U, V, Y; \
149 
150 #define ENDYUV2RGBLINE(dst_delta, ss) \
151  pu += 4 >> ss; \
152  pv += 4 >> ss; \
153  py_1 += 8 >> ss; \
154  py_2 += 8 >> ss; \
155  dst_1 += dst_delta >> ss; \
156  dst_2 += dst_delta >> ss; \
157  } \
158  if (c->dstW & (4 >> ss)) { \
159  int av_unused Y, U, V; \
160 
161 #define ENDYUV2RGBFUNC() \
162  } \
163  } \
164  return srcSliceH; \
165  }
166 
167 #define CLOSEYUV2RGBFUNC(dst_delta) \
168  ENDYUV2RGBLINE(dst_delta, 0) \
169  ENDYUV2RGBFUNC()
170 
171 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
172  LOADCHROMA(0);
173  PUTRGB48(dst_1, py_1, 0);
174  PUTRGB48(dst_2, py_2, 0);
175 
176  LOADCHROMA(1);
177  PUTRGB48(dst_2, py_2, 1);
178  PUTRGB48(dst_1, py_1, 1);
179 
180  LOADCHROMA(2);
181  PUTRGB48(dst_1, py_1, 2);
182  PUTRGB48(dst_2, py_2, 2);
183 
184  LOADCHROMA(3);
185  PUTRGB48(dst_2, py_2, 3);
186  PUTRGB48(dst_1, py_1, 3);
187 ENDYUV2RGBLINE(48, 0)
188  LOADCHROMA(0);
189  PUTRGB48(dst_1, py_1, 0);
190  PUTRGB48(dst_2, py_2, 0);
191 
192  LOADCHROMA(1);
193  PUTRGB48(dst_2, py_2, 1);
194  PUTRGB48(dst_1, py_1, 1);
195 ENDYUV2RGBLINE(48, 1)
196  LOADCHROMA(0);
197  PUTRGB48(dst_1, py_1, 0);
198  PUTRGB48(dst_2, py_2, 0);
200 
201 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
202  LOADCHROMA(0);
203  PUTBGR48(dst_1, py_1, 0);
204  PUTBGR48(dst_2, py_2, 0);
205 
206  LOADCHROMA(1);
207  PUTBGR48(dst_2, py_2, 1);
208  PUTBGR48(dst_1, py_1, 1);
209 
210  LOADCHROMA(2);
211  PUTBGR48(dst_1, py_1, 2);
212  PUTBGR48(dst_2, py_2, 2);
213 
214  LOADCHROMA(3);
215  PUTBGR48(dst_2, py_2, 3);
216  PUTBGR48(dst_1, py_1, 3);
217 ENDYUV2RGBLINE(48, 0)
218  LOADCHROMA(0);
219  PUTBGR48(dst_1, py_1, 0);
220  PUTBGR48(dst_2, py_2, 0);
221 
222  LOADCHROMA(1);
223  PUTBGR48(dst_2, py_2, 1);
224  PUTBGR48(dst_1, py_1, 1);
225 ENDYUV2RGBLINE(48, 1)
226  LOADCHROMA(0);
227  PUTBGR48(dst_1, py_1, 0);
228  PUTBGR48(dst_2, py_2, 0);
230 
231 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
232  LOADCHROMA(0);
233  PUTRGB(dst_1, py_1, 0);
234  PUTRGB(dst_2, py_2, 0);
235 
236  LOADCHROMA(1);
237  PUTRGB(dst_2, py_2, 1);
238  PUTRGB(dst_1, py_1, 1);
239 
240  LOADCHROMA(2);
241  PUTRGB(dst_1, py_1, 2);
242  PUTRGB(dst_2, py_2, 2);
243 
244  LOADCHROMA(3);
245  PUTRGB(dst_2, py_2, 3);
246  PUTRGB(dst_1, py_1, 3);
247 ENDYUV2RGBLINE(8, 0)
248  LOADCHROMA(0);
249  PUTRGB(dst_1, py_1, 0);
250  PUTRGB(dst_2, py_2, 0);
251 
252  LOADCHROMA(1);
253  PUTRGB(dst_2, py_2, 1);
254  PUTRGB(dst_1, py_1, 1);
255 ENDYUV2RGBLINE(8, 1)
256  LOADCHROMA(0);
257  PUTRGB(dst_1, py_1, 0);
258  PUTRGB(dst_2, py_2, 0);
260 
261 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
262  LOADCHROMA(0);
263  PUTRGBA(dst_1, py_1, pa_1, 0, 24);
264  PUTRGBA(dst_2, py_2, pa_2, 0, 24);
265 
266  LOADCHROMA(1);
267  PUTRGBA(dst_2, py_2, pa_2, 1, 24);
268  PUTRGBA(dst_1, py_1, pa_1, 1, 24);
269 
270  LOADCHROMA(2);
271  PUTRGBA(dst_1, py_1, pa_1, 2, 24);
272  PUTRGBA(dst_2, py_2, pa_2, 2, 24);
273 
274  LOADCHROMA(3);
275  PUTRGBA(dst_2, py_2, pa_2, 3, 24);
276  PUTRGBA(dst_1, py_1, pa_1, 3, 24);
277  pa_1 += 8; \
278  pa_2 += 8; \
279 ENDYUV2RGBLINE(8, 0)
280  LOADCHROMA(0);
281  PUTRGBA(dst_1, py_1, pa_1, 0, 24);
282  PUTRGBA(dst_2, py_2, pa_2, 0, 24);
283 
284  LOADCHROMA(1);
285  PUTRGBA(dst_2, py_2, pa_2, 1, 24);
286  PUTRGBA(dst_1, py_1, pa_1, 1, 24);
287  pa_1 += 4; \
288  pa_2 += 4; \
289 ENDYUV2RGBLINE(8, 1)
290  LOADCHROMA(0);
291  PUTRGBA(dst_1, py_1, pa_1, 0, 24);
292  PUTRGBA(dst_2, py_2, pa_2, 0, 24);
294 
295 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
296  LOADCHROMA(0);
297  PUTRGBA(dst_1, py_1, pa_1, 0, 0);
298  PUTRGBA(dst_2, py_2, pa_2, 0, 0);
299 
300  LOADCHROMA(1);
301  PUTRGBA(dst_2, py_2, pa_2, 1, 0);
302  PUTRGBA(dst_1, py_1, pa_1, 1, 0);
303 
304  LOADCHROMA(2);
305  PUTRGBA(dst_1, py_1, pa_1, 2, 0);
306  PUTRGBA(dst_2, py_2, pa_2, 2, 0);
307 
308  LOADCHROMA(3);
309  PUTRGBA(dst_2, py_2, pa_2, 3, 0);
310  PUTRGBA(dst_1, py_1, pa_1, 3, 0);
311  pa_1 += 8; \
312  pa_2 += 8; \
313 ENDYUV2RGBLINE(8, 0)
314  LOADCHROMA(0);
315  PUTRGBA(dst_1, py_1, pa_1, 0, 0);
316  PUTRGBA(dst_2, py_2, pa_2, 0, 0);
317 
318  LOADCHROMA(1);
319  PUTRGBA(dst_2, py_2, pa_2, 1, 0);
320  PUTRGBA(dst_1, py_1, pa_1, 1, 0);
321  pa_1 += 4; \
322  pa_2 += 4; \
323 ENDYUV2RGBLINE(8, 1)
324  LOADCHROMA(0);
325  PUTRGBA(dst_1, py_1, pa_1, 0, 0);
326  PUTRGBA(dst_2, py_2, pa_2, 0, 0);
328 
329 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
330  LOADCHROMA(0);
331  PUTRGB24(dst_1, py_1, 0);
332  PUTRGB24(dst_2, py_2, 0);
333 
334  LOADCHROMA(1);
335  PUTRGB24(dst_2, py_2, 1);
336  PUTRGB24(dst_1, py_1, 1);
337 
338  LOADCHROMA(2);
339  PUTRGB24(dst_1, py_1, 2);
340  PUTRGB24(dst_2, py_2, 2);
341 
342  LOADCHROMA(3);
343  PUTRGB24(dst_2, py_2, 3);
344  PUTRGB24(dst_1, py_1, 3);
345 ENDYUV2RGBLINE(24, 0)
346  LOADCHROMA(0);
347  PUTRGB24(dst_1, py_1, 0);
348  PUTRGB24(dst_2, py_2, 0);
349 
350  LOADCHROMA(1);
351  PUTRGB24(dst_2, py_2, 1);
352  PUTRGB24(dst_1, py_1, 1);
353 ENDYUV2RGBLINE(24, 1)
354  LOADCHROMA(0);
355  PUTRGB24(dst_1, py_1, 0);
356  PUTRGB24(dst_2, py_2, 0);
358 
359 // only trivial mods from yuv2rgb_c_24_rgb
360 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
361  LOADCHROMA(0);
362  PUTBGR24(dst_1, py_1, 0);
363  PUTBGR24(dst_2, py_2, 0);
364 
365  LOADCHROMA(1);
366  PUTBGR24(dst_2, py_2, 1);
367  PUTBGR24(dst_1, py_1, 1);
368 
369  LOADCHROMA(2);
370  PUTBGR24(dst_1, py_1, 2);
371  PUTBGR24(dst_2, py_2, 2);
372 
373  LOADCHROMA(3);
374  PUTBGR24(dst_2, py_2, 3);
375  PUTBGR24(dst_1, py_1, 3);
376 ENDYUV2RGBLINE(24, 0)
377  LOADCHROMA(0);
378  PUTBGR24(dst_1, py_1, 0);
379  PUTBGR24(dst_2, py_2, 0);
380 
381  LOADCHROMA(1);
382  PUTBGR24(dst_2, py_2, 1);
383  PUTBGR24(dst_1, py_1, 1);
384 ENDYUV2RGBLINE(24, 1)
385  LOADCHROMA(0);
386  PUTBGR24(dst_1, py_1, 0);
387  PUTBGR24(dst_2, py_2, 0);
389 
390 // This is exactly the same code as yuv2rgb_c_32 except for the types of
391 // r, g, b, dst_1, dst_2
392 YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
393  LOADCHROMA(0);
394  PUTRGB(dst_1, py_1, 0);
395  PUTRGB(dst_2, py_2, 0);
396 
397  LOADCHROMA(1);
398  PUTRGB(dst_2, py_2, 1);
399  PUTRGB(dst_1, py_1, 1);
400 
401  LOADCHROMA(2);
402  PUTRGB(dst_1, py_1, 2);
403  PUTRGB(dst_2, py_2, 2);
404 
405  LOADCHROMA(3);
406  PUTRGB(dst_2, py_2, 3);
407  PUTRGB(dst_1, py_1, 3);
409 
410 // r, g, b, dst_1, dst_2
411 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
412  const uint8_t *d16 = dither_4x4_16[y & 3];
413 
414 #define PUTRGB12(dst, src, i, o) \
415  Y = src[2 * i]; \
416  dst[2 * i] = r[Y + d16[0 + o]] + \
417  g[Y + d16[0 + o]] + \
418  b[Y + d16[0 + o]]; \
419  Y = src[2 * i + 1]; \
420  dst[2 * i + 1] = r[Y + d16[1 + o]] + \
421  g[Y + d16[1 + o]] + \
422  b[Y + d16[1 + o]];
423 
424  LOADCHROMA(0);
425  PUTRGB12(dst_1, py_1, 0, 0);
426  PUTRGB12(dst_2, py_2, 0, 0 + 8);
427 
428  LOADCHROMA(1);
429  PUTRGB12(dst_2, py_2, 1, 2 + 8);
430  PUTRGB12(dst_1, py_1, 1, 2);
431 
432  LOADCHROMA(2);
433  PUTRGB12(dst_1, py_1, 2, 4);
434  PUTRGB12(dst_2, py_2, 2, 4 + 8);
435 
436  LOADCHROMA(3);
437  PUTRGB12(dst_2, py_2, 3, 6 + 8);
438  PUTRGB12(dst_1, py_1, 3, 6);
440 
441 // r, g, b, dst_1, dst_2
442 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
443  const uint8_t *d32 = dither_8x8_32[y & 7];
444  const uint8_t *d64 = dither_8x8_73[y & 7];
445 
446 #define PUTRGB8(dst, src, i, o) \
447  Y = src[2 * i]; \
448  dst[2 * i] = r[Y + d32[0 + o]] + \
449  g[Y + d32[0 + o]] + \
450  b[Y + d64[0 + o]]; \
451  Y = src[2 * i + 1]; \
452  dst[2 * i + 1] = r[Y + d32[1 + o]] + \
453  g[Y + d32[1 + o]] + \
454  b[Y + d64[1 + o]];
455 
456  LOADCHROMA(0);
457  PUTRGB8(dst_1, py_1, 0, 0);
458  PUTRGB8(dst_2, py_2, 0, 0 + 8);
459 
460  LOADCHROMA(1);
461  PUTRGB8(dst_2, py_2, 1, 2 + 8);
462  PUTRGB8(dst_1, py_1, 1, 2);
463 
464  LOADCHROMA(2);
465  PUTRGB8(dst_1, py_1, 2, 4);
466  PUTRGB8(dst_2, py_2, 2, 4 + 8);
467 
468  LOADCHROMA(3);
469  PUTRGB8(dst_2, py_2, 3, 6 + 8);
470  PUTRGB8(dst_1, py_1, 3, 6);
472 
473 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
474  const uint8_t * d64 = dither_8x8_73[y & 7];
475  const uint8_t *d128 = dither_8x8_220[y & 7];
476  int acc;
477 
478 #define PUTRGB4D(dst, src, i, o) \
479  Y = src[2 * i]; \
480  acc = r[Y + d128[0 + o]] + \
481  g[Y + d64[0 + o]] + \
482  b[Y + d128[0 + o]]; \
483  Y = src[2 * i + 1]; \
484  acc |= (r[Y + d128[1 + o]] + \
485  g[Y + d64[1 + o]] + \
486  b[Y + d128[1 + o]]) << 4; \
487  dst[i] = acc;
488 
489  LOADCHROMA(0);
490  PUTRGB4D(dst_1, py_1, 0, 0);
491  PUTRGB4D(dst_2, py_2, 0, 0 + 8);
492 
493  LOADCHROMA(1);
494  PUTRGB4D(dst_2, py_2, 1, 2 + 8);
495  PUTRGB4D(dst_1, py_1, 1, 2);
496 
497  LOADCHROMA(2);
498  PUTRGB4D(dst_1, py_1, 2, 4);
499  PUTRGB4D(dst_2, py_2, 2, 4 + 8);
500 
501  LOADCHROMA(3);
502  PUTRGB4D(dst_2, py_2, 3, 6 + 8);
503  PUTRGB4D(dst_1, py_1, 3, 6);
505 
506 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
507  const uint8_t *d64 = dither_8x8_73[y & 7];
508  const uint8_t *d128 = dither_8x8_220[y & 7];
509 
510 #define PUTRGB4DB(dst, src, i, o) \
511  Y = src[2 * i]; \
512  dst[2 * i] = r[Y + d128[0 + o]] + \
513  g[Y + d64[0 + o]] + \
514  b[Y + d128[0 + o]]; \
515  Y = src[2 * i + 1]; \
516  dst[2 * i + 1] = r[Y + d128[1 + o]] + \
517  g[Y + d64[1 + o]] + \
518  b[Y + d128[1 + o]];
519 
520  LOADCHROMA(0);
521  PUTRGB4DB(dst_1, py_1, 0, 0);
522  PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
523 
524  LOADCHROMA(1);
525  PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
526  PUTRGB4DB(dst_1, py_1, 1, 2);
527 
528  LOADCHROMA(2);
529  PUTRGB4DB(dst_1, py_1, 2, 4);
530  PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
531 
532  LOADCHROMA(3);
533  PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
534  PUTRGB4DB(dst_1, py_1, 3, 6);
536 
537 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
538  const uint8_t *d128 = dither_8x8_220[y & 7];
539  char out_1 = 0, out_2 = 0;
540  g = c->table_gU[128] + c->table_gV[128];
541 
542 #define PUTRGB1(out, src, i, o) \
543  Y = src[2 * i]; \
544  out += out + g[Y + d128[0 + o]]; \
545  Y = src[2 * i + 1]; \
546  out += out + g[Y + d128[1 + o]];
547 
548  PUTRGB1(out_1, py_1, 0, 0);
549  PUTRGB1(out_2, py_2, 0, 0 + 8);
550 
551  PUTRGB1(out_2, py_2, 1, 2 + 8);
552  PUTRGB1(out_1, py_1, 1, 2);
553 
554  PUTRGB1(out_1, py_1, 2, 4);
555  PUTRGB1(out_2, py_2, 2, 4 + 8);
556 
557  PUTRGB1(out_2, py_2, 3, 6 + 8);
558  PUTRGB1(out_1, py_1, 3, 6);
559 
560  dst_1[0] = out_1;
561  dst_2[0] = out_2;
563 
565 {
566  SwsFunc t = NULL;
567 
568  if (HAVE_MMX)
569  t = ff_yuv2rgb_init_mmx(c);
570  else if (HAVE_VIS)
571  t = ff_yuv2rgb_init_vis(c);
572  else if (HAVE_ALTIVEC)
574  else if (ARCH_BFIN)
576 
577  if (t)
578  return t;
579 
581  "No accelerated colorspace conversion found from %s to %s.\n",
582  sws_format_name(c->srcFormat), sws_format_name(c->dstFormat));
583 
584  switch (c->dstFormat) {
585  case AV_PIX_FMT_BGR48BE:
586  case AV_PIX_FMT_BGR48LE:
587  return yuv2rgb_c_bgr48;
588  case AV_PIX_FMT_RGB48BE:
589  case AV_PIX_FMT_RGB48LE:
590  return yuv2rgb_c_48;
591  case AV_PIX_FMT_ARGB:
592  case AV_PIX_FMT_ABGR:
593  if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
594  return yuva2argb_c;
595  case AV_PIX_FMT_RGBA:
596  case AV_PIX_FMT_BGRA:
597  if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
598  return yuva2rgba_c;
599  else
600  return yuv2rgb_c_32;
601  case AV_PIX_FMT_RGB24:
602  return yuv2rgb_c_24_rgb;
603  case AV_PIX_FMT_BGR24:
604  return yuv2rgb_c_24_bgr;
605  case AV_PIX_FMT_RGB565:
606  case AV_PIX_FMT_BGR565:
607  case AV_PIX_FMT_RGB555:
608  case AV_PIX_FMT_BGR555:
609  return yuv2rgb_c_16;
610  case AV_PIX_FMT_RGB444:
611  case AV_PIX_FMT_BGR444:
612  return yuv2rgb_c_12_ordered_dither;
613  case AV_PIX_FMT_RGB8:
614  case AV_PIX_FMT_BGR8:
615  return yuv2rgb_c_8_ordered_dither;
616  case AV_PIX_FMT_RGB4:
617  case AV_PIX_FMT_BGR4:
618  return yuv2rgb_c_4_ordered_dither;
621  return yuv2rgb_c_4b_ordered_dither;
623  return yuv2rgb_c_1_ordered_dither;
624  default:
625  assert(0);
626  }
627  return NULL;
628 }
629 
630 static void fill_table(uint8_t *table[256], const int elemsize,
631  const int inc, void *y_tab)
632 {
633  int i;
634  int64_t cb = 0;
635  uint8_t *y_table = y_tab;
636 
637  y_table -= elemsize * (inc >> 9);
638 
639  for (i = 0; i < 256; i++) {
640  table[i] = y_table + elemsize * (cb >> 16);
641  cb += inc;
642  }
643 }
644 
645 static void fill_gv_table(int table[256], const int elemsize, const int inc)
646 {
647  int i;
648  int64_t cb = 0;
649  int off = -(inc >> 9);
650 
651  for (i = 0; i < 256; i++) {
652  table[i] = elemsize * (off + (cb >> 16));
653  cb += inc;
654  }
655 }
656 
657 static uint16_t roundToInt16(int64_t f)
658 {
659  int r = (f + (1 << 15)) >> 16;
660 
661  if (r < -0x7FFF)
662  return 0x8000;
663  else if (r > 0x7FFF)
664  return 0x7FFF;
665  else
666  return r;
667 }
668 
669 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
670  int fullRange, int brightness,
671  int contrast, int saturation)
672 {
673  const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
675  c->dstFormat == AV_PIX_FMT_BGR24 ||
682  c->dstFormat == AV_PIX_FMT_RGB8 ||
683  c->dstFormat == AV_PIX_FMT_RGB4 ||
686  const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
687  c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
688  c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
689  c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
690  c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
691  c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE);
692  const int bpp = c->dstFormatBpp;
693  uint8_t *y_table;
694  uint16_t *y_table16;
695  uint32_t *y_table32;
696  int i, base, rbase, gbase, bbase, abase, needAlpha;
697  const int yoffs = fullRange ? 384 : 326;
698 
699  int64_t crv = inv_table[0];
700  int64_t cbu = inv_table[1];
701  int64_t cgu = -inv_table[2];
702  int64_t cgv = -inv_table[3];
703  int64_t cy = 1 << 16;
704  int64_t oy = 0;
705  int64_t yb = 0;
706 
707  if (!fullRange) {
708  cy = (cy * 255) / 219;
709  oy = 16 << 16;
710  } else {
711  crv = (crv * 224) / 255;
712  cbu = (cbu * 224) / 255;
713  cgu = (cgu * 224) / 255;
714  cgv = (cgv * 224) / 255;
715  }
716 
717  cy = (cy * contrast) >> 16;
718  crv = (crv * contrast * saturation) >> 32;
719  cbu = (cbu * contrast * saturation) >> 32;
720  cgu = (cgu * contrast * saturation) >> 32;
721  cgv = (cgv * contrast * saturation) >> 32;
722  oy -= 256 * brightness;
723 
724  c->uOffset = 0x0400040004000400LL;
725  c->vOffset = 0x0400040004000400LL;
726  c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
727  c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
728  c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
729  c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
730  c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
731  c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
732 
733  c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
734  c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
735  c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
736  c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
737  c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
738  c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
739 
740  //scale coefficients by cy
741  crv = ((crv << 16) + 0x8000) / cy;
742  cbu = ((cbu << 16) + 0x8000) / cy;
743  cgu = ((cgu << 16) + 0x8000) / cy;
744  cgv = ((cgv << 16) + 0x8000) / cy;
745 
746  av_free(c->yuvTable);
747 
748  switch (bpp) {
749  case 1:
750  c->yuvTable = av_malloc(1024);
751  y_table = c->yuvTable;
752  yb = -(384 << 16) - oy;
753  for (i = 0; i < 1024 - 110; i++) {
754  y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
755  yb += cy;
756  }
757  fill_table(c->table_gU, 1, cgu, y_table + yoffs);
758  fill_gv_table(c->table_gV, 1, cgv);
759  break;
760  case 4:
761  case 4 | 128:
762  rbase = isRgb ? 3 : 0;
763  gbase = 1;
764  bbase = isRgb ? 0 : 3;
765  c->yuvTable = av_malloc(1024 * 3);
766  y_table = c->yuvTable;
767  yb = -(384 << 16) - oy;
768  for (i = 0; i < 1024 - 110; i++) {
769  int yval = av_clip_uint8((yb + 0x8000) >> 16);
770  y_table[i + 110] = (yval >> 7) << rbase;
771  y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
772  y_table[i + 110 + 2048] = (yval >> 7) << bbase;
773  yb += cy;
774  }
775  fill_table(c->table_rV, 1, crv, y_table + yoffs);
776  fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
777  fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
778  fill_gv_table(c->table_gV, 1, cgv);
779  break;
780  case 8:
781  rbase = isRgb ? 5 : 0;
782  gbase = isRgb ? 2 : 3;
783  bbase = isRgb ? 0 : 6;
784  c->yuvTable = av_malloc(1024 * 3);
785  y_table = c->yuvTable;
786  yb = -(384 << 16) - oy;
787  for (i = 0; i < 1024 - 38; i++) {
788  int yval = av_clip_uint8((yb + 0x8000) >> 16);
789  y_table[i + 16] = ((yval + 18) / 36) << rbase;
790  y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
791  y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
792  yb += cy;
793  }
794  fill_table(c->table_rV, 1, crv, y_table + yoffs);
795  fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
796  fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
797  fill_gv_table(c->table_gV, 1, cgv);
798  break;
799  case 12:
800  rbase = isRgb ? 8 : 0;
801  gbase = 4;
802  bbase = isRgb ? 0 : 8;
803  c->yuvTable = av_malloc(1024 * 3 * 2);
804  y_table16 = c->yuvTable;
805  yb = -(384 << 16) - oy;
806  for (i = 0; i < 1024; i++) {
807  uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
808  y_table16[i] = (yval >> 4) << rbase;
809  y_table16[i + 1024] = (yval >> 4) << gbase;
810  y_table16[i + 2048] = (yval >> 4) << bbase;
811  yb += cy;
812  }
813  if (isNotNe)
814  for (i = 0; i < 1024 * 3; i++)
815  y_table16[i] = av_bswap16(y_table16[i]);
816  fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
817  fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
818  fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
819  fill_gv_table(c->table_gV, 2, cgv);
820  break;
821  case 15:
822  case 16:
823  rbase = isRgb ? bpp - 5 : 0;
824  gbase = 5;
825  bbase = isRgb ? 0 : (bpp - 5);
826  c->yuvTable = av_malloc(1024 * 3 * 2);
827  y_table16 = c->yuvTable;
828  yb = -(384 << 16) - oy;
829  for (i = 0; i < 1024; i++) {
830  uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
831  y_table16[i] = (yval >> 3) << rbase;
832  y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
833  y_table16[i + 2048] = (yval >> 3) << bbase;
834  yb += cy;
835  }
836  if (isNotNe)
837  for (i = 0; i < 1024 * 3; i++)
838  y_table16[i] = av_bswap16(y_table16[i]);
839  fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
840  fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
841  fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
842  fill_gv_table(c->table_gV, 2, cgv);
843  break;
844  case 24:
845  case 48:
846  c->yuvTable = av_malloc(1024);
847  y_table = c->yuvTable;
848  yb = -(384 << 16) - oy;
849  for (i = 0; i < 1024; i++) {
850  y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
851  yb += cy;
852  }
853  fill_table(c->table_rV, 1, crv, y_table + yoffs);
854  fill_table(c->table_gU, 1, cgu, y_table + yoffs);
855  fill_table(c->table_bU, 1, cbu, y_table + yoffs);
856  fill_gv_table(c->table_gV, 1, cgv);
857  break;
858  case 32:
859  base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
860  c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
861  rbase = base + (isRgb ? 16 : 0);
862  gbase = base + 8;
863  bbase = base + (isRgb ? 0 : 16);
864  needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
865  if (!needAlpha)
866  abase = (base + 24) & 31;
867  c->yuvTable = av_malloc(1024 * 3 * 4);
868  y_table32 = c->yuvTable;
869  yb = -(384 << 16) - oy;
870  for (i = 0; i < 1024; i++) {
871  unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
872  y_table32[i] = (yval << rbase) +
873  (needAlpha ? 0 : (255u << abase));
874  y_table32[i + 1024] = yval << gbase;
875  y_table32[i + 2048] = yval << bbase;
876  yb += cy;
877  }
878  fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
879  fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
880  fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
881  fill_gv_table(c->table_gV, 4, cgv);
882  break;
883  default:
884  c->yuvTable = NULL;
885  av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
886  return -1;
887  }
888  return 0;
889 }
uint64_t vrCoeff
dst_1[0]
Definition: yuv2rgb.c:560
const char * sws_format_name(enum AVPixelFormat format)
Definition: utils.c:189
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
char out_2
Definition: yuv2rgb.c:539
const int32_t ff_yuv2rgb_coeffs[8][4]
Definition: yuv2rgb.c:43
#define SWS_CS_DEFAULT
Definition: swscale.h:103
pa_1
Definition: yuv2rgb.c:277
#define HAVE_ALTIVEC
Definition: config.h:54
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
packed RGB 1:2:1 bitstream, 4bpp, (msb)1B 2G 1R(lsb), a byte contains two pixels, the first pixel in ...
Definition: pixfmt.h:85
av_cold SwsFunc ff_yuv2rgb_init_vis(SwsContext *c)
Definition: yuv2rgb_vis.c:188
int acc
Definition: yuv2rgb.c:476
static void fill_table(uint8_t *table[256], const int elemsize, const int inc, void *y_tab)
Definition: yuv2rgb.c:630
#define ARCH_BFIN
Definition: config.h:18
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:114
#define av_bswap16
Definition: bswap.h:31
int dstFormatBpp
Number of bits per pixel of the destination pixel format.
packed RGB 4:4:4, 16bpp, (msb)4A 4R 4G 4B(lsb), big-endian, most significant bits to 0 ...
Definition: pixfmt.h:135
#define CONFIG_SWSCALE_ALPHA
Definition: config.h:318
#define PUTBGR24(dst, src, i)
Definition: yuv2rgb.c:84
const uint8_t dither_8x8_32[8][8]
Definition: output.c:56
packed RGB 1:2:1 bitstream, 4bpp, (msb)1R 2G 1B(lsb), a byte contains two pixels, the first pixel in ...
Definition: pixfmt.h:88
#define HAVE_VIS
Definition: config.h:56
uint64_t ubCoeff
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:112
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:86
#define PUTRGB(dst, src, i)
Definition: yuv2rgb.c:68
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
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:109
uint8_t * table_bU[256]
packed RGB 4:4:4, 16bpp, (msb)4A 4R 4G 4B(lsb), little-endian, most significant bits to 0 ...
Definition: pixfmt.h:134
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:111
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
av_cold SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c)
uint64_t yOffset
external api for the swscale stuff
enum AVPixelFormat dstFormat
Destination pixel format.
const uint8_t dither_8x8_73[8][8]
Definition: output.c:67
#define PUTRGBA(dst, ysrc, asrc, i, s)
Definition: yuv2rgb.c:94
#define isALPHA(x)
Definition: swscale-test.c:47
static float t
#define r
Definition: input.c:51
av_cold SwsFunc ff_yuv2rgb_init_mmx(SwsContext *c)
Definition: yuv2rgb.c:72
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
#define PUTBGR48(dst, src, i)
Definition: yuv2rgb.c:110
const uint8_t * d64
Definition: yuv2rgb.c:444
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
g
Definition: yuv2rgb.c:540
#define ENDYUV2RGBFUNC()
Definition: yuv2rgb.c:161
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
uint64_t ugCoeff
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
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:140
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
#define YUV2RGBFUNC(func_name, dst_type, alpha)
Definition: yuv2rgb.c:120
int off
Definition: dsputil_bfin.c:28
static uint16_t roundToInt16(int64_t f)
Definition: yuv2rgb.c:657
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:89
uint64_t vgCoeff
uint64_t uOffset
int32_t
const uint8_t dither_8x8_220[8][8]
Definition: output.c:79
int table_gV[256]
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
#define HAVE_MMX
Definition: config.h:46
#define PUTRGB4D(dst, src, i, o)
Definition: yuv2rgb.c:478
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:139
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:669
const uint8_t * d16
Definition: yuv2rgb.c:412
NULL
Definition: eval.c:52
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:84
SwsFunc ff_yuv2rgb_get_func_ptr_bfin(SwsContext *c)
Definition: yuv2rgb_bfin.c:170
#define CLOSEYUV2RGBFUNC(dst_delta)
Definition: yuv2rgb.c:167
SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
Definition: yuv2rgb.c:564
#define PUTRGB12(dst, src, i, o)
Definition: yuv2rgb.c:414
uint8_t * table_gU[256]
int(* SwsFunc)(struct SwsContext *context, const uint8_t *src[], int srcStride[], int srcSliceY, int srcSliceH, uint8_t *dst[], int dstStride[])
byte swapping routines
#define ENDYUV2RGBLINE(dst_delta, ss)
Definition: yuv2rgb.c:150
pa_2
Definition: yuv2rgb.c:278
const int * sws_getCoefficients(int colorspace)
Return a pointer to yuv<->rgb coefficients for the given colorspace suitable for sws_setColorspaceDet...
Definition: yuv2rgb.c:54
const uint8_t dither_4x4_16[4][8]
Definition: output.c:49
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), big-endian, most significant bit to 0 ...
Definition: pixfmt.h:113
#define PUTRGB48(dst, src, i)
Definition: yuv2rgb.c:100
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
#define PUTRGB4DB(dst, src, i, o)
Definition: yuv2rgb.c:510
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:108
#define PUTRGB8(dst, src, i, o)
Definition: yuv2rgb.c:446
enum AVPixelFormat srcFormat
Source pixel format.
const uint8_t * d128
Definition: yuv2rgb.c:475
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:87
#define PUTRGB1(out, src, i, o)
Definition: yuv2rgb.c:542
static void fill_gv_table(int table[256], const int elemsize, const int inc)
Definition: yuv2rgb.c:645
char out_1
Definition: yuv2rgb.c:539
uint64_t yCoeff
#define LOADCHROMA(i)
Definition: yuv2rgb.c:61
dst_2[0]
Definition: yuv2rgb.c:561
const uint8_t * d32
Definition: yuv2rgb.c:443
enum AVColorSpace colorspace
Definition: dirac.c:99
uint8_t * table_rV[256]
uint64_t vOffset
#define PUTRGB24(dst, src, i)
Definition: yuv2rgb.c:74