video_yuv.c 22.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/*****************************************************************************
 * video_yuv.c: YUV transformation functions
 * Provides functions to perform the YUV conversion. The functions provided here
 * are a complete and portable C implementation, and may be replaced in certain
 * case by optimized functions.
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
 *
 * Authors:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <math.h>                                            /* exp(), pow() */
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */

#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
#include "plugins.h"
#include "video.h"
#include "video_output.h"
#include "video_yuv.h"

#include "intf_msg.h"

/*****************************************************************************
 * vout_InitYUV: allocate and initialize translations tables
 *****************************************************************************
 * This function will allocate memory to store translation tables, depending
 * of the screen depth.
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
54
int yuv_CInit( vout_thread_t *p_vout )
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
    size_t      tables_size;                        /* tables size, in bytes */

    /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
    switch( p_vout->i_bytes_per_pixel )
    {
    case 1:
        tables_size = sizeof( u8 )
                * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
        break;
    case 2:
        tables_size = sizeof( u16 )
                * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
        break;
    case 3:
    case 4:
    default:
        tables_size = sizeof( u32 )
                * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
        break;
    }

    /* Allocate memory */
    p_vout->yuv.p_base = malloc( tables_size );
    if( p_vout->yuv.p_base == NULL )
    {
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
        return( 1 );
    }

    /* Allocate memory for conversion buffer and offset array */
    p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
    if( p_vout->yuv.p_buffer == NULL )
    {
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
        free( p_vout->yuv.p_base );
        return( 1 );
    }
93 94 95 96 97

    /* In 8bpp we have a twice as big offset table because we also
     * need the offsets for U and V (not only Y) */
    p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) *
                             ( ( p_vout->i_bytes_per_pixel == 1 ) ? 2 : 1 ) );
98 99 100 101 102 103 104 105 106 107 108 109 110 111
    if( p_vout->yuv.p_offset == NULL )
    {
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
        free( p_vout->yuv.p_base );
        free( p_vout->yuv.p_buffer );
        return( 1 );
    }

    /* Initialize tables */
    SetYUV( p_vout );
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
112
 * yuv_CEnd: destroy translations tables
113
 *****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
114
 * Free memory allocated by yuv_CCreate.
