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 /*****************************************************************************
29 *****************************************************************************/
32 #include <math.h> /* exp(), pow() */
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* free() */
35 #include <string.h> /* strerror() */
44 #include "video_output.h"
46 #include "video_common.h"
50 static int yuv_Probe ( probedata_t *p_data );
51 static int yuv_Init ( vout_thread_t *p_vout );
52 static int yuv_Reset ( vout_thread_t *p_vout );
53 static void yuv_End ( vout_thread_t *p_vout );
55 static void SetGammaTable ( int *pi_table, double f_gamma );
56 static void SetYUV ( vout_thread_t *p_vout );
58 /*****************************************************************************
59 * Functions exported as capabilities. They are declared as static so that
60 * we don't pollute the namespace too much.
61 *****************************************************************************/
62 void yuv_getfunctions( function_list_t * p_function_list )
64 p_function_list->pf_probe = yuv_Probe;
65 p_function_list->functions.yuv.pf_init = yuv_Init;
66 p_function_list->functions.yuv.pf_reset = yuv_Reset;
67 p_function_list->functions.yuv.pf_end = yuv_End;
70 /*****************************************************************************
71 * yuv_Probe: tests probe the audio device and return a score
72 *****************************************************************************
73 * This function tries to open the DSP and returns a score to the plugin
74 * manager so that it can choose the most appropriate one.
75 *****************************************************************************/
76 static int yuv_Probe( probedata_t *p_data )
78 /* This module always works */
82 /*****************************************************************************
83 * yuv_Init: allocate and initialize translations tables
84 *****************************************************************************
85 * This function will allocate memory to store translation tables, depending
86 * of the screen depth.
87 *****************************************************************************/
88 static int yuv_Init( vout_thread_t *p_vout )
90 size_t tables_size; /* tables size, in bytes */
92 /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
93 switch( p_vout->i_bytes_per_pixel )
96 tables_size = sizeof( u8 )
97 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
100 tables_size = sizeof( u16 )
101 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
106 tables_size = sizeof( u32 )
107 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
111 /* Allocate memory */
112 p_vout->yuv.p_base = malloc( tables_size );
113 if( p_vout->yuv.p_base == NULL )
115 intf_ErrMsg("error: %s", strerror(ENOMEM));
119 /* Allocate memory for conversion buffer and offset array */
120 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
121 if( p_vout->yuv.p_buffer == NULL )
123 intf_ErrMsg("error: %s", strerror(ENOMEM));
124 free( p_vout->yuv.p_base );
128 /* In 8bpp we have a twice as big offset table because we also
129 * need the offsets for U and V (not only Y) */
130 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) *
131 ( ( p_vout->i_bytes_per_pixel == 1 ) ? 2 : 1 ) );
132 if( p_vout->yuv.p_offset == NULL )
134 intf_ErrMsg("error: %s", strerror(ENOMEM));
135 free( p_vout->yuv.p_base );
136 free( p_vout->yuv.p_buffer );
140 /* Initialize tables */
145 /*****************************************************************************
146 * yuv_End: destroy translations tables
147 *****************************************************************************
148 * Free memory allocated by yuv_CCreate.
149 *****************************************************************************/
150 static void yuv_End( vout_thread_t *p_vout )
152 free( p_vout->yuv.p_base );
153 free( p_vout->yuv.p_buffer );
154 free( p_vout->yuv.p_offset );
157 /*****************************************************************************
158 * yuv_Reset: re-initialize translations tables
159 *****************************************************************************
160 * This function will initialize the tables allocated by vout_CreateTables and
161 * set functions pointers.
162 *****************************************************************************/
163 static int yuv_Reset( vout_thread_t *p_vout )
166 return( yuv_Init( p_vout ) );
169 /*****************************************************************************
170 * SetGammaTable: return intensity table transformed by gamma curve.
171 *****************************************************************************
172 * pi_table is a table of 256 entries from 0 to 255.
173 *****************************************************************************/
174 static void SetGammaTable( int *pi_table, double f_gamma )
176 int i_y; /* base intensity */
178 /* Use exp(gamma) instead of gamma */
179 f_gamma = exp( f_gamma );
181 /* Build gamma table */
182 for( i_y = 0; i_y < 256; i_y++ )
184 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
188 /*****************************************************************************
189 * SetYUV: compute tables and set function pointers
190 *****************************************************************************/
191 static void SetYUV( vout_thread_t *p_vout )
193 int pi_gamma[256]; /* gamma table */
194 int i_index; /* index in tables */
196 /* Build gamma table */
197 SetGammaTable( pi_gamma, p_vout->f_gamma );
200 * Set pointers and build YUV tables
202 if( p_vout->b_grayscale )
204 /* Grayscale: build gray table */
205 switch( p_vout->i_bytes_per_pixel )
209 u16 bright[256], transp[256];
211 p_vout->yuv.yuv.p_gray8 = (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
212 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
214 p_vout->yuv.yuv.p_gray8[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
215 p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
217 for( i_index = 0; i_index < 256; i_index++)
219 p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
220 bright[ i_index ] = i_index << 8;
221 transp[ i_index ] = 0;
223 /* the colors have been allocated, we can set the palette */
224 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
225 p_vout->i_white_pixel = 0xff;
226 p_vout->i_black_pixel = 0x00;
227 p_vout->i_gray_pixel = 0x44;
228 p_vout->i_blue_pixel = 0x3b;
233 p_vout->yuv.yuv.p_gray16 = (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
234 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
236 p_vout->yuv.yuv.p_gray16[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
237 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
239 for( i_index = 0; i_index < 256; i_index++)
241 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
246 p_vout->yuv.yuv.p_gray32 = (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
247 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
249 p_vout->yuv.yuv.p_gray32[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
250 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
252 for( i_index = 0; i_index < 256; i_index++)
254 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
261 /* Color: build red, green and blue tables */
262 switch( p_vout->i_bytes_per_pixel )
268 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
274 u16 red[256], green[256], blue[256], transp[256];
275 unsigned char lookup[PALETTE_TABLE_SIZE];
277 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
279 /* this loop calculates the intersection of an YUV box
280 * and the RGB cube. */
281 for ( y = 0; y <= 256; y += 16 )
283 for ( u = 0; u <= 256; u += 32 )
284 for ( v = 0; v <= 256; v += 32 )
286 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
287 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
288 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
293 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
294 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
296 /* this one should never happen unless someone fscked up my code */
297 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
299 /* clip the colors */
301 green[j] = CLIP( g );
307 p_vout->yuv.yuv.p_rgb8[i++] = j;
313 p_vout->yuv.yuv.p_rgb8[i++] = 0;
319 /* the colors have been allocated, we can set the palette */
320 /* there will eventually be a way to know which colors
321 * couldn't be allocated and try to find a replacement */
322 p_vout->p_set_palette( p_vout, red, green, blue, transp );
324 p_vout->i_white_pixel = 0xff;
325 p_vout->i_black_pixel = 0x00;
326 p_vout->i_gray_pixel = 0x44;
327 p_vout->i_blue_pixel = 0x3b;
330 /* this loop allocates colors that got outside
332 for ( y = 0; y <= 256; y += 16 )
334 for ( u = 0; u <= 256; u += 32 )
335 for ( v = 0; v <= 256; v += 32 )
338 int dist, mindist = 100000000;
340 if( lookup[i] || y==0)
347 for( u2 = 0; u2 <= 256; u2 += 32 )
348 for( v2 = 0; v2 <= 256; v2 += 32 )
350 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
351 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
353 /* find the nearest color */
356 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
361 /* find the nearest color */
362 if( dist + 128 < mindist )
364 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
365 mindist = dist + 128;
376 p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
377 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
379 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
380 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
382 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
384 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
385 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
387 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
389 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
390 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
392 for( i_index = 0; i_index < 256; i_index++ )
394 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
395 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
396 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
401 p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
402 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
404 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
405 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
407 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
409 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
410 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
412 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
414 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
415 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
417 for( i_index = 0; i_index < 256; i_index++ )
419 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
420 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
421 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
428 * Set functions pointers
430 if( p_vout->b_grayscale )
433 switch( p_vout->i_bytes_per_pixel )
436 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
437 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
438 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
441 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
442 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
443 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
446 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
447 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
448 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
451 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
452 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
453 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
460 switch( p_vout->i_bytes_per_pixel )
463 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
464 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
465 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
468 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
469 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
470 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
473 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
474 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
475 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
478 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
479 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
480 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
486 /*****************************************************************************
487 * SetOffset: build offset array for conversion functions
488 *****************************************************************************
489 * This function will build an offset array used in later conversion functions.
490 * It will also set horizontal and vertical scaling indicators. If b_double
491 * is set, the p_offset structure has interleaved Y and U/V offsets.
492 *****************************************************************************/
493 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
494 boolean_t *pb_h_scaling, int *pi_v_scaling,
495 int *p_offset, boolean_t b_double )
497 int i_x; /* x position in destination */
498 int i_scale_count; /* modulo counter */
501 * Prepare horizontal offset array
503 if( i_pic_width - i_width == 0 )
505 /* No horizontal scaling: YUV conversion is done directly to picture */
508 else if( i_pic_width - i_width > 0 )
510 /* Prepare scaling array for horizontal extension */
512 i_scale_count = i_pic_width;
515 for( i_x = i_width; i_x--; )
517 while( (i_scale_count -= i_width) > 0 )
522 i_scale_count += i_pic_width;
528 for( i_x = i_width; i_x--; )
530 while( (i_scale_count -= i_width) > 0 )
536 *p_offset++ = i_dummy;
537 i_dummy = 1 - i_dummy;
538 i_scale_count += i_pic_width;
542 else /* if( i_pic_width - i_width < 0 ) */
544 /* Prepare scaling array for horizontal reduction */
546 i_scale_count = i_width;
549 for( i_x = i_pic_width; i_x--; )
552 while( (i_scale_count -= i_pic_width) > 0 )
557 i_scale_count += i_width;
564 for( i_x = i_pic_width; i_x--; )
567 while( (i_scale_count -= i_pic_width) > 0 )
571 *p_offset++ = i_jump;
572 *p_offset++ = ( i_jump += i_remainder ) >> 1;
573 i_remainder = i_jump & 1;
574 i_scale_count += i_width;
580 * Set vertical scaling indicator
582 if( i_pic_height - i_height == 0 )
586 else if( i_pic_height - i_height > 0 )
590 else /* if( i_pic_height - i_height < 0 ) */