media/libvpx/vp9/common/vp9_scale.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp9/common/vp9_scale.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +#include "./vp9_rtcd.h"
    1.15 +#include "vp9/common/vp9_filter.h"
    1.16 +#include "vp9/common/vp9_scale.h"
    1.17 +
    1.18 +static INLINE int scaled_x(int val, const struct scale_factors_common *sfc) {
    1.19 +  return val * sfc->x_scale_fp >> REF_SCALE_SHIFT;
    1.20 +}
    1.21 +
    1.22 +static INLINE int scaled_y(int val, const struct scale_factors_common *sfc) {
    1.23 +  return val * sfc->y_scale_fp >> REF_SCALE_SHIFT;
    1.24 +}
    1.25 +
    1.26 +static int unscaled_value(int val, const struct scale_factors_common *sfc) {
    1.27 +  (void) sfc;
    1.28 +  return val;
    1.29 +}
    1.30 +
    1.31 +static MV32 scaled_mv(const MV *mv, const struct scale_factors *scale) {
    1.32 +  const MV32 res = {
    1.33 +    scaled_y(mv->row, scale->sfc) + scale->y_offset_q4,
    1.34 +    scaled_x(mv->col, scale->sfc) + scale->x_offset_q4
    1.35 +  };
    1.36 +  return res;
    1.37 +}
    1.38 +
    1.39 +static MV32 unscaled_mv(const MV *mv, const struct scale_factors *scale) {
    1.40 +  const MV32 res = {
    1.41 +    mv->row,
    1.42 +    mv->col
    1.43 +  };
    1.44 +  return res;
    1.45 +}
    1.46 +
    1.47 +static void set_offsets_with_scaling(struct scale_factors *scale,
    1.48 +                                     int row, int col) {
    1.49 +  scale->x_offset_q4 = scaled_x(col << SUBPEL_BITS, scale->sfc) & SUBPEL_MASK;
    1.50 +  scale->y_offset_q4 = scaled_y(row << SUBPEL_BITS, scale->sfc) & SUBPEL_MASK;
    1.51 +}
    1.52 +
    1.53 +static void set_offsets_without_scaling(struct scale_factors *scale,
    1.54 +                                        int row, int col) {
    1.55 +  scale->x_offset_q4 = 0;
    1.56 +  scale->y_offset_q4 = 0;
    1.57 +}
    1.58 +
    1.59 +static int get_fixed_point_scale_factor(int other_size, int this_size) {
    1.60 +  // Calculate scaling factor once for each reference frame
    1.61 +  // and use fixed point scaling factors in decoding and encoding routines.
    1.62 +  // Hardware implementations can calculate scale factor in device driver
    1.63 +  // and use multiplication and shifting on hardware instead of division.
    1.64 +  return (other_size << REF_SCALE_SHIFT) / this_size;
    1.65 +}
    1.66 +
    1.67 +static int check_scale_factors(int other_w, int other_h,
    1.68 +                               int this_w, int this_h) {
    1.69 +  return 2 * this_w >= other_w &&
    1.70 +         2 * this_h >= other_h &&
    1.71 +         this_w <= 16 * other_w &&
    1.72 +         this_h <= 16 * other_h;
    1.73 +}
    1.74 +
    1.75 +void vp9_setup_scale_factors_for_frame(struct scale_factors *scale,
    1.76 +                                       struct scale_factors_common *scale_comm,
    1.77 +                                       int other_w, int other_h,
    1.78 +                                       int this_w, int this_h) {
    1.79 +  if (!check_scale_factors(other_w, other_h, this_w, this_h)) {
    1.80 +    scale_comm->x_scale_fp = REF_INVALID_SCALE;
    1.81 +    scale_comm->y_scale_fp = REF_INVALID_SCALE;
    1.82 +    return;
    1.83 +  }
    1.84 +
    1.85 +  scale_comm->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
    1.86 +  scale_comm->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
    1.87 +  scale_comm->x_step_q4 = scaled_x(16, scale_comm);
    1.88 +  scale_comm->y_step_q4 = scaled_y(16, scale_comm);
    1.89 +
    1.90 +  if (vp9_is_scaled(scale_comm)) {
    1.91 +    scale_comm->scale_value_x = scaled_x;
    1.92 +    scale_comm->scale_value_y = scaled_y;
    1.93 +    scale_comm->set_scaled_offsets = set_offsets_with_scaling;
    1.94 +    scale_comm->scale_mv = scaled_mv;
    1.95 +  } else {
    1.96 +    scale_comm->scale_value_x = unscaled_value;
    1.97 +    scale_comm->scale_value_y = unscaled_value;
    1.98 +    scale_comm->set_scaled_offsets = set_offsets_without_scaling;
    1.99 +    scale_comm->scale_mv = unscaled_mv;
   1.100 +  }
   1.101 +
   1.102 +  // TODO(agrange): Investigate the best choice of functions to use here
   1.103 +  // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
   1.104 +  // to do at full-pel offsets. The current selection, where the filter is
   1.105 +  // applied in one direction only, and not at all for 0,0, seems to give the
   1.106 +  // best quality, but it may be worth trying an additional mode that does
   1.107 +  // do the filtering on full-pel.
   1.108 +  if (scale_comm->x_step_q4 == 16) {
   1.109 +    if (scale_comm->y_step_q4 == 16) {
   1.110 +      // No scaling in either direction.
   1.111 +      scale_comm->predict[0][0][0] = vp9_convolve_copy;
   1.112 +      scale_comm->predict[0][0][1] = vp9_convolve_avg;
   1.113 +      scale_comm->predict[0][1][0] = vp9_convolve8_vert;
   1.114 +      scale_comm->predict[0][1][1] = vp9_convolve8_avg_vert;
   1.115 +      scale_comm->predict[1][0][0] = vp9_convolve8_horiz;
   1.116 +      scale_comm->predict[1][0][1] = vp9_convolve8_avg_horiz;
   1.117 +    } else {
   1.118 +      // No scaling in x direction. Must always scale in the y direction.
   1.119 +      scale_comm->predict[0][0][0] = vp9_convolve8_vert;
   1.120 +      scale_comm->predict[0][0][1] = vp9_convolve8_avg_vert;
   1.121 +      scale_comm->predict[0][1][0] = vp9_convolve8_vert;
   1.122 +      scale_comm->predict[0][1][1] = vp9_convolve8_avg_vert;
   1.123 +      scale_comm->predict[1][0][0] = vp9_convolve8;
   1.124 +      scale_comm->predict[1][0][1] = vp9_convolve8_avg;
   1.125 +    }
   1.126 +  } else {
   1.127 +    if (scale_comm->y_step_q4 == 16) {
   1.128 +      // No scaling in the y direction. Must always scale in the x direction.
   1.129 +      scale_comm->predict[0][0][0] = vp9_convolve8_horiz;
   1.130 +      scale_comm->predict[0][0][1] = vp9_convolve8_avg_horiz;
   1.131 +      scale_comm->predict[0][1][0] = vp9_convolve8;
   1.132 +      scale_comm->predict[0][1][1] = vp9_convolve8_avg;
   1.133 +      scale_comm->predict[1][0][0] = vp9_convolve8_horiz;
   1.134 +      scale_comm->predict[1][0][1] = vp9_convolve8_avg_horiz;
   1.135 +    } else {
   1.136 +      // Must always scale in both directions.
   1.137 +      scale_comm->predict[0][0][0] = vp9_convolve8;
   1.138 +      scale_comm->predict[0][0][1] = vp9_convolve8_avg;
   1.139 +      scale_comm->predict[0][1][0] = vp9_convolve8;
   1.140 +      scale_comm->predict[0][1][1] = vp9_convolve8_avg;
   1.141 +      scale_comm->predict[1][0][0] = vp9_convolve8;
   1.142 +      scale_comm->predict[1][0][1] = vp9_convolve8_avg;
   1.143 +    }
   1.144 +  }
   1.145 +  // 2D subpel motion always gets filtered in both directions
   1.146 +  scale_comm->predict[1][1][0] = vp9_convolve8;
   1.147 +  scale_comm->predict[1][1][1] = vp9_convolve8_avg;
   1.148 +
   1.149 +  scale->sfc = scale_comm;
   1.150 +  scale->x_offset_q4 = 0;  // calculated per block
   1.151 +  scale->y_offset_q4 = 0;  // calculated per block
   1.152 +}

mercurial