filterintra_sse4.c 30.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11

12
13
#include <smmintrin.h>

Yaowu Xu's avatar
Yaowu Xu committed
14
#include "./av1_rtcd.h"
15
16
#include "aom_ports/mem.h"
#include "av1/common/enums.h"
hui su's avatar
hui su committed
17
#include "av1/common/reconintra.h"
18

19
20
21
22
23
#if USE_3TAP_INTRA_FILTER
void filterintra_sse4_3tap_dummy_func(void);
void filterintra_sse4_3tap_dummy_func(void) {}
#else

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
static INLINE void AddPixelsSmall(const uint8_t *above, const uint8_t *left,
                                  __m128i *sum) {
  const __m128i a = _mm_loadu_si128((const __m128i *)above);
  const __m128i l = _mm_loadu_si128((const __m128i *)left);
  const __m128i zero = _mm_setzero_si128();

  __m128i u0 = _mm_unpacklo_epi8(a, zero);
  __m128i u1 = _mm_unpacklo_epi8(l, zero);

  sum[0] = _mm_add_epi16(u0, u1);
}

static INLINE int GetMeanValue4x4(const uint8_t *above, const uint8_t *left,
                                  __m128i *params) {
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector, u;
  uint16_t sum_value;

  AddPixelsSmall(above, left, &sum_vector);

  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 2 values
  u = _mm_srli_si128(sum_vector, 2);
  sum_vector = _mm_add_epi16(sum_vector, u);

  sum_value = _mm_extract_epi16(sum_vector, 0);
  sum_value += 4;
  sum_value >>= 3;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

static INLINE int GetMeanValue8x8(const uint8_t *above, const uint8_t *left,
                                  __m128i *params) {
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector, u;
  uint16_t sum_value;

  AddPixelsSmall(above, left, &sum_vector);

  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 4 values
  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 2 values

  u = _mm_srli_si128(sum_vector, 2);
  sum_vector = _mm_add_epi16(sum_vector, u);

  sum_value = _mm_extract_epi16(sum_vector, 0);
  sum_value += 8;
  sum_value >>= 4;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

static INLINE void AddPixelsLarge(const uint8_t *above, const uint8_t *left,
                                  __m128i *sum) {
  const __m128i a = _mm_loadu_si128((const __m128i *)above);
  const __m128i l = _mm_loadu_si128((const __m128i *)left);
  const __m128i zero = _mm_setzero_si128();

  __m128i u0 = _mm_unpacklo_epi8(a, zero);
  __m128i u1 = _mm_unpacklo_epi8(l, zero);

  sum[0] = _mm_add_epi16(u0, u1);

  u0 = _mm_unpackhi_epi8(a, zero);
  u1 = _mm_unpackhi_epi8(l, zero);

  sum[0] = _mm_add_epi16(sum[0], u0);
  sum[0] = _mm_add_epi16(sum[0], u1);
}

static INLINE int GetMeanValue16x16(const uint8_t *above, const uint8_t *left,
clang-format's avatar
clang-format committed
95
                                    __m128i *params) {
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector, u;
  uint16_t sum_value;

  AddPixelsLarge(above, left, &sum_vector);

  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 4 values
  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 2 values

  u = _mm_srli_si128(sum_vector, 2);
  sum_vector = _mm_add_epi16(sum_vector, u);

  sum_value = _mm_extract_epi16(sum_vector, 0);
  sum_value += 16;
  sum_value >>= 5;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

static INLINE int GetMeanValue32x32(const uint8_t *above, const uint8_t *left,
                                    __m128i *params) {
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector[2], u;
  uint16_t sum_value;

  AddPixelsLarge(above, left, &sum_vector[0]);
  AddPixelsLarge(above + 16, left + 16, &sum_vector[1]);

  sum_vector[0] = _mm_add_epi16(sum_vector[0], sum_vector[1]);
  sum_vector[0] = _mm_hadd_epi16(sum_vector[0], zero);  // still has 4 values
  sum_vector[0] = _mm_hadd_epi16(sum_vector[0], zero);  // still has 2 values

  u = _mm_srli_si128(sum_vector[0], 2);
  sum_vector[0] = _mm_add_epi16(sum_vector[0], u);

  sum_value = _mm_extract_epi16(sum_vector[0], 0);
  sum_value += 32;
  sum_value >>= 6;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

// Note:
//  params[4] : mean value, 4 int32_t repetition
//
static INLINE int CalcRefPixelsMeanValue(const uint8_t *above,
clang-format's avatar
clang-format committed
142
143
                                         const uint8_t *left, int bs,
                                         __m128i *params) {
144
145
  int meanValue = 0;
  switch (bs) {
clang-format's avatar
clang-format committed
146
147
148
149
150
    case 4: meanValue = GetMeanValue4x4(above, left, params); break;
    case 8: meanValue = GetMeanValue8x8(above, left, params); break;
    case 16: meanValue = GetMeanValue16x16(above, left, params); break;
    case 32: meanValue = GetMeanValue32x32(above, left, params); break;
    default: assert(0);
151
152
153
154
155
156
157
158
  }
  return meanValue;
}

// Note:
//  params[0-3] : 4-tap filter coefficients (int32_t per coefficient)
//
static INLINE void GetIntraFilterParams(int bs, int mode, __m128i *params) {
clang-format's avatar
clang-format committed
159
160
161
  const TX_SIZE tx_size =
      (bs == 32) ? TX_32X32
                 : ((bs == 16) ? TX_16X16 : ((bs == 8) ? TX_8X8 : (TX_4X4)));
162
  // c0
hui su's avatar
hui su committed
163
164
165
166
  params[0] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][0],
                            av1_filter_intra_taps_4[tx_size][mode][0],
                            av1_filter_intra_taps_4[tx_size][mode][0],
                            av1_filter_intra_taps_4[tx_size][mode][0]);
167
  // c1
hui su's avatar
hui su committed
168
169
170
171
  params[1] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][1],
                            av1_filter_intra_taps_4[tx_size][mode][1],
                            av1_filter_intra_taps_4[tx_size][mode][1],
                            av1_filter_intra_taps_4[tx_size][mode][1]);
172
  // c2
hui su's avatar
hui su committed
173
174
175
176
  params[2] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][2],
                            av1_filter_intra_taps_4[tx_size][mode][2],
                            av1_filter_intra_taps_4[tx_size][mode][2],
                            av1_filter_intra_taps_4[tx_size][mode][2]);
