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() */
29 #include <string.h> /* strerror() */
30 #include <stdlib.h> /* malloc(), free() */
36 #if defined (MODULE_NAME_IS_i420_rgb)
37 # include "i420_rgb_c.h"
40 /*****************************************************************************
41 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
42 *****************************************************************************/
43 #define RGB2PIXEL( p_vout, i_r, i_g, i_b ) \
44 (((((uint32_t)i_r) >> p_vout->output.i_rrshift) \
45 << p_vout->output.i_lrshift) \
46 | ((((uint32_t)i_g) >> p_vout->output.i_rgshift) \
47 << p_vout->output.i_lgshift) \
48 | ((((uint32_t)i_b) >> p_vout->output.i_rbshift) \
49 << p_vout->output.i_lbshift))
51 /*****************************************************************************
52 * Local and extern prototypes.
53 *****************************************************************************/
54 static int Activate ( vlc_object_t * );
55 static void Deactivate ( vlc_object_t * );
57 #if defined (MODULE_NAME_IS_i420_rgb)
58 static void SetGammaTable ( int *pi_table, double f_gamma );
59 static void SetYUV ( vout_thread_t * );
60 static void Set8bppPalette ( vout_thread_t *, uint8_t * );
63 /*****************************************************************************
65 *****************************************************************************/
67 #if defined (MODULE_NAME_IS_i420_rgb)
68 set_description( _("I420,IYUV,YV12 to "
69 "RGB2,RV15,RV16,RV24,RV32 conversions") );
70 set_capability( "chroma", 80 );
71 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
72 set_description( _( "MMX I420,IYUV,YV12 to "
73 "RV15,RV16,RV24,RV32 conversions") );
74 set_capability( "chroma", 100 );
75 add_requirement( MMX );
76 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
77 set_description( _( "SSE2 I420,IYUV,YV12 to "
78 "RV15,RV16,RV24,RV32 conversions") );
79 set_capability( "chroma", 120 );
80 add_requirement( SSE2 );
82 set_callbacks( Activate, Deactivate );
85 /*****************************************************************************
86 * Activate: allocate a chroma function
87 *****************************************************************************
88 * This function allocates and initializes a chroma function
89 *****************************************************************************/
90 static int Activate( vlc_object_t *p_this )
92 vout_thread_t *p_vout = (vout_thread_t *)p_this;
93 #if defined (MODULE_NAME_IS_i420_rgb)
97 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
102 switch( p_vout->render.i_chroma )
104 case VLC_FOURCC('Y','V','1','2'):
105 case VLC_FOURCC('I','4','2','0'):
106 case VLC_FOURCC('I','Y','U','V'):
107 switch( p_vout->output.i_chroma )
109 #if defined (MODULE_NAME_IS_i420_rgb)
110 case VLC_FOURCC('R','G','B','2'):
111 p_vout->chroma.pf_convert = E_(I420_RGB8);
114 case VLC_FOURCC('R','V','1','5'):
115 case VLC_FOURCC('R','V','1','6'):
116 #if ! defined (MODULE_NAME_IS_i420_rgb)
117 /* If we don't have support for the bitmasks, bail out */
118 if( ( p_vout->output.i_rmask == 0x7c00
119 && p_vout->output.i_gmask == 0x03e0
120 && p_vout->output.i_bmask == 0x001f ) )
122 /* R5G5B6 pixel format */
123 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
124 p_vout->chroma.pf_convert = E_(I420_R5G5B5);
126 else if( ( p_vout->output.i_rmask == 0xf800
127 && p_vout->output.i_gmask == 0x07e0
128 && p_vout->output.i_bmask == 0x001f ) )
130 /* R5G6B5 pixel format */
131 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
132 p_vout->chroma.pf_convert = E_(I420_R5G6B5);
137 // generic C chroma converter */
138 p_vout->chroma.pf_convert = E_(I420_RGB16);
143 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
144 case VLC_FOURCC('R','V','2','4'):
147 case VLC_FOURCC('R','V','3','2'):
148 #if ! defined (MODULE_NAME_IS_i420_rgb)
149 /* If we don't have support for the bitmasks, bail out */
150 if( p_vout->output.i_rmask == 0x00ff0000
151 && p_vout->output.i_gmask == 0x0000ff00
152 && p_vout->output.i_bmask == 0x000000ff )
154 /* A8R8G8B8 pixel format */
155 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
156 p_vout->chroma.pf_convert = E_(I420_A8R8G8B8);
158 else if( p_vout->output.i_rmask == 0x0000ff00
159 && p_vout->output.i_gmask == 0x00ff0000
160 && p_vout->output.i_bmask == 0xff000000 )
162 /* B8G8R8A8 pixel format */
163 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
164 p_vout->chroma.pf_convert = E_(I420_B8G8R8A8);
169 // generic C chroma converter */
170 p_vout->chroma.pf_convert = E_(I420_RGB32);
183 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
184 if( p_vout->chroma.p_sys == NULL )
189 switch( p_vout->output.i_chroma )
191 #if defined (MODULE_NAME_IS_i420_rgb)
192 case VLC_FOURCC('R','G','B','2'):
193 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
197 case VLC_FOURCC('R','V','1','5'):
198 case VLC_FOURCC('R','V','1','6'):
199 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
202 case VLC_FOURCC('R','V','2','4'):
203 case VLC_FOURCC('R','V','3','2'):
204 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
208 p_vout->chroma.p_sys->p_buffer = NULL;
212 if( p_vout->chroma.p_sys->p_buffer == NULL )
214 free( p_vout->chroma.p_sys );
218 p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
219 * ( ( p_vout->output.i_chroma
220 == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
222 if( p_vout->chroma.p_sys->p_offset == NULL )
224 free( p_vout->chroma.p_sys->p_buffer );
225 free( p_vout->chroma.p_sys );
229 #if defined (MODULE_NAME_IS_i420_rgb)
230 switch( p_vout->output.i_chroma )
232 case VLC_FOURCC('R','G','B','2'):
233 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
235 case VLC_FOURCC('R','V','1','5'):
236 case VLC_FOURCC('R','V','1','6'):
237 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
239 default: /* RV24, RV32 */
240 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
244 p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
245 if( p_vout->chroma.p_sys->p_base == NULL )
247 free( p_vout->chroma.p_sys->p_offset );
248 free( p_vout->chroma.p_sys->p_buffer );
249 free( p_vout->chroma.p_sys );
259 /*****************************************************************************
260 * Deactivate: free the chroma function
261 *****************************************************************************
262 * This function frees the previously allocated chroma function
263 *****************************************************************************/
264 static void Deactivate( vlc_object_t *p_this )
266 vout_thread_t *p_vout = (vout_thread_t *)p_this;
268 #if defined (MODULE_NAME_IS_i420_rgb)
269 free( p_vout->chroma.p_sys->p_base );
271 free( p_vout->chroma.p_sys->p_offset );
272 free( p_vout->chroma.p_sys->p_buffer );
273 free( p_vout->chroma.p_sys );
276 #if defined (MODULE_NAME_IS_i420_rgb)
277 /*****************************************************************************
278 * SetGammaTable: return intensity table transformed by gamma curve.
279 *****************************************************************************
280 * pi_table is a table of 256 entries from 0 to 255.
281 *****************************************************************************/
282 static void SetGammaTable( int *pi_table, double f_gamma )
284 int i_y; /* base intensity */
286 /* Use exp(gamma) instead of gamma */
287 f_gamma = exp( f_gamma );
289 /* Build gamma table */
290 for( i_y = 0; i_y < 256; i_y++ )
292 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
296 /*****************************************************************************
297 * SetYUV: compute tables and set function pointers
298 *****************************************************************************/
299 static void SetYUV( vout_thread_t *p_vout )
301 int pi_gamma[256]; /* gamma table */
302 volatile int i_index; /* index in tables */
303 /* We use volatile here to work around a strange gcc-3.3.4
304 * optimization bug */
306 /* Build gamma table */
307 SetGammaTable( pi_gamma, p_vout->f_gamma );
310 * Set pointers and build YUV tables
313 /* Color: build red, green and blue tables */
314 switch( p_vout->output.i_chroma )
316 case VLC_FOURCC('R','G','B','2'):
317 p_vout->chroma.p_sys->p_rgb8 = (uint8_t *)p_vout->chroma.p_sys->p_base;
318 Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
321 case VLC_FOURCC('R','V','1','5'):
322 case VLC_FOURCC('R','V','1','6'):
323 p_vout->chroma.p_sys->p_rgb16 = (uint16_t *)p_vout->chroma.p_sys->p_base;
324 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
326 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
327 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
329 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
331 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
332 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
334 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
336 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
337 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
339 for( i_index = 0; i_index < 256; i_index++ )
341 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
342 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
343 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
347 case VLC_FOURCC('R','V','2','4'):
348 case VLC_FOURCC('R','V','3','2'):
349 p_vout->chroma.p_sys->p_rgb32 = (uint32_t *)p_vout->chroma.p_sys->p_base;
350 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
352 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
353 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
355 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
357 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
358 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
360 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
362 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
363 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
365 for( i_index = 0; i_index < 256; i_index++ )
367 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
368 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
369 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
375 static void Set8bppPalette( vout_thread_t *p_vout, uint8_t *p_rgb8 )
377 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
382 uint16_t *p_cmap_r=p_vout->chroma.p_sys->p_rgb_r;
383 uint16_t *p_cmap_g=p_vout->chroma.p_sys->p_rgb_g;
384 uint16_t *p_cmap_b=p_vout->chroma.p_sys->p_rgb_b;
386 unsigned char p_lookup[PALETTE_TABLE_SIZE];
388 /* This loop calculates the intersection of an YUV box and the RGB cube. */
389 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
391 for ( u = 0; u <= 256; u += 32 )
393 for ( v = 0; v <= 256; v += 32 )
395 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
396 g = y + ( (U_GREEN_COEF*(u-128)
397 + V_GREEN_COEF*(v-128)) >> SHIFT );
398 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
400 if( r >= 0x00 && g >= 0x00 && b >= 0x00
401 && r <= 0xff && g <= 0xff && b <= 0xff )
403 /* This one should never happen unless someone
404 * fscked up my code */
407 msg_Err( p_vout, "no colors left in palette" );
411 /* Clip the colors */
412 p_cmap_r[ j ] = CLIP( r );
413 p_cmap_g[ j ] = CLIP( g );
414 p_cmap_b[ j ] = CLIP( b );
417 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
418 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
436 /* The colors have been allocated, we can set the palette */
437 p_vout->output.pf_setpalette( p_vout, p_cmap_r, p_cmap_g, p_cmap_b );
440 /* There will eventually be a way to know which colors
441 * couldn't be allocated and try to find a replacement */
442 p_vout->i_white_pixel = 0xff;
443 p_vout->i_black_pixel = 0x00;
444 p_vout->i_gray_pixel = 0x44;
445 p_vout->i_blue_pixel = 0x3b;
448 /* This loop allocates colors that got outside the RGB cube */
449 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
451 for ( u = 0; u <= 256; u += 32 )
453 for ( v = 0; v <= 256; v += 32, i++ )
455 int u2, v2, dist, mindist = 100000000;
457 if( p_lookup[ i ] || y == 0 )
463 for( u2 = 0; u2 <= 256; u2 += 32 )
465 for( v2 = 0; v2 <= 256; v2 += 32 )
467 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
468 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
470 /* Find the nearest color */
471 if( p_lookup[ j ] && dist < mindist )
473 p_rgb8[ i ] = p_rgb8[ j ];
479 /* Find the nearest color */
480 if( p_lookup[ j ] && dist + 128 < mindist )
482 p_rgb8[ i ] = p_rgb8[ j ];
483 mindist = dist + 128;