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