2 * 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;
60 #define HAVE_AMD3DNOW 0
61 #define RENAME(a) a ## _MMX
62 #include "yuv2rgb_template.c"
68 #define RENAME(a) a ## _MMX2
69 #include "yuv2rgb_template.c"
73 const int32_t ff_yuv2rgb_coeffs[8][4] = {
74 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
75 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
76 {104597, 132201, 25675, 53279}, /* unspecified */
77 {104597, 132201, 25675, 53279}, /* reserved */
78 {104448, 132798, 24759, 53109}, /* FCC */
79 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
80 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
81 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
87 r = (void *)c->table_rV[V]; \
88 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
89 b = (void *)c->table_bU[U];
93 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
95 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
99 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
101 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
105 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
107 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
111 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
113 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
117 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
119 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
123 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
125 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
127 #define PROLOG(func_name, dst_type) \
128 static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
129 int srcSliceH, uint8_t* dst[], int dstStride[]){\
132 if (c->srcFormat == PIX_FMT_YUV422P){\
136 for (y=0; y<srcSliceH; y+=2){\
137 dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
138 dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
139 dst_type av_unused *r, *b;\
141 uint8_t *py_1= src[0] + y*srcStride[0];\
142 uint8_t *py_2= py_1 + srcStride[0];\
143 uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
144 uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
145 unsigned int h_size= c->dstW>>3;\
150 #define EPILOG1(dst_delta)\
159 int av_unused Y, U, V;\
167 #define EPILOG(dst_delta)\
171 PROLOG(yuv2rgb_c_32, uint32_t)
197 PROLOG(yuv2rgb_c_24_rgb, uint8_t)
223 // only trivial mods from yuv2rgb_c_24_rgb
224 PROLOG(yuv2rgb_c_24_bgr, uint8_t)
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 PROLOG(yuv2rgb_c_16, uint16_t)
270 // This is exactly the same code as yuv2rgb_c_32 except for the types of
271 // r, g, b, dst_1, dst_2
272 PROLOG(yuv2rgb_c_8, uint8_t)
290 // r, g, b, dst_1, dst_2
291 PROLOG(yuv2rgb_c_8_ordered_dither, uint8_t)
292 const uint8_t *d32= dither_8x8_32[y&7];
293 const uint8_t *d64= dither_8x8_73[y&7];
294 #define DST1bpp8(i,o) \
296 dst_1[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
298 dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
300 #define DST2bpp8(i,o) \
302 dst_2[2*i] = r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]]; \
304 dst_2[2*i+1] = r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
325 // This is exactly the same code as yuv2rgb_c_32 except for the types of
326 // r, g, b, dst_1, dst_2
327 PROLOG(yuv2rgb_c_4, uint8_t)
331 acc = r[Y] + g[Y] + b[Y]; \
333 acc |= (r[Y] + g[Y] + b[Y])<<4; \
338 acc = r[Y] + g[Y] + b[Y]; \
340 acc |= (r[Y] + g[Y] + b[Y])<<4; \
360 PROLOG(yuv2rgb_c_4_ordered_dither, uint8_t)
361 const uint8_t *d64= dither_8x8_73[y&7];
362 const uint8_t *d128=dither_8x8_220[y&7];
365 #define DST1bpp4(i,o) \
367 acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
369 acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
372 #define DST2bpp4(i,o) \
374 acc = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
376 acc |= (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
397 // This is exactly the same code as yuv2rgb_c_32 except for the types of
398 // r, g, b, dst_1, dst_2
399 PROLOG(yuv2rgb_c_4b, uint8_t)
417 PROLOG(yuv2rgb_c_4b_ordered_dither, uint8_t)
418 const uint8_t *d64= dither_8x8_73[y&7];
419 const uint8_t *d128=dither_8x8_220[y&7];
421 #define DST1bpp4b(i,o) \
423 dst_1[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
425 dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
427 #define DST2bpp4b(i,o) \
429 dst_2[2*i] = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
431 dst_2[2*i+1] = r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
451 PROLOG(yuv2rgb_c_1_ordered_dither, uint8_t)
452 const uint8_t *d128=dither_8x8_220[y&7];
453 char out_1=0, out_2=0;
454 g= c->table_gU[128] + c->table_gV[128];
456 #define DST1bpp1(i,o) \
458 out_1+= out_1 + g[Y+d128[0+o]]; \
460 out_1+= out_1 + g[Y+d128[1+o]];
462 #define DST2bpp1(i,o) \
464 out_2+= out_2 + g[Y+d128[8+o]]; \
466 out_2+= out_2 + g[Y+d128[9+o]];
484 SwsFunc sws_yuv2rgb_get_func_ptr (SwsContext *c)
486 #if HAVE_MMX2 || HAVE_MMX
487 if (c->flags & SWS_CPU_CAPS_MMX2){
488 switch(c->dstFormat){
489 case PIX_FMT_RGB32: return yuv420_rgb32_MMX2;
490 case PIX_FMT_BGR24: return yuv420_rgb24_MMX2;
491 case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
492 case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
495 if (c->flags & SWS_CPU_CAPS_MMX){
496 switch(c->dstFormat){
497 case PIX_FMT_RGB32: return yuv420_rgb32_MMX;
498 case PIX_FMT_BGR24: return yuv420_rgb24_MMX;
499 case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
500 case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
506 SwsFunc t= sws_yuv2rgb_init_vis(c);
512 SwsFunc t= sws_yuv2rgb_init_mlib(c);
517 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
519 SwsFunc t = sws_yuv2rgb_init_altivec(c);
525 if (c->flags & SWS_CPU_CAPS_BFIN)
527 SwsFunc t = ff_bfin_yuv2rgb_get_func_ptr (c);
532 av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
534 switch(c->dstFormat){
535 case PIX_FMT_BGR32_1:
536 case PIX_FMT_RGB32_1:
538 case PIX_FMT_RGB32: return yuv2rgb_c_32;
539 case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
540 case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
544 case PIX_FMT_BGR555: return yuv2rgb_c_16;
546 case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
548 case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
549 case PIX_FMT_RGB4_BYTE:
550 case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
551 case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
558 static int div_round (int dividend, int divisor)
561 return (dividend + (divisor>>1)) / divisor;
563 return -((-dividend + (divisor>>1)) / divisor);
566 int sws_yuv2rgb_c_init_tables (SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
568 const int isRgb = c->dstFormat==PIX_FMT_RGB32
569 || c->dstFormat==PIX_FMT_RGB32_1
570 || c->dstFormat==PIX_FMT_BGR24
571 || c->dstFormat==PIX_FMT_RGB565
572 || c->dstFormat==PIX_FMT_RGB555
573 || c->dstFormat==PIX_FMT_RGB8
574 || c->dstFormat==PIX_FMT_RGB4
575 || c->dstFormat==PIX_FMT_RGB4_BYTE
576 || c->dstFormat==PIX_FMT_MONOBLACK;
577 const int bpp = fmt_depth(c->dstFormat);
579 uint8_t table_Y[1024];
580 uint32_t *table_32 = 0;
581 uint16_t *table_16 = 0;
582 uint8_t *table_8 = 0;
583 uint8_t *table_332 = 0;
584 uint8_t *table_121 = 0;
585 uint8_t *table_1 = 0;
587 void *table_r = 0, *table_g = 0, *table_b = 0;
590 int64_t crv = inv_table[0];
591 int64_t cbu = inv_table[1];
592 int64_t cgu = -inv_table[2];
593 int64_t cgv = -inv_table[3];
597 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
602 crv= (crv*224) / 255;
603 cbu= (cbu*224) / 255;
604 cgu= (cgu*224) / 255;
605 cgv= (cgv*224) / 255;
608 cy = (cy *contrast )>>16;
609 crv= (crv*contrast * saturation)>>32;
610 cbu= (cbu*contrast * saturation)>>32;
611 cgu= (cgu*contrast * saturation)>>32;
612 cgv= (cgv*contrast * saturation)>>32;
613 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
614 oy -= 256*brightness;
616 for (i = 0; i < 1024; i++) {
619 j= (cy*(((i - 384)<<16) - oy) + (1<<31))>>32;
620 j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
626 table_start= table_32 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
627 base= (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
629 entry_size = sizeof (uint32_t);
630 table_r = table_32 + 197;
631 table_b = table_32 + 197 + 685;
632 table_g = table_32 + 197 + 2*682;
634 for (i = -197; i < 256+197; i++)
635 ((uint32_t *)table_r)[i] = table_Y[i+384] << ((isRgb ? 16 : 0) + base);
636 for (i = -132; i < 256+132; i++)
637 ((uint32_t *)table_g)[i] = table_Y[i+384] << (8 + base);
638 for (i = -232; i < 256+232; i++)
639 ((uint32_t *)table_b)[i] = table_Y[i+384] << ((isRgb ? 0 : 16) + base);
643 table_start= table_8 = av_malloc ((256 + 2*232) * sizeof (uint8_t));
645 entry_size = sizeof (uint8_t);
646 table_r = table_g = table_b = table_8 + 232;
648 for (i = -232; i < 256+232; i++)
649 ((uint8_t * )table_b)[i] = table_Y[i+384];
654 table_start= table_16 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
656 entry_size = sizeof (uint16_t);
657 table_r = table_16 + 197;
658 table_b = table_16 + 197 + 685;
659 table_g = table_16 + 197 + 2*682;
661 for (i = -197; i < 256+197; i++) {
662 int j = table_Y[i+384] >> 3;
665 j <<= ((bpp==16) ? 11 : 10);
667 ((uint16_t *)table_r)[i] = j;
669 for (i = -132; i < 256+132; i++) {
670 int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
672 ((uint16_t *)table_g)[i] = j << 5;
674 for (i = -232; i < 256+232; i++) {
675 int j = table_Y[i+384] >> 3;
678 j <<= ((bpp==16) ? 11 : 10);
680 ((uint16_t *)table_b)[i] = j;
685 table_start= table_332 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
687 entry_size = sizeof (uint8_t);
688 table_r = table_332 + 197;
689 table_b = table_332 + 197 + 685;
690 table_g = table_332 + 197 + 2*682;
692 for (i = -197; i < 256+197; i++) {
693 int j = (table_Y[i+384 - 16] + 18)/36;
698 ((uint8_t *)table_r)[i] = j;
700 for (i = -132; i < 256+132; i++) {
701 int j = (table_Y[i+384 - 16] + 18)/36;
706 ((uint8_t *)table_g)[i] = j << 2;
708 for (i = -232; i < 256+232; i++) {
709 int j = (table_Y[i+384 - 37] + 43)/85;
714 ((uint8_t *)table_b)[i] = j;
719 table_start= table_121 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
721 entry_size = sizeof (uint8_t);
722 table_r = table_121 + 197;
723 table_b = table_121 + 197 + 685;
724 table_g = table_121 + 197 + 2*682;
726 for (i = -197; i < 256+197; i++) {
727 int j = table_Y[i+384 - 110] >> 7;
732 ((uint8_t *)table_r)[i] = j;
734 for (i = -132; i < 256+132; i++) {
735 int j = (table_Y[i+384 - 37]+ 43)/85;
737 ((uint8_t *)table_g)[i] = j << 1;
739 for (i = -232; i < 256+232; i++) {
740 int j =table_Y[i+384 - 110] >> 7;
745 ((uint8_t *)table_b)[i] = j;
750 table_start= table_1 = av_malloc (256*2 * sizeof (uint8_t));
752 entry_size = sizeof (uint8_t);
754 table_r = table_b = NULL;
756 for (i = 0; i < 256+256; i++) {
757 int j = table_Y[i + 384 - 110]>>7;
759 ((uint8_t *)table_g)[i] = j;
765 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
770 for (i = 0; i < 256; i++) {
771 c->table_rV[i] = (uint8_t *)table_r + entry_size * div_round (crv * (i-128), cy);
772 c->table_gU[i] = (uint8_t *)table_g + entry_size * div_round (cgu * (i-128), cy);
773 c->table_gV[i] = entry_size * div_round (cgv * (i-128), cy);
774 c->table_bU[i] = (uint8_t *)table_b + entry_size * div_round (cbu * (i-128), cy);
777 av_free(c->yuvTable);
778 c->yuvTable= table_start;