]> git.sesse.net Git - vlc/blob - plugins/yuvmmx/video_yuv.c
* vlc.init becomes ~/.vlcrc
[vlc] / plugins / yuvmmx / video_yuv.c
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
8  *
9  * Authors:
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <math.h>                                            /* exp(), pow() */
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                            /* strerror() */
36
37 #include "config.h"
38 #include "common.h"
39 #include "threads.h"
40 #include "mtime.h"
41 #include "plugins.h"
42 #include "video.h"
43 #include "video_output.h"
44 #include "video_yuv.h"
45
46 #include "intf_msg.h"
47
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 )
55 {
56     size_t      tables_size;                        /* tables size, in bytes */
57
58     /* Computes tables size for 8bbp only */
59     if( p_vout->i_bytes_per_pixel == 1 )
60     {
61         tables_size = sizeof( u8 )
62                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
63
64         /* Allocate memory */
65         p_vout->yuv.p_base = malloc( tables_size );
66         if( p_vout->yuv.p_base == NULL )
67         {
68             intf_ErrMsg("error: %s\n", strerror(ENOMEM));
69             return( 1 );
70         }
71     }
72     else
73     {
74         p_vout->yuv.p_base = NULL;
75     }
76
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 )
80     {
81         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
82         free( p_vout->yuv.p_base );
83         return( 1 );
84     }
85     p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
86     if( p_vout->yuv.p_offset == NULL )
87     {
88         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
89         free( p_vout->yuv.p_base );
90         free( p_vout->yuv.p_buffer );
91         return( 1 );
92     }
93
94     /* Initialize tables */
95     SetYUV( p_vout );
96     return( 0 );
97 }
98
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 )
105 {
106     if( p_vout->i_bytes_per_pixel == 1 )
107     {
108         free( p_vout->yuv.p_base );
109     }
110
111     free( p_vout->yuv.p_buffer );
112     free( p_vout->yuv.p_offset );
113 }
114
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 )
122 {
123     yuv_MMXEnd( p_vout );
124     return( yuv_MMXInit( p_vout ) );
125 }
126
127 /* following functions are local */
128
129 /*****************************************************************************
130  * SetYUV: compute tables and set function pointers
131 + *****************************************************************************/
132 void SetYUV( vout_thread_t *p_vout )
133 {
134     int         i_index;                                  /* index in tables */
135
136     /*
137      * Set pointers and build YUV tables
138      */
139     if( p_vout->b_grayscale )
140     {
141         /* Grayscale: build gray table */
142         if( p_vout->i_bytes_per_pixel == 1 )
143         {
144             u16 bright[256], transp[256];
145
146             for( i_index = 0; i_index < 256; i_index++)
147             {
148                 bright[ i_index ] = i_index << 8;
149                 transp[ i_index ] = 0;
150             }
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;
157         }
158     }
159     else
160     {
161         /* Color: build red, green and blue tables */
162         if( p_vout->i_bytes_per_pixel == 1 )
163         {
164             #define RGB_MIN 0
165             #define RGB_MAX 255
166             #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
167             #define SHIFT 20
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))
172
173             int y,u,v;
174             int r,g,b;
175             int uvr, uvg, uvb;
176             int i = 0, j = 0;
177             u16 red[256], green[256], blue[256], transp[256];
178             unsigned char lookup[PALETTE_TABLE_SIZE];
179
180             p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
181
182             /* this loop calculates the intersection of an YUV box
183              * and the RGB cube. */
184             for ( y = 0; y <= 256; y += 16 )
185             {
186                 for ( u = 0; u <= 256; u += 32 )
187                 for ( v = 0; v <= 256; v += 32 )
188                 {
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;
192                     r = y + uvr;
193                     g = y + uvg;
194                     b = y + uvb;
195
196                     if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
197                             && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
198                     {
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; }
201
202                         /* clip the colors */
203                         red[j] = CLIP( r );
204                         green[j] = CLIP( g );
205                         blue[j] = CLIP( b );
206                         transp[j] = 0;
207
208                         /* allocate color */
209                         lookup[i] = 1;
210                         p_vout->yuv.yuv.p_rgb8[i++] = j;
211                         j++;
212                     }
213                     else
214                     {
215                         lookup[i] = 0;
216                         p_vout->yuv.yuv.p_rgb8[i++] = 0;
217                     }
218                 }
219                 i += 128-81;
220             }
221
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 );
226
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;
231
232             i = 0;
233             /* this loop allocates colors that got outside
234              * the RGB cube */
235             for ( y = 0; y <= 256; y += 16 )
236             {
237                 for ( u = 0; u <= 256; u += 32 )
238                 {
239                     for ( v = 0; v <= 256; v += 32 )
240                     {
241                         int u2, v2;
242                         int dist, mindist = 100000000;
243
244                         if( lookup[i] || y==0)
245                         {
246                             i++;
247                             continue;
248                         }
249
250                         /* heavy. yeah. */
251                         for( u2 = 0; u2 <= 256; u2 += 32 )
252                         for( v2 = 0; v2 <= 256; v2 += 32 )
253                         {
254                             j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
255                             dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
256                             if( lookup[j] )
257                             /* find the nearest color */
258                             if( dist < mindist )
259                             {
260                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
261                                 mindist = dist;
262                             }
263                             j -= 128;
264                             if( lookup[j] )
265                             /* find the nearest color */
266                             if( dist + 128 < mindist )
267                             {
268                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
269                                 mindist = dist + 128;
270                             }
271                         }
272                         i++;
273                     }
274                 }
275                 i += 128-81;
276             }
277         }
278     }
279
280     /*
281      * Set functions pointers
282      */
283     if( p_vout->b_grayscale )
284     {
285         /* Grayscale */
286         switch( p_vout->i_bytes_per_pixel )
287         {
288         case 1:
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;
292             break;
293         case 2:
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;
297             break;
298         case 3:
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;
302             break;
303         case 4:
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;
307             break;
308         }
309     }
310     else
311     {
312         /* Color */
313         switch( p_vout->i_bytes_per_pixel )
314         {
315         case 1:
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;
319             break;
320         case 2:
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;
324             break;
325         case 3:
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;
329             break;
330         case 4:
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;
334             break;
335         }
336     }
337 }
338
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 )
347 {
348     int i_x;                                    /* x position in destination */
349     int i_scale_count;                                     /* modulo counter */
350
351     /*
352      * Prepare horizontal offset array
353      */
354     if( i_pic_width - i_width > 0 )
355     {
356         /* Prepare scaling array for horizontal extension */
357         *pb_h_scaling =  1;
358         i_scale_count =         i_pic_width;
359         for( i_x = i_width; i_x--; )
360         {
361             while( (i_scale_count -= i_width) > 0 )
362             {
363                 *p_offset++ = 0;
364             }
365             *p_offset++ = 1;
366             i_scale_count += i_pic_width;
367         }
368     }
369     else if( i_pic_width - i_width < 0 )
370     {
371         /* Prepare scaling array for horizontal reduction */
372         *pb_h_scaling =  1;
373         i_scale_count =         i_pic_width;
374         for( i_x = i_pic_width; i_x--; )
375         {
376             *p_offset = 1;
377             while( (i_scale_count -= i_pic_width) >= 0 )
378             {
379                 *p_offset += 1;
380             }
381             p_offset++;
382             i_scale_count += i_width;
383         }
384     }
385     else
386     {
387         /* No horizontal scaling: YUV conversion is done directly to picture */
388         *pb_h_scaling = 0;
389     }
390
391     /*
392      * Set vertical scaling indicator
393      */
394     if( i_pic_height - i_height > 0 )
395     {
396         *pi_v_scaling = 1;
397     }
398     else if( i_pic_height - i_height < 0 )
399     {
400         *pi_v_scaling = -1;
401     }
402     else
403     {
404         *pi_v_scaling = 0;
405     }
406 }
407