Coverage Report

Created: 2025-07-18 07:02

/src/libhevc/encoder/ihevce_lap_interface.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2018 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
21
/**
22
******************************************************************************
23
* @file
24
*  ihevce_lap_interface.c
25
*
26
* @brief
27
*  This file contains function definitions related to look-ahead processing
28
*
29
* @author
30
*  ittiam
31
*
32
* @par List of Functions:
33
*
34
******************************************************************************
35
*/
36
37
/*****************************************************************************/
38
/* File Includes                                                             */
39
/*****************************************************************************/
40
41
/* System Include Files */
42
#include <stdio.h>
43
#include <string.h>
44
#include <stdlib.h>
45
#include <assert.h>
46
47
/* User Include Files */
48
#include "ihevc_typedefs.h"
49
#include "itt_video_api.h"
50
#include "ihevce_api.h"
51
52
#include "rc_cntrl_param.h"
53
#include "rc_frame_info_collector.h"
54
#include "rc_look_ahead_params.h"
55
56
#include "ihevc_defs.h"
57
#include "ihevc_macros.h"
58
#include "ihevc_debug.h"
59
#include "ihevc_structs.h"
60
#include "ihevc_platform_macros.h"
61
#include "ihevc_deblk.h"
62
#include "ihevc_itrans_recon.h"
63
#include "ihevc_chroma_itrans_recon.h"
64
#include "ihevc_chroma_intra_pred.h"
65
#include "ihevc_intra_pred.h"
66
#include "ihevc_inter_pred.h"
67
#include "ihevc_mem_fns.h"
68
#include "ihevc_padding.h"
69
#include "ihevc_weighted_pred.h"
70
#include "ihevc_sao.h"
71
#include "ihevc_resi_trans.h"
72
#include "ihevc_quant_iquant_ssd.h"
73
#include "ihevc_cabac_tables.h"
74
75
#include "ihevce_defs.h"
76
#include "ihevce_api.h"
77
#include "ihevce_hle_interface.h"
78
#include "ihevce_hle_q_func.h"
79
#include "ihevce_lap_enc_structs.h"
80
#include "ihevce_lap_interface.h"
81
#include "ihevce_lap_structs.h"
82
#include "ihevce_multi_thrd_structs.h"
83
#include "ihevce_function_selector.h"
84
#include "ihevce_me_common_defs.h"
85
#include "ihevce_enc_structs.h"
86
#include "ihevce_rc_enc_structs.h"
87
#include "ihevce_rc_interface.h"
88
#include "ihevce_buffer_que_interface.h"
89
90
/*****************************************************************************/
91
/* Globals                                                                   */
92
/*****************************************************************************/
93
WORD32 gau1_order_insert_pic_type[MAX_TEMPORAL_LAYERS][8] = {
94
    { P_PIC, B_PIC, P_PIC, B_PIC, P_PIC, B_PIC, P_PIC, B_PIC },
95
    { P_PIC, B_PIC, B1_PIC, B1_PIC, P_PIC, B_PIC, B1_PIC, B1_PIC },
96
    { P_PIC, B_PIC, B1_PIC, B2_PIC, B2_PIC, B1_PIC, B2_PIC, B2_PIC },
97
};
98
99
UWORD8 gau1_use_by_cur_pic_flag[MAX_REF_PICS] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
100
101
/*****************************************************************************/
102
/* Function Definitions                                                      */
103
/*****************************************************************************/
104
105
/*!
106
************************************************************************
107
* \brief
108
*    return number of records used by LAP
109
*
110
************************************************************************
111
*/
112
WORD32 ihevce_lap_get_num_mem_recs(void)
113
16.4k
{
114
16.4k
    return (NUM_LAP_MEM_RECS);
115
16.4k
}
116
117
/*!
118
************************************************************************
119
* @brief
120
*    return each record attributes of LAP
121
************************************************************************
122
*/
123
WORD32 ihevce_lap_get_mem_recs(iv_mem_rec_t *ps_mem_tab, WORD32 i4_mem_space)
124
8.22k
{
125
    /* number of NODE memory */
126
8.22k
    WORD32 max_nodes = MAX_SUB_GOP_SIZE - 1;
127
128
8.22k
    ps_mem_tab[LAP_CTXT].i4_mem_size = sizeof(lap_struct_t);
129
8.22k
    ps_mem_tab[LAP_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
130
8.22k
    ps_mem_tab[LAP_CTXT].i4_mem_alignment = 8;
131
132
    /* Node memory for 2 sub-gops*/
133
8.22k
    ps_mem_tab[LAP_NODE_MEM].i4_mem_size = (max_nodes * sizeof(ihevce_encode_node_t));
134
135
8.22k
    ps_mem_tab[LAP_NODE_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
136
137
8.22k
    ps_mem_tab[LAP_NODE_MEM].i4_mem_alignment = 8;
138
139
8.22k
    return (NUM_LAP_MEM_RECS);
140
8.22k
}
141
142
/*!
143
************************************************************************
144
* @brief
145
*    Init LAP structure
146
************************************************************************
147
*/
148
void *ihevce_lap_init(
149
    iv_mem_rec_t *ps_mem_tab,
150
    ihevce_lap_static_params_t *ps_lap_params,
151
    ihevce_static_cfg_params_t *ps_static_cfg_prms)
152
8.22k
{
153
8.22k
    WORD32 i4_src_interlace_field;
154
8.22k
    WORD32 i4_max_temporal_layers;
155
8.22k
    ihevce_encode_node_t *ps_encode_node_struct;
156
8.22k
    lap_struct_t *ps_lap_struct = (lap_struct_t *)ps_mem_tab[LAP_CTXT].pv_base;
157
8.22k
    ihevce_lap_static_params_t *ps_lap_static_params = &ps_lap_struct->s_lap_static_params;
158
8.22k
    ps_lap_struct->aps_encode_node[0] = (ihevce_encode_node_t *)ps_mem_tab[LAP_NODE_MEM].pv_base;
159
160
8.22k
    memcpy(
161
8.22k
        &ps_lap_struct->s_static_cfg_params,
162
8.22k
        ps_static_cfg_prms,
163
8.22k
        sizeof(ihevce_static_cfg_params_t));
164
8.22k
    memmove(ps_lap_static_params, ps_lap_params, sizeof(ihevce_lap_static_params_t));
165
8.22k
    ps_lap_static_params->e_arch_type = ps_static_cfg_prms->e_arch_type;
166
167
    /* Set the array to zero */
168
8.22k
    memset(&ps_lap_struct->ai4_capture_order_poc[0], 0, MAX_NUM_ENC_NODES * sizeof(WORD32));
169
8.22k
    memset(&ps_lap_struct->ai4_encode_order_poc[0], 0, MAX_NUM_ENC_NODES * sizeof(WORD32));
170
8.22k
    memset(&ps_lap_struct->ref_poc_array[0], 0xFF, sizeof(ps_lap_struct->ref_poc_array));
171
8.22k
    memset(&ps_lap_struct->ai4_pic_type_to_be_removed, 0, NUM_LAP2_LOOK_AHEAD * sizeof(WORD32));
172
8.22k
    memset(&ps_lap_struct->ai4_num_buffer[0], 0, sizeof(ps_lap_struct->ai4_num_buffer));
173
174
8.22k
    ps_lap_struct->i4_curr_poc = 0;
175
8.22k
    ps_lap_struct->i4_cra_poc = 0;
176
177
8.22k
    i4_max_temporal_layers = ps_lap_static_params->i4_max_temporal_layers;
178
8.22k
    i4_src_interlace_field = ps_lap_static_params->i4_src_interlace_field;
179
8.22k
    ps_lap_struct->i4_max_idr_period =
180
8.22k
        ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period;
181
8.22k
    ps_lap_struct->i4_min_idr_period =
182
8.22k
        ps_static_cfg_prms->s_coding_tools_prms.i4_min_closed_gop_period;
183
8.22k
    ps_lap_struct->i4_max_cra_period =
184
8.22k
        ps_static_cfg_prms->s_coding_tools_prms.i4_max_cra_open_gop_period;
185
8.22k
    ps_lap_struct->i4_max_i_period =
186
8.22k
        ps_static_cfg_prms->s_coding_tools_prms.i4_max_i_open_gop_period;
187
8.22k
    ps_lap_struct->i4_idr_counter = 0;
188
8.22k
    ps_lap_struct->i4_cra_counter = 0;
189
8.22k
    ps_lap_struct->i4_i_counter = 0;
190
8.22k
    ps_lap_struct->i4_idr_gop_num = -1;
191
8.22k
    ps_lap_struct->i4_curr_ref_pics = 0;
192
8.22k
    ps_lap_struct->i4_display_num = 0;
193
8.22k
    ps_lap_struct->i4_num_frm_type_decided = 0;
194
8.22k
    ps_lap_struct->i4_next_start_ctr = 0;
195
8.22k
    ps_lap_struct->ai1_pic_type[0] = PIC_TYPE_IDR;
196
197
8.22k
    ps_lap_struct->i4_enable_logo = ps_lap_static_params->i4_enable_logo;
198
8.22k
    ps_lap_struct->i4_cra_i_pic_flag = 0;
199
8.22k
    ps_lap_struct->i4_force_end_flag = 0;
200
8.22k
    ps_lap_struct->i4_sub_gop_size = (1 << i4_max_temporal_layers);
201
8.22k
    ps_lap_struct->i4_sub_gop_size_idr =
202
8.22k
        ps_lap_struct->i4_sub_gop_size + (i4_max_temporal_layers > 0);
203
204
8.22k
    ps_lap_struct->i4_is_all_i_pic_in_seq = 0;
205
206
8.22k
    if(ps_lap_struct->i4_max_idr_period == 1 || ps_lap_struct->i4_max_cra_period == 1 ||
207
8.22k
       ps_lap_struct->i4_max_i_period == 1)
208
573
    {
209
573
        ps_lap_struct->i4_is_all_i_pic_in_seq = 1;
210
573
    }
211
212
8.22k
    if(1 == i4_src_interlace_field && (!ps_lap_struct->i4_is_all_i_pic_in_seq))
213
0
    {
214
0
        ps_lap_struct->i4_sub_gop_size <<= 1;
215
0
        ps_lap_struct->i4_sub_gop_size_idr <<= 1;
216
0
    }
217
218
8.22k
    ps_lap_struct->i4_fixed_open_gop_period = 1;
219
8.22k
    ps_lap_struct->i4_fixed_i_period = 1;
220
221
8.22k
    if(ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period <=
222
8.22k
       ps_lap_struct->i4_sub_gop_size)
223
2.68k
    {
224
2.68k
        ps_lap_struct->i4_min_idr_period =
225
2.68k
            ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period;
226
2.68k
    }
227
8.22k
    if(ps_lap_struct->i4_max_idr_period)
228
5.74k
    {
229
5.74k
        if(ps_lap_struct->i4_max_cra_period)
230
3.64k
        {
231
3.64k
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_cra_period;
232
3.64k
        }
233
2.10k
        else if(ps_lap_struct->i4_max_i_period)
234
945
        {
235
945
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_i_period;
236
945
        }
237
1.15k
        else
238
1.15k
        {
239
1.15k
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_idr_period;
240
1.15k
        }
241
5.74k
    }
242
2.48k
    else
243
2.48k
    {
244
2.48k
        if(ps_lap_struct->i4_max_i_period)
245
669
        {
246
669
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_i_period;
247
669
        }
248
1.81k
        else if(ps_lap_struct->i4_max_cra_period)
249
589
        {
250
589
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_cra_period;
251
589
        }
252
2.48k
    }
253
254
8.22k
    if(!ps_lap_struct->i4_max_i_period)
255
3.59k
    {
256
3.59k
        ps_lap_struct->i4_max_i_period =
257
3.59k
            2 * MAX(ps_lap_struct->i4_max_idr_period, ps_lap_struct->i4_max_cra_period);
258
3.59k
    }
259
260
8.22k
    ps_lap_struct->i4_no_back_to_back_i_avoidance = 0;
261
262
    /*Infinite GOP case*/
263
8.22k
    if(!ps_lap_struct->i4_gop_period)
264
1.22k
    {
265
        /*max signed 32 bit value which will be ~ 414 days considering 60frames/fields per second*/
266
1.22k
        ps_lap_struct->i4_max_i_period = 0x7fffffff;
267
1.22k
        ps_lap_struct->i4_gop_period =
268
1.22k
            (INFINITE_GOP_CDR_TIME_S * (ps_static_cfg_prms->s_src_prms.i4_frm_rate_num /
269
1.22k
                                        ps_static_cfg_prms->s_src_prms.i4_frm_rate_denom));
270
1.22k
    }
271
272
8.22k
    if(ps_lap_struct->i4_gop_period < (2 * ps_lap_struct->i4_sub_gop_size))
273
638
    {
274
638
        ps_lap_struct->i4_no_back_to_back_i_avoidance = 1;
275
638
    }
276
277
8.22k
    ps_lap_struct->i4_rc_lap_period =
278
8.22k
        ps_static_cfg_prms->s_lap_prms.i4_rc_look_ahead_pics + MIN_L1_L0_STAGGER_NON_SEQ;
279
8.22k
    ps_lap_struct->pv_prev_inp_buf = NULL;
280
8.22k
    ps_lap_struct->i4_buf_deq_idx = 0;
281
8.22k
    ps_lap_struct->i4_deq_idx = 0;
282
8.22k
    ps_lap_struct->i4_enq_idx = 0;
283
8.22k
    ps_lap_struct->i4_lap2_counter = 0;
284
8.22k
    ps_lap_struct->i4_dyn_sub_gop_size = ps_lap_struct->i4_sub_gop_size;
285
8.22k
    ps_lap_struct->i4_buf_enq_idx = 0;
286
8.22k
    ps_lap_struct->i4_lap_out_idx = 0;
287
8.22k
    ps_lap_struct->i4_capture_idx = 0;
288
8.22k
    ps_lap_struct->i4_idr_flag = 1;
289
8.22k
    ps_lap_struct->i4_num_bufs_encode_order = 0;
290
8.22k
    ps_lap_struct->end_flag = 0;
291
8.22k
    ps_lap_struct->i4_immediate_idr_case = 0;
292
8.22k
    ps_lap_struct->i4_max_buf_in_enc_order = 0;
293
8.22k
    ps_lap_struct->i4_end_flag_pic_idx = 0;
294
8.22k
    memset(
295
8.22k
        &ps_lap_struct->api4_encode_order_array[0],
296
8.22k
        0,
297
8.22k
        sizeof(ihevce_lap_enc_buf_t *) * MAX_NUM_ENC_NODES);
298
8.22k
    ps_lap_struct->i4_sub_gop_pic_idx = 0;
299
8.22k
    ps_lap_struct->i4_force_idr_pos = 0;
300
8.22k
    ps_lap_struct->i4_num_dummy_pic = 0;
301
8.22k
    ps_lap_struct->i4_lap_encode_idx = 0;
302
8.22k
    ps_lap_struct->i4_deq_lap_buf = 0;
303
8.22k
    ps_lap_struct->i4_sub_gop_end = 0;
304
305
8.22k
    {
306
8.22k
        WORD32 node_offset, curr_layer;
307
8.22k
        WORD32 i;
308
        /*intialization of aps_lap_inp_buf*/
309
649k
        for(i = 0; i < MAX_QUEUE_LENGTH; i++)
310
641k
        {
311
641k
            ps_lap_struct->aps_lap_inp_buf[i] = NULL;
312
641k
        }
313
314
        /* init capture order and encode order pointer */
315
8.22k
        ps_lap_struct->pi4_capture_poc_ptr = &ps_lap_struct->ai4_capture_order_poc[0];
316
8.22k
        ps_lap_struct->pi4_encode_poc_ptr = &ps_lap_struct->ai4_encode_order_poc[0];
317
318
        /* init all the buffer status to default values */
319
8.22k
        ps_encode_node_struct = ps_lap_struct->aps_encode_node[0];
320
321
8.22k
        ps_encode_node_struct->pv_left_node = NULL;
322
8.22k
        ps_encode_node_struct->pv_right_node = NULL;
323
324
        /* Initialise the tree */
325
8.22k
        node_offset = 1;
326
8.22k
        curr_layer = 0;
327
8.22k
        ihevce_populate_tree_nodes(
328
8.22k
            ps_encode_node_struct,
329
8.22k
            ps_encode_node_struct,
330
8.22k
            &node_offset,
331
8.22k
            curr_layer,
332
8.22k
            ps_lap_static_params->i4_max_temporal_layers);
333
8.22k
    }
334
335
8.22k
    ps_mem_tab += NUM_LAP_MEM_RECS;
336
337
8.22k
    return ((void *)ps_lap_struct);
338
8.22k
}
339
340
/*!
341
******************************************************************************
342
* \if Function name : ihevce_populate_tree_nodes \endif
343
*
344
* \brief
345
*    LAP populate nodes function
346
*
347
* \param[in] encode_parent_node_t node pointer to base
348
*            encode_node_t        node pointer to current buffer
349
*            loop_count                layer count
350
*            hier_layer           total layers
351
* \return
352
*    None
353
*
354
* \author
355
*  Ittiam
356
*
357
*****************************************************************************
358
*/
359
void ihevce_populate_tree_nodes(
360
    ihevce_encode_node_t *encode_parent_node_t,
361
    ihevce_encode_node_t *encode_node_t,
362
    WORD32 *loop_count,
363
    WORD32 layer,
364
    WORD32 hier_layer)
365
20.8k
{
366
    /* If only I/P pictures, return NULL from the child nodes*/
367
20.8k
    if(hier_layer == 0)
368
6.11k
    {
369
6.11k
        encode_node_t->pv_left_node = NULL;
370
6.11k
        encode_node_t->pv_right_node = NULL;
371
6.11k
        return;
372
6.11k
    }
373
14.7k
    if(layer == hier_layer)
374
8.44k
        return;
375
376
6.33k
    layer = layer + 1;
377
378
    /* If the layers are not exhausted */
379
6.33k
    if(layer < hier_layer)
380
2.11k
    {
381
2.11k
        encode_node_t->pv_left_node = encode_parent_node_t + (*loop_count);
382
2.11k
        encode_node_t->pv_right_node = encode_parent_node_t + (*loop_count + 1);
383
2.11k
        (*loop_count) = (*loop_count) + 2;
384
2.11k
    }
385
4.22k
    else
386
4.22k
    {
387
4.22k
        encode_node_t->pv_left_node = NULL;
388
4.22k
        encode_node_t->pv_right_node = NULL;
389
4.22k
    }
390
391
    /* Populate Left tree nodes */
392
6.33k
    ihevce_populate_tree_nodes(
393
6.33k
        encode_parent_node_t,
394
6.33k
        (ihevce_encode_node_t *)encode_node_t->pv_left_node,
395
6.33k
        loop_count,
396
6.33k
        layer,
397
6.33k
        hier_layer);
398
399
    /* Populate right tree nodes */
400
6.33k
    ihevce_populate_tree_nodes(
401
6.33k
        encode_parent_node_t,
402
6.33k
        (ihevce_encode_node_t *)encode_node_t->pv_right_node,
403
6.33k
        loop_count,
404
6.33k
        layer,
405
6.33k
        hier_layer);
406
6.33k
}
407
408
/*!
409
************************************************************************
410
* \brief
411
*    pad input when its dimensions are not aligned to LCU size
412
************************************************************************
413
*/
414
void ihevce_lap_pad_input_bufs(
415
    ihevce_lap_enc_buf_t *ps_curr_inp, WORD32 align_pic_wd, WORD32 align_pic_ht)
416
149k
{
417
    /* local variables */
418
149k
    WORD32 ctr_horz, ctr_vert;
419
420
    /* ------- Horizontal Right Padding ------ */
421
149k
    if(align_pic_wd != ps_curr_inp->s_lap_out.s_input_buf.i4_y_wd)
422
5.78k
    {
423
5.78k
        UWORD8 *pu1_inp;
424
5.78k
        UWORD16 *pu2_inp;
425
5.78k
        WORD32 pad_wd;
426
5.78k
        WORD32 pad_ht;
427
428
        /* ------------- LUMA ----------------------------- */
429
        /* derive the pointers and dimensions to be padded */
430
5.78k
        pad_ht = ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht;
431
5.78k
        pad_wd = align_pic_wd - ps_curr_inp->s_lap_out.s_input_buf.i4_y_wd;
432
5.78k
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_y_buf;
433
5.78k
        pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_y_wd;
434
435
        /* loops for padding the right region for entire pic */
436
563k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
437
557k
        {
438
2.55M
            for(ctr_horz = 0; ctr_horz < pad_wd; ctr_horz++)
439
1.99M
            {
440
                /* last pixel is replicated */
441
1.99M
                pu1_inp[ctr_horz] = pu1_inp[-1];
442
1.99M
            }
443
444
            /* row level increments */
445
557k
            pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_y_strd;
446
557k
        }
447
448
        /* ------------- CHROMA ---------------------------- */
449
        /* derive the pointers and dimensions to be padded */
450
5.78k
        pad_ht = ps_curr_inp->s_lap_out.s_input_buf.i4_uv_ht;
451
5.78k
        pad_wd = align_pic_wd - ps_curr_inp->s_lap_out.s_input_buf.i4_uv_wd;
452
5.78k
        pad_wd >>= 1;
453
5.78k
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_u_buf;
454
5.78k
        pu2_inp = (UWORD16 *)(pu1_inp + ps_curr_inp->s_lap_out.s_input_buf.i4_uv_wd);
455
456
        /* loops for padding the right region for entire pic */
457
284k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
458
278k
        {
459
778k
            for(ctr_horz = 0; ctr_horz < pad_wd; ctr_horz++)
460
499k
            {
461
                /* last pixel is replicated, cb and cr pixel interleaved */
462
499k
                pu2_inp[ctr_horz] = pu2_inp[-1];
463
499k
            }
464
465
            /* row level increments */
466
278k
            pu2_inp += (ps_curr_inp->s_lap_out.s_input_buf.i4_uv_strd >> 1);
467
278k
        }
468
5.78k
    }
469
470
    /* ------- Vertical Bottom Padding ------ */
471
149k
    if(align_pic_ht != ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht)
472
3.19k
    {
473
3.19k
        UWORD8 *pu1_inp, *pu1_src;
474
3.19k
        WORD32 pad_ht;
475
476
        /* ------------- LUMA ----------------------------- */
477
        /* derive the pointers and dimensions to be padded */
478
3.19k
        pad_ht = align_pic_ht - ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht;
479
3.19k
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_y_buf;
480
3.19k
        pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht *
481
3.19k
                   ps_curr_inp->s_lap_out.s_input_buf.i4_y_strd;
482
483
        /* get the pointer of last row */
484
3.19k
        pu1_src = pu1_inp - ps_curr_inp->s_lap_out.s_input_buf.i4_y_strd;
485
486
        /* loops for padding the bottom region for entire row */
487
16.0k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
488
12.8k
        {
489
            /* copy the eniter orw including horz padd region */
490
12.8k
            memcpy(pu1_inp, pu1_src, align_pic_wd);
491
492
            /* row level increments */
493
12.8k
            pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_y_strd;
494
12.8k
        }
495
496
        /* ------------- CHROMA ----------------------------- */
497
        /* derive the pointers and dimensions to be padded */
498
3.19k
        pad_ht = (align_pic_ht >> 1) - ps_curr_inp->s_lap_out.s_input_buf.i4_uv_ht;
499
3.19k
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_u_buf;
500
3.19k
        pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_uv_ht *
501
3.19k
                   ps_curr_inp->s_lap_out.s_input_buf.i4_uv_strd;
502
503
        /* get the pointer of last row */
504
3.19k
        pu1_src = pu1_inp - ps_curr_inp->s_lap_out.s_input_buf.i4_uv_strd;
505
506
        /* loops for padding the bottom region for entire row */
507
9.62k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
508
6.43k
        {
509
            /* copy the eniter orw including horz padd region */
510
6.43k
            memcpy(pu1_inp, pu1_src, align_pic_wd);
511
512
            /* row level increments */
513
6.43k
            pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_uv_strd;
514
6.43k
        }
515
3.19k
    }
516
149k
    return;
517
149k
}
518
519
/*!
520
************************************************************************
521
* \brief
522
*    check for last inp buf
523
************************************************************************
524
*/
525
WORD32 ihevce_check_last_inp_buf(WORD32 *pi4_cmd_buf)
526
158k
{
527
158k
    WORD32 cmd = (*pi4_cmd_buf) & (IHEVCE_COMMANDS_TAG_MASK);
528
529
158k
    if(IHEVCE_SYNCH_API_FLUSH_TAG == cmd)
530
8.22k
        return 1;
531
149k
    return 0;
532
158k
}
533
534
/*!
535
************************************************************************
536
* \brief
537
*    lap parse sync commands
538
************************************************************************
539
*/
540
void ihevce_lap_parse_sync_cmd(
541
    ihevce_hle_ctxt_t *ps_hle_ctxt,
542
    ihevce_static_cfg_params_t *ps_static_cfg_prms,
543
    WORD32 *pi4_cmd_buf,
544
    ihevce_lap_enc_buf_t *ps_lap_inp_buf,
545
    WORD32 *pi4_flush_check,
546
    WORD32 *pi4_force_idr_check)
547
158k
{
548
158k
    WORD32 *pi4_tag_parse = pi4_cmd_buf;
549
158k
    WORD32 i4_cmd_size = ps_lap_inp_buf->s_input_buf.i4_cmd_buf_size;
550
158k
    WORD32 i4_buf_id = ps_lap_inp_buf->s_input_buf.i4_buf_id;
551
158k
#ifndef DISABLE_SEI
552
158k
    UWORD32 u4_num_sei = 0;
553
158k
#endif
554
158k
    WORD32 i4_end_flag = 0;
555
556
167k
    while(i4_cmd_size >= 4)
557
167k
    {
558
167k
        switch((*pi4_tag_parse) & (IHEVCE_COMMANDS_TAG_MASK))
559
167k
        {
560
8.22k
        case IHEVCE_SYNCH_API_FLUSH_TAG:
561
8.22k
            if(i4_cmd_size < 8 || pi4_tag_parse[1])
562
0
            {
563
0
                ps_hle_ctxt->ihevce_cmds_error_report(
564
0
                    ps_hle_ctxt->pv_cmd_err_cb_handle,
565
0
                    IHEVCE_SYNCH_ERR_LENGTH_NOT_ZERO,
566
0
                    1,
567
0
                    i4_buf_id);
568
0
                return;
569
0
            }
570
8.22k
            (*pi4_flush_check) = 1;
571
8.22k
            pi4_tag_parse += 2;
572
8.22k
            i4_cmd_size -= 8;
573
8.22k
#ifndef DISABLE_SEI
574
8.22k
            u4_num_sei++;
575
8.22k
#endif
576
8.22k
            break;
577
1.34k
        case IHEVCE_SYNCH_API_FORCE_IDR_TAG:
578
1.34k
            if(i4_cmd_size < 8 || pi4_tag_parse[1])
579
0
            {
580
0
                ps_hle_ctxt->ihevce_cmds_error_report(
581
0
                    ps_hle_ctxt->pv_cmd_err_cb_handle,
582
0
                    IHEVCE_SYNCH_ERR_LENGTH_NOT_ZERO,
583
0
                    1,
584
0
                    i4_buf_id);
585
0
                return;
586
0
            }
587
1.34k
            (*pi4_force_idr_check) = 1;
588
1.34k
            pi4_tag_parse += 2;
589
1.34k
            i4_cmd_size -= 8;
590
1.34k
#ifndef DISABLE_SEI
591
1.34k
            u4_num_sei++;
592
1.34k
#endif
593
1.34k
            break;
594
158k
        case IHEVCE_SYNCH_API_END_TAG:
595
158k
            i4_end_flag = 1;
596
158k
            i4_cmd_size -= 4;
597
158k
            break;
598
0
        default:
599
0
            ps_hle_ctxt->ihevce_cmds_error_report(
600
0
                ps_hle_ctxt->pv_cmd_err_cb_handle, IHEVCE_SYNCH_ERR_TLV_ERROR, 1, i4_buf_id);
601
0
            i4_end_flag = 1;
602
167k
        }
603
167k
        if(i4_end_flag)
604
158k
            break;
605
167k
    }
606
158k
#ifndef DISABLE_SEI
607
158k
    if(u4_num_sei > MAX_NUMBER_OF_SEI_PAYLOAD)  //Checking for max number of SEI messages.
608
0
        ps_hle_ctxt->ihevce_cmds_error_report(
609
0
            ps_hle_ctxt->pv_cmd_err_cb_handle, IHEVCE_SYNCH_ERR_TOO_MANY_SEI_MSG, 1, i4_buf_id);
610
158k
#endif
611
612
158k
    if(!i4_end_flag)
613
0
        ps_hle_ctxt->ihevce_cmds_error_report(
614
0
            ps_hle_ctxt->pv_cmd_err_cb_handle, IHEVCE_SYNCH_ERR_NO_END_TAG, 1, i4_buf_id);
615
158k
}
616
617
/*!
618
************************************************************************
619
* \brief
620
*    lap parse Async commands
621
************************************************************************
622
*/
623
void ihevce_lap_parse_async_cmd(
624
    ihevce_hle_ctxt_t *ps_hle_ctxt,
625
    WORD32 *pi4_cmd_buf,
626
    WORD32 i4_length,
627
    WORD32 i4_buf_id,
628
    WORD32 *pi4_num_set_bitrate_cmds,
629
    ihevce_dyn_config_prms_t *ps_dyn_br)
630
5.28k
{
631
5.28k
    WORD32 i4_end_flag = 0;
632
5.28k
    WORD32 *pi4_tag_parse = pi4_cmd_buf;
633
634
10.5k
    while(i4_length >= 4)
635
10.5k
    {
636
10.5k
        switch(*pi4_tag_parse)
637
10.5k
        {
638
5.28k
        case IHEVCE_ASYNCH_API_SETBITRATE_TAG:
639
5.28k
            if(i4_length < (8 + sizeof(ihevce_dyn_config_prms_t)) ||
640
5.28k
               pi4_tag_parse[1] != sizeof(ihevce_dyn_config_prms_t))
641
0
            {
642
0
                ps_hle_ctxt->ihevce_cmds_error_report(
643
0
                    ps_hle_ctxt->pv_cmd_err_cb_handle, IHEVCE_ASYNCH_ERR_BR_NOT_BYTE, 1, i4_buf_id);
644
0
                return;
645
0
            }
646
5.28k
            memcpy(
647
5.28k
                (void *)ps_dyn_br, (void *)(pi4_tag_parse + 2), sizeof(ihevce_dyn_config_prms_t));
648
5.28k
            pi4_tag_parse += (2 + (sizeof(ihevce_dyn_config_prms_t) >> 2));
649
5.28k
            i4_length -= (8 + sizeof(ihevce_dyn_config_prms_t));
650
5.28k
            *pi4_num_set_bitrate_cmds += 1;
651
5.28k
            ps_dyn_br++;
652
5.28k
            break;
653
5.28k
        case IHEVCE_ASYNCH_API_END_TAG:
654
5.28k
            i4_end_flag = 1;
655
5.28k
            i4_length -= 4;
656
5.28k
            break;
657
0
        default:
658
0
            ps_hle_ctxt->ihevce_cmds_error_report(
659
0
                ps_hle_ctxt->pv_cmd_err_cb_handle, IHEVCE_ASYNCH_ERR_TLV_ERROR, 1, i4_buf_id);
660
0
            i4_end_flag = 1;
661
10.5k
        }
662
10.5k
        if(i4_end_flag)
663
5.28k
            break;
664
10.5k
    }
665
5.28k
    if(!i4_end_flag)
666
0
        ps_hle_ctxt->ihevce_cmds_error_report(
667
0
            ps_hle_ctxt->pv_cmd_err_cb_handle, IHEVCE_ASYNCH_ERR_NO_END_TAG, 1, i4_buf_id);
668
5.28k
}
669
670
/*!
671
************************************************************************
672
* \brief
673
*    ref pics weight offset calculation
674
************************************************************************
675
*/
676
void ref_pics_weight_offset_calc(ihevce_lap_output_params_t *ps_lap_out, lap_struct_t *ps_lap_struct)
677
158k
{
678
158k
    WORD32 i, j;
679
158k
    WORD32 *ref_poc_array = ps_lap_struct->ref_poc_array;
680
158k
    WORD32 ai4_delta_poc[MAX_REF_PICS];
681
158k
    WORD32 ref_poc_arr_sort[MAX_REF_PICS];
682
683
    /* Default weighted pred parameters populated for  now */
684
158k
    ps_lap_out->i4_log2_luma_wght_denom = DENOM_DEFAULT;
685
158k
    ps_lap_out->i4_log2_chroma_wght_denom = DENOM_DEFAULT;
686
687
    /* sort the ref_poc_array based on delta as
688
     * in case weighted pred dup pics are inserted and it should consider
689
     * the neighbors first for prediction than farthest */
690
539k
    for(i = 0; i < ps_lap_struct->i4_curr_ref_pics; i++)
691
381k
    {
692
381k
        ai4_delta_poc[i] = ref_poc_array[i] - ps_lap_out->i4_poc;
693
381k
    }
694
695
539k
    for(i = 0; i < ps_lap_struct->i4_curr_ref_pics; i++)
696
381k
    {
697
381k
        WORD32 i4_min, temp;
698
381k
        i4_min = i;
699
1.23M
        for(j = i; j < ps_lap_struct->i4_curr_ref_pics; j++)
700
856k
        {
701
856k
            if(abs(ai4_delta_poc[j]) <= abs(ai4_delta_poc[i4_min]))
702
566k
            {
703
566k
                i4_min = j;
704
566k
            }
705
856k
        }
706
381k
        temp = ai4_delta_poc[i];
707
381k
        ai4_delta_poc[i] = ai4_delta_poc[i4_min];
708
381k
        ai4_delta_poc[i4_min] = temp;
709
381k
        ref_poc_arr_sort[i] = ai4_delta_poc[i] + ps_lap_out->i4_poc;
710
381k
    }
711
712
539k
    for(i = 0; i < ps_lap_struct->i4_curr_ref_pics; i++)
713
381k
    {
714
381k
        ps_lap_out->as_ref_pics[i].i4_ref_pic_delta_poc = ref_poc_arr_sort[i] - ps_lap_out->i4_poc;
715
381k
        ASSERT(ps_lap_out->as_ref_pics[i].i4_ref_pic_delta_poc);
716
717
        /* Enable flag for the reference pics to be used by curr pic */
718
381k
        ps_lap_out->as_ref_pics[i].i4_used_by_cur_pic_flag = gau1_use_by_cur_pic_flag[i];
719
720
        /* Currently no weighted prediction offset added */
721
381k
        ps_lap_out->as_ref_pics[i].i4_num_duplicate_entries_in_ref_list = 1;
722
381k
    }
723
158k
    return;
724
158k
}
725
726
/*!
727
************************************************************************
728
* \brief
729
*    ref b picture population
730
************************************************************************
731
*/
732
void ref_b_pic_population(
733
    WORD32 curr_layer, ihevce_lap_enc_buf_t *ps_lap_inp, lap_struct_t *ps_lap_struct)
734
26.0k
{
735
26.0k
    ihevce_lap_output_params_t *ps_lap_out = &ps_lap_inp->s_lap_out;
736
26.0k
    WORD32 *ref_poc_array = ps_lap_struct->ref_poc_array;
737
26.0k
    WORD32 *p_ref_poc_array = ref_poc_array;
738
26.0k
    WORD32 i4_interlace_field = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
739
26.0k
    WORD32 i4_max_ref_pics = ps_lap_struct->s_lap_static_params.i4_max_reference_frames;
740
26.0k
    WORD32 max_temporal_layers = ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
741
742
    /* LAP output structure */
743
26.0k
    ps_lap_out->i4_poc = ps_lap_struct->pi4_encode_poc_ptr[0];
744
26.0k
    ps_lap_out->i4_idr_gop_num = ps_lap_struct->i4_idr_gop_num;
745
26.0k
    ps_lap_out->i4_assoc_IRAP_poc = ps_lap_struct->i4_assoc_IRAP_poc;
746
26.0k
    ps_lap_out->i4_temporal_lyr_id = curr_layer;
747
26.0k
    ps_lap_out->i4_pic_type = IV_B_FRAME;
748
749
26.0k
    if((ps_lap_out->i4_poc > ps_lap_struct->i4_cra_poc) &&
750
26.0k
       (ref_poc_array[0] < ps_lap_struct->i4_cra_poc) && ps_lap_struct->i4_cra_i_pic_flag)
751
23
    {
752
23
        ref_poc_array[0] = ps_lap_struct->i4_cra_poc;
753
23
        ps_lap_struct->i4_curr_ref_pics = 1;
754
23
    }
755
756
26.0k
    ps_lap_out->i4_num_ref_pics = ps_lap_struct->i4_curr_ref_pics;
757
758
    /* Default: Cur pic is ref pic*/
759
26.0k
    ps_lap_out->i4_is_ref_pic = 1;
760
761
26.0k
    if(1 == i4_interlace_field)
762
0
    {
763
0
        WORD32 i4_bottom_field = ps_lap_inp->s_input_buf.i4_bottom_field;
764
0
        WORD32 first_field = (ps_lap_inp->s_input_buf.i4_topfield_first ^ i4_bottom_field);
765
766
        /*If current pic is top field B picture and is present in top hierarchical layer */
767
        /* Dereference the curr pic */
768
0
        if(ps_lap_out->i4_temporal_lyr_id == max_temporal_layers)
769
0
        {
770
0
            if(0 == first_field)
771
0
                ps_lap_out->i4_is_ref_pic = 0;
772
0
            else
773
0
                ps_lap_out->i4_is_ref_pic = 2;
774
0
        }
775
0
    }
776
26.0k
    else
777
26.0k
    {
778
        /*If progressive B picture and is present in top hierarchical layer */
779
26.0k
        if(ps_lap_out->i4_temporal_lyr_id >= max_temporal_layers)
780
17.8k
        {
781
17.8k
            ps_lap_out->i4_temporal_lyr_id = max_temporal_layers;
782
17.8k
            ps_lap_out->i4_is_ref_pic = 0;
783
17.8k
        }
784
26.0k
    }
785
786
26.0k
    ref_pics_weight_offset_calc(ps_lap_out, ps_lap_struct);
787
788
    /* Updating number of current reference Pictures for the Given Picture */
789
    /* If the current frame is n-layer B frame, donot increment*/
790
26.0k
    if(ps_lap_struct->i4_curr_ref_pics < i4_max_ref_pics)
791
11.8k
    {
792
11.8k
        if(ps_lap_out->i4_is_ref_pic)
793
3.58k
        {
794
3.58k
            ps_lap_struct->i4_curr_ref_pics++;
795
3.58k
        }
796
11.8k
    }
797
798
    /* Arrange the reference array in ascending order */
799
26.0k
    {
800
26.0k
        WORD32 i, j, temp;
801
89.7k
        for(i = 0; i < (ps_lap_struct->i4_curr_ref_pics - 1); i++)
802
63.6k
        {
803
179k
            for(j = i + 1; j < ps_lap_struct->i4_curr_ref_pics; j++)
804
116k
            {
805
116k
                if(ref_poc_array[i] > ref_poc_array[j])
806
37.5k
                {
807
37.5k
                    temp = ref_poc_array[i];
808
37.5k
                    ref_poc_array[i] = ref_poc_array[j];
809
37.5k
                    ref_poc_array[j] = temp;
810
37.5k
                }
811
116k
            }
812
63.6k
        }
813
26.0k
    }
814
815
26.0k
    {
816
26.0k
        WORD32 ref = ps_lap_out->i4_poc;
817
26.0k
        if(ps_lap_out->i4_is_ref_pic && ref > *p_ref_poc_array)
818
8.18k
        {
819
8.18k
            *p_ref_poc_array = ref;
820
8.18k
        }
821
26.0k
    }
822
823
26.0k
    return;
824
26.0k
}
825
826
/*!
827
************************************************************************
828
* \brief
829
*    ref i/p pic population
830
************************************************************************
831
*/
832
void ref_pic_population(ihevce_lap_enc_buf_t *ps_lap_inp, lap_struct_t *ps_lap_struct)
833
132k
{
834
132k
    ihevce_lap_output_params_t *ps_lap_out = &ps_lap_inp->s_lap_out;
835
132k
    WORD32 *ref_poc_array = ps_lap_struct->ref_poc_array;
836
132k
    WORD32 *p_ref_poc_array = ref_poc_array;
837
132k
    WORD32 i4_max_ref_pics = ps_lap_struct->s_lap_static_params.i4_max_reference_frames;
838
839
    /* Update the POC position */
840
132k
    ps_lap_out->i4_poc = ps_lap_struct->pi4_encode_poc_ptr[0];
841
842
    /* picture after CRA can't refer pic before CRA*/
843
132k
    if((ps_lap_out->i4_poc > ps_lap_struct->i4_cra_poc) &&
844
132k
       (ref_poc_array[0] <= ps_lap_struct->i4_cra_poc) && ps_lap_struct->i4_cra_i_pic_flag)
845
26.2k
    {
846
26.2k
        ref_poc_array[0] = ps_lap_struct->i4_cra_poc;
847
26.2k
        ps_lap_struct->i4_curr_ref_pics = 1;
848
26.2k
    }
849
850
    /* For every IDR period, set pic type as IDR frame and reset reference POC array to 0*/
851
132k
    if(IV_IDR_FRAME == ps_lap_out->i4_pic_type)
852
28.1k
    {
853
28.1k
        ps_lap_struct->i4_idr_gop_num++;
854
28.1k
        ps_lap_struct->i4_curr_ref_pics = 0;
855
28.1k
        ps_lap_out->i4_num_ref_pics = 0;
856
28.1k
        ps_lap_struct->i4_cra_i_pic_flag = 1;
857
28.1k
        ps_lap_struct->i4_cra_poc = ps_lap_out->i4_poc;
858
859
28.1k
        memset(ps_lap_struct->ref_poc_array, 0xFF, sizeof(WORD32) * MAX_REF_PICS);
860
28.1k
    }
861
103k
    else if(IV_I_FRAME == ps_lap_out->i4_pic_type)
862
14.1k
    {
863
        /* For the I-frames after CRA Frame, no pictures should be referenced */
864
14.1k
        if((1 == ps_lap_struct->i4_cra_i_pic_flag) && ps_lap_out->i4_is_cra_pic)
865
3.62k
        {
866
3.62k
            ps_lap_struct->i4_curr_ref_pics = 0;
867
3.62k
            ps_lap_out->i4_num_ref_pics = 0;
868
3.62k
        }
869
14.1k
        ps_lap_struct->i4_cra_poc = ps_lap_out->i4_poc;
870
14.1k
        ps_lap_struct->i4_cra_i_pic_flag = ps_lap_out->i4_is_cra_pic;
871
14.1k
    }
872
89.7k
    else if(IV_P_FRAME == ps_lap_out->i4_pic_type)
873
89.7k
    {
874
        /* If the current POC is the P POC after CRA I POC */
875
89.7k
        if(1 == ps_lap_struct->i4_cra_i_pic_flag)
876
20.9k
        {
877
20.9k
            ps_lap_struct->i4_curr_ref_pics = 1;
878
20.9k
            ps_lap_struct->i4_cra_i_pic_flag = 0;
879
20.9k
        }
880
89.7k
    }
881
882
132k
    if(ps_lap_out->i4_pic_type == IV_IDR_FRAME ||
883
132k
       (ps_lap_out->i4_pic_type == IV_I_FRAME && ps_lap_out->i4_is_cra_pic))
884
35.1k
    {
885
35.1k
        ps_lap_struct->i4_assoc_IRAP_poc = ps_lap_out->i4_poc;
886
35.1k
    }
887
888
    /*Update ps_lap_out*/
889
132k
    ps_lap_out->i4_idr_gop_num = ps_lap_struct->i4_idr_gop_num;
890
132k
    ps_lap_out->i4_is_ref_pic = 1;
891
132k
    ps_lap_out->i4_assoc_IRAP_poc = ps_lap_struct->i4_assoc_IRAP_poc;
892
893
    /* Reference POCS */
894
132k
    ps_lap_out->i4_num_ref_pics = ps_lap_struct->i4_curr_ref_pics;
895
896
    /* I and P frames are always mapped to layer zero*/
897
132k
    ps_lap_out->i4_temporal_lyr_id = 0;
898
899
132k
    ref_pics_weight_offset_calc(ps_lap_out, ps_lap_struct);
900
901
132k
    if(ps_lap_struct->i4_curr_ref_pics < i4_max_ref_pics)
902
79.2k
    {
903
79.2k
        if(ps_lap_out->i4_is_ref_pic)
904
79.2k
        {
905
79.2k
            ps_lap_struct->i4_curr_ref_pics++;
906
79.2k
        }
907
79.2k
    }
908
909
    /* Arrange the reference array in ascending order */
910
132k
    {
911
132k
        WORD32 i, j, temp;
912
374k
        for(i = 0; i < (ps_lap_struct->i4_curr_ref_pics - 1); i++)
913
242k
        {
914
692k
            for(j = i + 1; j < (ps_lap_struct->i4_curr_ref_pics); j++)
915
449k
            {
916
449k
                if(ref_poc_array[i] > ref_poc_array[j])
917
266k
                {
918
266k
                    temp = ref_poc_array[i];
919
266k
                    ref_poc_array[i] = ref_poc_array[j];
920
266k
                    ref_poc_array[j] = temp;
921
266k
                }
922
449k
            }
923
242k
        }
924
132k
    }
925
926
132k
    {
927
        /* add the current pictute at the start of the reference queue */
928
        /*For I and P pictures, all the previous frames are reference frames */
929
        /* If the current ref POC is greater than the least POC in reference array*/
930
        /* Then fill the reference array */
931
932
132k
        WORD32 ref = ps_lap_out->i4_poc;
933
934
132k
        if(ps_lap_out->i4_is_ref_pic && ref > *p_ref_poc_array)
935
132k
        {
936
132k
            *p_ref_poc_array = ref;
937
132k
        }
938
132k
    }
939
940
132k
    return;
941
132k
}
942
943
/*!
944
************************************************************************
945
* \brief
946
*    determine next sub-gop state
947
************************************************************************
948
*/
949
void ihevce_determine_next_sub_gop_state(lap_struct_t *ps_lap_struct)
950
133k
{
951
133k
    WORD32 i4_num_b_frames = -1;
952
133k
    WORD32 i4_sd = ps_lap_struct->i4_sub_gop_size;
953
133k
    WORD32 i4_sd_idr = ps_lap_struct->i4_sub_gop_size_idr;
954
133k
    WORD32 i4_Midr = ps_lap_struct->i4_max_idr_period;
955
133k
    WORD32 i4_midr = ps_lap_struct->i4_min_idr_period;
956
133k
    WORD32 i4_Mcra = ps_lap_struct->i4_max_cra_period;
957
133k
    WORD32 i4_Mi = ps_lap_struct->i4_max_i_period;
958
133k
    WORD32 i4_Cd = ps_lap_struct->i4_idr_counter;
959
133k
    WORD32 i4_Cc = ps_lap_struct->i4_cra_counter;
960
133k
    WORD32 i4_Ci = ps_lap_struct->i4_i_counter;
961
962
133k
    if(ps_lap_struct->i4_force_idr_pos)
963
577
    {
964
577
        ps_lap_struct->i4_num_frm_type_decided = 1;
965
577
        ps_lap_struct->ai1_pic_type[0] = PIC_TYPE_IDR;
966
577
        ps_lap_struct->i4_idr_counter = 0;
967
577
        ps_lap_struct->i4_cra_counter = 0;
968
577
        ps_lap_struct->i4_i_counter = 0;
969
577
        ps_lap_struct->i4_force_idr_pos = 0;
970
577
        ps_lap_struct->i4_sub_gop_pic_idx = 0;
971
577
    }
972
973
133k
    if(i4_Midr)
974
133k
        ASSERT(i4_Cd < i4_Midr);
975
976
133k
    if(i4_Mcra)
977
133k
        ASSERT(i4_Cc < i4_Mcra);
978
979
133k
    if(i4_Mi)
980
133k
        ASSERT(i4_Ci < i4_Mi);
981
982
    /*if all are i pictures */
983
133k
    if((i4_Midr == 1) || (i4_Mcra == 1) || (i4_Mi == 1))
984
16.5k
    {
985
16.5k
        ps_lap_struct->i4_num_frm_type_decided = 1;
986
16.5k
        if((i4_Midr == 1) || ((i4_Cd + i4_sd) == i4_Midr))
987
7.81k
        {
988
7.81k
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_IDR;
989
7.81k
            ps_lap_struct->i4_idr_counter = 0;
990
7.81k
            ps_lap_struct->i4_cra_counter = 0;
991
7.81k
            ps_lap_struct->i4_i_counter = 0;
992
7.81k
        }
993
8.77k
        else if((i4_Mcra == 1) || ((i4_Cc + i4_sd) == i4_Mcra))
994
4.15k
        {
995
4.15k
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_CRA;
996
4.15k
            ps_lap_struct->i4_idr_counter += 1;
997
4.15k
            ps_lap_struct->i4_cra_counter = 0;
998
4.15k
            ps_lap_struct->i4_i_counter = 0;
999
4.15k
        }
1000
4.62k
        else
1001
4.62k
        {
1002
4.62k
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_I;
1003
4.62k
            ps_lap_struct->i4_idr_counter += 1;
1004
4.62k
            ps_lap_struct->i4_cra_counter += 1;
1005
4.62k
            ps_lap_struct->i4_i_counter = 0;
1006
4.62k
        }
1007
16.5k
        return;
1008
16.5k
    }
1009
1010
116k
    if((i4_Cd + i4_sd_idr >= i4_Midr) && i4_Midr)
1011
13.3k
    {
1012
        /*if idr falls already on sub-gop aligned w.r.t Midr or if strict idr use case*/
1013
13.3k
        if(i4_sd_idr != i4_sd)
1014
3.08k
        {
1015
3.08k
            i4_num_b_frames = i4_Midr - i4_Cd - 2;
1016
3.08k
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1017
3.08k
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_P;
1018
3.08k
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 2] = PIC_TYPE_IDR;
1019
3.08k
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 2;
1020
3.08k
            ps_lap_struct->i4_idr_counter = 0;
1021
3.08k
            ps_lap_struct->i4_cra_counter = 0;
1022
3.08k
            ps_lap_struct->i4_i_counter = 0;
1023
3.08k
        }
1024
10.3k
        else
1025
10.3k
        {
1026
10.3k
            i4_num_b_frames = 0;
1027
10.3k
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_IDR;
1028
10.3k
            ps_lap_struct->i4_num_frm_type_decided = 1;
1029
10.3k
            ps_lap_struct->i4_idr_counter = 0;
1030
10.3k
            ps_lap_struct->i4_cra_counter = 0;
1031
10.3k
            ps_lap_struct->i4_i_counter = 0;
1032
10.3k
        }
1033
13.3k
    }
1034
    /*if next sub gop is going to have CRA as Cc reaches Mcra*/
1035
103k
    else if(((i4_Cc + i4_sd) >= i4_Mcra) && i4_Mcra)
1036
3.74k
    {
1037
3.74k
        if(((i4_Cc + i4_sd) == i4_Mcra) || (1 == ps_lap_struct->i4_fixed_open_gop_period))
1038
3.74k
        {
1039
3.74k
            i4_num_b_frames = i4_Mcra - i4_Cc - 1;
1040
3.74k
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1041
3.74k
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_CRA;
1042
3.74k
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1043
3.74k
            ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1044
3.74k
            ps_lap_struct->i4_cra_counter = 0;
1045
3.74k
            ps_lap_struct->i4_i_counter = 0;
1046
3.74k
        }
1047
0
        else
1048
0
        {
1049
0
            ps_lap_struct->ai1_pic_type[0] = PIC_TYPE_CRA;
1050
0
            i4_num_b_frames = i4_sd - 1;
1051
0
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1052
0
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_P;
1053
0
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1054
0
            ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1055
0
            ps_lap_struct->i4_cra_counter = ps_lap_struct->i4_num_frm_type_decided;
1056
0
            ps_lap_struct->i4_i_counter = ps_lap_struct->i4_num_frm_type_decided;
1057
0
        }
1058
3.74k
    }
1059
    /*if next sub gop is going to have I_slice as Ci reaches Mi*/
1060
99.6k
    else if((i4_Ci + i4_sd >= i4_Mi) && i4_Mi)
1061
3.11k
    {
1062
3.11k
        if(((i4_Ci + i4_sd) == i4_Mi) || (1 == ps_lap_struct->i4_fixed_i_period))
1063
3.11k
        {
1064
3.11k
            i4_num_b_frames = i4_Mi - i4_Ci - 1;
1065
3.11k
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1066
3.11k
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_I;
1067
3.11k
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1068
3.11k
            ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1069
3.11k
            ps_lap_struct->i4_cra_counter += ps_lap_struct->i4_num_frm_type_decided;
1070
3.11k
            ps_lap_struct->i4_i_counter = 0;
1071
3.11k
        }
1072
0
        else
1073
0
        {
1074
0
            ps_lap_struct->ai1_pic_type[0] = PIC_TYPE_I;
1075
0
            i4_num_b_frames = i4_sd - 1;
1076
0
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1077
0
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_P;
1078
0
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1079
0
            ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1080
0
            ps_lap_struct->i4_cra_counter += ps_lap_struct->i4_num_frm_type_decided;
1081
0
            ps_lap_struct->i4_i_counter = ps_lap_struct->i4_num_frm_type_decided;
1082
0
        }
1083
3.11k
    }
1084
    /* if next sub-gop is not going to be idr,cra,I*/
1085
96.5k
    else
1086
96.5k
    {
1087
96.5k
        i4_num_b_frames = i4_sd - 1;
1088
96.5k
        memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1089
96.5k
        ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_P;
1090
96.5k
        ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1091
96.5k
        ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1092
96.5k
        ps_lap_struct->i4_cra_counter += ps_lap_struct->i4_num_frm_type_decided;
1093
96.5k
        ps_lap_struct->i4_i_counter += ps_lap_struct->i4_num_frm_type_decided;
1094
96.5k
    }
1095
116k
    ASSERT(i4_num_b_frames != -1);
1096
1097
116k
    return;
1098
116k
}
1099
1100
/*!
1101
************************************************************************
1102
* \brief
1103
*    assign pic type to input buf
1104
************************************************************************
1105
*/
1106
void ihevce_assign_pic_type(lap_struct_t *ps_lap_struct, ihevce_lap_enc_buf_t *ps_lap_inp_buf)
1107
158k
{
1108
158k
    WORD8 pic_type = ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
1109
1110
158k
    switch(pic_type)
1111
158k
    {
1112
7.13k
    case PIC_TYPE_I:
1113
7.13k
    {
1114
7.13k
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_I_FRAME;
1115
7.13k
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1116
7.13k
        ps_lap_inp_buf->s_lap_out.i4_is_I_in_any_field = 1;
1117
7.13k
        break;
1118
0
    }
1119
88.9k
    case PIC_TYPE_P:
1120
88.9k
    {
1121
88.9k
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_P_FRAME;
1122
88.9k
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1123
88.9k
        break;
1124
0
    }
1125
26.7k
    case PIC_TYPE_B:
1126
26.7k
    {
1127
26.7k
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_B_FRAME;
1128
26.7k
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1129
26.7k
        break;
1130
0
    }
1131
28.1k
    case PIC_TYPE_IDR:
1132
28.1k
    {
1133
28.1k
        ps_lap_struct->i4_curr_poc = 0;
1134
28.1k
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_IDR_FRAME;
1135
28.1k
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1136
28.1k
        break;
1137
0
    }
1138
7.01k
    case PIC_TYPE_CRA:
1139
7.01k
    {
1140
7.01k
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_I_FRAME;
1141
7.01k
        ps_lap_inp_buf->s_lap_out.i4_is_I_in_any_field = 1;
1142
7.01k
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 1;
1143
7.01k
        break;
1144
0
    }
1145
0
    default:
1146
0
        ASSERT(0);
1147
158k
    }
1148
158k
    return;
1149
158k
}
1150
1151
/*!
1152
************************************************************************
1153
* \brief
1154
*    capture order traversal nodes
1155
************************************************************************
1156
*/
1157
void ihevce_encode_order_traversal_nodes(
1158
    ihevce_encode_node_t *encode_node_t,
1159
    ihevce_lap_enc_buf_t **encode_order,
1160
    WORD32 *loop_count,
1161
    WORD32 curr_layer,
1162
    lap_struct_t *ps_lap_struct)
1163
84.1k
{
1164
84.1k
    if(encode_node_t == NULL)
1165
48.0k
        return;
1166
1167
36.0k
    encode_order[*loop_count] = (ihevce_lap_enc_buf_t *)encode_node_t->ps_lap_top_buff;
1168
1169
36.0k
    if(encode_order[*loop_count] != NULL)
1170
26.0k
    {
1171
26.0k
        ihevce_lap_enc_buf_t *ps_lap_inp;
1172
1173
26.0k
        ps_lap_struct->pi4_encode_poc_ptr[0] = encode_node_t->data;
1174
26.0k
        ref_b_pic_population(curr_layer, encode_order[*loop_count], ps_lap_struct);
1175
1176
26.0k
        ps_lap_inp = (ihevce_lap_enc_buf_t *)encode_order[*loop_count];
1177
26.0k
        ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1178
1179
26.0k
        ps_lap_struct->pi4_encode_poc_ptr++;
1180
26.0k
    }
1181
1182
36.0k
    (*loop_count) = (*loop_count) + 1;
1183
1184
    /* Pre-order Left-node traversal*/
1185
36.0k
    ihevce_encode_order_traversal_nodes(
1186
36.0k
        (ihevce_encode_node_t *)encode_node_t->pv_left_node,
1187
36.0k
        encode_order,
1188
36.0k
        loop_count,
1189
36.0k
        curr_layer + 1,
1190
36.0k
        ps_lap_struct);
1191
1192
    /* Pre-order Right-node traversal*/
1193
36.0k
    ihevce_encode_order_traversal_nodes(
1194
36.0k
        (ihevce_encode_node_t *)encode_node_t->pv_right_node,
1195
36.0k
        encode_order,
1196
36.0k
        loop_count,
1197
36.0k
        curr_layer + 1,
1198
36.0k
        ps_lap_struct);
1199
36.0k
}
1200
1201
/*!
1202
************************************************************************
1203
* \brief
1204
*    capture order traversal nodes
1205
************************************************************************
1206
*/
1207
void ihevce_capture_order_traversal_nodes(
1208
    ihevce_encode_node_t *encode_node_t,
1209
    ihevce_lap_enc_buf_t **api4_capture_order_array,
1210
    WORD32 *capture_order_poc_array,
1211
    WORD32 *loop_count,
1212
    WORD32 i4_interlace_field)
1213
84.1k
{
1214
84.1k
    if(encode_node_t == NULL)
1215
48.0k
        return;
1216
1217
    /* Inorder Insertion for the left-child node */
1218
36.0k
    ihevce_capture_order_traversal_nodes(
1219
36.0k
        (ihevce_encode_node_t *)encode_node_t->pv_left_node,
1220
36.0k
        api4_capture_order_array,
1221
36.0k
        capture_order_poc_array,
1222
36.0k
        loop_count,
1223
36.0k
        i4_interlace_field);
1224
1225
36.0k
    if(i4_interlace_field)
1226
0
    {
1227
0
        encode_node_t->ps_lap_top_buff =
1228
0
            (ihevce_lap_enc_buf_t *)api4_capture_order_array[*loop_count];
1229
0
        encode_node_t->data = capture_order_poc_array[*loop_count];
1230
0
        encode_node_t->ps_lap_bottom_buff =
1231
0
            (ihevce_lap_enc_buf_t *)api4_capture_order_array[*loop_count + 1];
1232
0
    }
1233
36.0k
    else
1234
36.0k
    {
1235
36.0k
        encode_node_t->ps_lap_top_buff =
1236
36.0k
            (ihevce_lap_enc_buf_t *)api4_capture_order_array[*loop_count];
1237
36.0k
        encode_node_t->data = capture_order_poc_array[*loop_count];
1238
36.0k
    }
1239
36.0k
    if(i4_interlace_field)
1240
0
        (*loop_count) = (*loop_count) + 2;
1241
36.0k
    else
1242
36.0k
        (*loop_count) = (*loop_count) + 1;
1243
1244
    /* Inorder Insertion for the right-child node */
1245
36.0k
    ihevce_capture_order_traversal_nodes(
1246
36.0k
        (ihevce_encode_node_t *)encode_node_t->pv_right_node,
1247
36.0k
        api4_capture_order_array,
1248
36.0k
        capture_order_poc_array,
1249
36.0k
        loop_count,
1250
36.0k
        i4_interlace_field);
1251
36.0k
}
1252
1253
/*!
1254
************************************************************************
1255
* \brief
1256
*    I/P pic population
1257
************************************************************************
1258
*/
1259
void ihevce_ip_pic_population(
1260
    ihevce_encode_node_t *ps_encode_node, lap_struct_t *ps_lap_struct, WORD32 i4_first_gop)
1261
135k
{
1262
135k
    ihevce_lap_enc_buf_t *ps_lap_inp = NULL;
1263
135k
    WORD32 hier_layer = ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
1264
135k
    WORD32 sub_gop_size = ps_lap_struct->i4_dyn_sub_gop_size;
1265
135k
    ihevce_lap_enc_buf_t **api4_capture_order_array = ps_lap_struct->api4_capture_order_array;
1266
135k
    ihevce_lap_enc_buf_t **api4_encode_order_array = ps_lap_struct->api4_encode_order_array;
1267
135k
    WORD32 *ai4_capture_order_poc = ps_lap_struct->pi4_capture_poc_ptr;
1268
1269
    /* Populate the encode order POC dependent on IDR frames and Interlace Field*/
1270
135k
    if(1 == ps_lap_struct->i4_idr_flag)
1271
28.1k
    {
1272
28.1k
        if(i4_first_gop)
1273
8.22k
        {
1274
8.22k
            api4_encode_order_array[0] = api4_capture_order_array[0];
1275
1276
8.22k
            if(api4_encode_order_array[0] != NULL)
1277
8.22k
            {
1278
8.22k
                ps_lap_struct->pi4_encode_poc_ptr[0] = ai4_capture_order_poc[0];
1279
8.22k
                ref_pic_population(api4_encode_order_array[0], ps_lap_struct);
1280
1281
8.22k
                ps_lap_inp = api4_encode_order_array[0];
1282
8.22k
                ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1283
1284
8.22k
                ps_lap_struct->pi4_encode_poc_ptr++;
1285
8.22k
            }
1286
1287
8.22k
            if(ps_lap_struct->i4_immediate_idr_case != 1)
1288
0
            {
1289
0
                api4_encode_order_array[1] = api4_capture_order_array[sub_gop_size];
1290
1291
0
                if(api4_encode_order_array[1] != NULL)
1292
0
                {
1293
0
                    ps_lap_struct->pi4_encode_poc_ptr[0] = ai4_capture_order_poc[sub_gop_size];
1294
0
                    ref_pic_population(api4_encode_order_array[1], ps_lap_struct);
1295
1296
0
                    ps_lap_inp = api4_encode_order_array[1];
1297
0
                    ihevce_rc_populate_common_params(
1298
0
                        &ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1299
1300
0
                    ps_lap_struct->pi4_encode_poc_ptr++;
1301
0
                }
1302
0
            }
1303
8.22k
        }
1304
19.9k
        else
1305
19.9k
        {
1306
19.9k
            api4_encode_order_array[0] = api4_capture_order_array[sub_gop_size - 1];
1307
1308
19.9k
            if(api4_encode_order_array[0] != NULL)
1309
19.9k
            {
1310
19.9k
                ps_lap_struct->pi4_encode_poc_ptr[0] = ai4_capture_order_poc[sub_gop_size - 1];
1311
19.9k
                ref_pic_population(api4_encode_order_array[0], ps_lap_struct);
1312
1313
19.9k
                ps_lap_inp = api4_encode_order_array[0];
1314
19.9k
                ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1315
1316
19.9k
                ps_lap_struct->pi4_encode_poc_ptr++;
1317
19.9k
            }
1318
19.9k
        }
1319
28.1k
    }
1320
107k
    else
1321
107k
    {
1322
107k
        api4_encode_order_array[0] = api4_capture_order_array[sub_gop_size - 1];
1323
1324
107k
        if(api4_encode_order_array[0] != NULL)
1325
103k
        {
1326
103k
            ps_lap_struct->pi4_encode_poc_ptr[0] = ai4_capture_order_poc[sub_gop_size - 1];
1327
103k
            ref_pic_population(api4_encode_order_array[0], ps_lap_struct);
1328
1329
103k
            ps_lap_inp = api4_encode_order_array[0];
1330
103k
            ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1331
1332
103k
            ps_lap_struct->pi4_encode_poc_ptr++;
1333
103k
        }
1334
107k
    }
1335
135k
    return;
1336
135k
}
1337
1338
/*!
1339
************************************************************************
1340
* \brief
1341
*    B pic population
1342
************************************************************************
1343
*/
1344
void ihevce_b_pic_population(ihevce_encode_node_t *ps_encode_node, lap_struct_t *ps_lap_struct)
1345
12.0k
{
1346
12.0k
    WORD32 interlace_field = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
1347
12.0k
    ihevce_lap_enc_buf_t **api4_encode_order_array = ps_lap_struct->api4_encode_order_array;
1348
12.0k
    WORD32 *capture_order_poc_array = ps_lap_struct->pi4_capture_poc_ptr;
1349
12.0k
    WORD32 loop_count = 0;
1350
1351
    /* encoder_order offset changed dependent on IDR and Interlace Field */
1352
12.0k
    if(ps_lap_struct->i4_idr_flag)
1353
0
        loop_count = 1 + interlace_field;
1354
1355
    /* Inorder Insertion of POC in tree, for capture order */
1356
12.0k
    ihevce_capture_order_traversal_nodes(
1357
12.0k
        ps_encode_node,
1358
12.0k
        &ps_lap_struct->api4_capture_order_array[0],
1359
12.0k
        capture_order_poc_array,
1360
12.0k
        &loop_count,
1361
12.0k
        interlace_field);
1362
1363
    /* encoder_order offset changed dependent on IDR and Interlace Field */
1364
    /* If the gop_size is multiple of CRA period , decrement loop count */
1365
12.0k
    if(ps_lap_struct->i4_idr_flag)
1366
0
        loop_count = 2 + (interlace_field * 2);
1367
12.0k
    else
1368
12.0k
        loop_count = 1 + interlace_field;
1369
1370
    /* Pre-order traversal of the tree to get encode-order POCs*/
1371
12.0k
    ihevce_encode_order_traversal_nodes(
1372
12.0k
        ps_encode_node, api4_encode_order_array, &loop_count, 1, ps_lap_struct);
1373
1374
12.0k
    return;
1375
12.0k
}
1376
1377
/*!
1378
************************************************************************
1379
* \brief
1380
*    rc_update_model_control_by_lap_for_modified_sub_gop
1381
************************************************************************
1382
*/
1383
void rc_update_model_control_by_lap_for_modified_sub_gop(
1384
    lap_struct_t *ps_lap_struct, ihevce_lap_enc_buf_t *ps_lap_out_buf)
1385
149k
{
1386
149k
    ihevce_lap_output_params_t *ps_lap_out = &ps_lap_out_buf->s_lap_out;
1387
1388
    /* model update flag for rc*/
1389
149k
    if(ps_lap_out->i4_pic_type == IV_P_FRAME)
1390
84.5k
    {
1391
84.5k
        WORD32 i4_loop = 0;
1392
84.5k
        WORD32 i4_min_delta_poc = 0x7FFFFFFF;
1393
1394
337k
        for(i4_loop = 0; i4_loop < ps_lap_out->i4_num_ref_pics; i4_loop++)
1395
252k
        {
1396
252k
            if(i4_min_delta_poc > ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc))
1397
84.5k
            {
1398
84.5k
                i4_min_delta_poc = ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc);
1399
84.5k
            }
1400
252k
        }
1401
84.5k
    }
1402
1403
149k
    if(ps_lap_out->i4_pic_type == IV_B_FRAME)
1404
24.0k
    {
1405
24.0k
        WORD32 i4_loop = 0;
1406
24.0k
        WORD32 i4_min_delta_poc = 0x7FFFFFFF;
1407
24.0k
        WORD32 i4_min_delta_poc_for_b =
1408
24.0k
            (1 << ps_lap_struct->s_lap_static_params.i4_max_temporal_layers) /
1409
24.0k
            (ps_lap_out->i4_temporal_lyr_id + 1);
1410
1411
105k
        for(i4_loop = 0; i4_loop < ps_lap_out->i4_num_ref_pics; i4_loop++)
1412
81.2k
        {
1413
81.2k
            if(i4_min_delta_poc > ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc))
1414
24.0k
            {
1415
24.0k
                i4_min_delta_poc = ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc);
1416
24.0k
            }
1417
81.2k
        }
