ronnie
2022-10-23 68f5ca84d926736535296469a2d3fcbea06ca8a2
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
/******************************************************************************
 *
 * 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_dep_mngr.c
*
* \brief
*    This file contains all the functions related to Sync manager
*
* \date
*    12/12/2013
*
* \author
*    Ittiam
*
* List of Functions
*   <TODO: TO BE ADDED>
*
******************************************************************************
*/
 
/*****************************************************************************/
/* 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 "ihevc_debug.h"
#include "ihevc_macros.h"
#include "ihevc_platform_macros.h"
 
#include "ihevce_api.h"
#include "ihevce_dep_mngr_interface.h"
#include "ihevce_dep_mngr_private.h"
 
#include "cast_types.h"
#include "osal.h"
#include "osal_defaults.h"
 
/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_get_num_mem_recs \endif
*
* \brief
*    Number of memory records are returned for Dependency manager.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
WORD32 ihevce_dmgr_get_num_mem_recs()
{
    return (NUM_DEP_MNGR_MEM_RECS);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_get_mem_recs \endif
*
* \brief
*    Memory requirements are returned for Dependency manager.
*
* \param[in,out]  ps_mem_tab : pointer to memory descriptors table
* \param[in] dep_mngr_mode : Mode of operation of dependency manager
* \param[in] max_num_vert_units : Maximum nunber of units to be processed
* \param[in] num_tile_cols : Number of column tiles for which encoder is working
* \param[in] num_threads : Number of threads among which sync will be established
* \param[in] i4_mem_space : memspace in which memory request should be done
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
WORD32 ihevce_dmgr_get_mem_recs(
    iv_mem_rec_t *ps_mem_tab,
    WORD32 dep_mngr_mode,
    WORD32 max_num_vert_units,
    WORD32 num_tile_cols,
    WORD32 num_threads,
    WORD32 i4_mem_space)
{
    WORD32 num_vert_units;
    WORD32 num_wait_thrd_ids;
 
    /* Dependency manager state structure */
    ps_mem_tab[DEP_MNGR_CTXT].i4_mem_size = sizeof(dep_mngr_state_t);
    ps_mem_tab[DEP_MNGR_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_CTXT].i4_mem_alignment = 8;
 
    /* SANITY CHECK */
    ASSERT(
        (DEP_MNGR_FRM_FRM_SYNC == dep_mngr_mode) || (DEP_MNGR_ROW_FRM_SYNC == dep_mngr_mode) ||
        (DEP_MNGR_ROW_ROW_SYNC == dep_mngr_mode));
 
    /* Default value */
    if(num_tile_cols < 1)
    {
        num_tile_cols = 1;
    }
 
    /**************** Get Processed status Memory Requirements *********************/
    if(DEP_MNGR_FRM_FRM_SYNC == dep_mngr_mode)
    {
        /* for frame to frame sync
           2 words are used for holding num units processed prev
           2 words are used for holding num units processed curr
           */
        num_vert_units = (2 + 2) * num_threads;
    }
    else
    {
        /* for both frm-row and row-row num vertical units in frame is allocated */
        /* (* num_tile_cols) as each column tile can separately update and check */
        num_vert_units = max_num_vert_units * num_tile_cols;
    }
 
    ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].i4_mem_size = (sizeof(WORD32) * num_vert_units);
    ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].i4_mem_alignment = 8;
 
    /**************** Get Wait thread ids Memory Requirements *********************/
    if(DEP_MNGR_FRM_FRM_SYNC == dep_mngr_mode)
    {
        /* for frame to frame sync number of threads worth memory is allocated */
        num_wait_thrd_ids = num_threads;
    }
    else if(DEP_MNGR_ROW_ROW_SYNC == dep_mngr_mode)
    {
        /* for row to row sync number of vertical rows worth memory is allocated */
        num_wait_thrd_ids = max_num_vert_units;
    }
    else
    {
        /* for row to frame sync number of threads * number of vertical rows worth memory is allocated */
        num_wait_thrd_ids = max_num_vert_units * num_threads;
    }
 
    ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].i4_mem_size = (sizeof(WORD32) * num_wait_thrd_ids);
    ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].i4_mem_alignment = 8;
 
    /**************** Get Semaphore Requirements *********************/
    ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].i4_mem_size = (sizeof(void *) * num_threads);
    ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].i4_mem_alignment = 8;
 
    return (NUM_DEP_MNGR_MEM_RECS);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_map_get_mem_recs \endif
