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 );
93 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
94 if( p_vout->yuv.p_offset == NULL )
96 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
97 free( p_vout->yuv.p_base );
98 free( p_vout->yuv.p_buffer );
102 /* Initialize tables */
107 /*****************************************************************************
108 * yuv_CEnd: destroy translations tables
109 *****************************************************************************
110 * Free memory allocated by yuv_CCreate.
111 *****************************************************************************/
112 void yuv_CEnd( vout_thread_t *p_vout )
114 free( p_vout->yuv.p_base );
115 free( p_vout->yuv.p_buffer );
116 free( p_vout->yuv.p_offset );
119 /*****************************************************************************
120 * yuv_CReset: re-initialize translations tables
121 *****************************************************************************
122 * This function will initialize the tables allocated by vout_CreateTables and
123 * set functions pointers.
124 *****************************************************************************/
125 int yuv_CReset( vout_thread_t *p_vout )
128 return( yuv_CInit( p_vout ) );
131 /* following functions are local */
133 /*****************************************************************************
134 * SetGammaTable: return intensity table transformed by gamma curve.
135 *****************************************************************************
136 * pi_table is a table of 256 entries from 0 to 255.
137 *****************************************************************************/
138 void SetGammaTable( int *pi_table, double f_gamma )
140 int i_y; /* base intensity */
142 /* Use exp(gamma) instead of gamma */
143 f_gamma = exp( f_gamma );
145 /* Build gamma table */
146 for( i_y = 0; i_y < 256; i_y++ )
148 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
152 /*****************************************************************************
153 * SetYUV: compute tables and set function pointers
154 + *****************************************************************************/
155 void SetYUV( vout_thread_t *p_vout )
157 int pi_gamma[256]; /* gamma table */
158 int i_index; /* index in tables */
160 /* Build gamma table */
161 SetGammaTable( pi_gamma, p_vout->f_gamma );
164 * Set pointers and build YUV tables
166 if( p_vout->b_grayscale )
168 /* Grayscale: build gray table */
169 switch( p_vout->i_bytes_per_pixel )
173 u16 bright[256], transp[256];
175 p_vout->yuv.yuv.p_gray8 = (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
176 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
178 p_vout->yuv.yuv.p_gray8[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
179 p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
181 for( i_index = 0; i_index < 256; i_index++)
183 p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
184 bright[ i_index ] = i_index << 8;
185 transp[ i_index ] = 0;
187 /* the colors have been allocated, we can set the palette */
188 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
189 p_vout->i_white_pixel = 0xff;
190 p_vout->i_black_pixel = 0x00;
191 p_vout->i_gray_pixel = 0x44;
192 p_vout->i_blue_pixel = 0x3b;
197 p_vout->yuv.yuv.p_gray16 = (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
198 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
200 p_vout->yuv.yuv.p_gray16[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
201 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
203 for( i_index = 0; i_index < 256; i_index++)
205 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
210 p_vout->yuv.yuv.p_gray32 = (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
211 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
213 p_vout->yuv.yuv.p_gray32[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
214 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
216 for( i_index = 0; i_index < 256; i_index++)
218 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
225 /* Color: build red, green and blue tables */
226 switch( p_vout->i_bytes_per_pixel )
232 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
238 u16 red[256], green[256], blue[256], transp[256];
239 unsigned char lookup[PALETTE_TABLE_SIZE];
241 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
243 /* this loop calculates the intersection of an YUV box
244 * and the RGB cube. */
245 for ( y = 0; y <= 256; y += 16 )
247 for ( u = 0; u <= 256; u += 32 )
248 for ( v = 0; v <= 256; v += 32 )
250 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
251 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
252 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
257 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
258 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
260 /* this one should never happen unless someone fscked up my code */
261 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }
263 /* clip the colors */
265 green[j] = CLIP( g );
271 p_vout->yuv.yuv.p_rgb8[i++] = j;
277 p_vout->yuv.yuv.p_rgb8[i++] = 0;
283 /* the colors have been allocated, we can set the palette */
284 /* there will eventually be a way to know which colors
285 * couldn't be allocated and try to find a replacement */
286 p_vout->p_set_palette( p_vout, red, green, blue, transp );
288 p_vout->i_white_pixel = 0xff;
289 p_vout->i_black_pixel = 0x00;
290 p_vout->i_gray_pixel = 0x44;
291 p_vout->i_blue_pixel = 0x3b;
294 /* this loop allocates colors that got outside
296 for ( y = 0; y <= 256; y += 16 )
298 for ( u = 0; u <= 256; u += 32 )
299 for ( v = 0; v <= 256; v += 32 )
302 int dist, mindist = 100000000;
304 if( lookup[i] || y==0)
311 for( u2 = 0; u2 <= 256; u2 += 32 )
312 for( v2 = 0; v2 <= 256; v2 += 32 )
314 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
315 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
317 /* find the nearest color */
320 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
325 /* find the nearest color */
326 if( dist + 128 < mindist )
328 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
329 mindist = dist + 128;
340 p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
341 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
343 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
344 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
346 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
348 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
349 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
351 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
353 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
354 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
356 for( i_index = 0; i_index < 256; i_index++ )
358 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
359 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
360 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
365 p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
366 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
368 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
369 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
371 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
373 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
374 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
376 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
378 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
379 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
381 for( i_index = 0; i_index < 256; i_index++ )
383 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
384 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
385 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
392 * Set functions pointers
394 if( p_vout->b_grayscale )
397 switch( p_vout->i_bytes_per_pixel )
400 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
401 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
402 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
405 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
406 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
407 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
410 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
411 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
412 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
415 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
416 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
417 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
424 switch( p_vout->i_bytes_per_pixel )
427 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
428 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
429 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
432 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
433 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
434 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
437 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
438 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
439 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
442 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
443 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
444 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
450 /*****************************************************************************
451 * SetOffset: build offset array for conversion functions
452 *****************************************************************************
453 * This function will build an offset array used in later conversion functions.
454 * It will also set horizontal and vertical scaling indicators.
455 *****************************************************************************/
456 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
457 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset )
459 int i_x; /* x position in destination */
460 int i_scale_count; /* modulo counter */
463 * Prepare horizontal offset array
465 if( i_pic_width - i_width > 0 )
467 /* Prepare scaling array for horizontal extension */
469 i_scale_count = i_pic_width;
470 for( i_x = i_width; i_x--; )
472 while( (i_scale_count -= i_width) > 0 )
477 i_scale_count += i_pic_width;
480 else if( i_pic_width - i_width < 0 )
482 /* Prepare scaling array for horizontal reduction */
484 i_scale_count = i_pic_width;
485 for( i_x = i_pic_width; i_x--; )
488 while( (i_scale_count -= i_pic_width) >= 0 )
493 i_scale_count += i_width;
498 /* No horizontal scaling: YUV conversion is done directly to picture */
503 * Set vertical scaling indicator
505 if( i_pic_height - i_height > 0 )
509 else if( i_pic_height - i_height < 0 )