/src/avm/av2/common/scale.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 3-Clause Clear License |
5 | | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
6 | | * License was not distributed with this source code in the LICENSE file, you |
7 | | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
8 | | * Alliance for Open Media Patent License 1.0 was not distributed with this |
9 | | * source code in the PATENTS file, you can obtain it at |
10 | | * aomedia.org/license/patent-license/. |
11 | | */ |
12 | | |
13 | | #include "config/avm_dsp_rtcd.h" |
14 | | #include "config/av2_rtcd.h" |
15 | | |
16 | | #include "av2/common/filter.h" |
17 | | #include "av2/common/scale.h" |
18 | | #include "avm_dsp/avm_filter.h" |
19 | | |
20 | | // Note: Expect val to be in q4 precision |
21 | 0 | static INLINE int scaled_x(int val, const struct scale_factors *sf) { |
22 | 0 | const int off = |
23 | 0 | (sf->x_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1)); |
24 | 0 | const int64_t tval = (int64_t)val * sf->x_scale_fp + off; |
25 | 0 | return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval, |
26 | 0 | REF_SCALE_SHIFT - SCALE_EXTRA_BITS); |
27 | 0 | } |
28 | | |
29 | | // Note: Expect val to be in q4 precision |
30 | 0 | static INLINE int scaled_y(int val, const struct scale_factors *sf) { |
31 | 0 | const int off = |
32 | 0 | (sf->y_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1)); |
33 | 0 | const int64_t tval = (int64_t)val * sf->y_scale_fp + off; |
34 | 0 | return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval, |
35 | 0 | REF_SCALE_SHIFT - SCALE_EXTRA_BITS); |
36 | 0 | } |
37 | | |
38 | | // Note: Expect val to be in q16 precision |
39 | | static INLINE int64_t scaled_warp_x(int64_t val, |
40 | 0 | const struct scale_factors *sf) { |
41 | 0 | const int64_t tval = (int64_t)val * sf->x_scale_fp; |
42 | 0 | return (int64_t)ROUND_POWER_OF_TWO_SIGNED_64(tval, REF_SCALE_SHIFT); |
43 | 0 | } |
44 | | |
45 | | // Note: Expect val to be in q16 precision |
46 | | static INLINE int64_t scaled_warp_y(int64_t val, |
47 | 0 | const struct scale_factors *sf) { |
48 | 0 | const int64_t tval = (int64_t)val * sf->y_scale_fp; |
49 | 0 | return (int64_t)ROUND_POWER_OF_TWO_SIGNED_64(tval, REF_SCALE_SHIFT); |
50 | 0 | } |
51 | | // Note: Expect val to be in q16 precision |
52 | | static INLINE int64_t un_scaled_warp(int64_t val, |
53 | 0 | const struct scale_factors *sf) { |
54 | 0 | (void)sf; |
55 | 0 | return val; |
56 | 0 | } |
57 | | |
58 | | // Note: Expect val to be in q4 precision |
59 | 0 | static int unscaled_value(int val, const struct scale_factors *sf) { |
60 | 0 | (void)sf; |
61 | 0 | return val * (1 << SCALE_EXTRA_BITS); |
62 | 0 | } |
63 | | |
64 | 8.50k | static int get_fixed_point_scale_factor(int other_size, int this_size) { |
65 | | // Calculate scaling factor once for each reference frame |
66 | | // and use fixed point scaling factors in decoding and encoding routines. |
67 | | // Hardware implementations can calculate scale factor in device driver |
68 | | // and use multiplication and shifting on hardware instead of division. |
69 | 8.50k | return ((other_size << REF_SCALE_SHIFT) + this_size / 2) / this_size; |
70 | 8.50k | } |
71 | | |
72 | | // Given the fixed point scale, calculate coarse point scale. |
73 | 8.50k | static int fixed_point_scale_to_coarse_point_scale(int scale_fp) { |
74 | 8.50k | return ROUND_POWER_OF_TWO(scale_fp, REF_SCALE_SHIFT - SCALE_SUBPEL_BITS); |
75 | 8.50k | } |
76 | | |
77 | | // Note: x and y are integer precision, mvq4 is q4 precision. |
78 | | MV32 av2_scale_mv(const MV *mvq4, int x, int y, |
79 | 0 | const struct scale_factors *sf) { |
80 | 0 | const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf); |
81 | 0 | const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf); |
82 | 0 | const MV32 res = { scaled_y((y << SUBPEL_BITS) + mvq4->row, sf) - y_off_q4, |
83 | 0 | scaled_x((x << SUBPEL_BITS) + mvq4->col, sf) - x_off_q4 }; |
84 | 0 | return res; |
85 | 0 | } |
86 | | |
87 | | void av2_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w, |
88 | 4.25k | int other_h, int this_w, int this_h) { |
89 | 4.25k | if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) { |
90 | 0 | sf->x_scale_fp = REF_INVALID_SCALE; |
91 | 0 | sf->y_scale_fp = REF_INVALID_SCALE; |
92 | 0 | return; |
93 | 0 | } |
94 | | |
95 | 4.25k | sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w); |
96 | 4.25k | sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h); |
97 | | |
98 | 4.25k | sf->x_step_q4 = fixed_point_scale_to_coarse_point_scale(sf->x_scale_fp); |
99 | 4.25k | sf->y_step_q4 = fixed_point_scale_to_coarse_point_scale(sf->y_scale_fp); |
100 | | |
101 | 4.25k | if (av2_is_scaled(sf)) { |
102 | 0 | sf->scale_value_x = scaled_x; |
103 | 0 | sf->scale_value_y = scaled_y; |
104 | 0 | sf->scale_value_warp_x = scaled_warp_x; |
105 | 0 | sf->scale_value_warp_y = scaled_warp_y; |
106 | 4.25k | } else { |
107 | 4.25k | sf->scale_value_x = unscaled_value; |
108 | 4.25k | sf->scale_value_y = unscaled_value; |
109 | 4.25k | sf->scale_value_warp_x = un_scaled_warp; |
110 | 4.25k | sf->scale_value_warp_y = un_scaled_warp; |
111 | 4.25k | } |
112 | 4.25k | } |