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 #ifndef DISABLE_SEI
286 /* Content Light Level Information */
287 {
288 ps_curr_inp->s_sei.i1_sei_cll_enable =
289 (WORD8)ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.i4_sei_cll_enable;
290 ps_curr_inp->s_sei.s_cll_info_sei_params.u2_sei_max_cll =
291 ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.u2_sei_max_cll;
292 ps_curr_inp->s_sei.s_cll_info_sei_params.u2_sei_avg_cll =
293 ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.u2_sei_avg_cll;
294 }
295 #endif
296 if((NULL != ps_curr_out) && (NULL != ps_curr_inp))
297
298 {
299 WORD32 i;
300
301 /*PIC_INFO: reset the pic-level info flags*/
302 ps_curr_inp->s_pic_level_info.i8_total_cu = 0;
303 ps_curr_inp->s_pic_level_info.i8_total_intra_cu = 0;
304 ps_curr_inp->s_pic_level_info.i8_total_inter_cu = 0;
305 ps_curr_inp->s_pic_level_info.i8_total_skip_cu = 0;
306 ps_curr_inp->s_pic_level_info.i8_total_pu = 0;
307 ps_curr_inp->s_pic_level_info.i8_total_intra_pu = 0;
308 ps_curr_inp->s_pic_level_info.i8_total_non_skipped_inter_pu = 0;
309 ps_curr_inp->s_pic_level_info.i8_total_merge_pu = 0;
310 for(i = 0; i < 4; i++)
311 {
312 ps_curr_inp->s_pic_level_info.i8_total_cu_based_on_size[i] = 0;
313 ps_curr_inp->s_pic_level_info.i8_total_2nx2n_intra_pu[i] = 0;
314 ps_curr_inp->s_pic_level_info.i8_total_2nx2n_inter_pu[i] = 0;
315 ps_curr_inp->s_pic_level_info.i8_total_tu_based_on_size[i] = 0;
316 ps_curr_inp->s_pic_level_info.i8_total_smp_inter_pu[i] = 0;
317 if(i != 3)
318 {
319 ps_curr_inp->s_pic_level_info.i8_total_amp_inter_pu[i] = 0;
320 ps_curr_inp->s_pic_level_info.i8_total_nxn_inter_pu[i] = 0;
321 }
322 }
323
324 ps_curr_inp->s_pic_level_info.i8_total_nxn_intra_pu = 0;
325 ps_curr_inp->s_pic_level_info.i8_total_L0_mode = 0;
326 ps_curr_inp->s_pic_level_info.i8_total_L1_mode = 0;
327 ps_curr_inp->s_pic_level_info.i8_total_BI_mode = 0;
328 //ps_curr_inp->s_pic_level_info.u4_frame_intra_sad = ps_enc_ctxt->u4
329 for(i = 0; i < MAX_DPB_SIZE; i++)
330 {
331 ps_curr_inp->s_pic_level_info.i8_total_L0_ref_idx[i] = 0;
332 ps_curr_inp->s_pic_level_info.i8_total_L1_ref_idx[i] = 0;
333 }
334
335 ps_curr_inp->s_pic_level_info.i8_total_tu = 0;
336 ps_curr_inp->s_pic_level_info.i8_total_non_coded_tu = 0;
337 ps_curr_inp->s_pic_level_info.i8_total_intra_coded_tu = 0;
338 ps_curr_inp->s_pic_level_info.i8_total_inter_coded_tu = 0;
339
340 ps_curr_inp->s_pic_level_info.i8_total_qp = 0;
341 ps_curr_inp->s_pic_level_info.i8_total_qp_min_cu = 0;
342 ps_curr_inp->s_pic_level_info.i4_min_qp = 100;
343 ps_curr_inp->s_pic_level_info.i4_max_qp = 0;
344 ps_curr_inp->s_pic_level_info.i4_max_frame_qp = 0;
345
346 ps_curr_inp->s_pic_level_info.i8_sum_squared_frame_qp = 0;
347 ps_curr_inp->s_pic_level_info.i8_total_frame_qp = 0;
348 ps_curr_inp->s_pic_level_info.f_total_buffer_underflow = 0;
349 ps_curr_inp->s_pic_level_info.f_total_buffer_overflow = 0;
350 ps_curr_inp->s_pic_level_info.f_max_buffer_underflow = 0;
351 ps_curr_inp->s_pic_level_info.f_max_buffer_overflow = 0;
352
353 ps_curr_inp->s_pic_level_info.u8_bits_estimated_intra = 0;
354 ps_curr_inp->s_pic_level_info.u8_bits_estimated_inter = 0;
355 ps_curr_inp->s_pic_level_info.u8_bits_estimated_slice_header = 0;
356 ps_curr_inp->s_pic_level_info.u8_bits_estimated_sao = 0;
357 ps_curr_inp->s_pic_level_info.u8_bits_estimated_split_cu_flag = 0;
358 ps_curr_inp->s_pic_level_info.u8_bits_estimated_cu_hdr_bits = 0;
359 ps_curr_inp->s_pic_level_info.u8_bits_estimated_split_tu_flag = 0;
360 ps_curr_inp->s_pic_level_info.u8_bits_estimated_qp_delta_bits = 0;
361 ps_curr_inp->s_pic_level_info.u8_bits_estimated_cbf_luma_bits = 0;
362 ps_curr_inp->s_pic_level_info.u8_bits_estimated_cbf_chroma_bits = 0;
363
364 ps_curr_inp->s_pic_level_info.u8_bits_estimated_res_luma_bits = 0;
365 ps_curr_inp->s_pic_level_info.u8_bits_estimated_res_chroma_bits = 0;
366
367 ps_curr_inp->s_pic_level_info.u8_bits_estimated_ref_id = 0;
368 ps_curr_inp->s_pic_level_info.u8_bits_estimated_mvd = 0;
369 ps_curr_inp->s_pic_level_info.u8_bits_estimated_merge_flag = 0;
370 ps_curr_inp->s_pic_level_info.u8_bits_estimated_mpm_luma = 0;
371 ps_curr_inp->s_pic_level_info.u8_bits_estimated_mpm_chroma = 0;
372
373 if(1 == ps_curr_inp->i4_frm_proc_valid_flag)
374 {
375 #ifndef DISABLE_SEI
376 /* --- Init of buffering period and pic timing SEI related params ----*/
377 {
378 UWORD32 i4_dbf, i4_buffersize, i4_trgt_bit_rate;
379 if(ps_enc_ctxt->ps_stat_prms->s_config_prms.i4_rate_control_mode != 3)
380 {
381 ihevce_get_dbf_buffer_size(
382 (void *)ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
383 &i4_buffersize,
384 &i4_dbf,
385 &i4_trgt_bit_rate);
386 }
387 else
388 {
389 /* Default initializations in CQP mode */
390 WORD32 codec_level =
391 ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.as_tgt_params[0]
392 .i4_codec_level;
393 WORD32 codec_level_index = ihevce_get_level_index(codec_level);
394 WORD32 codec_tier =
395 ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.i4_codec_tier;
396
397 i4_buffersize =
398 (UWORD32)g_as_level_data[codec_level_index].i4_max_cpb[codec_tier] *
399 CBP_VCL_FACTOR;
400 i4_trgt_bit_rate =
401 (UWORD32)g_as_level_data[codec_level_index].i4_max_bit_rate[codec_tier] *
402 CBP_VCL_FACTOR;
403 i4_dbf = i4_buffersize;
404 }
405
406 ps_curr_inp->s_sei.s_buf_period_sei_params.u4_buffer_size_sei = i4_buffersize;
407 ps_curr_inp->s_sei.s_buf_period_sei_params.u4_dbf_sei = i4_dbf;
408 ps_curr_inp->s_sei.s_buf_period_sei_params.u4_target_bit_rate_sei =
409 i4_trgt_bit_rate;
410
411 /* ----------------- Derivation of u4_au_cpb_removal_delay_minus1 --------------------------------*/
412 ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1 =
413 u4_au_cpb_removal_delay_minus1;
414
415 /* ----------------- Derivation of u4_pic_dpb_output_delay --------------------------------*/
416 ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_pic_dpb_output_delay =
417 ps_curr_inp->ps_sps->ai1_sps_max_num_reorder_pics[0] +
418 ps_curr_inp->i4_display_num - u4_encode_frm_num;
419 }
420 #endif
421 /* call the core entropy coding entry point function */
422 entropy_error = ihevce_entropy_encode_frame(
423 pv_entropy_hdl, ps_curr_out, ps_curr_inp, ps_curr_out->i4_bitstream_buf_size);
424
425 #ifndef DISABLE_SEI
426 /* ----------------- Derivation of u4_au_cpb_removal_delay_minus1 --------------------------------*/
427 if(ps_curr_inp->s_sei.i1_buf_period_params_present_flag)
428 {
429 /* Reset u4_au_cpb_removal_delay_minus1 after every buffering period as subsequent pictiming is w.r.t new buffering period SEI */
430 u4_au_cpb_removal_delay_minus1 = 0;
431 }
432 else
433 {
434 /* cpb delay is circularly incremented with wrap around based on max length signalled in VUI */
435 UWORD8 u1_au_cpb_removal_delay_length =
436 ps_curr_inp->ps_sps->s_vui_parameters.s_vui_hrd_parameters
437 .u1_au_cpb_removal_delay_length_minus1 +
438 1;
439
440 UWORD32 u4_max_cpb_removal_delay_val =
441 (1 << u1_au_cpb_removal_delay_length) - 1;
442
443 u4_au_cpb_removal_delay_minus1 = (u4_au_cpb_removal_delay_minus1 + 1) &
444 u4_max_cpb_removal_delay_val;
445 }
446 #endif
447 /* Debug prints for entropy error */
448 if(entropy_error)
449 {
450 DBG_PRINTF("Entropy encode error %x\n", entropy_error);
451 DEBUG("Entropy encode error %d\n", entropy_error);
452 }
453 if(ps_enc_ctxt->ps_stat_prms->s_config_prms.i4_rate_control_mode != 3)
454 {
455 /* acquire mutex lock for rate control calls */
456 osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
457
458 UWORD32 removal_delay_minus1;
459 #ifndef DISABLE_SEI
460 removal_delay_minus1 =
461 ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1;
462 #else
463 removal_delay_minus1 = 0;
464 #endif
465 /* get frame rate/bit rate/max buffer size */
466 ihevce_vbv_compliance_frame_level_update(
467 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
468 (ps_curr_out->i4_bytes_generated << 3),
469 i4_resolution_id,
470 i4_bitrate_instance_num,
471 removal_delay_minus1);
472 /* release mutex lock after rate control calls */
473 osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
474 }
475
476 /*correct the mismatch between rdopt and entropy thread mismatch*/
477 {
478 /* acquire mutex lock for rate control calls */
479 osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
480
481 /*Set to -1 when no change in bitrate, other-wise set to encoder bufferfullness at that moment*/
482 ps_curr_out->i8_cur_vbv_level = ps_curr_inp->i8_buf_level_bitrate_change;
483 if(ps_curr_inp->i8_buf_level_bitrate_change != -1)
484 {
485 LWORD64 bitrate, buffer_size;
486 ASSERT(
487 i4_bitrate_instance_num ==
488 0); //since dynamic change in bitrate is not supported in multi bitrate and resolution
489 get_avg_bitrate_bufsize(
490 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
491 &bitrate,
492 &buffer_size);
493
494 change_bitrate_vbv_complaince(
495 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
496 bitrate,
497 buffer_size);
498 /*Change bitrate in SEI-VUI related context as well*/
499 ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms
500 .as_tgt_params[ps_enc_ctxt->i4_resolution_id]
501 .ai4_tgt_bitrate[i4_bitrate_instance_num] = (WORD32)bitrate;
502 ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms
503 .as_tgt_params[ps_enc_ctxt->i4_resolution_id]
504 .ai4_max_vbv_buffer_size[i4_bitrate_instance_num] = (WORD32)buffer_size;
505 }
506 /*account for error to meet bitrate more precisely*/
507 ihevce_rc_rdopt_entropy_bit_correct(
508 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
509 (ps_curr_out->i4_bytes_generated << 3),
510 inp_buf_id); //ps_curr_inp->i4_inp_timestamp_low
511
512 /* release mutex lock after rate control calls */
513 osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
514 }
515 u4_encode_frm_num++;
516 }
517 else
518 {
519 ps_curr_out->i4_bytes_generated = 0;
520 ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME;
521 }
522
523 ps_curr_out->i4_buf_id = out_buf_id;
524 end_flag = ps_curr_inp->i4_end_flag;
525 ps_curr_out->i4_end_flag = ps_curr_inp->i4_end_flag;
526
527 if(1 == ps_enc_ctxt->s_multi_thrd.i4_force_end_flag)
528 {
529 end_flag = 1;
530 ps_curr_out->i4_end_flag = 1;
531 }
532 if(!i4_use_dummy_buffer)
533 {
534 /* Call back to Apln. saying buffer is produced */
535 ps_hle_ctxt->ihevce_output_strm_fill_done(
536 ps_hle_ctxt->pv_out_cb_handle,
537 ps_curr_out,
538 i4_bufque_id, /* br intance */
539 i4_out_res_id /* res_instance*/);
540 }
541
542 if(ps_curr_inp->i4_frm_proc_valid_flag)
543 {
544 ps_curr_inp->s_pic_level_info.u8_total_bits_generated =
545 ps_curr_out->i4_bytes_generated * 8;
546 }
547
548 /* --- release the current output buffer ---- */
549 if(!i4_use_dummy_buffer)
550 {
551 ihevce_q_rel_buf(
552 (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id);
553 }
554
555 /* release the input buffer*/
556 ihevce_q_rel_buf(
557 (void *)ps_enc_ctxt,
558 (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num),
559 inp_buf_id);
560
561 /* reset the pointers to NULL */
562 ps_curr_inp = NULL;
563 ps_curr_out = NULL;
564 }
565 else
566 {
567 end_flag = ps_curr_inp->i4_end_flag;
568 if(NULL != ps_curr_inp)
569 {
570 /* release the input buffer*/
571 ihevce_q_rel_buf(
572 (void *)ps_enc_ctxt,
573 (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num),
574 inp_buf_id);
575 }
576
577 // ASSERT(0);
578 }
579 PROFILE_STOP(
580 &ps_hle_ctxt->profile_entropy[ps_enc_ctxt->i4_resolution_id][i4_bitrate_instance_num],
581 NULL);
582 }
583
584 /* Release all the buffers the application might have queued in */
585 /* Do this only if its not a force end */
586 if(1 != ps_enc_ctxt->s_multi_thrd.i4_force_end_flag)
587 {
588 end_flag = 0;
589 }
590 if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
591 {
592 ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control = 1;
593 i4_do_entr_last = ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id];
594 }
595 else
596 {
597 i4_do_entr_last = 1;
598 }
599
600 if((1 == i4_do_entr_last) && (!i4_use_dummy_buffer))
601 {
602 while(0 == end_flag)
603 {
604 if(1 == ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control) //FFMPEG application
605 {
606 /* ------- get a filled descriptor from output Que ------------ */
607 ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
608 (void *)ps_enc_ctxt,
609 (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id),
610 &out_buf_id,
611 BUFF_QUE_NON_BLOCKING_MODE);
612
613 /* Update the end_flag from application */
614 end_flag = (ps_curr_out == NULL);
615 }
616 else if(
617 0 == ps_enc_ctxt->ps_stat_prms
618 ->i4_outbuf_buf_free_control) // process call control based application
619 {
620 ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
621 (void *)ps_enc_ctxt,
622 (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id),
623 &out_buf_id,
624 BUFF_QUE_BLOCKING_MODE);
625 }
626 else
627 {
628 /* should not enter here */
629 ASSERT(0);
630 }
631
632 if(ps_curr_out)
633 {
634 end_flag = ps_curr_out->i4_is_last_buf;
635
636 /* Fill the min. necessory things */
637 ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
638 ps_curr_out->i4_end_flag = 1;
639 ps_curr_out->i4_bytes_generated = 0;
640
641 /* Call back to Apln. saying buffer is produced */
642 ps_hle_ctxt->ihevce_output_strm_fill_done(
643 ps_hle_ctxt->pv_out_cb_handle,
644 ps_curr_out,
645 i4_bufque_id, /* br intance */
646 i4_out_res_id /* res_instance*/);
647
648 /* --- release the current output buffer ---- */
649 ihevce_q_rel_buf(
650 (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id);
651 }
652 }
653 }
654 if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
655 {
656 /* Mres single out usecase: Set active res_id to zero for curretn res so that other resolutions exit entropy */
657 ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 0;
658 }
659
660 return (0);
661 }
662
663 /**
664 ******************************************************************************
665 *
666 * @brief Generate sps, pps and vps header for
667 *
668 * @par Description
669 * This function generates nal headers like SPS/PPS/VPS
670 *
671 * @param[in] ps_hle_ctxt
672 * pointer to high level interface context (handle)
673 *
674 * @param[in] i4_bitrate_instance_id
675 * bitrate id
676 *
677 * @param[in] i4_resolution_id
678 * resolution id
679 *
680 * @return success or failure error code
681 *
682 ******************************************************************************
683 */
ihevce_entropy_encode_header(ihevce_hle_ctxt_t * ps_hle_ctxt,WORD32 i4_bitrate_instance_id,WORD32 i4_resolution_id)684 WORD32 ihevce_entropy_encode_header(
685 ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 i4_bitrate_instance_id, WORD32 i4_resolution_id)
686 {
687 WORD32 ret = IHEVCE_SUCCESS;
688 bitstrm_t s_bit_strm;
689 bitstrm_t *ps_bitstrm = &s_bit_strm;
690 enc_ctxt_t *ps_enc_ctxt;
691 ihevce_tgt_layer_params_t *ps_tgt_lyr_prms;
692 sps_t *ps_sps;
693 vps_t *ps_vps;
694 pps_t *ps_pps;
695 iv_output_data_buffs_t *ps_curr_out = NULL;
696 WORD32 out_buf_id;
697
698 /* sanity checks */
699 if((ps_hle_ctxt == NULL) || (ps_hle_ctxt->i4_size != sizeof(ihevce_hle_ctxt_t)) ||
700 ps_hle_ctxt->i4_hle_init_done != 1)
701 return IHEVCE_FAIL;
702
703 ps_tgt_lyr_prms = (ihevce_tgt_layer_params_t *)&ps_hle_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms;
704
705 if(i4_resolution_id >= ps_tgt_lyr_prms->i4_num_res_layers)
706 return IHEVCE_FAIL;
707
708 if(i4_bitrate_instance_id >=
709 ps_tgt_lyr_prms->as_tgt_params[i4_resolution_id].i4_num_bitrate_instances)
710 return IHEVCE_FAIL;
711
712 ps_enc_ctxt = (enc_ctxt_t *)ps_hle_ctxt->apv_enc_hdl[i4_resolution_id];
713 ps_sps = &ps_enc_ctxt->as_sps[i4_bitrate_instance_id];
714 ps_vps = &ps_enc_ctxt->as_vps[i4_bitrate_instance_id];
715 ps_pps = &ps_enc_ctxt->as_pps[i4_bitrate_instance_id];
716
717 /* ------- get a filled descriptor from output Que ------------ */
718 ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
719 (void *)ps_enc_ctxt,
720 (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id),
721 &out_buf_id,
722 BUFF_QUE_BLOCKING_MODE);
723
724 /* Initialize the bitstream engine */
725 ret |= ihevce_bitstrm_init(
726 ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, ps_curr_out->i4_bitstream_buf_size);
727
728 /* Reset Bitstream NAL counter */
729 ps_bitstrm->i4_num_nal = 0;
730
731 /* generate vps */
732 ret |= ihevce_generate_vps(ps_bitstrm, ps_vps);
733
734 /* generate sps */
735 ret |= ihevce_generate_sps(ps_bitstrm, ps_sps);
736
737 /* generate pps */
738 ret |= ihevce_generate_pps(ps_bitstrm, ps_pps);
739
740 /* attach the time stamp of the input to output */
741 ps_curr_out->i4_out_timestamp_low = 0;
742 ps_curr_out->i4_out_timestamp_high = 0;
743
744 /*attach the app frame info of this buffer */
745 ps_curr_out->pv_app_frm_ctxt = NULL;
746
747 /* frame never skipped for now */
748 ps_curr_out->i4_frame_skipped = 0;
749
750 /* update error code and return */
751 ps_curr_out->i4_process_error_code = ret;
752
753 ps_curr_out->i4_bytes_generated = ps_bitstrm->u4_strm_buf_offset;
754
755 /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/
756 {
757 WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal;
758 WORD32 ctr = 0;
759
760 ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU);
761
762 ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals;
763 for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++)
764 {
765 /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
766 ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr];
767
768 #if POPULATE_NAL_SIZE
769
770 /* ----------Populate NAL Size -------------*/
771 if((ctr + 1) < num_non_vcl_prefix_nals)
772 {
773 ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1];
774 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
775 (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
776 }
777 else
778 {
779 ULWORD64 u8_next_nal_start =
780 (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
781 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
782 (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
783 }
784 ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0);
785
786 #elif POPULATE_NAL_OFFSET
787
788 /* ----------Populate NAL Offset -------------*/
789
790 ASSERT(u8_cur_nal_start >= u8_bitstream_base);
791 ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] =
792 (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
793
794 if(ctr)
795 {
796 /* sanity check on increasing NAL offsets */
797 ASSERT(
798 ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] >
799 ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]);
800 }
801 #endif /* POPULATE_NAL_SIZE */
802 }
803 }
804
805 ps_curr_out->i4_buf_id = out_buf_id;
806 ps_curr_out->i4_end_flag = 0;
807 if(IHEVCE_SUCCESS == ret)
808 {
809 ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
810 }
811 else
812 {
813 ps_curr_out->i4_process_ret_sts = IV_FAIL;
814 }
815 ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME;
816
817 /* Call back to Apln. saying buffer is produced */
818 ps_hle_ctxt->ihevce_output_strm_fill_done(
819 ps_hle_ctxt->pv_out_cb_handle, ps_curr_out, i4_bitrate_instance_id, i4_resolution_id);
820
821 /* release the input buffer*/
822 ihevce_q_rel_buf(
823 (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id), out_buf_id);
824
825 return ret;
826 }
827