2 * yuv2rgb.c, Software YUV to RGB coverter
4 * Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
7 * Functions broken out from display_x11.c and several new modes
8 * added by HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
10 * 15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
12 * This file is part of mpeg2dec, a free MPEG-2 video decoder
14 * mpeg2dec is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * mpeg2dec is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU Make; see the file COPYING. If not, write to
26 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 * MMX/MMX2 Template stuff from Michael Niedermayer (michaelni@gmx.at) (needed for fast movntq support)
36 //#include "video_out.h"
38 #include "../cpudetect.h"
39 #include "../mangle.h"
40 #include "../mp_msg.h"
43 #include "yuv2rgb_mlib.c"
46 #define DITHER1XBPP // only for mmx
49 #define CAN_COMPILE_X86_ASM
52 #ifdef CAN_COMPILE_X86_ASM
54 /* hope these constant values are cache line aligned */
55 uint64_t __attribute__((aligned(8))) mmx_80w = 0x0080008000800080;
56 uint64_t __attribute__((aligned(8))) mmx_10w = 0x1010101010101010;
57 uint64_t __attribute__((aligned(8))) mmx_00ffw = 0x00ff00ff00ff00ff;
58 uint64_t __attribute__((aligned(8))) mmx_Y_coeff = 0x253f253f253f253f;
60 /* hope these constant values are cache line aligned */
61 uint64_t __attribute__((aligned(8))) mmx_U_green = 0xf37df37df37df37d;
62 uint64_t __attribute__((aligned(8))) mmx_U_blue = 0x4093409340934093;
63 uint64_t __attribute__((aligned(8))) mmx_V_red = 0x3312331233123312;
64 uint64_t __attribute__((aligned(8))) mmx_V_green = 0xe5fce5fce5fce5fc;
66 /* hope these constant values are cache line aligned */
67 uint64_t __attribute__((aligned(8))) mmx_redmask = 0xf8f8f8f8f8f8f8f8;
68 uint64_t __attribute__((aligned(8))) mmx_grnmask = 0xfcfcfcfcfcfcfcfc;
70 uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
71 uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
72 uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
74 // the volatile is required because gcc otherwise optimizes some writes away not knowing that these
75 // are read in the asm block
76 volatile uint64_t __attribute__((aligned(8))) b5Dither;
77 volatile uint64_t __attribute__((aligned(8))) g5Dither;
78 volatile uint64_t __attribute__((aligned(8))) g6Dither;
79 volatile uint64_t __attribute__((aligned(8))) r5Dither;
81 uint64_t __attribute__((aligned(8))) dither4[2]={
83 0x0200020002000200LL,};
85 uint64_t __attribute__((aligned(8))) dither8[2]={
87 0x0004000400040004LL,};
98 #define RENAME(a) a ## _MMX
99 #include "yuv2rgb_template.c"
107 #define RENAME(a) a ## _MMX2
108 #include "yuv2rgb_template.c"
110 #endif // CAN_COMPILE_X86_ASM
113 uint32_t matrix_coefficients = 6;
115 const int32_t Inverse_Table_6_9[8][4] = {
116 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
117 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
118 {104597, 132201, 25675, 53279}, /* unspecified */
119 {104597, 132201, 25675, 53279}, /* reserved */
120 {104448, 132798, 24759, 53109}, /* FCC */
121 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
122 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
123 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
126 static void yuv2rgb_c_init (int bpp, int mode);
130 static void (* yuv2rgb_c_internal) (uint8_t *, uint8_t *,
131 uint8_t *, uint8_t *,
132 void *, void *, int);
134 static void yuv2rgb_c (void * dst, uint8_t * py,
135 uint8_t * pu, uint8_t * pv,
136 int h_size, int v_size,
137 int rgb_stride, int y_stride, int uv_stride)
142 yuv2rgb_c_internal (py, py + y_stride, pu, pv, dst, dst + rgb_stride,
148 dst += 2 * rgb_stride;
152 void yuv2rgb_init (int bpp, int mode)
155 #ifdef CAN_COMPILE_X86_ASM
158 if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
159 yuv2rgb = yuv2rgb_init_MMX2 (bpp, mode);
161 mp_msg(MSGT_SWS,MSGL_INFO,"Using MMX2 for colorspace transform\n");
163 mp_msg(MSGT_SWS,MSGL_WARN,"Cannot init MMX2 colorspace transform\n");
166 else if(gCpuCaps.hasMMX)
168 if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
169 yuv2rgb = yuv2rgb_init_MMX (bpp, mode);
171 mp_msg(MSGT_SWS,MSGL_INFO,"Using MMX for colorspace transform\n");
173 mp_msg(MSGT_SWS,MSGL_WARN,"Cannot init MMX colorspace transform\n");
178 if (yuv2rgb == NULL /*&& (config.flags & VO_MLIB_ENABLE)*/) {
179 yuv2rgb = yuv2rgb_init_mlib (bpp, mode);
181 mp_msg(MSGT_SWS,MSGL_INFO,"Using mlib for colorspace transform\n");
184 if (yuv2rgb == NULL) {
185 mp_msg(MSGT_SWS,MSGL_INFO,"No accelerated colorspace conversion found\n");
186 yuv2rgb_c_init (bpp, mode);
187 yuv2rgb = (yuv2rgb_fun)yuv2rgb_c;
191 void * table_rV[256];
192 void * table_gU[256];
194 void * table_bU[256];
200 g = table_gU[U] + table_gV[V]; \
205 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
207 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
211 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
213 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
217 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
219 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
223 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
225 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
229 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
231 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
235 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
237 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
239 static void yuv2rgb_c_32 (uint8_t * py_1, uint8_t * py_2,
240 uint8_t * pu, uint8_t * pv,
241 void * _dst_1, void * _dst_2, int h_size)
244 uint32_t * r, * g, * b;
245 uint32_t * dst_1, * dst_2;
277 // This is very near from the yuv2rgb_c_32 code
278 static void yuv2rgb_c_24_rgb (uint8_t * py_1, uint8_t * py_2,
279 uint8_t * pu, uint8_t * pv,
280 void * _dst_1, void * _dst_2, int h_size)
283 uint8_t * r, * g, * b;
284 uint8_t * dst_1, * dst_2;
316 // only trivial mods from yuv2rgb_c_24_rgb
317 static void yuv2rgb_c_24_bgr (uint8_t * py_1, uint8_t * py_2,
318 uint8_t * pu, uint8_t * pv,
319 void * _dst_1, void * _dst_2, int h_size)
322 uint8_t * r, * g, * b;
323 uint8_t * dst_1, * dst_2;
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 static void yuv2rgb_c_16 (uint8_t * py_1, uint8_t * py_2,
358 uint8_t * pu, uint8_t * pv,
359 void * _dst_1, void * _dst_2, int h_size)
362 uint16_t * r, * g, * b;
363 uint16_t * dst_1, * dst_2;
395 // This is exactly the same code as yuv2rgb_c_32 except for the types of
396 // r, g, b, dst_1, dst_2
397 static void yuv2rgb_c_8 (uint8_t * py_1, uint8_t * py_2,
398 uint8_t * pu, uint8_t * pv,
399 void * _dst_1, void * _dst_2, int h_size)
402 uint8_t * r, * g, * b;
403 uint8_t * dst_1, * dst_2;
436 static int div_round (int dividend, int divisor)
439 return (dividend + (divisor>>1)) / divisor;
441 return -((-dividend + (divisor>>1)) / divisor);
444 static void yuv2rgb_c_init (int bpp, int mode)
447 uint8_t table_Y[1024];
448 uint32_t *table_32 = 0;
449 uint16_t *table_16 = 0;
450 uint8_t *table_8 = 0;
451 uint8_t *table_332 = 0;
453 void *table_r = 0, *table_g = 0, *table_b = 0;
455 int crv = Inverse_Table_6_9[matrix_coefficients][0];
456 int cbu = Inverse_Table_6_9[matrix_coefficients][1];
457 int cgu = -Inverse_Table_6_9[matrix_coefficients][2];
458 int cgv = -Inverse_Table_6_9[matrix_coefficients][3];
460 for (i = 0; i < 1024; i++) {
463 j = (76309 * (i - 384 - 16) + 32768) >> 16;
464 j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
470 yuv2rgb_c_internal = yuv2rgb_c_32;
472 table_32 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
474 entry_size = sizeof (uint32_t);
475 table_r = table_32 + 197;
476 table_b = table_32 + 197 + 685;
477 table_g = table_32 + 197 + 2*682;
479 for (i = -197; i < 256+197; i++)
480 ((uint32_t *)table_r)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 16 : 0);
481 for (i = -132; i < 256+132; i++)
482 ((uint32_t *)table_g)[i] = table_Y[i+384] << 8;
483 for (i = -232; i < 256+232; i++)
484 ((uint32_t *)table_b)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 0 : 16);
488 // yuv2rgb_c_internal = (mode==MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
489 yuv2rgb_c_internal = (mode!=MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
491 table_8 = malloc ((256 + 2*232) * sizeof (uint8_t));
493 entry_size = sizeof (uint8_t);
494 table_r = table_g = table_b = table_8 + 232;
496 for (i = -232; i < 256+232; i++)
497 ((uint8_t * )table_b)[i] = table_Y[i+384];
502 yuv2rgb_c_internal = yuv2rgb_c_16;
504 table_16 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
506 entry_size = sizeof (uint16_t);
507 table_r = table_16 + 197;
508 table_b = table_16 + 197 + 685;
509 table_g = table_16 + 197 + 2*682;
511 for (i = -197; i < 256+197; i++) {
512 int j = table_Y[i+384] >> 3;
514 if (mode == MODE_RGB)
515 j <<= ((bpp==16) ? 11 : 10);
517 ((uint16_t *)table_r)[i] = j;
519 for (i = -132; i < 256+132; i++) {
520 int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
522 ((uint16_t *)table_g)[i] = j << 5;
524 for (i = -232; i < 256+232; i++) {
525 int j = table_Y[i+384] >> 3;
527 if (mode == MODE_BGR)
528 j <<= ((bpp==16) ? 11 : 10);
530 ((uint16_t *)table_b)[i] = j;
535 yuv2rgb_c_internal = yuv2rgb_c_8;
537 table_332 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
539 entry_size = sizeof (uint8_t);
540 table_r = table_332 + 197;
541 table_b = table_332 + 197 + 685;
542 table_g = table_332 + 197 + 2*682;
544 for (i = -197; i < 256+197; i++) {
545 int j = table_Y[i+384] >> 5;
547 if (mode == MODE_RGB)
550 ((uint8_t *)table_r)[i] = j;
552 for (i = -132; i < 256+132; i++) {
553 int j = table_Y[i+384] >> 5;
555 if (mode == MODE_BGR)
558 ((uint8_t *)table_g)[i] = j << 2;
560 for (i = -232; i < 256+232; i++) {
561 int j = table_Y[i+384] >> 6;
563 if (mode == MODE_BGR)
566 ((uint8_t *)table_b)[i] = j;
571 mp_msg(MSGT_SWS,MSGL_ERR,"%ibpp not supported by yuv2rgb\n", bpp);
575 for (i = 0; i < 256; i++) {
576 table_rV[i] = table_r + entry_size * div_round (crv * (i-128), 76309);
577 table_gU[i] = table_g + entry_size * div_round (cgu * (i-128), 76309);
578 table_gV[i] = entry_size * div_round (cgv * (i-128), 76309);
579 table_bU[i] = table_b + entry_size * div_round (cbu * (i-128), 76309);