1418
24.0k
    }
1419
149k
    return;
1420
149k
}
1421
1422
/*!
1423
************************************************************************
1424
* \brief
1425
*    Update num of pic type for rc
1426
************************************************************************
1427
*/
1428
void update_rc_num_pic_type(lap_struct_t *ps_lap_struct, ihevce_lap_enc_buf_t *ps_lap_out_buf)
1429
149k
{
1430
149k
    WORD32 i4_field_flag = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
1431
149k
    rc_lap_out_params_t *ps_rc_lap_out = &ps_lap_out_buf->s_rc_lap_out;
1432
1433
149k
    ps_lap_struct->i4_lap2_counter++;
1434
1435
149k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_I_FRAME ||
1436
149k
       ps_lap_out_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME)
1437
41.1k
    {
1438
41.1k
        ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = I_PIC;
1439
41.1k
        GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1440
41.1k
    }
1441
108k
    else if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_P_FRAME)
1442
84.5k
    {
1443
84.5k
        if(ps_lap_out_buf->s_lap_out.i4_first_field)
1444
84.5k
        {
1445
84.5k
            ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = P_PIC;
1446
84.5k
        }
1447
0
        else
1448
0
        {
1449
0
            ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = P1_PIC;
1450
0
        }
1451
84.5k
        GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1452
84.5k
    }
