summaryrefslogtreecommitdiffstats
path: root/third_party/aom/aom_dsp/arm/masked_sad_neon.c
blob: 9d263105e3bc292cfc8da35366ea3ce728228191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
95
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
/*
 * Copyright (c) 2023, Alliance for Open Media. All rights reserved
 *
 * 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.
 */

#include <arm_neon.h>

#include "config/aom_config.h"
#include "config/aom_dsp_rtcd.h"

#include "aom/aom_integer.h"
#include "aom_dsp/arm/blend_neon.h"
#include "aom_dsp/arm/mem_neon.h"
#include "aom_dsp/arm/sum_neon.h"
#include "aom_dsp/blend.h"

static INLINE uint16x8_t masked_sad_16x1_neon(uint16x8_t sad,
                                              const uint8_t *src,
                                              const uint8_t *a,
                                              const uint8_t *b,
                                              const uint8_t *m) {
  uint8x16_t m0 = vld1q_u8(m);
  uint8x16_t a0 = vld1q_u8(a);
  uint8x16_t b0 = vld1q_u8(b);
  uint8x16_t s0 = vld1q_u8(src);

  uint8x16_t blend_u8 = alpha_blend_a64_u8x16(m0, a0, b0);

  return vpadalq_u8(sad, vabdq_u8(blend_u8, s0));
}

static INLINE unsigned masked_sad_128xh_neon(const uint8_t *src, int src_stride,
                                             const uint8_t *a, int a_stride,
                                             const uint8_t *b, int b_stride,
                                             const uint8_t *m, int m_stride,
                                             int height) {
  // Eight accumulator vectors are required to avoid overflow in the 128x128
  // case.
  assert(height <= 128);
  uint16x8_t sad[] = { vdupq_n_u16(0), vdupq_n_u16(0), vdupq_n_u16(0),
                       vdupq_n_u16(0), vdupq_n_u16(0), vdupq_n_u16(0),
                       vdupq_n_u16(0), vdupq_n_u16(0) };

  do {
    sad[0] = masked_sad_16x1_neon(sad[0], &src[0], &a[0], &b[0], &m[0]);
    sad[1] = masked_sad_16x1_neon(sad[1], &src[16], &a[16], &b[16], &m[16]);
    sad[2] = masked_sad_16x1_neon(sad[2], &src[32], &a[32], &b[32], &m[32]);
    sad[3] = masked_sad_16x1_neon(sad[3], &src[48], &a[48], &b[48], &m[48]);
    sad[4] = masked_sad_16x1_neon(sad[4], &src[64], &a[64], &b[64], &m[64]);
    sad[5] = masked_sad_16x1_neon(sad[5], &src[80], &a[80], &b[80], &m[80]);
    sad[6] = masked_sad_16x1_neon(sad[6], &src[96], &a[96], &b[96], &m[96]);
    sad[7] = masked_sad_16x1_neon(sad[7], &src[112], &a[112], &b[112], &m[112]);

    src += src_stride;
    a += a_stride;
    b += b_stride;
    m += m_stride;
    height--;
  } while (height != 0);

  return horizontal_long_add_u16x8(sad[0], sad[1]) +
         horizontal_long_add_u16x8(sad[2], sad[3]) +
         horizontal_long_add_u16x8(sad[4], sad[5]) +
         horizontal_long_add_u16x8(sad[6], sad[7]);
}

static INLINE unsigned masked_sad_64xh_neon(const uint8_t *src, int src_stride,
                                            const uint8_t *a, int a_stride,
                                            const uint8_t *b, int b_stride,
                                            const uint8_t *m, int m_stride,
                                            int height) {
  // Four accumulator vectors are required to avoid overflow in the 64x128 case.
  assert(height <= 128);
  uint16x8_t sad[] = { vdupq_n_u16(0), vdupq_n_u16(0), vdupq_n_u16(0),
                       vdupq_n_u16(0) };

  do {
    sad[0] = masked_sad_16x1_neon(sad[0], &src[0], &a[0], &b[0], &m[0]);
    sad[1] = masked_sad_16x1_neon(sad[1], &src[16], &a[16], &b[16], &m[16]);
    sad[2] = masked_sad_16x1_neon(sad[2], &src[32], &a[32], &b[32], &m[32]);
    sad[3] = masked_sad_16x1_neon(sad[3], &src[48], &a[48], &b[48], &m[48]);

    src += src_stride;
    a += a_stride;
    b += b_stride;
    m += m_stride;
    height--;
  } while (height != 0);

  return horizontal_long_add_u16x8(sad[0], sad[1]) +
         horizontal_long_add_u16x8(sad[2], sad[3]);
}

static INLINE unsigned masked_sad_32xh_neon(const uint8_t *src, int src_stride,
                                            const uint8_t *a, int a_stride,
                                            const uint8_t *b, int b_stride,
                                            const uint8_t *m, int m_stride,
                                            int height) {
  // We could use a single accumulator up to height=64 without overflow.
  assert(height <= 64);
  uint16x8_t sad = vdupq_n_u16(0);

  do {
    sad = masked_sad_16x1_neon(sad, &src[0], &a[0], &b[0], &m[0]);
    sad = masked_sad_16x1_neon(sad, &src[16], &a[16], &b[16], &m[16]);

    src += src_stride;
    a += a_stride;
    b += b_stride;
    m += m_stride;
    height--;
  } while (height != 0);

  return horizontal_add_u16x8(sad);
}

