Coverage Report

Created: 2025-12-14 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_entropy_interface.c
Line
Count
Source
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
14.0k
{
123
14.0k
    return (NUM_ENTROPY_MEM_RECS);
124
14.0k
}
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
7.03k
{
174
    /* memories should be requested assuming worst case requirememnts */
175
7.03k
    WORD32 max_width = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_width;
176
7.03k
    WORD32 max_height = ps_init_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_height;
177
7.03k
    WORD32 max_align_width = ALIGN64(max_width);
178
7.03k
    WORD32 max_align_height = ALIGN64(max_height);
179
180
    /* Module context structure */
181
7.03k
    ps_mem_tab[ENTROPY_CTXT].i4_mem_size = sizeof(entropy_context_t);
182
7.03k
    ps_mem_tab[ENTROPY_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
183
7.03k
    ps_mem_tab[ENTROPY_CTXT].i4_mem_alignment = 64;
184
185
    /* top row cu skip flags (1 bit per 8x8CU)  */
186
7.03k
    ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_size = max_align_width >> 6;
187
7.03k
    ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
188
7.03k
    ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].i4_mem_alignment = 64;
189
190
    /* top row CU Depth (1 byte per 8x8CU) */
191
7.03k
    ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_size = (max_align_width >> 3);
192
7.03k
    ps_mem_tab[ENTROPY_TOP_CU_DEPTH].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
193
7.03k
    ps_mem_tab[ENTROPY_TOP_CU_DEPTH].i4_mem_alignment = 64;
194
195
    /* Dummy_buffer to handle first pass MBR case*/
196
7.03k
    ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_size = (max_align_width * max_align_height * 2);
197
7.03k
    ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
198
7.03k
    ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].i4_mem_alignment = 64;
199
200
7.03k
    return (NUM_ENTROPY_MEM_RECS);
201
7.03k
}
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
7.03k
{
229
7.03k
    entropy_context_t *ps_entropy_ctxt;
230
231
    /* Entropy state structure */
232
7.03k
    ps_entropy_ctxt = (entropy_context_t *)ps_mem_tab[ENTROPY_CTXT].pv_base;
233
7.03k
    memset(ps_entropy_ctxt, 0, sizeof(entropy_context_t));
234
235
7.03k
    ps_entropy_ctxt->pu1_skip_cu_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_SKIP_FLAGS].pv_base;
236
7.03k
    ps_entropy_ctxt->pu1_cu_depth_top = (UWORD8 *)ps_mem_tab[ENTROPY_TOP_CU_DEPTH].pv_base;
237
7.03k
    ps_entropy_ctxt->pv_dummy_out_buf = ps_mem_tab[ENTROPY_DUMMY_OUT_BUF].pv_base;
238
7.03k
    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
7.03k
    ps_entropy_ctxt->apu1_cbf_cb[0] = &ps_entropy_ctxt->au1_cbf_cb[0][1];
247
7.03k
    ps_entropy_ctxt->apu1_cbf_cr[0] = &ps_entropy_ctxt->au1_cbf_cr[0][1];
248
7.03k
    ps_entropy_ctxt->apu1_cbf_cb[1] = &ps_entropy_ctxt->au1_cbf_cb[1][1];
249
7.03k
    ps_entropy_ctxt->apu1_cbf_cr[1] = &ps_entropy_ctxt->au1_cbf_cr[1][1];
250
251
7.03k
    memset(ps_entropy_ctxt->au1_cbf_cb, 0, (MAX_TFR_DEPTH + 1) * 2 * sizeof(UWORD8));
252
253
    /* register codec level */
254
7.03k
    ps_entropy_ctxt->i4_codec_level =
255
7.03k
        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
7.03k
    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
7.03k
    ps_entropy_ctxt->pv_tile_params_base = pv_tile_params_base;
262
263
7.03k
    ps_entropy_ctxt->pv_sys_api = (void *)&ps_init_prms->s_sys_api;
