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