*
* \brief
*    Memory requirements are returned for Dependency manager.
*
* \param[in,out]  ps_mem_tab : pointer to memory descriptors table
* \param[in] num_units : Number of units in the map
* \param[in] num_threads : Number of threads among which sync will be established
* \param[in] i4_mem_space : memspace in which memory request should be done
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
WORD32 ihevce_dmgr_map_get_mem_recs(
    iv_mem_rec_t *ps_mem_tab, WORD32 num_units, WORD32 num_threads, WORD32 i4_mem_space)
{
    /* Dependency manager state structure */
    ps_mem_tab[DEP_MNGR_CTXT].i4_mem_size = sizeof(dep_mngr_state_t);
    ps_mem_tab[DEP_MNGR_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_CTXT].i4_mem_alignment = 8;
 
    /**************** Get Processed status Memory Requirements *********************/
    ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].i4_mem_size = (sizeof(WORD8) * num_units);
    ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].i4_mem_alignment = 8;
 
    /**************** Get Wait thread ids Memory Requirements *********************/
    /* Map-mode: semaphore post is unconditionally done on all threads */
    ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].i4_mem_size = (sizeof(WORD32) * num_threads);
    ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].i4_mem_alignment = 8;
 
    /**************** Get Semaphore Requirements *********************/
    ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].i4_mem_size = (sizeof(void *) * num_threads);
    ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].i4_mem_alignment = 8;
 
    return (NUM_DEP_MNGR_MEM_RECS);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_rst_frm_frm_sync \endif
