vpx_scale.c 19.6 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 */

/****************************************************************************
 *
 *   Module Title :     scale.c
 *
 *   Description  :     Image scaling functions.
 *
 ***************************************************************************/

/****************************************************************************
*  Header Files
****************************************************************************/
22
#include "./vpx_scale_rtcd.h"
John Koleszar's avatar
John Koleszar committed
23
#include "vpx_mem/vpx_mem.h"
24
#include "vpx_scale/vpx_scale.h"
John Koleszar's avatar
John Koleszar committed
25 26
#include "vpx_scale/yv12config.h"

John Koleszar's avatar
John Koleszar committed
27
typedef struct {
clang-format's avatar
clang-format committed
28 29
  int expanded_frame_width;
  int expanded_frame_height;
John Koleszar's avatar
John Koleszar committed
30

John Koleszar's avatar
John Koleszar committed
31 32 33 34
  int HScale;
  int HRatio;
  int VScale;
  int VRatio;
John Koleszar's avatar
John Koleszar committed
35

John Koleszar's avatar
John Koleszar committed
36 37
  YV12_BUFFER_CONFIG *src_yuv_config;
  YV12_BUFFER_CONFIG *dst_yuv_config;
John Koleszar's avatar
John Koleszar committed
38 39 40 41 42 43 44 45

} SCALE_VARS;

/****************************************************************************
 *
 *  ROUTINE       : scale1d_2t1_i
 *
 *  INPUTS        : const unsigned char *source : Pointer to data to be scaled.
clang-format's avatar
clang-format committed
46 47 48 49
 *                  int source_step             : Number of pixels to step on
 *                                                in source.
 *                  unsigned int source_scale   : Scale for source (UNUSED).
 *                  unsigned int source_length  : Length of source (UNUSED).
John Koleszar's avatar
John Koleszar committed
50
 *                  unsigned char *dest         : Pointer to output data array.
clang-format's avatar
clang-format committed
51 52 53 54 55
 *                  int dest_step               : Number of pixels to step on
 *                                                in destination.
 *                  unsigned int dest_scale     : Scale for destination
 *                                                (UNUSED).
 *                  unsigned int dest_length    : Length of destination.
John Koleszar's avatar
John Koleszar committed
56 57 58 59 60 61 62 63 64 65
 *
 *  OUTPUTS       : None.
 *
 *  RETURNS       : void
 *
 *  FUNCTION      : Performs 2-to-1 interpolated scaling.
 *
 *  SPECIAL NOTES : None.
 *
 ****************************************************************************/
clang-format's avatar
clang-format committed
66 67 68 69
static void scale1d_2t1_i(const unsigned char *source, int source_step,
                          unsigned int source_scale, unsigned int source_length,
                          unsigned char *dest, int dest_step,
                          unsigned int dest_scale, unsigned int dest_length) {
John Koleszar's avatar
John Koleszar committed
70 71 72
  unsigned int i, j;
  unsigned int temp;
  int source_pitch = source_step;
clang-format's avatar
clang-format committed
73 74 75
  (void)source_length;
  (void)source_scale;
  (void)dest_scale;
John Koleszar's avatar
John Koleszar committed
76 77 78 79

  source_step *= 2;
  dest[0] = source[0];

clang-format's avatar
clang-format committed
80 81
  for (i = dest_step, j = source_step; i < dest_length * dest_step;
       i += dest_step, j += source_step) {
John Koleszar's avatar
John Koleszar committed
82 83 84 85 86 87 88
    temp = 8;
    temp += 3 * source[j - source_pitch];
    temp += 10 * source[j];
    temp += 3 * source[j + source_pitch];
    temp >>= 4;
    dest[i] = (char)(temp);
  }
John Koleszar's avatar
John Koleszar committed
89 90 91 92 93 94 95
}

