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