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