177
  // c3
hui su's avatar
hui su committed
178
179
180
181
  params[3] = _mm_set_epi32(av1_filter_intra_taps_4[tx_size][mode][3],
                            av1_filter_intra_taps_4[tx_size][mode][3],
                            av1_filter_intra_taps_4[tx_size][mode][3],
                            av1_filter_intra_taps_4[tx_size][mode][3]);
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
}

static const int maxBlkSize = 32;

static INLINE void SavePred4x4(int *pred, const __m128i *mean, uint8_t *dst,
                               ptrdiff_t stride) {
  const int predStride = (maxBlkSize << 1) + 1;
  __m128i p0 = _mm_loadu_si128((const __m128i *)pred);
  __m128i p1 = _mm_loadu_si128((const __m128i *)(pred + predStride));
  __m128i p2 = _mm_loadu_si128((const __m128i *)(pred + 2 * predStride));
  __m128i p3 = _mm_loadu_si128((const __m128i *)(pred + 3 * predStride));

  p0 = _mm_add_epi32(p0, mean[0]);
  p1 = _mm_add_epi32(p1, mean[0]);
  p2 = _mm_add_epi32(p2, mean[0]);
  p3 = _mm_add_epi32(p3, mean[0]);

  p0 = _mm_packus_epi32(p0, p1);
  p1 = _mm_packus_epi32(p2, p3);
  p0 = _mm_packus_epi16(p0, p1);

  *((int *)dst) = _mm_cvtsi128_si32(p0);
  p0 = _mm_srli_si128(p0, 4);
  *((int *)(dst + stride)) = _mm_cvtsi128_si32(p0);
  p0 = _mm_srli_si128(p0, 4);
  *((int *)(dst + 2 * stride)) = _mm_cvtsi128_si32(p0);
  p0 = _mm_srli_si128(p0, 4);
  *((int *)(dst + 3 * stride)) = _mm_cvtsi128_si32(p0);
}

