Coverage Report

Created: 2025-06-13 07:07

/src/aom/aom_dsp/aom_convolve.c
Line
Count
Source (jump to first uncovered line)
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 <assert.h>
13
#include <string.h>
14
15
#include "config/aom_config.h"
16
#include "config/aom_dsp_rtcd.h"
17
18
#include "aom/aom_integer.h"
19
#include "aom_dsp/aom_dsp_common.h"
20
#include "aom_dsp/aom_filter.h"
21
#include "aom_ports/mem.h"
22
23
0
static inline int horz_scalar_product(const uint8_t *a, const int16_t *b) {
24
0
  int sum = 0;
25
0
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
26
0
  return sum;
27
0
}
28
29
static inline int vert_scalar_product(const uint8_t *a, ptrdiff_t a_stride,
30
0
                                      const int16_t *b) {
31
0
  int sum = 0;
32
0
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
33
0
  return sum;
34
0
}
35
36
static void convolve_horiz(const uint8_t *src, ptrdiff_t src_stride,
37
                           uint8_t *dst, ptrdiff_t dst_stride,
38
                           const InterpKernel *x_filters, int x0_q4,
39
0
                           int x_step_q4, int w, int h) {
40
0
  src -= SUBPEL_TAPS / 2 - 1;
41
0
  for (int y = 0; y < h; ++y) {
42
0
    int x_q4 = x0_q4;
43
0
    for (int x = 0; x < w; ++x) {
44
0
      const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
45
0
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
46
0
      const int sum = horz_scalar_product(src_x, x_filter);
47
0
      dst[x] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
48
0
      x_q4 += x_step_q4;
49
0
    }
50
0
    src += src_stride;
51
0
    dst += dst_stride;
52
0
  }
53
0
}
54
55
static void convolve_vert(const uint8_t *src, ptrdiff_t src_stride,
56
                          uint8_t *dst, ptrdiff_t dst_stride,
57
                          const InterpKernel *y_filters, int y0_q4,
58
0
                          int y_step_q4, int w, int h) {
59
0
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
60
61
0
  for (int x = 0; x < w; ++x) {
62
0
    int y_q4 = y0_q4;
63
0
    for (int y = 0; y < h; ++y) {
64
0
      const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
65
0
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
66
0
      const int sum = vert_scalar_product(src_y, src_stride, y_filter);
67
0
      dst[y * dst_stride] = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
68
0
      y_q4 += y_step_q4;
69
0
    }
70
0
    ++src;
71
0
    ++dst;
72
0
  }
73
0
}
74
75
0
static const InterpKernel *get_filter_base(const int16_t *filter) {
76
  // NOTE: This assumes that the filter table is 256-byte aligned.
77
0
  return (const InterpKernel *)(((intptr_t)filter) & ~((intptr_t)0xFF));
78
0
}
79
80
0
static int get_filter_offset(const int16_t *f, const InterpKernel *base) {
81
0
  return (int)((const InterpKernel *)(intptr_t)f - base);
82
0
}
83
84
void aom_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
85
                           uint8_t *dst, ptrdiff_t dst_stride,
86
                           const int16_t *filter_x, int x_step_q4,
87
                           const int16_t *filter_y, int y_step_q4, int w,
88
0
                           int h) {
89
0
  const InterpKernel *const filters_x = get_filter_base(filter_x);
90
0
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
91
92
0
  (void)filter_y;
93
0
  (void)y_step_q4;
94
95
0
  convolve_horiz(src, src_stride, dst, dst_stride, filters_x, x0_q4, x_step_q4,
96
0
                 w, h);
97
0
}
98
99
void aom_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
100
                          uint8_t *dst, ptrdiff_t dst_stride,
101
                          const int16_t *filter_x, int x_step_q4,
102
                          const int16_t *filter_y, int y_step_q4, int w,
