Coverage Report

Created: 2023-06-07 06:31

/src/aom/av1/common/x86/wiener_convolve_avx2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2018, 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 <immintrin.h>
13
#include <assert.h>
14
15
#include "config/av1_rtcd.h"
16
17
#include "av1/common/convolve.h"
18
#include "aom_dsp/aom_dsp_common.h"
19
#include "aom_dsp/aom_filter.h"
20
#include "aom_dsp/x86/convolve_avx2.h"
21
#include "aom_dsp/x86/synonyms.h"
22
#include "aom_dsp/x86/synonyms_avx2.h"
23
24
// 128-bit xmmwords are written as [ ... ] with the MSB on the left.
25
// 256-bit ymmwords are written as two xmmwords, [ ... ][ ... ] with the MSB
26
// on the left.
27
// A row of, say, 8-bit pixels with values p0, p1, p2, ..., p30, p31 will be
28
// loaded and stored as [ p31 ... p17 p16 ][ p15 ... p1 p0 ].
29
30
// Exploiting the range of wiener filter coefficients,
31
// horizontal filtering can be done in 16 bit intermediate precision.
32
// The details are as follows :
33
// Consider the horizontal wiener filter coefficients of the following form :
34
//      [C0, C1, C2, 2^(FILTER_BITS) -2 * (C0 + C1 + C2), C2, C1, C0]
35
// Subtracting  2^(FILTER_BITS) from the centre tap we get the following  :
36
//      [C0, C1, C2,     -2 * (C0 + C1 + C2),             C2, C1, C0]
37
// The sum of the product "C0 * p0 + C1 * p1 + C2 * p2 -2 * (C0 + C1 + C2) * p3
38
// + C2 * p4 + C1 * p5 + C0 * p6" would be in the range of signed 16 bit
39
// precision. Finally, after rounding the above result by round_0, we multiply
40
// the centre pixel by 2^(FILTER_BITS - round_0) and add it to get the
41
// horizontal filter output.
42
43
void av1_wiener_convolve_add_src_avx2(const uint8_t *src, ptrdiff_t src_stride,
44
                                      uint8_t *dst, ptrdiff_t dst_stride,
45
                                      const int16_t *filter_x, int x_step_q4,
46
                                      const int16_t *filter_y, int y_step_q4,
47
                                      int w, int h,
48
442k
                                      const ConvolveParams *conv_params) {
49
442k
  const int bd = 8;
50
442k
  assert(x_step_q4 == 16 && y_step_q4 == 16);
51
0
  assert(!(w & 7));
52
0
  (void)x_step_q4;
53
442k
  (void)y_step_q4;
54
55
442k
  DECLARE_ALIGNED(32, int16_t, im_block[(MAX_SB_SIZE + SUBPEL_TAPS) * 8]);
56
442k
  int im_h = h + SUBPEL_TAPS - 2;
57
442k
  int im_stride = 8;
58
442k
  memset(im_block + (im_h * im_stride), 0, MAX_SB_SIZE);
59
442k
  int i, j;
60
442k
  const int center_tap = (SUBPEL_TAPS - 1) / 2;
61
442k
  const uint8_t *const src_ptr = src - center_tap * src_stride - center_tap;
62
63
442k
  __m256i filt[4], coeffs_h[4], coeffs_v[4], filt_center;
64
65
442k
  assert(conv_params->round_0 > 0);
66
67
0
  filt[0] = _mm256_load_si256((__m256i const *)filt1_global_avx2);
68
442k
  filt[1] = _mm256_load_si256((__m256i const *)filt2_global_avx2);
69
442k
  filt[2] = _mm256_load_si256((__m256i const *)filt3_global_avx2);
70
442k
  filt[3] = _mm256_load_si256((__m256i const *)filt4_global_avx2);
71
72
442k
  filt_center = _mm256_load_si256((__m256i const *)filt_center_global_avx2);
73
74
442k
  const __m128i coeffs_x = _mm_loadu_si128((__m128i *)filter_x);
75
442k
  const __m256i filter_coeffs_x = _mm256_broadcastsi128_si256(coeffs_x);
76
77
  // coeffs 0 1 0 1 0 1 0 1
78
442k
  coeffs_h[0] =
79
442k
      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0200u));
80
  // coeffs 2 3 2 3 2 3 2 3
81
442k
  coeffs_h[1] =
82
442k
      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0604u));
83
  // coeffs 4 5 4 5 4 5 4 5
84
442k
  coeffs_h[2] =
85
442k
      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0a08u));
86
  // coeffs 6 7 6 7 6 7 6 7
87
442k
  coeffs_h[3] =
88
442k
      _mm256_shuffle_epi8(filter_coeffs_x, _mm256_set1_epi16(0x0e0cu));
89
90
442k
  const __m256i round_const_h =
91
442k
      _mm256_set1_epi16((1 << (conv_params->round_0 - 1)));