static void SavePred8x8(int *pred, const __m128i *mean, uint8_t *dst,
                        ptrdiff_t stride) {
  const int predStride = (maxBlkSize << 1) + 1;
  __m128i p0, p1, p2, p3;
  int r = 0;

  while (r < 8) {
    p0 = _mm_loadu_si128((const __m128i *)(pred + r * predStride));
    p1 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 4));
    r += 1;
    p2 = _mm_loadu_si128((const __m128i *)(pred + r * predStride));
    p3 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 4));

    p0 = _mm_add_epi32(p0, mean[0]);
    p1 = _mm_add_epi32(p1, mean[0]);
    p2 = _mm_add_epi32(p2, mean[0]);
    p3 = _mm_add_epi32(p3, mean[0]);

    p0 = _mm_packus_epi32(p0, p1);
    p1 = _mm_packus_epi32(p2, p3);
    p0 = _mm_packus_epi16(p0, p1);

    _mm_storel_epi64((__m128i *)dst, p0);
    dst += stride;
    p0 = _mm_srli_si128(p0, 8);
    _mm_storel_epi64((__m128i *)dst, p0);
    dst += stride;
    r += 1;
  }
}

static void SavePred16x16(int *pred, const __m128i *mean, uint8_t *dst,
                          ptrdiff_t stride) {
  const int predStride = (maxBlkSize << 1) + 1;
  __m128i p0, p1, p2, p3;
  int r = 0;

  while (r < 16) {
    p0 = _mm_loadu_si128((const __m128i *)(pred + r * predStride));
    p1 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 4));
    p2 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 8));
    p3 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 12));

    p0 = _mm_add_epi32(p0, mean[0]);
    p1 = _mm_add_epi32(p1, mean[0]);
    p2 = _mm_add_epi32(p2, mean[0]);
    p3 = _mm_add_epi32(p3, mean[0]);

    p0 = _mm_packus_epi32(p0, p1);
    p1 = _mm_packus_epi32(p2, p3);
    p0 = _mm_packus_epi16(p0, p1);

    _mm_storel_epi64((__m128i *)dst, p0);
    p0 = _mm_srli_si128(p0, 8);
    _mm_storel_epi64((__m128i *)(dst + 8), p0);
    dst += stride;
    r += 1;
  }
}

static void SavePred32x32(int *pred, const __m128i *mean, uint8_t *dst,
                          ptrdiff_t stride) {
  const int predStride = (maxBlkSize << 1) + 1;
  __m128i p0, p1, p2, p3, p4, p5, p6, p7;
  int r = 0;

  while (r < 32) {
    p0 = _mm_loadu_si128((const __m128i *)(pred + r * predStride));
    p1 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 4));
    p2 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 8));
    p3 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 12));

    p4 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 16));
    p5 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 20));
    p6 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 24));
    p7 = _mm_loadu_si128((const __m128i *)(pred + r * predStride + 28));

    p0 = _mm_add_epi32(p0, mean[0]);
    p1 = _mm_add_epi32(p1, mean[0]);
    p2 = _mm_add_epi32(p2, mean[0]);
    p3 = _mm_add_epi32(p3, mean[0]);

    p4 = _mm_add_epi32(p4, mean[0]);
    p5 = _mm_add_epi32(p5, mean[0]);
    p6 = _mm_add_epi32(p6, mean[0]);
    p7 = _mm_add_epi32(p7, mean[0]);

    p0 = _mm_packus_epi32(p0, p1);
    p1 = _mm_packus_epi32(p2, p3);
    p0 = _mm_packus_epi16(p0, p1);

    p4 = _mm_packus_epi32(p4, p5);
    p5 = _mm_packus_epi32(p6, p7);
    p4 = _mm_packus_epi16(p4, p5);

    _mm_storel_epi64((__m128i *)dst, p0);
    p0 = _mm_srli_si128(p0, 8);
    _mm_storel_epi64((__m128i *)(dst + 8), p0);

    _mm_storel_epi64((__m128i *)(dst + 16), p4);
    p4 = _mm_srli_si128(p4, 8);
    _mm_storel_epi64((__m128i *)(dst + 24), p4);

    dst += stride;
    r += 1;
  }
}

