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