lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
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
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/**
*******************************************************************************
* @file
*  ihevce_had_compute_neon.c
*
* @brief
*  Contains intrinsic definitions of functions for computing had
*
* @author
*  Ittiam
*
* @par List of Functions:
*
* @remarks
*  None
*
********************************************************************************
*/
 
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* System include files */
#include <string.h>
#include <assert.h>
#include <arm_neon.h>
 
/* User include files */
#include "ihevc_typedefs.h"
#include "itt_video_api.h"
#include "ihevc_cmn_utils_neon.h"
#include "ihevce_had_satd.h"
#include "ihevce_cmn_utils_instr_set_router.h"
 
/*****************************************************************************/
/* Globals                                                                   */
/*****************************************************************************/
const int16_t gu2_dc_mask[8] = { 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff };
 
/*****************************************************************************/
/* Function Macros                                                           */
/*****************************************************************************/
#define RESIDUE(k, is_chroma)                                                                      \
    if(!is_chroma)                                                                                 \
    {                                                                                              \
        const uint8x8_t s##k = vld1_u8(pu1_src);                                                   \
        const uint8x8_t p##k = vld1_u8(pu1_pred);                                                  \
        *r##k = vreinterpretq_s16_u16(vsubl_u8(s##k, p##k));                                       \
        pu1_src += src_strd;                                                                       \
        pu1_pred += pred_strd;                                                                     \
    }                                                                                              \
    else                                                                                           \
    {                                                                                              \
        const uint8x8_t s##k = vld2_u8(pu1_src).val[0];                                            \
        const uint8x8_t p##k = vld2_u8(pu1_pred).val[0];                                           \
        *r##k = vreinterpretq_s16_u16(vsubl_u8(s##k, p##k));                                       \
        pu1_src += src_strd;                                                                       \
        pu1_pred += pred_strd;                                                                     \
    }
 
/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/
 
static INLINE void
    hadamard4x4_2_one_pass(int16x8_t *r0, int16x8_t *r1, int16x8_t *r2, int16x8_t *r3)
{
    const int16x8_t a0 = vaddq_s16(*r0, *r2);
    const int16x8_t a1 = vaddq_s16(*r1, *r3);
    const int16x8_t a2 = vsubq_s16(*r0, *r2);
    const int16x8_t a3 = vsubq_s16(*r1, *r3);
 
    *r0 = vaddq_s16(a0, a1);
    *r1 = vsubq_s16(a0, a1);
    *r2 = vaddq_s16(a2, a3);
    *r3 = vsubq_s16(a2, a3);
}
 
static INLINE void hadamard4x4_2(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    int16x8_t *r0,
    int16x8_t *r1,
    int16x8_t *r2,
    int16x8_t *r3)
{
    // compute error between src and pred
    RESIDUE(0, 0);
    RESIDUE(1, 0);
    RESIDUE(2, 0);
    RESIDUE(3, 0);
 
    // vertical hadamard tx
    hadamard4x4_2_one_pass(r0, r1, r2, r3);
 
    // transpose
    transpose_s16_4x4q(r0, r1, r2, r3);
 
    // horizontal hadamard tx
    hadamard4x4_2_one_pass(r0, r1, r2, r3);
}
 
static INLINE void hadamard4x4_4(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    int16x8_t *r0,
    int16x8_t *r1,
    int16x8_t *r2,
    int16x8_t *r3,
    int16x8_t *r4,
    int16x8_t *r5,
    int16x8_t *r6,
    int16x8_t *r7)
{
    // hadamard 4x4_2n
    hadamard4x4_2(pu1_src, src_strd, pu1_pred, pred_strd, r0, r1, r2, r3);
 
    // hadamard 4x4_2n
    pu1_src += (4 * src_strd);
    pu1_pred += (4 * pred_strd);
    hadamard4x4_2(pu1_src, src_strd, pu1_pred, pred_strd, r4, r5, r6, r7);
}
 
static INLINE WORD32 hadamard_sad4x4_4(int16x8_t *a, WORD32 *pi4_hsad, WORD32 hsad_stride)
{
    int16x8_t p[8];
    int32x4_t b01, b23;
    int64x2_t c01, c23;
    int32x2_t d01, d23;
 
    // satd
    p[0] = vabsq_s16(a[0]);
    p[1] = vabsq_s16(a[1]);
    p[0] = vaddq_s16(p[0], p[1]);
    p[2] = vabsq_s16(a[2]);
    p[3] = vabsq_s16(a[3]);
    p[2] = vaddq_s16(p[2], p[3]);
 
    p[4] = vabsq_s16(a[4]);
    p[5] = vabsq_s16(a[5]);
    p[4] = vaddq_s16(p[4], p[5]);
    p[6] = vabsq_s16(a[6]);
    p[7] = vabsq_s16(a[7]);
    p[6] = vaddq_s16(p[6], p[7]);
 
    p[0] = vaddq_s16(p[0], p[2]);
    b01 = vpaddlq_s16(p[0]);
    c01 = vpaddlq_s32(b01);
    d01 = vrshrn_n_s64(c01, 2);
    vst1_s32(pi4_hsad, d01);
    pi4_hsad += hsad_stride;
 
    p[4] = vaddq_s16(p[4], p[6]);
    b23 = vpaddlq_s16(p[4]);
    c23 = vpaddlq_s32(b23);
    d23 = vrshrn_n_s64(c23, 2);
    vst1_s32(pi4_hsad, d23);
 
    d01 = vadd_s32(d01, d23);
 
    return (WORD32)(vget_lane_s64(vpaddl_s32(d01), 0));
}
 
