xref: /aosp_15_r20/external/libaom/av1/common/scale.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 #ifndef AOM_AV1_COMMON_SCALE_H_
13 #define AOM_AV1_COMMON_SCALE_H_
14 
15 #include "av1/common/convolve.h"
16 #include "av1/common/mv.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #define SCALE_NUMERATOR 8
23 
24 #define REF_SCALE_SHIFT 14
25 #define REF_NO_SCALE (1 << REF_SCALE_SHIFT)
26 #define REF_INVALID_SCALE -1
27 
28 struct scale_factors {
29   int x_scale_fp;  // horizontal fixed point scale factor
30   int y_scale_fp;  // vertical fixed point scale factor
31   int x_step_q4;
32   int y_step_q4;
33 };
34 
35 // Note: Expect val to be in q4 precision
av1_scaled_x(int val,const struct scale_factors * sf)36 static inline int av1_scaled_x(int val, const struct scale_factors *sf) {
37   const int off =
38       (sf->x_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
39   const int64_t tval = (int64_t)val * sf->x_scale_fp + off;
40   return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
41                                            REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
42 }
43 
44 // Note: Expect val to be in q4 precision
av1_scaled_y(int val,const struct scale_factors * sf)45 static inline int av1_scaled_y(int val, const struct scale_factors *sf) {
46   const int off =
47       (sf->y_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
48   const int64_t tval = (int64_t)val * sf->y_scale_fp + off;
49   return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
50                                            REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
51 }
52 
53 // Note: Expect val to be in q4 precision
av1_unscaled_value(int val,const struct scale_factors * sf)54 static inline int av1_unscaled_value(int val, const struct scale_factors *sf) {
55   (void)sf;
56   return val * (1 << SCALE_EXTRA_BITS);
57 }
58 
59 MV32 av1_scale_mv(const MV *mv, int x, int y, const struct scale_factors *sf);
60 
61 void av1_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
62                                        int other_h, int this_w, int this_h);
63 
av1_is_valid_scale(const struct scale_factors * sf)64 static inline int av1_is_valid_scale(const struct scale_factors *sf) {
65   assert(sf != NULL);
66   return sf->x_scale_fp != REF_INVALID_SCALE &&
67          sf->y_scale_fp != REF_INVALID_SCALE;
68 }
69 
av1_is_scaled(const struct scale_factors * sf)70 static inline int av1_is_scaled(const struct scale_factors *sf) {
71   assert(sf != NULL);
72   return av1_is_valid_scale(sf) &&
73          (sf->x_scale_fp != REF_NO_SCALE || sf->y_scale_fp != REF_NO_SCALE);
74 }
75 
76 // 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 static inline int valid_ref_frame_size(int ref_width, int ref_height,
78                                        int this_width, int this_height) {
79   return 2 * this_width >= ref_width && 2 * this_height >= ref_height &&
80          this_width <= 16 * ref_width && this_height <= 16 * ref_height;
81 }
82 
83 #ifdef __cplusplus
84 }  // extern "C"
85 #endif
86 
87 #endif  // AOM_AV1_COMMON_SCALE_H_
88