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