1 /*****************************************************************************
2 * i420_rgb.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2004 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Damien Fouilleul <damienf@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <math.h> /* exp(), pow() */
38 #if defined (MODULE_NAME_IS_i420_rgb)
39 # include "i420_rgb_c.h"
42 /*****************************************************************************
43 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
44 *****************************************************************************/
45 #define RGB2PIXEL( p_vout, i_r, i_g, i_b ) \
46 (((((uint32_t)i_r) >> p_vout->output.i_rrshift) \
47 << p_vout->output.i_lrshift) \
48 | ((((uint32_t)i_g) >> p_vout->output.i_rgshift) \
49 << p_vout->output.i_lgshift) \
50 | ((((uint32_t)i_b) >> p_vout->output.i_rbshift) \
51 << p_vout->output.i_lbshift))
53 /*****************************************************************************
54 * Local and extern prototypes.
55 *****************************************************************************/
56 static int Activate ( vlc_object_t * );
57 static void Deactivate ( vlc_object_t * );
59 #if defined (MODULE_NAME_IS_i420_rgb)
60 static void SetGammaTable ( int *pi_table, double f_gamma );
61 static void SetYUV ( vout_thread_t * );
62 static void Set8bppPalette ( vout_thread_t *, uint8_t * );
65 /*****************************************************************************
67 *****************************************************************************/
69 #if defined (MODULE_NAME_IS_i420_rgb)
70 set_description( _("I420,IYUV,YV12 to "
71 "RGB2,RV15,RV16,RV24,RV32 conversions") );
72 set_capability( "chroma", 80 );
73 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
74 set_description( _( "MMX I420,IYUV,YV12 to "
75 "RV15,RV16,RV24,RV32 conversions") );
76 set_capability( "chroma", 100 );
77 add_requirement( MMX );
78 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
79 set_description( _( "SSE2 I420,IYUV,YV12 to "
80 "RV15,RV16,RV24,RV32 conversions") );
81 set_capability( "chroma", 120 );
82 add_requirement( SSE2 );
84 set_callbacks( Activate, Deactivate );
87 /*****************************************************************************
88 * Activate: allocate a chroma function
89 *****************************************************************************
90 * This function allocates and initializes a chroma function
91 *****************************************************************************/
92 static int Activate( vlc_object_t *p_this )
94 vout_thread_t *p_vout = (vout_thread_t *)p_this;
95 #if defined (MODULE_NAME_IS_i420_rgb)
99 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
104 switch( p_vout->render.i_chroma )
106 case VLC_FOURCC('Y','V','1','2'):
107 case VLC_FOURCC('I','4','2','0'):
108 case VLC_FOURCC('I','Y','U','V'):
109 switch( p_vout->output.i_chroma )
111 #if defined (MODULE_NAME_IS_i420_rgb)
112 case VLC_FOURCC('R','G','B','2'):
113 p_vout->chroma.pf_convert = E_(I420_RGB8);
116 case VLC_FOURCC('R','V','1','5'):
117 case VLC_FOURCC('R','V','1','6'):
118 #if ! defined (MODULE_NAME_IS_i420_rgb)
119 /* If we don't have support for the bitmasks, bail out */
120 if( ( p_vout->output.i_rmask == 0x7c00
121 && p_vout->output.i_gmask == 0x03e0
122 && p_vout->output.i_bmask == 0x001f ) )
124 /* R5G5B6 pixel format */
125 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
126 p_vout->chroma.pf_convert = E_(I420_R5G5B5);
128 else if( ( p_vout->output.i_rmask == 0xf800
129 && p_vout->output.i_gmask == 0x07e0
130 && p_vout->output.i_bmask == 0x001f ) )
132 /* R5G6B5 pixel format */
133 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
134 p_vout->chroma.pf_convert = E_(I420_R5G6B5);
139 // generic C chroma converter */
140 p_vout->chroma.pf_convert = E_(I420_RGB16);
145 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
146 case VLC_FOURCC('R','V','2','4'):
149 case VLC_FOURCC('R','V','3','2'):
150 #if ! defined (MODULE_NAME_IS_i420_rgb)
151 /* If we don't have support for the bitmasks, bail out */
152 if( p_vout->output.i_rmask == 0x00ff0000
153 && p_vout->output.i_gmask == 0x0000ff00
154 && p_vout->output.i_bmask == 0x000000ff )
156 /* A8R8G8B8 pixel format */
157 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
158 p_vout->chroma.pf_convert = E_(I420_A8R8G8B8);
160 else if( p_vout->output.i_rmask == 0xff000000
161 && p_vout->output.i_gmask == 0x00ff0000
162 && p_vout->output.i_bmask == 0x0000ff00 )
164 /* R8G8B8A8 pixel format */
165 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
166 p_vout->chroma.pf_convert = E_(I420_R8G8B8A8);
168 else if( p_vout->output.i_rmask == 0x0000ff00
169 && p_vout->output.i_gmask == 0x00ff0000
170 && p_vout->output.i_bmask == 0xff000000 )
172 /* B8G8R8A8 pixel format */
173 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
174 p_vout->chroma.pf_convert = E_(I420_B8G8R8A8);
176 else if( p_vout->output.i_rmask == 0x000000ff
177 && p_vout->output.i_gmask == 0x0000ff00
178 && p_vout->output.i_bmask == 0x00ff0000 )
180 /* A8B8G8R8 pixel format */
181 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
182 p_vout->chroma.pf_convert = E_(I420_A8B8G8R8);
187 /* generic C chroma converter */
188 p_vout->chroma.pf_convert = E_(I420_RGB32);
201 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
202 if( p_vout->chroma.p_sys == NULL )
207 switch( p_vout->output.i_chroma )
209 #if defined (MODULE_NAME_IS_i420_rgb)
210 case VLC_FOURCC('R','G','B','2'):
211 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
215 case VLC_FOURCC('R','V','1','5'):
216 case VLC_FOURCC('R','V','1','6'):
217 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
220 case VLC_FOURCC('R','V','2','4'):
221 case VLC_FOURCC('R','V','3','2'):
222 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
226 p_vout->chroma.p_sys->p_buffer = NULL;
230 if( p_vout->chroma.p_sys->p_buffer == NULL )
232 free( p_vout->chroma.p_sys );
236 p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
237 * ( ( p_vout->output.i_chroma
238 == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
240 if( p_vout->chroma.p_sys->p_offset == NULL )
242 free( p_vout->chroma.p_sys->p_buffer );
243 free( p_vout->chroma.p_sys );
247 #if defined (MODULE_NAME_IS_i420_rgb)
248 switch( p_vout->output.i_chroma )
250 case VLC_FOURCC('R','G','B','2'):
251 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
253 case VLC_FOURCC('R','V','1','5'):
254 case VLC_FOURCC('R','V','1','6'):
255 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
257 default: /* RV24, RV32 */
258 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
262 p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
263 if( p_vout->chroma.p_sys->p_base == NULL )
265 free( p_vout->chroma.p_sys->p_offset );
266 free( p_vout->chroma.p_sys->p_buffer );
267 free( p_vout->chroma.p_sys );
277 /*****************************************************************************
278 * Deactivate: free the chroma function
279 *****************************************************************************
280 * This function frees the previously allocated chroma function
281 *****************************************************************************/
282 static void Deactivate( vlc_object_t *p_this )
284 vout_thread_t *p_vout = (vout_thread_t *)p_this;
286 #if defined (MODULE_NAME_IS_i420_rgb)
287 free( p_vout->chroma.p_sys->p_base );
289 free( p_vout->chroma.p_sys->p_offset );
290 free( p_vout->chroma.p_sys->p_buffer );
291 free( p_vout->chroma.p_sys );
294 #if defined (MODULE_NAME_IS_i420_rgb)
295 /*****************************************************************************
296 * SetGammaTable: return intensity table transformed by gamma curve.
297 *****************************************************************************
298 * pi_table is a table of 256 entries from 0 to 255.
299 *****************************************************************************/
300 static void SetGammaTable( int *pi_table, double f_gamma )
302 int i_y; /* base intensity */
304 /* Use exp(gamma) instead of gamma */
305 f_gamma = exp( f_gamma );
307 /* Build gamma table */
308 for( i_y = 0; i_y < 256; i_y++ )
310 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
314 /*****************************************************************************
315 * SetYUV: compute tables and set function pointers
316 *****************************************************************************/
317 static void SetYUV( vout_thread_t *p_vout )
319 int pi_gamma[256]; /* gamma table */
320 volatile int i_index; /* index in tables */
321 /* We use volatile here to work around a strange gcc-3.3.4
322 * optimization bug */
324 /* Build gamma table */
325 SetGammaTable( pi_gamma, p_vout->f_gamma );
328 * Set pointers and build YUV tables
331 /* Color: build red, green and blue tables */
332 switch( p_vout->output.i_chroma )
334 case VLC_FOURCC('R','G','B','2'):
335 p_vout->chroma.p_sys->p_rgb8 = (uint8_t *)p_vout->chroma.p_sys->p_base;
336 Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
339 case VLC_FOURCC('R','V','1','5'):
340 case VLC_FOURCC('R','V','1','6'):
341 p_vout->chroma.p_sys->p_rgb16 = (uint16_t *)p_vout->chroma.p_sys->p_base;
342 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
344 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
345 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
347 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
349 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
350 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
352 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
354 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
355 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
357 for( i_index = 0; i_index < 256; i_index++ )
359 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
360 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
361 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
365 case VLC_FOURCC('R','V','2','4'):
366 case VLC_FOURCC('R','V','3','2'):
367 p_vout->chroma.p_sys->p_rgb32 = (uint32_t *)p_vout->chroma.p_sys->p_base;
368 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
370 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
371 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
373 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
375 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
376 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
378 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
380 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
381 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
383 for( i_index = 0; i_index < 256; i_index++ )
385 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
386 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
387 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
393 static void Set8bppPalette( vout_thread_t *p_vout, uint8_t *p_rgb8 )
395 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
400 uint16_t *p_cmap_r=p_vout->chroma.p_sys->p_rgb_r;
401 uint16_t *p_cmap_g=p_vout->chroma.p_sys->p_rgb_g;
402 uint16_t *p_cmap_b=p_vout->chroma.p_sys->p_rgb_b;
404 unsigned char p_lookup[PALETTE_TABLE_SIZE];
406 /* This loop calculates the intersection of an YUV box and the RGB cube. */
407 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
409 for ( u = 0; u <= 256; u += 32 )
411 for ( v = 0; v <= 256; v += 32 )
413 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
414 g = y + ( (U_GREEN_COEF*(u-128)
415 + V_GREEN_COEF*(v-128)) >> SHIFT );
416 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
418 if( r >= 0x00 && g >= 0x00 && b >= 0x00
419 && r <= 0xff && g <= 0xff && b <= 0xff )
421 /* This one should never happen unless someone
422 * fscked up my code */
425 msg_Err( p_vout, "no colors left in palette" );
429 /* Clip the colors */
430 p_cmap_r[ j ] = CLIP( r );
431 p_cmap_g[ j ] = CLIP( g );
432 p_cmap_b[ j ] = CLIP( b );
435 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
436 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
454 /* The colors have been allocated, we can set the palette */
455 p_vout->output.pf_setpalette( p_vout, p_cmap_r, p_cmap_g, p_cmap_b );
458 /* There will eventually be a way to know which colors
459 * couldn't be allocated and try to find a replacement */
460 p_vout->i_white_pixel = 0xff;
461 p_vout->i_black_pixel = 0x00;
462 p_vout->i_gray_pixel = 0x44;
463 p_vout->i_blue_pixel = 0x3b;
466 /* This loop allocates colors that got outside the RGB cube */
467 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
469 for ( u = 0; u <= 256; u += 32 )
471 for ( v = 0; v <= 256; v += 32, i++ )
473 int u2, v2, dist, mindist = 100000000;
475 if( p_lookup[ i ] || y == 0 )
481 for( u2 = 0; u2 <= 256; u2 += 32 )
483 for( v2 = 0; v2 <= 256; v2 += 32 )
485 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
486 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
488 /* Find the nearest color */
489 if( p_lookup[ j ] && dist < mindist )
491 p_rgb8[ i ] = p_rgb8[ j ];
497 /* Find the nearest color */
498 if( p_lookup[ j ] && dist + 128 < mindist )
500 p_rgb8[ i ] = p_rgb8[ j ];
501 mindist = dist + 128;