]> git.sesse.net Git - vlc/blob - modules/video_chroma/i420_rgb.c
* ./modules/*: moved plugins to the new tree. Yet untested builds include
[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.1 2002/08/04 17:23:43 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 <errno.h>                                                 /* ENOMEM */
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  * Local and extern prototypes.
42  *****************************************************************************/
43 static int  Activate   ( vlc_object_t * );
44 static void Deactivate ( vlc_object_t * );
45
46 #if defined (MODULE_NAME_IS_i420_rgb)
47 static void SetGammaTable       ( int *pi_table, double f_gamma );
48 static void SetYUV              ( vout_thread_t * );
49 static void Set8bppPalette      ( vout_thread_t *, u8 * );
50 #endif
51
52 /*****************************************************************************
53  * Module descriptor.
54  *****************************************************************************/
55 vlc_module_begin();
56 #if defined (MODULE_NAME_IS_i420_rgb)
57     set_description( _("I420,IYUV,YV12 to "
58                        "RGB,RV15,RV16,RV24,RV32 conversions") );
59     set_capability( "chroma", 80 );
60 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
61     set_description( _( "MMX I420,IYUV,YV12 to "
62                         "RV15,RV16,RV24,RV32 conversions") );
63     set_capability( "chroma", 100 );
64     add_requirement( MMX );
65 #endif
66     set_callbacks( Activate, Deactivate );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * Activate: allocate a chroma function
71  *****************************************************************************
72  * This function allocates and initializes a chroma function
73  *****************************************************************************/
74 static int Activate( vlc_object_t *p_this )
75 {
76     vout_thread_t *p_vout = (vout_thread_t *)p_this;
77 #if defined (MODULE_NAME_IS_i420_rgb)
78     size_t i_tables_size;
79 #endif
80
81     if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
82     {
83         return -1;
84     }
85
86     switch( p_vout->render.i_chroma )
87     {
88         case VLC_FOURCC('Y','V','1','2'):
89         case VLC_FOURCC('I','4','2','0'):
90         case VLC_FOURCC('I','Y','U','V'):
91             switch( p_vout->output.i_chroma )
92             {
93 #if defined (MODULE_NAME_IS_i420_rgb)
94                 case VLC_FOURCC('R','G','B','2'):
95                     p_vout->chroma.pf_convert = E_(I420_RGB8);
96                     break;
97 #endif
98                 case VLC_FOURCC('R','V','1','5'):
99                     p_vout->chroma.pf_convert = E_(I420_RGB15);
100                     break;
101
102                 case VLC_FOURCC('R','V','1','6'):
103                     p_vout->chroma.pf_convert = E_(I420_RGB16);
104                     break;
105
106                 case VLC_FOURCC('R','V','2','4'):
107                 case VLC_FOURCC('R','V','3','2'):
108                     p_vout->chroma.pf_convert = E_(I420_RGB32);
109                     break;
110
111                 default:
112                     return -1;
113             }
114             break;
115
116         default:
117             return -1;
118     }
119     
120     p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
121     if( p_vout->chroma.p_sys == NULL )
122     {
123         return -1;
124     }
125
126     switch( p_vout->output.i_chroma )
127     {
128 #if defined (MODULE_NAME_IS_i420_rgb)
129         case VLC_FOURCC('R','G','B','2'):
130             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
131             break;
132 #endif
133
134         case VLC_FOURCC('R','V','1','5'):
135         case VLC_FOURCC('R','V','1','6'):
136             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
137             break;
138
139         case VLC_FOURCC('R','V','2','4'):
140         case VLC_FOURCC('R','V','3','2'):
141             p_vout->chroma.p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
142             break;
143
144         default:
145             p_vout->chroma.p_sys->p_buffer = NULL;
146             break;
147     }
148
149     if( p_vout->chroma.p_sys->p_buffer == NULL )
150     {
151         free( p_vout->chroma.p_sys );
152         return -1;
153     }
154
155     p_vout->chroma.p_sys->p_offset = malloc( p_vout->output.i_width
156                     * ( ( p_vout->output.i_chroma
157                            == VLC_FOURCC('R','G','B','2') ) ? 2 : 1 )
158                     * sizeof( int ) );
159     if( p_vout->chroma.p_sys->p_offset == NULL )
160     {
161         free( p_vout->chroma.p_sys->p_buffer );
162         free( p_vout->chroma.p_sys );
163         return -1;
164     }
165
166 #if defined (MODULE_NAME_IS_i420_rgb)
167     switch( p_vout->output.i_chroma )
168     {
169     case VLC_FOURCC('R','G','B','2'):
170         i_tables_size = sizeof( u8 ) * PALETTE_TABLE_SIZE;
171         break;
172     case VLC_FOURCC('R','V','1','5'):
173     case VLC_FOURCC('R','V','1','6'):
174         i_tables_size = sizeof( u16 ) * RGB_TABLE_SIZE;
175         break;
176     default: /* RV24, RV32 */
177         i_tables_size = sizeof( u32 ) * RGB_TABLE_SIZE;
178         break;
179     }
180
181     p_vout->chroma.p_sys->p_base = malloc( i_tables_size );
182     if( p_vout->chroma.p_sys->p_base == NULL )
183     {
184         free( p_vout->chroma.p_sys->p_offset );
185         free( p_vout->chroma.p_sys->p_buffer );
186         free( p_vout->chroma.p_sys );
187         return -1;
188     }
189
190     SetYUV( p_vout );
191 #endif
192
193     return 0; 
194 }
195
196 /*****************************************************************************
197  * Deactivate: free the chroma function
198  *****************************************************************************
199  * This function frees the previously allocated chroma function
200  *****************************************************************************/
201 static void Deactivate( vlc_object_t *p_this )
202 {
203     vout_thread_t *p_vout = (vout_thread_t *)p_this;
204
205 #if defined (MODULE_NAME_IS_i420_rgb)
206     free( p_vout->chroma.p_sys->p_base );
207 #endif
208     free( p_vout->chroma.p_sys->p_offset );
209     free( p_vout->chroma.p_sys->p_buffer );
210     free( p_vout->chroma.p_sys );
211 }
212
213 #if defined (MODULE_NAME_IS_i420_rgb)
214 /*****************************************************************************
215  * SetGammaTable: return intensity table transformed by gamma curve.
216  *****************************************************************************
217  * pi_table is a table of 256 entries from 0 to 255.
218  *****************************************************************************/
219 static void SetGammaTable( int *pi_table, double f_gamma )
220 {
221     int i_y;                                               /* base intensity */
222
223     /* Use exp(gamma) instead of gamma */
224     f_gamma = exp( f_gamma );
225
226     /* Build gamma table */
227     for( i_y = 0; i_y < 256; i_y++ )
228     {
229         pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
230     }
231 }
232
233 /*****************************************************************************
234  * SetYUV: compute tables and set function pointers
235  *****************************************************************************/
236 static void SetYUV( vout_thread_t *p_vout )
237 {
238     int         pi_gamma[256];                                /* gamma table */
239     int         i_index;                                  /* index in tables */
240
241     /* Build gamma table */
242     SetGammaTable( pi_gamma, p_vout->f_gamma );
243
244     /*
245      * Set pointers and build YUV tables
246      */
247
248     /* Color: build red, green and blue tables */
249     switch( p_vout->output.i_chroma )
250     {
251     case VLC_FOURCC('R','G','B','2'):
252         p_vout->chroma.p_sys->p_rgb8 = (u8 *)p_vout->chroma.p_sys->p_base;
253         Set8bppPalette( p_vout, p_vout->chroma.p_sys->p_rgb8 );
254         break;
255
256     case VLC_FOURCC('R','V','1','5'):
257     case VLC_FOURCC('R','V','1','6'):
258         p_vout->chroma.p_sys->p_rgb16 = (u16 *)p_vout->chroma.p_sys->p_base;
259         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
260         {
261             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
262             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
263         }
264         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
265         {
266             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
267             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
268         }
269         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
270         {
271             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
272             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
273         }
274         for( i_index = 0; i_index < 256; i_index++ )
275         {
276             p_vout->chroma.p_sys->p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
277             p_vout->chroma.p_sys->p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
278             p_vout->chroma.p_sys->p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
279         }
280         break;
281
282     case VLC_FOURCC('R','V','2','4'):
283     case VLC_FOURCC('R','V','3','2'):
284         p_vout->chroma.p_sys->p_rgb32 = (u32 *)p_vout->chroma.p_sys->p_base;
285         for( i_index = 0; i_index < RED_MARGIN; i_index++ )
286         {
287             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
288             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
289         }
290         for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
291         {
292             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
293             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
294         }
295         for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
296         {
297             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
298             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
299         }
300         for( i_index = 0; i_index < 256; i_index++ )
301         {
302             p_vout->chroma.p_sys->p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
303             p_vout->chroma.p_sys->p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
304             p_vout->chroma.p_sys->p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
305         }
306         break;
307     }
308 }
309
310 static void Set8bppPalette( vout_thread_t *p_vout, u8 *p_rgb8 )
311 {
312     #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
313
314     int y,u,v;
315     int r,g,b;
316     int i = 0, j = 0;
317     u16 red[ 256 ], green[ 256 ], blue[ 256 ];
318     unsigned char p_lookup[PALETTE_TABLE_SIZE];
319
320     /* This loop calculates the intersection of an YUV box and the RGB cube. */
321     for ( y = 0; y <= 256; y += 16, i += 128 - 81 )
322     {
323         for ( u = 0; u <= 256; u += 32 )
324         {
325             for ( v = 0; v <= 256; v += 32 )
326             {
327                 r = y + ( (V_RED_COEF*(v-128)) >> SHIFT );
328                 g = y + ( (U_GREEN_COEF*(u-128)
329                          + V_GREEN_COEF*(v-128)) >> SHIFT );
330                 b = y + ( (U_BLUE_COEF*(u-128)) >> SHIFT );
331
332                 if( r >= 0x00 && g >= 0x00 && b >= 0x00
333                         && r <= 0xff && g <= 0xff && b <= 0xff )
334                 {
335                     /* This one should never happen unless someone
336                      * fscked up my code */
337                     if( j == 256 )
338                     {
339                         msg_Err( p_vout, "no colors left in palette" );
340                         break;
341                     }
342
343                     /* Clip the colors */
344                     red[ j ] = CLIP( r );
345                     green[ j ] = CLIP( g );
346                     blue[ j ] = CLIP( b );
347
348                     /* Allocate color */
349                     p_lookup[ i ] = 1;
350                     p_rgb8[ i++ ] = j;
351                     j++;
352                 }
353                 else
354                 {
355                     p_lookup[ i ] = 0;
356                     p_rgb8[ i++ ] = 0;
357                 }
358             }
359         }
360     }
361
362     /* The colors have been allocated, we can set the palette */
363     p_vout->output.pf_setpalette( p_vout, red, green, blue );
364
365 #if 0
366     /* There will eventually be a way to know which colors
367      * couldn't be allocated and try to find a replacement */
368     p_vout->i_white_pixel = 0xff;
369     p_vout->i_black_pixel = 0x00;
370     p_vout->i_gray_pixel = 0x44;
371     p_vout->i_blue_pixel = 0x3b;
372 #endif
373
374     /* This loop allocates colors that got outside the RGB cube */
375     for ( i = 0, y = 0; y <= 256; y += 16, i += 128 - 81 )
376     {
377         for ( u = 0; u <= 256; u += 32 )
378         {
379             for ( v = 0; v <= 256; v += 32, i++ )
380             {
381                 int u2, v2, dist, mindist = 100000000;
382
383                 if( p_lookup[ i ] || y == 0 )
384                 {
385                     continue;
386                 }
387
388                 /* Heavy. yeah. */
389                 for( u2 = 0; u2 <= 256; u2 += 32 )
390                 {
391                     for( v2 = 0; v2 <= 256; v2 += 32 )
392                     {
393                         j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
394                         dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
395
396                         /* Find the nearest color */
397                         if( p_lookup[ j ] && dist < mindist )
398                         {
399                             p_rgb8[ i ] = p_rgb8[ j ];
400                             mindist = dist;
401                         }
402
403                         j -= 128;
404
405                         /* Find the nearest color */
406                         if( p_lookup[ j ] && dist + 128 < mindist )
407                         {
408                             p_rgb8[ i ] = p_rgb8[ j ];
409                             mindist = dist + 128;
410                         }
411                     }
412                 }
413             }
414         }
415     }
416 }
417
418 #endif
419