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 == 0xff000000
159 && p_vout->output.i_gmask == 0x00ff0000
160 && p_vout->output.i_bmask == 0x0000ff00 )
162 /* R8G8B8A8 pixel format */
163 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
164 p_vout->chroma.pf_convert = E_(I420_R8G8B8A8);
166 else if( p_vout->output.i_rmask == 0x0000ff00
167 && p_vout->output.i_gmask == 0x00ff0000
168 && p_vout->output.i_bmask == 0xff000000 )
170 /* B8G8R8A8 pixel format */
171 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
172 p_vout->chroma.pf_convert = E_(I420_B8G8R8A8);
174 else if( p_vout->output.i_rmask == 0x000000ff
175 && p_vout->output.i_gmask == 0x0000ff00
176 && p_vout->output.i_bmask == 0x00ff0000 )
178 /* A8B8G8R8 pixel format */
179 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
180 p_vout->chroma.pf_convert = E_(I420_A8B8G8R8);
185 /* generic C chroma converter */
186 p_vout->chroma.pf_convert = E_(I420_RGB32);
199 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
200 if( p_vout->chroma.p_sys == NULL )
205 switch( p_vout->output.i_chroma )
207 #if defined (MODULE_NAME_IS_i420_rgb)
208 case VLC_FOURCC('R','G','B','2'):
209 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
213 case VLC_FOURCC('R','V','1','5'):
214 case VLC_FOURCC('R','V','1','6'):
215 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
218 case VLC_FOURCC('R','V','2','4'):
219 case VLC_FOURCC('R','V','3','2'):
220 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
224 p_vout->chroma.p_sys->p_buffer = NULL;
228 if( p_vout->chroma.p_sys->p_buffer == NULL )
230 free( p_vout->chroma.p_sys );
234 p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
235 * ( ( p_vout->output.i_chroma
236 == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
238 if( p_vout->chroma.p_sys->p_offset == NULL )
240 free( p_vout->chroma.p_sys->p_buffer );
241 free( p_vout->chroma.p_sys );
245 #if defined (MODULE_NAME_IS_i420_rgb)
246 switch( p_vout->output.i_chroma )
248 case VLC_FOURCC('R','G','B','2'):
249 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
251 case VLC_FOURCC('R','V','1','5'):
252 case VLC_FOURCC('R','V','1','6'):
253 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
255 default: /* RV24, RV32 */
256 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
260 p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
261 if( p_vout->chroma.p_sys->p_base == NULL )
263 free( p_vout->chroma.p_sys->p_offset );
264 free( p_vout->chroma.p_sys->p_buffer );
265 free( p_vout->chroma.p_sys );
275 /*****************************************************************************
276 * Deactivate: free the chroma function
277 *****************************************************************************
278 * This function frees the previously allocated chroma function
279 *****************************************************************************/
280 static void Deactivate( vlc_object_t *p_this )
282 vout_thread_t *p_vout = (vout_thread_t *)p_this;
284 #if defined (MODULE_NAME_IS_i420_rgb)
285 free( p_vout->chroma.p_sys->p_base );
287 free( p_vout->chroma.p_sys->p_offset );
288 free( p_vout->chroma.p_sys->p_buffer );
289 free( p_vout->chroma.p_sys );
292 #if defined (MODULE_NAME_IS_i420_rgb)
293 /*****************************************************************************
294 * SetGammaTable: return intensity table transformed by gamma curve.
295 *****************************************************************************
296 * pi_table is a table of 256 entries from 0 to 255.
297 *****************************************************************************/
298 static void SetGammaTable( int *pi_table, double f_gamma )
300 int i_y; /* base intensity */
302 /* Use exp(gamma) instead of gamma */
303 f_gamma = exp( f_gamma );
305 /* Build gamma table */
306 for( i_y = 0; i_y < 256; i_y++ )
308 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
312 /*****************************************************************************
313 * SetYUV: compute tables and set function pointers
314 *****************************************************************************/
315 static void SetYUV( vout_thread_t *p_vout )
317 int pi_gamma[256]; /* gamma table */
318 volatile int i_index; /* index in tables */
319 /* We use volatile here to work around a strange gcc-3.3.4
320 * optimization bug */
322 /* Build gamma table */
323 SetGammaTable( pi_gamma, p_vout->f_gamma );
326 * Set pointers and build YUV tables
329 /* Color: build red, green and blue tables */
330 switch( p_vout->output.i_chroma )
332 case VLC_FOURCC('R','G','B','2'):
333 p_vout->chroma.p_sys->p_rgb8 = (uint8_t *)p_vout->chroma.p_sys->p_base;
334 Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
337 case VLC_FOURCC('R','V','1','5'):
338 case VLC_FOURCC('R','V','1','6'):
339 p_vout->chroma.p_sys->p_rgb16 = (uint16_t *)p_vout->chroma.p_sys->p_base;
340 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
342 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
343 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
345 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
347 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
348 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
350 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
352 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
353 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
355 for( i_index = 0; i_index < 256; i_index++ )
357 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
358 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
359 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
363 case VLC_FOURCC('R','V','2','4'):
364 case VLC_FOURCC('R','V','3','2'):
365 p_vout->chroma.p_sys->p_rgb32 = (uint32_t *)p_vout->chroma.p_sys->p_base;
366 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
368 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
369 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
371 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
373 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
374 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
376 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
378 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
379 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
381 for( i_index = 0; i_index < 256; i_index++ )
383 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
384 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
385 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
391 static void Set8bppPalette( vout_thread_t *p_vout, uint8_t *p_rgb8 )
393 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
398 uint16_t *p_cmap_r=p_vout->chroma.p_sys->p_rgb_r;
399 uint16_t *p_cmap_g=p_vout->chroma.p_sys->p_rgb_g;
400 uint16_t *p_cmap_b=p_vout->chroma.p_sys->p_rgb_b;
402 unsigned char p_lookup[PALETTE_TABLE_SIZE];
404 /* This loop calculates the intersection of an YUV box and the RGB cube. */
405 for ( 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 )
411 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
412 g = y + ( (U_GREEN_COEF*(u-128)
413 + V_GREEN_COEF*(v-128)) >> SHIFT );
414 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
416 if( r >= 0x00 && g >= 0x00 && b >= 0x00
417 && r <= 0xff && g <= 0xff && b <= 0xff )
419 /* This one should never happen unless someone
420 * fscked up my code */
423 msg_Err( p_vout, "no colors left in palette" );
427 /* Clip the colors */
428 p_cmap_r[ j ] = CLIP( r );
429 p_cmap_g[ j ] = CLIP( g );
430 p_cmap_b[ j ] = CLIP( b );
433 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
434 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
452 /* The colors have been allocated, we can set the palette */
453 p_vout->output.pf_setpalette( p_vout, p_cmap_r, p_cmap_g, p_cmap_b );
456 /* There will eventually be a way to know which colors
457 * couldn't be allocated and try to find a replacement */
458 p_vout->i_white_pixel = 0xff;
459 p_vout->i_black_pixel = 0x00;
460 p_vout->i_gray_pixel = 0x44;
461 p_vout->i_blue_pixel = 0x3b;
464 /* This loop allocates colors that got outside the RGB cube */
465 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
467 for ( u = 0; u <= 256; u += 32 )
469 for ( v = 0; v <= 256; v += 32, i++ )
471 int u2, v2, dist, mindist = 100000000;
473 if( p_lookup[ i ] || y == 0 )
479 for( u2 = 0; u2 <= 256; u2 += 32 )
481 for( v2 = 0; v2 <= 256; v2 += 32 )
483 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
484 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
486 /* Find the nearest color */
487 if( p_lookup[ j ] && dist < mindist )
489 p_rgb8[ i ] = p_rgb8[ j ];
495 /* Find the nearest color */
496 if( p_lookup[ j ] && dist + 128 < mindist )
498 p_rgb8[ i ] = p_rgb8[ j ];
499 mindist = dist + 128;