103
0
                          int h) {
104
0
  const InterpKernel *const filters_y = get_filter_base(filter_y);
105
0
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
106
107
0
  (void)filter_x;
108
0
  (void)x_step_q4;
109
110
0
  convolve_vert(src, src_stride, dst, dst_stride, filters_y, y0_q4, y_step_q4,
111
0
                w, h);
112
0
}
113
114
void aom_scaled_2d_c(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
115
                     ptrdiff_t dst_stride, const InterpKernel *filter,
116
                     int x0_q4, int x_step_q4, int y0_q4, int y_step_q4, int w,
117
0
                     int h) {
118
  // Note: Fixed size intermediate buffer, temp, places limits on parameters.
119
  // 2d filtering proceeds in 2 steps:
120
  //   (1) Interpolate horizontally into an intermediate buffer, temp.
121
  //   (2) Interpolate temp vertically to derive the sub-pixel result.
122
  // Deriving the maximum number of rows in the temp buffer (135):
123
  // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
124
  // --Largest block size is 64x64 pixels.
125
  // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
126
  //   original frame (in 1/16th pixel units).
127
  // --Must round-up because block may be located at sub-pixel position.
128
  // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
129
  // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
130
  // When calling in frame scaling function, the smallest scaling factor is x1/4
131
  // ==> y_step_q4 = 64. Since w and h are at most 16, the temp buffer is still
132
  // big enough.
133
0
  uint8_t temp[64 * 135];
134
0
  const int intermediate_height =
135
0
      (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
136
137
0
  assert(w <= 64);
138
0
  assert(h <= 64);
139
0
  assert(y_step_q4 <= 32 || (y_step_q4 <= 64 && h <= 32));
140
0
  assert(x_step_q4 <= 64);
141
142
0
  convolve_horiz(src - src_stride * (SUBPEL_TAPS / 2 - 1), src_stride, temp, 64,
143
0
                 filter, x0_q4, x_step_q4, w, intermediate_height);
144
0
  convolve_vert(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst, dst_stride, filter,
145
0
                y0_q4, y_step_q4, w, h);
146
0
}
147
148
void aom_convolve_copy_c(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
149
0
                         ptrdiff_t dst_stride, int w, int h) {
150
0
  for (int r = h; r > 0; --r) {
151
0
    memmove(dst, src, w);
152
0
    src += src_stride;
153
0
    dst += dst_stride;
154
0
  }
155
0
}
156
157
#if CONFIG_AV1_HIGHBITDEPTH
158
static inline int highbd_vert_scalar_product(const uint16_t *a,
159
                                             ptrdiff_t a_stride,
160
0
                                             const int16_t *b) {
161
0
  int sum = 0;
162
0
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
163
0
  return sum;
164
0
}
165
166
static inline int highbd_horz_scalar_product(const uint16_t *a,
167
0
                                             const int16_t *b) {
168
0
  int sum = 0;
169
0
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
170
0
  return sum;
171
0
}
172
173
static void highbd_convolve_horiz(const uint8_t *src8, ptrdiff_t src_stride,
174
                                  uint8_t *dst8, ptrdiff_t dst_stride,
175
                                  const InterpKernel *x_filters, int x0_q4,
176
0
                                  int x_step_q4, int w, int h, int bd) {
177
0
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
178
0
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
179
0
  src -= SUBPEL_TAPS / 2 - 1;
180
0
  for (int y = 0; y < h; ++y) {
181
0
    int x_q4 = x0_q4;
182
0
    for (int x = 0; x < w; ++x) {
183
0
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
184
0
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
185
0
      const int sum = highbd_horz_scalar_product(src_x, x_filter);
186
0
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
187
0
      x_q4 += x_step_q4;
188
0
    }
189
0
    src += src_stride;
190
0
    dst += dst_stride;
191
0
  }
192
0
}
193
194
static void highbd_convolve_vert(const uint8_t *src8, ptrdiff_t src_stride,
195
                                 uint8_t *dst8, ptrdiff_t dst_stride,
196
                                 const InterpKernel *y_filters, int y0_q4,
197
0
                                 int y_step_q4, int w, int h, int bd) {
198
0
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
199
0
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
200
0
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
201
0
  for (int x = 0; x < w; ++x) {
202
0
    int y_q4 = y0_q4;
203
0
    for (int y = 0; y < h; ++y) {
204
0
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
205
0
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
206
0
      const int sum = highbd_vert_scalar_product(src_y, src_stride, y_filter);
207
0
      dst[y * dst_stride] =
208
0
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
209
0
      y_q4 += y_step_q4;
210
0
    }
211
0
    ++src;
212
0
    ++dst;
213
0
  }
214
0
}
215
216
void aom_highbd_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
217
                                  uint8_t *dst, ptrdiff_t dst_stride,
218
                                  const int16_t *filter_x, int x_step_q4,
219
                                  const int16_t *filter_y, int y_step_q4, int w,
220
0
                                  int h, int bd) {
221
0
  const InterpKernel *const filters_x = get_filter_base(filter_x);
222
0
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
223
0
  (void)filter_y;
224
0
  (void)y_step_q4;
225
226
0
  highbd_convolve_horiz(src, src_stride, dst, dst_stride, filters_x, x0_q4,
227
0
                        x_step_q4, w, h, bd);
228
0
}
229
230
void aom_highbd_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
231
                                 uint8_t *dst, ptrdiff_t dst_stride,
232
                                 const int16_t *filter_x, int x_step_q4,
233
                                 const int16_t *filter_y, int y_step_q4, int w,
234
0
                                 int h, int bd) {
235
0
  const InterpKernel *const filters_y = get_filter_base(filter_y);
236
0
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
237
0
  (void)filter_x;
238
0
  (void)x_step_q4;
239
240
0
  highbd_convolve_vert(src, src_stride, dst, dst_stride, filters_y, y0_q4,
241
0
                       y_step_q4, w, h, bd);
242
0
}
243
244
void aom_highbd_convolve_copy_c(const uint16_t *src, ptrdiff_t src_stride,
245
                                uint16_t *dst, ptrdiff_t dst_stride, int w,
246
0
                                int h) {
247
0
  for (int y = 0; y < h; ++y) {
248
0
    memmove(dst, src, w * sizeof(src[0]));
249
0
    src += src_stride;
250
0
    dst += dst_stride;
251
0
  }
252
0
}
253
#endif  // CONFIG_AV1_HIGHBITDEPTH