1 /*****************************************************************************
2 * video_yuvmmx.c: MMX YUV transformation functions
3 * Provides functions to perform the YUV conversion.
4 *****************************************************************************
5 * Copyright (C) 1999, 2000 VideoLAN
6 * $Id: video_yuvmmx.c,v 1.11 2001/07/11 02:01:05 sam Exp $
8 * Authors: Samuel Hocevar <sam@zoy.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 *****************************************************************************/
26 #define MODULE_NAME yuvmmx
27 #include "modules_inner.h"
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <math.h> /* exp(), pow() */
35 #include <errno.h> /* ENOMEM */
36 #include <stdlib.h> /* free() */
37 #include <string.h> /* strerror() */
46 #include "video_output.h"
48 #include "video_common.h"
53 #include "modules_export.h"
55 static int yuv_Probe ( probedata_t *p_data );
56 static int yuv_Init ( vout_thread_t *p_vout );
57 static int yuv_Reset ( vout_thread_t *p_vout );
58 static void yuv_End ( vout_thread_t *p_vout );
60 static void SetYUV ( vout_thread_t *p_vout );
62 /*****************************************************************************
63 * Functions exported as capabilities. They are declared as static so that
64 * we don't pollute the namespace too much.
65 *****************************************************************************/
66 void _M( yuv_getfunctions )( function_list_t * p_function_list )
68 p_function_list->pf_probe = yuv_Probe;
69 p_function_list->functions.yuv.pf_init = yuv_Init;
70 p_function_list->functions.yuv.pf_reset = yuv_Reset;
71 p_function_list->functions.yuv.pf_end = yuv_End;
74 /*****************************************************************************
75 * yuv_Probe: tests probe the audio device and return a score
76 *****************************************************************************
77 * This function tries to open the DSP and returns a score to the plugin
78 * manager so that it can choose the most appropriate one.
79 *****************************************************************************/
80 static int yuv_Probe( probedata_t *p_data )
82 /* Test for MMX support in the CPU */
83 if( !TestCPU( CPU_CAPABILITY_MMX ) )
88 if( TestMethod( YUV_METHOD_VAR, "yuvmmx" )
89 || TestMethod( YUV_METHOD_VAR, "mmx" ) )
97 /*****************************************************************************
98 * yuv_Init: allocate and initialize translations tables
99 *****************************************************************************
100 * This function will allocate memory to store translation tables, depending
101 * of the screen depth.
102 *****************************************************************************/
103 static int yuv_Init( vout_thread_t *p_vout )
105 size_t tables_size; /* tables size, in bytes */
107 /* Computes tables size for 8bbp only */
108 if( p_vout->i_bytes_per_pixel == 1 )
110 tables_size = sizeof( u8 )
111 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
113 /* Allocate memory */
114 p_vout->yuv.p_base = malloc( tables_size );
115 if( p_vout->yuv.p_base == NULL )
117 intf_ErrMsg("error: %s", strerror(ENOMEM));
123 p_vout->yuv.p_base = NULL;
126 /* Allocate memory for conversion buffer and offset array */
127 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
128 if( p_vout->yuv.p_buffer == NULL )
130 intf_ErrMsg("error: %s", strerror(ENOMEM));
131 free( p_vout->yuv.p_base );
134 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
135 if( p_vout->yuv.p_offset == NULL )
137 intf_ErrMsg("error: %s", strerror(ENOMEM));
138 free( p_vout->yuv.p_base );
139 free( p_vout->yuv.p_buffer );
143 /* Initialize tables */
148 /*****************************************************************************
149 * yuv_End: destroy translations tables
150 *****************************************************************************
151 * Free memory allocated by yuv_CCreate.
152 *****************************************************************************/
153 static void yuv_End( vout_thread_t *p_vout )
155 free( p_vout->yuv.p_base );
156 free( p_vout->yuv.p_buffer );
157 free( p_vout->yuv.p_offset );
160 /*****************************************************************************
161 * yuv_Reset: re-initialize translations tables
162 *****************************************************************************
163 * This function will initialize the tables allocated by vout_CreateTables and
164 * set functions pointers.
165 *****************************************************************************/
166 static int yuv_Reset( vout_thread_t *p_vout )
169 return( yuv_Init( p_vout ) );
172 /*****************************************************************************
173 * SetYUV: compute tables and set function pointers
174 *****************************************************************************/
175 static void SetYUV( vout_thread_t *p_vout )
177 int i_index; /* index in tables */
180 * Set pointers and build YUV tables
182 if( p_vout->b_grayscale )
184 /* Grayscale: build gray table */
185 if( p_vout->i_bytes_per_pixel == 1 )
187 u16 bright[256], transp[256];
189 for( i_index = 0; i_index < 256; i_index++)
191 bright[ i_index ] = i_index << 8;
192 transp[ i_index ] = 0;
194 /* the colors have been allocated, we can set the palette */
195 p_vout->pf_setpalette( p_vout, bright, bright, bright, transp );
196 p_vout->i_white_pixel = 0xff;
197 p_vout->i_black_pixel = 0x00;
198 p_vout->i_gray_pixel = 0x44;
199 p_vout->i_blue_pixel = 0x3b;
204 /* Color: build red, green and blue tables */
205 if( p_vout->i_bytes_per_pixel == 1 )
209 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
211 #define U_GREEN_COEF ((int)(-0.391 * (1<<SHIFT) / 1.164))
212 #define U_BLUE_COEF ((int)(2.018 * (1<<SHIFT) / 1.164))
213 #define V_RED_COEF ((int)(1.596 * (1<<SHIFT) / 1.164))
214 #define V_GREEN_COEF ((int)(-0.813 * (1<<SHIFT) / 1.164))
220 u16 red[256], green[256], blue[256], transp[256];
221 unsigned char lookup[PALETTE_TABLE_SIZE];
223 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
225 /* this loop calculates the intersection of an YUV box
226 * and the RGB cube. */
227 for ( y = 0; y <= 256; y += 16 )
229 for ( u = 0; u <= 256; u += 32 )
230 for ( v = 0; v <= 256; v += 32 )
232 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
233 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
234 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
239 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
240 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
242 /* this one should never happen unless someone fscked up my code */
243 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
245 /* clip the colors */
247 green[j] = CLIP( g );
253 p_vout->yuv.yuv.p_rgb8[i++] = j;
259 p_vout->yuv.yuv.p_rgb8[i++] = 0;
265 /* the colors have been allocated, we can set the palette */
266 /* there will eventually be a way to know which colors
267 * couldn't be allocated and try to find a replacement */
268 p_vout->pf_setpalette( p_vout, red, green, blue, transp );
270 p_vout->i_white_pixel = 0xff;
271 p_vout->i_black_pixel = 0x00;
272 p_vout->i_gray_pixel = 0x44;
273 p_vout->i_blue_pixel = 0x3b;
276 /* this loop allocates colors that got outside
278 for ( y = 0; y <= 256; y += 16 )
280 for ( u = 0; u <= 256; u += 32 )
282 for ( v = 0; v <= 256; v += 32 )
285 int dist, mindist = 100000000;
287 if( lookup[i] || y==0)
294 for( u2 = 0; u2 <= 256; u2 += 32 )
295 for( v2 = 0; v2 <= 256; v2 += 32 )
297 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
298 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
300 /* find the nearest color */
303 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
308 /* find the nearest color */
309 if( dist + 128 < mindist )
311 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
312 mindist = dist + 128;
324 * Set functions pointers
328 switch( p_vout->i_bytes_per_pixel)
330 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
332 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr8 );
333 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr8 );
334 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr8 );
338 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr16 );
339 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr16 );
340 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr16 );
344 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr24 );
345 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr24 );
346 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr24 );
350 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr32 );
351 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr32 );
352 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr32 );
357 else if( p_vout->b_grayscale )
360 switch( p_vout->i_bytes_per_pixel )
362 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
364 p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray8 );
365 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray8 );
366 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray8 );
369 p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray16 );
370 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray16 );
371 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray16 );
374 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB24 );
375 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray24 );
376 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray24 );
379 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB32 );
380 p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray32 );
381 p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray32 );
389 switch( p_vout->i_bytes_per_pixel )
391 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
393 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB8 );
394 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB8 );
395 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB8 );
398 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB16 );
399 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB16 );
400 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB16 );
403 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB24 );
404 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB24 );
405 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB24 );
408 p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB32 );
409 p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB32 );
410 p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB32 );
417 /*****************************************************************************
418 * SetOffset: build offset array for conversion functions
419 *****************************************************************************
420 * This function will build an offset array used in later conversion functions.
421 * It will also set horizontal and vertical scaling indicators. If b_double
422 * is set, the p_offset structure has interleaved Y and U/V offsets.
423 *****************************************************************************/
424 void _M( SetOffset )( int i_width, int i_height, int i_pic_width,
425 int i_pic_height, boolean_t *pb_h_scaling,
426 int *pi_v_scaling, int *p_offset, boolean_t b_double )
428 int i_x; /* x position in destination */
429 int i_scale_count; /* modulo counter */
432 * Prepare horizontal offset array
434 if( i_pic_width - i_width == 0 )
436 /* No horizontal scaling: YUV conversion is done directly to picture */
439 else if( i_pic_width - i_width > 0 )
441 /* Prepare scaling array for horizontal extension */
443 i_scale_count = i_pic_width;
444 for( i_x = i_width; i_x--; )
446 while( (i_scale_count -= i_width) > 0 )
451 i_scale_count += i_pic_width;
454 else /* if( i_pic_width - i_width < 0 ) */
456 /* Prepare scaling array for horizontal reduction */
458 i_scale_count = i_width;
459 for( i_x = i_pic_width; i_x--; )
462 while( (i_scale_count -= i_pic_width) > 0 )
467 i_scale_count += i_width;
472 * Set vertical scaling indicator
474 if( i_pic_height - i_height == 0 )
478 else if( i_pic_height - i_height > 0 )
482 else /* if( i_pic_height - i_height < 0 ) */