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_scale.h"
13 #include "vpx_filter.h"
14 
scaled_x(int val,const struct scale_factors * sf)15 static 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 
scaled_y(int val,const struct scale_factors * sf)19 static 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 
unscaled_value(int val,const struct scale_factors * sf)23 static int unscaled_value(int val, const struct scale_factors *sf) {
24   (void)sf;
25   return val;
26 }
27 
get_fixed_point_scale_factor(int other_size,int this_size)28 static 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 
eb_vp9_scale_mv(const MV * mv,int x,int y,const struct scale_factors * sf)36 MV32 eb_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 = { scaled_y(mv->row, sf) + y_off_q4,
40                      scaled_x(mv->col, sf) + x_off_q4 };
41   return res;
42 }
43 
44 #if CONFIG_VP9_HIGHBITDEPTH
eb_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)45 void eb_vp9_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
46                                        int other_h, int this_w, int this_h,
47                                        int use_highbd) {
48 #else
49 void eb_vp9_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
50                                        int other_h, int this_w, int this_h) {
51 #endif
52   if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
53     sf->x_scale_fp = REF_INVALID_SCALE;
54     sf->y_scale_fp = REF_INVALID_SCALE;
55     return;
56   }
57 
58   sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
59   sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
60   sf->x_step_q4 = scaled_x(16, sf);
61   sf->y_step_q4 = scaled_y(16, sf);
62 
63   if (vp9_is_scaled(sf)) {
64     sf->scale_value_x = scaled_x;
65     sf->scale_value_y = scaled_y;
66   } else {
67     sf->scale_value_x = unscaled_value;
68     sf->scale_value_y = unscaled_value;
69   }
70 
71   // TODO(agrange): Investigate the best choice of functions to use here
72   // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
73   // to do at full-pel offsets. The current selection, where the filter is
74   // applied in one direction only, and not at all for 0,0, seems to give the
75   // best quality, but it may be worth trying an additional mode that does
76   // do the filtering on full-pel.
77 
78   if (sf->x_step_q4 == 16) {
79     if (sf->y_step_q4 == 16) {
80       // No scaling in either direction.
81       sf->predict[0][0][0] = vpx_convolve_copy;
82       sf->predict[0][0][1] = vpx_convolve_avg;
83       sf->predict[0][1][0] = eb_vp9_convolve8_vert;
84       sf->predict[0][1][1] = eb_vp9_convolve8_avg_vert;
85       sf->predict[1][0][0] = eb_vp9_convolve8_horiz;
86       sf->predict[1][0][1] = eb_vp9_convolve8_avg_horiz;
87     } else {
88 #if 0
89       // No scaling in x direction. Must always scale in the y direction.
90       sf->predict[0][0][0] = vpx_scaled_vert;
91       sf->predict[0][0][1] = vpx_scaled_avg_vert;
92       sf->predict[0][1][0] = vpx_scaled_vert;
93       sf->predict[0][1][1] = vpx_scaled_avg_vert;
94       sf->predict[1][0][0] = vpx_scaled_2d;
95       sf->predict[1][0][1] = vpx_scaled_avg_2d;
96 #endif
97     }
98   } else {
99     if (sf->y_step_q4 == 16) {
100 #if 0
101       // No scaling in the y direction. Must always scale in the x direction.
102       sf->predict[0][0][0] = vpx_scaled_horiz;
103       sf->predict[0][0][1] = vpx_scaled_avg_horiz;
104       sf->predict[0][1][0] = vpx_scaled_2d;
105       sf->predict[0][1][1] = vpx_scaled_avg_2d;
106       sf->predict[1][0][0] = vpx_scaled_horiz;
107       sf->predict[1][0][1] = vpx_scaled_avg_horiz;
108 #endif
109     } else {
110 #if 0
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 #endif
119     }
120   }
121 
122   // 2D subpel motion always gets filtered in both directions
123 
124   if ((sf->x_step_q4 != 16) || (sf->y_step_q4 != 16)) {
125 #if 0
126     sf->predict[1][1][0] = vpx_scaled_2d;
127     sf->predict[1][1][1] = vpx_scaled_avg_2d;
128 #endif
129   } else {
130     sf->predict[1][1][0] = eb_vp9_convolve8;
131     sf->predict[1][1][1] = eb_vp9_convolve8_avg;
132   }
133 
134 #if CONFIG_VP9_HIGHBITDEPTH
135   if (use_highbd) {
136     if (sf->x_step_q4 == 16) {
137       if (sf->y_step_q4 == 16) {
138         // No scaling in either direction.
139         sf->highbd_predict[0][0][0] = vpx_highbd_convolve_copy;
140         sf->highbd_predict[0][0][1] = vpx_highbd_convolve_avg;
141         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
142         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
143         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
144         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
145       } else {
146         // No scaling in x direction. Must always scale in the y direction.
147         sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_vert;
148         sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_vert;
149         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
150         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
151         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
152         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
153       }
154     } else {
155       if (sf->y_step_q4 == 16) {
156         // No scaling in the y direction. Must always scale in the x direction.
157         sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_horiz;
158         sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_horiz;
159         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
160         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
161         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
162         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
163       } else {
164         // Must always scale in both directions.
165         sf->highbd_predict[0][0][0] = vpx_highbd_convolve8;
166         sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg;
167         sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
168         sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
169         sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
170         sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
171       }
172     }
173     // 2D subpel motion always gets filtered in both directions.
174     sf->highbd_predict[1][1][0] = vpx_highbd_convolve8;
175     sf->highbd_predict[1][1][1] = vpx_highbd_convolve8_avg;
176   }
177 #endif
178 }
179