]> git.sesse.net Git - vlc/blob - plugins/yuv/video_yuv.c
* Added error checking in pthread wrapper ; as a result, intf_msg.h must
[vlc] / plugins / yuv / 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-2001 VideoLAN
8  * $Id: video_yuv.c,v 1.16 2001/11/28 15:08:06 massiot Exp $
9  *
10  * Authors: Vincent Seguin <seguin@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public
23  * License along with this program; if not, write to the
24  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  *****************************************************************************/
27
28 #define MODULE_NAME yuv
29 #include "modules_inner.h"
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include "defs.h"
35
36 #include <math.h>                                            /* exp(), pow() */
37 #include <errno.h>                                                 /* ENOMEM */
38 #include <stdlib.h>                                                /* free() */
39 #include <string.h>                                            /* strerror() */
40
41 #include "config.h"
42 #include "common.h"
43 #include "intf_msg.h"
44 #include "threads.h"
45 #include "mtime.h"
46 #include "tests.h"
47
48 #include "video.h"
49 #include "video_output.h"
50
51 #include "video_common.h"
52
53 #include "modules.h"
54 #include "modules_export.h"
55
56 static int     yuv_Probe      ( probedata_t *p_data );
57 static int     yuv_Init       ( vout_thread_t *p_vout );
58 static int     yuv_Reset      ( vout_thread_t *p_vout );
59 static void    yuv_End        ( vout_thread_t *p_vout );
60
61 static void    SetGammaTable  ( int *pi_table, double f_gamma );
62 static void    SetYUV         ( vout_thread_t *p_vout );
63
64 /*****************************************************************************
65  * Functions exported as capabilities. They are declared as static so that
66  * we don't pollute the namespace too much.
67  *****************************************************************************/
68 void _M( yuv_getfunctions )( function_list_t * p_function_list )
69 {
70     p_function_list->pf_probe = yuv_Probe;
71     p_function_list->functions.yuv.pf_init = yuv_Init;
72     p_function_list->functions.yuv.pf_reset = yuv_Reset;
73     p_function_list->functions.yuv.pf_end = yuv_End;
74 }
75
76 /*****************************************************************************
77  * yuv_Probe: tests probe the audio device and return a score
78  *****************************************************************************
79  * This function tries to open the DSP and returns a score to the plugin
80  * manager so that it can choose the most appropriate one.
81  *****************************************************************************/
82 static int yuv_Probe( probedata_t *p_data )
83 {
84     if( TestMethod( YUV_METHOD_VAR, "yuv" )
85          || TestMethod( YUV_METHOD_VAR, "c" ) )
86     {
87         return( 999 );
88     }
89
90     /* This module always works */
91     return( 50 );
92 }
93
94 /*****************************************************************************
95  * yuv_Init: allocate and initialize translations tables
96  *****************************************************************************
97  * This function will allocate memory to store translation tables, depending
98  * of the screen depth.
99  *****************************************************************************/
100 static int yuv_Init( vout_thread_t *p_vout )
101 {
102     size_t      tables_size;                        /* tables size, in bytes */
103
104     /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
105     switch( p_vout->i_bytes_per_pixel )
106     {
107     case 1:
108         tables_size = sizeof( u8 )
109                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
110         break;
111     case 2:
112         tables_size = sizeof( u16 )
113                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
114         break;
115     case 3:
116     case 4:
117     default:
118         tables_size = sizeof( u32 )
119                 * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
120         break;
121     }
122
123     /* Allocate memory */
124     p_vout->yuv.p_base = malloc( tables_size );
125     if( p_vout->yuv.p_base == NULL )
126     {
127         intf_ErrMsg("error: %s", strerror(ENOMEM));
128         return( 1 );
129     }
130
131     /* Allocate memory for conversion buffer and offset array */
132     p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
133     if( p_vout->yuv.p_buffer == NULL )
134     {
135         intf_ErrMsg("error: %s", strerror(ENOMEM));
136         free( p_vout->yuv.p_base );
137         return( 1 );
138     }
139
140     /* In 8bpp we have a twice as big offset table because we also
141      * need the offsets for U and V (not only Y) */
142     p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) *
143                              ( ( p_vout->i_bytes_per_pixel == 1 ) ? 2 : 1 ) );
144     if( p_vout->yuv.p_offset == NULL )
145     {
146         intf_ErrMsg("error: %s", strerror(ENOMEM));
147         free( p_vout->yuv.p_base );
148         free( p_vout->yuv.p_buffer );
149         return( 1 );
150     }
151
152     /* Initialize tables */
153     SetYUV( p_vout );
154     return( 0 );
155 }
156
157 /*****************************************************************************
158  * yuv_End: destroy translations tables
159  *****************************************************************************
160  * Free memory allocated by yuv_CCreate.
161  *****************************************************************************/
162 static void yuv_End( vout_thread_t *p_vout )
163 {
164     free( p_vout->yuv.p_base );
165     free( p_vout->yuv.p_buffer );
166     free( p_vout->yuv.p_offset );
167 }
168
169 /*****************************************************************************
170  * yuv_Reset: re-initialize translations tables
171  *****************************************************************************
172  * This function will initialize the tables allocated by vout_CreateTables and
173  * set functions pointers.
174  *****************************************************************************/
175 static int yuv_Reset( vout_thread_t *p_vout )
176 {
177     yuv_End( p_vout );
178     return( yuv_Init( p_vout ) );
179 }
180
181 /*****************************************************************************
182  * SetGammaTable: return intensity table transformed by gamma curve.
183  *****************************************************************************
184  * pi_table is a table of 256 entries from 0 to 255.
185  *****************************************************************************/
186 static void SetGammaTable( int *pi_table, double f_gamma )
187 {
188     int         i_y;                                       /* base intensity */
189
190     /* Use exp(gamma) instead of gamma */
191     f_gamma = exp( f_gamma );
192
193     /* Build gamma table */
194     for( i_y = 0; i_y < 256; i_y++ )
195     {
196         pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
197     }
198  }
199
200 /*****************************************************************************
201  * SetYUV: compute tables and set function pointers
202  *****************************************************************************/
203 static void SetYUV( vout_thread_t *p_vout )
204 {
205     int         pi_gamma[256];                                /* gamma table */
206     int         i_index;                                  /* index in tables */
207
208     /* Build gamma table */
209     SetGammaTable( pi_gamma, p_vout->f_gamma );
210
211     /*
212      * Set pointers and build YUV tables
213      */
214     if( p_vout->b_grayscale )
215     {
216         /* Grayscale: build gray table */
217         switch( p_vout->i_bytes_per_pixel )
218         {
219         case 1:
220             {
221                 u16 bright[256], transp[256];
222
223                 p_vout->yuv.yuv.p_gray8 =  (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
224                 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
225                 {
226                     p_vout->yuv.yuv.p_gray8[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
227                     p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
228                 }
229                 for( i_index = 0; i_index < 256; i_index++)
230                 {
231                     p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
232                     bright[ i_index ] = i_index << 8;
233                     transp[ i_index ] = 0;
234                 }
235                 /* the colors have been allocated, we can set the palette */
236                 p_vout->pf_setpalette( p_vout, bright, bright, bright, transp );
237                 p_vout->i_white_pixel = 0xff;
238                 p_vout->i_black_pixel = 0x00;
239                 p_vout->i_gray_pixel = 0x44;
240                 p_vout->i_blue_pixel = 0x3b;
241
242                 break;
243             }
244         case 2:
245             p_vout->yuv.yuv.p_gray16 =  (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
246             for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
247             {
248                 p_vout->yuv.yuv.p_gray16[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
249                 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
250             }
251             for( i_index = 0; i_index < 256; i_index++)
252             {
253                 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
254             }
255             break;
256         case 3:
257         case 4:
258             p_vout->yuv.yuv.p_gray32 =  (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
259             for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
260             {
261                 p_vout->yuv.yuv.p_gray32[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
262                 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
263             }
264             for( i_index = 0; i_index < 256; i_index++)
265             {
266                 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
267             }
268             break;
269          }
270     }
271     else
272     {
273         /* Color: build red, green and blue tables */
274         switch( p_vout->i_bytes_per_pixel )
275         {
276         case 1:
277             {
278                 #define RGB_MIN 0
279                 #define RGB_MAX 255
280                 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
281
282                 int y,u,v;
283                 int r,g,b;
284                 int uvr, uvg, uvb;
285                 int i = 0, j = 0;
286                 u16 red[256], green[256], blue[256], transp[256];
287                 unsigned char lookup[PALETTE_TABLE_SIZE];
288
289                 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
290
291                 /* this loop calculates the intersection of an YUV box
292                  * and the RGB cube. */
293                 for ( y = 0; y <= 256; y += 16 )
294                 {
295                     for ( u = 0; u <= 256; u += 32 )
296                     for ( v = 0; v <= 256; v += 32 )
297                     {
298                         uvr = (V_RED_COEF*(v-128)) >> SHIFT;
299                         uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
300                         uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
301                         r = y + uvr;
302                         g = y + uvg;
303                         b = y + uvb;
304
305                         if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
306                                 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
307                         {
308                             /* this one should never happen unless someone fscked up my code */
309                             if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette" ); break; }
310
311                             /* clip the colors */
312                             red[j] = CLIP( r );
313                             green[j] = CLIP( g );
314                             blue[j] = CLIP( b );
315                             transp[j] = 0;
316
317                             /* allocate color */
318                             lookup[i] = 1;
319                             p_vout->yuv.yuv.p_rgb8[i++] = j;
320                             j++;
321                         }
322                         else
323                         {
324                             lookup[i] = 0;
325                             p_vout->yuv.yuv.p_rgb8[i++] = 0;
326                         }
327                     }
328                     i += 128-81;
329                 }
330
331                 /* the colors have been allocated, we can set the palette */
332                 /* there will eventually be a way to know which colors
333                  * couldn't be allocated and try to find a replacement */
334                 p_vout->pf_setpalette( p_vout, red, green, blue, transp );
335
336                 p_vout->i_white_pixel = 0xff;
337                 p_vout->i_black_pixel = 0x00;
338                 p_vout->i_gray_pixel = 0x44;
339                 p_vout->i_blue_pixel = 0x3b;
340
341                 i = 0;
342                 /* this loop allocates colors that got outside
343                  * the RGB cube */
344                 for ( y = 0; y <= 256; y += 16 )
345                 {
346                     for ( u = 0; u <= 256; u += 32 )
347                     for ( v = 0; v <= 256; v += 32 )
348                     {
349                         int u2, v2;
350                         int dist, mindist = 100000000;
351
352                         if( lookup[i] || y==0)
353                         {
354                             i++;
355                             continue;
356                         }
357
358                         /* heavy. yeah. */
359                         for( u2 = 0; u2 <= 256; u2 += 32 )
360                         for( v2 = 0; v2 <= 256; v2 += 32 )
361                         {
362                             j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
363                             dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
364                             if( lookup[j] )
365                             /* find the nearest color */
366                             if( dist < mindist )
367                             {
368                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
369                                 mindist = dist;
370                             }
371                             j -= 128;
372                             if( lookup[j] )
373                             /* find the nearest color */
374                             if( dist + 128 < mindist )
375                             {
376                                 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
377                                 mindist = dist + 128;
378                             }
379                         }
380                         i++;
381                     }
382                     i += 128-81;
383                 }
384
385                 break;
386             }
387         case 2:
388             p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
389             for( i_index = 0; i_index < RED_MARGIN; i_index++ )
390             {
391                 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
392                 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
393             }
394             for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
395             {
396                 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
397                 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
398             }
399             for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
400             {
401                 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
402                 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
403             }
404             for( i_index = 0; i_index < 256; i_index++ )
405             {
406                 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
407                 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
408                 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
409             }
410             break;
411         case 3:
412         case 4:
413             p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
414             for( i_index = 0; i_index < RED_MARGIN; i_index++ )
415             {
416                 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
417                 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
418             }
419             for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
420             {
421                 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
422                 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
423             }
424             for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
425             {
426                 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
427                 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
428             }
429             for( i_index = 0; i_index < 256; i_index++ )
430             {
431                 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
432                 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
433                 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
434             }
435             break;
436         }
437     }
438
439     /*
440      * Set functions pointers
441      */
442     if( p_vout->b_YCbr)
443     {
444         switch( p_vout->i_bytes_per_pixel)
445         {
446 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
447         case 1:
448             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr8 );
449             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr8 );
450             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr8 );
451             break;
452         
453         case 2:
454             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr16 );
455             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr16 );
456             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr16 );
457            break;
458         
459         case 3:
460             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr24 );
461             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr24 );
462             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr24 );
463              break;
464         
465         case 4:
466             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420YCbr32 );
467             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422YCbr32 );
468             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444YCbr32 );
469             break;
470         }
471 #undef _X
472     }    
473     else if( p_vout->b_grayscale )
474     {
475         /* Grayscale */
476         switch( p_vout->i_bytes_per_pixel )
477         {
478 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
479         case 1:
480             p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray8 );
481             p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray8 );
482             p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray8 );
483             break;
484         case 2:
485             p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray16 );
486             p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray16 );
487             p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray16 );
488             break;
489         case 3:
490             p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray24 );
491             p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray24 );
492             p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray24 );
493             break;
494         case 4:
495             p_vout->yuv.pf_yuv420 = _X( ConvertY4Gray32 );
496             p_vout->yuv.pf_yuv422 = _X( ConvertY4Gray32 );
497             p_vout->yuv.pf_yuv444 = _X( ConvertY4Gray32 );
498             break;
499 #undef _X
500         }
501     }
502     else
503     {
504         /* Color */
505         switch( p_vout->i_bytes_per_pixel )
506         {
507 #define _X( foo ) (vout_yuv_convert_t *) _M( foo )
508         case 1:
509             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB8 );
510             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB8 );
511             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB8 );
512             break;
513         case 2:
514             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB16 );
515             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB16 );
516             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB16 );
517             break;
518         case 3:
519             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB24 );
520             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB24 );
521             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB24 );
522             break;
523         case 4:
524             p_vout->yuv.pf_yuv420 = _X( ConvertYUV420RGB32 );
525             p_vout->yuv.pf_yuv422 = _X( ConvertYUV422RGB32 );
526             p_vout->yuv.pf_yuv444 = _X( ConvertYUV444RGB32 );
527             break;
528 #undef _X
529       }
530  
531     }
532 }
533
534 /*****************************************************************************
535  * SetOffset: build offset array for conversion functions
536  *****************************************************************************
537  * This function will build an offset array used in later conversion functions.
538  * It will also set horizontal and vertical scaling indicators. If b_double
539  * is set, the p_offset structure has interleaved Y and U/V offsets.
540  *****************************************************************************/
541 void _M( SetOffset )( int i_width, int i_height, int i_pic_width,
542                       int i_pic_height, boolean_t *pb_h_scaling,
543                       int *pi_v_scaling, int *p_offset, boolean_t b_double )
544 {
545     int i_x;                                    /* x position in destination */
546     int i_scale_count;                                     /* modulo counter */
547
548     /*
549      * Prepare horizontal offset array
550      */
551     if( i_pic_width - i_width == 0 )
552     {
553         /* No horizontal scaling: YUV conversion is done directly to picture */
554         *pb_h_scaling = 0;
555     }
556     else if( i_pic_width - i_width > 0 )
557     {
558         /* Prepare scaling array for horizontal extension */
559         *pb_h_scaling = 1;
560         i_scale_count = i_pic_width;
561         if( !b_double )
562         {
563             for( i_x = i_width; i_x--; )
564             {
565                 while( (i_scale_count -= i_width) > 0 )
566                 {
567                     *p_offset++ = 0;
568                 }
569                 *p_offset++ = 1;
570                 i_scale_count += i_pic_width;
571             }
572         }
573         else
574         {
575             int i_dummy = 0;
576             for( i_x = i_width; i_x--; )
577             {
578                 while( (i_scale_count -= i_width) > 0 )
579                 {
580                     *p_offset++ = 0;
581                     *p_offset++ = 0;
582                 }
583                 *p_offset++ = 1;
584                 *p_offset++ = i_dummy;
585                 i_dummy = 1 - i_dummy;
586                 i_scale_count += i_pic_width;
587             }
588         }
589     }
590     else /* if( i_pic_width - i_width < 0 ) */
591     {
592         /* Prepare scaling array for horizontal reduction */
593         *pb_h_scaling = 1;
594         i_scale_count = i_width;
595         if( !b_double )
596         {
597            for( i_x = i_pic_width; i_x--; )
598             {
599                 *p_offset = 1;
600                 while( (i_scale_count -= i_pic_width) > 0 )
601                 {
602                     *p_offset += 1;
603                 }
604                 p_offset++;
605                 i_scale_count += i_width;
606             }
607         }
608         else
609         {
610             int i_remainder = 0;
611             int i_jump;
612             for( i_x = i_pic_width; i_x--; )
613             {
614                 i_jump = 1;
615                 while( (i_scale_count -= i_pic_width) > 0 )
616                 {
617                     i_jump += 1;
618                 }
619                 *p_offset++ = i_jump;
620                 *p_offset++ = ( i_jump += i_remainder ) >> 1;
621                 i_remainder = i_jump & 1;
622                 i_scale_count += i_width;
623             }
624         }
625      }
626
627     /*
628      * Set vertical scaling indicator
629      */
630     if( i_pic_height - i_height == 0 )
631     {
632         *pi_v_scaling = 0;
633     }
634     else if( i_pic_height - i_height > 0 )
635     {
636         *pi_v_scaling = 1;
637     }
638     else /* if( i_pic_height - i_height < 0 ) */
639     {
640         *pi_v_scaling = -1;
641     }
642 }
643