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>
38 #include <vlc_picture.h>
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
*, const video_format_t
* );
50 static void Set8bppPalette( filter_t
*, uint8_t * );
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
* );
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) >> vfmt->i_rrshift) \
66 | ((((uint32_t)i_g) >> vfmt->i_rgshift) \
68 | ((((uint32_t)i_b) >> vfmt->i_rbshift) \
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Activate ( vlc_object_t
* );
75 static void Deactivate ( vlc_object_t
* );
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()
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()
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)
94 set_callbacks( Activate
, Deactivate
)
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
;
106 size_t i_tables_size
;
109 if( !vlc_CPU_capable() )
111 if( p_filter
->fmt_out
.video
.i_width
& 1
112 || p_filter
->fmt_out
.video
.i_height
& 1 )
117 if( p_filter
->fmt_in
.video
.orientation
!= p_filter
->fmt_out
.video
.orientation
)
122 switch( p_filter
->fmt_in
.video
.i_chroma
)
126 switch( p_filter
->fmt_out
.video
.i_chroma
)
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
;
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
;
190 p_filter
->pf_video_filter
= I420_RGB8_Filter
;
192 case VLC_CODEC_RGB15
:
193 case VLC_CODEC_RGB16
:
194 p_filter
->pf_video_filter
= I420_RGB16_Filter
;
196 case VLC_CODEC_RGB32
:
197 p_filter
->pf_video_filter
= I420_RGB32_Filter
;
209 filter_sys_t
*p_sys
= malloc( sizeof( filter_sys_t
) );
212 p_filter
->p_sys
= p_sys
;
214 p_sys
->i_buffer_size
= 0;
215 p_sys
->p_buffer
= NULL
;
216 switch( p_filter
->fmt_out
.video
.i_chroma
)
220 p_sys
->i_bytespp
= 1;
223 case VLC_CODEC_RGB15
:
224 case VLC_CODEC_RGB16
:
225 p_sys
->i_bytespp
= 2;
227 case VLC_CODEC_RGB24
:
228 case VLC_CODEC_RGB32
:
229 p_sys
->i_bytespp
= 4;
236 p_sys
->p_offset
= malloc( p_filter
->fmt_out
.video
.i_width
237 * ( ( p_filter
->fmt_out
.video
.i_chroma
238 == VLC_CODEC_RGB8
) ? 2 : 1 )
240 if( p_sys
->p_offset
== NULL
)
247 switch( p_filter
->fmt_out
.video
.i_chroma
)
250 i_tables_size
= sizeof( uint8_t ) * PALETTE_TABLE_SIZE
;
252 case VLC_CODEC_RGB15
:
253 case VLC_CODEC_RGB16
:
254 i_tables_size
= sizeof( uint16_t ) * RGB_TABLE_SIZE
;
256 default: /* RV24, RV32 */
257 i_tables_size
= sizeof( uint32_t ) * RGB_TABLE_SIZE
;
261 p_sys
->p_base
= malloc( i_tables_size
);
262 if( p_sys
->p_base
== NULL
)
264 free( p_sys
->p_offset
);
270 video_format_Init( &vfmt
, p_filter
->fmt_out
.video
.i_chroma
);
271 video_format_Copy( &vfmt
, &p_filter
->fmt_out
.video
);
272 if( !vfmt
.i_bmask
|| !vfmt
.i_gmask
|| !vfmt
.i_bmask
)
273 msg_Warn( p_filter
, "source did not set proper target RGB masks, using default" );
274 video_format_FixRgb( &vfmt
);
275 SetYUV( p_filter
, &vfmt
);
276 video_format_Clean( &vfmt
);
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
;
290 filter_sys_t
*p_sys
= p_filter
->p_sys
;
293 free( p_sys
->p_base
);
295 free( p_sys
->p_offset
);
296 free( p_sys
->p_buffer
);
301 VIDEO_FILTER_WRAPPER( I420_R5G5B5
)
302 VIDEO_FILTER_WRAPPER( I420_R5G6B5
)
303 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8
)
304 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8
)
305 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8
)
306 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8
)
308 VIDEO_FILTER_WRAPPER( I420_RGB8
)
309 VIDEO_FILTER_WRAPPER( I420_RGB16
)
310 VIDEO_FILTER_WRAPPER( I420_RGB32
)
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
, const video_format_t
*vfmt
)
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 filter_sys_t
*p_sys
= p_filter
->p_sys
;
343 /* Build gamma table */
344 SetGammaTable( pi_gamma
, 0 ); //p_filter/*FIXME wasn't used anywhere anyway*/->f_gamma );
347 * Set pointers and build YUV tables
350 /* Color: build red, green and blue tables */
351 switch( p_filter
->fmt_out
.video
.i_chroma
)
354 p_sys
->p_rgb8
= (uint8_t *)p_sys
->p_base
;
355 Set8bppPalette( p_filter
, p_sys
->p_rgb8
);
358 case VLC_CODEC_RGB15
:
359 case VLC_CODEC_RGB16
:
360 p_sys
->p_rgb16
= (uint16_t *)p_sys
->p_base
;
361 for( i_index
= 0; i_index
< RED_MARGIN
; i_index
++ )
363 p_sys
->p_rgb16
[RED_OFFSET
- RED_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, pi_gamma
[0], 0, 0 );
364 p_sys
->p_rgb16
[RED_OFFSET
+ 256 + i_index
] = RGB2PIXEL( p_filter
, pi_gamma
[255], 0, 0 );
366 for( i_index
= 0; i_index
< GREEN_MARGIN
; i_index
++ )
368 p_sys
->p_rgb16
[GREEN_OFFSET
- GREEN_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, 0, pi_gamma
[0], 0 );
369 p_sys
->p_rgb16
[GREEN_OFFSET
+ 256 + i_index
] = RGB2PIXEL( p_filter
, 0, pi_gamma
[255], 0 );
371 for( i_index
= 0; i_index
< BLUE_MARGIN
; i_index
++ )
373 p_sys
->p_rgb16
[BLUE_OFFSET
- BLUE_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, 0, 0, pi_gamma
[0] );
374 p_sys
->p_rgb16
[BLUE_OFFSET
+ BLUE_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, 0, 0, pi_gamma
[255] );
376 for( i_index
= 0; i_index
< 256; i_index
++ )
378 p_sys
->p_rgb16
[RED_OFFSET
+ i_index
] = RGB2PIXEL( p_filter
, pi_gamma
[ i_index
], 0, 0 );
379 p_sys
->p_rgb16
[GREEN_OFFSET
+ i_index
] = RGB2PIXEL( p_filter
, 0, pi_gamma
[ i_index
], 0 );
380 p_sys
->p_rgb16
[BLUE_OFFSET
+ i_index
] = RGB2PIXEL( p_filter
, 0, 0, pi_gamma
[ i_index
] );
384 case VLC_CODEC_RGB24
:
385 case VLC_CODEC_RGB32
:
386 p_sys
->p_rgb32
= (uint32_t *)p_sys
->p_base
;
387 for( i_index
= 0; i_index
< RED_MARGIN
; i_index
++ )
389 p_sys
->p_rgb32
[RED_OFFSET
- RED_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, pi_gamma
[0], 0, 0 );
390 p_sys
->p_rgb32
[RED_OFFSET
+ 256 + i_index
] = RGB2PIXEL( p_filter
, pi_gamma
[255], 0, 0 );
392 for( i_index
= 0; i_index
< GREEN_MARGIN
; i_index
++ )
394 p_sys
->p_rgb32
[GREEN_OFFSET
- GREEN_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, 0, pi_gamma
[0], 0 );
395 p_sys
->p_rgb32
[GREEN_OFFSET
+ 256 + i_index
] = RGB2PIXEL( p_filter
, 0, pi_gamma
[255], 0 );
397 for( i_index
= 0; i_index
< BLUE_MARGIN
; i_index
++ )
399 p_sys
->p_rgb32
[BLUE_OFFSET
- BLUE_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, 0, 0, pi_gamma
[0] );
400 p_sys
->p_rgb32
[BLUE_OFFSET
+ BLUE_MARGIN
+ i_index
] = RGB2PIXEL( p_filter
, 0, 0, pi_gamma
[255] );
402 for( i_index
= 0; i_index
< 256; i_index
++ )
404 p_sys
->p_rgb32
[RED_OFFSET
+ i_index
] = RGB2PIXEL( p_filter
, pi_gamma
[ i_index
], 0, 0 );
405 p_sys
->p_rgb32
[GREEN_OFFSET
+ i_index
] = RGB2PIXEL( p_filter
, 0, pi_gamma
[ i_index
], 0 );
406 p_sys
->p_rgb32
[BLUE_OFFSET
+ i_index
] = RGB2PIXEL( p_filter
, 0, 0, pi_gamma
[ i_index
] );
412 static void Set8bppPalette( filter_t
*p_filter
, uint8_t *p_rgb8
)
414 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
415 filter_sys_t
*p_sys
= p_filter
->p_sys
;
420 uint16_t *p_cmap_r
= p_sys
->p_rgb_r
;
421 uint16_t *p_cmap_g
= p_sys
->p_rgb_g
;
422 uint16_t *p_cmap_b
= p_sys
->p_rgb_b
;
424 unsigned char p_lookup
[PALETTE_TABLE_SIZE
];
426 /* This loop calculates the intersection of an YUV box and the RGB cube. */
427 for ( y
= 0; y
<= 256; y
+= 16, i
+= 128 - 81 )
429 for ( u
= 0; u
<= 256; u
+= 32 )
431 for ( v
= 0; v
<= 256; v
+= 32 )
433 r
= y
+ ( (V_RED_COEF
*(v
-128)) >> SHIFT
);
434 g
= y
+ ( (U_GREEN_COEF
*(u
-128)
435 + V_GREEN_COEF
*(v
-128)) >> SHIFT
);
436 b
= y
+ ( (U_BLUE_COEF
*(u
-128)) >> SHIFT
);
438 if( r
>= 0x00 && g
>= 0x00 && b
>= 0x00
439 && r
<= 0xff && g
<= 0xff && b
<= 0xff )
441 /* This one should never happen unless someone
442 * fscked up my code */
445 msg_Err( p_filter
, "no colors left in palette" );
449 /* Clip the colors */
450 p_cmap_r
[ j
] = CLIP( r
);
451 p_cmap_g
[ j
] = CLIP( g
);
452 p_cmap_b
[ j
] = CLIP( b
);
455 printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j
,
456 p_cmap_r
[ j
] >>8, p_cmap_g
[ j
] >>8,
474 /* The colors have been allocated, we can set the palette */
475 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
476 p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
479 /* There will eventually be a way to know which colors
480 * couldn't be allocated and try to find a replacement */
481 p_vout
->i_white_pixel
= 0xff;
482 p_vout
->i_black_pixel
= 0x00;
483 p_vout
->i_gray_pixel
= 0x44;
484 p_vout
->i_blue_pixel
= 0x3b;
487 /* This loop allocates colors that got outside the RGB cube */
488 for ( i
= 0, y
= 0; y
<= 256; y
+= 16, i
+= 128 - 81 )
490 for ( u
= 0; u
<= 256; u
+= 32 )
492 for ( v
= 0; v
<= 256; v
+= 32, i
++ )
494 int u2
, v2
, dist
, mindist
= 100000000;
496 if( p_lookup
[ i
] || y
== 0 )
502 for( u2
= 0; u2
<= 256; u2
+= 32 )
504 for( v2
= 0; v2
<= 256; v2
+= 32 )
506 j
= ((y
>>4)<<7) + (u2
>>5)*9 + (v2
>>5);
507 dist
= (u
-u2
)*(u
-u2
) + (v
-v2
)*(v
-v2
);
509 /* Find the nearest color */
510 if( p_lookup
[ j
] && dist
< mindist
)
512 p_rgb8
[ i
] = p_rgb8
[ j
];
518 /* Find the nearest color */
519 if( p_lookup
[ j
] && dist
+ 128 < mindist
)
521 p_rgb8
[ i
] = p_rgb8
[ j
];
522 mindist
= dist
+ 128;