*
* \brief
*    Resets the values stored to init value
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_rst_frm_frm_sync(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD32 thrds;
    ULWORD64 *pu8_num_units_proc_prev;
    ULWORD64 *pu8_num_units_proc_curr;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Reset the num units processed by each thread */
    pu8_num_units_proc_curr = (ULWORD64 *)ps_dep_mngr_state->pv_units_prcsd_in_row;
    pu8_num_units_proc_prev = pu8_num_units_proc_curr + ps_dep_mngr_state->i4_num_thrds;
 
    /* Reset the values thread ids waiting */
    for(thrds = 0; thrds < ps_dep_mngr_state->i4_num_thrds; thrds++)
    {
        pu8_num_units_proc_prev[thrds] = 0;
        pu8_num_units_proc_curr[thrds] = 0;
        ps_dep_mngr_state->pi4_wait_thrd_id[thrds] = -1;
    }
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_rst_row_frm_sync \endif
*
* \brief
*    Resets the values stored to init value
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_rst_row_frm_sync(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD32 ctr, thrds;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Reset the values of number of units processed in a row */
    for(ctr = 0; ctr < ps_dep_mngr_state->i4_num_vert_units; ctr++)
    {
        ((WORD32 *)ps_dep_mngr_state->pv_units_prcsd_in_row)[ctr] = 0;
    }
 
    /* Reset the values thread ids waiting on each row  */
    for(ctr = 0; ctr < ps_dep_mngr_state->i4_num_vert_units; ctr++)
    {
        for(thrds = 0; thrds < ps_dep_mngr_state->i4_num_thrds; thrds++)
        {
            ps_dep_mngr_state->pi4_wait_thrd_id[thrds + (ps_dep_mngr_state->i4_num_thrds * ctr)] =
                -1;
        }
    }
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_map_rst_sync \endif
*
* \brief
*    Resets the values stored to init value
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_map_rst_sync(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD8 *pi1_ptr;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    pi1_ptr = (WORD8 *)ps_dep_mngr_state->pv_units_prcsd_in_row -
              ps_dep_mngr_state->ai4_tile_xtra_ctb[0] * ps_dep_mngr_state->i4_num_horz_units -
              ps_dep_mngr_state->ai4_tile_xtra_ctb[1];
 
    memset(
        pi1_ptr,
        MAP_CTB_INIT,
        ps_dep_mngr_state->i4_num_vert_units * ps_dep_mngr_state->i4_num_horz_units *
            sizeof(WORD8));
 
    //ps_dep_mngr_state->i4_frame_map_complete = 0;
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_rst_row_row_sync \endif
*
* \brief
*    Resets the values stored to init value
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_rst_row_row_sync(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD32 ctr;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Reset the values of number of units processed in a row */
    for(ctr = 0; ctr < (ps_dep_mngr_state->i4_num_vert_units * ps_dep_mngr_state->i4_num_tile_cols);
        ctr++)
    {
        ((WORD32 *)ps_dep_mngr_state->pv_units_prcsd_in_row)[ctr] = 0;
    }
 
    /* Reset the values thread ids waiting on each row  */
    for(ctr = 0; ctr < ps_dep_mngr_state->i4_num_vert_units; ctr++)
    {
        ps_dep_mngr_state->pi4_wait_thrd_id[ctr] = -1;
    }
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_init \endif
*
* \brief
*    Intialization for Dependency manager state structure .
*
* \param[in] ps_mem_tab : pointer to memory descriptors table
* \param[in] pv_osal_handle : osal handle
* \param[in] dep_mngr_mode : Mode of operation of dependency manager
* \param[in] max_num_vert_units : Maximum nunber of units to be processed (Frame Data)
* \param[in] max_num_horz_units : Maximun Number of Horizontal units to be processed (Frame Data)
* \param[in] num_tile_cols : Number of column tiles for which encoder is working
* \param[in] sem_enable : Whether you want to enable semaphore or not
             1 : Sem. Enabled, 0 : Spin lock enabled (do-while)
* \param[in] num_threads : Number of threads among which sync will be established
* \param[in] i4_mem_space : memspace in which memory request should be do
*
* \return
*    Handle to context
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void *ihevce_dmgr_init(
    iv_mem_rec_t *ps_mem_tab,
    void *pv_osal_handle,
    WORD32 dep_mngr_mode,
    WORD32 max_num_vert_units,
    WORD32 max_num_horz_units,
    WORD32 num_tile_cols,
    WORD32 num_threads,
    WORD32 sem_enable)
{
    dep_mngr_state_t *ps_dep_mngr_state;
 
    (void)pv_osal_handle;
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)ps_mem_tab[DEP_MNGR_CTXT].pv_base;
 
    /* dep manager memory init */
    ps_dep_mngr_state->ppv_thrd_sem_handles = (void **)ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].pv_base;
    ps_dep_mngr_state->pi4_wait_thrd_id = (WORD32 *)ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].pv_base;
    ps_dep_mngr_state->pv_units_prcsd_in_row = ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].pv_base;
 
    /* SANITY CHECK */
    ASSERT(NULL != pv_osal_handle);
    ASSERT(
        (DEP_MNGR_FRM_FRM_SYNC == dep_mngr_mode) || (DEP_MNGR_ROW_FRM_SYNC == dep_mngr_mode) ||
        (DEP_MNGR_ROW_ROW_SYNC == dep_mngr_mode));
 
    /* Default value */
    if(num_tile_cols < 1)
    {
        num_tile_cols = 1;
    }
 
    /* reset the state structure variables */
    ps_dep_mngr_state->i4_num_horz_units = max_num_horz_units;
    ps_dep_mngr_state->i4_num_vert_units = max_num_vert_units;
    ps_dep_mngr_state->i1_sem_enable = sem_enable;
    ps_dep_mngr_state->i4_dep_mngr_mode = dep_mngr_mode;
    ps_dep_mngr_state->i4_num_thrds = num_threads;
    ps_dep_mngr_state->i4_num_tile_cols = num_tile_cols;
 
    /* call the reset function baed on mode */
    if(DEP_MNGR_FRM_FRM_SYNC == dep_mngr_mode)
    {
        ihevce_dmgr_rst_frm_frm_sync((void *)ps_dep_mngr_state);
    }
    else if(DEP_MNGR_ROW_ROW_SYNC == dep_mngr_mode)
    {
        ihevce_dmgr_rst_row_row_sync((void *)ps_dep_mngr_state);
    }
    else
    {
        ihevce_dmgr_rst_row_frm_sync((void *)ps_dep_mngr_state);
    }
 
    return ((void *)ps_dep_mngr_state);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_map_init \endif
*
* \brief
*    Intialization for Dependency manager state structure .
*
* \param[in] ps_mem_tab : pointer to memory descriptors table
* \param[in] max_num_vert_units : Maximum nunber of units to be processed
* \param[in] max_num_horz_units : Maximun Number of Horizontal units to be processed
* \param[in] sem_enable : Whether you want to enable semaphore or not
             1 : Sem. Enabled, 0 : Spin lock enabled (do-while)
* \param[in] num_threads : Number of threads among which sync will be established
* \param[in] ai4_tile_xtra_ctb : Array containing the number of CTBs which are
*            are present in the Search Range outside the tile in dist-client mode.
*            In standalone mode this array should be zero.
*
* \return
*    Handle to context
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void *ihevce_dmgr_map_init(
    iv_mem_rec_t *ps_mem_tab,
    WORD32 max_num_vert_units,
    WORD32 max_num_horz_units,
    WORD32 sem_enable,
    WORD32 num_threads,
    WORD32 ai4_tile_xtra_ctb[4])
{
    WORD32 ctr;
    dep_mngr_state_t *ps_dep_mngr_state;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)ps_mem_tab[DEP_MNGR_CTXT].pv_base;
 
    ps_dep_mngr_state->ai4_tile_xtra_ctb[0] = ai4_tile_xtra_ctb[0];
    ps_dep_mngr_state->ai4_tile_xtra_ctb[1] = ai4_tile_xtra_ctb[1];
    ps_dep_mngr_state->ai4_tile_xtra_ctb[2] = ai4_tile_xtra_ctb[2];
    ps_dep_mngr_state->ai4_tile_xtra_ctb[3] = ai4_tile_xtra_ctb[3];
 
    /* dep manager memory init */
    ps_dep_mngr_state->pv_units_prcsd_in_row = ps_mem_tab[DEP_MNGR_UNITS_PRCSD_MEM].pv_base;
    ps_dep_mngr_state->pi4_wait_thrd_id = (WORD32 *)ps_mem_tab[DEP_MNGR_WAIT_THRD_ID_MEM].pv_base;
    ps_dep_mngr_state->ppv_thrd_sem_handles = (void **)ps_mem_tab[DEP_MNGR_SEM_HANDLE_MEM].pv_base;
 
    /* Pointing to first CTB of tile */
    ps_dep_mngr_state->pv_units_prcsd_in_row =
        (void*)((WORD8*)ps_dep_mngr_state->pv_units_prcsd_in_row +
        ps_dep_mngr_state->ai4_tile_xtra_ctb[1] +
        max_num_horz_units * ps_dep_mngr_state->ai4_tile_xtra_ctb[0]);
 
    /* Map-mode: semaphore post is unconditionally done on all threads. Hence
    store these one time IDs. The use of pi4_wait_thrd_id itself can be removed
    altogether for map-mode, but keeping it for the sake of laziness  */
    for(ctr = 0; ctr < num_threads; ctr++)
    {
        ps_dep_mngr_state->pi4_wait_thrd_id[ctr] = ctr;
    }
 
    /* reset the state structure variables */
    ps_dep_mngr_state->i4_num_horz_units = max_num_horz_units;
    ps_dep_mngr_state->i4_num_vert_units = max_num_vert_units;
    ps_dep_mngr_state->i1_sem_enable = sem_enable;
    ps_dep_mngr_state->i4_dep_mngr_mode = DEP_MNGR_MAP_SYNC;
    ps_dep_mngr_state->i4_num_thrds = num_threads;
 
    /* call the reset function baed on mode */
    ihevce_dmgr_map_rst_sync((void *)ps_dep_mngr_state);
 
    return ((void *)ps_dep_mngr_state);
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_del \endif
*
* \brief
*    Delete the Dependency manager state structure.
* Note : Destroys the mutex only. System has to free the allocated memory
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_del(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
 
    /* dep manager state structure */
    (void)ps_dep_mngr_state;
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_register_sem_hdls \endif
*
* \brief
*    Register sem handles of threads wihci are part of dependency group
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
* \param[in] ppv_thread_sem_hdl : arry of pointer to all the sem handles
* \param[in] num_threads : Number of threads part of this dependency group
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_reg_sem_hdls(void *pv_dep_mngr_state, void **ppv_thread_sem_hdl, WORD32 num_threads)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD32 ctr;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    ASSERT(num_threads <= ps_dep_mngr_state->i4_num_thrds);
 
    for(ctr = 0; ctr < num_threads; ctr++)
    {
        ps_dep_mngr_state->ppv_thrd_sem_handles[ctr] = ppv_thread_sem_hdl[ctr];
    }
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_set_prev_done_frm_frm_sync \endif
*
* \brief
*    Set the values to dependency not resolved state
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_set_prev_done_frm_frm_sync(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD32 thrds;
    ULWORD64 *pu8_num_units_proc_curr;
    ULWORD64 *pu8_num_units_proc_prev;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Reset the values num threads entering processing state  */
    pu8_num_units_proc_curr = (ULWORD64 *)ps_dep_mngr_state->pv_units_prcsd_in_row;
    pu8_num_units_proc_prev =
        (ULWORD64 *)(pu8_num_units_proc_curr + ps_dep_mngr_state->i4_num_thrds);
 
    /* Reset the values thread ids waiting */
    for(thrds = 0; thrds < ps_dep_mngr_state->i4_num_thrds; thrds++)
    {
        pu8_num_units_proc_prev[thrds] = 1;
        ps_dep_mngr_state->pi4_wait_thrd_id[thrds] = -1;
    }
 
    return;
}
 
/*!
******************************************************************************
* \if Function name : ihevce_dmgr_set_done_frm_frm_sync \endif
*
* \brief
*    Set the values to dependency met state
*
* \param[in,out]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    None
*
* \author
*  Ittiam
*
*****************************************************************************
*/
void ihevce_dmgr_set_done_frm_frm_sync(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD32 thrds;
    ULWORD64 *pu8_num_units_proc_curr;
 
    /* dep manager state structure */
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Reset the values num threads entering processing state  */
    pu8_num_units_proc_curr = (ULWORD64 *)ps_dep_mngr_state->pv_units_prcsd_in_row;
 
    /* Reset the values thread ids waiting */
    for(thrds = 0; thrds < ps_dep_mngr_state->i4_num_thrds; thrds++)
    {
        pu8_num_units_proc_curr[thrds] = 1;
        ps_dep_mngr_state->pi4_wait_thrd_id[thrds] = -1;
    }
 
    return;
}
 
/*!
**************************************************************************
* \if Function name : ihevce_dmgr_chk_row_row_sync \endif
*
* \brief
*    This function checks whether the dependency is met to proceed with
*    processing. If condition is not met, it should go to a sem_wait state,
*    else start processing.
*
* \param[in]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
* \param[in]  cur_offset    : Current offset of the dep. variable
* \param[in]  dep_offset    : Offset from the current value to meet the dep.
* \param[in]  dep_row       : The position of the Ref.
* \param[in]  cur_tile_col  : The current column tile number (not tile id)
*   Assuming the dependency is within the tile only (Acroos tiles won't work now)
* \param[in]  thrd_id       : Thread id of the current thread checking for dependency
*
* \return
*    0 on Success and -1 on error
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 ihevce_dmgr_chk_row_row_sync(
    void *pv_dep_mngr_state,
    WORD32 cur_offset,
    WORD32 dep_offset,
    WORD32 dep_row,
    WORD32 cur_tile_col,
    WORD32 thrd_id)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    volatile WORD32 *pi4_ref_value;
    WORD32 ref_value;
 
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Sanity Check */
    ASSERT(dep_row >= 0);
    ASSERT(dep_row < ps_dep_mngr_state->i4_num_vert_units);
    ASSERT(cur_tile_col >= 0);
    ASSERT(cur_tile_col < ps_dep_mngr_state->i4_num_tile_cols);
 
    pi4_ref_value = ((volatile WORD32 *)(ps_dep_mngr_state->pv_units_prcsd_in_row)) +
                    (cur_tile_col * ps_dep_mngr_state->i4_num_vert_units) + dep_row;
 
    /* Sanity Check */
    ASSERT((cur_offset + dep_offset) <= ps_dep_mngr_state->i4_num_horz_units);
 
    /* Check whether Dep. is met */
    while(1)
    {
        ref_value = *pi4_ref_value;
 
        if(ref_value >= (cur_offset + dep_offset))
            break;
 
        if(1 == ps_dep_mngr_state->i1_sem_enable)
        {
            void *pv_sem_handle;
            WORD32 ret_val;
 
            (void)ret_val;
            pv_sem_handle = ps_dep_mngr_state->ppv_thrd_sem_handles[thrd_id];
 
            /* register the thread id before going to pend state */
            ps_dep_mngr_state->pi4_wait_thrd_id[dep_row] = thrd_id;
 
            /* go to the pend state */
            ret_val = osal_sem_wait(pv_sem_handle);
            //ASSERT(0 == ret_val);
        }
    }
 
    return 0;
}
 
/*!
**************************************************************************
* \if Function name : ihevce_dmgr_set_row_row_sync \endif
*
* \brief
*    This function sets the dependency and wakes up the proper semaphores
*    to start processing.
*
* \param[in]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
* \param[in]  cur_offset     : Current offset processed
* \param[in]  cur_row       : The cur. vertical position
* \param[in]  cur_tile_col  : The current column tile number (not tile id)
*   Assuming the dependency is within the tile only (Acroos tiles won't work now)
*
* \return
*    0 on Success and -1 on error
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 ihevce_dmgr_set_row_row_sync(
    void *pv_dep_mngr_state, WORD32 cur_offset, WORD32 cur_row, WORD32 cur_tile_col)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD32 *pi4_units_prcsd;
    void *pv_sem_handle;
    WORD32 ret_val;
 
    (void)ret_val;
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Sanity Check */
    ASSERT(cur_offset >= 0);
    ASSERT(cur_offset <= ps_dep_mngr_state->i4_num_horz_units);
    ASSERT(cur_row <= ps_dep_mngr_state->i4_num_vert_units);
    ASSERT(cur_tile_col >= 0);
    ASSERT(cur_tile_col < ps_dep_mngr_state->i4_num_tile_cols);
 
    DATA_SYNC();
 
    pi4_units_prcsd = ((WORD32 *)(ps_dep_mngr_state->pv_units_prcsd_in_row)) +
                      (cur_tile_col * ps_dep_mngr_state->i4_num_vert_units) + cur_row;
 
    /* Update the number of units processed */
    *pi4_units_prcsd = cur_offset;
 
    if(1 == ps_dep_mngr_state->i1_sem_enable)
    {
        WORD32 wait_thrd_id;
 
        wait_thrd_id = ps_dep_mngr_state->pi4_wait_thrd_id[cur_row];
 
        /* Post on threads waiting on the current row */
        if(-1 != wait_thrd_id)
        {
            pv_sem_handle = ps_dep_mngr_state->ppv_thrd_sem_handles[wait_thrd_id];
            /* Post on the semaphore */
            ret_val = osal_sem_post(pv_sem_handle);
            //ASSERT(0 == ret_val);
 
            ps_dep_mngr_state->pi4_wait_thrd_id[cur_row] = -1;
        }
 
        /* towards end of row all threads are posted (to avoid any corner cases) */
        if(cur_offset == ps_dep_mngr_state->i4_num_horz_units)
        {
            WORD32 ctr;
 
            for(ctr = 0; ctr < ps_dep_mngr_state->i4_num_thrds; ctr++)
            {
                ret_val = osal_sem_post(ps_dep_mngr_state->ppv_thrd_sem_handles[ctr]);
                //ASSERT(0 == ret_val);
            }
        }
    }
 
    return 0;
}
 