264
265
7.03k
    ps_entropy_ctxt->i4_slice_segment_mode = ps_init_prms->s_slice_params.i4_slice_segment_mode;
266
267
    /* Set slice segment length */
268
7.03k
    if((ps_entropy_ctxt->i4_slice_segment_mode == 1) ||
269
7.03k
       (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
7.03k
    else
275
7.03k
    {
276
7.03k
        ps_entropy_ctxt->i4_slice_segment_max_length = 0;
277
7.03k
    }
278
279
    /* return the handle to caller */
280
7.03k
    return ((void *)ps_entropy_ctxt);
281
7.03k
}
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
104k
{
311
104k
    WORD32 ret = IHEVCE_SUCCESS;
312
104k
    WORD32 tile_ctr, total_tiles = 1;
313
104k
    entropy_context_t *ps_entropy_ctxt = (entropy_context_t *)pv_entropy_hdl;
314
315
    /* current frame slice type and nal type */
316
104k
    WORD32 slice_type = ps_curr_inp->s_slice_hdr.i1_slice_type;
317
318
    /* current frame slice type and nal type */
319
104k
    WORD32 nal_type = ps_curr_inp->i4_slice_nal_type;
320
321
    /* read vps, sps and pps from input params */
322
104k
    vps_t *ps_vps = ps_curr_inp->ps_vps;
323
104k
    sps_t *ps_sps = ps_curr_inp->ps_sps;
324
104k
    pps_t *ps_pps = ps_curr_inp->ps_pps;
325
104k
#ifndef DISABLE_SEI
326
104k
    sei_params_t *ps_sei = &ps_curr_inp->s_sei;
327
104k
#endif
328
104k
    ihevce_tile_params_t *ps_tile_params_base;
329
104k
    WORD32 out_buf_size = i4_out_buf_size;
330
331
    /* Headers are repeated once per IDR. Should be changed to every CRA */
332
104k
    WORD32 insert_vps_sps_pps =
333
104k
        ((slice_type == ISLICE) &&
334
35.3k
         (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
335
336
104k
    WORD32 insert_per_cra =
337
104k
        ((slice_type == ISLICE) &&
338
35.3k
         (((NAL_IDR_N_LP == nal_type) || (NAL_CRA == nal_type)) || (NAL_IDR_W_LP == nal_type)));
339
104k
    bitstrm_t *ps_bitstrm = &ps_entropy_ctxt->s_bit_strm;
340
341
104k
    ULWORD64 u8_bits_slice_header_prev;
342
343
104k
    WORD32 i4_slice_segment_max_length_bckp;
344
104k
    WORD32 i4_max_num_slices;
345
346
104k
    ihevce_sys_api_t *ps_sys_api = (ihevce_sys_api_t *)ps_entropy_ctxt->pv_sys_api;
347
348
#if POPULATE_NAL_OFFSET
349
    ULWORD64 u8_bitstream_base = (ULWORD64)ps_curr_out->pv_bitstream_bufs;
350
#endif
351
104k
    if(0 == ps_entropy_ctxt->i4_sps_at_cdr_enable)
352
68.5k
    {
353
68.5k
        insert_vps_sps_pps =
354
68.5k
            ((slice_type == ISLICE) && ((NAL_IDR_N_LP == nal_type) || (NAL_IDR_W_LP == nal_type)));
355
68.5k
    }
356
    /* intialize vps, sps, pps, sei and slice header in entropy context */
357
104k
    ps_entropy_ctxt->ps_vps = ps_vps;
358
104k
    ps_entropy_ctxt->ps_sps = ps_sps;
359
104k
    ps_entropy_ctxt->ps_pps = ps_pps;
360
104k
#ifndef DISABLE_SEI
361
104k
    ps_entropy_ctxt->ps_sei = ps_sei;
362
104k
#endif
363
104k
    ps_entropy_ctxt->ps_slice_hdr = &ps_curr_inp->s_slice_hdr;
364
104k
    ps_entropy_ctxt->i4_is_cu_cbf_zero = 1;
365
366
104k
    ps_entropy_ctxt->ps_pic_level_info = &ps_curr_inp->s_pic_level_info;
367
368
    /* intialize the frame level ctb pointer for current slice */
369
104k
    ps_entropy_ctxt->ps_frm_ctb = ps_curr_inp->ps_frm_ctb_data;
370
371
    /* Initiallizing to indicate the start of frame */
372
104k
    ps_entropy_ctxt->i4_next_slice_seg_x = 0;
373
104k
    ps_entropy_ctxt->i4_next_slice_seg_y = 0;
374
375
    /* enable the residue encode flag */
376
104k
    ps_entropy_ctxt->i4_enable_res_encode = 1;
377
378
    /* Initialize the bitstream engine */
379
104k
    ret |= ihevce_bitstrm_init(ps_bitstrm, (UWORD8 *)ps_curr_out->pv_bitstream_bufs, out_buf_size);
380
381
    /* Reset Bitstream NAL counter */
382
104k
    ps_bitstrm->i4_num_nal = 0;
383
384
    /*PIC INFO: Store the Bits before slice header is encoded*/
385
104k
    u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
386
387
    /* generate AUD if enabled from the application */
388
104k
    if(1 == ps_curr_inp->i1_aud_present_flag)
389
0
    {
390
0
        UWORD8 u1_pic_type;
391
392
0
        switch(slice_type)
393
0
        {
394
0
        case ISLICE:
395
0
            u1_pic_type = 0;
396
0
            break;
397
0
        case PSLICE:
398
0
            u1_pic_type = 1;
399
0
            break;
400
0
        default:
401
0
            u1_pic_type = 2;
402
0
            break;
403
0
        }
404
405
0
        ret |= ihevce_generate_aud(ps_bitstrm, u1_pic_type);
406
0
    }
407
408
104k
    if(insert_vps_sps_pps)
409
26.6k
    {
410
        /* generate vps */
411
26.6k
        ret |= ihevce_generate_vps(ps_bitstrm, ps_entropy_ctxt->ps_vps);
412
413
        /* generate sps */
414
26.6k
        ret |= ihevce_generate_sps(ps_bitstrm, ps_entropy_ctxt->ps_sps);
415
416
        /* generate pps */
417
26.6k
        ret |= ihevce_generate_pps(ps_bitstrm, ps_entropy_ctxt->ps_pps);
418
26.6k
    }
419
420
104k
#ifndef DISABLE_SEI
421
    /* generate sei */
422
104k
    if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
423
31.9k
    {
424
31.9k
        WORD32 i4_insert_prefix_sei =
425
31.9k
            ps_entropy_ctxt->ps_sei->i1_buf_period_params_present_flag ||
426
31.9k
            ps_entropy_ctxt->ps_sei->i1_pic_timing_params_present_flag ||
427
31.9k
            ps_entropy_ctxt->ps_sei->i1_recovery_point_params_present_flag ||
428
31.9k
            ps_entropy_ctxt->ps_sei->i4_sei_mastering_disp_colour_vol_params_present_flags ||
429
31.9k
            ps_curr_inp->u4_num_sei_payload || ps_curr_inp->s_sei.i1_sei_cll_enable;
430
431
31.9k
        if(i4_insert_prefix_sei)
432
0
        {
433
0
            ret |= ihevce_generate_sei(
434
0
                ps_bitstrm,
435
0
                ps_entropy_ctxt->ps_sei,
436
0
                &ps_entropy_ctxt->ps_sps->s_vui_parameters,
437
0
                insert_per_cra,
438
0
                NAL_PREFIX_SEI,
439
0
                ps_curr_inp->u4_num_sei_payload,
440
0
                &ps_curr_inp->as_sei_payload[0]);
441
0
        }
442
31.9k
    }
443
104k
#endif
444
445
    /*PIC INFO: Populate slice header bits */
446
104k
    ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
447
104k
        (ps_bitstrm->u4_strm_buf_offset * 8) - u8_bits_slice_header_prev;
448
449
104k
    ps_tile_params_base = (ihevce_tile_params_t *)ps_entropy_ctxt->pv_tile_params_base;
450
451
104k
    ps_curr_out->i4_bytes_generated = 0;  //Init
452
453
    /* ------------------- Initialize non-VCL prefix NAL Size/offsets --------------------*/
454
104k
    {
455
104k
        WORD32 num_non_vcl_prefix_nals = ps_bitstrm->i4_num_nal;
456
104k
        WORD32 ctr = 0;
457
458
104k
        ASSERT(num_non_vcl_prefix_nals <= MAX_NUM_PREFIX_NALS_PER_AU);
459
460
104k
        ps_curr_out->i4_num_non_vcl_prefix_nals = num_non_vcl_prefix_nals;
461
184k
        for(ctr = 0; ctr < MIN(num_non_vcl_prefix_nals, MAX_NUM_PREFIX_NALS_PER_AU); ctr++)
462
80.0k
        {
463
            /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
464
80.0k
            ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr];
465
466
80.0k
#if POPULATE_NAL_SIZE
467
468
            /* ----------Populate NAL Size  -------------*/
469
80.0k
            if((ctr + 1) < num_non_vcl_prefix_nals)
470
53.3k
            {
471
53.3k
                ULWORD64 u8_next_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + 1];
472
53.3k
                ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
473
53.3k
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
474
53.3k
            }
475
26.6k
            else
476
26.6k
            {
477
26.6k
                ULWORD64 u8_next_nal_start =
478
26.6k
                    (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
479
26.6k
                ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] =
480
26.6k
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
481
26.6k
            }
482
80.0k
            ASSERT(ps_curr_out->ai4_size_non_vcl_prefix_nals[ctr] > 0);
483
484
#elif POPULATE_NAL_OFFSET
485
486
            /* ----------Populate NAL Offset  -------------*/
487
488
            ASSERT(u8_cur_nal_start >= u8_bitstream_base);
489
            ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] =
490
                (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
491
492
            if(ctr)
493
            {
494
                /* sanity check on increasing NAL offsets */
495
                ASSERT(
496
                    ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr] >
497
                    ps_curr_out->ai4_off_non_vcl_prefix_nals[ctr - 1]);
498
            }
499
#endif /* POPULATE_NAL_SIZE */
500
80.0k
        }
501
104k
    }
502
503
104k
    total_tiles = ps_tile_params_base->i4_num_tiles;
504
505
    /* frame level NUM slices related params initialisations */
506
104k
    {
507
104k
        WORD32 codec_level_index = ihevce_get_level_index(ps_entropy_ctxt->i4_codec_level);
508
509
104k
        i4_max_num_slices = g_as_level_data[codec_level_index].i4_max_slices_per_picture;
510
104k
        ps_entropy_ctxt->i4_num_slice_seg = 0;
511
104k
    }
512
513
    /* back up slice arg length before pic encoding */
514
104k
    i4_slice_segment_max_length_bckp = ps_entropy_ctxt->i4_slice_segment_max_length;
515
516
209k
    for(tile_ctr = 0; tile_ctr < total_tiles; tile_ctr++)
517
104k
    {
518
104k
        WORD32 i4_end_of_slice = 0;
519
520
        /* Loop over all the slice segments */
521
209k
        while(0 == i4_end_of_slice)
522
104k
        {
523
104k
            WORD32 i4_bytes_generated, i4_slice_header_bits;
524
525
            /*PIC INFO: Store the Bits before slice header is encoded*/
526
104k
            u8_bits_slice_header_prev = (ps_bitstrm->u4_strm_buf_offset * 8);
527
528
            /* generate slice header */
529
104k
            ret |= ihevce_generate_slice_header(
530
104k
                ps_bitstrm,
531
104k
                nal_type,
532
104k
                ps_entropy_ctxt->ps_slice_hdr,
533
104k
                ps_entropy_ctxt->ps_pps,
534
104k
                ps_entropy_ctxt->ps_sps,
535
104k
                &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
536
104k
                &ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset,
537
104k
                (ps_tile_params_base + tile_ctr),
538
104k
                ps_entropy_ctxt->i4_next_slice_seg_x,
539
104k
                ps_entropy_ctxt->i4_next_slice_seg_y);
540
541
104k
            i4_slice_header_bits =
542
104k
                (ps_bitstrm->u4_strm_buf_offset * 8) - (WORD32)u8_bits_slice_header_prev;
543
544
            /* Update slice segment length with bytes in slice header */
545
104k
            if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
546
0
            {
547
0
                ps_entropy_ctxt->i4_slice_seg_len = (i4_slice_header_bits / 8);
548
0
            }
549
104k
            else  //Initiallize to zero
550
104k
            {
551
104k
                ps_entropy_ctxt->i4_slice_seg_len = 0;
552
104k
            }
553
554
            /*PIC INFO: Populate slice header bits */
555
104k
            ps_entropy_ctxt->ps_pic_level_info->u8_bits_estimated_slice_header +=
556
104k
                i4_slice_header_bits;
557
558
            /* check if number of slices generated in is MAX -1 as per codec_level */
559
104k
            if(ps_entropy_ctxt->i4_num_slice_seg == (i4_max_num_slices - 1))
560
0
            {
561
                /* i4_slice_segment_max_length is set to a huge positive value          */
562
                /* so that remaining CTBS in the picture gets encoded as a single slice */
563
0
                ps_entropy_ctxt->i4_slice_segment_max_length = 0x7FFFFFFF;
564
0
            }
565
566
            /* encode the slice data */
567
104k
            ret |= ihevce_encode_slice_data(
568
104k
                ps_entropy_ctxt, (ps_tile_params_base + tile_ctr), &i4_end_of_slice);
569
570
            /* increment the number of slices generated */
571
104k
            ps_entropy_ctxt->i4_num_slice_seg++;
572
573
104k
            if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
574
2.82k
            {
575
                /*after encoding is done each slice offset is available. Enter these offset in slice header*/
576
2.82k
                ihevce_insert_entry_offset_slice_header(
577
2.82k
                    &ps_entropy_ctxt->s_dup_bit_strm_ent_offset,
578
2.82k
                    ps_entropy_ctxt->ps_slice_hdr,
579
2.82k
                    ps_entropy_ctxt->ps_pps,
580
2.82k
                    ps_entropy_ctxt->s_cabac_ctxt.u4_first_slice_start_offset);
581
2.82k
            }
582
583
            /* compute the bytes generated and return */
584
104k
            if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
585
2.82k
            {
586
2.82k
                i4_bytes_generated = ps_entropy_ctxt->s_dup_bit_strm_ent_offset.u4_strm_buf_offset;
587
2.82k
            }
588
101k
            else
589
101k
            {
590
101k
                i4_bytes_generated = ps_entropy_ctxt->s_cabac_ctxt.u4_strm_buf_offset;
591
101k
            }
592
593
            /* Updating bytes generated and Updating strm_buffer pointer */
594
104k
            ps_curr_out->i4_bytes_generated += i4_bytes_generated;
595
596
            /* Re-Initialize the bitstream engine after each tile or slice */
597
104k
            ihevce_bitstrm_init(
598
104k
                ps_bitstrm, (ps_bitstrm->pu1_strm_buffer + i4_bytes_generated), out_buf_size);
599
104k
        }
600
104k
    }
601
602
    /* Max slices related warning prints based on last slice status */
603
104k
    if(ps_entropy_ctxt->i4_num_slice_seg == i4_max_num_slices)
604
0
    {
605
0
        if(ps_entropy_ctxt->i4_slice_seg_len >= i4_slice_segment_max_length_bckp)
606
0
        {
607
0
            if(1 == ps_entropy_ctxt->i4_slice_segment_mode)
608
0
            {
609
0
                ps_sys_api->ihevce_printf(
610
0
                    ps_sys_api->pv_cb_handle,
611
0
                    "IHEVCE_WARNING: Last slice contains %d CTBs exceeds %d (Max limit of CTBs "
612
0
                    "configured). As per codec_level max number of slices per frame is %d\n",
613
0
                    ps_entropy_ctxt->i4_slice_seg_len,
614
0
                    i4_slice_segment_max_length_bckp,
615
0
                    i4_max_num_slices);
616
0
            }
617
0
            else if(2 == ps_entropy_ctxt->i4_slice_segment_mode)
618
0
            {
619
0
                ps_sys_api->ihevce_printf(
620
0
                    ps_sys_api->pv_cb_handle,
621
0
                    "IHEVCE_WARNING: Last slice contains %d Bytes exceeds %d (Max limit of Bytes "
622
0
                    "configured). As per codec_level max number of slices per frame is %d\n",
623
0
                    ps_entropy_ctxt->i4_slice_seg_len,
624
0
                    i4_slice_segment_max_length_bckp,
625
0
                    i4_max_num_slices);
626
0
            }
627
0
        }
628
0
    }
629
630
    /* restore slice arg length after pic encoding */
631
104k
    ps_entropy_ctxt->i4_slice_segment_max_length = i4_slice_segment_max_length_bckp;
632
633
    /* ---------------------- Initialize VCL NAL Size/offsets ---------------------------*/
634
104k
    {
635
104k
        WORD32 vcl_start = ps_curr_out->i4_num_non_vcl_prefix_nals;
636
104k
        WORD32 num_vcl_nals = ps_bitstrm->i4_num_nal - vcl_start;
637
104k
        WORD32 ctr = 0;
638
639
104k
        ASSERT(num_vcl_nals > 0);
640
104k
        ASSERT(num_vcl_nals <= MAX_NUM_VCL_NALS_PER_AU);
641
642
104k
        ps_curr_out->i4_num_vcl_nals = num_vcl_nals;
643
209k
        for(ctr = 0; ctr < MIN(num_vcl_nals, MAX_NUM_VCL_NALS_PER_AU); ctr++)
644
104k
        {
645
            /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
646
104k
            ULWORD64 u8_cur_nal_start = (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start];
647
648
104k
#if POPULATE_NAL_SIZE
649
650
            /* ----------Populate NAL Size  -------------*/
651
104k
            if((ctr + 1) < num_vcl_nals)
652
0
            {
653
0
                ULWORD64 u8_next_nal_start =
654
0
                    (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + vcl_start + 1];
655
0
                ps_curr_out->ai4_size_vcl_nals[ctr] =
656
0
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
657
0
            }
658
104k
            else
659
104k
            {
660
104k
                ULWORD64 u8_next_nal_start =
661
104k
                    (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
662
104k
                ps_curr_out->ai4_size_vcl_nals[ctr] =
663
104k
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
664
104k
            }
665
104k
            ASSERT(ps_curr_out->ai4_size_vcl_nals[ctr] > 0);
666
667
#elif POPULATE_NAL_OFFSET
668
669
            /* ----------Populate NAL Offset  -------------*/
670
671
            ASSERT(u8_cur_nal_start >= u8_bitstream_base);
672
            ps_curr_out->ai4_off_vcl_nals[ctr] = (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
673
674
            if(ctr)
675
            {
676
                /* sanity check on increasing NAL offsets */
677
                ASSERT(ps_curr_out->ai4_off_vcl_nals[ctr] > ps_curr_out->ai4_off_vcl_nals[ctr - 1]);
678
            }
679
#endif /* POPULATE_NAL_SIZE */
680
104k
        }
681
104k
    }
682
683
104k
#ifndef DISABLE_SEI
684
    /* generate suffix sei */
685
104k
    if(1 == ps_entropy_ctxt->ps_sei->i1_sei_parameters_present_flag)
686
31.9k
    {
687
        /* Insert hash SEI */
688
31.9k
        if(0 != ps_entropy_ctxt->ps_sei->i1_decoded_pic_hash_sei_flag)
689
0
        {
690
0
            ret |= ihevce_generate_sei(
691
0
                ps_bitstrm,
692
0
                ps_entropy_ctxt->ps_sei,
693
0
                &ps_entropy_ctxt->ps_sps->s_vui_parameters,
694
0
                insert_per_cra,
695
0
                NAL_SUFFIX_SEI,
696
0
                ps_curr_inp->u4_num_sei_payload,
697
0
                &ps_curr_inp->as_sei_payload[0]);
698
0
        }
699
700
        /* Updating bytes generated */
701
31.9k
        ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
702
31.9k
    }
703
104k
#endif
704
705
    /* generate end of sequence nal */
706
104k
    if((1 == ps_curr_inp->i1_eos_present_flag) && (ps_curr_inp->i4_is_end_of_idr_gop == 1))
707
0
    {
708
0
        ret |= ihevce_generate_eos(ps_bitstrm);
709
        /* Updating bytes generated */
710
0
        ps_curr_out->i4_bytes_generated += ps_bitstrm->u4_strm_buf_offset;
711
0
    }
712
713
    /* ------------------- Initialize non-VCL suffix NAL Size/offsets -----------------------*/
714
104k
    {
715
104k
        WORD32 non_vcl_suffix_start =
716
104k
            ps_curr_out->i4_num_non_vcl_prefix_nals + ps_curr_out->i4_num_vcl_nals;
717
104k
        WORD32 num_non_vcl_suffix_nals = ps_bitstrm->i4_num_nal - non_vcl_suffix_start;
718
104k
        WORD32 ctr = 0;
719
720
104k
        ASSERT(num_non_vcl_suffix_nals >= 0);
721
104k
        ASSERT(num_non_vcl_suffix_nals <= MAX_NUM_SUFFIX_NALS_PER_AU);
722
723
104k
        ps_curr_out->i4_num_non_vcl_suffix_nals = num_non_vcl_suffix_nals;
724
104k
        for(ctr = 0; ctr < MIN(num_non_vcl_suffix_nals, MAX_NUM_SUFFIX_NALS_PER_AU); ctr++)
725
0
        {
726
            /* NAL offset is derive by subtracting Bistream base from NAL start pointer */
727
0
            ULWORD64 u8_cur_nal_start =
728
0
                (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start];
729
730
0
#if POPULATE_NAL_SIZE
731
732
            /* ----------Populate NAL Size  -------------*/
733
0
            if((ctr + 1) < num_non_vcl_suffix_nals)
734
0
            {
735
0
                ULWORD64 u8_next_nal_start =
736
0
                    (ULWORD64)ps_bitstrm->apu1_nal_start[ctr + non_vcl_suffix_start + 1];
737
0
                ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
738
0
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
739
0
            }
740
0
            else
741
0
            {
742
0
                ULWORD64 u8_next_nal_start =
743
0
                    (ULWORD64)ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset;
744
0
                ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] =
745
0
                    (UWORD32)(u8_next_nal_start - u8_cur_nal_start);
746
0
            }
747
0
            ASSERT(ps_curr_out->ai4_size_non_vcl_suffix_nals[ctr] > 0);
748
749
#elif POPULATE_NAL_OFFSET
750
751
            /* ----------Populate NAL Offset  -------------*/
752
753
            ASSERT(u8_cur_nal_start >= u8_bitstream_base);
754
            ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] =
755
                (UWORD32)(u8_cur_nal_start - u8_bitstream_base);
756
757
            if(ctr)
758
            {
759
                /* sanity check on increasing NAL offsets */
760
                ASSERT(
761
                    ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr] >
762
                    ps_curr_out->ai4_off_non_vcl_suffix_nals[ctr - 1]);
763
            }
764
#endif /* POPULATE_NAL_SIZE */
765
0
        }
766
104k
    }