115
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
116
void yuv_CEnd( vout_thread_t *p_vout )
117
{
Sam Hocevar's avatar
Sam Hocevar committed
118 119 120
    free( p_vout->yuv.p_base );
    free( p_vout->yuv.p_buffer );
    free( p_vout->yuv.p_offset );
121 122 123
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
124
 * yuv_CReset: re-initialize translations tables
125
 *****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
126 127
 * This function will initialize the tables allocated by vout_CreateTables and
 * set functions pointers.
128
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
129
int yuv_CReset( vout_thread_t *p_vout )
130
{
Sam Hocevar's avatar
Sam Hocevar committed
131 132
    yuv_CEnd( p_vout );
    return( yuv_CInit( p_vout ) );
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
}

/* following functions are local */

/*****************************************************************************
 * SetGammaTable: return intensity table transformed by gamma curve.
 *****************************************************************************
 * pi_table is a table of 256 entries from 0 to 255.
 *****************************************************************************/
void SetGammaTable( int *pi_table, double f_gamma )
{
    int         i_y;                                       /* base intensity */

    /* Use exp(gamma) instead of gamma */
    f_gamma = exp( f_gamma );

    /* Build gamma table */
    for( i_y = 0; i_y < 256; i_y++ )
    {
        pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
    }
 }

/*****************************************************************************
 * SetYUV: compute tables and set function pointers
+ *****************************************************************************/
void SetYUV( vout_thread_t *p_vout )
{
    int         pi_gamma[256];                                /* gamma table */
    int         i_index;                                  /* index in tables */

    /* Build gamma table */
    SetGammaTable( pi_gamma, p_vout->f_gamma );

    /*
     * Set pointers and build YUV tables
     */
    if( p_vout->b_grayscale )
    {
        /* Grayscale: build gray table */
        switch( p_vout->i_bytes_per_pixel )
        {
        case 1:
            {
                u16 bright[256], transp[256];

                p_vout->yuv.yuv.p_gray8 =  (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
                for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
                {
                    p_vout->yuv.yuv.p_gray8[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
                    p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
                }
                for( i_index = 0; i_index < 256; i_index++)
                {
                    p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
                    bright[ i_index ] = i_index << 8;
                    transp[ i_index ] = 0;
                }
                /* the colors have been allocated, we can set the palette */
                p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
                p_vout->i_white_pixel = 0xff;
                p_vout->i_black_pixel = 0x00;
                p_vout->i_gray_pixel = 0x44;
                p_vout->i_blue_pixel = 0x3b;

                break;
            }
        case 2:
            p_vout->yuv.yuv.p_gray16 =  (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
            for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_gray16[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
                p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
            }
            for( i_index = 0; i_index < 256; i_index++)
            {
                p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
            }
            break;
        case 3:
        case 4:
            p_vout->yuv.yuv.p_gray32 =  (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
            for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_gray32[ -i_index ] =      RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
                p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
            }
            for( i_index = 0; i_index < 256; i_index++)
            {
                p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
            }
            break;
         }
    }
    else
    {
        /* Color: build red, green and blue tables */
        switch( p_vout->i_bytes_per_pixel )
        {
        case 1:
            {
                #define RGB_MIN 0
                #define RGB_MAX 255
                #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )

                int y,u,v;
                int r,g,b;
                int uvr, uvg, uvb;
                int i = 0, j = 0;
                u16 red[256], green[256], blue[256], transp[256];
                unsigned char lookup[PALETTE_TABLE_SIZE];

                p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;

                /* this loop calculates the intersection of an YUV box
                 * and the RGB cube. */
                for ( y = 0; y <= 256; y += 16 )
                {
                    for ( u = 0; u <= 256; u += 32 )
                    for ( v = 0; v <= 256; v += 32 )
                    {
                        uvr = (V_RED_COEF*(v-128)) >> SHIFT;
                        uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
                        uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
                        r = y + uvr;
                        g = y + uvg;
                        b = y + uvb;

                        if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
                                && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
                        {
                            /* this one should never happen unless someone fscked up my code */
                            if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }

                            /* clip the colors */
                            red[j] = CLIP( r );
                            green[j] = CLIP( g );
                            blue[j] = CLIP( b );
                            transp[j] = 0;

                            /* allocate color */
                            lookup[i] = 1;
                            p_vout->yuv.yuv.p_rgb8[i++] = j;
                            j++;
                        }
                        else
                        {
                            lookup[i] = 0;
                            p_vout->yuv.yuv.p_rgb8[i++] = 0;
                        }
                    }
                    i += 128-81;
                }

                /* the colors have been allocated, we can set the palette */
                /* there will eventually be a way to know which colors
                 * couldn't be allocated and try to find a replacement */
                p_vout->p_set_palette( p_vout, red, green, blue, transp );

                p_vout->i_white_pixel = 0xff;
                p_vout->i_black_pixel = 0x00;
                p_vout->i_gray_pixel = 0x44;
                p_vout->i_blue_pixel = 0x3b;

                i = 0;
                /* this loop allocates colors that got outside
                 * the RGB cube */
                for ( y = 0; y <= 256; y += 16 )
                {
                    for ( u = 0; u <= 256; u += 32 )
                    for ( v = 0; v <= 256; v += 32 )
                    {
                        int u2, v2;
                        int dist, mindist = 100000000;

                        if( lookup[i] || y==0)
                        {
                            i++;
                            continue;
                        }

                        /* heavy. yeah. */
                        for( u2 = 0; u2 <= 256; u2 += 32 )
                        for( v2 = 0; v2 <= 256; v2 += 32 )
                        {
                            j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
                            dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
                            if( lookup[j] )
                            /* find the nearest color */
                            if( dist < mindist )
                            {
                                p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
                                mindist = dist;
                            }
                            j -= 128;
                            if( lookup[j] )
                            /* find the nearest color */
                            if( dist + 128 < mindist )
                            {
                                p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
                                mindist = dist + 128;
                            }
                        }
                        i++;
                    }
                    i += 128-81;
                }

                break;
            }
        case 2:
            p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
            for( i_index = 0; i_index < RED_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
                p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
            }
            for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
                p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
            }
            for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
                p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
            }
            for( i_index = 0; i_index < 256; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
                p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
                p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
            }
            break;
        case 3:
        case 4:
            p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
            for( i_index = 0; i_index < RED_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
                p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] =        RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
            }
            for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
                p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] =          RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
            }
            for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
                p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
            }
            for( i_index = 0; i_index < 256; i_index++ )
            {
                p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] =   RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
                p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
                p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] =  RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
            }
            break;
        }
    }

    /*
     * Set functions pointers
     */
    if( p_vout->b_grayscale )
    {
        /* Grayscale */
        switch( p_vout->i_bytes_per_pixel )
        {
        case 1:
            p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
            p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
            p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
            break;
        case 2:
            p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
            p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
            p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
            break;
        case 3:
            p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
            p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
            p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
            break;
        case 4:
            p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
            p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
            p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
            break;
        }
    }
    else
    {
        /* Color */
        switch( p_vout->i_bytes_per_pixel )
        {
        case 1:
            p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
            p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
            p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
            break;
        case 2:
            p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB16;
            p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB16;
            p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB16;
            break;
        case 3:
            p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB24;
            p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB24;
            p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB24;
            break;
        case 4:
            p_vout->yuv.p_Convert420 =   (vout_yuv_convert_t *) ConvertYUV420RGB32;
            p_vout->yuv.p_Convert422 =   (vout_yuv_convert_t *) ConvertYUV422RGB32;
            p_vout->yuv.p_Convert444 =   (vout_yuv_convert_t *) ConvertYUV444RGB32;
            break;
        }
    }
}