/****************************************************************************
 *
 *  ROUTINE       : scale1d_2t1_ps
 *
 *  INPUTS        : const unsigned char *source : Pointer to data to be scaled.
clang-format's avatar
clang-format committed
96 97 98 99
 *                  int source_step             : Number of pixels to step on
 *                                                in source.
 *                  unsigned int source_scale   : Scale for source (UNUSED).
 *                  unsigned int source_length  : Length of source (UNUSED).
John Koleszar's avatar
John Koleszar committed
100
 *                  unsigned char *dest         : Pointer to output data array.
clang-format's avatar
clang-format committed
101 102 103 104 105
 *                  int dest_step               : Number of pixels to step on
 *                                                in destination.
 *                  unsigned int dest_scale     : Scale for destination
 *                                                (UNUSED).
 *                  unsigned int dest_length    : Length of destination.
John Koleszar's avatar
John Koleszar committed
106 107 108 109 110 111 112 113 114 115
 *
 *  OUTPUTS       : None.
 *
 *  RETURNS       : void
 *
 *  FUNCTION      : Performs 2-to-1 point subsampled scaling.
 *
 *  SPECIAL NOTES : None.
 *
 ****************************************************************************/
clang-format's avatar
clang-format committed
116 117 118 119 120
static void scale1d_2t1_ps(const unsigned char *source, int source_step,
                           unsigned int source_scale,
                           unsigned int source_length, unsigned char *dest,
                           int dest_step, unsigned int dest_scale,
                           unsigned int dest_length) {
John Koleszar's avatar
John Koleszar committed
121 122
  unsigned int i, j;

clang-format's avatar
clang-format committed
123 124 125
  (void)source_length;
  (void)source_scale;
  (void)dest_scale;
John Koleszar's avatar
John Koleszar committed
126 127 128 129 130 131

  source_step *= 2;
  j = 0;

  for (i = 0; i < dest_length * dest_step; i += dest_step, j += source_step)
    dest[i] = source[j];
John Koleszar's avatar
John Koleszar committed
132 133 134 135 136 137
}
/****************************************************************************
 *
 *  ROUTINE       : scale1d_c
 *
 *  INPUTS        : const unsigned char *source : Pointer to data to be scaled.
clang-format's avatar
clang-format committed
138 139 140 141
 *                  int source_step             : Number of pixels to step on
 *                                                in source.
 *                  unsigned int source_scale   : Scale for source.
 *                  unsigned int source_length  : Length of source (UNUSED).
John Koleszar's avatar
John Koleszar committed
142
 *                  unsigned char *dest         : Pointer to output data array.
clang-format's avatar
clang-format committed
143 144 145 146
 *                  int dest_step               : Number of pixels to step on
 *                                                in destination.
 *                  unsigned int dest_scale     : Scale for destination.
 *                  unsigned int dest_length    : Length of destination.
John Koleszar's avatar
John Koleszar committed
147 148 149 150 151 152 153 154 155 156
 *
 *  OUTPUTS       : None.
 *
 *  RETURNS       : void
 *
 *  FUNCTION      : Performs linear interpolation in one dimension.
 *
 *  SPECIAL NOTES : None.
 *
 ****************************************************************************/
clang-format's avatar
clang-format committed
157 158 159 160
static void scale1d_c(const unsigned char *source, int source_step,
                      unsigned int source_scale, unsigned int source_length,
                      unsigned char *dest, int dest_step,
                      unsigned int dest_scale, unsigned int dest_length) {
John Koleszar's avatar
John Koleszar committed
161 162 163 164 165 166 167
  unsigned int i;
  unsigned int round_value = dest_scale / 2;
  unsigned int left_modifier = dest_scale;
  unsigned int right_modifier = 0;
  unsigned char left_pixel = *source;
  unsigned char right_pixel = *(source + source_step);

clang-format's avatar
clang-format committed
168
  (void)source_length;
John Koleszar's avatar
John Koleszar committed
169 170

  /* These asserts are needed if there are boundary issues... */
171 172 173
  /* assert ( dest_scale > source_scale );*/
  /* assert ( (source_length - 1) * dest_scale >= (dest_length - 1) *
   * source_scale);*/
John Koleszar's avatar
John Koleszar committed
174 175

  for (i = 0; i < dest_length * dest_step; i += dest_step) {
clang-format's avatar
clang-format committed
176 177 178
    dest[i] = (char)((left_modifier * left_pixel +
                      right_modifier * right_pixel + round_value) /
                     dest_scale);
John Koleszar's avatar
John Koleszar committed
179 180 181 182 183 184 185 186

    right_modifier += source_scale;

    while (right_modifier > dest_scale) {
      right_modifier -= dest_scale;
      source += source_step;
      left_pixel = *source;
      right_pixel = *(source + source_step);
John Koleszar's avatar
John Koleszar committed
187
    }
John Koleszar's avatar
John Koleszar committed
188 189 190

    left_modifier = dest_scale - right_modifier;
  }
John Koleszar's avatar
John Koleszar committed
191 192 193 194 195 196
}

