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