1 /*****************************************************************************
2 * video_yuv.c: MMX 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_MMXInit( vout_thread_t *p_vout )
56 size_t tables_size; /* tables size, in bytes */
58 /* Computes tables size for 8bbp only */
59 if( p_vout->i_bytes_per_pixel == 1 )
61 tables_size = sizeof( u8 )
62 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
65 p_vout->yuv.p_base = malloc( tables_size );
66 if( p_vout->yuv.p_base == NULL )
68 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
74 p_vout->yuv.p_base = NULL;
77 /* Allocate memory for conversion buffer and offset array */
78 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
79 if( p_vout->yuv.p_buffer == NULL )
81 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
82 free( p_vout->yuv.p_base );
85 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
86 if( p_vout->yuv.p_offset == NULL )
88 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
89 free( p_vout->yuv.p_base );
90 free( p_vout->yuv.p_buffer );
94 /* Initialize tables */
99 /*****************************************************************************
100 * yuv_MMXEnd: destroy translations tables
101 *****************************************************************************
102 * Free memory allocated by yuv_MMXCreate.
103 *****************************************************************************/
104 void yuv_MMXEnd( vout_thread_t *p_vout )
106 if( p_vout->i_bytes_per_pixel == 1 )
108 free( p_vout->yuv.p_base );
111 free( p_vout->yuv.p_buffer );
112 free( p_vout->yuv.p_offset );
115 /*****************************************************************************
116 * yuv_MMXReset: re-initialize translations tables
117 *****************************************************************************
118 * This function will initialize the tables allocated by vout_CreateTables and
119 * set functions pointers.
120 *****************************************************************************/
121 int yuv_MMXReset( vout_thread_t *p_vout )
123 yuv_MMXEnd( p_vout );
124 return( yuv_MMXInit( p_vout ) );
127 /* following functions are local */
129 /*****************************************************************************
130 * SetYUV: compute tables and set function pointers
131 + *****************************************************************************/
132 void SetYUV( vout_thread_t *p_vout )
134 int pi_gamma[256]; /* gamma table */
135 int i_index; /* index in tables */
137 /* Build gamma table */
138 SetGammaTable( pi_gamma, p_vout->f_gamma );
141 * Set pointers and build YUV tables
143 if( p_vout->b_grayscale )
145 /* Grayscale: build gray table */
146 if( p_vout->i_bytes_per_pixel == 1 )
148 u16 bright[256], transp[256];
150 for( i_index = 0; i_index < 256; i_index++)
152 bright[ i_index ] = i_index << 8;
153 transp[ i_index ] = 0;
155 /* the colors have been allocated, we can set the palette */
156 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
157 p_vout->i_white_pixel = 0xff;
158 p_vout->i_black_pixel = 0x00;
159 p_vout->i_gray_pixel = 0x44;
160 p_vout->i_blue_pixel = 0x3b;
165 /* Color: build red, green and blue tables */
166 if( p_vout->i_bytes_per_pixel == 1 )
170 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
172 #define U_GREEN_COEF ((int)(-0.391 * (1<<SHIFT) / 1.164))
173 #define U_BLUE_COEF ((int)(2.018 * (1<<SHIFT) / 1.164))
174 #define V_RED_COEF ((int)(1.596 * (1<<SHIFT) / 1.164))
175 #define V_GREEN_COEF ((int)(-0.813 * (1<<SHIFT) / 1.164))
181 u16 red[256], green[256], blue[256], transp[256];
182 unsigned char lookup[PALETTE_TABLE_SIZE];
184 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
186 /* this loop calculates the intersection of an YUV box
187 * and the RGB cube. */
188 for ( y = 0; y <= 256; y += 16 )
190 for ( u = 0; u <= 256; u += 32 )
191 for ( v = 0; v <= 256; v += 32 )
193 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
194 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
195 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
200 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
201 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
203 /* this one should never happen unless someone fscked up my code */
204 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }
206 /* clip the colors */
208 green[j] = CLIP( g );
214 p_vout->yuv.yuv.p_rgb8[i++] = j;
220 p_vout->yuv.yuv.p_rgb8[i++] = 0;
226 /* the colors have been allocated, we can set the palette */
227 /* there will eventually be a way to know which colors
228 * couldn't be allocated and try to find a replacement */
229 p_vout->p_set_palette( p_vout, red, green, blue, 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;
237 /* this loop allocates colors that got outside
239 for ( y = 0; y <= 256; y += 16 )
241 for ( u = 0; u <= 256; u += 32 )
243 for ( v = 0; v <= 256; v += 32 )
246 int dist, mindist = 100000000;
248 if( lookup[i] || y==0)
255 for( u2 = 0; u2 <= 256; u2 += 32 )
256 for( v2 = 0; v2 <= 256; v2 += 32 )
258 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
259 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
261 /* find the nearest color */
264 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
269 /* find the nearest color */
270 if( dist + 128 < mindist )
272 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
273 mindist = dist + 128;
285 * Set functions pointers
287 if( p_vout->b_grayscale )
290 switch( p_vout->i_bytes_per_pixel )
293 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
294 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
295 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
298 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
299 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
300 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
303 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
304 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
305 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
308 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
309 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
310 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
317 switch( p_vout->i_bytes_per_pixel )
320 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
321 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
322 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
325 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
326 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
327 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
330 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
331 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
332 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
335 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
336 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
337 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
343 /*****************************************************************************
344 * SetOffset: build offset array for conversion functions
345 *****************************************************************************
346 * This function will build an offset array used in later conversion functions.
347 * It will also set horizontal and vertical scaling indicators.
348 *****************************************************************************/
349 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
350 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset )
352 int i_x; /* x position in destination */
353 int i_scale_count; /* modulo counter */
356 * Prepare horizontal offset array
358 if( i_pic_width - i_width > 0 )
360 /* Prepare scaling array for horizontal extension */
362 i_scale_count = i_pic_width;
363 for( i_x = i_width; i_x--; )
365 while( (i_scale_count -= i_width) > 0 )
370 i_scale_count += i_pic_width;
373 else if( i_pic_width - i_width < 0 )
375 /* Prepare scaling array for horizontal reduction */
377 i_scale_count = i_pic_width;
378 for( i_x = i_pic_width; i_x--; )
381 while( (i_scale_count -= i_pic_width) >= 0 )
386 i_scale_count += i_width;
391 /* No horizontal scaling: YUV conversion is done directly to picture */
396 * Set vertical scaling indicator
398 if( i_pic_height - i_height > 0 )
402 else if( i_pic_height - i_height < 0 )