vp9_scale.c 5.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "./vp9_rtcd.h"
12
#include "vp9/common/vp9_filter.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
13
#include "vp9/common/vp9_onyxc_int.h"
14 15
#include "vp9/common/vp9_scale.h"

16
static INLINE int scaled_x(int val, const struct scale_factors *scale) {
17 18 19
  return val * scale->x_scale_fp >> VP9_REF_SCALE_SHIFT;
}

20
static INLINE int scaled_y(int val, const struct scale_factors *scale) {
21 22 23 24 25 26 27 28 29 30
  return val * scale->y_scale_fp >> VP9_REF_SCALE_SHIFT;
}

static int unscaled_value(int val, const struct scale_factors *scale) {
  (void) scale;
  return val;
}

static MV32 scaled_mv(const MV *mv, const struct scale_factors *scale) {
  const MV32 res = {
31 32
    scaled_y(mv->row, scale) + scale->y_offset_q4,
    scaled_x(mv->col, scale) + scale->x_offset_q4
33 34 35 36 37 38 39 40 41 42 43 44 45 46
  };
  return res;
}

static MV32 unscaled_mv(const MV *mv, const struct scale_factors *scale) {
  const MV32 res = {
    mv->row,
    mv->col
  };
  return res;
}

static void set_offsets_with_scaling(struct scale_factors *scale,
                                     int row, int col) {
47 48
  scale->x_offset_q4 = scaled_x(col << SUBPEL_BITS, scale) & SUBPEL_MASK;
  scale->y_offset_q4 = scaled_y(row << SUBPEL_BITS, scale) & SUBPEL_MASK;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

static void set_offsets_without_scaling(struct scale_factors *scale,
                                        int row, int col) {
  scale->x_offset_q4 = 0;
  scale->y_offset_q4 = 0;
}

static int get_fixed_point_scale_factor(int other_size, int this_size) {
  // Calculate scaling factor once for each reference frame
  // and use fixed point scaling factors in decoding and encoding routines.
  // Hardware implementations can calculate scale factor in device driver
  // and use multiplication and shifting on hardware instead of division.
  return (other_size << VP9_REF_SCALE_SHIFT) / this_size;
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
65 66 67 68 69 70 71 72 73 74
static int check_scale_factors(int other_w, int other_h,
                               int this_w, int this_h) {
  return 2 * this_w >= other_w &&
         2 * this_h >= other_h &&
         this_w <= 16 * other_w &&
         this_h <= 16 * other_h;
}

void vp9_setup_scale_factors_for_frame(struct VP9Common *cm,
                                       struct scale_factors *scale,
75 76
                                       int other_w, int other_h,
                                       int this_w, int this_h) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
77 78 79 80 81
  if (!check_scale_factors(other_w, other_h, this_w, this_h))
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
                       "Invalid scale factors");


82
  scale->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
83 84
  scale->x_offset_q4 = 0;  // calculated per block
  scale->x_step_q4 = scaled_x(16, scale);
85 86

  scale->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
87 88
  scale->y_offset_q4 = 0;  // calculated per block
  scale->y_step_q4 = scaled_y(16, scale);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  if (other_w == this_w && other_h == this_h) {
    scale->scale_value_x = unscaled_value;
    scale->scale_value_y = unscaled_value;
    scale->set_scaled_offsets = set_offsets_without_scaling;
    scale->scale_mv = unscaled_mv;
  } else {
    scale->scale_value_x = scaled_x;
    scale->scale_value_y = scaled_y;
    scale->set_scaled_offsets = set_offsets_with_scaling;
    scale->scale_mv = scaled_mv;
  }

  // TODO(agrange): Investigate the best choice of functions to use here
  // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
  // to do at full-pel offsets. The current selection, where the filter is
  // applied in one direction only, and not at all for 0,0, seems to give the
  // best quality, but it may be worth trying an additional mode that does
  // do the filtering on full-pel.
  if (scale->x_step_q4 == 16) {
    if (scale->y_step_q4 == 16) {
      // No scaling in either direction.
      scale->predict[0][0][0] = vp9_convolve_copy;
      scale->predict[0][0][1] = vp9_convolve_avg;
      scale->predict[0][1][0] = vp9_convolve8_vert;
      scale->predict[0][1][1] = vp9_convolve8_avg_vert;
      scale->predict[1][0][0] = vp9_convolve8_horiz;
      scale->predict[1][0][1] = vp9_convolve8_avg_horiz;
    } else {
      // No scaling in x direction. Must always scale in the y direction.
      scale->predict[0][0][0] = vp9_convolve8_vert;
      scale->predict[0][0][1] = vp9_convolve8_avg_vert;
      scale->predict[0][1][0] = vp9_convolve8_vert;
      scale->predict[0][1][1] = vp9_convolve8_avg_vert;
      scale->predict[1][0][0] = vp9_convolve8;
      scale->predict[1][0][1] = vp9_convolve8_avg;
    }
  } else {
    if (scale->y_step_q4 == 16) {
      // No scaling in the y direction. Must always scale in the x direction.
      scale->predict[0][0][0] = vp9_convolve8_horiz;
      scale->predict[0][0][1] = vp9_convolve8_avg_horiz;
      scale->predict[0][1][0] = vp9_convolve8;
      scale->predict[0][1][1] = vp9_convolve8_avg;
      scale->predict[1][0][0] = vp9_convolve8_horiz;
      scale->predict[1][0][1] = vp9_convolve8_avg_horiz;
    } else {
      // Must always scale in both directions.
      scale->predict[0][0][0] = vp9_convolve8;
      scale->predict[0][0][1] = vp9_convolve8_avg;
      scale->predict[0][1][0] = vp9_convolve8;
      scale->predict[0][1][1] = vp9_convolve8_avg;
      scale->predict[1][0][0] = vp9_convolve8;
      scale->predict[1][0][1] = vp9_convolve8_avg;
    }
  }
  // 2D subpel motion always gets filtered in both directions
  scale->predict[1][1][0] = vp9_convolve8;
  scale->predict[1][1][1] = vp9_convolve8_avg;
}