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
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
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
/******************************************************************************
 *
 * 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_ipe_pass.c
*
* \brief
*    This file contains interface functions of Intra Prediction Estimation
*    module
* \date
*    18/09/2012
*
* \author
*    Ittiam
*
*
* List of Functions
*
*
******************************************************************************
*/
 
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* System include files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <math.h>
 
/* User include files */
#include "ihevc_typedefs.h"
#include "itt_video_api.h"
#include "ihevce_api.h"
 
#include "rc_cntrl_param.h"
#include "rc_frame_info_collector.h"
#include "rc_look_ahead_params.h"
 
#include "ihevc_debug.h"
#include "ihevc_defs.h"
#include "ihevc_structs.h"
#include "ihevc_platform_macros.h"
#include "ihevc_deblk.h"
#include "ihevc_itrans_recon.h"
#include "ihevc_chroma_itrans_recon.h"
#include "ihevc_chroma_intra_pred.h"
#include "ihevc_intra_pred.h"
#include "ihevc_inter_pred.h"
#include "ihevc_mem_fns.h"
#include "ihevc_padding.h"
#include "ihevc_weighted_pred.h"
#include "ihevc_sao.h"
#include "ihevc_resi_trans.h"
#include "ihevc_quant_iquant_ssd.h"
#include "ihevc_cabac_tables.h"
#include "ihevc_quant_tables.h"
 
#include "ihevce_defs.h"
#include "ihevce_hle_interface.h"
#include "ihevce_lap_enc_structs.h"
#include "ihevce_multi_thrd_structs.h"
#include "ihevce_multi_thrd_funcs.h"
#include "ihevce_me_common_defs.h"
#include "ihevce_had_satd.h"
#include "ihevce_error_codes.h"
#include "ihevce_bitstream.h"
#include "ihevce_cabac.h"
#include "ihevce_rdoq_macros.h"
#include "ihevce_function_selector.h"
#include "ihevce_enc_structs.h"
#include "ihevce_entropy_structs.h"
#include "ihevce_cmn_utils_instr_set_router.h"
#include "ihevce_enc_loop_structs.h"
#include "ihevce_inter_pred.h"
#include "ihevc_weighted_pred.h"
#include "ihevce_ipe_instr_set_router.h"
#include "ihevce_ipe_structs.h"
#include "ihevce_ipe_pass.h"
#include "ihevce_decomp_pre_intra_structs.h"
#include "ihevce_decomp_pre_intra_pass.h"
#include "ihevce_recur_bracketing.h"
#include "ihevce_nbr_avail.h"
#include "ihevce_global_tables.h"
#include "ihevc_resi_trans.h"
 
#include "cast_types.h"
#include "osal.h"
#include "osal_defaults.h"
 
/*****************************************************************************/
/* Global Tables                                                             */
/*****************************************************************************/
 
/**
******************************************************************************
* @brief  Look up table for choosing the appropriate function for
*         Intra prediction
*
* @remarks Same look up table enums are used for luma & chroma but each
*          have seperate functions implemented
******************************************************************************
*/
WORD32 g_i4_ipe_funcs[MAX_NUM_IP_MODES] = {
    IPE_FUNC_MODE_0, /* Mode 0 */
    IPE_FUNC_MODE_1, /* Mode 1 */
    IPE_FUNC_MODE_2, /* Mode 2 */
    IPE_FUNC_MODE_3TO9, /* Mode 3 */
    IPE_FUNC_MODE_3TO9, /* Mode 4 */
    IPE_FUNC_MODE_3TO9, /* Mode 5 */
    IPE_FUNC_MODE_3TO9, /* Mode 6 */
    IPE_FUNC_MODE_3TO9, /* Mode 7 */
    IPE_FUNC_MODE_3TO9, /* Mode 8 */
    IPE_FUNC_MODE_3TO9, /* Mode 9 */
    IPE_FUNC_MODE_10, /* Mode 10 */
    IPE_FUNC_MODE_11TO17, /* Mode 11 */
    IPE_FUNC_MODE_11TO17, /* Mode 12 */
    IPE_FUNC_MODE_11TO17, /* Mode 13 */
    IPE_FUNC_MODE_11TO17, /* Mode 14 */
    IPE_FUNC_MODE_11TO17, /* Mode 15 */
    IPE_FUNC_MODE_11TO17, /* Mode 16 */
    IPE_FUNC_MODE_11TO17, /* Mode 17 */
    IPE_FUNC_MODE_18_34, /* Mode 18 */
    IPE_FUNC_MODE_19TO25, /* Mode 19 */
    IPE_FUNC_MODE_19TO25, /* Mode 20 */
    IPE_FUNC_MODE_19TO25, /* Mode 21 */
    IPE_FUNC_MODE_19TO25, /* Mode 22 */
    IPE_FUNC_MODE_19TO25, /* Mode 23 */
    IPE_FUNC_MODE_19TO25, /* Mode 24 */
    IPE_FUNC_MODE_19TO25, /* Mode 25 */
    IPE_FUNC_MODE_26, /* Mode 26 */
    IPE_FUNC_MODE_27TO33, /* Mode 27 */
    IPE_FUNC_MODE_27TO33, /* Mode 26 */
    IPE_FUNC_MODE_27TO33, /* Mode 29 */
    IPE_FUNC_MODE_27TO33, /* Mode 30 */
    IPE_FUNC_MODE_27TO33, /* Mode 31 */
    IPE_FUNC_MODE_27TO33, /* Mode 32 */
    IPE_FUNC_MODE_27TO33, /* Mode 33 */
    IPE_FUNC_MODE_18_34, /* Mode 34 */
};
 
/**
******************************************************************************
* @brief  Look up table for deciding whether to use original samples or
*         filtered reference samples for Intra prediction
*
* @remarks This table has the flags for transform size of 8, 16 and 32
*          Input is log2nT - 3 and intra prediction mode
******************************************************************************
*/
UWORD8 gau1_ipe_filter_flag[3][MAX_NUM_IP_MODES] = {
    { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 }
};
 
/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/
 