/****************************************************************************
 *
 *  ROUTINE       : Scale2D
 *
clang-format's avatar
clang-format committed
197 198 199 200 201 202 203 204 205 206 207 208 209
 *  INPUTS        : const unsigned char *source    : Pointer to data to be
 *                                                   scaled.
 *                  int source_pitch               : Stride of source image.
 *                  unsigned int source_width      : Width of input image.
 *                  unsigned int source_height     : Height of input image.
 *                  unsigned char *dest            : Pointer to output data
 *                                                   array.
 *                  int dest_pitch                 : Stride of destination
 *                                                   image.
 *                  unsigned int dest_width        : Width of destination image.
 *                  unsigned int dest_height       : Height of destination
 *                                                   image.
 *                  unsigned char *temp_area       : Pointer to temp work area.
John Koleszar's avatar
John Koleszar committed
210
 *                  unsigned char temp_area_height : Height of temp work area.
clang-format's avatar
clang-format committed
211 212 213 214 215 216 217 218 219
 *                  unsigned int hscale            : Horizontal scale factor
 *                                                   numerator.
 *                  unsigned int hratio            : Horizontal scale factor
 *                                                   denominator.
 *                  unsigned int vscale            : Vertical scale factor
 *                                                   numerator.
 *                  unsigned int vratio            : Vertical scale factor
 *                                                   denominator.
 *                  unsigned int interlaced        : Interlace flag.
John Koleszar's avatar
John Koleszar committed
220 221 222 223 224 225 226 227 228 229 230
 *
 *  OUTPUTS       : None.
 *
 *  RETURNS       : void
 *
 *  FUNCTION      : Performs 2-tap linear interpolation in two dimensions.
 *
 *  SPECIAL NOTES : Expansion is performed one band at a time to help with
 *                  caching.
 *
 ****************************************************************************/
