/src/libhevc/encoder/ihevce_entropy_interface.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 | | * @file ihevce_entropy_interface.c |
23 | | * |
24 | | * @brief |
25 | | * This file contains function definitions for entropy interface related to |
26 | | * memory init and process apis |
27 | | * |
28 | | * @author |
29 | | * Ittiam |
30 | | * |
31 | | * List of Functions |
32 | | * ihevce_entropy_get_num_mem_recs() |
33 | | * ihevce_entropy_size_of_out_buffer() |
34 | | * ihevce_entropy_get_mem_recs() |
35 | | * ihevce_entropy_init() |
36 | | * ihevce_entropy_encode_frame() |
37 | | * |
38 | | ****************************************************************************** |
39 | | */ |
40 | | |
41 | | /*****************************************************************************/ |
42 | | /* File Includes */ |
43 | | /*****************************************************************************/ |
44 | | /* System include files */ |
45 | | #include <stdio.h> |
46 | | #include <string.h> |
47 | | #include <stdlib.h> |
48 | | #include <assert.h> |
49 | | #include <stdarg.h> |
50 | | #include <math.h> |
51 | | |
52 | | /* User include files */ |
53 | | #include "ihevc_typedefs.h" |
54 | | #include "itt_video_api.h" |
55 | | #include "ihevce_api.h" |
56 | | |
57 | | #include "rc_cntrl_param.h" |
58 | | #include "rc_frame_info_collector.h" |
59 | | #include "rc_look_ahead_params.h" |
60 | | |
61 | | #include "ihevc_defs.h" |
62 | | #include "ihevc_macros.h" |
63 | | #include "ihevc_debug.h" |
64 | | #include "ihevc_structs.h" |
65 | | #include "ihevc_platform_macros.h" |
66 | | #include "ihevc_deblk.h" |
67 | | #include "ihevc_itrans_recon.h" |
68 | | #include "ihevc_chroma_itrans_recon.h" |
69 | | #include "ihevc_chroma_intra_pred.h" |
70 | | #include "ihevc_intra_pred.h" |
71 | | #include "ihevc_inter_pred.h" |
72 | | #include "ihevc_mem_fns.h" |
73 | | #include "ihevc_padding.h" |
74 | | #include "ihevc_weighted_pred.h" |
75 | | #include "ihevc_sao.h" |
76 | | #include "ihevc_resi_trans.h" |
77 | | #include "ihevc_quant_iquant_ssd.h" |
78 | | #include "ihevc_cabac_tables.h" |
79 | | #include "ihevc_trans_tables.h" |
80 | | #include "ihevc_trans_macros.h" |
81 | | |
82 | | #include "ihevce_defs.h" |
83 | | #include "ihevce_lap_enc_structs.h" |
84 | | #include "ihevce_multi_thrd_structs.h" |
85 | | #include "ihevce_multi_thrd_funcs.h" |
86 | | #include "ihevce_me_common_defs.h" |
87 | | #include "ihevce_had_satd.h" |
88 | | #include "ihevce_error_codes.h" |
89 | | #include "ihevce_error_checks.h" |
90 | | #include "ihevce_bitstream.h" |
91 | | #include "ihevce_cabac.h" |
92 | | #include "ihevce_rdoq_macros.h" |
93 | | #include "ihevce_function_selector.h" |
94 | | #include "ihevce_enc_structs.h" |
95 | | #include "ihevce_global_tables.h" |
96 | | #include "ihevce_entropy_structs.h" |
97 | | #include "ihevce_entropy_interface.h" |
98 | | #include "ihevce_encode_header.h" |
99 | | #include "ihevce_encode_header_sei_vui.h" |
100 | | #include "ihevce_trace.h" |
101 | | |
102 | | #include "cast_types.h" |
103 | | #include "osal.h" |
104 | | #include "osal_defaults.h" |
105 | | |
106 | | /*****************************************************************************/ |
107 | | /* Function Definitions */ |
108 | | /*****************************************************************************/ |
109 | | |
110 | | /** |
111 | | ****************************************************************************** |
112 | | * |
113 | | * @brief Number of memory records are returned for entropy module |
114 | | * |
115 | | * @par Description |
116 | | * |
117 | | * @return number of memory records |
118 | | * |
119 | | ****************************************************************************** |
120 | | */ |
121 | | WORD32 ihevce_entropy_get_num_mem_recs(void) |
122 | 0 | { |
123 | 0 | return (NUM_ENTROPY_MEM_RECS); |
124 | 0 | } |
125 | | |
126 | | /** |
127 | | ****************************************************************************** |
128 | | * |
129 | | * @brief Estimated bitstream buffer size basing on input dimensions |
130 | | * |
131 | | * @par Description |
132 | | * |
133 | | * @return bitstream buffer size |
134 | | * |
135 | | ****************************************************************************** |
136 | | */ |
137 | | WORD32 ihevce_entropy_size_of_out_buffer(frm_proc_ent_cod_ctxt_t *ps_curr_inp) |
138 | 0 | { |
139 | 0 | WORD32 i4_size; |
140 | |
|
141 | 0 | i4_size = (WORD32)( |
142 | 0 | ps_curr_inp->ps_sps->i2_pic_height_in_luma_samples * |
143 | 0 | ps_curr_inp->ps_sps->i2_pic_width_in_luma_samples); |
144 | |
|
145 | 0 | return (i4_size); |
146 | 0 | } |
147 | | |
148 | | /** |
149 | | ****************************************************************************** |
150 | | * |
151 | | * @brief Populates Memory requirements of the entropy module |
152 | | * |
153 | | * @par Description |
154 | | * |
155 | | * @param[inout] ps_mem_tab |
156 | | * pointer to memory descriptors table |
157 | | * |
158 | | * @param[in] ps_init_prms |
159 | | * Create time static parameters |
160 | | * |
161 | | * @param[in] i4_mem_space |
162 | | * memspace in whihc memory request should be done |
163 | | * |
164 | | * @return number of memory requirements filled |
165 | | * |
166 | | ****************************************************************************** |
167 | | */ |
168 | | WORD32 ihevce_entropy_get_mem_recs( |
169 | | iv_mem_rec_t *ps_mem_tab, |
170 | | ihevce_static_cfg_params_t *ps_init_prms, |
171 | | WORD32 i4_mem_space, |
172 | | WORD32 i4_resolution_id) |
173 | 0 | { |
174 | | /* memories should be requested assuming worst case requirememnts */ |
175 | 0 | WORD32 max_width = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width; |
176 | 0 | WORD32 max_height = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_height; |
177 | 0 | WORD32 max_align_width = ALIGN64(max_width); |
178 | 0 | WORD32 max_align_height = ALIGN64(max_height); |
179 | | |
180 | | /* Module context structure */ |
181 | 0 | ps_mem_tab[ENTROPY_CTXT].i4_mem_size = sizeof(entropy_context_t); |
182 | 0 | ps_mem_tab[ENTROPY_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space; |
183 | 0 | ps_mem_tab[ENTROPY_CTXT].i4_mem_alignment = 64; |
184 | | |
185 | | /* top row cu skip flags (1 bit per 8x8CU) */ |
186 | 0 | ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_size = max_align_width >> 6; |
187 | 0 | ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space; |
188 | 0 | ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_alignment = 64; |
189 | | |
190 | | /* top row CU Depth (1 byte per 8x8CU) */ |
191 | 0 | ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_size = (max_align_width >> 3); |
192 | 0 | ps_mem_tab[ENTROPY_TOP_CU_DEPTH].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space; |
193 | 0 | ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_alignment = 64; |
194 | | |
195 | | /* Dummy_buffer to handle first pass MBR case*/ |
196 | 0 | ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_size = (max_align_width * max_align_height * 2); |
197 | 0 | ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space; |
198 | 0 | ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_alignment = 64; |
199 | |
|
200 | 0 | return (NUM_ENTROPY_MEM_RECS); |
201 | 0 | } |
202 | | |
203 | | /** |
204 | | ****************************************************************************** |
205 | | * |
206 | | * @brief Intialization of entropy module |
207 | | * |
208 | | * @par Description |
209 | | * pointers of the memory requests done in ihevce_entropy_get_mem_recs() are |
210 | | * used to initialized the entropy module and the handle is returned |
211 | | * |
212 | | * @param[inout] ps_mem_tab |
213 | | * pointer to memory descriptors table |
214 | | * |
215 | | * @param[in] ps_init_prms |
216 | | * Create time static parameters |
217 | | * |
218 | | * @return |
219 | | * Handle of the entropy module returned as void ptr |
220 | | * |
221 | | ****************************************************************************** |
222 | | */ |
223 | | void *ihevce_entropy_init( |
224 | | iv_mem_rec_t *ps_mem_tab, |
225 | | ihevce_static_cfg_params_t *ps_init_prms, |
226 | | void *pv_tile_params_base, |
227 | | WORD32 i4_res_id) |
228 | 0 | { |
229 | 0 | entropy_context_t *ps_entropy_ctxt; |
230 | | |
231 | | /* Entropy state structure */ |
232 | 0 | ps_entropy_ctxt = (entropy_context_t *)ps_mem_tab[ENTROPY_CTXT].pv_base; |
233 | 0 | memset(ps_entropy_ctxt, 0, sizeof(entropy_context_t)); |
234 | |
|
235 | 0 | ps_entropy_ctxt->pu1_skip_cu_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].pv_base; |
236 | 0 | ps_entropy_ctxt->pu1_cu_depth_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_CU_DEPTH].pv_base; |
237 | 0 | ps_entropy_ctxt->pv_dummy_out_buf = ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].pv_base; |
238 | 0 | ps_entropy_ctxt->i4_bitstream_buf_size = ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_size; |
239 | | |
240 | | /* perform all one time initialisation here */ |
241 | | /*************************************************************************/ |
242 | | /* Note pu1_cbf_cb, pu1_cbf_cr initialization are done with array idx 1 */ |
243 | | /* This is because these flags are accessed as pu1_cbf_cb[tfr_depth - 1] */ |
244 | | /* without cheking for tfr_depth= 0 */ |
245 | | /*************************************************************************/ |
246 | 0 | ps_entropy_ctxt->apu1_cbf_cb[0] = &ps_entropy_ctxt->au1_cbf_cb[0][1]; |
247 | 0 | ps_entropy_ctxt->apu1_cbf_cr[0] = &ps_entropy_ctxt->au1_cbf_cr[0][1]; |
248 | 0 | ps_entropy_ctxt->apu1_cbf_cb[1] = &ps_entropy_ctxt->au1_cbf_cb[1][1]; |
249 | 0 | ps_entropy_ctxt->apu1_cbf_cr[1] = &ps_entropy_ctxt->au1_cbf_cr[1][1]; |
250 | |
|
251 | 0 | memset(ps_entropy_ctxt->au1_cbf_cb, 0, (MAX_TFR_DEPTH + 1) * 2 * sizeof(UWORD8)); |
252 | | |
253 | | /* register codec level */ |
254 | 0 | ps_entropy_ctxt->i4_codec_level = |
255 | 0 | ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id].i4_codec_level; |
256 | | |
257 | | /* Flag to enable/disable insertion of SPS, VPS & PPS at every CRA frame */ |
258 | 0 | ps_entropy_ctxt->i4_sps_at_cdr_enable = ps_init_prms->s_out_strm_prms.i4_sps_at_cdr_enable; |
259 | | |
260 | | /* Store Tile params base into entropy context */ |
261 | 0 | ps_entropy_ctxt->pv_tile_params_base = pv_tile_params_base; |
262 | |
|
263 | 0 | ps_entropy_ctxt->pv_sys_api = (void *)&ps_init_prms->s_sys_api; |
264 | |
|
265 | 0 | ps_entropy_ctxt->i4_slice_segment_mode = ps_init_prms->s_slice_params.i4_slice_segment_mode; |
266 | | |
267 | | /* Set slice segment length */ |
268 | 0 | if((ps_entropy_ctxt->i4_slice_segment_mode == 1) || |
269 | 0 | (ps_entropy_ctxt->i4_slice_segment_mode == 2)) |
270 | 0 | { |
271 | 0 | ps_entropy_ctxt->i4_slice_segment_max_length = |
272 | 0 | ps_init_prms->s_slice_params.i4_slice_segment_argument; |
273 | 0 | } |
274 | 0 | else |
275 | 0 | { |
276 | 0 | ps_entropy_ctxt->i4_slice_segment_max_length = 0; |
277 | 0 | } |
278 | | |
279 | | /* return the handle to caller */ |
280 | 0 | return ((void *)ps_entropy_ctxt); |
281 | 0 | } |
282 | | |
283 | | /** |
284 | | ****************************************************************************** |
285 | | * |
286 | | * @brief entry point for entropy coding of a frame |
287 | | * |
288 | | * @par Description |
289 | | * This function generates nal headers like SPS/PPS/slice header and call the |
290 | | * slice data entropy coding function |
291 | | * |
292 | | * @param[in] ps_enc_ctxt |
293 | | * pointer to encoder context (handle) |
294 | | * |
295 | | * @param[out] ps_curr_out |
296 | | * pointer to output data buffer context where bitstream is generated |
297 | | * |
298 | | * @param[out] ps_curr_inp |
299 | | * pointer to entropy input params context |
300 | | * |
301 | | * @return success or failure error code |
302 | | * |
303 | | ****************************************************************************** |
304 | | */ |
305 | | WORD32 ihevce_entropy_encode_frame( |
306 | | void *pv_entropy_hdl, |
307 | | iv_output_data_buffs_t *ps_curr_out, |
308 | | frm_proc_ent_cod_ctxt_t *ps_curr_inp, |
309 | | WORD32 i4_out_buf_size) |
310 | 0 | { |
311 | 0 | WORD32 ret = IHEVCE_SUCCESS; |
312 | 0 | WORD32 tile_ctr, total_tiles = 1; |
313 | 0 | entropy_context_t *ps_entropy_ctxt = (entropy_context_t *)pv_entropy_hdl; |
314 | | |
315 | | /* current frame slice type and nal type */ |
316 | 0 | WORD32 slice_type = ps_curr_inp->s_slice_hdr.i1_slice_type; |
317 | | |
318 | | /* current frame slice type and nal type */ |
319 | 0 | WORD32 nal_type = ps_curr_inp->i4_slice_nal_type; |
320 | | |
321 | | /* read vps, sps and pps from input params */ |
322 | 0 | vps_t *ps_vps = ps_curr_inp->ps_vps; |
323 | 0 | sps_t *ps_sps = ps_curr_inp->ps_sps; |
324 | 0 | pps_t *ps_pps = ps_curr_inp->ps_pps; |
325 | 0 | sei_params_t *ps_sei = &ps_curr_inp->s_sei; |
326 | 0 | ihevce_tile_params_t *ps_tile_params_base; |
327 | 0 | WORD32 out_buf_size = i4_out_buf_size; |
328 | | |
329 | | /* Headers are repeated once per IDR. Should be changed to every CRA */ |
330 | 0 | WORD32 insert_vps_sps_pps = |
331 | 0 | ((slice_type == ISLICE) && |
332 | 0 | (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type))); |
333 | |
|
334 | 0 | WORD32 insert_per_cra = |
335 | 0 | ((slice_type == ISLICE) && |
336 | 0 | (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type))); |
337 | 0 | bitstrm_t *ps_bitstrm = &ps_entropy_ctxt->s_bit_strm; |
338 | |
|
339 | 0 | ULWORD64 u8_bits_slice_header_prev; |
340 | |
|
341 | 0 | WORD32 i4_slice_segment_max_length_bckp; |
342 | 0 | WORD32 i4_max_num_slices; |
343 | |
|
344 | 0 | ihevce_sys_api_t *ps_sys_api = (ihevce_sys_api_t *)ps_entropy_ctxt->pv_sys_api; |
345 | |
|
346 | | #if POPULATE_NAL_OFFSET |
347 | | ULWORD64 u8_bitstream_base = (ULWORD64)ps_curr_out->pv_bitstream_bufs; |
348 | | #endif |
349 | 0 | if(0 == ps_entropy_ctxt->i4_sps_at_cdr_enable) |
350 | 0 | { |
351 | 0 | insert_vps_sps_pps = |
352 | 0 | ((slice_type == ISLICE) && ((NAL_IDR_N_LP == nal_type) || (NAL_IDR_W_LP == nal_type))); |
353 | 0 | } |
354 | | /* intialize vps, sps, pps, sei and slice header in entropy context */ |
355 | 0 | ps_entropy_ctxt->ps_vps = ps_vps; |
356 | 0 | ps_entropy_ctxt->ps_sps = ps_sps; |
357 | 0 | ps_entropy_ctxt->ps_pps = ps_pps; |
358 | 0 | ps_entropy_ctxt->ps_sei = ps_sei; |
359 | 0 | ps_entropy_ctxt->ps_slice_hdr = &ps_curr_inp->s_slice_hdr; |
360 | 0 | ps_entropy_ctxt->i4_is_cu_cbf_zero = 1; |
361 | |
|
362 | 0 | ps_entropy_ctxt->ps_pic_level_info = &ps_curr_inp->s_pic_level_info; |
363 | | |
364 | | /* intialize the frame level ctb pointer for current slice */ |
365 | 0 | ps_entropy_ctxt->ps_frm_ctb = ps_curr_inp->ps_frm_ctb_data; |
366 | | |
367 | | /* Initiallizing to indicate the start of frame */ |
368 | 0 | ps_entropy_ctxt->i4_next_slice_seg_x = 0; |
369 | 0 | ps_entropy_ctxt->i4_next_slice_seg_y = 0; |
370 | | |
371 | | /* enable the residue encode flag */ |
372 | 0 | ps_entropy_ctxt->i4_enable_res_encode = 1; |
373 | | |
374 | | /* Initialize the bitstream engine */ |
375 | 0 | ret |= ihevce_bitstrm_init(ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, out_buf_size); |
376 | | |
377 | | /* Reset Bitstream NAL counter */ |
378 | 0 | ps_bitstrm->i4_num_nal = 0; |
379 | | |
380 | | /*PIC INFO: Store the Bits before slice header is encoded*/ |
381 | 0 | u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8); |
382 | | |
383 | | /* generate AUD if enabled from the application */ |
384 | 0 | if(1 == ps_curr_inp->i1_aud_present_flag) |
385 | 0 | { |
386 | 0 | UWORD8 u1_pic_type; |
387 | |
|
388 | 0 | switch(slice_type) |
389 | 0 | { |
390 | 0 | case ISLICE: |
391 | 0 | u1_pic_type = 0; |
392 | 0 | break; |
393 | 0 | case PSLICE: |
394 | 0 | u1_pic_type = 1; |
395 | 0 | break; |
396 | 0 | default: |
397 | 0 | u1_pic_type = 2; |
398 | 0 | break; |
399 | 0 | } |
400 | | |
401 | 0 | ret |= ihevce_generate_aud(ps_bitstrm, u1_pic_type); |
402 | 0 | } |
403 | | |
404 | 0 | if(insert_vps_sps_pps) |
405 | 0 | { |
406 | | /* generate vps */ |
407 | 0 | ret |= ihevce_generate_vps(ps_bitstrm, ps_entropy_ctxt->ps_vps); |
408 | | |
409 | | /* generate sps */ |
410 | 0 | ret |= ihevce_generate_sps(ps_bitstrm, ps_entropy_ctxt->ps_sps); |
411 | | |
412 | | /* generate pps */ |
413 | 0 | ret |= ihevce_generate_pps(ps_bitstrm, ps_entropy_ctxt->ps_pps); |
414 | 0 | } |
415 | | |
416 | | /* generate sei */ |
417 | 0 | if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag) |
418 | 0 | { |
419 | 0 | WORD32 i4_insert_prefix_sei = |
420 | 0 | ps_entropy_ctxt->ps_sei->i1_buf_period_params_present_flag || |
421 | 0 | ps_entropy_ctxt->ps_sei->i1_pic_timing_params_present_flag || |
422 | 0 | ps_entropy_ctxt->ps_sei->i1_recovery_point_params_present_flag || |
423 | 0 | ps_entropy_ctxt->ps_sei->i4_sei_mastering_disp_colour_vol_params_present_flags || |
424 | 0 | ps_curr_inp->u4_num_sei_payload || ps_curr_inp->s_sei.i1_sei_cll_enable; |
425 | |
|
426 | 0 | if(i4_insert_prefix_sei) |
427 | 0 | { |
428 | 0 | ret |= ihevce_generate_sei( |
429 | 0 | ps_bitstrm, |
430 | 0 | ps_entropy_ctxt->ps_sei, |
431 | 0 | &ps_entropy_ctxt->ps_sps->s_vui_parameters, |
432 | 0 | insert_per_cra, |
433 | 0 | NAL_PREFIX_SEI, |
434 | 0 | ps_curr_inp->u4_num_sei_payload, |
435 | 0 | &ps_curr_inp->as_sei_payload[0]); |
436 | 0 | } |
437 | 0 | } |
438 | | |
439 | | /*PIC INFO: Populate slice header bits */ |
440 | 0 | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header += |
441 | 0 | (ps_bitstrm->u4_strm_buf_offset * 8) - u8_bits_slice_header_prev; |
442 | |
|
443 | 0 | ps_tile_params_base = (ihevce_tile_params_t *)ps_entropy_ctxt->pv_tile_params_base; |
444 | |
|
445 | 0 | ps_curr_out->i4_bytes_generated = 0; //Init |
446 | | |
447 | | /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/ |
448 | 0 | { |
449 | 0 | WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal; |
450 | 0 | WORD32 ctr = 0; |
451 | |
|
452 | 0 | ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU); |
453 | | |
454 | 0 | ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals; |
455 | 0 | for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++) |
456 | 0 | { |
457 | | /* NAL offset is derive by subtracting Bistream base from NAL start pointer */ |
458 | 0 | ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr]; |
459 | |
|
460 | 0 | #if POPULATE_NAL_SIZE |
461 | | |
462 | | /* ----------Populate NAL Size -------------*/ |
463 | 0 | if((ctr + 1) < num_non_vcl_prefix_nals) |
464 | 0 | { |
465 | 0 | ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1]; |
466 | 0 | ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] = |
467 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
468 | 0 | } |
469 | 0 | else |
470 | 0 | { |
471 | 0 | ULWORD64 u8_next_nal_start = |
472 | 0 | (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset; |
473 | 0 | ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] = |
474 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
475 | 0 | } |
476 | 0 | ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0); |
477 | |
|
478 | | #elif POPULATE_NAL_OFFSET |
479 | | |
480 | | /* ----------Populate NAL Offset -------------*/ |
481 | | |
482 | | ASSERT(u8_cur_nal_start >= u8_bitstream_base); |
483 | | ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] = |
484 | | (UWORD32)(u8_cur_nal_start - u8_bitstream_base); |
485 | | |
486 | | if(ctr) |
487 | | { |
488 | | /* sanity check on increasing NAL offsets */ |
489 | | ASSERT( |
490 | | ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] > |
491 | | ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]); |
492 | | } |
493 | | #endif /* POPULATE_NAL_SIZE */ |
494 | 0 | } |
495 | 0 | } |
496 | | |
497 | 0 | total_tiles = ps_tile_params_base->i4_num_tiles; |
498 | | |
499 | | /* frame level NUM slices related params initialisations */ |
500 | 0 | { |
501 | 0 | WORD32 codec_level_index = ihevce_get_level_index(ps_entropy_ctxt->i4_codec_level); |
502 | |
|
503 | 0 | i4_max_num_slices = g_as_level_data[codec_level_index].i4_max_slices_per_picture; |
504 | 0 | ps_entropy_ctxt->i4_num_slice_seg = 0; |
505 | 0 | } |
506 | | |
507 | | /* back up slice arg length before pic encoding */ |
508 | 0 | i4_slice_segment_max_length_bckp = ps_entropy_ctxt->i4_slice_segment_max_length; |
509 | |
|
510 | 0 | for(tile_ctr = 0; tile_ctr < total_tiles; tile_ctr++) |
511 | 0 | { |
512 | 0 | WORD32 i4_end_of_slice = 0; |
513 | | |
514 | | /* Loop over all the slice segments */ |
515 | 0 | while(0 == i4_end_of_slice) |
516 | 0 | { |
517 | 0 | WORD32 i4_bytes_generated, i4_slice_header_bits; |
518 | | |
519 | | /*PIC INFO: Store the Bits before slice header is encoded*/ |
520 | 0 | u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8); |
521 | | |
522 | | /* generate slice header */ |
523 | 0 | ret |= ihevce_generate_slice_header( |
524 | 0 | ps_bitstrm, |
525 | 0 | nal_type, |
526 | 0 | ps_entropy_ctxt->ps_slice_hdr, |
527 | 0 | ps_entropy_ctxt->ps_pps, |
528 | 0 | ps_entropy_ctxt->ps_sps, |
529 | 0 | &ps_entropy_ctxt->s_dup_bit_strm_ent_offset, |
530 | 0 | &ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset, |
531 | 0 | (ps_tile_params_base + tile_ctr), |
532 | 0 | ps_entropy_ctxt->i4_next_slice_seg_x, |
533 | 0 | ps_entropy_ctxt->i4_next_slice_seg_y); |
534 | |
|
535 | 0 | i4_slice_header_bits = |
536 | 0 | (ps_bitstrm->u4_strm_buf_offset * 8) - (WORD32)u8_bits_slice_header_prev; |
537 | | |
538 | | /* Update slice segment length with bytes in slice header */ |
539 | 0 | if(2 == ps_entropy_ctxt->i4_slice_segment_mode) |
540 | 0 | { |
541 | 0 | ps_entropy_ctxt->i4_slice_seg_len = (i4_slice_header_bits / 8); |
542 | 0 | } |
543 | 0 | else //Initiallize to zero |
544 | 0 | { |
545 | 0 | ps_entropy_ctxt->i4_slice_seg_len = 0; |
546 | 0 | } |
547 | | |
548 | | /*PIC INFO: Populate slice header bits */ |
549 | 0 | ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header += |
550 | 0 | i4_slice_header_bits; |
551 | | |
552 | | /* check if number of slices generated in is MAX -1 as per codec_level */ |
553 | 0 | if(ps_entropy_ctxt->i4_num_slice_seg == (i4_max_num_slices - 1)) |
554 | 0 | { |
555 | | /* i4_slice_segment_max_length is set to a huge positive value */ |
556 | | /* so that remaining CTBS in the picture gets encoded as a single slice */ |
557 | 0 | ps_entropy_ctxt->i4_slice_segment_max_length = 0x7FFFFFFF; |
558 | 0 | } |
559 | | |
560 | | /* encode the slice data */ |
561 | 0 | ret |= ihevce_encode_slice_data( |
562 | 0 | ps_entropy_ctxt, (ps_tile_params_base + tile_ctr), &i4_end_of_slice); |
563 | | |
564 | | /* increment the number of slices generated */ |
565 | 0 | ps_entropy_ctxt->i4_num_slice_seg++; |
566 | |
|
567 | 0 | if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag) |
568 | 0 | { |
569 | | /*after encoding is done each slice offset is available. Enter these offset in slice header*/ |
570 | 0 | ihevce_insert_entry_offset_slice_header( |
571 | 0 | &ps_entropy_ctxt->s_dup_bit_strm_ent_offset, |
572 | 0 | ps_entropy_ctxt->ps_slice_hdr, |
573 | 0 | ps_entropy_ctxt->ps_pps, |
574 | 0 | ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset); |
575 | 0 | } |
576 | | |
577 | | /* compute the bytes generated and return */ |
578 | 0 | if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag) |
579 | 0 | { |
580 | 0 | i4_bytes_generated = ps_entropy_ctxt->s_dup_bit_strm_ent_offset.u4_strm_buf_offset; |
581 | 0 | } |
582 | 0 | else |
583 | 0 | { |
584 | 0 | i4_bytes_generated = ps_entropy_ctxt->s_cabac_ctxt.u4_strm_buf_offset; |
585 | 0 | } |
586 | | |
587 | | /* Updating bytes generated and Updating strm_buffer pointer */ |
588 | 0 | ps_curr_out->i4_bytes_generated += i4_bytes_generated; |
589 | | |
590 | | /* Re-Initialize the bitstream engine after each tile or slice */ |
591 | 0 | ihevce_bitstrm_init( |
592 | 0 | ps_bitstrm, (ps_bitstrm->pu1_strm_buffer + i4_bytes_generated), out_buf_size); |
593 | 0 | } |
594 | 0 | } |
595 | | |
596 | | /* Max slices related warning prints based on last slice status */ |
597 | 0 | if(ps_entropy_ctxt->i4_num_slice_seg == i4_max_num_slices) |
598 | 0 | { |
599 | 0 | if(ps_entropy_ctxt->i4_slice_seg_len >= i4_slice_segment_max_length_bckp) |
600 | 0 | { |
601 | 0 | if(1 == ps_entropy_ctxt->i4_slice_segment_mode) |
602 | 0 | { |
603 | 0 | ps_sys_api->ihevce_printf( |
604 | 0 | ps_sys_api->pv_cb_handle, |
605 | 0 | "IHEVCE_WARNING: Last slice contains %d CTBs exceeds %d (Max limit of CTBs " |
606 | 0 | "configured). As per codec_level max number of slices per frame is %d\n", |
607 | 0 | ps_entropy_ctxt->i4_slice_seg_len, |
608 | 0 | i4_slice_segment_max_length_bckp, |
609 | 0 | i4_max_num_slices); |
610 | 0 | } |
611 | 0 | else if(2 == ps_entropy_ctxt->i4_slice_segment_mode) |
612 | 0 | { |
613 | 0 | ps_sys_api->ihevce_printf( |
614 | 0 | ps_sys_api->pv_cb_handle, |
615 | 0 | "IHEVCE_WARNING: Last slice contains %d Bytes exceeds %d (Max limit of Bytes " |
616 | 0 | "configured). As per codec_level max number of slices per frame is %d\n", |
617 | 0 | ps_entropy_ctxt->i4_slice_seg_len, |
618 | 0 | i4_slice_segment_max_length_bckp, |
619 | 0 | i4_max_num_slices); |
620 | 0 | } |
621 | 0 | } |
622 | 0 | } |
623 | | |
624 | | /* restore slice arg length after pic encoding */ |
625 | 0 | ps_entropy_ctxt->i4_slice_segment_max_length = i4_slice_segment_max_length_bckp; |
626 | | |
627 | | /* ---------------------- Initialize VCL NAL Size/offsets ---------------------------*/ |
628 | 0 | { |
629 | 0 | WORD32 vcl_start = ps_curr_out->i4_num_non_vcl_prefix_nals; |
630 | 0 | WORD32 num_vcl_nals = ps_bitstrm->i4_num_nal - vcl_start; |
631 | 0 | WORD32 ctr = 0; |
632 | |
|
633 | 0 | ASSERT(num_vcl_nals > 0); |
634 | 0 | ASSERT(num_vcl_nals <= MAX_NUM_VCL_NALS_PER_AU); |
635 | | |
636 | 0 | ps_curr_out->i4_num_vcl_nals = num_vcl_nals; |
637 | 0 | for(ctr = 0; ctr < MIN(num_vcl_nals, MAX_NUM_VCL_NALS_PER_AU); ctr++) |
638 | 0 | { |
639 | | /* NAL offset is derive by subtracting Bistream base from NAL start pointer */ |
640 | 0 | ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start]; |
641 | |
|
642 | 0 | #if POPULATE_NAL_SIZE |
643 | | |
644 | | /* ----------Populate NAL Size -------------*/ |
645 | 0 | if((ctr + 1) < num_vcl_nals) |
646 | 0 | { |
647 | 0 | ULWORD64 u8_next_nal_start = |
648 | 0 | (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start + 1]; |
649 | 0 | ps_curr_out->ai4_size_vcl_nals[ctr] = |
650 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
651 | 0 | } |
652 | 0 | else |
653 | 0 | { |
654 | 0 | ULWORD64 u8_next_nal_start = |
655 | 0 | (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset; |
656 | 0 | ps_curr_out->ai4_size_vcl_nals[ctr] = |
657 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
658 | 0 | } |
659 | 0 | ASSERT(ps_curr_out->ai4_size_vcl_nals[ctr] > 0); |
660 | |
|
661 | | #elif POPULATE_NAL_OFFSET |
662 | | |
663 | | /* ----------Populate NAL Offset -------------*/ |
664 | | |
665 | | ASSERT(u8_cur_nal_start >= u8_bitstream_base); |
666 | | ps_curr_out->ai4_off_vcl_nals[ctr] = (UWORD32)(u8_cur_nal_start - u8_bitstream_base); |
667 | | |
668 | | if(ctr) |
669 | | { |
670 | | /* sanity check on increasing NAL offsets */ |
671 | | ASSERT(ps_curr_out->ai4_off_vcl_nals[ctr] > ps_curr_out->ai4_off_vcl_nals[ctr - 1]); |
672 | | } |
673 | | #endif /* POPULATE_NAL_SIZE */ |
674 | 0 | } |
675 | 0 | } |
676 | | |
677 | | /* generate suffix sei */ |
678 | 0 | if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag) |
679 | 0 | { |
680 | | /* Insert hash SEI */ |
681 | 0 | if(0 != ps_entropy_ctxt->ps_sei->i1_decoded_pic_hash_sei_flag) |
682 | 0 | { |
683 | 0 | ret |= ihevce_generate_sei( |
684 | 0 | ps_bitstrm, |
685 | 0 | ps_entropy_ctxt->ps_sei, |
686 | 0 | &ps_entropy_ctxt->ps_sps->s_vui_parameters, |
687 | 0 | insert_per_cra, |
688 | 0 | NAL_SUFFIX_SEI, |
689 | 0 | ps_curr_inp->u4_num_sei_payload, |
690 | 0 | &ps_curr_inp->as_sei_payload[0]); |
691 | 0 | } |
692 | | |
693 | | /* Updating bytes generated */ |
694 | 0 | ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset; |
695 | 0 | } |
696 | | |
697 | | /* generate end of sequence nal */ |
698 | 0 | if((1 == ps_curr_inp->i1_eos_present_flag) && (ps_curr_inp->i4_is_end_of_idr_gop == 1)) |
699 | 0 | { |
700 | 0 | ret |= ihevce_generate_eos(ps_bitstrm); |
701 | | /* Updating bytes generated */ |
702 | 0 | ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset; |
703 | 0 | } |
704 | | |
705 | | /* ------------------- Initialize non-VCL suffix NAL Size/offsets -----------------------*/ |
706 | 0 | { |
707 | 0 | WORD32 non_vcl_suffix_start = |
708 | 0 | ps_curr_out->i4_num_non_vcl_prefix_nals + ps_curr_out->i4_num_vcl_nals; |
709 | 0 | WORD32 num_non_vcl_suffix_nals = ps_bitstrm->i4_num_nal - non_vcl_suffix_start; |
710 | 0 | WORD32 ctr = 0; |
711 | |
|
712 | 0 | ASSERT(num_non_vcl_suffix_nals >= 0); |
713 | 0 | ASSERT(num_non_vcl_suffix_nals <= MAX_NUM_SUFFIX_NALS_PER_AU); |
714 | | |
715 | 0 | ps_curr_out->i4_num_non_vcl_suffix_nals = num_non_vcl_suffix_nals; |
716 | 0 | for(ctr = 0; ctr < MIN(num_non_vcl_suffix_nals, MAX_NUM_SUFFIX_NALS_PER_AU); ctr++) |
717 | 0 | { |
718 | | /* NAL offset is derive by subtracting Bistream base from NAL start pointer */ |
719 | 0 | ULWORD64 u8_cur_nal_start = |
720 | 0 | (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start]; |
721 | |
|
722 | 0 | #if POPULATE_NAL_SIZE |
723 | | |
724 | | /* ----------Populate NAL Size -------------*/ |
725 | 0 | if((ctr + 1) < num_non_vcl_suffix_nals) |
726 | 0 | { |
727 | 0 | ULWORD64 u8_next_nal_start = |
728 | 0 | (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start + 1]; |
729 | 0 | ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] = |
730 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
731 | 0 | } |
732 | 0 | else |
733 | 0 | { |
734 | 0 | ULWORD64 u8_next_nal_start = |
735 | 0 | (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset; |
736 | 0 | ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] = |
737 | 0 | (UWORD32)(u8_next_nal_start - u8_cur_nal_start); |
738 | 0 | } |
739 | 0 | ASSERT(ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] > 0); |
740 | |
|
741 | | #elif POPULATE_NAL_OFFSET |
742 | | |
743 | | /* ----------Populate NAL Offset -------------*/ |
744 | | |
745 | | ASSERT(u8_cur_nal_start >= u8_bitstream_base); |
746 | | ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] = |
747 | | (UWORD32)(u8_cur_nal_start - u8_bitstream_base); |
748 | | |
749 | | if(ctr) |
750 | | { |
751 | | /* sanity check on increasing NAL offsets */ |
752 | | ASSERT( |
753 | | ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] > |
754 | | ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr - 1]); |
755 | | } |
756 | | #endif /* POPULATE_NAL_SIZE */ |
757 | 0 | } |
758 | 0 | } |
759 | | |
760 | | /*PIC INFO: Populatinf Ref POC, weights and offset*/ |
761 | 0 | { |
762 | 0 | WORD32 i; |
763 | 0 | ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l0_active = |
764 | 0 | ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active; |
765 | 0 | ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l1_active = |
766 | 0 | ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active; |
767 | 0 | for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active; i++) |
768 | 0 | { |
769 | 0 | ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l0[i] = |
770 | 0 | ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l0[i]; |
771 | 0 | ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l0[i] = |
772 | 0 | ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l0[i]; |
773 | 0 | ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l0[i] = |
774 | 0 | (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l0[i] / |
775 | 0 | (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom); |
776 | 0 | ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l0[i] = |
777 | 0 | ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l0[i]; |
778 | 0 | } |
779 | 0 | for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active; i++) |
780 | 0 | { |
781 | 0 | ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l1[i] = |
782 | 0 | ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l1[i]; |
783 | 0 | ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l1[i] = |
784 | 0 | ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l1[i]; |
785 | 0 | ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l1[i] = |
786 | 0 | (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l1[i] / |
787 | 0 | (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom); |
788 | 0 | ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l1[i] = |
789 | 0 | ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l1[i]; |
790 | 0 | } |
791 | 0 | } |
792 | | |
793 | | /* attach the time stamp of the input to output */ |
794 | 0 | ps_curr_out->i4_out_timestamp_low = ps_curr_inp->i4_inp_timestamp_low; |
795 | |
|
796 | 0 | ps_curr_out->i4_out_timestamp_high = ps_curr_inp->i4_inp_timestamp_high; |
797 | | |
798 | | /*attach the app frame info of this buffer */ |
799 | 0 | ps_curr_out->pv_app_frm_ctxt = ps_curr_inp->pv_app_frm_ctxt; |
800 | | |
801 | | /* frame never skipped for now */ |
802 | 0 | ps_curr_out->i4_frame_skipped = 0; |
803 | | |
804 | | /* update error code and return */ |
805 | 0 | ps_curr_out->i4_process_error_code = ret; |
806 | |
|
807 | 0 | switch(slice_type) |
808 | 0 | { |
809 | 0 | case ISLICE: |
810 | 0 | if((nal_type == NAL_IDR_N_LP) || (NAL_IDR_W_LP == nal_type)) |
811 | 0 | { |
812 | 0 | ps_curr_out->i4_encoded_frame_type = IV_IDR_FRAME; |
813 | 0 | } |
814 | 0 | else |
815 | 0 | { |
816 | 0 | ps_curr_out->i4_encoded_frame_type = IV_I_FRAME; |
817 | 0 | } |
818 | 0 | break; |
819 | 0 | case PSLICE: |
820 | 0 | ps_curr_out->i4_encoded_frame_type = IV_P_FRAME; |
821 | 0 | break; |
822 | 0 | case BSLICE: |
823 | 0 | ps_curr_out->i4_encoded_frame_type = IV_B_FRAME; |
824 | 0 | break; |
825 | 0 | } |
826 | | |
827 | 0 | if(IHEVCE_SUCCESS == ret) |
828 | 0 | { |
829 | 0 | ps_curr_out->i4_process_ret_sts = IV_SUCCESS; |
830 | 0 | } |
831 | 0 | else |
832 | 0 | { |
833 | 0 | ps_curr_out->i4_process_ret_sts = IV_FAIL; |
834 | 0 | } |
835 | |
|
836 | 0 | return (ret); |
837 | 0 | } |