liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
/******************************************************************************
 *
 * 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_entropy_interface.c
*
* @brief
*    This file contains function definitions for entropy interface related to
*    memory init and process apis
*
* @author
*    Ittiam
*
* List of Functions
*   ihevce_entropy_get_num_mem_recs()
*   ihevce_entropy_size_of_out_buffer()
*   ihevce_entropy_get_mem_recs()
*   ihevce_entropy_init()
*   ihevce_entropy_encode_frame()
*
******************************************************************************
*/
 
/*****************************************************************************/
/* 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_defs.h"
#include "ihevc_macros.h"
#include "ihevc_debug.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_trans_tables.h"
#include "ihevc_trans_macros.h"
 
#include "ihevce_defs.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_error_checks.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_global_tables.h"
#include "ihevce_entropy_structs.h"
#include "ihevce_entropy_interface.h"
#include "ihevce_encode_header.h"
#include "ihevce_encode_header_sei_vui.h"
#include "ihevce_trace.h"
 
#include "cast_types.h"
#include "osal.h"
#include "osal_defaults.h"
 
/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/
 
/**
******************************************************************************
*
*  @brief Number of memory records are returned for entropy module
*
*  @par   Description
*
*  @return      number of memory records
*
******************************************************************************
*/
WORD32 ihevce_entropy_get_num_mem_recs(void)
{
    return (NUM_ENTROPY_MEM_RECS);
}
 
/**
******************************************************************************
*
*  @brief Estimated bitstream buffer size basing on input dimensions
*
*  @par   Description
*
*  @return      bitstream buffer size
*
******************************************************************************
*/
WORD32 ihevce_entropy_size_of_out_buffer(frm_proc_ent_cod_ctxt_t *ps_curr_inp)
{
    WORD32 i4_size;
 
    i4_size = (WORD32)(
        ps_curr_inp->ps_sps->i2_pic_height_in_luma_samples *
        ps_curr_inp->ps_sps->i2_pic_width_in_luma_samples);
 
    return (i4_size);
}
 
