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 i_index; /* index in tables */
137 * Set pointers and build YUV tables
139 if( p_vout->b_grayscale )
141 /* Grayscale: build gray table */
142 if( p_vout->i_bytes_per_pixel == 1 )
144 u16 bright[256], transp[256];
146 for( i_index = 0; i_index < 256; i_index++)
148 bright[ i_index ] = i_index << 8;
149 transp[ i_index ] = 0;
151 /* the colors have been allocated, we can set the palette */
152 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
153 p_vout->i_white_pixel = 0xff;
154 p_vout->i_black_pixel = 0x00;
155 p_vout->i_gray_pixel = 0x44;
156 p_vout->i_blue_pixel = 0x3b;
161 /* Color: build red, green and blue tables */
162 if( p_vout->i_bytes_per_pixel == 1 )
166 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
168 #define U_GREEN_COEF ((int)(-0.391 * (1<<SHIFT) / 1.164))
169 #define U_BLUE_COEF ((int)(2.018 * (1<<SHIFT) / 1.164))
170 #define V_RED_COEF ((int)(1.596 * (1<<SHIFT) / 1.164))
171 #define V_GREEN_COEF ((int)(-0.813 * (1<<SHIFT) / 1.164))
177 u16 red[256], green[256], blue[256], transp[256];
178 unsigned char lookup[PALETTE_TABLE_SIZE];
180 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
182 /* this loop calculates the intersection of an YUV box
183 * and the RGB cube. */
184 for ( y = 0; y <= 256; y += 16 )
186 for ( u = 0; u <= 256; u += 32 )
187 for ( v = 0; v <= 256; v += 32 )
189 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
190 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
191 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
196 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
197 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
199 /* this one should never happen unless someone fscked up my code */
200 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }
202 /* clip the colors */
204 green[j] = CLIP( g );
210 p_vout->yuv.yuv.p_rgb8[i++] = j;
216 p_vout->yuv.yuv.p_rgb8[i++] = 0;
222 /* the colors have been allocated, we can set the palette */
223 /* there will eventually be a way to know which colors
224 * couldn't be allocated and try to find a replacement */
225 p_vout->p_set_palette( p_vout, red, green, blue, transp );
227 p_vout->i_white_pixel = 0xff;
228 p_vout->i_black_pixel = 0x00;
229 p_vout->i_gray_pixel = 0x44;
230 p_vout->i_blue_pixel = 0x3b;
233 /* this loop allocates colors that got outside
235 for ( y = 0; y <= 256; y += 16 )
237 for ( u = 0; u <= 256; u += 32 )
239 for ( v = 0; v <= 256; v += 32 )
242 int dist, mindist = 100000000;
244 if( lookup[i] || y==0)
251 for( u2 = 0; u2 <= 256; u2 += 32 )
252 for( v2 = 0; v2 <= 256; v2 += 32 )
254 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
255 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
257 /* find the nearest color */
260 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
265 /* find the nearest color */
266 if( dist + 128 < mindist )
268 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
269 mindist = dist + 128;
281 * Set functions pointers
283 if( p_vout->b_grayscale )
286 switch( p_vout->i_bytes_per_pixel )
289 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
290 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
291 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
294 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
295 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
296 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
299 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
300 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
301 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
304 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
305 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
306 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
313 switch( p_vout->i_bytes_per_pixel )
316 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
317 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
318 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
321 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
322 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
323 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
326 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
327 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
328 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
331 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
332 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
333 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
339 /*****************************************************************************
340 * SetOffset: build offset array for conversion functions
341 *****************************************************************************
342 * This function will build an offset array used in later conversion functions.
343 * It will also set horizontal and vertical scaling indicators.
344 *****************************************************************************/
345 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
346 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset )
348 int i_x; /* x position in destination */
349 int i_scale_count; /* modulo counter */
352 * Prepare horizontal offset array
354 if( i_pic_width - i_width > 0 )
356 /* Prepare scaling array for horizontal extension */
358 i_scale_count = i_pic_width;
359 for( i_x = i_width; i_x--; )
361 while( (i_scale_count -= i_width) > 0 )
366 i_scale_count += i_pic_width;
369 else if( i_pic_width - i_width < 0 )
371 /* Prepare scaling array for horizontal reduction */
373 i_scale_count = i_pic_width;
374 for( i_x = i_pic_width; i_x--; )
377 while( (i_scale_count -= i_pic_width) >= 0 )
382 i_scale_count += i_width;
387 /* No horizontal scaling: YUV conversion is done directly to picture */
392 * Set vertical scaling indicator
394 if( i_pic_height - i_height > 0 )
398 else if( i_pic_height - i_height < 0 )