static INLINE WORD32 hadamard_sad8x8_using4x4(int16x8_t *a, WORD32 *early_cbf, WORD32 i4_frm_qstep)
{
    int16x8_t p[8];
    const int16x8_t threshold = vdupq_n_s16((int16_t)(i4_frm_qstep >> 8));
    int32x4_t b;
    int64x2_t c;
    int64_t satd;
    WORD32 i;
 
    for(i = 0; i < 4; i++)
    {
        int16x8_t p0 = vaddq_s16(a[i], a[i + 4]);
        int16x8_t p1 = vsubq_s16(a[i], a[i + 4]);
 
        int16x4_t q0 = vadd_s16(vget_low_s16(p0), vget_high_s16(p0));
        int16x4_t q1 = vsub_s16(vget_low_s16(p0), vget_high_s16(p0));
        int16x4_t q2 = vadd_s16(vget_low_s16(p1), vget_high_s16(p1));
        int16x4_t q3 = vsub_s16(vget_low_s16(p1), vget_high_s16(p1));
 
        a[i] = vcombine_s16(q0, q2);
        a[i + 4] = vcombine_s16(q1, q3);
    }
 
#define EARLY_EXIT(k)                                                                              \
    {                                                                                              \
        p[k] = vabsq_s16(a[k]);                                                                    \
        if(*early_cbf == 0)                                                                        \
        {                                                                                          \
            uint16x8_t cmp;                                                                        \
            cmp = vcgtq_s16(p[k], threshold);                                                      \
            if(vget_lane_s64(vreinterpret_s64_u16(vget_low_u16(cmp)), 0) ||                        \
               vget_lane_s64(vreinterpret_s64_u16(vget_high_u16(cmp)), 0))                         \
            {                                                                                      \
                *early_cbf = 1;                                                                    \
            }                                                                                      \
        }                                                                                          \
    }
    // satd
    EARLY_EXIT(0);
    EARLY_EXIT(1);
    p[0] = vaddq_s16(p[0], p[1]);
    EARLY_EXIT(2);
    EARLY_EXIT(3);
    p[2] = vaddq_s16(p[2], p[3]);
 
    EARLY_EXIT(4);
    EARLY_EXIT(5);
    p[4] = vaddq_s16(p[4], p[5]);
    EARLY_EXIT(6);
    EARLY_EXIT(7);
#undef EARLY_EXIT
    p[6] = vaddq_s16(p[6], p[7]);
 
    p[0] = vaddq_s16(p[0], p[2]);
    p[4] = vaddq_s16(p[4], p[6]);
    p[0] = vaddq_s16(p[0], p[4]);
    b = vpaddlq_s16(p[0]);
    c = vpaddlq_s32(b);
    satd = vget_lane_s64(vadd_s64(vget_low_s64(c), vget_high_s64(c)), 0);
 
    return ((satd + 4) >> 3);
}
 
static INLINE void hadamard8x8_one_pass(
    int16x8_t *r0,
    int16x8_t *r1,
    int16x8_t *r2,
    int16x8_t *r3,
    int16x8_t *r4,
    int16x8_t *r5,
    int16x8_t *r6,
    int16x8_t *r7)
{
    const int16x8_t a0 = vaddq_s16(*r0, *r4);
    const int16x8_t a4 = vsubq_s16(*r0, *r4);
    const int16x8_t a1 = vaddq_s16(*r1, *r5);
    const int16x8_t a5 = vsubq_s16(*r1, *r5);
    const int16x8_t a2 = vaddq_s16(*r2, *r6);
    const int16x8_t a6 = vsubq_s16(*r2, *r6);
    const int16x8_t a3 = vaddq_s16(*r3, *r7);
    const int16x8_t a7 = vsubq_s16(*r3, *r7);
 
    const int16x8_t b0 = vaddq_s16(a0, a2);
    const int16x8_t b2 = vsubq_s16(a0, a2);
    const int16x8_t b1 = vaddq_s16(a1, a3);
    const int16x8_t b3 = vsubq_s16(a1, a3);
    const int16x8_t b4 = vaddq_s16(a4, a6);
    const int16x8_t b6 = vsubq_s16(a4, a6);
    const int16x8_t b5 = vaddq_s16(a5, a7);
    const int16x8_t b7 = vsubq_s16(a5, a7);
 
    *r0 = vaddq_s16(b0, b1);
    *r1 = vsubq_s16(b0, b1);
    *r2 = vaddq_s16(b2, b3);
    *r3 = vsubq_s16(b2, b3);
    *r4 = vaddq_s16(b4, b5);
    *r5 = vsubq_s16(b4, b5);
    *r6 = vaddq_s16(b6, b7);
    *r7 = vsubq_s16(b6, b7);
}
 
static INLINE void hadamard8x8(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    int16x8_t *r0,
    int16x8_t *r1,
    int16x8_t *r2,
    int16x8_t *r3,
    int16x8_t *r4,
    int16x8_t *r5,
    int16x8_t *r6,
    int16x8_t *r7,
    WORD32 is_chroma)
{
    // compute error between src and pred
    RESIDUE(0, is_chroma);
    RESIDUE(1, is_chroma);
    RESIDUE(2, is_chroma);
    RESIDUE(3, is_chroma);
    RESIDUE(4, is_chroma);
    RESIDUE(5, is_chroma);
    RESIDUE(6, is_chroma);
    RESIDUE(7, is_chroma);
 
    // vertical hadamard tx
    hadamard8x8_one_pass(r0, r1, r2, r3, r4, r5, r6, r7);
 
    // transpose
    transpose_s16_8x8(r0, r1, r2, r3, r4, r5, r6, r7);
 
    // horizontal hadamard tx
    hadamard8x8_one_pass(r0, r1, r2, r3, r4, r5, r6, r7);
}
 
