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;
57 // The volatile is required because gcc otherwise optimizes some writes away
58 // not knowing that these are read in the ASM block.
59 static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
60 static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
61 static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
62 static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
71 #define RENAME(a) a ## _MMX
72 #include "yuv2rgb_template.c"
79 #define RENAME(a) a ## _MMX2
80 #include "yuv2rgb_template.c"
84 const int32_t Inverse_Table_6_9[8][4] = {
85 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
86 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
87 {104597, 132201, 25675, 53279}, /* unspecified */
88 {104597, 132201, 25675, 53279}, /* reserved */
89 {104448, 132798, 24759, 53109}, /* FCC */
90 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
91 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
92 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
98 r = (void *)c->table_rV[V]; \
99 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
100 b = (void *)c->table_bU[U];
104 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
106 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
110 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
112 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
116 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
118 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
122 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
124 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
128 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
130 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
134 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
136 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
138 #define PROLOG(func_name, dst_type) \
139 static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
140 int srcSliceH, uint8_t* dst[], int dstStride[]){\
143 if (c->srcFormat == PIX_FMT_YUV422P){\
147 for (y=0; y<srcSliceH; y+=2){\
148 dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
149 dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
150 dst_type av_unused *r, *b;\
152 uint8_t *py_1= src[0] + y*srcStride[0];\
153 uint8_t *py_2= py_1 + srcStride[0];\
154 uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
155 uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
156 unsigned int h_size= c->dstW>>3;\
161 #define EPILOG1(dst_delta)\
170 int av_unused Y, U, V;\
178 #define EPILOG(dst_delta)\
182 PROLOG(yuv2rgb_c_32, uint32_t)
208 PROLOG(yuv2rgb_c_24_rgb, uint8_t)
234 // only trivial mods from yuv2rgb_c_24_rgb
235 PROLOG(yuv2rgb_c_24_bgr, uint8_t)
261 // This is exactly the same code as yuv2rgb_c_32 except for the types of
262 // r, g, b, dst_1, dst_2
263 PROLOG(yuv2rgb_c_16, uint16_t)
281 // This is exactly the same code as yuv2rgb_c_32 except for the types of
282 // r, g, b, dst_1, dst_2
283 PROLOG(yuv2rgb_c_8, uint8_t)
301 // r, g, b, dst_1, dst_2
302 PROLOG(yuv2rgb_c_8_ordered_dither, uint8_t)
303 const uint8_t *d32= dither_8x8_32[y&7];
304 const uint8_t *d64= dither_8x8_73[y&7];
305 #define DST1bpp8(i,o) \
307 dst_1[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
309 dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
311 #define DST2bpp8(i,o) \
313 dst_2[2*i] = r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]]; \
315 dst_2[2*i+1] = r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
336 // This is exactly the same code as yuv2rgb_c_32 except for the types of
337 // r, g, b, dst_1, dst_2
338 PROLOG(yuv2rgb_c_4, uint8_t)
342 acc = r[Y] + g[Y] + b[Y]; \
344 acc |= (r[Y] + g[Y] + b[Y])<<4; \
349 acc = r[Y] + g[Y] + b[Y]; \
351 acc |= (r[Y] + g[Y] + b[Y])<<4; \
371 PROLOG(yuv2rgb_c_4_ordered_dither, uint8_t)
372 const uint8_t *d64= dither_8x8_73[y&7];
373 const uint8_t *d128=dither_8x8_220[y&7];
376 #define DST1bpp4(i,o) \
378 acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
380 acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
383 #define DST2bpp4(i,o) \
385 acc = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
387 acc |= (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
408 // This is exactly the same code as yuv2rgb_c_32 except for the types of
409 // r, g, b, dst_1, dst_2
410 PROLOG(yuv2rgb_c_4b, uint8_t)
428 PROLOG(yuv2rgb_c_4b_ordered_dither, uint8_t)
429 const uint8_t *d64= dither_8x8_73[y&7];
430 const uint8_t *d128=dither_8x8_220[y&7];
432 #define DST1bpp4b(i,o) \
434 dst_1[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
436 dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
438 #define DST2bpp4b(i,o) \
440 dst_2[2*i] = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
442 dst_2[2*i+1] = r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
462 PROLOG(yuv2rgb_c_1_ordered_dither, uint8_t)
463 const uint8_t *d128=dither_8x8_220[y&7];
464 char out_1=0, out_2=0;
465 g= c->table_gU[128] + c->table_gV[128];
467 #define DST1bpp1(i,o) \
469 out_1+= out_1 + g[Y+d128[0+o]]; \
471 out_1+= out_1 + g[Y+d128[1+o]];
473 #define DST2bpp1(i,o) \
475 out_2+= out_2 + g[Y+d128[8+o]]; \
477 out_2+= out_2 + g[Y+d128[9+o]];
495 SwsFunc yuv2rgb_get_func_ptr (SwsContext *c)
497 #if defined(HAVE_MMX2) || defined(HAVE_MMX)
498 if (c->flags & SWS_CPU_CAPS_MMX2){
499 switch(c->dstFormat){
500 case PIX_FMT_RGB32: return yuv420_rgb32_MMX2;
501 case PIX_FMT_BGR24: return yuv420_rgb24_MMX2;
502 case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
503 case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
506 if (c->flags & SWS_CPU_CAPS_MMX){
507 switch(c->dstFormat){
508 case PIX_FMT_RGB32: return yuv420_rgb32_MMX;
509 case PIX_FMT_BGR24: return yuv420_rgb24_MMX;
510 case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
511 case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
517 SwsFunc t= yuv2rgb_init_vis(c);
523 SwsFunc t= yuv2rgb_init_mlib(c);
528 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
530 SwsFunc t = yuv2rgb_init_altivec(c);
536 if (c->flags & SWS_CPU_CAPS_BFIN)
538 SwsFunc t = ff_bfin_yuv2rgb_get_func_ptr (c);
543 av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
545 switch(c->dstFormat){
546 case PIX_FMT_BGR32_1:
547 case PIX_FMT_RGB32_1:
549 case PIX_FMT_RGB32: return yuv2rgb_c_32;
550 case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
551 case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
555 case PIX_FMT_BGR555: return yuv2rgb_c_16;
557 case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
559 case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
560 case PIX_FMT_RGB4_BYTE:
561 case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
562 case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
569 static int div_round (int dividend, int divisor)
572 return (dividend + (divisor>>1)) / divisor;
574 return -((-dividend + (divisor>>1)) / divisor);
577 int yuv2rgb_c_init_tables (SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
579 const int isRgb = c->dstFormat==PIX_FMT_RGB32
580 || c->dstFormat==PIX_FMT_RGB32_1
581 || c->dstFormat==PIX_FMT_BGR24
582 || c->dstFormat==PIX_FMT_RGB565
583 || c->dstFormat==PIX_FMT_RGB555
584 || c->dstFormat==PIX_FMT_RGB8
585 || c->dstFormat==PIX_FMT_RGB4
586 || c->dstFormat==PIX_FMT_RGB4_BYTE
587 || c->dstFormat==PIX_FMT_MONOBLACK;
588 const int bpp = fmt_depth(c->dstFormat);
590 uint8_t table_Y[1024];
591 uint32_t *table_32 = 0;
592 uint16_t *table_16 = 0;
593 uint8_t *table_8 = 0;
594 uint8_t *table_332 = 0;
595 uint8_t *table_121 = 0;
596 uint8_t *table_1 = 0;
598 void *table_r = 0, *table_g = 0, *table_b = 0;
601 int64_t crv = inv_table[0];
602 int64_t cbu = inv_table[1];
603 int64_t cgu = -inv_table[2];
604 int64_t cgv = -inv_table[3];
608 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
613 crv= (crv*224) / 255;
614 cbu= (cbu*224) / 255;
615 cgu= (cgu*224) / 255;
616 cgv= (cgv*224) / 255;
619 cy = (cy *contrast )>>16;
620 crv= (crv*contrast * saturation)>>32;
621 cbu= (cbu*contrast * saturation)>>32;
622 cgu= (cgu*contrast * saturation)>>32;
623 cgv= (cgv*contrast * saturation)>>32;
624 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
625 oy -= 256*brightness;
627 for (i = 0; i < 1024; i++) {
630 j= (cy*(((i - 384)<<16) - oy) + (1<<31))>>32;
631 j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
637 table_start= table_32 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
638 base= (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
640 entry_size = sizeof (uint32_t);
641 table_r = table_32 + 197;
642 table_b = table_32 + 197 + 685;
643 table_g = table_32 + 197 + 2*682;
645 for (i = -197; i < 256+197; i++)
646 ((uint32_t *)table_r)[i] = table_Y[i+384] << ((isRgb ? 16 : 0) + base);
647 for (i = -132; i < 256+132; i++)
648 ((uint32_t *)table_g)[i] = table_Y[i+384] << (8 + base);
649 for (i = -232; i < 256+232; i++)
650 ((uint32_t *)table_b)[i] = table_Y[i+384] << ((isRgb ? 0 : 16) + base);
654 table_start= table_8 = av_malloc ((256 + 2*232) * sizeof (uint8_t));
656 entry_size = sizeof (uint8_t);
657 table_r = table_g = table_b = table_8 + 232;
659 for (i = -232; i < 256+232; i++)
660 ((uint8_t * )table_b)[i] = table_Y[i+384];
665 table_start= table_16 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
667 entry_size = sizeof (uint16_t);
668 table_r = table_16 + 197;
669 table_b = table_16 + 197 + 685;
670 table_g = table_16 + 197 + 2*682;
672 for (i = -197; i < 256+197; i++) {
673 int j = table_Y[i+384] >> 3;
676 j <<= ((bpp==16) ? 11 : 10);
678 ((uint16_t *)table_r)[i] = j;
680 for (i = -132; i < 256+132; i++) {
681 int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
683 ((uint16_t *)table_g)[i] = j << 5;
685 for (i = -232; i < 256+232; i++) {
686 int j = table_Y[i+384] >> 3;
689 j <<= ((bpp==16) ? 11 : 10);
691 ((uint16_t *)table_b)[i] = j;
696 table_start= table_332 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
698 entry_size = sizeof (uint8_t);
699 table_r = table_332 + 197;
700 table_b = table_332 + 197 + 685;
701 table_g = table_332 + 197 + 2*682;
703 for (i = -197; i < 256+197; i++) {
704 int j = (table_Y[i+384 - 16] + 18)/36;
709 ((uint8_t *)table_r)[i] = j;
711 for (i = -132; i < 256+132; i++) {
712 int j = (table_Y[i+384 - 16] + 18)/36;
717 ((uint8_t *)table_g)[i] = j << 2;
719 for (i = -232; i < 256+232; i++) {
720 int j = (table_Y[i+384 - 37] + 43)/85;
725 ((uint8_t *)table_b)[i] = j;
730 table_start= table_121 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
732 entry_size = sizeof (uint8_t);
733 table_r = table_121 + 197;
734 table_b = table_121 + 197 + 685;
735 table_g = table_121 + 197 + 2*682;
737 for (i = -197; i < 256+197; i++) {
738 int j = table_Y[i+384 - 110] >> 7;
743 ((uint8_t *)table_r)[i] = j;
745 for (i = -132; i < 256+132; i++) {
746 int j = (table_Y[i+384 - 37]+ 43)/85;
748 ((uint8_t *)table_g)[i] = j << 1;
750 for (i = -232; i < 256+232; i++) {
751 int j =table_Y[i+384 - 110] >> 7;
756 ((uint8_t *)table_b)[i] = j;
761 table_start= table_1 = av_malloc (256*2 * sizeof (uint8_t));
763 entry_size = sizeof (uint8_t);
765 table_r = table_b = NULL;
767 for (i = 0; i < 256+256; i++) {
768 int j = table_Y[i + 384 - 110]>>7;
770 ((uint8_t *)table_g)[i] = j;
776 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
781 for (i = 0; i < 256; i++) {
782 c->table_rV[i] = (uint8_t *)table_r + entry_size * div_round (crv * (i-128), cy);
783 c->table_gU[i] = (uint8_t *)table_g + entry_size * div_round (cgu * (i-128), cy);
784 c->table_gV[i] = entry_size * div_round (cgv * (i-128), cy);
785 c->table_bU[i] = (uint8_t *)table_b + entry_size * div_round (cbu * (i-128), cy);
788 av_free(c->yuvTable);
789 c->yuvTable= table_start;