/*!
**************************************************************************
* \if Function name : ihevce_dmgr_chk_frm_frm_sync \endif
*
* \brief
*    This function checks whether the dependency is met to proceed with
*    processing. If condition is not met, it should go to a sem_wait state,
*    else start processing.
*    For Barrier case, the thread will wait till all threads have completed
*    the processing on the previosu instance of same stage
* \param[in]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
* \param[in]  thrd_id       : Thread id checking for dependency
*
* \return
*    0 on Success and -1 on error
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 ihevce_dmgr_chk_frm_frm_sync(void *pv_dep_mngr_state, WORD32 thrd_id)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    void *pv_sem_handle;
    volatile ULWORD64 *pu8_num_units_proc_prev;
    volatile ULWORD64 *pu8_num_units_proc_curr;
    ULWORD64 prev_value;
    ULWORD64 curr_value;
 
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
    pv_sem_handle = ps_dep_mngr_state->ppv_thrd_sem_handles[thrd_id];
 
    pu8_num_units_proc_curr = (volatile ULWORD64 *)ps_dep_mngr_state->pv_units_prcsd_in_row;
    pu8_num_units_proc_prev =
        (volatile ULWORD64 *)(pu8_num_units_proc_curr + ps_dep_mngr_state->i4_num_thrds);
 
    /* Check whether Dep. is met */
    while(1)
    {
        WORD32 ret_val;
 
        (void)ret_val;
        curr_value = pu8_num_units_proc_curr[thrd_id];
        prev_value = pu8_num_units_proc_prev[thrd_id];
 
        if(curr_value == (prev_value + 1))
        {
            break;
        }
        else
        {
            /* register the thread id before going to pend state */
            ps_dep_mngr_state->pi4_wait_thrd_id[thrd_id] = thrd_id;
 
            /* go to the pend state */
            ret_val = osal_sem_wait(pv_sem_handle);
            //ASSERT(0 == ret_val);
        }
    }
 
    /* store curr value to prev for next iteration */
    pu8_num_units_proc_prev[thrd_id] = pu8_num_units_proc_curr[thrd_id];
 
    return 0;
}
 
