/src/libhevc/encoder/ihevce_entropy_cod.c
Line | Count | Source (jump to first uncovered line) |
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 | | */ |
143 | | WORD32 ihevce_ent_coding_thrd(void *pv_frm_proc_thrd_ctxt) |
144 | 0 | { |
145 | | /* local variabels */ |
146 | 0 | frm_proc_thrd_ctxt_t *ps_thrd_ctxt; |
147 | 0 | enc_ctxt_t *ps_enc_ctxt; |
148 | 0 | WORD32 i4_thrd_id; |
149 | 0 | ihevce_hle_ctxt_t *ps_hle_ctxt; |
150 | 0 | WORD32 end_flag; |
151 | 0 | WORD32 out_buf_id; |
152 | 0 | WORD32 inp_buf_id; |
153 | 0 | WORD32 entropy_error = 0; |
154 | 0 | WORD32 i4_bitrate_instance_num, i4_resolution_id, i4_out_res_id; |
155 | 0 | WORD32 i4_bufque_id; |
156 | 0 | UWORD32 u4_encode_frm_num = 0; |
157 | 0 | UWORD32 u4_au_cpb_removal_delay_minus1 = 0; |
158 | 0 | WORD32 i4_no_output = 0; |
159 | 0 | WORD32 i4_do_entr_last = 1; |
160 | 0 | WORD32 i4_use_dummy_buffer = 0; |
161 | 0 | void *pv_entropy_hdl; |
162 | 0 | entropy_context_t *ps_entropy_ctxt; |
163 | |
|
164 | 0 | iv_output_data_buffs_t *ps_curr_out = NULL; |
165 | 0 | frm_proc_ent_cod_ctxt_t *ps_curr_inp = NULL; |
166 | 0 | iv_output_data_buffs_t s_curr_out_dummy; |
167 | | |
168 | | /* derive local variables */ |
169 | 0 | ps_thrd_ctxt = (frm_proc_thrd_ctxt_t *)pv_frm_proc_thrd_ctxt; |
170 | 0 | i4_thrd_id = ps_thrd_ctxt->i4_thrd_id; |
171 | 0 | ps_hle_ctxt = (ihevce_hle_ctxt_t *)ps_thrd_ctxt->ps_hle_ctxt; |
172 | 0 | ps_enc_ctxt = (enc_ctxt_t *)ps_thrd_ctxt->pv_enc_ctxt; |
173 | 0 | end_flag = 0; |
174 | 0 | i4_bitrate_instance_num = i4_thrd_id; |
175 | 0 | i4_bufque_id = i4_thrd_id; |
176 | 0 | i4_resolution_id = ps_enc_ctxt->i4_resolution_id; |
177 | 0 | 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 | 0 | i4_bufque_id = i4_bitrate_instance_num; |
183 | 0 | if(i4_bitrate_instance_num == 0) |
184 | 0 | { |
185 | 0 | i4_bufque_id = ps_enc_ctxt->i4_ref_mbr_id; |
186 | 0 | } |
187 | 0 | else if(i4_bitrate_instance_num == ps_enc_ctxt->i4_ref_mbr_id) |
188 | 0 | { |
189 | 0 | i4_bufque_id = 0; |
190 | 0 | } |
191 | |
|
192 | 0 | if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out) |
193 | 0 | { |
194 | 0 | i4_bufque_id = 0; |
195 | 0 | i4_out_res_id = 0; |
196 | 0 | } |
197 | 0 | pv_entropy_hdl = ps_enc_ctxt->s_module_ctxt.apv_ent_cod_ctxt[i4_bitrate_instance_num]; |
198 | 0 | ps_entropy_ctxt = (entropy_context_t *)pv_entropy_hdl; |
199 | | /* ---------- Processing Loop until end command is recieved --------- */ |
200 | 0 | while(0 == end_flag) |
201 | 0 | { |
202 | | /*Get a buffer pointer*/ |
203 | | /* ------- get next input buffer from Frame buffer que ---------- */ |
204 | 0 | ps_curr_inp = (frm_proc_ent_cod_ctxt_t *)ihevce_q_get_filled_buff( |
205 | 0 | (void *)ps_enc_ctxt, |
206 | 0 | (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num), |
207 | 0 | &inp_buf_id, |
208 | 0 | BUFF_QUE_BLOCKING_MODE); |
209 | 0 | if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out) |
210 | 0 | { |
211 | 0 | if(1 == ps_curr_inp->i4_out_flush_flag) |
212 | 0 | { |
213 | 0 | if(1 == ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id]) |
214 | 0 | ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 0; |
215 | 0 | else |
216 | 0 | ASSERT(0); |
217 | 0 | } |
218 | 0 | else |
219 | 0 | { |
220 | 0 | if(0 == ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id]) |
221 | 0 | { |
222 | | /* During change in resolution check whether prev res is active before starting to dump new resolution */ |
223 | 0 | WORD32 other_res_active = 1; |
224 | 0 | WORD32 ctr; |
225 | 0 | volatile WORD32 *pi4_active_res_check; |
226 | 0 | pi4_active_res_check = ps_enc_ctxt->s_multi_thrd.pi4_active_res_id; |
227 | 0 | while(other_res_active) |
228 | 0 | { |
229 | | /* Continue in polling mode untill all the other resolutions are in passive mode */ |
230 | 0 | other_res_active = 0; |
231 | 0 | for(ctr = 0; |
232 | 0 | ctr < ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_num_res_layers; |
233 | 0 | ctr++) |
234 | 0 | { |
235 | 0 | if(ctr != i4_resolution_id) |
236 | 0 | { |
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 | 0 | other_res_active |= pi4_active_res_check[ctr]; |
241 | 0 | } |
242 | 0 | } |
243 | 0 | if(1 == ps_curr_inp->i4_end_flag) |
244 | 0 | { |
245 | 0 | i4_no_output = 1; |
246 | 0 | } |
247 | 0 | } |
248 | |
|
249 | 0 | if(0 == ps_curr_inp->i4_end_flag) |
250 | 0 | { |
251 | 0 | ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 1; |
252 | 0 | } |
253 | 0 | } |
254 | 0 | } |
255 | 0 | } |
256 | 0 | if(0 == ps_curr_inp->i4_out_flush_flag) |
257 | 0 | { |
258 | 0 | if(1 == i4_no_output) |
259 | 0 | { |
260 | 0 | ps_curr_out = NULL; |
261 | 0 | } |
262 | 0 | else |
263 | 0 | { |
264 | 0 | if(!i4_use_dummy_buffer) |
265 | 0 | { |
266 | | /* ------- get a filled descriptor from output Que ------------ */ |
267 | 0 | ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff( |
268 | 0 | (void *)ps_enc_ctxt, |
269 | 0 | (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), |
270 | 0 | &out_buf_id, |
271 | 0 | BUFF_QUE_BLOCKING_MODE); |
272 | 0 | } |
273 | 0 | else |
274 | 0 | { |
275 | 0 | ps_curr_out = &s_curr_out_dummy; |
276 | 0 | out_buf_id = 0; |
277 | 0 | ps_curr_out->i4_bitstream_buf_size = ps_entropy_ctxt->i4_bitstream_buf_size; |
278 | 0 | ps_curr_out->pv_bitstream_bufs = ps_entropy_ctxt->pv_dummy_out_buf; |
279 | 0 | } |
280 | 0 | } |
281 | 0 | } |
282 | |
|
283 | 0 | PROFILE_START( |
284 | 0 | &ps_hle_ctxt->profile_entropy[ps_enc_ctxt->i4_resolution_id][i4_bitrate_instance_num]); |
285 | | /* Content Light Level Information */ |
286 | 0 | { |
287 | 0 | ps_curr_inp->s_sei.i1_sei_cll_enable = |
288 | 0 | (WORD8)ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.i4_sei_cll_enable; |
289 | 0 | ps_curr_inp->s_sei.s_cll_info_sei_params.u2_sei_max_cll = |
290 | 0 | ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.u2_sei_max_cll; |
291 | 0 | ps_curr_inp->s_sei.s_cll_info_sei_params.u2_sei_avg_cll = |
292 | 0 | ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.u2_sei_avg_cll; |
293 | 0 | } |
294 | 0 | if((NULL != ps_curr_out) && (NULL != ps_curr_inp)) |
295 | | |
296 | 0 | { |
297 | 0 | WORD32 i; |
298 | | |
299 | | /*PIC_INFO: reset the pic-level info flags*/ |
300 | 0 | ps_curr_inp->s_pic_level_info.i8_total_cu = 0; |
301 | 0 | ps_curr_inp->s_pic_level_info.i8_total_intra_cu = 0; |
302 | 0 | ps_curr_inp->s_pic_level_info.i8_total_inter_cu = 0; |
303 | 0 | ps_curr_inp->s_pic_level_info.i8_total_skip_cu = 0; |
304 | 0 | ps_curr_inp->s_pic_level_info.i8_total_pu = 0; |
305 | 0 | ps_curr_inp->s_pic_level_info.i8_total_intra_pu = 0; |
306 | 0 | ps_curr_inp->s_pic_level_info.i8_total_non_skipped_inter_pu = 0; |
307 | 0 | ps_curr_inp->s_pic_level_info.i8_total_merge_pu = 0; |
308 | 0 | for(i = 0; i < 4; i++) |
309 | 0 | { |
310 | 0 | ps_curr_inp->s_pic_level_info.i8_total_cu_based_on_size[i] = 0; |
311 | 0 | ps_curr_inp->s_pic_level_info.i8_total_2nx2n_intra_pu[i] = 0; |
312 | 0 | ps_curr_inp->s_pic_level_info.i8_total_2nx2n_inter_pu[i] = 0; |
313 | 0 | ps_curr_inp->s_pic_level_info.i8_total_tu_based_on_size[i] = 0; |
314 | 0 | ps_curr_inp->s_pic_level_info.i8_total_smp_inter_pu[i] = 0; |
315 | 0 | if(i != 3) |
316 | 0 | { |
317 | 0 | ps_curr_inp->s_pic_level_info.i8_total_amp_inter_pu[i] = 0; |
318 | 0 | ps_curr_inp->s_pic_level_info.i8_total_nxn_inter_pu[i] = 0; |
319 | 0 | } |
320 | 0 | } |
321 | |
|
322 | 0 | ps_curr_inp->s_pic_level_info.i8_total_nxn_intra_pu = 0; |
323 | 0 | ps_curr_inp->s_pic_level_info.i8_total_L0_mode = 0; |
324 | 0 | ps_curr_inp->s_pic_level_info.i8_total_L1_mode = 0; |
325 | 0 | 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 | 0 | for(i = 0; i < MAX_DPB_SIZE; i++) |
328 | 0 | { |
329 | 0 | ps_curr_inp->s_pic_level_info.i8_total_L0_ref_idx[i] = 0; |
330 | 0 | ps_curr_inp->s_pic_level_info.i8_total_L1_ref_idx[i] = 0; |
331 | 0 | } |
332 | |
|
333 | 0 | ps_curr_inp->s_pic_level_info.i8_total_tu = 0; |
334 | 0 | ps_curr_inp->s_pic_level_info.i8_total_non_coded_tu = 0; |
335 | 0 | ps_curr_inp->s_pic_level_info.i8_total_intra_coded_tu = 0; |
336 | 0 | ps_curr_inp->s_pic_level_info.i8_total_inter_coded_tu = 0; |
337 | |
|
338 | 0 | ps_curr_inp->s_pic_level_info.i8_total_qp = 0; |
339 | 0 | ps_curr_inp->s_pic_level_info.i8_total_qp_min_cu = 0; |
340 | 0 | ps_curr_inp->s_pic_level_info.i4_min_qp = 100; |
341 | 0 | ps_curr_inp->s_pic_level_info.i4_max_qp = 0; |
342 | 0 | ps_curr_inp->s_pic_level_info.i4_max_frame_qp = 0; |
343 | |
|
344 | 0 | ps_curr_inp->s_pic_level_info.i8_sum_squared_frame_qp = 0; |
345 | 0 | ps_curr_inp->s_pic_level_info.i8_total_frame_qp = 0; |
346 | 0 | ps_curr_inp->s_pic_level_info.f_total_buffer_underflow = 0; |
347 | 0 | ps_curr_inp->s_pic_level_info.f_total_buffer_overflow = 0; |
348 | 0 | ps_curr_inp->s_pic_level_info.f_max_buffer_underflow = 0; |
349 | 0 | ps_curr_inp->s_pic_level_info.f_max_buffer_overflow = 0; |
350 | |
|
351 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_intra = 0; |
352 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_inter = 0; |
353 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_slice_header = 0; |
354 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_sao = 0; |
355 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_split_cu_flag = 0; |
356 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_cu_hdr_bits = 0; |
357 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_split_tu_flag = 0; |
358 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_qp_delta_bits = 0; |
359 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_cbf_luma_bits = 0; |
360 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_cbf_chroma_bits = 0; |
361 | |
|
362 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_res_luma_bits = 0; |
363 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_res_chroma_bits = 0; |
364 | |
|
365 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_ref_id = 0; |
366 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_mvd = 0; |
367 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_merge_flag = 0; |
368 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_mpm_luma = 0; |
369 | 0 | ps_curr_inp->s_pic_level_info.u8_bits_estimated_mpm_chroma = 0; |
370 | |
|
371 | 0 | if(1 == ps_curr_inp->i4_frm_proc_valid_flag) |
372 | 0 | { |
373 | | /* --- Init of buffering period and pic timing SEI related params ----*/ |
374 | 0 | { |
375 | 0 | UWORD32 i4_dbf, i4_buffersize, i4_trgt_bit_rate; |
376 | 0 | if(ps_enc_ctxt->ps_stat_prms->s_config_prms.i4_rate_control_mode != 3) |
377 | 0 | { |
378 | 0 | ihevce_get_dbf_buffer_size( |
379 | 0 | (void *)ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num], |
380 | 0 | &i4_buffersize, |
381 | 0 | &i4_dbf, |
382 | 0 | &i4_trgt_bit_rate); |
383 | 0 | } |
384 | 0 | else |
385 | 0 | { |
386 | | /* Default initializations in CQP mode */ |
387 | 0 | WORD32 codec_level = |
388 | 0 | ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.as_tgt_params[0] |
389 | 0 | .i4_codec_level; |
390 | 0 | WORD32 codec_level_index = ihevce_get_level_index(codec_level); |
391 | 0 | WORD32 codec_tier = |
392 | 0 | ps_enc_ctxt->ps_stat_prms->s_out_strm_prms.i4_codec_tier; |
393 | |
|
394 | 0 | i4_buffersize = |
395 | 0 | (UWORD32)g_as_level_data[codec_level_index].i4_max_cpb[codec_tier] * |
396 | 0 | CBP_VCL_FACTOR; |
397 | 0 | i4_trgt_bit_rate = |
398 | 0 | (UWORD32)g_as_level_data[codec_level_index].i4_max_bit_rate[codec_tier] * |
399 | 0 | CBP_VCL_FACTOR; |
400 | 0 | i4_dbf = i4_buffersize; |
401 | 0 | } |
402 | |
|
403 | 0 | ps_curr_inp->s_sei.s_buf_period_sei_params.u4_buffer_size_sei = i4_buffersize; |
404 | 0 | ps_curr_inp->s_sei.s_buf_period_sei_params.u4_dbf_sei = i4_dbf; |
405 | 0 | ps_curr_inp->s_sei.s_buf_period_sei_params.u4_target_bit_rate_sei = |
406 | 0 | i4_trgt_bit_rate; |
407 | | |
408 | | /* ----------------- Derivation of u4_au_cpb_removal_delay_minus1 --------------------------------*/ |
409 | 0 | ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_au_cpb_removal_delay_minus1 = |
410 | 0 | u4_au_cpb_removal_delay_minus1; |
411 | | |
412 | | /* ----------------- Derivation of u4_pic_dpb_output_delay --------------------------------*/ |
413 | 0 | ps_curr_inp->s_sei.s_pic_timing_sei_params.u4_pic_dpb_output_delay = |
414 | 0 | ps_curr_inp->ps_sps->ai1_sps_max_num_reorder_pics[0] + |
415 | 0 | ps_curr_inp->i4_display_num - u4_encode_frm_num; |
416 | 0 | } |
417 | | /* call the core entropy coding entry point function */ |
418 | 0 | entropy_error = ihevce_entropy_encode_frame( |
419 | 0 | 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 | 0 | if(ps_curr_inp->s_sei.i1_buf_period_params_present_flag) |
423 | 0 | { |
424 | | /* Reset u4_au_cpb_removal_delay_minus1 after every buffering period as subsequent pictiming is w.r.t new buffering period SEI */ |
425 | 0 | u4_au_cpb_removal_delay_minus1 = 0; |
426 | 0 | } |
427 | 0 | else |
428 | 0 | { |
429 | | /* cpb delay is circularly incremented with wrap around based on max length signalled in VUI */ |
430 | 0 | UWORD8 u1_au_cpb_removal_delay_length = |
431 | 0 | ps_curr_inp->ps_sps->s_vui_parameters.s_vui_hrd_parameters |
432 | 0 | .u1_au_cpb_removal_delay_length_minus1 + |
433 | 0 | 1; |
434 | |
|
435 | 0 | UWORD32 u4_max_cpb_removal_delay_val = |
436 | 0 | (1 << u1_au_cpb_removal_delay_length) - 1; |
437 | |
|
438 | 0 | u4_au_cpb_removal_delay_minus1 = (u4_au_cpb_removal_delay_minus1 + 1) & |
439 | 0 | u4_max_cpb_removal_delay_val; |
440 | 0 | } |
441 | | /* Debug prints for entropy error */ |
442 | 0 | if(entropy_error) |
443 | 0 | { |
444 | 0 | DBG_PRINTF("Entropy encode error %x\n", entropy_error); |
445 | 0 | DEBUG("Entropy encode error %d\n", entropy_error); |
446 | 0 | } |
447 | 0 | if(ps_enc_ctxt->ps_stat_prms->s_config_prms.i4_rate_control_mode != 3) |
448 | 0 | { |
449 | | /* acquire mutex lock for rate control calls */ |
450 | 0 | osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl); |
451 | | |
452 | | /* get frame rate/bit rate/max buffer size */ |
453 | 0 | ihevce_vbv_compliance_frame_level_update( |
454 | 0 | ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num], |
455 | 0 | (ps_curr_out->i4_bytes_generated << 3), |
456 | 0 | i4_resolution_id, |
457 | 0 | i4_bitrate_instance_num, |
458 | 0 | 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 | 0 | osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl); |
461 | 0 | } |
462 | | |
463 | | /*correct the mismatch between rdopt and entropy thread mismatch*/ |
464 | 0 | { |
465 | | /* acquire mutex lock for rate control calls */ |
466 | 0 | 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 | 0 | ps_curr_out->i8_cur_vbv_level = ps_curr_inp->i8_buf_level_bitrate_change; |
470 | 0 | if(ps_curr_inp->i8_buf_level_bitrate_change != -1) |
471 | 0 | { |
472 | 0 | LWORD64 bitrate, buffer_size; |
473 | 0 | ASSERT( |
474 | 0 | i4_bitrate_instance_num == |
475 | 0 | 0); //since dynamic change in bitrate is not supported in multi bitrate and resolution |
476 | 0 | get_avg_bitrate_bufsize( |
477 | 0 | ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num], |
478 | 0 | &bitrate, |
479 | 0 | &buffer_size); |
480 | |
|
481 | 0 | change_bitrate_vbv_complaince( |
482 | 0 | ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num], |
483 | 0 | bitrate, |
484 | 0 | buffer_size); |
485 | | /*Change bitrate in SEI-VUI related context as well*/ |
486 | 0 | ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms |
487 | 0 | .as_tgt_params[ps_enc_ctxt->i4_resolution_id] |
488 | 0 | .ai4_tgt_bitrate[i4_bitrate_instance_num] = (WORD32)bitrate; |
489 | 0 | ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms |
490 | 0 | .as_tgt_params[ps_enc_ctxt->i4_resolution_id] |
491 | 0 | .ai4_max_vbv_buffer_size[i4_bitrate_instance_num] = (WORD32)buffer_size; |
492 | 0 | } |
493 | | /*account for error to meet bitrate more precisely*/ |
494 | 0 | ihevce_rc_rdopt_entropy_bit_correct( |
495 | 0 | ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[i4_bitrate_instance_num], |
496 | 0 | (ps_curr_out->i4_bytes_generated << 3), |
497 | 0 | inp_buf_id); //ps_curr_inp->i4_inp_timestamp_low |
498 | | |
499 | | /* release mutex lock after rate control calls */ |
500 | 0 | osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl); |
501 | 0 | } |
502 | 0 | u4_encode_frm_num++; |
503 | 0 | } |
504 | 0 | else |
505 | 0 | { |
506 | 0 | ps_curr_out->i4_bytes_generated = 0; |
507 | 0 | ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME; |
508 | 0 | } |
509 | | |
510 | 0 | ps_curr_out->i4_buf_id = out_buf_id; |
511 | 0 | end_flag = ps_curr_inp->i4_end_flag; |
512 | 0 | ps_curr_out->i4_end_flag = ps_curr_inp->i4_end_flag; |
513 | |
|
514 | 0 | if(1 == ps_enc_ctxt->s_multi_thrd.i4_force_end_flag) |
515 | 0 | { |
516 | 0 | end_flag = 1; |
517 | 0 | ps_curr_out->i4_end_flag = 1; |
518 | 0 | } |
519 | 0 | if(!i4_use_dummy_buffer) |
520 | 0 | { |
521 | | /* Call back to Apln. saying buffer is produced */ |
522 | 0 | ps_hle_ctxt->ihevce_output_strm_fill_done( |
523 | 0 | ps_hle_ctxt->pv_out_cb_handle, |
524 | 0 | ps_curr_out, |
525 | 0 | i4_bufque_id, /* br intance */ |
526 | 0 | i4_out_res_id /* res_instance*/); |
527 | 0 | } |
528 | |
|
529 | 0 | if(ps_curr_inp->i4_frm_proc_valid_flag) |
530 | 0 | { |
531 | 0 | ps_curr_inp->s_pic_level_info.u8_total_bits_generated = |
532 | 0 | ps_curr_out->i4_bytes_generated * 8; |
533 | 0 | } |
534 | | |
535 | | /* --- release the current output buffer ---- */ |
536 | 0 | if(!i4_use_dummy_buffer) |
537 | 0 | { |
538 | 0 | ihevce_q_rel_buf( |
539 | 0 | (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id); |
540 | 0 | } |
541 | | |
542 | | /* release the input buffer*/ |
543 | 0 | ihevce_q_rel_buf( |
544 | 0 | (void *)ps_enc_ctxt, |
545 | 0 | (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num), |
546 | 0 | inp_buf_id); |
547 | | |
548 | | /* reset the pointers to NULL */ |
549 | 0 | ps_curr_inp = NULL; |
550 | 0 | ps_curr_out = NULL; |
551 | 0 | } |
552 | 0 | else |
553 | 0 | { |
554 | 0 | end_flag = ps_curr_inp->i4_end_flag; |
555 | 0 | if(NULL != ps_curr_inp) |
556 | 0 | { |
557 | | /* release the input buffer*/ |
558 | 0 | ihevce_q_rel_buf( |
559 | 0 | (void *)ps_enc_ctxt, |
560 | 0 | (IHEVCE_FRM_PRS_ENT_COD_Q + i4_bitrate_instance_num), |
561 | 0 | inp_buf_id); |
562 | 0 | } |
563 | | |
564 | | // ASSERT(0); |
565 | 0 | } |
566 | 0 | PROFILE_STOP( |
567 | 0 | &ps_hle_ctxt->profile_entropy[ps_enc_ctxt->i4_resolution_id][i4_bitrate_instance_num], |
568 | 0 | NULL); |
569 | 0 | } |
570 | | |
571 | | /* Release all the buffers the application might have queued in */ |
572 | | /* Do this only if its not a force end */ |
573 | 0 | if(1 != ps_enc_ctxt->s_multi_thrd.i4_force_end_flag) |
574 | 0 | { |
575 | 0 | end_flag = 0; |
576 | 0 | } |
577 | 0 | if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out) |
578 | 0 | { |
579 | 0 | ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control = 1; |
580 | 0 | i4_do_entr_last = ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id]; |
581 | 0 | } |
582 | 0 | else |
583 | 0 | { |
584 | 0 | i4_do_entr_last = 1; |
585 | 0 | } |
586 | |
|
587 | 0 | if((1 == i4_do_entr_last) && (!i4_use_dummy_buffer)) |
588 | 0 | { |
589 | 0 | while(0 == end_flag) |
590 | 0 | { |
591 | 0 | if(1 == ps_enc_ctxt->ps_stat_prms->i4_outbuf_buf_free_control) //FFMPEG application |
592 | 0 | { |
593 | | /* ------- get a filled descriptor from output Que ------------ */ |
594 | 0 | ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff( |
595 | 0 | (void *)ps_enc_ctxt, |
596 | 0 | (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), |
597 | 0 | &out_buf_id, |
598 | 0 | BUFF_QUE_NON_BLOCKING_MODE); |
599 | | |
600 | | /* Update the end_flag from application */ |
601 | 0 | end_flag = (ps_curr_out == NULL); |
602 | 0 | } |
603 | 0 | else if( |
604 | 0 | 0 == ps_enc_ctxt->ps_stat_prms |
605 | 0 | ->i4_outbuf_buf_free_control) // process call control based application |
606 | 0 | { |
607 | 0 | ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff( |
608 | 0 | (void *)ps_enc_ctxt, |
609 | 0 | (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), |
610 | 0 | &out_buf_id, |
611 | 0 | BUFF_QUE_BLOCKING_MODE); |
612 | 0 | } |
613 | 0 | else |
614 | 0 | { |
615 | | /* should not enter here */ |
616 | 0 | ASSERT(0); |
617 | 0 | } |
618 | | |
619 | 0 | if(ps_curr_out) |
620 | 0 | { |
621 | 0 | end_flag = ps_curr_out->i4_is_last_buf; |
622 | | |
623 | | /* Fill the min. necessory things */ |
624 | 0 | ps_curr_out->i4_process_ret_sts = IV_SUCCESS; |
625 | 0 | ps_curr_out->i4_end_flag = 1; |
626 | 0 | ps_curr_out->i4_bytes_generated = 0; |
627 | | |
628 | | /* Call back to Apln. saying buffer is produced */ |
629 | 0 | ps_hle_ctxt->ihevce_output_strm_fill_done( |
630 | 0 | ps_hle_ctxt->pv_out_cb_handle, |
631 | 0 | ps_curr_out, |
632 | 0 | i4_bufque_id, /* br intance */ |
633 | 0 | i4_out_res_id /* res_instance*/); |
634 | | |
635 | | /* --- release the current output buffer ---- */ |
636 | 0 | ihevce_q_rel_buf( |
637 | 0 | (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bufque_id), out_buf_id); |
638 | 0 | } |
639 | 0 | } |
640 | 0 | } |
641 | 0 | if(1 == ps_enc_ctxt->ps_stat_prms->s_tgt_lyr_prms.i4_mres_single_out) |
642 | 0 | { |
643 | | /* Mres single out usecase: Set active res_id to zero for curretn res so that other resolutions exit entropy */ |
644 | 0 | ps_enc_ctxt->s_multi_thrd.pi4_active_res_id[i4_resolution_id] = 0; |
645 | 0 | } |
646 | |
|
647 | 0 | return (0); |
648 | 0 | } |
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 | | */ |
671 | | WORD32 ihevce_entropy_encode_header( |
672 | | ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 i4_bitrate_instance_id, WORD32 i4_resolution_id) |
673 | 0 | { |
674 | 0 | WORD32 ret = IHEVCE_SUCCESS; |
675 | 0 | bitstrm_t s_bit_strm; |
676 | 0 | bitstrm_t *ps_bitstrm = &s_bit_strm; |
677 | 0 | enc_ctxt_t *ps_enc_ctxt; |
678 | 0 | ihevce_tgt_layer_params_t *ps_tgt_lyr_prms; |
679 | 0 | sps_t *ps_sps; |
680 | 0 | vps_t *ps_vps; |
681 | 0 | pps_t *ps_pps; |
682 | 0 | iv_output_data_buffs_t *ps_curr_out = NULL; |
683 | 0 | WORD32 out_buf_id; |
684 | | |
685 | | /* sanity checks */ |
686 | 0 | if((ps_hle_ctxt == NULL) || (ps_hle_ctxt->i4_size != sizeof(ihevce_hle_ctxt_t)) || |
687 | 0 | ps_hle_ctxt->i4_hle_init_done != 1) |
688 | 0 | return IHEVCE_FAIL; |
689 | | |
690 | 0 | ps_tgt_lyr_prms = (ihevce_tgt_layer_params_t *)&ps_hle_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms; |
691 | |
|
692 | 0 | if(i4_resolution_id >= ps_tgt_lyr_prms->i4_num_res_layers) |
693 | 0 | return IHEVCE_FAIL; |
694 | | |
695 | 0 | if(i4_bitrate_instance_id >= |
696 | 0 | ps_tgt_lyr_prms->as_tgt_params[i4_resolution_id].i4_num_bitrate_instances) |
697 | 0 | return IHEVCE_FAIL; |
698 | | |
699 | 0 | ps_enc_ctxt = (enc_ctxt_t *)ps_hle_ctxt->apv_enc_hdl[i4_resolution_id]; |
700 | 0 | ps_sps = &ps_enc_ctxt->as_sps[i4_bitrate_instance_id]; |
701 | 0 | ps_vps = &ps_enc_ctxt->as_vps[i4_bitrate_instance_id]; |
702 | 0 | ps_pps = &ps_enc_ctxt->as_pps[i4_bitrate_instance_id]; |
703 | | |
704 | | /* ------- get a filled descriptor from output Que ------------ */ |
705 | 0 | ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_filled_buff( |
706 | 0 | (void *)ps_enc_ctxt, |
707 | 0 | (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id), |
708 | 0 | &out_buf_id, |
709 | 0 | BUFF_QUE_BLOCKING_MODE); |
710 | | |
711 | | /* Initialize the bitstream engine */ |
712 | 0 | ret |= ihevce_bitstrm_init( |
713 | 0 | ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, ps_curr_out->i4_bitstream_buf_size); |
714 | | |
715 | | /* Reset Bitstream NAL counter */ |
716 | 0 | ps_bitstrm->i4_num_nal = 0; |
717 | | |
718 | | /* generate vps */ |
719 | 0 | ret |= ihevce_generate_vps(ps_bitstrm, ps_vps); |
720 | | |
721 | | /* generate sps */ |
722 | 0 | ret |= ihevce_generate_sps(ps_bitstrm, ps_sps); |
723 | | |
724 | | /* generate pps */ |
725 | 0 | ret |= ihevce_generate_pps(ps_bitstrm, ps_pps); |
726 | | |
727 | | /* attach the time stamp of the input to output */ |
728 | 0 | ps_curr_out->i4_out_timestamp_low = 0; |
729 | 0 | ps_curr_out->i4_out_timestamp_high = 0; |
730 | | |
731 | | /*attach the app frame info of this buffer */ |
732 | 0 | ps_curr_out->pv_app_frm_ctxt = NULL; |
733 | | |
734 | | /* frame never skipped for now */ |
735 | 0 | ps_curr_out->i4_frame_skipped = 0; |
736 | | |
737 | | /* update error code and return */ |
738 | 0 | ps_curr_out->i4_process_error_code = ret; |
739 | |
|
740 | 0 | ps_curr_out->i4_bytes_generated = ps_bitstrm->u4_strm_buf_offset; |
741 | | |
742 | | /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/ |
743 | 0 | { |
744 | 0 | WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal; |
745 | 0 | WORD32 ctr = 0; |
746 | |
|
747 | 0 | ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU); |
748 | | |
749 | 0 | ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals; |
750 | 0 | for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++) |
751 | 0 | { |
752 | | /* NAL offset is derive by subtracting Bistream base from NAL start pointer */ |
753 | 0 | ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr]; |
754 | |
|
755 | 0 | #if POPULATE_NAL_SIZE |
756 | | |
757 | | /* ----------Populate NAL Size -------------*/ |
758 | 0 | if((ctr + 1) < num_non_vcl_prefix_nals) |
759 | 0 | { |
760 | 0 | ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1]; |
761 | 0 | ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] = |
762 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
763 | 0 | } |
764 | 0 | else |
765 | 0 | { |
766 | 0 | ULWORD64 u8_next_nal_start = |
767 | 0 | (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset; |
768 | 0 | ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] = |
769 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
770 | 0 | } |
771 | 0 | 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 | 0 | } |
790 | 0 | } |
791 | | |
792 | 0 | ps_curr_out->i4_buf_id = out_buf_id; |
793 | 0 | ps_curr_out->i4_end_flag = 0; |
794 | 0 | if(IHEVCE_SUCCESS == ret) |
795 | 0 | { |
796 | 0 | ps_curr_out->i4_process_ret_sts = IV_SUCCESS; |
797 | 0 | } |
798 | 0 | else |
799 | 0 | { |
800 | 0 | ps_curr_out->i4_process_ret_sts = IV_FAIL; |
801 | 0 | } |
802 | 0 | ps_curr_out->i4_encoded_frame_type = IV_NA_FRAME; |
803 | | |
804 | | /* Call back to Apln. saying buffer is produced */ |
805 | 0 | ps_hle_ctxt->ihevce_output_strm_fill_done( |
806 | 0 | ps_hle_ctxt->pv_out_cb_handle, ps_curr_out, i4_bitrate_instance_id, i4_resolution_id); |
807 | | |
808 | | /* release the input buffer*/ |
809 | 0 | ihevce_q_rel_buf( |
810 | 0 | (void *)ps_enc_ctxt, (IHEVCE_OUTPUT_DATA_Q + i4_bitrate_instance_id), out_buf_id); |
811 | |
|
812 | 0 | return ret; |
813 | 0 | } |