clang-format's avatar
clang-format committed
231 232 233 234 235 236 237
static void Scale2D(
    /*const*/
    unsigned char *source, int source_pitch, unsigned int source_width,
    unsigned int source_height, unsigned char *dest, int dest_pitch,
    unsigned int dest_width, unsigned int dest_height, unsigned char *temp_area,
    unsigned char temp_area_height, unsigned int hscale, unsigned int hratio,
    unsigned int vscale, unsigned int vratio, unsigned int interlaced) {
John Koleszar's avatar
John Koleszar committed
238 239 240 241 242 243
  /*unsigned*/
  int i, j, k;
  int bands;
  int dest_band_height;
  int source_band_height;

clang-format's avatar
clang-format committed
244 245 246 247
  typedef void (*Scale1D)(const unsigned char *source, int source_step,
                          unsigned int source_scale, unsigned int source_length,
                          unsigned char *dest, int dest_step,
                          unsigned int dest_scale, unsigned int dest_length);
John Koleszar's avatar
John Koleszar committed
248 249 250 251

  Scale1D Scale1Dv = scale1d_c;
  Scale1D Scale1Dh = scale1d_c;

clang-format's avatar
clang-format committed
252 253 254 255
  void (*horiz_line_scale)(const unsigned char *, unsigned int, unsigned char *,
                           unsigned int) = NULL;
  void (*vert_band_scale)(unsigned char *, unsigned int, unsigned char *,
                          unsigned int, unsigned int) = NULL;
John Koleszar's avatar
John Koleszar committed
256 257 258 259

  int ratio_scalable = 1;
  int interpolation = 0;

clang-format's avatar
clang-format committed
260 261 262
  unsigned char *source_base; /* = (unsigned char *) ((source_pitch >= 0) ?
                                 source : (source + ((source_height-1) *
                                 source_pitch))); */
John Koleszar's avatar
John Koleszar committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  unsigned char *line_src;

  source_base = (unsigned char *)source;

  if (source_pitch < 0) {
    int offset;

    offset = (source_height - 1);
    offset *= source_pitch;

    source_base += offset;
  }

  /* find out the ratio for each direction */
  switch (hratio * 10 / hscale) {
John Koleszar's avatar
John Koleszar committed
278
    case 8:
John Koleszar's avatar
John Koleszar committed
279
      /* 4-5 Scale in Width direction */
Yaowu Xu's avatar
Yaowu Xu committed
280
      horiz_line_scale = vpx_horizontal_line_5_4_scale;
John Koleszar's avatar
John Koleszar committed
281
      break;
John Koleszar's avatar
John Koleszar committed
282
    case 6:
John Koleszar's avatar
John Koleszar committed
283
      /* 3-5 Scale in Width direction */
Yaowu Xu's avatar
Yaowu Xu committed
284
      horiz_line_scale = vpx_horizontal_line_5_3_scale;
John Koleszar's avatar
John Koleszar committed
285
      break;
John Koleszar's avatar
John Koleszar committed
286
    case 5:
John Koleszar's avatar
John Koleszar committed
287
      /* 1-2 Scale in Width direction */
Yaowu Xu's avatar
Yaowu Xu committed
288
      horiz_line_scale = vpx_horizontal_line_2_1_scale;
John Koleszar's avatar
John Koleszar committed
289
      break;
John Koleszar's avatar
John Koleszar committed
290
    default:
John Koleszar's avatar
John Koleszar committed
291 292 293 294 295
      /* The ratio is not acceptable now */
      /* throw("The ratio is not acceptable for now!"); */
      ratio_scalable = 0;
      break;
  }
John Koleszar's avatar
John Koleszar committed
296

John Koleszar's avatar
John Koleszar committed
297
  switch (vratio * 10 / vscale) {
John Koleszar's avatar
John Koleszar committed
298
    case 8:
John Koleszar's avatar
John Koleszar committed
299
      /* 4-5 Scale in vertical direction */
clang-format's avatar
clang-format committed
300 301 302
      vert_band_scale = vpx_vertical_band_5_4_scale;
      source_band_height = 5;
      dest_band_height = 4;
John Koleszar's avatar
John Koleszar committed
303
      break;
John Koleszar's avatar
John Koleszar committed
304
    case 6:
John Koleszar's avatar
John Koleszar committed
305
      /* 3-5 Scale in vertical direction */
clang-format's avatar
clang-format committed
306 307 308
      vert_band_scale = vpx_vertical_band_5_3_scale;
      source_band_height = 5;
      dest_band_height = 3;
John Koleszar's avatar
John Koleszar committed
309
      break;
John Koleszar's avatar
John Koleszar committed
310
    case 5:
John Koleszar's avatar
John Koleszar committed
311
      /* 1-2 Scale in vertical direction */
John Koleszar's avatar
John Koleszar committed
312

John Koleszar's avatar
John Koleszar committed
313 314
      if (interlaced) {
        /* if the content is interlaced, point sampling is used */
clang-format's avatar
clang-format committed
315
        vert_band_scale = vpx_vertical_band_2_1_scale;
John Koleszar's avatar
John Koleszar committed
316 317 318
      } else {
        interpolation = 1;
        /* if the content is progressive, interplo */
clang-format's avatar
clang-format committed
319
        vert_band_scale = vpx_vertical_band_2_1_scale_i;
John Koleszar's avatar
John Koleszar committed
320
      }
John Koleszar's avatar
John Koleszar committed
321

clang-format's avatar
clang-format committed
322 323
      source_band_height = 2;
      dest_band_height = 1;
John Koleszar's avatar
John Koleszar committed
324
      break;
John Koleszar's avatar
John Koleszar committed
325
    default:
John Koleszar's avatar
John Koleszar committed
326 327 328 329 330 331 332 333 334 335 336 337
      /* The ratio is not acceptable now */
      /* throw("The ratio is not acceptable for now!"); */
      ratio_scalable = 0;
      break;
  }

  if (ratio_scalable) {
    if (source_height == dest_height) {
      /* for each band of the image */
      for (k = 0; k < (int)dest_height; k++) {
        horiz_line_scale(source, source_width, dest, dest_width);
        source += source_pitch;
clang-format's avatar
clang-format committed
338
        dest += dest_pitch;
John Koleszar's avatar
John Koleszar committed
339 340 341
      }

      return;
John Koleszar's avatar
John Koleszar committed
342 343
    }

John Koleszar's avatar
John Koleszar committed
344
    if (interpolation) {
clang-format's avatar
clang-format committed
345
      if (source < source_base) source = source_base;
John Koleszar's avatar
John Koleszar committed
346 347

      horiz_line_scale(source, source_width, temp_area, dest_width);
John Koleszar's avatar
John Koleszar committed
348 349
    }

clang-format's avatar
clang-format committed
350 351 352
    for (k = 0;
         k < (int)(dest_height + dest_band_height - 1) / dest_band_height;
         k++) {
John Koleszar's avatar
John Koleszar committed
353 354 355
      /* scale one band horizontally */
      for (i = 0; i < source_band_height; i++) {
        /* Trap case where we could read off the base of the source buffer */
John Koleszar's avatar
John Koleszar committed
356

John Koleszar's avatar
John Koleszar committed
357
        line_src = (unsigned char *)source + i * source_pitch;
John Koleszar's avatar
John Koleszar committed
358

clang-format's avatar
clang-format committed
359
        if (line_src < source_base) line_src = source_base;
John Koleszar's avatar
John Koleszar committed
360 361

        horiz_line_scale(line_src, source_width,
clang-format's avatar
clang-format committed
362
                         temp_area + (i + 1) * dest_pitch, dest_width);
John Koleszar's avatar
John Koleszar committed
363 364 365
      }

      /* Vertical scaling is in place */
clang-format's avatar
clang-format committed
366 367
      vert_band_scale(temp_area + dest_pitch, dest_pitch, dest, dest_pitch,
                      dest_width);
John Koleszar's avatar
John Koleszar committed
368

John Koleszar's avatar
John Koleszar committed
369
      if (interpolation)
clang-format's avatar
clang-format committed
370 371
        memcpy(temp_area, temp_area + source_band_height * dest_pitch,
               dest_width);
John Koleszar's avatar
John Koleszar committed
372 373

      /* Next band... */
clang-format's avatar
clang-format committed
374 375
      source += (unsigned long)source_band_height * source_pitch;
      dest += (unsigned long)dest_band_height * dest_pitch;
John Koleszar's avatar
John Koleszar committed
376
    }
John Koleszar's avatar
John Koleszar committed
377 378 379 380

    return;
  }

clang-format's avatar
clang-format committed
381
  if (hscale == 2 && hratio == 1) Scale1Dh = scale1d_2t1_ps;
John Koleszar's avatar
John Koleszar committed
382 383 384 385

  if (vscale == 2 && vratio == 1) {
    if (interlaced)
      Scale1Dv = scale1d_2t1_ps;
John Koleszar's avatar
John Koleszar committed
386
    else
John Koleszar's avatar
John Koleszar committed
387 388 389 390 391 392
      Scale1Dv = scale1d_2t1_i;
  }

  if (source_height == dest_height) {
    /* for each band of the image */
    for (k = 0; k < (int)dest_height; k++) {
clang-format's avatar
clang-format committed
393 394
      Scale1Dh(source, 1, hscale, source_width + 1, dest, 1, hratio,
               dest_width);
John Koleszar's avatar
John Koleszar committed
395
      source += source_pitch;
clang-format's avatar
clang-format committed
396
      dest += dest_pitch;
John Koleszar's avatar
John Koleszar committed
397 398
    }

John Koleszar's avatar
John Koleszar committed
399 400 401 402
    return;
  }

  if (dest_height > source_height) {
clang-format's avatar
clang-format committed
403
    dest_band_height = temp_area_height - 1;
John Koleszar's avatar
John Koleszar committed
404 405 406
    source_band_height = dest_band_height * source_height / dest_height;
  } else {
    source_band_height = temp_area_height - 1;
clang-format's avatar
clang-format committed
407
    dest_band_height = source_band_height * vratio / vscale;
John Koleszar's avatar
John Koleszar committed
408
  }
John Koleszar's avatar
John Koleszar committed
409

clang-format's avatar
clang-format committed
410 411 412 413
  /* first row needs to be done so that we can stay one row ahead for vertical
   * zoom */
  Scale1Dh(source, 1, hscale, source_width + 1, temp_area, 1, hratio,
           dest_width);
John Koleszar's avatar
John Koleszar committed
414

John Koleszar's avatar
John Koleszar committed
415 416 417 418 419 420
  /* for each band of the image */
  bands = (dest_height + dest_band_height - 1) / dest_band_height;

  for (k = 0; k < bands; k++) {
    /* scale one band horizontally */
    for (i = 1; i < source_band_height + 1; i++) {
clang-format's avatar
clang-format committed
421
      if (k * source_band_height + i < (int)source_height) {
John Koleszar's avatar
John Koleszar committed
422 423 424
        Scale1Dh(source + i * source_pitch, 1, hscale, source_width + 1,
                 temp_area + i * dest_pitch, 1, hratio, dest_width);
      } else { /*  Duplicate the last row */
425
        /* copy temp_area row 0 over from last row in the past */
clang-format's avatar
clang-format committed
426 427
        memcpy(temp_area + i * dest_pitch, temp_area + (i - 1) * dest_pitch,
               dest_pitch);
John Koleszar's avatar
John Koleszar committed
428 429
      }
    }
John Koleszar's avatar
John Koleszar committed
430

John Koleszar's avatar
John Koleszar committed
431 432 433 434
    /* scale one band vertically */
    for (j = 0; j < (int)dest_width; j++) {
      Scale1Dv(&temp_area[j], dest_pitch, vscale, source_band_height + 1,
               &dest[j], dest_pitch, vratio, dest_band_height);
John Koleszar's avatar
John Koleszar committed
435
    }
John Koleszar's avatar
John Koleszar committed
436 437

    /* copy temp_area row 0 over from last row in the past */
James Zern's avatar
James Zern committed
438
    memcpy(temp_area, temp_area + source_band_height * dest_pitch, dest_pitch);
John Koleszar's avatar
John Koleszar committed
439 440 441

    /* move to the next band */
    source += source_band_height * source_pitch;
clang-format's avatar
clang-format committed
442
    dest += dest_band_height * dest_pitch;
John Koleszar's avatar
John Koleszar committed
443
  }
John Koleszar's avatar
John Koleszar committed
444 445 446 447
}

