]> git.sesse.net Git - vlc/blob - plugins/yuvmmx/video_yuv.c
4937a19f2c7fcd265e6142df7ed26e087200970d
[vlc] / plugins / yuvmmx / video_yuv.c
1 /*****************************************************************************
2  * video_yuv.c: 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 - 3 Bpp use 32 bits pixel entries in tables */
59     switch( p_vout->i_bytes_per_pixel )
60     {
61     case 1:
62         tables_size = sizeof( u8 )
63                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
64         break;
65     case 2:
66         tables_size = sizeof( u16 )
67                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
68         break;
69     case 3:
70     case 4:
71     default:
72         tables_size = sizeof( u32 )
73                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
74         break;
75     }
76
77     /* Allocate memory */
78     p_vout->yuv.p_base = malloc( tables_size );
79     if( p_vout->yuv.p_base == NULL )
80     {
81         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
82         return( 1 );
83     }
84
85     /* Allocate memory for conversion buffer and offset array */
86     p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
87     if( p_vout->yuv.p_buffer == NULL )
88     {
89         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
90         free( p_vout->yuv.p_base );
91         return( 1 );
92     }
93     p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
94     if( p_vout->yuv.p_offset == NULL )
95     {
96         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
97         free( p_vout->yuv.p_base );
98         free( p_vout->yuv.p_buffer );
99         return( 1 );
100     }
101
102     /* Initialize tables */
103     SetYUV( p_vout );
104     return( 0 );
105 }
106
107 /*****************************************************************************
108  * yuv_MMXEnd: destroy translations tables
109  *****************************************************************************
110  * Free memory allocated by yuv_MMXCreate.
111  *****************************************************************************/
112 void yuv_MMXEnd( vout_thread_t *p_vout )
113 {
114     free( p_vout->yuv.p_base );
115     free( p_vout->yuv.p_buffer );
116     free( p_vout->yuv.p_offset );
117 }
118
119 /*****************************************************************************
120  * yuv_MMXReset: re-initialize translations tables
121  *****************************************************************************
122  * This function will initialize the tables allocated by vout_CreateTables and
123  * set functions pointers.
124  *****************************************************************************/
125 int yuv_MMXReset( vout_thread_t *p_vout )
126 {
127     yuv_MMXEnd( p_vout );
128     return( yuv_MMXInit( p_vout ) );
129 }
130
131 /* following functions are local */
132
133 /*****************************************************************************
134  * SetGammaTable: return intensity table transformed by gamma curve.
135  *****************************************************************************
136  * pi_table is a table of 256 entries from 0 to 255.
137  *****************************************************************************/
138 void SetGammaTable( int *pi_table, double f_gamma )
139 {
140     int         i_y;                                       /* base intensity */
141
142     /* Use exp(gamma) instead of gamma */
143     f_gamma = exp( f_gamma );
144
145     /* Build gamma table */
146     for( i_y = 0; i_y < 256; i_y++ )
147     {
148         pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
149     }
150  }
151
152 /*****************************************************************************
153  * SetYUV: compute tables and set function pointers
154 + *****************************************************************************/
155 void SetYUV( vout_thread_t *p_vout )
156 {
157     int         pi_gamma[256];                                /* gamma table */
158     int         i_index;                                  /* index in tables */
159
160     /* Build gamma table */
161     SetGammaTable( pi_gamma, p_vout->f_gamma );
162
163     /*
164      * Set pointers and build YUV tables
165      */
166     if( p_vout->b_grayscale )
167     {
168         /* Grayscale: build gray table */
169         switch( p_vout->i_bytes_per_pixel )
170         {
171         case 1:
172             {
173                 u16 bright[256], transp[256];
174
175                 p_vout->yuv.yuv.p_gray8 =  (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
176                 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
177                 {
178                     p_vout->yuv.yuv.p_gray8[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
179                     p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
180                 }
181                 for( i_index = 0; i_index < 256; i_index++)
182                 {
183                     p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
184                     bright[ i_index ] = i_index << 8;
185                     transp[ i_index ] = 0;
186                 }
187                 /* the colors have been allocated, we can set the palette */
188                 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
189                 p_vout->i_white_pixel = 0xff;
190                 p_vout->i_black_pixel = 0x00;
191                 p_vout->i_gray_pixel = 0x44;
192                 p_vout->i_blue_pixel = 0x3b;
193
194                 break;
195             }
196         case 2:
197             p_vout->yuv.yuv.p_gray16 =  (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
198             for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
199             {
200                 p_vout->yuv.yuv.p_gray16[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
201                 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
202             }
203             for( i_index = 0; i_index < 256; i_index++)
204             {
205                 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
206             }
207             break;
208         case 3:
209         case 4:
210             p_vout->yuv.yuv.p_gray32 =  (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
211             for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
212             {
213                 p_vout->yuv.yuv.p_gray32[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
214                 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
215             }
216             for( i_index = 0; i_index < 256; i_index++)
217             {
218                 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
219             }
220             break;
221          }
222     }
223     else
224     {
225         /* Color: build red, green and blue tables */
226         switch( p_vout->i_bytes_per_pixel )
227         {
228         case 1:
229             {
230                 #define RGB_MIN 0
231                 #define RGB_MAX 255
232                 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
233
234                 int y,u,v;
235                 int r,g,b;
236                 int uvr, uvg, uvb;
237                 int i = 0, j = 0;
238                 u16 red[256], green[256], blue[256], transp[256];
239                 unsigned char lookup[PALETTE_TABLE_SIZE];
240
241                 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
242
243                 /* this loop calculates the intersection of an YUV box
244                  * and the RGB cube. */
245                 for ( y = 0; y <= 256; y += 16 )
246                 {
247                     for ( u = 0; u <= 256; u += 32 )
248                     for ( v = 0; v <= 256; v += 32 )
249                     {
250                         uvr = (V_RED_COEF*(v-128)) >> SHIFT;
251                         uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
252                         uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
253                         r = y + uvr;
254                         g = y + uvg;
255                         b = y + uvb;
256
257                         if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
258                                 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
259                         {
260                             /* this one should never happen unless someone fscked up my code */
261                             if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }
262
263                             /* clip the colors */
264                             red[j] = CLIP( r );
265                             green[j] = CLIP( g );
266                             blue[j] = CLIP( b );
267                             transp[j] = 0;
268
269                             /* allocate color */
270                             lookup[i] = 1;
271                             p_vout->yuv.yuv.p_rgb8[i++] = j;
272                             j++;
273                         }
274                         else
275                         {
276                             lookup[i] = 0;
277                             p_vout->yuv.yuv.p_rgb8[i++] = 0;
278                         }
279                     }
280                     i += 128-81;
281                 }
282
283                 /* the colors have been allocated, we can set the palette */
284                 /* there will eventually be a way to know which colors
285                  * couldn't be allocated and try to find a replacement */
286                 p_vout->p_set_palette( p_vout, red, green, blue, transp );
287
288                 p_vout->i_white_pixel = 0xff;
289                 p_vout->i_black_pixel = 0x00;
290                 p_vout->i_gray_pixel = 0x44;
291                 p_vout->i_blue_pixel = 0x3b;
292
293                 i = 0;
294                 /* this loop allocates colors that got outside
295                  * the RGB cube */
296                 for ( y = 0; y <= 256; y += 16 )
297                 {
298                     for ( u = 0; u <= 256; u += 32 )
299                     for ( v = 0; v <= 256; v += 32 )
300                     {
301                         int u2, v2;
302                         int dist, mindist = 100000000;
303
304                         if( lookup[i] || y==0)
305                         {
306                             i++;
307                             continue;
308                         }
309
310                         /* heavy. yeah. */
311                         for( u2 = 0; u2 <= 256; u2 += 32 )
312                         for( v2 = 0; v2 <= 256; v2 += 32 )
313                         {
314                             j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
315                             dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
316                             if( lookup[j] )
317                             /* find the nearest color */
318                             if( dist < mindist )
319                             {
320                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
321                                 mindist = dist;
322                             }
323                             j -= 128;
324                             if( lookup[j] )
325                             /* find the nearest color */
326                             if( dist + 128 < mindist )
327                             {
328                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
329                                 mindist = dist + 128;
330                             }
331                         }
332                         i++;
333                     }
334                     i += 128-81;
335                 }
336
337                 break;
338             }
339         case 2:
340             p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
341             for( i_index = 0; i_index < RED_MARGIN; i_index++ )
342             {
343                 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
344                 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
345             }
346             for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
347             {
348                 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
349                 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
350             }
351             for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
352             {
353                 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
354                 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
355             }
356             for( i_index = 0; i_index < 256; i_index++ )
357             {
358                 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
359                 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
360                 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
361             }
362             break;
363         case 3:
364         case 4:
365             p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
366             for( i_index = 0; i_index < RED_MARGIN; i_index++ )
367             {
368                 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
369                 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
370             }
371             for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
372             {
373                 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
374                 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
375             }
376             for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
377             {
378                 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
379                 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
380             }
381             for( i_index = 0; i_index < 256; i_index++ )
382             {
383                 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
384                 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
385                 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
386             }
387             break;
388         }
389     }
390
391     /*
392      * Set functions pointers
393      */
394     if( p_vout->b_grayscale )
395     {
396         /* Grayscale */
397         switch( p_vout->i_bytes_per_pixel )
398         {
399         case 1:
400             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
401             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
402             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
403             break;
404         case 2:
405             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
406             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
407             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
408             break;
409         case 3:
410             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
411             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
412             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
413             break;
414         case 4:
415             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
416             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
417             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
418             break;
419         }
420     }
421     else
422     {
423         /* Color */
424         switch( p_vout->i_bytes_per_pixel )
425         {
426         case 1:
427             p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
428             p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
429             p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
430             break;
431         case 2:
432             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB16;
433             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB16;
434             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB16;
435             break;
436         case 3:
437             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB24;
438             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB24;
439             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB24;
440             break;
441         case 4:
442             p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB32;
443             p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB32;
444             p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB32;
445             break;
446         }
447     }
448 }
449
450 /*****************************************************************************
451  * SetOffset: build offset array for conversion functions
452  *****************************************************************************
453  * This function will build an offset array used in later conversion functions.
454  * It will also set horizontal and vertical scaling indicators.
455  *****************************************************************************/
456 void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
457                 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset )
458 {
459     int i_x;                                    /* x position in destination */
460     int i_scale_count;                                     /* modulo counter */
461
462     /*
463      * Prepare horizontal offset array
464      */
465     if( i_pic_width - i_width > 0 )
466     {
467         /* Prepare scaling array for horizontal extension */
468         *pb_h_scaling =  1;
469         i_scale_count =         i_pic_width;
470         for( i_x = i_width; i_x--; )
471         {
472             while( (i_scale_count -= i_width) > 0 )
473             {
474                 *p_offset++ = 0;
475             }
476             *p_offset++ = 1;
477             i_scale_count += i_pic_width;
478         }
479     }
480     else if( i_pic_width - i_width < 0 )
481     {
482         /* Prepare scaling array for horizontal reduction */
483         *pb_h_scaling =  1;
484         i_scale_count =         i_pic_width;
485         for( i_x = i_pic_width; i_x--; )
486         {
487             *p_offset = 1;
488             while( (i_scale_count -= i_pic_width) >= 0 )
489             {
490                 *p_offset += 1;
491             }
492             p_offset++;
493             i_scale_count += i_width;
494         }
495     }
496     else
497     {
498         /* No horizontal scaling: YUV conversion is done directly to picture */
499         *pb_h_scaling = 0;
500     }
501
502     /*
503      * Set vertical scaling indicator
504      */
505     if( i_pic_height - i_height > 0 )
506     {
507         *pi_v_scaling = 1;
508     }
509     else if( i_pic_height - i_height < 0 )
510     {
511         *pi_v_scaling = -1;
512     }
513     else
514     {
515         *pi_v_scaling = 0;
516     }
517 }
518