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