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