Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/common/vp9_scale.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "./vpx_dsp_rtcd.h"
12
#include "vp9/common/vp9_filter.h"
13
#include "vp9/common/vp9_scale.h"
14
#include "vpx_dsp/vpx_filter.h"
15
16
182M
static INLINE int scaled_x(int val, const struct scale_factors *sf) {
17
182M
  return (int)((int64_t)val * sf->x_scale_fp >> REF_SCALE_SHIFT);
18
182M
}
19
20
182M
static INLINE int scaled_y(int val, const struct scale_factors *sf) {
21
182M
  return (int)((int64_t)val * sf->y_scale_fp >> REF_SCALE_SHIFT);
22
182M
}
23
24
62.9M
static int unscaled_value(int val, const struct scale_factors *sf) {
25
62.9M
  (void)sf;
26
62.9M
  return val;
27
62.9M
}
28
29
654k
static int get_fixed_point_scale_factor(int other_size, int this_size) {
30
  // Calculate scaling factor once for each reference frame
31
  // and use fixed point scaling factors in decoding and encoding routines.
32
  // Hardware implementations can calculate scale factor in device driver
33
  // and use multiplication and shifting on hardware instead of division.
34
654k
  return (other_size << REF_SCALE_SHIFT) / this_size;
35
654k
}
36
37
90.2M
MV32 vp9_scale_mv(const MV *mv, int x, int y, const struct scale_factors *sf) {
38
90.2M
  const int x_off_q4 = scaled_x(x << SUBPEL_BITS, sf) & SUBPEL_MASK;
39
90.2M
  const int y_off_q4 = scaled_y(y << SUBPEL_BITS, sf) & SUBPEL_MASK;
40
90.2M
  const MV32 res = { scaled_y(mv->row, sf) + y_off_q4,
41
90.2M
                     scaled_x(mv->col, sf) + x_off_q4 };
42
90.2M
  return res;
43
90.2M
}
44
45
#if CONFIG_VP9_HIGHBITDEPTH
46
void vp9_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
47
                                       int other_h, int this_w, int this_h,