/****************************************************************************
 *
Johann's avatar
Johann committed
448
 *  ROUTINE       : vpx_scale_frame
John Koleszar's avatar
John Koleszar committed
449
 *
clang-format's avatar
clang-format committed
450 451 452 453 454
 *  INPUTS        : YV12_BUFFER_CONFIG *src        : Pointer to frame to be
 *                                                   scaled.
 *                  YV12_BUFFER_CONFIG *dst        : Pointer to buffer to hold
 *                                                   scaled frame.
 *                  unsigned char *temp_area       : Pointer to temp work area.
John Koleszar's avatar
John Koleszar committed
455
 *                  unsigned char temp_area_height : Height of temp work area.
clang-format's avatar
clang-format committed
456 457 458 459 460 461 462 463 464
 *                  unsigned int hscale            : Horizontal scale factor
 *                                                   numerator.
 *                  unsigned int hratio            : Horizontal scale factor
 *                                                   denominator.
 *                  unsigned int vscale            : Vertical scale factor
 *                                                   numerator.
 *                  unsigned int vratio            : Vertical scale factor
 *                                                   denominator.
 *                  unsigned int interlaced        : Interlace flag.
John Koleszar's avatar
John Koleszar committed
465 466 467 468 469 470 471 472 473 474 475
 *
 *  OUTPUTS       : None.
 *
 *  RETURNS       : void
 *
 *  FUNCTION      : Performs 2-tap linear interpolation in two dimensions.
 *
 *  SPECIAL NOTES : Expansion is performed one band at a time to help with
 *                  caching.
 *
 ****************************************************************************/
