qt: playlist: use item title if available
[vlc.git] / modules / video_chroma / i420_rgb.c
blobb0cc8e8d239705c56429b380c9c1fd3a7d775a63
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
6 * Authors: Sam Hocevar <sam@zoy.org>
7 * Damien Fouilleul <damienf@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_picture.h>
36 #include <vlc_cpu.h>
38 #include "i420_rgb.h"
39 #ifdef PLAIN
40 # include "i420_rgb_c.h"
42 static void SetYUV( filter_t *, const video_format_t * );
43 static void Set8bppPalette( filter_t *, uint8_t * );
44 #endif
46 /*****************************************************************************
47 * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
48 *****************************************************************************/
49 #define RGB2PIXEL( p_filter, i_r, i_g, i_b ) \
50 ((((i_r) >> i_rrshift) << i_lrshift) \
51 | (((i_g) >> i_rgshift) << i_lgshift) \
52 | (((i_b) >> i_rbshift) << i_lbshift))
54 /*****************************************************************************
55 * Module descriptor.
56 *****************************************************************************/
57 static int Activate ( filter_t * );
58 static void Deactivate ( filter_t * );
60 vlc_module_begin ()
61 #if defined (SSE2)
62 set_description( N_( "SSE2 I420,IYUV,YV12 to "
63 "RV15,RV16,RV24,RV32 conversions") )
64 set_callback_video_converter( Activate, 120 )
65 # define vlc_CPU_capable() vlc_CPU_SSE2()
66 #elif defined (MMX)
67 set_description( N_( "MMX I420,IYUV,YV12 to "
68 "RV15,RV16,RV24,RV32 conversions") )
69 set_callback_video_converter( Activate, 100 )
70 # define vlc_CPU_capable() vlc_CPU_MMX()
71 #else
72 set_description( N_("I420,IYUV,YV12 to "
73 "RGB8,RV15,RV16,RV24,RV32 conversions") )
74 set_callback_video_converter( Activate, 80 )
75 # define vlc_CPU_capable() (true)
76 #endif
77 vlc_module_end ()
79 #ifndef PLAIN
80 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_R5G5B5, Deactivate )
81 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_R5G6B5, Deactivate )
82 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_A8R8G8B8, Deactivate )
83 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_R8G8B8A8, Deactivate )
84 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_B8G8R8A8, Deactivate )
85 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_A8B8G8R8, Deactivate )
86 #else
87 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_RGB8, Deactivate )
88 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_RGB16, Deactivate )
89 VIDEO_FILTER_WRAPPER_CLOSE_EXT( I420_RGB32, Deactivate )
90 #endif
92 /*****************************************************************************
93 * Activate: allocate a chroma function
94 *****************************************************************************
95 * This function allocates and initializes a chroma function
96 *****************************************************************************/
97 static int Activate( filter_t *p_filter )
99 #ifdef PLAIN
100 size_t i_tables_size;
101 #endif
103 if( !vlc_CPU_capable() )
104 return VLC_EGENERIC;
105 if( p_filter->fmt_out.video.i_width & 1
106 || p_filter->fmt_out.video.i_height & 1 )
108 return VLC_EGENERIC;
111 if( p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
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 #ifndef PLAIN
123 case VLC_CODEC_RGB15:
124 case VLC_CODEC_RGB16:
125 /* If we don't have support for the bitmasks, bail out */
126 if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
127 && p_filter->fmt_out.video.i_gmask == 0x03e0
128 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
130 /* R5G5B6 pixel format */
131 msg_Dbg(p_filter, "RGB pixel format is R5G5B5");
132 p_filter->ops = &I420_R5G5B5_ops;
134 else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
135 && p_filter->fmt_out.video.i_gmask == 0x07e0
136 && p_filter->fmt_out.video.i_bmask == 0x001f ) )
138 /* R5G6B5 pixel format */
139 msg_Dbg(p_filter, "RGB pixel format is R5G6B5");
140 p_filter->ops = &I420_R5G6B5_ops;
142 else
143 return VLC_EGENERIC;
144 break;
145 case VLC_CODEC_RGB32:
146 /* If we don't have support for the bitmasks, bail out */
147 if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
148 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
149 && p_filter->fmt_out.video.i_bmask == 0x000000ff )
151 /* A8R8G8B8 pixel format */
152 msg_Dbg(p_filter, "RGB pixel format is A8R8G8B8");
153 p_filter->ops = &I420_A8R8G8B8_ops;
155 else if( p_filter->fmt_out.video.i_rmask == 0xff000000
156 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
157 && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
159 /* R8G8B8A8 pixel format */
160 msg_Dbg(p_filter, "RGB pixel format is R8G8B8A8");
161 p_filter->ops = &I420_R8G8B8A8_ops;
163 else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
164 && p_filter->fmt_out.video.i_gmask == 0x00ff0000
165 && p_filter->fmt_out.video.i_bmask == 0xff000000 )
167 /* B8G8R8A8 pixel format */
168 msg_Dbg(p_filter, "RGB pixel format is B8G8R8A8");
169 p_filter->ops = &I420_B8G8R8A8_ops;
171 else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
172 && p_filter->fmt_out.video.i_gmask == 0x0000ff00
173 && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
175 /* A8B8G8R8 pixel format */
176 msg_Dbg(p_filter, "RGB pixel format is A8B8G8R8");
177 p_filter->ops = &I420_A8B8G8R8_ops;
179 else
180 return VLC_EGENERIC;
181 break;
182 #else
183 case VLC_CODEC_RGB8:
184 p_filter->ops = &I420_RGB8_ops;
185 break;
186 case VLC_CODEC_RGB15:
187 case VLC_CODEC_RGB16:
188 p_filter->ops = &I420_RGB16_ops;
189 break;
190 case VLC_CODEC_RGB32:
191 p_filter->ops = &I420_RGB32_ops;
192 break;
193 #endif
194 default:
195 return VLC_EGENERIC;
197 break;
199 default:
200 return VLC_EGENERIC;
203 filter_sys_t *p_sys = malloc( sizeof( filter_sys_t ) );
204 if( p_sys == NULL )
205 return VLC_EGENERIC;
206 p_filter->p_sys = p_sys;
208 p_sys->i_buffer_size = 0;
209 p_sys->p_buffer = NULL;
210 switch( p_filter->fmt_out.video.i_chroma )
212 #ifdef PLAIN
213 case VLC_CODEC_RGB8:
214 p_sys->i_bytespp = 1;
215 break;
216 #endif
217 case VLC_CODEC_RGB15:
218 case VLC_CODEC_RGB16:
219 p_sys->i_bytespp = 2;
220 break;
221 case VLC_CODEC_RGB24:
222 case VLC_CODEC_RGB32:
223 p_sys->i_bytespp = 4;
224 break;
225 default:
226 free( p_sys );
227 return VLC_EGENERIC;
230 p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
231 * ( ( p_filter->fmt_out.video.i_chroma
232 == VLC_CODEC_RGB8 ) ? 2 : 1 )
233 * sizeof( int ) );
234 if( p_sys->p_offset == NULL )
236 free( p_sys );
237 return VLC_EGENERIC;
240 #ifdef PLAIN
241 switch( p_filter->fmt_out.video.i_chroma )
243 case VLC_CODEC_RGB8:
244 i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
245 break;
246 case VLC_CODEC_RGB15:
247 case VLC_CODEC_RGB16:
248 i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
249 break;
250 default: /* RV24, RV32 */
251 i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
252 break;
255 p_sys->p_base = malloc( i_tables_size );
256 if( p_sys->p_base == NULL )
258 free( p_sys->p_offset );
259 free( p_sys );
260 return -1;
263 video_format_t vfmt;
264 video_format_Init( &vfmt, p_filter->fmt_out.video.i_chroma );
265 video_format_Copy( &vfmt, &p_filter->fmt_out.video );
266 if( !vfmt.i_bmask || !vfmt.i_gmask || !vfmt.i_bmask )
267 msg_Warn( p_filter, "source did not set proper target RGB masks, using default" );
268 video_format_FixRgb( &vfmt );
269 SetYUV( p_filter, &vfmt );
270 video_format_Clean( &vfmt );
271 #endif
273 return 0;
276 /*****************************************************************************
277 * Deactivate: free the chroma function
278 *****************************************************************************
279 * This function frees the previously allocated chroma function
280 *****************************************************************************/
281 static void Deactivate( filter_t *p_filter )
283 filter_sys_t *p_sys = p_filter->p_sys;
285 #ifdef PLAIN
286 free( p_sys->p_base );
287 #endif
288 free( p_sys->p_offset );
289 free( p_sys->p_buffer );
290 free( p_sys );
293 #ifdef PLAIN
294 /*****************************************************************************
295 * SetYUV: compute tables and set function pointers
296 *****************************************************************************/
297 static void SetYUV( filter_t *p_filter, const video_format_t *vfmt )
299 filter_sys_t *p_sys = p_filter->p_sys;
300 unsigned i_lrshift = ctz(vfmt->i_rmask);
301 unsigned i_lgshift = ctz(vfmt->i_gmask);
302 unsigned i_lbshift = ctz(vfmt->i_bmask);
303 unsigned i_rrshift = 8 - vlc_popcount(vfmt->i_rmask);
304 unsigned i_rgshift = 8 - vlc_popcount(vfmt->i_gmask);
305 unsigned i_rbshift = 8 - vlc_popcount(vfmt->i_bmask);
308 * Set pointers and build YUV tables
311 /* Color: build red, green and blue tables */
312 switch( p_filter->fmt_out.video.i_chroma )
314 case VLC_CODEC_RGB8:
315 p_sys->p_rgb8 = (uint8_t *)p_sys->p_base;
316 Set8bppPalette( p_filter, p_sys->p_rgb8 );
317 break;
319 case VLC_CODEC_RGB15:
320 case VLC_CODEC_RGB16:
321 p_sys->p_rgb16 = (uint16_t *)p_sys->p_base;
322 for( unsigned i_index = 0; i_index < RED_MARGIN; i_index++ )
324 p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 0 );
325 p_sys->p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 255, 0, 0 );
327 for( unsigned i_index = 0; i_index < GREEN_MARGIN; i_index++ )
329 p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 0 );
330 p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, 255, 0 );
332 for( unsigned i_index = 0; i_index < BLUE_MARGIN; i_index++ )
334 p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 0 );
335 p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 255 );
337 for( unsigned i_index = 0; i_index < 256; i_index++ )
339 p_sys->p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, i_index, 0, 0 );
340 p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, i_index, 0 );
341 p_sys->p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, i_index );
343 break;
345 case VLC_CODEC_RGB24:
346 case VLC_CODEC_RGB32:
347 p_sys->p_rgb32 = (uint32_t *)p_sys->p_base;
348 for( unsigned i_index = 0; i_index < RED_MARGIN; i_index++ )
350 p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 0 );
351 p_sys->p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 255, 0, 0 );
353 for( unsigned i_index = 0; i_index < GREEN_MARGIN; i_index++ )
355 p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 0 );
356 p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_filter, 0, 255, 0 );
358 for( unsigned i_index = 0; i_index < BLUE_MARGIN; i_index++ )
360 p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 0 );
361 p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, 255 );
363 for( unsigned i_index = 0; i_index < 256; i_index++ )
365 p_sys->p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_filter, i_index, 0, 0 );
366 p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, i_index, 0 );
367 p_sys->p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, 0, i_index );
369 break;
373 static void Set8bppPalette( filter_t *p_filter, uint8_t *p_rgb8 )
375 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
376 filter_sys_t *p_sys = p_filter->p_sys;
378 int y,u,v;
379 int r,g,b;
380 int i = 0, j = 0;
381 uint16_t *p_cmap_r = p_sys->p_rgb_r;
382 uint16_t *p_cmap_g = p_sys->p_rgb_g;
383 uint16_t *p_cmap_b = p_sys->p_rgb_b;
385 unsigned char p_lookup[PALETTE_TABLE_SIZE];
387 /* This loop calculates the intersection of an YUV box and the RGB cube. */
388 for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
390 for ( u = 0; u <= 256; u += 32 )
392 for ( v = 0; v <= 256; v += 32 )
394 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
395 g = y + ( (U_GREEN_COEF*(u-128)
396 + V_GREEN_COEF*(v-128)) >> SHIFT );
397 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
399 if( r >= 0x00 && g >= 0x00 && b >= 0x00
400 && r <= 0xff && g <= 0xff && b <= 0xff )
402 /* This one should never happen unless someone
403 * fscked up my code */
404 if( j == 256 )
406 msg_Err( p_filter, "no colors left in palette" );
407 break;
410 /* Clip the colors */
411 p_cmap_r[ j ] = CLIP( r );
412 p_cmap_g[ j ] = CLIP( g );
413 p_cmap_b[ j ] = CLIP( b );
415 #if 0
416 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
417 p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
418 p_cmap_b[ j ] >>8);
419 #endif
421 /* Allocate color */
422 p_lookup[ i ] = 1;
423 p_rgb8[ i++ ] = j;
424 j++;
426 else
428 p_lookup[ i ] = 0;
429 p_rgb8[ i++ ] = 0;
435 /* The colors have been allocated, we can set the palette */
436 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
437 p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
439 #if 0
440 /* There will eventually be a way to know which colors
441 * couldn't be allocated and try to find a replacement */
442 p_vout->i_white_pixel = 0xff;
443 p_vout->i_black_pixel = 0x00;
444 p_vout->i_gray_pixel = 0x44;
445 p_vout->i_blue_pixel = 0x3b;
446 #endif
448 /* This loop allocates colors that got outside the RGB cube */
449 for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
451 for ( u = 0; u <= 256; u += 32 )
453 for ( v = 0; v <= 256; v += 32, i++ )
455 int u2, v2, dist, mindist = 100000000;
457 if( p_lookup[ i ] || y == 0 )
459 continue;
462 /* Heavy. yeah. */
463 for( u2 = 0; u2 <= 256; u2 += 32 )
465 for( v2 = 0; v2 <= 256; v2 += 32 )
467 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
468 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
470 /* Find the nearest color */
471 if( p_lookup[ j ] && dist < mindist )
473 p_rgb8[ i ] = p_rgb8[ j ];
474 mindist = dist;
477 j -= 128;
479 /* Find the nearest color */
480 if( p_lookup[ j ] && dist + 128 < mindist )
482 p_rgb8[ i ] = p_rgb8[ j ];
483 mindist = dist + 128;
491 #endif