]> git.sesse.net Git - vlc/blob - modules/video_chroma/i420_rgb.c
LGPL
[vlc] / modules / video_chroma / i420_rgb.c
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$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <math.h>                                            /* exp(), pow() */
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include <vlc_cpu.h>
39
40 #include "i420_rgb.h"
41 #if defined (MODULE_NAME_IS_i420_rgb)
42 #   include "i420_rgb_c.h"
43     static picture_t *I420_RGB8_Filter         ( filter_t *, picture_t * );
44 //    static picture_t *I420_RGB16_dither_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 * );
47 #else
48     static picture_t *I420_R5G5B5_Filter       ( filter_t *, picture_t * );
49     static picture_t *I420_R5G6B5_Filter       ( filter_t *, picture_t * );
50     static picture_t *I420_A8R8G8B8_Filter     ( filter_t *, picture_t * );
51     static picture_t *I420_R8G8B8A8_Filter     ( filter_t *, picture_t * );
52     static picture_t *I420_B8G8R8A8_Filter     ( filter_t *, picture_t * );
53     static picture_t *I420_A8B8G8R8_Filter     ( filter_t *, picture_t * );
54 #endif
55
56 /*****************************************************************************
57  * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
58  *****************************************************************************/
59 #define RGB2PIXEL( p_filter, i_r, i_g, i_b )                 \
60     (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) \
61                        << p_filter->fmt_out.video.i_lrshift) \
62    | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) \
63                        << p_filter->fmt_out.video.i_lgshift) \
64    | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) \
65                        << p_filter->fmt_out.video.i_lbshift))
66
67 /*****************************************************************************
68  * Local and extern prototypes.
69  *****************************************************************************/
70 static int  Activate   ( vlc_object_t * );
71 static void Deactivate ( vlc_object_t * );
72
73 #if defined (MODULE_NAME_IS_i420_rgb)
74 static void SetGammaTable       ( int *pi_table, double f_gamma );
75 static void SetYUV              ( filter_t * );
76 static void Set8bppPalette      ( filter_t *, uint8_t * );
77 #endif
78
79 /*****************************************************************************
80  * Module descriptor.
81  *****************************************************************************/
82 vlc_module_begin ()
83 #if defined (MODULE_NAME_IS_i420_rgb)
84     set_description( N_("I420,IYUV,YV12 to "
85                        "RGB2,RV15,RV16,RV24,RV32 conversions") )
86     set_capability( "video filter2", 80 )
87 # define vlc_CPU_capable() (true)
88 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
89     set_description( N_( "MMX I420,IYUV,YV12 to "
90                         "RV15,RV16,RV24,RV32 conversions") )
91     set_capability( "video filter2", 100 )
92 # define vlc_CPU_capable() vlc_CPU_MMX()
93 #elif defined (MODULE_NAME_IS_i420_rgb_sse2)
94     set_description( N_( "SSE2 I420,IYUV,YV12 to "
95                         "RV15,RV16,RV24,RV32 conversions") )
96     set_capability( "video filter2", 120 )
97 # define vlc_CPU_capable() vlc_CPU_SSE2()
98 #endif
99     set_callbacks( Activate, Deactivate )
100 vlc_module_end ()
101
102 /*****************************************************************************
103  * Activate: allocate a chroma function
104  *****************************************************************************
105  * This function allocates and initializes a chroma function
106  *****************************************************************************/
107 static int Activate( vlc_object_t *p_this )
108 {
109     filter_t *p_filter = (filter_t *)p_this;
110 #if defined (MODULE_NAME_IS_i420_rgb)
111     size_t i_tables_size;
112 #endif
113
114     if( !vlc_CPU_capable() )
115         return VLC_EGENERIC;
116     if( p_filter->fmt_out.video.i_width & 1
117      || p_filter->fmt_out.video.i_height & 1 )
118     {
119         return VLC_EGENERIC;
120     }
121
122     switch( p_filter->fmt_in.video.i_chroma )
123     {
124         case VLC_CODEC_YV12:
125         case VLC_CODEC_I420:
126             switch( p_filter->fmt_out.video.i_chroma )
127             {
128 #if defined (MODULE_NAME_IS_i420_rgb)
129                 case VLC_CODEC_RGB8:
130                     p_filter->pf_video_filter = I420_RGB8_Filter;
131                     break;
132 #endif
133                 case VLC_CODEC_RGB15:
134                 case VLC_CODEC_RGB16:
135 #if ! defined (MODULE_NAME_IS_i420_rgb)
136                     /* If we don't have support for the bitmasks, bail out */
137                     if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
138                        && p_filter->fmt_out.video.i_gmask == 0x03e0
139                        && p_filter->fmt_out.video.i_bmask == 0x001f ) )
140                     {
141                         /* R5G5B6 pixel format */
142                         msg_Dbg(p_this, "RGB pixel format is R5G5B5");
143                         p_filter->pf_video_filter = I420_R5G5B5_Filter;
144                     }
145                     else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
146                             && p_filter->fmt_out.video.i_gmask == 0x07e0
147                             && p_filter->fmt_out.video.i_bmask == 0x001f ) )
148                     {
149                         /* R5G6B5 pixel format */
150                         msg_Dbg(p_this, "RGB pixel format is R5G6B5");
151                         p_filter->pf_video_filter = I420_R5G6B5_Filter;
152                     }
153                     else
154                         return VLC_EGENERIC;
155 #else
156                     // generic C chroma converter */
157                     p_filter->pf_video_filter = I420_RGB16_Filter;
158 #endif
159                     break;
160
161 #if 0
162                 /* Hmmm, is there only X11 using 32bits per pixel for RV24 ? */
163                 case VLC_CODEC_RGB24:
164 #endif
165
166                 case VLC_CODEC_RGB32:
167 #if ! defined (MODULE_NAME_IS_i420_rgb)
168                     /* If we don't have support for the bitmasks, bail out */
169                     if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
170                      && p_filter->fmt_out.video.i_gmask == 0x0000ff00
171                      && p_filter->fmt_out.video.i_bmask == 0x000000ff )
172                     {
173                         /* A8R8G8B8 pixel format */
174                         msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
175                         p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
176                     }
177                     else if( p_filter->fmt_out.video.i_rmask == 0xff000000
178                           && p_filter->fmt_out.video.i_gmask == 0x00ff0000
179                           && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
180                     {
181                         /* R8G8B8A8 pixel format */
182                         msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
183                         p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
184                     }
185                     else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
186                           && p_filter->fmt_out.video.i_gmask == 0x00ff0000
187                           && p_filter->fmt_out.video.i_bmask == 0xff000000 )
188                     {
189                         /* B8G8R8A8 pixel format */
190                         msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
191                         p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
192                     }
193                     else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
194                           && p_filter->fmt_out.video.i_gmask == 0x0000ff00
195                           && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
196                     {
197                         /* A8B8G8R8 pixel format */
198                         msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
199                         p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
200                     }
201                     else
202                         return VLC_EGENERIC;
203 #else
204                     /* generic C chroma converter */
205                     p_filter->pf_video_filter = I420_RGB32_Filter;
206 #endif
207                     break;
208
209                 default:
210                     return VLC_EGENERIC;
211             }
212             break;
213
214         default:
215             return VLC_EGENERIC;
216     }
217
218     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
219     if( p_filter->p_sys == NULL )
220     {
221         return VLC_EGENERIC;
222     }
223
224     switch( p_filter->fmt_out.video.i_chroma )
225     {
226 #if defined (MODULE_NAME_IS_i420_rgb)
227         case VLC_CODEC_RGB8:
228             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
229             break;
230 #endif
231
232         case VLC_CODEC_RGB15:
233         case VLC_CODEC_RGB16:
234             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
235             break;
236
237         case VLC_CODEC_RGB24:
238         case VLC_CODEC_RGB32:
239             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
240             break;
241
242         default:
243             p_filter->p_sys->p_buffer = NULL;
244             break;
245     }
246
247     if( p_filter->p_sys->p_buffer == NULL )
248     {
249         free( p_filter->p_sys );
250         return VLC_EGENERIC;
251     }
252
253     p_filter->p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
254                     * ( ( p_filter->fmt_out.video.i_chroma
255                            == VLC_CODEC_RGB8 ) ? 2 : 1 )
256                     * sizeof( int ) );
257     if( p_filter->p_sys->p_offset == NULL )
258     {
259         free( p_filter->p_sys->p_buffer );
260         free( p_filter->p_sys );
261         return VLC_EGENERIC;
262     }
263
264 #if defined (MODULE_NAME_IS_i420_rgb)
265     switch( p_filter->fmt_out.video.i_chroma )
266     {
267     case VLC_CODEC_RGB8:
268         i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
269         break;
270     case VLC_CODEC_RGB15:
271     case VLC_CODEC_RGB16:
272         i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
273         break;
274     default: /* RV24, RV32 */
275         i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
276         break;
277     }
278
279     p_filter->p_sys->p_base = malloc( i_tables_size );
280     if( p_filter->p_sys->p_base == NULL )
281     {
282         free( p_filter->p_sys->p_offset );
283         free( p_filter->p_sys->p_buffer );
284         free( p_filter->p_sys );
285         return -1;
286     }
287
288     SetYUV( p_filter );
289 #endif
290
291     return 0;
292 }
293
294 /*****************************************************************************
295  * Deactivate: free the chroma function
296  *****************************************************************************
297  * This function frees the previously allocated chroma function
298  *****************************************************************************/
299 static void Deactivate( vlc_object_t *p_this )
300 {
301     filter_t *p_filter = (filter_t *)p_this;
302
303 #if defined (MODULE_NAME_IS_i420_rgb)
304     free( p_filter->p_sys->p_base );
305 #endif
306     free( p_filter->p_sys->p_offset );
307     free( p_filter->p_sys->p_buffer );
308     free( p_filter->p_sys );
309 }
310
311 #if defined (MODULE_NAME_IS_i420_rgb)
312 VIDEO_FILTER_WRAPPER( I420_RGB8 )
313 VIDEO_FILTER_WRAPPER( I420_RGB16 )
314 //VIDEO_FILTER_WRAPPER( I420_RGB16_dither )
315 VIDEO_FILTER_WRAPPER( I420_RGB32 )
316 #else
317 VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
318 VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
319 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
320 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
321 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
322 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
323 #endif
324
325 #if defined (MODULE_NAME_IS_i420_rgb)
326 /*****************************************************************************
327  * SetGammaTable: return intensity table transformed by gamma curve.
328  *****************************************************************************
329  * pi_table is a table of 256 entries from 0 to 255.
330  *****************************************************************************/
331 static void SetGammaTable( int *pi_table, double f_gamma )
332 {
333     int i_y;                                               /* base intensity */
334
335     /* Use exp(gamma) instead of gamma */
336     f_gamma = exp( f_gamma );
337
338     /* Build gamma table */
339     for( i_y = 0; i_y < 256; i_y++ )
340     {
341         pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
342     }
343 }
344
345 /*****************************************************************************
346  * SetYUV: compute tables and set function pointers
347  *****************************************************************************/
348 static void SetYUV( filter_t *p_filter )
349 {
350     int          pi_gamma[256];                               /* gamma table */
351     volatile int i_index;                                 /* index in tables */
352                    /* We use volatile here to work around a strange gcc-3.3.4
353                     * optimization bug */
354
355     /* Build gamma table */
356     SetGammaTable( pi_gamma, 0 ); //p_filter/*FIXME wasn't used anywhere anyway*/->f_gamma );
357
358     /*
359      * Set pointers and build YUV tables
360      */
361
362     /* Color: build red, green and blue tables */
363     switch( p_filter->fmt_out.video.i_chroma )
364     {
365     case VLC_CODEC_RGB8:
366         p_filter->p_sys->p_rgb8 = (uint8_t *)p_filter->p_sys->p_base;
367         Set8bppPalette( p_filter, p_filter->p_sys->p_rgb8 );
368         break;
369
370     case VLC_CODEC_RGB15:
371     case VLC_CODEC_RGB16:
372         p_filter->p_sys->p_rgb16 = (uint16_t *)p_filter->p_sys->p_base;
373         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
374         {
375             p_filter->p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
376             p_filter->p_sys->p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
377         }
378         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
379         {
380             p_filter->p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
381             p_filter->p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
382         }
383         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
384         {
385             p_filter->p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
386             p_filter->p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
387         }
388         for( i_index = 0; i_index < 256; i_index++ )
389         {
390             p_filter->p_sys->p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
391             p_filter->p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
392             p_filter->p_sys->p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
393         }
394         break;
395
396     case VLC_CODEC_RGB24:
397     case VLC_CODEC_RGB32:
398         p_filter->p_sys->p_rgb32 = (uint32_t *)p_filter->p_sys->p_base;
399         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
400         {
401             p_filter->p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_filter, pi_gamma[0], 0, 0 );
402             p_filter->p_sys->p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_filter, pi_gamma[255], 0, 0 );
403         }
404         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
405         {
406             p_filter->p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[0], 0 );
407             p_filter->p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_filter, 0, pi_gamma[255], 0 );
408         }
409         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
410         {
411             p_filter->p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[0] );
412             p_filter->p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_filter, 0, 0, pi_gamma[255] );
413         }
414         for( i_index = 0; i_index < 256; i_index++ )
415         {
416             p_filter->p_sys->p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_filter, pi_gamma[ i_index ], 0, 0 );
417             p_filter->p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_filter, 0, pi_gamma[ i_index ], 0 );
418             p_filter->p_sys->p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_filter, 0, 0, pi_gamma[ i_index ] );
419         }
420         break;
421     }
422 }
423
424 static void Set8bppPalette( filter_t *p_filter, uint8_t *p_rgb8 )
425 {
426     #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
427
428     int y,u,v;
429     int r,g,b;
430     int i = 0, j = 0;
431     uint16_t *p_cmap_r = p_filter->p_sys->p_rgb_r;
432     uint16_t *p_cmap_g = p_filter->p_sys->p_rgb_g;
433     uint16_t *p_cmap_b = p_filter->p_sys->p_rgb_b;
434
435     unsigned char p_lookup[PALETTE_TABLE_SIZE];
436
437     /* This loop calculates the intersection of an YUV box and the RGB cube. */
438     for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
439     {
440         for ( u = 0; u <= 256; u += 32 )
441         {
442             for ( v = 0; v <= 256; v += 32 )
443             {
444                 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
445                 g = y + ( (U_GREEN_COEF*(u-128)
446                          + V_GREEN_COEF*(v-128)) >> SHIFT );
447                 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
448
449                 if( r >= 0x00 && g >= 0x00 && b >= 0x00
450                         && r <= 0xff && g <= 0xff && b <= 0xff )
451                 {
452                     /* This one should never happen unless someone
453                      * fscked up my code */
454                     if( j == 256 )
455                     {
456                         msg_Err( p_filter, "no colors left in palette" );
457                         break;
458                     }
459
460                     /* Clip the colors */
461                     p_cmap_r[ j ] = CLIP( r );
462                     p_cmap_g[ j ] = CLIP( g );
463                     p_cmap_b[ j ] = CLIP( b );
464
465 #if 0
466             printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
467                p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8,
468                p_cmap_b[ j ] >>8);
469 #endif
470
471                     /* Allocate color */
472                     p_lookup[ i ] = 1;
473                     p_rgb8[ i++ ] = j;
474                     j++;
475                 }
476                 else
477                 {
478                     p_lookup[ i ] = 0;
479                     p_rgb8[ i++ ] = 0;
480                 }
481             }
482         }
483     }
484
485     /* The colors have been allocated, we can set the palette */
486     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
487     p_filter->fmt_out.video.pf_setpalette( p_filter, p_cmap_r, p_cmap_g, p_cmap_b );*/
488
489 #if 0
490     /* There will eventually be a way to know which colors
491      * couldn't be allocated and try to find a replacement */
492     p_vout->i_white_pixel = 0xff;
493     p_vout->i_black_pixel = 0x00;
494     p_vout->i_gray_pixel = 0x44;
495     p_vout->i_blue_pixel = 0x3b;
496 #endif
497
498     /* This loop allocates colors that got outside the RGB cube */
499     for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
500     {
501         for ( u = 0; u <= 256; u += 32 )
502         {
503             for ( v = 0; v <= 256; v += 32, i++ )
504             {
505                 int u2, v2, dist, mindist = 100000000;
506
507                 if( p_lookup[ i ] || y == 0 )
508                 {
509                     continue;
510                 }
511
512                 /* Heavy. yeah. */
513                 for( u2 = 0; u2 <= 256; u2 += 32 )
514                 {
515                     for( v2 = 0; v2 <= 256; v2 += 32 )
516                     {
517                         j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
518                         dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
519
520                         /* Find the nearest color */
521                         if( p_lookup[ j ] && dist < mindist )
522                         {
523                             p_rgb8[ i ] = p_rgb8[ j ];
524                             mindist = dist;
525                         }
526
527                         j -= 128;
528
529                         /* Find the nearest color */
530                         if( p_lookup[ j ] && dist + 128 < mindist )
531                         {
532                             p_rgb8[ i ] = p_rgb8[ j ];
533                             mindist = dist + 128;
534                         }
535                     }
536                 }
537             }
538         }
539     }
540 }
541
542 #endif
543