static INLINE unsigned masked_sad_16xh_neon(const uint8_t *src, int src_stride,
                                            const uint8_t *a, int a_stride,
                                            const uint8_t *b, int b_stride,
                                            const uint8_t *m, int m_stride,
                                            int height) {
  // We could use a single accumulator up to height=128 without overflow.
  assert(height <= 128);
  uint16x8_t sad = vdupq_n_u16(0);

  do {
    sad = masked_sad_16x1_neon(sad, src, a, b, m);

    src += src_stride;
    a += a_stride;
    b += b_stride;
    m += m_stride;
    height--;
  } while (height != 0);

  return horizontal_add_u16x8(sad);
}

static INLINE unsigned masked_sad_8xh_neon(const uint8_t *src, int src_stride,
                                           const uint8_t *a, int a_stride,
                                           const uint8_t *b, int b_stride,
                                           const uint8_t *m, int m_stride,
                                           int height) {
  // We could use a single accumulator up to height=128 without overflow.
  assert(height <= 128);
  uint16x4_t sad = vdup_n_u16(0);

  do {
    uint8x8_t m0 = vld1_u8(m);
    uint8x8_t a0 = vld1_u8(a);
    uint8x8_t b0 = vld1_u8(b);
    uint8x8_t s0 = vld1_u8(src);

    uint8x8_t blend_u8 = alpha_blend_a64_u8x8(m0, a0, b0);

    sad = vpadal_u8(sad, vabd_u8(blend_u8, s0));

    src += src_stride;
    a += a_stride;
    b += b_stride;
    m += m_stride;
    height--;
  } while (height != 0);

  return horizontal_add_u16x4(sad);
}

static INLINE unsigned masked_sad_4xh_neon(const uint8_t *src, int src_stride,
                                           const uint8_t *a, int a_stride,
                                           const uint8_t *b, int b_stride,
                                           const uint8_t *m, int m_stride,
                                           int height) {
  // Process two rows per loop iteration.
  assert(height % 2 == 0);

  // We could use a single accumulator up to height=256 without overflow.
  assert(height <= 256);
  uint16x4_t sad = vdup_n_u16(0);

  do {
    uint8x8_t m0 = load_unaligned_u8(m, m_stride);
    uint8x8_t a0 = load_unaligned_u8(a, a_stride);
    uint8x8_t b0 = load_unaligned_u8(b, b_stride);
    uint8x8_t s0 = load_unaligned_u8(src, src_stride);

    uint8x8_t blend_u8 = alpha_blend_a64_u8x8(m0, a0, b0);

    sad = vpadal_u8(sad, vabd_u8(blend_u8, s0));

    src += 2 * src_stride;
    a += 2 * a_stride;
    b += 2 * b_stride;
    m += 2 * m_stride;
    height -= 2;
  } while (height != 0);

  return horizontal_add_u16x4(sad);
}

#define MASKED_SAD_WXH_NEON(width, height)                                    \
  unsigned aom_masked_sad##width##x##height##_neon(                           \
      const uint8_t *src, int src_stride, const uint8_t *ref, int ref_stride, \
      const uint8_t *second_pred, const uint8_t *msk, int msk_stride,         \
      int invert_mask) {                                                      \
    if (!invert_mask)                                                         \
      return masked_sad_##width##xh_neon(src, src_stride, ref, ref_stride,    \
                                         second_pred, width, msk, msk_stride, \
                                         height);                             \
    else                                                                      \
      return masked_sad_##width##xh_neon(src, src_stride, second_pred, width, \
                                         ref, ref_stride, msk, msk_stride,    \
                                         height);                             \
  }

MASKED_SAD_WXH_NEON(4, 4)
MASKED_SAD_WXH_NEON(4, 8)
MASKED_SAD_WXH_NEON(8, 4)
MASKED_SAD_WXH_NEON(8, 8)
MASKED_SAD_WXH_NEON(8, 16)
MASKED_SAD_WXH_NEON(16, 8)
MASKED_SAD_WXH_NEON(16, 16)
MASKED_SAD_WXH_NEON(16, 32)
MASKED_SAD_WXH_NEON(32, 16)
MASKED_SAD_WXH_NEON(32, 32)
MASKED_SAD_WXH_NEON(32, 64)
MASKED_SAD_WXH_NEON(64, 32)
MASKED_SAD_WXH_NEON(64, 64)
MASKED_SAD_WXH_NEON(64, 128)
MASKED_SAD_WXH_NEON(128, 64)
MASKED_SAD_WXH_NEON(128, 128)
#if !CONFIG_REALTIME_ONLY
MASKED_SAD_WXH_NEON(4, 16)
MASKED_SAD_WXH_NEON(16, 4)
MASKED_SAD_WXH_NEON(8, 32)
MASKED_SAD_WXH_NEON(32, 8)
MASKED_SAD_WXH_NEON(16, 64)
MASKED_SAD_WXH_NEON(64, 16)
#endif