Coverage Report

Created: 2025-07-23 06:32

/src/aom/av1/common/x86/intra_edge_sse4.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2017, 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 <smmintrin.h>
14
15
#include "config/aom_config.h"
16
#include "config/av1_rtcd.h"
17
18
2.84M
void av1_filter_intra_edge_sse4_1(uint8_t *p, int sz, int strength) {
19
2.84M
  if (!strength) return;
20
21
1.95M
  DECLARE_ALIGNED(16, static const int8_t, kern[3][16]) = {
22
1.95M
    { 4, 8, 4, 0, 4, 8, 4, 0, 4, 8, 4, 0, 4, 8, 4, 0 },  // strength 1: 4,8,4
23
1.95M
    { 5, 6, 5, 0, 5, 6, 5, 0, 5, 6, 5, 0, 5, 6, 5, 0 },  // strength 2: 5,6,5
24
1.95M
    { 2, 4, 4, 4, 2, 0, 0, 0, 2, 4, 4, 4, 2, 0, 0, 0 }  // strength 3: 2,4,4,4,2
25
1.95M
  };
26
27
1.95M
  DECLARE_ALIGNED(16, static const int8_t, v_const[5][16]) = {
28
1.95M
    { 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6 },
29
1.95M
    { 4, 5, 6, 7, 5, 6, 7, 8, 6, 7, 8, 9, 7, 8, 9, 10 },
30
1.95M
    { 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8 },
31
1.95M
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
32
1.95M
  };
33
34
  // Extend the first and last samples to simplify the loop for the 5-tap case
35
1.95M
  p[-1] = p[0];
36
1.95M
  __m128i last = _mm_set1_epi8((char)p[sz - 1]);
37
1.95M
  _mm_storeu_si128((__m128i *)&p[sz], last);
38
39
  // Adjust input pointer for filter support area
40
1.95M
  uint8_t *in = (strength == 3) ? p - 1 : p;
41
42
  // Avoid modifying first sample
43
1.95M
  uint8_t *out = p + 1;
44
1.95M
  int len = sz - 1;
45
46
1.95M
  const int use_3tap_filter = (strength < 3);
47
48
1.95M
  if (use_3tap_filter) {
49
846k
    __m128i coef0 = _mm_lddqu_si128((__m128i const *)kern[strength - 1]);
50
846k
    __m128i shuf0 = _mm_lddqu_si128((__m128i const *)v_const[0]);
51
846k
    __m128i shuf1 = _mm_lddqu_si128((__m128i const *)v_const[1]);
52
846k
    __m128i iden = _mm_lddqu_si128((__m128i *)v_const[3]);
53
846k
    __m128i in0 = _mm_lddqu_si128((__m128i *)in);
54
2.22M
    while (len > 0) {
55
1.37M
      int n_out = (len < 8) ? len : 8;
56
1.37M
      __m128i d0 = _mm_shuffle_epi8(in0, shuf0);
57
1.37M
      __m128i d1 = _mm_shuffle_epi8(in0, shuf1);
58
1.37M
      d0 = _mm_maddubs_epi16(d0, coef0);
59
1.37M
      d1 = _mm_maddubs_epi16(d1, coef0);
60
1.37M
      d0 = _mm_hadd_epi16(d0, d1);
61
1.37M
      __m128i eight = _mm_set1_epi16(8);
62
1.37M
      d0 = _mm_add_epi16(d0, eight);
63
1.37M
      d0 = _mm_srai_epi16(d0, 4);
64
1.37M
      d0 = _mm_packus_epi16(d0, d0);
65
1.37M
      __m128i out0 = _mm_lddqu_si128((__m128i *)out);
66
1.37M
      __m128i n0 = _mm_set1_epi8(n_out);
67
1.37M
      __m128i mask = _mm_cmpgt_epi8(n0, iden);
68
1.37M
      out0 = _mm_blendv_epi8(out0, d0, mask);
69
1.37M
      _mm_storel_epi64((__m128i *)out, out0);
70
1.37M
      __m128i in1 = _mm_lddqu_si128((__m128i *)(in + 16));
71
1.37M
      in0 = _mm_alignr_epi8(in1, in0, 8);
72
1.37M
      in += 8;
73
1.37M
      out += 8;
74
1.37M
      len -= n_out;
75
1.37M
    }
76
1.10M
  } else {  // 5-tap filter
77
1.10M
    __m128i coef0 = _mm_lddqu_si128((__m128i const *)kern[strength - 1]);
78
1.10M
    __m128i two = _mm_set1_epi8(2);
79
1.10M
    __m128i shuf_a = _mm_lddqu_si128((__m128i const *)v_const[2]);
80
1.10M
    __m128i shuf_b = _mm_add_epi8(shuf_a, two);
81
1.10M
    __m128i shuf_c = _mm_add_epi8(shuf_b, two);
82
1.10M
    __m128i shuf_d = _mm_add_epi8(shuf_c, two);
83
1.10M
    __m128i iden = _mm_lddqu_si128((__m128i *)v_const[3]);
84
1.10M
    __m128i in0 = _mm_lddqu_si128((__m128i *)in);
85
5.64M
    while (len > 0) {
86
4.53M
      int n_out = (len < 8) ? len : 8;
87
4.53M
      __m128i d0 = _mm_shuffle_epi8(in0, shuf_a);
88
4.53M
      __m128i d1 = _mm_shuffle_epi8(in0, shuf_b);
89
4.53M
      __m128i d2 = _mm_shuffle_epi8(in0, shuf_c);
90
4.53M
      __m128i d3 = _mm_shuffle_epi8(in0, shuf_d);
91
4.53M
      d0 = _mm_maddubs_epi16(d0, coef0);
92
4.53M
      d1 = _mm_maddubs_epi16(d1, coef0);
93
4.53M
      d2 = _mm_maddubs_epi16(d2, coef0);
94
4.53M
      d3 = _mm_maddubs_epi16(d3, coef0);
95
4.53M
      d0 = _mm_hadd_epi16(d0, d1);
96
4.53M
      d2 = _mm_hadd_epi16(d2, d3);
97
4.53M
      d0 = _mm_hadd_epi16(d0, d2);
98
4.53M
      __m128i eight = _mm_set1_epi16(8);
99
4.53M
      d0 = _mm_add_epi16(d0, eight);
100
4.53M
      d0 = _mm_srai_epi16(d0, 4);
101
4.53M
      d0 = _mm_packus_epi16(d0, d0);
102
4.53M
      __m128i out0 = _mm_lddqu_si128((__m128i *)out);
103
4.53M
      __m128i n0 = _mm_set1_epi8(n_out);
104
4.53M
      __m128i mask = _mm_cmpgt_epi8(n0, iden);
105
4.53M
      out0 = _mm_blendv_epi8(out0, d0, mask);
106
4.53M
      _mm_storel_epi64((__m128i *)out, out0);
107
4.53M
      __m128i in1 = _mm_lddqu_si128((__m128i *)(in + 16));
108
4.53M
      in0 = _mm_alignr_epi8(in1, in0, 8);
109
4.53M
      in += 8;
110
4.53M
      out += 8;
111
4.53M
      len -= n_out;
112
4.53M
    }
113
1.10M
  }
114
1.95M
}
115
116
705k
void av1_upsample_intra_edge_sse4_1(uint8_t *p, int sz) {
117
  // interpolate half-sample positions
118
705k
  assert(sz <= 24);
119
120
705k
  DECLARE_ALIGNED(16, static const int8_t, kernel[1][16]) = {
121
705k
    { -1, 9, 9, -1, -1, 9, 9, -1, -1, 9, 9, -1, -1, 9, 9, -1 }
122
705k
  };
123
124
705k
  DECLARE_ALIGNED(
125
705k
      16, static const int8_t,
126
705k
      v_const[2][16]) = { { 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6 },
127
705k
                          { 4, 5, 6, 7, 5, 6, 7, 8, 6, 7, 8, 9, 7, 8, 9, 10 } };
128
129
  // Extend first/last samples (upper-left p[-1], last p[sz-1])
130
  // to support 4-tap filter
131
705k
  p[-2] = p[-1];
132
705k
  p[sz] = p[sz - 1];
133
134
705k
  uint8_t *in = &p[-2];
135
705k
  uint8_t *out = &p[-2];
136
137
705k
  int n = sz + 1;  // Input length including upper-left sample
138
139
705k
  __m128i in0 = _mm_lddqu_si128((__m128i *)&in[0]);
140
705k
  __m128i in16 = _mm_lddqu_si128((__m128i *)&in[16]);
141
142
705k
  __m128i coef0 = _mm_lddqu_si128((__m128i *)kernel[0]);
143
705k
  __m128i shuf0 = _mm_lddqu_si128((__m128i *)v_const[0]);
144
705k
  __m128i shuf1 = _mm_lddqu_si128((__m128i *)v_const[1]);
145
146
1.50M
  while (n > 0) {
147
796k
    __m128i in8 = _mm_alignr_epi8(in16, in0, 8);
148
796k
    __m128i d0 = _mm_shuffle_epi8(in0, shuf0);
149
796k
    __m128i d1 = _mm_shuffle_epi8(in0, shuf1);
150
796k
    __m128i d2 = _mm_shuffle_epi8(in8, shuf0);
151
796k
    __m128i d3 = _mm_shuffle_epi8(in8, shuf1);
152
796k
    d0 = _mm_maddubs_epi16(d0, coef0);
153
796k
    d1 = _mm_maddubs_epi16(d1, coef0);
154
796k
    d2 = _mm_maddubs_epi16(d2, coef0);
155
796k
    d3 = _mm_maddubs_epi16(d3, coef0);
156
796k
    d0 = _mm_hadd_epi16(d0, d1);
157
796k
    d2 = _mm_hadd_epi16(d2, d3);
158
796k
    __m128i eight = _mm_set1_epi16(8);
159
796k
    d0 = _mm_add_epi16(d0, eight);
160
796k
    d2 = _mm_add_epi16(d2, eight);
161
796k
    d0 = _mm_srai_epi16(d0, 4);
162
796k
    d2 = _mm_srai_epi16(d2, 4);
163
796k
    d0 = _mm_packus_epi16(d0, d2);
164
796k
    __m128i in1 = _mm_alignr_epi8(in16, in0, 1);
165
796k
    __m128i out0 = _mm_unpacklo_epi8(in1, d0);
166
796k
    __m128i out1 = _mm_unpackhi_epi8(in1, d0);
167
796k
    _mm_storeu_si128((__m128i *)&out[0], out0);
168
796k
    _mm_storeu_si128((__m128i *)&out[16], out1);
169
796k
    in0 = in16;
170
796k
    in16 = _mm_setzero_si128();
171
796k
    out += 32;
172
796k
    n -= 16;
173
796k
  }
174
705k
}
175
176
#if CONFIG_AV1_HIGHBITDEPTH
177
178
3.05M
void av1_highbd_filter_intra_edge_sse4_1(uint16_t *p, int sz, int strength) {
179
3.05M
  if (!strength) return;
180
181
2.07M
  DECLARE_ALIGNED(16, static const int16_t, kern[3][8]) = {
182
2.07M
    { 4, 8, 4, 8, 4, 8, 4, 8 },  // strength 1: 4,8,4
183
2.07M
    { 5, 6, 5, 6, 5, 6, 5, 6 },  // strength 2: 5,6,5
184
2.07M
    { 2, 4, 2, 4, 2, 4, 2, 4 }   // strength 3: 2,4,4,4,2
185
2.07M
  };
186
187
2.07M
  DECLARE_ALIGNED(16, static const int16_t,
188
2.07M
                  v_const[1][8]) = { { 0, 1, 2, 3, 4, 5, 6, 7 } };
189
190
  // Extend the first and last samples to simplify the loop for the 5-tap case
191
2.07M
  p[-1] = p[0];
192
2.07M
  __m128i last = _mm_set1_epi16(p[sz - 1]);
193
2.07M
  _mm_storeu_si128((__m128i *)&p[sz], last);
194
195
  // Adjust input pointer for filter support area
196
2.07M
  uint16_t *in = (strength == 3) ? p - 1 : p;
197
198
  // Avoid modifying first sample
199
2.07M
  uint16_t *out = p + 1;
200
2.07M
  int len = sz - 1;
201
202
2.07M
  const int use_3tap_filter = (strength < 3);
203
204
2.07M
  if (use_3tap_filter) {
205
835k
    __m128i coef0 = _mm_lddqu_si128((__m128i const *)kern[strength - 1]);
206
835k
    __m128i iden = _mm_lddqu_si128((__m128i *)v_const[0]);
207
835k
    __m128i in0 = _mm_lddqu_si128((__m128i *)&in[0]);
208
835k
    __m128i in8 = _mm_lddqu_si128((__m128i *)&in[8]);
209
2.30M
    while (len > 0) {
210
1.46M
      int n_out = (len < 8) ? len : 8;
211
1.46M
      __m128i in1 = _mm_alignr_epi8(in8, in0, 2);
212
1.46M
      __m128i in2 = _mm_alignr_epi8(in8, in0, 4);
213
1.46M
      __m128i in02 = _mm_add_epi16(in0, in2);
214
1.46M
      __m128i d0 = _mm_unpacklo_epi16(in02, in1);
215
1.46M
      __m128i d1 = _mm_unpackhi_epi16(in02, in1);
216
1.46M
      d0 = _mm_mullo_epi16(d0, coef0);
217
1.46M
      d1 = _mm_mullo_epi16(d1, coef0);
218
1.46M
      d0 = _mm_hadd_epi16(d0, d1);
219
1.46M
      __m128i eight = _mm_set1_epi16(8);
220
1.46M
      d0 = _mm_add_epi16(d0, eight);
221
1.46M
      d0 = _mm_srli_epi16(d0, 4);
222
1.46M
      __m128i out0 = _mm_lddqu_si128((__m128i *)out);
223
1.46M
      __m128i n0 = _mm_set1_epi16(n_out);
224
1.46M
      __m128i mask = _mm_cmpgt_epi16(n0, iden);
225
1.46M
      out0 = _mm_blendv_epi8(out0, d0, mask);
226
1.46M
      _mm_storeu_si128((__m128i *)out, out0);
227
1.46M
      in += 8;
228
1.46M
      in0 = in8;
229
1.46M
      in8 = _mm_lddqu_si128((__m128i *)&in[8]);
230
1.46M
      out += 8;
231
1.46M
      len -= n_out;
232
1.46M
    }
233
1.23M
  } else {  // 5-tap filter
234
1.23M
    __m128i coef0 = _mm_lddqu_si128((__m128i const *)kern[strength - 1]);
235
1.23M
    __m128i iden = _mm_lddqu_si128((__m128i *)v_const[0]);
236
1.23M
    __m128i in0 = _mm_lddqu_si128((__m128i *)&in[0]);
237
1.23M
    __m128i in8 = _mm_lddqu_si128((__m128i *)&in[8]);
238
6.34M
    while (len > 0) {
239
5.10M
      int n_out = (len < 8) ? len : 8;
240
5.10M
      __m128i in1 = _mm_alignr_epi8(in8, in0, 2);
241
5.10M
      __m128i in2 = _mm_alignr_epi8(in8, in0, 4);
242
5.10M
      __m128i in3 = _mm_alignr_epi8(in8, in0, 6);
243
5.10M
      __m128i in4 = _mm_alignr_epi8(in8, in0, 8);
244
5.10M
      __m128i in04 = _mm_add_epi16(in0, in4);
245
5.10M
      __m128i in123 = _mm_add_epi16(in1, in2);
246
5.10M
      in123 = _mm_add_epi16(in123, in3);
247
5.10M
      __m128i d0 = _mm_unpacklo_epi16(in04, in123);
248
5.10M
      __m128i d1 = _mm_unpackhi_epi16(in04, in123);
249
5.10M
      d0 = _mm_mullo_epi16(d0, coef0);
250
5.10M
      d1 = _mm_mullo_epi16(d1, coef0);
251
5.10M
      d0 = _mm_hadd_epi16(d0, d1);
252
5.10M
      __m128i eight = _mm_set1_epi16(8);
253
5.10M
      d0 = _mm_add_epi16(d0, eight);
254
5.10M
      d0 = _mm_srli_epi16(d0, 4);
255
5.10M
      __m128i out0 = _mm_lddqu_si128((__m128i *)out);
256
5.10M
      __m128i n0 = _mm_set1_epi16(n_out);
257
5.10M
      __m128i mask = _mm_cmpgt_epi16(n0, iden);
258
5.10M
      out0 = _mm_blendv_epi8(out0, d0, mask);
259
5.10M
      _mm_storeu_si128((__m128i *)out, out0);
260
5.10M
      in += 8;
261
5.10M
      in0 = in8;
262
5.10M
      in8 = _mm_lddqu_si128((__m128i *)&in[8]);
263
5.10M
      out += 8;
264
5.10M
      len -= n_out;
265
5.10M
    }
266
1.23M
  }
267
2.07M
}
268
269
752k
void av1_highbd_upsample_intra_edge_sse4_1(uint16_t *p, int sz, int bd) {
270
  // interpolate half-sample positions
271
752k
  assert(sz <= 24);
272
273
752k
  DECLARE_ALIGNED(16, static const int16_t,
274
752k
                  kernel[1][8]) = { { -1, 9, -1, 9, -1, 9, -1, 9 } };
275
276
  // Extend first/last samples (upper-left p[-1], last p[sz-1])
277
  // to support 4-tap filter
278
752k
  p[-2] = p[-1];
279
752k
  p[sz] = p[sz - 1];
280
281
752k
  uint16_t *in = &p[-2];
282
752k
  uint16_t *out = in;
283
752k
  int n = sz + 1;
284
285
752k
  __m128i in0 = _mm_lddqu_si128((__m128i *)&in[0]);
286
752k
  __m128i in8 = _mm_lddqu_si128((__m128i *)&in[8]);
287
752k
  __m128i in16 = _mm_lddqu_si128((__m128i *)&in[16]);
288
752k
  __m128i in24 = _mm_lddqu_si128((__m128i *)&in[24]);
289
290
2.18M
  while (n > 0) {
291
1.43M
    __m128i in1 = _mm_alignr_epi8(in8, in0, 2);
292
1.43M
    __m128i in2 = _mm_alignr_epi8(in8, in0, 4);
293
1.43M
    __m128i in3 = _mm_alignr_epi8(in8, in0, 6);
294
1.43M
    __m128i sum0 = _mm_add_epi16(in0, in3);
295
1.43M
    __m128i sum1 = _mm_add_epi16(in1, in2);
296
1.43M
    __m128i d0 = _mm_unpacklo_epi16(sum0, sum1);
297
1.43M
    __m128i d1 = _mm_unpackhi_epi16(sum0, sum1);
298
1.43M
    __m128i coef0 = _mm_lddqu_si128((__m128i *)kernel[0]);
299
1.43M
    d0 = _mm_madd_epi16(d0, coef0);
300
1.43M
    d1 = _mm_madd_epi16(d1, coef0);
301
1.43M
    __m128i eight = _mm_set1_epi32(8);
302
1.43M
    d0 = _mm_add_epi32(d0, eight);
303
1.43M
    d1 = _mm_add_epi32(d1, eight);
304
1.43M
    d0 = _mm_srai_epi32(d0, 4);
305
1.43M
    d1 = _mm_srai_epi32(d1, 4);
306
1.43M
    d0 = _mm_packus_epi32(d0, d1);
307
1.43M
    __m128i max0 = _mm_set1_epi16((1 << bd) - 1);
308
1.43M
    d0 = _mm_min_epi16(d0, max0);
309
1.43M
    __m128i out0 = _mm_unpacklo_epi16(in1, d0);
310
1.43M
    __m128i out1 = _mm_unpackhi_epi16(in1, d0);
311
1.43M
    _mm_storeu_si128((__m128i *)&out[0], out0);
312
1.43M
    _mm_storeu_si128((__m128i *)&out[8], out1);
313
1.43M
    in0 = in8;
314
1.43M
    in8 = in16;
315
1.43M
    in16 = in24;
316
1.43M
    in24 = _mm_setzero_si128();
317
1.43M
    out += 16;
318
1.43M
    n -= 8;
319
1.43M
  }
320
752k
}
321
322
#endif  // CONFIG_AV1_HIGHBITDEPTH