/*!
******************************************************************************
* \if Function name : ihevce_ipe_recompute_lambda_from_min_8x8_act_in_ctb \endif
*
* \brief
*    This function recomputes lambda using min 8x8 act in CTB
*
* \author
*    Ittiam
*
* \return
*    Nothing
*
******************************************************************************
*/
void ihevce_ipe_recompute_lambda_from_min_8x8_act_in_ctb(
    ihevce_ipe_ctxt_t *ps_ctxt, ihevce_ed_ctb_l1_t *ps_ed_ctb_l1)
{
    WORD32 i4_cu_qp = 0;
    WORD32 i4_mod_factor_num;
#if MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON
    WORD32 i4_activity;
#endif
    WORD32 i4_qscale;
    WORD32 i4_curr_satd;
    long double ld_avg_satd;
 
#if MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON
    WORD32 i4_mod_factor_denom = QP_MOD_FACTOR_DEN;
#endif
 
    if(ISLICE == ps_ctxt->i4_slice_type)
    {
        i4_mod_factor_num = INTRA_QP_MOD_FACTOR_NUM;
    }
    else
    {
        i4_mod_factor_num = INTER_QP_MOD_FACTOR_NUM;
    }
 
#if LAMDA_BASED_ON_QUANT
    i4_curr_satd = ps_ed_ctb_l1->i4_32x32_satd[0][2];
    i8_avg_satd = ps_ctxt->i8_curr_frame_32x32_avg_act[2];
#else
    i4_curr_satd = ps_ed_ctb_l1->i4_32x32_satd[0][3];
 
    ld_avg_satd = 2.0 + ps_ctxt->ld_curr_frame_16x16_log_avg[0];
 
#endif
    if(ps_ctxt->i4_l0ipe_qp_mod)
    {
#if MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON
        i4_cu_qp = ihevce_cu_level_qp_mod(
            ps_ctxt->i4_qscale,
            i4_curr_satd,
            ld_avg_satd,
            ps_ctxt->f_strength,
            &i4_activity,
            &i4_qscale,
            ps_ctxt->ps_rc_quant_ctxt);
#endif
    }
    ihevce_get_ipe_ol_cu_lambda_prms(ps_ctxt, i4_cu_qp);
}
/*!
******************************************************************************
* \if Function name : ihevce_ipe_pass_satd \endif
*
* \brief
*    This function calcuates the SATD for a given size and returns the value
*
* \date
*    18/09/2012
*
* \author
*    Ittiam
*
* \return
*
* List of Functions
*
******************************************************************************
*/
UWORD32 ihevce_ipe_pass_satd(WORD16 *pi2_coeff, WORD32 coeff_stride, WORD32 trans_size)
{
    WORD32 i, j, satd;
 
    satd = 0;
 
    /* run a loop and find the satd by doing ABS */
    for(i = 0; i < trans_size; i++)
    {
        for(j = 0; j < trans_size; j++)
        {
            satd += abs(*pi2_coeff++);
        }
        /* row level update */
        pi2_coeff += coeff_stride - trans_size;
    }
 
    {
        WORD32 transform_shift;
        WORD32 log2_trans_size;
 
        GETRANGE(log2_trans_size, trans_size);
        log2_trans_size -= 1;
        transform_shift = MAX_TR_DYNAMIC_RANGE - BIT_DEPTH - log2_trans_size;
        satd >>= transform_shift;
    }
 
    return (satd);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_ipe_get_num_mem_recs \endif
*
* \brief
*    Number of memory records are returned for IPE module
*
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
WORD32 ihevce_ipe_get_num_mem_recs(void)
{
    return (NUM_IPE_MEM_RECS);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_ipe_get_mem_recs \endif
*
* \brief
*    Memory requirements are returned for IPE.
*
* \param[in,out]  ps_mem_tab : pointer to memory descriptors table
* \param[in] ps_init_prms : Create time static parameters
* \param[in] i4_num_proc_thrds : Number of processing threads for this module
* \param[in] i4_mem_space : memspace in whihc memory request should be done
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
WORD32
    ihevce_ipe_get_mem_recs(iv_mem_rec_t *ps_mem_tab, WORD32 i4_num_proc_thrds, WORD32 i4_mem_space)
{
    /* memories should be requested assuming worst case requirememnts */
 
    /* Module context structure */
    ps_mem_tab[IPE_CTXT].i4_mem_size = sizeof(ihevce_ipe_master_ctxt_t);
 
    ps_mem_tab[IPE_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
 
    ps_mem_tab[IPE_CTXT].i4_mem_alignment = 8;
 
    /* Threads ctxt structure */
    ps_mem_tab[IPE_THRDS_CTXT].i4_mem_size = i4_num_proc_thrds * sizeof(ihevce_ipe_ctxt_t);
 
    ps_mem_tab[IPE_THRDS_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
 
    ps_mem_tab[IPE_THRDS_CTXT].i4_mem_alignment = 32;
 
    return (NUM_IPE_MEM_RECS);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_ipe_init \endif
*
* \brief
*    Intialization for IPE context state structure .
*
* \param[in] ps_mem_tab : pointer to memory descriptors table
* \param[in] ps_init_prms : Create time static parameters
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void *ihevce_ipe_init(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    WORD32 i4_num_proc_thrds,
    WORD32 i4_ref_id,
    func_selector_t *ps_func_selector,
    rc_quant_t *ps_rc_quant_ctxt,
    WORD32 i4_resolution_id,
    UWORD8 u1_is_popcnt_available)
{
    WORD32 i4_thrds;
    UWORD32 u4_width, u4_ctb_in_a_row;
    //  WORD32 i4_ctr;
    ihevce_ipe_master_ctxt_t *ps_master_ctxt;
    ihevce_ipe_ctxt_t *ps_ctxt;
 
    /* IPE master state structure */
    ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)ps_mem_tab[IPE_CTXT].pv_base;
 
    ps_master_ctxt->i4_num_proc_thrds = i4_num_proc_thrds;
 
    ps_ctxt = (ihevce_ipe_ctxt_t *)ps_mem_tab[IPE_THRDS_CTXT].pv_base;
 
    ps_ctxt->ps_rc_quant_ctxt = ps_rc_quant_ctxt;
 
    /*width of the input YUV to be encoded. */
    u4_width = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width;
    /*making the width a multiple of CTB size*/
    u4_width += SET_CTB_ALIGN(
        ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width, MAX_CTB_SIZE);
 
    u4_ctb_in_a_row = (u4_width / MAX_CTB_SIZE);
 
    /* perform all one initialisation here */
    for(i4_thrds = 0; i4_thrds < ps_master_ctxt->i4_num_proc_thrds; i4_thrds++)
    {
        ps_master_ctxt->aps_ipe_thrd_ctxt[i4_thrds] = ps_ctxt;
 
        /* initialise the CU and TU sizes */
        ps_ctxt->u1_ctb_size = (1 << ps_init_prms->s_config_prms.i4_max_log2_cu_size);
        ps_ctxt->u1_min_cu_size = (1 << ps_init_prms->s_config_prms.i4_min_log2_cu_size);
        ps_ctxt->u1_min_tu_size = (1 << ps_init_prms->s_config_prms.i4_min_log2_tu_size);
 
        /** Register the function selector pointer*/
        ps_ctxt->ps_func_selector = ps_func_selector;
 
        /* Initiailize the encoder quality preset           */
        /* IPE algorithm is controlled based on this preset */
        ps_ctxt->i4_quality_preset =
            ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset;
 
        if(ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P7)
        {
            ps_ctxt->i4_quality_preset = IHEVCE_QUALITY_P6;
        }
 
        /* initialise all the pointer to start of arrays */
        ps_ctxt->ps_ipe_cu_tree = &ps_ctxt->as_ipe_cu_tree[0];
 
        /* initialize QP */
        ps_ctxt->i1_QP =
            ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].ai4_frame_qp[i4_ref_id];
        ps_ctxt->u1_num_b_frames =
            (1 << ps_init_prms->s_coding_tools_prms.i4_max_temporal_layers) - 1;
 
        ps_ctxt->b_sad_type = IPE_SAD_TYPE;
        ps_ctxt->u1_ipe_step_size = IPE_STEP_SIZE;
 
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_0] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_planar_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_1] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_dc_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_2] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode2_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_3TO9] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_3_to_9_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_10] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_horz_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_11TO17] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_11_to_17_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_18_34] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_18_34_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_19TO25] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_19_to_25_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_26] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_ver_fptr;
        ps_ctxt->apf_ipe_lum_ip[IPE_FUNC_MODE_27TO33] =
            ps_ctxt->ps_func_selector->ihevc_intra_pred_luma_mode_27_to_33_fptr;
 
        /* nbr parameters initialization */
        /* perform all one initialisation here */
 
        ps_ctxt->i4_nbr_map_strd = MAX_PU_IN_CTB_ROW + 1 + 8;
 
        ps_ctxt->pu1_ctb_nbr_map = ps_ctxt->au1_nbr_ctb_map[0];
 
        /* move the pointer to 1,2 location */
        ps_ctxt->pu1_ctb_nbr_map += ps_ctxt->i4_nbr_map_strd;
        ps_ctxt->pu1_ctb_nbr_map++;
        ps_ctxt->i4_l0ipe_qp_mod = ps_init_prms->s_config_prms.i4_cu_level_rc & 1;
        ps_ctxt->i4_pass = ps_init_prms->s_pass_prms.i4_pass;
        if(ps_init_prms->s_coding_tools_prms.i4_use_default_sc_mtx == 0)
        {
            /* initialise the scale & rescale matricies */
            ps_ctxt->api2_scal_mat[0] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[1] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[2] = (WORD16 *)&gi2_flat_scale_mat_8x8[0];
            ps_ctxt->api2_scal_mat[3] = (WORD16 *)&gi2_flat_scale_mat_16x16[0];
            ps_ctxt->api2_scal_mat[4] = (WORD16 *)&gi2_flat_scale_mat_32x32[0];
            /*init for inter matrix*/
            ps_ctxt->api2_scal_mat[5] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[6] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[7] = (WORD16 *)&gi2_flat_scale_mat_8x8[0];
            ps_ctxt->api2_scal_mat[8] = (WORD16 *)&gi2_flat_scale_mat_16x16[0];
            ps_ctxt->api2_scal_mat[9] = (WORD16 *)&gi2_flat_scale_mat_32x32[0];
 
            /*init for rescale matrix*/
            ps_ctxt->api2_rescal_mat[0] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[1] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[2] = (WORD16 *)&gi2_flat_rescale_mat_8x8[0];
            ps_ctxt->api2_rescal_mat[3] = (WORD16 *)&gi2_flat_rescale_mat_16x16[0];
            ps_ctxt->api2_rescal_mat[4] = (WORD16 *)&gi2_flat_rescale_mat_32x32[0];
            /*init for rescale inter matrix*/
            ps_ctxt->api2_rescal_mat[5] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[6] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[7] = (WORD16 *)&gi2_flat_rescale_mat_8x8[0];
            ps_ctxt->api2_rescal_mat[8] = (WORD16 *)&gi2_flat_rescale_mat_16x16[0];
            ps_ctxt->api2_rescal_mat[9] = (WORD16 *)&gi2_flat_rescale_mat_32x32[0];
        }
        else if(ps_init_prms->s_coding_tools_prms.i4_use_default_sc_mtx == 1)
        {
            /* initialise the scale & rescale matricies */
            ps_ctxt->api2_scal_mat[0] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[1] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[2] = (WORD16 *)&gi2_intra_default_scale_mat_8x8[0];
            ps_ctxt->api2_scal_mat[3] = (WORD16 *)&gi2_intra_default_scale_mat_16x16[0];
            ps_ctxt->api2_scal_mat[4] = (WORD16 *)&gi2_intra_default_scale_mat_32x32[0];
            /*init for inter matrix*/
            ps_ctxt->api2_scal_mat[5] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[6] = (WORD16 *)&gi2_flat_scale_mat_4x4[0];
            ps_ctxt->api2_scal_mat[7] = (WORD16 *)&gi2_inter_default_scale_mat_8x8[0];
            ps_ctxt->api2_scal_mat[8] = (WORD16 *)&gi2_inter_default_scale_mat_16x16[0];
            ps_ctxt->api2_scal_mat[9] = (WORD16 *)&gi2_inter_default_scale_mat_32x32[0];
 
            /*init for rescale matrix*/
            ps_ctxt->api2_rescal_mat[0] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[1] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[2] = (WORD16 *)&gi2_intra_default_rescale_mat_8x8[0];
            ps_ctxt->api2_rescal_mat[3] = (WORD16 *)&gi2_intra_default_rescale_mat_16x16[0];
            ps_ctxt->api2_rescal_mat[4] = (WORD16 *)&gi2_intra_default_rescale_mat_32x32[0];
            /*init for rescale inter matrix*/
            ps_ctxt->api2_rescal_mat[5] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[6] = (WORD16 *)&gi2_flat_rescale_mat_4x4[0];
            ps_ctxt->api2_rescal_mat[7] = (WORD16 *)&gi2_inter_default_rescale_mat_8x8[0];
            ps_ctxt->api2_rescal_mat[8] = (WORD16 *)&gi2_inter_default_rescale_mat_16x16[0];
            ps_ctxt->api2_rescal_mat[9] = (WORD16 *)&gi2_inter_default_rescale_mat_32x32[0];
        }
        else
        {
            ASSERT(0);
        }
 
        ps_ctxt->u1_bit_depth = ps_init_prms->s_tgt_lyr_prms.i4_internal_bit_depth;
 
        /**
        * Initialize the intra prediction modes map for the CTB to INTRA_DC
        **/
        {
            WORD32 row, col;
            for(row = 0; row < (MAX_TU_ROW_IN_CTB + 1); row++)
                for(col = 0; col < (MAX_TU_COL_IN_CTB + 1); col++)
                    ps_ctxt->au1_ctb_mode_map[row][col] = INTRA_DC;
        }
 
        ihevce_cmn_utils_instr_set_router(
            &ps_ctxt->s_cmn_opt_func, u1_is_popcnt_available, ps_init_prms->e_arch_type);
 
        ihevce_ipe_instr_set_router(
            &ps_ctxt->s_ipe_optimised_function_list, ps_init_prms->e_arch_type);
 
        /* increment the thread ctxt pointer */
        ps_ctxt++;
    }
 
    /* return the handle to caller */
    return ((void *)ps_master_ctxt);
}
/*!
******************************************************************************
* \if Function name : ihevce_ipe_get_frame_intra_satd_cost \endif
*
* \brief
*    Function to export frame-level accumalated SATD .
*
* \param[in] pv_ctxt : pointer to IPE module
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
LWORD64 ihevce_ipe_get_frame_intra_satd_cost(
    void *pv_ctxt,
    LWORD64 *pi8_frame_satd_by_qpmod,
    LWORD64 *pi8_frame_acc_mode_bits_cost,
    LWORD64 *pi8_frame_acc_activity_factor,
    LWORD64 *pi8_frame_l0_acc_satd)
{
    WORD32 i4_thrds;
 
    ihevce_ipe_master_ctxt_t *ps_master_ctxt;
    ihevce_ipe_ctxt_t *ps_ctxt;
    LWORD64 i8_frame_acc_satd_cost = 0;
    LWORD64 i8_frame_acc_satd = 0;
    LWORD64 i8_frame_satd_by_qpmod = 0;
    LWORD64 i8_frame_acc_mode_bits_cost = 0;
    LWORD64 i8_frame_acc_activity_factor = 0;
    /* IPE master state structure */
    ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)pv_ctxt;
 
    /* perform all one initialisation here */
    for(i4_thrds = 0; i4_thrds < ps_master_ctxt->i4_num_proc_thrds; i4_thrds++)
    {
        ps_ctxt = ps_master_ctxt->aps_ipe_thrd_ctxt[i4_thrds];
 
        i8_frame_acc_satd_cost += ps_ctxt->i8_frame_acc_satd_cost;
        i8_frame_satd_by_qpmod += (ps_ctxt->i8_frame_acc_satd_by_modqp_q10 >> SATD_BY_ACT_Q_FAC);
        i8_frame_acc_mode_bits_cost += ps_ctxt->i8_frame_acc_mode_bits_cost;
 
        i8_frame_acc_activity_factor += ps_ctxt->i8_frame_acc_act_factor;
 
        i8_frame_acc_satd += ps_ctxt->i8_frame_acc_satd;
    }
    *pi8_frame_satd_by_qpmod = i8_frame_satd_by_qpmod;
 
    *pi8_frame_acc_mode_bits_cost = i8_frame_acc_mode_bits_cost;
 
    *pi8_frame_acc_activity_factor = i8_frame_acc_activity_factor;
 
    *pi8_frame_l0_acc_satd = i8_frame_acc_satd;
 
    return (i8_frame_acc_satd_cost);
}
 
