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.8 2002/04/19 13:56:10 sam 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 <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
30 #include <stdlib.h> /* malloc(), free() */
32 #include <videolan/vlc.h>
35 #include "video_output.h"
38 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
39 # include "i420_rgb_c.h"
42 /*****************************************************************************
43 * Local and extern prototypes.
44 *****************************************************************************/
45 static void chroma_getfunctions ( function_list_t * p_function_list );
47 static int chroma_Init ( vout_thread_t * );
48 static void chroma_End ( vout_thread_t * );
50 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
51 static void SetGammaTable ( int *pi_table, double f_gamma );
52 static void SetYUV ( vout_thread_t * );
53 static void Set8bppPalette ( vout_thread_t *, u8 * );
56 /*****************************************************************************
57 * Build configuration tree.
58 *****************************************************************************/
63 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
64 SET_DESCRIPTION( _("I420,IYUV,YV12 to "
65 "RGB,RV15,RV16,RV24,RV32 conversions") )
66 ADD_CAPABILITY( CHROMA, 80 )
67 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
68 SET_DESCRIPTION( _( "MMX I420,IYUV,YV12 to "
69 "RV15,RV16,RV24,RV32 conversions") )
70 ADD_CAPABILITY( CHROMA, 100 )
71 ADD_REQUIREMENT( MMX )
76 chroma_getfunctions( &p_module->p_functions->chroma );
79 MODULE_DEACTIVATE_START
80 MODULE_DEACTIVATE_STOP
82 /*****************************************************************************
83 * Functions exported as capabilities. They are declared as static so that
84 * we don't pollute the namespace too much.
85 *****************************************************************************/
86 static void chroma_getfunctions( function_list_t * p_function_list )
88 p_function_list->functions.chroma.pf_init = chroma_Init;
89 p_function_list->functions.chroma.pf_end = chroma_End;
92 /*****************************************************************************
93 * chroma_Init: allocate a chroma function
94 *****************************************************************************
95 * This function allocates and initializes a chroma function
96 *****************************************************************************/
97 static int chroma_Init( vout_thread_t *p_vout )
99 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
100 size_t i_tables_size;
103 if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
108 switch( p_vout->render.i_chroma )
113 switch( p_vout->output.i_chroma )
115 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
117 p_vout->chroma.pf_convert = _M( I420_RGB8 );
121 p_vout->chroma.pf_convert = _M( I420_RGB15 );
125 p_vout->chroma.pf_convert = _M( I420_RGB16 );
130 p_vout->chroma.pf_convert = _M( I420_RGB32 );
142 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
143 if( p_vout->chroma.p_sys == NULL )
148 switch( p_vout->output.i_chroma )
150 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
152 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
158 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
163 p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
167 p_vout->chroma.p_sys->p_buffer = NULL;
171 if( p_vout->chroma.p_sys->p_buffer == NULL )
173 free( p_vout->chroma.p_sys );
177 p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
178 * ( ( p_vout->output.i_chroma == FOURCC_RGB2 ) ? 2 : 1 )
180 if( p_vout->chroma.p_sys->p_offset == NULL )
182 free( p_vout->chroma.p_sys->p_buffer );
183 free( p_vout->chroma.p_sys );
187 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
188 switch( p_vout->output.i_chroma )
191 i_tables_size = sizeof( u8 ) * PALETTE_TABLE_SIZE;
195 i_tables_size = sizeof( u16 ) * RGB_TABLE_SIZE;
197 default: /* RV24, RV32 */
198 i_tables_size = sizeof( u32 ) * RGB_TABLE_SIZE;
202 p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
203 if( p_vout->chroma.p_sys->p_base == NULL )
205 free( p_vout->chroma.p_sys->p_offset );
206 free( p_vout->chroma.p_sys->p_buffer );
207 free( p_vout->chroma.p_sys );
217 /*****************************************************************************
218 * chroma_End: free the chroma function
219 *****************************************************************************
220 * This function frees the previously allocated chroma function
221 *****************************************************************************/
222 static void chroma_End( vout_thread_t *p_vout )
224 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
225 free( p_vout->chroma.p_sys->p_base );
227 free( p_vout->chroma.p_sys->p_offset );
228 free( p_vout->chroma.p_sys->p_buffer );
229 free( p_vout->chroma.p_sys );
232 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
233 /*****************************************************************************
234 * SetGammaTable: return intensity table transformed by gamma curve.
235 *****************************************************************************
236 * pi_table is a table of 256 entries from 0 to 255.
237 *****************************************************************************/
238 static void SetGammaTable( int *pi_table, double f_gamma )
240 int i_y; /* base intensity */
242 /* Use exp(gamma) instead of gamma */
243 f_gamma = exp( f_gamma );
245 /* Build gamma table */
246 for( i_y = 0; i_y < 256; i_y++ )
248 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
252 /*****************************************************************************
253 * SetYUV: compute tables and set function pointers
254 *****************************************************************************/
255 static void SetYUV( vout_thread_t *p_vout )
257 int pi_gamma[256]; /* gamma table */
258 int i_index; /* index in tables */
260 /* Build gamma table */
261 SetGammaTable( pi_gamma, p_vout->f_gamma );
264 * Set pointers and build YUV tables
267 /* Color: build red, green and blue tables */
268 switch( p_vout->output.i_chroma )
271 p_vout->chroma.p_sys->p_rgb8 = (u8 *)p_vout->chroma.p_sys->p_base;
272 Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
277 p_vout->chroma.p_sys->p_rgb16 = (u16 *)p_vout->chroma.p_sys->p_base;
278 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
280 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
281 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
283 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
285 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
286 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
288 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
290 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
291 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
293 for( i_index = 0; i_index < 256; i_index++ )
295 p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
296 p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
297 p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
303 p_vout->chroma.p_sys->p_rgb32 = (u32 *)p_vout->chroma.p_sys->p_base;
304 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
306 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
307 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
309 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
311 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
312 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
314 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
316 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
317 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
319 for( i_index = 0; i_index < 256; i_index++ )
321 p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
322 p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
323 p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
329 static void Set8bppPalette( vout_thread_t *p_vout, u8 *p_rgb8 )
331 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
336 u16 red[ 256 ], green[ 256 ], blue[ 256 ];
337 unsigned char p_lookup[PALETTE_TABLE_SIZE];
339 /* This loop calculates the intersection of an YUV box and the RGB cube. */
340 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
342 for ( u = 0; u <= 256; u += 32 )
344 for ( v = 0; v <= 256; v += 32 )
346 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
347 g = y + ( (U_GREEN_COEF*(u-128)
348 + V_GREEN_COEF*(v-128)) >> SHIFT );
349 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
351 if( r >= 0x00 && g >= 0x00 && b >= 0x00
352 && r <= 0xff && g <= 0xff && b <= 0xff )
354 /* This one should never happen unless someone
355 * fscked up my code */
358 intf_ErrMsg( "vout error: no colors left in palette" );
362 /* Clip the colors */
363 red[ j ] = CLIP( r );
364 green[ j ] = CLIP( g );
365 blue[ j ] = CLIP( b );
381 /* The colors have been allocated, we can set the palette */
382 p_vout->output.pf_setpalette( p_vout, red, green, blue );
385 /* There will eventually be a way to know which colors
386 * couldn't be allocated and try to find a replacement */
387 p_vout->i_white_pixel = 0xff;
388 p_vout->i_black_pixel = 0x00;
389 p_vout->i_gray_pixel = 0x44;
390 p_vout->i_blue_pixel = 0x3b;
393 /* This loop allocates colors that got outside the RGB cube */
394 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
396 for ( u = 0; u <= 256; u += 32 )
398 for ( v = 0; v <= 256; v += 32, i++ )
400 int u2, v2, dist, mindist = 100000000;
402 if( p_lookup[ i ] || y == 0 )
408 for( u2 = 0; u2 <= 256; u2 += 32 )
410 for( v2 = 0; v2 <= 256; v2 += 32 )
412 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
413 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
415 /* Find the nearest color */
416 if( p_lookup[ j ] && dist < mindist )
418 p_rgb8[ i ] = p_rgb8[ j ];
424 /* Find the nearest color */
425 if( p_lookup[ j ] && dist + 128 < mindist )
427 p_rgb8[ i ] = p_rgb8[ j ];
428 mindist = dist + 128;