]> git.sesse.net Git - vlc/blob - plugins/yuv/video_yuvmmx.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[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 #define MODULE_NAME yuvmmx
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <math.h>                                            /* exp(), pow() */
34 #include <errno.h>                                                 /* ENOMEM */
35 #include <stdlib.h>                                                /* free() */
36 #include <string.h>                                            /* strerror() */
37
38 #include "config.h"
39 #include "common.h"
40 #include "threads.h"
41 #include "mtime.h"
42 #include "tests.h"
43
44 #include "modules.h"
45
46 #include "video.h"
47 #include "video_output.h"
48
49 #include "video_common.h"
50
51 #include "intf_msg.h"
52
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 );
57
58 static void    SetYUV         ( vout_thread_t *p_vout );
59
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 )
65 {
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;
70 }
71
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 )
79 {
80     /* Test for MMX support in the CPU */
81     if( TestCPU( CPU_CAPABILITY_MMX ) )
82     {
83         if( TestMethod( YUV_METHOD_VAR, "yuvmmx" ) )
84         {
85             return( 999 );
86         }
87         else
88         {
89             return( 100 );
90         }
91     }
92     else
93     {
94         return( 0 );
95     }
96 }
97
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 )
105 {
106     size_t      tables_size;                        /* tables size, in bytes */
107
108     /* Computes tables size for 8bbp only */
109     if( p_vout->i_bytes_per_pixel == 1 )
110     {
111         tables_size = sizeof( u8 )
112                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
113
114         /* Allocate memory */
115         p_vout->yuv.p_base = malloc( tables_size );
116         if( p_vout->yuv.p_base == NULL )
117         {
118             intf_ErrMsg("error: %s", strerror(ENOMEM));
119             return( 1 );
120         }
121     }
122     else
123     {
124         p_vout->yuv.p_base = NULL;
125     }
126
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 )
130     {
131         intf_ErrMsg("error: %s", strerror(ENOMEM));
132         free( p_vout->yuv.p_base );
133         return( 1 );
134     }
135     p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
136     if( p_vout->yuv.p_offset == NULL )
137     {
138         intf_ErrMsg("error: %s", strerror(ENOMEM));
139         free( p_vout->yuv.p_base );
140         free( p_vout->yuv.p_buffer );
141         return( 1 );
142     }
143
144     /* Initialize tables */
145     SetYUV( p_vout );
146     return( 0 );
147 }
148
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 )
155 {
156     free( p_vout->yuv.p_base );
157     free( p_vout->yuv.p_buffer );
158     free( p_vout->yuv.p_offset );
159 }
160
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 )
168 {
169     yuv_End( p_vout );
170     return( yuv_Init( p_vout ) );
171 }
172
173 /*****************************************************************************
174  * SetYUV: compute tables and set function pointers
175  *****************************************************************************/
176 static void SetYUV( vout_thread_t *p_vout )
177 {
178     int         i_index;                                  /* index in tables */
179
180     /*
181      * Set pointers and build YUV tables
182      */
183     if( p_vout->b_grayscale )
184     {
185         /* Grayscale: build gray table */
186         if( p_vout->i_bytes_per_pixel == 1 )
187         {
188             u16 bright[256], transp[256];
189
190             for( i_index = 0; i_index < 256; i_index++)
191             {
192                 bright[ i_index ] = i_index << 8;
193                 transp[ i_index ] = 0;
194             }
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;
201         }
202     }
203     else
204     {
205         /* Color: build red, green and blue tables */
206         if( p_vout->i_bytes_per_pixel == 1 )
207         {
208             #define RGB_MIN 0
209             #define RGB_MAX 255
210             #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
211             #define SHIFT 20
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))
216
217             int y,u,v;
218             int r,g,b;
219             int uvr, uvg, uvb;
220             int i = 0, j = 0;
221             u16 red[256], green[256], blue[256], transp[256];
222             unsigned char lookup[PALETTE_TABLE_SIZE];
223
224             p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
225
226             /* this loop calculates the intersection of an YUV box
227              * and the RGB cube. */
228             for ( y = 0; y <= 256; y += 16 )
229             {
230                 for ( u = 0; u <= 256; u += 32 )
231                 for ( v = 0; v <= 256; v += 32 )
232                 {
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;
236                     r = y + uvr;
237                     g = y + uvg;
238                     b = y + uvb;
239
240                     if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
241                             && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
242                     {
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; }
245
246                         /* clip the colors */
247                         red[j] = CLIP( r );
248                         green[j] = CLIP( g );
249                         blue[j] = CLIP( b );
250                         transp[j] = 0;
251
252                         /* allocate color */
253                         lookup[i] = 1;
254                         p_vout->yuv.yuv.p_rgb8[i++] = j;
255                         j++;
256                     }
257                     else
258                     {
259                         lookup[i] = 0;
260                         p_vout->yuv.yuv.p_rgb8[i++] = 0;
261                     }
262                 }
263                 i += 128-81;
264             }
265
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 );
270
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;
275
276             i = 0;
277             /* this loop allocates colors that got outside
278              * the RGB cube */
279             for ( y = 0; y <= 256; y += 16 )
280             {
281                 for ( u = 0; u <= 256; u += 32 )
282                 {
283                     for ( v = 0; v <= 256; v += 32 )
284                     {
285                         int u2, v2;
286                         int dist, mindist = 100000000;
287
288                         if( lookup[i] || y==0)
289                         {
290                             i++;
291                             continue;
292                         }
293
294                         /* heavy. yeah. */
295                         for( u2 = 0; u2 <= 256; u2 += 32 )
296                         for( v2 = 0; v2 <= 256; v2 += 32 )
297                         {
298                             j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
299                             dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
300                             if( lookup[j] )
301                             /* find the nearest color */
302                             if( dist < mindist )
303                             {
304                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
305                                 mindist = dist;
306                             }
307                             j -= 128;
308                             if( lookup[j] )
309                             /* find the nearest color */
310                             if( dist + 128 < mindist )
311                             {
312                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
313                                 mindist = dist + 128;
314                             }
315                         }
316                         i++;
317                     }
318                 }
319                 i += 128-81;
320             }
321         }
322     }
323
324     /*
325      * Set functions pointers
326      */
327     if( p_vout->b_grayscale )
328     {
329         /* Grayscale */
330         switch( p_vout->i_bytes_per_pixel )
331         {
332         case 1:
333             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
334             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
335             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
336             break;
337         case 2:
338             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
339             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
340             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
341             break;
342         case 3:
343             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
344             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
345             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
346             break;
347         case 4:
348             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
349             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
350             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
351             break;
352         }
353     }
354     else
355     {
356         /* Color */
357         switch( p_vout->i_bytes_per_pixel )
358         {
359         case 1:
360             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
361             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
362             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
363             break;
364         case 2:
365             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB16;
366             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB16;
367             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB16;
368             break;
369         case 3:
370             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB24;
371             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB24;
372             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB24;
373             break;
374         case 4:
375             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB32;
376             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB32;
377             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB32;
378             break;
379         }
380     }
381 }
382
383 /*****************************************************************************
384  * SetOffset: build offset array for conversion functions
385  *****************************************************************************
386  * This function will build an offset array used in later conversion functions.
387  * It will also set horizontal and vertical scaling indicators. If b_double
388  * is set, the p_offset structure has interleaved Y and U/V offsets.
389  *****************************************************************************/
390 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
391                 boolean_t *pb_h_scaling, int *pi_v_scaling,
392                 int *p_offset, boolean_t b_double )
393 {
394     int i_x;                                    /* x position in destination */
395     int i_scale_count;                                     /* modulo counter */
396
397     /*
398      * Prepare horizontal offset array
399      */
400     if( i_pic_width - i_width == 0 )
401     {
402         /* No horizontal scaling: YUV conversion is done directly to picture */
403         *pb_h_scaling = 0;
404     }
405     else if( i_pic_width - i_width > 0 )
406     {
407         /* Prepare scaling array for horizontal extension */
408         *pb_h_scaling =  1;
409         i_scale_count =  i_pic_width;
410         for( i_x = i_width; i_x--; )
411         {
412             while( (i_scale_count -= i_width) > 0 )
413             {
414                 *p_offset++ = 0;
415             }
416             *p_offset++ = 1;
417             i_scale_count += i_pic_width;
418         }
419     }
420     else /* if( i_pic_width - i_width < 0 ) */
421     {
422         /* Prepare scaling array for horizontal reduction */
423         *pb_h_scaling =  1;
424         i_scale_count =  i_width;
425         for( i_x = i_pic_width; i_x--; )
426         {
427             *p_offset = 1;
428             while( (i_scale_count -= i_pic_width) > 0 )
429             {
430                 *p_offset += 1;
431             }
432             p_offset++;
433             i_scale_count += i_width;
434         }
435     }
436
437     /*
438      * Set vertical scaling indicator
439      */
440     if( i_pic_height - i_height == 0 )
441     {
442         *pi_v_scaling = 0;
443     }
444     else if( i_pic_height - i_height > 0 )
445     {
446         *pi_v_scaling = 1;
447     }
448     else /* if( i_pic_height - i_height < 0 ) */
449     {
450         *pi_v_scaling = -1;
451     }
452 }
453