767
768
    /*PIC INFO: Populatinf Ref POC, weights and offset*/
769
104k
    {
770
104k
        WORD32 i;
771
104k
        ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l0_active =
772
104k
            ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active;
773
104k
        ps_entropy_ctxt->ps_pic_level_info->i1_num_ref_idx_l1_active =
774
104k
            ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active;
775
274k
        for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
776
169k
        {
777
169k
            ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l0[i] =
778
169k
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l0[i];
779
169k
            ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l0[i] =
780
169k
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l0[i];
781
169k
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l0[i] =
782
169k
                (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l0[i] /
783
169k
                (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
784
169k
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l0[i] =
785
169k
                ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l0[i];
786
169k
        }
787
120k
        for(i = 0; i < ps_entropy_ctxt->ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
788
15.7k
        {
789
15.7k
            ps_entropy_ctxt->ps_pic_level_info->i4_ref_poc_l1[i] =
790
15.7k
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i4_ref_poc_l1[i];
791
15.7k
            ps_entropy_ctxt->ps_pic_level_info->i1_list_entry_l1[i] =
792
15.7k
                ps_entropy_ctxt->ps_slice_hdr->s_rplm.i1_list_entry_l1[i];
793
15.7k
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_weight_l1[i] =
794
15.7k
                (DOUBLE)ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_weight_l1[i] /
795
15.7k
                (1 << ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i1_luma_log2_weight_denom);
796
15.7k
            ps_entropy_ctxt->ps_pic_level_info->i2_luma_offset_l1[i] =
797
15.7k
                ps_entropy_ctxt->ps_slice_hdr->s_wt_ofst.i2_luma_offset_l1[i];
798
15.7k
        }
799
104k
    }
800
801
    /* attach the time stamp of the input to output */
802
104k
    ps_curr_out->i4_out_timestamp_low = ps_curr_inp->i4_inp_timestamp_low;
803
804
104k
    ps_curr_out->i4_out_timestamp_high = ps_curr_inp->i4_inp_timestamp_high;
805
806
    /*attach the app frame info of this buffer */
807
104k
    ps_curr_out->pv_app_frm_ctxt = ps_curr_inp->pv_app_frm_ctxt;
808
809
    /* frame never skipped for now */
810
104k
    ps_curr_out->i4_frame_skipped = 0;
811
812
    /* update error code and return */
813
104k
    ps_curr_out->i4_process_error_code = ret;
814
815
104k
    switch(slice_type)
816
104k
    {
817
35.3k
    case ISLICE:
818
35.3k
        if((nal_type == NAL_IDR_N_LP) || (NAL_IDR_W_LP == nal_type))
819
22.9k
        {
820
22.9k
            ps_curr_out->i4_encoded_frame_type = IV_IDR_FRAME;
821
22.9k
        }
822
12.3k
        else
823
12.3k
        {
824
12.3k
            ps_curr_out->i4_encoded_frame_type = IV_I_FRAME;
825
12.3k
        }
826
35.3k
        break;
827
56.1k
    case PSLICE:
828
56.1k
        ps_curr_out->i4_encoded_frame_type = IV_P_FRAME;
829
56.1k
        break;
830
13.2k
    case BSLICE:
831
13.2k
        ps_curr_out->i4_encoded_frame_type = IV_B_FRAME;
832
13.2k
        break;
833
104k
    }
834
835
104k
    if(IHEVCE_SUCCESS == ret)
836
104k
    {
837
104k
        ps_curr_out->i4_process_ret_sts = IV_SUCCESS;
838
104k
    }
839
168
    else
840
168
    {
841
168
        ps_curr_out->i4_process_ret_sts = IV_FAIL;
842
168
    }
843
844
104k
    return (ret);
845
104k
}