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
36static int check_scale_factors(int other_w, int other_h,
37                               int this_w, int this_h) {
38  return 2 * this_w >= other_w &&
39         2 * this_h >= other_h &&
40         this_w <= 16 * other_w &&
41         this_h <= 16 * other_h;
42}
43
44MV32 vp9_scale_mv(const MV *mv, int x, int y, const struct scale_factors *sf) {
45  const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf) & SUBPEL_MASK;
46  const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf) & SUBPEL_MASK;
47  const MV32 res = {
48    scaled_y(mv->row, sf) + y_off_q4,
49    scaled_x(mv->col, sf) + x_off_q4
50  };
51  return res;
52}
53
54void vp9_setup_scale_factors_for_frame(struct scale_factors *sf,
55                                       int other_w, int other_h,
56                                       int this_w, int this_h) {
57  if (!check_scale_factors(other_w, other_h, this_w, this_h)) {
58    sf->x_scale_fp = REF_INVALID_SCALE;
59    sf->y_scale_fp = REF_INVALID_SCALE;
60    return;
61  }
62
63  sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
64  sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
65  sf->x_step_q4 = scaled_x(16, sf);
66  sf->y_step_q4 = scaled_y(16, sf);
67
68  if (vp9_is_scaled(sf)) {
69    sf->scale_value_x = scaled_x;
70    sf->scale_value_y = scaled_y;
71  } else {
72    sf->scale_value_x = unscaled_value;
73    sf->scale_value_y = unscaled_value;
74  }
75
76  // TODO(agrange): Investigate the best choice of functions to use here
77  // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
78  // to do at full-pel offsets. The current selection, where the filter is
79  // applied in one direction only, and not at all for 0,0, seems to give the
80  // best quality, but it may be worth trying an additional mode that does
81  // do the filtering on full-pel.
82  if (sf->x_step_q4 == 16) {
83    if (sf->y_step_q4 == 16) {
84      // No scaling in either direction.
85      sf->predict[0][0][0] = vp9_convolve_copy;
86      sf->predict[0][0][1] = vp9_convolve_avg;
87      sf->predict[0][1][0] = vp9_convolve8_vert;
88      sf->predict[0][1][1] = vp9_convolve8_avg_vert;
89      sf->predict[1][0][0] = vp9_convolve8_horiz;
90      sf->predict[1][0][1] = vp9_convolve8_avg_horiz;
91    } else {
92      // No scaling in x direction. Must always scale in the y direction.
93      sf->predict[0][0][0] = vp9_convolve8_vert;
94      sf->predict[0][0][1] = vp9_convolve8_avg_vert;
95      sf->predict[0][1][0] = vp9_convolve8_vert;
96      sf->predict[0][1][1] = vp9_convolve8_avg_vert;
97      sf->predict[1][0][0] = vp9_convolve8;
98      sf->predict[1][0][1] = vp9_convolve8_avg;
99    }
100  } else {
101    if (sf->y_step_q4 == 16) {
102      // No scaling in the y direction. Must always scale in the x direction.
103      sf->predict[0][0][0] = vp9_convolve8_horiz;
104      sf->predict[0][0][1] = vp9_convolve8_avg_horiz;
105      sf->predict[0][1][0] = vp9_convolve8;
106      sf->predict[0][1][1] = vp9_convolve8_avg;
107      sf->predict[1][0][0] = vp9_convolve8_horiz;
108      sf->predict[1][0][1] = vp9_convolve8_avg_horiz;
109    } else {
110      // Must always scale in both directions.
111      sf->predict[0][0][0] = vp9_convolve8;
112      sf->predict[0][0][1] = vp9_convolve8_avg;
113      sf->predict[0][1][0] = vp9_convolve8;
114      sf->predict[0][1][1] = vp9_convolve8_avg;
115      sf->predict[1][0][0] = vp9_convolve8;
116      sf->predict[1][0][1] = vp9_convolve8_avg;
117    }
118  }
119  // 2D subpel motion always gets filtered in both directions
120  sf->predict[1][1][0] = vp9_convolve8;
121  sf->predict[1][1][1] = vp9_convolve8_avg;
122}
123