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
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *****************************************************************************/
27 #define MODULE_NAME yuv
28 #include "modules_inner.h"
30 /*****************************************************************************
32 *****************************************************************************/
35 #include <math.h> /* exp(), pow() */
36 #include <errno.h> /* ENOMEM */
37 #include <stdlib.h> /* free() */
38 #include <string.h> /* strerror() */
48 #include "video_output.h"
50 #include "video_common.h"
54 static int yuv_Probe ( probedata_t *p_data );
55 static int yuv_Init ( vout_thread_t *p_vout );
56 static int yuv_Reset ( vout_thread_t *p_vout );
57 static void yuv_End ( vout_thread_t *p_vout );
59 static void SetGammaTable ( int *pi_table, double f_gamma );
60 static void SetYUV ( vout_thread_t *p_vout );
62 /*****************************************************************************
63 * Functions exported as capabilities. They are declared as static so that
64 * we don't pollute the namespace too much.
65 *****************************************************************************/
66 void _M( yuv_getfunctions )( function_list_t * p_function_list )
68 p_function_list->pf_probe = yuv_Probe;
69 p_function_list->functions.yuv.pf_init = yuv_Init;
70 p_function_list->functions.yuv.pf_reset = yuv_Reset;
71 p_function_list->functions.yuv.pf_end = yuv_End;
74 /*****************************************************************************
75 * yuv_Probe: tests probe the audio device and return a score
76 *****************************************************************************
77 * This function tries to open the DSP and returns a score to the plugin
78 * manager so that it can choose the most appropriate one.
79 *****************************************************************************/
80 static int yuv_Probe( probedata_t *p_data )
82 if( TestMethod( YUV_METHOD_VAR, "yuv" ) )
87 /* This module always works */
91 /*****************************************************************************
92 * yuv_Init: allocate and initialize translations tables
93 *****************************************************************************
94 * This function will allocate memory to store translation tables, depending
95 * of the screen depth.
96 *****************************************************************************/
97 static int yuv_Init( vout_thread_t *p_vout )
99 size_t tables_size; /* tables size, in bytes */
101 /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
102 switch( p_vout->i_bytes_per_pixel )
105 tables_size = sizeof( u8 )
106 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
109 tables_size = sizeof( u16 )
110 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
115 tables_size = sizeof( u32 )
116 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
120 /* Allocate memory */
121 p_vout->yuv.p_base = malloc( tables_size );
122 if( p_vout->yuv.p_base == NULL )
124 intf_ErrMsg("error: %s", strerror(ENOMEM));
128 /* Allocate memory for conversion buffer and offset array */
129 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
130 if( p_vout->yuv.p_buffer == NULL )
132 intf_ErrMsg("error: %s", strerror(ENOMEM));
133 free( p_vout->yuv.p_base );
137 /* In 8bpp we have a twice as big offset table because we also
138 * need the offsets for U and V (not only Y) */
139 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) *
140 ( ( p_vout->i_bytes_per_pixel == 1 ) ? 2 : 1 ) );
141 if( p_vout->yuv.p_offset == NULL )
143 intf_ErrMsg("error: %s", strerror(ENOMEM));
144 free( p_vout->yuv.p_base );
145 free( p_vout->yuv.p_buffer );
149 /* Initialize tables */
154 /*****************************************************************************
155 * yuv_End: destroy translations tables
156 *****************************************************************************
157 * Free memory allocated by yuv_CCreate.
158 *****************************************************************************/
159 static void yuv_End( vout_thread_t *p_vout )
161 free( p_vout->yuv.p_base );
162 free( p_vout->yuv.p_buffer );
163 free( p_vout->yuv.p_offset );
166 /*****************************************************************************
167 * yuv_Reset: re-initialize translations tables
168 *****************************************************************************
169 * This function will initialize the tables allocated by vout_CreateTables and
170 * set functions pointers.
171 *****************************************************************************/
172 static int yuv_Reset( vout_thread_t *p_vout )
175 return( yuv_Init( p_vout ) );
178 /*****************************************************************************
179 * SetGammaTable: return intensity table transformed by gamma curve.
180 *****************************************************************************
181 * pi_table is a table of 256 entries from 0 to 255.
182 *****************************************************************************/
183 static void SetGammaTable( int *pi_table, double f_gamma )
185 int i_y; /* base intensity */
187 /* Use exp(gamma) instead of gamma */
188 f_gamma = exp( f_gamma );
190 /* Build gamma table */
191 for( i_y = 0; i_y < 256; i_y++ )
193 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
197 /*****************************************************************************
198 * SetYUV: compute tables and set function pointers
199 *****************************************************************************/
200 static void SetYUV( vout_thread_t *p_vout )
202 int pi_gamma[256]; /* gamma table */
203 int i_index; /* index in tables */
205 /* Build gamma table */
206 SetGammaTable( pi_gamma, p_vout->f_gamma );
209 * Set pointers and build YUV tables
211 if( p_vout->b_grayscale )
213 /* Grayscale: build gray table */
214 switch( p_vout->i_bytes_per_pixel )
218 u16 bright[256], transp[256];
220 p_vout->yuv.yuv.p_gray8 = (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
221 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
223 p_vout->yuv.yuv.p_gray8[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
224 p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
226 for( i_index = 0; i_index < 256; i_index++)
228 p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
229 bright[ i_index ] = i_index << 8;
230 transp[ i_index ] = 0;
232 /* the colors have been allocated, we can set the palette */
233 p_vout->pf_setpalette( p_vout, bright, bright, bright, transp );
234 p_vout->i_white_pixel = 0xff;
235 p_vout->i_black_pixel = 0x00;
236 p_vout->i_gray_pixel = 0x44;
237 p_vout->i_blue_pixel = 0x3b;
242 p_vout->yuv.yuv.p_gray16 = (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
243 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
245 p_vout->yuv.yuv.p_gray16[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
246 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
248 for( i_index = 0; i_index < 256; i_index++)
250 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
255 p_vout->yuv.yuv.p_gray32 = (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
256 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
258 p_vout->yuv.yuv.p_gray32[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
259 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
261 for( i_index = 0; i_index < 256; i_index++)
263 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
270 /* Color: build red, green and blue tables */
271 switch( p_vout->i_bytes_per_pixel )
277 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
283 u16 red[256], green[256], blue[256], transp[256];
284 unsigned char lookup[PALETTE_TABLE_SIZE];
286 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
288 /* this loop calculates the intersection of an YUV box
289 * and the RGB cube. */
290 for ( y = 0; y <= 256; y += 16 )
292 for ( u = 0; u <= 256; u += 32 )
293 for ( v = 0; v <= 256; v += 32 )
295 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
296 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
297 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
302 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
303 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
305 /* this one should never happen unless someone fscked up my code */
306 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
308 /* clip the colors */
310 green[j] = CLIP( g );
316 p_vout->yuv.yuv.p_rgb8[i++] = j;
322 p_vout->yuv.yuv.p_rgb8[i++] = 0;
328 /* the colors have been allocated, we can set the palette */
329 /* there will eventually be a way to know which colors
330 * couldn't be allocated and try to find a replacement */
331 p_vout->pf_setpalette( p_vout, red, green, blue, transp );
333 p_vout->i_white_pixel = 0xff;
334 p_vout->i_black_pixel = 0x00;
335 p_vout->i_gray_pixel = 0x44;
336 p_vout->i_blue_pixel = 0x3b;
339 /* this loop allocates colors that got outside
341 for ( y = 0; y <= 256; y += 16 )
343 for ( u = 0; u <= 256; u += 32 )
344 for ( v = 0; v <= 256; v += 32 )
347 int dist, mindist = 100000000;
349 if( lookup[i] || y==0)
356 for( u2 = 0; u2 <= 256; u2 += 32 )
357 for( v2 = 0; v2 <= 256; v2 += 32 )
359 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
360 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
362 /* find the nearest color */
365 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
370 /* find the nearest color */
371 if( dist + 128 < mindist )
373 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
374 mindist = dist + 128;
385 p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
386 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
388 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
389 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
391 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
393 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
394 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
396 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
398 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
399 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
401 for( i_index = 0; i_index < 256; i_index++ )
403 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
404 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
405 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
410 p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
411 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
413 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
414 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
416 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
418 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
419 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
421 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
423 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
424 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
426 for( i_index = 0; i_index < 256; i_index++ )
428 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
429 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
430 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
437 * Set functions pointers
439 if( p_vout->b_grayscale )
442 switch( p_vout->i_bytes_per_pixel )
445 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
446 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
447 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
450 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
451 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
452 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
455 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
456 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
457 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
460 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
461 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
462 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
469 switch( p_vout->i_bytes_per_pixel )
472 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
473 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
474 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
477 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
478 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
479 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
482 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
483 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
484 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
487 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
488 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
489 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
495 /*****************************************************************************
496 * SetOffset: build offset array for conversion functions
497 *****************************************************************************
498 * This function will build an offset array used in later conversion functions.
499 * It will also set horizontal and vertical scaling indicators. If b_double
500 * is set, the p_offset structure has interleaved Y and U/V offsets.
501 *****************************************************************************/
502 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
503 boolean_t *pb_h_scaling, int *pi_v_scaling,
504 int *p_offset, boolean_t b_double )
506 int i_x; /* x position in destination */
507 int i_scale_count; /* modulo counter */
510 * Prepare horizontal offset array
512 if( i_pic_width - i_width == 0 )
514 /* No horizontal scaling: YUV conversion is done directly to picture */
517 else if( i_pic_width - i_width > 0 )
519 /* Prepare scaling array for horizontal extension */
521 i_scale_count = i_pic_width;
524 for( i_x = i_width; i_x--; )
526 while( (i_scale_count -= i_width) > 0 )
531 i_scale_count += i_pic_width;
537 for( i_x = i_width; i_x--; )
539 while( (i_scale_count -= i_width) > 0 )
545 *p_offset++ = i_dummy;
546 i_dummy = 1 - i_dummy;
547 i_scale_count += i_pic_width;
551 else /* if( i_pic_width - i_width < 0 ) */
553 /* Prepare scaling array for horizontal reduction */
555 i_scale_count = i_width;
558 for( i_x = i_pic_width; i_x--; )
561 while( (i_scale_count -= i_pic_width) > 0 )
566 i_scale_count += i_width;
573 for( i_x = i_pic_width; i_x--; )
576 while( (i_scale_count -= i_pic_width) > 0 )
580 *p_offset++ = i_jump;
581 *p_offset++ = ( i_jump += i_remainder ) >> 1;
582 i_remainder = i_jump & 1;
583 i_scale_count += i_width;
589 * Set vertical scaling indicator
591 if( i_pic_height - i_height == 0 )
595 else if( i_pic_height - i_height > 0 )
599 else /* if( i_pic_height - i_height < 0 ) */