48
328k
                                       int use_highbd) {
49
#else
50
void vp9_setup_scale_factors_for_frame(struct scale_factors *sf, int other_w,
51
                                       int other_h, int this_w, int this_h) {
52
#endif
53
328k
  if (!valid_ref_frame_size(other_w, other_h, this_w, this_h)) {
54
1.36k
    sf->x_scale_fp = REF_INVALID_SCALE;
55
1.36k
    sf->y_scale_fp = REF_INVALID_SCALE;
56
1.36k
    return;
57
1.36k
  }
58
59
327k
  sf->x_scale_fp = get_fixed_point_scale_factor(other_w, this_w);
60
327k
  sf->y_scale_fp = get_fixed_point_scale_factor(other_h, this_h);
61
327k
  sf->x_step_q4 = scaled_x(16, sf);
62
327k
  sf->y_step_q4 = scaled_y(16, sf);
63
64
327k
  if (vp9_is_scaled(sf)) {
65
10.7k
    sf->scale_value_x = scaled_x;
66
10.7k
    sf->scale_value_y = scaled_y;
67
316k
  } else {
68
316k
    sf->scale_value_x = unscaled_value;
69
316k
    sf->scale_value_y = unscaled_value;
70
316k
  }
71
72
  // TODO(agrange): Investigate the best choice of functions to use here
73
  // for EIGHTTAP_SMOOTH. Since it is not interpolating, need to choose what
74
  // to do at full-pel offsets. The current selection, where the filter is
75
  // applied in one direction only, and not at all for 0,0, seems to give the
76
  // best quality, but it may be worth trying an additional mode that does
77
  // do the filtering on full-pel.
78
79
327k
  if (sf->x_step_q4 == 16) {
80
319k
    if (sf->y_step_q4 == 16) {
81
      // No scaling in either direction.
82
316k
      sf->predict[0][0][0] = vpx_convolve_copy;
83
316k
      sf->predict[0][0][1] = vpx_convolve_avg;
84
316k
      sf->predict[0][1][0] = vpx_convolve8_vert;
85
316k
      sf->predict[0][1][1] = vpx_convolve8_avg_vert;
86
316k
      sf->predict[1][0][0] = vpx_convolve8_horiz;
87
316k
      sf->predict[1][0][1] = vpx_convolve8_avg_horiz;
88
316k
    } else {
89
      // No scaling in x direction. Must always scale in the y direction.
90
2.73k
      sf->predict[0][0][0] = vpx_scaled_vert;
91
2.73k
      sf->predict[0][0][1] = vpx_scaled_avg_vert;
92
2.73k
      sf->predict[0][1][0] = vpx_scaled_vert;
93
2.73k
      sf->predict[0][1][1] = vpx_scaled_avg_vert;
94
2.73k
      sf->predict[1][0][0] = vpx_scaled_2d;
95
2.73k
      sf->predict[1][0][1] = vpx_scaled_avg_2d;
96
2.73k
    }
97
319k
  } else {
98
8.01k
    if (sf->y_step_q4 == 16) {
99
      // No scaling in the y direction. Must always scale in the x direction.
100
2.14k
      sf->predict[0][0][0] = vpx_scaled_horiz;
101
2.14k
      sf->predict[0][0][1] = vpx_scaled_avg_horiz;
102
2.14k
      sf->predict[0][1][0] = vpx_scaled_2d;
103
2.14k
      sf->predict[0][1][1] = vpx_scaled_avg_2d;
104
2.14k
      sf->predict[1][0][0] = vpx_scaled_horiz;
105
2.14k
      sf->predict[1][0][1] = vpx_scaled_avg_horiz;
106
5.87k
    } else {
107
      // Must always scale in both directions.
108
5.87k
      sf->predict[0][0][0] = vpx_scaled_2d;
109
5.87k
      sf->predict[0][0][1] = vpx_scaled_avg_2d;
110
5.87k
      sf->predict[0][1][0] = vpx_scaled_2d;
111
5.87k
      sf->predict[0][1][1] = vpx_scaled_avg_2d;
112
5.87k
      sf->predict[1][0][0] = vpx_scaled_2d;
113
5.87k
      sf->predict[1][0][1] = vpx_scaled_avg_2d;
114
5.87k
    }
115
8.01k
  }
116
117
  // 2D subpel motion always gets filtered in both directions
118
119
327k
  if ((sf->x_step_q4 != 16) || (sf->y_step_q4 != 16)) {
120
10.7k
    sf->predict[1][1][0] = vpx_scaled_2d;
121
10.7k
    sf->predict[1][1][1] = vpx_scaled_avg_2d;
122
316k
  } else {
123
316k
    sf->predict[1][1][0] = vpx_convolve8;
124
316k
    sf->predict[1][1][1] = vpx_convolve8_avg;
125
316k
  }
126
127
327k
#if CONFIG_VP9_HIGHBITDEPTH
128
327k
  if (use_highbd) {
129
86.0k
    if (sf->x_step_q4 == 16) {
130
83.6k
      if (sf->y_step_q4 == 16) {
131
        // No scaling in either direction.
132
81.4k
        sf->highbd_predict[0][0][0] = vpx_highbd_convolve_copy;
133
81.4k
        sf->highbd_predict[0][0][1] = vpx_highbd_convolve_avg;
134
81.4k
        sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
135
81.4k
        sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
136
81.4k
        sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
137
81.4k
        sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
138
81.4k
      } else {
139
        // No scaling in x direction. Must always scale in the y direction.
140
2.20k
        sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_vert;
141
2.20k
        sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_vert;
142
2.20k
        sf->highbd_predict[0][1][0] = vpx_highbd_convolve8_vert;
143
2.20k
        sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg_vert;
144
2.20k
        sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
145
2.20k
        sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
146
2.20k
      }
147
83.6k
    } else {
148
2.47k
      if (sf->y_step_q4 == 16) {
149
        // No scaling in the y direction. Must always scale in the x direction.
150
678
        sf->highbd_predict[0][0][0] = vpx_highbd_convolve8_horiz;
151
678
        sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg_horiz;
152
678
        sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
153
678
        sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
154
678
        sf->highbd_predict[1][0][0] = vpx_highbd_convolve8_horiz;
155
678
        sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg_horiz;
156
1.79k
      } else {
157
        // Must always scale in both directions.
158
1.79k
        sf->highbd_predict[0][0][0] = vpx_highbd_convolve8;
159
1.79k
        sf->highbd_predict[0][0][1] = vpx_highbd_convolve8_avg;
160
1.79k
        sf->highbd_predict[0][1][0] = vpx_highbd_convolve8;
161
1.79k
        sf->highbd_predict[0][1][1] = vpx_highbd_convolve8_avg;
162
1.79k
        sf->highbd_predict[1][0][0] = vpx_highbd_convolve8;
163
1.79k
        sf->highbd_predict[1][0][1] = vpx_highbd_convolve8_avg;
164
1.79k
      }
165
2.47k
    }
166
    // 2D subpel motion always gets filtered in both directions.
167
86.0k
    sf->highbd_predict[1][1][0] = vpx_highbd_convolve8;
168
86.0k
    sf->highbd_predict[1][1][1] = vpx_highbd_convolve8_avg;
169
86.0k
  }
170
327k
#endif
171
327k
}