/**
*******************************************************************************
* \if Function name : ihevce_intra_pred_ref_filtering \endif
*
* \brief
*    Intra prediction interpolation filter for ref_filtering for Encoder
*
* \par Description:
*    Reference DC filtering for neighboring samples dependent  on TU size and
*    mode  Refer to section 8.4.4.2.3 in the standard
*
* \param[in] pu1_src pointer to the source
* \param[out] pu1_dst pointer to the destination
* \param[in] nt integer Transform Block size
*
* \returns
*  none
*
* \author
*  Ittiam
*
*******************************************************************************
*/
 
#if IHEVCE_INTRA_REF_FILTERING == C
void ihevce_intra_pred_ref_filtering(UWORD8 *pu1_src, WORD32 nt, UWORD8 *pu1_dst)
{
    WORD32 i; /* Generic indexing variable */
    WORD32 four_nt = 4 * nt;
 
    /* Extremities Untouched*/
    pu1_dst[0] = pu1_src[0];
    pu1_dst[4 * nt] = pu1_src[4 * nt];
    /* Perform bilinear filtering of Reference Samples */
    for(i = 0; i < (four_nt - 1); i++)
    {
        pu1_dst[i + 1] = (pu1_src[i] + 2 * pu1_src[i + 1] + pu1_src[i + 2] + 2) >> 2;
    }
}
#endif
 
