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