static INLINE UWORD32 ihevce_HAD_8x8_8bit_plane_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD32 is_chroma,
    WORD32 ac_only)
{
    int16x8_t a0, a1, a2, a3, a4, a5, a6, a7;
    int32x4_t b;
    int64x2_t c;
    int64_t satd;
 
    // hadamard 8x8
    hadamard8x8(
        pu1_src, src_strd, pu1_pred, pred_strd, &a0, &a1, &a2, &a3, &a4, &a5, &a6, &a7, is_chroma);
 
    if(ac_only)
    {
        const int16x8_t mask = vld1q_s16(gu2_dc_mask);
        a0 = vandq_s16(a0, mask);
    }
 
    // satd
    a0 = vabsq_s16(a0);
    a1 = vabsq_s16(a1);
    a0 = vaddq_s16(a0, a1);
    a2 = vabsq_s16(a2);
    a3 = vabsq_s16(a3);
    a2 = vaddq_s16(a2, a3);
 
    a4 = vabsq_s16(a4);
    a5 = vabsq_s16(a5);
    a4 = vaddq_s16(a4, a5);
    a6 = vabsq_s16(a6);
    a7 = vabsq_s16(a7);
    a6 = vaddq_s16(a6, a7);
 
    a0 = vaddq_s16(a0, a2);
    a4 = vaddq_s16(a4, a6);
    a0 = vaddq_s16(a0, a4);
    b = vpaddlq_s16(a0);
    c = vpaddlq_s32(b);
    satd = vget_lane_s64(vadd_s64(vget_low_s64(c), vget_high_s64(c)), 0);
 
    return ((satd + 4) >> 3);
}
 
static INLINE UWORD32 ihevce_HAD_4x4_8bit_plane_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD32 is_chroma,
    WORD32 ac_only)
{
    uint8x16_t src_u8, pred_u8;
    int16x8_t res_01, res_23;
    int16x4_t h[4];
    int16x4_t v[4];
    int16x4x2_t trans_4[2];
    int16x8_t combined_rows[4];
    int32x4x2_t trans_8;
    int32x4_t sad_32_4[3];
    int32x2_t sad_32_2;
    int64x1_t sad_64_1;
    int32_t sad;
 
    if(!is_chroma)
    {
        src_u8 = load_unaligned_u8q(pu1_src, src_strd);
        pred_u8 = load_unaligned_u8q(pu1_pred, pred_strd);
    }
    else
    {
        src_u8 = load_unaligned_u8qi(pu1_src, src_strd);
        pred_u8 = load_unaligned_u8qi(pu1_pred, pred_strd);
    }
    res_01 = vreinterpretq_s16_u16(vsubl_u8(vget_low_u8(src_u8), vget_low_u8(pred_u8)));
    res_23 = vreinterpretq_s16_u16(vsubl_u8(vget_high_u8(src_u8), vget_high_u8(pred_u8)));
 
    h[0] = vadd_s16(vget_low_s16(res_01), vget_high_s16(res_23));
    h[1] = vadd_s16(vget_high_s16(res_01), vget_low_s16(res_23));
    h[2] = vsub_s16(vget_high_s16(res_01), vget_low_s16(res_23));
    h[3] = vsub_s16(vget_low_s16(res_01), vget_high_s16(res_23));
 
    v[0] = vadd_s16(h[0], h[1]);
    v[1] = vadd_s16(h[3], h[2]);
    v[2] = vsub_s16(h[0], h[1]);
    v[3] = vsub_s16(h[3], h[2]);
 
    trans_4[0] = vtrn_s16(v[0], v[2]);
    trans_4[1] = vtrn_s16(v[1], v[3]);
 
    combined_rows[0] = vcombine_s16(trans_4[0].val[0], trans_4[1].val[0]);
    combined_rows[1] = vcombine_s16(trans_4[0].val[1], trans_4[1].val[1]);
 
    combined_rows[2] = vaddq_s16(combined_rows[0], combined_rows[1]);
    combined_rows[3] = vsubq_s16(combined_rows[0], combined_rows[1]);
 
    trans_8 =
        vtrnq_s32(vreinterpretq_s32_s16(combined_rows[2]), vreinterpretq_s32_s16(combined_rows[3]));
 
    combined_rows[0] =
        vaddq_s16(vreinterpretq_s16_s32(trans_8.val[0]), vreinterpretq_s16_s32(trans_8.val[1]));
    combined_rows[0] = vabsq_s16(combined_rows[0]);
    combined_rows[1] =
        vsubq_s16(vreinterpretq_s16_s32(trans_8.val[0]), vreinterpretq_s16_s32(trans_8.val[1]));
    combined_rows[1] = vabsq_s16(combined_rows[1]);
 
    if(ac_only)
    {
        const int16x8_t mask = vld1q_s16(gu2_dc_mask);
        combined_rows[0] = vandq_s16(combined_rows[0], mask);
    }
 
    sad_32_4[0] = vpaddlq_s16(combined_rows[0]);
    sad_32_4[1] = vpaddlq_s16(combined_rows[1]);
    sad_32_4[2] = vaddq_s32(sad_32_4[0], sad_32_4[1]);
    sad_32_2 = vadd_s32(vget_high_s32(sad_32_4[2]), vget_low_s32(sad_32_4[2]));
    sad_64_1 = vpaddl_s32(sad_32_2);
    sad = vget_lane_s64(sad_64_1, 0);
 
    return ((sad + 2) >> 2);
}
 