/*!
**************************************************************************
* \if Function name : ihevce_dmgr_update_frm_frm_sync \endif
*
* \brief
*    This function sets the dependency and wakes up the proper semaphores
*    to start processing.
*    For barrier case, if the dep. is met, all waiting threads should be waked up
*
* \param[in]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
*
* \return
*    0 on Success and -1 on error
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 ihevce_dmgr_update_frm_frm_sync(void *pv_dep_mngr_state)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    void *pv_sem_handle;
    volatile ULWORD64 *pu8_num_units_proc_curr;
    WORD32 ctr;
 
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    pu8_num_units_proc_curr = (volatile ULWORD64 *)ps_dep_mngr_state->pv_units_prcsd_in_row;
 
    /* Post on All vertical waiting threads semaphores & update the cur unit proc */
    for(ctr = 0; ctr < ps_dep_mngr_state->i4_num_thrds; ctr++)
    {
        WORD32 ret_val;
        WORD32 wait_thrd_id;
 
        (void)ret_val;
        /* increment the curr unit counter for all threads */
        pu8_num_units_proc_curr[ctr] = pu8_num_units_proc_curr[ctr] + 1;
 
        wait_thrd_id = ctr;
        //wait_thrd_id    = ps_dep_mngr_state->pi4_wait_thrd_id[ctr];
 
        if(-1 != wait_thrd_id)
        {
            pv_sem_handle = ps_dep_mngr_state->ppv_thrd_sem_handles[wait_thrd_id];
            /* Post on the semaphore */
            ret_val = osal_sem_post(pv_sem_handle);
            //ASSERT(0 == ret_val);
 
            ps_dep_mngr_state->pi4_wait_thrd_id[ctr] = -1;
        }
    }
 
    return 0;
}
 
