1 /*****************************************************************************
2 * video_yuvmmx.c: MMX YUV transformation functions
3 * Provides functions to perform the YUV conversion.
4 *****************************************************************************
5 * Copyright (C) 1999, 2000 VideoLAN
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 *****************************************************************************/
25 #define MODULE_NAME yuvmmx
26 #include "modules_inner.h"
28 /*****************************************************************************
30 *****************************************************************************/
33 #include <math.h> /* exp(), pow() */
34 #include <errno.h> /* ENOMEM */
35 #include <stdlib.h> /* free() */
36 #include <string.h> /* strerror() */
47 #include "video_output.h"
49 #include "video_common.h"
53 static int yuv_Probe ( probedata_t *p_data );
54 static int yuv_Init ( vout_thread_t *p_vout );
55 static int yuv_Reset ( vout_thread_t *p_vout );
56 static void yuv_End ( vout_thread_t *p_vout );
58 static void SetYUV ( vout_thread_t *p_vout );
60 /*****************************************************************************
61 * Functions exported as capabilities. They are declared as static so that
62 * we don't pollute the namespace too much.
63 *****************************************************************************/
64 void _M( yuv_getfunctions )( function_list_t * p_function_list )
66 p_function_list->pf_probe = yuv_Probe;
67 p_function_list->functions.yuv.pf_init = yuv_Init;
68 p_function_list->functions.yuv.pf_reset = yuv_Reset;
69 p_function_list->functions.yuv.pf_end = yuv_End;
72 /*****************************************************************************
73 * yuv_Probe: tests probe the audio device and return a score
74 *****************************************************************************
75 * This function tries to open the DSP and returns a score to the plugin
76 * manager so that it can choose the most appropriate one.
77 *****************************************************************************/
78 static int yuv_Probe( probedata_t *p_data )
80 /* Test for MMX support in the CPU */
81 if( TestCPU( CPU_CAPABILITY_MMX ) )
83 if( TestMethod( YUV_METHOD_VAR, "yuvmmx" ) )
98 /*****************************************************************************
99 * yuv_Init: allocate and initialize translations tables
100 *****************************************************************************
101 * This function will allocate memory to store translation tables, depending
102 * of the screen depth.
103 *****************************************************************************/
104 static int yuv_Init( vout_thread_t *p_vout )
106 size_t tables_size; /* tables size, in bytes */
108 /* Computes tables size for 8bbp only */
109 if( p_vout->i_bytes_per_pixel == 1 )
111 tables_size = sizeof( u8 )
112 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
114 /* Allocate memory */
115 p_vout->yuv.p_base = malloc( tables_size );
116 if( p_vout->yuv.p_base == NULL )
118 intf_ErrMsg("error: %s", strerror(ENOMEM));
124 p_vout->yuv.p_base = NULL;
127 /* Allocate memory for conversion buffer and offset array */
128 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
129 if( p_vout->yuv.p_buffer == NULL )
131 intf_ErrMsg("error: %s", strerror(ENOMEM));
132 free( p_vout->yuv.p_base );
135 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
136 if( p_vout->yuv.p_offset == NULL )
138 intf_ErrMsg("error: %s", strerror(ENOMEM));
139 free( p_vout->yuv.p_base );
140 free( p_vout->yuv.p_buffer );
144 /* Initialize tables */
149 /*****************************************************************************
150 * yuv_End: destroy translations tables
151 *****************************************************************************
152 * Free memory allocated by yuv_CCreate.
153 *****************************************************************************/
154 static void yuv_End( vout_thread_t *p_vout )
156 free( p_vout->yuv.p_base );
157 free( p_vout->yuv.p_buffer );
158 free( p_vout->yuv.p_offset );
161 /*****************************************************************************
162 * yuv_Reset: re-initialize translations tables
163 *****************************************************************************
164 * This function will initialize the tables allocated by vout_CreateTables and
165 * set functions pointers.
166 *****************************************************************************/
167 static int yuv_Reset( vout_thread_t *p_vout )
170 return( yuv_Init( p_vout ) );
173 /*****************************************************************************
174 * SetYUV: compute tables and set function pointers
175 *****************************************************************************/
176 static void SetYUV( vout_thread_t *p_vout )
178 int i_index; /* index in tables */
181 * Set pointers and build YUV tables
183 if( p_vout->b_grayscale )
185 /* Grayscale: build gray table */
186 if( p_vout->i_bytes_per_pixel == 1 )
188 u16 bright[256], transp[256];
190 for( i_index = 0; i_index < 256; i_index++)
192 bright[ i_index ] = i_index << 8;
193 transp[ i_index ] = 0;
195 /* the colors have been allocated, we can set the palette */
196 p_vout->pf_setpalette( p_vout, bright, bright, bright, transp );
197 p_vout->i_white_pixel = 0xff;
198 p_vout->i_black_pixel = 0x00;
199 p_vout->i_gray_pixel = 0x44;
200 p_vout->i_blue_pixel = 0x3b;
205 /* Color: build red, green and blue tables */
206 if( p_vout->i_bytes_per_pixel == 1 )
210 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
212 #define U_GREEN_COEF ((int)(-0.391 * (1<<SHIFT) / 1.164))
213 #define U_BLUE_COEF ((int)(2.018 * (1<<SHIFT) / 1.164))
214 #define V_RED_COEF ((int)(1.596 * (1<<SHIFT) / 1.164))
215 #define V_GREEN_COEF ((int)(-0.813 * (1<<SHIFT) / 1.164))
221 u16 red[256], green[256], blue[256], transp[256];
222 unsigned char lookup[PALETTE_TABLE_SIZE];
224 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
226 /* this loop calculates the intersection of an YUV box
227 * and the RGB cube. */
228 for ( y = 0; y <= 256; y += 16 )
230 for ( u = 0; u <= 256; u += 32 )
231 for ( v = 0; v <= 256; v += 32 )
233 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
234 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
235 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
240 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
241 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
243 /* this one should never happen unless someone fscked up my code */
244 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
246 /* clip the colors */
248 green[j] = CLIP( g );
254 p_vout->yuv.yuv.p_rgb8[i++] = j;
260 p_vout->yuv.yuv.p_rgb8[i++] = 0;
266 /* the colors have been allocated, we can set the palette */
267 /* there will eventually be a way to know which colors
268 * couldn't be allocated and try to find a replacement */
269 p_vout->pf_setpalette( p_vout, red, green, blue, transp );
271 p_vout->i_white_pixel = 0xff;
272 p_vout->i_black_pixel = 0x00;
273 p_vout->i_gray_pixel = 0x44;
274 p_vout->i_blue_pixel = 0x3b;
277 /* this loop allocates colors that got outside
279 for ( y = 0; y <= 256; y += 16 )
281 for ( u = 0; u <= 256; u += 32 )
283 for ( v = 0; v <= 256; v += 32 )
286 int dist, mindist = 100000000;
288 if( lookup[i] || y==0)
295 for( u2 = 0; u2 <= 256; u2 += 32 )
296 for( v2 = 0; v2 <= 256; v2 += 32 )
298 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
299 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
301 /* find the nearest color */
304 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
309 /* find the nearest color */
310 if( dist + 128 < mindist )
312 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
313 mindist = dist + 128;
325 * Set functions pointers
329 switch( p_vout->i_bytes_per_pixel)
332 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420YCbr8;
333 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422YCbr8;
334 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444YCbr8;
338 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420YCbr16;
339 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422YCbr16;
340 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444YCbr16;
344 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420YCbr24;
345 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422YCbr24;
346 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444YCbr24;
350 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420YCbr32;
351 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422YCbr32;
352 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444YCbr32;
356 else if( p_vout->b_grayscale )
359 switch( p_vout->i_bytes_per_pixel )
362 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertY4Gray8;
363 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertY4Gray8;
364 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertY4Gray8;
367 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertY4Gray16;
368 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertY4Gray16;
369 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertY4Gray16;
372 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
373 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertY4Gray24;
374 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertY4Gray24;
377 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
378 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertY4Gray32;
379 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertY4Gray32;
386 switch( p_vout->i_bytes_per_pixel )
389 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
390 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
391 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
394 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
395 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
396 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
399 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
400 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
401 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
404 p_vout->yuv.pf_yuv420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
405 p_vout->yuv.pf_yuv422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
406 p_vout->yuv.pf_yuv444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
412 /*****************************************************************************
413 * SetOffset: build offset array for conversion functions
414 *****************************************************************************
415 * This function will build an offset array used in later conversion functions.
416 * It will also set horizontal and vertical scaling indicators. If b_double
417 * is set, the p_offset structure has interleaved Y and U/V offsets.
418 *****************************************************************************/
419 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
420 boolean_t *pb_h_scaling, int *pi_v_scaling,
421 int *p_offset, boolean_t b_double )
423 int i_x; /* x position in destination */
424 int i_scale_count; /* modulo counter */
427 * Prepare horizontal offset array
429 if( i_pic_width - i_width == 0 )
431 /* No horizontal scaling: YUV conversion is done directly to picture */
434 else if( i_pic_width - i_width > 0 )
436 /* Prepare scaling array for horizontal extension */
438 i_scale_count = i_pic_width;
439 for( i_x = i_width; i_x--; )
441 while( (i_scale_count -= i_width) > 0 )
446 i_scale_count += i_pic_width;
449 else /* if( i_pic_width - i_width < 0 ) */
451 /* Prepare scaling array for horizontal reduction */
453 i_scale_count = i_width;
454 for( i_x = i_pic_width; i_x--; )
457 while( (i_scale_count -= i_pic_width) > 0 )
462 i_scale_count += i_width;
467 * Set vertical scaling indicator
469 if( i_pic_height - i_height == 0 )
473 else if( i_pic_height - i_height > 0 )
477 else /* if( i_pic_height - i_height < 0 ) */