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