xref: /aosp_15_r20/external/libaom/av1/common/scale.c (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 #include "config/aom_dsp_rtcd.h"
13*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/filter.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/scale.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_filter.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
get_fixed_point_scale_factor(int other_size,int this_size)19*77c1e3ccSAndroid Build Coastguard Worker static int get_fixed_point_scale_factor(int other_size, int this_size) {
20*77c1e3ccSAndroid Build Coastguard Worker   // Calculate scaling factor once for each reference frame
21*77c1e3ccSAndroid Build Coastguard Worker   // and use fixed point scaling factors in decoding and encoding routines.
22*77c1e3ccSAndroid Build Coastguard Worker   // Hardware implementations can calculate scale factor in device driver
23*77c1e3ccSAndroid Build Coastguard Worker   // and use multiplication and shifting on hardware instead of division.
24*77c1e3ccSAndroid Build Coastguard Worker   return ((other_size << REF_SCALE_SHIFT) + this_size / 2) / this_size;
25*77c1e3ccSAndroid Build Coastguard Worker }
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker // Given the fixed point scale, calculate coarse point scale.
fixed_point_scale_to_coarse_point_scale(int scale_fp)28*77c1e3ccSAndroid Build Coastguard Worker static int fixed_point_scale_to_coarse_point_scale(int scale_fp) {
29*77c1e3ccSAndroid Build Coastguard Worker   return ROUND_POWER_OF_TWO(scale_fp, REF_SCALE_SHIFT - SCALE_SUBPEL_BITS);
30*77c1e3ccSAndroid Build Coastguard Worker }
31*77c1e3ccSAndroid Build Coastguard Worker 
32*77c1e3ccSAndroid Build Coastguard Worker // 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)33*77c1e3ccSAndroid Build Coastguard Worker MV32 av1_scale_mv(const MV *mvq4, int x, int y,
34*77c1e3ccSAndroid Build Coastguard Worker                   const struct scale_factors *sf) {
35*77c1e3ccSAndroid Build Coastguard Worker   const int x_off_q4 = av1_scaled_x(x << SUBPEL_BITS, sf);
36*77c1e3ccSAndroid Build Coastguard Worker   const int y_off_q4 = av1_scaled_y(y << SUBPEL_BITS, sf);
37*77c1e3ccSAndroid Build Coastguard Worker   const MV32 res = {
38*77c1e3ccSAndroid Build Coastguard Worker     av1_scaled_y((y << SUBPEL_BITS) + mvq4->row, sf) - y_off_q4,
39*77c1e3ccSAndroid Build Coastguard Worker     av1_scaled_x((x << SUBPEL_BITS) + mvq4->col, sf) - x_off_q4
40*77c1e3ccSAndroid Build Coastguard Worker   };
41*77c1e3ccSAndroid Build Coastguard Worker   return res;
42*77c1e3ccSAndroid Build Coastguard Worker }
43*77c1e3ccSAndroid Build Coastguard Worker 
av1_setup_scale_factors_for_frame(struct scale_factors * sf,int other_w,int other_h,int this_w,int this_h)44*77c1e3ccSAndroid Build Coastguard Worker void av1_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
45*77c1e3ccSAndroid Build Coastguard Worker                                        int other_h, int this_w, int this_h) {
46*77c1e3ccSAndroid Build Coastguard Worker   if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
47*77c1e3ccSAndroid Build Coastguard Worker     sf->x_scale_fp = REF_INVALID_SCALE;
48*77c1e3ccSAndroid Build Coastguard Worker     sf->y_scale_fp = REF_INVALID_SCALE;
49*77c1e3ccSAndroid Build Coastguard Worker     return;
50*77c1e3ccSAndroid Build Coastguard Worker   }
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker   sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
53*77c1e3ccSAndroid Build Coastguard Worker   sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker   sf->x_step_q4 = fixed_point_scale_to_coarse_point_scale(sf->x_scale_fp);
56*77c1e3ccSAndroid Build Coastguard Worker   sf->y_step_q4 = fixed_point_scale_to_coarse_point_scale(sf->y_scale_fp);
57*77c1e3ccSAndroid Build Coastguard Worker }
58