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