Coverage Report

Created: 2025-07-11 06:43

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