]> git.sesse.net Git - vlc/blob - modules/video_chroma/i420_rgb.c
mediacodec: skip prerolled frames
[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 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 #ifdef PLAIN
42 # include "i420_rgb_c.h"
43 static picture_t *I420_RGB8_Filter( filter_t *, picture_t * );
44 static picture_t *I420_RGB16_Filter( filter_t *, picture_t * );
45 static picture_t *I420_RGB32_Filter( filter_t *, picture_t * );
46
47 static void SetGammaTable( int *pi_table, double f_gamma );
48 static void SetYUV( filter_t * );
49 static void Set8bppPalette( filter_t *, uint8_t * );
50 #else
51 static picture_t *I420_R5G5B5_Filter( filter_t *, picture_t * );
52 static picture_t *I420_R5G6B5_Filter( filter_t *, picture_t * );
53 static picture_t *I420_A8R8G8B8_Filter( filter_t *, picture_t * );
54 static picture_t *I420_R8G8B8A8_Filter( filter_t *, picture_t * );
55 static picture_t *I420_B8G8R8A8_Filter( filter_t *, picture_t * );
56 static picture_t *I420_A8B8G8R8_Filter( filter_t *, picture_t * );
57 #endif
58
59 /*****************************************************************************
60  * RGB2PIXEL: assemble RGB components to a pixel value, returns a uint32_t
61  *****************************************************************************/
62 #define RGB2PIXEL( p_filter, i_r, i_g, i_b )                 \
63     (((((uint32_t)i_r) >> p_filter->fmt_out.video.i_rrshift) \
64                        << p_filter->fmt_out.video.i_lrshift) \
65    | ((((uint32_t)i_g) >> p_filter->fmt_out.video.i_rgshift) \
66                        << p_filter->fmt_out.video.i_lgshift) \
67    | ((((uint32_t)i_b) >> p_filter->fmt_out.video.i_rbshift) \
68                        << p_filter->fmt_out.video.i_lbshift))
69
70 /*****************************************************************************
71  * Module descriptor.
72  *****************************************************************************/
73 static int  Activate   ( vlc_object_t * );
74 static void Deactivate ( vlc_object_t * );
75
76 vlc_module_begin ()
77 #if defined (SSE2)
78     set_description( N_( "SSE2 I420,IYUV,YV12 to "
79                         "RV15,RV16,RV24,RV32 conversions") )
80     set_capability( "video filter2", 120 )
81 # define vlc_CPU_capable() vlc_CPU_SSE2()
82 #elif defined (MMX)
83     set_description( N_( "MMX I420,IYUV,YV12 to "
84                         "RV15,RV16,RV24,RV32 conversions") )
85     set_capability( "video filter2", 100 )
86 # define vlc_CPU_capable() vlc_CPU_MMX()
87 #else
88     set_description( N_("I420,IYUV,YV12 to "
89                        "RGB2,RV15,RV16,RV24,RV32 conversions") )
90     set_capability( "video filter2", 80 )
91 # define vlc_CPU_capable() (true)
92 #endif
93     set_callbacks( Activate, Deactivate )
94 vlc_module_end ()
95
96 /*****************************************************************************
97  * Activate: allocate a chroma function
98  *****************************************************************************
99  * This function allocates and initializes a chroma function
100  *****************************************************************************/
101 static int Activate( vlc_object_t *p_this )
102 {
103     filter_t *p_filter = (filter_t *)p_this;
104 #ifdef PLAIN
105     size_t i_tables_size;
106 #endif
107
108     if( !vlc_CPU_capable() )
109         return VLC_EGENERIC;
110     if( p_filter->fmt_out.video.i_width & 1
111      || p_filter->fmt_out.video.i_height & 1 )
112     {
113         return VLC_EGENERIC;
114     }
115
116     if( p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
117     {
118         return VLC_EGENERIC;
119     }
120
121     switch( p_filter->fmt_in.video.i_chroma )
122     {
123         case VLC_CODEC_YV12:
124         case VLC_CODEC_I420:
125             switch( p_filter->fmt_out.video.i_chroma )
126             {
127 #ifndef PLAIN
128                 case VLC_CODEC_RGB15:
129                 case VLC_CODEC_RGB16:
130                     /* If we don't have support for the bitmasks, bail out */
131                     if( ( p_filter->fmt_out.video.i_rmask == 0x7c00
132                        && p_filter->fmt_out.video.i_gmask == 0x03e0
133                        && p_filter->fmt_out.video.i_bmask == 0x001f ) )
134                     {
135                         /* R5G5B6 pixel format */
136                         msg_Dbg(p_this, "RGB pixel format is R5G5B5");
137                         p_filter->pf_video_filter = I420_R5G5B5_Filter;
138                     }
139                     else if( ( p_filter->fmt_out.video.i_rmask == 0xf800
140                             && p_filter->fmt_out.video.i_gmask == 0x07e0
141                             && p_filter->fmt_out.video.i_bmask == 0x001f ) )
142                     {
143                         /* R5G6B5 pixel format */
144                         msg_Dbg(p_this, "RGB pixel format is R5G6B5");
145                         p_filter->pf_video_filter = I420_R5G6B5_Filter;
146                     }
147                     else
148                         return VLC_EGENERIC;
149                     break;
150                 case VLC_CODEC_RGB32:
151                     /* If we don't have support for the bitmasks, bail out */
152                     if( p_filter->fmt_out.video.i_rmask == 0x00ff0000
153                      && p_filter->fmt_out.video.i_gmask == 0x0000ff00
154                      && p_filter->fmt_out.video.i_bmask == 0x000000ff )
155                     {
156                         /* A8R8G8B8 pixel format */
157                         msg_Dbg(p_this, "RGB pixel format is A8R8G8B8");
158                         p_filter->pf_video_filter = I420_A8R8G8B8_Filter;
159                     }
160                     else if( p_filter->fmt_out.video.i_rmask == 0xff000000
161                           && p_filter->fmt_out.video.i_gmask == 0x00ff0000
162                           && p_filter->fmt_out.video.i_bmask == 0x0000ff00 )
163                     {
164                         /* R8G8B8A8 pixel format */
165                         msg_Dbg(p_this, "RGB pixel format is R8G8B8A8");
166                         p_filter->pf_video_filter = I420_R8G8B8A8_Filter;
167                     }
168                     else if( p_filter->fmt_out.video.i_rmask == 0x0000ff00
169                           && p_filter->fmt_out.video.i_gmask == 0x00ff0000
170                           && p_filter->fmt_out.video.i_bmask == 0xff000000 )
171                     {
172                         /* B8G8R8A8 pixel format */
173                         msg_Dbg(p_this, "RGB pixel format is B8G8R8A8");
174                         p_filter->pf_video_filter = I420_B8G8R8A8_Filter;
175                     }
176                     else if( p_filter->fmt_out.video.i_rmask == 0x000000ff
177                           && p_filter->fmt_out.video.i_gmask == 0x0000ff00
178                           && p_filter->fmt_out.video.i_bmask == 0x00ff0000 )
179                     {
180                         /* A8B8G8R8 pixel format */
181                         msg_Dbg(p_this, "RGB pixel format is A8B8G8R8");
182                         p_filter->pf_video_filter = I420_A8B8G8R8_Filter;
183                     }
184                     else
185                         return VLC_EGENERIC;
186                     break;
187 #else
188                 case VLC_CODEC_RGB8:
189                     p_filter->pf_video_filter = I420_RGB8_Filter;
190                     break;
191                 case VLC_CODEC_RGB15:
192                 case VLC_CODEC_RGB16:
193                     p_filter->pf_video_filter = I420_RGB16_Filter;
194                     break;
195                 case VLC_CODEC_RGB32:
196                     p_filter->pf_video_filter = I420_RGB32_Filter;
197                     break;
198 #endif
199                 default:
200                     return VLC_EGENERIC;
201             }
202             break;
203
204         default:
205             return VLC_EGENERIC;
206     }
207
208     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
209     if( p_filter->p_sys == NULL )
210     {
211         return VLC_EGENERIC;
212     }
213
214     switch( p_filter->fmt_out.video.i_chroma )
215     {
216 #ifdef PLAIN
217         case VLC_CODEC_RGB8:
218             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH );
219             break;
220 #endif
221         case VLC_CODEC_RGB15:
222         case VLC_CODEC_RGB16:
223             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 2 );
224             break;
225         case VLC_CODEC_RGB24:
226         case VLC_CODEC_RGB32:
227             p_filter->p_sys->p_buffer = malloc( VOUT_MAX_WIDTH * 4 );
228             break;
229         default:
230             p_filter->p_sys->p_buffer = NULL;
231             break;
232     }
233
234     if( p_filter->p_sys->p_buffer == NULL )
235     {
236         free( p_filter->p_sys );
237         return VLC_EGENERIC;
238     }
239
240     p_filter->p_sys->p_offset = malloc( p_filter->fmt_out.video.i_width
241                     * ( ( p_filter->fmt_out.video.i_chroma
242                            == VLC_CODEC_RGB8 ) ? 2 : 1 )
243                     * sizeof( int ) );
244     if( p_filter->p_sys->p_offset == NULL )
245     {
246         free( p_filter->p_sys->p_buffer );
247         free( p_filter->p_sys );
248         return VLC_EGENERIC;
249     }
250
251 #ifdef PLAIN
252     switch( p_filter->fmt_out.video.i_chroma )
253     {
254     case VLC_CODEC_RGB8:
255         i_tables_size = sizeof( uint8_t ) * PALETTE_TABLE_SIZE;
256         break;
257     case VLC_CODEC_RGB15:
258     case VLC_CODEC_RGB16:
259         i_tables_size = sizeof( uint16_t ) * RGB_TABLE_SIZE;
260         break;
261     default: /* RV24, RV32 */
262         i_tables_size = sizeof( uint32_t ) * RGB_TABLE_SIZE;
263         break;
264     }
265
266     p_filter->p_sys->p_base = malloc( i_tables_size );
267     if( p_filter->p_sys->p_base == NULL )
268     {
269         free( p_filter->p_sys->p_offset );
270         free( p_filter->p_sys->p_buffer );
271         free( p_filter->p_sys );
272         return -1;
273     }
274
275     SetYUV( p_filter );
276 #endif
277
278     return 0;
279 }
280
281 /*****************************************************************************
282  * Deactivate: free the chroma function
283  *****************************************************************************
284  * This function frees the previously allocated chroma function
285  *****************************************************************************/
286 static void Deactivate( vlc_object_t *p_this )
287 {
288     filter_t *p_filter = (filter_t *)p_this;
289
290 #ifdef PLAIN
291     free( p_filter->p_sys->p_base );
292 #endif
293     free( p_filter->p_sys->p_offset );
294     free( p_filter->p_sys->p_buffer );
295     free( p_filter->p_sys );
296 }
297
298 #ifndef PLAIN
299 VIDEO_FILTER_WRAPPER( I420_R5G5B5 )
300 VIDEO_FILTER_WRAPPER( I420_R5G6B5 )
301 VIDEO_FILTER_WRAPPER( I420_A8R8G8B8 )
302 VIDEO_FILTER_WRAPPER( I420_R8G8B8A8 )
303 VIDEO_FILTER_WRAPPER( I420_B8G8R8A8 )
304 VIDEO_FILTER_WRAPPER( I420_A8B8G8R8 )
305 #else
306 VIDEO_FILTER_WRAPPER( I420_RGB8 )
307 VIDEO_FILTER_WRAPPER( I420_RGB16 )
308 VIDEO_FILTER_WRAPPER( I420_RGB32 )
309
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 #endif