/*!
******************************************************************************
* \if Function name : ihevce_ipe_process_ctb \endif
*
* \brief
*    CTB level IPE function
*
* \param[in] pv_ctxt : pointer to IPE module
* \param[in] ps_frm_ctb_prms : CTB characteristics parameters
* \param[in] ps_curr_src  : pointer to input yuv buffer (row buffer)
* \param[out] ps_ctb_out : pointer to CTB analyse output structure (row buffer)
* \param[out] ps_row_cu : pointer to CU analyse output structure (row buffer)
*
* \return
*    None
*
* Note : This function will receive CTB pointers which may point to
* blocks of CTB size or smaller (at the right and bottom edges of the picture)
* This function recursively creates smaller square partitions and passes them
* on for intra processing estimation
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_ipe_process_ctb(
    ihevce_ipe_ctxt_t *ps_ctxt,
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
    iv_enc_yuv_buf_t *ps_curr_src,
    ihevce_ipe_cu_tree_t *ps_curr_ctb_node,
    ipe_l0_ctb_analyse_for_me_t *ps_l0_ipe_out_ctb,
    ctb_analyse_t *ps_ctb_out,
    //cu_analyse_t      *ps_row_cu,
    ihevce_ed_blk_t *ps_ed_l1_ctb,
    ihevce_ed_blk_t *ps_ed_l2_ctb,
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1)
{
    /* reset the map buffer to 0*/
    memset(
        &ps_ctxt->au1_nbr_ctb_map[0][0],
        0,
        (MAX_PU_IN_CTB_ROW + 1 + 8) * (MAX_PU_IN_CTB_ROW + 1 + 8));
 
    /* set the CTB neighbour availability flags */
    ihevce_set_ctb_nbr(
        &ps_ctxt->s_ctb_nbr_avail_flags,
        ps_ctxt->pu1_ctb_nbr_map,
        ps_ctxt->i4_nbr_map_strd,
        ps_ctxt->u2_ctb_num_in_row,
        ps_ctxt->u2_ctb_row_num,
        ps_frm_ctb_prms);
 
    /* IPE cu and mode decision */
    ihevce_bracketing_analysis(
        ps_ctxt,
        ps_curr_ctb_node,
        ps_curr_src,
        ps_ctb_out,
        //ps_row_cu,
        ps_ed_l1_ctb,
        ps_ed_l2_ctb,
        ps_ed_ctb_l1,
        ps_l0_ipe_out_ctb);
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_ipe_process_row \endif
*
* \brief
*    Row level IPE function
*
* \param[in] pv_ctxt : pointer to IPE module
* \param[in] ps_frm_ctb_prms : CTB characteristics parameters
* \param[in] ps_curr_src  : pointer to input yuv buffer (row buffer)
* \param[out] ps_ctb_out : pointer to CTB analyse output structure (row buffer)
* \param[out] ps_cu_out : pointer to CU analyse output structure (row buffer)
*\param[out]  pi4_num_ctbs_cur_row  : pointer to store the number of ctbs processed in current row
*\param[in]  pi4_num_ctbs_top_row  : pointer to check the number of ctbs processed in top row
*
* \return
*    None
*
* Note : Currently the frame level calculations done assumes that
*        framewidth of the input are excat multiple of ctbsize
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_ipe_process_row(
    ihevce_ipe_ctxt_t *ps_ctxt,
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
    iv_enc_yuv_buf_t *ps_curr_src,
    ipe_l0_ctb_analyse_for_me_t *ps_ipe_ctb_out_row,
    ctb_analyse_t *ps_ctb_out,
    //cu_analyse_t   *ps_row_cu,
    ihevce_ed_blk_t *ps_ed_l1_row,
    ihevce_ed_blk_t *ps_ed_l2_row,
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1_row,
    WORD32 blk_inc_ctb_l1,
    WORD32 blk_inc_ctb_l2)
{
    /* local variables */
    UWORD16 ctb_ctr;
    iv_enc_yuv_buf_t s_curr_src_bufs;
    ipe_l0_ctb_analyse_for_me_t *ps_l0_ipe_out_ctb;
    UWORD16 u2_pic_wdt;
    UWORD16 u2_pic_hgt;
    ihevce_ed_blk_t *ps_ed_l1_ctb;
    ihevce_ed_blk_t *ps_ed_l2_ctb;
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1;
 
    UWORD8 u1_ctb_size;
 
    u2_pic_wdt = ps_frm_ctb_prms->i4_cu_aligned_pic_wd;
    u2_pic_hgt = ps_frm_ctb_prms->i4_cu_aligned_pic_ht;
 
    u1_ctb_size = ps_ctxt->u1_ctb_size;
 
    /* ----------------------------------------------------- */
    /* store the stride and dimensions of source             */
    /* buffer pointers will be over written at every CTB row */
    /* ----------------------------------------------------- */
    memcpy(&s_curr_src_bufs, ps_curr_src, sizeof(iv_enc_yuv_buf_t));
    ps_l0_ipe_out_ctb = ps_ipe_ctb_out_row;
 
    /* --------- Loop over all the CTBs in a row --------------- */
    for(ctb_ctr = 0; ctb_ctr < ps_frm_ctb_prms->i4_num_ctbs_horz; ctb_ctr++)
    {
        //UWORD8            num_cus_in_ctb;
 
        UWORD8 *pu1_tmp;
 
        /* Create pointer to ctb node */
        ihevce_ipe_cu_tree_t *ps_ctb_node;
 
        WORD32 nbr_flags;
 
        WORD32 row;
        /* luma src */
        pu1_tmp = (UWORD8 *)ps_curr_src->pv_y_buf;
        pu1_tmp += (ctb_ctr * ps_frm_ctb_prms->i4_ctb_size);
 
        s_curr_src_bufs.pv_y_buf = pu1_tmp;
 
        /* Cb & CR pixel interleaved src */
        pu1_tmp = (UWORD8 *)ps_curr_src->pv_u_buf;
        pu1_tmp += (ctb_ctr * (ps_frm_ctb_prms->i4_ctb_size >> 1));
 
        s_curr_src_bufs.pv_u_buf = pu1_tmp;
 
        /* Store the number of current ctb within row in the context */
        ps_ctxt->u2_ctb_num_in_row = ctb_ctr;
 
        /* Initialize number of coding units in ctb to 0 */
        ps_ctb_out->u1_num_cus_in_ctb = 0;
        /* Initialize split flag to 0 - No partition  */
        ps_ctb_out->u4_cu_split_flags = 0;
        /* store the cu pointer for current ctb out */
        //ps_ctb_out->ps_coding_units_in_ctb = ps_row_cu;
 
        /* Initialize the CTB parameters at the root node level */
        ps_ctb_node = ps_ctxt->ps_ipe_cu_tree;
        ps_ctb_node->ps_parent = NULL;
        ps_ctb_node->u1_depth = 0;
        ps_ctb_node->u1_cu_size = u1_ctb_size;
        ps_ctb_node->u2_x0 = 0;
        ps_ctb_node->u2_y0 = 0;
 
        ps_ctb_node->u2_orig_x = ctb_ctr * ps_ctb_node->u1_cu_size;
        ps_ctb_node->u2_orig_y = ps_ctxt->u2_ctb_row_num * ps_ctb_node->u1_cu_size;
 
        ps_ctb_node->u1_width = u1_ctb_size;
        ps_ctb_node->u1_height = u1_ctb_size;
#if !(PIC_ALIGN_CTB_SIZE)
        if(ps_ctxt->u2_ctb_num_in_row == (ps_frm_ctb_prms->i4_num_ctbs_horz - 1))
        {
            ps_ctb_node->u1_width = u2_pic_wdt - (ps_ctxt->u2_ctb_num_in_row) * (u1_ctb_size);
        }
        if(ps_ctxt->u2_ctb_row_num == (ps_frm_ctb_prms->i4_num_ctbs_vert - 1))
        {
            ps_ctb_node->u1_height = u2_pic_hgt - (ps_ctxt->u2_ctb_row_num) * (u1_ctb_size);
        }
#endif
 
        switch(ps_ctb_node->u1_cu_size)
        {
        case 64:
            ps_ctb_node->u1_log2_nt = 6;
            ps_ctb_node->u1_part_flag_pos = 0;
            break;
        case 32:
            ps_ctb_node->u1_log2_nt = 5;
            ps_ctb_node->u1_part_flag_pos = 4;
            break;
        case 16:
            ps_ctb_node->u1_log2_nt = 4;
            ps_ctb_node->u1_part_flag_pos = 8;
            break;
        }
 
        /* Set neighbor flags for the CTB */
        nbr_flags = 0;
 
        if(ps_ctxt->u2_ctb_num_in_row != 0)
        {
            nbr_flags |= LEFT_FLAG; /* Set Left Flag if not in first column */
            ps_ctb_node->u1_num_left_avail = ((u2_pic_hgt - ps_ctb_node->u2_orig_y) >= u1_ctb_size)
                                                 ? u1_ctb_size
                                                 : u2_pic_hgt - ps_ctb_node->u2_orig_y;
        }
        else
        {
            ps_ctb_node->u1_num_left_avail = 0;
        }
 
        if((ps_ctxt->u2_ctb_num_in_row != 0) && (ps_ctxt->u2_ctb_row_num != 0))
            nbr_flags |= TOP_LEFT_FLAG; /* Set Top-Left Flag if not in first row or first column */
 
        if(ps_ctxt->u2_ctb_row_num != 0)
        {
            nbr_flags |= TOP_FLAG; /* Set Top Flag if not in first row */
            ps_ctb_node->u1_num_top_avail = ((u2_pic_wdt - ps_ctb_node->u2_orig_x) >= u1_ctb_size)
                                                ? u1_ctb_size
                                                : u2_pic_wdt - ps_ctb_node->u2_orig_x;
        }
        else
        {
            ps_ctb_node->u1_num_top_avail = 0;
        }
 
        if(ps_ctxt->u2_ctb_row_num != 0)
        {
            if(ps_ctxt->u2_ctb_num_in_row == (ps_frm_ctb_prms->i4_num_ctbs_horz - 1))
                ps_ctb_node->u1_num_top_right_avail = 0;
            else
            {
                ps_ctb_node->u1_num_top_right_avail =
                    ((u2_pic_wdt - ps_ctb_node->u2_orig_x - u1_ctb_size) >= u1_ctb_size)
                        ? u1_ctb_size
                        : u2_pic_wdt - ps_ctb_node->u2_orig_x - u1_ctb_size;
                nbr_flags |=
                    TOP_RIGHT_FLAG; /* Set Top-Right Flag if not in first row or last column*/
            }
        }
        else
        {
            ps_ctb_node->u1_num_top_right_avail = 0;
        }
 
        ps_ctb_node->u1_num_bottom_left_avail = 0;
 
        ps_ctb_node->i4_nbr_flag = nbr_flags;
 
        /**
        * Update CTB Mode Map
        * In case this is first CTB in a row, set left most column to INTRA_DC (NA)
        * else copy last column to first column
        **/
        if(ctb_ctr == 0)
        {
            for(row = 0; row < (MAX_TU_ROW_IN_CTB + 1); row++)
            {
                ps_ctxt->au1_ctb_mode_map[row][0] = INTRA_DC;
            }
        }
        else
        {
            for(row = 0; row < (MAX_TU_ROW_IN_CTB + 1); row++)
            {
                ps_ctxt->au1_ctb_mode_map[row][0] =
                    ps_ctxt->au1_ctb_mode_map[row][MAX_TU_COL_IN_CTB];
            }
        }
 
        /* --------- IPE call at CTB level ------------------ */
 
        /* IPE CTB function is expected to Decide on the CUs sizes  */
        /* and populate the best intra prediction modes and TX flags*/
        /* Interface of this CTb level function is kept open */
 
        ps_ed_l1_ctb = ps_ed_l1_row + ctb_ctr * blk_inc_ctb_l1;
        ps_ed_l2_ctb = ps_ed_l2_row + ctb_ctr * blk_inc_ctb_l2;
        ps_ed_ctb_l1 = ps_ed_ctb_l1_row + ctb_ctr;
 
        if(ps_ctxt->u1_use_lambda_derived_from_min_8x8_act_in_ctb)
        {
            /*HACK : MAMATHA, This function assumes that data is accumalated
            for all probable CU-TU combinations for INcomplete CTB, which is currently not the case,
            hence not recomputing lamda for the incomplete CTB */
            if((ps_ctb_node->u1_width == u1_ctb_size) && (ps_ctb_node->u1_height == u1_ctb_size))
            {
                ihevce_ipe_recompute_lambda_from_min_8x8_act_in_ctb(ps_ctxt, ps_ed_ctb_l1);
            }
        }
 
        ihevce_ipe_process_ctb(
            ps_ctxt,
            ps_frm_ctb_prms,
            &s_curr_src_bufs,
            ps_ctb_node,
            ps_l0_ipe_out_ctb,
            ps_ctb_out,
            //ps_row_cu,
            ps_ed_l1_ctb,
            ps_ed_l2_ctb,
            ps_ed_ctb_l1);
 
        /* -------------- ctb level updates ----------------- */
 
        ps_l0_ipe_out_ctb++;
        //num_cus_in_ctb = ps_ctb_out->u1_num_cus_in_ctb;
 
        //ps_row_cu += num_cus_in_ctb;
 
        ps_ctb_out++;
    }
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_ipe_process \endif
*
* \brief
*    Frame level IPE function
*
* \param[in] pv_ctxt : pointer to IPE module
* \param[in] ps_frm_ctb_prms : CTB characteristics parameters
* \param[in] ps_inp  : pointer to input yuv buffer (frame buffer)
* \param[out] ps_ctb_out : pointer to CTB analyse output structure (frame buffer)
* \param[out] ps_cu_out : pointer to CU analyse output structure (frame buffer)
*
* \return
*    None
*
* Note : Currently the frame level calculations done assumes that
*        framewidth of the input are excat multiple of ctbsize
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_ipe_process(
    void *pv_ctxt,
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
    frm_lambda_ctxt_t *ps_frm_lamda,
    ihevce_lap_enc_buf_t *ps_curr_inp,
    pre_enc_L0_ipe_encloop_ctxt_t *ps_L0_IPE_curr_out_pre_enc,
    ctb_analyse_t *ps_ctb_out,
    //cu_analyse_t               *ps_cu_out,
    ipe_l0_ctb_analyse_for_me_t *ps_ipe_ctb_out,
    void *pv_multi_thrd_ctxt,
    WORD32 slice_type,
    ihevce_ed_blk_t *ps_ed_pic_l1,
    ihevce_ed_blk_t *ps_ed_pic_l2,
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1_pic,
    WORD32 thrd_id,
    WORD32 i4_ping_pong)
{
    /* local variables */
    ihevce_ipe_master_ctxt_t *ps_master_ctxt;
    iv_enc_yuv_buf_t *ps_inp = &ps_curr_inp->s_lap_out.s_input_buf;
    ihevce_ipe_ctxt_t *ps_ctxt;
    iv_enc_yuv_buf_t s_curr_src_bufs;
    WORD32 end_of_frame;
 
    ihevce_ed_blk_t *ps_ed_l1_row;
    ihevce_ed_blk_t *ps_ed_l2_row;
    ihevce_ed_ctb_l1_t *ps_ed_ctb_l1_row;
    WORD32 blk_inc_ctb_l1 = 0;
    WORD32 blk_inc_ctb_l2 = 0;
 
    /* Layer 1 pre intra analysis related initilization.
    * Compute no of 8x8 blks in the ctb which which is
    * same as no of 4x4 blks in the ctb in layer 1 */
    blk_inc_ctb_l1 = ps_frm_ctb_prms->i4_ctb_size >> 3;
    blk_inc_ctb_l1 = blk_inc_ctb_l1 * blk_inc_ctb_l1;
 
    /* Layer 2 pre intra analysis related initilization.
    * Compute no of 16x16 blks in the ctb which which is
    * same as no of 8x8 blks in the ctb in layer 2 */
    blk_inc_ctb_l2 = ps_frm_ctb_prms->i4_ctb_size >> 4;
    blk_inc_ctb_l2 = blk_inc_ctb_l2 * blk_inc_ctb_l2;
 
    /* ----------------------------------------------------- */
    /* store the stride and dimensions of source             */
    /* buffer pointers will be over written at every CTB row */
    /* ----------------------------------------------------- */
    memcpy(&s_curr_src_bufs, ps_inp, sizeof(iv_enc_yuv_buf_t));
 
    ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)pv_ctxt;
    ps_ctxt = ps_master_ctxt->aps_ipe_thrd_ctxt[thrd_id];
    end_of_frame = 0;
 
    if(ISLICE == slice_type)
    {
        ps_ctxt->b_sad_type = IPE_SAD_TYPE;
        ps_ctxt->i4_ol_satd_lambda = ps_frm_lamda->i4_ol_satd_lambda_qf;
        ps_ctxt->i4_ol_sad_lambda = ps_frm_lamda->i4_ol_sad_lambda_qf;
    }
    else
    {
        ps_ctxt->b_sad_type = IPE_SAD_TYPE; /* SAD */
        ps_ctxt->i4_ol_satd_lambda = ps_frm_lamda->i4_ol_satd_lambda_qf;
        ps_ctxt->i4_ol_sad_lambda = ps_frm_lamda->i4_ol_sad_lambda_qf;
    }
 
    ihevce_populate_ipe_ol_cu_lambda_prms(
        (void *)ps_ctxt,
        ps_frm_lamda,
        slice_type,
        ps_curr_inp->s_lap_out.i4_temporal_lyr_id,
        IPE_LAMBDA_TYPE);
 
    /* register the slice type in the ctxt */
    ps_ctxt->i4_slice_type = slice_type;
 
    /** Frame-levelSATD cost accumalator init to 0 */
    ps_ctxt->i8_frame_acc_satd_cost = 0;
 
    /** Frame-levelSATD accumalator init to 0 */
    ps_ctxt->i8_frame_acc_satd = 0;
 
    /** Frame-level Activity factor accumalator init to 1 */
    ps_ctxt->i8_frame_acc_act_factor = 1;
 
    /** Frame-levelMode Bits cost accumalator init to 0 */
    ps_ctxt->i8_frame_acc_mode_bits_cost = 0;
 
    /** Frame -level SATD/qp acc init to 0*/
    ps_ctxt->i8_frame_acc_satd_by_modqp_q10 = 0;
 
    /* ------------ Loop over all the CTB rows --------------- */
    while(0 == end_of_frame)
    {
        UWORD8 *pu1_tmp;
        WORD32 vert_ctr;
        //cu_analyse_t *ps_row_cu;
        ctb_analyse_t *ps_ctb_out_row;
        job_queue_t *ps_job;
        ipe_l0_ctb_analyse_for_me_t *ps_ipe_ctb_out_row;
 
        /* Get the current row from the job queue */
        ps_job = (job_queue_t *)ihevce_pre_enc_grp_get_next_job(
            pv_multi_thrd_ctxt, IPE_JOB_LYR0, 1, i4_ping_pong);
 
        /* If all rows are done, set the end of process flag to 1, */
        /* and the current row to -1 */
        if(NULL == ps_job)
        {
            vert_ctr = -1;
            end_of_frame = 1;
        }
        else
        {
            ASSERT(IPE_JOB_LYR0 == ps_job->i4_pre_enc_task_type);
 
            /* Obtain the current row's details from the job */
            vert_ctr = ps_job->s_job_info.s_ipe_job_info.i4_ctb_row_no;
            //DBG_PRINTF("IPE PASS : Thread id %d, Vert Ctr %d\n",thrd_id,vert_ctr);
 
            /* Update the ipe context with current row number */
            ps_ctxt->u2_ctb_row_num = vert_ctr;
 
            /* derive the current ctb row pointers */
 
            /* luma src */
            pu1_tmp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_y_buf;
            pu1_tmp += (vert_ctr * ps_frm_ctb_prms->i4_ctb_size * ps_inp->i4_y_strd);
 
            s_curr_src_bufs.pv_y_buf = pu1_tmp;
 
            /* Cb & CR pixel interleaved src */
            pu1_tmp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_u_buf;
            pu1_tmp += (vert_ctr * (ps_frm_ctb_prms->i4_ctb_size >> 1) * ps_inp->i4_uv_strd);
 
            s_curr_src_bufs.pv_u_buf = pu1_tmp;
 
            /* row intra analyse cost buffer */
            ps_ipe_ctb_out_row = ps_ipe_ctb_out + vert_ctr * ps_frm_ctb_prms->i4_num_ctbs_horz;
 
            /* row ctb out structure */
            ps_ctb_out_row = ps_ctb_out + vert_ctr * ps_frm_ctb_prms->i4_num_ctbs_horz;
 
            /* call the row level processing function */
            ps_ed_l1_row =
                ps_ed_pic_l1 + ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l1 * vert_ctr;
            ps_ed_l2_row =
                ps_ed_pic_l2 + ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l2 * vert_ctr;
            ps_ed_ctb_l1_row = ps_ed_ctb_l1_pic + ps_frm_ctb_prms->i4_num_ctbs_horz * vert_ctr;
            ihevce_ipe_process_row(
                ps_ctxt,
                ps_frm_ctb_prms,
                &s_curr_src_bufs,
                ps_ipe_ctb_out_row,
                ps_ctb_out_row,
                //ps_row_cu,
                ps_ed_l1_row,
                ps_ed_l2_row,
                ps_ed_ctb_l1_row,
                blk_inc_ctb_l1,
                blk_inc_ctb_l2);
 
            memset(
                ps_ed_l1_row,
                0,
                ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l1 * sizeof(ihevce_ed_blk_t));
            memset(
                ps_ed_l2_row,
                0,
                ps_frm_ctb_prms->i4_num_ctbs_horz * blk_inc_ctb_l2 * sizeof(ihevce_ed_blk_t));
 
            /* set the output dependency */
            ihevce_pre_enc_grp_job_set_out_dep(pv_multi_thrd_ctxt, ps_job, i4_ping_pong);
        }
    }
 
    /* EIID: Print stat regarding how many 16x16 blocks are skipped in the frame, valid for single thread only */
    //DBG_PRINTF("num_16x16_analyze_skipped: %d\n",ps_ctxt->u4_num_16x16_skips_at_L0_IPE);
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_get_frame_lambda_prms \endif
*
* \brief
*    Function whihc calculates the Lambda params for current picture
*
* \param[in] ps_enc_ctxt : encoder ctxt pointer
* \param[in] ps_cur_pic_ctxt : current pic ctxt
* \param[in] i4_cur_frame_qp : current pic QP
* \param[in] first_field : is first field flag
* \param[in] i4_temporal_lyr_id : Current picture layer id
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_get_ipe_ol_cu_lambda_prms(void *pv_ctxt, WORD32 i4_cur_cu_qp)
{
    ihevce_ipe_ctxt_t *ps_ctxt = (ihevce_ipe_ctxt_t *)pv_ctxt;
    //WORD32 chroma_qp = gau1_ihevc_chroma_qp_scale[i4_cur_cu_qp];
 
    /* Store the params for IPE pass */
    ps_ctxt->i4_ol_satd_lambda = ps_ctxt->i4_ol_satd_lambda_qf_array[i4_cur_cu_qp];
    ps_ctxt->i4_ol_sad_lambda = ps_ctxt->i4_ol_sad_lambda_qf_array[i4_cur_cu_qp];
}
 
/*!
******************************************************************************
* \if Function name : ihevce_get_frame_lambda_prms \endif
*
* \brief
*    Function whihc calculates the Lambda params for current picture
*
* \param[in] ps_enc_ctxt : encoder ctxt pointer
* \param[in] ps_cur_pic_ctxt : current pic ctxt
* \param[in] i4_cur_frame_qp : current pic QP
* \param[in] first_field : is first field flag
* \param[in] i4_temporal_lyr_id : Current picture layer id
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_populate_ipe_ol_cu_lambda_prms(
    void *pv_ctxt,
    frm_lambda_ctxt_t *ps_frm_lamda,
    WORD32 i4_slice_type,
    WORD32 i4_temporal_lyr_id,
    WORD32 i4_lambda_type)
{
    WORD32 i4_curr_cu_qp;
    double lambda_modifier;
    double lambda_uv_modifier;
    double lambda;
    double lambda_uv;
 
    ihevce_ipe_ctxt_t *ps_ctxt = (ihevce_ipe_ctxt_t *)pv_ctxt;
 
    WORD32 i4_qp_bd_offset = 6 * (ps_ctxt->u1_bit_depth - 8);
 
    for(i4_curr_cu_qp =
            ps_ctxt->ps_rc_quant_ctxt->i2_min_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset;
        i4_curr_cu_qp <= ps_ctxt->ps_rc_quant_ctxt->i2_max_qp;
        i4_curr_cu_qp++)
    {
        WORD32 chroma_qp = i4_curr_cu_qp;
 
        if((BSLICE == i4_slice_type) && (i4_temporal_lyr_id))
        {
            lambda_modifier = ps_frm_lamda->lambda_modifier *
                              CLIP3((((double)(i4_curr_cu_qp - 12)) / 6.0), 2.00, 4.00);
            lambda_uv_modifier = ps_frm_lamda->lambda_uv_modifier *
                                 CLIP3((((double)(chroma_qp - 12)) / 6.0), 2.00, 4.00);
        }
        else
        {
            lambda_modifier = ps_frm_lamda->lambda_modifier;
            lambda_uv_modifier = ps_frm_lamda->lambda_uv_modifier;
        }
        if(ps_ctxt->i4_use_const_lamda_modifier)
        {
            if(ISLICE == i4_slice_type)
            {
                lambda_modifier = ps_ctxt->f_i_pic_lamda_modifier;
                lambda_uv_modifier = ps_ctxt->f_i_pic_lamda_modifier;
            }
            else
            {
                lambda_modifier = CONST_LAMDA_MOD_VAL;
                lambda_uv_modifier = CONST_LAMDA_MOD_VAL;
            }
        }
 
        switch(i4_lambda_type)
        {
        case 0:
        {
            i4_qp_bd_offset = 0;
 
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda *= lambda_modifier;
            lambda_uv *= lambda_uv_modifier;
            if(ps_ctxt->i4_use_const_lamda_modifier)
            {
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
            }
            else
            {
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
            }
 
            ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp];
 
            ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp];
 
            break;
        }
        case 1:
        {
            ASSERT(0); /* should not enter the path for IPE*/
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda *= lambda_modifier;
            lambda_uv *= lambda_uv_modifier;
            if(ps_ctxt->i4_use_const_lamda_modifier)
            {
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
            }
            else
            {
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
            }
 
            ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp];
 
            ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp];
 
            break;
        }
        case 2:
        {
            ASSERT(0); /* should not enter the path for IPE*/
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda *= lambda_modifier;
            lambda_uv *= lambda_uv_modifier;
            if(ps_ctxt->i4_use_const_lamda_modifier)
            {
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
            }
            else
            {
                ps_ctxt->i4_ol_sad_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
            }
            i4_qp_bd_offset = 0;
 
            lambda = pow(2.0, (((double)(i4_curr_cu_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda_uv = pow(2.0, (((double)(chroma_qp + i4_qp_bd_offset - 12)) / 3.0));
 
            lambda *= lambda_modifier;
            lambda_uv *= lambda_uv_modifier;
            if(ps_ctxt->i4_use_const_lamda_modifier)
            {
                ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda)) * (1 << LAMBDA_Q_SHIFT));
            }
            else
            {
                ps_ctxt->i4_ol_sad_type2_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda) / 2) * (1 << LAMBDA_Q_SHIFT));
 
                ps_ctxt->i4_ol_satd_type2_lambda_qf_array[i4_curr_cu_qp] =
                    (WORD32)((sqrt(lambda * 1.9) / 2) * (1 << LAMBDA_Q_SHIFT));
            }
            break;
        }
        default:
        {
            /* Intended to be a barren wasteland! */
            ASSERT(0);
        }
        }
    }
}
 
