Coverage Report

Created: 2026-09-14 06:39

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