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 "./vpx_dsp_rtcd.h"
12 #include "vp9/common/vp9_filter.h"
13 #include "vp9/common/vp9_scale.h"
14 #include "vpx_dsp/vpx_filter.h"
15 
scaled_x(int val,const struct scale_factors * sf)16 static INLINE int scaled_x(int val, const struct scale_factors *sf) {
17   return (int)((int64_t)val * sf->x_scale_fp >> REF_SCALE_SHIFT);
18 }
19 
scaled_y(int val,const struct scale_factors * sf)20 static INLINE int scaled_y(int val, const struct scale_factors *sf) {
21   return (int)((int64_t)val * sf->y_scale_fp >> REF_SCALE_SHIFT);
22 }
23 
unscaled_value(int val,const struct scale_factors * sf)24 static int unscaled_value(int val, const struct scale_factors *sf) {
25   (void) sf;
26   return val;
27 }
28 
get_fixed_point_scale_factor(int other_size,int this_size)29 static int get_fixed_point_scale_factor(int other_size, int this_size) {
30   // Calculate scaling factor once for each reference frame
31   // and use fixed point scaling factors in decoding and encoding routines.
32   // Hardware implementations can calculate scale factor in device driver
33   // and use multiplication and shifting on hardware instead of division.
34   return (other_size << REF_SCALE_SHIFT) / this_size;
35 }
36 
vp9_scale_mv(const MV * mv,int x,int y,const struct scale_factors * sf)37 MV32 vp9_scale_mv(const MV *mv, int x, int y, const struct scale_factors *sf) {
38   const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf) & SUBPEL_MASK;
39   const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf) & SUBPEL_MASK;
40   const MV32 res = {
41     scaled_y(mv->row, sf) + y_off_q4,
42     scaled_x(mv->col, sf) + x_off_q4
43   };
44   return res;
45 }
46 
47 #if CONFIG_VP9_HIGHBITDEPTH
vp9_setup_scale_factors_for_frame(struct scale_factors * sf,int other_w,int other_h,int this_w,int this_h,int use_highbd)48 void vp9_setup_scale_factors_for_frame(struct scale_factors *sf,
49                                        int other_w, int other_h,
50                                        int this_w, int this_h,
51                                        int use_highbd) {
52 #else
53 void vp9_setup_scale_factors_for_frame(struct scale_factors *sf,
54                                        int other_w, int other_h,
55                                        int this_w, int this_h) {
56 #endif
57   if (!valid_ref_frame_size(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 
83   if (sf->x_step_q4 == 16) {
84     if (sf->y_step_q4 == 16) {
85       // No scaling in either direction.
86       sf->predict[0][0][0] = vpx_convolve_copy;
87       sf->predict[0][0][1] = vpx_convolve_avg;
88       sf->predict[0][1][0] = vpx_convolve8_vert;
89       sf->predict[0][1][1] = vpx_convolve8_avg_vert;
90       sf->predict[1][0][0] = vpx_convolve8_horiz;
91       sf->predict[1][0][1] = vpx_convolve8_avg_horiz;
92     } else {
93       // No scaling in x direction. Must always scale in the y direction.
94       sf->predict[0][0][0] = vpx_scaled_vert;
95       sf->predict[0][0][1] = vpx_scaled_avg_vert;
96       sf->predict[0][1][0] = vpx_scaled_vert;
97       sf->predict[0][1][1] = vpx_scaled_avg_vert;
98       sf->predict[1][0][0] = vpx_scaled_2d;
99       sf->predict[1][0][1] = vpx_scaled_avg_2d;
100     }
101   } else {
102     if (sf->y_step_q4 == 16) {
103       // No scaling in the y direction. Must always scale in the x direction.
104       sf->predict[0][0][0] = vpx_scaled_horiz;
105       sf->predict[0][0][1] = vpx_scaled_avg_horiz;
106       sf->predict[0][1][0] = vpx_scaled_2d;
107       sf->predict[0][1][1] = vpx_scaled_avg_2d;
108       sf->predict[1][0][0] = vpx_scaled_horiz;
109       sf->predict[1][0][1] = vpx_scaled_avg_horiz;
110     } else {
111       // Must always scale in both directions.
112       sf->predict[0][0][0] = vpx_scaled_2d;
113       sf->predict[0][0][1] = vpx_scaled_avg_2d;
114       sf->predict[0][1][0] = vpx_scaled_2d;
115       sf->predict[0][1][1] = vpx_scaled_avg_2d;
116       sf->predict[1][0][0] = vpx_scaled_2d;
117       sf->predict[1][0][1] = vpx_scaled_avg_2d;
118     }
119   }
120 
121   // 2D subpel motion always gets filtered in both directions
122 
123   if ((sf->x_step_q4 != 16) || (sf->y_step_q4 != 16)) {
124     sf->predict[1][1][0] = vpx_scaled_2d;
125     sf->predict[1][1][1] = vpx_scaled_avg_2d;
126   } else {
127     sf->predict[1][1][0] = vpx_convolve8;
128     sf->predict[1][1][1] = vpx_convolve8_avg;
129   }
130 
131 #if CONFIG_VP9_HIGHBITDEPTH
132   if (use_highbd) {
133     if (sf->x_step_q4 == 16) {
134       if (sf->y_step_q4 == 16) {
135         // No scaling in either direction.
136         sf->highbd_predict[0][0][0] = vpx_highbd_convolve_copy;
137         sf->highbd_predict[0][0][1] = vpx_highbd_convolve_avg;
138         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
139         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
140         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
141         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
142       } else {
143         // No scaling in x direction. Must always scale in the y direction.
144         sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_vert;
145         sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_vert;
146         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
147         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
148         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
149         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
150       }
151     } else {
152       if (sf->y_step_q4 == 16) {
153         // No scaling in the y direction. Must always scale in the x direction.
154         sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_horiz;
155         sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_horiz;
156         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
157         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
158         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
159         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
160       } else {
161         // Must always scale in both directions.
162         sf->highbd_predict[0][0][0] = vpx_highbd_convolve8;
163         sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg;
164         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
165         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
166         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
167         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
168       }
169     }
170     // 2D subpel motion always gets filtered in both directions.
171     sf->highbd_predict[1][1][0] = vpx_highbd_convolve8;
172     sf->highbd_predict[1][1][1] = vpx_highbd_convolve8_avg;
173   }
174 #endif
175 }
176