1453
24.0k
    else if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_B_FRAME)
1454
24.0k
    {
1455
24.0k
        if(ps_lap_out_buf->s_lap_out.i4_temporal_lyr_id == 1)
1456
8.18k
        {
1457
8.18k
            if(ps_lap_out_buf->s_lap_out.i4_first_field)
1458
8.18k
            {
1459
8.18k
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = B_PIC;
1460
8.18k
            }
1461
0
            else
1462
0
            {
1463
0
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = BB_PIC;
1464
0
            }
1465
8.18k
            GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1466
8.18k
        }
1467
15.8k
        else if(ps_lap_out_buf->s_lap_out.i4_temporal_lyr_id == 2)
1468
15.8k
        {
1469
15.8k
            if(ps_lap_out_buf->s_lap_out.i4_first_field)
1470
15.8k
            {
1471
15.8k
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = B1_PIC;
1472
15.8k
            }
1473
0
            else
1474
0
            {
1475
0
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = B11_PIC;
1476
0
            }
1477
15.8k
            GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1478
15.8k
        }
1479
0
        else if(ps_lap_out_buf->s_lap_out.i4_temporal_lyr_id == 3)
1480
0
        {
1481
0
            if(ps_lap_out_buf->s_lap_out.i4_first_field)
1482
0
            {
1483
0
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = B2_PIC;
1484
0
            }
1485
0
            else
1486
0
            {
1487
0
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = B22_PIC;
1488
0
            }
1489
0
            GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1490
0
        }
1491
0
        else
1492
0
        {
1493
0
            ASSERT(0);
1494
0
        }
1495
24.0k
    }