/**
******************************************************************************
*
*  @brief Populates Memory requirements of the entropy module
*
*  @par   Description
*
*  @param[inout]   ps_mem_tab
*  pointer to memory descriptors table
*
*  @param[in]      ps_init_prms
*  Create time static parameters
*
*  @param[in]      i4_mem_space
*  memspace in whihc memory request should be done
*
*  @return      number of memory requirements filled
*
******************************************************************************
*/
WORD32 ihevce_entropy_get_mem_recs(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    WORD32 i4_mem_space,
    WORD32 i4_resolution_id)
{
    /* memories should be requested assuming worst case requirememnts */
    WORD32 max_width = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width;
    WORD32 max_height = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_height;
    WORD32 max_align_width = ALIGN64(max_width);
    WORD32 max_align_height = ALIGN64(max_height);
 
    /* Module context structure */
    ps_mem_tab[ENTROPY_CTXT].i4_mem_size = sizeof(entropy_context_t);
    ps_mem_tab[ENTROPY_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[ENTROPY_CTXT].i4_mem_alignment = 64;
 
    /* top row cu skip flags (1 bit per 8x8CU)  */
    ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_size = max_align_width >> 6;
    ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_alignment = 64;
 
    /* top row CU Depth (1 byte per 8x8CU) */
    ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_size = (max_align_width >> 3);
    ps_mem_tab[ENTROPY_TOP_CU_DEPTH].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_alignment = 64;
 
    /* Dummy_buffer to handle first pass MBR case*/
    ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_size = (max_align_width * max_align_height * 2);
    ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
    ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_alignment = 64;
 
    return (NUM_ENTROPY_MEM_RECS);
}
 
/**
******************************************************************************
*
*  @brief Intialization of entropy module
*
*  @par   Description
*  pointers of the memory requests done in ihevce_entropy_get_mem_recs() are
*  used to initialized the entropy module and the handle is returned
*
*  @param[inout]   ps_mem_tab
*  pointer to memory descriptors table
*
*  @param[in]      ps_init_prms
*  Create time static parameters
*
*  @return
*  Handle of the entropy module returned as void ptr
*
******************************************************************************
*/
void *ihevce_entropy_init(
    iv_mem_rec_t *ps_mem_tab,
    ihevce_static_cfg_params_t *ps_init_prms,
    void *pv_tile_params_base,
    WORD32 i4_res_id)
{
    entropy_context_t *ps_entropy_ctxt;
 
    /* Entropy state structure */
    ps_entropy_ctxt = (entropy_context_t *)ps_mem_tab[ENTROPY_CTXT].pv_base;
    memset(ps_entropy_ctxt, 0, sizeof(entropy_context_t));
 
    ps_entropy_ctxt->pu1_skip_cu_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].pv_base;
    ps_entropy_ctxt->pu1_cu_depth_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_CU_DEPTH].pv_base;
    ps_entropy_ctxt->pv_dummy_out_buf = ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].pv_base;
    ps_entropy_ctxt->i4_bitstream_buf_size = ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_size;
 
    /* perform all one time initialisation here */
    /*************************************************************************/
    /* Note pu1_cbf_cb, pu1_cbf_cr initialization are done with array idx 1  */
    /* This is because these flags are accessed as pu1_cbf_cb[tfr_depth - 1] */
    /* without cheking for tfr_depth= 0                                      */
    /*************************************************************************/
    ps_entropy_ctxt->apu1_cbf_cb[0] = &ps_entropy_ctxt->au1_cbf_cb[0][1];
    ps_entropy_ctxt->apu1_cbf_cr[0] = &ps_entropy_ctxt->au1_cbf_cr[0][1];
    ps_entropy_ctxt->apu1_cbf_cb[1] = &ps_entropy_ctxt->au1_cbf_cb[1][1];
    ps_entropy_ctxt->apu1_cbf_cr[1] = &ps_entropy_ctxt->au1_cbf_cr[1][1];
 
    memset(ps_entropy_ctxt->au1_cbf_cb, 0, (MAX_TFR_DEPTH + 1) * 2 * sizeof(UWORD8));
 
    /* register codec level */
    ps_entropy_ctxt->i4_codec_level =
        ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id].i4_codec_level;
 
    /*  Flag to enable/disable insertion of SPS, VPS & PPS at every CRA frame   */
    ps_entropy_ctxt->i4_sps_at_cdr_enable = ps_init_prms->s_out_strm_prms.i4_sps_at_cdr_enable;
 
    /* Store Tile params base into entropy context */
    ps_entropy_ctxt->pv_tile_params_base = pv_tile_params_base;
 
    ps_entropy_ctxt->pv_sys_api = (void *)&ps_init_prms->s_sys_api;
 
    ps_entropy_ctxt->i4_slice_segment_mode = ps_init_prms->s_slice_params.i4_slice_segment_mode;
 
    /* Set slice segment length */
    if((ps_entropy_ctxt->i4_slice_segment_mode == 1) ||
       (ps_entropy_ctxt->i4_slice_segment_mode == 2))
    {
        ps_entropy_ctxt->i4_slice_segment_max_length =
            ps_init_prms->s_slice_params.i4_slice_segment_argument;
    }
    else
    {
        ps_entropy_ctxt->i4_slice_segment_max_length = 0;
    }
 
    /* return the handle to caller */
    return ((void *)ps_entropy_ctxt);
}
 
