aboutsummaryrefslogtreecommitdiff
path: root/vpx_dsp/arm/highbd_variance_neon.c
blob: 309ae7fd35630f8a04902d949e61c62e11e38271 (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
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
323
324
325
326
327
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
/*
 *  Copyright (c) 2022 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <arm_neon.h>

#include "./vpx_dsp_rtcd.h"
#include "./vpx_config.h"

#include "vpx/vpx_integer.h"
#include "vpx_dsp/arm/mem_neon.h"
#include "vpx_dsp/arm/sum_neon.h"
#include "vpx_ports/mem.h"

// Process a block of width 4 two rows at a time.
static INLINE void highbd_variance_4xh_neon(const uint16_t *src_ptr,
                                            int src_stride,
                                            const uint16_t *ref_ptr,
                                            int ref_stride, int h,
                                            uint64_t *sse, int64_t *sum) {
  int16x8_t sum_s16 = vdupq_n_s16(0);
  int32x4_t sse_s32 = vdupq_n_s32(0);

  int i = h;
  do {
    const uint16x8_t s = load_unaligned_u16q(src_ptr, src_stride);
    const uint16x8_t r = load_unaligned_u16q(ref_ptr, ref_stride);

    int16x8_t diff = vreinterpretq_s16_u16(vsubq_u16(s, r));
    sum_s16 = vaddq_s16(sum_s16, diff);

    sse_s32 = vmlal_s16(sse_s32, vget_low_s16(diff), vget_low_s16(diff));
    sse_s32 = vmlal_s16(sse_s32, vget_high_s16(diff), vget_high_s16(diff));

    src_ptr += 2 * src_stride;
    ref_ptr += 2 * ref_stride;
    i -= 2;
  } while (i != 0);

  *sum = horizontal_add_int16x8(sum_s16);
  *sse = horizontal_add_int32x4(sse_s32);
}

// For 8-bit and 10-bit data, since we're using two int32x4 accumulators, all
// block sizes can be processed in 32-bit elements (1023*1023*64*16 = 1071645696
// for a 64x64 block).
static INLINE void highbd_variance_large_neon(const uint16_t *src_ptr,
                                              int src_stride,
                                              const uint16_t *ref_ptr,
                                              int ref_stride, int w, int h,
                                              uint64_t *sse, int64_t *sum) {
  int32x4_t sum_s32 = vdupq_n_s32(0);
  int32x4_t sse_s32[2] = { vdupq_n_s32(0), vdupq_n_s32(0) };

  int i = h;
  do {
    int j = 0;
    do {
      const uint16x8_t s = vld1q_u16(src_ptr + j);
      const uint16x8_t r = vld1q_u16(ref_ptr + j);

      const int16x8_t diff = vreinterpretq_s16_u16(vsubq_u16(s, r));
      sum_s32 = vpadalq_s16(sum_s32, diff);

      sse_s32[0] =
          vmlal_s16(sse_s32[0], vget_low_s16(diff), vget_low_s16(diff));
      sse_s32[1] =
          vmlal_s16(sse_s32[1], vget_high_s16(diff), vget_high_s16(diff));

      j += 8;
    } while (j < w);

    src_ptr += src_stride;
    ref_ptr += ref_stride;
  } while (--i != 0);

  *sum = horizontal_add_int32x4(sum_s32);
  *sse = horizontal_long_add_uint32x4(vaddq_u32(
      vreinterpretq_u32_s32(sse_s32[0]), vreinterpretq_u32_s32(sse_s32[1])));
}

static INLINE void highbd_variance_8xh_neon(const uint16_t *src, int src_stride,
                                            const uint16_t *ref, int ref_stride,
                                            int h, uint64_t *sse,
                                            int64_t *sum) {
  highbd_variance_large_neon(src, src_stride, ref, ref_stride, 8, h, sse, sum);
}

static INLINE void highbd_variance_16xh_neon(const uint16_t *src,
                                             int src_stride,
                                             const uint16_t *ref,
                                             int ref_stride, int h,
                                             uint64_t *sse, int64_t *sum) {
  highbd_variance_large_neon(src, src_stride, ref, ref_stride, 16, h, sse, sum);
}

static INLINE void highbd_variance_32xh_neon(const uint16_t *src,
                                             int src_stride,
                                             const uint16_t *ref,
                                             int ref_stride, int h,
                                             uint64_t *sse, int64_t *sum) {
  highbd_variance_large_neon(src, src_stride, ref, ref_stride, 32, h, sse, sum);
}

static INLINE void highbd_variance_64xh_neon(const uint16_t *src,
                                             int src_stride,
                                             const uint16_t *ref,
                                             int ref_stride, int h,
                                             uint64_t *sse, int64_t *sum) {
  highbd_variance_large_neon(src, src_stride, ref, ref_stride, 64, h, sse, sum);
}

// For 12-bit data, we can only accumulate up to 128 elements in the sum of
// squares (4095*4095*128 = 2146435200), and because we're using two int32x4
// accumulators, we can only process up to 32 32-element rows (32*32/8 = 128)
// or 16 64-element rows before we have to accumulate into 64-bit elements.
// Therefore blocks of size 32x64, 64x32 and 64x64 are processed in a different
// helper function.

// Process a block of any size where the width is divisible by 8, with
// accumulation into 64-bit elements.
static INLINE void highbd_variance_xlarge_neon(
    const uint16_t *src_ptr, int src_stride, const uint16_t *ref_ptr,
    int ref_stride, int w, int h, int h_limit, uint64_t *sse, int64_t *sum) {
  int32x4_t sum_s32 = vdupq_n_s32(0);
  int64x2_t sse_s64 = vdupq_n_s64(0);

  // 'h_limit' is the number of 'w'-width rows we can process before our 32-bit
  // accumulator overflows. After hitting this limit we accumulate into 64-bit
  // elements.
  int h_tmp = h > h_limit ? h_limit : h;

  int i = 0;
  do {
    int32x4_t sse_s32[2] = { vdupq_n_s32(0), vdupq_n_s32(0) };
    do {
      int j = 0;
      do {
        const uint16x8_t s0 = vld1q_u16(src_ptr + j);
        const uint16x8_t r0 = vld1q_u16(ref_ptr + j);

        const int16x8_t diff = vreinterpretq_s16_u16(vsubq_u16(s0, r0));
        sum_s32 = vpadalq_s16(sum_s32, diff);

        sse_s32[0] =
            vmlal_s16(sse_s32[0], vget_low_s16(diff), vget_low_s16(diff));
        sse_s32[1] =
            vmlal_s16(sse_s32[1], vget_high_s16(diff), vget_high_s16(diff));

        j += 8;
      } while (j < w);

      src_ptr += src_stride;
      ref_ptr += ref_stride;
      i++;
    } while (i < h_tmp);

    sse_s64 = vpadalq_s32(sse_s64, sse_s32[0]);
    sse_s64 = vpadalq_s32(sse_s64, sse_s32[1]);
    h_tmp += h_limit;
  } while (i < h);

  *sum = horizontal_add_int32x4(sum_s32);
  *sse = (uint64_t)horizontal_add_int64x2(sse_s64);
}

static INLINE void highbd_variance_32xh_xlarge_neon(
    const uint16_t *src, int src_stride, const uint16_t *ref, int ref_stride,
    int h, uint64_t *sse, int64_t *sum) {
  highbd_variance_xlarge_neon(src, src_stride, ref, ref_stride, 32, h, 32, sse,
                              sum);
}

static INLINE void highbd_variance_64xh_xlarge_neon(
    const uint16_t *src, int src_stride, const uint16_t *ref, int ref_stride,
    int h, uint64_t *sse, int64_t *sum) {
  highbd_variance_xlarge_neon(src, src_stride, ref, ref_stride, 64, h, 16, sse,
                              sum);
}

#define HBD_VARIANCE_WXH_8_NEON(w, h)                                 \
  uint32_t vpx_highbd_8_variance##w##x##h##_neon(                     \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr, \
      int ref_stride, uint32_t *sse) {                                \
    int sum;                                                          \
    uint64_t sse_long = 0;                                            \
    int64_t sum_long = 0;                                             \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                     \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                     \
    highbd_variance_##w##xh_neon(src, src_stride, ref, ref_stride, h, \
                                 &sse_long, &sum_long);               \
    *sse = (uint32_t)sse_long;                                        \
    sum = (int)sum_long;                                              \
    return *sse - (uint32_t)(((int64_t)sum * sum) / (w * h));         \
  }

#define HBD_VARIANCE_WXH_10_NEON(w, h)                                \
  uint32_t vpx_highbd_10_variance##w##x##h##_neon(                    \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr, \
      int ref_stride, uint32_t *sse) {                                \
    int sum;                                                          \
    int64_t var;                                                      \
    uint64_t sse_long = 0;                                            \
    int64_t sum_long = 0;                                             \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                     \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                     \
    highbd_variance_##w##xh_neon(src, src_stride, ref, ref_stride, h, \
                                 &sse_long, &sum_long);               \
    *sse = (uint32_t)ROUND_POWER_OF_TWO(sse_long, 4);                 \
    sum = (int)ROUND_POWER_OF_TWO(sum_long, 2);                       \
    var = (int64_t)(*sse) - (((int64_t)sum * sum) / (w * h));         \
    return (var >= 0) ? (uint32_t)var : 0;                            \
  }

#define HBD_VARIANCE_WXH_12_NEON(w, h)                                \
  uint32_t vpx_highbd_12_variance##w##x##h##_neon(                    \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr, \
      int ref_stride, uint32_t *sse) {                                \
    int sum;                                                          \
    int64_t var;                                                      \
    uint64_t sse_long = 0;                                            \
    int64_t sum_long = 0;                                             \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                     \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                     \
    highbd_variance_##w##xh_neon(src, src_stride, ref, ref_stride, h, \
                                 &sse_long, &sum_long);               \
    *sse = (uint32_t)ROUND_POWER_OF_TWO(sse_long, 8);                 \
    sum = (int)ROUND_POWER_OF_TWO(sum_long, 4);                       \
    var = (int64_t)(*sse) - (((int64_t)sum * sum) / (w * h));         \
    return (var >= 0) ? (uint32_t)var : 0;                            \
  }

#define HBD_VARIANCE_WXH_12_XLARGE_NEON(w, h)                                \
  uint32_t vpx_highbd_12_variance##w##x##h##_neon(                           \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr,        \
      int ref_stride, uint32_t *sse) {                                       \
    int sum;                                                                 \
    int64_t var;                                                             \
    uint64_t sse_long = 0;                                                   \
    int64_t sum_long = 0;                                                    \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                            \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                            \
    highbd_variance_##w##xh_xlarge_neon(src, src_stride, ref, ref_stride, h, \
                                        &sse_long, &sum_long);               \
    *sse = (uint32_t)ROUND_POWER_OF_TWO(sse_long, 8);                        \
    sum = (int)ROUND_POWER_OF_TWO(sum_long, 4);                              \
    var = (int64_t)(*sse) - (((int64_t)sum * sum) / (w * h));                \
    return (var >= 0) ? (uint32_t)var : 0;                                   \
  }

// 8-bit
HBD_VARIANCE_WXH_8_NEON(4, 4)
HBD_VARIANCE_WXH_8_NEON(4, 8)

HBD_VARIANCE_WXH_8_NEON(8, 4)
HBD_VARIANCE_WXH_8_NEON(8, 8)
HBD_VARIANCE_WXH_8_NEON(8, 16)

HBD_VARIANCE_WXH_8_NEON(16, 8)
HBD_VARIANCE_WXH_8_NEON(16, 16)
HBD_VARIANCE_WXH_8_NEON(16, 32)

HBD_VARIANCE_WXH_8_NEON(32, 16)
HBD_VARIANCE_WXH_8_NEON(32, 32)
HBD_VARIANCE_WXH_8_NEON(32, 64)

HBD_VARIANCE_WXH_8_NEON(64, 32)
HBD_VARIANCE_WXH_8_NEON(64, 64)

// 10-bit
HBD_VARIANCE_WXH_10_NEON(4, 4)
HBD_VARIANCE_WXH_10_NEON(4, 8)

HBD_VARIANCE_WXH_10_NEON(8, 4)
HBD_VARIANCE_WXH_10_NEON(8, 8)
HBD_VARIANCE_WXH_10_NEON(8, 16)

HBD_VARIANCE_WXH_10_NEON(16, 8)
HBD_VARIANCE_WXH_10_NEON(16, 16)
HBD_VARIANCE_WXH_10_NEON(16, 32)

HBD_VARIANCE_WXH_10_NEON(32, 16)
HBD_VARIANCE_WXH_10_NEON(32, 32)
HBD_VARIANCE_WXH_10_NEON(32, 64)

HBD_VARIANCE_WXH_10_NEON(64, 32)
HBD_VARIANCE_WXH_10_NEON(64, 64)

// 12-bit
HBD_VARIANCE_WXH_12_NEON(4, 4)
HBD_VARIANCE_WXH_12_NEON(4, 8)

HBD_VARIANCE_WXH_12_NEON(8, 4)
HBD_VARIANCE_WXH_12_NEON(8, 8)
HBD_VARIANCE_WXH_12_NEON(8, 16)

HBD_VARIANCE_WXH_12_NEON(16, 8)
HBD_VARIANCE_WXH_12_NEON(16, 16)
HBD_VARIANCE_WXH_12_NEON(16, 32)

HBD_VARIANCE_WXH_12_NEON(32, 16)
HBD_VARIANCE_WXH_12_NEON(32, 32)
HBD_VARIANCE_WXH_12_XLARGE_NEON(32, 64)

HBD_VARIANCE_WXH_12_XLARGE_NEON(64, 32)
HBD_VARIANCE_WXH_12_XLARGE_NEON(64, 64)

#define HIGHBD_GET_VAR(S)                                             \
  void vpx_highbd_8_get##S##x##S##var_neon(                           \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr, \
      int ref_stride, uint32_t *sse, int *sum) {                      \
    uint64_t sse_long = 0;                                            \
    int64_t sum_long = 0;                                             \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                     \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                     \
    highbd_variance_##S##xh_neon(src, src_stride, ref, ref_stride, S, \
                                 &sse_long, &sum_long);               \
    *sse = (uint32_t)sse_long;                                        \
    *sum = (int)sum_long;                                             \
  }                                                                   \
                                                                      \
  void vpx_highbd_10_get##S##x##S##var_neon(                          \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr, \
      int ref_stride, uint32_t *sse, int *sum) {                      \
    uint64_t sse_long = 0;                                            \
    int64_t sum_long = 0;                                             \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                     \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                     \
    highbd_variance_##S##xh_neon(src, src_stride, ref, ref_stride, S, \
                                 &sse_long, &sum_long);               \
    *sse = (uint32_t)ROUND_POWER_OF_TWO(sse_long, 4);                 \
    *sum = (int)ROUND_POWER_OF_TWO(sum_long, 2);                      \
  }                                                                   \
                                                                      \
  void vpx_highbd_12_get##S##x##S##var_neon(                          \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr, \
      int ref_stride, uint32_t *sse, int *sum) {                      \
    uint64_t sse_long = 0;                                            \
    int64_t sum_long = 0;                                             \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                     \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                     \
    highbd_variance_##S##xh_neon(src, src_stride, ref, ref_stride, S, \
                                 &sse_long, &sum_long);               \
    *sse = (uint32_t)ROUND_POWER_OF_TWO(sse_long, 8);                 \
    *sum = (int)ROUND_POWER_OF_TWO(sum_long, 4);                      \
  }

HIGHBD_GET_VAR(8)
HIGHBD_GET_VAR(16)

static INLINE uint32_t highbd_mse_wxh_neon(const uint16_t *src_ptr,
                                           int src_stride,
                                           const uint16_t *ref_ptr,
                                           int ref_stride, int w, int h) {
  uint32x4_t sse_u32[2] = { vdupq_n_u32(0), vdupq_n_u32(0) };

  int i = h;
  do {
    int j = 0;
    do {
      uint16x8_t s = vld1q_u16(src_ptr + j);
      uint16x8_t r = vld1q_u16(ref_ptr + j);

      uint16x8_t diff = vabdq_u16(s, r);

      sse_u32[0] =
          vmlal_u16(sse_u32[0], vget_low_u16(diff), vget_low_u16(diff));
      sse_u32[1] =
          vmlal_u16(sse_u32[1], vget_high_u16(diff), vget_high_u16(diff));

      j += 8;
    } while (j < w);

    src_ptr += src_stride;
    ref_ptr += ref_stride;
  } while (--i != 0);

  return horizontal_add_uint32x4(vaddq_u32(sse_u32[0], sse_u32[1]));
}

static INLINE uint32_t highbd_mse8_8xh_neon(const uint16_t *src_ptr,
                                            int src_stride,
                                            const uint16_t *ref_ptr,
                                            int ref_stride, int h) {
  return highbd_mse_wxh_neon(src_ptr, src_stride, ref_ptr, ref_stride, 8, h);
}

static INLINE uint32_t highbd_mse8_16xh_neon(const uint16_t *src_ptr,
                                             int src_stride,
                                             const uint16_t *ref_ptr,
                                             int ref_stride, int h) {
  return highbd_mse_wxh_neon(src_ptr, src_stride, ref_ptr, ref_stride, 16, h);
}

#define HIGHBD_MSE_WXH_NEON(w, h)                                         \
  uint32_t vpx_highbd_8_mse##w##x##h##_neon(                              \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr,     \
      int ref_stride, uint32_t *sse) {                                    \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                         \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                         \
    *sse = highbd_mse8_##w##xh_neon(src, src_stride, ref, ref_stride, h); \
    return *sse;                                                          \
  }                                                                       \
                                                                          \
  uint32_t vpx_highbd_10_mse##w##x##h##_neon(                             \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr,     \
      int ref_stride, uint32_t *sse) {                                    \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                         \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                         \
    *sse = highbd_mse_wxh_neon(src, src_stride, ref, ref_stride, w, h);   \
    *sse = ROUND_POWER_OF_TWO(*sse, 4);                                   \
    return *sse;                                                          \
  }                                                                       \
                                                                          \
  uint32_t vpx_highbd_12_mse##w##x##h##_neon(                             \
      const uint8_t *src_ptr, int src_stride, const uint8_t *ref_ptr,     \
      int ref_stride, uint32_t *sse) {                                    \
    uint16_t *src = CONVERT_TO_SHORTPTR(src_ptr);                         \
    uint16_t *ref = CONVERT_TO_SHORTPTR(ref_ptr);                         \
    *sse = highbd_mse_wxh_neon(src, src_stride, ref, ref_stride, w, h);   \
    *sse = ROUND_POWER_OF_TWO(*sse, 8);                                   \
    return *sse;                                                          \
  }

HIGHBD_MSE_WXH_NEON(16, 16)
HIGHBD_MSE_WXH_NEON(16, 8)
HIGHBD_MSE_WXH_NEON(8, 16)
HIGHBD_MSE_WXH_NEON(8, 8)

#undef HIGHBD_MSE_WXH_NEON