add_savefile: remove callback parameter
[vlc/asuraparaju-public.git] / modules / video_chroma / i420_rgb.c
blobcfaeb734a5679c1d6f0d73cf46406ef2f7004415
1 /*****************************************************************************
2 * i420_rgb.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2004, 2008 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <math.h> /* exp(), pow() */
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
39 #include "i420_rgb.h"
40 #if defined (MODULE_NAME_IS_i420_rgb)
41 # include "i420_rgb_c.h"
42 static picture_t *I420_RGB8_Filter ( filter_t *, picture_t * );
43 // static picture_t *I420_RGB16_dither_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 * );
46 #else
47 static picture_t *I420_R5G5B5_Filter ( filter_t *, picture_t * );
48 static picture_t *I420_R5G6B5_Filter ( filter_t *, picture_t * );
49 static picture_t *I420_A8R8G8B8_Filter ( filter_t *, picture_t * );
50 static picture_t *I420_R8G8B8A8_Filter ( filter_t *, picture_t * );
51 static picture_t *I420_B8G8R8A8_Filter ( filter_t *, picture_t * );
52 static picture_t *I420_A8B8G8R8_Filter ( filter_t *, picture_t * );
53 #endif
55 /*****************************************************************************
56 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
57 *****************************************************************************/
58 #define RGB2PIXEL( p_filter, i_r, i_g, i_b ) \
59 (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) \
60 << p_filter->fmt_out.video.i_lrshift) \
61 | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) \
62 << p_filter->fmt_out.video.i_lgshift) \
63 | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) \
64 << p_filter->fmt_out.video.i_lbshift))
66 /*****************************************************************************
67 * Local and extern prototypes.
68 *****************************************************************************/
69 static int Activate ( vlc_object_t * );
70 static void Deactivate ( vlc_object_t * );
72 #if defined (MODULE_NAME_IS_i420_rgb)
73 static void SetGammaTable ( int *pi_table, double f_gamma );
74 static void SetYUV ( filter_t * );
75 static void Set8bppPalette ( filter_t *, uint8_t * );
76 #endif
78 /*****************************************************************************
79 * Module descriptor.
80 *****************************************************************************/
81 vlc_module_begin ()
82 #if defined (MODULE_NAME_IS_i420_rgb)
83 set_description( N_("I420,IYUV,YV12 to "
84 "RGB2,RV15,RV16,RV24,RV32 conversions") )
85 set_capability( "video filter2", 80 )
86 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
87 set_description( N_( "MMX I420,IYUV,YV12 to "
88 "RV15,RV16,RV24,RV32 conversions") )
89 set_capability( "video filter2", 100 )
90 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
91 set_description( N_( "SSE2 I420,IYUV,YV12 to "
92 "RV15,RV16,RV24,RV32 conversions") )
93 set_capability( "video filter2", 120 )
94 #endif
95 set_callbacks( Activate, Deactivate )
96 vlc_module_end ()
98 /*****************************************************************************
99 * Activate: allocate a chroma function
100 *****************************************************************************
101 * This function allocates and initializes a chroma function
102 *****************************************************************************/
103 static int Activate( vlc_object_t *p_this )
105 filter_t *p_filter = (filter_t *)p_this;
106 #if defined (MODULE_NAME_IS_i420_rgb)
107 size_t i_tables_size;
108 #endif
110 if( p_filter->fmt_out.video.i_width & 1
111 || p_filter->fmt_out.video.i_height & 1 )
113 return VLC_EGENERIC;
116 switch( p_filter->fmt_in.video.i_chroma )
118 case VLC_CODEC_YV12:
119 case VLC_CODEC_I420:
120 switch( p_filter->fmt_out.video.i_chroma )
122 #if defined (MODULE_NAME_IS_i420_rgb)
123 case VLC_CODEC_RGB8:
124 p_filter->pf_video_filter = I420_RGB8_Filter;
125 break;
126 #endif
127 case VLC_CODEC_RGB15:
128 case VLC_CODEC_RGB16:
129 #if ! defined (MODULE_NAME_IS_i420_rgb)
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;
147 else
148 return VLC_EGENERIC;
149 #else
150 // generic C chroma converter */
151 p_filter->pf_video_filter = I420_RGB16_Filter;
152 #endif
153 break;
155 #if 0
156 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
157 case VLC_CODEC_RGB24:
158 #endif
160 case VLC_CODEC_RGB32:
161 #if ! defined (MODULE_NAME_IS_i420_rgb)
162 /* If we don't have support for the bitmasks, bail out */
163 if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
164 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
165 && p_filter->fmt_out.video.i_bmask == 0x000000ff )
167 /* A8R8G8B8 pixel format */
168 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
169 p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
171 else if( p_filter->fmt_out.video.i_rmask == 0xff000000
172 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
173 && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
175 /* R8G8B8A8 pixel format */
176 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
177 p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
179 else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
180 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
181 && p_filter->fmt_out.video.i_bmask == 0xff000000 )
183 /* B8G8R8A8 pixel format */
184 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
185 p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
187 else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
188 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
189 && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
191 /* A8B8G8R8 pixel format */
192 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
193 p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
195 else
196 return VLC_EGENERIC;
197 #else
198 /* generic C chroma converter */
199 p_filter->pf_video_filter = I420_RGB32_Filter;
200 #endif
201 break;
203 default:
204 return VLC_EGENERIC;
206 break;
208 default:
209 return VLC_EGENERIC;
212 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
213 if( p_filter->p_sys == NULL )
215 return VLC_EGENERIC;
218 switch( p_filter->fmt_out.video.i_chroma )
220 #if defined (MODULE_NAME_IS_i420_rgb)
221 case VLC_CODEC_RGB8:
222 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
223 break;
224 #endif
226 case VLC_CODEC_RGB15:
227 case VLC_CODEC_RGB16:
228 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
229 break;
231 case VLC_CODEC_RGB24:
232 case VLC_CODEC_RGB32:
233 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
234 break;
236 default:
237 p_filter->p_sys->p_buffer = NULL;
238 break;
241 if( p_filter->p_sys->p_buffer == NULL )
243 free( p_filter->p_sys );
244 return VLC_EGENERIC;
247 p_filter->p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
248 * ( ( p_filter->fmt_out.video.i_chroma
249 == VLC_CODEC_RGB8 ) ? 2 : 1 )
250 * sizeof( int ) );
251 if( p_filter->p_sys->p_offset == NULL )
253 free( p_filter->p_sys->p_buffer );
254 free( p_filter->p_sys );
255 return VLC_EGENERIC;
258 #if defined (MODULE_NAME_IS_i420_rgb)
259 switch( p_filter->fmt_out.video.i_chroma )
261 case VLC_CODEC_RGB8:
262 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
263 break;
264 case VLC_CODEC_RGB15:
265 case VLC_CODEC_RGB16:
266 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
267 break;
268 default: /* RV24, RV32 */
269 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
270 break;
273 p_filter->p_sys->p_base = malloc( i_tables_size );
274 if( p_filter->p_sys->p_base == NULL )
276 free( p_filter->p_sys->p_offset );
277 free( p_filter->p_sys->p_buffer );
278 free( p_filter->p_sys );
279 return -1;
282 SetYUV( p_filter );
283 #endif
285 return 0;
288 /*****************************************************************************
289 * Deactivate: free the chroma function
290 *****************************************************************************
291 * This function frees the previously allocated chroma function
292 *****************************************************************************/
293 static void Deactivate( vlc_object_t *p_this )
295 filter_t *p_filter = (filter_t *)p_this;
297 #if defined (MODULE_NAME_IS_i420_rgb)
298 free( p_filter->p_sys->p_base );
299 #endif
300 free( p_filter->p_sys->p_offset );
301 free( p_filter->p_sys->p_buffer );
302 free( p_filter->p_sys );
305 #if defined (MODULE_NAME_IS_i420_rgb)
306 VIDEO_FILTER_WRAPPER( I420_RGB8 )
307 VIDEO_FILTER_WRAPPER( I420_RGB16 )
308 //VIDEO_FILTER_WRAPPER( I420_RGB16_dither )
309 VIDEO_FILTER_WRAPPER( I420_RGB32 )
310 #else
311 VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
312 VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
313 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
314 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
315 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
316 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
317 #endif
319 #if defined (MODULE_NAME_IS_i420_rgb)
320 /*****************************************************************************
321 * SetGammaTable: return intensity table transformed by gamma curve.
322 *****************************************************************************
323 * pi_table is a table of 256 entries from 0 to 255.
324 *****************************************************************************/
325 static void SetGammaTable( int *pi_table, double f_gamma )
327 int i_y; /* base intensity */
329 /* Use exp(gamma) instead of gamma */
330 f_gamma = exp( f_gamma );
332 /* Build gamma table */
333 for( i_y = 0; i_y < 256; i_y++ )
335 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
339 /*****************************************************************************
340 * SetYUV: compute tables and set function pointers
341 *****************************************************************************/
342 static void SetYUV( filter_t *p_filter )
344 int pi_gamma[256]; /* gamma table */
345 volatile int i_index; /* index in tables */
346 /* We use volatile here to work around a strange gcc-3.3.4
347 * optimization bug */
349 /* Build gamma table */
350 SetGammaTable( pi_gamma, 0 ); //p_filter/*FIXME wasn't used anywhere anyway*/->f_gamma );
353 * Set pointers and build YUV tables
356 /* Color: build red, green and blue tables */
357 switch( p_filter->fmt_out.video.i_chroma )
359 case VLC_CODEC_RGB8:
360 p_filter->p_sys->p_rgb8 = (uint8_t *)p_filter->p_sys->p_base;
361 Set8bppPalette( p_filter, p_filter->p_sys->p_rgb8 );
362 break;
364 case VLC_CODEC_RGB15:
365 case VLC_CODEC_RGB16:
366 p_filter->p_sys->p_rgb16 = (uint16_t *)p_filter->p_sys->p_base;
367 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
369 p_filter->p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
370 p_filter->p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
372 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
374 p_filter->p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
375 p_filter->p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
377 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
379 p_filter->p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
380 p_filter->p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
382 for( i_index = 0; i_index < 256; i_index++ )
384 p_filter->p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
385 p_filter->p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
386 p_filter->p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
388 break;
390 case VLC_CODEC_RGB24:
391 case VLC_CODEC_RGB32:
392 p_filter->p_sys->p_rgb32 = (uint32_t *)p_filter->p_sys->p_base;
393 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
395 p_filter->p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
396 p_filter->p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
398 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
400 p_filter->p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
401 p_filter->p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
403 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
405 p_filter->p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
406 p_filter->p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
408 for( i_index = 0; i_index < 256; i_index++ )
410 p_filter->p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
411 p_filter->p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
412 p_filter->p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
414 break;
418 static void Set8bppPalette( filter_t *p_filter, uint8_t *p_rgb8 )
420 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
422 int y,u,v;
423 int r,g,b;
424 int i = 0, j = 0;
425 uint16_t *p_cmap_r = p_filter->p_sys->p_rgb_r;
426 uint16_t *p_cmap_g = p_filter->p_sys->p_rgb_g;
427 uint16_t *p_cmap_b = p_filter->p_sys->p_rgb_b;
429 unsigned char p_lookup[PALETTE_TABLE_SIZE];
431 /* This loop calculates the intersection of an YUV box and the RGB cube. */
432 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
434 for ( u = 0; u <= 256; u += 32 )
436 for ( v = 0; v <= 256; v += 32 )
438 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
439 g = y + ( (U_GREEN_COEF*(u-128)
440 + V_GREEN_COEF*(v-128)) >> SHIFT );
441 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
443 if( r >= 0x00 && g >= 0x00 && b >= 0x00
444 && r <= 0xff && g <= 0xff && b <= 0xff )
446 /* This one should never happen unless someone
447 * fscked up my code */
448 if( j == 256 )
450 msg_Err( p_filter, "no colors left in palette" );
451 break;
454 /* Clip the colors */
455 p_cmap_r[ j ] = CLIP( r );
456 p_cmap_g[ j ] = CLIP( g );
457 p_cmap_b[ j ] = CLIP( b );
459 #if 0
460 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
461 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
462 p_cmap_b[ j ] >>8);
463 #endif
465 /* Allocate color */
466 p_lookup[ i ] = 1;
467 p_rgb8[ i++ ] = j;
468 j++;
470 else
472 p_lookup[ i ] = 0;
473 p_rgb8[ i++ ] = 0;
479 /* The colors have been allocated, we can set the palette */
480 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
481 p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
483 #if 0
484 /* There will eventually be a way to know which colors
485 * couldn't be allocated and try to find a replacement */
486 p_vout->i_white_pixel = 0xff;
487 p_vout->i_black_pixel = 0x00;
488 p_vout->i_gray_pixel = 0x44;
489 p_vout->i_blue_pixel = 0x3b;
490 #endif
492 /* This loop allocates colors that got outside the RGB cube */
493 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
495 for ( u = 0; u <= 256; u += 32 )
497 for ( v = 0; v <= 256; v += 32, i++ )
499 int u2, v2, dist, mindist = 100000000;
501 if( p_lookup[ i ] || y == 0 )
503 continue;
506 /* Heavy. yeah. */
507 for( u2 = 0; u2 <= 256; u2 += 32 )
509 for( v2 = 0; v2 <= 256; v2 += 32 )
511 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
512 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
514 /* Find the nearest color */
515 if( p_lookup[ j ] && dist < mindist )
517 p_rgb8[ i ] = p_rgb8[ j ];
518 mindist = dist;
521 j -= 128;
523 /* Find the nearest color */
524 if( p_lookup[ j ] && dist + 128 < mindist )
526 p_rgb8[ i ] = p_rgb8[ j ];
527 mindist = dist + 128;
536 #endif