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