Coverage Report

Created: 2022-08-24 06:17

/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_convolve8_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_scaled_2d_c(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
149
                     ptrdiff_t dst_stride, const InterpKernel *filter,
150
                     int x0_q4, int x_step_q4, int y0_q4, int y_step_q4, int w,
151
0
                     int h) {
152
0
  aom_convolve8_c(src, src_stride, dst, dst_stride, filter, x0_q4, x_step_q4,
153
0
                  y0_q4, y_step_q4, w, h);
154
0
}
155
156
void aom_convolve_copy_c(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
157
171k
                         ptrdiff_t dst_stride, int w, int h) {
158
3.74M
  for (int r = h; r > 0; --r) {
159
3.56M
    memmove(dst, src, w);
160
3.56M
    src += src_stride;
161
3.56M
    dst += dst_stride;
162
3.56M
  }
163
171k
}
164
165
#if CONFIG_AV1_HIGHBITDEPTH
166
static INLINE int highbd_vert_scalar_product(const uint16_t *a,
167
                                             ptrdiff_t a_stride,
168
0
                                             const int16_t *b) {
169
0
  int sum = 0;
170
0
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k * a_stride] * b[k];
171
0
  return sum;
172
0
}
173
174
static INLINE int highbd_horz_scalar_product(const uint16_t *a,
175
0
                                             const int16_t *b) {
176
0
  int sum = 0;
177
0
  for (int k = 0; k < SUBPEL_TAPS; ++k) sum += a[k] * b[k];
178
0
  return sum;
179
0
}
180
181
static void highbd_convolve_horiz(const uint8_t *src8, ptrdiff_t src_stride,
182
                                  uint8_t *dst8, ptrdiff_t dst_stride,
183
                                  const InterpKernel *x_filters, int x0_q4,
184
0
                                  int x_step_q4, int w, int h, int bd) {
185
0
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
186
0
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
187
0
  src -= SUBPEL_TAPS / 2 - 1;
188
0
  for (int y = 0; y < h; ++y) {
189
0
    int x_q4 = x0_q4;
190
0
    for (int x = 0; x < w; ++x) {
191
0
      const uint16_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
192
0
      const int16_t *const x_filter = x_filters[x_q4 & SUBPEL_MASK];
193
0
      const int sum = highbd_horz_scalar_product(src_x, x_filter);
194
0
      dst[x] = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
195
0
      x_q4 += x_step_q4;
196
0
    }
197
0
    src += src_stride;
198
0
    dst += dst_stride;
199
0
  }
200
0
}
201
202
static void highbd_convolve_vert(const uint8_t *src8, ptrdiff_t src_stride,
203
                                 uint8_t *dst8, ptrdiff_t dst_stride,
204
                                 const InterpKernel *y_filters, int y0_q4,
205
0
                                 int y_step_q4, int w, int h, int bd) {
206
0
  uint16_t *src = CONVERT_TO_SHORTPTR(src8);
207
0
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
208
0
  src -= src_stride * (SUBPEL_TAPS / 2 - 1);
209
0
  for (int x = 0; x < w; ++x) {
210
0
    int y_q4 = y0_q4;
211
0
    for (int y = 0; y < h; ++y) {
212
0
      const uint16_t *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
213
0
      const int16_t *const y_filter = y_filters[y_q4 & SUBPEL_MASK];
214
0
      const int sum = highbd_vert_scalar_product(src_y, src_stride, y_filter);
215
0
      dst[y * dst_stride] =
216
0
          clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
217
0
      y_q4 += y_step_q4;
218
0
    }
219
0
    ++src;
220
0
    ++dst;
221
0
  }
222
0
}
223
224
void aom_highbd_convolve8_horiz_c(const uint8_t *src, ptrdiff_t src_stride,
225
                                  uint8_t *dst, ptrdiff_t dst_stride,
226
                                  const int16_t *filter_x, int x_step_q4,
227
                                  const int16_t *filter_y, int y_step_q4, int w,
228
0
                                  int h, int bd) {
229
0
  const InterpKernel *const filters_x = get_filter_base(filter_x);
230
0
  const int x0_q4 = get_filter_offset(filter_x, filters_x);
231
0
  (void)filter_y;
232
0
  (void)y_step_q4;
233
234
0
  highbd_convolve_horiz(src, src_stride, dst, dst_stride, filters_x, x0_q4,
235
0
                        x_step_q4, w, h, bd);
236
0
}
237
238
void aom_highbd_convolve8_vert_c(const uint8_t *src, ptrdiff_t src_stride,
239
                                 uint8_t *dst, ptrdiff_t dst_stride,
240
                                 const int16_t *filter_x, int x_step_q4,
241
                                 const int16_t *filter_y, int y_step_q4, int w,
242
0
                                 int h, int bd) {
243
0
  const InterpKernel *const filters_y = get_filter_base(filter_y);
244
0
  const int y0_q4 = get_filter_offset(filter_y, filters_y);
245
0
  (void)filter_x;
246
0
  (void)x_step_q4;
247
248
0
  highbd_convolve_vert(src, src_stride, dst, dst_stride, filters_y, y0_q4,
249
0
                       y_step_q4, w, h, bd);
250
0
}
251
252
void aom_highbd_convolve_copy_c(const uint16_t *src, ptrdiff_t src_stride,
253
                                uint16_t *dst, ptrdiff_t dst_stride, int w,
254
8.60k
                                int h) {
255
136k
  for (int y = 0; y < h; ++y) {
256
127k
    memmove(dst, src, w * sizeof(src[0]));
257
127k
    src += src_stride;
258
127k
    dst += dst_stride;
259
127k
  }
260
8.60k
}
261
#endif  // CONFIG_AV1_HIGHBITDEPTH