1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "./aom_dsp_rtcd.h"
13 #include "av1/common/filter.h"
14 #include "av1/common/scale.h"
15 #include "aom_dsp/aom_filter.h"
16 
17 // Note: Expect val to be in q4 precision
scaled_x(int val,const struct scale_factors * sf)18 static INLINE int scaled_x(int val, const struct scale_factors *sf) {
19   const int off =
20       (sf->x_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
21   const int64_t tval = (int64_t)val * sf->x_scale_fp + off;
22   return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
23                                            REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
24 }
25 
26 // Note: Expect val to be in q4 precision
scaled_y(int val,const struct scale_factors * sf)27 static INLINE int scaled_y(int val, const struct scale_factors *sf) {
28   const int off =
29       (sf->y_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
30   const int64_t tval = (int64_t)val * sf->y_scale_fp + off;
31   return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
32                                            REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
33 }
34 
35 // Note: Expect val to be in q4 precision
unscaled_value(int val,const struct scale_factors * sf)36 static int unscaled_value(int val, const struct scale_factors *sf) {
37   (void)sf;
38   return val << SCALE_EXTRA_BITS;
39 }
40 
get_fixed_point_scale_factor(int other_size,int this_size)41 static int get_fixed_point_scale_factor(int other_size, int this_size) {
42   // Calculate scaling factor once for each reference frame
43   // and use fixed point scaling factors in decoding and encoding routines.
44   // Hardware implementations can calculate scale factor in device driver
45   // and use multiplication and shifting on hardware instead of division.
46   return ((other_size << REF_SCALE_SHIFT) + this_size / 2) / this_size;
47 }
48 
get_coarse_point_scale_factor(int other_size,int this_size)49 static int get_coarse_point_scale_factor(int other_size, int this_size) {
50   // Calculate scaling factor once for each reference frame
51   // and use fixed point scaling factors in decoding and encoding routines.
52   // Hardware implementations can calculate scale factor in device driver
53   // and use multiplication and shifting on hardware instead of division.
54   return ((other_size << SCALE_SUBPEL_BITS) + this_size / 2) / this_size;
55 }
56 
57 // Note: x and y are integer precision, mvq4 is q4 precision.
av1_scale_mv(const MV * mvq4,int x,int y,const struct scale_factors * sf)58 MV32 av1_scale_mv(const MV *mvq4, int x, int y,
59                   const struct scale_factors *sf) {
60   const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf);
61   const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf);
62   const MV32 res = { scaled_y((y << SUBPEL_BITS) + mvq4->row, sf) - y_off_q4,
63                      scaled_x((x << SUBPEL_BITS) + mvq4->col, sf) - x_off_q4 };
64   return res;
65 }
66 
67 #if CONFIG_HIGHBITDEPTH
av1_setup_scale_factors_for_frame(struct scale_factors * sf,int other_w,int other_h,int this_w,int this_h,int use_highbd)68 void av1_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
69                                        int other_h, int this_w, int this_h,
70                                        int use_highbd) {
71 #else
72 void av1_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
73                                        int other_h, int this_w, int this_h) {
74 #endif
75   if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
76     sf->x_scale_fp = REF_INVALID_SCALE;
77     sf->y_scale_fp = REF_INVALID_SCALE;
78     return;
79   }
80 
81   sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
82   sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
83 
84   sf->x_step_q4 = get_coarse_point_scale_factor(other_w, this_w);
85   sf->y_step_q4 = get_coarse_point_scale_factor(other_h, this_h);
86 
87   if (av1_is_scaled(sf)) {
88     sf->scale_value_x = scaled_x;
89     sf->scale_value_y = scaled_y;
90   } else {
91     sf->scale_value_x = unscaled_value;
92     sf->scale_value_y = unscaled_value;
93   }
94 
95   // TODO(agrange): Investigate the best choice of functions to use here
96   // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
97   // to do at full-pel offsets. The current selection, where the filter is
98   // applied in one direction only, and not at all for 0,0, seems to give the
99   // best quality, but it may be worth trying an additional mode that does
100   // do the filtering on full-pel.
101   if (sf->x_step_q4 == SCALE_SUBPEL_SHIFTS) {
102     if (sf->y_step_q4 == SCALE_SUBPEL_SHIFTS) {
103       // No scaling in either direction.
104       sf->predict[0][0][0] = aom_convolve_copy;
105       sf->predict[0][0][1] = aom_convolve_avg;
106       sf->predict[0][1][0] = aom_convolve8_vert;
107       sf->predict[0][1][1] = aom_convolve8_avg_vert;
108       sf->predict[1][0][0] = aom_convolve8_horiz;
109       sf->predict[1][0][1] = aom_convolve8_avg_horiz;
110     } else {
111       // No scaling in x direction. Must always scale in the y direction.
112       sf->predict[0][0][0] = aom_convolve8_vert;
113       sf->predict[0][0][1] = aom_convolve8_avg_vert;
114       sf->predict[0][1][0] = aom_convolve8_vert;
115       sf->predict[0][1][1] = aom_convolve8_avg_vert;
116       sf->predict[1][0][0] = aom_convolve8;
117       sf->predict[1][0][1] = aom_convolve8_avg;
118     }
119   } else {
120     if (sf->y_step_q4 == SCALE_SUBPEL_SHIFTS) {
121       // No scaling in the y direction. Must always scale in the x direction.
122       sf->predict[0][0][0] = aom_convolve8_horiz;
123       sf->predict[0][0][1] = aom_convolve8_avg_horiz;
124       sf->predict[0][1][0] = aom_convolve8;
125       sf->predict[0][1][1] = aom_convolve8_avg;
126       sf->predict[1][0][0] = aom_convolve8_horiz;
127       sf->predict[1][0][1] = aom_convolve8_avg_horiz;
128     } else {
129       // Must always scale in both directions.
130       sf->predict[0][0][0] = aom_convolve8;
131       sf->predict[0][0][1] = aom_convolve8_avg;
132       sf->predict[0][1][0] = aom_convolve8;
133       sf->predict[0][1][1] = aom_convolve8_avg;
134       sf->predict[1][0][0] = aom_convolve8;
135       sf->predict[1][0][1] = aom_convolve8_avg;
136     }
137   }
138   // 2D subpel motion always gets filtered in both directions
139   sf->predict[1][1][0] = aom_convolve8;
140   sf->predict[1][1][1] = aom_convolve8_avg;
141 
142 #if CONFIG_HIGHBITDEPTH
143   if (use_highbd) {
144     if (sf->x_step_q4 == SCALE_SUBPEL_SHIFTS) {
145       if (sf->y_step_q4 == SCALE_SUBPEL_SHIFTS) {
146         // No scaling in either direction.
147         sf->highbd_predict[0][0][0] = aom_highbd_convolve_copy;
148         sf->highbd_predict[0][0][1] = aom_highbd_convolve_avg;
149         sf->highbd_predict[0][1][0] = aom_highbd_convolve8_vert;
150         sf->highbd_predict[0][1][1] = aom_highbd_convolve8_avg_vert;
151         sf->highbd_predict[1][0][0] = aom_highbd_convolve8_horiz;
152         sf->highbd_predict[1][0][1] = aom_highbd_convolve8_avg_horiz;
153       } else {
154         // No scaling in x direction. Must always scale in the y direction.
155         sf->highbd_predict[0][0][0] = aom_highbd_convolve8_vert;
156         sf->highbd_predict[0][0][1] = aom_highbd_convolve8_avg_vert;
157         sf->highbd_predict[0][1][0] = aom_highbd_convolve8_vert;
158         sf->highbd_predict[0][1][1] = aom_highbd_convolve8_avg_vert;
159         sf->highbd_predict[1][0][0] = aom_highbd_convolve8;
160         sf->highbd_predict[1][0][1] = aom_highbd_convolve8_avg;
161       }
162     } else {
163       if (sf->y_step_q4 == SCALE_SUBPEL_SHIFTS) {
164         // No scaling in the y direction. Must always scale in the x direction.
165         sf->highbd_predict[0][0][0] = aom_highbd_convolve8_horiz;
166         sf->highbd_predict[0][0][1] = aom_highbd_convolve8_avg_horiz;
167         sf->highbd_predict[0][1][0] = aom_highbd_convolve8;
168         sf->highbd_predict[0][1][1] = aom_highbd_convolve8_avg;
169         sf->highbd_predict[1][0][0] = aom_highbd_convolve8_horiz;
170         sf->highbd_predict[1][0][1] = aom_highbd_convolve8_avg_horiz;
171       } else {
172         // Must always scale in both directions.
173         sf->highbd_predict[0][0][0] = aom_highbd_convolve8;
174         sf->highbd_predict[0][0][1] = aom_highbd_convolve8_avg;
175         sf->highbd_predict[0][1][0] = aom_highbd_convolve8;
176         sf->highbd_predict[0][1][1] = aom_highbd_convolve8_avg;
177         sf->highbd_predict[1][0][0] = aom_highbd_convolve8;
178         sf->highbd_predict[1][0][1] = aom_highbd_convolve8_avg;
179       }
180     }
181     // 2D subpel motion always gets filtered in both directions.
182     sf->highbd_predict[1][1][0] = aom_highbd_convolve8;
183     sf->highbd_predict[1][1][1] = aom_highbd_convolve8_avg;
184   }
185 #endif  // CONFIG_HIGHBITDEPTH
186 }
187