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_2x2_4[2][8];
45 extern const uint8_t dither_2x2_8[2][8];
46 extern const uint8_t dither_8x8_32[8][8];
47 extern const uint8_t dither_8x8_73[8][8];
48 extern const uint8_t dither_8x8_220[8][8];
52 /* hope these constant values are cache line aligned */
53 DECLARE_ASM_CONST(8, uint64_t, mmx_00ffw) = 0x00ff00ff00ff00ffULL;
54 DECLARE_ASM_CONST(8, uint64_t, mmx_redmask) = 0xf8f8f8f8f8f8f8f8ULL;
55 DECLARE_ASM_CONST(8, uint64_t, mmx_grnmask) = 0xfcfcfcfcfcfcfcfcULL;
64 #define RENAME(a) a ## _MMX
65 #include "yuv2rgb_template.c"
72 #define RENAME(a) a ## _MMX2
73 #include "yuv2rgb_template.c"
77 const int32_t Inverse_Table_6_9[8][4] = {
78 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
79 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
80 {104597, 132201, 25675, 53279}, /* unspecified */
81 {104597, 132201, 25675, 53279}, /* reserved */
82 {104448, 132798, 24759, 53109}, /* FCC */
83 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
84 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
85 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
91 r = (void *)c->table_rV[V]; \
92 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
93 b = (void *)c->table_bU[U];
97 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
99 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
103 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
105 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
109 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
111 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
115 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
117 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
121 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
123 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
127 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
129 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
131 #define PROLOG(func_name, dst_type) \
132 static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
133 int srcSliceH, uint8_t* dst[], int dstStride[]){\
136 if (c->srcFormat == PIX_FMT_YUV422P){\
140 for (y=0; y<srcSliceH; y+=2){\
141 dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
142 dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
143 dst_type av_unused *r, *b;\
145 uint8_t *py_1= src[0] + y*srcStride[0];\
146 uint8_t *py_2= py_1 + srcStride[0];\
147 uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
148 uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
149 unsigned int h_size= c->dstW>>3;\
154 #define EPILOG1(dst_delta)\
163 int av_unused Y, U, V;\
171 #define EPILOG(dst_delta)\
175 PROLOG(yuv2rgb_c_32, uint32_t)
201 PROLOG(yuv2rgb_c_24_rgb, uint8_t)
227 // only trivial mods from yuv2rgb_c_24_rgb
228 PROLOG(yuv2rgb_c_24_bgr, uint8_t)
254 // This is exactly the same code as yuv2rgb_c_32 except for the types of
255 // r, g, b, dst_1, dst_2
256 PROLOG(yuv2rgb_c_16, uint16_t)
274 // This is exactly the same code as yuv2rgb_c_32 except for the types of
275 // r, g, b, dst_1, dst_2
276 PROLOG(yuv2rgb_c_8, uint8_t)
294 // r, g, b, dst_1, dst_2
295 PROLOG(yuv2rgb_c_8_ordered_dither, uint8_t)
296 const uint8_t *d32= dither_8x8_32[y&7];
297 const uint8_t *d64= dither_8x8_73[y&7];
298 #define DST1bpp8(i,o) \
300 dst_1[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
302 dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
304 #define DST2bpp8(i,o) \
306 dst_2[2*i] = r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]]; \
308 dst_2[2*i+1] = r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
329 // This is exactly the same code as yuv2rgb_c_32 except for the types of
330 // r, g, b, dst_1, dst_2
331 PROLOG(yuv2rgb_c_4, uint8_t)
335 acc = r[Y] + g[Y] + b[Y]; \
337 acc |= (r[Y] + g[Y] + b[Y])<<4; \
342 acc = r[Y] + g[Y] + b[Y]; \
344 acc |= (r[Y] + g[Y] + b[Y])<<4; \
364 PROLOG(yuv2rgb_c_4_ordered_dither, uint8_t)
365 const uint8_t *d64= dither_8x8_73[y&7];
366 const uint8_t *d128=dither_8x8_220[y&7];
369 #define DST1bpp4(i,o) \
371 acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
373 acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
376 #define DST2bpp4(i,o) \
378 acc = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
380 acc |= (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
401 // This is exactly the same code as yuv2rgb_c_32 except for the types of
402 // r, g, b, dst_1, dst_2
403 PROLOG(yuv2rgb_c_4b, uint8_t)
421 PROLOG(yuv2rgb_c_4b_ordered_dither, uint8_t)
422 const uint8_t *d64= dither_8x8_73[y&7];
423 const uint8_t *d128=dither_8x8_220[y&7];
425 #define DST1bpp4b(i,o) \
427 dst_1[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
429 dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
431 #define DST2bpp4b(i,o) \
433 dst_2[2*i] = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
435 dst_2[2*i+1] = r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
455 PROLOG(yuv2rgb_c_1_ordered_dither, uint8_t)
456 const uint8_t *d128=dither_8x8_220[y&7];
457 char out_1=0, out_2=0;
458 g= c->table_gU[128] + c->table_gV[128];
460 #define DST1bpp1(i,o) \
462 out_1+= out_1 + g[Y+d128[0+o]]; \
464 out_1+= out_1 + g[Y+d128[1+o]];
466 #define DST2bpp1(i,o) \
468 out_2+= out_2 + g[Y+d128[8+o]]; \
470 out_2+= out_2 + g[Y+d128[9+o]];
488 SwsFunc yuv2rgb_get_func_ptr (SwsContext *c)
490 #if defined(HAVE_MMX2) || defined(HAVE_MMX)
491 if (c->flags & SWS_CPU_CAPS_MMX2){
492 switch(c->dstFormat){
493 case PIX_FMT_RGB32: return yuv420_rgb32_MMX2;
494 case PIX_FMT_BGR24: return yuv420_rgb24_MMX2;
495 case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
496 case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
499 if (c->flags & SWS_CPU_CAPS_MMX){
500 switch(c->dstFormat){
501 case PIX_FMT_RGB32: return yuv420_rgb32_MMX;
502 case PIX_FMT_BGR24: return yuv420_rgb24_MMX;
503 case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
504 case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
510 SwsFunc t= yuv2rgb_init_vis(c);
516 SwsFunc t= yuv2rgb_init_mlib(c);
521 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
523 SwsFunc t = yuv2rgb_init_altivec(c);
529 if (c->flags & SWS_CPU_CAPS_BFIN)
531 SwsFunc t = ff_bfin_yuv2rgb_get_func_ptr (c);
536 av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
538 switch(c->dstFormat){
539 case PIX_FMT_BGR32_1:
540 case PIX_FMT_RGB32_1:
542 case PIX_FMT_RGB32: return yuv2rgb_c_32;
543 case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
544 case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
548 case PIX_FMT_BGR555: return yuv2rgb_c_16;
550 case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
552 case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
553 case PIX_FMT_RGB4_BYTE:
554 case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
555 case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
562 static int div_round (int dividend, int divisor)
565 return (dividend + (divisor>>1)) / divisor;
567 return -((-dividend + (divisor>>1)) / divisor);
570 int yuv2rgb_c_init_tables (SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
572 const int isRgb = c->dstFormat==PIX_FMT_RGB32
573 || c->dstFormat==PIX_FMT_RGB32_1
574 || c->dstFormat==PIX_FMT_BGR24
575 || c->dstFormat==PIX_FMT_RGB565
576 || c->dstFormat==PIX_FMT_RGB555
577 || c->dstFormat==PIX_FMT_RGB8
578 || c->dstFormat==PIX_FMT_RGB4
579 || c->dstFormat==PIX_FMT_RGB4_BYTE
580 || c->dstFormat==PIX_FMT_MONOBLACK;
581 const int bpp = fmt_depth(c->dstFormat);
583 uint8_t table_Y[1024];
584 uint32_t *table_32 = 0;
585 uint16_t *table_16 = 0;
586 uint8_t *table_8 = 0;
587 uint8_t *table_332 = 0;
588 uint8_t *table_121 = 0;
589 uint8_t *table_1 = 0;
591 void *table_r = 0, *table_g = 0, *table_b = 0;
594 int64_t crv = inv_table[0];
595 int64_t cbu = inv_table[1];
596 int64_t cgu = -inv_table[2];
597 int64_t cgv = -inv_table[3];
601 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
606 crv= (crv*224) / 255;
607 cbu= (cbu*224) / 255;
608 cgu= (cgu*224) / 255;
609 cgv= (cgv*224) / 255;
612 cy = (cy *contrast )>>16;
613 crv= (crv*contrast * saturation)>>32;
614 cbu= (cbu*contrast * saturation)>>32;
615 cgu= (cgu*contrast * saturation)>>32;
616 cgv= (cgv*contrast * saturation)>>32;
617 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
618 oy -= 256*brightness;
620 for (i = 0; i < 1024; i++) {
623 j= (cy*(((i - 384)<<16) - oy) + (1<<31))>>32;
624 j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
630 table_start= table_32 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
631 base= (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
633 entry_size = sizeof (uint32_t);
634 table_r = table_32 + 197;
635 table_b = table_32 + 197 + 685;
636 table_g = table_32 + 197 + 2*682;
638 for (i = -197; i < 256+197; i++)
639 ((uint32_t *)table_r)[i] = table_Y[i+384] << ((isRgb ? 16 : 0) + base);
640 for (i = -132; i < 256+132; i++)
641 ((uint32_t *)table_g)[i] = table_Y[i+384] << (8 + base);
642 for (i = -232; i < 256+232; i++)
643 ((uint32_t *)table_b)[i] = table_Y[i+384] << ((isRgb ? 0 : 16) + base);
647 table_start= table_8 = av_malloc ((256 + 2*232) * sizeof (uint8_t));
649 entry_size = sizeof (uint8_t);
650 table_r = table_g = table_b = table_8 + 232;
652 for (i = -232; i < 256+232; i++)
653 ((uint8_t * )table_b)[i] = table_Y[i+384];
658 table_start= table_16 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
660 entry_size = sizeof (uint16_t);
661 table_r = table_16 + 197;
662 table_b = table_16 + 197 + 685;
663 table_g = table_16 + 197 + 2*682;
665 for (i = -197; i < 256+197; i++) {
666 int j = table_Y[i+384] >> 3;
669 j <<= ((bpp==16) ? 11 : 10);
671 ((uint16_t *)table_r)[i] = j;
673 for (i = -132; i < 256+132; i++) {
674 int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
676 ((uint16_t *)table_g)[i] = j << 5;
678 for (i = -232; i < 256+232; i++) {
679 int j = table_Y[i+384] >> 3;
682 j <<= ((bpp==16) ? 11 : 10);
684 ((uint16_t *)table_b)[i] = j;
689 table_start= table_332 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
691 entry_size = sizeof (uint8_t);
692 table_r = table_332 + 197;
693 table_b = table_332 + 197 + 685;
694 table_g = table_332 + 197 + 2*682;
696 for (i = -197; i < 256+197; i++) {
697 int j = (table_Y[i+384 - 16] + 18)/36;
702 ((uint8_t *)table_r)[i] = j;
704 for (i = -132; i < 256+132; i++) {
705 int j = (table_Y[i+384 - 16] + 18)/36;
710 ((uint8_t *)table_g)[i] = j << 2;
712 for (i = -232; i < 256+232; i++) {
713 int j = (table_Y[i+384 - 37] + 43)/85;
718 ((uint8_t *)table_b)[i] = j;
723 table_start= table_121 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
725 entry_size = sizeof (uint8_t);
726 table_r = table_121 + 197;
727 table_b = table_121 + 197 + 685;
728 table_g = table_121 + 197 + 2*682;
730 for (i = -197; i < 256+197; i++) {
731 int j = table_Y[i+384 - 110] >> 7;
736 ((uint8_t *)table_r)[i] = j;
738 for (i = -132; i < 256+132; i++) {
739 int j = (table_Y[i+384 - 37]+ 43)/85;
741 ((uint8_t *)table_g)[i] = j << 1;
743 for (i = -232; i < 256+232; i++) {
744 int j =table_Y[i+384 - 110] >> 7;
749 ((uint8_t *)table_b)[i] = j;
754 table_start= table_1 = av_malloc (256*2 * sizeof (uint8_t));
756 entry_size = sizeof (uint8_t);
758 table_r = table_b = NULL;
760 for (i = 0; i < 256+256; i++) {
761 int j = table_Y[i + 384 - 110]>>7;
763 ((uint8_t *)table_g)[i] = j;
769 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
774 for (i = 0; i < 256; i++) {
775 c->table_rV[i] = (uint8_t *)table_r + entry_size * div_round (crv * (i-128), cy);
776 c->table_gU[i] = (uint8_t *)table_g + entry_size * div_round (cgu * (i-128), cy);
777 c->table_gV[i] = entry_size * div_round (cgv * (i-128), cy);
778 c->table_bU[i] = (uint8_t *)table_b + entry_size * div_round (cbu * (i-128), cy);
781 av_free(c->yuvTable);
782 c->yuvTable= table_start;