UWORD32 ihevce_HAD_4x4_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    (void)pi2_dst;
    (void)dst_strd;
    return ihevce_HAD_4x4_8bit_plane_neon(pu1_src, src_strd, pu1_pred, pred_strd, 0, 0);
}
 
UWORD32 ihevce_chroma_compute_AC_HAD_4x4_8bit_neon(
    UWORD8 *pu1_origin,
    WORD32 src_strd,
    UWORD8 *pu1_pred_buf,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    (void)pi2_dst;
    (void)dst_strd;
    return ihevce_HAD_4x4_8bit_plane_neon(pu1_origin, src_strd, pu1_pred_buf, pred_strd, 1, 1);
}
 
UWORD32 ihevce_HAD_8x8_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    (void)pi2_dst;
    (void)dst_strd;
    return ihevce_HAD_8x8_8bit_plane_neon(pu1_src, src_strd, pu1_pred, pred_strd, 0, 0);
}
 
UWORD32 ihevce_compute_ac_had_8x8_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    (void)pi2_dst;
    (void)dst_strd;
    return ihevce_HAD_8x8_8bit_plane_neon(pu1_src, src_strd, pu1_pred, pred_strd, 0, 1);
}
 
UWORD32 ihevce_HAD_16x16_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    int16x8_t b0[8];
    int16x8_t b1[8];
    int16x8_t b2[8];
    int16x8_t b3[8];
    uint32x4_t sum = vdupq_n_u32(0);
    uint64x2_t c;
    uint64_t satd;
    WORD32 i;
 
    (void)pi2_dst;
    (void)dst_strd;
 
    // hadamard 8x8 - b0
    hadamard8x8(
        pu1_src,
        src_strd,
        pu1_pred,
        pred_strd,
        &b0[0],
        &b0[1],
        &b0[2],
        &b0[3],
        &b0[4],
        &b0[5],
        &b0[6],
        &b0[7],
        0);
    // hadamard 8x8 - b1
    hadamard8x8(
        pu1_src + 8,
        src_strd,
        pu1_pred + 8,
        pred_strd,
        &b1[0],
        &b1[1],
        &b1[2],
        &b1[3],
        &b1[4],
        &b1[5],
        &b1[6],
        &b1[7],
        0);
    // hadamard 8x8 - b2
    hadamard8x8(
        pu1_src + (8 * src_strd),
        src_strd,
        pu1_pred + (8 * pred_strd),
        pred_strd,
        &b2[0],
        &b2[1],
        &b2[2],
        &b2[3],
        &b2[4],
        &b2[5],
        &b2[6],
        &b2[7],
        0);
    // hadamard 8x8 - b3
    hadamard8x8(
        pu1_src + (8 * src_strd) + 8,
        src_strd,
        pu1_pred + (8 * pred_strd) + 8,
        pred_strd,
        &b3[0],
        &b3[1],
        &b3[2],
        &b3[3],
        &b3[4],
        &b3[5],
        &b3[6],
        &b3[7],
        0);
 
    for(i = 0; i < 8; i++)
    {
        int16x8_t p0 = vhaddq_s16(b0[i], b1[i]);
        int16x8_t p1 = vhsubq_s16(b0[i], b1[i]);
        int16x8_t p2 = vhaddq_s16(b2[i], b3[i]);
        int16x8_t p3 = vhsubq_s16(b2[i], b3[i]);
 
        int16x8_t q0 = vaddq_s16(p0, p2);
        int16x8_t q1 = vsubq_s16(p0, p2);
        int16x8_t q2 = vaddq_s16(p1, p3);
        int16x8_t q3 = vsubq_s16(p1, p3);
 
        uint16x8_t r0 =
            vaddq_u16(vreinterpretq_u16_s16(vabsq_s16(q0)), vreinterpretq_u16_s16(vabsq_s16(q1)));
        uint16x8_t r1 =
            vaddq_u16(vreinterpretq_u16_s16(vabsq_s16(q2)), vreinterpretq_u16_s16(vabsq_s16(q3)));
 
        uint32x4_t s0 = vaddl_u16(vget_low_u16(r0), vget_high_u16(r0));
        uint32x4_t s1 = vaddl_u16(vget_low_u16(r1), vget_high_u16(r1));
 
        sum = vaddq_u32(sum, s0);
        sum = vaddq_u32(sum, s1);
    }
 
    c = vpaddlq_u32(sum);
    satd = vget_lane_u64(vadd_u64(vget_low_u64(c), vget_high_u64(c)), 0);
 
    return ((satd + 4) >> 3);
}
 
UWORD32 ihevce_chroma_HAD_4x4_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    (void)pi2_dst;
    (void)dst_strd;
    return ihevce_HAD_4x4_8bit_plane_neon(pu1_src, src_strd, pu1_pred, pred_strd, 1, 0);
}
 
UWORD32 ihevce_chroma_HAD_8x8_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    (void)pi2_dst;
    (void)dst_strd;
    return ihevce_HAD_8x8_8bit_plane_neon(pu1_src, src_strd, pu1_pred, pred_strd, 1, 0);
}
 