static void SavePrediction(int *pred, const __m128i *mean, int bs, uint8_t *dst,
                           ptrdiff_t stride) {
  switch (bs) {
clang-format's avatar
clang-format committed
323
324
325
326
327
    case 4: SavePred4x4(pred, mean, dst, stride); break;
    case 8: SavePred8x8(pred, mean, dst, stride); break;
    case 16: SavePred16x16(pred, mean, dst, stride); break;
    case 32: SavePred32x32(pred, mean, dst, stride); break;
    default: assert(0);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
  }
}

typedef void (*ProducePixelsFunc)(__m128i *p, const __m128i *prm, int *pred,
                                  const int predStride);

static void ProduceFourPixels(__m128i *p, const __m128i *prm, int *pred,
                              const int predStride) {
  __m128i u0, u1, u2;
  int c0 = _mm_extract_epi32(prm[1], 0);
  int x = *(pred + predStride);
  int sum;

  u0 = _mm_mullo_epi32(p[0], prm[2]);
  u1 = _mm_mullo_epi32(p[1], prm[0]);
  u2 = _mm_mullo_epi32(p[2], prm[3]);

  u0 = _mm_add_epi32(u0, u1);
  u0 = _mm_add_epi32(u0, u2);

  sum = _mm_extract_epi32(u0, 0);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 1) = x;

  sum = _mm_extract_epi32(u0, 1);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 2) = x;

  sum = _mm_extract_epi32(u0, 2);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 3) = x;

  sum = _mm_extract_epi32(u0, 3);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 4) = x;
}

static void ProduceThreePixels(__m128i *p, const __m128i *prm, int *pred,
                               const int predStride) {
  __m128i u0, u1, u2;
  int c0 = _mm_extract_epi32(prm[1], 0);
  int x = *(pred + predStride);
  int sum;

  u0 = _mm_mullo_epi32(p[0], prm[2]);
  u1 = _mm_mullo_epi32(p[1], prm[0]);
  u2 = _mm_mullo_epi32(p[2], prm[3]);

  u0 = _mm_add_epi32(u0, u1);
  u0 = _mm_add_epi32(u0, u2);

  sum = _mm_extract_epi32(u0, 0);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 1) = x;

  sum = _mm_extract_epi32(u0, 1);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 2) = x;

  sum = _mm_extract_epi32(u0, 2);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 3) = x;
}

static void ProduceTwoPixels(__m128i *p, const __m128i *prm, int *pred,
                             const int predStride) {
  __m128i u0, u1, u2;
  int c0 = _mm_extract_epi32(prm[1], 0);
  int x = *(pred + predStride);
  int sum;

  u0 = _mm_mullo_epi32(p[0], prm[2]);
  u1 = _mm_mullo_epi32(p[1], prm[0]);
  u2 = _mm_mullo_epi32(p[2], prm[3]);

  u0 = _mm_add_epi32(u0, u1);
  u0 = _mm_add_epi32(u0, u2);

  sum = _mm_extract_epi32(u0, 0);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 1) = x;

  sum = _mm_extract_epi32(u0, 1);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 2) = x;
}

static void ProduceOnePixels(__m128i *p, const __m128i *prm, int *pred,
                             const int predStride) {
  __m128i u0, u1, u2;
  int c0 = _mm_extract_epi32(prm[1], 0);
  int x = *(pred + predStride);
  int sum;

  u0 = _mm_mullo_epi32(p[0], prm[2]);
  u1 = _mm_mullo_epi32(p[1], prm[0]);
  u2 = _mm_mullo_epi32(p[2], prm[3]);

  u0 = _mm_add_epi32(u0, u1);
  u0 = _mm_add_epi32(u0, u2);

  sum = _mm_extract_epi32(u0, 0);
  sum += c0 * x;
  x = ROUND_POWER_OF_TWO_SIGNED(sum, FILTER_INTRA_PREC_BITS);
  *(pred + predStride + 1) = x;
}

static ProducePixelsFunc prodPixelsFuncTab[4] = {
clang-format's avatar
clang-format committed
445
446
  ProduceOnePixels, ProduceTwoPixels, ProduceThreePixels, ProduceFourPixels
};
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507

