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() */
34 #if defined (MODULE_NAME_IS_i420_rgb)
35 # include "i420_rgb_c.h"
38 /*****************************************************************************
39 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
40 *****************************************************************************/
41 #define RGB2PIXEL( p_vout, i_r, i_g, i_b ) \
42 (((((uint32_t)i_r) >> p_vout->output.i_rrshift) \
43 << p_vout->output.i_lrshift) \
44 | ((((uint32_t)i_g) >> p_vout->output.i_rgshift) \
45 << p_vout->output.i_lgshift) \
46 | ((((uint32_t)i_b) >> p_vout->output.i_rbshift) \
47 << p_vout->output.i_lbshift))
49 /*****************************************************************************
50 * Local and extern prototypes.
51 *****************************************************************************/
52 static int Activate ( vlc_object_t * );
53 static void Deactivate ( vlc_object_t * );
55 #if defined (MODULE_NAME_IS_i420_rgb)
56 static void SetGammaTable ( int *pi_table, double f_gamma );
57 static void SetYUV ( vout_thread_t * );
58 static void Set8bppPalette ( vout_thread_t *, uint8_t * );
61 /*****************************************************************************
63 *****************************************************************************/
65 #if defined (MODULE_NAME_IS_i420_rgb)
66 set_description( _("I420,IYUV,YV12 to "
67 "RGB2,RV15,RV16,RV24,RV32 conversions") );
68 set_capability( "chroma", 80 );
69 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
70 set_description( _( "MMX I420,IYUV,YV12 to "
71 "RV15,RV16,RV24,RV32 conversions") );
72 set_capability( "chroma", 100 );
73 add_requirement( MMX );
74 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
75 set_description( _( "SSE2 I420,IYUV,YV12 to "
76 "RV15,RV16,RV24,RV32 conversions") );
77 set_capability( "chroma", 120 );
78 add_requirement( SSE2 );
80 set_callbacks( Activate, Deactivate );
83 /*****************************************************************************
84 * Activate: allocate a chroma function
85 *****************************************************************************
86 * This function allocates and initializes a chroma function
87 *****************************************************************************/
88 static int Activate( vlc_object_t *p_this )
90 vout_thread_t *p_vout = (vout_thread_t *)p_this;
91 #if defined (MODULE_NAME_IS_i420_rgb)
95 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
100 switch( p_vout->render.i_chroma )
102 case VLC_FOURCC('Y','V','1','2'):
103 case VLC_FOURCC('I','4','2','0'):
104 case VLC_FOURCC('I','Y','U','V'):
105 switch( p_vout->output.i_chroma )
107 #if defined (MODULE_NAME_IS_i420_rgb)
108 case VLC_FOURCC('R','G','B','2'):
109 p_vout->chroma.pf_convert = E_(I420_RGB8);
112 case VLC_FOURCC('R','V','1','5'):
113 case VLC_FOURCC('R','V','1','6'):
114 #if ! defined (MODULE_NAME_IS_i420_rgb)
115 /* If we don't have support for the bitmasks, bail out */
116 if( ( p_vout->output.i_rmask == 0x7c00
117 && p_vout->output.i_gmask == 0x03e0
118 && p_vout->output.i_bmask == 0x001f ) )
120 /* R5G5B6 pixel format */
121 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
122 p_vout->chroma.pf_convert = E_(I420_R5G5B5);
124 else if( ( p_vout->output.i_rmask == 0xf800
125 && p_vout->output.i_gmask == 0x07e0
126 && p_vout->output.i_bmask == 0x001f ) )
128 /* R5G6B5 pixel format */
129 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
130 p_vout->chroma.pf_convert = E_(I420_R5G6B5);
135 // generic C chroma converter */
136 p_vout->chroma.pf_convert = E_(I420_RGB16);
141 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
142 case VLC_FOURCC('R','V','2','4'):
145 case VLC_FOURCC('R','V','3','2'):
146 #if ! defined (MODULE_NAME_IS_i420_rgb)
147 /* If we don't have support for the bitmasks, bail out */
148 if( p_vout->output.i_rmask == 0x00ff0000
149 && p_vout->output.i_gmask == 0x0000ff00
150 && p_vout->output.i_bmask == 0x000000ff )
152 /* A8R8G8B8 pixel format */
153 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
154 p_vout->chroma.pf_convert = E_(I420_A8R8G8B8);
156 else if( p_vout->output.i_rmask == 0xff000000
157 && p_vout->output.i_gmask == 0x00ff0000
158 && p_vout->output.i_bmask == 0x0000ff00 )
160 /* R8G8B8A8 pixel format */
161 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
162 p_vout->chroma.pf_convert = E_(I420_R8G8B8A8);
164 else if( p_vout->output.i_rmask == 0x0000ff00
165 && p_vout->output.i_gmask == 0x00ff0000
166 && p_vout->output.i_bmask == 0xff000000 )
168 /* B8G8R8A8 pixel format */
169 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
170 p_vout->chroma.pf_convert = E_(I420_B8G8R8A8);
172 else if( p_vout->output.i_rmask == 0x000000ff
173 && p_vout->output.i_gmask == 0x0000ff00
174 && p_vout->output.i_bmask == 0x00ff0000 )
176 /* A8B8G8R8 pixel format */
177 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
178 p_vout->chroma.pf_convert = E_(I420_A8B8G8R8);
183 /* generic C chroma converter */
184 p_vout->chroma.pf_convert = E_(I420_RGB32);
197 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
198 if( p_vout->chroma.p_sys == NULL )
203 switch( p_vout->output.i_chroma )
205 #if defined (MODULE_NAME_IS_i420_rgb)
206 case VLC_FOURCC('R','G','B','2'):
207 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
211 case VLC_FOURCC('R','V','1','5'):
212 case VLC_FOURCC('R','V','1','6'):
213 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
216 case VLC_FOURCC('R','V','2','4'):
217 case VLC_FOURCC('R','V','3','2'):
218 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
222 p_vout->chroma.p_sys->p_buffer = NULL;
226 if( p_vout->chroma.p_sys->p_buffer == NULL )
228 free( p_vout->chroma.p_sys );
232 p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
233 * ( ( p_vout->output.i_chroma
234 == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
236 if( p_vout->chroma.p_sys->p_offset == NULL )
238 free( p_vout->chroma.p_sys->p_buffer );
239 free( p_vout->chroma.p_sys );
243 #if defined (MODULE_NAME_IS_i420_rgb)
244 switch( p_vout->output.i_chroma )
246 case VLC_FOURCC('R','G','B','2'):
247 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
249 case VLC_FOURCC('R','V','1','5'):
250 case VLC_FOURCC('R','V','1','6'):
251 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
253 default: /* RV24, RV32 */
254 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
258 p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
259 if( p_vout->chroma.p_sys->p_base == NULL )
261 free( p_vout->chroma.p_sys->p_offset );
262 free( p_vout->chroma.p_sys->p_buffer );
263 free( p_vout->chroma.p_sys );
273 /*****************************************************************************
274 * Deactivate: free the chroma function
275 *****************************************************************************
276 * This function frees the previously allocated chroma function
277 *****************************************************************************/
278 static void Deactivate( vlc_object_t *p_this )
280 vout_thread_t *p_vout = (vout_thread_t *)p_this;
282 #if defined (MODULE_NAME_IS_i420_rgb)
283 free( p_vout->chroma.p_sys->p_base );
285 free( p_vout->chroma.p_sys->p_offset );
286 free( p_vout->chroma.p_sys->p_buffer );
287 free( p_vout->chroma.p_sys );
290 #if defined (MODULE_NAME_IS_i420_rgb)
291 /*****************************************************************************
292 * SetGammaTable: return intensity table transformed by gamma curve.
293 *****************************************************************************
294 * pi_table is a table of 256 entries from 0 to 255.
295 *****************************************************************************/
296 static void SetGammaTable( int *pi_table, double f_gamma )
298 int i_y; /* base intensity */
300 /* Use exp(gamma) instead of gamma */
301 f_gamma = exp( f_gamma );
303 /* Build gamma table */
304 for( i_y = 0; i_y < 256; i_y++ )
306 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
310 /*****************************************************************************
311 * SetYUV: compute tables and set function pointers
312 *****************************************************************************/
313 static void SetYUV( vout_thread_t *p_vout )
315 int pi_gamma[256]; /* gamma table */
316 volatile int i_index; /* index in tables */
317 /* We use volatile here to work around a strange gcc-3.3.4
318 * optimization bug */
320 /* Build gamma table */
321 SetGammaTable( pi_gamma, p_vout->f_gamma );
324 * Set pointers and build YUV tables
327 /* Color: build red, green and blue tables */
328 switch( p_vout->output.i_chroma )
330 case VLC_FOURCC('R','G','B','2'):
331 p_vout->chroma.p_sys->p_rgb8 = (uint8_t *)p_vout->chroma.p_sys->p_base;
332 Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
335 case VLC_FOURCC('R','V','1','5'):
336 case VLC_FOURCC('R','V','1','6'):
337 p_vout->chroma.p_sys->p_rgb16 = (uint16_t *)p_vout->chroma.p_sys->p_base;
338 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
340 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
341 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
343 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
345 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
346 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
348 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
350 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
351 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
353 for( i_index = 0; i_index < 256; i_index++ )
355 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
356 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
357 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
361 case VLC_FOURCC('R','V','2','4'):
362 case VLC_FOURCC('R','V','3','2'):
363 p_vout->chroma.p_sys->p_rgb32 = (uint32_t *)p_vout->chroma.p_sys->p_base;
364 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
366 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
367 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
369 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
371 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
372 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
374 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
376 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
377 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
379 for( i_index = 0; i_index < 256; i_index++ )
381 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
382 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
383 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
389 static void Set8bppPalette( vout_thread_t *p_vout, uint8_t *p_rgb8 )
391 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
396 uint16_t *p_cmap_r=p_vout->chroma.p_sys->p_rgb_r;
397 uint16_t *p_cmap_g=p_vout->chroma.p_sys->p_rgb_g;
398 uint16_t *p_cmap_b=p_vout->chroma.p_sys->p_rgb_b;
400 unsigned char p_lookup[PALETTE_TABLE_SIZE];
402 /* This loop calculates the intersection of an YUV box and the RGB cube. */
403 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
405 for ( u = 0; u <= 256; u += 32 )
407 for ( v = 0; v <= 256; v += 32 )
409 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
410 g = y + ( (U_GREEN_COEF*(u-128)
411 + V_GREEN_COEF*(v-128)) >> SHIFT );
412 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
414 if( r >= 0x00 && g >= 0x00 && b >= 0x00
415 && r <= 0xff && g <= 0xff && b <= 0xff )
417 /* This one should never happen unless someone
418 * fscked up my code */
421 msg_Err( p_vout, "no colors left in palette" );
425 /* Clip the colors */
426 p_cmap_r[ j ] = CLIP( r );
427 p_cmap_g[ j ] = CLIP( g );
428 p_cmap_b[ j ] = CLIP( b );
431 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
432 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
450 /* The colors have been allocated, we can set the palette */
451 p_vout->output.pf_setpalette( p_vout, p_cmap_r, p_cmap_g, p_cmap_b );
454 /* There will eventually be a way to know which colors
455 * couldn't be allocated and try to find a replacement */
456 p_vout->i_white_pixel = 0xff;
457 p_vout->i_black_pixel = 0x00;
458 p_vout->i_gray_pixel = 0x44;
459 p_vout->i_blue_pixel = 0x3b;
462 /* This loop allocates colors that got outside the RGB cube */
463 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
465 for ( u = 0; u <= 256; u += 32 )
467 for ( v = 0; v <= 256; v += 32, i++ )
469 int u2, v2, dist, mindist = 100000000;
471 if( p_lookup[ i ] || y == 0 )
477 for( u2 = 0; u2 <= 256; u2 += 32 )
479 for( v2 = 0; v2 <= 256; v2 += 32 )
481 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
482 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
484 /* Find the nearest color */
485 if( p_lookup[ j ] && dist < mindist )
487 p_rgb8[ i ] = p_rgb8[ j ];
493 /* Find the nearest color */
494 if( p_lookup[ j ] && dist + 128 < mindist )
496 p_rgb8[ i ] = p_rgb8[ j ];
497 mindist = dist + 128;