2 * yuv2rgb.c, Software YUV to RGB converter
4 * Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * Functions broken out from display_x11.c and several new modes
7 * added by HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
9 * 15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
11 * MMX/MMX2 template stuff (needed for fast movntq support),
12 * 1,4,8bpp support and context / deglobalize stuff
13 * by Michael Niedermayer (michaelni@gmx.at)
15 * This file is part of mpeg2dec, a free MPEG-2 video decoder
17 * mpeg2dec is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2, or (at your option)
22 * mpeg2dec is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with mpeg2dec; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "swscale_internal.h"
42 #define DITHER1XBPP // only for MMX
44 extern const uint8_t dither_8x8_32
[8][8];
45 extern const uint8_t dither_8x8_73
[8][8];
46 extern const uint8_t dither_8x8_220
[8][8];
50 /* hope these constant values are cache line aligned */
51 DECLARE_ASM_CONST(8, uint64_t, mmx_00ffw
) = 0x00ff00ff00ff00ffULL
;
52 DECLARE_ASM_CONST(8, uint64_t, mmx_redmask
) = 0xf8f8f8f8f8f8f8f8ULL
;
53 DECLARE_ASM_CONST(8, uint64_t, mmx_grnmask
) = 0xfcfcfcfcfcfcfcfcULL
;
62 #define RENAME(a) a ## _MMX
63 #include "yuv2rgb_template.c"
70 #define RENAME(a) a ## _MMX2
71 #include "yuv2rgb_template.c"
75 const int32_t Inverse_Table_6_9
[8][4] = {
76 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
77 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
78 {104597, 132201, 25675, 53279}, /* unspecified */
79 {104597, 132201, 25675, 53279}, /* reserved */
80 {104448, 132798, 24759, 53109}, /* FCC */
81 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
82 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
83 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
89 r = (void *)c->table_rV[V]; \
90 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
91 b = (void *)c->table_bU[U];
95 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
97 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
101 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
103 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
107 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
109 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
113 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
115 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
119 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
121 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
125 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
127 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
129 #define PROLOG(func_name, dst_type) \
130 static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
131 int srcSliceH, uint8_t* dst[], int dstStride[]){\
134 if (c->srcFormat == PIX_FMT_YUV422P){\
138 for (y=0; y<srcSliceH; y+=2){\
139 dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
140 dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
141 dst_type av_unused *r, *b;\
143 uint8_t *py_1= src[0] + y*srcStride[0];\
144 uint8_t *py_2= py_1 + srcStride[0];\
145 uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
146 uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
147 unsigned int h_size= c->dstW>>3;\
152 #define EPILOG1(dst_delta)\
161 int av_unused Y, U, V;\
169 #define EPILOG(dst_delta)\
173 PROLOG(yuv2rgb_c_32
, uint32_t)
199 PROLOG(yuv2rgb_c_24_rgb
, uint8_t)
225 // only trivial mods from yuv2rgb_c_24_rgb
226 PROLOG(yuv2rgb_c_24_bgr
, uint8_t)
252 // This is exactly the same code as yuv2rgb_c_32 except for the types of
253 // r, g, b, dst_1, dst_2
254 PROLOG(yuv2rgb_c_16
, uint16_t)
272 // This is exactly the same code as yuv2rgb_c_32 except for the types of
273 // r, g, b, dst_1, dst_2
274 PROLOG(yuv2rgb_c_8
, uint8_t)
292 // r, g, b, dst_1, dst_2
293 PROLOG(yuv2rgb_c_8_ordered_dither
, uint8_t)
294 const uint8_t *d32
= dither_8x8_32
[y
&7];
295 const uint8_t *d64
= dither_8x8_73
[y
&7];
296 #define DST1bpp8(i,o) \
298 dst_1[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
300 dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
302 #define DST2bpp8(i,o) \
304 dst_2[2*i] = r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]]; \
306 dst_2[2*i+1] = r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
327 // This is exactly the same code as yuv2rgb_c_32 except for the types of
328 // r, g, b, dst_1, dst_2
329 PROLOG(yuv2rgb_c_4
, uint8_t)
333 acc = r[Y] + g[Y] + b[Y]; \
335 acc |= (r[Y] + g[Y] + b[Y])<<4; \
340 acc = r[Y] + g[Y] + b[Y]; \
342 acc |= (r[Y] + g[Y] + b[Y])<<4; \
362 PROLOG(yuv2rgb_c_4_ordered_dither
, uint8_t)
363 const uint8_t *d64
= dither_8x8_73
[y
&7];
364 const uint8_t *d128
=dither_8x8_220
[y
&7];
367 #define DST1bpp4(i,o) \
369 acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
371 acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
374 #define DST2bpp4(i,o) \
376 acc = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
378 acc |= (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
399 // This is exactly the same code as yuv2rgb_c_32 except for the types of
400 // r, g, b, dst_1, dst_2
401 PROLOG(yuv2rgb_c_4b
, uint8_t)
419 PROLOG(yuv2rgb_c_4b_ordered_dither
, uint8_t)
420 const uint8_t *d64
= dither_8x8_73
[y
&7];
421 const uint8_t *d128
=dither_8x8_220
[y
&7];
423 #define DST1bpp4b(i,o) \
425 dst_1[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
427 dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
429 #define DST2bpp4b(i,o) \
431 dst_2[2*i] = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
433 dst_2[2*i+1] = r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
453 PROLOG(yuv2rgb_c_1_ordered_dither
, uint8_t)
454 const uint8_t *d128
=dither_8x8_220
[y
&7];
455 char out_1
=0, out_2
=0;
456 g
= c
->table_gU
[128] + c
->table_gV
[128];
458 #define DST1bpp1(i,o) \
460 out_1+= out_1 + g[Y+d128[0+o]]; \
462 out_1+= out_1 + g[Y+d128[1+o]];
464 #define DST2bpp1(i,o) \
466 out_2+= out_2 + g[Y+d128[8+o]]; \
468 out_2+= out_2 + g[Y+d128[9+o]];
486 SwsFunc
yuv2rgb_get_func_ptr (SwsContext
*c
)
488 #if defined(HAVE_MMX2) || defined(HAVE_MMX)
489 if (c
->flags
& SWS_CPU_CAPS_MMX2
){
490 switch(c
->dstFormat
){
491 case PIX_FMT_RGB32
: return yuv420_rgb32_MMX2
;
492 case PIX_FMT_BGR24
: return yuv420_rgb24_MMX2
;
493 case PIX_FMT_RGB565
: return yuv420_rgb16_MMX2
;
494 case PIX_FMT_RGB555
: return yuv420_rgb15_MMX2
;
497 if (c
->flags
& SWS_CPU_CAPS_MMX
){
498 switch(c
->dstFormat
){
499 case PIX_FMT_RGB32
: return yuv420_rgb32_MMX
;
500 case PIX_FMT_BGR24
: return yuv420_rgb24_MMX
;
501 case PIX_FMT_RGB565
: return yuv420_rgb16_MMX
;
502 case PIX_FMT_RGB555
: return yuv420_rgb15_MMX
;
508 SwsFunc t
= yuv2rgb_init_vis(c
);
514 SwsFunc t
= yuv2rgb_init_mlib(c
);
519 if (c
->flags
& SWS_CPU_CAPS_ALTIVEC
)
521 SwsFunc t
= yuv2rgb_init_altivec(c
);
527 if (c
->flags
& SWS_CPU_CAPS_BFIN
)
529 SwsFunc t
= ff_bfin_yuv2rgb_get_func_ptr (c
);
534 av_log(c
, AV_LOG_WARNING
, "No accelerated colorspace conversion found.\n");
536 switch(c
->dstFormat
){
537 case PIX_FMT_BGR32_1
:
538 case PIX_FMT_RGB32_1
:
540 case PIX_FMT_RGB32
: return yuv2rgb_c_32
;
541 case PIX_FMT_RGB24
: return yuv2rgb_c_24_rgb
;
542 case PIX_FMT_BGR24
: return yuv2rgb_c_24_bgr
;
546 case PIX_FMT_BGR555
: return yuv2rgb_c_16
;
548 case PIX_FMT_BGR8
: return yuv2rgb_c_8_ordered_dither
;
550 case PIX_FMT_BGR4
: return yuv2rgb_c_4_ordered_dither
;
551 case PIX_FMT_RGB4_BYTE
:
552 case PIX_FMT_BGR4_BYTE
: return yuv2rgb_c_4b_ordered_dither
;
553 case PIX_FMT_MONOBLACK
: return yuv2rgb_c_1_ordered_dither
;
560 static int div_round (int dividend
, int divisor
)
563 return (dividend
+ (divisor
>>1)) / divisor
;
565 return -((-dividend
+ (divisor
>>1)) / divisor
);
568 int yuv2rgb_c_init_tables (SwsContext
*c
, const int inv_table
[4], int fullRange
, int brightness
, int contrast
, int saturation
)
570 const int isRgb
= c
->dstFormat
==PIX_FMT_RGB32
571 || c
->dstFormat
==PIX_FMT_RGB32_1
572 || c
->dstFormat
==PIX_FMT_BGR24
573 || c
->dstFormat
==PIX_FMT_RGB565
574 || c
->dstFormat
==PIX_FMT_RGB555
575 || c
->dstFormat
==PIX_FMT_RGB8
576 || c
->dstFormat
==PIX_FMT_RGB4
577 || c
->dstFormat
==PIX_FMT_RGB4_BYTE
578 || c
->dstFormat
==PIX_FMT_MONOBLACK
;
579 const int bpp
= fmt_depth(c
->dstFormat
);
581 uint8_t table_Y
[1024];
582 uint32_t *table_32
= 0;
583 uint16_t *table_16
= 0;
584 uint8_t *table_8
= 0;
585 uint8_t *table_332
= 0;
586 uint8_t *table_121
= 0;
587 uint8_t *table_1
= 0;
589 void *table_r
= 0, *table_g
= 0, *table_b
= 0;
592 int64_t crv
= inv_table
[0];
593 int64_t cbu
= inv_table
[1];
594 int64_t cgu
= -inv_table
[2];
595 int64_t cgv
= -inv_table
[3];
599 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
604 crv
= (crv
*224) / 255;
605 cbu
= (cbu
*224) / 255;
606 cgu
= (cgu
*224) / 255;
607 cgv
= (cgv
*224) / 255;
610 cy
= (cy
*contrast
)>>16;
611 crv
= (crv
*contrast
* saturation
)>>32;
612 cbu
= (cbu
*contrast
* saturation
)>>32;
613 cgu
= (cgu
*contrast
* saturation
)>>32;
614 cgv
= (cgv
*contrast
* saturation
)>>32;
615 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
616 oy
-= 256*brightness
;
618 for (i
= 0; i
< 1024; i
++) {
621 j
= (cy
*(((i
- 384)<<16) - oy
) + (1<<31))>>32;
622 j
= (j
< 0) ? 0 : ((j
> 255) ? 255 : j
);
628 table_start
= table_32
= av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
629 base
= (c
->dstFormat
== PIX_FMT_RGB32_1
|| c
->dstFormat
== PIX_FMT_BGR32_1
) ? 8 : 0;
631 entry_size
= sizeof (uint32_t);
632 table_r
= table_32
+ 197;
633 table_b
= table_32
+ 197 + 685;
634 table_g
= table_32
+ 197 + 2*682;
636 for (i
= -197; i
< 256+197; i
++)
637 ((uint32_t *)table_r
)[i
] = table_Y
[i
+384] << ((isRgb
? 16 : 0) + base
);
638 for (i
= -132; i
< 256+132; i
++)
639 ((uint32_t *)table_g
)[i
] = table_Y
[i
+384] << (8 + base
);
640 for (i
= -232; i
< 256+232; i
++)
641 ((uint32_t *)table_b
)[i
] = table_Y
[i
+384] << ((isRgb
? 0 : 16) + base
);
645 table_start
= table_8
= av_malloc ((256 + 2*232) * sizeof (uint8_t));
647 entry_size
= sizeof (uint8_t);
648 table_r
= table_g
= table_b
= table_8
+ 232;
650 for (i
= -232; i
< 256+232; i
++)
651 ((uint8_t * )table_b
)[i
] = table_Y
[i
+384];
656 table_start
= table_16
= av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
658 entry_size
= sizeof (uint16_t);
659 table_r
= table_16
+ 197;
660 table_b
= table_16
+ 197 + 685;
661 table_g
= table_16
+ 197 + 2*682;
663 for (i
= -197; i
< 256+197; i
++) {
664 int j
= table_Y
[i
+384] >> 3;
667 j
<<= ((bpp
==16) ? 11 : 10);
669 ((uint16_t *)table_r
)[i
] = j
;
671 for (i
= -132; i
< 256+132; i
++) {
672 int j
= table_Y
[i
+384] >> ((bpp
==16) ? 2 : 3);
674 ((uint16_t *)table_g
)[i
] = j
<< 5;
676 for (i
= -232; i
< 256+232; i
++) {
677 int j
= table_Y
[i
+384] >> 3;
680 j
<<= ((bpp
==16) ? 11 : 10);
682 ((uint16_t *)table_b
)[i
] = j
;
687 table_start
= table_332
= av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
689 entry_size
= sizeof (uint8_t);
690 table_r
= table_332
+ 197;
691 table_b
= table_332
+ 197 + 685;
692 table_g
= table_332
+ 197 + 2*682;
694 for (i
= -197; i
< 256+197; i
++) {
695 int j
= (table_Y
[i
+384 - 16] + 18)/36;
700 ((uint8_t *)table_r
)[i
] = j
;
702 for (i
= -132; i
< 256+132; i
++) {
703 int j
= (table_Y
[i
+384 - 16] + 18)/36;
708 ((uint8_t *)table_g
)[i
] = j
<< 2;
710 for (i
= -232; i
< 256+232; i
++) {
711 int j
= (table_Y
[i
+384 - 37] + 43)/85;
716 ((uint8_t *)table_b
)[i
] = j
;
721 table_start
= table_121
= av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
723 entry_size
= sizeof (uint8_t);
724 table_r
= table_121
+ 197;
725 table_b
= table_121
+ 197 + 685;
726 table_g
= table_121
+ 197 + 2*682;
728 for (i
= -197; i
< 256+197; i
++) {
729 int j
= table_Y
[i
+384 - 110] >> 7;
734 ((uint8_t *)table_r
)[i
] = j
;
736 for (i
= -132; i
< 256+132; i
++) {
737 int j
= (table_Y
[i
+384 - 37]+ 43)/85;
739 ((uint8_t *)table_g
)[i
] = j
<< 1;
741 for (i
= -232; i
< 256+232; i
++) {
742 int j
=table_Y
[i
+384 - 110] >> 7;
747 ((uint8_t *)table_b
)[i
] = j
;
752 table_start
= table_1
= av_malloc (256*2 * sizeof (uint8_t));
754 entry_size
= sizeof (uint8_t);
756 table_r
= table_b
= NULL
;
758 for (i
= 0; i
< 256+256; i
++) {
759 int j
= table_Y
[i
+ 384 - 110]>>7;
761 ((uint8_t *)table_g
)[i
] = j
;
767 av_log(c
, AV_LOG_ERROR
, "%ibpp not supported by yuv2rgb\n", bpp
);
772 for (i
= 0; i
< 256; i
++) {
773 c
->table_rV
[i
] = (uint8_t *)table_r
+ entry_size
* div_round (crv
* (i
-128), cy
);
774 c
->table_gU
[i
] = (uint8_t *)table_g
+ entry_size
* div_round (cgu
* (i
-128), cy
);
775 c
->table_gV
[i
] = entry_size
* div_round (cgv
* (i
-128), cy
);
776 c
->table_bU
[i
] = (uint8_t *)table_b
+ entry_size
* div_round (cbu
* (i
-128), cy
);
779 av_free(c
->yuvTable
);
780 c
->yuvTable
= table_start
;