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
437
438
439
440
441
442
443
444
445
446
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
|
/*
* Copyright (c) 2016, 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 <assert.h>
#include "config/aom_config.h"
#include "config/av1_rtcd.h"
#include "aom_dsp/arm/mem_neon.h"
#include "aom_dsp/arm/sum_neon.h"
#include "av1/common/cdef_block.h"
void cdef_copy_rect8_8bit_to_16bit_neon(uint16_t *dst, int dstride,
const uint8_t *src, int sstride,
int width, int height) {
do {
const uint8_t *src_ptr = src;
uint16_t *dst_ptr = dst;
int w = 0;
while (width - w >= 16) {
uint8x16_t row = vld1q_u8(src_ptr + w);
uint8x16x2_t row_u16 = { { row, vdupq_n_u8(0) } };
vst2q_u8((uint8_t *)(dst_ptr + w), row_u16);
w += 16;
}
if (width - w >= 8) {
uint8x8_t row = vld1_u8(src_ptr + w);
vst1q_u16(dst_ptr + w, vmovl_u8(row));
w += 8;
}
if (width - w == 4) {
for (int i = w; i < w + 4; i++) {
dst_ptr[i] = src_ptr[i];
}
}
src += sstride;
dst += dstride;
} while (--height != 0);
}
void cdef_copy_rect8_16bit_to_16bit_neon(uint16_t *dst, int dstride,
const uint16_t *src, int sstride,
int width, int height) {
do {
const uint16_t *src_ptr = src;
uint16_t *dst_ptr = dst;
int w = 0;
while (width - w >= 8) {
uint16x8_t row = vld1q_u16(src_ptr + w);
vst1q_u16(dst_ptr + w, row);
w += 8;
}
if (width - w == 4) {
uint16x4_t row = vld1_u16(src_ptr + w);
vst1_u16(dst_ptr + w, row);
}
src += sstride;
dst += dstride;
} while (--height != 0);
}
// partial A is a 16-bit vector of the form:
// [x8 x7 x6 x5 x4 x3 x2 x1] and partial B has the form:
// [0 y1 y2 y3 y4 y5 y6 y7].
// This function computes (x1^2+y1^2)*C1 + (x2^2+y2^2)*C2 + ...
// (x7^2+y2^7)*C7 + (x8^2+0^2)*C8 where the C1..C8 constants are in const1
// and const2.
static INLINE uint32x4_t fold_mul_and_sum_neon(int16x8_t partiala,
int16x8_t partialb,
uint32x4_t const1,
uint32x4_t const2) {
// Reverse partial B.
// pattern = { 12 13 10 11 8 9 6 7 4 5 2 3 0 1 14 15 }.
uint8x16_t pattern = vreinterpretq_u8_u64(
vcombine_u64(vcreate_u64((uint64_t)0x07060908 << 32 | 0x0b0a0d0c),
vcreate_u64((uint64_t)0x0f0e0100 << 32 | 0x03020504)));
#if AOM_ARCH_AARCH64
partialb =
vreinterpretq_s16_s8(vqtbl1q_s8(vreinterpretq_s8_s16(partialb), pattern));
#else
int8x8x2_t p = { { vget_low_s8(vreinterpretq_s8_s16(partialb)),
vget_high_s8(vreinterpretq_s8_s16(partialb)) } };
int8x8_t shuffle_hi = vtbl2_s8(p, vget_high_s8(vreinterpretq_s8_u8(pattern)));
int8x8_t shuffle_lo = vtbl2_s8(p, vget_low_s8(vreinterpretq_s8_u8(pattern)));
partialb = vreinterpretq_s16_s8(vcombine_s8(shuffle_lo, shuffle_hi));
#endif
// Square and add the corresponding x and y values.
int32x4_t cost_lo = vmull_s16(vget_low_s16(partiala), vget_low_s16(partiala));
cost_lo = vmlal_s16(cost_lo, vget_low_s16(partialb), vget_low_s16(partialb));
int32x4_t cost_hi =
vmull_s16(vget_high_s16(partiala), vget_high_s16(partiala));
cost_hi =
vmlal_s16(cost_hi, vget_high_s16(partialb), vget_high_s16(partialb));
// Multiply by constant.
uint32x4_t cost = vmulq_u32(vreinterpretq_u32_s32(cost_lo), const1);
cost = vmlaq_u32(cost, vreinterpretq_u32_s32(cost_hi), const2);
return cost;
}
// This function computes the cost along directions 4, 5, 6, 7. (4 is diagonal
// down-right, 6 is vertical).
//
// For each direction the lines are shifted so that we can perform a
// basic sum on each vector element. For example, direction 5 is "south by
// southeast", so we need to add the pixels along each line i below:
//
// 0 1 2 3 4 5 6 7
// 0 1 2 3 4 5 6 7
// 8 0 1 2 3 4 5 6
// 8 0 1 2 3 4 5 6
// 9 8 0 1 2 3 4 5
// 9 8 0 1 2 3 4 5
// 10 9 8 0 1 2 3 4
// 10 9 8 0 1 2 3 4
//
// For this to fit nicely in vectors, the lines need to be shifted like so:
// 0 1 2 3 4 5 6 7
// 0 1 2 3 4 5 6 7
// 8 0 1 2 3 4 5 6
// 8 0 1 2 3 4 5 6
// 9 8 0 1 2 3 4 5
// 9 8 0 1 2 3 4 5
// 10 9 8 0 1 2 3 4
// 10 9 8 0 1 2 3 4
//
// In this configuration we can now perform SIMD additions to get the cost
// along direction 5. Since this won't fit into a single 128-bit vector, we use
// two of them to compute each half of the new configuration, and pad the empty
// spaces with zeros. Similar shifting is done for other directions, except
// direction 6 which is straightforward as it's the vertical direction.
static INLINE uint32x4_t compute_vert_directions_neon(int16x8_t lines[8],
uint32_t cost[4]) {
const int16x8_t zero = vdupq_n_s16(0);
// Partial sums for lines 0 and 1.
int16x8_t partial4a = vextq_s16(zero, lines[0], 1);
partial4a = vaddq_s16(partial4a, vextq_s16(zero, lines[1], 2));
int16x8_t partial4b = vextq_s16(lines[0], zero, 1);
partial4b = vaddq_s16(partial4b, vextq_s16(lines[1], zero, 2));
int16x8_t tmp = vaddq_s16(lines[0], lines[1]);
int16x8_t partial5a = vextq_s16(zero, tmp, 3);
int16x8_t partial5b = vextq_s16(tmp, zero, 3);
int16x8_t partial7a = vextq_s16(zero, tmp, 6);
int16x8_t partial7b = vextq_s16(tmp, zero, 6);
int16x8_t partial6 = tmp;
// Partial sums for lines 2 and 3.
partial4a = vaddq_s16(partial4a, vextq_s16(zero, lines[2], 3));
partial4a = vaddq_s16(partial4a, vextq_s16(zero, lines[3], 4));
partial4b = vaddq_s16(partial4b, vextq_s16(lines[2], zero, 3));
partial4b = vaddq_s16(partial4b, vextq_s16(lines[3], zero, 4));
tmp = vaddq_s16(lines[2], lines[3]);
partial5a = vaddq_s16(partial5a, vextq_s16(zero, tmp, 4));
partial5b = vaddq_s16(partial5b, vextq_s16(tmp, zero, 4));
partial7a = vaddq_s16(partial7a, vextq_s16(zero, tmp, 5));
partial7b = vaddq_s16(partial7b, vextq_s16(tmp, zero, 5));
partial6 = vaddq_s16(partial6, tmp);
// Partial sums for lines 4 and 5.
partial4a = vaddq_s16(partial4a, vextq_s16(zero, lines[4], 5));
partial4a = vaddq_s16(partial4a, vextq_s16(zero, lines[5], 6));
partial4b = vaddq_s16(partial4b, vextq_s16(lines[4], zero, 5));
partial4b = vaddq_s16(partial4b, vextq_s16(lines[5], zero, 6));
tmp = vaddq_s16(lines[4], lines[5]);
partial5a = vaddq_s16(partial5a, vextq_s16(zero, tmp, 5));
partial5b = vaddq_s16(partial5b, vextq_s16(tmp, zero, 5));
partial7a = vaddq_s16(partial7a, vextq_s16(zero, tmp, 4));
partial7b = vaddq_s16(partial7b, vextq_s16(tmp, zero, 4));
partial6 = vaddq_s16(partial6, tmp);
// Partial sums for lines 6 and 7.
partial4a = vaddq_s16(partial4a, vextq_s16(zero, lines[6], 7));
partial4a = vaddq_s16(partial4a, lines[7]);
partial4b = vaddq_s16(partial4b, vextq_s16(lines[6], zero, 7));
tmp = vaddq_s16(lines[6], lines[7]);
partial5a = vaddq_s16(partial5a, vextq_s16(zero, tmp, 6));
partial5b = vaddq_s16(partial5b, vextq_s16(tmp, zero, 6));
partial7a = vaddq_s16(partial7a, vextq_s16(zero, tmp, 3));
partial7b = vaddq_s16(partial7b, vextq_s16(tmp, zero, 3));
partial6 = vaddq_s16(partial6, tmp);
uint32x4_t const0 = vreinterpretq_u32_u64(
vcombine_u64(vcreate_u64((uint64_t)420 << 32 | 840),
vcreate_u64((uint64_t)210 << 32 | 280)));
uint32x4_t const1 = vreinterpretq_u32_u64(
vcombine_u64(vcreate_u64((uint64_t)140 << 32 | 168),
vcreate_u64((uint64_t)105 << 32 | 120)));
uint32x4_t const2 = vreinterpretq_u32_u64(
vcombine_u64(vcreate_u64(0), vcreate_u64((uint64_t)210 << 32 | 420)));
uint32x4_t const3 = vreinterpretq_u32_u64(
vcombine_u64(vcreate_u64((uint64_t)105 << 32 | 140),
vcreate_u64((uint64_t)105 << 32 | 105)));
// Compute costs in terms of partial sums.
int32x4_t partial6_s32 =
vmull_s16(vget_low_s16(partial6), vget_low_s16(partial6));
partial6_s32 =
vmlal_s16(partial6_s32, vget_high_s16(partial6), vget_high_s16(partial6));
uint32x4_t costs[4];
costs[0] = fold_mul_and_sum_neon(partial4a, partial4b, const0, const1);
costs[1] = fold_mul_and_sum_neon(partial5a, partial5b, const2, const3);
costs[2] = vmulq_n_u32(vreinterpretq_u32_s32(partial6_s32), 105);
costs[3] = fold_mul_and_sum_neon(partial7a, partial7b, const2, const3);
costs[0] = horizontal_add_4d_u32x4(costs);
vst1q_u32(cost, costs[0]);
return costs[0];
}
static INLINE uint32x4_t fold_mul_and_sum_pairwise_neon(int16x8_t partiala,
int16x8_t partialb,
int16x8_t partialc,
uint32x4_t const0) {
// Reverse partial c.
// pattern = { 10 11 8 9 6 7 4 5 2 3 0 1 12 13 14 15 }.
uint8x16_t pattern = vreinterpretq_u8_u64(
vcombine_u64(vcreate_u64((uint64_t)0x05040706 << 32 | 0x09080b0a),
vcreate_u64((uint64_t)0x0f0e0d0c << 32 | 0x01000302)));
#if AOM_ARCH_AARCH64
partialc =
vreinterpretq_s16_s8(vqtbl1q_s8(vreinterpretq_s8_s16(partialc), pattern));
#else
int8x8x2_t p = { { vget_low_s8(vreinterpretq_s8_s16(partialc)),
vget_high_s8(vreinterpretq_s8_s16(partialc)) } };
int8x8_t shuffle_hi = vtbl2_s8(p, vget_high_s8(vreinterpretq_s8_u8(pattern)));
int8x8_t shuffle_lo = vtbl2_s8(p, vget_low_s8(vreinterpretq_s8_u8(pattern)));
partialc = vreinterpretq_s16_s8(vcombine_s8(shuffle_lo, shuffle_hi));
#endif
int32x4_t partiala_s32 = vpaddlq_s16(partiala);
int32x4_t partialb_s32 = vpaddlq_s16(partialb);
int32x4_t partialc_s32 = vpaddlq_s16(partialc);
partiala_s32 = vmulq_s32(partiala_s32, partiala_s32);
partialb_s32 = vmulq_s32(partialb_s32, partialb_s32);
partialc_s32 = vmulq_s32(partialc_s32, partialc_s32);
partiala_s32 = vaddq_s32(partiala_s32, partialc_s32);
uint32x4_t cost = vmulq_n_u32(vreinterpretq_u32_s32(partialb_s32), 105);
cost = vmlaq_u32(cost, vreinterpretq_u32_s32(partiala_s32), const0);
return cost;
}
// This function computes the cost along directions 0, 1, 2, 3. (0 means
// 45-degree up-right, 2 is horizontal).
//
// For direction 1 and 3 ("east northeast" and "east southeast") the shifted
// lines need three vectors instead of two. For direction 1 for example, we need
// to compute the sums along the line i below:
// 0 0 1 1 2 2 3 3
// 1 1 2 2 3 3 4 4
// 2 2 3 3 4 4 5 5
// 3 3 4 4 5 5 6 6
// 4 4 5 5 6 6 7 7
// 5 5 6 6 7 7 8 8
// 6 6 7 7 8 8 9 9
// 7 7 8 8 9 9 10 10
//
// Which means we need the following configuration:
// 0 0 1 1 2 2 3 3
// 1 1 2 2 3 3 4 4
// 2 2 3 3 4 4 5 5
// 3 3 4 4 5 5 6 6
// 4 4 5 5 6 6 7 7
// 5 5 6 6 7 7 8 8
// 6 6 7 7 8 8 9 9
// 7 7 8 8 9 9 10 10
//
// Three vectors are needed to compute this, as well as some extra pairwise
// additions.
static uint32x4_t compute_horiz_directions_neon(int16x8_t lines[8],
uint32_t cost[4]) {
const int16x8_t zero = vdupq_n_s16(0);
// Compute diagonal directions (1, 2, 3).
// Partial sums for lines 0 and 1.
int16x8_t partial0a = lines[0];
partial0a = vaddq_s16(partial0a, vextq_s16(zero, lines[1], 7));
int16x8_t partial0b = vextq_s16(lines[1], zero, 7);
int16x8_t partial1a = vaddq_s16(lines[0], vextq_s16(zero, lines[1], 6));
int16x8_t partial1b = vextq_s16(lines[1], zero, 6);
int16x8_t partial3a = vextq_s16(lines[0], zero, 2);
partial3a = vaddq_s16(partial3a, vextq_s16(lines[1], zero, 4));
int16x8_t partial3b = vextq_s16(zero, lines[0], 2);
partial3b = vaddq_s16(partial3b, vextq_s16(zero, lines[1], 4));
// Partial sums for lines 2 and 3.
partial0a = vaddq_s16(partial0a, vextq_s16(zero, lines[2], 6));
partial0a = vaddq_s16(partial0a, vextq_s16(zero, lines[3], 5));
partial0b = vaddq_s16(partial0b, vextq_s16(lines[2], zero, 6));
partial0b = vaddq_s16(partial0b, vextq_s16(lines[3], zero, 5));
partial1a = vaddq_s16(partial1a, vextq_s16(zero, lines[2], 4));
partial1a = vaddq_s16(partial1a, vextq_s16(zero, lines[3], 2));
partial1b = vaddq_s16(partial1b, vextq_s16(lines[2], zero, 4));
partial1b = vaddq_s16(partial1b, vextq_s16(lines[3], zero, 2));
partial3a = vaddq_s16(partial3a, vextq_s16(lines[2], zero, 6));
partial3b = vaddq_s16(partial3b, vextq_s16(zero, lines[2], 6));
partial3b = vaddq_s16(partial3b, lines[3]);
// Partial sums for lines 4 and 5.
partial0a = vaddq_s16(partial0a, vextq_s16(zero, lines[4], 4));
partial0a = vaddq_s16(partial0a, vextq_s16(zero, lines[5], 3));
partial0b = vaddq_s16(partial0b, vextq_s16(lines[4], zero, 4));
partial0b = vaddq_s16(partial0b, vextq_s16(lines[5], zero, 3));
partial1b = vaddq_s16(partial1b, lines[4]);
partial1b = vaddq_s16(partial1b, vextq_s16(zero, lines[5], 6));
int16x8_t partial1c = vextq_s16(lines[5], zero, 6);
partial3b = vaddq_s16(partial3b, vextq_s16(lines[4], zero, 2));
partial3b = vaddq_s16(partial3b, vextq_s16(lines[5], zero, 4));
int16x8_t partial3c = vextq_s16(zero, lines[4], 2);
partial3c = vaddq_s16(partial3c, vextq_s16(zero, lines[5], 4));
// Partial sums for lines 6 and 7.
partial0a = vaddq_s16(partial0a, vextq_s16(zero, lines[6], 2));
partial0a = vaddq_s16(partial0a, vextq_s16(zero, lines[7], 1));
partial0b = vaddq_s16(partial0b, vextq_s16(lines[6], zero, 2));
partial0b = vaddq_s16(partial0b, vextq_s16(lines[7], zero, 1));
partial1b = vaddq_s16(partial1b, vextq_s16(zero, lines[6], 4));
partial1b = vaddq_s16(partial1b, vextq_s16(zero, lines[7], 2));
partial1c = vaddq_s16(partial1c, vextq_s16(lines[6], zero, 4));
partial1c = vaddq_s16(partial1c, vextq_s16(lines[7], zero, 2));
partial3b = vaddq_s16(partial3b, vextq_s16(lines[6], zero, 6));
partial3c = vaddq_s16(partial3c, vextq_s16(zero, lines[6], 6));
partial3c = vaddq_s16(partial3c, lines[7]);
// Special case for direction 2 as it's just a sum along each line.
int16x8_t lines03[4] = { lines[0], lines[1], lines[2], lines[3] };
int16x8_t lines47[4] = { lines[4], lines[5], lines[6], lines[7] };
int32x4_t partial2a = horizontal_add_4d_s16x8(lines03);
int32x4_t partial2b = horizontal_add_4d_s16x8(lines47);
uint32x4_t partial2a_u32 =
vreinterpretq_u32_s32(vmulq_s32(partial2a, partial2a));
uint32x4_t partial2b_u32 =
vreinterpretq_u32_s32(vmulq_s32(partial2b, partial2b));
uint32x4_t const0 = vreinterpretq_u32_u64(
vcombine_u64(vcreate_u64((uint64_t)420 << 32 | 840),
vcreate_u64((uint64_t)210 << 32 | 280)));
uint32x4_t const1 = vreinterpretq_u32_u64(
vcombine_u64(vcreate_u64((uint64_t)140 << 32 | 168),
vcreate_u64((uint64_t)105 << 32 | 120)));
uint32x4_t const2 = vreinterpretq_u32_u64(
vcombine_u64(vcreate_u64((uint64_t)210 << 32 | 420),
vcreate_u64((uint64_t)105 << 32 | 140)));
uint32x4_t costs[4];
costs[0] = fold_mul_and_sum_neon(partial0a, partial0b, const0, const1);
costs[1] =
fold_mul_and_sum_pairwise_neon(partial1a, partial1b, partial1c, const2);
costs[2] = vaddq_u32(partial2a_u32, partial2b_u32);
costs[2] = vmulq_n_u32(costs[2], 105);
costs[3] =
fold_mul_and_sum_pairwise_neon(partial3c, partial3b, partial3a, const2);
costs[0] = horizontal_add_4d_u32x4(costs);
vst1q_u32(cost, costs[0]);
return costs[0];
}
int cdef_find_dir_neon(const uint16_t *img, int stride, int32_t *var,
int coeff_shift) {
uint32_t cost[8];
uint32_t best_cost = 0;
int best_dir = 0;
int16x8_t lines[8];
for (int i = 0; i < 8; i++) {
uint16x8_t s = vld1q_u16(&img[i * stride]);
lines[i] = vreinterpretq_s16_u16(
vsubq_u16(vshlq_u16(s, vdupq_n_s16(-coeff_shift)), vdupq_n_u16(128)));
}
// Compute "mostly vertical" directions.
uint32x4_t cost47 = compute_vert_directions_neon(lines, cost + 4);
// Compute "mostly horizontal" directions.
uint32x4_t cost03 = compute_horiz_directions_neon(lines, cost);
// Find max cost as well as its index to get best_dir.
// The max cost needs to be propagated in the whole vector to find its
// position in the original cost vectors cost03 and cost47.
uint32x4_t cost07 = vmaxq_u32(cost03, cost47);
#if AOM_ARCH_AARCH64
best_cost = vmaxvq_u32(cost07);
uint32x4_t max_cost = vdupq_n_u32(best_cost);
uint8x16x2_t costs = { { vreinterpretq_u8_u32(vceqq_u32(max_cost, cost03)),
vreinterpretq_u8_u32(
vceqq_u32(max_cost, cost47)) } };
// idx = { 28, 24, 20, 16, 12, 8, 4, 0 };
uint8x8_t idx = vreinterpret_u8_u64(vcreate_u64(0x0004080c1014181cULL));
// Get the lowest 8 bit of each 32-bit elements and reverse them.
uint8x8_t tbl = vqtbl2_u8(costs, idx);
uint64_t a = vget_lane_u64(vreinterpret_u64_u8(tbl), 0);
best_dir = aom_clzll(a) >> 3;
#else
uint32x2_t cost64 = vpmax_u32(vget_low_u32(cost07), vget_high_u32(cost07));
cost64 = vpmax_u32(cost64, cost64);
uint32x4_t max_cost = vcombine_u32(cost64, cost64);
best_cost = vget_lane_u32(cost64, 0);
uint16x8_t costs = vcombine_u16(vmovn_u32(vceqq_u32(max_cost, cost03)),
vmovn_u32(vceqq_u32(max_cost, cost47)));
uint8x8_t idx =
vand_u8(vmovn_u16(costs),
vreinterpret_u8_u64(vcreate_u64(0x8040201008040201ULL)));
int sum = horizontal_add_u8x8(idx);
best_dir = get_msb(sum ^ (sum - 1));
#endif
// Difference between the optimal variance and the variance along the
// orthogonal direction. Again, the sum(x^2) terms cancel out.
*var = best_cost - cost[(best_dir + 4) & 7];
// We'd normally divide by 840, but dividing by 1024 is close enough
// for what we're going to do with this.
*var >>= 10;
return best_dir;
}
void cdef_find_dir_dual_neon(const uint16_t *img1, const uint16_t *img2,
int stride, int32_t *var_out_1st,
int32_t *var_out_2nd, int coeff_shift,
int *out_dir_1st_8x8, int *out_dir_2nd_8x8) {
// Process first 8x8.
*out_dir_1st_8x8 = cdef_find_dir(img1, stride, var_out_1st, coeff_shift);
// Process second 8x8.
*out_dir_2nd_8x8 = cdef_find_dir(img2, stride, var_out_2nd, coeff_shift);
}
// sign(a-b) * min(abs(a-b), max(0, threshold - (abs(a-b) >> adjdamp)))
static INLINE int16x8_t constrain16(uint16x8_t a, uint16x8_t b,
unsigned int threshold, int adjdamp) {
uint16x8_t diff = vabdq_u16(a, b);
const uint16x8_t a_gt_b = vcgtq_u16(a, b);
const uint16x8_t s = vqsubq_u16(vdupq_n_u16(threshold),
vshlq_u16(diff, vdupq_n_s16(-adjdamp)));
const int16x8_t clip = vreinterpretq_s16_u16(vminq_u16(diff, s));
return vbslq_s16(a_gt_b, clip, vnegq_s16(clip));
}
static INLINE void primary_filter(uint16x8_t s, uint16x8_t tap[4],
const int *pri_taps, int pri_strength,
int pri_damping, int16x8_t *sum) {
// Near taps
int16x8_t n0 = constrain16(tap[0], s, pri_strength, pri_damping);
int16x8_t n1 = constrain16(tap[1], s, pri_strength, pri_damping);
// sum += pri_taps[0] * (n0 + n1)
n0 = vaddq_s16(n0, n1);
*sum = vmlaq_n_s16(*sum, n0, pri_taps[0]);
// Far taps
int16x8_t f0 = constrain16(tap[2], s, pri_strength, pri_damping);
int16x8_t f1 = constrain16(tap[3], s, pri_strength, pri_damping);
// sum += pri_taps[1] * (f0 + f1)
f0 = vaddq_s16(f0, f1);
*sum = vmlaq_n_s16(*sum, f0, pri_taps[1]);
}
static INLINE void secondary_filter(uint16x8_t s, uint16x8_t tap[8],
const int *sec_taps, int sec_strength,
int sec_damping, int16x8_t *sum) {
// Near taps
int16x8_t s0 = constrain16(tap[0], s, sec_strength, sec_damping);
int16x8_t s1 = constrain16(tap[1], s, sec_strength, sec_damping);
int16x8_t s2 = constrain16(tap[2], s, sec_strength, sec_damping);
int16x8_t s3 = constrain16(tap[3], s, sec_strength, sec_damping);
// sum += sec_taps[0] * (p0 + p1 + p2 + p3)
s0 = vaddq_s16(s0, s1);
s2 = vaddq_s16(s2, s3);
s0 = vaddq_s16(s0, s2);
*sum = vmlaq_n_s16(*sum, s0, sec_taps[0]);
// Far taps
s0 = constrain16(tap[4], s, sec_strength, sec_damping);
s1 = constrain16(tap[5], s, sec_strength, sec_damping);
s2 = constrain16(tap[6], s, sec_strength, sec_damping);
s3 = constrain16(tap[7], s, sec_strength, sec_damping);
// sum += sec_taps[1] * (p0 + p1 + p2 + p3)
s0 = vaddq_s16(s0, s1);
s2 = vaddq_s16(s2, s3);
s0 = vaddq_s16(s0, s2);
*sum = vmlaq_n_s16(*sum, s0, sec_taps[1]);
}
void cdef_filter_8_0_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
uint16x8_t max, min;
const uint16x8_t cdef_large_value_mask =
vdupq_n_u16(((uint16_t)~CDEF_VERY_LARGE));
const int po1 = cdef_directions[dir][0];
const int po2 = cdef_directions[dir][1];
const int s1o1 = cdef_directions[dir + 2][0];
const int s1o2 = cdef_directions[dir + 2][1];
const int s2o1 = cdef_directions[dir - 2][0];
const int s2o2 = cdef_directions[dir - 2][1];
const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
const int *sec_taps = cdef_sec_taps;
if (pri_strength) {
pri_damping = AOMMAX(0, pri_damping - get_msb(pri_strength));
}
if (sec_strength) {
sec_damping = AOMMAX(0, sec_damping - get_msb(sec_strength));
}
if (block_width == 8) {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = vld1q_u16(in);
max = min = s;
uint16x8_t pri_src[4];
// Primary near taps
pri_src[0] = vld1q_u16(in + po1);
pri_src[1] = vld1q_u16(in - po1);
// Primary far taps
pri_src[2] = vld1q_u16(in + po2);
pri_src[3] = vld1q_u16(in - po2);
primary_filter(s, pri_src, pri_taps, pri_strength, pri_damping, &sum);
// The source is 16 bits, however, we only really care about the lower
// 8 bits. The upper 8 bits contain the "large" flag. After the final
// primary max has been calculated, zero out the upper 8 bits. Use this
// to find the "16 bit" max.
uint8x16_t pri_max0 = vmaxq_u8(vreinterpretq_u8_u16(pri_src[0]),
vreinterpretq_u8_u16(pri_src[1]));
uint8x16_t pri_max1 = vmaxq_u8(vreinterpretq_u8_u16(pri_src[2]),
vreinterpretq_u8_u16(pri_src[3]));
pri_max0 = vmaxq_u8(pri_max0, pri_max1);
max = vmaxq_u16(max, vandq_u16(vreinterpretq_u16_u8(pri_max0),
cdef_large_value_mask));
uint16x8_t pri_min0 = vminq_u16(pri_src[0], pri_src[1]);
uint16x8_t pri_min1 = vminq_u16(pri_src[2], pri_src[3]);
pri_min0 = vminq_u16(pri_min0, pri_min1);
min = vminq_u16(min, pri_min0);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = vld1q_u16(in + s1o1);
sec_src[1] = vld1q_u16(in - s1o1);
sec_src[2] = vld1q_u16(in + s2o1);
sec_src[3] = vld1q_u16(in - s2o1);
// Secondary far taps
sec_src[4] = vld1q_u16(in + s1o2);
sec_src[5] = vld1q_u16(in - s1o2);
sec_src[6] = vld1q_u16(in + s2o2);
sec_src[7] = vld1q_u16(in - s2o2);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
// The source is 16 bits, however, we only really care about the lower
// 8 bits. The upper 8 bits contain the "large" flag. After the final
// primary max has been calculated, zero out the upper 8 bits. Use this
// to find the "16 bit" max.
uint8x16_t sec_max0 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[0]),
vreinterpretq_u8_u16(sec_src[1]));
uint8x16_t sec_max1 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[2]),
vreinterpretq_u8_u16(sec_src[3]));
uint8x16_t sec_max2 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[4]),
vreinterpretq_u8_u16(sec_src[5]));
uint8x16_t sec_max3 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[6]),
vreinterpretq_u8_u16(sec_src[7]));
sec_max0 = vmaxq_u8(sec_max0, sec_max1);
sec_max2 = vmaxq_u8(sec_max2, sec_max3);
sec_max0 = vmaxq_u8(sec_max0, sec_max2);
max = vmaxq_u16(max, vandq_u16(vreinterpretq_u16_u8(sec_max0),
cdef_large_value_mask));
uint16x8_t sec_min0 = vminq_u16(sec_src[0], sec_src[1]);
uint16x8_t sec_min1 = vminq_u16(sec_src[2], sec_src[3]);
uint16x8_t sec_min2 = vminq_u16(sec_src[4], sec_src[5]);
uint16x8_t sec_min3 = vminq_u16(sec_src[6], sec_src[7]);
sec_min0 = vminq_u16(sec_min0, sec_min1);
sec_min2 = vminq_u16(sec_min2, sec_min3);
sec_min0 = vminq_u16(sec_min0, sec_min2);
min = vminq_u16(min, sec_min0);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
int16x8_t res_s16 = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
res_s16 = vminq_s16(vmaxq_s16(res_s16, vreinterpretq_s16_u16(min)),
vreinterpretq_s16_u16(max));
const uint8x8_t res_u8 = vqmovun_s16(res_s16);
vst1_u8(dst8, res_u8);
in += CDEF_BSTRIDE;
dst8 += dstride;
} while (--h != 0);
} else {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
max = min = s;
uint16x8_t pri_src[4];
// Primary near taps
pri_src[0] = load_unaligned_u16_4x2(in + po1, CDEF_BSTRIDE);
pri_src[1] = load_unaligned_u16_4x2(in - po1, CDEF_BSTRIDE);
// Primary far taps
pri_src[2] = load_unaligned_u16_4x2(in + po2, CDEF_BSTRIDE);
pri_src[3] = load_unaligned_u16_4x2(in - po2, CDEF_BSTRIDE);
primary_filter(s, pri_src, pri_taps, pri_strength, pri_damping, &sum);
// The source is 16 bits, however, we only really care about the lower
// 8 bits. The upper 8 bits contain the "large" flag. After the final
// primary max has been calculated, zero out the upper 8 bits. Use this
// to find the "16 bit" max.
uint8x16_t pri_max0 = vmaxq_u8(vreinterpretq_u8_u16(pri_src[0]),
vreinterpretq_u8_u16(pri_src[1]));
uint8x16_t pri_max1 = vmaxq_u8(vreinterpretq_u8_u16(pri_src[2]),
vreinterpretq_u8_u16(pri_src[3]));
pri_max0 = vmaxq_u8(pri_max0, pri_max1);
max = vmaxq_u16(max, vandq_u16(vreinterpretq_u16_u8(pri_max0),
cdef_large_value_mask));
uint16x8_t pri_min1 = vminq_u16(pri_src[0], pri_src[1]);
uint16x8_t pri_min2 = vminq_u16(pri_src[2], pri_src[3]);
pri_min1 = vminq_u16(pri_min1, pri_min2);
min = vminq_u16(min, pri_min1);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = load_unaligned_u16_4x2(in + s1o1, CDEF_BSTRIDE);
sec_src[1] = load_unaligned_u16_4x2(in - s1o1, CDEF_BSTRIDE);
sec_src[2] = load_unaligned_u16_4x2(in + s2o1, CDEF_BSTRIDE);
sec_src[3] = load_unaligned_u16_4x2(in - s2o1, CDEF_BSTRIDE);
// Secondary far taps
sec_src[4] = load_unaligned_u16_4x2(in + s1o2, CDEF_BSTRIDE);
sec_src[5] = load_unaligned_u16_4x2(in - s1o2, CDEF_BSTRIDE);
sec_src[6] = load_unaligned_u16_4x2(in + s2o2, CDEF_BSTRIDE);
sec_src[7] = load_unaligned_u16_4x2(in - s2o2, CDEF_BSTRIDE);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
// The source is 16 bits, however, we only really care about the lower
// 8 bits. The upper 8 bits contain the "large" flag. After the final
// primary max has been calculated, zero out the upper 8 bits. Use this
// to find the "16 bit" max.
uint8x16_t sec_max0 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[0]),
vreinterpretq_u8_u16(sec_src[1]));
uint8x16_t sec_max1 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[2]),
vreinterpretq_u8_u16(sec_src[3]));
uint8x16_t sec_max2 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[4]),
vreinterpretq_u8_u16(sec_src[5]));
uint8x16_t sec_max3 = vmaxq_u8(vreinterpretq_u8_u16(sec_src[6]),
vreinterpretq_u8_u16(sec_src[7]));
sec_max0 = vmaxq_u8(sec_max0, sec_max1);
sec_max2 = vmaxq_u8(sec_max2, sec_max3);
sec_max0 = vmaxq_u8(sec_max0, sec_max2);
max = vmaxq_u16(max, vandq_u16(vreinterpretq_u16_u8(sec_max0),
cdef_large_value_mask));
uint16x8_t sec_min0 = vminq_u16(sec_src[0], sec_src[1]);
uint16x8_t sec_min1 = vminq_u16(sec_src[2], sec_src[3]);
uint16x8_t sec_min2 = vminq_u16(sec_src[4], sec_src[5]);
uint16x8_t sec_min3 = vminq_u16(sec_src[6], sec_src[7]);
sec_min0 = vminq_u16(sec_min0, sec_min1);
sec_min2 = vminq_u16(sec_min2, sec_min3);
sec_min0 = vminq_u16(sec_min0, sec_min2);
min = vminq_u16(min, sec_min0);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
int16x8_t res_s16 = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
res_s16 = vminq_s16(vmaxq_s16(res_s16, vreinterpretq_s16_u16(min)),
vreinterpretq_s16_u16(max));
const uint8x8_t res_u8 = vqmovun_s16(res_s16);
store_u8x4_strided_x2(dst8, dstride, res_u8);
in += 2 * CDEF_BSTRIDE;
dst8 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
void cdef_filter_8_1_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
(void)sec_strength;
(void)sec_damping;
const int po1 = cdef_directions[dir][0];
const int po2 = cdef_directions[dir][1];
const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
if (pri_strength) {
pri_damping = AOMMAX(0, pri_damping - get_msb(pri_strength));
}
if (block_width == 8) {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = vld1q_u16(in);
uint16x8_t tap[4];
// Primary near taps
tap[0] = vld1q_u16(in + po1);
tap[1] = vld1q_u16(in - po1);
// Primary far taps
tap[2] = vld1q_u16(in + po2);
tap[3] = vld1q_u16(in - po2);
primary_filter(s, tap, pri_taps, pri_strength, pri_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res_s16 = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
const uint8x8_t res_u8 = vqmovun_s16(res_s16);
vst1_u8(dst8, res_u8);
in += CDEF_BSTRIDE;
dst8 += dstride;
} while (--h != 0);
} else {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
uint16x8_t pri_src[4];
// Primary near taps
pri_src[0] = load_unaligned_u16_4x2(in + po1, CDEF_BSTRIDE);
pri_src[1] = load_unaligned_u16_4x2(in - po1, CDEF_BSTRIDE);
// Primary far taps
pri_src[2] = load_unaligned_u16_4x2(in + po2, CDEF_BSTRIDE);
pri_src[3] = load_unaligned_u16_4x2(in - po2, CDEF_BSTRIDE);
primary_filter(s, pri_src, pri_taps, pri_strength, pri_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res_s16 = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
const uint8x8_t res_u8 = vqmovun_s16(res_s16);
store_u8x4_strided_x2(dst8, dstride, res_u8);
in += 2 * CDEF_BSTRIDE;
dst8 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
void cdef_filter_8_2_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
(void)pri_strength;
(void)pri_damping;
(void)coeff_shift;
const int s1o1 = cdef_directions[dir + 2][0];
const int s1o2 = cdef_directions[dir + 2][1];
const int s2o1 = cdef_directions[dir - 2][0];
const int s2o2 = cdef_directions[dir - 2][1];
const int *sec_taps = cdef_sec_taps;
if (sec_strength) {
sec_damping = AOMMAX(0, sec_damping - get_msb(sec_strength));
}
if (block_width == 8) {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = vld1q_u16(in);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = vld1q_u16(in + s1o1);
sec_src[1] = vld1q_u16(in - s1o1);
sec_src[2] = vld1q_u16(in + s2o1);
sec_src[3] = vld1q_u16(in - s2o1);
// Secondary far taps
sec_src[4] = vld1q_u16(in + s1o2);
sec_src[5] = vld1q_u16(in - s1o2);
sec_src[6] = vld1q_u16(in + s2o2);
sec_src[7] = vld1q_u16(in - s2o2);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res_s16 = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
const uint8x8_t res_u8 = vqmovun_s16(res_s16);
vst1_u8(dst8, res_u8);
in += CDEF_BSTRIDE;
dst8 += dstride;
} while (--h != 0);
} else {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = load_unaligned_u16_4x2(in + s1o1, CDEF_BSTRIDE);
sec_src[1] = load_unaligned_u16_4x2(in - s1o1, CDEF_BSTRIDE);
sec_src[2] = load_unaligned_u16_4x2(in + s2o1, CDEF_BSTRIDE);
sec_src[3] = load_unaligned_u16_4x2(in - s2o1, CDEF_BSTRIDE);
// Secondary far taps
sec_src[4] = load_unaligned_u16_4x2(in + s1o2, CDEF_BSTRIDE);
sec_src[5] = load_unaligned_u16_4x2(in - s1o2, CDEF_BSTRIDE);
sec_src[6] = load_unaligned_u16_4x2(in + s2o2, CDEF_BSTRIDE);
sec_src[7] = load_unaligned_u16_4x2(in - s2o2, CDEF_BSTRIDE);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res_s16 = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
const uint8x8_t res_u8 = vqmovun_s16(res_s16);
store_u8x4_strided_x2(dst8, dstride, res_u8);
in += 2 * CDEF_BSTRIDE;
dst8 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
void cdef_filter_8_3_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
(void)pri_strength;
(void)sec_strength;
(void)dir;
(void)pri_damping;
(void)sec_damping;
(void)coeff_shift;
(void)block_width;
if (block_width == 8) {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
const uint16x8_t s = vld1q_u16(in);
const uint8x8_t res = vqmovn_u16(s);
vst1_u8(dst8, res);
in += CDEF_BSTRIDE;
dst8 += dstride;
} while (--h != 0);
} else {
uint8_t *dst8 = (uint8_t *)dest;
int h = block_height;
do {
const uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
const uint8x8_t res = vqmovn_u16(s);
store_u8x4_strided_x2(dst8, dstride, res);
in += 2 * CDEF_BSTRIDE;
dst8 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
void cdef_filter_16_0_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
uint16x8_t max, min;
const uint16x8_t cdef_large_value_mask =
vdupq_n_u16(((uint16_t)~CDEF_VERY_LARGE));
const int po1 = cdef_directions[dir][0];
const int po2 = cdef_directions[dir][1];
const int s1o1 = cdef_directions[dir + 2][0];
const int s1o2 = cdef_directions[dir + 2][1];
const int s2o1 = cdef_directions[dir - 2][0];
const int s2o2 = cdef_directions[dir - 2][1];
const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
const int *sec_taps = cdef_sec_taps;
if (pri_strength) {
pri_damping = AOMMAX(0, pri_damping - get_msb(pri_strength));
}
if (sec_strength) {
sec_damping = AOMMAX(0, sec_damping - get_msb(sec_strength));
}
if (block_width == 8) {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = vld1q_u16(in);
max = min = s;
uint16x8_t pri_src[4];
// Primary near taps
pri_src[0] = vld1q_u16(in + po1);
pri_src[1] = vld1q_u16(in - po1);
// Primary far taps
pri_src[2] = vld1q_u16(in + po2);
pri_src[3] = vld1q_u16(in - po2);
primary_filter(s, pri_src, pri_taps, pri_strength, pri_damping, &sum);
uint16x8_t pri_min0 = vminq_u16(pri_src[0], pri_src[1]);
uint16x8_t pri_min1 = vminq_u16(pri_src[2], pri_src[3]);
pri_min0 = vminq_u16(pri_min0, pri_min1);
min = vminq_u16(min, pri_min0);
/* Convert CDEF_VERY_LARGE to 0 before calculating max. */
pri_src[0] = vandq_u16(pri_src[0], cdef_large_value_mask);
pri_src[1] = vandq_u16(pri_src[1], cdef_large_value_mask);
pri_src[2] = vandq_u16(pri_src[2], cdef_large_value_mask);
pri_src[3] = vandq_u16(pri_src[3], cdef_large_value_mask);
uint16x8_t pri_max0 = vmaxq_u16(pri_src[0], pri_src[1]);
uint16x8_t pri_max1 = vmaxq_u16(pri_src[2], pri_src[3]);
pri_max0 = vmaxq_u16(pri_max0, pri_max1);
max = vmaxq_u16(max, pri_max0);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = vld1q_u16(in + s1o1);
sec_src[1] = vld1q_u16(in - s1o1);
sec_src[2] = vld1q_u16(in + s2o1);
sec_src[3] = vld1q_u16(in - s2o1);
// Secondary far taps
sec_src[4] = vld1q_u16(in + s1o2);
sec_src[5] = vld1q_u16(in - s1o2);
sec_src[6] = vld1q_u16(in + s2o2);
sec_src[7] = vld1q_u16(in - s2o2);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
uint16x8_t sec_min0 = vminq_u16(sec_src[0], sec_src[1]);
uint16x8_t sec_min1 = vminq_u16(sec_src[2], sec_src[3]);
uint16x8_t sec_min2 = vminq_u16(sec_src[4], sec_src[5]);
uint16x8_t sec_min3 = vminq_u16(sec_src[6], sec_src[7]);
sec_min0 = vminq_u16(sec_min0, sec_min1);
sec_min2 = vminq_u16(sec_min2, sec_min3);
sec_min0 = vminq_u16(sec_min0, sec_min2);
min = vminq_u16(min, sec_min0);
/* Convert CDEF_VERY_LARGE to 0 before calculating max. */
sec_src[0] = vandq_u16(sec_src[0], cdef_large_value_mask);
sec_src[1] = vandq_u16(sec_src[1], cdef_large_value_mask);
sec_src[2] = vandq_u16(sec_src[2], cdef_large_value_mask);
sec_src[3] = vandq_u16(sec_src[3], cdef_large_value_mask);
sec_src[4] = vandq_u16(sec_src[4], cdef_large_value_mask);
sec_src[5] = vandq_u16(sec_src[5], cdef_large_value_mask);
sec_src[6] = vandq_u16(sec_src[6], cdef_large_value_mask);
sec_src[7] = vandq_u16(sec_src[7], cdef_large_value_mask);
uint16x8_t sec_max0 = vmaxq_u16(sec_src[0], sec_src[1]);
uint16x8_t sec_max1 = vmaxq_u16(sec_src[2], sec_src[3]);
uint16x8_t sec_max2 = vmaxq_u16(sec_src[4], sec_src[5]);
uint16x8_t sec_max3 = vmaxq_u16(sec_src[6], sec_src[7]);
sec_max0 = vmaxq_u16(sec_max0, sec_max1);
sec_max2 = vmaxq_u16(sec_max2, sec_max3);
sec_max0 = vmaxq_u16(sec_max0, sec_max2);
max = vmaxq_u16(max, sec_max0);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
int16x8_t res = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
res = vminq_s16(vmaxq_s16(res, vreinterpretq_s16_u16(min)),
vreinterpretq_s16_u16(max));
vst1q_u16(dst16, vreinterpretq_u16_s16(res));
in += CDEF_BSTRIDE;
dst16 += dstride;
} while (--h != 0);
} else {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
max = min = s;
uint16x8_t pri_src[4];
// Primary near taps
pri_src[0] = load_unaligned_u16_4x2(in + po1, CDEF_BSTRIDE);
pri_src[1] = load_unaligned_u16_4x2(in - po1, CDEF_BSTRIDE);
// Primary far taps
pri_src[2] = load_unaligned_u16_4x2(in + po2, CDEF_BSTRIDE);
pri_src[3] = load_unaligned_u16_4x2(in - po2, CDEF_BSTRIDE);
primary_filter(s, pri_src, pri_taps, pri_strength, pri_damping, &sum);
uint16x8_t pri_min1 = vminq_u16(pri_src[0], pri_src[1]);
uint16x8_t pri_min2 = vminq_u16(pri_src[2], pri_src[3]);
pri_min1 = vminq_u16(pri_min1, pri_min2);
min = vminq_u16(min, pri_min1);
/* Convert CDEF_VERY_LARGE to 0 before calculating max. */
pri_src[0] = vandq_u16(pri_src[0], cdef_large_value_mask);
pri_src[1] = vandq_u16(pri_src[1], cdef_large_value_mask);
pri_src[2] = vandq_u16(pri_src[2], cdef_large_value_mask);
pri_src[3] = vandq_u16(pri_src[3], cdef_large_value_mask);
uint16x8_t pri_max0 = vmaxq_u16(pri_src[0], pri_src[1]);
uint16x8_t pri_max1 = vmaxq_u16(pri_src[2], pri_src[3]);
pri_max0 = vmaxq_u16(pri_max0, pri_max1);
max = vmaxq_u16(max, pri_max0);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = load_unaligned_u16_4x2(in + s1o1, CDEF_BSTRIDE);
sec_src[1] = load_unaligned_u16_4x2(in - s1o1, CDEF_BSTRIDE);
sec_src[2] = load_unaligned_u16_4x2(in + s2o1, CDEF_BSTRIDE);
sec_src[3] = load_unaligned_u16_4x2(in - s2o1, CDEF_BSTRIDE);
// Secondary far taps
sec_src[4] = load_unaligned_u16_4x2(in + s1o2, CDEF_BSTRIDE);
sec_src[5] = load_unaligned_u16_4x2(in - s1o2, CDEF_BSTRIDE);
sec_src[6] = load_unaligned_u16_4x2(in + s2o2, CDEF_BSTRIDE);
sec_src[7] = load_unaligned_u16_4x2(in - s2o2, CDEF_BSTRIDE);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
uint16x8_t sec_min0 = vminq_u16(sec_src[0], sec_src[1]);
uint16x8_t sec_min1 = vminq_u16(sec_src[2], sec_src[3]);
uint16x8_t sec_min2 = vminq_u16(sec_src[4], sec_src[5]);
uint16x8_t sec_min3 = vminq_u16(sec_src[6], sec_src[7]);
sec_min0 = vminq_u16(sec_min0, sec_min1);
sec_min2 = vminq_u16(sec_min2, sec_min3);
sec_min0 = vminq_u16(sec_min0, sec_min2);
min = vminq_u16(min, sec_min0);
/* Convert CDEF_VERY_LARGE to 0 before calculating max. */
sec_src[0] = vandq_u16(sec_src[0], cdef_large_value_mask);
sec_src[1] = vandq_u16(sec_src[1], cdef_large_value_mask);
sec_src[2] = vandq_u16(sec_src[2], cdef_large_value_mask);
sec_src[3] = vandq_u16(sec_src[3], cdef_large_value_mask);
sec_src[4] = vandq_u16(sec_src[4], cdef_large_value_mask);
sec_src[5] = vandq_u16(sec_src[5], cdef_large_value_mask);
sec_src[6] = vandq_u16(sec_src[6], cdef_large_value_mask);
sec_src[7] = vandq_u16(sec_src[7], cdef_large_value_mask);
uint16x8_t sec_max0 = vmaxq_u16(sec_src[0], sec_src[1]);
uint16x8_t sec_max1 = vmaxq_u16(sec_src[2], sec_src[3]);
uint16x8_t sec_max2 = vmaxq_u16(sec_src[4], sec_src[5]);
uint16x8_t sec_max3 = vmaxq_u16(sec_src[6], sec_src[7]);
sec_max0 = vmaxq_u16(sec_max0, sec_max1);
sec_max2 = vmaxq_u16(sec_max2, sec_max3);
sec_max0 = vmaxq_u16(sec_max0, sec_max2);
max = vmaxq_u16(max, sec_max0);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
int16x8_t res = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
res = vminq_s16(vmaxq_s16(res, vreinterpretq_s16_u16(min)),
vreinterpretq_s16_u16(max));
store_u16x4_strided_x2(dst16, dstride, vreinterpretq_u16_s16(res));
in += 2 * CDEF_BSTRIDE;
dst16 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
void cdef_filter_16_1_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
(void)sec_strength;
(void)sec_damping;
const int po1 = cdef_directions[dir][0];
const int po2 = cdef_directions[dir][1];
const int *pri_taps = cdef_pri_taps[(pri_strength >> coeff_shift) & 1];
if (pri_strength) {
pri_damping = AOMMAX(0, pri_damping - get_msb(pri_strength));
}
if (block_width == 8) {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = vld1q_u16(in);
uint16x8_t tap[4];
// Primary near taps
tap[0] = vld1q_u16(in + po1);
tap[1] = vld1q_u16(in - po1);
// Primary far taps
tap[2] = vld1q_u16(in + po2);
tap[3] = vld1q_u16(in - po2);
primary_filter(s, tap, pri_taps, pri_strength, pri_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
vst1q_u16(dst16, vreinterpretq_u16_s16(res));
in += CDEF_BSTRIDE;
dst16 += dstride;
} while (--h != 0);
} else {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
uint16x8_t pri_src[4];
// Primary near taps
pri_src[0] = load_unaligned_u16_4x2(in + po1, CDEF_BSTRIDE);
pri_src[1] = load_unaligned_u16_4x2(in - po1, CDEF_BSTRIDE);
// Primary far taps
pri_src[2] = load_unaligned_u16_4x2(in + po2, CDEF_BSTRIDE);
pri_src[3] = load_unaligned_u16_4x2(in - po2, CDEF_BSTRIDE);
primary_filter(s, pri_src, pri_taps, pri_strength, pri_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
store_u16x4_strided_x2(dst16, dstride, vreinterpretq_u16_s16(res));
in += 2 * CDEF_BSTRIDE;
dst16 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
void cdef_filter_16_2_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
(void)pri_strength;
(void)pri_damping;
(void)coeff_shift;
const int s1o1 = cdef_directions[dir + 2][0];
const int s1o2 = cdef_directions[dir + 2][1];
const int s2o1 = cdef_directions[dir - 2][0];
const int s2o2 = cdef_directions[dir - 2][1];
const int *sec_taps = cdef_sec_taps;
if (sec_strength) {
sec_damping = AOMMAX(0, sec_damping - get_msb(sec_strength));
}
if (block_width == 8) {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = vld1q_u16(in);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = vld1q_u16(in + s1o1);
sec_src[1] = vld1q_u16(in - s1o1);
sec_src[2] = vld1q_u16(in + s2o1);
sec_src[3] = vld1q_u16(in - s2o1);
// Secondary far taps
sec_src[4] = vld1q_u16(in + s1o2);
sec_src[5] = vld1q_u16(in - s1o2);
sec_src[6] = vld1q_u16(in + s2o2);
sec_src[7] = vld1q_u16(in - s2o2);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
vst1q_u16(dst16, vreinterpretq_u16_s16(res));
in += CDEF_BSTRIDE;
dst16 += dstride;
} while (--h != 0);
} else {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
int16x8_t sum = vdupq_n_s16(0);
uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
uint16x8_t sec_src[8];
// Secondary near taps
sec_src[0] = load_unaligned_u16_4x2(in + s1o1, CDEF_BSTRIDE);
sec_src[1] = load_unaligned_u16_4x2(in - s1o1, CDEF_BSTRIDE);
sec_src[2] = load_unaligned_u16_4x2(in + s2o1, CDEF_BSTRIDE);
sec_src[3] = load_unaligned_u16_4x2(in - s2o1, CDEF_BSTRIDE);
// Secondary far taps
sec_src[4] = load_unaligned_u16_4x2(in + s1o2, CDEF_BSTRIDE);
sec_src[5] = load_unaligned_u16_4x2(in - s1o2, CDEF_BSTRIDE);
sec_src[6] = load_unaligned_u16_4x2(in + s2o2, CDEF_BSTRIDE);
sec_src[7] = load_unaligned_u16_4x2(in - s2o2, CDEF_BSTRIDE);
secondary_filter(s, sec_src, sec_taps, sec_strength, sec_damping, &sum);
// res = s + ((sum - (sum < 0) + 8) >> 4)
sum =
vaddq_s16(sum, vreinterpretq_s16_u16(vcltq_s16(sum, vdupq_n_s16(0))));
const int16x8_t res = vrsraq_n_s16(vreinterpretq_s16_u16(s), sum, 4);
store_u16x4_strided_x2(dst16, dstride, vreinterpretq_u16_s16(res));
in += 2 * CDEF_BSTRIDE;
dst16 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
void cdef_filter_16_3_neon(void *dest, int dstride, const uint16_t *in,
int pri_strength, int sec_strength, int dir,
int pri_damping, int sec_damping, int coeff_shift,
int block_width, int block_height) {
(void)pri_strength;
(void)sec_strength;
(void)dir;
(void)pri_damping;
(void)sec_damping;
(void)coeff_shift;
(void)block_width;
if (block_width == 8) {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
const uint16x8_t s = vld1q_u16(in);
vst1q_u16(dst16, s);
in += CDEF_BSTRIDE;
dst16 += dstride;
} while (--h != 0);
} else {
uint16_t *dst16 = (uint16_t *)dest;
int h = block_height;
do {
const uint16x8_t s = load_unaligned_u16_4x2(in, CDEF_BSTRIDE);
store_u16x4_strided_x2(dst16, dstride, s);
in += 2 * CDEF_BSTRIDE;
dst16 += 2 * dstride;
h -= 2;
} while (h != 0);
}
}
|