video_chroma: added I420_ABGR32 support (mostly for opengl), some clean up as well
[vlc.git] / 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 == 0xff000000
159                           && p_vout->output.i_gmask == 0x00ff0000
160                           && p_vout->output.i_bmask == 0x0000ff00 )
161                     {
162                         /* R8G8B8A8 pixel format */
163                         msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
164                         //p_vout->chroma.pf_convert = E_(I420_B8G8R8A8);
165                         return -1;
166                     }
167                     else if( p_vout->output.i_rmask == 0x0000ff00
168                           && p_vout->output.i_gmask == 0x00ff0000
169                           && p_vout->output.i_bmask == 0xff000000 )
170                     {
171                         /* B8G8R8A8 pixel format */
172                         msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
173                         p_vout->chroma.pf_convert = E_(I420_B8G8R8A8);
174                     }
175                     else if( p_vout->output.i_rmask == 0x000000ff
176                           && p_vout->output.i_gmask == 0x0000ff00
177                           && p_vout->output.i_bmask == 0x00ff0000 )
178                     {
179                         /* A8B8G8R8 pixel format */
180                         msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
181                         p_vout->chroma.pf_convert = E_(I420_A8B8G8R8);
182                     }
183                     else
184                         return -1;
185 #else
186                     /* generic C chroma converter */
187                     p_vout->chroma.pf_convert = E_(I420_RGB32);
188 #endif
189                     break;
190
191                 default:
192                     return -1;
193             }
194             break;
195
196         default:
197             return -1;
198     }
199
200     p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
201     if( p_vout->chroma.p_sys == NULL )
202     {
203         return -1;
204     }
205
206     switch( p_vout->output.i_chroma )
207     {
208 #if defined (MODULE_NAME_IS_i420_rgb)
209         case VLC_FOURCC('R','G','B','2'):
210             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
211             break;
212 #endif
213
214         case VLC_FOURCC('R','V','1','5'):
215         case VLC_FOURCC('R','V','1','6'):
216             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
217             break;
218
219         case VLC_FOURCC('R','V','2','4'):
220         case VLC_FOURCC('R','V','3','2'):
221             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
222             break;
223
224         default:
225             p_vout->chroma.p_sys->p_buffer = NULL;
226             break;
227     }
228
229     if( p_vout->chroma.p_sys->p_buffer == NULL )
230     {
231         free( p_vout->chroma.p_sys );
232         return -1;
233     }
234
235     p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
236                     * ( ( p_vout->output.i_chroma
237                            == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
238                     * sizeof( int ) );
239     if( p_vout->chroma.p_sys->p_offset == NULL )
240     {
241         free( p_vout->chroma.p_sys->p_buffer );
242         free( p_vout->chroma.p_sys );
243         return -1;
244     }
245
246 #if defined (MODULE_NAME_IS_i420_rgb)
247     switch( p_vout->output.i_chroma )
248     {
249     case VLC_FOURCC('R','G','B','2'):
250         i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
251         break;
252     case VLC_FOURCC('R','V','1','5'):
253     case VLC_FOURCC('R','V','1','6'):
254         i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
255         break;
256     default: /* RV24, RV32 */
257         i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
258         break;
259     }
260
261     p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
262     if( p_vout->chroma.p_sys->p_base == NULL )
263     {
264         free( p_vout->chroma.p_sys->p_offset );
265         free( p_vout->chroma.p_sys->p_buffer );
266         free( p_vout->chroma.p_sys );
267         return -1;
268     }
269
270     SetYUV( p_vout );
271 #endif
272
273     return 0;
274 }
275
276 /*****************************************************************************
277  * Deactivate: free the chroma function
278  *****************************************************************************
279  * This function frees the previously allocated chroma function
280  *****************************************************************************/
281 static void Deactivate( vlc_object_t *p_this )
282 {
283     vout_thread_t *p_vout = (vout_thread_t *)p_this;
284
285 #if defined (MODULE_NAME_IS_i420_rgb)
286     free( p_vout->chroma.p_sys->p_base );
287 #endif
288     free( p_vout->chroma.p_sys->p_offset );
289     free( p_vout->chroma.p_sys->p_buffer );
290     free( p_vout->chroma.p_sys );
291 }
292
293 #if defined (MODULE_NAME_IS_i420_rgb)
294 /*****************************************************************************
295  * SetGammaTable: return intensity table transformed by gamma curve.
296  *****************************************************************************
297  * pi_table is a table of 256 entries from 0 to 255.
298  *****************************************************************************/
299 static void SetGammaTable( int *pi_table, double f_gamma )
300 {
301     int i_y;                                               /* base intensity */
302
303     /* Use exp(gamma) instead of gamma */
304     f_gamma = exp( f_gamma );
305
306     /* Build gamma table */
307     for( i_y = 0; i_y < 256; i_y++ )
308     {
309         pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
310     }
311 }
312
313 /*****************************************************************************
314  * SetYUV: compute tables and set function pointers
315  *****************************************************************************/
316 static void SetYUV( vout_thread_t *p_vout )
317 {
318     int          pi_gamma[256];                               /* gamma table */
319     volatile int i_index;                                 /* index in tables */
320                    /* We use volatile here to work around a strange gcc-3.3.4
321                     * optimization bug */
322
323     /* Build gamma table */
324     SetGammaTable( pi_gamma, p_vout->f_gamma );
325
326     /*
327      * Set pointers and build YUV tables
328      */
329
330     /* Color: build red, green and blue tables */
331     switch( p_vout->output.i_chroma )
332     {
333     case VLC_FOURCC('R','G','B','2'):
334         p_vout->chroma.p_sys->p_rgb8 = (uint8_t *)p_vout->chroma.p_sys->p_base;
335         Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
336         break;
337
338     case VLC_FOURCC('R','V','1','5'):
339     case VLC_FOURCC('R','V','1','6'):
340         p_vout->chroma.p_sys->p_rgb16 = (uint16_t *)p_vout->chroma.p_sys->p_base;
341         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
342         {
343             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
344             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
345         }
346         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
347         {
348             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
349             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
350         }
351         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
352         {
353             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
354             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
355         }
356         for( i_index = 0; i_index < 256; i_index++ )
357         {
358             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
359             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
360             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
361         }
362         break;
363
364     case VLC_FOURCC('R','V','2','4'):
365     case VLC_FOURCC('R','V','3','2'):
366         p_vout->chroma.p_sys->p_rgb32 = (uint32_t *)p_vout->chroma.p_sys->p_base;
367         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
368         {
369             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
370             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
371         }
372         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
373         {
374             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
375             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
376         }
377         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
378         {
379             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
380             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
381         }
382         for( i_index = 0; i_index < 256; i_index++ )
383         {
384             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
385             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
386             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
387         }
388         break;
389     }
390 }
391
392 static void Set8bppPalette( vout_thread_t *p_vout, uint8_t *p_rgb8 )
393 {
394     #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
395
396     int y,u,v;
397     int r,g,b;
398     int i = 0, j = 0;
399     uint16_t *p_cmap_r=p_vout->chroma.p_sys->p_rgb_r;
400     uint16_t *p_cmap_g=p_vout->chroma.p_sys->p_rgb_g;
401     uint16_t *p_cmap_b=p_vout->chroma.p_sys->p_rgb_b;
402
403     unsigned char p_lookup[PALETTE_TABLE_SIZE];
404
405     /* This loop calculates the intersection of an YUV box and the RGB cube. */
406     for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
407     {
408         for ( u = 0; u <= 256; u += 32 )
409         {
410             for ( v = 0; v <= 256; v += 32 )
411             {
412                 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
413                 g = y + ( (U_GREEN_COEF*(u-128)
414                          + V_GREEN_COEF*(v-128)) >> SHIFT );
415                 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
416
417                 if( r >= 0x00 && g >= 0x00 && b >= 0x00
418                         && r <= 0xff && g <= 0xff && b <= 0xff )
419                 {
420                     /* This one should never happen unless someone
421                      * fscked up my code */
422                     if( j == 256 )
423                     {
424                         msg_Err( p_vout, "no colors left in palette" );
425                         break;
426                     }
427
428                     /* Clip the colors */
429                     p_cmap_r[ j ] = CLIP( r );
430                     p_cmap_g[ j ] = CLIP( g );
431                     p_cmap_b[ j ] = CLIP( b );
432
433 #if 0
434                     printf("+++Alloc RGB cmap %d (%d, %d, %d)\n", j,
435                            p_cmap_r[ j ] >>8, p_cmap_g[ j ] >>8, 
436                            p_cmap_b[ j ] >>8);
437 #endif
438
439                     /* Allocate color */
440                     p_lookup[ i ] = 1;
441                     p_rgb8[ i++ ] = j;
442                     j++;
443                 }
444                 else
445                 {
446                     p_lookup[ i ] = 0;
447                     p_rgb8[ i++ ] = 0;
448                 }
449             }
450         }
451     }
452
453     /* The colors have been allocated, we can set the palette */
454     p_vout->output.pf_setpalette( p_vout, p_cmap_r, p_cmap_g, p_cmap_b );
455
456 #if 0
457     /* There will eventually be a way to know which colors
458      * couldn't be allocated and try to find a replacement */
459     p_vout->i_white_pixel = 0xff;
460     p_vout->i_black_pixel = 0x00;
461     p_vout->i_gray_pixel = 0x44;
462     p_vout->i_blue_pixel = 0x3b;
463 #endif
464
465     /* This loop allocates colors that got outside the RGB cube */
466     for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
467     {
468         for ( u = 0; u <= 256; u += 32 )
469         {
470             for ( v = 0; v <= 256; v += 32, i++ )
471             {
472                 int u2, v2, dist, mindist = 100000000;
473
474                 if( p_lookup[ i ] || y == 0 )
475                 {
476                     continue;
477                 }
478
479                 /* Heavy. yeah. */
480                 for( u2 = 0; u2 <= 256; u2 += 32 )
481                 {
482                     for( v2 = 0; v2 <= 256; v2 += 32 )
483                     {
484                         j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
485                         dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
486
487                         /* Find the nearest color */
488                         if( p_lookup[ j ] && dist < mindist )
489                         {
490                             p_rgb8[ i ] = p_rgb8[ j ];
491                             mindist = dist;
492                         }
493
494                         j -= 128;
495
496                         /* Find the nearest color */
497                         if( p_lookup[ j ] && dist + 128 < mindist )
498                         {
499                             p_rgb8[ i ] = p_rgb8[ j ];
500                             mindist = dist + 128;
501                         }
502                     }
503                 }
504             }
505         }
506     }
507 }
508
509 #endif
510