• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
sws_getCoefficients(int colorspace)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 PUTRGB24(dst, src, i)                       \
84     Y              = src[2 * i];                    \
85     dst[6 * i + 0] = r[Y];                          \
86     dst[6 * i + 1] = g[Y];                          \
87     dst[6 * i + 2] = b[Y];                          \
88     Y              = src[2 * i + 1];                \
89     dst[6 * i + 3] = r[Y];                          \
90     dst[6 * i + 4] = g[Y];                          \
91     dst[6 * i + 5] = b[Y];
92 
93 #define PUTBGR24(dst, src, i)                       \
94     Y              = src[2 * i];                    \
95     dst[6 * i + 0] = b[Y];                          \
96     dst[6 * i + 1] = g[Y];                          \
97     dst[6 * i + 2] = r[Y];                          \
98     Y              = src[2 * i + 1];                \
99     dst[6 * i + 3] = b[Y];                          \
100     dst[6 * i + 4] = g[Y];                          \
101     dst[6 * i + 5] = r[Y];
102 
103 #define PUTRGBA(dst, ysrc, asrc, i, s)                                  \
104     Y              = ysrc[2 * i];                                       \
105     dst[2 * i]     = r[Y] + g[Y] + b[Y] + (asrc[2 * i]     << s);       \
106     Y              = ysrc[2 * i + 1];                                   \
107     dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
108 
109 #define PUTRGB48(dst, src, i)                       \
110     Y                = src[ 2 * i];                 \
111     dst[12 * i +  0] = dst[12 * i +  1] = r[Y];     \
112     dst[12 * i +  2] = dst[12 * i +  3] = g[Y];     \
113     dst[12 * i +  4] = dst[12 * i +  5] = b[Y];     \
114     Y                = src[ 2 * i + 1];             \
115     dst[12 * i +  6] = dst[12 * i +  7] = r[Y];     \
116     dst[12 * i +  8] = dst[12 * i +  9] = g[Y];     \
117     dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
118 
119 #define PUTBGR48(dst, src, i)                       \
120     Y                = src[2 * i];                  \
121     dst[12 * i +  0] = dst[12 * i +  1] = b[Y];     \
122     dst[12 * i +  2] = dst[12 * i +  3] = g[Y];     \
123     dst[12 * i +  4] = dst[12 * i +  5] = r[Y];     \
124     Y                = src[2  * i +  1];            \
125     dst[12 * i +  6] = dst[12 * i +  7] = b[Y];     \
126     dst[12 * i +  8] = dst[12 * i +  9] = g[Y];     \
127     dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
128 
129 #define YUV2RGBFUNC(func_name, dst_type, alpha)                             \
130     static int func_name(SwsContext *c, const uint8_t *src[],               \
131                          int srcStride[], int srcSliceY, int srcSliceH,     \
132                          uint8_t *dst[], int dstStride[])                   \
133     {                                                                       \
134         int y;                                                              \
135                                                                             \
136         if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) {                    \
137             srcStride[1] *= 2;                                              \
138             srcStride[2] *= 2;                                              \
139         }                                                                   \
140         for (y = 0; y < srcSliceH; y += 2) {                                \
141             int yd = y + srcSliceY;                                         \
142             dst_type *dst_1 =                                               \
143                 (dst_type *)(dst[0] + (yd)     * dstStride[0]);             \
144             dst_type *dst_2 =                                               \
145                 (dst_type *)(dst[0] + (yd + 1) * dstStride[0]);             \
146             dst_type av_unused *r, *g, *b;                                  \
147             const uint8_t *py_1 = src[0] +  y       * srcStride[0];         \
148             const uint8_t *py_2 = py_1   +            srcStride[0];         \
149             const uint8_t *pu   = src[1] + (y >> 1) * srcStride[1];         \
150             const uint8_t *pv   = src[2] + (y >> 1) * srcStride[2];         \
151             const uint8_t av_unused *pa_1, *pa_2;                           \
152             unsigned int h_size = c->dstW >> 3;                             \
153             if (alpha) {                                                    \
154                 pa_1 = src[3] + y * srcStride[3];                           \
155                 pa_2 = pa_1   +     srcStride[3];                           \
156             }                                                               \
157             while (h_size--) {                                              \
158                 int av_unused U, V, Y;                                      \
159 
160 #define ENDYUV2RGBLINE(dst_delta, ss)               \
161     pu    += 4 >> ss;                               \
162     pv    += 4 >> ss;                               \
163     py_1  += 8 >> ss;                               \
164     py_2  += 8 >> ss;                               \
165     dst_1 += dst_delta >> ss;                       \
166     dst_2 += dst_delta >> ss;                       \
167     }                                               \
168     if (c->dstW & (4 >> ss)) {                      \
169         int av_unused Y, U, V;                      \
170 
171 #define ENDYUV2RGBFUNC()                            \
172             }                                       \
173         }                                           \
174         return srcSliceH;                           \
175     }
176 
177 #define CLOSEYUV2RGBFUNC(dst_delta)                 \
178     ENDYUV2RGBLINE(dst_delta, 0)                    \
179     ENDYUV2RGBFUNC()
180 
181 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
182     LOADCHROMA(0);
183     PUTRGB48(dst_1, py_1, 0);
184     PUTRGB48(dst_2, py_2, 0);
185 
186     LOADCHROMA(1);
187     PUTRGB48(dst_2, py_2, 1);
188     PUTRGB48(dst_1, py_1, 1);
189 
190     LOADCHROMA(2);
191     PUTRGB48(dst_1, py_1, 2);
192     PUTRGB48(dst_2, py_2, 2);
193 
194     LOADCHROMA(3);
195     PUTRGB48(dst_2, py_2, 3);
196     PUTRGB48(dst_1, py_1, 3);
197 ENDYUV2RGBLINE(48, 0)
198     LOADCHROMA(0);
199     PUTRGB48(dst_1, py_1, 0);
200     PUTRGB48(dst_2, py_2, 0);
201 
202     LOADCHROMA(1);
203     PUTRGB48(dst_2, py_2, 1);
204     PUTRGB48(dst_1, py_1, 1);
205 ENDYUV2RGBLINE(48, 1)
206     LOADCHROMA(0);
207     PUTRGB48(dst_1, py_1, 0);
208     PUTRGB48(dst_2, py_2, 0);
209 ENDYUV2RGBFUNC()
210 
211 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
212     LOADCHROMA(0);
213     PUTBGR48(dst_1, py_1, 0);
214     PUTBGR48(dst_2, py_2, 0);
215 
216     LOADCHROMA(1);
217     PUTBGR48(dst_2, py_2, 1);
218     PUTBGR48(dst_1, py_1, 1);
219 
220     LOADCHROMA(2);
221     PUTBGR48(dst_1, py_1, 2);
222     PUTBGR48(dst_2, py_2, 2);
223 
224     LOADCHROMA(3);
225     PUTBGR48(dst_2, py_2, 3);
226     PUTBGR48(dst_1, py_1, 3);
227 ENDYUV2RGBLINE(48, 0)
228     LOADCHROMA(0);
229     PUTBGR48(dst_1, py_1, 0);
230     PUTBGR48(dst_2, py_2, 0);
231 
232     LOADCHROMA(1);
233     PUTBGR48(dst_2, py_2, 1);
234     PUTBGR48(dst_1, py_1, 1);
235 ENDYUV2RGBLINE(48, 1)
236     LOADCHROMA(0);
237     PUTBGR48(dst_1, py_1, 0);
238     PUTBGR48(dst_2, py_2, 0);
239 ENDYUV2RGBFUNC()
240 
241 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
242     LOADCHROMA(0);
243     PUTRGB(dst_1, py_1, 0);
244     PUTRGB(dst_2, py_2, 0);
245 
246     LOADCHROMA(1);
247     PUTRGB(dst_2, py_2, 1);
248     PUTRGB(dst_1, py_1, 1);
249 
250     LOADCHROMA(2);
251     PUTRGB(dst_1, py_1, 2);
252     PUTRGB(dst_2, py_2, 2);
253 
254     LOADCHROMA(3);
255     PUTRGB(dst_2, py_2, 3);
256     PUTRGB(dst_1, py_1, 3);
257 ENDYUV2RGBLINE(8, 0)
258     LOADCHROMA(0);
259     PUTRGB(dst_1, py_1, 0);
260     PUTRGB(dst_2, py_2, 0);
261 
262     LOADCHROMA(1);
263     PUTRGB(dst_2, py_2, 1);
264     PUTRGB(dst_1, py_1, 1);
265 ENDYUV2RGBLINE(8, 1)
266     LOADCHROMA(0);
267     PUTRGB(dst_1, py_1, 0);
268     PUTRGB(dst_2, py_2, 0);
269 ENDYUV2RGBFUNC()
270 
271 #if HAVE_BIGENDIAN
272 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
273 #else
274 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
275 #endif
276     LOADCHROMA(0);
277     PUTRGBA(dst_1, py_1, pa_1, 0, 24);
278     PUTRGBA(dst_2, py_2, pa_2, 0, 24);
279 
280     LOADCHROMA(1);
281     PUTRGBA(dst_2, py_2, pa_2, 1, 24);
282     PUTRGBA(dst_1, py_1, pa_1, 1, 24);
283 
284     LOADCHROMA(2);
285     PUTRGBA(dst_1, py_1, pa_1, 2, 24);
286     PUTRGBA(dst_2, py_2, pa_2, 2, 24);
287 
288     LOADCHROMA(3);
289     PUTRGBA(dst_2, py_2, pa_2, 3, 24);
290     PUTRGBA(dst_1, py_1, pa_1, 3, 24);
291     pa_1 += 8;
292     pa_2 += 8;
293 ENDYUV2RGBLINE(8, 0)
294     LOADCHROMA(0);
295     PUTRGBA(dst_1, py_1, pa_1, 0, 24);
296     PUTRGBA(dst_2, py_2, pa_2, 0, 24);
297 
298     LOADCHROMA(1);
299     PUTRGBA(dst_2, py_2, pa_2, 1, 24);
300     PUTRGBA(dst_1, py_1, pa_1, 1, 24);
301     pa_1 += 4;
302     pa_2 += 4;
303 ENDYUV2RGBLINE(8, 1)
304     LOADCHROMA(0);
305     PUTRGBA(dst_1, py_1, pa_1, 0, 24);
306     PUTRGBA(dst_2, py_2, pa_2, 0, 24);
307 ENDYUV2RGBFUNC()
308 
309 #if HAVE_BIGENDIAN
310 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
311 #else
312 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
313 #endif
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 
322     LOADCHROMA(2);
323     PUTRGBA(dst_1, py_1, pa_1, 2, 0);
324     PUTRGBA(dst_2, py_2, pa_2, 2, 0);
325 
326     LOADCHROMA(3);
327     PUTRGBA(dst_2, py_2, pa_2, 3, 0);
328     PUTRGBA(dst_1, py_1, pa_1, 3, 0);
329     pa_1 += 8;
330     pa_2 += 8;
331 ENDYUV2RGBLINE(8, 0)
332     LOADCHROMA(0);
333     PUTRGBA(dst_1, py_1, pa_1, 0, 0);
334     PUTRGBA(dst_2, py_2, pa_2, 0, 0);
335 
336     LOADCHROMA(1);
337     PUTRGBA(dst_2, py_2, pa_2, 1, 0);
338     PUTRGBA(dst_1, py_1, pa_1, 1, 0);
339     pa_1 += 4;
340     pa_2 += 4;
341 ENDYUV2RGBLINE(8, 1)
342     LOADCHROMA(0);
343     PUTRGBA(dst_1, py_1, pa_1, 0, 0);
344     PUTRGBA(dst_2, py_2, pa_2, 0, 0);
345 ENDYUV2RGBFUNC()
346 
347 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
348     LOADCHROMA(0);
349     PUTRGB24(dst_1, py_1, 0);
350     PUTRGB24(dst_2, py_2, 0);
351 
352     LOADCHROMA(1);
353     PUTRGB24(dst_2, py_2, 1);
354     PUTRGB24(dst_1, py_1, 1);
355 
356     LOADCHROMA(2);
357     PUTRGB24(dst_1, py_1, 2);
358     PUTRGB24(dst_2, py_2, 2);
359 
360     LOADCHROMA(3);
361     PUTRGB24(dst_2, py_2, 3);
362     PUTRGB24(dst_1, py_1, 3);
363 ENDYUV2RGBLINE(24, 0)
364     LOADCHROMA(0);
365     PUTRGB24(dst_1, py_1, 0);
366     PUTRGB24(dst_2, py_2, 0);
367 
368     LOADCHROMA(1);
369     PUTRGB24(dst_2, py_2, 1);
370     PUTRGB24(dst_1, py_1, 1);
371 ENDYUV2RGBLINE(24, 1)
372     LOADCHROMA(0);
373     PUTRGB24(dst_1, py_1, 0);
374     PUTRGB24(dst_2, py_2, 0);
375 ENDYUV2RGBFUNC()
376 
377 // only trivial mods from yuv2rgb_c_24_rgb
378 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
379     LOADCHROMA(0);
380     PUTBGR24(dst_1, py_1, 0);
381     PUTBGR24(dst_2, py_2, 0);
382 
383     LOADCHROMA(1);
384     PUTBGR24(dst_2, py_2, 1);
385     PUTBGR24(dst_1, py_1, 1);
386 
387     LOADCHROMA(2);
388     PUTBGR24(dst_1, py_1, 2);
389     PUTBGR24(dst_2, py_2, 2);
390 
391     LOADCHROMA(3);
392     PUTBGR24(dst_2, py_2, 3);
393     PUTBGR24(dst_1, py_1, 3);
394 ENDYUV2RGBLINE(24, 0)
395     LOADCHROMA(0);
396     PUTBGR24(dst_1, py_1, 0);
397     PUTBGR24(dst_2, py_2, 0);
398 
399     LOADCHROMA(1);
400     PUTBGR24(dst_2, py_2, 1);
401     PUTBGR24(dst_1, py_1, 1);
402 ENDYUV2RGBLINE(24, 1)
403     LOADCHROMA(0);
404     PUTBGR24(dst_1, py_1, 0);
405     PUTBGR24(dst_2, py_2, 0);
406 ENDYUV2RGBFUNC()
407 
408 YUV2RGBFUNC(yuv2rgb_c_16_ordered_dither, uint16_t, 0)
409     const uint8_t *d16 = ff_dither_2x2_8[y & 1];
410     const uint8_t *e16 = ff_dither_2x2_4[y & 1];
411     const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
412 
413 #define PUTRGB16(dst, src, i, o)                    \
414     Y              = src[2 * i];                    \
415     dst[2 * i]     = r[Y + d16[0 + o]] +            \
416                      g[Y + e16[0 + o]] +            \
417                      b[Y + f16[0 + o]];             \
418     Y              = src[2 * i + 1];                \
419     dst[2 * i + 1] = r[Y + d16[1 + o]] +            \
420                      g[Y + e16[1 + o]] +            \
421                      b[Y + f16[1 + o]];
422     LOADCHROMA(0);
423     PUTRGB16(dst_1, py_1, 0, 0);
424     PUTRGB16(dst_2, py_2, 0, 0 + 8);
425 
426     LOADCHROMA(1);
427     PUTRGB16(dst_2, py_2, 1, 2 + 8);
428     PUTRGB16(dst_1, py_1, 1, 2);
429 
430     LOADCHROMA(2);
431     PUTRGB16(dst_1, py_1, 2, 4);
432     PUTRGB16(dst_2, py_2, 2, 4 + 8);
433 
434     LOADCHROMA(3);
435     PUTRGB16(dst_2, py_2, 3, 6 + 8);
436     PUTRGB16(dst_1, py_1, 3, 6);
437 CLOSEYUV2RGBFUNC(8)
438 
439 YUV2RGBFUNC(yuv2rgb_c_15_ordered_dither, uint16_t, 0)
440     const uint8_t *d16 = ff_dither_2x2_8[y & 1];
441     const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
442 
443 #define PUTRGB15(dst, src, i, o)                    \
444     Y              = src[2 * i];                    \
445     dst[2 * i]     = r[Y + d16[0 + o]] +            \
446                      g[Y + d16[1 + o]] +            \
447                      b[Y + e16[0 + o]];             \
448     Y              = src[2 * i + 1];                \
449     dst[2 * i + 1] = r[Y + d16[1 + o]] +            \
450                      g[Y + d16[0 + o]] +            \
451                      b[Y + e16[1 + o]];
452     LOADCHROMA(0);
453     PUTRGB15(dst_1, py_1, 0, 0);
454     PUTRGB15(dst_2, py_2, 0, 0 + 8);
455 
456     LOADCHROMA(1);
457     PUTRGB15(dst_2, py_2, 1, 2 + 8);
458     PUTRGB15(dst_1, py_1, 1, 2);
459 
460     LOADCHROMA(2);
461     PUTRGB15(dst_1, py_1, 2, 4);
462     PUTRGB15(dst_2, py_2, 2, 4 + 8);
463 
464     LOADCHROMA(3);
465     PUTRGB15(dst_2, py_2, 3, 6 + 8);
466     PUTRGB15(dst_1, py_1, 3, 6);
467 CLOSEYUV2RGBFUNC(8)
468 
469 // r, g, b, dst_1, dst_2
470 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
471     const uint8_t *d16 = ff_dither_4x4_16[y & 3];
472 
473 #define PUTRGB12(dst, src, i, o)                    \
474     Y              = src[2 * i];                    \
475     dst[2 * i]     = r[Y + d16[0 + o]] +            \
476                      g[Y + d16[0 + o]] +            \
477                      b[Y + d16[0 + o]];             \
478     Y              = src[2 * i + 1];                \
479     dst[2 * i + 1] = r[Y + d16[1 + o]] +            \
480                      g[Y + d16[1 + o]] +            \
481                      b[Y + d16[1 + o]];
482 
483     LOADCHROMA(0);
484     PUTRGB12(dst_1, py_1, 0, 0);
485     PUTRGB12(dst_2, py_2, 0, 0 + 8);
486 
487     LOADCHROMA(1);
488     PUTRGB12(dst_2, py_2, 1, 2 + 8);
489     PUTRGB12(dst_1, py_1, 1, 2);
490 
491     LOADCHROMA(2);
492     PUTRGB12(dst_1, py_1, 2, 4);
493     PUTRGB12(dst_2, py_2, 2, 4 + 8);
494 
495     LOADCHROMA(3);
496     PUTRGB12(dst_2, py_2, 3, 6 + 8);
497     PUTRGB12(dst_1, py_1, 3, 6);
498 CLOSEYUV2RGBFUNC(8)
499 
500 // r, g, b, dst_1, dst_2
501 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
502     const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
503     const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
504 
505 #define PUTRGB8(dst, src, i, o)                     \
506     Y              = src[2 * i];                    \
507     dst[2 * i]     = r[Y + d32[0 + o]] +            \
508                      g[Y + d32[0 + o]] +            \
509                      b[Y + d64[0 + o]];             \
510     Y              = src[2 * i + 1];                \
511     dst[2 * i + 1] = r[Y + d32[1 + o]] +            \
512                      g[Y + d32[1 + o]] +            \
513                      b[Y + d64[1 + o]];
514 
515     LOADCHROMA(0);
516     PUTRGB8(dst_1, py_1, 0, 0);
517     PUTRGB8(dst_2, py_2, 0, 0 + 8);
518 
519     LOADCHROMA(1);
520     PUTRGB8(dst_2, py_2, 1, 2 + 8);
521     PUTRGB8(dst_1, py_1, 1, 2);
522 
523     LOADCHROMA(2);
524     PUTRGB8(dst_1, py_1, 2, 4);
525     PUTRGB8(dst_2, py_2, 2, 4 + 8);
526 
527     LOADCHROMA(3);
528     PUTRGB8(dst_2, py_2, 3, 6 + 8);
529     PUTRGB8(dst_1, py_1, 3, 6);
530 
531 ENDYUV2RGBLINE(8, 0)
532     const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
533     const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
534     LOADCHROMA(0);
535     PUTRGB8(dst_1, py_1, 0, 0);
536     PUTRGB8(dst_2, py_2, 0, 0 + 8);
537 
538     LOADCHROMA(1);
539     PUTRGB8(dst_2, py_2, 1, 2 + 8);
540     PUTRGB8(dst_1, py_1, 1, 2);
541 
542 ENDYUV2RGBLINE(8, 1)
543     const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
544     const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
545     LOADCHROMA(0);
546     PUTRGB8(dst_1, py_1, 0, 0);
547     PUTRGB8(dst_2, py_2, 0, 0 + 8);
548 
549 ENDYUV2RGBFUNC()
550 
551 
552 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
553     const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
554     const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
555     int acc;
556 
557 #define PUTRGB4D(dst, src, i, o)                    \
558     Y      = src[2 * i];                            \
559     acc    = r[Y + d128[0 + o]] +                   \
560              g[Y +  d64[0 + o]] +                   \
561              b[Y + d128[0 + o]];                    \
562     Y      = src[2 * i + 1];                        \
563     acc   |= (r[Y + d128[1 + o]] +                  \
564               g[Y +  d64[1 + o]] +                  \
565               b[Y + d128[1 + o]]) << 4;             \
566     dst[i] = acc;
567 
568     LOADCHROMA(0);
569     PUTRGB4D(dst_1, py_1, 0, 0);
570     PUTRGB4D(dst_2, py_2, 0, 0 + 8);
571 
572     LOADCHROMA(1);
573     PUTRGB4D(dst_2, py_2, 1, 2 + 8);
574     PUTRGB4D(dst_1, py_1, 1, 2);
575 
576     LOADCHROMA(2);
577     PUTRGB4D(dst_1, py_1, 2, 4);
578     PUTRGB4D(dst_2, py_2, 2, 4 + 8);
579 
580     LOADCHROMA(3);
581     PUTRGB4D(dst_2, py_2, 3, 6 + 8);
582     PUTRGB4D(dst_1, py_1, 3, 6);
583 
584 ENDYUV2RGBLINE(4, 0)
585     const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
586     const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
587     int acc;
588     LOADCHROMA(0);
589     PUTRGB4D(dst_1, py_1, 0, 0);
590     PUTRGB4D(dst_2, py_2, 0, 0 + 8);
591 
592     LOADCHROMA(1);
593     PUTRGB4D(dst_2, py_2, 1, 2 + 8);
594     PUTRGB4D(dst_1, py_1, 1, 2);
595 
596 ENDYUV2RGBLINE(4, 1)
597     const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
598     const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
599     int acc;
600     LOADCHROMA(0);
601     PUTRGB4D(dst_1, py_1, 0, 0);
602     PUTRGB4D(dst_2, py_2, 0, 0 + 8);
603 ENDYUV2RGBFUNC()
604 
605 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
606     const uint8_t *d64  = ff_dither_8x8_73[yd & 7];
607     const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
608 
609 #define PUTRGB4DB(dst, src, i, o)                   \
610     Y              = src[2 * i];                    \
611     dst[2 * i]     = r[Y + d128[0 + o]] +           \
612                      g[Y +  d64[0 + o]] +           \
613                      b[Y + d128[0 + o]];            \
614     Y              = src[2 * i + 1];                \
615     dst[2 * i + 1] = r[Y + d128[1 + o]] +           \
616                      g[Y +  d64[1 + o]] +           \
617                      b[Y + d128[1 + o]];
618 
619     LOADCHROMA(0);
620     PUTRGB4DB(dst_1, py_1, 0, 0);
621     PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
622 
623     LOADCHROMA(1);
624     PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
625     PUTRGB4DB(dst_1, py_1, 1, 2);
626 
627     LOADCHROMA(2);
628     PUTRGB4DB(dst_1, py_1, 2, 4);
629     PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
630 
631     LOADCHROMA(3);
632     PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
633     PUTRGB4DB(dst_1, py_1, 3, 6);
634 ENDYUV2RGBLINE(8, 0)
635     const uint8_t *d64  = ff_dither_8x8_73[yd & 7];
636     const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
637     LOADCHROMA(0);
638     PUTRGB4DB(dst_1, py_1, 0, 0);
639     PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
640 
641     LOADCHROMA(1);
642     PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
643     PUTRGB4DB(dst_1, py_1, 1, 2);
644 ENDYUV2RGBLINE(8, 1)
645     const uint8_t *d64  = ff_dither_8x8_73[yd & 7];
646     const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
647     LOADCHROMA(0);
648     PUTRGB4DB(dst_1, py_1, 0, 0);
649     PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
650 ENDYUV2RGBFUNC()
651 
652 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
653     const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
654     char out_1 = 0, out_2 = 0;
655     g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
656 
657 #define PUTRGB1(out, src, i, o)                     \
658     Y    = src[2 * i];                              \
659     out += out + g[Y + d128[0 + o]];                \
660     Y    = src[2 * i + 1];                          \
661     out += out + g[Y + d128[1 + o]];
662 
663     PUTRGB1(out_1, py_1, 0, 0);
664     PUTRGB1(out_2, py_2, 0, 0 + 8);
665 
666     PUTRGB1(out_2, py_2, 1, 2 + 8);
667     PUTRGB1(out_1, py_1, 1, 2);
668 
669     PUTRGB1(out_1, py_1, 2, 4);
670     PUTRGB1(out_2, py_2, 2, 4 + 8);
671 
672     PUTRGB1(out_2, py_2, 3, 6 + 8);
673     PUTRGB1(out_1, py_1, 3, 6);
674 
675     dst_1[0] = out_1;
676     dst_2[0] = out_2;
677 CLOSEYUV2RGBFUNC(1)
678 
ff_yuv2rgb_get_func_ptr(SwsContext * c)679 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
680 {
681     SwsFunc t = NULL;
682 
683     if (ARCH_PPC)
684         t = ff_yuv2rgb_init_ppc(c);
685     if (ARCH_X86)
686         t = ff_yuv2rgb_init_x86(c);
687 
688     if (t)
689         return t;
690 
691     av_log(c, AV_LOG_WARNING,
692            "No accelerated colorspace conversion found from %s to %s.\n",
693            av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
694 
695     switch (c->dstFormat) {
696     case AV_PIX_FMT_BGR48BE:
697     case AV_PIX_FMT_BGR48LE:
698         return yuv2rgb_c_bgr48;
699     case AV_PIX_FMT_RGB48BE:
700     case AV_PIX_FMT_RGB48LE:
701         return yuv2rgb_c_48;
702     case AV_PIX_FMT_ARGB:
703     case AV_PIX_FMT_ABGR:
704         if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat))
705             return yuva2argb_c;
706     case AV_PIX_FMT_RGBA:
707     case AV_PIX_FMT_BGRA:
708         return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
709     case AV_PIX_FMT_RGB24:
710         return yuv2rgb_c_24_rgb;
711     case AV_PIX_FMT_BGR24:
712         return yuv2rgb_c_24_bgr;
713     case AV_PIX_FMT_RGB565:
714     case AV_PIX_FMT_BGR565:
715         return yuv2rgb_c_16_ordered_dither;
716     case AV_PIX_FMT_RGB555:
717     case AV_PIX_FMT_BGR555:
718         return yuv2rgb_c_15_ordered_dither;
719     case AV_PIX_FMT_RGB444:
720     case AV_PIX_FMT_BGR444:
721         return yuv2rgb_c_12_ordered_dither;
722     case AV_PIX_FMT_RGB8:
723     case AV_PIX_FMT_BGR8:
724         return yuv2rgb_c_8_ordered_dither;
725     case AV_PIX_FMT_RGB4:
726     case AV_PIX_FMT_BGR4:
727         return yuv2rgb_c_4_ordered_dither;
728     case AV_PIX_FMT_RGB4_BYTE:
729     case AV_PIX_FMT_BGR4_BYTE:
730         return yuv2rgb_c_4b_ordered_dither;
731     case AV_PIX_FMT_MONOBLACK:
732         return yuv2rgb_c_1_ordered_dither;
733     }
734     return NULL;
735 }
736 
fill_table(uint8_t * table[256+2* YUVRGB_TABLE_HEADROOM],const int elemsize,const int64_t inc,void * y_tab)737 static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
738                        const int64_t inc, void *y_tab)
739 {
740     int i;
741     uint8_t *y_table = y_tab;
742 
743     y_table -= elemsize * (inc >> 9);
744 
745     for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
746         int64_t cb = av_clip_uint8(i-YUVRGB_TABLE_HEADROOM)*inc;
747         table[i] = y_table + elemsize * (cb >> 16);
748     }
749 }
750 
fill_gv_table(int table[256+2* YUVRGB_TABLE_HEADROOM],const int elemsize,const int64_t inc)751 static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int64_t inc)
752 {
753     int i;
754     int off    = -(inc >> 9);
755 
756     for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
757         int64_t cb = av_clip_uint8(i-YUVRGB_TABLE_HEADROOM)*inc;
758         table[i] = elemsize * (off + (cb >> 16));
759     }
760 }
761 
roundToInt16(int64_t f)762 static uint16_t roundToInt16(int64_t f)
763 {
764     int r = (f + (1 << 15)) >> 16;
765 
766     if (r < -0x7FFF)
767         return 0x8000;
768     else if (r > 0x7FFF)
769         return 0x7FFF;
770     else
771         return r;
772 }
773 
ff_yuv2rgb_c_init_tables(SwsContext * c,const int inv_table[4],int fullRange,int brightness,int contrast,int saturation)774 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
775                                      int fullRange, int brightness,
776                                      int contrast, int saturation)
777 {
778     const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32     ||
779                       c->dstFormat == AV_PIX_FMT_RGB32_1   ||
780                       c->dstFormat == AV_PIX_FMT_BGR24     ||
781                       c->dstFormat == AV_PIX_FMT_RGB565BE  ||
782                       c->dstFormat == AV_PIX_FMT_RGB565LE  ||
783                       c->dstFormat == AV_PIX_FMT_RGB555BE  ||
784                       c->dstFormat == AV_PIX_FMT_RGB555LE  ||
785                       c->dstFormat == AV_PIX_FMT_RGB444BE  ||
786                       c->dstFormat == AV_PIX_FMT_RGB444LE  ||
787                       c->dstFormat == AV_PIX_FMT_RGB8      ||
788                       c->dstFormat == AV_PIX_FMT_RGB4      ||
789                       c->dstFormat == AV_PIX_FMT_RGB4_BYTE ||
790                       c->dstFormat == AV_PIX_FMT_MONOBLACK;
791     const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
792                         c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
793                         c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
794                         c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
795                         c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
796                         c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE) ||
797                         c->dstFormat == AV_PIX_FMT_NE(X2RGB10LE, X2RGB10BE);
798     const int bpp = c->dstFormatBpp;
799     uint8_t *y_table;
800     uint16_t *y_table16;
801     uint32_t *y_table32;
802     int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
803     const int yoffs = (fullRange ? 384 : 326) + YUVRGB_TABLE_LUMA_HEADROOM;
804     const int table_plane_size = 1024 + 2*YUVRGB_TABLE_LUMA_HEADROOM;
805 
806     int64_t crv =  inv_table[0];
807     int64_t cbu =  inv_table[1];
808     int64_t cgu = -inv_table[2];
809     int64_t cgv = -inv_table[3];
810     int64_t cy  = 1 << 16;
811     int64_t oy  = 0;
812     int64_t yb  = 0;
813 
814     if (!fullRange) {
815         cy = (cy * 255) / 219;
816         oy = 16 << 16;
817     } else {
818         crv = (crv * 224) / 255;
819         cbu = (cbu * 224) / 255;
820         cgu = (cgu * 224) / 255;
821         cgv = (cgv * 224) / 255;
822     }
823 
824     cy   = (cy  * contrast)              >> 16;
825     crv  = (crv * contrast * saturation) >> 32;
826     cbu  = (cbu * contrast * saturation) >> 32;
827     cgu  = (cgu * contrast * saturation) >> 32;
828     cgv  = (cgv * contrast * saturation) >> 32;
829     oy  -= 256 * brightness;
830 
831     c->uOffset = 0x0400040004000400LL;
832     c->vOffset = 0x0400040004000400LL;
833     c->yCoeff  = roundToInt16(cy  * (1 << 13)) * 0x0001000100010001ULL;
834     c->vrCoeff = roundToInt16(crv * (1 << 13)) * 0x0001000100010001ULL;
835     c->ubCoeff = roundToInt16(cbu * (1 << 13)) * 0x0001000100010001ULL;
836     c->vgCoeff = roundToInt16(cgv * (1 << 13)) * 0x0001000100010001ULL;
837     c->ugCoeff = roundToInt16(cgu * (1 << 13)) * 0x0001000100010001ULL;
838     c->yOffset = roundToInt16(oy  * (1 <<  3)) * 0x0001000100010001ULL;
839 
840     c->yuv2rgb_y_coeff   = (int16_t)roundToInt16(cy  * (1 << 13));
841     c->yuv2rgb_y_offset  = (int16_t)roundToInt16(oy  * (1 <<  9));
842     c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv * (1 << 13));
843     c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv * (1 << 13));
844     c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu * (1 << 13));
845     c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu * (1 << 13));
846 
847     //scale coefficients by cy
848     crv = ((crv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
849     cbu = ((cbu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
850     cgu = ((cgu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
851     cgv = ((cgv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
852 
853     av_freep(&c->yuvTable);
854 
855 #define ALLOC_YUV_TABLE(x)          \
856         c->yuvTable = av_malloc(x); \
857         if (!c->yuvTable)           \
858             return AVERROR(ENOMEM);
859     switch (bpp) {
860     case 1:
861         ALLOC_YUV_TABLE(table_plane_size);
862         y_table     = c->yuvTable;
863         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
864         for (i = 0; i < table_plane_size - 110; i++) {
865             y_table[i + 110]  = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
866             yb               += cy;
867         }
868         fill_table(c->table_gU, 1, cgu, y_table + yoffs);
869         fill_gv_table(c->table_gV, 1, cgv);
870         break;
871     case 4:
872     case 4 | 128:
873         rbase       = isRgb ? 3 : 0;
874         gbase       = 1;
875         bbase       = isRgb ? 0 : 3;
876         ALLOC_YUV_TABLE(table_plane_size * 3);
877         y_table     = c->yuvTable;
878         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
879         for (i = 0; i < table_plane_size - 110; i++) {
880             int yval                = av_clip_uint8((yb + 0x8000) >> 16);
881             y_table[i + 110]        = (yval >> 7)        << rbase;
882             y_table[i +  37 +   table_plane_size] = ((yval + 43) / 85) << gbase;
883             y_table[i + 110 + 2*table_plane_size] = (yval >> 7)        << bbase;
884             yb += cy;
885         }
886         fill_table(c->table_rV, 1, crv, y_table + yoffs);
887         fill_table(c->table_gU, 1, cgu, y_table + yoffs +   table_plane_size);
888         fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
889         fill_gv_table(c->table_gV, 1, cgv);
890         break;
891     case 8:
892         rbase       = isRgb ? 5 : 0;
893         gbase       = isRgb ? 2 : 3;
894         bbase       = isRgb ? 0 : 6;
895         ALLOC_YUV_TABLE(table_plane_size * 3);
896         y_table     = c->yuvTable;
897         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
898         for (i = 0; i < table_plane_size - 38; i++) {
899             int yval               = av_clip_uint8((yb + 0x8000) >> 16);
900             y_table[i + 16]        = ((yval + 18) / 36) << rbase;
901             y_table[i + 16 +   table_plane_size] = ((yval + 18) / 36) << gbase;
902             y_table[i + 37 + 2*table_plane_size] = ((yval + 43) / 85) << bbase;
903             yb += cy;
904         }
905         fill_table(c->table_rV, 1, crv, y_table + yoffs);
906         fill_table(c->table_gU, 1, cgu, y_table + yoffs +   table_plane_size);
907         fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
908         fill_gv_table(c->table_gV, 1, cgv);
909         break;
910     case 12:
911         rbase       = isRgb ? 8 : 0;
912         gbase       = 4;
913         bbase       = isRgb ? 0 : 8;
914         ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
915         y_table16   = c->yuvTable;
916         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
917         for (i = 0; i < table_plane_size; i++) {
918             uint8_t yval        = av_clip_uint8((yb + 0x8000) >> 16);
919             y_table16[i]        = (yval >> 4) << rbase;
920             y_table16[i +   table_plane_size] = (yval >> 4) << gbase;
921             y_table16[i + 2*table_plane_size] = (yval >> 4) << bbase;
922             yb += cy;
923         }
924         if (isNotNe)
925             for (i = 0; i < table_plane_size * 3; i++)
926                 y_table16[i] = av_bswap16(y_table16[i]);
927         fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
928         fill_table(c->table_gU, 2, cgu, y_table16 + yoffs +   table_plane_size);
929         fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
930         fill_gv_table(c->table_gV, 2, cgv);
931         break;
932     case 15:
933     case 16:
934         rbase       = isRgb ? bpp - 5 : 0;
935         gbase       = 5;
936         bbase       = isRgb ? 0 : (bpp - 5);
937         ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
938         y_table16   = c->yuvTable;
939         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
940         for (i = 0; i < table_plane_size; i++) {
941             uint8_t yval        = av_clip_uint8((yb + 0x8000) >> 16);
942             y_table16[i]        = (yval >> 3)          << rbase;
943             y_table16[i +   table_plane_size] = (yval >> (18 - bpp)) << gbase;
944             y_table16[i + 2*table_plane_size] = (yval >> 3)          << bbase;
945             yb += cy;
946         }
947         if (isNotNe)
948             for (i = 0; i < table_plane_size * 3; i++)
949                 y_table16[i] = av_bswap16(y_table16[i]);
950         fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
951         fill_table(c->table_gU, 2, cgu, y_table16 + yoffs +   table_plane_size);
952         fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
953         fill_gv_table(c->table_gV, 2, cgv);
954         break;
955     case 24:
956     case 48:
957         ALLOC_YUV_TABLE(table_plane_size);
958         y_table     = c->yuvTable;
959         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
960         for (i = 0; i < table_plane_size; i++) {
961             y_table[i]  = av_clip_uint8((yb + 0x8000) >> 16);
962             yb         += cy;
963         }
964         fill_table(c->table_rV, 1, crv, y_table + yoffs);
965         fill_table(c->table_gU, 1, cgu, y_table + yoffs);
966         fill_table(c->table_bU, 1, cbu, y_table + yoffs);
967         fill_gv_table(c->table_gV, 1, cgv);
968         break;
969     case 30:
970         rbase = 20;
971         gbase = 10;
972         bbase = 0;
973         needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
974         if (!needAlpha)
975             abase = 30;
976         ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
977         y_table32   = c->yuvTable;
978         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
979         for (i = 0; i < table_plane_size; i++) {
980             unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
981             y_table32[i]= (yval << rbase) + (needAlpha ? 0 : (255u << abase));
982             y_table32[i + table_plane_size] = yval << gbase;
983             y_table32[i + 2 * table_plane_size] = yval << bbase;
984             yb += cy;
985         }
986         if (isNotNe) {
987             for (i = 0; i < table_plane_size * 3; i++)
988                 y_table32[i] = av_bswap32(y_table32[i]);
989         }
990         fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
991         fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
992         fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2 * table_plane_size);
993         fill_gv_table(c->table_gV, 4, cgv);
994         break;
995     case 32:
996     case 64:
997         base      = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
998                      c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
999         rbase     = base + (isRgb ? 16 : 0);
1000         gbase     = base + 8;
1001         bbase     = base + (isRgb ? 0 : 16);
1002         needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
1003         if (!needAlpha)
1004             abase = (base + 24) & 31;
1005         ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
1006         y_table32   = c->yuvTable;
1007         yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1008         for (i = 0; i < table_plane_size; i++) {
1009             unsigned yval       = av_clip_uint8((yb + 0x8000) >> 16);
1010             y_table32[i]        = (yval << rbase) +
1011                                   (needAlpha ? 0 : (255u << abase));
1012             y_table32[i +   table_plane_size] =  yval << gbase;
1013             y_table32[i + 2*table_plane_size] =  yval << bbase;
1014             yb += cy;
1015         }
1016         fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
1017         fill_table(c->table_gU, 4, cgu, y_table32 + yoffs +   table_plane_size);
1018         fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2*table_plane_size);
1019         fill_gv_table(c->table_gV, 4, cgv);
1020         break;
1021     default:
1022         if(!isPlanar(c->dstFormat) || bpp <= 24)
1023             av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
1024         return AVERROR(EINVAL);
1025     }
1026     return 0;
1027 }
1028