• 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  ******************************************************************************
23  * \file ihevce_entropy_cod.c
24  *
25  * \brief
26  *    This file contains interface function definitions related to Entroy coding
27  *
28  * \date
29  *    18/09/2012
30  *
31  * \author
32  *    Ittiam
33  *
34  * List of Functions
35  *
36  *
37  ******************************************************************************
38  */
39 
40 /*****************************************************************************/
41 /* File Includes                                                             */
42 /*****************************************************************************/
43 /* System include files */
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <assert.h>
48 #include <stdarg.h>
49 #include <math.h>
50 
51 /* User include files */
52 #include "ihevc_typedefs.h"
53 #include "itt_video_api.h"
54 #include "ihevce_api.h"
55 
56 #include "rc_cntrl_param.h"
57 #include "rc_frame_info_collector.h"
58 #include "rc_look_ahead_params.h"
59 
60 #include "ihevc_defs.h"
61 #include "ihevc_macros.h"
62 #include "ihevc_debug.h"
63 #include "ihevc_structs.h"
64 #include "ihevc_platform_macros.h"
65 #include "ihevc_deblk.h"
66 #include "ihevc_itrans_recon.h"
67 #include "ihevc_chroma_itrans_recon.h"
68 #include "ihevc_chroma_intra_pred.h"
69 #include "ihevc_intra_pred.h"
70 #include "ihevc_inter_pred.h"
71 #include "ihevc_mem_fns.h"
72 #include "ihevc_padding.h"
73 #include "ihevc_weighted_pred.h"
74 #include "ihevc_sao.h"
75 #include "ihevc_resi_trans.h"
76 #include "ihevc_quant_iquant_ssd.h"
77 #include "ihevc_cabac_tables.h"
78 #include "ihevc_trans_tables.h"
79 #include "ihevc_trans_macros.h"
80 
81 #include "ihevce_defs.h"
82 #include "ihevce_hle_interface.h"
83 #include "ihevce_hle_q_func.h"
84 #include "ihevce_lap_enc_structs.h"
85 #include "ihevce_multi_thrd_structs.h"
86 #include "ihevce_multi_thrd_funcs.h"
87 #include "ihevce_me_common_defs.h"
88 #include "ihevce_had_satd.h"
89 #include "ihevce_error_codes.h"
90 #include "ihevce_error_checks.h"
91 #include "ihevce_bitstream.h"
92 #include "ihevce_cabac.h"
93 #include "ihevce_rdoq_macros.h"
94 #include "ihevce_function_selector.h"
95 #include "ihevce_enc_structs.h"
96 #include "ihevce_global_tables.h"
97 #include "ihevce_entropy_structs.h"
98 #include "ihevce_entropy_interface.h"
99 #include "ihevce_rc_enc_structs.h"
100 #include "ihevce_rc_interface.h"
101 #include "ihevce_encode_header.h"
102 #include "ihevce_encode_header_sei_vui.h"
103 #include "ihevce_trace.h"
104 
105 #include "cast_types.h"
106 #include "osal.h"
107 #include "osal_defaults.h"
108 
109 /*****************************************************************************/
110 /* Extern variables                                                          */
111 /*****************************************************************************/
112 UWORD8 gau1_pic_type_string[5][11] = {
113     { "I-SLICE  " }, { "P-SLICE  " }, { "B-SLICE  " }, { "IDR-SLICE" }, { "b-SLICE  " }
114 };
115 
116 /*****************************************************************************/
117 /* Function Macros                                                           */
118 /*****************************************************************************/
119 #define PSNR_FROM_MSE(x, bit_depth)                                                                \
120     ((x == 0) ? 99.999999 : (20 * log10(((1 << bit_depth) - 1) / sqrt(x))))
121 
122 /*****************************************************************************/
123 /* Function Definitions                                                      */
124 /*****************************************************************************/
125 
126 /*!
127  ******************************************************************************
128  * \if Function name : ihevce_ent_coding_thrd \endif
129  *
130  * \brief
131  *   Entropy coding thread interface function
132  *
133  * \param[in] Frame process pointer
134  *
135  * \return
136  *    None
137  *
138  * \author
139  *  Ittiam
140  *
141  *****************************************************************************
142  */
ihevce_ent_coding_thrd(void * pv_frm_proc_thrd_ctxt)143 WORD32 ihevce_ent_coding_thrd(void *pv_frm_proc_thrd_ctxt)
144 {
145     /* local variabels */
146     frm_proc_thrd_ctxt_t *ps_thrd_ctxt;
147     enc_ctxt_t *ps_enc_ctxt;
148     WORD32 i4_thrd_id;
149     ihevce_hle_ctxt_t *ps_hle_ctxt;
150     WORD32 end_flag;
151     WORD32 out_buf_id;
152     WORD32 inp_buf_id;
153     WORD32 entropy_error = 0;
154     WORD32 i4_bitrate_instance_num, i4_resolution_id, i4_out_res_id;
155     WORD32 i4_bufque_id;
156     UWORD32 u4_encode_frm_num = 0;
157     UWORD32 u4_au_cpb_removal_delay_minus1 = 0;
158     WORD32 i4_no_output = 0;
159     WORD32 i4_do_entr_last = 1;
160     WORD32 i4_use_dummy_buffer = 0;
161     void *pv_entropy_hdl;
162     entropy_context_t *ps_entropy_ctxt;
163 
164     iv_output_data_buffs_t *ps_curr_out = NULL;
165     frm_proc_ent_cod_ctxt_t *ps_curr_inp = NULL;
166     iv_output_data_buffs_t s_curr_out_dummy;
167 
168     /* derive local variables */
169     ps_thrd_ctxt = (frm_proc_thrd_ctxt_t *)pv_frm_proc_thrd_ctxt;
170     i4_thrd_id = ps_thrd_ctxt->i4_thrd_id;
171     ps_hle_ctxt = (ihevce_hle_ctxt_t *)ps_thrd_ctxt->ps_hle_ctxt;
172     ps_enc_ctxt = (enc_ctxt_t *)ps_thrd_ctxt->pv_enc_ctxt;
173     end_flag = 0;
174     i4_bitrate_instance_num = i4_thrd_id;
175     i4_bufque_id = i4_thrd_id;
176     i4_resolution_id = ps_enc_ctxt->i4_resolution_id;
177     i4_out_res_id = i4_resolution_id;
178 
179     /*swaping of buf_id for 0th and reference bitrate location, as encoder
180     assumes always 0th loc for reference bitrate and app must receive in
181     the configured order*/
182     i4_bufque_id = i4_bitrate_instance_num;
183     if(i4_bitrate_instance_num == 0)
184     {
185         i4_bufque_id = ps_enc_ctxt->i4_ref_mbr_id;
186     }
187     else if(i4_bitrate_instance_num == ps_enc_ctxt->i4_ref_mbr_id)
188     {
189         i4_bufque_id = 0;
190     }
191 
192     if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
193     {
194         i4_bufque_id = 0;
195         i4_out_res_id = 0;
196     }
197     pv_entropy_hdl = ps_enc_ctxt->s_module_ctxt.apv_ent_cod_ctxt[i4_bitrate_instance_num];
198     ps_entropy_ctxt = (entropy_context_t *)pv_entropy_hdl;
199     /* ---------- Processing Loop until end command is recieved --------- */
200     while(0 == end_flag)
201     {
202         /*Get a buffer pointer*/
203         /* ------- get next input buffer from Frame buffer que ---------- */
204         ps_curr_inp = (frm_proc_ent_cod_ctxt_t *)ihevce_q_get_filled_buff(
205             (void *)ps_enc_ctxt,
206             (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num),
207             &inp_buf_id,
208             BUFF_QUE_BLOCKING_MODE);
209         if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
210         {
211             if(1 == ps_curr_inp->i4_out_flush_flag)
212             {
213                 if(1 == ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id])
214                     ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 0;
215                 else
216                     ASSERT(0);
217             }
218             else
219             {
220                 if(0 == ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id])
221                 {
222                     /* During change in resolution check whether prev res is active before starting to dump new resolution */
223                     WORD32 other_res_active = 1;
224                     WORD32 ctr;
225                     volatile WORD32 *pi4_active_res_check;
226                     pi4_active_res_check = ps_enc_ctxt->s_multi_thrd.pi4_active_res_id;
227                     while(other_res_active)
228                     {
229                         /* Continue in polling mode untill all the other resolutions are in passive mode */
230                         other_res_active = 0;
231                         for(ctr = 0;
232                             ctr < ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_num_res_layers;
233                             ctr++)
234                         {
235                             if(ctr != i4_resolution_id)
236                             {
237                                 /* Check whether any resolution other than current resolution is active */
238                                 /* If its active it means that previous resolution has not finished entropy */
239                                 /* Wait for it to finish entropy*/
240                                 other_res_active |= pi4_active_res_check[ctr];
241                             }
242                         }
243                         if(1 == ps_curr_inp->i4_end_flag)
244                         {
245                             i4_no_output = 1;
246                         }
247                     }
248 
249                     if(0 == ps_curr_inp->i4_end_flag)
250                     {
251                         ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 1;
252                     }
253                 }
254             }
255         }
256         if(0 == ps_curr_inp->i4_out_flush_flag)
257         {
258             if(1 == i4_no_output)
259             {
260                 ps_curr_out = NULL;
261             }
262             else
263             {
264                 if(!i4_use_dummy_buffer)
265                 {
266                     /* ------- get a filled descriptor from output Que ------------ */
267                     ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
268                         (void *)ps_enc_ctxt,
269                         (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id),
270                         &out_buf_id,
271                         BUFF_QUE_BLOCKING_MODE);
272                 }
273                 else
274                 {
275                     ps_curr_out = &s_curr_out_dummy;
276                     out_buf_id = 0;
277                     ps_curr_out->i4_bitstream_buf_size = ps_entropy_ctxt->i4_bitstream_buf_size;
278                     ps_curr_out->pv_bitstream_bufs = ps_entropy_ctxt->pv_dummy_out_buf;
279                 }
280             }
281         }
282 
283         PROFILE_START(
284             &ps_hle_ctxt->profile_entropy[ps_enc_ctxt->i4_resolution_id][i4_bitrate_instance_num]);
285         /* Content Light Level Information */
286         {
287             ps_curr_inp->s_sei.i1_sei_cll_enable =
288                 (WORD8)ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.i4_sei_cll_enable;
289             ps_curr_inp->s_sei.s_cll_info_sei_params.u2_sei_max_cll =
290                 ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.u2_sei_max_cll;
291             ps_curr_inp->s_sei.s_cll_info_sei_params.u2_sei_avg_cll =
292                 ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.u2_sei_avg_cll;
293         }
294         if((NULL != ps_curr_out) && (NULL != ps_curr_inp))
295 
296         {
297             WORD32 i;
298 
299             /*PIC_INFO: reset the pic-level info flags*/
300             ps_curr_inp->s_pic_level_info.i8_total_cu = 0;
301             ps_curr_inp->s_pic_level_info.i8_total_intra_cu = 0;
302             ps_curr_inp->s_pic_level_info.i8_total_inter_cu = 0;
303             ps_curr_inp->s_pic_level_info.i8_total_skip_cu = 0;
304             ps_curr_inp->s_pic_level_info.i8_total_pu = 0;
305             ps_curr_inp->s_pic_level_info.i8_total_intra_pu = 0;
306             ps_curr_inp->s_pic_level_info.i8_total_non_skipped_inter_pu = 0;
307             ps_curr_inp->s_pic_level_info.i8_total_merge_pu = 0;
308             for(i = 0; i < 4; i++)
309             {
310                 ps_curr_inp->s_pic_level_info.i8_total_cu_based_on_size[i] = 0;
311                 ps_curr_inp->s_pic_level_info.i8_total_2nx2n_intra_pu[i] = 0;
312                 ps_curr_inp->s_pic_level_info.i8_total_2nx2n_inter_pu[i] = 0;
313                 ps_curr_inp->s_pic_level_info.i8_total_tu_based_on_size[i] = 0;
314                 ps_curr_inp->s_pic_level_info.i8_total_smp_inter_pu[i] = 0;
315                 if(i != 3)
316                 {
317                     ps_curr_inp->s_pic_level_info.i8_total_amp_inter_pu[i] = 0;
318                     ps_curr_inp->s_pic_level_info.i8_total_nxn_inter_pu[i] = 0;
319                 }
320             }
321 
322             ps_curr_inp->s_pic_level_info.i8_total_nxn_intra_pu = 0;
323             ps_curr_inp->s_pic_level_info.i8_total_L0_mode = 0;
324             ps_curr_inp->s_pic_level_info.i8_total_L1_mode = 0;
325             ps_curr_inp->s_pic_level_info.i8_total_BI_mode = 0;
326             //ps_curr_inp->s_pic_level_info.u4_frame_intra_sad = ps_enc_ctxt->u4
327             for(i = 0; i < MAX_DPB_SIZE; i++)
328             {
329                 ps_curr_inp->s_pic_level_info.i8_total_L0_ref_idx[i] = 0;
330                 ps_curr_inp->s_pic_level_info.i8_total_L1_ref_idx[i] = 0;
331             }
332 
333             ps_curr_inp->s_pic_level_info.i8_total_tu = 0;
334             ps_curr_inp->s_pic_level_info.i8_total_non_coded_tu = 0;
335             ps_curr_inp->s_pic_level_info.i8_total_intra_coded_tu = 0;
336             ps_curr_inp->s_pic_level_info.i8_total_inter_coded_tu = 0;
337 
338             ps_curr_inp->s_pic_level_info.i8_total_qp = 0;
339             ps_curr_inp->s_pic_level_info.i8_total_qp_min_cu = 0;
340             ps_curr_inp->s_pic_level_info.i4_min_qp = 100;
341             ps_curr_inp->s_pic_level_info.i4_max_qp = 0;
342             ps_curr_inp->s_pic_level_info.i4_max_frame_qp = 0;
343 
344             ps_curr_inp->s_pic_level_info.i8_sum_squared_frame_qp = 0;
345             ps_curr_inp->s_pic_level_info.i8_total_frame_qp = 0;
346             ps_curr_inp->s_pic_level_info.f_total_buffer_underflow = 0;
347             ps_curr_inp->s_pic_level_info.f_total_buffer_overflow = 0;
348             ps_curr_inp->s_pic_level_info.f_max_buffer_underflow = 0;
349             ps_curr_inp->s_pic_level_info.f_max_buffer_overflow = 0;
350 
351             ps_curr_inp->s_pic_level_info.u8_bits_estimated_intra = 0;
352             ps_curr_inp->s_pic_level_info.u8_bits_estimated_inter = 0;
353             ps_curr_inp->s_pic_level_info.u8_bits_estimated_slice_header = 0;
354             ps_curr_inp->s_pic_level_info.u8_bits_estimated_sao = 0;
355             ps_curr_inp->s_pic_level_info.u8_bits_estimated_split_cu_flag = 0;
356             ps_curr_inp->s_pic_level_info.u8_bits_estimated_cu_hdr_bits = 0;
357             ps_curr_inp->s_pic_level_info.u8_bits_estimated_split_tu_flag = 0;
358             ps_curr_inp->s_pic_level_info.u8_bits_estimated_qp_delta_bits = 0;
359             ps_curr_inp->s_pic_level_info.u8_bits_estimated_cbf_luma_bits = 0;
360             ps_curr_inp->s_pic_level_info.u8_bits_estimated_cbf_chroma_bits = 0;
361 
362             ps_curr_inp->s_pic_level_info.u8_bits_estimated_res_luma_bits = 0;
363             ps_curr_inp->s_pic_level_info.u8_bits_estimated_res_chroma_bits = 0;
364 
365             ps_curr_inp->s_pic_level_info.u8_bits_estimated_ref_id = 0;
366             ps_curr_inp->s_pic_level_info.u8_bits_estimated_mvd = 0;
367             ps_curr_inp->s_pic_level_info.u8_bits_estimated_merge_flag = 0;
368             ps_curr_inp->s_pic_level_info.u8_bits_estimated_mpm_luma = 0;
369             ps_curr_inp->s_pic_level_info.u8_bits_estimated_mpm_chroma = 0;
370 
371             if(1 == ps_curr_inp->i4_frm_proc_valid_flag)
372             {
373                 /* --- Init of buffering period and pic timing SEI related params ----*/
374                 {
375                     UWORD32 i4_dbf, i4_buffersize, i4_trgt_bit_rate;
376                     if(ps_enc_ctxt->ps_stat_prms->s_config_prms.i4_rate_control_mode != 3)
377                     {
378                         ihevce_get_dbf_buffer_size(
379                             (void *)ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
380                             &i4_buffersize,
381                             &i4_dbf,
382                             &i4_trgt_bit_rate);
383                     }
384                     else
385                     {
386                         /* Default initializations in CQP mode */
387                         WORD32 codec_level =
388                             ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.as_tgt_params[0]
389                                 .i4_codec_level;
390                         WORD32 codec_level_index = ihevce_get_level_index(codec_level);
391 
392                         i4_buffersize =
393                             (UWORD32)g_as_level_data[codec_level_index].i4_max_cpb[HIGH_TIER];
394                         i4_trgt_bit_rate =
395                             (UWORD32)g_as_level_data[codec_level_index].i4_max_bit_rate[HIGH_TIER];
396                         i4_dbf = i4_buffersize;
397                     }
398 
399                     ps_curr_inp->s_sei.s_buf_period_sei_params.u4_buffer_size_sei = i4_buffersize;
400                     ps_curr_inp->s_sei.s_buf_period_sei_params.u4_dbf_sei = i4_dbf;
401                     ps_curr_inp->s_sei.s_buf_period_sei_params.u4_target_bit_rate_sei =
402                         i4_trgt_bit_rate;
403 
404                     /* ----------------- Derivation of u4_au_cpb_removal_delay_minus1  --------------------------------*/
405                     ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1 =
406                         u4_au_cpb_removal_delay_minus1;
407 
408                     /* ----------------- Derivation of u4_pic_dpb_output_delay  --------------------------------*/
409                     ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_pic_dpb_output_delay =
410                         ps_curr_inp->ps_sps->ai1_sps_max_num_reorder_pics[0] +
411                         ps_curr_inp->i4_display_num - u4_encode_frm_num;
412                 }
413                 /* call the core entropy coding entry point function */
414                 entropy_error = ihevce_entropy_encode_frame(
415                     pv_entropy_hdl, ps_curr_out, ps_curr_inp, ps_curr_out->i4_bitstream_buf_size);
416 
417                 /* ----------------- Derivation of u4_au_cpb_removal_delay_minus1  --------------------------------*/
418                 if(ps_curr_inp->s_sei.i1_buf_period_params_present_flag)
419                 {
420                     /* Reset u4_au_cpb_removal_delay_minus1 after every buffering period as subsequent pictiming is w.r.t new buffering period SEI */
421                     u4_au_cpb_removal_delay_minus1 = 0;
422                 }
423                 else
424                 {
425                     /* cpb delay is circularly incremented with wrap around based on max length signalled in VUI */
426                     UWORD8 u1_au_cpb_removal_delay_length =
427                         ps_curr_inp->ps_sps->s_vui_parameters.s_vui_hrd_parameters
428                             .u1_au_cpb_removal_delay_length_minus1 +
429                         1;
430 
431                     UWORD32 u4_max_cpb_removal_delay_val =
432                         (1 << u1_au_cpb_removal_delay_length) - 1;
433 
434                     u4_au_cpb_removal_delay_minus1 = (u4_au_cpb_removal_delay_minus1 + 1) &
435                                                      u4_max_cpb_removal_delay_val;
436                 }
437                 /* Debug prints for entropy error */
438                 if(entropy_error)
439                 {
440                     DBG_PRINTF("Entropy encode error %x\n", entropy_error);
441                     DEBUG("Entropy encode error %d\n", entropy_error);
442                 }
443                 if(ps_enc_ctxt->ps_stat_prms->s_config_prms.i4_rate_control_mode != 3)
444                 {
445                     /* acquire mutex lock for rate control calls */
446                     osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
447 
448                     /* get frame rate/bit rate/max buffer size */
449                     ihevce_vbv_compliance_frame_level_update(
450                         ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
451                         (ps_curr_out->i4_bytes_generated << 3),
452                         i4_resolution_id,
453                         i4_bitrate_instance_num,
454                         ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1);
455                     /* release mutex lock after rate control calls */
456                     osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
457                 }
458 
459                 /*correct the mismatch between rdopt and entropy thread mismatch*/
460                 {
461                     /* acquire mutex lock for rate control calls */
462                     osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
463 
464                     /*Set to -1 when no change in bitrate, other-wise set to encoder bufferfullness at that moment*/
465                     ps_curr_out->i8_cur_vbv_level = ps_curr_inp->i8_buf_level_bitrate_change;
466                     if(ps_curr_inp->i8_buf_level_bitrate_change != -1)
467                     {
468                         LWORD64 bitrate, buffer_size;
469                         ASSERT(
470                             i4_bitrate_instance_num ==
471                             0);  //since dynamic change in bitrate is not supported in multi bitrate and resolution
472                         get_avg_bitrate_bufsize(
473                             ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
474                             &bitrate,
475                             &buffer_size);
476 
477                         change_bitrate_vbv_complaince(
478                             ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
479                             bitrate,
480                             buffer_size);
481                         /*Change bitrate in SEI-VUI related context as well*/
482                         ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms
483                             .as_tgt_params[ps_enc_ctxt->i4_resolution_id]
484                             .ai4_tgt_bitrate[i4_bitrate_instance_num] = (WORD32)bitrate;
485                         ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms
486                             .as_tgt_params[ps_enc_ctxt->i4_resolution_id]
487                             .ai4_max_vbv_buffer_size[i4_bitrate_instance_num] = (WORD32)buffer_size;
488                     }
489                     /*account for error to meet bitrate more precisely*/
490                     ihevce_rc_rdopt_entropy_bit_correct(
491                         ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
492                         (ps_curr_out->i4_bytes_generated << 3),
493                         inp_buf_id);  //ps_curr_inp->i4_inp_timestamp_low
494 
495                     /* release mutex lock after rate control calls */
496                     osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
497                 }
498                 u4_encode_frm_num++;
499             }
500             else
501             {
502                 ps_curr_out->i4_bytes_generated = 0;
503                 ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME;
504             }
505 
506             ps_curr_out->i4_buf_id = out_buf_id;
507             end_flag = ps_curr_inp->i4_end_flag;
508             ps_curr_out->i4_end_flag = ps_curr_inp->i4_end_flag;
509 
510             if(1 == ps_enc_ctxt->s_multi_thrd.i4_force_end_flag)
511             {
512                 end_flag = 1;
513                 ps_curr_out->i4_end_flag = 1;
514             }
515             if(!i4_use_dummy_buffer)
516             {
517                 /* Call back to Apln. saying buffer is produced */
518                 ps_hle_ctxt->ihevce_output_strm_fill_done(
519                     ps_hle_ctxt->pv_out_cb_handle,
520                     ps_curr_out,
521                     i4_bufque_id, /* br intance */
522                     i4_out_res_id /* res_instance*/);
523             }
524 
525             if(ps_curr_inp->i4_frm_proc_valid_flag)
526             {
527                 ps_curr_inp->s_pic_level_info.u8_total_bits_generated =
528                     ps_curr_out->i4_bytes_generated * 8;
529             }
530 
531             /* --- release the current output buffer ---- */
532             if(!i4_use_dummy_buffer)
533             {
534                 ihevce_q_rel_buf(
535                     (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id);
536             }
537 
538             /* release the input buffer*/
539             ihevce_q_rel_buf(
540                 (void *)ps_enc_ctxt,
541                 (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num),
542                 inp_buf_id);
543 
544             /* reset the pointers to NULL */
545             ps_curr_inp = NULL;
546             ps_curr_out = NULL;
547         }
548         else
549         {
550             end_flag = ps_curr_inp->i4_end_flag;
551             if(NULL != ps_curr_inp)
552             {
553                 /* release the input buffer*/
554                 ihevce_q_rel_buf(
555                     (void *)ps_enc_ctxt,
556                     (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num),
557                     inp_buf_id);
558             }
559 
560             // ASSERT(0);
561         }
562         PROFILE_STOP(
563             &ps_hle_ctxt->profile_entropy[ps_enc_ctxt->i4_resolution_id][i4_bitrate_instance_num],
564             NULL);
565     }
566 
567     /* Release all the buffers the application might have queued in */
568     /* Do this only if its not a force end */
569     if(1 != ps_enc_ctxt->s_multi_thrd.i4_force_end_flag)
570     {
571         end_flag = 0;
572     }
573     if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
574     {
575         ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control = 1;
576         i4_do_entr_last = ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id];
577     }
578     else
579     {
580         i4_do_entr_last = 1;
581     }
582 
583     if((1 == i4_do_entr_last) && (!i4_use_dummy_buffer))
584     {
585         while(0 == end_flag)
586         {
587             if(1 == ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control)  //FFMPEG application
588             {
589                 /* ------- get a filled descriptor from output Que ------------ */
590                 ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
591                     (void *)ps_enc_ctxt,
592                     (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id),
593                     &out_buf_id,
594                     BUFF_QUE_NON_BLOCKING_MODE);
595 
596                 /* Update the end_flag from application */
597                 end_flag = (ps_curr_out == NULL);
598             }
599             else if(
600                 0 == ps_enc_ctxt->ps_stat_prms
601                          ->i4_outbuf_buf_free_control)  // process call control based application
602             {
603                 ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
604                     (void *)ps_enc_ctxt,
605                     (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id),
606                     &out_buf_id,
607                     BUFF_QUE_BLOCKING_MODE);
608             }
609             else
610             {
611                 /* should not enter here */
612                 ASSERT(0);
613             }
614 
615             if(ps_curr_out)
616             {
617                 end_flag = ps_curr_out->i4_is_last_buf;
618 
619                 /* Fill the min. necessory things */
620                 ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
621                 ps_curr_out->i4_end_flag = 1;
622                 ps_curr_out->i4_bytes_generated = 0;
623 
624                 /* Call back to Apln. saying buffer is produced */
625                 ps_hle_ctxt->ihevce_output_strm_fill_done(
626                     ps_hle_ctxt->pv_out_cb_handle,
627                     ps_curr_out,
628                     i4_bufque_id, /* br intance */
629                     i4_out_res_id /* res_instance*/);
630 
631                 /* --- release the current output buffer ---- */
632                 ihevce_q_rel_buf(
633                     (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id);
634             }
635         }
636     }
637     if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
638     {
639         /* Mres single out usecase: Set active res_id to zero for curretn res so that other resolutions exit entropy */
640         ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 0;
641     }
642 
643     return (0);
644 }
645 
646 /**
647 ******************************************************************************
648 *
649 *  @brief Generate sps, pps and vps header for
650 *
651 *  @par   Description
652 *  This function generates nal headers like SPS/PPS/VPS
653 *
654 *  @param[in]   ps_hle_ctxt
655 *  pointer to high level interface context (handle)
656 *
657 *  @param[in]   i4_bitrate_instance_id
658 *  bitrate id
659 *
660 *  @param[in]   i4_resolution_id
661 *  resolution id
662 *
663 *  @return      success or failure error code
664 *
665 ******************************************************************************
666 */
ihevce_entropy_encode_header(ihevce_hle_ctxt_t * ps_hle_ctxt,WORD32 i4_bitrate_instance_id,WORD32 i4_resolution_id)667 WORD32 ihevce_entropy_encode_header(
668     ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 i4_bitrate_instance_id, WORD32 i4_resolution_id)
669 {
670     WORD32 ret = IHEVCE_SUCCESS;
671     bitstrm_t s_bit_strm;
672     bitstrm_t *ps_bitstrm = &s_bit_strm;
673     enc_ctxt_t *ps_enc_ctxt;
674     ihevce_tgt_layer_params_t *ps_tgt_lyr_prms;
675     sps_t *ps_sps;
676     vps_t *ps_vps;
677     pps_t *ps_pps;
678     iv_output_data_buffs_t *ps_curr_out = NULL;
679     WORD32 out_buf_id;
680 
681     /* sanity checks */
682     if((ps_hle_ctxt == NULL) || (ps_hle_ctxt->i4_size != sizeof(ihevce_hle_ctxt_t)) ||
683        ps_hle_ctxt->i4_hle_init_done != 1)
684         return IHEVCE_FAIL;
685 
686     ps_tgt_lyr_prms = (ihevce_tgt_layer_params_t *)&ps_hle_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms;
687 
688     if(i4_resolution_id >= ps_tgt_lyr_prms->i4_num_res_layers)
689         return IHEVCE_FAIL;
690 
691     if(i4_bitrate_instance_id >=
692        ps_tgt_lyr_prms->as_tgt_params[i4_resolution_id].i4_num_bitrate_instances)
693         return IHEVCE_FAIL;
694 
695     ps_enc_ctxt = (enc_ctxt_t *)ps_hle_ctxt->apv_enc_hdl[i4_resolution_id];
696     ps_sps = &ps_enc_ctxt->as_sps[i4_bitrate_instance_id];
697     ps_vps = &ps_enc_ctxt->as_vps[i4_bitrate_instance_id];
698     ps_pps = &ps_enc_ctxt->as_pps[i4_bitrate_instance_id];
699 
700     /* ------- get a filled descriptor from output Que ------------ */
701     ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
702         (void *)ps_enc_ctxt,
703         (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id),
704         &out_buf_id,
705         BUFF_QUE_BLOCKING_MODE);
706 
707     /* Initialize the bitstream engine */
708     ret |= ihevce_bitstrm_init(
709         ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, ps_curr_out->i4_bitstream_buf_size);
710 
711     /* Reset Bitstream NAL counter */
712     ps_bitstrm->i4_num_nal = 0;
713 
714     /* generate vps */
715     ret |= ihevce_generate_vps(ps_bitstrm, ps_vps);
716 
717     /* generate sps */
718     ret |= ihevce_generate_sps(ps_bitstrm, ps_sps);
719 
720     /* generate pps */
721     ret |= ihevce_generate_pps(ps_bitstrm, ps_pps);
722 
723     /* attach the time stamp of the input to output */
724     ps_curr_out->i4_out_timestamp_low = 0;
725     ps_curr_out->i4_out_timestamp_high = 0;
726 
727     /*attach the app frame info of this buffer */
728     ps_curr_out->pv_app_frm_ctxt = NULL;
729 
730     /* frame never skipped for now */
731     ps_curr_out->i4_frame_skipped = 0;
732 
733     /* update error code and return */
734     ps_curr_out->i4_process_error_code = ret;
735 
736     ps_curr_out->i4_bytes_generated = ps_bitstrm->u4_strm_buf_offset;
737 
738     /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/
739     {
740         WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal;
741         WORD32 ctr = 0;
742 
743         ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU);
744 
745         ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals;
746         for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++)
747         {
748             /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
749             ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr];
750 
751 #if POPULATE_NAL_SIZE
752 
753             /* ----------Populate NAL Size  -------------*/
754             if((ctr + 1) < num_non_vcl_prefix_nals)
755             {
756                 ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1];
757                 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
758                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
759             }
760             else
761             {
762                 ULWORD64 u8_next_nal_start =
763                     (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
764                 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
765                     (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
766             }
767             ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0);
768 
769 #elif POPULATE_NAL_OFFSET
770 
771             /* ----------Populate NAL Offset  -------------*/
772 
773             ASSERT(u8_cur_nal_start >= u8_bitstream_base);
774             ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] =
775                 (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
776 
777             if(ctr)
778             {
779                 /* sanity check on increasing NAL offsets */
780                 ASSERT(
781                     ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] >
782                     ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]);
783             }
784 #endif /* POPULATE_NAL_SIZE */
785         }
786     }
787 
788     ps_curr_out->i4_buf_id = out_buf_id;
789     ps_curr_out->i4_end_flag = 0;
790     if(IHEVCE_SUCCESS == ret)
791     {
792         ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
793     }
794     else
795     {
796         ps_curr_out->i4_process_ret_sts = IV_FAIL;
797     }
798     ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME;
799 
800     /* Call back to Apln. saying buffer is produced */
801     ps_hle_ctxt->ihevce_output_strm_fill_done(
802         ps_hle_ctxt->pv_out_cb_handle, ps_curr_out, i4_bitrate_instance_id, i4_resolution_id);
803 
804     /* release the input buffer*/
805     ihevce_q_rel_buf(
806         (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id), out_buf_id);
807 
808     return ret;
809 }
810