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