UWORD32 ihevce_chroma_HAD_16x16_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    UWORD32 au4_satd[4];
 
    (void)pi2_dst;
    (void)dst_strd;
    au4_satd[0] = ihevce_HAD_8x8_8bit_plane_neon(pu1_src, src_strd, pu1_pred, pred_strd, 1, 0);
    au4_satd[1] =
        ihevce_HAD_8x8_8bit_plane_neon(pu1_src + 16, src_strd, pu1_pred + 16, pred_strd, 1, 0);
    au4_satd[2] = ihevce_HAD_8x8_8bit_plane_neon(
        pu1_src + 8 * src_strd, src_strd, pu1_pred + 8 * pred_strd, pred_strd, 1, 0);
    au4_satd[3] = ihevce_HAD_8x8_8bit_plane_neon(
        pu1_src + 8 * src_strd + 16, src_strd, pu1_pred + 8 * pred_strd + 16, pred_strd, 1, 0);
 
    return au4_satd[0] + au4_satd[1] + au4_satd[2] + au4_satd[3];
}
 
UWORD32 ihevce_HAD_32x32_8bit_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd)
{
    int16x8_t a[4][4][8];
    uint32x4_t sum = vdupq_n_u32(0);
    WORD32 b8, b16;
    uint64x2_t c;
    uint64_t satd;
    WORD32 i, j;
 
    (void)pi2_dst;
    (void)dst_strd;
    // hadamard 32x32
    for(b16 = 0; b16 < 4; b16++)
    {
        UWORD8 *pu1_src_b16 = pu1_src + (b16 >> 1) * (src_strd * 16) + ((b16 & 1) * 16);
        UWORD8 *pu1_pred_b16 = pu1_pred + (b16 >> 1) * (pred_strd * 16) + ((b16 & 1) * 16);
        // hadamard 16x16
        for(b8 = 0; b8 < 4; b8++)
        {
            UWORD8 *pu1_src_b8 = pu1_src_b16 + (b8 >> 1) * (src_strd * 8) + ((b8 & 1) * 8);
            UWORD8 *pu1_pred_b8 = pu1_pred_b16 + (b8 >> 1) * (pred_strd * 8) + ((b8 & 1) * 8);
            // hadamard 8x8
            hadamard8x8(
                pu1_src_b8,
                src_strd,
                pu1_pred_b8,
                pred_strd,
                &a[b16][b8][0],
                &a[b16][b8][1],
                &a[b16][b8][2],
                &a[b16][b8][3],
                &a[b16][b8][4],
                &a[b16][b8][5],
                &a[b16][b8][6],
                &a[b16][b8][7],
                0);
        }
        for(i = 0; i < 8; i++)
        {
            int16x8_t p0 = vhaddq_s16(a[b16][0][i], a[b16][1][i]);
            int16x8_t p1 = vhsubq_s16(a[b16][0][i], a[b16][1][i]);
            int16x8_t p2 = vhaddq_s16(a[b16][2][i], a[b16][3][i]);
            int16x8_t p3 = vhsubq_s16(a[b16][2][i], a[b16][3][i]);
 
            a[b16][0][i] = vaddq_s16(p0, p2);
            a[b16][1][i] = vsubq_s16(p0, p2);
            a[b16][2][i] = vaddq_s16(p1, p3);
            a[b16][3][i] = vsubq_s16(p1, p3);
 
            a[b16][0][i] = vshrq_n_s16(a[b16][0][i], 2);
            a[b16][1][i] = vshrq_n_s16(a[b16][1][i], 2);
            a[b16][2][i] = vshrq_n_s16(a[b16][2][i], 2);
            a[b16][3][i] = vshrq_n_s16(a[b16][3][i], 2);
        }
    }
    for(j = 0; j < 4; j++)
    {
        for(i = 0; i < 8; i++)
        {
            int16x8_t p0 = vaddq_s16(a[0][j][i], a[1][j][i]);
            int16x8_t p1 = vsubq_s16(a[0][j][i], a[1][j][i]);
            int16x8_t p2 = vaddq_s16(a[2][j][i], a[3][j][i]);
            int16x8_t p3 = vsubq_s16(a[2][j][i], a[3][j][i]);
 
            int16x8_t q0 = vaddq_s16(p0, p2);
            int16x8_t q1 = vsubq_s16(p0, p2);
            int16x8_t q2 = vaddq_s16(p1, p3);
            int16x8_t q3 = vsubq_s16(p1, p3);
 
            uint16x8_t r0 = vaddq_u16(
                vreinterpretq_u16_s16(vabsq_s16(q0)), vreinterpretq_u16_s16(vabsq_s16(q1)));
            uint16x8_t r1 = vaddq_u16(
                vreinterpretq_u16_s16(vabsq_s16(q2)), vreinterpretq_u16_s16(vabsq_s16(q3)));
 
            uint32x4_t s0 = vaddl_u16(vget_low_u16(r0), vget_high_u16(r0));
            uint32x4_t s1 = vaddl_u16(vget_low_u16(r1), vget_high_u16(r1));
 
            sum = vaddq_u32(sum, s0);
            sum = vaddq_u32(sum, s1);
        }
    }
    c = vpaddlq_u32(sum);
    satd = vget_lane_u64(vadd_u64(vget_low_u64(c), vget_high_u64(c)), 0);
 
    return ((satd + 2) >> 2);
}
 
WORD32 ihevce_had4_4x4_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst4x4,
    WORD32 dst_strd,
    WORD32 *pi4_hsad,
    WORD32 hsad_stride,
    WORD32 i4_frm_qstep)
{
    int16x8_t a[8];
 
    (void)pi2_dst4x4;
    (void)dst_strd;
    (void)i4_frm_qstep;
 
    /* -------- Compute four 4x4 HAD Transforms of 8x8 in one call--------- */
    hadamard4x4_4(
        pu1_src,
        src_strd,
        pu1_pred,
        pred_strd,
        &a[0],
        &a[1],
        &a[2],
        &a[3],
        &a[4],
        &a[5],
        &a[6],
        &a[7]);
 
    return hadamard_sad4x4_4(a, pi4_hsad, hsad_stride);
}
 