1496
0
    else
1497
0
    {
1498
0
        ASSERT(0);
1499
0
    }
1500
1501
149k
    if(!ps_lap_struct->i4_rc_lap_period)
1502
0
    {
1503
0
        if(ps_lap_struct->i4_rc_lap_period < ps_lap_struct->i4_gop_period)
1504
0
        {
1505
0
            WORD32 i4_loop;
1506
0
            WORD32 idx = 0;
1507
0
            WORD32 i4_max_temporal_layer =
1508
0
                ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
1509
1510
0
            for(i4_loop = 0;
1511
0
                i4_loop < (ps_lap_struct->i4_gop_period - ps_lap_struct->i4_rc_lap_period);
1512
0
                i4_loop++)
1513
0
            {
1514
0
                ps_rc_lap_out->i4_next_sc_i_in_rc_look_ahead++;
1515
1516
0
                if(i4_max_temporal_layer == 0)
1517
0
                {
1518
0
                    if(ps_lap_struct->i4_is_all_i_pic_in_seq)
1519
0
                    {
1520
0
                        ps_rc_lap_out->ai4_num_pic_type[I_PIC]++;
1521
0
                    }
1522
0
                    else
1523
0
                    {
1524
                        /*second field*/
1525
0
                        if((i4_loop & 1) && i4_field_flag)
1526
0
                        {
1527
0
                            ps_rc_lap_out->ai4_num_pic_type[P1_PIC]++;
1528
0
                        }
1529
0
                        else
1530
0
                        {
1531
0
                            ps_rc_lap_out->ai4_num_pic_type[P_PIC]++;
1532
0
                        }
1533
0
                    }
1534
0
                }
1535
0
                else
1536
0
                {
1537
0
                    ps_rc_lap_out->ai4_num_pic_type
1538
0
                        [gau1_order_insert_pic_type[i4_max_temporal_layer - 1][idx]]++;
1539
1540
0
                    GET_IDX_CIRCULAR_BUF(idx, 1, (8 << i4_field_flag));
1541
0
                }
1542
0
            }
1543
0
        }
1544
0
    }
