Coverage Report

Created: 2025-07-23 08:18

/src/aom/av1/common/scale.c
Line
Count
Source
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
#include "config/aom_dsp_rtcd.h"
13
#include "config/av1_rtcd.h"
14
15
#include "av1/common/filter.h"
16
#include "av1/common/scale.h"
17
#include "aom_dsp/aom_filter.h"
18
19
// Note: Expect val to be in q4 precision
20
22.0k
static INLINE int scaled_x(int val, const struct scale_factors *sf) {
21
22.0k
  const int off =
22
22.0k
      (sf->x_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
23
22.0k
  const int64_t tval = (int64_t)val * sf->x_scale_fp + off;
24
22.0k
  return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
25
22.0k
                                           REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
26
22.0k
}
27
28
// Note: Expect val to be in q4 precision
29
22.0k
static INLINE int scaled_y(int val, const struct scale_factors *sf) {
30
22.0k
  const int off =
31
22.0k
      (sf->y_scale_fp - (1 << REF_SCALE_SHIFT)) * (1 << (SUBPEL_BITS - 1));
32
22.0k
  const int64_t tval = (int64_t)val * sf->y_scale_fp + off;
33
22.0k
  return (int)ROUND_POWER_OF_TWO_SIGNED_64(tval,
34
22.0k
                                           REF_SCALE_SHIFT - SCALE_EXTRA_BITS);
35
22.0k
}
36
37
// Note: Expect val to be in q4 precision
38
14.5k
static int unscaled_value(int val, const struct scale_factors *sf) {
39
14.5k
  (void)sf;
40
14.5k
  return val * (1 << SCALE_EXTRA_BITS);
41
14.5k
}
42
43
63.0k
static int get_fixed_point_scale_factor(int other_size, int this_size) {
44
  // Calculate scaling factor once for each reference frame
45
  // and use fixed point scaling factors in decoding and encoding routines.
46
  // Hardware implementations can calculate scale factor in device driver
47
  // and use multiplication and shifting on hardware instead of division.
48
63.0k
  return ((other_size << REF_SCALE_SHIFT) + this_size / 2) / this_size;
49
63.0k
}
50
51
// Given the fixed point scale, calculate coarse point scale.
52
63.0k
static int fixed_point_scale_to_coarse_point_scale(int scale_fp) {
53
63.0k
  return ROUND_POWER_OF_TWO(scale_fp, REF_SCALE_SHIFT - SCALE_SUBPEL_BITS);
54
63.0k
}
55
56
// Note: x and y are integer precision, mvq4 is q4 precision.
57
MV32 av1_scale_mv(const MV *mvq4, int x, int y,
58
5.50k
                  const struct scale_factors *sf) {
59
5.50k
  const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf);
60
5.50k
  const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf);
61
5.50k
  const MV32 res = { scaled_y((y << SUBPEL_BITS) + mvq4->row, sf) - y_off_q4,
62
5.50k
                     scaled_x((x << SUBPEL_BITS) + mvq4->col, sf) - x_off_q4 };
63
5.50k
  return res;
64
5.50k
}
65
66
void av1_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
67
31.5k
                                       int other_h, int this_w, int this_h) {
68
31.5k
  if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
69
2
    sf->x_scale_fp = REF_INVALID_SCALE;
70
2
    sf->y_scale_fp = REF_INVALID_SCALE;
71
2
    return;
72
2
  }
73
74
31.5k
  sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
75
31.5k
  sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
76
77
31.5k
  sf->x_step_q4 = fixed_point_scale_to_coarse_point_scale(sf->x_scale_fp);
78
31.5k
  sf->y_step_q4 = fixed_point_scale_to_coarse_point_scale(sf->y_scale_fp);
79
80
31.5k
  if (av1_is_scaled(sf)) {
81
9.91k
    sf->scale_value_x = scaled_x;
82
9.91k
    sf->scale_value_y = scaled_y;
83
21.6k
  } else {
84
21.6k
    sf->scale_value_x = unscaled_value;
85
21.6k
    sf->scale_value_y = unscaled_value;
86
21.6k
  }
87
31.5k
}