xref: /aosp_15_r20/external/libhevc/encoder/ihevce_entropy_interface.c (revision c83a76b084498d55f252f48b2e3786804cdf24b7)
1 /******************************************************************************
2  *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 /**
21 ******************************************************************************
22 * @file ihevce_entropy_interface.c
23 *
24 * @brief
25 *    This file contains function definitions for entropy interface related to
26 *    memory init and process apis
27 *
28 * @author
29 *    Ittiam
30 *
31 * List of Functions
32 *   ihevce_entropy_get_num_mem_recs()
33 *   ihevce_entropy_size_of_out_buffer()
34 *   ihevce_entropy_get_mem_recs()
35 *   ihevce_entropy_init()
36 *   ihevce_entropy_encode_frame()
37 *
38 ******************************************************************************
39 */
40 
41 /*****************************************************************************/
42 /* File Includes                                                             */
43 /*****************************************************************************/
44 /* System include files */
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <assert.h>
49 #include <stdarg.h>
50 #include <math.h>
51 
52 /* User include files */
53 #include "ihevc_typedefs.h"
54 #include "itt_video_api.h"
55 #include "ihevce_api.h"
56 
57 #include "rc_cntrl_param.h"
58 #include "rc_frame_info_collector.h"
59 #include "rc_look_ahead_params.h"
60 
61 #include "ihevc_defs.h"
62 #include "ihevc_macros.h"
63 #include "ihevc_debug.h"
64 #include "ihevc_structs.h"
65 #include "ihevc_platform_macros.h"
66 #include "ihevc_deblk.h"
67 #include "ihevc_itrans_recon.h"
68 #include "ihevc_chroma_itrans_recon.h"
69 #include "ihevc_chroma_intra_pred.h"
70 #include "ihevc_intra_pred.h"
71 #include "ihevc_inter_pred.h"
72 #include "ihevc_mem_fns.h"
73 #include "ihevc_padding.h"
74 #include "ihevc_weighted_pred.h"
75 #include "ihevc_sao.h"
76 #include "ihevc_resi_trans.h"
77 #include "ihevc_quant_iquant_ssd.h"
78 #include "ihevc_cabac_tables.h"
79 #include "ihevc_trans_tables.h"
80 #include "ihevc_trans_macros.h"
81 
82 #include "ihevce_defs.h"
83 #include "ihevce_lap_enc_structs.h"
84 #include "ihevce_multi_thrd_structs.h"
85 #include "ihevce_multi_thrd_funcs.h"
86 #include "ihevce_me_common_defs.h"
87 #include "ihevce_had_satd.h"
88 #include "ihevce_error_codes.h"
89 #include "ihevce_error_checks.h"
90 #include "ihevce_bitstream.h"
91 #include "ihevce_cabac.h"
92 #include "ihevce_rdoq_macros.h"
93 #include "ihevce_function_selector.h"
94 #include "ihevce_enc_structs.h"
95 #include "ihevce_global_tables.h"
96 #include "ihevce_entropy_structs.h"
97 #include "ihevce_entropy_interface.h"
98 #include "ihevce_encode_header.h"
99 #include "ihevce_encode_header_sei_vui.h"
100 #include "ihevce_trace.h"
101 
102 #include "cast_types.h"
103 #include "osal.h"
104 #include "osal_defaults.h"
105 
106 /*****************************************************************************/
107 /* Function Definitions                                                      */
108 /*****************************************************************************/
109 
110 /**
111 ******************************************************************************
112 *
113 *  @brief Number of memory records are returned for entropy module
114 *
115 *  @par   Description
116 *
117 *  @return      number of memory records
118 *
119 ******************************************************************************
120 */
ihevce_entropy_get_num_mem_recs(void)121 WORD32 ihevce_entropy_get_num_mem_recs(void)
122 {
123     return (NUM_ENTROPY_MEM_RECS);
124 }
125 
126 /**
127 ******************************************************************************
128 *
129 *  @brief Estimated bitstream buffer size basing on input dimensions
130 *
131 *  @par   Description
132 *
133 *  @return      bitstream buffer size
134 *
135 ******************************************************************************
136 */
ihevce_entropy_size_of_out_buffer(frm_proc_ent_cod_ctxt_t * ps_curr_inp)137 WORD32 ihevce_entropy_size_of_out_buffer(frm_proc_ent_cod_ctxt_t *ps_curr_inp)
138 {
139     WORD32 i4_size;
140 
141     i4_size = (WORD32)(
142         ps_curr_inp->ps_sps->i2_pic_height_in_luma_samples *
143         ps_curr_inp->ps_sps->i2_pic_width_in_luma_samples);
144 
145     return (i4_size);
146 }
147 
148 /**
149 ******************************************************************************
150 *
151 *  @brief Populates Memory requirements of the entropy module
152 *
153 *  @par   Description
154 *
155 *  @param[inout]   ps_mem_tab
156 *  pointer to memory descriptors table
157 *
158 *  @param[in]      ps_init_prms
159 *  Create time static parameters
160 *
161 *  @param[in]      i4_mem_space
162 *  memspace in whihc memory request should be done
163 *
164 *  @return      number of memory requirements filled
165 *
166 ******************************************************************************
167 */
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)168 WORD32 ihevce_entropy_get_mem_recs(
169     iv_mem_rec_t *ps_mem_tab,
170     ihevce_static_cfg_params_t *ps_init_prms,
171     WORD32 i4_mem_space,
172     WORD32 i4_resolution_id)
173 {
174     /* memories should be requested assuming worst case requirememnts */
175     WORD32 max_width = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width;
176     WORD32 max_height = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_height;
177     WORD32 max_align_width = ALIGN64(max_width);
178     WORD32 max_align_height = ALIGN64(max_height);
179 
180     /* Module context structure */
181     ps_mem_tab[ENTROPY_CTXT].i4_mem_size = sizeof(entropy_context_t);
182     ps_mem_tab[ENTROPY_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
183     ps_mem_tab[ENTROPY_CTXT].i4_mem_alignment = 64;
184 
185     /* top row cu skip flags (1 bit per 8x8CU)  */
186     ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_size = max_align_width >> 6;
187     ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
188     ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_alignment = 64;
189 
190     /* top row CU Depth (1 byte per 8x8CU) */
191     ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_size = (max_align_width >> 3);
192     ps_mem_tab[ENTROPY_TOP_CU_DEPTH].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
193     ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_alignment = 64;
194 
195     /* Dummy_buffer to handle first pass MBR case*/
196     ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_size = (max_align_width * max_align_height * 2);
197     ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
198     ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_alignment = 64;
199 
200     return (NUM_ENTROPY_MEM_RECS);
201 }
202 
203 /**
204 ******************************************************************************
205 *
206 *  @brief Intialization of entropy module
207 *
208 *  @par   Description
209 *  pointers of the memory requests done in ihevce_entropy_get_mem_recs() are
210 *  used to initialized the entropy module and the handle is returned
211 *
212 *  @param[inout]   ps_mem_tab
213 *  pointer to memory descriptors table
214 *
215 *  @param[in]      ps_init_prms
216 *  Create time static parameters
217 *
218 *  @return
219 *  Handle of the entropy module returned as void ptr
220 *
221 ******************************************************************************
222 */
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)223 void *ihevce_entropy_init(
224     iv_mem_rec_t *ps_mem_tab,
225     ihevce_static_cfg_params_t *ps_init_prms,
226     void *pv_tile_params_base,
227     WORD32 i4_res_id)
228 {
229     entropy_context_t *ps_entropy_ctxt;
230 
231     /* Entropy state structure */
232     ps_entropy_ctxt = (entropy_context_t *)ps_mem_tab[ENTROPY_CTXT].pv_base;
233     memset(ps_entropy_ctxt, 0, sizeof(entropy_context_t));
234 
235     ps_entropy_ctxt->pu1_skip_cu_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].pv_base;
236     ps_entropy_ctxt->pu1_cu_depth_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_CU_DEPTH].pv_base;
237     ps_entropy_ctxt->pv_dummy_out_buf = ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].pv_base;
238     ps_entropy_ctxt->i4_bitstream_buf_size = ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_size;
239 
240     /* perform all one time initialisation here */
241     /*************************************************************************/
242     /* Note pu1_cbf_cb, pu1_cbf_cr initialization are done with array idx 1  */
243     /* This is because these flags are accessed as pu1_cbf_cb[tfr_depth - 1] */
244     /* without cheking for tfr_depth= 0                                      */
245     /*************************************************************************/
246     ps_entropy_ctxt->apu1_cbf_cb[0] = &ps_entropy_ctxt->au1_cbf_cb[0][1];
247     ps_entropy_ctxt->apu1_cbf_cr[0] = &ps_entropy_ctxt->au1_cbf_cr[0][1];
248     ps_entropy_ctxt->apu1_cbf_cb[1] = &ps_entropy_ctxt->au1_cbf_cb[1][1];
249     ps_entropy_ctxt->apu1_cbf_cr[1] = &ps_entropy_ctxt->au1_cbf_cr[1][1];
250 
251     memset(ps_entropy_ctxt->au1_cbf_cb, 0, (MAX_TFR_DEPTH + 1) * 2 * sizeof(UWORD8));
252 
253     /* register codec level */
254     ps_entropy_ctxt->i4_codec_level =
255         ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id].i4_codec_level;
256 
257     /*  Flag to enable/disable insertion of SPS, VPS & PPS at every CRA frame   */
258     ps_entropy_ctxt->i4_sps_at_cdr_enable = ps_init_prms->s_out_strm_prms.i4_sps_at_cdr_enable;
259 
260     /* Store Tile params base into entropy context */
261     ps_entropy_ctxt->pv_tile_params_base = pv_tile_params_base;
262 
263     ps_entropy_ctxt->pv_sys_api = (void *)&ps_init_prms->s_sys_api;
264 
265     ps_entropy_ctxt->i4_slice_segment_mode = ps_init_prms->s_slice_params.i4_slice_segment_mode;
266 
267     /* Set slice segment length */
268     if((ps_entropy_ctxt->i4_slice_segment_mode == 1) ||
269        (ps_entropy_ctxt->i4_slice_segment_mode == 2))
270     {
271         ps_entropy_ctxt->i4_slice_segment_max_length =
272             ps_init_prms->s_slice_params.i4_slice_segment_argument;
273     }
274     else
275     {
276         ps_entropy_ctxt->i4_slice_segment_max_length = 0;
277     }
278 
279     /* return the handle to caller */
280     return ((void *)ps_entropy_ctxt);
281 }
282 
283 /**
284 ******************************************************************************
285 *
286 *  @brief entry point for entropy coding of a frame
287 *
288 *  @par   Description
289 *  This function generates nal headers like SPS/PPS/slice header and call the
290 *  slice data entropy coding function
291 *
292 *  @param[in]   ps_enc_ctxt
293 *  pointer to encoder context (handle)
294 *
295 *  @param[out]   ps_curr_out
296 *  pointer to output data buffer context where bitstream is generated
297 *
298 *  @param[out]   ps_curr_inp
299 *  pointer to entropy input params context
300 *
301 *  @return      success or failure error code
302 *
303 ******************************************************************************
304 */
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)305 WORD32 ihevce_entropy_encode_frame(
306     void *pv_entropy_hdl,
307     iv_output_data_buffs_t *ps_curr_out,
308     frm_proc_ent_cod_ctxt_t *ps_curr_inp,
309     WORD32 i4_out_buf_size)
310 {
311     WORD32 ret = IHEVCE_SUCCESS;
312     WORD32 tile_ctr, total_tiles = 1;
313     entropy_context_t *ps_entropy_ctxt = (entropy_context_t *)pv_entropy_hdl;
314 
315     /* current frame slice type and nal type */
316     WORD32 slice_type = ps_curr_inp->s_slice_hdr.i1_slice_type;
317 
318     /* current frame slice type and nal type */
319     WORD32 nal_type = ps_curr_inp->i4_slice_nal_type;
320 
321     /* read vps, sps and pps from input params */
322     vps_t *ps_vps = ps_curr_inp->ps_vps;
323     sps_t *ps_sps = ps_curr_inp->ps_sps;
324     pps_t *ps_pps = ps_curr_inp->ps_pps;
325 #ifndef DISABLE_SEI
326     sei_params_t *ps_sei = &ps_curr_inp->s_sei;
327 #endif
328     ihevce_tile_params_t *ps_tile_params_base;
329     WORD32 out_buf_size = i4_out_buf_size;
330 
331     /* Headers are repeated once per IDR. Should be changed to every CRA */
332     WORD32 insert_vps_sps_pps =
333         ((slice_type == ISLICE) &&
334          (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
335 
336     WORD32 insert_per_cra =
337         ((slice_type == ISLICE) &&
338          (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
339     bitstrm_t *ps_bitstrm = &ps_entropy_ctxt->s_bit_strm;
340 
341     ULWORD64 u8_bits_slice_header_prev;
342 
343     WORD32 i4_slice_segment_max_length_bckp;
344     WORD32 i4_max_num_slices;
345 
346     ihevce_sys_api_t *ps_sys_api = (ihevce_sys_api_t *)ps_entropy_ctxt->pv_sys_api;
347 
348 #if POPULATE_NAL_OFFSET
349     ULWORD64 u8_bitstream_base = (ULWORD64)ps_curr_out->pv_bitstream_bufs;
350 #endif
351     if(0 == ps_entropy_ctxt->i4_sps_at_cdr_enable)
352     {
353         insert_vps_sps_pps =
354             ((slice_type == ISLICE) && ((NAL_IDR_N_LP == nal_type) || (NAL_IDR_W_LP == nal_type)));
355     }
356     /* intialize vps, sps, pps, sei and slice header in entropy context */
357     ps_entropy_ctxt->ps_vps = ps_vps;
358     ps_entropy_ctxt->ps_sps = ps_sps;
359     ps_entropy_ctxt->ps_pps = ps_pps;
360 #ifndef DISABLE_SEI
361     ps_entropy_ctxt->ps_sei = ps_sei;
362 #endif
363     ps_entropy_ctxt->ps_slice_hdr = &ps_curr_inp->s_slice_hdr;
364     ps_entropy_ctxt->i4_is_cu_cbf_zero = 1;
365 
366     ps_entropy_ctxt->ps_pic_level_info = &ps_curr_inp->s_pic_level_info;
367 
368     /* intialize the frame level ctb pointer for current slice */
369     ps_entropy_ctxt->ps_frm_ctb = ps_curr_inp->ps_frm_ctb_data;
370 
371     /* Initiallizing to indicate the start of frame */
372     ps_entropy_ctxt->i4_next_slice_seg_x = 0;
373     ps_entropy_ctxt->i4_next_slice_seg_y = 0;
374 
375     /* enable the residue encode flag */
376     ps_entropy_ctxt->i4_enable_res_encode = 1;
377 
378     /* Initialize the bitstream engine */
379     ret |= ihevce_bitstrm_init(ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, out_buf_size);
380 
381     /* Reset Bitstream NAL counter */
382     ps_bitstrm->i4_num_nal = 0;
383 
384     /*PIC INFO: Store the Bits before slice header is encoded*/
385     u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
386 
387     /* generate AUD if enabled from the application */
388     if(1 == ps_curr_inp->i1_aud_present_flag)
389     {
390         UWORD8 u1_pic_type;
391 
392         switch(slice_type)
393         {
394         case ISLICE:
395             u1_pic_type = 0;
396             break;
397         case PSLICE:
398             u1_pic_type = 1;
399             break;
400         default:
401             u1_pic_type = 2;
402             break;
403         }
404 
405         ret |= ihevce_generate_aud(ps_bitstrm, u1_pic_type);
406     }
407 
408     if(insert_vps_sps_pps)
409     {
410         /* generate vps */
411         ret |= ihevce_generate_vps(ps_bitstrm, ps_entropy_ctxt->ps_vps);
412 
413         /* generate sps */
414         ret |= ihevce_generate_sps(ps_bitstrm, ps_entropy_ctxt->ps_sps);
415 
416         /* generate pps */
417         ret |= ihevce_generate_pps(ps_bitstrm, ps_entropy_ctxt->ps_pps);
418     }
419 
420 #ifndef DISABLE_SEI
421     /* generate sei */
422     if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
423     {
424         WORD32 i4_insert_prefix_sei =
425             ps_entropy_ctxt->ps_sei->i1_buf_period_params_present_flag ||
426             ps_entropy_ctxt->ps_sei->i1_pic_timing_params_present_flag ||
427             ps_entropy_ctxt->ps_sei->i1_recovery_point_params_present_flag ||
428             ps_entropy_ctxt->ps_sei->i4_sei_mastering_disp_colour_vol_params_present_flags ||
429             ps_curr_inp->u4_num_sei_payload || ps_curr_inp->s_sei.i1_sei_cll_enable;
430 
431         if(i4_insert_prefix_sei)
432         {
433             ret |= ihevce_generate_sei(
434                 ps_bitstrm,
435                 ps_entropy_ctxt->ps_sei,
436                 &ps_entropy_ctxt->ps_sps->s_vui_parameters,
437                 insert_per_cra,
438                 NAL_PREFIX_SEI,
439                 ps_curr_inp->u4_num_sei_payload,
440                 &ps_curr_inp->as_sei_payload[0]);
441         }
442     }
443 #endif
444 
445     /*PIC INFO: Populate slice header bits */
446     ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
447         (ps_bitstrm->u4_strm_buf_offset * 8) - u8_bits_slice_header_prev;
448 
449     ps_tile_params_base = (ihevce_tile_params_t *)ps_entropy_ctxt->pv_tile_params_base;
450 
451     ps_curr_out->i4_bytes_generated = 0;  //Init
452 
453     /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/
454     {
455         WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal;
456         WORD32 ctr = 0;
457 
458         ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU);
459 
460         ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals;
461         for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++)
462         {
463             /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
464             ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr];
465 
466 #if POPULATE_NAL_SIZE
467 
468             /* ----------Populate NAL Size  -------------*/
469             if((ctr + 1) < num_non_vcl_prefix_nals)
470             {
471                 ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1];
472                 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
473                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
474             }
475             else
476             {
477                 ULWORD64 u8_next_nal_start =
478                     (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
479                 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
480                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
481             }
482             ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0);
483 
484 #elif POPULATE_NAL_OFFSET
485 
486             /* ----------Populate NAL Offset  -------------*/
487 
488             ASSERT(u8_cur_nal_start >= u8_bitstream_base);
489             ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] =
490                 (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
491 
492             if(ctr)
493             {
494                 /* sanity check on increasing NAL offsets */
495                 ASSERT(
496                     ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] >
497                     ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]);
498             }
499 #endif /* POPULATE_NAL_SIZE */
500         }
501     }
502 
503     total_tiles = ps_tile_params_base->i4_num_tiles;
504 
505     /* frame level NUM slices related params initialisations */
506     {
507         WORD32 codec_level_index = ihevce_get_level_index(ps_entropy_ctxt->i4_codec_level);
508 
509         i4_max_num_slices = g_as_level_data[codec_level_index].i4_max_slices_per_picture;
510         ps_entropy_ctxt->i4_num_slice_seg = 0;
511     }
512 
513     /* back up slice arg length before pic encoding */
514     i4_slice_segment_max_length_bckp = ps_entropy_ctxt->i4_slice_segment_max_length;
515 
516     for(tile_ctr = 0; tile_ctr < total_tiles; tile_ctr++)
517     {
518         WORD32 i4_end_of_slice = 0;
519 
520         /* Loop over all the slice segments */
521         while(0 == i4_end_of_slice)
522         {
523             WORD32 i4_bytes_generated, i4_slice_header_bits;
524 
525             /*PIC INFO: Store the Bits before slice header is encoded*/
526             u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
527 
528             /* generate slice header */
529             ret |= ihevce_generate_slice_header(
530                 ps_bitstrm,
531                 nal_type,
532                 ps_entropy_ctxt->ps_slice_hdr,
533                 ps_entropy_ctxt->ps_pps,
534                 ps_entropy_ctxt->ps_sps,
535                 &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
536                 &ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset,
537                 (ps_tile_params_base + tile_ctr),
538                 ps_entropy_ctxt->i4_next_slice_seg_x,
539                 ps_entropy_ctxt->i4_next_slice_seg_y);
540 
541             i4_slice_header_bits =
542                 (ps_bitstrm->u4_strm_buf_offset * 8) - (WORD32)u8_bits_slice_header_prev;
543 
544             /* Update slice segment length with bytes in slice header */
545             if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
546             {
547                 ps_entropy_ctxt->i4_slice_seg_len = (i4_slice_header_bits / 8);
548             }
549             else  //Initiallize to zero
550             {
551                 ps_entropy_ctxt->i4_slice_seg_len = 0;
552             }
553 
554             /*PIC INFO: Populate slice header bits */
555             ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
556                 i4_slice_header_bits;
557 
558             /* check if number of slices generated in is MAX -1 as per codec_level */
559             if(ps_entropy_ctxt->i4_num_slice_seg == (i4_max_num_slices - 1))
560             {
561                 /* i4_slice_segment_max_length is set to a huge positive value          */
562                 /* so that remaining CTBS in the picture gets encoded as a single slice */
563                 ps_entropy_ctxt->i4_slice_segment_max_length = 0x7FFFFFFF;
564             }
565 
566             /* encode the slice data */
567             ret |= ihevce_encode_slice_data(
568                 ps_entropy_ctxt, (ps_tile_params_base + tile_ctr), &i4_end_of_slice);
569 
570             /* increment the number of slices generated */
571             ps_entropy_ctxt->i4_num_slice_seg++;
572 
573             if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
574             {
575                 /*after encoding is done each slice offset is available. Enter these offset in slice header*/
576                 ihevce_insert_entry_offset_slice_header(
577                     &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
578                     ps_entropy_ctxt->ps_slice_hdr,
579                     ps_entropy_ctxt->ps_pps,
580                     ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset);
581             }
582 
583             /* compute the bytes generated and return */
584             if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
585             {
586                 i4_bytes_generated = ps_entropy_ctxt->s_dup_bit_strm_ent_offset.u4_strm_buf_offset;
587             }
588             else
589             {
590                 i4_bytes_generated = ps_entropy_ctxt->s_cabac_ctxt.u4_strm_buf_offset;
591             }
592 
593             /* Updating bytes generated and Updating strm_buffer pointer */
594             ps_curr_out->i4_bytes_generated += i4_bytes_generated;
595 
596             /* Re-Initialize the bitstream engine after each tile or slice */
597             ihevce_bitstrm_init(
598                 ps_bitstrm, (ps_bitstrm->pu1_strm_buffer + i4_bytes_generated), out_buf_size);
599         }
600     }
601 
602     /* Max slices related warning prints based on last slice status */
603     if(ps_entropy_ctxt->i4_num_slice_seg == i4_max_num_slices)
604     {
605         if(ps_entropy_ctxt->i4_slice_seg_len >= i4_slice_segment_max_length_bckp)
606         {
607             if(1 == ps_entropy_ctxt->i4_slice_segment_mode)
608             {
609                 ps_sys_api->ihevce_printf(
610                     ps_sys_api->pv_cb_handle,
611                     "IHEVCE_WARNING: Last slice contains %d CTBs exceeds %d (Max limit of CTBs "
612                     "configured). As per codec_level max number of slices per frame is %d\n",
613                     ps_entropy_ctxt->i4_slice_seg_len,
614                     i4_slice_segment_max_length_bckp,
615                     i4_max_num_slices);
616             }
617             else if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
618             {
619                 ps_sys_api->ihevce_printf(
620                     ps_sys_api->pv_cb_handle,
621                     "IHEVCE_WARNING: Last slice contains %d Bytes exceeds %d (Max limit of Bytes "
622                     "configured). As per codec_level max number of slices per frame is %d\n",
623                     ps_entropy_ctxt->i4_slice_seg_len,
624                     i4_slice_segment_max_length_bckp,
625                     i4_max_num_slices);
626             }
627         }
628     }
629 
630     /* restore slice arg length after pic encoding */
631     ps_entropy_ctxt->i4_slice_segment_max_length = i4_slice_segment_max_length_bckp;
632 
633     /* ---------------------- Initialize VCL NAL Size/offsets ---------------------------*/
634     {
635         WORD32 vcl_start = ps_curr_out->i4_num_non_vcl_prefix_nals;
636         WORD32 num_vcl_nals = ps_bitstrm->i4_num_nal - vcl_start;
637         WORD32 ctr = 0;
638 
639         ASSERT(num_vcl_nals > 0);
640         ASSERT(num_vcl_nals <= MAX_NUM_VCL_NALS_PER_AU);
641 
642         ps_curr_out->i4_num_vcl_nals = num_vcl_nals;
643         for(ctr = 0; ctr < MIN(num_vcl_nals, MAX_NUM_VCL_NALS_PER_AU); ctr++)
644         {
645             /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
646             ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start];
647 
648 #if POPULATE_NAL_SIZE
649 
650             /* ----------Populate NAL Size  -------------*/
651             if((ctr + 1) < num_vcl_nals)
652             {
653                 ULWORD64 u8_next_nal_start =
654                     (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start + 1];
655                 ps_curr_out->ai4_size_vcl_nals[ctr] =
656                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
657             }
658             else
659             {
660                 ULWORD64 u8_next_nal_start =
661                     (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
662                 ps_curr_out->ai4_size_vcl_nals[ctr] =
663                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
664             }
665             ASSERT(ps_curr_out->ai4_size_vcl_nals[ctr] > 0);
666 
667 #elif POPULATE_NAL_OFFSET
668 
669             /* ----------Populate NAL Offset  -------------*/
670 
671             ASSERT(u8_cur_nal_start >= u8_bitstream_base);
672             ps_curr_out->ai4_off_vcl_nals[ctr] = (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
673 
674             if(ctr)
675             {
676                 /* sanity check on increasing NAL offsets */
677                 ASSERT(ps_curr_out->ai4_off_vcl_nals[ctr] > ps_curr_out->ai4_off_vcl_nals[ctr - 1]);
678             }
679 #endif /* POPULATE_NAL_SIZE */
680         }
681     }
682 
683 #ifndef DISABLE_SEI
684     /* generate suffix sei */
685     if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
686     {
687         /* Insert hash SEI */
688         if(0 != ps_entropy_ctxt->ps_sei->i1_decoded_pic_hash_sei_flag)
689         {
690             ret |= ihevce_generate_sei(
691                 ps_bitstrm,
692                 ps_entropy_ctxt->ps_sei,
693                 &ps_entropy_ctxt->ps_sps->s_vui_parameters,
694                 insert_per_cra,
695                 NAL_SUFFIX_SEI,
696                 ps_curr_inp->u4_num_sei_payload,
697                 &ps_curr_inp->as_sei_payload[0]);
698         }
699 
700         /* Updating bytes generated */
701         ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
702     }
703 #endif
704 
705     /* generate end of sequence nal */
706     if((1 == ps_curr_inp->i1_eos_present_flag) && (ps_curr_inp->i4_is_end_of_idr_gop == 1))
707     {
708         ret |= ihevce_generate_eos(ps_bitstrm);
709         /* Updating bytes generated */
710         ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
711     }
712 
713     /* ------------------- Initialize non-VCL suffix NAL Size/offsets -----------------------*/
714     {
715         WORD32 non_vcl_suffix_start =
716             ps_curr_out->i4_num_non_vcl_prefix_nals + ps_curr_out->i4_num_vcl_nals;
717         WORD32 num_non_vcl_suffix_nals = ps_bitstrm->i4_num_nal - non_vcl_suffix_start;
718         WORD32 ctr = 0;
719 
720         ASSERT(num_non_vcl_suffix_nals >= 0);
721         ASSERT(num_non_vcl_suffix_nals <= MAX_NUM_SUFFIX_NALS_PER_AU);
722 
723         ps_curr_out->i4_num_non_vcl_suffix_nals = num_non_vcl_suffix_nals;
724         for(ctr = 0; ctr < MIN(num_non_vcl_suffix_nals, MAX_NUM_SUFFIX_NALS_PER_AU); ctr++)
725         {
726             /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
727             ULWORD64 u8_cur_nal_start =
728                 (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start];
729 
730 #if POPULATE_NAL_SIZE
731 
732             /* ----------Populate NAL Size  -------------*/
733             if((ctr + 1) < num_non_vcl_suffix_nals)
734             {
735                 ULWORD64 u8_next_nal_start =
736                     (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start + 1];
737                 ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
738                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
739             }
740             else
741             {
742                 ULWORD64 u8_next_nal_start =
743                     (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
744                 ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
745                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
746             }
747             ASSERT(ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] > 0);
748 
749 #elif POPULATE_NAL_OFFSET
750 
751             /* ----------Populate NAL Offset  -------------*/
752 
753             ASSERT(u8_cur_nal_start >= u8_bitstream_base);
754             ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] =
755                 (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
756 
757             if(ctr)
758             {
759                 /* sanity check on increasing NAL offsets */
760                 ASSERT(
761                     ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] >
762                     ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr - 1]);
763             }
764 #endif /* POPULATE_NAL_SIZE */
765         }
766     }
767 
768     /*PIC INFO: Populatinf Ref POC, weights and offset*/
769     {
770         WORD32 i;
771         ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l0_active =
772             ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active;
773         ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l1_active =
774             ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active;
775         for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
776         {
777             ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l0[i] =
778                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l0[i];
779             ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l0[i] =
780                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l0[i];
781             ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l0[i] =
782                 (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l0[i] /
783                 (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
784             ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l0[i] =
785                 ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l0[i];
786         }
787         for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
788         {
789             ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l1[i] =
790                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l1[i];
791             ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l1[i] =
792                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l1[i];
793             ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l1[i] =
794                 (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l1[i] /
795                 (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
796             ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l1[i] =
797                 ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l1[i];
798         }
799     }
800 
801     /* attach the time stamp of the input to output */
802     ps_curr_out->i4_out_timestamp_low = ps_curr_inp->i4_inp_timestamp_low;
803 
804     ps_curr_out->i4_out_timestamp_high = ps_curr_inp->i4_inp_timestamp_high;
805 
806     /*attach the app frame info of this buffer */
807     ps_curr_out->pv_app_frm_ctxt = ps_curr_inp->pv_app_frm_ctxt;
808 
809     /* frame never skipped for now */
810     ps_curr_out->i4_frame_skipped = 0;
811 
812     /* update error code and return */
813     ps_curr_out->i4_process_error_code = ret;
814 
815     switch(slice_type)
816     {
817     case ISLICE:
818         if((nal_type == NAL_IDR_N_LP) || (NAL_IDR_W_LP == nal_type))
819         {
820             ps_curr_out->i4_encoded_frame_type = IV_IDR_FRAME;
821         }
822         else
823         {
824             ps_curr_out->i4_encoded_frame_type = IV_I_FRAME;
825         }
826         break;
827     case PSLICE:
828         ps_curr_out->i4_encoded_frame_type = IV_P_FRAME;
829         break;
830     case BSLICE:
831         ps_curr_out->i4_encoded_frame_type = IV_B_FRAME;
832         break;
833     }
834 
835     if(IHEVCE_SUCCESS == ret)
836     {
837         ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
838     }
839     else
840     {
841         ps_curr_out->i4_process_ret_sts = IV_FAIL;
842     }
843 
844     return (ret);
845 }
846