]> git.sesse.net Git - vlc/blob - modules/video_chroma/i420_rgb.c
* modules/misc/network/ipv6.c: gave a quick try to implementing ttl for ipv6.
[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 VideoLAN
5  * $Id: i420_rgb.c,v 1.3 2002/11/25 19:29:10 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <math.h>                                            /* exp(), pow() */
28 #include <string.h>                                            /* strerror() */
29 #include <stdlib.h>                                      /* malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33
34 #include "i420_rgb.h"
35 #if defined (MODULE_NAME_IS_i420_rgb)
36 #   include "i420_rgb_c.h"
37 #endif
38
39 /*****************************************************************************
40  * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
41  *****************************************************************************/
42 #define RGB2PIXEL( p_vout, i_r, i_g, i_b )          \
43     (((((uint32_t)i_r) >> p_vout->output.i_rrshift) \
44                        << p_vout->output.i_lrshift) \
45    | ((((uint32_t)i_g) >> p_vout->output.i_rgshift) \
46                        << p_vout->output.i_lgshift) \
47    | ((((uint32_t)i_b) >> p_vout->output.i_rbshift) \
48                        << p_vout->output.i_lbshift))
49
50 /*****************************************************************************
51  * Local and extern prototypes.
52  *****************************************************************************/
53 static int  Activate   ( vlc_object_t * );
54 static void Deactivate ( vlc_object_t * );
55
56 #if defined (MODULE_NAME_IS_i420_rgb)
57 static void SetGammaTable       ( int *pi_table, double f_gamma );
58 static void SetYUV              ( vout_thread_t * );
59 static void Set8bppPalette      ( vout_thread_t *, u8 * );
60 #endif
61
62 /*****************************************************************************
63  * Module descriptor.
64  *****************************************************************************/
65 vlc_module_begin();
66 #if defined (MODULE_NAME_IS_i420_rgb)
67     set_description( _("I420,IYUV,YV12 to "
68                        "RGB2,RV15,RV16,RV24,RV32 conversions") );
69     set_capability( "chroma", 80 );
70 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
71     set_description( _( "MMX I420,IYUV,YV12 to "
72                         "RV15,RV16,RV24,RV32 conversions") );
73     set_capability( "chroma", 100 );
74     add_requirement( MMX );
75 #endif
76     set_callbacks( Activate, Deactivate );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * Activate: allocate a chroma function
81  *****************************************************************************
82  * This function allocates and initializes a chroma function
83  *****************************************************************************/
84 static int Activate( vlc_object_t *p_this )
85 {
86     vout_thread_t *p_vout = (vout_thread_t *)p_this;
87 #if defined (MODULE_NAME_IS_i420_rgb)
88     size_t i_tables_size;
89 #endif
90
91     if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
92     {
93         return -1;
94     }
95
96     switch( p_vout->render.i_chroma )
97     {
98         case VLC_FOURCC('Y','V','1','2'):
99         case VLC_FOURCC('I','4','2','0'):
100         case VLC_FOURCC('I','Y','U','V'):
101             switch( p_vout->output.i_chroma )
102             {
103 #if defined (MODULE_NAME_IS_i420_rgb)
104                 case VLC_FOURCC('R','G','B','2'):
105                     p_vout->chroma.pf_convert = E_(I420_RGB8);
106                     break;
107 #endif
108                 case VLC_FOURCC('R','V','1','5'):
109                 case VLC_FOURCC('R','V','1','6'):
110 #if defined (MODULE_NAME_IS_i420_rgb_mmx)
111                     /* If we don't have support for the bitmasks, bail out */
112                     if( ( p_vout->output.i_rmask != 0x7c00
113                            || p_vout->output.i_gmask != 0x03e0
114                            || p_vout->output.i_bmask != 0x001f )
115                      && ( p_vout->output.i_rmask != 0xf800
116                            || p_vout->output.i_gmask != 0x07e0
117                            || p_vout->output.i_bmask != 0x001f ) )
118                     {
119                         return -1;
120                     }
121 #endif
122                     p_vout->chroma.pf_convert = E_(I420_RGB16);
123                     break;
124
125                 case VLC_FOURCC('R','V','2','4'):
126                 case VLC_FOURCC('R','V','3','2'):
127 #if defined (MODULE_NAME_IS_i420_rgb_mmx)
128                     /* If we don't have support for the bitmasks, bail out */
129                     if( p_vout->output.i_rmask != 0x00ff0000
130                          || p_vout->output.i_gmask != 0x0000ff00
131                          || p_vout->output.i_bmask != 0x000000ff )
132                     {
133                         return -1;
134                     }
135 #endif
136                     p_vout->chroma.pf_convert = E_(I420_RGB32);
137                     break;
138
139                 default:
140                     return -1;
141             }
142             break;
143
144         default:
145             return -1;
146     }
147     
148     p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
149     if( p_vout->chroma.p_sys == NULL )
150     {
151         return -1;
152     }
153
154     switch( p_vout->output.i_chroma )
155     {
156 #if defined (MODULE_NAME_IS_i420_rgb)
157         case VLC_FOURCC('R','G','B','2'):
158             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
159             break;
160 #endif
161
162         case VLC_FOURCC('R','V','1','5'):
163         case VLC_FOURCC('R','V','1','6'):
164             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
165             break;
166
167         case VLC_FOURCC('R','V','2','4'):
168         case VLC_FOURCC('R','V','3','2'):
169             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
170             break;
171
172         default:
173             p_vout->chroma.p_sys->p_buffer = NULL;
174             break;
175     }
176
177     if( p_vout->chroma.p_sys->p_buffer == NULL )
178     {
179         free( p_vout->chroma.p_sys );
180         return -1;
181     }
182
183     p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
184                     * ( ( p_vout->output.i_chroma
185                            == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
186                     * sizeof( int ) );
187     if( p_vout->chroma.p_sys->p_offset == NULL )
188     {
189         free( p_vout->chroma.p_sys->p_buffer );
190         free( p_vout->chroma.p_sys );
191         return -1;
192     }
193
194 #if defined (MODULE_NAME_IS_i420_rgb)
195     switch( p_vout->output.i_chroma )
196     {
197     case VLC_FOURCC('R','G','B','2'):
198         i_tables_size = sizeof( u8 ) * PALETTE_TABLE_SIZE;
199         break;
200     case VLC_FOURCC('R','V','1','5'):
201     case VLC_FOURCC('R','V','1','6'):
202         i_tables_size = sizeof( u16 ) * RGB_TABLE_SIZE;
203         break;
204     default: /* RV24, RV32 */
205         i_tables_size = sizeof( u32 ) * RGB_TABLE_SIZE;
206         break;
207     }
208
209     p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
210     if( p_vout->chroma.p_sys->p_base == NULL )
211     {
212         free( p_vout->chroma.p_sys->p_offset );
213         free( p_vout->chroma.p_sys->p_buffer );
214         free( p_vout->chroma.p_sys );
215         return -1;
216     }
217
218     SetYUV( p_vout );
219 #endif
220
221     return 0; 
222 }
223
224 /*****************************************************************************
225  * Deactivate: free the chroma function
226  *****************************************************************************
227  * This function frees the previously allocated chroma function
228  *****************************************************************************/
229 static void Deactivate( vlc_object_t *p_this )
230 {
231     vout_thread_t *p_vout = (vout_thread_t *)p_this;
232
233 #if defined (MODULE_NAME_IS_i420_rgb)
234     free( p_vout->chroma.p_sys->p_base );
235 #endif
236     free( p_vout->chroma.p_sys->p_offset );
237     free( p_vout->chroma.p_sys->p_buffer );
238     free( p_vout->chroma.p_sys );
239 }
240
241 #if defined (MODULE_NAME_IS_i420_rgb)
242 /*****************************************************************************
243  * SetGammaTable: return intensity table transformed by gamma curve.
244  *****************************************************************************
245  * pi_table is a table of 256 entries from 0 to 255.
246  *****************************************************************************/
247 static void SetGammaTable( int *pi_table, double f_gamma )
248 {
249     int i_y;                                               /* base intensity */
250
251     /* Use exp(gamma) instead of gamma */
252     f_gamma = exp( f_gamma );
253
254     /* Build gamma table */
255     for( i_y = 0; i_y < 256; i_y++ )
256     {
257         pi_table[ i_y ] = (int)( pow( (double)i_y / 256, f_gamma ) * 256 );
258     }
259 }
260
261 /*****************************************************************************
262  * SetYUV: compute tables and set function pointers
263  *****************************************************************************/
264 static void SetYUV( vout_thread_t *p_vout )
265 {
266     int         pi_gamma[256];                                /* gamma table */
267     int         i_index;                                  /* index in tables */
268
269     /* Build gamma table */
270     SetGammaTable( pi_gamma, p_vout->f_gamma );
271
272     /*
273      * Set pointers and build YUV tables
274      */
275
276     /* Color: build red, green and blue tables */
277     switch( p_vout->output.i_chroma )
278     {
279     case VLC_FOURCC('R','G','B','2'):
280         p_vout->chroma.p_sys->p_rgb8 = (u8 *)p_vout->chroma.p_sys->p_base;
281         Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
282         break;
283
284     case VLC_FOURCC('R','V','1','5'):
285     case VLC_FOURCC('R','V','1','6'):
286         p_vout->chroma.p_sys->p_rgb16 = (u16 *)p_vout->chroma.p_sys->p_base;
287         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
288         {
289             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
290             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
291         }
292         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
293         {
294             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
295             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
296         }
297         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
298         {
299             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
300             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
301         }
302         for( i_index = 0; i_index < 256; i_index++ )
303         {
304             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
305             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
306             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
307         }
308         break;
309
310     case VLC_FOURCC('R','V','2','4'):
311     case VLC_FOURCC('R','V','3','2'):
312         p_vout->chroma.p_sys->p_rgb32 = (u32 *)p_vout->chroma.p_sys->p_base;
313         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
314         {
315             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
316             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
317         }
318         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
319         {
320             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
321             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
322         }
323         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
324         {
325             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
326             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
327         }
328         for( i_index = 0; i_index < 256; i_index++ )
329         {
330             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
331             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
332             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
333         }
334         break;
335     }
336 }
337
338 static void Set8bppPalette( vout_thread_t *p_vout, u8 *p_rgb8 )
339 {
340     #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
341
342     int y,u,v;
343     int r,g,b;
344     int i = 0, j = 0;
345     u16 red[ 256 ], green[ 256 ], blue[ 256 ];
346     unsigned char p_lookup[PALETTE_TABLE_SIZE];
347
348     /* This loop calculates the intersection of an YUV box and the RGB cube. */
349     for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
350     {
351         for ( u = 0; u <= 256; u += 32 )
352         {
353             for ( v = 0; v <= 256; v += 32 )
354             {
355                 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
356                 g = y + ( (U_GREEN_COEF*(u-128)
357                          + V_GREEN_COEF*(v-128)) >> SHIFT );
358                 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
359
360                 if( r >= 0x00 && g >= 0x00 && b >= 0x00
361                         && r <= 0xff && g <= 0xff && b <= 0xff )
362                 {
363                     /* This one should never happen unless someone
364                      * fscked up my code */
365                     if( j == 256 )
366                     {
367                         msg_Err( p_vout, "no colors left in palette" );
368                         break;
369                     }
370
371                     /* Clip the colors */
372                     red[ j ] = CLIP( r );
373                     green[ j ] = CLIP( g );
374                     blue[ j ] = CLIP( b );
375
376                     /* Allocate color */
377                     p_lookup[ i ] = 1;
378                     p_rgb8[ i++ ] = j;
379                     j++;
380                 }
381                 else
382                 {
383                     p_lookup[ i ] = 0;
384                     p_rgb8[ i++ ] = 0;
385                 }
386             }
387         }
388     }
389
390     /* The colors have been allocated, we can set the palette */
391     p_vout->output.pf_setpalette( p_vout, red, green, blue );
392
393 #if 0
394     /* There will eventually be a way to know which colors
395      * couldn't be allocated and try to find a replacement */
396     p_vout->i_white_pixel = 0xff;
397     p_vout->i_black_pixel = 0x00;
398     p_vout->i_gray_pixel = 0x44;
399     p_vout->i_blue_pixel = 0x3b;
400 #endif
401
402     /* This loop allocates colors that got outside the RGB cube */
403     for ( i = 0, 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, i++ )
408             {
409                 int u2, v2, dist, mindist = 100000000;
410
411                 if( p_lookup[ i ] || y == 0 )
412                 {
413                     continue;
414                 }
415
416                 /* Heavy. yeah. */
417                 for( u2 = 0; u2 <= 256; u2 += 32 )
418                 {
419                     for( v2 = 0; v2 <= 256; v2 += 32 )
420                     {
421                         j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
422                         dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
423
424                         /* Find the nearest color */
425                         if( p_lookup[ j ] && dist < mindist )
426                         {
427                             p_rgb8[ i ] = p_rgb8[ j ];
428                             mindist = dist;
429                         }
430
431                         j -= 128;
432
433                         /* Find the nearest color */
434                         if( p_lookup[ j ] && dist + 128 < mindist )
435                         {
436                             p_rgb8[ i ] = p_rgb8[ j ];
437                             mindist = dist + 128;
438                         }
439                     }
440                 }
441             }
442         }
443     }
444 }
445
446 #endif
447