]> git.sesse.net Git - vlc/blob - plugins/yuvmmx/video_yuv.c
. rajout de l'option -Winline
[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         pi_gamma[256];                                /* gamma table */
135     int         i_index;                                  /* index in tables */
136
137     /* Build gamma table */
138     SetGammaTable( pi_gamma, p_vout->f_gamma );
139
140     /*
141      * Set pointers and build YUV tables
142      */
143     if( p_vout->b_grayscale )
144     {
145         /* Grayscale: build gray table */
146         if( p_vout->i_bytes_per_pixel == 1 )
147         {
148             u16 bright[256], transp[256];
149
150             for( i_index = 0; i_index < 256; i_index++)
151             {
152                 bright[ i_index ] = i_index << 8;
153                 transp[ i_index ] = 0;
154             }
155             /* the colors have been allocated, we can set the palette */
156             p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
157             p_vout->i_white_pixel = 0xff;
158             p_vout->i_black_pixel = 0x00;
159             p_vout->i_gray_pixel = 0x44;
160             p_vout->i_blue_pixel = 0x3b;
161         }
162     }
163     else
164     {
165         /* Color: build red, green and blue tables */
166         if( p_vout->i_bytes_per_pixel == 1 )
167         {
168             #define RGB_MIN 0
169             #define RGB_MAX 255
170             #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
171             #define SHIFT 20
172             #define U_GREEN_COEF    ((int)(-0.391 * (1<<SHIFT) / 1.164))
173             #define U_BLUE_COEF     ((int)(2.018 * (1<<SHIFT) / 1.164))
174             #define V_RED_COEF      ((int)(1.596 * (1<<SHIFT) / 1.164))
175             #define V_GREEN_COEF    ((int)(-0.813 * (1<<SHIFT) / 1.164))
176
177             int y,u,v;
178             int r,g,b;
179             int uvr, uvg, uvb;
180             int i = 0, j = 0;
181             u16 red[256], green[256], blue[256], transp[256];
182             unsigned char lookup[PALETTE_TABLE_SIZE];
183
184             p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
185
186             /* this loop calculates the intersection of an YUV box
187              * and the RGB cube. */
188             for ( y = 0; y <= 256; y += 16 )
189             {
190                 for ( u = 0; u <= 256; u += 32 )
191                 for ( v = 0; v <= 256; v += 32 )
192                 {
193                     uvr = (V_RED_COEF*(v-128)) >> SHIFT;
194                     uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
195                     uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
196                     r = y + uvr;
197                     g = y + uvg;
198                     b = y + uvb;
199
200                     if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
201                             && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
202                     {
203                         /* this one should never happen unless someone fscked up my code */
204                         if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }
205
206                         /* clip the colors */
207                         red[j] = CLIP( r );
208                         green[j] = CLIP( g );
209                         blue[j] = CLIP( b );
210                         transp[j] = 0;
211
212                         /* allocate color */
213                         lookup[i] = 1;
214                         p_vout->yuv.yuv.p_rgb8[i++] = j;
215                         j++;
216                     }
217                     else
218                     {
219                         lookup[i] = 0;
220                         p_vout->yuv.yuv.p_rgb8[i++] = 0;
221                     }
222                 }
223                 i += 128-81;
224             }
225
226             /* the colors have been allocated, we can set the palette */
227             /* there will eventually be a way to know which colors
228              * couldn't be allocated and try to find a replacement */
229             p_vout->p_set_palette( p_vout, red, green, blue, transp );
230
231             p_vout->i_white_pixel = 0xff;
232             p_vout->i_black_pixel = 0x00;
233             p_vout->i_gray_pixel = 0x44;
234             p_vout->i_blue_pixel = 0x3b;
235
236             i = 0;
237             /* this loop allocates colors that got outside
238              * the RGB cube */
239             for ( y = 0; y <= 256; y += 16 )
240             {
241                 for ( u = 0; u <= 256; u += 32 )
242                 {
243                     for ( v = 0; v <= 256; v += 32 )
244                     {
245                         int u2, v2;
246                         int dist, mindist = 100000000;
247
248                         if( lookup[i] || y==0)
249                         {
250                             i++;
251                             continue;
252                         }
253
254                         /* heavy. yeah. */
255                         for( u2 = 0; u2 <= 256; u2 += 32 )
256                         for( v2 = 0; v2 <= 256; v2 += 32 )
257                         {
258                             j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
259                             dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
260                             if( lookup[j] )
261                             /* find the nearest color */
262                             if( dist < mindist )
263                             {
264                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
265                                 mindist = dist;
266                             }
267                             j -= 128;
268                             if( lookup[j] )
269                             /* find the nearest color */
270                             if( dist + 128 < mindist )
271                             {
272                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
273                                 mindist = dist + 128;
274                             }
275                         }
276                         i++;
277                     }
278                 }
279                 i += 128-81;
280             }
281         }
282     }
283
284     /*
285      * Set functions pointers
286      */
287     if( p_vout->b_grayscale )
288     {
289         /* Grayscale */
290         switch( p_vout->i_bytes_per_pixel )
291         {
292         case 1:
293             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
294             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
295             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
296             break;
297         case 2:
298             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
299             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
300             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
301             break;
302         case 3:
303             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
304             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
305             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
306             break;
307         case 4:
308             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
309             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
310             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
311             break;
312         }
313     }
314     else
315     {
316         /* Color */
317         switch( p_vout->i_bytes_per_pixel )
318         {
319         case 1:
320             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
321             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
322             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
323             break;
324         case 2:
325             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB16;
326             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB16;
327             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB16;
328             break;
329         case 3:
330             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB24;
331             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB24;
332             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB24;
333             break;
334         case 4:
335             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB32;
336             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB32;
337             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB32;
338             break;
339         }
340     }
341 }
342
343 /*****************************************************************************
344  * SetOffset: build offset array for conversion functions
345  *****************************************************************************
346  * This function will build an offset array used in later conversion functions.
347  * It will also set horizontal and vertical scaling indicators.
348  *****************************************************************************/
349 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
350                 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset )
351 {
352     int i_x;                                    /* x position in destination */
353     int i_scale_count;                                     /* modulo counter */
354
355     /*
356      * Prepare horizontal offset array
357      */
358     if( i_pic_width - i_width > 0 )
359     {
360         /* Prepare scaling array for horizontal extension */
361         *pb_h_scaling =  1;
362         i_scale_count =         i_pic_width;
363         for( i_x = i_width; i_x--; )
364         {
365             while( (i_scale_count -= i_width) > 0 )
366             {
367                 *p_offset++ = 0;
368             }
369             *p_offset++ = 1;
370             i_scale_count += i_pic_width;
371         }
372     }
373     else if( i_pic_width - i_width < 0 )
374     {
375         /* Prepare scaling array for horizontal reduction */
376         *pb_h_scaling =  1;
377         i_scale_count =         i_pic_width;
378         for( i_x = i_pic_width; i_x--; )
379         {
380             *p_offset = 1;
381             while( (i_scale_count -= i_pic_width) >= 0 )
382             {
383                 *p_offset += 1;
384             }
385             p_offset++;
386             i_scale_count += i_width;
387         }
388     }
389     else
390     {
391         /* No horizontal scaling: YUV conversion is done directly to picture */
392         *pb_h_scaling = 0;
393     }
394
395     /*
396      * Set vertical scaling indicator
397      */
398     if( i_pic_height - i_height > 0 )
399     {
400         *pi_v_scaling = 1;
401     }
402     else if( i_pic_height - i_height < 0 )
403     {
404         *pi_v_scaling = -1;
405     }
406     else
407     {
408         *pi_v_scaling = 0;
409     }
410 }
411