2732
|
1 /*
|
|
2 * yuv2rgb.c, Software YUV to RGB coverter
|
|
3 *
|
|
4 * Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
|
5 * All Rights Reserved.
|
|
6 *
|
|
7 * Functions broken out from display_x11.c and several new modes
|
|
8 * added by Håkan Hjort <d95hjort@dtek.chalmers.se>
|
|
9 *
|
|
10 * 15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
|
|
11 *
|
|
12 * This file is part of mpeg2dec, a free MPEG-2 video decoder
|
|
13 *
|
|
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)
|
|
17 * any later version.
|
|
18 *
|
|
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.
|
|
23 *
|
|
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.
|
|
27 *
|
3143
|
28 * MMX/MMX2 Template stuff from Michael Niedermayer (michaelni@gmx.at) (needed for fast movntq support)
|
2732
|
29 */
|
|
30
|
|
31 #include <stdio.h>
|
|
32 #include <stdlib.h>
|
|
33 #include <inttypes.h>
|
|
34
|
|
35 #include "config.h"
|
|
36 //#include "video_out.h"
|
|
37 #include "rgb2rgb.h"
|
3143
|
38 #include "../cpudetect.h"
|
2732
|
39
|
|
40 #ifdef HAVE_MLIB
|
|
41 #include "yuv2rgb_mlib.c"
|
|
42 #endif
|
|
43
|
3143
|
44 #define DITHER1XBPP // only for mmx
|
|
45
|
|
46 #ifdef ARCH_X86
|
|
47 #define CAN_COMPILE_X86_ASM
|
|
48 #endif
|
|
49
|
|
50 #ifdef CAN_COMPILE_X86_ASM
|
|
51
|
|
52 /* hope these constant values are cache line aligned */
|
|
53 uint64_t __attribute__((aligned(8))) mmx_80w = 0x0080008000800080;
|
|
54 uint64_t __attribute__((aligned(8))) mmx_10w = 0x1010101010101010;
|
|
55 uint64_t __attribute__((aligned(8))) mmx_00ffw = 0x00ff00ff00ff00ff;
|
|
56 uint64_t __attribute__((aligned(8))) mmx_Y_coeff = 0x253f253f253f253f;
|
|
57
|
|
58 /* hope these constant values are cache line aligned */
|
|
59 uint64_t __attribute__((aligned(8))) mmx_U_green = 0xf37df37df37df37d;
|
|
60 uint64_t __attribute__((aligned(8))) mmx_U_blue = 0x4093409340934093;
|
|
61 uint64_t __attribute__((aligned(8))) mmx_V_red = 0x3312331233123312;
|
|
62 uint64_t __attribute__((aligned(8))) mmx_V_green = 0xe5fce5fce5fce5fc;
|
|
63
|
|
64 /* hope these constant values are cache line aligned */
|
|
65 uint64_t __attribute__((aligned(8))) mmx_redmask = 0xf8f8f8f8f8f8f8f8;
|
|
66 uint64_t __attribute__((aligned(8))) mmx_grnmask = 0xfcfcfcfcfcfcfcfc;
|
|
67
|
|
68 uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
|
|
69 uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
|
|
70 uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
|
|
71
|
|
72 // the volatile is required because gcc otherwise optimizes some writes away not knowing that these
|
|
73 // are read in the asm block
|
|
74 volatile uint64_t __attribute__((aligned(8))) b5Dither;
|
|
75 volatile uint64_t __attribute__((aligned(8))) g5Dither;
|
|
76 volatile uint64_t __attribute__((aligned(8))) g6Dither;
|
|
77 volatile uint64_t __attribute__((aligned(8))) r5Dither;
|
|
78
|
|
79 uint64_t __attribute__((aligned(8))) dither4[2]={
|
|
80 0x0103010301030103LL,
|
|
81 0x0200020002000200LL,};
|
|
82
|
|
83 uint64_t __attribute__((aligned(8))) dither8[2]={
|
|
84 0x0602060206020602LL,
|
|
85 0x0004000400040004LL,};
|
|
86
|
|
87 #undef HAVE_MMX
|
|
88 #undef ARCH_X86
|
|
89
|
|
90 //MMX versions
|
|
91 #undef RENAME
|
|
92 #define HAVE_MMX
|
|
93 #undef HAVE_MMX2
|
|
94 #undef HAVE_3DNOW
|
|
95 #define ARCH_X86
|
|
96 #define RENAME(a) a ## _MMX
|
|
97 #include "yuv2rgb_template.c"
|
|
98
|
|
99 //MMX2 versions
|
|
100 #undef RENAME
|
|
101 #define HAVE_MMX
|
|
102 #define HAVE_MMX2
|
|
103 #undef HAVE_3DNOW
|
|
104 #define ARCH_X86
|
|
105 #define RENAME(a) a ## _MMX2
|
|
106 #include "yuv2rgb_template.c"
|
|
107
|
|
108 #endif // CAN_COMPILE_X86_ASM
|
2732
|
109
|
|
110
|
|
111 uint32_t matrix_coefficients = 6;
|
|
112
|
|
113 const int32_t Inverse_Table_6_9[8][4] = {
|
|
114 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
|
|
115 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
|
|
116 {104597, 132201, 25675, 53279}, /* unspecified */
|
|
117 {104597, 132201, 25675, 53279}, /* reserved */
|
|
118 {104448, 132798, 24759, 53109}, /* FCC */
|
|
119 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
|
|
120 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
|
|
121 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
|
|
122 };
|
|
123
|
|
124 static void yuv2rgb_c_init (int bpp, int mode);
|
|
125
|
|
126 yuv2rgb_fun yuv2rgb;
|
|
127
|
|
128 static void (* yuv2rgb_c_internal) (uint8_t *, uint8_t *,
|
|
129 uint8_t *, uint8_t *,
|
|
130 void *, void *, int);
|
|
131
|
3143
|
132 static void yuv2rgb_c (void * dst, uint8_t * py,
|
|
133 uint8_t * pu, uint8_t * pv,
|
|
134 int h_size, int v_size,
|
|
135 int rgb_stride, int y_stride, int uv_stride)
|
2732
|
136 {
|
|
137 v_size >>= 1;
|
|
138
|
|
139 while (v_size--) {
|
|
140 yuv2rgb_c_internal (py, py + y_stride, pu, pv, dst, dst + rgb_stride,
|
|
141 h_size);
|
|
142
|
|
143 py += 2 * y_stride;
|
|
144 pu += uv_stride;
|
|
145 pv += uv_stride;
|
|
146 dst += 2 * rgb_stride;
|
|
147 }
|
|
148 }
|
|
149
|
3143
|
150 void yuv2rgb_init (int bpp, int mode)
|
2732
|
151 {
|
|
152 yuv2rgb = NULL;
|
3143
|
153 #ifdef CAN_COMPILE_X86_ASM
|
|
154 if(gCpuCaps.hasMMX2)
|
|
155 {
|
|
156 if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
|
|
157 yuv2rgb = yuv2rgb_init_MMX2 (bpp, mode);
|
|
158 if (yuv2rgb != NULL)
|
|
159 printf ("Using MMX2 for colorspace transform\n");
|
|
160 else
|
|
161 printf ("Cannot init MMX2 colorspace transform\n");
|
|
162 }
|
|
163 }
|
|
164 else if(gCpuCaps.hasMMX)
|
|
165 {
|
|
166 if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
|
|
167 yuv2rgb = yuv2rgb_init_MMX (bpp, mode);
|
|
168 if (yuv2rgb != NULL)
|
|
169 printf ("Using MMX for colorspace transform\n");
|
|
170 else
|
|
171 printf ("Cannot init MMX colorspace transform\n");
|
|
172 }
|
2732
|
173 }
|
|
174 #endif
|
|
175 #ifdef HAVE_MLIB
|
|
176 if (yuv2rgb == NULL /*&& (config.flags & VO_MLIB_ENABLE)*/) {
|
|
177 yuv2rgb = yuv2rgb_init_mlib (bpp, mode);
|
|
178 if (yuv2rgb != NULL)
|
|
179 printf ("Using mlib for colorspace transform\n");
|
|
180 }
|
|
181 #endif
|
|
182 if (yuv2rgb == NULL) {
|
|
183 printf ("No accelerated colorspace conversion found\n");
|
|
184 yuv2rgb_c_init (bpp, mode);
|
|
185 yuv2rgb = (yuv2rgb_fun)yuv2rgb_c;
|
|
186 }
|
|
187 }
|
|
188
|
|
189 void * table_rV[256];
|
|
190 void * table_gU[256];
|
|
191 int table_gV[256];
|
|
192 void * table_bU[256];
|
|
193
|
|
194 #define RGB(i) \
|
|
195 U = pu[i]; \
|
|
196 V = pv[i]; \
|
|
197 r = table_rV[V]; \
|
|
198 g = table_gU[U] + table_gV[V]; \
|
|
199 b = table_bU[U];
|
|
200
|
|
201 #define DST1(i) \
|
|
202 Y = py_1[2*i]; \
|
|
203 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
|
|
204 Y = py_1[2*i+1]; \
|
|
205 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
|
|
206
|
|
207 #define DST2(i) \
|
|
208 Y = py_2[2*i]; \
|
|
209 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
|
|
210 Y = py_2[2*i+1]; \
|
|
211 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
|
|
212
|
|
213 #define DST1RGB(i) \
|
|
214 Y = py_1[2*i]; \
|
|
215 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
|
|
216 Y = py_1[2*i+1]; \
|
|
217 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
|
|
218
|
|
219 #define DST2RGB(i) \
|
|
220 Y = py_2[2*i]; \
|
|
221 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
|
|
222 Y = py_2[2*i+1]; \
|
|
223 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
|
|
224
|
|
225 #define DST1BGR(i) \
|
|
226 Y = py_1[2*i]; \
|
|
227 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
|
|
228 Y = py_1[2*i+1]; \
|
|
229 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
|
|
230
|
|
231 #define DST2BGR(i) \
|
|
232 Y = py_2[2*i]; \
|
|
233 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
|
|
234 Y = py_2[2*i+1]; \
|
|
235 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
|
|
236
|
|
237 static void yuv2rgb_c_32 (uint8_t * py_1, uint8_t * py_2,
|
|
238 uint8_t * pu, uint8_t * pv,
|
|
239 void * _dst_1, void * _dst_2, int h_size)
|
|
240 {
|
|
241 int U, V, Y;
|
|
242 uint32_t * r, * g, * b;
|
|
243 uint32_t * dst_1, * dst_2;
|
|
244
|
|
245 h_size >>= 3;
|
|
246 dst_1 = _dst_1;
|
|
247 dst_2 = _dst_2;
|
|
248
|
|
249 while (h_size--) {
|
|
250 RGB(0);
|
|
251 DST1(0);
|
|
252 DST2(0);
|
|
253
|
|
254 RGB(1);
|
|
255 DST2(1);
|
|
256 DST1(1);
|
|
257
|
|
258 RGB(2);
|
|
259 DST1(2);
|
|
260 DST2(2);
|
|
261
|
|
262 RGB(3);
|
|
263 DST2(3);
|
|
264 DST1(3);
|
|
265
|
|
266 pu += 4;
|
|
267 pv += 4;
|
|
268 py_1 += 8;
|
|
269 py_2 += 8;
|
|
270 dst_1 += 8;
|
|
271 dst_2 += 8;
|
|
272 }
|
|
273 }
|
|
274
|
|
275 // This is very near from the yuv2rgb_c_32 code
|
|
276 static void yuv2rgb_c_24_rgb (uint8_t * py_1, uint8_t * py_2,
|
|
277 uint8_t * pu, uint8_t * pv,
|
|
278 void * _dst_1, void * _dst_2, int h_size)
|
|
279 {
|
|
280 int U, V, Y;
|
|
281 uint8_t * r, * g, * b;
|
|
282 uint8_t * dst_1, * dst_2;
|
|
283
|
|
284 h_size >>= 3;
|
|
285 dst_1 = _dst_1;
|
|
286 dst_2 = _dst_2;
|
|
287
|
|
288 while (h_size--) {
|
|
289 RGB(0);
|
|
290 DST1RGB(0);
|
|
291 DST2RGB(0);
|
|
292
|
|
293 RGB(1);
|
|
294 DST2RGB(1);
|
|
295 DST1RGB(1);
|
|
296
|
|
297 RGB(2);
|
|
298 DST1RGB(2);
|
|
299 DST2RGB(2);
|
|
300
|
|
301 RGB(3);
|
|
302 DST2RGB(3);
|
|
303 DST1RGB(3);
|
|
304
|
|
305 pu += 4;
|
|
306 pv += 4;
|
|
307 py_1 += 8;
|
|
308 py_2 += 8;
|
|
309 dst_1 += 24;
|
|
310 dst_2 += 24;
|
|
311 }
|
|
312 }
|
|
313
|
|
314 // only trivial mods from yuv2rgb_c_24_rgb
|
|
315 static void yuv2rgb_c_24_bgr (uint8_t * py_1, uint8_t * py_2,
|
|
316 uint8_t * pu, uint8_t * pv,
|
|
317 void * _dst_1, void * _dst_2, int h_size)
|
|
318 {
|
|
319 int U, V, Y;
|
|
320 uint8_t * r, * g, * b;
|
|
321 uint8_t * dst_1, * dst_2;
|
|
322
|
|
323 h_size >>= 3;
|
|
324 dst_1 = _dst_1;
|
|
325 dst_2 = _dst_2;
|
|
326
|
|
327 while (h_size--) {
|
|
328 RGB(0);
|
|
329 DST1BGR(0);
|
|
330 DST2BGR(0);
|
|
331
|
|
332 RGB(1);
|
|
333 DST2BGR(1);
|
|
334 DST1BGR(1);
|
|
335
|
|
336 RGB(2);
|
|
337 DST1BGR(2);
|
|
338 DST2BGR(2);
|
|
339
|
|
340 RGB(3);
|
|
341 DST2BGR(3);
|
|
342 DST1BGR(3);
|
|
343
|
|
344 pu += 4;
|
|
345 pv += 4;
|
|
346 py_1 += 8;
|
|
347 py_2 += 8;
|
|
348 dst_1 += 24;
|
|
349 dst_2 += 24;
|
|
350 }
|
|
351 }
|
|
352
|
|
353 // This is exactly the same code as yuv2rgb_c_32 except for the types of
|
|
354 // r, g, b, dst_1, dst_2
|
|
355 static void yuv2rgb_c_16 (uint8_t * py_1, uint8_t * py_2,
|
|
356 uint8_t * pu, uint8_t * pv,
|
|
357 void * _dst_1, void * _dst_2, int h_size)
|
|
358 {
|
|
359 int U, V, Y;
|
|
360 uint16_t * r, * g, * b;
|
|
361 uint16_t * dst_1, * dst_2;
|
|
362
|
|
363 h_size >>= 3;
|
|
364 dst_1 = _dst_1;
|
|
365 dst_2 = _dst_2;
|
|
366
|
|
367 while (h_size--) {
|
|
368 RGB(0);
|
|
369 DST1(0);
|
|
370 DST2(0);
|
|
371
|
|
372 RGB(1);
|
|
373 DST2(1);
|
|
374 DST1(1);
|
|
375
|
|
376 RGB(2);
|
|
377 DST1(2);
|
|
378 DST2(2);
|
|
379
|
|
380 RGB(3);
|
|
381 DST2(3);
|
|
382 DST1(3);
|
|
383
|
|
384 pu += 4;
|
|
385 pv += 4;
|
|
386 py_1 += 8;
|
|
387 py_2 += 8;
|
|
388 dst_1 += 8;
|
|
389 dst_2 += 8;
|
|
390 }
|
|
391 }
|
|
392
|
|
393 static int div_round (int dividend, int divisor)
|
|
394 {
|
|
395 if (dividend > 0)
|
|
396 return (dividend + (divisor>>1)) / divisor;
|
|
397 else
|
|
398 return -((-dividend + (divisor>>1)) / divisor);
|
|
399 }
|
|
400
|
|
401 static void yuv2rgb_c_init (int bpp, int mode)
|
|
402 {
|
|
403 int i;
|
|
404 uint8_t table_Y[1024];
|
|
405 uint32_t *table_32 = 0;
|
|
406 uint16_t *table_16 = 0;
|
|
407 uint8_t *table_8 = 0;
|
|
408 uint32_t entry_size = 0;
|
|
409 void *table_r = 0, *table_g = 0, *table_b = 0;
|
|
410
|
|
411 int crv = Inverse_Table_6_9[matrix_coefficients][0];
|
|
412 int cbu = Inverse_Table_6_9[matrix_coefficients][1];
|
|
413 int cgu = -Inverse_Table_6_9[matrix_coefficients][2];
|
|
414 int cgv = -Inverse_Table_6_9[matrix_coefficients][3];
|
|
415
|
|
416 for (i = 0; i < 1024; i++) {
|
|
417 int j;
|
|
418
|
|
419 j = (76309 * (i - 384 - 16) + 32768) >> 16;
|
|
420 j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
|
|
421 table_Y[i] = j;
|
|
422 }
|
|
423
|
|
424 switch (bpp) {
|
|
425 case 32:
|
|
426 yuv2rgb_c_internal = yuv2rgb_c_32;
|
|
427
|
|
428 table_32 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
|
|
429
|
|
430 entry_size = sizeof (uint32_t);
|
|
431 table_r = table_32 + 197;
|
|
432 table_b = table_32 + 197 + 685;
|
|
433 table_g = table_32 + 197 + 2*682;
|
|
434
|
|
435 for (i = -197; i < 256+197; i++)
|
|
436 ((uint32_t *)table_r)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 16 : 0);
|
|
437 for (i = -132; i < 256+132; i++)
|
|
438 ((uint32_t *)table_g)[i] = table_Y[i+384] << 8;
|
|
439 for (i = -232; i < 256+232; i++)
|
|
440 ((uint32_t *)table_b)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 0 : 16);
|
|
441 break;
|
|
442
|
|
443 case 24:
|
|
444 // yuv2rgb_c_internal = (mode==MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
|
|
445 yuv2rgb_c_internal = (mode!=MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
|
|
446
|
|
447 table_8 = malloc ((256 + 2*232) * sizeof (uint8_t));
|
|
448
|
|
449 entry_size = sizeof (uint8_t);
|
|
450 table_r = table_g = table_b = table_8 + 232;
|
|
451
|
|
452 for (i = -232; i < 256+232; i++)
|
|
453 ((uint8_t * )table_b)[i] = table_Y[i+384];
|
|
454 break;
|
|
455
|
|
456 case 15:
|
|
457 case 16:
|
|
458 yuv2rgb_c_internal = yuv2rgb_c_16;
|
|
459
|
|
460 table_16 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
|
|
461
|
|
462 entry_size = sizeof (uint16_t);
|
|
463 table_r = table_16 + 197;
|
|
464 table_b = table_16 + 197 + 685;
|
|
465 table_g = table_16 + 197 + 2*682;
|
|
466
|
|
467 for (i = -197; i < 256+197; i++) {
|
|
468 int j = table_Y[i+384] >> 3;
|
|
469
|
|
470 if (mode == MODE_RGB)
|
|
471 j <<= ((bpp==16) ? 11 : 10);
|
|
472
|
|
473 ((uint16_t *)table_r)[i] = j;
|
|
474 }
|
|
475 for (i = -132; i < 256+132; i++) {
|
|
476 int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
|
|
477
|
|
478 ((uint16_t *)table_g)[i] = j << 5;
|
|
479 }
|
|
480 for (i = -232; i < 256+232; i++) {
|
|
481 int j = table_Y[i+384] >> 3;
|
|
482
|
|
483 if (mode == MODE_BGR)
|
|
484 j <<= ((bpp==16) ? 11 : 10);
|
|
485
|
|
486 ((uint16_t *)table_b)[i] = j;
|
|
487 }
|
|
488 break;
|
|
489
|
|
490 default:
|
|
491 printf ("%ibpp not supported by yuv2rgb\n", bpp);
|
|
492 //exit (1);
|
|
493 }
|
|
494
|
|
495 for (i = 0; i < 256; i++) {
|
|
496 table_rV[i] = table_r + entry_size * div_round (crv * (i-128), 76309);
|
|
497 table_gU[i] = table_g + entry_size * div_round (cgu * (i-128), 76309);
|
|
498 table_gV[i] = entry_size * div_round (cgv * (i-128), 76309);
|
|
499 table_bU[i] = table_b + entry_size * div_round (cbu * (i-128), 76309);
|
|
500 }
|
|
501 }
|