92
442k
  const __m256i round_const_horz =
93
442k
      _mm256_set1_epi16((1 << (bd + FILTER_BITS - conv_params->round_0 - 1)));
94
442k
  const __m256i clamp_low = _mm256_setzero_si256();
95
442k
  const __m256i clamp_high =
96
442k
      _mm256_set1_epi16(WIENER_CLAMP_LIMIT(conv_params->round_0, bd) - 1);
97
442k
  const __m128i round_shift_h = _mm_cvtsi32_si128(conv_params->round_0);
98
99
  // Add an offset to account for the "add_src" part of the convolve function.
100
442k
  const __m128i zero_128 = _mm_setzero_si128();
101
442k
  const __m128i offset_0 = _mm_insert_epi16(zero_128, 1 << FILTER_BITS, 3);
102
442k
  const __m128i coeffs_y = _mm_add_epi16(xx_loadu_128(filter_y), offset_0);
103
104
442k
  const __m256i filter_coeffs_y = _mm256_broadcastsi128_si256(coeffs_y);
105
106
  // coeffs 0 1 0 1 0 1 0 1
107
442k
  coeffs_v[0] = _mm256_shuffle_epi32(filter_coeffs_y, 0x00);
108
  // coeffs 2 3 2 3 2 3 2 3
109
442k
  coeffs_v[1] = _mm256_shuffle_epi32(filter_coeffs_y, 0x55);
110
  // coeffs 4 5 4 5 4 5 4 5
111
442k
  coeffs_v[2] = _mm256_shuffle_epi32(filter_coeffs_y, 0xaa);
112
  // coeffs 6 7 6 7 6 7 6 7
113
442k
  coeffs_v[3] = _mm256_shuffle_epi32(filter_coeffs_y, 0xff);
114
115
442k
  const __m256i round_const_v =
116
442k
      _mm256_set1_epi32((1 << (conv_params->round_1 - 1)) -
117
442k
                        (1 << (bd + conv_params->round_1 - 1)));
118
442k
  const __m128i round_shift_v = _mm_cvtsi32_si128(conv_params->round_1);
