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() */
43 #include "video_output.h"
44 #include "video_yuv.h"
48 /*****************************************************************************
49 * vout_InitYUV: allocate and initialize translations tables
50 *****************************************************************************
51 * This function will allocate memory to store translation tables, depending
52 * of the screen depth.
53 *****************************************************************************/
54 int yuv_CInit( vout_thread_t *p_vout )
56 size_t tables_size; /* tables size, in bytes */
58 /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
59 switch( p_vout->i_bytes_per_pixel )
62 tables_size = sizeof( u8 )
63 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
66 tables_size = sizeof( u16 )
67 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
72 tables_size = sizeof( u32 )
73 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
78 p_vout->yuv.p_base = malloc( tables_size );
79 if( p_vout->yuv.p_base == NULL )
81 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
85 /* Allocate memory for conversion buffer and offset array */
86 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
87 if( p_vout->yuv.p_buffer == NULL )
89 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
90 free( p_vout->yuv.p_base );
94 /* In 8bpp we have a twice as big offset table because we also
95 * need the offsets for U and V (not only Y) */
96 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) *
97 ( ( p_vout->i_bytes_per_pixel == 1 ) ? 2 : 1 ) );
98 if( p_vout->yuv.p_offset == NULL )
100 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
101 free( p_vout->yuv.p_base );
102 free( p_vout->yuv.p_buffer );
106 /* Initialize tables */
111 /*****************************************************************************
112 * yuv_CEnd: destroy translations tables
113 *****************************************************************************
114 * Free memory allocated by yuv_CCreate.
115 *****************************************************************************/
116 void yuv_CEnd( vout_thread_t *p_vout )
118 free( p_vout->yuv.p_base );
119 free( p_vout->yuv.p_buffer );
120 free( p_vout->yuv.p_offset );
123 /*****************************************************************************
124 * yuv_CReset: re-initialize translations tables
125 *****************************************************************************
126 * This function will initialize the tables allocated by vout_CreateTables and
127 * set functions pointers.
128 *****************************************************************************/
129 int yuv_CReset( vout_thread_t *p_vout )
132 return( yuv_CInit( p_vout ) );
135 /* following functions are local */
137 /*****************************************************************************
138 * SetGammaTable: return intensity table transformed by gamma curve.
139 *****************************************************************************
140 * pi_table is a table of 256 entries from 0 to 255.
141 *****************************************************************************/
142 void SetGammaTable( int *pi_table, double f_gamma )
144 int i_y; /* base intensity */
146 /* Use exp(gamma) instead of gamma */
147 f_gamma = exp( f_gamma );
149 /* Build gamma table */
150 for( i_y = 0; i_y < 256; i_y++ )
152 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
156 /*****************************************************************************
157 * SetYUV: compute tables and set function pointers
158 + *****************************************************************************/
159 void SetYUV( vout_thread_t *p_vout )
161 int pi_gamma[256]; /* gamma table */
162 int i_index; /* index in tables */
164 /* Build gamma table */
165 SetGammaTable( pi_gamma, p_vout->f_gamma );
168 * Set pointers and build YUV tables
170 if( p_vout->b_grayscale )
172 /* Grayscale: build gray table */
173 switch( p_vout->i_bytes_per_pixel )
177 u16 bright[256], transp[256];
179 p_vout->yuv.yuv.p_gray8 = (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
180 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
182 p_vout->yuv.yuv.p_gray8[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
183 p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
185 for( i_index = 0; i_index < 256; i_index++)
187 p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
188 bright[ i_index ] = i_index << 8;
189 transp[ i_index ] = 0;
191 /* the colors have been allocated, we can set the palette */
192 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
193 p_vout->i_white_pixel = 0xff;
194 p_vout->i_black_pixel = 0x00;
195 p_vout->i_gray_pixel = 0x44;
196 p_vout->i_blue_pixel = 0x3b;
201 p_vout->yuv.yuv.p_gray16 = (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
202 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
204 p_vout->yuv.yuv.p_gray16[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
205 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
207 for( i_index = 0; i_index < 256; i_index++)
209 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
214 p_vout->yuv.yuv.p_gray32 = (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
215 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
217 p_vout->yuv.yuv.p_gray32[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
218 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
220 for( i_index = 0; i_index < 256; i_index++)
222 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
229 /* Color: build red, green and blue tables */
230 switch( p_vout->i_bytes_per_pixel )
236 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
242 u16 red[256], green[256], blue[256], transp[256];
243 unsigned char lookup[PALETTE_TABLE_SIZE];
245 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
247 /* this loop calculates the intersection of an YUV box
248 * and the RGB cube. */
249 for ( y = 0; y <= 256; y += 16 )
251 for ( u = 0; u <= 256; u += 32 )
252 for ( v = 0; v <= 256; v += 32 )
254 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
255 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
256 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
261 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
262 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
264 /* this one should never happen unless someone fscked up my code */
265 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }
267 /* clip the colors */
269 green[j] = CLIP( g );
275 p_vout->yuv.yuv.p_rgb8[i++] = j;
281 p_vout->yuv.yuv.p_rgb8[i++] = 0;
287 /* the colors have been allocated, we can set the palette */
288 /* there will eventually be a way to know which colors
289 * couldn't be allocated and try to find a replacement */
290 p_vout->p_set_palette( p_vout, red, green, blue, transp );
292 p_vout->i_white_pixel = 0xff;
293 p_vout->i_black_pixel = 0x00;
294 p_vout->i_gray_pixel = 0x44;
295 p_vout->i_blue_pixel = 0x3b;
298 /* this loop allocates colors that got outside
300 for ( y = 0; y <= 256; y += 16 )
302 for ( u = 0; u <= 256; u += 32 )
303 for ( v = 0; v <= 256; v += 32 )
306 int dist, mindist = 100000000;
308 if( lookup[i] || y==0)
315 for( u2 = 0; u2 <= 256; u2 += 32 )
316 for( v2 = 0; v2 <= 256; v2 += 32 )
318 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
319 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
321 /* find the nearest color */
324 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
329 /* find the nearest color */
330 if( dist + 128 < mindist )
332 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
333 mindist = dist + 128;
344 p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
345 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
347 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
348 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
350 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
352 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
353 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
355 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
357 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
358 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
360 for( i_index = 0; i_index < 256; i_index++ )
362 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
363 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
364 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
369 p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
370 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
372 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
373 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
375 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
377 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
378 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
380 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
382 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
383 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
385 for( i_index = 0; i_index < 256; i_index++ )
387 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
388 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
389 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
396 * Set functions pointers
398 if( p_vout->b_grayscale )
401 switch( p_vout->i_bytes_per_pixel )
404 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
405 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
406 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
409 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
410 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
411 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
414 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
415 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
416 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
419 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
420 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
421 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
428 switch( p_vout->i_bytes_per_pixel )
431 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
432 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
433 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
436 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
437 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
438 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
441 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
442 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
443 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
446 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
447 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
448 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
454 /*****************************************************************************
455 * SetOffset: build offset array for conversion functions
456 *****************************************************************************
457 * This function will build an offset array used in later conversion functions.
458 * It will also set horizontal and vertical scaling indicators. If b_double
459 * is set, the p_offset structure has interleaved Y and U/V offsets.
460 *****************************************************************************/
461 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
462 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset,
465 int i_x; /* x position in destination */
466 int i_scale_count; /* modulo counter */
469 * Prepare horizontal offset array
471 if( i_pic_width - i_width > 0 )
473 /* Prepare scaling array for horizontal extension */
475 i_scale_count = i_pic_width;
479 for( i_x = i_width; i_x--; )
481 while( (i_scale_count -= i_width) > 0 )
487 *p_offset++ = i_dummy & 1;
489 i_scale_count += i_pic_width;
494 for( i_x = i_width; i_x--; )
496 while( (i_scale_count -= i_width) > 0 )
501 i_scale_count += i_pic_width;
505 else if( i_pic_width - i_width < 0 )
507 /* Prepare scaling array for horizontal reduction */
509 i_scale_count = i_pic_width;
514 for( i_x = i_pic_width; i_x--; )
517 while( (i_scale_count -= i_pic_width) >= 0 )
521 *p_offset++ = i_jump;
522 *p_offset++ = ( i_jump += i_remainder ) >> 1;
523 i_remainder = i_jump & 1;
524 i_scale_count += i_width;
529 for( i_x = i_pic_width; i_x--; )
532 while( (i_scale_count -= i_pic_width) >= 0 )
537 i_scale_count += i_width;
543 /* No horizontal scaling: YUV conversion is done directly to picture */
548 * Set vertical scaling indicator
550 if( i_pic_height - i_height > 0 )
554 else if( i_pic_height - i_height < 0 )