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 WORD32 codec_tier =
392 ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.i4_codec_tier;
393
394 i4_buffersize =
395 (UWORD32)g_as_level_data[codec_level_index].i4_max_cpb[codec_tier] *
396 CBP_VCL_FACTOR;
397 i4_trgt_bit_rate =
398 (UWORD32)g_as_level_data[codec_level_index].i4_max_bit_rate[codec_tier] *
399 CBP_VCL_FACTOR;
400 i4_dbf = i4_buffersize;
401 }
402
403 ps_curr_inp->s_sei.s_buf_period_sei_params.u4_buffer_size_sei = i4_buffersize;
404 ps_curr_inp->s_sei.s_buf_period_sei_params.u4_dbf_sei = i4_dbf;
405 ps_curr_inp->s_sei.s_buf_period_sei_params.u4_target_bit_rate_sei =
406 i4_trgt_bit_rate;
407
408 /* ----------------- Derivation of u4_au_cpb_removal_delay_minus1 --------------------------------*/
409 ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1 =
410 u4_au_cpb_removal_delay_minus1;
411
412 /* ----------------- Derivation of u4_pic_dpb_output_delay --------------------------------*/
413 ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_pic_dpb_output_delay =
414 ps_curr_inp->ps_sps->ai1_sps_max_num_reorder_pics[0] +
415 ps_curr_inp->i4_display_num - u4_encode_frm_num;
416 }
417 /* call the core entropy coding entry point function */
418 entropy_error = ihevce_entropy_encode_frame(
419 pv_entropy_hdl, ps_curr_out, ps_curr_inp, ps_curr_out->i4_bitstream_buf_size);
420
421 /* ----------------- Derivation of u4_au_cpb_removal_delay_minus1 --------------------------------*/
422 if(ps_curr_inp->s_sei.i1_buf_period_params_present_flag)
423 {
424 /* Reset u4_au_cpb_removal_delay_minus1 after every buffering period as subsequent pictiming is w.r.t new buffering period SEI */
425 u4_au_cpb_removal_delay_minus1 = 0;
426 }
427 else
428 {
429 /* cpb delay is circularly incremented with wrap around based on max length signalled in VUI */
430 UWORD8 u1_au_cpb_removal_delay_length =
431 ps_curr_inp->ps_sps->s_vui_parameters.s_vui_hrd_parameters
432 .u1_au_cpb_removal_delay_length_minus1 +
433 1;
434
435 UWORD32 u4_max_cpb_removal_delay_val =
436 (1 << u1_au_cpb_removal_delay_length) - 1;
437
438 u4_au_cpb_removal_delay_minus1 = (u4_au_cpb_removal_delay_minus1 + 1) &
439 u4_max_cpb_removal_delay_val;
440 }
441 /* Debug prints for entropy error */
442 if(entropy_error)
443 {
444 DBG_PRINTF("Entropy encode error %x\n", entropy_error);
445 DEBUG("Entropy encode error %d\n", entropy_error);
446 }
447 if(ps_enc_ctxt->ps_stat_prms->s_config_prms.i4_rate_control_mode != 3)
448 {
449 /* acquire mutex lock for rate control calls */
450 osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
451
452 /* get frame rate/bit rate/max buffer size */
453 ihevce_vbv_compliance_frame_level_update(
454 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
455 (ps_curr_out->i4_bytes_generated << 3),
456 i4_resolution_id,
457 i4_bitrate_instance_num,
458 ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1);
459 /* release mutex lock after rate control calls */
460 osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
461 }
462
463 /*correct the mismatch between rdopt and entropy thread mismatch*/
464 {
465 /* acquire mutex lock for rate control calls */
466 osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
467
468 /*Set to -1 when no change in bitrate, other-wise set to encoder bufferfullness at that moment*/
469 ps_curr_out->i8_cur_vbv_level = ps_curr_inp->i8_buf_level_bitrate_change;
470 if(ps_curr_inp->i8_buf_level_bitrate_change != -1)
471 {
472 LWORD64 bitrate, buffer_size;
473 ASSERT(
474 i4_bitrate_instance_num ==
475 0); //since dynamic change in bitrate is not supported in multi bitrate and resolution
476 get_avg_bitrate_bufsize(
477 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
478 &bitrate,
479 &buffer_size);
480
481 change_bitrate_vbv_complaince(
482 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
483 bitrate,
484 buffer_size);
485 /*Change bitrate in SEI-VUI related context as well*/
486 ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms
487 .as_tgt_params[ps_enc_ctxt->i4_resolution_id]
488 .ai4_tgt_bitrate[i4_bitrate_instance_num] = (WORD32)bitrate;
489 ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms
490 .as_tgt_params[ps_enc_ctxt->i4_resolution_id]
491 .ai4_max_vbv_buffer_size[i4_bitrate_instance_num] = (WORD32)buffer_size;
492 }
493 /*account for error to meet bitrate more precisely*/
494 ihevce_rc_rdopt_entropy_bit_correct(
495 ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num],
496 (ps_curr_out->i4_bytes_generated << 3),
497 inp_buf_id); //ps_curr_inp->i4_inp_timestamp_low
498
499 /* release mutex lock after rate control calls */
500 osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
501 }
502 u4_encode_frm_num++;
503 }
504 else
505 {
506 ps_curr_out->i4_bytes_generated = 0;
507 ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME;
508 }
509
510 ps_curr_out->i4_buf_id = out_buf_id;
511 end_flag = ps_curr_inp->i4_end_flag;
512 ps_curr_out->i4_end_flag = ps_curr_inp->i4_end_flag;
513
514 if(1 == ps_enc_ctxt->s_multi_thrd.i4_force_end_flag)
515 {
516 end_flag = 1;
517 ps_curr_out->i4_end_flag = 1;
518 }
519 if(!i4_use_dummy_buffer)
520 {
521 /* Call back to Apln. saying buffer is produced */
522 ps_hle_ctxt->ihevce_output_strm_fill_done(
523 ps_hle_ctxt->pv_out_cb_handle,
524 ps_curr_out,
525 i4_bufque_id, /* br intance */
526 i4_out_res_id /* res_instance*/);
527 }
528
529 if(ps_curr_inp->i4_frm_proc_valid_flag)
530 {
531 ps_curr_inp->s_pic_level_info.u8_total_bits_generated =
532 ps_curr_out->i4_bytes_generated * 8;
533 }
534
535 /* --- release the current output buffer ---- */
536 if(!i4_use_dummy_buffer)
537 {
538 ihevce_q_rel_buf(
539 (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id);
540 }
541
542 /* release the input buffer*/
543 ihevce_q_rel_buf(
544 (void *)ps_enc_ctxt,
545 (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num),
546 inp_buf_id);
547
548 /* reset the pointers to NULL */
549 ps_curr_inp = NULL;
550 ps_curr_out = NULL;
551 }
552 else
553 {
554 end_flag = ps_curr_inp->i4_end_flag;
555 if(NULL != ps_curr_inp)
556 {
557 /* release the input buffer*/
558 ihevce_q_rel_buf(
559 (void *)ps_enc_ctxt,
560 (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num),
561 inp_buf_id);
562 }
563
564 // ASSERT(0);
565 }
566 PROFILE_STOP(
567 &ps_hle_ctxt->profile_entropy[ps_enc_ctxt->i4_resolution_id][i4_bitrate_instance_num],
568 NULL);
569 }
570
571 /* Release all the buffers the application might have queued in */
572 /* Do this only if its not a force end */
573 if(1 != ps_enc_ctxt->s_multi_thrd.i4_force_end_flag)
574 {
575 end_flag = 0;
576 }
577 if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
578 {
579 ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control = 1;
580 i4_do_entr_last = ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id];
581 }
582 else
583 {
584 i4_do_entr_last = 1;
585 }
586
587 if((1 == i4_do_entr_last) && (!i4_use_dummy_buffer))
588 {
589 while(0 == end_flag)
590 {
591 if(1 == ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control) //FFMPEG application
592 {
593 /* ------- get a filled descriptor from output Que ------------ */
594 ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
595 (void *)ps_enc_ctxt,
596 (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id),
597 &out_buf_id,
598 BUFF_QUE_NON_BLOCKING_MODE);
599
600 /* Update the end_flag from application */
601 end_flag = (ps_curr_out == NULL);
602 }
603 else if(
604 0 == ps_enc_ctxt->ps_stat_prms
605 ->i4_outbuf_buf_free_control) // process call control based application
606 {
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_BLOCKING_MODE);
612 }
613 else
614 {
615 /* should not enter here */
616 ASSERT(0);
617 }
618
619 if(ps_curr_out)
620 {
621 end_flag = ps_curr_out->i4_is_last_buf;
622
623 /* Fill the min. necessory things */
624 ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
625 ps_curr_out->i4_end_flag = 1;
626 ps_curr_out->i4_bytes_generated = 0;
627
628 /* Call back to Apln. saying buffer is produced */
629 ps_hle_ctxt->ihevce_output_strm_fill_done(
630 ps_hle_ctxt->pv_out_cb_handle,
631 ps_curr_out,
632 i4_bufque_id, /* br intance */
633 i4_out_res_id /* res_instance*/);
634
635 /* --- release the current output buffer ---- */
636 ihevce_q_rel_buf(
637 (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id);
638 }
639 }
640 }
641 if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out)
642 {
643 /* Mres single out usecase: Set active res_id to zero for curretn res so that other resolutions exit entropy */
644 ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 0;
645 }
646
647 return (0);
648 }
649
650 /**
651 ******************************************************************************
652 *
653 * @brief Generate sps, pps and vps header for
654 *
655 * @par Description
656 * This function generates nal headers like SPS/PPS/VPS
657 *
658 * @param[in] ps_hle_ctxt
659 * pointer to high level interface context (handle)
660 *
661 * @param[in] i4_bitrate_instance_id
662 * bitrate id
663 *
664 * @param[in] i4_resolution_id
665 * resolution id
666 *
667 * @return success or failure error code
668 *
669 ******************************************************************************
670 */
ihevce_entropy_encode_header(ihevce_hle_ctxt_t * ps_hle_ctxt,WORD32 i4_bitrate_instance_id,WORD32 i4_resolution_id)671 WORD32 ihevce_entropy_encode_header(
672 ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 i4_bitrate_instance_id, WORD32 i4_resolution_id)
673 {
674 WORD32 ret = IHEVCE_SUCCESS;
675 bitstrm_t s_bit_strm;
676 bitstrm_t *ps_bitstrm = &s_bit_strm;
677 enc_ctxt_t *ps_enc_ctxt;
678 ihevce_tgt_layer_params_t *ps_tgt_lyr_prms;
679 sps_t *ps_sps;
680 vps_t *ps_vps;
681 pps_t *ps_pps;
682 iv_output_data_buffs_t *ps_curr_out = NULL;
683 WORD32 out_buf_id;
684
685 /* sanity checks */
686 if((ps_hle_ctxt == NULL) || (ps_hle_ctxt->i4_size != sizeof(ihevce_hle_ctxt_t)) ||
687 ps_hle_ctxt->i4_hle_init_done != 1)
688 return IHEVCE_FAIL;
689
690 ps_tgt_lyr_prms = (ihevce_tgt_layer_params_t *)&ps_hle_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms;
691
692 if(i4_resolution_id >= ps_tgt_lyr_prms->i4_num_res_layers)
693 return IHEVCE_FAIL;
694
695 if(i4_bitrate_instance_id >=
696 ps_tgt_lyr_prms->as_tgt_params[i4_resolution_id].i4_num_bitrate_instances)
697 return IHEVCE_FAIL;
698
699 ps_enc_ctxt = (enc_ctxt_t *)ps_hle_ctxt->apv_enc_hdl[i4_resolution_id];
700 ps_sps = &ps_enc_ctxt->as_sps[i4_bitrate_instance_id];
701 ps_vps = &ps_enc_ctxt->as_vps[i4_bitrate_instance_id];
702 ps_pps = &ps_enc_ctxt->as_pps[i4_bitrate_instance_id];
703
704 /* ------- get a filled descriptor from output Que ------------ */
705 ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff(
706 (void *)ps_enc_ctxt,
707 (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id),
708 &out_buf_id,
709 BUFF_QUE_BLOCKING_MODE);
710
711 /* Initialize the bitstream engine */
712 ret |= ihevce_bitstrm_init(
713 ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, ps_curr_out->i4_bitstream_buf_size);
714
715 /* Reset Bitstream NAL counter */
716 ps_bitstrm->i4_num_nal = 0;
717
718 /* generate vps */
719 ret |= ihevce_generate_vps(ps_bitstrm, ps_vps);
720
721 /* generate sps */
722 ret |= ihevce_generate_sps(ps_bitstrm, ps_sps);
723
724 /* generate pps */
725 ret |= ihevce_generate_pps(ps_bitstrm, ps_pps);
726
727 /* attach the time stamp of the input to output */
728 ps_curr_out->i4_out_timestamp_low = 0;
729 ps_curr_out->i4_out_timestamp_high = 0;
730
731 /*attach the app frame info of this buffer */
732 ps_curr_out->pv_app_frm_ctxt = NULL;
733
734 /* frame never skipped for now */
735 ps_curr_out->i4_frame_skipped = 0;
736
737 /* update error code and return */
738 ps_curr_out->i4_process_error_code = ret;
739
740 ps_curr_out->i4_bytes_generated = ps_bitstrm->u4_strm_buf_offset;
741
742 /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/
743 {
744 WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal;
745 WORD32 ctr = 0;
746
747 ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU);
748
749 ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals;
750 for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++)
751 {
752 /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
753 ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr];
754
755 #if POPULATE_NAL_SIZE
756
757 /* ----------Populate NAL Size -------------*/
758 if((ctr + 1) < num_non_vcl_prefix_nals)
759 {
760 ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1];
761 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
762 (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
763 }
764 else
765 {
766 ULWORD64 u8_next_nal_start =
767 (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
768 ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
769 (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
770 }
771 ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0);
772
773 #elif POPULATE_NAL_OFFSET
774
775 /* ----------Populate NAL Offset -------------*/
776
777 ASSERT(u8_cur_nal_start >= u8_bitstream_base);
778 ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] =
779 (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
780
781 if(ctr)
782 {
783 /* sanity check on increasing NAL offsets */
784 ASSERT(
785 ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] >
786 ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]);
787 }
788 #endif /* POPULATE_NAL_SIZE */
789 }
790 }
791
792 ps_curr_out->i4_buf_id = out_buf_id;
793 ps_curr_out->i4_end_flag = 0;
794 if(IHEVCE_SUCCESS == ret)
795 {
796 ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
797 }
798 else
799 {
800 ps_curr_out->i4_process_ret_sts = IV_FAIL;
801 }
802 ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME;
803
804 /* Call back to Apln. saying buffer is produced */
805 ps_hle_ctxt->ihevce_output_strm_fill_done(
806 ps_hle_ctxt->pv_out_cb_handle, ps_curr_out, i4_bitrate_instance_id, i4_resolution_id);
807
808 /* release the input buffer*/
809 ihevce_q_rel_buf(
810 (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id), out_buf_id);
811
812 return ret;
813 }
814