119
120
1.93M
  for (j = 0; j < w; j += 8) {
121
34.7M
    for (i = 0; i < im_h; i += 2) {
122
33.2M
      __m256i data = _mm256_castsi128_si256(
123
33.2M
          _mm_loadu_si128((__m128i *)&src_ptr[(i * src_stride) + j]));
124
125
      // Load the next line
126
33.2M
      if (i + 1 < im_h)
127
33.5M
        data = _mm256_inserti128_si256(
128
33.2M
            data,
129
33.2M
            _mm_loadu_si128(
130
33.2M
                (__m128i *)&src_ptr[(i * src_stride) + j + src_stride]),
131
33.2M
            1);
132
133
33.2M
      __m256i res = convolve_lowbd_x(data, coeffs_h, filt);
134
135
33.2M
      res =
136
33.2M
          _mm256_sra_epi16(_mm256_add_epi16(res, round_const_h), round_shift_h);
137
138
33.2M
      __m256i data_0 = _mm256_shuffle_epi8(data, filt_center);
139
140
      // multiply the center pixel by 2^(FILTER_BITS - round_0) and add it to
141
      // the result
142
33.2M
      data_0 = _mm256_slli_epi16(data_0, FILTER_BITS - conv_params->round_0);
143
33.2M
      res = _mm256_add_epi16(res, data_0);
144
33.2M
      res = _mm256_add_epi16(res, round_const_horz);
145
33.2M
      const __m256i res_clamped =
146
33.2M
          _mm256_min_epi16(_mm256_max_epi16(res, clamp_low), clamp_high);
147
33.2M
      _mm256_store_si256((__m256i *)&im_block[i * im_stride], res_clamped);
148
33.2M
    }
149
150
    /* Vertical filter */
151
1.49M
    {
152
1.49M
      __m256i src_0 = _mm256_loadu_si256((__m256i *)(im_block + 0 * im_stride));
153
1.49M
      __m256i src_1 = _mm256_loadu_si256((__m256i *)(im_block + 1 * im_stride));
154
1.49M
      __m256i src_2 = _mm256_loadu_si256((__m256i *)(im_block + 2 * im_stride));
155
1.49M
      __m256i src_3 = _mm256_loadu_si256((__m256i *)(im_block + 3 * im_stride));
156
1.49M
      __m256i src_4 = _mm256_loadu_si256((__m256i *)(im_block + 4 * im_stride));
157
1.49M
      __m256i src_5 = _mm256_loadu_si256((__m256i *)(im_block + 5 * im_stride));
158
159
1.49M
      __m256i s[8];
160
1.49M
      s[0] = _mm256_unpacklo_epi16(src_0, src_1);
161
1.49M
      s[1] = _mm256_unpacklo_epi16(src_2, src_3);
162
1.49M
      s[2] = _mm256_unpacklo_epi16(src_4, src_5);
163
164
1.49M
      s[4] = _mm256_unpackhi_epi16(src_0, src_1);
165
1.49M
      s[5] = _mm256_unpackhi_epi16(src_2, src_3);
166
1.49M
      s[6] = _mm256_unpackhi_epi16(src_4, src_5);
167
168
18.5M
      for (i = 0; i < h - 1; i += 2) {
169
17.0M
        const int16_t *data = &im_block[i * im_stride];
170
171
17.0M
        const __m256i s6 =
172
17.0M
            _mm256_loadu_si256((__m256i *)(data + 6 * im_stride));
173
17.0M
        const __m256i s7 =
174
17.0M
            _mm256_loadu_si256((__m256i *)(data + 7 * im_stride));
175
176
17.0M
        s[3] = _mm256_unpacklo_epi16(s6, s7);
177
17.0M
        s[7] = _mm256_unpackhi_epi16(s6, s7);
178
179
17.0M
        __m256i res_a = convolve(s, coeffs_v);
180
17.0M
        __m256i res_b = convolve(s + 4, coeffs_v);
181
182
17.0M
        const __m256i res_a_round = _mm256_sra_epi32(
183
17.0M
            _mm256_add_epi32(res_a, round_const_v), round_shift_v);
184
17.0M
        const __m256i res_b_round = _mm256_sra_epi32(
185
17.0M
            _mm256_add_epi32(res_b, round_const_v), round_shift_v);
186
187
        /* rounding code */
188
        // 16 bit conversion
189
17.0M
        const __m256i res_16bit = _mm256_packs_epi32(res_a_round, res_b_round);
190
        // 8 bit conversion and saturation to uint8
191
17.0M
        const __m256i res_8b = _mm256_packus_epi16(res_16bit, res_16bit);
192
193
17.0M
        const __m128i res_0 = _mm256_castsi256_si128(res_8b);
194
17.0M
        const __m128i res_1 = _mm256_extracti128_si256(res_8b, 1);
195
196
        // Store values into the destination buffer
197
17.0M
        __m128i *const p_0 = (__m128i *)&dst[i * dst_stride + j];
198
17.0M
        __m128i *const p_1 = (__m128i *)&dst[i * dst_stride + j + dst_stride];
199
200
17.0M
        _mm_storel_epi64(p_0, res_0);
201
17.0M
        _mm_storel_epi64(p_1, res_1);
202
203
17.0M
        s[0] = s[1];
204
17.0M
        s[1] = s[2];
205
17.0M
        s[2] = s[3];
206
207
17.0M
        s[4] = s[5];
208
17.0M
        s[5] = s[6];
209
17.0M
        s[6] = s[7];
210
17.0M
      }
211
1.49M
      if (h - i) {
212
1.31k
        s[0] = _mm256_permute2x128_si256(s[0], s[4], 0x20);
213
1.31k
        s[1] = _mm256_permute2x128_si256(s[1], s[5], 0x20);
214
1.31k
        s[2] = _mm256_permute2x128_si256(s[2], s[6], 0x20);
215
216
1.31k
        const int16_t *data = &im_block[i * im_stride];
217
1.31k
        const __m128i s6_ = _mm_loadu_si128((__m128i *)(data + 6 * im_stride));
218
1.31k
        const __m128i s7_ = _mm_loadu_si128((__m128i *)(data + 7 * im_stride));
219
220
1.31k
        __m128i s3 = _mm_unpacklo_epi16(s6_, s7_);
221
1.31k
        __m128i s7 = _mm_unpackhi_epi16(s6_, s7_);
222
223
1.31k
        s[3] = _mm256_inserti128_si256(_mm256_castsi128_si256(s3), s7, 1);
224
1.31k
        __m256i convolveres = convolve(s, coeffs_v);
225
226
1.31k
        const __m256i res_round = _mm256_sra_epi32(
227
1.31k
            _mm256_add_epi32(convolveres, round_const_v), round_shift_v);
228
229
        /* rounding code */
230
        // 16 bit conversion
231
1.31k
        __m128i reslo = _mm256_castsi256_si128(res_round);
232
1.31k
        __m128i reshi = _mm256_extracti128_si256(res_round, 1);
233
1.31k
        const __m128i res_16bit = _mm_packus_epi32(reslo, reshi);
234
235
        // 8 bit conversion and saturation to uint8
236
1.31k
        const __m128i res_8b = _mm_packus_epi16(res_16bit, res_16bit);
237
1.31k
        __m128i *const p_0 = (__m128i *)&dst[i * dst_stride + j];
238
1.31k
        _mm_storel_epi64(p_0, res_8b);
239
1.31k
      }
240
1.49M
    }
241
1.49M
  }
242
442k
}