/*!
**************************************************************************
* \if Function name : ihevce_dmgr_map_chk \endif
*
* \brief
*   This function checks whether all entries in the dependency map are set
*
* \param[in]  pu1_start    : Pointer to the start of the search area
* \param[in]  i4_num_ctb_x : Size   of search area
* \param[in]  i4_num_ctb_y : Size   of search area
* \param[in]  i4_stride    : Stride of search area
*
* \return
*    1 on Success otherwise 0
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32
    ihevce_dmgr_map_chk(WORD8 *pu1_start, WORD32 i4_num_ctb_x, WORD32 i4_num_ctb_y, WORD32 i4_stride)
{
    WORD8 *pi1_ctb = pu1_start;
    WORD32 row, col;
    WORD32 map_ready_flag = MAP_CTB_COMPLETE;
 
    for(row = 0; row < i4_num_ctb_y; row++)
    {
        for(col = 0; col < i4_num_ctb_x; col++)
        {
            map_ready_flag &= pi1_ctb[col];
        }
        pi1_ctb += i4_stride;
    }
 
    /* NOTE: early exit in the above loop can taken if map_ready_flag
    is found to be zero somewhere at the start itself */
    return (map_ready_flag == MAP_CTB_COMPLETE);
}
 
/*!
**************************************************************************
* \if Function name : ihevce_dmgr_map_chk_sync \endif
*
* \brief
*   This function checks whether the dependency is met by searching in a
*   rectangular area. If condition is not met, it should go to a sem_wait state,
*    else start processing.
*
* \param[in]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
* \param[in]  thrd_id     : Thread id of the current thread checking for dependency
* \param[in]  offset_x    : Offset of current CTB in Tile in ctb-unit
* \param[in]  offset_y    : Offset of current CTB in Tile in ctb-unit
* \param[in]  i4_sr_ctb_x : Search Range in ctb-unit
* \param[in]  i4_sr_ctb_y : Search Range in ctb-unit
*
* \return
*    0 on Success and -1 on error
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 ihevce_dmgr_map_chk_sync(
    void *pv_dep_mngr_state,
    WORD32 thrd_id,
    WORD32 offset_x,
    WORD32 offset_y,
    WORD32 i4_sr_ctb_x,
    WORD32 i4_sr_ctb_y)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    volatile WORD8 *pi1_ctb;
    WORD8 *pi1_tile_start;
    WORD32 i4_avail_left, i4_avail_right, i4_avail_top, i4_avail_bot;
    WORD32 i4_num_ctb_x, i4_num_ctb_y;
    WORD32 i4_stride;
    WORD32 i4_tile_wd, i4_tile_ht;  //in ctb units
 
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    i4_tile_wd = ps_dep_mngr_state->i4_num_horz_units - ps_dep_mngr_state->ai4_tile_xtra_ctb[1] -
                 ps_dep_mngr_state->ai4_tile_xtra_ctb[2];
 
    i4_tile_ht = ps_dep_mngr_state->i4_num_vert_units - ps_dep_mngr_state->ai4_tile_xtra_ctb[0] -
                 ps_dep_mngr_state->ai4_tile_xtra_ctb[3];
 
    i4_stride = ps_dep_mngr_state->i4_num_horz_units;
 
    /* Sanity Checks, confirm if ctb offsets are within tiles */
    ASSERT(offset_x >= 0);
    ASSERT(offset_y >= 0);
    ASSERT(offset_x < i4_tile_wd);
    ASSERT(offset_y < i4_tile_ht);
 
    pi1_tile_start = (WORD8 *)ps_dep_mngr_state->pv_units_prcsd_in_row;
    pi1_ctb = (volatile WORD8 *)pi1_tile_start;
 
    if(ps_dep_mngr_state->ai4_tile_xtra_ctb[0])
    {
        i4_avail_top = i4_sr_ctb_y;
    }
    else
    {
        i4_avail_top = MIN(i4_sr_ctb_y, offset_y);
    }
 
    if(ps_dep_mngr_state->ai4_tile_xtra_ctb[1])
    {
        i4_avail_left = i4_sr_ctb_x;
    }
    else
    {
        i4_avail_left = MIN(i4_sr_ctb_x, offset_x);
    }
 
    if(ps_dep_mngr_state->ai4_tile_xtra_ctb[2])
    {
        i4_avail_right = i4_sr_ctb_x;
    }
    else
    {
        i4_avail_right = MIN(i4_sr_ctb_x, (i4_tile_wd - offset_x - 1));
    }
 
    if(ps_dep_mngr_state->ai4_tile_xtra_ctb[3])
    {
        i4_avail_bot = i4_sr_ctb_y;
    }
    else
    {
        i4_avail_bot = MIN(i4_sr_ctb_y, (i4_tile_ht - offset_y - 1));
    }
 
    i4_num_ctb_x = (i4_avail_left + 1 + i4_avail_right);
    i4_num_ctb_y = (i4_avail_top + 1 + i4_avail_bot);
 
    /* Point to the start of the search-area */
    pi1_ctb += ((offset_y - i4_avail_top) * i4_stride + (offset_x - i4_avail_left));
 
    /* Check whether Dep. is met */
    while(1)
    {
        if(1 == ihevce_dmgr_map_chk((WORD8 *)pi1_ctb, i4_num_ctb_x, i4_num_ctb_y, i4_stride))
        {
            break;
        }
        else
        {
            if(1 == ps_dep_mngr_state->i1_sem_enable)
            {
                osal_sem_wait(ps_dep_mngr_state->ppv_thrd_sem_handles[thrd_id]);
            }
        }
    }
 
    return 0;
}
 
