1 /*****************************************************************************
2 * i420_rgb.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2004, 2008 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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>
42 # include "i420_rgb_c.h"
43 static picture_t *I420_RGB8_Filter( filter_t *, picture_t * );
44 static picture_t *I420_RGB16_Filter( filter_t *, picture_t * );
45 static picture_t *I420_RGB32_Filter( filter_t *, picture_t * );
47 static void SetGammaTable( int *pi_table, double f_gamma );
48 static void SetYUV( filter_t * );
49 static void Set8bppPalette( filter_t *, uint8_t * );
51 static picture_t *I420_R5G5B5_Filter( filter_t *, picture_t * );
52 static picture_t *I420_R5G6B5_Filter( filter_t *, picture_t * );
53 static picture_t *I420_A8R8G8B8_Filter( filter_t *, picture_t * );
54 static picture_t *I420_R8G8B8A8_Filter( filter_t *, picture_t * );
55 static picture_t *I420_B8G8R8A8_Filter( filter_t *, picture_t * );
56 static picture_t *I420_A8B8G8R8_Filter( filter_t *, picture_t * );
59 /*****************************************************************************
60 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
61 *****************************************************************************/
62 #define RGB2PIXEL( p_filter, i_r, i_g, i_b ) \
63 (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) \
64 << p_filter->fmt_out.video.i_lrshift) \
65 | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) \
66 << p_filter->fmt_out.video.i_lgshift) \
67 | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) \
68 << p_filter->fmt_out.video.i_lbshift))
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Activate ( vlc_object_t * );
74 static void Deactivate ( vlc_object_t * );
78 set_description( N_( "SSE2 I420,IYUV,YV12 to "
79 "RV15,RV16,RV24,RV32 conversions") )
80 set_capability( "video filter2", 120 )
81 # define vlc_CPU_capable() vlc_CPU_SSE2()
83 set_description( N_( "MMX I420,IYUV,YV12 to "
84 "RV15,RV16,RV24,RV32 conversions") )
85 set_capability( "video filter2", 100 )
86 # define vlc_CPU_capable() vlc_CPU_MMX()
88 set_description( N_("I420,IYUV,YV12 to "
89 "RGB2,RV15,RV16,RV24,RV32 conversions") )
90 set_capability( "video filter2", 80 )
91 # define vlc_CPU_capable() (true)
93 set_callbacks( Activate, Deactivate )
96 /*****************************************************************************
97 * Activate: allocate a chroma function
98 *****************************************************************************
99 * This function allocates and initializes a chroma function
100 *****************************************************************************/
101 static int Activate( vlc_object_t *p_this )
103 filter_t *p_filter = (filter_t *)p_this;
105 size_t i_tables_size;
108 if( !vlc_CPU_capable() )
110 if( p_filter->fmt_out.video.i_width & 1
111 || p_filter->fmt_out.video.i_height & 1 )
116 if( p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
121 switch( p_filter->fmt_in.video.i_chroma )
125 switch( p_filter->fmt_out.video.i_chroma )
128 case VLC_CODEC_RGB15:
129 case VLC_CODEC_RGB16:
130 /* If we don't have support for the bitmasks, bail out */
131 if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
132 && p_filter->fmt_out.video.i_gmask == 0x03e0
133 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
135 /* R5G5B6 pixel format */
136 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
137 p_filter->pf_video_filter = I420_R5G5B5_Filter;
139 else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
140 && p_filter->fmt_out.video.i_gmask == 0x07e0
141 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
143 /* R5G6B5 pixel format */
144 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
145 p_filter->pf_video_filter = I420_R5G6B5_Filter;
150 case VLC_CODEC_RGB32:
151 /* If we don't have support for the bitmasks, bail out */
152 if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
153 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
154 && p_filter->fmt_out.video.i_bmask == 0x000000ff )
156 /* A8R8G8B8 pixel format */
157 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
158 p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
160 else if( p_filter->fmt_out.video.i_rmask == 0xff000000
161 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
162 && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
164 /* R8G8B8A8 pixel format */
165 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
166 p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
168 else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
169 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
170 && p_filter->fmt_out.video.i_bmask == 0xff000000 )
172 /* B8G8R8A8 pixel format */
173 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
174 p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
176 else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
177 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
178 && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
180 /* A8B8G8R8 pixel format */
181 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
182 p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
189 p_filter->pf_video_filter = I420_RGB8_Filter;
191 case VLC_CODEC_RGB15:
192 case VLC_CODEC_RGB16:
193 p_filter->pf_video_filter = I420_RGB16_Filter;
195 case VLC_CODEC_RGB32:
196 p_filter->pf_video_filter = I420_RGB32_Filter;
208 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
209 if( p_filter->p_sys == NULL )
214 switch( p_filter->fmt_out.video.i_chroma )
218 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
221 case VLC_CODEC_RGB15:
222 case VLC_CODEC_RGB16:
223 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
225 case VLC_CODEC_RGB24:
226 case VLC_CODEC_RGB32:
227 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_CODEC_RGB8 ) ? 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 );
252 switch( p_filter->fmt_out.video.i_chroma )
255 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
257 case VLC_CODEC_RGB15:
258 case VLC_CODEC_RGB16:
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;
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 );
299 VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
300 VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
301 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
302 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
303 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
304 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
306 VIDEO_FILTER_WRAPPER( I420_RGB8 )
307 VIDEO_FILTER_WRAPPER( I420_RGB16 )
308 VIDEO_FILTER_WRAPPER( I420_RGB32 )
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;