WORD32 ihevce_had_8x8_using_4_4x4_r_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd,
    WORD32 **ppi4_hsad,
    WORD32 **ppi4_tu_split,
    WORD32 **ppi4_tu_early_cbf,
    WORD32 pos_x_y_4x4,
    WORD32 num_4x4_in_row,
    WORD32 lambda,
    WORD32 lambda_q_shift,
    WORD32 i4_frm_qstep,
    WORD32 i4_cur_depth,
    WORD32 i4_max_depth,
    WORD32 i4_max_tr_size,
    WORD32 *pi4_tu_split_cost,
    void *pv_func_sel)
{
    WORD32 pos_x = pos_x_y_4x4 & 0xFFFF;
    WORD32 pos_y = (pos_x_y_4x4 >> 16) & 0xFFFF;
 
    WORD32 *pi4_4x4_hsad;
    WORD32 *pi4_8x8_hsad;
    WORD32 *pi4_8x8_tu_split;
    WORD32 *pi4_8x8_tu_early_cbf;
 
    WORD32 cost_child, cost_parent;
    WORD32 best_cost;
    WORD32 early_cbf = 0;
    const UWORD8 u1_cur_tr_size = 8;
 
    WORD32 i;
 
    int16x8_t a[8];
 
    (void)pv_func_sel;
 
    assert(pos_x >= 0);
    assert(pos_y >= 0);
 
    /* Initialize pointers to  store 4x4 and 8x8 HAD SATDs */
    pi4_4x4_hsad = ppi4_hsad[HAD_4x4] + pos_x + pos_y * num_4x4_in_row;
    pi4_8x8_hsad = ppi4_hsad[HAD_8x8] + (pos_x >> 1) + (pos_y >> 1) * (num_4x4_in_row >> 1);
    pi4_8x8_tu_split = ppi4_tu_split[HAD_8x8] + (pos_x >> 1) + (pos_y >> 1) * (num_4x4_in_row >> 1);
    pi4_8x8_tu_early_cbf =
        ppi4_tu_early_cbf[HAD_8x8] + (pos_x >> 1) + (pos_y >> 1) * (num_4x4_in_row >> 1);
 
    /* -------- Compute four 4x4 HAD Transforms of 8x8 in one call--------- */
    hadamard4x4_4(
        pu1_src,
        src_strd,
        pu1_pred,
        pred_strd,
        &a[0],
        &a[1],
        &a[2],
        &a[3],
        &a[4],
        &a[5],
        &a[6],
        &a[7]);
 
    /* -------- cost child -------- */
    cost_child = hadamard_sad4x4_4(a, pi4_4x4_hsad, num_4x4_in_row);
    /* 4 CBF Flags, extra 1 becoz of the 0.5 bits per bin is assumed */
    cost_child += ((4) * lambda) >> (lambda_q_shift + 1);
 
    /* -------- cost parent -------- */
    cost_parent = hadamard_sad8x8_using4x4(a, &early_cbf, i4_frm_qstep);
    for(i = 0; i < 8; i++, pi2_dst += dst_strd)
        vst1q_s16(pi2_dst, a[i]);
 
    if(i4_cur_depth < i4_max_depth)
    {
        if((cost_child < cost_parent) || (i4_max_tr_size < u1_cur_tr_size))
        {
            *pi4_tu_split_cost += (4 * lambda) >> (lambda_q_shift + 1);
            best_cost = cost_child;
            best_cost <<= 1;
            best_cost++;
            pi4_8x8_tu_split[0] = 1;
            pi4_8x8_hsad[0] = cost_child;
        }
        else
        {
            best_cost = cost_parent;
            best_cost <<= 1;
            pi4_8x8_tu_split[0] = 0;
            pi4_8x8_hsad[0] = cost_parent;
        }
    }
    else
    {
        best_cost = cost_parent;
        best_cost <<= 1;
        pi4_8x8_tu_split[0] = 0;
        pi4_8x8_hsad[0] = cost_parent;
    }
 
    pi4_8x8_tu_early_cbf[0] = early_cbf;
 
    /* best cost has tu_split_flag at LSB(Least significant bit) */
    return ((best_cost << 1) + early_cbf);
}
 
