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>
41 #if defined (MODULE_NAME_IS_i420_rgb)
42 # include "i420_rgb_c.h"
45 /*****************************************************************************
46 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
47 *****************************************************************************/
48 #define RGB2PIXEL( p_filter, i_r, i_g, i_b ) \
49 (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) \
50 << p_filter->fmt_out.video.i_lrshift) \
51 | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) \
52 << p_filter->fmt_out.video.i_lgshift) \
53 | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) \
54 << p_filter->fmt_out.video.i_lbshift))
56 /*****************************************************************************
57 * Local and extern prototypes.
58 *****************************************************************************/
59 static int Activate ( vlc_object_t * );
60 static void Deactivate ( vlc_object_t * );
62 #if defined (MODULE_NAME_IS_i420_rgb)
63 static void SetGammaTable ( int *pi_table, double f_gamma );
64 static void SetYUV ( filter_t * );
65 static void Set8bppPalette ( filter_t *, uint8_t * );
68 /*****************************************************************************
70 *****************************************************************************/
72 #if defined (MODULE_NAME_IS_i420_rgb)
73 set_description( N_("I420,IYUV,YV12 to "
74 "RGB2,RV15,RV16,RV24,RV32 conversions") );
75 set_capability( "video filter2", 80 );
76 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
77 set_description( N_( "MMX I420,IYUV,YV12 to "
78 "RV15,RV16,RV24,RV32 conversions") );
79 set_capability( "video filter2", 100 );
80 add_requirement( MMX );
81 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
82 set_description( N_( "SSE2 I420,IYUV,YV12 to "
83 "RV15,RV16,RV24,RV32 conversions") );
84 set_capability( "video filter2", 120 );
85 add_requirement( SSE2 );
87 set_callbacks( Activate, Deactivate );
90 /*****************************************************************************
91 * Activate: allocate a chroma function
92 *****************************************************************************
93 * This function allocates and initializes a chroma function
94 *****************************************************************************/
95 static int Activate( vlc_object_t *p_this )
97 filter_t *p_filter = (filter_t *)p_this;
98 #if defined (MODULE_NAME_IS_i420_rgb)
102 if( p_filter->fmt_out.video.i_width & 1
103 || p_filter->fmt_out.video.i_height & 1 )
108 switch( p_filter->fmt_in.video.i_chroma )
110 case VLC_FOURCC('Y','V','1','2'):
111 case VLC_FOURCC('I','4','2','0'):
112 case VLC_FOURCC('I','Y','U','V'):
113 switch( p_filter->fmt_out.video.i_chroma )
115 #if defined (MODULE_NAME_IS_i420_rgb)
116 case VLC_FOURCC('R','G','B','2'):
117 p_filter->pf_video_filter = I420_RGB8_Filter;
120 case VLC_FOURCC('R','V','1','5'):
121 case VLC_FOURCC('R','V','1','6'):
122 #if ! defined (MODULE_NAME_IS_i420_rgb)
123 /* If we don't have support for the bitmasks, bail out */
124 if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
125 && p_filter->fmt_out.video.i_gmask == 0x03e0
126 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
128 /* R5G5B6 pixel format */
129 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
130 p_filter->pf_video_filter = I420_R5G5B5_Filter;
132 else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
133 && p_filter->fmt_out.video.i_gmask == 0x07e0
134 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
136 /* R5G6B5 pixel format */
137 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
138 p_filter->pf_video_filter = I420_R5G6B5_Filter;
143 // generic C chroma converter */
144 p_filter->pf_video_filter = I420_RGB16_Filter;
149 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
150 case VLC_FOURCC('R','V','2','4'):
153 case VLC_FOURCC('R','V','3','2'):
154 #if ! defined (MODULE_NAME_IS_i420_rgb)
155 /* If we don't have support for the bitmasks, bail out */
156 if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
157 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
158 && p_filter->fmt_out.video.i_bmask == 0x000000ff )
160 /* A8R8G8B8 pixel format */
161 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
162 p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
164 else if( p_filter->fmt_out.video.i_rmask == 0xff000000
165 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
166 && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
168 /* R8G8B8A8 pixel format */
169 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
170 p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
172 else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
173 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
174 && p_filter->fmt_out.video.i_bmask == 0xff000000 )
176 /* B8G8R8A8 pixel format */
177 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
178 p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
180 else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
181 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
182 && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
184 /* A8B8G8R8 pixel format */
185 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
186 p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
191 /* generic C chroma converter */
192 p_filter->pf_video_filter = I420_RGB32_Filter;
205 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
206 if( p_filter->p_sys == NULL )
211 switch( p_filter->fmt_out.video.i_chroma )
213 #if defined (MODULE_NAME_IS_i420_rgb)
214 case VLC_FOURCC('R','G','B','2'):
215 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
219 case VLC_FOURCC('R','V','1','5'):
220 case VLC_FOURCC('R','V','1','6'):
221 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
224 case VLC_FOURCC('R','V','2','4'):
225 case VLC_FOURCC('R','V','3','2'):
226 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
230 p_filter->p_sys->p_buffer = NULL;
234 if( p_filter->p_sys->p_buffer == NULL )
236 free( p_filter->p_sys );
240 p_filter->p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
241 * ( ( p_filter->fmt_out.video.i_chroma
242 == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
244 if( p_filter->p_sys->p_offset == NULL )
246 free( p_filter->p_sys->p_buffer );
247 free( p_filter->p_sys );
251 #if defined (MODULE_NAME_IS_i420_rgb)
252 switch( p_filter->fmt_out.video.i_chroma )
254 case VLC_FOURCC('R','G','B','2'):
255 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
257 case VLC_FOURCC('R','V','1','5'):
258 case VLC_FOURCC('R','V','1','6'):
259 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
261 default: /* RV24, RV32 */
262 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
266 p_filter->p_sys->p_base = malloc( i_tables_size );
267 if( p_filter->p_sys->p_base == NULL )
269 free( p_filter->p_sys->p_offset );
270 free( p_filter->p_sys->p_buffer );
271 free( p_filter->p_sys );
281 /*****************************************************************************
282 * Deactivate: free the chroma function
283 *****************************************************************************
284 * This function frees the previously allocated chroma function
285 *****************************************************************************/
286 static void Deactivate( vlc_object_t *p_this )
288 filter_t *p_filter = (filter_t *)p_this;
290 #if defined (MODULE_NAME_IS_i420_rgb)
291 free( p_filter->p_sys->p_base );
293 free( p_filter->p_sys->p_offset );
294 free( p_filter->p_sys->p_buffer );
295 free( p_filter->p_sys );
298 #if defined (MODULE_NAME_IS_i420_rgb)
299 VIDEO_FILTER_WRAPPER( I420_RGB8 )
300 VIDEO_FILTER_WRAPPER( I420_RGB16 )
301 VIDEO_FILTER_WRAPPER( I420_RGB32 )
303 VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
304 VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
305 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
306 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
307 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
308 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
311 #if defined (MODULE_NAME_IS_i420_rgb)
312 /*****************************************************************************
313 * SetGammaTable: return intensity table transformed by gamma curve.
314 *****************************************************************************
315 * pi_table is a table of 256 entries from 0 to 255.
316 *****************************************************************************/
317 static void SetGammaTable( int *pi_table, double f_gamma )
319 int i_y; /* base intensity */
321 /* Use exp(gamma) instead of gamma */
322 f_gamma = exp( f_gamma );
324 /* Build gamma table */
325 for( i_y = 0; i_y < 256; i_y++ )
327 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
331 /*****************************************************************************
332 * SetYUV: compute tables and set function pointers
333 *****************************************************************************/
334 static void SetYUV( filter_t *p_filter )
336 int pi_gamma[256]; /* gamma table */
337 volatile int i_index; /* index in tables */
338 /* We use volatile here to work around a strange gcc-3.3.4
339 * optimization bug */
341 /* Build gamma table */
342 SetGammaTable( pi_gamma, 0 ); //p_filter/*FIXME wasn't used anywhere anyway*/->f_gamma );
345 * Set pointers and build YUV tables
348 /* Color: build red, green and blue tables */
349 switch( p_filter->fmt_out.video.i_chroma )
351 case VLC_FOURCC('R','G','B','2'):
352 p_filter->p_sys->p_rgb8 = (uint8_t *)p_filter->p_sys->p_base;
353 Set8bppPalette( p_filter, p_filter->p_sys->p_rgb8 );
356 case VLC_FOURCC('R','V','1','5'):
357 case VLC_FOURCC('R','V','1','6'):
358 p_filter->p_sys->p_rgb16 = (uint16_t *)p_filter->p_sys->p_base;
359 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
361 p_filter->p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
362 p_filter->p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
364 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
366 p_filter->p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
367 p_filter->p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
369 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
371 p_filter->p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
372 p_filter->p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
374 for( i_index = 0; i_index < 256; i_index++ )
376 p_filter->p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
377 p_filter->p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
378 p_filter->p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
382 case VLC_FOURCC('R','V','2','4'):
383 case VLC_FOURCC('R','V','3','2'):
384 p_filter->p_sys->p_rgb32 = (uint32_t *)p_filter->p_sys->p_base;
385 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
387 p_filter->p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
388 p_filter->p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
390 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
392 p_filter->p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
393 p_filter->p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
395 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
397 p_filter->p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
398 p_filter->p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
400 for( i_index = 0; i_index < 256; i_index++ )
402 p_filter->p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
403 p_filter->p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
404 p_filter->p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
410 static void Set8bppPalette( filter_t *p_filter, uint8_t *p_rgb8 )
412 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
417 uint16_t *p_cmap_r = p_filter->p_sys->p_rgb_r;
418 uint16_t *p_cmap_g = p_filter->p_sys->p_rgb_g;
419 uint16_t *p_cmap_b = p_filter->p_sys->p_rgb_b;
421 unsigned char p_lookup[PALETTE_TABLE_SIZE];
423 /* This loop calculates the intersection of an YUV box and the RGB cube. */
424 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
426 for ( u = 0; u <= 256; u += 32 )
428 for ( v = 0; v <= 256; v += 32 )
430 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
431 g = y + ( (U_GREEN_COEF*(u-128)
432 + V_GREEN_COEF*(v-128)) >> SHIFT );
433 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
435 if( r >= 0x00 && g >= 0x00 && b >= 0x00
436 && r <= 0xff && g <= 0xff && b <= 0xff )
438 /* This one should never happen unless someone
439 * fscked up my code */
442 msg_Err( p_filter, "no colors left in palette" );
446 /* Clip the colors */
447 p_cmap_r[ j ] = CLIP( r );
448 p_cmap_g[ j ] = CLIP( g );
449 p_cmap_b[ j ] = CLIP( b );
452 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
453 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
471 /* The colors have been allocated, we can set the palette */
472 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
473 p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
476 /* There will eventually be a way to know which colors
477 * couldn't be allocated and try to find a replacement */
478 p_vout->i_white_pixel = 0xff;
479 p_vout->i_black_pixel = 0x00;
480 p_vout->i_gray_pixel = 0x44;
481 p_vout->i_blue_pixel = 0x3b;
484 /* This loop allocates colors that got outside the RGB cube */
485 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
487 for ( u = 0; u <= 256; u += 32 )
489 for ( v = 0; v <= 256; v += 32, i++ )
491 int u2, v2, dist, mindist = 100000000;
493 if( p_lookup[ i ] || y == 0 )
499 for( u2 = 0; u2 <= 256; u2 += 32 )
501 for( v2 = 0; v2 <= 256; v2 += 32 )
503 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
504 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
506 /* Find the nearest color */
507 if( p_lookup[ j ] && dist < mindist )
509 p_rgb8[ i ] = p_rgb8[ j ];
515 /* Find the nearest color */
516 if( p_lookup[ j ] && dist + 128 < mindist )
518 p_rgb8[ i ] = p_rgb8[ j ];
519 mindist = dist + 128;