/*!
**************************************************************************
* \if Function name : ihevce_dmgr_map_set_sync \endif
*
* \brief
*    This function sets the dependency and wakes up the proper semaphores
*    to start processing.
*
* \param[in]  pv_dep_mngr_state  : Pointer to Sync Manager handle.
* \param[in]  offset_x           : Offset of current CTB in Tile(ctb unit)
* \param[in]  offset_y           : Offset of current CTB in Tile(ctb unit)
*
* \return
*    0 on Success and -1 on error
*
* \author
*  Ittiam
*
**************************************************************************
*/
WORD32 ihevce_dmgr_map_set_sync(
    void *pv_dep_mngr_state, WORD32 offset_x, WORD32 offset_y, WORD32 i4_map_value)
{
    dep_mngr_state_t *ps_dep_mngr_state;
    WORD8 *pi1_tile_start;
    WORD32 map_stride;
 
    ps_dep_mngr_state = (dep_mngr_state_t *)pv_dep_mngr_state;
 
    /* Sanity Checks */
    ASSERT(offset_x >= (-ps_dep_mngr_state->ai4_tile_xtra_ctb[1]));
    ASSERT(offset_y >= (-ps_dep_mngr_state->ai4_tile_xtra_ctb[0]));
 
    ASSERT(
        offset_x <
        (ps_dep_mngr_state->i4_num_horz_units - ps_dep_mngr_state->ai4_tile_xtra_ctb[1]));
 
    ASSERT(
        offset_y <
        (ps_dep_mngr_state->i4_num_vert_units - ps_dep_mngr_state->ai4_tile_xtra_ctb[0]));
 
    DATA_SYNC();
 
    map_stride = ps_dep_mngr_state->i4_num_horz_units;
 
    pi1_tile_start = (WORD8 *)ps_dep_mngr_state->pv_units_prcsd_in_row;
 
    /* Set the flag to indicate that this CTB has been processed */
    *(pi1_tile_start + offset_y * map_stride + offset_x) = (WORD8)i4_map_value;
 
    if(1 == ps_dep_mngr_state->i1_sem_enable)
    {
        WORD32 wait_thrd_id;
 
        /* Post on threads waiting on the current row */
        for(wait_thrd_id = 0; wait_thrd_id < ps_dep_mngr_state->i4_num_thrds; wait_thrd_id++)
        {
            /* Post on the semaphore */
            /* Map-mode: semaphore post is unconditionally done on all threads */
            osal_sem_post(ps_dep_mngr_state->ppv_thrd_sem_handles[wait_thrd_id]);
        }
    }
 
    return 0;
}