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