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() */
45 #include "video_output.h"
47 #include "video_common.h"
51 static int yuv_Probe ( probedata_t *p_data );
52 static int yuv_Init ( vout_thread_t *p_vout );
53 static int yuv_Reset ( vout_thread_t *p_vout );
54 static void yuv_End ( vout_thread_t *p_vout );
56 static void SetGammaTable ( int *pi_table, double f_gamma );
57 static void SetYUV ( vout_thread_t *p_vout );
59 /*****************************************************************************
60 * Functions exported as capabilities. They are declared as static so that
61 * we don't pollute the namespace too much.
62 *****************************************************************************/
63 void yuv_getfunctions( function_list_t * p_function_list )
65 p_function_list->pf_probe = yuv_Probe;
66 p_function_list->functions.yuv.pf_init = yuv_Init;
67 p_function_list->functions.yuv.pf_reset = yuv_Reset;
68 p_function_list->functions.yuv.pf_end = yuv_End;
71 /*****************************************************************************
72 * yuv_Probe: tests probe the audio device and return a score
73 *****************************************************************************
74 * This function tries to open the DSP and returns a score to the plugin
75 * manager so that it can choose the most appropriate one.
76 *****************************************************************************/
77 static int yuv_Probe( probedata_t *p_data )
79 if( TestMethod( YUV_METHOD_VAR, "yuv" ) )
84 /* This module always works */
88 /*****************************************************************************
89 * yuv_Init: allocate and initialize translations tables
90 *****************************************************************************
91 * This function will allocate memory to store translation tables, depending
92 * of the screen depth.
93 *****************************************************************************/
94 static int yuv_Init( vout_thread_t *p_vout )
96 size_t tables_size; /* tables size, in bytes */
98 /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
99 switch( p_vout->i_bytes_per_pixel )
102 tables_size = sizeof( u8 )
103 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
106 tables_size = sizeof( u16 )
107 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
112 tables_size = sizeof( u32 )
113 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
117 /* Allocate memory */
118 p_vout->yuv.p_base = malloc( tables_size );
119 if( p_vout->yuv.p_base == NULL )
121 intf_ErrMsg("error: %s", strerror(ENOMEM));
125 /* Allocate memory for conversion buffer and offset array */
126 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
127 if( p_vout->yuv.p_buffer == NULL )
129 intf_ErrMsg("error: %s", strerror(ENOMEM));
130 free( p_vout->yuv.p_base );
134 /* In 8bpp we have a twice as big offset table because we also
135 * need the offsets for U and V (not only Y) */
136 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) *
137 ( ( p_vout->i_bytes_per_pixel == 1 ) ? 2 : 1 ) );
138 if( p_vout->yuv.p_offset == NULL )
140 intf_ErrMsg("error: %s", strerror(ENOMEM));
141 free( p_vout->yuv.p_base );
142 free( p_vout->yuv.p_buffer );
146 /* Initialize tables */
151 /*****************************************************************************
152 * yuv_End: destroy translations tables
153 *****************************************************************************
154 * Free memory allocated by yuv_CCreate.
155 *****************************************************************************/
156 static void yuv_End( vout_thread_t *p_vout )
158 free( p_vout->yuv.p_base );
159 free( p_vout->yuv.p_buffer );
160 free( p_vout->yuv.p_offset );
163 /*****************************************************************************
164 * yuv_Reset: re-initialize translations tables
165 *****************************************************************************
166 * This function will initialize the tables allocated by vout_CreateTables and
167 * set functions pointers.
168 *****************************************************************************/
169 static int yuv_Reset( vout_thread_t *p_vout )
172 return( yuv_Init( p_vout ) );
175 /*****************************************************************************
176 * SetGammaTable: return intensity table transformed by gamma curve.
177 *****************************************************************************
178 * pi_table is a table of 256 entries from 0 to 255.
179 *****************************************************************************/
180 static void SetGammaTable( int *pi_table, double f_gamma )
182 int i_y; /* base intensity */
184 /* Use exp(gamma) instead of gamma */
185 f_gamma = exp( f_gamma );
187 /* Build gamma table */
188 for( i_y = 0; i_y < 256; i_y++ )
190 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
194 /*****************************************************************************
195 * SetYUV: compute tables and set function pointers
196 *****************************************************************************/
197 static void SetYUV( vout_thread_t *p_vout )
199 int pi_gamma[256]; /* gamma table */
200 int i_index; /* index in tables */
202 /* Build gamma table */
203 SetGammaTable( pi_gamma, p_vout->f_gamma );
206 * Set pointers and build YUV tables
208 if( p_vout->b_grayscale )
210 /* Grayscale: build gray table */
211 switch( p_vout->i_bytes_per_pixel )
215 u16 bright[256], transp[256];
217 p_vout->yuv.yuv.p_gray8 = (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
218 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
220 p_vout->yuv.yuv.p_gray8[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
221 p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
223 for( i_index = 0; i_index < 256; i_index++)
225 p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
226 bright[ i_index ] = i_index << 8;
227 transp[ i_index ] = 0;
229 /* the colors have been allocated, we can set the palette */
230 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
231 p_vout->i_white_pixel = 0xff;
232 p_vout->i_black_pixel = 0x00;
233 p_vout->i_gray_pixel = 0x44;
234 p_vout->i_blue_pixel = 0x3b;
239 p_vout->yuv.yuv.p_gray16 = (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
240 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
242 p_vout->yuv.yuv.p_gray16[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
243 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
245 for( i_index = 0; i_index < 256; i_index++)
247 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
252 p_vout->yuv.yuv.p_gray32 = (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
253 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
255 p_vout->yuv.yuv.p_gray32[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
256 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
258 for( i_index = 0; i_index < 256; i_index++)
260 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
267 /* Color: build red, green and blue tables */
268 switch( p_vout->i_bytes_per_pixel )
274 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
280 u16 red[256], green[256], blue[256], transp[256];
281 unsigned char lookup[PALETTE_TABLE_SIZE];
283 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
285 /* this loop calculates the intersection of an YUV box
286 * and the RGB cube. */
287 for ( y = 0; y <= 256; y += 16 )
289 for ( u = 0; u <= 256; u += 32 )
290 for ( v = 0; v <= 256; v += 32 )
292 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
293 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
294 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
299 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
300 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
302 /* this one should never happen unless someone fscked up my code */
303 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
305 /* clip the colors */
307 green[j] = CLIP( g );
313 p_vout->yuv.yuv.p_rgb8[i++] = j;
319 p_vout->yuv.yuv.p_rgb8[i++] = 0;
325 /* the colors have been allocated, we can set the palette */
326 /* there will eventually be a way to know which colors
327 * couldn't be allocated and try to find a replacement */
328 p_vout->p_set_palette( p_vout, red, green, blue, transp );
330 p_vout->i_white_pixel = 0xff;
331 p_vout->i_black_pixel = 0x00;
332 p_vout->i_gray_pixel = 0x44;
333 p_vout->i_blue_pixel = 0x3b;
336 /* this loop allocates colors that got outside
338 for ( y = 0; y <= 256; y += 16 )
340 for ( u = 0; u <= 256; u += 32 )
341 for ( v = 0; v <= 256; v += 32 )
344 int dist, mindist = 100000000;
346 if( lookup[i] || y==0)
353 for( u2 = 0; u2 <= 256; u2 += 32 )
354 for( v2 = 0; v2 <= 256; v2 += 32 )
356 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
357 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
359 /* find the nearest color */
362 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
367 /* find the nearest color */
368 if( dist + 128 < mindist )
370 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
371 mindist = dist + 128;
382 p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
383 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
385 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
386 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
388 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
390 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
391 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
393 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
395 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
396 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
398 for( i_index = 0; i_index < 256; i_index++ )
400 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
401 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
402 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
407 p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
408 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
410 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
411 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
413 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
415 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
416 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
418 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
420 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
421 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
423 for( i_index = 0; i_index < 256; i_index++ )
425 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
426 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
427 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
434 * Set functions pointers
436 if( p_vout->b_grayscale )
439 switch( p_vout->i_bytes_per_pixel )
442 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
443 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
444 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
447 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
448 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
449 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
452 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
453 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
454 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
457 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
458 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
459 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
466 switch( p_vout->i_bytes_per_pixel )
469 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
470 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
471 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
474 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
475 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
476 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
479 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
480 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
481 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
484 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
485 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
486 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
492 /*****************************************************************************
493 * SetOffset: build offset array for conversion functions
494 *****************************************************************************
495 * This function will build an offset array used in later conversion functions.
496 * It will also set horizontal and vertical scaling indicators. If b_double
497 * is set, the p_offset structure has interleaved Y and U/V offsets.
498 *****************************************************************************/
499 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
500 boolean_t *pb_h_scaling, int *pi_v_scaling,
501 int *p_offset, boolean_t b_double )
503 int i_x; /* x position in destination */
504 int i_scale_count; /* modulo counter */
507 * Prepare horizontal offset array
509 if( i_pic_width - i_width == 0 )
511 /* No horizontal scaling: YUV conversion is done directly to picture */
514 else if( i_pic_width - i_width > 0 )
516 /* Prepare scaling array for horizontal extension */
518 i_scale_count = i_pic_width;
521 for( i_x = i_width; i_x--; )
523 while( (i_scale_count -= i_width) > 0 )
528 i_scale_count += i_pic_width;
534 for( i_x = i_width; i_x--; )
536 while( (i_scale_count -= i_width) > 0 )
542 *p_offset++ = i_dummy;
543 i_dummy = 1 - i_dummy;
544 i_scale_count += i_pic_width;
548 else /* if( i_pic_width - i_width < 0 ) */
550 /* Prepare scaling array for horizontal reduction */
552 i_scale_count = i_width;
555 for( i_x = i_pic_width; i_x--; )
558 while( (i_scale_count -= i_pic_width) > 0 )
563 i_scale_count += i_width;
570 for( i_x = i_pic_width; i_x--; )
573 while( (i_scale_count -= i_pic_width) > 0 )
577 *p_offset++ = i_jump;
578 *p_offset++ = ( i_jump += i_remainder ) >> 1;
579 i_remainder = i_jump & 1;
580 i_scale_count += i_width;
586 * Set vertical scaling indicator
588 if( i_pic_height - i_height == 0 )
592 else if( i_pic_height - i_height > 0 )
596 else /* if( i_pic_height - i_height < 0 ) */