1545
149k
    else
1546
149k
    {
1547
149k
        ASSERT(ps_lap_struct->i4_lap2_counter <= ps_lap_struct->i4_rc_lap_period);
1548
1549
149k
        if(ps_lap_struct->i4_lap2_counter == ps_lap_struct->i4_rc_lap_period)
1550
149k
        {
1551
149k
            WORD32 i4_loop, i4_period, i4_next_i_pic = 0;
1552
149k
            WORD32 i4_stop_count = 0;
1553
149k
            WORD32 i4_temp_deq = ps_lap_struct->i4_deq_idx;
1554
149k
            WORD32 i4_first_pic_type = ps_lap_struct->ai4_pic_type_to_be_removed[i4_temp_deq];
1555
1556
149k
            if(ps_lap_struct->i4_rc_lap_period >= ps_lap_struct->i4_gop_period)
1557
12.5k
            {
1558
12.5k
                i4_period = ps_lap_struct->i4_gop_period;
1559
12.5k
            }
1560
137k
            else
1561
137k
            {
1562
137k
                i4_period = ps_lap_struct->i4_rc_lap_period;
1563
137k
            }
1564
1565
299k
            for(i4_loop = 0; i4_loop < i4_period; i4_loop++)
1566
149k
            {
1567
149k
                if(ps_lap_struct->ai4_pic_type_to_be_removed[i4_temp_deq] == I_PIC && i4_loop &&
1568
149k
                   i4_first_pic_type == I_PIC)
1569
0
                {
1570
0
                    i4_stop_count = 1;
1571
0
                }
1572
1573
149k
                if(!i4_stop_count)
1574
149k
                {
1575
149k
                    ps_rc_lap_out->i4_next_sc_i_in_rc_look_ahead++;
1576
149k
                }
1577
1578
149k
                ps_rc_lap_out
1579
149k
                    ->ai4_num_pic_type[ps_lap_struct->ai4_pic_type_to_be_removed[i4_temp_deq]]++;
1580
1581
149k
                GET_IDX_CIRCULAR_BUF(i4_temp_deq, 1, NUM_LAP2_LOOK_AHEAD);
1582
149k
            }
1583
149k
            if(ps_lap_struct->i4_rc_lap_period < ps_lap_struct->i4_gop_period)
1584
137k
            {
1585
137k
                WORD32 i4_loop;
1586
137k
                WORD32 idx = 0;
1587
137k
                WORD32 i4_max_temporal_layer =
1588
137k
                    ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
1589
1590
137k
                for(i4_loop = 0;
1591
2.83M
                    i4_loop < (ps_lap_struct->i4_gop_period - ps_lap_struct->i4_rc_lap_period) &&
1592
2.83M
                    (!i4_next_i_pic);
1593
2.69M
                    i4_loop++)
1594
2.69M
                {
1595
2.69M
                    if(!i4_stop_count)
1596
2.69M
                    {
1597
2.69M
                        ps_rc_lap_out->i4_next_sc_i_in_rc_look_ahead++;
1598
2.69M
                    }
1599
1600
2.69M
                    if(i4_max_temporal_layer == 0)
1601
2.29M
                    {
1602
2.29M
                        if(ps_lap_struct->i4_is_all_i_pic_in_seq)
1603
18.3k
                        {
1604
18.3k
                            ps_rc_lap_out->ai4_num_pic_type[I_PIC]++;
1605
18.3k
                        }
1606
2.27M
                        else
1607
2.27M
                        {
1608
                            /*second field*/
1609
2.27M
                            if((i4_loop & 1) && i4_field_flag)
1610
0
                            {
1611
0
                                ps_rc_lap_out->ai4_num_pic_type[P1_PIC]++;
1612
0
                            }
1613
2.27M
                            else
1614
2.27M
                            {
1615
2.27M
                                ps_rc_lap_out->ai4_num_pic_type[P_PIC]++;
1616
2.27M
                            }
1617
2.27M
                        }
1618
2.29M
                    }
1619
399k
                    else
1620
399k
                    {
1621
399k
                        ps_rc_lap_out->ai4_num_pic_type
1622
399k
                            [gau1_order_insert_pic_type[i4_max_temporal_layer - 1][idx]]++;
1623
399k
                        GET_IDX_CIRCULAR_BUF(idx, 1, (8 << i4_field_flag));
1624
399k
                    }
1625
2.69M
                }
1626
137k
            }
1627
            /*remove one pic type*/
1628
149k
            GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_deq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1629
149k
            ps_lap_struct->i4_lap2_counter--;
1630
149k
        }
1631
149k
    }
