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