1 /*****************************************************************************
2 * video_yuv.c: YUV transformation functions
3 * Provides functions to perform the YUV conversion. The functions provided here
4 * are a complete and portable C implementation, and may be replaced in certain
5 * case by optimized functions.
6 *****************************************************************************
7 * Copyright (C) 1999, 2000 VideoLAN
8 * $Id: video_yuv.c,v 1.14 2001/06/03 12:47:21 sam Exp $
10 * Authors: Vincent Seguin <seguin@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public
23 * License along with this program; if not, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
26 *****************************************************************************/
28 #define MODULE_NAME yuv
29 #include "modules_inner.h"
31 /*****************************************************************************
33 *****************************************************************************/
36 #include <math.h> /* exp(), pow() */
37 #include <errno.h> /* ENOMEM */
38 #include <stdlib.h> /* free() */
39 #include <string.h> /* strerror() */
48 #include "video_output.h"
50 #include "video_common.h"
55 #include "modules_export.h"
57 static int yuv_Probe ( probedata_t *p_data );
58 static int yuv_Init ( vout_thread_t *p_vout );
59 static int yuv_Reset ( vout_thread_t *p_vout );
60 static void yuv_End ( vout_thread_t *p_vout );
62 static void SetGammaTable ( int *pi_table, double f_gamma );
63 static void SetYUV ( vout_thread_t *p_vout );
65 /*****************************************************************************
66 * Functions exported as capabilities. They are declared as static so that
67 * we don't pollute the namespace too much.
68 *****************************************************************************/
69 void _M( yuv_getfunctions )( function_list_t * p_function_list )
71 p_function_list->pf_probe = yuv_Probe;
72 p_function_list->functions.yuv.pf_init = yuv_Init;
73 p_function_list->functions.yuv.pf_reset = yuv_Reset;
74 p_function_list->functions.yuv.pf_end = yuv_End;
77 /*****************************************************************************
78 * yuv_Probe: tests probe the audio device and return a score
79 *****************************************************************************
80 * This function tries to open the DSP and returns a score to the plugin
81 * manager so that it can choose the most appropriate one.
82 *****************************************************************************/
83 static int yuv_Probe( probedata_t *p_data )
85 if( TestMethod( YUV_METHOD_VAR, "yuv" ) )
90 /* This module always works */
94 /*****************************************************************************
95 * yuv_Init: allocate and initialize translations tables
96 *****************************************************************************
97 * This function will allocate memory to store translation tables, depending
98 * of the screen depth.
99 *****************************************************************************/
100 static int yuv_Init( vout_thread_t *p_vout )
102 size_t tables_size; /* tables size, in bytes */
104 /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
105 switch( p_vout->i_bytes_per_pixel )
108 tables_size = sizeof( u8 )
109 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
112 tables_size = sizeof( u16 )
113 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
118 tables_size = sizeof( u32 )
119 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
123 /* Allocate memory */
124 p_vout->yuv.p_base = malloc( tables_size );
125 if( p_vout->yuv.p_base == NULL )
127 intf_ErrMsg("error: %s", strerror(ENOMEM));
131 /* Allocate memory for conversion buffer and offset array */
132 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
133 if( p_vout->yuv.p_buffer == NULL )
135 intf_ErrMsg("error: %s", strerror(ENOMEM));
136 free( p_vout->yuv.p_base );
140 /* In 8bpp we have a twice as big offset table because we also
141 * need the offsets for U and V (not only Y) */
142 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) *
143 ( ( p_vout->i_bytes_per_pixel == 1 ) ? 2 : 1 ) );
144 if( p_vout->yuv.p_offset == NULL )
146 intf_ErrMsg("error: %s", strerror(ENOMEM));
147 free( p_vout->yuv.p_base );
148 free( p_vout->yuv.p_buffer );
152 /* Initialize tables */
157 /*****************************************************************************
158 * yuv_End: destroy translations tables
159 *****************************************************************************
160 * Free memory allocated by yuv_CCreate.
161 *****************************************************************************/
162 static void yuv_End( vout_thread_t *p_vout )
164 free( p_vout->yuv.p_base );
165 free( p_vout->yuv.p_buffer );
166 free( p_vout->yuv.p_offset );
169 /*****************************************************************************
170 * yuv_Reset: re-initialize translations tables
171 *****************************************************************************
172 * This function will initialize the tables allocated by vout_CreateTables and
173 * set functions pointers.
174 *****************************************************************************/
175 static int yuv_Reset( vout_thread_t *p_vout )
178 return( yuv_Init( p_vout ) );
181 /*****************************************************************************
182 * SetGammaTable: return intensity table transformed by gamma curve.
183 *****************************************************************************
184 * pi_table is a table of 256 entries from 0 to 255.
185 *****************************************************************************/
186 static void SetGammaTable( int *pi_table, double f_gamma )
188 int i_y; /* base intensity */
190 /* Use exp(gamma) instead of gamma */
191 f_gamma = exp( f_gamma );
193 /* Build gamma table */
194 for( i_y = 0; i_y < 256; i_y++ )
196 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
200 /*****************************************************************************
201 * SetYUV: compute tables and set function pointers
202 *****************************************************************************/
203 static void SetYUV( vout_thread_t *p_vout )
205 int pi_gamma[256]; /* gamma table */
206 int i_index; /* index in tables */
208 /* Build gamma table */
209 SetGammaTable( pi_gamma, p_vout->f_gamma );
212 * Set pointers and build YUV tables
214 if( p_vout->b_grayscale )
216 /* Grayscale: build gray table */
217 switch( p_vout->i_bytes_per_pixel )
221 u16 bright[256], transp[256];
223 p_vout->yuv.yuv.p_gray8 = (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
224 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
226 p_vout->yuv.yuv.p_gray8[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
227 p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
229 for( i_index = 0; i_index < 256; i_index++)
231 p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
232 bright[ i_index ] = i_index << 8;
233 transp[ i_index ] = 0;
235 /* the colors have been allocated, we can set the palette */
236 p_vout->pf_setpalette( p_vout, bright, bright, bright, transp );
237 p_vout->i_white_pixel = 0xff;
238 p_vout->i_black_pixel = 0x00;
239 p_vout->i_gray_pixel = 0x44;
240 p_vout->i_blue_pixel = 0x3b;
245 p_vout->yuv.yuv.p_gray16 = (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
246 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
248 p_vout->yuv.yuv.p_gray16[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
249 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
251 for( i_index = 0; i_index < 256; i_index++)
253 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
258 p_vout->yuv.yuv.p_gray32 = (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
259 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
261 p_vout->yuv.yuv.p_gray32[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
262 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
264 for( i_index = 0; i_index < 256; i_index++)
266 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
273 /* Color: build red, green and blue tables */
274 switch( p_vout->i_bytes_per_pixel )
280 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
286 u16 red[256], green[256], blue[256], transp[256];
287 unsigned char lookup[PALETTE_TABLE_SIZE];
289 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
291 /* this loop calculates the intersection of an YUV box
292 * and the RGB cube. */
293 for ( y = 0; y <= 256; y += 16 )
295 for ( u = 0; u <= 256; u += 32 )
296 for ( v = 0; v <= 256; v += 32 )
298 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
299 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
300 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
305 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
306 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
308 /* this one should never happen unless someone fscked up my code */
309 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
311 /* clip the colors */
313 green[j] = CLIP( g );
319 p_vout->yuv.yuv.p_rgb8[i++] = j;
325 p_vout->yuv.yuv.p_rgb8[i++] = 0;
331 /* the colors have been allocated, we can set the palette */
332 /* there will eventually be a way to know which colors
333 * couldn't be allocated and try to find a replacement */
334 p_vout->pf_setpalette( p_vout, red, green, blue, transp );
336 p_vout->i_white_pixel = 0xff;
337 p_vout->i_black_pixel = 0x00;
338 p_vout->i_gray_pixel = 0x44;
339 p_vout->i_blue_pixel = 0x3b;
342 /* this loop allocates colors that got outside
344 for ( y = 0; y <= 256; y += 16 )
346 for ( u = 0; u <= 256; u += 32 )
347 for ( v = 0; v <= 256; v += 32 )
350 int dist, mindist = 100000000;
352 if( lookup[i] || y==0)
359 for( u2 = 0; u2 <= 256; u2 += 32 )
360 for( v2 = 0; v2 <= 256; v2 += 32 )
362 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
363 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
365 /* find the nearest color */
368 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
373 /* find the nearest color */
374 if( dist + 128 < mindist )
376 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
377 mindist = dist + 128;
388 p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
389 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
391 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
392 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
394 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
396 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
397 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
399 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
401 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
402 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
404 for( i_index = 0; i_index < 256; i_index++ )
406 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
407 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
408 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
413 p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
414 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
416 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
417 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
419 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
421 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
422 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
424 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
426 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
427 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
429 for( i_index = 0; i_index < 256; i_index++ )
431 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
432 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
433 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
440 * Set functions pointers
444 switch( p_vout->i_bytes_per_pixel)
446 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
448 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr8 );
449 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr8 );
450 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr8 );
454 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr16 );
455 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr16 );
456 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr16 );
460 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr24 );
461 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr24 );
462 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr24 );
466 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr32 );
467 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr32 );
468 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr32 );
473 else if( p_vout->b_grayscale )
476 switch( p_vout->i_bytes_per_pixel )
478 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
480 p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray8 );
481 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray8 );
482 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray8 );
485 p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray16 );
486 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray16 );
487 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray16 );
490 p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray24 );
491 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray24 );
492 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray24 );
495 p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray32 );
496 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray32 );
497 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray32 );
505 switch( p_vout->i_bytes_per_pixel )
507 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
509 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB8 );
510 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB8 );
511 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB8 );
514 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB16 );
515 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB16 );
516 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB16 );
519 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB24 );
520 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB24 );
521 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB24 );
524 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB32 );
525 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB32 );
526 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB32 );
534 /*****************************************************************************
535 * SetOffset: build offset array for conversion functions
536 *****************************************************************************
537 * This function will build an offset array used in later conversion functions.
538 * It will also set horizontal and vertical scaling indicators. If b_double
539 * is set, the p_offset structure has interleaved Y and U/V offsets.
540 *****************************************************************************/
541 void _M( SetOffset )( int i_width, int i_height, int i_pic_width,
542 int i_pic_height, boolean_t *pb_h_scaling,
543 int *pi_v_scaling, int *p_offset, boolean_t b_double )
545 int i_x; /* x position in destination */
546 int i_scale_count; /* modulo counter */
549 * Prepare horizontal offset array
551 if( i_pic_width - i_width == 0 )
553 /* No horizontal scaling: YUV conversion is done directly to picture */
556 else if( i_pic_width - i_width > 0 )
558 /* Prepare scaling array for horizontal extension */
560 i_scale_count = i_pic_width;
563 for( i_x = i_width; i_x--; )
565 while( (i_scale_count -= i_width) > 0 )
570 i_scale_count += i_pic_width;
576 for( i_x = i_width; i_x--; )
578 while( (i_scale_count -= i_width) > 0 )
584 *p_offset++ = i_dummy;
585 i_dummy = 1 - i_dummy;
586 i_scale_count += i_pic_width;
590 else /* if( i_pic_width - i_width < 0 ) */
592 /* Prepare scaling array for horizontal reduction */
594 i_scale_count = i_width;
597 for( i_x = i_pic_width; i_x--; )
600 while( (i_scale_count -= i_pic_width) > 0 )
605 i_scale_count += i_width;
612 for( i_x = i_pic_width; i_x--; )
615 while( (i_scale_count -= i_pic_width) > 0 )
619 *p_offset++ = i_jump;
620 *p_offset++ = ( i_jump += i_remainder ) >> 1;
621 i_remainder = i_jump & 1;
622 i_scale_count += i_width;
628 * Set vertical scaling indicator
630 if( i_pic_height - i_height == 0 )
634 else if( i_pic_height - i_height > 0 )
638 else /* if( i_pic_height - i_height < 0 ) */