/*****************************************************************************
 * SetOffset: build offset array for conversion functions
 *****************************************************************************
 * This function will build an offset array used in later conversion functions.
458 459
 * It will also set horizontal and vertical scaling indicators. If b_double
 * is set, the p_offset structure has interleaved Y and U/V offsets.
460 461
 *****************************************************************************/
void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
462 463
                boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset,
                boolean_t b_double )
464 465 466 467 468 469 470
{
    int i_x;                                    /* x position in destination */
    int i_scale_count;                                     /* modulo counter */

    /*
     * Prepare horizontal offset array
     */
Sam Hocevar's avatar
Sam Hocevar committed
471 472 473 474 475 476
    if( i_pic_width - i_width == 0 )
    {
        /* No horizontal scaling: YUV conversion is done directly to picture */
        *pb_h_scaling = 0;
    }
    else if( i_pic_width - i_width > 0 )
477 478
    {
        /* Prepare scaling array for horizontal extension */
479 480
        *pb_h_scaling = 1;
        i_scale_count = i_pic_width;
Sam Hocevar's avatar
Sam Hocevar committed
481
        if( !b_double )
482
        {
483
            for( i_x = i_width; i_x--; )
484
            {
485 486 487 488 489 490 491 492 493 494
                while( (i_scale_count -= i_width) > 0 )
                {
                    *p_offset++ = 0;
                }
                *p_offset++ = 1;
                i_scale_count += i_pic_width;
            }
        }
        else
        {
Sam Hocevar's avatar
Sam Hocevar committed
495
            int i_dummy = 0;
496 497 498 499 500
            for( i_x = i_width; i_x--; )
            {
                while( (i_scale_count -= i_width) > 0 )
                {
                    *p_offset++ = 0;
Sam Hocevar's avatar
Sam Hocevar committed
501
                    *p_offset++ = 0;
502 503
                }
                *p_offset++ = 1;
Sam Hocevar's avatar
Sam Hocevar committed
504 505
                *p_offset++ = i_dummy;
                i_dummy = 1 - i_dummy;
506
                i_scale_count += i_pic_width;
507 508 509
            }
        }
    }
Sam Hocevar's avatar
Sam Hocevar committed
510
    else /* if( i_pic_width - i_width < 0 ) */
511 512
    {
        /* Prepare scaling array for horizontal reduction */
Sam Hocevar's avatar
Sam Hocevar committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
        *pb_h_scaling = 1;
        i_scale_count = i_width;
        if( !b_double )
        {
           for( i_x = i_pic_width; i_x--; )
            {
                *p_offset = 1;
                while( (i_scale_count -= i_pic_width) > 0 )
                {
                    *p_offset += 1;
                }
                p_offset++;
                i_scale_count += i_width;
            }
        }
        else
529
        {
530 531 532
            int i_remainder = 0;
            int i_jump;
            for( i_x = i_pic_width; i_x--; )
533
            {
534
                i_jump = 1;
Sam Hocevar's avatar
Sam Hocevar committed
535
                while( (i_scale_count -= i_pic_width) > 0 )
536 537 538 539 540 541 542 543
                {
                    i_jump += 1;
                }
                *p_offset++ = i_jump;
                *p_offset++ = ( i_jump += i_remainder ) >> 1;
                i_remainder = i_jump & 1;
                i_scale_count += i_width;
            }
544
        }
Sam Hocevar's avatar
Sam Hocevar committed
545
     }
546 547 548 549

    /*
     * Set vertical scaling indicator
     */
Sam Hocevar's avatar
Sam Hocevar committed
550
    if( i_pic_height - i_height == 0 )
551
    {
Sam Hocevar's avatar
Sam Hocevar committed
552
        *pi_v_scaling = 0;
553
    }
Sam Hocevar's avatar
Sam Hocevar committed
554
    else if( i_pic_height - i_height > 0 )
555
    {
Sam Hocevar's avatar
Sam Hocevar committed
556
        *pi_v_scaling = 1;
557
    }
Sam Hocevar's avatar
Sam Hocevar committed
558
    else /* if( i_pic_height - i_height < 0 ) */
559
    {
Sam Hocevar's avatar
Sam Hocevar committed
560
        *pi_v_scaling = -1;
561 562 563
    }
}