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