1 /*****************************************************************************
2 * i420_rgb.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2004, 2008 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 *****************************************************************************/
33 #include <math.h> /* exp(), pow() */
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
40 #if defined (MODULE_NAME_IS_i420_rgb)
41 # include "i420_rgb_c.h"
44 /*****************************************************************************
45 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
46 *****************************************************************************/
47 #define RGB2PIXEL( p_filter, i_r, i_g, i_b ) \
48 (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) \
49 << p_filter->fmt_out.video.i_lrshift) \
50 | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) \
51 << p_filter->fmt_out.video.i_lgshift) \
52 | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) \
53 << p_filter->fmt_out.video.i_lbshift))
55 /*****************************************************************************
56 * Local and extern prototypes.
57 *****************************************************************************/
58 static int Activate ( vlc_object_t * );
59 static void Deactivate ( vlc_object_t * );
61 #if defined (MODULE_NAME_IS_i420_rgb)
62 static void SetGammaTable ( int *pi_table, double f_gamma );
63 static void SetYUV ( filter_t * );
64 static void Set8bppPalette ( filter_t *, uint8_t * );
67 /*****************************************************************************
69 *****************************************************************************/
71 #if defined (MODULE_NAME_IS_i420_rgb)
72 set_description( N_("I420,IYUV,YV12 to "
73 "RGB2,RV15,RV16,RV24,RV32 conversions") )
74 set_capability( "video filter2", 80 )
75 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
76 set_description( N_( "MMX I420,IYUV,YV12 to "
77 "RV15,RV16,RV24,RV32 conversions") )
78 set_capability( "video filter2", 100 )
79 add_requirement( MMX )
80 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
81 set_description( N_( "SSE2 I420,IYUV,YV12 to "
82 "RV15,RV16,RV24,RV32 conversions") )
83 set_capability( "video filter2", 120 )
84 add_requirement( SSE2 )
86 set_callbacks( Activate, Deactivate )
89 /*****************************************************************************
90 * Activate: allocate a chroma function
91 *****************************************************************************
92 * This function allocates and initializes a chroma function
93 *****************************************************************************/
94 static int Activate( vlc_object_t *p_this )
96 filter_t *p_filter = (filter_t *)p_this;
97 #if defined (MODULE_NAME_IS_i420_rgb)
101 if( p_filter->fmt_out.video.i_width & 1
102 || p_filter->fmt_out.video.i_height & 1 )
107 switch( p_filter->fmt_in.video.i_chroma )
111 switch( p_filter->fmt_out.video.i_chroma )
113 #if defined (MODULE_NAME_IS_i420_rgb)
115 p_filter->pf_video_filter = I420_RGB8_Filter;
118 case VLC_CODEC_RGB15:
119 case VLC_CODEC_RGB16:
120 #if ! defined (MODULE_NAME_IS_i420_rgb)
121 /* If we don't have support for the bitmasks, bail out */
122 if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
123 && p_filter->fmt_out.video.i_gmask == 0x03e0
124 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
126 /* R5G5B6 pixel format */
127 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
128 p_filter->pf_video_filter = I420_R5G5B5_Filter;
130 else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
131 && p_filter->fmt_out.video.i_gmask == 0x07e0
132 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
134 /* R5G6B5 pixel format */
135 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
136 p_filter->pf_video_filter = I420_R5G6B5_Filter;
141 // generic C chroma converter */
142 p_filter->pf_video_filter = I420_RGB16_Filter;
147 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
148 case VLC_CODEC_RGB24:
151 case VLC_CODEC_RGB32:
152 #if ! defined (MODULE_NAME_IS_i420_rgb)
153 /* If we don't have support for the bitmasks, bail out */
154 if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
155 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
156 && p_filter->fmt_out.video.i_bmask == 0x000000ff )
158 /* A8R8G8B8 pixel format */
159 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
160 p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
162 else if( p_filter->fmt_out.video.i_rmask == 0xff000000
163 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
164 && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
166 /* R8G8B8A8 pixel format */
167 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
168 p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
170 else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
171 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
172 && p_filter->fmt_out.video.i_bmask == 0xff000000 )
174 /* B8G8R8A8 pixel format */
175 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
176 p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
178 else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
179 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
180 && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
182 /* A8B8G8R8 pixel format */
183 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
184 p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
189 /* generic C chroma converter */
190 p_filter->pf_video_filter = I420_RGB32_Filter;
203 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
204 if( p_filter->p_sys == NULL )
209 switch( p_filter->fmt_out.video.i_chroma )
211 #if defined (MODULE_NAME_IS_i420_rgb)
213 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
217 case VLC_CODEC_RGB15:
218 case VLC_CODEC_RGB16:
219 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
222 case VLC_CODEC_RGB24:
223 case VLC_CODEC_RGB32:
224 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
228 p_filter->p_sys->p_buffer = NULL;
232 if( p_filter->p_sys->p_buffer == NULL )
234 free( p_filter->p_sys );
238 p_filter->p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
239 * ( ( p_filter->fmt_out.video.i_chroma
240 == VLC_CODEC_RGB8 ) ? 2 : 1 )
242 if( p_filter->p_sys->p_offset == NULL )
244 free( p_filter->p_sys->p_buffer );
245 free( p_filter->p_sys );
249 #if defined (MODULE_NAME_IS_i420_rgb)
250 switch( p_filter->fmt_out.video.i_chroma )
253 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
255 case VLC_CODEC_RGB15:
256 case VLC_CODEC_RGB16:
257 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
259 default: /* RV24, RV32 */
260 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
264 p_filter->p_sys->p_base = malloc( i_tables_size );
265 if( p_filter->p_sys->p_base == NULL )
267 free( p_filter->p_sys->p_offset );
268 free( p_filter->p_sys->p_buffer );
269 free( p_filter->p_sys );
279 /*****************************************************************************
280 * Deactivate: free the chroma function
281 *****************************************************************************
282 * This function frees the previously allocated chroma function
283 *****************************************************************************/
284 static void Deactivate( vlc_object_t *p_this )
286 filter_t *p_filter = (filter_t *)p_this;
288 #if defined (MODULE_NAME_IS_i420_rgb)
289 free( p_filter->p_sys->p_base );
291 free( p_filter->p_sys->p_offset );
292 free( p_filter->p_sys->p_buffer );
293 free( p_filter->p_sys );
296 #if defined (MODULE_NAME_IS_i420_rgb)
297 VIDEO_FILTER_WRAPPER( I420_RGB8 )
298 VIDEO_FILTER_WRAPPER( I420_RGB16 )
299 VIDEO_FILTER_WRAPPER( I420_RGB32 )
301 VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
302 VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
303 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
304 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
305 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
306 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
309 #if defined (MODULE_NAME_IS_i420_rgb)
310 /*****************************************************************************
311 * SetGammaTable: return intensity table transformed by gamma curve.
312 *****************************************************************************
313 * pi_table is a table of 256 entries from 0 to 255.
314 *****************************************************************************/
315 static void SetGammaTable( int *pi_table, double f_gamma )
317 int i_y; /* base intensity */
319 /* Use exp(gamma) instead of gamma */
320 f_gamma = exp( f_gamma );
322 /* Build gamma table */
323 for( i_y = 0; i_y < 256; i_y++ )
325 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
329 /*****************************************************************************
330 * SetYUV: compute tables and set function pointers
331 *****************************************************************************/
332 static void SetYUV( filter_t *p_filter )
334 int pi_gamma[256]; /* gamma table */
335 volatile int i_index; /* index in tables */
336 /* We use volatile here to work around a strange gcc-3.3.4
337 * optimization bug */
339 /* Build gamma table */
340 SetGammaTable( pi_gamma, 0 ); //p_filter/*FIXME wasn't used anywhere anyway*/->f_gamma );
343 * Set pointers and build YUV tables
346 /* Color: build red, green and blue tables */
347 switch( p_filter->fmt_out.video.i_chroma )
350 p_filter->p_sys->p_rgb8 = (uint8_t *)p_filter->p_sys->p_base;
351 Set8bppPalette( p_filter, p_filter->p_sys->p_rgb8 );
354 case VLC_CODEC_RGB15:
355 case VLC_CODEC_RGB16:
356 p_filter->p_sys->p_rgb16 = (uint16_t *)p_filter->p_sys->p_base;
357 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
359 p_filter->p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
360 p_filter->p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
362 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
364 p_filter->p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
365 p_filter->p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
367 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
369 p_filter->p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
370 p_filter->p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
372 for( i_index = 0; i_index < 256; i_index++ )
374 p_filter->p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
375 p_filter->p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
376 p_filter->p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
380 case VLC_CODEC_RGB24:
381 case VLC_CODEC_RGB32:
382 p_filter->p_sys->p_rgb32 = (uint32_t *)p_filter->p_sys->p_base;
383 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
385 p_filter->p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
386 p_filter->p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
388 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
390 p_filter->p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
391 p_filter->p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
393 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
395 p_filter->p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
396 p_filter->p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
398 for( i_index = 0; i_index < 256; i_index++ )
400 p_filter->p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
401 p_filter->p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
402 p_filter->p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
408 static void Set8bppPalette( filter_t *p_filter, uint8_t *p_rgb8 )
410 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
415 uint16_t *p_cmap_r = p_filter->p_sys->p_rgb_r;
416 uint16_t *p_cmap_g = p_filter->p_sys->p_rgb_g;
417 uint16_t *p_cmap_b = p_filter->p_sys->p_rgb_b;
419 unsigned char p_lookup[PALETTE_TABLE_SIZE];
421 /* This loop calculates the intersection of an YUV box and the RGB cube. */
422 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
424 for ( u = 0; u <= 256; u += 32 )
426 for ( v = 0; v <= 256; v += 32 )
428 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
429 g = y + ( (U_GREEN_COEF*(u-128)
430 + V_GREEN_COEF*(v-128)) >> SHIFT );
431 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
433 if( r >= 0x00 && g >= 0x00 && b >= 0x00
434 && r <= 0xff && g <= 0xff && b <= 0xff )
436 /* This one should never happen unless someone
437 * fscked up my code */
440 msg_Err( p_filter, "no colors left in palette" );
444 /* Clip the colors */
445 p_cmap_r[ j ] = CLIP( r );
446 p_cmap_g[ j ] = CLIP( g );
447 p_cmap_b[ j ] = CLIP( b );
450 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
451 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
469 /* The colors have been allocated, we can set the palette */
470 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
471 p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
474 /* There will eventually be a way to know which colors
475 * couldn't be allocated and try to find a replacement */
476 p_vout->i_white_pixel = 0xff;
477 p_vout->i_black_pixel = 0x00;
478 p_vout->i_gray_pixel = 0x44;
479 p_vout->i_blue_pixel = 0x3b;
482 /* This loop allocates colors that got outside the RGB cube */
483 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
485 for ( u = 0; u <= 256; u += 32 )
487 for ( v = 0; v <= 256; v += 32, i++ )
489 int u2, v2, dist, mindist = 100000000;
491 if( p_lookup[ i ] || y == 0 )
497 for( u2 = 0; u2 <= 256; u2 += 32 )
499 for( v2 = 0; v2 <= 256; v2 += 32 )
501 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
502 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
504 /* Find the nearest color */
505 if( p_lookup[ j ] && dist < mindist )
507 p_rgb8[ i ] = p_rgb8[ j ];
513 /* Find the nearest color */
514 if( p_lookup[ j ] && dist + 128 < mindist )
516 p_rgb8[ i ] = p_rgb8[ j ];
517 mindist = dist + 128;