clang-format's avatar
clang-format committed
476 477 478 479 480
void vpx_scale_frame(YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
                     unsigned char *temp_area, unsigned char temp_height,
                     unsigned int hscale, unsigned int hratio,
                     unsigned int vscale, unsigned int vratio,
                     unsigned int interlaced) {
John Koleszar's avatar
John Koleszar committed
481 482 483 484 485
  int i;
  int dw = (hscale - 1 + src->y_width * hratio) / hscale;
  int dh = (vscale - 1 + src->y_height * vratio) / vscale;

  /* call our internal scaling routines!! */
clang-format's avatar
clang-format committed
486 487
  Scale2D((unsigned char *)src->y_buffer, src->y_stride, src->y_width,
          src->y_height, (unsigned char *)dst->y_buffer, dst->y_stride, dw, dh,
John Koleszar's avatar
John Koleszar committed
488 489 490 491
          temp_area, temp_height, hscale, hratio, vscale, vratio, interlaced);

  if (dw < (int)dst->y_width)
    for (i = 0; i < dh; i++)
clang-format's avatar
clang-format committed
492 493
      memset(dst->y_buffer + i * dst->y_stride + dw - 1,
             dst->y_buffer[i * dst->y_stride + dw - 2], dst->y_width - dw + 1);
John Koleszar's avatar
John Koleszar committed
494 495 496

  if (dh < (int)dst->y_height)
    for (i = dh - 1; i < (int)dst->y_height; i++)
clang-format's avatar
clang-format committed
497 498
      memcpy(dst->y_buffer + i * dst->y_stride,
             dst->y_buffer + (dh - 2) * dst->y_stride, dst->y_width + 1);
John Koleszar's avatar
John Koleszar committed
499

clang-format's avatar
clang-format committed
500 501 502 503
  Scale2D((unsigned char *)src->u_buffer, src->uv_stride, src->uv_width,
          src->uv_height, (unsigned char *)dst->u_buffer, dst->uv_stride,
          dw / 2, dh / 2, temp_area, temp_height, hscale, hratio, vscale,
          vratio, interlaced);
John Koleszar's avatar
John Koleszar committed
504 505 506

  if (dw / 2 < (int)dst->uv_width)
    for (i = 0; i < dst->uv_height; i++)
clang-format's avatar
clang-format committed
507 508 509
      memset(dst->u_buffer + i * dst->uv_stride + dw / 2 - 1,
             dst->u_buffer[i * dst->uv_stride + dw / 2 - 2],
             dst->uv_width - dw / 2 + 1);
John Koleszar's avatar
John Koleszar committed
510 511 512

  if (dh / 2 < (int)dst->uv_height)
    for (i = dh / 2 - 1; i < (int)dst->y_height / 2; i++)
clang-format's avatar
clang-format committed
513 514
      memcpy(dst->u_buffer + i * dst->uv_stride,
             dst->u_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width);
John Koleszar's avatar
John Koleszar committed
515

clang-format's avatar
clang-format committed
516 517 518 519
  Scale2D((unsigned char *)src->v_buffer, src->uv_stride, src->uv_width,
          src->uv_height, (unsigned char *)dst->v_buffer, dst->uv_stride,
          dw / 2, dh / 2, temp_area, temp_height, hscale, hratio, vscale,
          vratio, interlaced);
John Koleszar's avatar
John Koleszar committed
520 521 522

  if (dw / 2 < (int)dst->uv_width)
    for (i = 0; i < dst->uv_height; i++)
clang-format's avatar
clang-format committed
523 524 525
      memset(dst->v_buffer + i * dst->uv_stride + dw / 2 - 1,
             dst->v_buffer[i * dst->uv_stride + dw / 2 - 2],
             dst->uv_width - dw / 2 + 1);
John Koleszar's avatar
John Koleszar committed
526

clang-format's avatar
clang-format committed
527
  if (dh / 2 < (int)dst->uv_height)
John Koleszar's avatar
John Koleszar committed
528
    for (i = dh / 2 - 1; i < (int)dst->y_height / 2; i++)
clang-format's avatar
clang-format committed
529 530
      memcpy(dst->v_buffer + i * dst->uv_stride,
             dst->v_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width);
John Koleszar's avatar
John Koleszar committed
531
}