1632
1633
149k
    {
1634
149k
        WORD32 i4_loop;
1635
149k
        WORD32 idx = 0;
1636
149k
        WORD32 i4_max_temporal_layer = ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
1637
149k
        WORD32 i4_num_pictype = 0;
1638
1639
1.49M
        for(i4_loop = 0; i4_loop < MAX_PIC_TYPE; i4_loop++)
1640
1.34M
        {
1641
1.34M
            i4_num_pictype += ps_rc_lap_out->ai4_num_pic_type[i4_loop];
1642
1.34M
        }
1643
1644
149k
        if(!i4_num_pictype)
1645
0
        {
1646
0
            ps_rc_lap_out->i4_next_sc_i_in_rc_look_ahead = ps_lap_struct->i4_gop_period;
1647
1648
0
            for(i4_loop = 0; i4_loop < (ps_lap_struct->i4_gop_period); i4_loop++)
1649
0
            {
1650
0
                if(i4_max_temporal_layer == 0)
1651
0
                {
1652
0
                    if(ps_lap_struct->i4_is_all_i_pic_in_seq)
1653
0
                    {
1654
0
                        ps_rc_lap_out->ai4_num_pic_type[I_PIC]++;
1655
0
                    }
1656
0
                    else
1657
0
                    {
1658
                        /*second field*/
1659
0
                        if((i4_loop & 1) && i4_field_flag)
1660
0
                        {
1661
0
                            ps_rc_lap_out->ai4_num_pic_type[P1_PIC]++;
1662
0
                        }
1663
0
                        else
1664
0
                        {
1665
0
                            ps_rc_lap_out->ai4_num_pic_type[P_PIC]++;
1666
0
                        }
1667
0
                    }
1668
0
                }
1669
0
                else
1670
0
                {
1671
0
                    ps_rc_lap_out->ai4_num_pic_type
1672
0
                        [gau1_order_insert_pic_type[i4_max_temporal_layer - 1][idx]]++;
1673
1674
0
                    GET_IDX_CIRCULAR_BUF(idx, 1, (8 << i4_field_flag));
1675
0
                }
1676
0
            }
1677
0
        }
1678
149k
    }
1679
    /*FOR RC : ensure  at least 1 I pic in the gop period at any case*/
1680
149k
    if(!ps_rc_lap_out->ai4_num_pic_type[I_PIC])
1681
108k
    {
1682
108k
        ASSERT(ps_rc_lap_out->ai4_num_pic_type[P_PIC]);
1683
108k
        ps_lap_out_buf->s_rc_lap_out.ai4_num_pic_type[P_PIC]--;
1684
108k
        ps_lap_out_buf->s_rc_lap_out.ai4_num_pic_type[I_PIC]++;
1685
108k
    }
1686
149k
    return;
1687
149k
}
1688
1689
/*!
1690
************************************************************************
1691
* \brief
1692
*    pre rel lap output update
1693
************************************************************************
1694
*/
1695
void ihevce_pre_rel_lapout_update(lap_struct_t *ps_lap_struct, ihevce_lap_enc_buf_t *ps_lap_out_buf)
1696
149k
{
1697
149k
    WORD32 i4_first_field = 1;
1698
149k
    WORD32 i4_field = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
1699
1700
149k
    if(i4_field)
1701
0
    {
1702
0
        i4_first_field = ps_lap_out_buf->s_lap_out.i4_first_field;
1703
0
    }
1704
1705
149k
    ps_lap_out_buf->s_lap_out.i4_used = 0;
1706
1707
149k
    rc_update_model_control_by_lap_for_modified_sub_gop(ps_lap_struct, ps_lap_out_buf);
1708
149k
    update_rc_num_pic_type(ps_lap_struct, ps_lap_out_buf);
1709
1710
    /* curr buf next is null, prev buf next is curr and prev buff equal to curr*/
1711
1712
149k
    ps_lap_out_buf->s_rc_lap_out.ps_rc_lap_out_next_encode = NULL;
1713
149k
    if(ps_lap_struct->pv_prev_inp_buf != NULL &&
1714
149k
       ps_lap_struct->s_lap_static_params.s_lap_params.i4_rc_look_ahead_pics)
1715
0
    {
1716
0
        ((ihevce_lap_enc_buf_t *)ps_lap_struct->pv_prev_inp_buf)
1717
0
            ->s_rc_lap_out.ps_rc_lap_out_next_encode = (void *)&ps_lap_out_buf->s_rc_lap_out;
1718
0
    }
1719
1720
149k
    ps_lap_struct->pv_prev_inp_buf = (void *)ps_lap_out_buf;
1721
149k
    ps_lap_out_buf->s_lap_out.i4_is_prev_pic_in_Tid0_same_scene = 1;
1722
1723
    /*with force idr below check is not valid*/
1724
#if(!FORCE_IDR_TEST)
1725
    if(ps_lap_struct->i4_max_idr_period == ps_lap_struct->i4_min_idr_period)
1726
    {
1727
        if(!ps_lap_out_buf->s_lap_out.i4_poc)
1728
        {
1729
            ASSERT(ps_lap_struct->i4_max_prev_poc == (ps_lap_struct->i4_max_idr_period - 1));
1730
            ps_lap_struct->i4_max_prev_poc = 0;
1731
        }
1732
    }
1733
#endif
1734
1735
    /*assert if num of reference frame is zero in case of P or B frame*/
1736
149k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_P_FRAME ||
1737
149k
       ps_lap_out_buf->s_lap_out.i4_pic_type == IV_B_FRAME)
1738
108k
    {
1739
108k
        ASSERT(ps_lap_out_buf->s_lap_out.i4_num_ref_pics != 0);
1740
108k
    }
1741
1742
    /*assert if poc = 0 and pictype is not an idr*/
1743
149k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type != IV_IDR_FRAME &&
1744
149k
       ps_lap_out_buf->s_lap_out.i4_poc == 0)
1745
0
    {
1746
0
        ASSERT(0);
1747
0
    }
1748
149k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME &&
1749
149k
       ps_lap_out_buf->s_lap_out.i4_poc != 0)
1750
0
    {
1751
0
        ASSERT(0);
1752
0
    }
1753
149k
    if(ps_lap_out_buf->s_lap_out.i4_poc < 0)
1754
0
    {
1755
0
        ASSERT(0);
1756
0
    }
1757
1758
#if(!FORCE_IDR_TEST)
1759
    if((!ps_lap_struct->i4_max_idr_period) && ps_lap_out_buf->s_lap_out.i4_display_num != 0)
1760
    {
1761
        ASSERT(ps_lap_out_buf->s_lap_out.i4_pic_type != IV_IDR_FRAME);
1762
    }
1763
#endif
1764
149k
    if(!ps_lap_struct->i4_max_cra_period)
1765
61.2k
    {
1766
61.2k
        ASSERT(ps_lap_out_buf->s_lap_out.i4_is_cra_pic != 1);
1767
61.2k
    }
1768
1769
149k
    if(ps_lap_out_buf->s_lap_out.i4_force_idr_flag)
1770
1.34k
    {
1771
1.34k
        ASSERT(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME);
1772
1.34k
    }
1773
149k
    ps_lap_out_buf->s_lap_out.i4_curr_frm_qp = -1;
1774
149k
}
1775
1776
/*!
1777
************************************************************************
1778
* \brief
1779
*    lap queue input
1780
************************************************************************
1781
*/
1782
void ihevce_lap_queue_input(
1783
    lap_struct_t *ps_lap_struct, ihevce_lap_enc_buf_t *ps_input_lap_enc_buf, WORD32 *pi4_tree_num)
1784
177k
{
1785
177k
    ihevce_encode_node_t *ps_encode_node =
1786
177k
        (ihevce_encode_node_t *)ps_lap_struct->aps_encode_node[*pi4_tree_num];
1787
1788
177k
    WORD32 i4_capture_idx = ps_lap_struct->i4_capture_idx;
1789
1790
    /* Static Lap parameters */
1791
177k
    ihevce_lap_static_params_t *ps_lap_static_params =
1792
177k
        (ihevce_lap_static_params_t *)&ps_lap_struct->s_lap_static_params;
1793
1794
177k
    WORD32 hier_layer = ps_lap_static_params->i4_max_temporal_layers;
1795
177k
    WORD32 sub_gop_size = ps_lap_struct->i4_dyn_sub_gop_size;
1796
1797
    /* queue the current input in capture array */
1798
177k
    {
1799
177k
        WORD32 first_gop_flag;
1800
1801
177k
        if(!i4_capture_idx)
1802
137k
        {
1803
137k
            memset(
1804
137k
                &ps_lap_struct->api4_capture_order_array[0],
1805
137k
                0,
1806
137k
                sizeof(ihevce_lap_enc_buf_t *) * MAX_NUM_ENC_NODES);
1807
137k
        }
1808
177k
        ps_lap_struct->api4_capture_order_array[i4_capture_idx] = ps_input_lap_enc_buf;
1809
1810
177k
        if(ps_input_lap_enc_buf != NULL)
1811
158k
        {
1812
158k
            if(ps_input_lap_enc_buf->s_lap_out.i4_end_flag == 1)
1813
8.22k
                ps_lap_struct->i4_end_flag_pic_idx = i4_capture_idx;
1814
158k
            ps_lap_struct->ai4_capture_order_poc[i4_capture_idx] = ps_lap_struct->i4_curr_poc++;
1815
158k
        }
1816
1817
177k
        if((1 == ps_lap_struct->i4_num_dummy_pic) && (ps_lap_struct->i4_sub_gop_end == 0))
1818
188
        {
1819
188
            ps_lap_struct->i4_sub_gop_end = i4_capture_idx - 1;
1820
188
        }
1821
177k
        i4_capture_idx++;
1822
1823
        /* to take care of buffering 1 extra picture at start or at IDR interval*/
1824
177k
        if(!ps_lap_struct->i4_is_all_i_pic_in_seq)
1825
160k
        {
1826
160k
            if(ps_lap_static_params->i4_src_interlace_field && sub_gop_size <= 2)
1827
0
            {
1828
0
                first_gop_flag = 0;
1829
0
            }
1830
160k
            else
1831
160k
            {
1832
160k
                first_gop_flag = ps_lap_struct->i4_idr_flag
1833
160k
                                 << ps_lap_static_params->i4_src_interlace_field;
1834
160k
            }
1835
160k
        }
1836
16.5k
        else
1837
16.5k
        {
1838
16.5k
            first_gop_flag = ps_lap_struct->i4_idr_flag;
1839
16.5k
        }
1840
1841
        /* For every IDR period, set idr_flag and reset POC value and gop_size to 0*/
1842
177k
        if(ps_input_lap_enc_buf != NULL)
1843
158k
        {
1844
158k
            if((!first_gop_flag) && (ps_input_lap_enc_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME))
1845
19.9k
            {
1846
19.9k
                ps_lap_struct->pi4_encode_poc_ptr = &ps_lap_struct->ai4_encode_order_poc[0];
1847
19.9k
                ps_lap_struct->i4_idr_flag = 1;
1848
19.9k
                ps_lap_struct->i4_curr_poc = 0;
1849
19.9k
                ps_lap_struct->ai4_capture_order_poc[i4_capture_idx - 1] =
1850
19.9k
                    ps_lap_struct->i4_curr_poc++;
1851
19.9k
            }
1852
158k
        }
1853
1854
177k
        if(first_gop_flag &&
1855
177k
           (ps_lap_struct->i4_is_all_i_pic_in_seq || ps_lap_struct->i4_immediate_idr_case))
1856
8.22k
        {
1857
8.22k
            sub_gop_size = 0;
1858
8.22k
        }
1859
1860
177k
        if(!first_gop_flag && ps_lap_struct->i4_immediate_idr_case &&
1861
177k
           (i4_capture_idx != (sub_gop_size + first_gop_flag)))
1862
1.98k
        {
1863
1.98k
            sub_gop_size = 1 << ps_lap_static_params->i4_src_interlace_field;
1864
1.98k
            ps_lap_struct->i4_dyn_sub_gop_size = 1 << ps_lap_static_params->i4_src_interlace_field;
1865
1.98k
        }
1866
1867
        /* reset the queue idx end of every gop */
1868
177k
        if(i4_capture_idx == (sub_gop_size + first_gop_flag))
1869
135k
        {
1870
135k
            ps_lap_struct->pi4_encode_poc_ptr = &ps_lap_struct->ai4_encode_order_poc[0];
1871
1872
135k
            if(ps_lap_struct->i4_end_flag_pic_idx && (1 != sub_gop_size))
1873
971
            {
1874
971
                WORD32 i4_temp_poc = 0;
1875
971
                ihevce_lap_enc_buf_t *ps_temp_lap_enc_buf = NULL;
1876
1877
                /*swap the lap enc buf and poc*/
1878
971
                ps_temp_lap_enc_buf =
1879
971
                    ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx - 1];
1880
971
                ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx - 1] =
1881
971
                    NULL;
1882
971
                ps_lap_struct->api4_capture_order_array[i4_capture_idx - 2] =
1883
971
                    ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx];
1884
1885
971
                if((i4_capture_idx - 2) != ps_lap_struct->i4_end_flag_pic_idx)
1886
738
                    ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx] =
1887
738
                        NULL;
1888
1889
971
                ps_temp_lap_enc_buf->s_lap_out.i4_pic_type = IV_P_FRAME;
1890
971
                ps_lap_struct->api4_capture_order_array[i4_capture_idx - 1] = ps_temp_lap_enc_buf;
1891
1892
971
                i4_temp_poc =
1893
971
                    ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_end_flag_pic_idx - 1];
