1/*
2 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  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.
9 */
10
11#include "./vp9_rtcd.h"
12#include "vp9/common/vp9_filter.h"
13#include "vp9/common/vp9_scale.h"
14
15static INLINE int scaled_x(int val, const struct scale_factors *sf) {
16  return (int)((int64_t)val * sf->x_scale_fp >> REF_SCALE_SHIFT);
17}
18
19static INLINE int scaled_y(int val, const struct scale_factors *sf) {
20  return (int)((int64_t)val * sf->y_scale_fp >> REF_SCALE_SHIFT);
21}
22
23static int unscaled_value(int val, const struct scale_factors *sf) {
24  (void) sf;
25  return val;
26}
27
28static int get_fixed_point_scale_factor(int other_size, int this_size) {
29  // Calculate scaling factor once for each reference frame
30  // and use fixed point scaling factors in decoding and encoding routines.
31  // Hardware implementations can calculate scale factor in device driver
32  // and use multiplication and shifting on hardware instead of division.
33  return (other_size << REF_SCALE_SHIFT) / this_size;
34}
35
36MV32 vp9_scale_mv(const MV *mv, int x, int y, const struct scale_factors *sf) {
37  const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf) & SUBPEL_MASK;
38  const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf) & SUBPEL_MASK;
39  const MV32 res = {
40    scaled_y(mv->row, sf) + y_off_q4,
41    scaled_x(mv->col, sf) + x_off_q4
42  };
43  return res;
44}
45
46#if CONFIG_VP9_HIGHBITDEPTH
47void vp9_setup_scale_factors_for_frame(struct scale_factors *sf,
48                                       int other_w, int other_h,
49                                       int this_w, int this_h,
50                                       int use_high) {
51#else
52void vp9_setup_scale_factors_for_frame(struct scale_factors *sf,
53                                       int other_w, int other_h,
54                                       int this_w, int this_h) {
55#endif
56  if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
57    sf->x_scale_fp = REF_INVALID_SCALE;
58    sf->y_scale_fp = REF_INVALID_SCALE;
59    return;
60  }
61
62  sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
63  sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
64  sf->x_step_q4 = scaled_x(16, sf);
65  sf->y_step_q4 = scaled_y(16, sf);
66
67  if (vp9_is_scaled(sf)) {
68    sf->scale_value_x = scaled_x;
69    sf->scale_value_y = scaled_y;
70  } else {
71    sf->scale_value_x = unscaled_value;
72    sf->scale_value_y = unscaled_value;
73  }
74
75  // TODO(agrange): Investigate the best choice of functions to use here
76  // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
77  // to do at full-pel offsets. The current selection, where the filter is
78  // applied in one direction only, and not at all for 0,0, seems to give the
79  // best quality, but it may be worth trying an additional mode that does
80  // do the filtering on full-pel.
81  if (sf->x_step_q4 == 16) {
82    if (sf->y_step_q4 == 16) {
83      // No scaling in either direction.
84      sf->predict[0][0][0] = vp9_convolve_copy;
85      sf->predict[0][0][1] = vp9_convolve_avg;
86      sf->predict[0][1][0] = vp9_convolve8_vert;
87      sf->predict[0][1][1] = vp9_convolve8_avg_vert;
88      sf->predict[1][0][0] = vp9_convolve8_horiz;
89      sf->predict[1][0][1] = vp9_convolve8_avg_horiz;
90    } else {
91      // No scaling in x direction. Must always scale in the y direction.
92      sf->predict[0][0][0] = vp9_convolve8_vert;
93      sf->predict[0][0][1] = vp9_convolve8_avg_vert;
94      sf->predict[0][1][0] = vp9_convolve8_vert;
95      sf->predict[0][1][1] = vp9_convolve8_avg_vert;
96      sf->predict[1][0][0] = vp9_convolve8;
97      sf->predict[1][0][1] = vp9_convolve8_avg;
98    }
99  } else {
100    if (sf->y_step_q4 == 16) {
101      // No scaling in the y direction. Must always scale in the x direction.
102      sf->predict[0][0][0] = vp9_convolve8_horiz;
103      sf->predict[0][0][1] = vp9_convolve8_avg_horiz;
104      sf->predict[0][1][0] = vp9_convolve8;
105      sf->predict[0][1][1] = vp9_convolve8_avg;
106      sf->predict[1][0][0] = vp9_convolve8_horiz;
107      sf->predict[1][0][1] = vp9_convolve8_avg_horiz;
108    } else {
109      // Must always scale in both directions.
110      sf->predict[0][0][0] = vp9_convolve8;
111      sf->predict[0][0][1] = vp9_convolve8_avg;
112      sf->predict[0][1][0] = vp9_convolve8;
113      sf->predict[0][1][1] = vp9_convolve8_avg;
114      sf->predict[1][0][0] = vp9_convolve8;
115      sf->predict[1][0][1] = vp9_convolve8_avg;
116    }
117  }
118  // 2D subpel motion always gets filtered in both directions
119  sf->predict[1][1][0] = vp9_convolve8;
120  sf->predict[1][1][1] = vp9_convolve8_avg;
121#if CONFIG_VP9_HIGHBITDEPTH
122  if (use_high) {
123    if (sf->x_step_q4 == 16) {
124      if (sf->y_step_q4 == 16) {
125        // No scaling in either direction.
126        sf->high_predict[0][0][0] = vp9_high_convolve_copy;
127        sf->high_predict[0][0][1] = vp9_high_convolve_avg;
128        sf->high_predict[0][1][0] = vp9_high_convolve8_vert;
129        sf->high_predict[0][1][1] = vp9_high_convolve8_avg_vert;
130        sf->high_predict[1][0][0] = vp9_high_convolve8_horiz;
131        sf->high_predict[1][0][1] = vp9_high_convolve8_avg_horiz;
132      } else {
133        // No scaling in x direction. Must always scale in the y direction.
134        sf->high_predict[0][0][0] = vp9_high_convolve8_vert;
135        sf->high_predict[0][0][1] = vp9_high_convolve8_avg_vert;
136        sf->high_predict[0][1][0] = vp9_high_convolve8_vert;
137        sf->high_predict[0][1][1] = vp9_high_convolve8_avg_vert;
138        sf->high_predict[1][0][0] = vp9_high_convolve8;
139        sf->high_predict[1][0][1] = vp9_high_convolve8_avg;
140      }
141    } else {
142      if (sf->y_step_q4 == 16) {
143        // No scaling in the y direction. Must always scale in the x direction.
144        sf->high_predict[0][0][0] = vp9_high_convolve8_horiz;
145        sf->high_predict[0][0][1] = vp9_high_convolve8_avg_horiz;
146        sf->high_predict[0][1][0] = vp9_high_convolve8;
147        sf->high_predict[0][1][1] = vp9_high_convolve8_avg;
148        sf->high_predict[1][0][0] = vp9_high_convolve8_horiz;
149        sf->high_predict[1][0][1] = vp9_high_convolve8_avg_horiz;
150      } else {
151        // Must always scale in both directions.
152        sf->high_predict[0][0][0] = vp9_high_convolve8;
153        sf->high_predict[0][0][1] = vp9_high_convolve8_avg;
154        sf->high_predict[0][1][0] = vp9_high_convolve8;
155        sf->high_predict[0][1][1] = vp9_high_convolve8_avg;
156        sf->high_predict[1][0][0] = vp9_high_convolve8;
157        sf->high_predict[1][0][1] = vp9_high_convolve8_avg;
158      }
159    }
160    // 2D subpel motion always gets filtered in both directions.
161    sf->high_predict[1][1][0] = vp9_high_convolve8;
162    sf->high_predict[1][1][1] = vp9_high_convolve8_avg;
163  }
164#endif
165}
166