demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / video_chroma / i420_rgb.c
blobc476cbdf6457a1b9ec995394d50ec82127f71034
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
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 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 /*****************************************************************************
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>
38 #include <vlc_picture.h>
39 #include <vlc_cpu.h>
41 #include "i420_rgb.h"
42 #ifdef PLAIN
43 # include "i420_rgb_c.h"
44 static picture_t *I420_RGB8_Filter( filter_t *, picture_t * );
45 static picture_t *I420_RGB16_Filter( filter_t *, picture_t * );
46 static picture_t *I420_RGB32_Filter( filter_t *, picture_t * );
48 static void SetGammaTable( int *pi_table, double f_gamma );
49 static void SetYUV( filter_t * );
50 static void Set8bppPalette( filter_t *, uint8_t * );
51 #else
52 static picture_t *I420_R5G5B5_Filter( filter_t *, picture_t * );
53 static picture_t *I420_R5G6B5_Filter( filter_t *, picture_t * );
54 static picture_t *I420_A8R8G8B8_Filter( filter_t *, picture_t * );
55 static picture_t *I420_R8G8B8A8_Filter( filter_t *, picture_t * );
56 static picture_t *I420_B8G8R8A8_Filter( filter_t *, picture_t * );
57 static picture_t *I420_A8B8G8R8_Filter( filter_t *, picture_t * );
58 #endif
60 /*****************************************************************************
61 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
62 *****************************************************************************/
63 #define RGB2PIXEL( p_filter, i_r, i_g, i_b ) \
64 (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) \
65 << p_filter->fmt_out.video.i_lrshift) \
66 | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) \
67 << p_filter->fmt_out.video.i_lgshift) \
68 | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) \
69 << p_filter->fmt_out.video.i_lbshift))
71 /*****************************************************************************
72 * Module descriptor.
73 *****************************************************************************/
74 static int Activate ( vlc_object_t * );
75 static void Deactivate ( vlc_object_t * );
77 vlc_module_begin ()
78 #if defined (SSE2)
79 set_description( N_( "SSE2 I420,IYUV,YV12 to "
80 "RV15,RV16,RV24,RV32 conversions") )
81 set_capability( "video converter", 120 )
82 # define vlc_CPU_capable() vlc_CPU_SSE2()
83 #elif defined (MMX)
84 set_description( N_( "MMX I420,IYUV,YV12 to "
85 "RV15,RV16,RV24,RV32 conversions") )
86 set_capability( "video converter", 100 )
87 # define vlc_CPU_capable() vlc_CPU_MMX()
88 #else
89 set_description( N_("I420,IYUV,YV12 to "
90 "RGB2,RV15,RV16,RV24,RV32 conversions") )
91 set_capability( "video converter", 80 )
92 # define vlc_CPU_capable() (true)
93 #endif
94 set_callbacks( Activate, Deactivate )
95 vlc_module_end ()
97 /*****************************************************************************
98 * Activate: allocate a chroma function
99 *****************************************************************************
100 * This function allocates and initializes a chroma function
101 *****************************************************************************/
102 static int Activate( vlc_object_t *p_this )
104 filter_t *p_filter = (filter_t *)p_this;
105 #ifdef PLAIN
106 size_t i_tables_size;
107 #endif
109 if( !vlc_CPU_capable() )
110 return VLC_EGENERIC;
111 if( p_filter->fmt_out.video.i_width & 1
112 || p_filter->fmt_out.video.i_height & 1 )
114 return VLC_EGENERIC;
117 if( p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
119 return VLC_EGENERIC;
122 switch( p_filter->fmt_in.video.i_chroma )
124 case VLC_CODEC_YV12:
125 case VLC_CODEC_I420:
126 switch( p_filter->fmt_out.video.i_chroma )
128 #ifndef PLAIN
129 case VLC_CODEC_RGB15:
130 case VLC_CODEC_RGB16:
131 /* If we don't have support for the bitmasks, bail out */
132 if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
133 && p_filter->fmt_out.video.i_gmask == 0x03e0
134 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
136 /* R5G5B6 pixel format */
137 msg_Dbg(p_this, "RGB pixel format is R5G5B5");
138 p_filter->pf_video_filter = I420_R5G5B5_Filter;
140 else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
141 && p_filter->fmt_out.video.i_gmask == 0x07e0
142 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
144 /* R5G6B5 pixel format */
145 msg_Dbg(p_this, "RGB pixel format is R5G6B5");
146 p_filter->pf_video_filter = I420_R5G6B5_Filter;
148 else
149 return VLC_EGENERIC;
150 break;
151 case VLC_CODEC_RGB32:
152 /* If we don't have support for the bitmasks, bail out */
153 if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
154 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
155 && p_filter->fmt_out.video.i_bmask == 0x000000ff )
157 /* A8R8G8B8 pixel format */
158 msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
159 p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
161 else if( p_filter->fmt_out.video.i_rmask == 0xff000000
162 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
163 && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
165 /* R8G8B8A8 pixel format */
166 msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
167 p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
169 else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
170 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
171 && p_filter->fmt_out.video.i_bmask == 0xff000000 )
173 /* B8G8R8A8 pixel format */
174 msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
175 p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
177 else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
178 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
179 && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
181 /* A8B8G8R8 pixel format */
182 msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
183 p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
185 else
186 return VLC_EGENERIC;
187 break;
188 #else
189 case VLC_CODEC_RGB8:
190 p_filter->pf_video_filter = I420_RGB8_Filter;
191 break;
192 case VLC_CODEC_RGB15:
193 case VLC_CODEC_RGB16:
194 p_filter->pf_video_filter = I420_RGB16_Filter;
195 break;
196 case VLC_CODEC_RGB32:
197 p_filter->pf_video_filter = I420_RGB32_Filter;
198 break;
199 #endif
200 default:
201 return VLC_EGENERIC;
203 break;
205 default:
206 return VLC_EGENERIC;
209 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
210 if( p_filter->p_sys == NULL )
212 return VLC_EGENERIC;
215 switch( p_filter->fmt_out.video.i_chroma )
217 #ifdef PLAIN
218 case VLC_CODEC_RGB8:
219 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
220 break;
221 #endif
222 case VLC_CODEC_RGB15:
223 case VLC_CODEC_RGB16:
224 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
225 break;
226 case VLC_CODEC_RGB24:
227 case VLC_CODEC_RGB32:
228 p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
229 break;
230 default:
231 p_filter->p_sys->p_buffer = NULL;
232 break;
235 if( p_filter->p_sys->p_buffer == NULL )
237 free( p_filter->p_sys );
238 return VLC_EGENERIC;
241 p_filter->p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
242 * ( ( p_filter->fmt_out.video.i_chroma
243 == VLC_CODEC_RGB8 ) ? 2 : 1 )
244 * sizeof( int ) );
245 if( p_filter->p_sys->p_offset == NULL )
247 free( p_filter->p_sys->p_buffer );
248 free( p_filter->p_sys );
249 return VLC_EGENERIC;
252 #ifdef PLAIN
253 switch( p_filter->fmt_out.video.i_chroma )
255 case VLC_CODEC_RGB8:
256 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
257 break;
258 case VLC_CODEC_RGB15:
259 case VLC_CODEC_RGB16:
260 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
261 break;
262 default: /* RV24, RV32 */
263 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
264 break;
267 p_filter->p_sys->p_base = malloc( i_tables_size );
268 if( p_filter->p_sys->p_base == NULL )
270 free( p_filter->p_sys->p_offset );
271 free( p_filter->p_sys->p_buffer );
272 free( p_filter->p_sys );
273 return -1;
276 SetYUV( p_filter );
277 #endif
279 return 0;
282 /*****************************************************************************
283 * Deactivate: free the chroma function
284 *****************************************************************************
285 * This function frees the previously allocated chroma function
286 *****************************************************************************/
287 static void Deactivate( vlc_object_t *p_this )
289 filter_t *p_filter = (filter_t *)p_this;
291 #ifdef PLAIN
292 free( p_filter->p_sys->p_base );
293 #endif
294 free( p_filter->p_sys->p_offset );
295 free( p_filter->p_sys->p_buffer );
296 free( p_filter->p_sys );
299 #ifndef PLAIN
300 VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
301 VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
302 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
303 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
304 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
305 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
306 #else
307 VIDEO_FILTER_WRAPPER( I420_RGB8 )
308 VIDEO_FILTER_WRAPPER( I420_RGB16 )
309 VIDEO_FILTER_WRAPPER( I420_RGB32 )
311 /*****************************************************************************
312 * SetGammaTable: return intensity table transformed by gamma curve.
313 *****************************************************************************
314 * pi_table is a table of 256 entries from 0 to 255.
315 *****************************************************************************/
316 static void SetGammaTable( int *pi_table, double f_gamma )
318 int i_y; /* base intensity */
320 /* Use exp(gamma) instead of gamma */
321 f_gamma = exp( f_gamma );
323 /* Build gamma table */
324 for( i_y = 0; i_y < 256; i_y++ )
326 pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
330 /*****************************************************************************
331 * SetYUV: compute tables and set function pointers
332 *****************************************************************************/
333 static void SetYUV( filter_t *p_filter )
335 int pi_gamma[256]; /* gamma table */
336 volatile int i_index; /* index in tables */
337 /* We use volatile here to work around a strange gcc-3.3.4
338 * optimization bug */
340 /* Build gamma table */
341 SetGammaTable( pi_gamma, 0 ); //p_filter/*FIXME wasn't used anywhere anyway*/->f_gamma );
344 * Set pointers and build YUV tables
347 /* Color: build red, green and blue tables */
348 switch( p_filter->fmt_out.video.i_chroma )
350 case VLC_CODEC_RGB8:
351 p_filter->p_sys->p_rgb8 = (uint8_t *)p_filter->p_sys->p_base;
352 Set8bppPalette( p_filter, p_filter->p_sys->p_rgb8 );
353 break;
355 case VLC_CODEC_RGB15:
356 case VLC_CODEC_RGB16:
357 p_filter->p_sys->p_rgb16 = (uint16_t *)p_filter->p_sys->p_base;
358 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
360 p_filter->p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
361 p_filter->p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
363 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
365 p_filter->p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
366 p_filter->p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
368 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
370 p_filter->p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
371 p_filter->p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
373 for( i_index = 0; i_index < 256; i_index++ )
375 p_filter->p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
376 p_filter->p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
377 p_filter->p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
379 break;
381 case VLC_CODEC_RGB24:
382 case VLC_CODEC_RGB32:
383 p_filter->p_sys->p_rgb32 = (uint32_t *)p_filter->p_sys->p_base;
384 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
386 p_filter->p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
387 p_filter->p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
389 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
391 p_filter->p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
392 p_filter->p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
394 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
396 p_filter->p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
397 p_filter->p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
399 for( i_index = 0; i_index < 256; i_index++ )
401 p_filter->p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
402 p_filter->p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
403 p_filter->p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
405 break;
409 static void Set8bppPalette( filter_t *p_filter, uint8_t *p_rgb8 )
411 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
413 int y,u,v;
414 int r,g,b;
415 int i = 0, j = 0;
416 uint16_t *p_cmap_r = p_filter->p_sys->p_rgb_r;
417 uint16_t *p_cmap_g = p_filter->p_sys->p_rgb_g;
418 uint16_t *p_cmap_b = p_filter->p_sys->p_rgb_b;
420 unsigned char p_lookup[PALETTE_TABLE_SIZE];
422 /* This loop calculates the intersection of an YUV box and the RGB cube. */
423 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
425 for ( u = 0; u <= 256; u += 32 )
427 for ( v = 0; v <= 256; v += 32 )
429 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
430 g = y + ( (U_GREEN_COEF*(u-128)
431 + V_GREEN_COEF*(v-128)) >> SHIFT );
432 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
434 if( r >= 0x00 && g >= 0x00 && b >= 0x00
435 && r <= 0xff && g <= 0xff && b <= 0xff )
437 /* This one should never happen unless someone
438 * fscked up my code */
439 if( j == 256 )
441 msg_Err( p_filter, "no colors left in palette" );
442 break;
445 /* Clip the colors */
446 p_cmap_r[ j ] = CLIP( r );
447 p_cmap_g[ j ] = CLIP( g );
448 p_cmap_b[ j ] = CLIP( b );
450 #if 0
451 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
452 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
453 p_cmap_b[ j ] >>8);
454 #endif
456 /* Allocate color */
457 p_lookup[ i ] = 1;
458 p_rgb8[ i++ ] = j;
459 j++;
461 else
463 p_lookup[ i ] = 0;
464 p_rgb8[ i++ ] = 0;
470 /* The colors have been allocated, we can set the palette */
471 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
472 p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
474 #if 0
475 /* There will eventually be a way to know which colors
476 * couldn't be allocated and try to find a replacement */
477 p_vout->i_white_pixel = 0xff;
478 p_vout->i_black_pixel = 0x00;
479 p_vout->i_gray_pixel = 0x44;
480 p_vout->i_blue_pixel = 0x3b;
481 #endif
483 /* This loop allocates colors that got outside the RGB cube */
484 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
486 for ( u = 0; u <= 256; u += 32 )
488 for ( v = 0; v <= 256; v += 32, i++ )
490 int u2, v2, dist, mindist = 100000000;
492 if( p_lookup[ i ] || y == 0 )
494 continue;
497 /* Heavy. yeah. */
498 for( u2 = 0; u2 <= 256; u2 += 32 )
500 for( v2 = 0; v2 <= 256; v2 += 32 )
502 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
503 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
505 /* Find the nearest color */
506 if( p_lookup[ j ] && dist < mindist )
508 p_rgb8[ i ] = p_rgb8[ j ];
509 mindist = dist;
512 j -= 128;
514 /* Find the nearest color */
515 if( p_lookup[ j ] && dist + 128 < mindist )
517 p_rgb8[ i ] = p_rgb8[ j ];
518 mindist = dist + 128;
526 #endif