]> git.sesse.net Git - vlc/blob - plugins/yuv/video_yuvmmx.c
[ include/config.h.in has changed, don't forget tu run ./configure ]
[vlc] / plugins / yuv / video_yuvmmx.c
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  *
7  * Authors:
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <math.h>                                            /* exp(), pow() */
31 #include <errno.h>                                                 /* ENOMEM */
32 #include <stdlib.h>                                                /* free() */
33 #include <string.h>                                            /* strerror() */
34
35 #include "config.h"
36 #include "common.h"
37 #include "threads.h"
38 #include "mtime.h"
39 #include "tests.h"
40
41 #include "modules.h"
42
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "video_common.h"
47
48 #include "intf_msg.h"
49
50 static int     yuv_Probe      ( probedata_t *p_data );
51 static int     yuv_Init       ( vout_thread_t *p_vout );
52 static int     yuv_Reset      ( vout_thread_t *p_vout );
53 static void    yuv_End        ( vout_thread_t *p_vout );
54
55 static void    SetYUV         ( vout_thread_t *p_vout );
56
57 /*****************************************************************************
58  * Functions exported as capabilities. They are declared as static so that
59  * we don't pollute the namespace too much.
60  *****************************************************************************/
61 void yuv_getfunctions( function_list_t * p_function_list )
62 {
63     p_function_list->pf_probe = yuv_Probe;
64     p_function_list->functions.yuv.pf_init = yuv_Init;
65     p_function_list->functions.yuv.pf_reset = yuv_Reset;
66     p_function_list->functions.yuv.pf_end = yuv_End;
67 }
68
69 /*****************************************************************************
70  * yuv_Probe: tests probe the audio device and return a score
71  *****************************************************************************
72  * This function tries to open the DSP and returns a score to the plugin
73  * manager so that it can choose the most appropriate one.
74  *****************************************************************************/
75 static int yuv_Probe( probedata_t *p_data )
76 {
77     /* Test for MMX support in the CPU */
78     if( TestCPU() & CPU_CAPABILITY_MMX )
79     {
80         if( TestMethod( YUV_METHOD_VAR, "yuvmmx" ) )
81         {
82             return( 999 );
83         }
84         else
85         {
86             return( 100 );
87         }
88     }
89     else
90     {
91         return( 0 );
92     }
93 }
94
95 /*****************************************************************************
96  * yuv_Init: allocate and initialize translations tables
97  *****************************************************************************
98  * This function will allocate memory to store translation tables, depending
99  * of the screen depth.
100  *****************************************************************************/
101 static int yuv_Init( vout_thread_t *p_vout )
102 {
103     size_t      tables_size;                        /* tables size, in bytes */
104
105     /* Computes tables size for 8bbp only */
106     if( p_vout->i_bytes_per_pixel == 1 )
107     {
108         tables_size = sizeof( u8 )
109                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
110
111         /* Allocate memory */
112         p_vout->yuv.p_base = malloc( tables_size );
113         if( p_vout->yuv.p_base == NULL )
114         {
115             intf_ErrMsg("error: %s", strerror(ENOMEM));
116             return( 1 );
117         }
118     }
119     else
120     {
121         p_vout->yuv.p_base = NULL;
122     }
123
124     /* Allocate memory for conversion buffer and offset array */
125     p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
126     if( p_vout->yuv.p_buffer == NULL )
127     {
128         intf_ErrMsg("error: %s", strerror(ENOMEM));
129         free( p_vout->yuv.p_base );
130         return( 1 );
131     }
132     p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
133     if( p_vout->yuv.p_offset == NULL )
134     {
135         intf_ErrMsg("error: %s", strerror(ENOMEM));
136         free( p_vout->yuv.p_base );
137         free( p_vout->yuv.p_buffer );
138         return( 1 );
139     }
140
141     /* Initialize tables */
142     SetYUV( p_vout );
143     return( 0 );
144 }
145
146 /*****************************************************************************
147  * yuv_End: destroy translations tables
148  *****************************************************************************
149  * Free memory allocated by yuv_CCreate.
150  *****************************************************************************/
151 static void yuv_End( vout_thread_t *p_vout )
152 {
153     free( p_vout->yuv.p_base );
154     free( p_vout->yuv.p_buffer );
155     free( p_vout->yuv.p_offset );
156 }
157
158 /*****************************************************************************
159  * yuv_Reset: re-initialize translations tables
160  *****************************************************************************
161  * This function will initialize the tables allocated by vout_CreateTables and
162  * set functions pointers.
163  *****************************************************************************/
164 static int yuv_Reset( vout_thread_t *p_vout )
165 {
166     yuv_End( p_vout );
167     return( yuv_Init( p_vout ) );
168 }
169
170 /*****************************************************************************
171  * SetYUV: compute tables and set function pointers
172  *****************************************************************************/
173 static void SetYUV( vout_thread_t *p_vout )
174 {
175     int         i_index;                                  /* index in tables */
176
177     /*
178      * Set pointers and build YUV tables
179      */
180     if( p_vout->b_grayscale )
181     {
182         /* Grayscale: build gray table */
183         if( p_vout->i_bytes_per_pixel == 1 )
184         {
185             u16 bright[256], transp[256];
186
187             for( i_index = 0; i_index < 256; i_index++)
188             {
189                 bright[ i_index ] = i_index << 8;
190                 transp[ i_index ] = 0;
191             }
192             /* the colors have been allocated, we can set the palette */
193             p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
194             p_vout->i_white_pixel = 0xff;
195             p_vout->i_black_pixel = 0x00;
196             p_vout->i_gray_pixel = 0x44;
197             p_vout->i_blue_pixel = 0x3b;
198         }
199     }
200     else
201     {
202         /* Color: build red, green and blue tables */
203         if( p_vout->i_bytes_per_pixel == 1 )
204         {
205             #define RGB_MIN 0
206             #define RGB_MAX 255
207             #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
208             #define SHIFT 20
209             #define U_GREEN_COEF    ((int)(-0.391 * (1<<SHIFT) / 1.164))
210             #define U_BLUE_COEF     ((int)(2.018 * (1<<SHIFT) / 1.164))
211             #define V_RED_COEF      ((int)(1.596 * (1<<SHIFT) / 1.164))
212             #define V_GREEN_COEF    ((int)(-0.813 * (1<<SHIFT) / 1.164))
213
214             int y,u,v;
215             int r,g,b;
216             int uvr, uvg, uvb;
217             int i = 0, j = 0;
218             u16 red[256], green[256], blue[256], transp[256];
219             unsigned char lookup[PALETTE_TABLE_SIZE];
220
221             p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
222
223             /* this loop calculates the intersection of an YUV box
224              * and the RGB cube. */
225             for ( y = 0; y <= 256; y += 16 )
226             {
227                 for ( u = 0; u <= 256; u += 32 )
228                 for ( v = 0; v <= 256; v += 32 )
229                 {
230                     uvr = (V_RED_COEF*(v-128)) >> SHIFT;
231                     uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
232                     uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
233                     r = y + uvr;
234                     g = y + uvg;
235                     b = y + uvb;
236
237                     if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
238                             && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
239                     {
240                         /* this one should never happen unless someone fscked up my code */
241                         if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
242
243                         /* clip the colors */
244                         red[j] = CLIP( r );
245                         green[j] = CLIP( g );
246                         blue[j] = CLIP( b );
247                         transp[j] = 0;
248
249                         /* allocate color */
250                         lookup[i] = 1;
251                         p_vout->yuv.yuv.p_rgb8[i++] = j;
252                         j++;
253                     }
254                     else
255                     {
256                         lookup[i] = 0;
257                         p_vout->yuv.yuv.p_rgb8[i++] = 0;
258                     }
259                 }
260                 i += 128-81;
261             }
262
263             /* the colors have been allocated, we can set the palette */
264             /* there will eventually be a way to know which colors
265              * couldn't be allocated and try to find a replacement */
266             p_vout->p_set_palette( p_vout, red, green, blue, transp );
267
268             p_vout->i_white_pixel = 0xff;
269             p_vout->i_black_pixel = 0x00;
270             p_vout->i_gray_pixel = 0x44;
271             p_vout->i_blue_pixel = 0x3b;
272
273             i = 0;
274             /* this loop allocates colors that got outside
275              * the RGB cube */
276             for ( y = 0; y <= 256; y += 16 )
277             {
278                 for ( u = 0; u <= 256; u += 32 )
279                 {
280                     for ( v = 0; v <= 256; v += 32 )
281                     {
282                         int u2, v2;
283                         int dist, mindist = 100000000;
284
285                         if( lookup[i] || y==0)
286                         {
287                             i++;
288                             continue;
289                         }
290
291                         /* heavy. yeah. */
292                         for( u2 = 0; u2 <= 256; u2 += 32 )
293                         for( v2 = 0; v2 <= 256; v2 += 32 )
294                         {
295                             j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
296                             dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
297                             if( lookup[j] )
298                             /* find the nearest color */
299                             if( dist < mindist )
300                             {
301                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
302                                 mindist = dist;
303                             }
304                             j -= 128;
305                             if( lookup[j] )
306                             /* find the nearest color */
307                             if( dist + 128 < mindist )
308                             {
309                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
310                                 mindist = dist + 128;
311                             }
312                         }
313                         i++;
314                     }
315                 }
316                 i += 128-81;
317             }
318         }
319     }
320
321     /*
322      * Set functions pointers
323      */
324     if( p_vout->b_grayscale )
325     {
326         /* Grayscale */
327         switch( p_vout->i_bytes_per_pixel )
328         {
329         case 1:
330             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
331             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
332             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
333             break;
334         case 2:
335             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
336             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
337             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
338             break;
339         case 3:
340             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
341             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
342             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
343             break;
344         case 4:
345             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
346             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
347             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
348             break;
349         }
350     }
351     else
352     {
353         /* Color */
354         switch( p_vout->i_bytes_per_pixel )
355         {
356         case 1:
357             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
358             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
359             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
360             break;
361         case 2:
362             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB16;
363             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB16;
364             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB16;
365             break;
366         case 3:
367             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB24;
368             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB24;
369             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB24;
370             break;
371         case 4:
372             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB32;
373             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB32;
374             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB32;
375             break;
376         }
377     }
378 }
379
380 /*****************************************************************************
381  * SetOffset: build offset array for conversion functions
382  *****************************************************************************
383  * This function will build an offset array used in later conversion functions.
384  * It will also set horizontal and vertical scaling indicators. If b_double
385  * is set, the p_offset structure has interleaved Y and U/V offsets.
386  *****************************************************************************/
387 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
388                 boolean_t *pb_h_scaling, int *pi_v_scaling,
389                 int *p_offset, boolean_t b_double )
390 {
391     int i_x;                                    /* x position in destination */
392     int i_scale_count;                                     /* modulo counter */
393
394     /*
395      * Prepare horizontal offset array
396      */
397     if( i_pic_width - i_width == 0 )
398     {
399         /* No horizontal scaling: YUV conversion is done directly to picture */
400         *pb_h_scaling = 0;
401     }
402     else if( i_pic_width - i_width > 0 )
403     {
404         /* Prepare scaling array for horizontal extension */
405         *pb_h_scaling =  1;
406         i_scale_count =  i_pic_width;
407         for( i_x = i_width; i_x--; )
408         {
409             while( (i_scale_count -= i_width) > 0 )
410             {
411                 *p_offset++ = 0;
412             }
413             *p_offset++ = 1;
414             i_scale_count += i_pic_width;
415         }
416     }
417     else /* if( i_pic_width - i_width < 0 ) */
418     {
419         /* Prepare scaling array for horizontal reduction */
420         *pb_h_scaling =  1;
421         i_scale_count =  i_width;
422         for( i_x = i_pic_width; i_x--; )
423         {
424             *p_offset = 1;
425             while( (i_scale_count -= i_pic_width) > 0 )
426             {
427                 *p_offset += 1;
428             }
429             p_offset++;
430             i_scale_count += i_width;
431         }
432     }
433
434     /*
435      * Set vertical scaling indicator
436      */
437     if( i_pic_height - i_height == 0 )
438     {
439         *pi_v_scaling = 0;
440     }
441     else if( i_pic_height - i_height > 0 )
442     {
443         *pi_v_scaling = 1;
444     }
445     else /* if( i_pic_height - i_height < 0 ) */
446     {
447         *pi_v_scaling = -1;
448     }
449 }
450