Move yuv2rgb code to subdirs.
[libav.git] / libswscale / yuv2rgb.c
CommitLineData
e76709d8
KS
1/*
2 * software YUV to RGB converter
3 *
4 * Copyright (C) 2009 Konstantin Shishkov
5 *
e76709d8
KS
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#include <assert.h>
30
31#include "config.h"
32#include "rgb2rgb.h"
33#include "swscale.h"
34#include "swscale_internal.h"
d0ce212a 35#include "libavutil/x86_cpu.h"
e76709d8 36
e76709d8
KS
37extern const uint8_t dither_8x8_32[8][8];
38extern const uint8_t dither_8x8_73[8][8];
39extern const uint8_t dither_8x8_220[8][8];
40
e76709d8
KS
41const int32_t ff_yuv2rgb_coeffs[8][4] = {
42 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
43 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
44 {104597, 132201, 25675, 53279}, /* unspecified */
45 {104597, 132201, 25675, 53279}, /* reserved */
46 {104448, 132798, 24759, 53109}, /* FCC */
47 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
48 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
49 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
50};
51
52#define LOADCHROMA(i) \
53 U = pu[i]; \
54 V = pv[i]; \
55 r = (void *)c->table_rV[V]; \
56 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
57 b = (void *)c->table_bU[U];
58
59#define PUTRGB(dst,src,i,o) \
60 Y = src[2*i+o]; \
61 dst[2*i ] = r[Y] + g[Y] + b[Y]; \
62 Y = src[2*i+1-o]; \
63 dst[2*i+1] = r[Y] + g[Y] + b[Y];
64
65#define PUTRGB24(dst,src,i) \
66 Y = src[2*i]; \
67 dst[6*i+0] = r[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = b[Y]; \
68 Y = src[2*i+1]; \
69 dst[6*i+3] = r[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = b[Y];
70
71#define PUTBGR24(dst,src,i) \
72 Y = src[2*i]; \
73 dst[6*i+0] = b[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = r[Y]; \
74 Y = src[2*i+1]; \
75 dst[6*i+3] = b[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = r[Y];
76
3acd545f
CS
77#define PUTRGBA(dst,ysrc,asrc,i,o,s) \
78 Y = ysrc[2*i+o]; \
79 dst[2*i ] = r[Y] + g[Y] + b[Y] + (asrc[2*i ]<<s); \
80 Y = ysrc[2*i+1-o]; \
81 dst[2*i+1] = r[Y] + g[Y] + b[Y] + (asrc[2*i+1]<<s);
82
9a10a076 83#define YUV2RGBFUNC(func_name, dst_type, alpha) \
e76709d8
KS
84static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
85 int srcSliceH, uint8_t* dst[], int dstStride[]){\
86 int y;\
87\
3acd545f 88 if (!alpha && c->srcFormat == PIX_FMT_YUV422P) {\
e76709d8
KS
89 srcStride[1] *= 2;\
90 srcStride[2] *= 2;\
91 }\
92 for (y=0; y<srcSliceH; y+=2) {\
93 dst_type *dst_1 = (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
94 dst_type *dst_2 = (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
95 dst_type av_unused *r, *b;\
96 dst_type *g;\
97 uint8_t *py_1 = src[0] + y*srcStride[0];\
98 uint8_t *py_2 = py_1 + srcStride[0];\
99 uint8_t *pu = src[1] + (y>>1)*srcStride[1];\
100 uint8_t *pv = src[2] + (y>>1)*srcStride[2];\
3acd545f 101 uint8_t av_unused *pa_1, *pa_2;\
e76709d8 102 unsigned int h_size = c->dstW>>3;\
3acd545f
CS
103 if (alpha){\
104 pa_1 = src[3] + y*srcStride[3];\
105 pa_2 = pa_1 + srcStride[3];\
106 }\
e76709d8
KS
107 while (h_size--) {\
108 int av_unused U, V;\
109 int Y;\
110
111#define ENDYUV2RGBLINE(dst_delta)\
112 pu += 4;\
113 pv += 4;\
114 py_1 += 8;\
115 py_2 += 8;\
116 dst_1 += dst_delta;\
117 dst_2 += dst_delta;\
118 }\
119 if (c->dstW & 4) {\
120 int av_unused Y, U, V;\
121
122#define ENDYUV2RGBFUNC()\
123 }\
124 }\
125 return srcSliceH;\
126}
127
128#define CLOSEYUV2RGBFUNC(dst_delta)\
129 ENDYUV2RGBLINE(dst_delta)\
130 ENDYUV2RGBFUNC()
131
9a10a076 132YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
e76709d8
KS
133 LOADCHROMA(0);
134 PUTRGB(dst_1,py_1,0,0);
135 PUTRGB(dst_2,py_2,0,1);
136
137 LOADCHROMA(1);
138 PUTRGB(dst_2,py_2,1,1);
139 PUTRGB(dst_1,py_1,1,0);
140 LOADCHROMA(1);
141 PUTRGB(dst_2,py_2,1,1);
142 PUTRGB(dst_1,py_1,1,0);
143
144 LOADCHROMA(2);
145 PUTRGB(dst_1,py_1,2,0);
146 PUTRGB(dst_2,py_2,2,1);
147
148 LOADCHROMA(3);
149 PUTRGB(dst_2,py_2,3,1);
150 PUTRGB(dst_1,py_1,3,0);
151ENDYUV2RGBLINE(8)
152 LOADCHROMA(0);
153 PUTRGB(dst_1,py_1,0,0);
154 PUTRGB(dst_2,py_2,0,1);
155
156 LOADCHROMA(1);
157 PUTRGB(dst_2,py_2,1,1);
158 PUTRGB(dst_1,py_1,1,0);
159ENDYUV2RGBFUNC()
160
3acd545f
CS
161YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
162 LOADCHROMA(0);
163 PUTRGBA(dst_1,py_1,pa_1,0,0,24);
164 PUTRGBA(dst_2,py_2,pa_2,0,1,24);
165
166 LOADCHROMA(1);
167 PUTRGBA(dst_2,py_2,pa_1,1,1,24);
168 PUTRGBA(dst_1,py_1,pa_2,1,0,24);
169 LOADCHROMA(1);
170 PUTRGBA(dst_2,py_2,pa_1,1,1,24);
171 PUTRGBA(dst_1,py_1,pa_2,1,0,24);
172
173 LOADCHROMA(2);
174 PUTRGBA(dst_1,py_1,pa_1,2,0,24);
175 PUTRGBA(dst_2,py_2,pa_2,2,1,24);
176
177 LOADCHROMA(3);
178 PUTRGBA(dst_2,py_2,pa_1,3,1,24);
179 PUTRGBA(dst_1,py_1,pa_2,3,0,24);
180 pa_1 += 8;\
181 pa_2 += 8;\
182ENDYUV2RGBLINE(8)
183 LOADCHROMA(0);
184 PUTRGBA(dst_1,py_1,pa_1,0,0,24);
185 PUTRGBA(dst_2,py_2,pa_2,0,1,24);
186
187 LOADCHROMA(1);
188 PUTRGBA(dst_2,py_2,pa_1,1,1,24);
189 PUTRGBA(dst_1,py_1,pa_2,1,0,24);
190ENDYUV2RGBFUNC()
191
192YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
193 LOADCHROMA(0);
194 PUTRGBA(dst_1,py_1,pa_1,0,0,0);
195 PUTRGBA(dst_2,py_2,pa_2,0,1,0);
196
197 LOADCHROMA(1);
198 PUTRGBA(dst_2,py_2,pa_2,1,1,0);
199 PUTRGBA(dst_1,py_1,pa_1,1,0,0);
200 LOADCHROMA(1);
201 PUTRGBA(dst_2,py_2,pa_2,1,1,0);
202 PUTRGBA(dst_1,py_1,pa_1,1,0,0);
203
204 LOADCHROMA(2);
205 PUTRGBA(dst_1,py_1,pa_1,2,0,0);
206 PUTRGBA(dst_2,py_2,pa_2,2,1,0);
207
208 LOADCHROMA(3);
209 PUTRGBA(dst_2,py_2,pa_2,3,1,0);
210 PUTRGBA(dst_1,py_1,pa_1,3,0,0);
211 pa_1 += 8;\
212 pa_2 += 8;\
213ENDYUV2RGBLINE(8)
214 LOADCHROMA(0);
215 PUTRGBA(dst_1,py_1,pa_1,0,0,0);
216 PUTRGBA(dst_2,py_2,pa_2,0,1,0);
217
218 LOADCHROMA(1);
219 PUTRGBA(dst_2,py_2,pa_2,1,1,0);
220 PUTRGBA(dst_1,py_1,pa_1,1,0,0);
221ENDYUV2RGBFUNC()
222
9a10a076 223YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
e76709d8
KS
224 LOADCHROMA(0);
225 PUTRGB24(dst_1,py_1,0);
226 PUTRGB24(dst_2,py_2,0);
227
228 LOADCHROMA(1);
229 PUTRGB24(dst_2,py_2,1);
230 PUTRGB24(dst_1,py_1,1);
231
232 LOADCHROMA(2);
233 PUTRGB24(dst_1,py_1,2);
234 PUTRGB24(dst_2,py_2,2);
235
236 LOADCHROMA(3);
237 PUTRGB24(dst_2,py_2,3);
238 PUTRGB24(dst_1,py_1,3);
239ENDYUV2RGBLINE(24)
240 LOADCHROMA(0);
241 PUTRGB24(dst_1,py_1,0);
242 PUTRGB24(dst_2,py_2,0);
243
244 LOADCHROMA(1);
245 PUTRGB24(dst_2,py_2,1);
246 PUTRGB24(dst_1,py_1,1);
247ENDYUV2RGBFUNC()
248
249// only trivial mods from yuv2rgb_c_24_rgb
9a10a076 250YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
e76709d8
KS
251 LOADCHROMA(0);
252 PUTBGR24(dst_1,py_1,0);
253 PUTBGR24(dst_2,py_2,0);
254
255 LOADCHROMA(1);
256 PUTBGR24(dst_2,py_2,1);
257 PUTBGR24(dst_1,py_1,1);
258
259 LOADCHROMA(2);
260 PUTBGR24(dst_1,py_1,2);
261 PUTBGR24(dst_2,py_2,2);
262
263 LOADCHROMA(3);
264 PUTBGR24(dst_2,py_2,3);
265 PUTBGR24(dst_1,py_1,3);
266ENDYUV2RGBLINE(24)
267 LOADCHROMA(0);
268 PUTBGR24(dst_1,py_1,0);
269 PUTBGR24(dst_2,py_2,0);
270
271 LOADCHROMA(1);
272 PUTBGR24(dst_2,py_2,1);
273 PUTBGR24(dst_1,py_1,1);
274ENDYUV2RGBFUNC()
275
276// This is exactly the same code as yuv2rgb_c_32 except for the types of
277// r, g, b, dst_1, dst_2
9a10a076 278YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
e76709d8
KS
279 LOADCHROMA(0);
280 PUTRGB(dst_1,py_1,0,0);
281 PUTRGB(dst_2,py_2,0,1);
282
283 LOADCHROMA(1);
284 PUTRGB(dst_2,py_2,1,1);
285 PUTRGB(dst_1,py_1,1,0);
286
287 LOADCHROMA(2);
288 PUTRGB(dst_1,py_1,2,0);
289 PUTRGB(dst_2,py_2,2,1);
290
291 LOADCHROMA(3);
292 PUTRGB(dst_2,py_2,3,1);
293 PUTRGB(dst_1,py_1,3,0);
294CLOSEYUV2RGBFUNC(8)
295
296// This is exactly the same code as yuv2rgb_c_32 except for the types of
297// r, g, b, dst_1, dst_2
9a10a076 298YUV2RGBFUNC(yuv2rgb_c_8, uint8_t, 0)
e76709d8
KS
299 LOADCHROMA(0);
300 PUTRGB(dst_1,py_1,0,0);
301 PUTRGB(dst_2,py_2,0,1);
302
303 LOADCHROMA(1);
304 PUTRGB(dst_2,py_2,1,1);
305 PUTRGB(dst_1,py_1,1,0);
306
307 LOADCHROMA(2);
308 PUTRGB(dst_1,py_1,2,0);
309 PUTRGB(dst_2,py_2,2,1);
310
311 LOADCHROMA(3);
312 PUTRGB(dst_2,py_2,3,1);
313 PUTRGB(dst_1,py_1,3,0);
314CLOSEYUV2RGBFUNC(8)
315
316// r, g, b, dst_1, dst_2
9a10a076 317YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
e76709d8
KS
318 const uint8_t *d32 = dither_8x8_32[y&7];
319 const uint8_t *d64 = dither_8x8_73[y&7];
320#define PUTRGB8(dst,src,i,o) \
321 Y = src[2*i]; \
322 dst[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
323 Y = src[2*i+1]; \
324 dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
325
326 LOADCHROMA(0);
327 PUTRGB8(dst_1,py_1,0,0);
328 PUTRGB8(dst_2,py_2,0,0+8);
329
330 LOADCHROMA(1);
331 PUTRGB8(dst_2,py_2,1,2+8);
332 PUTRGB8(dst_1,py_1,1,2);
333
334 LOADCHROMA(2);
335 PUTRGB8(dst_1,py_1,2,4);
336 PUTRGB8(dst_2,py_2,2,4+8);
337
338 LOADCHROMA(3);
339 PUTRGB8(dst_2,py_2,3,6+8);
340 PUTRGB8(dst_1,py_1,3,6);
341CLOSEYUV2RGBFUNC(8)
342
343
344// This is exactly the same code as yuv2rgb_c_32 except for the types of
345// r, g, b, dst_1, dst_2
9a10a076 346YUV2RGBFUNC(yuv2rgb_c_4, uint8_t, 0)
e76709d8
KS
347 int acc;
348#define PUTRGB4(dst,src,i) \
349 Y = src[2*i]; \
350 acc = r[Y] + g[Y] + b[Y]; \
351 Y = src[2*i+1]; \
352 acc |= (r[Y] + g[Y] + b[Y])<<4; \
353 dst[i] = acc;
354
355 LOADCHROMA(0);
356 PUTRGB4(dst_1,py_1,0);
357 PUTRGB4(dst_2,py_2,0);
358
359 LOADCHROMA(1);
360 PUTRGB4(dst_2,py_2,1);
361 PUTRGB4(dst_1,py_1,1);
362
363 LOADCHROMA(2);
364 PUTRGB4(dst_1,py_1,2);
365 PUTRGB4(dst_2,py_2,2);
366
367 LOADCHROMA(3);
368 PUTRGB4(dst_2,py_2,3);
369 PUTRGB4(dst_1,py_1,3);
370CLOSEYUV2RGBFUNC(4)
371
9a10a076 372YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
e76709d8
KS
373 const uint8_t *d64 = dither_8x8_73[y&7];
374 const uint8_t *d128 = dither_8x8_220[y&7];
375 int acc;
376
377#define PUTRGB4D(dst,src,i,o) \
378 Y = src[2*i]; \
379 acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
380 Y = src[2*i+1]; \
381 acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
382 dst[i]= acc;
383
384 LOADCHROMA(0);
385 PUTRGB4D(dst_1,py_1,0,0);
386 PUTRGB4D(dst_2,py_2,0,0+8);
387
388 LOADCHROMA(1);
389 PUTRGB4D(dst_2,py_2,1,2+8);
390 PUTRGB4D(dst_1,py_1,1,2);
391
392 LOADCHROMA(2);
393 PUTRGB4D(dst_1,py_1,2,4);
394 PUTRGB4D(dst_2,py_2,2,4+8);
395
396 LOADCHROMA(3);
397 PUTRGB4D(dst_2,py_2,3,6+8);
398 PUTRGB4D(dst_1,py_1,3,6);
399CLOSEYUV2RGBFUNC(4)
400
401// This is exactly the same code as yuv2rgb_c_32 except for the types of
402// r, g, b, dst_1, dst_2
9a10a076 403YUV2RGBFUNC(yuv2rgb_c_4b, uint8_t, 0)
e76709d8
KS
404 LOADCHROMA(0);
405 PUTRGB(dst_1,py_1,0,0);
406 PUTRGB(dst_2,py_2,0,1);
407
408 LOADCHROMA(1);
409 PUTRGB(dst_2,py_2,1,1);
410 PUTRGB(dst_1,py_1,1,0);
411
412 LOADCHROMA(2);
413 PUTRGB(dst_1,py_1,2,0);
414 PUTRGB(dst_2,py_2,2,1);
415
416 LOADCHROMA(3);
417 PUTRGB(dst_2,py_2,3,1);
418 PUTRGB(dst_1,py_1,3,0);
419CLOSEYUV2RGBFUNC(8)
420
9a10a076 421YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
e76709d8
KS
422 const uint8_t *d64 = dither_8x8_73[y&7];
423 const uint8_t *d128 = dither_8x8_220[y&7];
424
425#define PUTRGB4DB(dst,src,i,o) \
426 Y = src[2*i]; \
427 dst[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
428 Y = src[2*i+1]; \
429 dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
430
431 LOADCHROMA(0);
432 PUTRGB4DB(dst_1,py_1,0,0);
433 PUTRGB4DB(dst_2,py_2,0,0+8);
434
435 LOADCHROMA(1);
436 PUTRGB4DB(dst_2,py_2,1,2+8);
437 PUTRGB4DB(dst_1,py_1,1,2);
438
439 LOADCHROMA(2);
440 PUTRGB4DB(dst_1,py_1,2,4);
441 PUTRGB4DB(dst_2,py_2,2,4+8);
442
443 LOADCHROMA(3);
444 PUTRGB4DB(dst_2,py_2,3,6+8);
445 PUTRGB4DB(dst_1,py_1,3,6);
446CLOSEYUV2RGBFUNC(8)
447
9a10a076 448YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
e76709d8
KS
449 const uint8_t *d128 = dither_8x8_220[y&7];
450 char out_1 = 0, out_2 = 0;
451 g= c->table_gU[128] + c->table_gV[128];
452
453#define PUTRGB1(out,src,i,o) \
454 Y = src[2*i]; \
455 out+= out + g[Y+d128[0+o]]; \
456 Y = src[2*i+1]; \
457 out+= out + g[Y+d128[1+o]];
458
459 PUTRGB1(out_1,py_1,0,0);
460 PUTRGB1(out_2,py_2,0,0+8);
461
462 PUTRGB1(out_2,py_2,1,2+8);
463 PUTRGB1(out_1,py_1,1,2);
464
465 PUTRGB1(out_1,py_1,2,4);
466 PUTRGB1(out_2,py_2,2,4+8);
467
468 PUTRGB1(out_2,py_2,3,6+8);
469 PUTRGB1(out_1,py_1,3,6);
470
471 dst_1[0]= out_1;
472 dst_2[0]= out_2;
473CLOSEYUV2RGBFUNC(1)
474
780daf2b 475SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
e76709d8
KS
476{
477 SwsFunc t = NULL;
478#if (HAVE_MMX2 || HAVE_MMX) && CONFIG_GPL
befa8e66 479 t = ff_yuv2rgb_init_mmx(c);
e76709d8
KS
480#endif
481#if HAVE_VIS
780daf2b 482 t = ff_yuv2rgb_init_vis(c);
e76709d8
KS
483#endif
484#if CONFIG_MLIB
780daf2b 485 t = ff_yuv2rgb_init_mlib(c);
e76709d8
KS
486#endif
487#if HAVE_ALTIVEC && CONFIG_GPL
488 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
780daf2b 489 t = ff_yuv2rgb_init_altivec(c);
e76709d8
KS
490#endif
491
492#if ARCH_BFIN
493 if (c->flags & SWS_CPU_CAPS_BFIN)
780daf2b 494 t = ff_yuv2rgb_get_func_ptr_bfin(c);
e76709d8
KS
495#endif
496
497 if (t)
498 return t;
499
500 av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
501
502 switch (c->dstFormat) {
3acd545f
CS
503 case PIX_FMT_ARGB:
504 case PIX_FMT_ABGR: if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) return yuva2argb_c;
505 case PIX_FMT_RGBA:
506 case PIX_FMT_BGRA: return (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) ? yuva2rgba_c : yuv2rgb_c_32;
e76709d8
KS
507 case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
508 case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
509 case PIX_FMT_RGB565:
510 case PIX_FMT_BGR565:
511 case PIX_FMT_RGB555:
512 case PIX_FMT_BGR555: return yuv2rgb_c_16;
513 case PIX_FMT_RGB8:
514 case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
515 case PIX_FMT_RGB4:
516 case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
517 case PIX_FMT_RGB4_BYTE:
518 case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
519 case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
520 default:
521 assert(0);
522 }
523 return NULL;
524}
525
526static void fill_table(uint8_t* table[256], const int elemsize, const int inc, uint8_t *y_table)
527{
528 int i;
529 int64_t cb = 0;
530
531 y_table -= elemsize * (inc >> 9);
532
533 for (i = 0; i < 256; i++) {
534 table[i] = y_table + elemsize * (cb >> 16);
535 cb += inc;
536 }
537}
538
539static void fill_gv_table(int table[256], const int elemsize, const int inc)
540{
541 int i;
542 int64_t cb = 0;
543 int off = -(inc >> 9);
544
545 for (i = 0; i < 256; i++) {
546 table[i] = elemsize * (off + (cb >> 16));
547 cb += inc;
548 }
549}
550
780daf2b
DB
551av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
552 int brightness, int contrast, int saturation)
e76709d8
KS
553{
554 const int isRgb = c->dstFormat==PIX_FMT_RGB32
555 || c->dstFormat==PIX_FMT_RGB32_1
556 || c->dstFormat==PIX_FMT_BGR24
557 || c->dstFormat==PIX_FMT_RGB565
558 || c->dstFormat==PIX_FMT_RGB555
559 || c->dstFormat==PIX_FMT_RGB8
560 || c->dstFormat==PIX_FMT_RGB4
561 || c->dstFormat==PIX_FMT_RGB4_BYTE
562 || c->dstFormat==PIX_FMT_MONOBLACK;
563 const int bpp = fmt_depth(c->dstFormat);
564 uint8_t *y_table;
565 uint16_t *y_table16;
566 uint32_t *y_table32;
3acd545f 567 int i, base, rbase, gbase, bbase, abase, needAlpha;
e76709d8
KS
568 const int yoffs = fullRange ? 384 : 326;
569
570 int64_t crv = inv_table[0];
571 int64_t cbu = inv_table[1];
572 int64_t cgu = -inv_table[2];
573 int64_t cgv = -inv_table[3];
574 int64_t cy = 1<<16;
575 int64_t oy = 0;
576
577 int64_t yb = 0;
578
579 if (!fullRange) {
580 cy = (cy*255) / 219;
581 oy = 16<<16;
582 } else {
583 crv = (crv*224) / 255;
584 cbu = (cbu*224) / 255;
585 cgu = (cgu*224) / 255;
586 cgv = (cgv*224) / 255;
587 }
588
589 cy = (cy *contrast ) >> 16;
590 crv = (crv*contrast * saturation) >> 32;
591 cbu = (cbu*contrast * saturation) >> 32;
592 cgu = (cgu*contrast * saturation) >> 32;
593 cgv = (cgv*contrast * saturation) >> 32;
594 oy -= 256*brightness;
595
596 //scale coefficients by cy
597 crv = ((crv << 16) + 0x8000) / cy;
598 cbu = ((cbu << 16) + 0x8000) / cy;
599 cgu = ((cgu << 16) + 0x8000) / cy;
600 cgv = ((cgv << 16) + 0x8000) / cy;
601
602 av_free(c->yuvTable);
603
604 switch (bpp) {
605 case 1:
606 c->yuvTable = av_malloc(1024);
607 y_table = c->yuvTable;
608 yb = -(384<<16) - oy;
609 for (i = 0; i < 1024-110; i++) {
610 y_table[i+110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
611 yb += cy;
612 }
613 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
614 fill_gv_table(c->table_gV, 1, cgv);
615 break;
616 case 4:
617 case 4|128:
618 rbase = isRgb ? 3 : 0;
619 gbase = 1;
620 bbase = isRgb ? 0 : 3;
621 c->yuvTable = av_malloc(1024*3);
622 y_table = c->yuvTable;
623 yb = -(384<<16) - oy;
624 for (i = 0; i < 1024-110; i++) {
625 int yval = av_clip_uint8((yb + 0x8000) >> 16);
626 y_table[i+110 ] = (yval >> 7) << rbase;
627 y_table[i+ 37+1024] = ((yval + 43) / 85) << gbase;
628 y_table[i+110+2048] = (yval >> 7) << bbase;
629 yb += cy;
630 }
631 fill_table(c->table_rV, 1, crv, y_table + yoffs);
632 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
633 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
634 fill_gv_table(c->table_gV, 1, cgv);
635 break;
636 case 8:
637 rbase = isRgb ? 5 : 0;
638 gbase = isRgb ? 2 : 3;
639 bbase = isRgb ? 0 : 6;
640 c->yuvTable = av_malloc(1024*3);
641 y_table = c->yuvTable;
642 yb = -(384<<16) - oy;
643 for (i = 0; i < 1024-38; i++) {
644 int yval = av_clip_uint8((yb + 0x8000) >> 16);
645 y_table[i+16 ] = ((yval + 18) / 36) << rbase;
646 y_table[i+16+1024] = ((yval + 18) / 36) << gbase;
647 y_table[i+37+2048] = ((yval + 43) / 85) << bbase;
648 yb += cy;
649 }
650 fill_table(c->table_rV, 1, crv, y_table + yoffs);
651 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
652 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
653 fill_gv_table(c->table_gV, 1, cgv);
654 break;
655 case 15:
656 case 16:
657 rbase = isRgb ? bpp - 5 : 0;
658 gbase = 5;
659 bbase = isRgb ? 0 : (bpp - 5);
660 c->yuvTable = av_malloc(1024*3*2);
661 y_table16 = c->yuvTable;
662 yb = -(384<<16) - oy;
663 for (i = 0; i < 1024; i++) {
664 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
665 y_table16[i ] = (yval >> 3) << rbase;
666 y_table16[i+1024] = (yval >> (18 - bpp)) << gbase;
667 y_table16[i+2048] = (yval >> 3) << bbase;
668 yb += cy;
669 }
670 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
671 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
672 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
673 fill_gv_table(c->table_gV, 2, cgv);
674 break;
675 case 24:
676 c->yuvTable = av_malloc(1024);
677 y_table = c->yuvTable;
678 yb = -(384<<16) - oy;
679 for (i = 0; i < 1024; i++) {
680 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
681 yb += cy;
682 }
683 fill_table(c->table_rV, 1, crv, y_table + yoffs);
684 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
685 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
686 fill_gv_table(c->table_gV, 1, cgv);
687 break;
688 case 32:
689 base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
690 rbase = base + (isRgb ? 16 : 0);
691 gbase = base + 8;
692 bbase = base + (isRgb ? 0 : 16);
3acd545f
CS
693 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
694 if (!needAlpha)
695 abase = (base + 24) & 31;
e76709d8
KS
696 c->yuvTable = av_malloc(1024*3*4);
697 y_table32 = c->yuvTable;
698 yb = -(384<<16) - oy;
699 for (i = 0; i < 1024; i++) {
700 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
3acd545f 701 y_table32[i ] = (yval << rbase) + (needAlpha ? 0 : (255 << abase));
e76709d8
KS
702 y_table32[i+1024] = yval << gbase;
703 y_table32[i+2048] = yval << bbase;
704 yb += cy;
705 }
706 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
707 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
708 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
709 fill_gv_table(c->table_gV, 4, cgv);
710 break;
711 default:
712 c->yuvTable = NULL;
713 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
714 return -1;
715 }
716 return 0;
717}