static WORD32 ihevce_compute_16x16HAD_using_8x8_neon(
    WORD16 *pi2_8x8_had,
    WORD32 had8_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd,
    WORD32 i4_frm_qstep,
    WORD32 *pi4_cbf)
{
    int16x8_t b0[8];
    int16x8_t b1[8];
    int16x8_t b2[8];
    int16x8_t b3[8];
    const int16x8_t threshold = vdupq_n_s16((int16_t)(i4_frm_qstep >> 8));
    uint32x4_t sum = vdupq_n_u32(0);
    uint64x2_t c;
    uint64_t satd;
    WORD32 i;
 
    for(i = 0; i < 8; i++, pi2_8x8_had += had8_strd)
    {
        b0[i] = vld1q_s16(pi2_8x8_had);
        b1[i] = vld1q_s16(pi2_8x8_had + 8);
    }
    for(i = 0; i < 8; i++, pi2_8x8_had += had8_strd)
    {
        b2[i] = vld1q_s16(pi2_8x8_had);
        b3[i] = vld1q_s16(pi2_8x8_had + 8);
    }
 
#define EARLY_EXIT(k)                                                                              \
    {                                                                                              \
        p##k = vabsq_s16(q##k);                                                                    \
        if(*pi4_cbf == 0)                                                                          \
        {                                                                                          \
            uint16x8_t cmp;                                                                        \
            cmp = vcgtq_s16(p##k, threshold);                                                      \
            if(vget_lane_s64(vreinterpret_s64_u16(vget_low_u16(cmp)), 0) ||                        \
               vget_lane_s64(vreinterpret_s64_u16(vget_high_u16(cmp)), 0))                         \
            {                                                                                      \
                *pi4_cbf = 1;                                                                      \
            }                                                                                      \
        }                                                                                          \
    }
    for(i = 0; i < 8; i++, pi2_dst += dst_strd)
    {
        int16x8_t p0 = vhaddq_s16(b0[i], b1[i]);
        int16x8_t p1 = vhsubq_s16(b0[i], b1[i]);
        int16x8_t p2 = vhaddq_s16(b2[i], b3[i]);
        int16x8_t p3 = vhsubq_s16(b2[i], b3[i]);
 
        int16x8_t q0 = vaddq_s16(p0, p2);
        int16x8_t q1 = vsubq_s16(p0, p2);
        int16x8_t q2 = vaddq_s16(p1, p3);
        int16x8_t q3 = vsubq_s16(p1, p3);
 
        vst1q_s16(pi2_dst, q0);
        EARLY_EXIT(0);
        vst1q_s16(pi2_dst + 8, q1);
        EARLY_EXIT(1);
        vst1q_s16(pi2_dst + 8 * dst_strd, q2);
        EARLY_EXIT(2);
        vst1q_s16(pi2_dst + 8 * dst_strd + 8, q3);
        EARLY_EXIT(3);
        uint16x8_t r0 = vaddq_u16(vreinterpretq_u16_s16(p0), vreinterpretq_u16_s16(p1));
        uint16x8_t r1 = vaddq_u16(vreinterpretq_u16_s16(p2), vreinterpretq_u16_s16(p3));
 
        uint32x4_t s0 = vaddl_u16(vget_low_u16(r0), vget_high_u16(r0));
        uint32x4_t s1 = vaddl_u16(vget_low_u16(r1), vget_high_u16(r1));
 
        sum = vaddq_u32(sum, s0);
        sum = vaddq_u32(sum, s1);
    }
 
    c = vpaddlq_u32(sum);
    satd = vget_lane_u64(vadd_u64(vget_low_u64(c), vget_high_u64(c)), 0);
 
    return ((satd + 4) >> 3);
}
 
WORD32 ihevce_had_16x16_r_neon(
    UWORD8 *pu1_src,
    WORD32 src_strd,
    UWORD8 *pu1_pred,
    WORD32 pred_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd,
    WORD32 **ppi4_hsad,
    WORD32 **ppi4_tu_split,
    WORD32 **ppi4_tu_early_cbf,
    WORD32 pos_x_y_4x4,
    WORD32 num_4x4_in_row,
    WORD32 lambda,
    WORD32 lambda_q_shift,
    WORD32 i4_frm_qstep,
    WORD32 i4_cur_depth,
    WORD32 i4_max_depth,
    WORD32 i4_max_tr_size,
    WORD32 *pi4_tu_split_cost,
    void *pv_func_sel)
{
    WORD16 ai2_8x8_had[256];
 
    WORD32 *pi4_16x16_hsad;
    WORD32 *pi4_16x16_tu_split;
    WORD32 *pi4_16x16_tu_early_cbf;
 
    WORD32 best_cost, best_cost_tu_split;
    WORD32 tu_split_flag = 0;
    WORD32 i4_early_cbf_flag = 0, early_cbf = 0;
    WORD32 cost_parent, cost_child = 0;
 
    const UWORD8 u1_cur_tr_size = 16;
 
    WORD32 i;
 
    WORD16 *pi2_y0;
    UWORD8 *src, *pred;
    WORD32 pos_x_y_4x4_0;
 
    WORD32 pos_x = pos_x_y_4x4 & 0xFFFF;
    WORD32 pos_y = (pos_x_y_4x4 >> 16) & 0xFFFF;
 
    assert(pos_x >= 0);
    assert(pos_y >= 0);
 
    /* Initialize pointers to  store 16x16 SATDs */
    pi4_16x16_hsad = ppi4_hsad[HAD_16x16] + (pos_x >> 2) + (pos_y >> 2) * (num_4x4_in_row >> 2);
 
    pi4_16x16_tu_split =
        ppi4_tu_split[HAD_16x16] + (pos_x >> 2) + (pos_y >> 2) * (num_4x4_in_row >> 2);
 
    pi4_16x16_tu_early_cbf =
        ppi4_tu_early_cbf[HAD_16x16] + (pos_x >> 2) + (pos_y >> 2) * (num_4x4_in_row >> 2);
 
    /* -------- Compute four 8x8 HAD Transforms of 16x16 call--------- */
    for(i = 0; i < 4; i++)
    {
        src = pu1_src + (i & 0x01) * 8 + (i >> 1) * src_strd * 8;
        pred = pu1_pred + (i & 0x01) * 8 + (i >> 1) * pred_strd * 8;
        pi2_y0 = ai2_8x8_had + (i & 0x01) * 8 + (i >> 1) * 16 * 8;
        pos_x_y_4x4_0 = pos_x_y_4x4 + (i & 0x01) * 2 + (i >> 1) * (2 << 16);
 
        best_cost_tu_split = ihevce_had_8x8_using_4_4x4_r_neon(
            src,
            src_strd,
            pred,
            pred_strd,
            pi2_y0,
            16,
            ppi4_hsad,
            ppi4_tu_split,
            ppi4_tu_early_cbf,
            pos_x_y_4x4_0,
            num_4x4_in_row,
            lambda,
            lambda_q_shift,
            i4_frm_qstep,
            i4_cur_depth + 1,
            i4_max_depth,
            i4_max_tr_size,
            pi4_tu_split_cost,
            pv_func_sel);
 
        /* Cost is shifted by two bits for Tu_split_flag and early cbf flag */
        best_cost = (best_cost_tu_split >> 2);
 
        /* Last but one bit stores the information regarding the TU_Split */
        tu_split_flag += (best_cost_tu_split & 0x3) >> 1;
 
        /* Last bit stores the information regarding the early_cbf */
        i4_early_cbf_flag += (best_cost_tu_split & 0x1);
 
        cost_child += best_cost;
 
        tu_split_flag <<= 1;
        i4_early_cbf_flag <<= 1;
    }
 
    /* -------- Compute 16x16 HAD Transform using 8x8 results ------------- */
    pi2_y0 = ai2_8x8_had;
 
    /* Threshold currently passed as "0" */
    cost_parent = ihevce_compute_16x16HAD_using_8x8_neon(
        pi2_y0, 16, pi2_dst, dst_strd, i4_frm_qstep, &early_cbf);
 
    /* 4 TU_Split flags , 4 CBF Flags, extra 1 becoz of the 0.5 bits per bin is assumed */
    cost_child += ((4 + 4) * lambda) >> (lambda_q_shift + 1);
 
    i4_early_cbf_flag += early_cbf;
 
    /* Right now the depth is hard-coded to 4: The depth can be modified from the config file
    which decides the extent to which TU_REC needs to be done */
    if(i4_cur_depth < i4_max_depth)
    {
        if((cost_child < cost_parent) || (i4_max_tr_size < u1_cur_tr_size))
        {
            *pi4_tu_split_cost += ((4 + 4) * lambda) >> (lambda_q_shift + 1);
            tu_split_flag += 1;
            best_cost = cost_child;
        }
        else
        {
            tu_split_flag += 0;
            best_cost = cost_parent;
        }
    }
    else
    {
        tu_split_flag += 0;
        best_cost = cost_parent;
    }
 
    pi4_16x16_hsad[0] = best_cost;
    pi4_16x16_tu_split[0] = tu_split_flag;
    pi4_16x16_tu_early_cbf[0] = i4_early_cbf_flag;
 
    /*returning two values(best cost & tu_split_flag) as a single value*/
    return ((best_cost << 10) + (tu_split_flag << 5) + i4_early_cbf_flag);
}
 
UWORD32 ihevce_compute_32x32HAD_using_16x16_neon(
    WORD16 *pi2_16x16_had,
    WORD32 had16_strd,
    WORD16 *pi2_dst,
    WORD32 dst_strd,
    WORD32 i4_frm_qstep,
    WORD32 *pi4_cbf)
{
    int16x8_t a[4][4][8];
    uint32x4_t sum = vdupq_n_u32(0);
    const int16x8_t threshold = vdupq_n_s16((int16_t)(i4_frm_qstep >> 8));
    WORD32 b8, b16;
    uint64x2_t c;
    WORD32 i, j;
 
    (void)pi2_dst;
    (void)dst_strd;
 
    for(b16 = 0; b16 < 4; b16++)
    {
        WORD16 *pi2_b16 = pi2_16x16_had + (b16 >> 1) * (had16_strd * 16) + ((b16 & 1) * 16);
 
        for(b8 = 0; b8 < 4; b8++)
        {
            WORD16 *pi2_b8 = pi2_b16 + (b8 >> 1) * (had16_strd * 8) + ((b8 & 1) * 8);
 
            for(i = 0; i < 8; i++, pi2_b8 += had16_strd)
            {
                a[b16][b8][i] = vld1q_s16(pi2_b8);
                a[b16][b8][i] = vshrq_n_s16(a[b16][b8][i], 2);
            }
        }
    }
 
    for(j = 0; j < 4; j++)
    {
        for(i = 0; i < 8; i++)
        {
            int16x8_t p0 = vaddq_s16(a[0][j][i], a[1][j][i]);
            int16x8_t p1 = vsubq_s16(a[0][j][i], a[1][j][i]);
            int16x8_t p2 = vaddq_s16(a[2][j][i], a[3][j][i]);
            int16x8_t p3 = vsubq_s16(a[2][j][i], a[3][j][i]);
 
            int16x8_t q0 = vaddq_s16(p0, p2);
            int16x8_t q1 = vsubq_s16(p0, p2);
            int16x8_t q2 = vaddq_s16(p1, p3);
            int16x8_t q3 = vsubq_s16(p1, p3);
 
            EARLY_EXIT(0);
            EARLY_EXIT(1);
            EARLY_EXIT(2);
            EARLY_EXIT(3);
 
            uint16x8_t r0 = vaddq_u16(vreinterpretq_u16_s16(p0), vreinterpretq_u16_s16(p1));
            uint16x8_t r1 = vaddq_u16(vreinterpretq_u16_s16(p2), vreinterpretq_u16_s16(p3));
 
            uint32x4_t s0 = vaddl_u16(vget_low_u16(r0), vget_high_u16(r0));
            uint32x4_t s1 = vaddl_u16(vget_low_u16(r1), vget_high_u16(r1));
 
            sum = vaddq_u32(sum, s0);
            sum = vaddq_u32(sum, s1);
        }
    }
    c = vpaddlq_u32(sum);
 
    return vget_lane_u64(vadd_u64(vget_low_u64(c), vget_high_u64(c)), 0);
}