1 /*****************************************************************************
2 * i420_rgb.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: i420_rgb.c,v 1.5 2003/12/04 12:33:43 gbazin Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <math.h> /* exp(), pow() */
28 #include <string.h> /* strerror() */
29 #include <stdlib.h> /* malloc(), free() */
35 #if defined (MODULE_NAME_IS_i420_rgb)
36 # include "i420_rgb_c.h"
39 /*****************************************************************************
40 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
41 *****************************************************************************/
42 #define RGB2PIXEL( p_vout, i_r, i_g, i_b ) \
43 (((((uint32_t)i_r) >> p_vout->output.i_rrshift) \
44 << p_vout->output.i_lrshift) \
45 | ((((uint32_t)i_g) >> p_vout->output.i_rgshift) \
46 << p_vout->output.i_lgshift) \
47 | ((((uint32_t)i_b) >> p_vout->output.i_rbshift) \
48 << p_vout->output.i_lbshift))
50 /*****************************************************************************
51 * Local and extern prototypes.
52 *****************************************************************************/
53 static int Activate ( vlc_object_t * );
54 static void Deactivate ( vlc_object_t * );
56 #if defined (MODULE_NAME_IS_i420_rgb)
57 static void SetGammaTable ( int *pi_table, double f_gamma );
58 static void SetYUV ( vout_thread_t * );
59 static void Set8bppPalette ( vout_thread_t *, uint8_t * );
62 /*****************************************************************************
64 *****************************************************************************/
66 #if defined (MODULE_NAME_IS_i420_rgb)
67 set_description( _("I420,IYUV,YV12 to "
68 "RGB2,RV15,RV16,RV24,RV32 conversions") );
69 set_capability( "chroma", 80 );
70 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
71 set_description( _( "MMX I420,IYUV,YV12 to "
72 "RV15,RV16,RV24,RV32 conversions") );
73 set_capability( "chroma", 100 );
74 add_requirement( MMX );
76 set_callbacks( Activate, Deactivate );
79 /*****************************************************************************
80 * Activate: allocate a chroma function
81 *****************************************************************************
82 * This function allocates and initializes a chroma function
83 *****************************************************************************/
84 static int Activate( vlc_object_t *p_this )
86 vout_thread_t *p_vout = (vout_thread_t *)p_this;
87 #if defined (MODULE_NAME_IS_i420_rgb)
91 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
96 switch( p_vout->render.i_chroma )
98 case VLC_FOURCC('Y','V','1','2'):
99 case VLC_FOURCC('I','4','2','0'):
100 case VLC_FOURCC('I','Y','U','V'):
101 switch( p_vout->output.i_chroma )
103 #if defined (MODULE_NAME_IS_i420_rgb)
104 case VLC_FOURCC('R','G','B','2'):
105 p_vout->chroma.pf_convert = E_(I420_RGB8);
108 case VLC_FOURCC('R','V','1','5'):
109 case VLC_FOURCC('R','V','1','6'):
110 #if defined (MODULE_NAME_IS_i420_rgb_mmx)
111 /* If we don't have support for the bitmasks, bail out */
112 if( ( p_vout->output.i_rmask != 0x7c00
113 || p_vout->output.i_gmask != 0x03e0
114 || p_vout->output.i_bmask != 0x001f )
115 && ( p_vout->output.i_rmask != 0xf800
116 || p_vout->output.i_gmask != 0x07e0
117 || p_vout->output.i_bmask != 0x001f ) )
122 p_vout->chroma.pf_convert = E_(I420_RGB16);
125 #ifndef WIN32 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
126 case VLC_FOURCC('R','V','2','4'):
128 case VLC_FOURCC('R','V','3','2'):
129 #if defined (MODULE_NAME_IS_i420_rgb_mmx)
130 /* If we don't have support for the bitmasks, bail out */
131 if( p_vout->output.i_rmask != 0x00ff0000
132 || p_vout->output.i_gmask != 0x0000ff00
133 || p_vout->output.i_bmask != 0x000000ff )
138 p_vout->chroma.pf_convert = E_(I420_RGB32);
150 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
151 if( p_vout->chroma.p_sys == NULL )
156 switch( p_vout->output.i_chroma )
158 #if defined (MODULE_NAME_IS_i420_rgb)
159 case VLC_FOURCC('R','G','B','2'):
160 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
164 case VLC_FOURCC('R','V','1','5'):
165 case VLC_FOURCC('R','V','1','6'):
166 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
169 case VLC_FOURCC('R','V','2','4'):
170 case VLC_FOURCC('R','V','3','2'):
171 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
175 p_vout->chroma.p_sys->p_buffer = NULL;
179 if( p_vout->chroma.p_sys->p_buffer == NULL )
181 free( p_vout->chroma.p_sys );
185 p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
186 * ( ( p_vout->output.i_chroma
187 == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
189 if( p_vout->chroma.p_sys->p_offset == NULL )
191 free( p_vout->chroma.p_sys->p_buffer );
192 free( p_vout->chroma.p_sys );
196 #if defined (MODULE_NAME_IS_i420_rgb)
197 switch( p_vout->output.i_chroma )
199 case VLC_FOURCC('R','G','B','2'):
200 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
202 case VLC_FOURCC('R','V','1','5'):
203 case VLC_FOURCC('R','V','1','6'):
204 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
206 default: /* RV24, RV32 */
207 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
211 p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
212 if( p_vout->chroma.p_sys->p_base == NULL )
214 free( p_vout->chroma.p_sys->p_offset );
215 free( p_vout->chroma.p_sys->p_buffer );
216 free( p_vout->chroma.p_sys );
226 /*****************************************************************************
227 * Deactivate: free the chroma function
228 *****************************************************************************
229 * This function frees the previously allocated chroma function
230 *****************************************************************************/
231 static void Deactivate( vlc_object_t *p_this )
233 vout_thread_t *p_vout = (vout_thread_t *)p_this;
235 #if defined (MODULE_NAME_IS_i420_rgb)
236 free( p_vout->chroma.p_sys->p_base );
238 free( p_vout->chroma.p_sys->p_offset );
239 free( p_vout->chroma.p_sys->p_buffer );
240 free( p_vout->chroma.p_sys );
243 #if defined (MODULE_NAME_IS_i420_rgb)
244 /*****************************************************************************
245 * SetGammaTable: return intensity table transformed by gamma curve.
246 *****************************************************************************
247 * pi_table is a table of 256 entries from 0 to 255.
248 *****************************************************************************/
249 static void SetGammaTable( int *pi_table, double f_gamma )
251 int i_y; /* base intensity */
253 /* Use exp(gamma) instead of gamma */
254 f_gamma = exp( f_gamma );
256 /* Build gamma table */
257 for( i_y = 0; i_y < 256; i_y++ )
259 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
263 /*****************************************************************************
264 * SetYUV: compute tables and set function pointers
265 *****************************************************************************/
266 static void SetYUV( vout_thread_t *p_vout )
268 int pi_gamma[256]; /* gamma table */
269 int i_index; /* index in tables */
271 /* Build gamma table */
272 SetGammaTable( pi_gamma, p_vout->f_gamma );
275 * Set pointers and build YUV tables
278 /* Color: build red, green and blue tables */
279 switch( p_vout->output.i_chroma )
281 case VLC_FOURCC('R','G','B','2'):
282 p_vout->chroma.p_sys->p_rgb8 = (uint8_t *)p_vout->chroma.p_sys->p_base;
283 Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
286 case VLC_FOURCC('R','V','1','5'):
287 case VLC_FOURCC('R','V','1','6'):
288 p_vout->chroma.p_sys->p_rgb16 = (uint16_t *)p_vout->chroma.p_sys->p_base;
289 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
291 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
292 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
294 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
296 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
297 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
299 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
301 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
302 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
304 for( i_index = 0; i_index < 256; i_index++ )
306 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
307 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
308 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
312 case VLC_FOURCC('R','V','2','4'):
313 case VLC_FOURCC('R','V','3','2'):
314 p_vout->chroma.p_sys->p_rgb32 = (uint32_t *)p_vout->chroma.p_sys->p_base;
315 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
317 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
318 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
320 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
322 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
323 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
325 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
327 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
328 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
330 for( i_index = 0; i_index < 256; i_index++ )
332 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
333 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
334 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
340 static void Set8bppPalette( vout_thread_t *p_vout, uint8_t *p_rgb8 )
342 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
347 uint16_t red[ 256 ], green[ 256 ], blue[ 256 ];
348 unsigned char p_lookup[PALETTE_TABLE_SIZE];
350 /* This loop calculates the intersection of an YUV box and the RGB cube. */
351 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
353 for ( u = 0; u <= 256; u += 32 )
355 for ( v = 0; v <= 256; v += 32 )
357 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
358 g = y + ( (U_GREEN_COEF*(u-128)
359 + V_GREEN_COEF*(v-128)) >> SHIFT );
360 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
362 if( r >= 0x00 && g >= 0x00 && b >= 0x00
363 && r <= 0xff && g <= 0xff && b <= 0xff )
365 /* This one should never happen unless someone
366 * fscked up my code */
369 msg_Err( p_vout, "no colors left in palette" );
373 /* Clip the colors */
374 red[ j ] = CLIP( r );
375 green[ j ] = CLIP( g );
376 blue[ j ] = CLIP( b );
392 /* The colors have been allocated, we can set the palette */
393 p_vout->output.pf_setpalette( p_vout, red, green, blue );
396 /* There will eventually be a way to know which colors
397 * couldn't be allocated and try to find a replacement */
398 p_vout->i_white_pixel = 0xff;
399 p_vout->i_black_pixel = 0x00;
400 p_vout->i_gray_pixel = 0x44;
401 p_vout->i_blue_pixel = 0x3b;
404 /* This loop allocates colors that got outside the RGB cube */
405 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
407 for ( u = 0; u <= 256; u += 32 )
409 for ( v = 0; v <= 256; v += 32, i++ )
411 int u2, v2, dist, mindist = 100000000;
413 if( p_lookup[ i ] || y == 0 )
419 for( u2 = 0; u2 <= 256; u2 += 32 )
421 for( v2 = 0; v2 <= 256; v2 += 32 )
423 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
424 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
426 /* Find the nearest color */
427 if( p_lookup[ j ] && dist < mindist )
429 p_rgb8[ i ] = p_rgb8[ j ];
435 /* Find the nearest color */
436 if( p_lookup[ j ] && dist + 128 < mindist )
438 p_rgb8[ i ] = p_rgb8[ j ];
439 mindist = dist + 128;