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