xref: /aosp_15_r20/external/libaom/av1/common/scale.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_COMMON_SCALE_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_COMMON_SCALE_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/convolve.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/mv.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
19*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
20*77c1e3ccSAndroid Build Coastguard Worker #endif
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #define SCALE_NUMERATOR 8
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker #define REF_SCALE_SHIFT 14
25*77c1e3ccSAndroid Build Coastguard Worker #define REF_NO_SCALE (1 << REF_SCALE_SHIFT)
26*77c1e3ccSAndroid Build Coastguard Worker #define REF_INVALID_SCALE -1
27*77c1e3ccSAndroid Build Coastguard Worker 
28*77c1e3ccSAndroid Build Coastguard Worker struct scale_factors {
29*77c1e3ccSAndroid Build Coastguard Worker   int x_scale_fp;  // horizontal fixed point scale factor
30*77c1e3ccSAndroid Build Coastguard Worker   int y_scale_fp;  // vertical fixed point scale factor
31*77c1e3ccSAndroid Build Coastguard Worker   int x_step_q4;
32*77c1e3ccSAndroid Build Coastguard Worker   int y_step_q4;
33*77c1e3ccSAndroid Build Coastguard Worker };
34*77c1e3ccSAndroid Build Coastguard Worker 
35*77c1e3ccSAndroid Build Coastguard Worker // Note: Expect val to be in q4 precision
av1_scaled_x(int val,const struct scale_factors * sf)36*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_scaled_x(int val, const struct scale_factors *sf) {
37*77c1e3ccSAndroid Build Coastguard Worker   const int off =
38*77c1e3ccSAndroid Build Coastguard Worker       (sf->x_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
39*77c1e3ccSAndroid Build Coastguard Worker   const int64_t tval = (int64_t)val * sf->x_scale_fp + off;
40*77c1e3ccSAndroid Build Coastguard Worker   return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
41*77c1e3ccSAndroid Build Coastguard Worker                                            REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
42*77c1e3ccSAndroid Build Coastguard Worker }
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker // Note: Expect val to be in q4 precision
av1_scaled_y(int val,const struct scale_factors * sf)45*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_scaled_y(int val, const struct scale_factors *sf) {
46*77c1e3ccSAndroid Build Coastguard Worker   const int off =
47*77c1e3ccSAndroid Build Coastguard Worker       (sf->y_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
48*77c1e3ccSAndroid Build Coastguard Worker   const int64_t tval = (int64_t)val * sf->y_scale_fp + off;
49*77c1e3ccSAndroid Build Coastguard Worker   return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
50*77c1e3ccSAndroid Build Coastguard Worker                                            REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
51*77c1e3ccSAndroid Build Coastguard Worker }
52*77c1e3ccSAndroid Build Coastguard Worker 
53*77c1e3ccSAndroid Build Coastguard Worker // Note: Expect val to be in q4 precision
av1_unscaled_value(int val,const struct scale_factors * sf)54*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_unscaled_value(int val, const struct scale_factors *sf) {
55*77c1e3ccSAndroid Build Coastguard Worker   (void)sf;
56*77c1e3ccSAndroid Build Coastguard Worker   return val * (1 << SCALE_EXTRA_BITS);
57*77c1e3ccSAndroid Build Coastguard Worker }
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker MV32 av1_scale_mv(const MV *mv, int x, int y, const struct scale_factors *sf);
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker void av1_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
62*77c1e3ccSAndroid Build Coastguard Worker                                        int other_h, int this_w, int this_h);
63*77c1e3ccSAndroid Build Coastguard Worker 
av1_is_valid_scale(const struct scale_factors * sf)64*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_is_valid_scale(const struct scale_factors *sf) {
65*77c1e3ccSAndroid Build Coastguard Worker   assert(sf != NULL);
66*77c1e3ccSAndroid Build Coastguard Worker   return sf->x_scale_fp != REF_INVALID_SCALE &&
67*77c1e3ccSAndroid Build Coastguard Worker          sf->y_scale_fp != REF_INVALID_SCALE;
68*77c1e3ccSAndroid Build Coastguard Worker }
69*77c1e3ccSAndroid Build Coastguard Worker 
av1_is_scaled(const struct scale_factors * sf)70*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_is_scaled(const struct scale_factors *sf) {
71*77c1e3ccSAndroid Build Coastguard Worker   assert(sf != NULL);
72*77c1e3ccSAndroid Build Coastguard Worker   return av1_is_valid_scale(sf) &&
73*77c1e3ccSAndroid Build Coastguard Worker          (sf->x_scale_fp != REF_NO_SCALE || sf->y_scale_fp != REF_NO_SCALE);
74*77c1e3ccSAndroid Build Coastguard Worker }
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker // See AV1 spec, Section 6.8.6. Frame size with refs semantics.
valid_ref_frame_size(int ref_width,int ref_height,int this_width,int this_height)77*77c1e3ccSAndroid Build Coastguard Worker static inline int valid_ref_frame_size(int ref_width, int ref_height,
78*77c1e3ccSAndroid Build Coastguard Worker                                        int this_width, int this_height) {
79*77c1e3ccSAndroid Build Coastguard Worker   return 2 * this_width >= ref_width && 2 * this_height >= ref_height &&
80*77c1e3ccSAndroid Build Coastguard Worker          this_width <= 16 * ref_width && this_height <= 16 * ref_height;
81*77c1e3ccSAndroid Build Coastguard Worker }
82*77c1e3ccSAndroid Build Coastguard Worker 
83*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
84*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
85*77c1e3ccSAndroid Build Coastguard Worker #endif
86*77c1e3ccSAndroid Build Coastguard Worker 
87*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_COMMON_SCALE_H_
88