/**
******************************************************************************
*
*  @brief entry point for entropy coding of a frame
*
*  @par   Description
*  This function generates nal headers like SPS/PPS/slice header and call the
*  slice data entropy coding function
*
*  @param[in]   ps_enc_ctxt
*  pointer to encoder context (handle)
*
*  @param[out]   ps_curr_out
*  pointer to output data buffer context where bitstream is generated
*
*  @param[out]   ps_curr_inp
*  pointer to entropy input params context
*
*  @return      success or failure error code
*
******************************************************************************
*/
WORD32 ihevce_entropy_encode_frame(
    void *pv_entropy_hdl,
    iv_output_data_buffs_t *ps_curr_out,
    frm_proc_ent_cod_ctxt_t *ps_curr_inp,
    WORD32 i4_out_buf_size)
{
    WORD32 ret = IHEVCE_SUCCESS;
    WORD32 tile_ctr, total_tiles = 1;
    entropy_context_t *ps_entropy_ctxt = (entropy_context_t *)pv_entropy_hdl;
 
    /* current frame slice type and nal type */
    WORD32 slice_type = ps_curr_inp->s_slice_hdr.i1_slice_type;
 
    /* current frame slice type and nal type */
    WORD32 nal_type = ps_curr_inp->i4_slice_nal_type;
 
    /* read vps, sps and pps from input params */
    vps_t *ps_vps = ps_curr_inp->ps_vps;
    sps_t *ps_sps = ps_curr_inp->ps_sps;
    pps_t *ps_pps = ps_curr_inp->ps_pps;
    sei_params_t *ps_sei = &ps_curr_inp->s_sei;
    ihevce_tile_params_t *ps_tile_params_base;
    WORD32 out_buf_size = i4_out_buf_size;
 
    /* Headers are repeated once per IDR. Should be changed to every CRA */
    WORD32 insert_vps_sps_pps =
        ((slice_type == ISLICE) &&
         (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
 
    WORD32 insert_per_cra =
        ((slice_type == ISLICE) &&
         (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
    bitstrm_t *ps_bitstrm = &ps_entropy_ctxt->s_bit_strm;
 
    ULWORD64 u8_bits_slice_header_prev;
 
    WORD32 i4_slice_segment_max_length_bckp;
    WORD32 i4_max_num_slices;
 
    ihevce_sys_api_t *ps_sys_api = (ihevce_sys_api_t *)ps_entropy_ctxt->pv_sys_api;
 
#if POPULATE_NAL_OFFSET
    ULWORD64 u8_bitstream_base = (ULWORD64)ps_curr_out->pv_bitstream_bufs;
#endif
    if(0 == ps_entropy_ctxt->i4_sps_at_cdr_enable)
    {
        insert_vps_sps_pps =
            ((slice_type == ISLICE) && ((NAL_IDR_N_LP == nal_type) || (NAL_IDR_W_LP == nal_type)));
    }
    /* intialize vps, sps, pps, sei and slice header in entropy context */
    ps_entropy_ctxt->ps_vps = ps_vps;
    ps_entropy_ctxt->ps_sps = ps_sps;
    ps_entropy_ctxt->ps_pps = ps_pps;
    ps_entropy_ctxt->ps_sei = ps_sei;
    ps_entropy_ctxt->ps_slice_hdr = &ps_curr_inp->s_slice_hdr;
    ps_entropy_ctxt->i4_is_cu_cbf_zero = 1;
 
    ps_entropy_ctxt->ps_pic_level_info = &ps_curr_inp->s_pic_level_info;
 
    /* intialize the frame level ctb pointer for current slice */
    ps_entropy_ctxt->ps_frm_ctb = ps_curr_inp->ps_frm_ctb_data;
 
    /* Initiallizing to indicate the start of frame */
    ps_entropy_ctxt->i4_next_slice_seg_x = 0;
    ps_entropy_ctxt->i4_next_slice_seg_y = 0;
 
    /* enable the residue encode flag */
    ps_entropy_ctxt->i4_enable_res_encode = 1;
 
    /* Initialize the bitstream engine */
    ret |= ihevce_bitstrm_init(ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, out_buf_size);
 
    /* Reset Bitstream NAL counter */
    ps_bitstrm->i4_num_nal = 0;
 
    /*PIC INFO: Store the Bits before slice header is encoded*/
    u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
 
    /* generate AUD if enabled from the application */
    if(1 == ps_curr_inp->i1_aud_present_flag)
    {
        UWORD8 u1_pic_type;
 
        switch(slice_type)
        {
        case ISLICE:
            u1_pic_type = 0;
            break;
        case PSLICE:
            u1_pic_type = 1;
            break;
        default:
            u1_pic_type = 2;
            break;
        }
 
        ret |= ihevce_generate_aud(ps_bitstrm, u1_pic_type);
    }
 
    if(insert_vps_sps_pps)
    {
        /* generate vps */
        ret |= ihevce_generate_vps(ps_bitstrm, ps_entropy_ctxt->ps_vps);
 
        /* generate sps */
        ret |= ihevce_generate_sps(ps_bitstrm, ps_entropy_ctxt->ps_sps);
 
        /* generate pps */
        ret |= ihevce_generate_pps(ps_bitstrm, ps_entropy_ctxt->ps_pps);
    }
 
    /* generate sei */
    if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
    {
        WORD32 i4_insert_prefix_sei =
            ps_entropy_ctxt->ps_sei->i1_buf_period_params_present_flag ||
            ps_entropy_ctxt->ps_sei->i1_pic_timing_params_present_flag ||
            ps_entropy_ctxt->ps_sei->i1_recovery_point_params_present_flag ||
            ps_entropy_ctxt->ps_sei->i4_sei_mastering_disp_colour_vol_params_present_flags ||
            ps_curr_inp->u4_num_sei_payload || ps_curr_inp->s_sei.i1_sei_cll_enable;
 
        if(i4_insert_prefix_sei)
        {
            ret |= ihevce_generate_sei(
                ps_bitstrm,
                ps_entropy_ctxt->ps_sei,
                &ps_entropy_ctxt->ps_sps->s_vui_parameters,
                insert_per_cra,
                NAL_PREFIX_SEI,
                ps_curr_inp->u4_num_sei_payload,
                &ps_curr_inp->as_sei_payload[0]);
        }
    }
 
    /*PIC INFO: Populate slice header bits */
    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
        (ps_bitstrm->u4_strm_buf_offset * 8) - u8_bits_slice_header_prev;
 
    ps_tile_params_base = (ihevce_tile_params_t *)ps_entropy_ctxt->pv_tile_params_base;
 
    ps_curr_out->i4_bytes_generated = 0;  //Init
 
    /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/
    {
        WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal;
        WORD32 ctr = 0;
 
        ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU);
 
        ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals;
        for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++)
        {
            /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
            ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr];
 
#if POPULATE_NAL_SIZE
 
            /* ----------Populate NAL Size  -------------*/
            if((ctr + 1) < num_non_vcl_prefix_nals)
            {
                ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1];
                ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
            }
            else
            {
                ULWORD64 u8_next_nal_start =
                    (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
                ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
            }
            ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0);
 
#elif POPULATE_NAL_OFFSET
 
            /* ----------Populate NAL Offset  -------------*/
 
            ASSERT(u8_cur_nal_start >= u8_bitstream_base);
            ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] =
                (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
 
            if(ctr)
            {
                /* sanity check on increasing NAL offsets */
                ASSERT(
                    ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] >
                    ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]);
            }
#endif /* POPULATE_NAL_SIZE */
        }
    }
 
    total_tiles = ps_tile_params_base->i4_num_tiles;
 
    /* frame level NUM slices related params initialisations */
    {
        WORD32 codec_level_index = ihevce_get_level_index(ps_entropy_ctxt->i4_codec_level);
 
        i4_max_num_slices = g_as_level_data[codec_level_index].i4_max_slices_per_picture;
        ps_entropy_ctxt->i4_num_slice_seg = 0;
    }
 
    /* back up slice arg length before pic encoding */
    i4_slice_segment_max_length_bckp = ps_entropy_ctxt->i4_slice_segment_max_length;
 
    for(tile_ctr = 0; tile_ctr < total_tiles; tile_ctr++)
    {
        WORD32 i4_end_of_slice = 0;
 
        /* Loop over all the slice segments */
        while(0 == i4_end_of_slice)
        {
            WORD32 i4_bytes_generated, i4_slice_header_bits;
 
            /*PIC INFO: Store the Bits before slice header is encoded*/
            u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
 
            /* generate slice header */
            ret |= ihevce_generate_slice_header(
                ps_bitstrm,
                nal_type,
                ps_entropy_ctxt->ps_slice_hdr,
                ps_entropy_ctxt->ps_pps,
                ps_entropy_ctxt->ps_sps,
                &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
                &ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset,
                (ps_tile_params_base + tile_ctr),
                ps_entropy_ctxt->i4_next_slice_seg_x,
                ps_entropy_ctxt->i4_next_slice_seg_y);
 
            i4_slice_header_bits =
                (ps_bitstrm->u4_strm_buf_offset * 8) - (WORD32)u8_bits_slice_header_prev;
 
            /* Update slice segment length with bytes in slice header */
            if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
            {
                ps_entropy_ctxt->i4_slice_seg_len = (i4_slice_header_bits / 8);
            }
            else  //Initiallize to zero
            {
                ps_entropy_ctxt->i4_slice_seg_len = 0;
            }
 
            /*PIC INFO: Populate slice header bits */
            ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
                i4_slice_header_bits;
 
            /* check if number of slices generated in is MAX -1 as per codec_level */
            if(ps_entropy_ctxt->i4_num_slice_seg == (i4_max_num_slices - 1))
            {
                /* i4_slice_segment_max_length is set to a huge positive value          */
                /* so that remaining CTBS in the picture gets encoded as a single slice */
                ps_entropy_ctxt->i4_slice_segment_max_length = 0x7FFFFFFF;
            }
 
            /* encode the slice data */
            ret |= ihevce_encode_slice_data(
                ps_entropy_ctxt, (ps_tile_params_base + tile_ctr), &i4_end_of_slice);
 
            /* increment the number of slices generated */
            ps_entropy_ctxt->i4_num_slice_seg++;
 
            if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
            {
                /*after encoding is done each slice offset is available. Enter these offset in slice header*/
                ihevce_insert_entry_offset_slice_header(
                    &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
                    ps_entropy_ctxt->ps_slice_hdr,
                    ps_entropy_ctxt->ps_pps,
                    ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset);
            }
 
            /* compute the bytes generated and return */
            if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
            {
                i4_bytes_generated = ps_entropy_ctxt->s_dup_bit_strm_ent_offset.u4_strm_buf_offset;
            }
            else
            {
                i4_bytes_generated = ps_entropy_ctxt->s_cabac_ctxt.u4_strm_buf_offset;
            }
 
            /* Updating bytes generated and Updating strm_buffer pointer */
            ps_curr_out->i4_bytes_generated += i4_bytes_generated;
 
            /* Re-Initialize the bitstream engine after each tile or slice */
            ihevce_bitstrm_init(
                ps_bitstrm, (ps_bitstrm->pu1_strm_buffer + i4_bytes_generated), out_buf_size);
        }
    }
 
    /* Max slices related warning prints based on last slice status */
    if(ps_entropy_ctxt->i4_num_slice_seg == i4_max_num_slices)
    {
        if(ps_entropy_ctxt->i4_slice_seg_len >= i4_slice_segment_max_length_bckp)
        {
            if(1 == ps_entropy_ctxt->i4_slice_segment_mode)
            {
                ps_sys_api->ihevce_printf(
                    ps_sys_api->pv_cb_handle,
                    "IHEVCE_WARNING: Last slice contains %d CTBs exceeds %d (Max limit of CTBs "
                    "configured). As per codec_level max number of slices per frame is %d\n",
                    ps_entropy_ctxt->i4_slice_seg_len,
                    i4_slice_segment_max_length_bckp,
                    i4_max_num_slices);
            }
            else if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
            {
                ps_sys_api->ihevce_printf(
                    ps_sys_api->pv_cb_handle,
                    "IHEVCE_WARNING: Last slice contains %d Bytes exceeds %d (Max limit of Bytes "
                    "configured). As per codec_level max number of slices per frame is %d\n",
                    ps_entropy_ctxt->i4_slice_seg_len,
                    i4_slice_segment_max_length_bckp,
                    i4_max_num_slices);
            }
        }
    }
 
    /* restore slice arg length after pic encoding */
    ps_entropy_ctxt->i4_slice_segment_max_length = i4_slice_segment_max_length_bckp;
 
    /* ---------------------- Initialize VCL NAL Size/offsets ---------------------------*/
    {
        WORD32 vcl_start = ps_curr_out->i4_num_non_vcl_prefix_nals;
        WORD32 num_vcl_nals = ps_bitstrm->i4_num_nal - vcl_start;
        WORD32 ctr = 0;
 
        ASSERT(num_vcl_nals > 0);
        ASSERT(num_vcl_nals <= MAX_NUM_VCL_NALS_PER_AU);
 
        ps_curr_out->i4_num_vcl_nals = num_vcl_nals;
        for(ctr = 0; ctr < MIN(num_vcl_nals, MAX_NUM_VCL_NALS_PER_AU); ctr++)
        {
            /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
            ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start];
 
#if POPULATE_NAL_SIZE
 
            /* ----------Populate NAL Size  -------------*/
            if((ctr + 1) < num_vcl_nals)
            {
                ULWORD64 u8_next_nal_start =
                    (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start + 1];
                ps_curr_out->ai4_size_vcl_nals[ctr] =
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
            }
            else
            {
                ULWORD64 u8_next_nal_start =
                    (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
                ps_curr_out->ai4_size_vcl_nals[ctr] =
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
            }
            ASSERT(ps_curr_out->ai4_size_vcl_nals[ctr] > 0);
 
#elif POPULATE_NAL_OFFSET
 
            /* ----------Populate NAL Offset  -------------*/
 
            ASSERT(u8_cur_nal_start >= u8_bitstream_base);
            ps_curr_out->ai4_off_vcl_nals[ctr] = (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
 
            if(ctr)
            {
                /* sanity check on increasing NAL offsets */
                ASSERT(ps_curr_out->ai4_off_vcl_nals[ctr] > ps_curr_out->ai4_off_vcl_nals[ctr - 1]);
            }
#endif /* POPULATE_NAL_SIZE */
        }
    }
 
    /* generate suffix sei */
    if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
    {
        /* Insert hash SEI */
        if(0 != ps_entropy_ctxt->ps_sei->i1_decoded_pic_hash_sei_flag)
        {
            ret |= ihevce_generate_sei(
                ps_bitstrm,
                ps_entropy_ctxt->ps_sei,
                &ps_entropy_ctxt->ps_sps->s_vui_parameters,
                insert_per_cra,
                NAL_SUFFIX_SEI,
                ps_curr_inp->u4_num_sei_payload,
                &ps_curr_inp->as_sei_payload[0]);
        }
 
        /* Updating bytes generated */
        ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
    }
 
    /* generate end of sequence nal */
    if((1 == ps_curr_inp->i1_eos_present_flag) && (ps_curr_inp->i4_is_end_of_idr_gop == 1))
    {
        ret |= ihevce_generate_eos(ps_bitstrm);
        /* Updating bytes generated */
        ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
    }
 
    /* ------------------- Initialize non-VCL suffix NAL Size/offsets -----------------------*/
    {
        WORD32 non_vcl_suffix_start =
            ps_curr_out->i4_num_non_vcl_prefix_nals + ps_curr_out->i4_num_vcl_nals;
        WORD32 num_non_vcl_suffix_nals = ps_bitstrm->i4_num_nal - non_vcl_suffix_start;
        WORD32 ctr = 0;
 
        ASSERT(num_non_vcl_suffix_nals >= 0);
        ASSERT(num_non_vcl_suffix_nals <= MAX_NUM_SUFFIX_NALS_PER_AU);
 
        ps_curr_out->i4_num_non_vcl_suffix_nals = num_non_vcl_suffix_nals;
        for(ctr = 0; ctr < MIN(num_non_vcl_suffix_nals, MAX_NUM_SUFFIX_NALS_PER_AU); ctr++)
        {
            /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
            ULWORD64 u8_cur_nal_start =
                (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start];
 
#if POPULATE_NAL_SIZE
 
            /* ----------Populate NAL Size  -------------*/
            if((ctr + 1) < num_non_vcl_suffix_nals)
            {
                ULWORD64 u8_next_nal_start =
                    (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start + 1];
                ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
            }
            else
            {
                ULWORD64 u8_next_nal_start =
                    (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
                ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
            }
            ASSERT(ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] > 0);
 
#elif POPULATE_NAL_OFFSET
 
            /* ----------Populate NAL Offset  -------------*/
 
            ASSERT(u8_cur_nal_start >= u8_bitstream_base);
            ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] =
                (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
 
            if(ctr)
            {
                /* sanity check on increasing NAL offsets */
                ASSERT(
                    ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] >
                    ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr - 1]);
            }
#endif /* POPULATE_NAL_SIZE */
        }
    }
 
    /*PIC INFO: Populatinf Ref POC, weights and offset*/
    {
        WORD32 i;
        ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l0_active =
            ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active;
        ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l1_active =
            ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active;
        for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
        {
            ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l0[i] =
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l0[i];
            ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l0[i] =
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l0[i];
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l0[i] =
                (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l0[i] /
                (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l0[i] =
                ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l0[i];
        }
        for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
        {
            ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l1[i] =
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l1[i];
            ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l1[i] =
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l1[i];
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l1[i] =
                (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l1[i] /
                (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l1[i] =
                ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l1[i];
        }
    }
 
    /* attach the time stamp of the input to output */
    ps_curr_out->i4_out_timestamp_low = ps_curr_inp->i4_inp_timestamp_low;
 
    ps_curr_out->i4_out_timestamp_high = ps_curr_inp->i4_inp_timestamp_high;
 
    /*attach the app frame info of this buffer */
    ps_curr_out->pv_app_frm_ctxt = ps_curr_inp->pv_app_frm_ctxt;
 
    /* frame never skipped for now */
    ps_curr_out->i4_frame_skipped = 0;
 
    /* update error code and return */
    ps_curr_out->i4_process_error_code = ret;
 
    switch(slice_type)
    {
    case ISLICE:
        if((nal_type == NAL_IDR_N_LP) || (NAL_IDR_W_LP == nal_type))
        {
            ps_curr_out->i4_encoded_frame_type = IV_IDR_FRAME;
        }
        else
        {
            ps_curr_out->i4_encoded_frame_type = IV_I_FRAME;
        }
        break;
    case PSLICE:
        ps_curr_out->i4_encoded_frame_type = IV_P_FRAME;
        break;
    case BSLICE:
        ps_curr_out->i4_encoded_frame_type = IV_B_FRAME;
        break;
    }
 
    if(IHEVCE_SUCCESS == ret)
    {
        ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
    }
    else
    {
        ps_curr_out->i4_process_ret_sts = IV_FAIL;
    }
 
    return (ret);
}