static void ProducePixels(int *pred, const __m128i *prm, int remain) {
  __m128i p[3];
  const int predStride = (maxBlkSize << 1) + 1;
  int index;

  p[0] = _mm_loadu_si128((const __m128i *)pred);
  p[1] = _mm_loadu_si128((const __m128i *)(pred + 1));
  p[2] = _mm_loadu_si128((const __m128i *)(pred + 2));

  if (remain <= 2) {
    return;
  }
  if (remain > 5) {
    index = 3;
  } else {
    index = remain - 3;
  }
  prodPixelsFuncTab[index](p, prm, pred, predStride);
}

// Note:
//  At column index c, the remaining pixels are R = 2 * bs + 1 - r - c
//  the number of pixels to produce is R - 2 = 2 * bs - r - c - 1
static void GeneratePrediction(const uint8_t *above, const uint8_t *left,
                               const int bs, const __m128i *prm, int meanValue,
                               uint8_t *dst, ptrdiff_t stride) {
  int pred[33][65];
  int r, c, colBound;
  int remainings;

  for (r = 0; r < bs; ++r) {
    pred[r + 1][0] = (int)left[r] - meanValue;
  }

  above -= 1;
  for (c = 0; c < 2 * bs + 1; ++c) {
    pred[0][c] = (int)above[c] - meanValue;
  }

  r = 0;
  c = 0;
  while (r < bs) {
    colBound = (bs << 1) - r;
    for (c = 0; c < colBound; c += 4) {
      remainings = colBound - c + 1;
      ProducePixels(&pred[r][c], prm, remainings);
    }
    r += 1;
  }

  SavePrediction(&pred[1][1], &prm[4], bs, dst, stride);
}