#define ME_COST_THRSHOLD 7
/*!
******************************************************************************
* \if Function name : ihevce_get_frame_lambda_prms \endif
*
* \brief
*    Function whihc calculates the Lambda params for current picture
*
* \param[in] ps_enc_ctxt : encoder ctxt pointer
* \param[in] ps_cur_pic_ctxt : current pic ctxt
* \param[in] i4_cur_frame_qp : current pic QP
* \param[in] first_field : is first field flag
* \param[in] i4_temporal_lyr_id : Current picture layer id
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
#define MAX_64BIT_VAL 0x7fffffffffffffff
void ihevce_populate_ipe_frame_init(
    void *pv_ctxt,
    ihevce_static_cfg_params_t *ps_stat_prms,
    WORD32 i4_curr_frm_qp,
    WORD32 i4_slice_type,
    WORD32 i4_thrd_id,
    pre_enc_me_ctxt_t *ps_curr_out,
    WORD8 i1_cu_qp_delta_enabled_flag,
    rc_quant_t *ps_rc_quant_ctxt,
    WORD32 i4_quality_preset,
    WORD32 i4_temporal_lyr_id,
    ihevce_lap_output_params_t *ps_lap_out)
{
    ihevce_ipe_master_ctxt_t *ps_master_ctxt = (ihevce_ipe_master_ctxt_t *)pv_ctxt;
    WORD32 i4_i;
    WORD32 ai4_mod_factor_num[2];
 
    ihevce_ipe_ctxt_t *ps_ctxt = ps_master_ctxt->aps_ipe_thrd_ctxt[i4_thrd_id];
    ps_ctxt->i4_hevc_qp = i4_curr_frm_qp;
    ps_ctxt->i4_quality_preset = i4_quality_preset;
    ps_ctxt->i4_temporal_lyr_id = i4_temporal_lyr_id;
    ps_ctxt->ps_rc_quant_ctxt = ps_rc_quant_ctxt;
    ps_ctxt->i4_qscale =
        ps_ctxt->ps_rc_quant_ctxt
            ->pi4_qp_to_qscale[i4_curr_frm_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset];
 
    ps_ctxt->i4_frm_qp = i4_curr_frm_qp + ps_ctxt->ps_rc_quant_ctxt->i1_qp_offset;
    ps_ctxt->i4_slice_type = i4_slice_type;  //EIID
    ps_ctxt->i4_temporal_layer = ps_lap_out->i4_temporal_lyr_id;
    ps_ctxt->i4_is_ref_pic = ps_lap_out->i4_is_ref_pic;
    ps_ctxt->u4_num_16x16_skips_at_L0_IPE = 0;
    ps_ctxt->i4_use_const_lamda_modifier = USE_CONSTANT_LAMBDA_MODIFIER;
    ps_ctxt->i4_use_const_lamda_modifier =
        ps_ctxt->i4_use_const_lamda_modifier ||
        ((ps_stat_prms->s_coding_tools_prms.i4_vqet &
          (1 << BITPOS_IN_VQ_TOGGLE_FOR_CONTROL_TOGGLER)) &&
         ((ps_stat_prms->s_coding_tools_prms.i4_vqet &
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_NOISE_PRESERVATION)) ||
          (ps_stat_prms->s_coding_tools_prms.i4_vqet &
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_1)) ||
          (ps_stat_prms->s_coding_tools_prms.i4_vqet &
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_2)) ||
          (ps_stat_prms->s_coding_tools_prms.i4_vqet &
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_3))));
    {
        ps_ctxt->f_i_pic_lamda_modifier = ps_lap_out->f_i_pic_lamda_modifier;
    }
#if POW_OPT
    for(i4_i = 0; i4_i < 2; i4_i++)
    {
        ps_ctxt->ld_curr_frame_8x8_log_avg[i4_i] = ps_curr_out->ld_curr_frame_8x8_log_avg[i4_i];
        ps_ctxt->ld_curr_frame_16x16_log_avg[i4_i] = ps_curr_out->ld_curr_frame_16x16_log_avg[i4_i];
        ps_ctxt->ld_curr_frame_32x32_log_avg[i4_i] = ps_curr_out->ld_curr_frame_32x32_log_avg[i4_i];
    }
 
    ps_ctxt->ld_curr_frame_16x16_log_avg[2] = ps_curr_out->ld_curr_frame_16x16_log_avg[2];
    ps_ctxt->ld_curr_frame_32x32_log_avg[2] = ps_curr_out->ld_curr_frame_32x32_log_avg[2];
    ps_ctxt->i8_curr_frame_avg_mean_act = ps_curr_out->i8_curr_frame_avg_mean_act;
#else
    for(i4_i = 0; i4_i < 2; i4_i++)
    {
        ps_ctxt->i8_curr_frame_8x8_avg_act[i4_i] = ps_curr_out->i8_curr_frame_8x8_avg_act[i4_i];
        ps_ctxt->i8_curr_frame_16x16_avg_act[i4_i] = ps_curr_out->i8_curr_frame_16x16_avg_act[i4_i];
        ps_ctxt->i8_curr_frame_32x32_avg_act[i4_i] = ps_curr_out->i8_curr_frame_32x32_avg_act[i4_i];
    }
 
    ps_ctxt->i8_curr_frame_16x16_avg_act[2] = ps_curr_out->i8_curr_frame_16x16_avg_act[2];
    ps_ctxt->i8_curr_frame_32x32_avg_act[2] = ps_curr_out->i8_curr_frame_32x32_avg_act[2];
#endif
 
    ps_ctxt->pi2_trans_out =
        (WORD16 *)&ps_ctxt->au1_pred_samples[0];  //overlaying trans coeff memory with pred_samples
    ps_ctxt->pi2_trans_tmp = (WORD16 *)&ps_ctxt->au1_pred_samples[2048];
 
    /*Mod factor NUM */
    ps_ctxt->ai4_mod_factor_derived_by_variance[0] =
        ps_curr_out->ai4_mod_factor_derived_by_variance[0];
    ps_ctxt->ai4_mod_factor_derived_by_variance[1] =
        ps_curr_out->ai4_mod_factor_derived_by_variance[1];
 
    ps_ctxt->f_strength = ps_curr_out->f_strength;
 
    if(ps_stat_prms->s_coding_tools_prms.i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_CONTROL_TOGGLER))
    {
        if(ps_stat_prms->s_coding_tools_prms.i4_vqet &
           (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_NOISE_PRESERVATION))
        {
            ps_ctxt->i4_enable_noise_detection = 1;
        }
        else
        {
            ps_ctxt->i4_enable_noise_detection = 0;
        }
    }
    else
    {
        ps_ctxt->i4_enable_noise_detection = 0;
    }
 
    {
        if(ISLICE == ps_ctxt->i4_slice_type)
        {
            ai4_mod_factor_num[0] = INTRA_QP_MOD_FACTOR_NUM;  //16;
            ai4_mod_factor_num[1] = INTRA_QP_MOD_FACTOR_NUM;  //16;
        }
        else
        {
            ai4_mod_factor_num[0] = INTER_QP_MOD_FACTOR_NUM;  //4;
            ai4_mod_factor_num[1] = INTER_QP_MOD_FACTOR_NUM;  //4;
        }
 
#if ENABLE_QP_MOD_BASED_ON_SPATIAL_VARIANCE
        for(i4_i = 0; i4_i < 2; i4_i++)
        {
            WORD32 mod_factor_num_val =
                ps_ctxt->ai4_mod_factor_derived_by_variance[i4_i] * QP_MOD_FACTOR_DEN;
 
            ai4_mod_factor_num[i4_i] = CLIP3(mod_factor_num_val, 1, ai4_mod_factor_num[i4_i]);
            ps_ctxt->ai4_mod_factor_derived_by_variance[i4_i] = ai4_mod_factor_num[i4_i];
        }
#else
        for(i4_i = 0; i4_i < 2; i4_i++)
        {
            ps_ctxt->ai4_mod_factor_derived_by_variance[i4_i] = ai4_mod_factor_num[i4_i];
        }
#endif
    }
 
    ps_ctxt->u1_use_lambda_derived_from_min_8x8_act_in_ctb = MODULATE_LAMDA_WHEN_SPATIAL_MOD_ON &&
                                                             i1_cu_qp_delta_enabled_flag;
 
    ps_ctxt->u1_use_satd = 1;
    ps_ctxt->u1_level_1_refine_on = 1;
    ps_ctxt->u1_disable_child_cu_decide = 0;
 
#if !OLD_XTREME_SPEED
    if(((ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P5) ||
        (ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P6)) &&
       (ps_ctxt->i4_slice_type != ISLICE))
    {
        ps_ctxt->u1_use_satd = 0;
        ps_ctxt->u1_level_1_refine_on = 1;
        ps_ctxt->u1_disable_child_cu_decide = 0;
    }
 
#endif
 
    if((ps_ctxt->i4_quality_preset == IHEVCE_QUALITY_P4) && (ps_ctxt->i4_slice_type != ISLICE))
        ps_ctxt->u1_use_satd = 0;
    if(ps_ctxt->i4_quality_preset > IHEVCE_QUALITY_P3)
        ps_ctxt->u1_use_satd = 0;
}