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 *
|
|
28 */
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include <stdlib.h>
|
|
32 #include <inttypes.h>
|
|
33
|
|
34 #include "config.h"
|
|
35 //#include "video_out.h"
|
|
36 #include "rgb2rgb.h"
|
|
37
|
|
38 #ifdef HAVE_MLIB
|
|
39 #include "yuv2rgb_mlib.c"
|
|
40 #endif
|
|
41
|
|
42 extern yuv2rgb_fun yuv2rgb_init_mmx (int bpp, int mode);
|
|
43
|
|
44
|
|
45 uint32_t matrix_coefficients = 6;
|
|
46
|
|
47 const int32_t Inverse_Table_6_9[8][4] = {
|
|
48 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
|
|
49 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
|
|
50 {104597, 132201, 25675, 53279}, /* unspecified */
|
|
51 {104597, 132201, 25675, 53279}, /* reserved */
|
|
52 {104448, 132798, 24759, 53109}, /* FCC */
|
|
53 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
|
|
54 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
|
|
55 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
|
|
56 };
|
|
57
|
|
58 static void yuv2rgb_c_init (int bpp, int mode);
|
|
59
|
|
60 yuv2rgb_fun yuv2rgb;
|
|
61
|
|
62 static void (* yuv2rgb_c_internal) (uint8_t *, uint8_t *,
|
|
63 uint8_t *, uint8_t *,
|
|
64 void *, void *, int);
|
|
65
|
|
66 static void yuv2rgb_c (void * dst, uint8_t * py,
|
|
67 uint8_t * pu, uint8_t * pv,
|
|
68 int h_size, int v_size,
|
|
69 int rgb_stride, int y_stride, int uv_stride)
|
|
70 {
|
|
71 v_size >>= 1;
|
|
72
|
|
73 while (v_size--) {
|
|
74 yuv2rgb_c_internal (py, py + y_stride, pu, pv, dst, dst + rgb_stride,
|
|
75 h_size);
|
|
76
|
|
77 py += 2 * y_stride;
|
|
78 pu += uv_stride;
|
|
79 pv += uv_stride;
|
|
80 dst += 2 * rgb_stride;
|
|
81 }
|
|
82 }
|
|
83
|
|
84 void yuv2rgb_init (int bpp, int mode)
|
|
85 {
|
|
86 yuv2rgb = NULL;
|
|
87 #ifdef HAVE_MMX
|
|
88 if (yuv2rgb == NULL /*&& (config.flags & VO_MMX_ENABLE)*/) {
|
|
89 yuv2rgb = yuv2rgb_init_mmx (bpp, mode);
|
|
90 if (yuv2rgb != NULL)
|
|
91 printf ("Using MMX for colorspace transform\n");
|
|
92 else
|
|
93 printf ("Cannot init MMX colorspace transform\n");
|
|
94 }
|
|
95 #endif
|
|
96 #ifdef HAVE_MLIB
|
|
97 if (yuv2rgb == NULL /*&& (config.flags & VO_MLIB_ENABLE)*/) {
|
|
98 yuv2rgb = yuv2rgb_init_mlib (bpp, mode);
|
|
99 if (yuv2rgb != NULL)
|
|
100 printf ("Using mlib for colorspace transform\n");
|
|
101 }
|
|
102 #endif
|
|
103 if (yuv2rgb == NULL) {
|
|
104 printf ("No accelerated colorspace conversion found\n");
|
|
105 yuv2rgb_c_init (bpp, mode);
|
|
106 yuv2rgb = (yuv2rgb_fun)yuv2rgb_c;
|
|
107 }
|
|
108 }
|
|
109
|
|
110 void * table_rV[256];
|
|
111 void * table_gU[256];
|
|
112 int table_gV[256];
|
|
113 void * table_bU[256];
|
|
114
|
|
115 #define RGB(i) \
|
|
116 U = pu[i]; \
|
|
117 V = pv[i]; \
|
|
118 r = table_rV[V]; \
|
|
119 g = table_gU[U] + table_gV[V]; \
|
|
120 b = table_bU[U];
|
|
121
|
|
122 #define DST1(i) \
|
|
123 Y = py_1[2*i]; \
|
|
124 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
|
|
125 Y = py_1[2*i+1]; \
|
|
126 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
|
|
127
|
|
128 #define DST2(i) \
|
|
129 Y = py_2[2*i]; \
|
|
130 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
|
|
131 Y = py_2[2*i+1]; \
|
|
132 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
|
|
133
|
|
134 #define DST1RGB(i) \
|
|
135 Y = py_1[2*i]; \
|
|
136 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
|
|
137 Y = py_1[2*i+1]; \
|
|
138 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
|
|
139
|
|
140 #define DST2RGB(i) \
|
|
141 Y = py_2[2*i]; \
|
|
142 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
|
|
143 Y = py_2[2*i+1]; \
|
|
144 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
|
|
145
|
|
146 #define DST1BGR(i) \
|
|
147 Y = py_1[2*i]; \
|
|
148 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
|
|
149 Y = py_1[2*i+1]; \
|
|
150 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
|
|
151
|
|
152 #define DST2BGR(i) \
|
|
153 Y = py_2[2*i]; \
|
|
154 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
|
|
155 Y = py_2[2*i+1]; \
|
|
156 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
|
|
157
|
|
158 static void yuv2rgb_c_32 (uint8_t * py_1, uint8_t * py_2,
|
|
159 uint8_t * pu, uint8_t * pv,
|
|
160 void * _dst_1, void * _dst_2, int h_size)
|
|
161 {
|
|
162 int U, V, Y;
|
|
163 uint32_t * r, * g, * b;
|
|
164 uint32_t * dst_1, * dst_2;
|
|
165
|
|
166 h_size >>= 3;
|
|
167 dst_1 = _dst_1;
|
|
168 dst_2 = _dst_2;
|
|
169
|
|
170 while (h_size--) {
|
|
171 RGB(0);
|
|
172 DST1(0);
|
|
173 DST2(0);
|
|
174
|
|
175 RGB(1);
|
|
176 DST2(1);
|
|
177 DST1(1);
|
|
178
|
|
179 RGB(2);
|
|
180 DST1(2);
|
|
181 DST2(2);
|
|
182
|
|
183 RGB(3);
|
|
184 DST2(3);
|
|
185 DST1(3);
|
|
186
|
|
187 pu += 4;
|
|
188 pv += 4;
|
|
189 py_1 += 8;
|
|
190 py_2 += 8;
|
|
191 dst_1 += 8;
|
|
192 dst_2 += 8;
|
|
193 }
|
|
194 }
|
|
195
|
|
196 // This is very near from the yuv2rgb_c_32 code
|
|
197 static void yuv2rgb_c_24_rgb (uint8_t * py_1, uint8_t * py_2,
|
|
198 uint8_t * pu, uint8_t * pv,
|
|
199 void * _dst_1, void * _dst_2, int h_size)
|
|
200 {
|
|
201 int U, V, Y;
|
|
202 uint8_t * r, * g, * b;
|
|
203 uint8_t * dst_1, * dst_2;
|
|
204
|
|
205 h_size >>= 3;
|
|
206 dst_1 = _dst_1;
|
|
207 dst_2 = _dst_2;
|
|
208
|
|
209 while (h_size--) {
|
|
210 RGB(0);
|
|
211 DST1RGB(0);
|
|
212 DST2RGB(0);
|
|
213
|
|
214 RGB(1);
|
|
215 DST2RGB(1);
|
|
216 DST1RGB(1);
|
|
217
|
|
218 RGB(2);
|
|
219 DST1RGB(2);
|
|
220 DST2RGB(2);
|
|
221
|
|
222 RGB(3);
|
|
223 DST2RGB(3);
|
|
224 DST1RGB(3);
|
|
225
|
|
226 pu += 4;
|
|
227 pv += 4;
|
|
228 py_1 += 8;
|
|
229 py_2 += 8;
|
|
230 dst_1 += 24;
|
|
231 dst_2 += 24;
|
|
232 }
|
|
233 }
|
|
234
|
|
235 // only trivial mods from yuv2rgb_c_24_rgb
|
|
236 static void yuv2rgb_c_24_bgr (uint8_t * py_1, uint8_t * py_2,
|
|
237 uint8_t * pu, uint8_t * pv,
|
|
238 void * _dst_1, void * _dst_2, int h_size)
|
|
239 {
|
|
240 int U, V, Y;
|
|
241 uint8_t * r, * g, * b;
|
|
242 uint8_t * dst_1, * dst_2;
|
|
243
|
|
244 h_size >>= 3;
|
|
245 dst_1 = _dst_1;
|
|
246 dst_2 = _dst_2;
|
|
247
|
|
248 while (h_size--) {
|
|
249 RGB(0);
|
|
250 DST1BGR(0);
|
|
251 DST2BGR(0);
|
|
252
|
|
253 RGB(1);
|
|
254 DST2BGR(1);
|
|
255 DST1BGR(1);
|
|
256
|
|
257 RGB(2);
|
|
258 DST1BGR(2);
|
|
259 DST2BGR(2);
|
|
260
|
|
261 RGB(3);
|
|
262 DST2BGR(3);
|
|
263 DST1BGR(3);
|
|
264
|
|
265 pu += 4;
|
|
266 pv += 4;
|
|
267 py_1 += 8;
|
|
268 py_2 += 8;
|
|
269 dst_1 += 24;
|
|
270 dst_2 += 24;
|
|
271 }
|
|
272 }
|
|
273
|
|
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 static void yuv2rgb_c_16 (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 uint16_t * r, * g, * b;
|
|
282 uint16_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 DST1(0);
|
|
291 DST2(0);
|
|
292
|
|
293 RGB(1);
|
|
294 DST2(1);
|
|
295 DST1(1);
|
|
296
|
|
297 RGB(2);
|
|
298 DST1(2);
|
|
299 DST2(2);
|
|
300
|
|
301 RGB(3);
|
|
302 DST2(3);
|
|
303 DST1(3);
|
|
304
|
|
305 pu += 4;
|
|
306 pv += 4;
|
|
307 py_1 += 8;
|
|
308 py_2 += 8;
|
|
309 dst_1 += 8;
|
|
310 dst_2 += 8;
|
|
311 }
|
|
312 }
|
|
313
|
|
314 static int div_round (int dividend, int divisor)
|
|
315 {
|
|
316 if (dividend > 0)
|
|
317 return (dividend + (divisor>>1)) / divisor;
|
|
318 else
|
|
319 return -((-dividend + (divisor>>1)) / divisor);
|
|
320 }
|
|
321
|
|
322 static void yuv2rgb_c_init (int bpp, int mode)
|
|
323 {
|
|
324 int i;
|
|
325 uint8_t table_Y[1024];
|
|
326 uint32_t *table_32 = 0;
|
|
327 uint16_t *table_16 = 0;
|
|
328 uint8_t *table_8 = 0;
|
|
329 uint32_t entry_size = 0;
|
|
330 void *table_r = 0, *table_g = 0, *table_b = 0;
|
|
331
|
|
332 int crv = Inverse_Table_6_9[matrix_coefficients][0];
|
|
333 int cbu = Inverse_Table_6_9[matrix_coefficients][1];
|
|
334 int cgu = -Inverse_Table_6_9[matrix_coefficients][2];
|
|
335 int cgv = -Inverse_Table_6_9[matrix_coefficients][3];
|
|
336
|
|
337 for (i = 0; i < 1024; i++) {
|
|
338 int j;
|
|
339
|
|
340 j = (76309 * (i - 384 - 16) + 32768) >> 16;
|
|
341 j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
|
|
342 table_Y[i] = j;
|
|
343 }
|
|
344
|
|
345 switch (bpp) {
|
|
346 case 32:
|
|
347 yuv2rgb_c_internal = yuv2rgb_c_32;
|
|
348
|
|
349 table_32 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
|
|
350
|
|
351 entry_size = sizeof (uint32_t);
|
|
352 table_r = table_32 + 197;
|
|
353 table_b = table_32 + 197 + 685;
|
|
354 table_g = table_32 + 197 + 2*682;
|
|
355
|
|
356 for (i = -197; i < 256+197; i++)
|
|
357 ((uint32_t *)table_r)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 16 : 0);
|
|
358 for (i = -132; i < 256+132; i++)
|
|
359 ((uint32_t *)table_g)[i] = table_Y[i+384] << 8;
|
|
360 for (i = -232; i < 256+232; i++)
|
|
361 ((uint32_t *)table_b)[i] = table_Y[i+384] << ((mode==MODE_RGB) ? 0 : 16);
|
|
362 break;
|
|
363
|
|
364 case 24:
|
|
365 // yuv2rgb_c_internal = (mode==MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
|
|
366 yuv2rgb_c_internal = (mode!=MODE_RGB) ? yuv2rgb_c_24_rgb : yuv2rgb_c_24_bgr;
|
|
367
|
|
368 table_8 = malloc ((256 + 2*232) * sizeof (uint8_t));
|
|
369
|
|
370 entry_size = sizeof (uint8_t);
|
|
371 table_r = table_g = table_b = table_8 + 232;
|
|
372
|
|
373 for (i = -232; i < 256+232; i++)
|
|
374 ((uint8_t * )table_b)[i] = table_Y[i+384];
|
|
375 break;
|
|
376
|
|
377 case 15:
|
|
378 case 16:
|
|
379 yuv2rgb_c_internal = yuv2rgb_c_16;
|
|
380
|
|
381 table_16 = malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
|
|
382
|
|
383 entry_size = sizeof (uint16_t);
|
|
384 table_r = table_16 + 197;
|
|
385 table_b = table_16 + 197 + 685;
|
|
386 table_g = table_16 + 197 + 2*682;
|
|
387
|
|
388 for (i = -197; i < 256+197; i++) {
|
|
389 int j = table_Y[i+384] >> 3;
|
|
390
|
|
391 if (mode == MODE_RGB)
|
|
392 j <<= ((bpp==16) ? 11 : 10);
|
|
393
|
|
394 ((uint16_t *)table_r)[i] = j;
|
|
395 }
|
|
396 for (i = -132; i < 256+132; i++) {
|
|
397 int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
|
|
398
|
|
399 ((uint16_t *)table_g)[i] = j << 5;
|
|
400 }
|
|
401 for (i = -232; i < 256+232; i++) {
|
|
402 int j = table_Y[i+384] >> 3;
|
|
403
|
|
404 if (mode == MODE_BGR)
|
|
405 j <<= ((bpp==16) ? 11 : 10);
|
|
406
|
|
407 ((uint16_t *)table_b)[i] = j;
|
|
408 }
|
|
409 break;
|
|
410
|
|
411 default:
|
|
412 printf ("%ibpp not supported by yuv2rgb\n", bpp);
|
|
413 //exit (1);
|
|
414 }
|
|
415
|
|
416 for (i = 0; i < 256; i++) {
|
|
417 table_rV[i] = table_r + entry_size * div_round (crv * (i-128), 76309);
|
|
418 table_gU[i] = table_g + entry_size * div_round (cgu * (i-128), 76309);
|
|
419 table_gV[i] = entry_size * div_round (cgv * (i-128), 76309);
|
|
420 table_bU[i] = table_b + entry_size * div_round (cbu * (i-128), 76309);
|
|
421 }
|
|
422 }
|