• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     sei_params_t *ps_sei = &ps_curr_inp->s_sei;
326     ihevce_tile_params_t *ps_tile_params_base;
327     WORD32 out_buf_size = i4_out_buf_size;
328 
329     /* Headers are repeated once per IDR. Should be changed to every CRA */
330     WORD32 insert_vps_sps_pps =
331         ((slice_type == ISLICE) &&
332          (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
333 
334     WORD32 insert_per_cra =
335         ((slice_type == ISLICE) &&
336          (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
337     bitstrm_t *ps_bitstrm = &ps_entropy_ctxt->s_bit_strm;
338 
339     ULWORD64 u8_bits_slice_header_prev;
340 
341     WORD32 i4_slice_segment_max_length_bckp;
342     WORD32 i4_max_num_slices;
343 
344     ihevce_sys_api_t *ps_sys_api = (ihevce_sys_api_t *)ps_entropy_ctxt->pv_sys_api;
345 
346 #if POPULATE_NAL_OFFSET
347     ULWORD64 u8_bitstream_base = (ULWORD64)ps_curr_out->pv_bitstream_bufs;
348 #endif
349     if(0 == ps_entropy_ctxt->i4_sps_at_cdr_enable)
350     {
351         insert_vps_sps_pps =
352             ((slice_type == ISLICE) && ((NAL_IDR_N_LP == nal_type) || (NAL_IDR_W_LP == nal_type)));
353     }
354     /* intialize vps, sps, pps, sei and slice header in entropy context */
355     ps_entropy_ctxt->ps_vps = ps_vps;
356     ps_entropy_ctxt->ps_sps = ps_sps;
357     ps_entropy_ctxt->ps_pps = ps_pps;
358     ps_entropy_ctxt->ps_sei = ps_sei;
359     ps_entropy_ctxt->ps_slice_hdr = &ps_curr_inp->s_slice_hdr;
360     ps_entropy_ctxt->i4_is_cu_cbf_zero = 1;
361 
362     ps_entropy_ctxt->ps_pic_level_info = &ps_curr_inp->s_pic_level_info;
363 
364     /* intialize the frame level ctb pointer for current slice */
365     ps_entropy_ctxt->ps_frm_ctb = ps_curr_inp->ps_frm_ctb_data;
366 
367     /* Initiallizing to indicate the start of frame */
368     ps_entropy_ctxt->i4_next_slice_seg_x = 0;
369     ps_entropy_ctxt->i4_next_slice_seg_y = 0;
370 
371     /* enable the residue encode flag */
372     ps_entropy_ctxt->i4_enable_res_encode = 1;
373 
374     /* Initialize the bitstream engine */
375     ret |= ihevce_bitstrm_init(ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, out_buf_size);
376 
377     /* Reset Bitstream NAL counter */
378     ps_bitstrm->i4_num_nal = 0;
379 
380     /*PIC INFO: Store the Bits before slice header is encoded*/
381     u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
382 
383     /* generate AUD if enabled from the application */
384     if(1 == ps_curr_inp->i1_aud_present_flag)
385     {
386         UWORD8 u1_pic_type;
387 
388         switch(slice_type)
389         {
390         case ISLICE:
391             u1_pic_type = 0;
392             break;
393         case PSLICE:
394             u1_pic_type = 1;
395             break;
396         default:
397             u1_pic_type = 2;
398             break;
399         }
400 
401         ret |= ihevce_generate_aud(ps_bitstrm, u1_pic_type);
402     }
403 
404     if(insert_vps_sps_pps)
405     {
406         /* generate vps */
407         ret |= ihevce_generate_vps(ps_bitstrm, ps_entropy_ctxt->ps_vps);
408 
409         /* generate sps */
410         ret |= ihevce_generate_sps(ps_bitstrm, ps_entropy_ctxt->ps_sps);
411 
412         /* generate pps */
413         ret |= ihevce_generate_pps(ps_bitstrm, ps_entropy_ctxt->ps_pps);
414     }
415 
416     /* generate sei */
417     if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
418     {
419         WORD32 i4_insert_prefix_sei =
420             ps_entropy_ctxt->ps_sei->i1_buf_period_params_present_flag ||
421             ps_entropy_ctxt->ps_sei->i1_pic_timing_params_present_flag ||
422             ps_entropy_ctxt->ps_sei->i1_recovery_point_params_present_flag ||
423             ps_entropy_ctxt->ps_sei->i4_sei_mastering_disp_colour_vol_params_present_flags ||
424             ps_curr_inp->u4_num_sei_payload || ps_curr_inp->s_sei.i1_sei_cll_enable;
425 
426         if(i4_insert_prefix_sei)
427         {
428             ret |= ihevce_generate_sei(
429                 ps_bitstrm,
430                 ps_entropy_ctxt->ps_sei,
431                 &ps_entropy_ctxt->ps_sps->s_vui_parameters,
432                 insert_per_cra,
433                 NAL_PREFIX_SEI,
434                 ps_curr_inp->u4_num_sei_payload,
435                 &ps_curr_inp->as_sei_payload[0]);
436         }
437     }
438 
439     /*PIC INFO: Populate slice header bits */
440     ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
441         (ps_bitstrm->u4_strm_buf_offset * 8) - u8_bits_slice_header_prev;
442 
443     ps_tile_params_base = (ihevce_tile_params_t *)ps_entropy_ctxt->pv_tile_params_base;
444 
445     ps_curr_out->i4_bytes_generated = 0;  //Init
446 
447     /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/
448     {
449         WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal;
450         WORD32 ctr = 0;
451 
452         ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU);
453 
454         ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals;
455         for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++)
456         {
457             /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
458             ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr];
459 
460 #if POPULATE_NAL_SIZE
461 
462             /* ----------Populate NAL Size  -------------*/
463             if((ctr + 1) < num_non_vcl_prefix_nals)
464             {
465                 ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1];
466                 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
467                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
468             }
469             else
470             {
471                 ULWORD64 u8_next_nal_start =
472                     (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
473                 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
474                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
475             }
476             ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0);
477 
478 #elif POPULATE_NAL_OFFSET
479 
480             /* ----------Populate NAL Offset  -------------*/
481 
482             ASSERT(u8_cur_nal_start >= u8_bitstream_base);
483             ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] =
484                 (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
485 
486             if(ctr)
487             {
488                 /* sanity check on increasing NAL offsets */
489                 ASSERT(
490                     ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] >
491                     ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]);
492             }
493 #endif /* POPULATE_NAL_SIZE */
494         }
495     }
496 
497     total_tiles = ps_tile_params_base->i4_num_tiles;
498 
499     /* frame level NUM slices related params initialisations */
500     {
501         WORD32 codec_level_index = ihevce_get_level_index(ps_entropy_ctxt->i4_codec_level);
502 
503         i4_max_num_slices = g_as_level_data[codec_level_index].i4_max_slices_per_picture;
504         ps_entropy_ctxt->i4_num_slice_seg = 0;
505     }
506 
507     /* back up slice arg length before pic encoding */
508     i4_slice_segment_max_length_bckp = ps_entropy_ctxt->i4_slice_segment_max_length;
509 
510     for(tile_ctr = 0; tile_ctr < total_tiles; tile_ctr++)
511     {
512         WORD32 i4_end_of_slice = 0;
513 
514         /* Loop over all the slice segments */
515         while(0 == i4_end_of_slice)
516         {
517             WORD32 i4_bytes_generated, i4_slice_header_bits;
518 
519             /*PIC INFO: Store the Bits before slice header is encoded*/
520             u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
521 
522             /* generate slice header */
523             ret |= ihevce_generate_slice_header(
524                 ps_bitstrm,
525                 nal_type,
526                 ps_entropy_ctxt->ps_slice_hdr,
527                 ps_entropy_ctxt->ps_pps,
528                 ps_entropy_ctxt->ps_sps,
529                 &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
530                 &ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset,
531                 (ps_tile_params_base + tile_ctr),
532                 ps_entropy_ctxt->i4_next_slice_seg_x,
533                 ps_entropy_ctxt->i4_next_slice_seg_y);
534 
535             i4_slice_header_bits =
536                 (ps_bitstrm->u4_strm_buf_offset * 8) - (WORD32)u8_bits_slice_header_prev;
537 
538             /* Update slice segment length with bytes in slice header */
539             if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
540             {
541                 ps_entropy_ctxt->i4_slice_seg_len = (i4_slice_header_bits / 8);
542             }
543             else  //Initiallize to zero
544             {
545                 ps_entropy_ctxt->i4_slice_seg_len = 0;
546             }
547 
548             /*PIC INFO: Populate slice header bits */
549             ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
550                 i4_slice_header_bits;
551 
552             /* check if number of slices generated in is MAX -1 as per codec_level */
553             if(ps_entropy_ctxt->i4_num_slice_seg == (i4_max_num_slices - 1))
554             {
555                 /* i4_slice_segment_max_length is set to a huge positive value          */
556                 /* so that remaining CTBS in the picture gets encoded as a single slice */
557                 ps_entropy_ctxt->i4_slice_segment_max_length = 0x7FFFFFFF;
558             }
559 
560             /* encode the slice data */
561             ret |= ihevce_encode_slice_data(
562                 ps_entropy_ctxt, (ps_tile_params_base + tile_ctr), &i4_end_of_slice);
563 
564             /* increment the number of slices generated */
565             ps_entropy_ctxt->i4_num_slice_seg++;
566 
567             if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
568             {
569                 /*after encoding is done each slice offset is available. Enter these offset in slice header*/
570                 ihevce_insert_entry_offset_slice_header(
571                     &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
572                     ps_entropy_ctxt->ps_slice_hdr,
573                     ps_entropy_ctxt->ps_pps,
574                     ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset);
575             }
576 
577             /* compute the bytes generated and return */
578             if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
579             {
580                 i4_bytes_generated = ps_entropy_ctxt->s_dup_bit_strm_ent_offset.u4_strm_buf_offset;
581             }
582             else
583             {
584                 i4_bytes_generated = ps_entropy_ctxt->s_cabac_ctxt.u4_strm_buf_offset;
585             }
586 
587             /* Updating bytes generated and Updating strm_buffer pointer */
588             ps_curr_out->i4_bytes_generated += i4_bytes_generated;
589 
590             /* Re-Initialize the bitstream engine after each tile or slice */
591             ihevce_bitstrm_init(
592                 ps_bitstrm, (ps_bitstrm->pu1_strm_buffer + i4_bytes_generated), out_buf_size);
593         }
594     }
595 
596     /* Max slices related warning prints based on last slice status */
597     if(ps_entropy_ctxt->i4_num_slice_seg == i4_max_num_slices)
598     {
599         if(ps_entropy_ctxt->i4_slice_seg_len >= i4_slice_segment_max_length_bckp)
600         {
601             if(1 == ps_entropy_ctxt->i4_slice_segment_mode)
602             {
603                 ps_sys_api->ihevce_printf(
604                     ps_sys_api->pv_cb_handle,
605                     "IHEVCE_WARNING: Last slice contains %d CTBs exceeds %d (Max limit of CTBs "
606                     "configured). As per codec_level max number of slices per frame is %d\n",
607                     ps_entropy_ctxt->i4_slice_seg_len,
608                     i4_slice_segment_max_length_bckp,
609                     i4_max_num_slices);
610             }
611             else if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
612             {
613                 ps_sys_api->ihevce_printf(
614                     ps_sys_api->pv_cb_handle,
615                     "IHEVCE_WARNING: Last slice contains %d Bytes exceeds %d (Max limit of Bytes "
616                     "configured). As per codec_level max number of slices per frame is %d\n",
617                     ps_entropy_ctxt->i4_slice_seg_len,
618                     i4_slice_segment_max_length_bckp,
619                     i4_max_num_slices);
620             }
621         }
622     }
623 
624     /* restore slice arg length after pic encoding */
625     ps_entropy_ctxt->i4_slice_segment_max_length = i4_slice_segment_max_length_bckp;
626 
627     /* ---------------------- Initialize VCL NAL Size/offsets ---------------------------*/
628     {
629         WORD32 vcl_start = ps_curr_out->i4_num_non_vcl_prefix_nals;
630         WORD32 num_vcl_nals = ps_bitstrm->i4_num_nal - vcl_start;
631         WORD32 ctr = 0;
632 
633         ASSERT(num_vcl_nals > 0);
634         ASSERT(num_vcl_nals <= MAX_NUM_VCL_NALS_PER_AU);
635 
636         ps_curr_out->i4_num_vcl_nals = num_vcl_nals;
637         for(ctr = 0; ctr < MIN(num_vcl_nals, MAX_NUM_VCL_NALS_PER_AU); ctr++)
638         {
639             /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
640             ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start];
641 
642 #if POPULATE_NAL_SIZE
643 
644             /* ----------Populate NAL Size  -------------*/
645             if((ctr + 1) < num_vcl_nals)
646             {
647                 ULWORD64 u8_next_nal_start =
648                     (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start + 1];
649                 ps_curr_out->ai4_size_vcl_nals[ctr] =
650                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
651             }
652             else
653             {
654                 ULWORD64 u8_next_nal_start =
655                     (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
656                 ps_curr_out->ai4_size_vcl_nals[ctr] =
657                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
658             }
659             ASSERT(ps_curr_out->ai4_size_vcl_nals[ctr] > 0);
660 
661 #elif POPULATE_NAL_OFFSET
662 
663             /* ----------Populate NAL Offset  -------------*/
664 
665             ASSERT(u8_cur_nal_start >= u8_bitstream_base);
666             ps_curr_out->ai4_off_vcl_nals[ctr] = (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
667 
668             if(ctr)
669             {
670                 /* sanity check on increasing NAL offsets */
671                 ASSERT(ps_curr_out->ai4_off_vcl_nals[ctr] > ps_curr_out->ai4_off_vcl_nals[ctr - 1]);
672             }
673 #endif /* POPULATE_NAL_SIZE */
674         }
675     }
676 
677     /* generate suffix sei */
678     if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
679     {
680         /* Insert hash SEI */
681         if(0 != ps_entropy_ctxt->ps_sei->i1_decoded_pic_hash_sei_flag)
682         {
683             ret |= ihevce_generate_sei(
684                 ps_bitstrm,
685                 ps_entropy_ctxt->ps_sei,
686                 &ps_entropy_ctxt->ps_sps->s_vui_parameters,
687                 insert_per_cra,
688                 NAL_SUFFIX_SEI,
689                 ps_curr_inp->u4_num_sei_payload,
690                 &ps_curr_inp->as_sei_payload[0]);
691         }
692 
693         /* Updating bytes generated */
694         ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
695     }
696 
697     /* generate end of sequence nal */
698     if((1 == ps_curr_inp->i1_eos_present_flag) && (ps_curr_inp->i4_is_end_of_idr_gop == 1))
699     {
700         ret |= ihevce_generate_eos(ps_bitstrm);
701         /* Updating bytes generated */
702         ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
703     }
704 
705     /* ------------------- Initialize non-VCL suffix NAL Size/offsets -----------------------*/
706     {
707         WORD32 non_vcl_suffix_start =
708             ps_curr_out->i4_num_non_vcl_prefix_nals + ps_curr_out->i4_num_vcl_nals;
709         WORD32 num_non_vcl_suffix_nals = ps_bitstrm->i4_num_nal - non_vcl_suffix_start;
710         WORD32 ctr = 0;
711 
712         ASSERT(num_non_vcl_suffix_nals >= 0);
713         ASSERT(num_non_vcl_suffix_nals <= MAX_NUM_SUFFIX_NALS_PER_AU);
714 
715         ps_curr_out->i4_num_non_vcl_suffix_nals = num_non_vcl_suffix_nals;
716         for(ctr = 0; ctr < MIN(num_non_vcl_suffix_nals, MAX_NUM_SUFFIX_NALS_PER_AU); ctr++)
717         {
718             /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
719             ULWORD64 u8_cur_nal_start =
720                 (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start];
721 
722 #if POPULATE_NAL_SIZE
723 
724             /* ----------Populate NAL Size  -------------*/
725             if((ctr + 1) < num_non_vcl_suffix_nals)
726             {
727                 ULWORD64 u8_next_nal_start =
728                     (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start + 1];
729                 ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
730                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
731             }
732             else
733             {
734                 ULWORD64 u8_next_nal_start =
735                     (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
736                 ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
737                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
738             }
739             ASSERT(ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] > 0);
740 
741 #elif POPULATE_NAL_OFFSET
742 
743             /* ----------Populate NAL Offset  -------------*/
744 
745             ASSERT(u8_cur_nal_start >= u8_bitstream_base);
746             ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] =
747                 (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
748 
749             if(ctr)
750             {
751                 /* sanity check on increasing NAL offsets */
752                 ASSERT(
753                     ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] >
754                     ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr - 1]);
755             }
756 #endif /* POPULATE_NAL_SIZE */
757         }
758     }
759 
760     /*PIC INFO: Populatinf Ref POC, weights and offset*/
761     {
762         WORD32 i;
763         ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l0_active =
764             ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active;
765         ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l1_active =
766             ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active;
767         for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
768         {
769             ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l0[i] =
770                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l0[i];
771             ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l0[i] =
772                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l0[i];
773             ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l0[i] =
774                 (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l0[i] /
775                 (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
776             ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l0[i] =
777                 ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l0[i];
778         }
779         for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
780         {
781             ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l1[i] =
782                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l1[i];
783             ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l1[i] =
784                 ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l1[i];
785             ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l1[i] =
786                 (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l1[i] /
787                 (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
788             ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l1[i] =
789                 ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l1[i];
790         }
791     }
792 
793     /* attach the time stamp of the input to output */
794     ps_curr_out->i4_out_timestamp_low = ps_curr_inp->i4_inp_timestamp_low;
795 
796     ps_curr_out->i4_out_timestamp_high = ps_curr_inp->i4_inp_timestamp_high;
797 
798     /*attach the app frame info of this buffer */
799     ps_curr_out->pv_app_frm_ctxt = ps_curr_inp->pv_app_frm_ctxt;
800 
801     /* frame never skipped for now */
802     ps_curr_out->i4_frame_skipped = 0;
803 
804     /* update error code and return */
805     ps_curr_out->i4_process_error_code = ret;
806 
807     switch(slice_type)
808     {
809     case ISLICE:
810         if((nal_type == NAL_IDR_N_LP) || (NAL_IDR_W_LP == nal_type))
811         {
812             ps_curr_out->i4_encoded_frame_type = IV_IDR_FRAME;
813         }
814         else
815         {
816             ps_curr_out->i4_encoded_frame_type = IV_I_FRAME;
817         }
818         break;
819     case PSLICE:
820         ps_curr_out->i4_encoded_frame_type = IV_P_FRAME;
821         break;
822     case BSLICE:
823         ps_curr_out->i4_encoded_frame_type = IV_B_FRAME;
824         break;
825     }
826 
827     if(IHEVCE_SUCCESS == ret)
828     {
829         ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
830     }
831     else
832     {
833         ps_curr_out->i4_process_ret_sts = IV_FAIL;
834     }
835 
836     return (ret);
837 }
838