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() */
35 #include <vlc_plugin.h>
39 #if defined (MODULE_NAME_IS_i420_rgb)
40 # include "i420_rgb_c.h"
43 /*****************************************************************************
44 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
45 *****************************************************************************/
46 #define RGB2PIXEL( p_vout, i_r, i_g, i_b ) \
47 (((((uint32_t)i_r) >> p_vout->output.i_rrshift) \
48 << p_vout->output.i_lrshift) \
49 | ((((uint32_t)i_g) >> p_vout->output.i_rgshift) \
50 << p_vout->output.i_lgshift) \
51 | ((((uint32_t)i_b) >> p_vout->output.i_rbshift) \
52 << p_vout->output.i_lbshift))
54 /*****************************************************************************
55 * Local and extern prototypes.
56 *****************************************************************************/
57 static int Activate ( vlc_object_t * );
58 static void Deactivate ( vlc_object_t * );
60 #if defined (MODULE_NAME_IS_i420_rgb)
61 static void SetGammaTable ( int *pi_table, double f_gamma );
62 static void SetYUV ( vout_thread_t * );
63 static void Set8bppPalette ( vout_thread_t *, uint8_t * );
66 /*****************************************************************************
68 *****************************************************************************/
70 #if defined (MODULE_NAME_IS_i420_rgb)
71 set_description( _("I420,IYUV,YV12 to "
72 "RGB2,RV15,RV16,RV24,RV32 conversions") );
73 set_capability( "chroma", 80 );
74 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
75 set_description( _( "MMX I420,IYUV,YV12 to "
76 "RV15,RV16,RV24,RV32 conversions") );
77 set_capability( "chroma", 100 );
78 add_requirement( MMX );
79 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
80 set_description( _( "SSE2 I420,IYUV,YV12 to "
81 "RV15,RV16,RV24,RV32 conversions") );
82 set_capability( "chroma", 120 );
83 add_requirement( SSE2 );
85 set_callbacks( Activate, Deactivate );
88 /*****************************************************************************
89 * Activate: allocate a chroma function
90 *****************************************************************************
91 * This function allocates and initializes a chroma function
92 *****************************************************************************/
93 static int Activate( vlc_object_t *p_this )
95 vout_thread_t *p_vout = (vout_thread_t *)p_this;
96 #if defined (MODULE_NAME_IS_i420_rgb)
100 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
105 switch( p_vout->render.i_chroma )
107 case VLC_FOURCC('Y','V','1','2'):
108 case VLC_FOURCC('I','4','2','0'):
109 case VLC_FOURCC('I','Y','U','V'):
110 switch( p_vout->output.i_chroma )
112 #if defined (MODULE_NAME_IS_i420_rgb)
113 case VLC_FOURCC('R','G','B','2'):
114 p_vout->chroma.pf_convert = E_(I420_RGB8);
117 case VLC_FOURCC('R','V','1','5'):
118 case VLC_FOURCC('R','V','1','6'):
119 #if ! defined (MODULE_NAME_IS_i420_rgb)
120 /* If we don't have support for the bitmasks, bail out */
121 if( ( p_vout->output.i_rmask == 0x7c00
122 && p_vout->output.i_gmask == 0x03e0
123 && p_vout->output.i_bmask == 0x001f ) )
125 /* R5G5B6 pixel format */
126 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
127 p_vout->chroma.pf_convert = E_(I420_R5G5B5);
129 else if( ( p_vout->output.i_rmask == 0xf800
130 && p_vout->output.i_gmask == 0x07e0
131 && p_vout->output.i_bmask == 0x001f ) )
133 /* R5G6B5 pixel format */
134 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
135 p_vout->chroma.pf_convert = E_(I420_R5G6B5);
140 // generic C chroma converter */
141 p_vout->chroma.pf_convert = E_(I420_RGB16);
146 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
147 case VLC_FOURCC('R','V','2','4'):
150 case VLC_FOURCC('R','V','3','2'):
151 #if ! defined (MODULE_NAME_IS_i420_rgb)
152 /* If we don't have support for the bitmasks, bail out */
153 if( p_vout->output.i_rmask == 0x00ff0000
154 && p_vout->output.i_gmask == 0x0000ff00
155 && p_vout->output.i_bmask == 0x000000ff )
157 /* A8R8G8B8 pixel format */
158 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
159 p_vout->chroma.pf_convert = E_(I420_A8R8G8B8);
161 else if( p_vout->output.i_rmask == 0xff000000
162 && p_vout->output.i_gmask == 0x00ff0000
163 && p_vout->output.i_bmask == 0x0000ff00 )
165 /* R8G8B8A8 pixel format */
166 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
167 p_vout->chroma.pf_convert = E_(I420_R8G8B8A8);
169 else if( p_vout->output.i_rmask == 0x0000ff00
170 && p_vout->output.i_gmask == 0x00ff0000
171 && p_vout->output.i_bmask == 0xff000000 )
173 /* B8G8R8A8 pixel format */
174 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
175 p_vout->chroma.pf_convert = E_(I420_B8G8R8A8);
177 else if( p_vout->output.i_rmask == 0x000000ff
178 && p_vout->output.i_gmask == 0x0000ff00
179 && p_vout->output.i_bmask == 0x00ff0000 )
181 /* A8B8G8R8 pixel format */
182 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
183 p_vout->chroma.pf_convert = E_(I420_A8B8G8R8);
188 /* generic C chroma converter */
189 p_vout->chroma.pf_convert = E_(I420_RGB32);
202 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
203 if( p_vout->chroma.p_sys == NULL )
208 switch( p_vout->output.i_chroma )
210 #if defined (MODULE_NAME_IS_i420_rgb)
211 case VLC_FOURCC('R','G','B','2'):
212 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
216 case VLC_FOURCC('R','V','1','5'):
217 case VLC_FOURCC('R','V','1','6'):
218 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
221 case VLC_FOURCC('R','V','2','4'):
222 case VLC_FOURCC('R','V','3','2'):
223 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
227 p_vout->chroma.p_sys->p_buffer = NULL;
231 if( p_vout->chroma.p_sys->p_buffer == NULL )
233 free( p_vout->chroma.p_sys );
237 p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
238 * ( ( p_vout->output.i_chroma
239 == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
241 if( p_vout->chroma.p_sys->p_offset == NULL )
243 free( p_vout->chroma.p_sys->p_buffer );
244 free( p_vout->chroma.p_sys );
248 #if defined (MODULE_NAME_IS_i420_rgb)
249 switch( p_vout->output.i_chroma )
251 case VLC_FOURCC('R','G','B','2'):
252 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
254 case VLC_FOURCC('R','V','1','5'):
255 case VLC_FOURCC('R','V','1','6'):
256 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
258 default: /* RV24, RV32 */
259 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
263 p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
264 if( p_vout->chroma.p_sys->p_base == NULL )
266 free( p_vout->chroma.p_sys->p_offset );
267 free( p_vout->chroma.p_sys->p_buffer );
268 free( p_vout->chroma.p_sys );
278 /*****************************************************************************
279 * Deactivate: free the chroma function
280 *****************************************************************************
281 * This function frees the previously allocated chroma function
282 *****************************************************************************/
283 static void Deactivate( vlc_object_t *p_this )
285 vout_thread_t *p_vout = (vout_thread_t *)p_this;
287 #if defined (MODULE_NAME_IS_i420_rgb)
288 free( p_vout->chroma.p_sys->p_base );
290 free( p_vout->chroma.p_sys->p_offset );
291 free( p_vout->chroma.p_sys->p_buffer );
292 free( p_vout->chroma.p_sys );
295 #if defined (MODULE_NAME_IS_i420_rgb)
296 /*****************************************************************************
297 * SetGammaTable: return intensity table transformed by gamma curve.
298 *****************************************************************************
299 * pi_table is a table of 256 entries from 0 to 255.
300 *****************************************************************************/
301 static void SetGammaTable( int *pi_table, double f_gamma )
303 int i_y; /* base intensity */
305 /* Use exp(gamma) instead of gamma */
306 f_gamma = exp( f_gamma );
308 /* Build gamma table */
309 for( i_y = 0; i_y < 256; i_y++ )
311 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
315 /*****************************************************************************
316 * SetYUV: compute tables and set function pointers
317 *****************************************************************************/
318 static void SetYUV( vout_thread_t *p_vout )
320 int pi_gamma[256]; /* gamma table */
321 volatile int i_index; /* index in tables */
322 /* We use volatile here to work around a strange gcc-3.3.4
323 * optimization bug */
325 /* Build gamma table */
326 SetGammaTable( pi_gamma, p_vout->f_gamma );
329 * Set pointers and build YUV tables
332 /* Color: build red, green and blue tables */
333 switch( p_vout->output.i_chroma )
335 case VLC_FOURCC('R','G','B','2'):
336 p_vout->chroma.p_sys->p_rgb8 = (uint8_t *)p_vout->chroma.p_sys->p_base;
337 Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
340 case VLC_FOURCC('R','V','1','5'):
341 case VLC_FOURCC('R','V','1','6'):
342 p_vout->chroma.p_sys->p_rgb16 = (uint16_t *)p_vout->chroma.p_sys->p_base;
343 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
345 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
346 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
348 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
350 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
351 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
353 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
355 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
356 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
358 for( i_index = 0; i_index < 256; i_index++ )
360 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
361 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
362 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
366 case VLC_FOURCC('R','V','2','4'):
367 case VLC_FOURCC('R','V','3','2'):
368 p_vout->chroma.p_sys->p_rgb32 = (uint32_t *)p_vout->chroma.p_sys->p_base;
369 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
371 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
372 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
374 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
376 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
377 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
379 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
381 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
382 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
384 for( i_index = 0; i_index < 256; i_index++ )
386 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
387 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
388 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
394 static void Set8bppPalette( vout_thread_t *p_vout, uint8_t *p_rgb8 )
396 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
401 uint16_t *p_cmap_r=p_vout->chroma.p_sys->p_rgb_r;
402 uint16_t *p_cmap_g=p_vout->chroma.p_sys->p_rgb_g;
403 uint16_t *p_cmap_b=p_vout->chroma.p_sys->p_rgb_b;
405 unsigned char p_lookup[PALETTE_TABLE_SIZE];
407 /* This loop calculates the intersection of an YUV box and the RGB cube. */
408 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
410 for ( u = 0; u <= 256; u += 32 )
412 for ( v = 0; v <= 256; v += 32 )
414 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
415 g = y + ( (U_GREEN_COEF*(u-128)
416 + V_GREEN_COEF*(v-128)) >> SHIFT );
417 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
419 if( r >= 0x00 && g >= 0x00 && b >= 0x00
420 && r <= 0xff && g <= 0xff && b <= 0xff )
422 /* This one should never happen unless someone
423 * fscked up my code */
426 msg_Err( p_vout, "no colors left in palette" );
430 /* Clip the colors */
431 p_cmap_r[ j ] = CLIP( r );
432 p_cmap_g[ j ] = CLIP( g );
433 p_cmap_b[ j ] = CLIP( b );
436 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
437 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
455 /* The colors have been allocated, we can set the palette */
456 p_vout->output.pf_setpalette( p_vout, p_cmap_r, p_cmap_g, p_cmap_b );
459 /* There will eventually be a way to know which colors
460 * couldn't be allocated and try to find a replacement */
461 p_vout->i_white_pixel = 0xff;
462 p_vout->i_black_pixel = 0x00;
463 p_vout->i_gray_pixel = 0x44;
464 p_vout->i_blue_pixel = 0x3b;
467 /* This loop allocates colors that got outside the RGB cube */
468 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
470 for ( u = 0; u <= 256; u += 32 )
472 for ( v = 0; v <= 256; v += 32, i++ )
474 int u2, v2, dist, mindist = 100000000;
476 if( p_lookup[ i ] || y == 0 )
482 for( u2 = 0; u2 <= 256; u2 += 32 )
484 for( v2 = 0; v2 <= 256; v2 += 32 )
486 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
487 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
489 /* Find the nearest color */
490 if( p_lookup[ j ] && dist < mindist )
492 p_rgb8[ i ] = p_rgb8[ j ];
498 /* Find the nearest color */
499 if( p_lookup[ j ] && dist + 128 < mindist )
501 p_rgb8[ i ] = p_rgb8[ j ];
502 mindist = dist + 128;