1894
971
                ps_lap_struct->ai4_capture_order_poc[i4_capture_idx - 2] =
1895
971
                    ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_end_flag_pic_idx];
1896
1897
971
                ps_lap_struct->ai4_capture_order_poc[i4_capture_idx - 1] = i4_temp_poc;
1898
971
            }
1899
1900
135k
            if(ps_lap_struct->i4_num_dummy_pic)
1901
188
            {
1902
188
                WORD32 pic_idx;
1903
188
                ihevce_lap_enc_buf_t *ps_temp_lap_enc_buf = NULL;
1904
188
                static const WORD32 subgop_temporal_layer3[8] = { 7, 3, 1, 0, 2, 5, 4, 6 };
1905
188
                static const WORD32 subgop_temporal_layer2[4] = { 3, 1, 0, 2 };
1906
188
                const WORD32 *subgop_pic_idx = (ps_lap_static_params->i4_max_temporal_layers == 2)
1907
188
                                                   ? &subgop_temporal_layer2[0]
1908
188
                                                   : &subgop_temporal_layer3[0];
1909
188
                WORD32 max_pic_count = ps_lap_struct->i4_sub_gop_end + 1;
1910
1911
572
                for(pic_idx = 0; pic_idx < max_pic_count; pic_idx++)
1912
384
                {
1913
384
                    WORD32 i4_temp_idx = ps_lap_static_params->i4_max_temporal_layers > 1
1914
384
                                             ? subgop_pic_idx[pic_idx]
1915
384
                                             : 1;
1916
1917
384
                    if(NULL == ps_lap_struct->api4_capture_order_array[i4_temp_idx])
1918
328
                    {
1919
328
                        ps_temp_lap_enc_buf =
1920
328
                            ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_sub_gop_end];
1921
328
                        if(pic_idx == 0)
1922
188
                        {
1923
188
                            ps_temp_lap_enc_buf->s_lap_out.i4_pic_type = IV_P_FRAME;
1924
188
                        }
1925
328
                        ps_lap_struct->api4_capture_order_array[i4_temp_idx] = ps_temp_lap_enc_buf;
1926
328
                        ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_sub_gop_end] =
1927
328
                            NULL;
1928
1929
328
                        ps_lap_struct->ai4_capture_order_poc[i4_temp_idx] =
1930
328
                            ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_sub_gop_end];
1931
328
                        ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_sub_gop_end] = 0;
1932
328
                        ps_lap_struct->i4_sub_gop_end--;
1933
328
                    }
1934
384
                }
1935
188
                ps_lap_struct->i4_sub_gop_end = 0;
1936
188
            }
1937
135k
            i4_capture_idx = 0;
1938
1939
            /* add the number of pics in sub gop to the gop counter */
1940
            /* Get reordered Buffer for encoder, wait till all sub-gop buffers are output */
1941
1942
            /* Popluate I/P pictures */
1943
135k
            ihevce_ip_pic_population(ps_encode_node, ps_lap_struct, first_gop_flag);
1944
1945
            /* For hierarchical layers, Populate B picture */
1946
135k
            if((hier_layer > 0) &&
1947
135k
               sub_gop_size > (1 << ps_lap_static_params->i4_src_interlace_field))
1948
12.0k
            {
1949
12.0k
                ihevce_b_pic_population(ps_encode_node, ps_lap_struct);
1950
12.0k
            }
1951
1952
135k
            ps_lap_struct->i4_num_bufs_encode_order = sub_gop_size + first_gop_flag;
1953
1954
            /* correction of encode order in case of multiple non reference B*/
1955
135k
            if(ps_lap_struct->i4_dyn_sub_gop_size > ps_lap_struct->i4_sub_gop_size)
1956
0
            {
1957
0
                WORD32 i4_loop;
1958
0
                ihevce_lap_enc_buf_t *ps_lap_enc_buf, *ps_lap_enc_buf_tmp[MAX_NUM_ENC_NODES];
1959
0
                WORD32 i4_enc_cnt, i4_cap_cnt;
1960
1961
0
                i4_cap_cnt = first_gop_flag;
1962
0
                i4_enc_cnt = 0;
1963
1964
0
                for(i4_loop = 0; i4_loop < ps_lap_struct->i4_num_bufs_encode_order; i4_loop++)
1965
0
                {
1966
0
                    ps_lap_enc_buf = ps_lap_struct->api4_encode_order_array[i4_loop];
1967
1968
0
                    if(ps_lap_enc_buf != NULL && !ps_lap_enc_buf->s_lap_out.i4_is_ref_pic &&
1969
0
                       (ps_lap_enc_buf->s_lap_out.i4_temporal_lyr_id ==
1970
0
                        ps_lap_struct->s_lap_static_params.i4_max_temporal_layers))
1971
0
                    {
1972
0
                        if(ps_lap_enc_buf != ps_lap_struct->api4_capture_order_array[i4_cap_cnt])
1973
0
                        {
1974
0
                            ps_lap_enc_buf_tmp[i4_enc_cnt] =
1975
0
                                ps_lap_struct->api4_capture_order_array[i4_cap_cnt];
1976
0
                            i4_enc_cnt++;
1977
0
                            i4_loop++;
1978
0
                        }
1979
0
                        i4_cap_cnt += 2;
1980
0
                        ps_lap_enc_buf_tmp[i4_enc_cnt] = ps_lap_enc_buf;
1981
0
                        i4_enc_cnt++;
1982
0
                        ps_lap_enc_buf_tmp[i4_enc_cnt] =
1983
0
                            ps_lap_struct->api4_capture_order_array[i4_cap_cnt];
1984
0
                        i4_enc_cnt++;
1985
0
                        i4_cap_cnt += 2;
1986
0
                        i4_loop++;
1987
0
                    }
1988
0
                    else
1989
0
                    {
1990
0
                        ps_lap_enc_buf_tmp[i4_enc_cnt] = ps_lap_enc_buf;
1991
0
                        i4_enc_cnt++;
1992
0
                    }
1993
0
                }
1994
0
                for(i4_loop = 0; i4_loop < ps_lap_struct->i4_num_bufs_encode_order; i4_loop++)
1995
0
                {
1996
0
                    ps_lap_struct->api4_encode_order_array[i4_loop] = ps_lap_enc_buf_tmp[i4_loop];
1997
0
                }
1998
0
            }
1999
2000
            /* reset the IDR flag */
2001
135k
            ps_lap_struct->i4_idr_flag = 0;
2002
135k
            ps_lap_struct->i4_dyn_sub_gop_size = ps_lap_struct->i4_sub_gop_size;
2003
2004
            /*Copy encode array to lap output buf*/
2005
135k
            memcpy(
2006
135k
                &ps_lap_struct->api4_lap_out_buf[ps_lap_struct->i4_lap_encode_idx],
2007
135k
                &ps_lap_struct->api4_encode_order_array[0],
2008
135k
                sizeof(ihevce_lap_enc_buf_t *) * ps_lap_struct->i4_num_bufs_encode_order);
2009
2010
135k
            memset(
2011
135k
                &ps_lap_struct->api4_encode_order_array[0],
2012
135k
                0,
2013
135k
                sizeof(ihevce_lap_enc_buf_t *) * ps_lap_struct->i4_num_bufs_encode_order);
2014
2015
135k
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_lap_encode_idx] =
2016
135k
                ps_lap_struct->i4_num_bufs_encode_order - ps_lap_struct->i4_num_dummy_pic;
2017
2018
135k
            ps_lap_struct->i4_lap_encode_idx++;
2019
135k
            ps_lap_struct->i4_lap_encode_idx &= (MAX_SUBGOP_IN_ENCODE_QUEUE - 1);
2020
135k
        }
2021
2022
        /* store the capture index */
2023
177k
        ps_lap_struct->i4_capture_idx = i4_capture_idx;
2024
177k
        ps_lap_struct->i4_immediate_idr_case = 0;
2025
177k
    }
2026
177k
    return;
2027
177k
}
2028
2029
/*!
2030
************************************************************************
2031
* \brief
2032
*    lap process
2033
************************************************************************
2034
*/
2035
ihevce_lap_enc_buf_t *ihevce_lap_process(void *pv_interface_ctxt, ihevce_lap_enc_buf_t *ps_curr_inp)
2036
176k
{
2037
176k
    lap_intface_t *ps_lap_interface = (lap_intface_t *)pv_interface_ctxt;
2038
176k
    lap_struct_t *ps_lap_struct = (lap_struct_t *)ps_lap_interface->pv_lap_module_ctxt;
2039
176k
    ihevce_hle_ctxt_t *ps_hle_ctxt = (ihevce_hle_ctxt_t *)ps_lap_interface->pv_hle_ctxt;
2040
176k
    ihevce_lap_enc_buf_t *ps_lap_inp_buf = ps_curr_inp;
2041
176k
    ihevce_tgt_params_t *ps_tgt_params =
2042
176k
        &ps_lap_struct->s_static_cfg_params.s_tgt_lyr_prms.as_tgt_params[0];
2043
176k
    WORD32 i4_field_flag = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
2044
176k
    WORD32 i4_flush_check = 0;
2045
176k
    WORD32 i4_force_idr_check = 0;
2046
176k
    WORD32 i4_tree_num = 0;
2047
176k
    iv_input_ctrl_buffs_t *ps_ctrl_buf = NULL;
2048
176k
    WORD32 buf_id = 0;
2049
176k
    WORD32 i4_lap_window_size = 1 << ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
2050
2051
176k
    ps_lap_interface->i4_ctrl_in_que_blocking_mode = BUFF_QUE_NON_BLOCKING_MODE;
2052
2053
    /* ----------- LAP processing ----------- */
2054
176k
    if(ps_lap_struct->end_flag != 1)
2055
158k
    {
2056
158k
        ASSERT(NULL != ps_curr_inp);
2057
2058
        /* ---------- get the filled control command buffer ------------ */
2059
158k
        ps_ctrl_buf = (iv_input_ctrl_buffs_t *)ihevce_q_get_filled_buff(
2060
158k
            ps_hle_ctxt->apv_enc_hdl[0],
2061
158k
            ps_lap_interface->i4_ctrl_in_que_id,
2062
158k
            &buf_id,
2063
158k
            ps_lap_interface->i4_ctrl_in_que_blocking_mode);
2064
2065
        /* ----------- check the command ---------------------- */
2066
158k
        if(NULL != ps_ctrl_buf)
2067
5.28k
        {
2068
            /* check for async errors */
2069
5.28k
            ihevce_dyn_config_prms_t as_dyn_br[MAX_NUM_DYN_BITRATE_CMDS];
2070
5.28k
            WORD32 i4_num_set_bitrate_cmds = 0;
2071
5.28k
            WORD32 bitrt_ctr = 0;
2072
2073
5.28k
            ihevce_lap_parse_async_cmd(
2074
5.28k
                ps_hle_ctxt,
2075
5.28k
                (WORD32 *)ps_ctrl_buf->pv_asynch_ctrl_bufs,
2076
5.28k
                ps_ctrl_buf->i4_cmd_buf_size,
2077
5.28k
                ps_ctrl_buf->i4_buf_id,
2078
5.28k
                &i4_num_set_bitrate_cmds,
2079
5.28k
                &as_dyn_br[0]);
2080
2081
            /* Call the call back function to register the new bitrate */
2082
10.5k
            for(bitrt_ctr = 0; bitrt_ctr < i4_num_set_bitrate_cmds; bitrt_ctr++)
2083
5.28k
            {
2084
5.28k
                ps_lap_interface->ihevce_dyn_bitrate_cb(
2085
5.28k
                    (void *)ps_hle_ctxt, (void *)&as_dyn_br[bitrt_ctr]);
2086
5.28k
            }
2087
2088
            /* release async ctrl buffer*/
2089
5.28k
            ihevce_q_rel_buf(
2090
5.28k
                ps_hle_ctxt->apv_enc_hdl[0], IHEVCE_INPUT_ASYNCH_CTRL_Q, ps_ctrl_buf->i4_buf_id);
2091
5.28k
        }
2092
2093
158k
        {
2094
158k
            WORD32 *pi4_cmd_buf = (WORD32 *)ps_lap_inp_buf->s_input_buf.pv_synch_ctrl_bufs;
2095
2096
            /* check for sync cmd buffer error */
2097
            /* check FLUSH comand and Force IDR in the complete buffer */
2098
158k
            i4_flush_check = 0;
2099
158k
            i4_force_idr_check = 0;
2100
158k
            ihevce_lap_parse_sync_cmd(
2101
158k
                ps_hle_ctxt,
2102
158k
                &ps_lap_struct->s_static_cfg_params,
2103
158k
                pi4_cmd_buf,
2104
158k
                ps_lap_inp_buf,
2105
158k
                &i4_flush_check,
2106
158k
                &i4_force_idr_check);
2107
2108
158k
            if(i4_flush_check)
2109
8.22k
                ps_lap_struct->end_flag = 1;
2110
2111
158k
            ps_lap_inp_buf->s_lap_out.i4_out_flush_flag = 0;
2112
158k
            ps_lap_inp_buf->s_lap_out.i4_end_flag = ps_lap_struct->end_flag;
2113
2114
            /* check if input buffer is a valid buffer */
2115
158k
            if(1 == ps_lap_inp_buf->s_input_buf.i4_inp_frm_data_valid_flag)
2116
149k
            {
2117
                /* Initialise laps input buffer descriptors */
2118
149k
                memset(&ps_lap_inp_buf->s_lap_out, 0, sizeof(ihevce_lap_output_params_t));
2119
149k
                memset(&ps_lap_inp_buf->s_rc_lap_out, 0, sizeof(rc_lap_out_params_t));
2120
                /* Default initialization of lapout parameters */
2121
149k
                ps_lap_inp_buf->s_lap_out.i4_scene_type = SCENE_TYPE_NORMAL;
2122
149k
                ps_lap_inp_buf->s_lap_out.u4_scene_num = 0;
2123
149k
                ps_lap_inp_buf->s_lap_out.i4_display_num = ps_lap_struct->i4_display_num;
2124
149k
                ps_lap_inp_buf->s_lap_out.i4_quality_preset = ps_tgt_params->i4_quality_preset;
2125
149k
                ps_lap_inp_buf->s_lap_out.i1_weighted_pred_flag = 0;
2126
149k
                ps_lap_inp_buf->s_lap_out.i1_weighted_bipred_flag = 0;
2127
149k
                ps_lap_inp_buf->s_lap_out.i4_log2_luma_wght_denom = DENOM_DEFAULT;
2128
149k
                ps_lap_inp_buf->s_lap_out.i4_log2_chroma_wght_denom = DENOM_DEFAULT;
2129
149k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0].i4_num_duplicate_entries_in_ref_list = 1;
2130
149k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0].i4_used_by_cur_pic_flag = 1;
2131
149k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0].as_wght_off[0].u1_luma_weight_enable_flag =
2132
149k
                    0;
2133
149k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0]
2134
149k
                    .as_wght_off[0]
2135
149k
                    .u1_chroma_weight_enable_flag = 0;
2136
149k
                ps_lap_inp_buf->s_lap_out.i4_first_field = 1;
2137
149k
                ps_lap_inp_buf->s_lap_out.i4_force_idr_flag = 0;
2138
149k
                ps_lap_inp_buf->s_lap_out.i4_curr_frm_qp = ps_tgt_params->ai4_frame_qp[0];
2139
149k
                ps_lap_inp_buf->s_lap_out.i4_used = 1;
2140
149k
                if(i4_force_idr_check)