static void FilterPrediction(const uint8_t *above, const uint8_t *left, int bs,
                             __m128i *prm, uint8_t *dst, ptrdiff_t stride) {
  int meanValue = 0;
  meanValue = CalcRefPixelsMeanValue(above, left, bs, &prm[4]);
  GeneratePrediction(above, left, bs, prm, meanValue, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
508
509
void av1_dc_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                    const uint8_t *above, const uint8_t *left) {
510
511
512
513
514
  __m128i prm[5];
  GetIntraFilterParams(bs, DC_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
515
516
void av1_v_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                   const uint8_t *above, const uint8_t *left) {
517
518
519
520
521
  __m128i prm[5];
  GetIntraFilterParams(bs, V_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
522
523
void av1_h_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                   const uint8_t *above, const uint8_t *left) {
524
525
526
527
528
  __m128i prm[5];
  GetIntraFilterParams(bs, H_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
529
530
531
void av1_d45_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                     const uint8_t *above,
                                     const uint8_t *left) {
532
533
534
535
536
  __m128i prm[5];
  GetIntraFilterParams(bs, D45_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
537
538
539
void av1_d135_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                      const uint8_t *above,
                                      const uint8_t *left) {
540
541
542
543
544
  __m128i prm[5];
  GetIntraFilterParams(bs, D135_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
545
546
547
void av1_d117_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                      const uint8_t *above,
                                      const uint8_t *left) {
548
549
550
551
552
  __m128i prm[5];
  GetIntraFilterParams(bs, D117_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
553
554
555
void av1_d153_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                      const uint8_t *above,
                                      const uint8_t *left) {
556
557
558
559
560
  __m128i prm[5];
  GetIntraFilterParams(bs, D153_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
561
562
563
void av1_d207_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                      const uint8_t *above,
                                      const uint8_t *left) {
564
565
566
567
568
  __m128i prm[5];
  GetIntraFilterParams(bs, D207_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
569
570
571
void av1_d63_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                     const uint8_t *above,
                                     const uint8_t *left) {
572
573
574
575
576
  __m128i prm[5];
  GetIntraFilterParams(bs, D63_PRED, &prm[0]);
  FilterPrediction(above, left, bs, prm, dst, stride);
}

Urvang Joshi's avatar
Urvang Joshi committed
577
578
579
void av1_paeth_filter_predictor_sse4_1(uint8_t *dst, ptrdiff_t stride, int bs,
                                       const uint8_t *above,
                                       const uint8_t *left) {
580
  __m128i prm[5];
Urvang Joshi's avatar
Urvang Joshi committed
581
  GetIntraFilterParams(bs, PAETH_PRED, &prm[0]);
582
583
  FilterPrediction(above, left, bs, prm, dst, stride);
}
584
585

// ============== High Bit Depth ==============
586
#if CONFIG_HIGHBITDEPTH
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
static INLINE int HighbdGetMeanValue4x4(const uint16_t *above,
                                        const uint16_t *left, const int bd,
                                        __m128i *params) {
  const __m128i a = _mm_loadu_si128((const __m128i *)above);
  const __m128i l = _mm_loadu_si128((const __m128i *)left);
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector, u;
  uint16_t sum_value;
  (void)bd;

  sum_vector = _mm_add_epi16(a, l);

  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 2 values
  u = _mm_srli_si128(sum_vector, 2);
  sum_vector = _mm_add_epi16(sum_vector, u);

  sum_value = _mm_extract_epi16(sum_vector, 0);
  sum_value += 4;
  sum_value >>= 3;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

static INLINE int HighbdGetMeanValue8x8(const uint16_t *above,
                                        const uint16_t *left, const int bd,
                                        __m128i *params) {
  const __m128i a = _mm_loadu_si128((const __m128i *)above);
  const __m128i l = _mm_loadu_si128((const __m128i *)left);
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector, u;
  uint16_t sum_value;
  (void)bd;

  sum_vector = _mm_add_epi16(a, l);

  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 4 values
  sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 2 values

  u = _mm_srli_si128(sum_vector, 2);
  sum_vector = _mm_add_epi16(sum_vector, u);

  sum_value = _mm_extract_epi16(sum_vector, 0);
  sum_value += 8;
  sum_value >>= 4;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

// Note:
//  Process 16 pixels above and left, 10-bit depth
//  Add to the last 8 pixels sum
static INLINE void AddPixels10bit(const uint16_t *above, const uint16_t *left,
clang-format's avatar
clang-format committed
639
                                  __m128i *sum) {
640
641
642
643
644
645
646
647
648
649
650
651
652
  __m128i a = _mm_loadu_si128((const __m128i *)above);
  __m128i l = _mm_loadu_si128((const __m128i *)left);
  sum[0] = _mm_add_epi16(a, l);
  a = _mm_loadu_si128((const __m128i *)(above + 8));
  l = _mm_loadu_si128((const __m128i *)(left + 8));
  sum[0] = _mm_add_epi16(sum[0], a);
  sum[0] = _mm_add_epi16(sum[0], l);
}

// Note:
//  Process 16 pixels above and left, 12-bit depth
//  Add to the last 8 pixels sum
static INLINE void AddPixels12bit(const uint16_t *above, const uint16_t *left,
clang-format's avatar
clang-format committed
653
                                  __m128i *sum) {
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
  __m128i a = _mm_loadu_si128((const __m128i *)above);
  __m128i l = _mm_loadu_si128((const __m128i *)left);
  const __m128i zero = _mm_setzero_si128();
  __m128i v0, v1;

  v0 = _mm_unpacklo_epi16(a, zero);
  v1 = _mm_unpacklo_epi16(l, zero);
  sum[0] = _mm_add_epi32(v0, v1);

  v0 = _mm_unpackhi_epi16(a, zero);
  v1 = _mm_unpackhi_epi16(l, zero);
  sum[0] = _mm_add_epi32(sum[0], v0);
  sum[0] = _mm_add_epi32(sum[0], v1);

  a = _mm_loadu_si128((const __m128i *)(above + 8));
  l = _mm_loadu_si128((const __m128i *)(left + 8));

  v0 = _mm_unpacklo_epi16(a, zero);
  v1 = _mm_unpacklo_epi16(l, zero);
  sum[0] = _mm_add_epi32(sum[0], v0);
  sum[0] = _mm_add_epi32(sum[0], v1);

  v0 = _mm_unpackhi_epi16(a, zero);
  v1 = _mm_unpackhi_epi16(l, zero);
  sum[0] = _mm_add_epi32(sum[0], v0);
  sum[0] = _mm_add_epi32(sum[0], v1);
}

static INLINE int HighbdGetMeanValue16x16(const uint16_t *above,
                                          const uint16_t *left, const int bd,
                                          __m128i *params) {
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector, u;
  uint32_t sum_value = 0;

  if (10 == bd) {
    AddPixels10bit(above, left, &sum_vector);
    sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 4 values
    sum_vector = _mm_hadd_epi16(sum_vector, zero);  // still has 2 values

    u = _mm_srli_si128(sum_vector, 2);
    sum_vector = _mm_add_epi16(sum_vector, u);
    sum_value = _mm_extract_epi16(sum_vector, 0);
  } else if (12 == bd) {
    AddPixels12bit(above, left, &sum_vector);

    sum_vector = _mm_hadd_epi32(sum_vector, zero);
    u = _mm_srli_si128(sum_vector, 4);
    sum_vector = _mm_add_epi32(u, sum_vector);
    sum_value = _mm_extract_epi32(sum_vector, 0);
  }

  sum_value += 16;
  sum_value >>= 5;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

static INLINE int HighbdGetMeanValue32x32(const uint16_t *above,
                                          const uint16_t *left, const int bd,
                                          __m128i *params) {
  const __m128i zero = _mm_setzero_si128();
  __m128i sum_vector[2], u;
  uint32_t sum_value = 0;

  if (10 == bd) {
    AddPixels10bit(above, left, &sum_vector[0]);
    AddPixels10bit(above + 16, left + 16, &sum_vector[1]);

    sum_vector[0] = _mm_add_epi16(sum_vector[0], sum_vector[1]);
    sum_vector[0] = _mm_hadd_epi16(sum_vector[0], zero);  // still has 4 values
    sum_vector[0] = _mm_hadd_epi16(sum_vector[0], zero);  // still has 2 values

    u = _mm_srli_si128(sum_vector[0], 2);
    sum_vector[0] = _mm_add_epi16(sum_vector[0], u);
    sum_value = _mm_extract_epi16(sum_vector[0], 0);
  } else if (12 == bd) {
    AddPixels12bit(above, left, &sum_vector[0]);
    AddPixels12bit(above + 16, left + 16, &sum_vector[1]);

    sum_vector[0] = _mm_add_epi32(sum_vector[0], sum_vector[1]);
    sum_vector[0] = _mm_hadd_epi32(sum_vector[0], zero);
    u = _mm_srli_si128(sum_vector[0], 4);
    sum_vector[0] = _mm_add_epi32(u, sum_vector[0]);
    sum_value = _mm_extract_epi32(sum_vector[0], 0);
  }

  sum_value += 32;
  sum_value >>= 6;
  *params = _mm_set1_epi32(sum_value);
  return sum_value;
}

// Note:
//  params[4] : mean value, 4 int32_t repetition
//
static INLINE int HighbdCalcRefPixelsMeanValue(const uint16_t *above,
                                               const uint16_t *left, int bs,
                                               const int bd, __m128i *params) {
  int meanValue = 0;
  switch (bs) {
clang-format's avatar
clang-format committed
755
756
    case 4: meanValue = HighbdGetMeanValue4x4(above, left, bd, params); break;
    case 8: meanValue = HighbdGetMeanValue8x8(above, left, bd, params); break;
757
758
759
760
761
762
    case 16:
      meanValue = HighbdGetMeanValue16x16(above, left, bd, params);
      break;
    case 32:
      meanValue = HighbdGetMeanValue32x32(above, left, bd, params);
      break;
clang-format's avatar
clang-format committed
763
    default: assert(0);
764
765
766
767
768
769
770
771
  }
  return meanValue;
}

// Note:
//  At column index c, the remaining pixels are R = 2 * bs + 1 - r - c
//  the number of pixels to produce is R - 2 = 2 * bs - r - c - 1
static void HighbdGeneratePrediction(const uint16_t *above,
clang-format's avatar
clang-format committed
772
773
774
                                     const uint16_t *left, const int bs,
                                     const int bd, const __m128i *prm,
                                     int meanValue, uint16_t *dst,
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
                                     ptrdiff_t stride) {
  int pred[33][65];
  int r, c, colBound;
  int remainings;
  int ipred;

  for (r = 0; r < bs; ++r) {
    pred[r + 1][0] = (int)left[r] - meanValue;
  }

  above -= 1;
  for (c = 0; c < 2 * bs + 1; ++c) {
    pred[0][c] = (int)above[c] - meanValue;
  }

  r = 0;
  c = 0;
  while (r < bs) {
    colBound = (bs << 1) - r;
    for (c = 0; c < colBound; c += 4) {
      remainings = colBound - c + 1;
      ProducePixels(&pred[r][c], prm, remainings);
    }
    r += 1;
  }

  for (r = 0; r < bs; ++r) {
    for (c = 0; c < bs; ++c) {
      ipred = pred[r + 1][c + 1] + meanValue;
      dst[c] = clip_pixel_highbd(ipred, bd);
    }
    dst += stride;
  }
}

static void HighbdFilterPrediction(const uint16_t *above, const uint16_t *left,
                                   int bs, const int bd, __m128i *prm,
                                   uint16_t *dst, ptrdiff_t stride) {
  int meanValue = 0;
  meanValue = HighbdCalcRefPixelsMeanValue(above, left, bs, bd, &prm[4]);
  HighbdGeneratePrediction(above, left, bs, bd, prm, meanValue, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
818
819
820
void av1_highbd_dc_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                           int bs, const uint16_t *above,
                                           const uint16_t *left, int bd) {
821
822
823
824
825
  __m128i prm[5];
  GetIntraFilterParams(bs, DC_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
826
827
828
void av1_highbd_v_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                          int bs, const uint16_t *above,
                                          const uint16_t *left, int bd) {
829
830
831
832
833
  __m128i prm[5];
  GetIntraFilterParams(bs, V_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
834
835
836
void av1_highbd_h_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                          int bs, const uint16_t *above,
                                          const uint16_t *left, int bd) {
837
838
839
840
841
  __m128i prm[5];
  GetIntraFilterParams(bs, H_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
842
843
844
void av1_highbd_d45_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                            int bs, const uint16_t *above,
                                            const uint16_t *left, int bd) {
845
846
847
848
849
  __m128i prm[5];
  GetIntraFilterParams(bs, D45_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
850
851
852
void av1_highbd_d135_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                             int bs, const uint16_t *above,
                                             const uint16_t *left, int bd) {
853
854
855
856
857
  __m128i prm[5];
  GetIntraFilterParams(bs, D135_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
858
859
860
void av1_highbd_d117_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                             int bs, const uint16_t *above,
                                             const uint16_t *left, int bd) {
861
862
863
864
865
  __m128i prm[5];
  GetIntraFilterParams(bs, D117_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
866
867
868
void av1_highbd_d153_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                             int bs, const uint16_t *above,
                                             const uint16_t *left, int bd) {
869
870
871
872
873
  __m128i prm[5];
  GetIntraFilterParams(bs, D153_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
874
875
876
void av1_highbd_d207_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                             int bs, const uint16_t *above,
                                             const uint16_t *left, int bd) {
877
878
879
880
881
  __m128i prm[5];
  GetIntraFilterParams(bs, D207_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
882
883
884
void av1_highbd_d63_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                            int bs, const uint16_t *above,
                                            const uint16_t *left, int bd) {
885
886
887
888
889
  __m128i prm[5];
  GetIntraFilterParams(bs, D63_PRED, &prm[0]);
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}

Urvang Joshi's avatar
Urvang Joshi committed
890
891
892
void av1_highbd_paeth_filter_predictor_sse4_1(uint16_t *dst, ptrdiff_t stride,
                                              int bs, const uint16_t *above,
                                              const uint16_t *left, int bd) {
893
  __m128i prm[5];
Urvang Joshi's avatar
Urvang Joshi committed
894
  GetIntraFilterParams(bs, PAETH_PRED, &prm[0]);
895
896
  HighbdFilterPrediction(above, left, bs, bd, prm, dst, stride);
}
897
#endif  // CONFIG_HIGHBITDEPTH
898
899

#endif  // USE_3TAP_INTRA_FILTER