2141
1.34k
                {
2142
1.34k
                    ps_lap_inp_buf->s_lap_out.i4_force_idr_flag = 1;
2143
1.34k
                }
2144
                /* Populate input params in lap out struct */
2145
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.pv_y_buf =
2146
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.pv_y_buf;
2147
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.pv_u_buf =
2148
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.pv_u_buf;
2149
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.pv_v_buf =
2150
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.pv_v_buf;
2151
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_y_wd =
2152
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_y_wd;
2153
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_y_ht =
2154
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_y_ht;
2155
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_y_strd =
2156
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_y_strd;
2157
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_uv_wd =
2158
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_uv_wd;
2159
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_uv_ht =
2160
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_uv_ht;
2161
149k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_uv_strd =
2162
149k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_uv_strd;
2163
2164
149k
                ps_lap_struct->i4_display_num++;
2165
149k
                ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_enq_idx] = ps_lap_inp_buf;
2166
                /* update first field flag */
2167
149k
                ps_lap_inp_buf->s_lap_out.i4_first_field = 1;
2168
149k
                if(i4_field_flag)
2169
0
                {
2170
0
                    ps_lap_inp_buf->s_lap_out.i4_first_field =
2171
0
                        (ps_lap_inp_buf->s_input_buf.i4_topfield_first ^
2172
0
                         ps_lap_inp_buf->s_input_buf.i4_bottom_field);
2173
0
                }
2174
2175
                /* force idr in case interlace input can be taken only for first field */
2176
149k
                if(!ps_lap_inp_buf->s_lap_out.i4_first_field)
2177
0
                {
2178
0
                    ps_lap_inp_buf->s_lap_out.i4_force_idr_flag = 0;
2179
0
                }
2180
2181
149k
                if((i4_lap_window_size > 1) &&
2182
149k
                   (ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr] != PIC_TYPE_IDR))
2183
32.9k
                {
2184
32.9k
                    ps_lap_struct->i4_sub_gop_pic_idx++;
2185
32.9k
                    if(ps_lap_struct->i4_sub_gop_pic_idx > i4_lap_window_size)
2186
7.21k
                    {
2187
7.21k
                        ps_lap_struct->i4_sub_gop_pic_idx =
2188
7.21k
                            ps_lap_struct->i4_sub_gop_pic_idx - i4_lap_window_size;
2189
7.21k
                    }
2190
32.9k
                }
2191
116k
                else if(1 == i4_lap_window_size)
2192
113k
                {
2193
113k
                    ps_lap_struct->i4_sub_gop_pic_idx = 1;
2194
113k
                }
2195
2196
149k
                if(i4_force_idr_check &&
2197
149k
                   (ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr] != PIC_TYPE_IDR))
2198
577
                {
2199
577
                    ps_lap_struct->i4_force_idr_pos = ps_lap_struct->i4_sub_gop_pic_idx;
2200
577
                }
2201
2202
                /* store pictype for next subgop */
2203
149k
                if((0 == ps_lap_struct->i4_num_frm_type_decided) &&
2204
149k
                   (ps_lap_struct->i4_force_idr_pos == 0))
2205
122k
                {
2206
122k
                    ps_lap_struct->ai1_pic_type[0] =
2207
122k
                        ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
2208
2209
122k
                    ihevce_determine_next_sub_gop_state(ps_lap_struct);
2210
2211
122k
                    ps_lap_struct->i4_next_start_ctr = 0;
2212
122k
                }
2213
27.5k
                else if(
2214
27.5k
                    i4_force_idr_check &&
2215
27.5k
                    (ps_lap_struct->i4_force_idr_pos <= ps_lap_struct->i4_sub_gop_size))
2216
577
                {
2217
                    /*check force idr pos is 1st pic in sub-gop then don't add dummy pics*/
2218
577
                    if(ps_lap_struct->i4_force_idr_pos != 1)
2219
188
                    {
2220
188
                        WORD32 sub_gop_pos = ps_lap_struct->i4_force_idr_pos;
2221
556
                        while(sub_gop_pos <= ps_lap_struct->i4_sub_gop_size)
2222
368
                        {
2223
368
                            ps_lap_struct->i4_num_dummy_pic++;
2224
368
                            ihevce_lap_queue_input(ps_lap_struct, NULL, &i4_tree_num);
2225
368
                            sub_gop_pos++;
2226
368
                        }
2227
188
                        ps_lap_struct->i4_num_dummy_pic = 0;
2228
188
                    }
2229
577
                    ps_lap_struct->ai1_pic_type[0] =
2230
577
                        ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
2231
2232
577
                    ihevce_determine_next_sub_gop_state(ps_lap_struct);
2233
2234
577
                    ps_lap_struct->i4_next_start_ctr = 0;
2235
577
                }
2236
2237
149k
                if(/*ps_lap_struct->i4_init_delay_over &&*/ 0 !=
2238
149k
                   ps_lap_struct->i4_num_frm_type_decided)
2239
149k
                {
2240
149k
                    ihevce_assign_pic_type(
2241
149k
                        ps_lap_struct,
2242
149k
                        ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]);
2243
2244
149k
                    ps_lap_struct->i4_num_frm_type_decided--;
2245
2246
149k
                    if(NULL != ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx])
2247
149k
                    {
2248
                        /*special case of two consequetive idr at the start of encode or due to force idr*/
2249
149k
                        ps_lap_struct->i4_immediate_idr_case =
2250
149k
                            ps_lap_struct->i4_is_all_i_pic_in_seq;
2251
149k
                        if(ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]
2252
149k
                               ->s_lap_out.i4_pic_type == IV_IDR_FRAME)
2253
27.5k
                        {
2254
27.5k
                            ps_lap_struct->i4_immediate_idr_case = 1;
2255
27.5k
                        }
2256
122k
                        else
2257
122k
                        {
2258
122k
                            WORD32 i4_prev_idx = ps_lap_struct->i4_buf_deq_idx > 0
2259
122k
                                                     ? ps_lap_struct->i4_buf_deq_idx - 1
2260
122k
                                                     : ps_lap_struct->i4_buf_deq_idx;
2261
                            /*field case of single IDR field followed by P*/
2262
122k
                            if(NULL != ps_lap_struct->aps_lap_inp_buf[i4_prev_idx] &&
2263
122k
                               i4_field_flag &&
2264
122k
                               ps_lap_struct->aps_lap_inp_buf[i4_prev_idx]->s_lap_out.i4_pic_type ==
2265
0
                                   IV_IDR_FRAME &&
2266
122k
                               !ps_lap_struct->i4_num_frm_type_decided)
2267
0
                            {
2268
0
                                ps_lap_struct->i4_immediate_idr_case = 1;
2269
0
                            }
2270
122k
                        }
2271
149k
                    }
2272
2273
                    /* Queue in the current input Buffer to LAP que */
2274
149k
                    ihevce_lap_queue_input(
2275
149k
                        ps_lap_struct,
2276
149k
                        ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx],
2277
149k
                        &i4_tree_num);
2278
2279
149k
                    ps_lap_struct->i4_next_start_ctr++;
2280
149k
                    ps_lap_struct->i4_buf_deq_idx++;
2281
149k
                    if(ps_lap_struct->i4_buf_deq_idx >= MAX_QUEUE_LENGTH)
2282
929
                        ps_lap_struct->i4_buf_deq_idx = 0;
2283
149k
                }
2284
2285
149k
                ps_lap_struct->i4_buf_enq_idx++;
2286
149k
                if(ps_lap_struct->i4_buf_enq_idx >= MAX_QUEUE_LENGTH)
2287
929
                    ps_lap_struct->i4_buf_enq_idx = 0;
2288
149k
            } /* end if for valid input buffer check*/
2289
158k
        }
2290
2291
        /* source pixel padding if width/height is not aligned to 8 pixel */
2292
158k
        if(ps_lap_inp_buf->s_input_buf.i4_inp_frm_data_valid_flag)
2293
149k
        {
2294
149k
            ihevce_src_params_t *ps_src_prms = &ps_lap_struct->s_static_cfg_params.s_src_prms;
2295
149k
            WORD32 i4_align_wd = ps_src_prms->i4_width;
2296
149k
            WORD32 i4_align_ht = ps_src_prms->i4_height;
2297
149k
            WORD32 min_cu_size =
2298
149k
                (1 << ps_lap_struct->s_static_cfg_params.s_config_prms.i4_min_log2_cu_size);
2299
2300
149k
            i4_align_wd += SET_CTB_ALIGN(ps_src_prms->i4_width, min_cu_size);
2301
149k
            i4_align_ht += SET_CTB_ALIGN(ps_src_prms->i4_height, min_cu_size);
2302
2303
149k
            ihevce_lap_pad_input_bufs(ps_lap_inp_buf, i4_align_wd, i4_align_ht);
2304
149k
        }
2305
158k
        {
2306
158k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_width = 0;
2307
158k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_height = 0;
2308
158k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_x_offset = 0;
2309
158k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_y_offset = 0;
2310
158k
        }
2311
158k
    }
2312
2313
176k
    if(ps_lap_struct->end_flag == 1)
2314
27.0k
    {
2315
27.0k
        ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_enq_idx] = ps_lap_inp_buf;
2316
2317
        /*to be filed*/
2318
27.0k
        if(0 == ps_lap_struct->i4_num_frm_type_decided)
2319
10.4k
        {
2320
10.4k
            ps_lap_struct->ai1_pic_type[0] =
2321
10.4k
                ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
2322
2323
10.4k
            ihevce_determine_next_sub_gop_state(ps_lap_struct);
2324
2325
10.4k
            ps_lap_struct->i4_next_start_ctr = 0;
2326
10.4k
        }
2327
2328
27.0k
        if(NULL != ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx])
2329
8.22k
        {
2330
8.22k
            ihevce_assign_pic_type(
2331
8.22k
                ps_lap_struct, ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]);
2332
8.22k
        }
2333
2334
27.0k
        ps_lap_struct->i4_num_frm_type_decided--;
2335
2336
27.0k
        if(NULL != ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx])
2337
8.22k
        {
2338
            /*special case of two consequetive idr at the start of encode or due to force idr*/
2339
8.22k
            ps_lap_struct->i4_immediate_idr_case = ps_lap_struct->i4_is_all_i_pic_in_seq;
2340
2341
8.22k
            if(ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]
2342
8.22k
                   ->s_lap_out.i4_pic_type == IV_IDR_FRAME)
2343
577
            {
2344
577
                ps_lap_struct->i4_immediate_idr_case = 1;
2345
577
            }
2346
7.64k
            else
2347
7.64k
            {
2348
7.64k
                WORD32 i4_prev_idx = ps_lap_struct->i4_buf_deq_idx > 0
2349
7.64k
                                         ? ps_lap_struct->i4_buf_deq_idx - 1
2350
7.64k
                                         : ps_lap_struct->i4_buf_deq_idx;
2351
                /*field case of single IDR field followed by P*/
2352
7.64k
                if(NULL != ps_lap_struct->aps_lap_inp_buf[i4_prev_idx] && i4_field_flag &&
2353
7.64k
                   ps_lap_struct->aps_lap_inp_buf[i4_prev_idx]->s_lap_out.i4_pic_type ==
2354
0
                       IV_IDR_FRAME &&
2355
7.64k
                   !ps_lap_struct->i4_num_frm_type_decided)
2356
0
                {
2357
0
                    ps_lap_struct->i4_immediate_idr_case = 1;
2358
0
                }
2359
7.64k
            }
2360
8.22k
        }
2361
        /* Queue in the current input Buffer to LAP que */
2362
27.0k
        ihevce_lap_queue_input(
2363
27.0k
            ps_lap_struct,
2364
27.0k
            ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx],
2365
27.0k
            &i4_tree_num);
2366
27.0k
        ps_lap_struct->i4_max_buf_in_enc_order =
2367
27.0k
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf];
2368
27.0k
        ps_lap_struct->i4_next_start_ctr++;
2369
27.0k
        ps_lap_struct->i4_buf_deq_idx++;
2370
2371
27.0k
        if(ps_lap_struct->i4_buf_deq_idx >= MAX_QUEUE_LENGTH)
2372
10
            ps_lap_struct->i4_buf_deq_idx = 0;
2373
2374
27.0k
        ps_lap_struct->i4_buf_enq_idx++;
2375
27.0k
        if(ps_lap_struct->i4_buf_enq_idx >= MAX_QUEUE_LENGTH)
2376
10
            ps_lap_struct->i4_buf_enq_idx = 0;
2377
27.0k
    }
2378
2379
176k
    if(1 == ps_lap_struct->i4_force_end_flag)
2380
0
    {
2381
0
        ihevce_force_end(ps_hle_ctxt);
2382
0
    }
2383
2384
    /*return encode order pic to pre enc*/
2385
176k
    ps_lap_inp_buf = NULL;
2386
2387
176k
    if(NULL !=
2388
176k
       ps_lap_struct->api4_lap_out_buf[ps_lap_struct->i4_deq_lap_buf][ps_lap_struct->i4_lap_out_idx])
2389
158k
    {
2390
158k
        ps_lap_inp_buf =
2391
158k
            ps_lap_struct
2392
158k
                ->api4_lap_out_buf[ps_lap_struct->i4_deq_lap_buf][ps_lap_struct->i4_lap_out_idx];
2393
158k
        ps_lap_struct
2394
158k
            ->api4_lap_out_buf[ps_lap_struct->i4_deq_lap_buf][ps_lap_struct->i4_lap_out_idx] = NULL;
2395
158k
        if(!ps_lap_inp_buf->s_lap_out.i4_end_flag)
2396
149k
            ihevce_pre_rel_lapout_update(ps_lap_struct, ps_lap_inp_buf);
2397
2398
158k
        ps_lap_struct->i4_max_buf_in_enc_order =
2399
158k
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf];
2400
158k
    }
2401
2402
176k
    ps_lap_struct->i4_lap_out_idx++;
2403
176k
    if(ps_lap_struct->i4_lap_out_idx == ps_lap_struct->i4_max_buf_in_enc_order)
2404
138k
    {
2405
138k
        if(ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf])
2406
134k
        {
2407
134k
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf] = 0;
2408
134k
            ps_lap_struct->i4_deq_lap_buf++;
2409
134k
            ps_lap_struct->i4_deq_lap_buf &= (MAX_SUBGOP_IN_ENCODE_QUEUE - 1);
2410
134k
        }
2411
2412
138k
        ps_lap_struct->i4_lap_out_idx = 0;
2413
138k
    }
2414
2415
176k
    return (ps_lap_inp_buf);
2416
176k
}
2417
2418
/*!
2419
************************************************************************
2420
* \brief
2421
*    lap get input buffer requirement count
2422
************************************************************************
2423
*/
2424
WORD32 ihevce_lap_get_num_ip_bufs(ihevce_lap_static_params_t *ps_lap_stat_prms)
2425
16.4k
{
2426
16.4k
    WORD32 i4_lap_window_size = 1;
2427
16.4k
    WORD32 gop_delay = 1 << ps_lap_stat_prms->i4_max_temporal_layers;
2428
2429
16.4k
    if(ps_lap_stat_prms->s_lap_params.i4_rc_look_ahead_pics != 0)
2430
0
    {
2431
0
        i4_lap_window_size = 1 + ps_lap_stat_prms->s_lap_params.i4_rc_look_ahead_pics;
2432
0
    }
2433
2434
16.4k
    gop_delay += (i4_lap_window_size);
2435
16.4k
    return gop_delay;
2436
16.4k
}