Coverage Report

Created: 2026-06-10 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_lap_interface.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2018 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
21
/**
22
******************************************************************************
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
398
{
114
398
    return (NUM_LAP_MEM_RECS);
115
398
}
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
199
{
125
    /* number of NODE memory */
126
199
    WORD32 max_nodes = MAX_SUB_GOP_SIZE - 1;
127
128
199
    ps_mem_tab[LAP_CTXT].i4_mem_size = sizeof(lap_struct_t);
129
199
    ps_mem_tab[LAP_CTXT].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
130
199
    ps_mem_tab[LAP_CTXT].i4_mem_alignment = 8;
131
132
    /* Node memory for 2 sub-gops*/
133
199
    ps_mem_tab[LAP_NODE_MEM].i4_mem_size = (max_nodes * sizeof(ihevce_encode_node_t));
134
135
199
    ps_mem_tab[LAP_NODE_MEM].e_mem_type = (IV_MEM_TYPE_T)i4_mem_space;
136
137
199
    ps_mem_tab[LAP_NODE_MEM].i4_mem_alignment = 8;
138
139
199
    return (NUM_LAP_MEM_RECS);
140
199
}
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
199
{
153
199
    WORD32 i4_src_interlace_field;
154
199
    WORD32 i4_max_temporal_layers;
155
199
    ihevce_encode_node_t *ps_encode_node_struct;
156
199
    lap_struct_t *ps_lap_struct = (lap_struct_t *)ps_mem_tab[LAP_CTXT].pv_base;
157
199
    ihevce_lap_static_params_t *ps_lap_static_params = &ps_lap_struct->s_lap_static_params;
158
199
    ps_lap_struct->aps_encode_node[0] = (ihevce_encode_node_t *)ps_mem_tab[LAP_NODE_MEM].pv_base;
159
160
199
    memcpy(
161
199
        &ps_lap_struct->s_static_cfg_params,
162
199
        ps_static_cfg_prms,
163
199
        sizeof(ihevce_static_cfg_params_t));
164
199
    memmove(ps_lap_static_params, ps_lap_params, sizeof(ihevce_lap_static_params_t));
165
199
    ps_lap_static_params->e_arch_type = ps_static_cfg_prms->e_arch_type;
166
167
    /* Set the array to zero */
168
199
    memset(&ps_lap_struct->ai4_capture_order_poc[0], 0, MAX_NUM_ENC_NODES * sizeof(WORD32));
169
199
    memset(&ps_lap_struct->ai4_encode_order_poc[0], 0, MAX_NUM_ENC_NODES * sizeof(WORD32));
170
199
    memset(&ps_lap_struct->ref_poc_array[0], 0xFF, sizeof(ps_lap_struct->ref_poc_array));
171
199
    memset(&ps_lap_struct->ai4_pic_type_to_be_removed, 0, NUM_LAP2_LOOK_AHEAD * sizeof(WORD32));
172
199
    memset(&ps_lap_struct->ai4_num_buffer[0], 0, sizeof(ps_lap_struct->ai4_num_buffer));
173
174
199
    ps_lap_struct->i4_curr_poc = 0;
175
199
    ps_lap_struct->i4_cra_poc = 0;
176
177
199
    i4_max_temporal_layers = ps_lap_static_params->i4_max_temporal_layers;
178
199
    i4_src_interlace_field = ps_lap_static_params->i4_src_interlace_field;
179
199
    ps_lap_struct->i4_max_idr_period =
180
199
        ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period;
181
199
    ps_lap_struct->i4_min_idr_period =
182
199
        ps_static_cfg_prms->s_coding_tools_prms.i4_min_closed_gop_period;
183
199
    ps_lap_struct->i4_max_cra_period =
184
199
        ps_static_cfg_prms->s_coding_tools_prms.i4_max_cra_open_gop_period;
185
199
    ps_lap_struct->i4_max_i_period =
186
199
        ps_static_cfg_prms->s_coding_tools_prms.i4_max_i_open_gop_period;
187
199
    ps_lap_struct->i4_idr_counter = 0;
188
199
    ps_lap_struct->i4_cra_counter = 0;
189
199
    ps_lap_struct->i4_i_counter = 0;
190
199
    ps_lap_struct->i4_idr_gop_num = -1;
191
199
    ps_lap_struct->i4_curr_ref_pics = 0;
192
199
    ps_lap_struct->i4_display_num = 0;
193
199
    ps_lap_struct->i4_num_frm_type_decided = 0;
194
199
    ps_lap_struct->i4_next_start_ctr = 0;
195
199
    ps_lap_struct->ai1_pic_type[0] = PIC_TYPE_IDR;
196
197
199
    ps_lap_struct->i4_enable_logo = ps_lap_static_params->i4_enable_logo;
198
199
    ps_lap_struct->i4_cra_i_pic_flag = 0;
199
199
    ps_lap_struct->i4_force_end_flag = 0;
200
199
    ps_lap_struct->i4_sub_gop_size = (1 << i4_max_temporal_layers);
201
199
    ps_lap_struct->i4_sub_gop_size_idr =
202
199
        ps_lap_struct->i4_sub_gop_size + (i4_max_temporal_layers > 0);
203
204
199
    ps_lap_struct->i4_is_all_i_pic_in_seq = 0;
205
206
199
    if(ps_lap_struct->i4_max_idr_period == 1 || ps_lap_struct->i4_max_cra_period == 1 ||
207
189
       ps_lap_struct->i4_max_i_period == 1)
208
14
    {
209
14
        ps_lap_struct->i4_is_all_i_pic_in_seq = 1;
210
14
    }
211
212
199
    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
199
    ps_lap_struct->i4_fixed_open_gop_period = 1;
219
199
    ps_lap_struct->i4_fixed_i_period = 1;
220
221
199
    if(ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period <=
222
199
       ps_lap_struct->i4_sub_gop_size)
223
122
    {
224
122
        ps_lap_struct->i4_min_idr_period =
225
122
            ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period;
226
122
    }
227
199
    if(ps_lap_struct->i4_max_idr_period)
228
87
    {
229
87
        if(ps_lap_struct->i4_max_cra_period)
230
60
        {
231
60
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_cra_period;
232
60
        }
233
27
        else if(ps_lap_struct->i4_max_i_period)
234
14
        {
235
14
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_i_period;
236
14
        }
237
13
        else
238
13
        {
239
13
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_idr_period;
240
13
        }
241
87
    }
242
112
    else
243
112
    {
244
112
        if(ps_lap_struct->i4_max_i_period)
245
21
        {
246
21
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_i_period;
247
21
        }
248
91
        else if(ps_lap_struct->i4_max_cra_period)
249
63
        {
250
63
            ps_lap_struct->i4_gop_period = ps_lap_struct->i4_max_cra_period;
251
63
        }
252
112
    }
253
254
199
    if(!ps_lap_struct->i4_max_i_period)
255
106
    {
256
106
        ps_lap_struct->i4_max_i_period =
257
106
            2 * MAX(ps_lap_struct->i4_max_idr_period, ps_lap_struct->i4_max_cra_period);
258
106
    }
259
260
199
    ps_lap_struct->i4_no_back_to_back_i_avoidance = 0;
261
262
    /*Infinite GOP case*/
263
199
    if(!ps_lap_struct->i4_gop_period)
264
28
    {
265
        /*max signed 32 bit value which will be ~ 414 days considering 60frames/fields per second*/
266
28
        ps_lap_struct->i4_max_i_period = 0x7fffffff;
267
28
        ps_lap_struct->i4_gop_period =
268
28
            (INFINITE_GOP_CDR_TIME_S * (ps_static_cfg_prms->s_src_prms.i4_frm_rate_num /
269
28
                                        ps_static_cfg_prms->s_src_prms.i4_frm_rate_denom));
270
28
    }
271
272
199
    if(ps_lap_struct->i4_gop_period < (2 * ps_lap_struct->i4_sub_gop_size))
273
12
    {
274
12
        ps_lap_struct->i4_no_back_to_back_i_avoidance = 1;
275
12
    }
276
277
199
    ps_lap_struct->i4_rc_lap_period =
278
199
        ps_static_cfg_prms->s_lap_prms.i4_rc_look_ahead_pics + MIN_L1_L0_STAGGER_NON_SEQ;
279
199
    ps_lap_struct->pv_prev_inp_buf = NULL;
280
199
    ps_lap_struct->i4_buf_deq_idx = 0;
281
199
    ps_lap_struct->i4_deq_idx = 0;
282
199
    ps_lap_struct->i4_enq_idx = 0;
283
199
    ps_lap_struct->i4_lap2_counter = 0;
284
199
    ps_lap_struct->i4_dyn_sub_gop_size = ps_lap_struct->i4_sub_gop_size;
285
199
    ps_lap_struct->i4_buf_enq_idx = 0;
286
199
    ps_lap_struct->i4_lap_out_idx = 0;
287
199
    ps_lap_struct->i4_capture_idx = 0;
288
199
    ps_lap_struct->i4_idr_flag = 1;
289
199
    ps_lap_struct->i4_num_bufs_encode_order = 0;
290
199
    ps_lap_struct->end_flag = 0;
291
199
    ps_lap_struct->i4_immediate_idr_case = 0;
292
199
    ps_lap_struct->i4_max_buf_in_enc_order = 0;
293
199
    ps_lap_struct->i4_end_flag_pic_idx = 0;
294
199
    memset(
295
199
        &ps_lap_struct->api4_encode_order_array[0],
296
199
        0,
297
199
        sizeof(ihevce_lap_enc_buf_t *) * MAX_NUM_ENC_NODES);
298
199
    ps_lap_struct->i4_sub_gop_pic_idx = 0;
299
199
    ps_lap_struct->i4_force_idr_pos = 0;
300
199
    ps_lap_struct->i4_num_dummy_pic = 0;
301
199
    ps_lap_struct->i4_lap_encode_idx = 0;
302
199
    ps_lap_struct->i4_deq_lap_buf = 0;
303
199
    ps_lap_struct->i4_sub_gop_end = 0;
304
305
199
    {
306
199
        WORD32 node_offset, curr_layer;
307
199
        WORD32 i;
308
        /*intialization of aps_lap_inp_buf*/
309
15.7k
        for(i = 0; i < MAX_QUEUE_LENGTH; i++)
310
15.5k
        {
311
15.5k
            ps_lap_struct->aps_lap_inp_buf[i] = NULL;
312
15.5k
        }
313
314
        /* init capture order and encode order pointer */
315
199
        ps_lap_struct->pi4_capture_poc_ptr = &ps_lap_struct->ai4_capture_order_poc[0];
316
199
        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
199
        ps_encode_node_struct = ps_lap_struct->aps_encode_node[0];
320
321
199
        ps_encode_node_struct->pv_left_node = NULL;
322
199
        ps_encode_node_struct->pv_right_node = NULL;
323
324
        /* Initialise the tree */
325
199
        node_offset = 1;
326
199
        curr_layer = 0;
327
199
        ihevce_populate_tree_nodes(
328
199
            ps_encode_node_struct,
329
199
            ps_encode_node_struct,
330
199
            &node_offset,
331
199
            curr_layer,
332
199
            ps_lap_static_params->i4_max_temporal_layers);
333
199
    }
334
335
199
    ps_mem_tab += NUM_LAP_MEM_RECS;
336
337
199
    return ((void *)ps_lap_struct);
338
199
}
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
271
{
366
    /* If only I/P pictures, return NULL from the child nodes*/
367
271
    if(hier_layer == 0)
368
187
    {
369
187
        encode_node_t->pv_left_node = NULL;
370
187
        encode_node_t->pv_right_node = NULL;
371
187
        return;
372
187
    }
373
84
    if(layer == hier_layer)
374
48
        return;
375
376
36
    layer = layer + 1;
377
378
    /* If the layers are not exhausted */
379
36
    if(layer < hier_layer)
380
12
    {
381
12
        encode_node_t->pv_left_node = encode_parent_node_t + (*loop_count);
382
12
        encode_node_t->pv_right_node = encode_parent_node_t + (*loop_count + 1);
383
12
        (*loop_count) = (*loop_count) + 2;
384
12
    }
385
24
    else
386
24
    {
387
24
        encode_node_t->pv_left_node = NULL;
388
24
        encode_node_t->pv_right_node = NULL;
389
24
    }
390
391
    /* Populate Left tree nodes */
392
36
    ihevce_populate_tree_nodes(
393
36
        encode_parent_node_t,
394
36
        (ihevce_encode_node_t *)encode_node_t->pv_left_node,
395
36
        loop_count,
396
36
        layer,
397
36
        hier_layer);
398
399
    /* Populate right tree nodes */
400
36
    ihevce_populate_tree_nodes(
401
36
        encode_parent_node_t,
402
36
        (ihevce_encode_node_t *)encode_node_t->pv_right_node,
403
36
        loop_count,
404
36
        layer,
405
36
        hier_layer);
406
36
}
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
2.52k
{
417
    /* local variables */
418
2.52k
    WORD32 ctr_horz, ctr_vert;
419
420
    /* ------- Horizontal Right Padding ------ */
421
2.52k
    if(align_pic_wd != ps_curr_inp->s_lap_out.s_input_buf.i4_y_wd)
422
1.00k
    {
423
1.00k
        UWORD8 *pu1_inp;
424
1.00k
        UWORD16 *pu2_inp;
425
1.00k
        WORD32 pad_wd;
426
1.00k
        WORD32 pad_ht;
427
428
        /* ------------- LUMA ----------------------------- */
429
        /* derive the pointers and dimensions to be padded */
430
1.00k
        pad_ht = ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht;
431
1.00k
        pad_wd = align_pic_wd - ps_curr_inp->s_lap_out.s_input_buf.i4_y_wd;
432
1.00k
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_y_buf;
433
1.00k
        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
94.7k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
437
93.7k
        {
438
475k
            for(ctr_horz = 0; ctr_horz < pad_wd; ctr_horz++)
439
381k
            {
440
                /* last pixel is replicated */
441
381k
                pu1_inp[ctr_horz] = pu1_inp[-1];
442
381k
            }
443
444
            /* row level increments */
445
93.7k
            pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_y_strd;
446
93.7k
        }
447
448
        /* ------------- CHROMA ---------------------------- */
449
        /* derive the pointers and dimensions to be padded */
450
1.00k
        pad_ht = ps_curr_inp->s_lap_out.s_input_buf.i4_uv_ht;
451
1.00k
        pad_wd = align_pic_wd - ps_curr_inp->s_lap_out.s_input_buf.i4_uv_wd;
452
1.00k
        pad_wd >>= 1;
453
1.00k
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_u_buf;
454
1.00k
        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
47.8k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
458
46.8k
        {
459
142k
            for(ctr_horz = 0; ctr_horz < pad_wd; ctr_horz++)
460
95.3k
            {
461
                /* last pixel is replicated, cb and cr pixel interleaved */
462
95.3k
                pu2_inp[ctr_horz] = pu2_inp[-1];
463
95.3k
            }
464
465
            /* row level increments */
466
46.8k
            pu2_inp += (ps_curr_inp->s_lap_out.s_input_buf.i4_uv_strd >> 1);
467
46.8k
        }
468
1.00k
    }
469
470
    /* ------- Vertical Bottom Padding ------ */
471
2.52k
    if(align_pic_ht != ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht)
472
919
    {
473
919
        UWORD8 *pu1_inp, *pu1_src;
474
919
        WORD32 pad_ht;
475
476
        /* ------------- LUMA ----------------------------- */
477
        /* derive the pointers and dimensions to be padded */
478
919
        pad_ht = align_pic_ht - ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht;
479
919
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_y_buf;
480
919
        pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_y_ht *
481
919
                   ps_curr_inp->s_lap_out.s_input_buf.i4_y_strd;
482
483
        /* get the pointer of last row */
484
919
        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
4.53k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
488
3.62k
        {
489
            /* copy the eniter orw including horz padd region */
490
3.62k
            memcpy(pu1_inp, pu1_src, align_pic_wd);
491
492
            /* row level increments */
493
3.62k
            pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_y_strd;
494
3.62k
        }
495
496
        /* ------------- CHROMA ----------------------------- */
497
        /* derive the pointers and dimensions to be padded */
498
919
        pad_ht = (align_pic_ht >> 1) - ps_curr_inp->s_lap_out.s_input_buf.i4_uv_ht;
499
919
        pu1_inp = (UWORD8 *)ps_curr_inp->s_lap_out.s_input_buf.pv_u_buf;
500
919
        pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_uv_ht *
501
919
                   ps_curr_inp->s_lap_out.s_input_buf.i4_uv_strd;
502
503
        /* get the pointer of last row */
504
919
        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
2.72k
        for(ctr_vert = 0; ctr_vert < pad_ht; ctr_vert++)
508
1.81k
        {
509
            /* copy the eniter orw including horz padd region */
510
1.81k
            memcpy(pu1_inp, pu1_src, align_pic_wd);
511
512
            /* row level increments */
513
1.81k
            pu1_inp += ps_curr_inp->s_lap_out.s_input_buf.i4_uv_strd;
514
1.81k
        }
515
919
    }
516
2.52k
    return;
517
2.52k
}
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
2.72k
{
527
2.72k
    WORD32 cmd = (*pi4_cmd_buf) & (IHEVCE_COMMANDS_TAG_MASK);
528
529
2.72k
    if(IHEVCE_SYNCH_API_FLUSH_TAG == cmd)
530
199
        return 1;
531
2.52k
    return 0;
532
2.72k
}
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
2.72k
{
548
2.72k
    WORD32 *pi4_tag_parse = pi4_cmd_buf;
549
2.72k
    WORD32 i4_cmd_size = ps_lap_inp_buf->s_input_buf.i4_cmd_buf_size;
550
2.72k
    WORD32 i4_buf_id = ps_lap_inp_buf->s_input_buf.i4_buf_id;
551
2.72k
#ifndef DISABLE_SEI
552
2.72k
    UWORD32 u4_num_sei = 0;
553
2.72k
#endif
554
2.72k
    WORD32 i4_end_flag = 0;
555
556
3.00k
    while(i4_cmd_size >= 4)
557
3.00k
    {
558
3.00k
        switch((*pi4_tag_parse) & (IHEVCE_COMMANDS_TAG_MASK))
559
3.00k
        {
560
199
        case IHEVCE_SYNCH_API_FLUSH_TAG:
561
199
            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
199
            (*pi4_flush_check) = 1;
571
199
            pi4_tag_parse += 2;
572
199
            i4_cmd_size -= 8;
573
199
#ifndef DISABLE_SEI
574
199
            u4_num_sei++;
575
199
#endif
576
199
            break;
577
76
        case IHEVCE_SYNCH_API_FORCE_IDR_TAG:
578
76
            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
76
            (*pi4_force_idr_check) = 1;
588
76
            pi4_tag_parse += 2;
589
76
            i4_cmd_size -= 8;
590
76
#ifndef DISABLE_SEI
591
76
            u4_num_sei++;
592
76
#endif
593
76
            break;
594
2.72k
        case IHEVCE_SYNCH_API_END_TAG:
595
2.72k
            i4_end_flag = 1;
596
2.72k
            i4_cmd_size -= 4;
597
2.72k
            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
3.00k
        }
603
3.00k
        if(i4_end_flag)
604
2.72k
            break;
605
3.00k
    }
606
2.72k
#ifndef DISABLE_SEI
607
2.72k
    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
2.72k
#endif
611
612
2.72k
    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
2.72k
}
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
167
{
631
167
    WORD32 i4_end_flag = 0;
632
167
    WORD32 *pi4_tag_parse = pi4_cmd_buf;
633
634
334
    while(i4_length >= 4)
635
334
    {
636
334
        switch(*pi4_tag_parse)
637
334
        {
638
167
        case IHEVCE_ASYNCH_API_SETBITRATE_TAG:
639
167
            if(i4_length < (8 + sizeof(ihevce_dyn_config_prms_t)) ||
640
167
               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
167
            memcpy(
647
167
                (void *)ps_dyn_br, (void *)(pi4_tag_parse + 2), sizeof(ihevce_dyn_config_prms_t));
648
167
            pi4_tag_parse += (2 + (sizeof(ihevce_dyn_config_prms_t) >> 2));
649
167
            i4_length -= (8 + sizeof(ihevce_dyn_config_prms_t));
650
167
            *pi4_num_set_bitrate_cmds += 1;
651
167
            ps_dyn_br++;
652
167
            break;
653
167
        case IHEVCE_ASYNCH_API_END_TAG:
654
167
            i4_end_flag = 1;
655
167
            i4_length -= 4;
656
167
            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
334
        }
662
334
        if(i4_end_flag)
663
167
            break;
664
334
    }
665
167
    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
167
}
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
2.72k
{
678
2.72k
    WORD32 i, j;
679
2.72k
    WORD32 *ref_poc_array = ps_lap_struct->ref_poc_array;
680
2.72k
    WORD32 ai4_delta_poc[MAX_REF_PICS];
681
2.72k
    WORD32 ref_poc_arr_sort[MAX_REF_PICS];
682
683
    /* Default weighted pred parameters populated for  now */
684
2.72k
    ps_lap_out->i4_log2_luma_wght_denom = DENOM_DEFAULT;
685
2.72k
    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
9.90k
    for(i = 0; i < ps_lap_struct->i4_curr_ref_pics; i++)
691
7.17k
    {
692
7.17k
        ai4_delta_poc[i] = ref_poc_array[i] - ps_lap_out->i4_poc;
693
7.17k
    }
694
695
9.90k
    for(i = 0; i < ps_lap_struct->i4_curr_ref_pics; i++)
696
7.17k
    {
697
7.17k
        WORD32 i4_min, temp;
698
7.17k
        i4_min = i;
699
23.5k
        for(j = i; j < ps_lap_struct->i4_curr_ref_pics; j++)
700
16.3k
        {
701
16.3k
            if(abs(ai4_delta_poc[j]) <= abs(ai4_delta_poc[i4_min]))
702
10.1k
            {
703
10.1k
                i4_min = j;
704
10.1k
            }
705
16.3k
        }
706
7.17k
        temp = ai4_delta_poc[i];
707
7.17k
        ai4_delta_poc[i] = ai4_delta_poc[i4_min];
708
7.17k
        ai4_delta_poc[i4_min] = temp;
709
7.17k
        ref_poc_arr_sort[i] = ai4_delta_poc[i] + ps_lap_out->i4_poc;
710
7.17k
    }
711
712
9.90k
    for(i = 0; i < ps_lap_struct->i4_curr_ref_pics; i++)
713
7.17k
    {
714
7.17k
        ps_lap_out->as_ref_pics[i].i4_ref_pic_delta_poc = ref_poc_arr_sort[i] - ps_lap_out->i4_poc;
715
7.17k
        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
7.17k
        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
7.17k
        ps_lap_out->as_ref_pics[i].i4_num_duplicate_entries_in_ref_list = 1;
722
7.17k
    }
723
2.72k
    return;
724
2.72k
}
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
42
{
735
42
    ihevce_lap_output_params_t *ps_lap_out = &ps_lap_inp->s_lap_out;
736
42
    WORD32 *ref_poc_array = ps_lap_struct->ref_poc_array;
737
42
    WORD32 *p_ref_poc_array = ref_poc_array;
738
42
    WORD32 i4_interlace_field = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
739
42
    WORD32 i4_max_ref_pics = ps_lap_struct->s_lap_static_params.i4_max_reference_frames;
740
42
    WORD32 max_temporal_layers = ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
741
742
    /* LAP output structure */
743
42
    ps_lap_out->i4_poc = ps_lap_struct->pi4_encode_poc_ptr[0];
744
42
    ps_lap_out->i4_idr_gop_num = ps_lap_struct->i4_idr_gop_num;
745
42
    ps_lap_out->i4_assoc_IRAP_poc = ps_lap_struct->i4_assoc_IRAP_poc;
746
42
    ps_lap_out->i4_temporal_lyr_id = curr_layer;
747
42
    ps_lap_out->i4_pic_type = IV_B_FRAME;
748
749
42
    if((ps_lap_out->i4_poc > ps_lap_struct->i4_cra_poc) &&
750
42
       (ref_poc_array[0] < ps_lap_struct->i4_cra_poc) && ps_lap_struct->i4_cra_i_pic_flag)
751
0
    {
752
0
        ref_poc_array[0] = ps_lap_struct->i4_cra_poc;
753
0
        ps_lap_struct->i4_curr_ref_pics = 1;
754
0
    }
755
756
42
    ps_lap_out->i4_num_ref_pics = ps_lap_struct->i4_curr_ref_pics;
757
758
    /* Default: Cur pic is ref pic*/
759
42
    ps_lap_out->i4_is_ref_pic = 1;
760
761
42
    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
42
    else
777
42
    {
778
        /*If progressive B picture and is present in top hierarchical layer */
779
42
        if(ps_lap_out->i4_temporal_lyr_id >= max_temporal_layers)
780
31
        {
781
31
            ps_lap_out->i4_temporal_lyr_id = max_temporal_layers;
782
31
            ps_lap_out->i4_is_ref_pic = 0;
783
31
        }
784
42
    }
785
786
42
    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
42
    if(ps_lap_struct->i4_curr_ref_pics < i4_max_ref_pics)
791
29
    {
792
29
        if(ps_lap_out->i4_is_ref_pic)
793
7
        {
794
7
            ps_lap_struct->i4_curr_ref_pics++;
795
7
        }
796
29
    }
797
798
    /* Arrange the reference array in ascending order */
799
42
    {
800
42
        WORD32 i, j, temp;
801
127
        for(i = 0; i < (ps_lap_struct->i4_curr_ref_pics - 1); i++)
802
85
        {
803
231
            for(j = i + 1; j < ps_lap_struct->i4_curr_ref_pics; j++)
804
146
            {
805
146
                if(ref_poc_array[i] > ref_poc_array[j])
806
53
                {
807
53
                    temp = ref_poc_array[i];
808
53
                    ref_poc_array[i] = ref_poc_array[j];
809
53
                    ref_poc_array[j] = temp;
810
53
                }
811
146
            }
812
85
        }
813
42
    }
814
815
42
    {
816
42
        WORD32 ref = ps_lap_out->i4_poc;
817
42
        if(ps_lap_out->i4_is_ref_pic && ref > *p_ref_poc_array)
818
11
        {
819
11
            *p_ref_poc_array = ref;
820
11
        }
821
42
    }
822
823
42
    return;
824
42
}
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
2.68k
{
834
2.68k
    ihevce_lap_output_params_t *ps_lap_out = &ps_lap_inp->s_lap_out;
835
2.68k
    WORD32 *ref_poc_array = ps_lap_struct->ref_poc_array;
836
2.68k
    WORD32 *p_ref_poc_array = ref_poc_array;
837
2.68k
    WORD32 i4_max_ref_pics = ps_lap_struct->s_lap_static_params.i4_max_reference_frames;
838
839
    /* Update the POC position */
840
2.68k
    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
2.68k
    if((ps_lap_out->i4_poc > ps_lap_struct->i4_cra_poc) &&
844
2.24k
       (ref_poc_array[0] <= ps_lap_struct->i4_cra_poc) && ps_lap_struct->i4_cra_i_pic_flag)
845
346
    {
846
346
        ref_poc_array[0] = ps_lap_struct->i4_cra_poc;
847
346
        ps_lap_struct->i4_curr_ref_pics = 1;
848
346
    }
849
850
    /* For every IDR period, set pic type as IDR frame and reset reference POC array to 0*/
851
2.68k
    if(IV_IDR_FRAME == ps_lap_out->i4_pic_type)
852
446
    {
853
446
        ps_lap_struct->i4_idr_gop_num++;
854
446
        ps_lap_struct->i4_curr_ref_pics = 0;
855
446
        ps_lap_out->i4_num_ref_pics = 0;
856
446
        ps_lap_struct->i4_cra_i_pic_flag = 1;
857
446
        ps_lap_struct->i4_cra_poc = ps_lap_out->i4_poc;
858
859
446
        memset(ps_lap_struct->ref_poc_array, 0xFF, sizeof(WORD32) * MAX_REF_PICS);
860
446
    }
861
2.24k
    else if(IV_I_FRAME == ps_lap_out->i4_pic_type)
862
131
    {
863
        /* For the I-frames after CRA Frame, no pictures should be referenced */
864
131
        if((1 == ps_lap_struct->i4_cra_i_pic_flag) && ps_lap_out->i4_is_cra_pic)
865
1
        {
866
1
            ps_lap_struct->i4_curr_ref_pics = 0;
867
1
            ps_lap_out->i4_num_ref_pics = 0;
868
1
        }
869
131
        ps_lap_struct->i4_cra_poc = ps_lap_out->i4_poc;
870
131
        ps_lap_struct->i4_cra_i_pic_flag = ps_lap_out->i4_is_cra_pic;
871
131
    }
872
2.10k
    else if(IV_P_FRAME == ps_lap_out->i4_pic_type)
873
2.10k
    {
874
        /* If the current POC is the P POC after CRA I POC */
875
2.10k
        if(1 == ps_lap_struct->i4_cra_i_pic_flag)
876
332
        {
877
332
            ps_lap_struct->i4_curr_ref_pics = 1;
878
332
            ps_lap_struct->i4_cra_i_pic_flag = 0;
879
332
        }
880
2.10k
    }
881
882
2.68k
    if(ps_lap_out->i4_pic_type == IV_IDR_FRAME ||
883
2.24k
       (ps_lap_out->i4_pic_type == IV_I_FRAME && ps_lap_out->i4_is_cra_pic))
884
513
    {
885
513
        ps_lap_struct->i4_assoc_IRAP_poc = ps_lap_out->i4_poc;
886
513
    }
887
888
    /*Update ps_lap_out*/
889
2.68k
    ps_lap_out->i4_idr_gop_num = ps_lap_struct->i4_idr_gop_num;
890
2.68k
    ps_lap_out->i4_is_ref_pic = 1;
891
2.68k
    ps_lap_out->i4_assoc_IRAP_poc = ps_lap_struct->i4_assoc_IRAP_poc;
892
893
    /* Reference POCS */
894
2.68k
    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
2.68k
    ps_lap_out->i4_temporal_lyr_id = 0;
898
899
2.68k
    ref_pics_weight_offset_calc(ps_lap_out, ps_lap_struct);
900
901
2.68k
    if(ps_lap_struct->i4_curr_ref_pics < i4_max_ref_pics)
902
1.35k
    {
903
1.35k
        if(ps_lap_out->i4_is_ref_pic)
904
1.35k
        {
905
1.35k
            ps_lap_struct->i4_curr_ref_pics++;
906
1.35k
        }
907
1.35k
    }
908
909
    /* Arrange the reference array in ascending order */
910
2.68k
    {
911
2.68k
        WORD32 i, j, temp;
912
8.41k
        for(i = 0; i < (ps_lap_struct->i4_curr_ref_pics - 1); i++)
913
5.72k
        {
914
16.5k
            for(j = i + 1; j < (ps_lap_struct->i4_curr_ref_pics); j++)
915
10.8k
            {
916
10.8k
                if(ref_poc_array[i] > ref_poc_array[j])
917
6.54k
                {
918
6.54k
                    temp = ref_poc_array[i];
919
6.54k
                    ref_poc_array[i] = ref_poc_array[j];
920
6.54k
                    ref_poc_array[j] = temp;
921
6.54k
                }
922
10.8k
            }
923
5.72k
        }
924
2.68k
    }
925
926
2.68k
    {
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
2.68k
        WORD32 ref = ps_lap_out->i4_poc;
933
934
2.68k
        if(ps_lap_out->i4_is_ref_pic && ref > *p_ref_poc_array)
935
2.68k
        {
936
2.68k
            *p_ref_poc_array = ref;
937
2.68k
        }
938
2.68k
    }
939
940
2.68k
    return;
941
2.68k
}
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
2.70k
{
951
2.70k
    WORD32 i4_num_b_frames = -1;
952
2.70k
    WORD32 i4_sd = ps_lap_struct->i4_sub_gop_size;
953
2.70k
    WORD32 i4_sd_idr = ps_lap_struct->i4_sub_gop_size_idr;
954
2.70k
    WORD32 i4_Midr = ps_lap_struct->i4_max_idr_period;
955
2.70k
    WORD32 i4_midr = ps_lap_struct->i4_min_idr_period;
956
2.70k
    WORD32 i4_Mcra = ps_lap_struct->i4_max_cra_period;
957
2.70k
    WORD32 i4_Mi = ps_lap_struct->i4_max_i_period;
958
2.70k
    WORD32 i4_Cd = ps_lap_struct->i4_idr_counter;
959
2.70k
    WORD32 i4_Cc = ps_lap_struct->i4_cra_counter;
960
2.70k
    WORD32 i4_Ci = ps_lap_struct->i4_i_counter;
961
962
2.70k
    if(ps_lap_struct->i4_force_idr_pos)
963
53
    {
964
53
        ps_lap_struct->i4_num_frm_type_decided = 1;
965
53
        ps_lap_struct->ai1_pic_type[0] = PIC_TYPE_IDR;
966
53
        ps_lap_struct->i4_idr_counter = 0;
967
53
        ps_lap_struct->i4_cra_counter = 0;
968
53
        ps_lap_struct->i4_i_counter = 0;
969
53
        ps_lap_struct->i4_force_idr_pos = 0;
970
53
        ps_lap_struct->i4_sub_gop_pic_idx = 0;
971
53
    }
972
973
2.70k
    if(i4_Midr)
974
2.70k
        ASSERT(i4_Cd < i4_Midr);
975
976
2.70k
    if(i4_Mcra)
977
2.70k
        ASSERT(i4_Cc < i4_Mcra);
978
979
2.70k
    if(i4_Mi)
980
2.70k
        ASSERT(i4_Ci < i4_Mi);
981
982
    /*if all are i pictures */
983
2.70k
    if((i4_Midr == 1) || (i4_Mcra == 1) || (i4_Mi == 1))
984
152
    {
985
152
        ps_lap_struct->i4_num_frm_type_decided = 1;
986
152
        if((i4_Midr == 1) || ((i4_Cd + i4_sd) == i4_Midr))
987
124
        {
988
124
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_IDR;
989
124
            ps_lap_struct->i4_idr_counter = 0;
990
124
            ps_lap_struct->i4_cra_counter = 0;
991
124
            ps_lap_struct->i4_i_counter = 0;
992
124
        }
993
28
        else if((i4_Mcra == 1) || ((i4_Cc + i4_sd) == i4_Mcra))
994
6
        {
995
6
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_CRA;
996
6
            ps_lap_struct->i4_idr_counter += 1;
997
6
            ps_lap_struct->i4_cra_counter = 0;
998
6
            ps_lap_struct->i4_i_counter = 0;
999
6
        }
1000
22
        else
1001
22
        {
1002
22
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_I;
1003
22
            ps_lap_struct->i4_idr_counter += 1;
1004
22
            ps_lap_struct->i4_cra_counter += 1;
1005
22
            ps_lap_struct->i4_i_counter = 0;
1006
22
        }
1007
152
        return;
1008
152
    }
1009
1010
2.55k
    if((i4_Cd + i4_sd_idr >= i4_Midr) && i4_Midr)
1011
89
    {
1012
        /*if idr falls already on sub-gop aligned w.r.t Midr or if strict idr use case*/
1013
89
        if(i4_sd_idr != i4_sd)
1014
10
        {
1015
10
            i4_num_b_frames = i4_Midr - i4_Cd - 2;
1016
10
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1017
10
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_P;
1018
10
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 2] = PIC_TYPE_IDR;
1019
10
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 2;
1020
10
            ps_lap_struct->i4_idr_counter = 0;
1021
10
            ps_lap_struct->i4_cra_counter = 0;
1022
10
            ps_lap_struct->i4_i_counter = 0;
1023
10
        }
1024
79
        else
1025
79
        {
1026
79
            i4_num_b_frames = 0;
1027
79
            ps_lap_struct->ai1_pic_type[1] = PIC_TYPE_IDR;
1028
79
            ps_lap_struct->i4_num_frm_type_decided = 1;
1029
79
            ps_lap_struct->i4_idr_counter = 0;
1030
79
            ps_lap_struct->i4_cra_counter = 0;
1031
79
            ps_lap_struct->i4_i_counter = 0;
1032
79
        }
1033
89
    }
1034
    /*if next sub gop is going to have CRA as Cc reaches Mcra*/
1035
2.46k
    else if(((i4_Cc + i4_sd) >= i4_Mcra) && i4_Mcra)
1036
75
    {
1037
75
        if(((i4_Cc + i4_sd) == i4_Mcra) || (1 == ps_lap_struct->i4_fixed_open_gop_period))
1038
75
        {
1039
75
            i4_num_b_frames = i4_Mcra - i4_Cc - 1;
1040
75
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1041
75
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_CRA;
1042
75
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1043
75
            ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1044
75
            ps_lap_struct->i4_cra_counter = 0;
1045
75
            ps_lap_struct->i4_i_counter = 0;
1046
75
        }
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
75
    }
1059
    /*if next sub gop is going to have I_slice as Ci reaches Mi*/
1060
2.38k
    else if((i4_Ci + i4_sd >= i4_Mi) && i4_Mi)
1061
55
    {
1062
55
        if(((i4_Ci + i4_sd) == i4_Mi) || (1 == ps_lap_struct->i4_fixed_i_period))
1063
55
        {
1064
55
            i4_num_b_frames = i4_Mi - i4_Ci - 1;
1065
55
            memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1066
55
            ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_I;
1067
55
            ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1068
55
            ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1069
55
            ps_lap_struct->i4_cra_counter += ps_lap_struct->i4_num_frm_type_decided;
1070
55
            ps_lap_struct->i4_i_counter = 0;
1071
55
        }
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
55
    }
1084
    /* if next sub-gop is not going to be idr,cra,I*/
1085
2.33k
    else
1086
2.33k
    {
1087
2.33k
        i4_num_b_frames = i4_sd - 1;
1088
2.33k
        memset(&ps_lap_struct->ai1_pic_type[1], PIC_TYPE_B, i4_num_b_frames);
1089
2.33k
        ps_lap_struct->ai1_pic_type[i4_num_b_frames + 1] = PIC_TYPE_P;
1090
2.33k
        ps_lap_struct->i4_num_frm_type_decided = i4_num_b_frames + 1;
1091
2.33k
        ps_lap_struct->i4_idr_counter += ps_lap_struct->i4_num_frm_type_decided;
1092
2.33k
        ps_lap_struct->i4_cra_counter += ps_lap_struct->i4_num_frm_type_decided;
1093
2.33k
        ps_lap_struct->i4_i_counter += ps_lap_struct->i4_num_frm_type_decided;
1094
2.33k
    }
1095
2.55k
    ASSERT(i4_num_b_frames != -1);
1096
1097
2.55k
    return;
1098
2.55k
}
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
2.72k
{
1108
2.72k
    WORD8 pic_type = ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
1109
1110
2.72k
    switch(pic_type)
1111
2.72k
    {
1112
64
    case PIC_TYPE_I:
1113
64
    {
1114
64
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_I_FRAME;
1115
64
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1116
64
        ps_lap_inp_buf->s_lap_out.i4_is_I_in_any_field = 1;
1117
64
        break;
1118
0
    }
1119
2.10k
    case PIC_TYPE_P:
1120
2.10k
    {
1121
2.10k
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_P_FRAME;
1122
2.10k
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1123
2.10k
        break;
1124
0
    }
1125
45
    case PIC_TYPE_B:
1126
45
    {
1127
45
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_B_FRAME;
1128
45
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1129
45
        break;
1130
0
    }
1131
446
    case PIC_TYPE_IDR:
1132
446
    {
1133
446
        ps_lap_struct->i4_curr_poc = 0;
1134
446
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_IDR_FRAME;
1135
446
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 0;
1136
446
        break;
1137
0
    }
1138
67
    case PIC_TYPE_CRA:
1139
67
    {
1140
67
        ps_lap_inp_buf->s_lap_out.i4_pic_type = IV_I_FRAME;
1141
67
        ps_lap_inp_buf->s_lap_out.i4_is_I_in_any_field = 1;
1142
67
        ps_lap_inp_buf->s_lap_out.i4_is_cra_pic = 1;
1143
67
        break;
1144
0
    }
1145
0
    default:
1146
0
        ASSERT(0);
1147
2.72k
    }
1148
2.72k
    return;
1149
2.72k
}
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
245
{
1164
245
    if(encode_node_t == NULL)
1165
140
        return;
1166
1167
105
    encode_order[*loop_count] = (ihevce_lap_enc_buf_t *)encode_node_t->ps_lap_top_buff;
1168
1169
105
    if(encode_order[*loop_count] != NULL)
1170
42
    {
1171
42
        ihevce_lap_enc_buf_t *ps_lap_inp;
1172
1173
42
        ps_lap_struct->pi4_encode_poc_ptr[0] = encode_node_t->data;
1174
42
        ref_b_pic_population(curr_layer, encode_order[*loop_count], ps_lap_struct);
1175
1176
42
        ps_lap_inp = (ihevce_lap_enc_buf_t *)encode_order[*loop_count];
1177
42
        ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1178
1179
42
        ps_lap_struct->pi4_encode_poc_ptr++;
1180
42
    }
1181
1182
105
    (*loop_count) = (*loop_count) + 1;
1183
1184
    /* Pre-order Left-node traversal*/
1185
105
    ihevce_encode_order_traversal_nodes(
1186
105
        (ihevce_encode_node_t *)encode_node_t->pv_left_node,
1187
105
        encode_order,
1188
105
        loop_count,
1189
105
        curr_layer + 1,
1190
105
        ps_lap_struct);
1191
1192
    /* Pre-order Right-node traversal*/
1193
105
    ihevce_encode_order_traversal_nodes(
1194
105
        (ihevce_encode_node_t *)encode_node_t->pv_right_node,
1195
105
        encode_order,
1196
105
        loop_count,
1197
105
        curr_layer + 1,
1198
105
        ps_lap_struct);
1199
105
}
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
245
{
1214
245
    if(encode_node_t == NULL)
1215
140
        return;
1216
1217
    /* Inorder Insertion for the left-child node */
1218
105
    ihevce_capture_order_traversal_nodes(
1219
105
        (ihevce_encode_node_t *)encode_node_t->pv_left_node,
1220
105
        api4_capture_order_array,
1221
105
        capture_order_poc_array,
1222
105
        loop_count,
1223
105
        i4_interlace_field);
1224
1225
105
    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
105
    else
1234
105
    {
1235
105
        encode_node_t->ps_lap_top_buff =
1236
105
            (ihevce_lap_enc_buf_t *)api4_capture_order_array[*loop_count];
1237
105
        encode_node_t->data = capture_order_poc_array[*loop_count];
1238
105
    }
1239
105
    if(i4_interlace_field)
1240
0
        (*loop_count) = (*loop_count) + 2;
1241
105
    else
1242
105
        (*loop_count) = (*loop_count) + 1;
1243
1244
    /* Inorder Insertion for the right-child node */
1245
105
    ihevce_capture_order_traversal_nodes(
1246
105
        (ihevce_encode_node_t *)encode_node_t->pv_right_node,
1247
105
        api4_capture_order_array,
1248
105
        capture_order_poc_array,
1249
105
        loop_count,
1250
105
        i4_interlace_field);
1251
105
}
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
2.70k
{
1262
2.70k
    ihevce_lap_enc_buf_t *ps_lap_inp = NULL;
1263
2.70k
    WORD32 hier_layer = ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
1264
2.70k
    WORD32 sub_gop_size = ps_lap_struct->i4_dyn_sub_gop_size;
1265
2.70k
    ihevce_lap_enc_buf_t **api4_capture_order_array = ps_lap_struct->api4_capture_order_array;
1266
2.70k
    ihevce_lap_enc_buf_t **api4_encode_order_array = ps_lap_struct->api4_encode_order_array;
1267
2.70k
    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
2.70k
    if(1 == ps_lap_struct->i4_idr_flag)
1271
446
    {
1272
446
        if(i4_first_gop)
1273
199
        {
1274
199
            api4_encode_order_array[0] = api4_capture_order_array[0];
1275
1276
199
            if(api4_encode_order_array[0] != NULL)
1277
199
            {
1278
199
                ps_lap_struct->pi4_encode_poc_ptr[0] = ai4_capture_order_poc[0];
1279
199
                ref_pic_population(api4_encode_order_array[0], ps_lap_struct);
1280
1281
199
                ps_lap_inp = api4_encode_order_array[0];
1282
199
                ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1283
1284
199
                ps_lap_struct->pi4_encode_poc_ptr++;
1285
199
            }
1286
1287
199
            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
199
        }
1304
247
        else
1305
247
        {
1306
247
            api4_encode_order_array[0] = api4_capture_order_array[sub_gop_size - 1];
1307
1308
247
            if(api4_encode_order_array[0] != NULL)
1309
247
            {
1310
247
                ps_lap_struct->pi4_encode_poc_ptr[0] = ai4_capture_order_poc[sub_gop_size - 1];
1311
247
                ref_pic_population(api4_encode_order_array[0], ps_lap_struct);
1312
1313
247
                ps_lap_inp = api4_encode_order_array[0];
1314
247
                ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1315
1316
247
                ps_lap_struct->pi4_encode_poc_ptr++;
1317
247
            }
1318
247
        }
1319
446
    }
1320
2.26k
    else
1321
2.26k
    {
1322
2.26k
        api4_encode_order_array[0] = api4_capture_order_array[sub_gop_size - 1];
1323
1324
2.26k
        if(api4_encode_order_array[0] != NULL)
1325
2.24k
        {
1326
2.24k
            ps_lap_struct->pi4_encode_poc_ptr[0] = ai4_capture_order_poc[sub_gop_size - 1];
1327
2.24k
            ref_pic_population(api4_encode_order_array[0], ps_lap_struct);
1328
1329
2.24k
            ps_lap_inp = api4_encode_order_array[0];
1330
2.24k
            ihevce_rc_populate_common_params(&ps_lap_inp->s_lap_out, &ps_lap_inp->s_rc_lap_out);
1331
1332
2.24k
            ps_lap_struct->pi4_encode_poc_ptr++;
1333
2.24k
        }
1334
2.26k
    }
1335
2.70k
    return;
1336
2.70k
}
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
35
{
1346
35
    WORD32 interlace_field = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
1347
35
    ihevce_lap_enc_buf_t **api4_encode_order_array = ps_lap_struct->api4_encode_order_array;
1348
35
    WORD32 *capture_order_poc_array = ps_lap_struct->pi4_capture_poc_ptr;
1349
35
    WORD32 loop_count = 0;
1350
1351
    /* encoder_order offset changed dependent on IDR and Interlace Field */
1352
35
    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
35
    ihevce_capture_order_traversal_nodes(
1357
35
        ps_encode_node,
1358
35
        &ps_lap_struct->api4_capture_order_array[0],
1359
35
        capture_order_poc_array,
1360
35
        &loop_count,
1361
35
        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
35
    if(ps_lap_struct->i4_idr_flag)
1366
0
        loop_count = 2 + (interlace_field * 2);
1367
35
    else
1368
35
        loop_count = 1 + interlace_field;
1369
1370
    /* Pre-order traversal of the tree to get encode-order POCs*/
1371
35
    ihevce_encode_order_traversal_nodes(
1372
35
        ps_encode_node, api4_encode_order_array, &loop_count, 1, ps_lap_struct);
1373
1374
35
    return;
1375
35
}
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
2.52k
{
1386
2.52k
    ihevce_lap_output_params_t *ps_lap_out = &ps_lap_out_buf->s_lap_out;
1387
1388
    /* model update flag for rc*/
1389
2.52k
    if(ps_lap_out->i4_pic_type == IV_P_FRAME)
1390
1.94k
    {
1391
1.94k
        WORD32 i4_loop = 0;
1392
1.94k
        WORD32 i4_min_delta_poc = 0x7FFFFFFF;
1393
1394
8.05k
        for(i4_loop = 0; i4_loop < ps_lap_out->i4_num_ref_pics; i4_loop++)
1395
6.10k
        {
1396
6.10k
            if(i4_min_delta_poc > ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc))
1397
1.94k
            {
1398
1.94k
                i4_min_delta_poc = ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc);
1399
1.94k
            }
1400
6.10k
        }
1401
1.94k
    }
1402
1403
2.52k
    if(ps_lap_out->i4_pic_type == IV_B_FRAME)
1404
31
    {
1405
31
        WORD32 i4_loop = 0;
1406
31
        WORD32 i4_min_delta_poc = 0x7FFFFFFF;
1407
31
        WORD32 i4_min_delta_poc_for_b =
1408
31
            (1 << ps_lap_struct->s_lap_static_params.i4_max_temporal_layers) /
1409
31
            (ps_lap_out->i4_temporal_lyr_id + 1);
1410
1411
129
        for(i4_loop = 0; i4_loop < ps_lap_out->i4_num_ref_pics; i4_loop++)
1412
98
        {
1413
98
            if(i4_min_delta_poc > ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc))
1414
31
            {
1415
31
                i4_min_delta_poc = ABS(ps_lap_out->as_ref_pics[i4_loop].i4_ref_pic_delta_poc);
1416
31
            }
1417
98
        }
1418
31
    }
1419
2.52k
    return;
1420
2.52k
}
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
2.52k
{
1430
2.52k
    WORD32 i4_field_flag = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
1431
2.52k
    rc_lap_out_params_t *ps_rc_lap_out = &ps_lap_out_buf->s_rc_lap_out;
1432
1433
2.52k
    ps_lap_struct->i4_lap2_counter++;
1434
1435
2.52k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_I_FRAME ||
1436
2.40k
       ps_lap_out_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME)
1437
552
    {
1438
552
        ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = I_PIC;
1439
552
        GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1440
552
    }
1441
1.97k
    else if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_P_FRAME)
1442
1.94k
    {
1443
1.94k
        if(ps_lap_out_buf->s_lap_out.i4_first_field)
1444
1.94k
        {
1445
1.94k
            ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = P_PIC;
1446
1.94k
        }
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
1.94k
        GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1452
1.94k
    }
1453
31
    else if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_B_FRAME)
1454
31
    {
1455
31
        if(ps_lap_out_buf->s_lap_out.i4_temporal_lyr_id == 1)
1456
11
        {
1457
11
            if(ps_lap_out_buf->s_lap_out.i4_first_field)
1458
11
            {
1459
11
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = B_PIC;
1460
11
            }
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
11
            GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1466
11
        }
1467
20
        else if(ps_lap_out_buf->s_lap_out.i4_temporal_lyr_id == 2)
1468
20
        {
1469
20
            if(ps_lap_out_buf->s_lap_out.i4_first_field)
1470
20
            {
1471
20
                ps_lap_struct->ai4_pic_type_to_be_removed[ps_lap_struct->i4_enq_idx] = B1_PIC;
1472
20
            }
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
20
            GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_enq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1478
20
        }
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
31
    }
1496
0
    else
1497
0
    {
1498
0
        ASSERT(0);
1499
0
    }
1500
1501
2.52k
    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
2.52k
    else
1546
2.52k
    {
1547
2.52k
        ASSERT(ps_lap_struct->i4_lap2_counter <= ps_lap_struct->i4_rc_lap_period);
1548
1549
2.52k
        if(ps_lap_struct->i4_lap2_counter == ps_lap_struct->i4_rc_lap_period)
1550
2.52k
        {
1551
2.52k
            WORD32 i4_loop, i4_period, i4_next_i_pic = 0;
1552
2.52k
            WORD32 i4_stop_count = 0;
1553
2.52k
            WORD32 i4_temp_deq = ps_lap_struct->i4_deq_idx;
1554
2.52k
            WORD32 i4_first_pic_type = ps_lap_struct->ai4_pic_type_to_be_removed[i4_temp_deq];
1555
1556
2.52k
            if(ps_lap_struct->i4_rc_lap_period >= ps_lap_struct->i4_gop_period)
1557
136
            {
1558
136
                i4_period = ps_lap_struct->i4_gop_period;
1559
136
            }
1560
2.39k
            else
1561
2.39k
            {
1562
2.39k
                i4_period = ps_lap_struct->i4_rc_lap_period;
1563
2.39k
            }
1564
1565
5.05k
            for(i4_loop = 0; i4_loop < i4_period; i4_loop++)
1566
2.52k
            {
1567
2.52k
                if(ps_lap_struct->ai4_pic_type_to_be_removed[i4_temp_deq] == I_PIC && i4_loop &&
1568
0
                   i4_first_pic_type == I_PIC)
1569
0
                {
1570
0
                    i4_stop_count = 1;
1571
0
                }
1572
1573
2.52k
                if(!i4_stop_count)
1574
2.52k
                {
1575
2.52k
                    ps_rc_lap_out->i4_next_sc_i_in_rc_look_ahead++;
1576
2.52k
                }
1577
1578
2.52k
                ps_rc_lap_out
1579
2.52k
                    ->ai4_num_pic_type[ps_lap_struct->ai4_pic_type_to_be_removed[i4_temp_deq]]++;
1580
1581
2.52k
                GET_IDX_CIRCULAR_BUF(i4_temp_deq, 1, NUM_LAP2_LOOK_AHEAD);
1582
2.52k
            }
1583
2.52k
            if(ps_lap_struct->i4_rc_lap_period < ps_lap_struct->i4_gop_period)
1584
2.39k
            {
1585
2.39k
                WORD32 i4_loop;
1586
2.39k
                WORD32 idx = 0;
1587
2.39k
                WORD32 i4_max_temporal_layer =
1588
2.39k
                    ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
1589
1590
2.39k
                for(i4_loop = 0;
1591
57.6k
                    i4_loop < (ps_lap_struct->i4_gop_period - ps_lap_struct->i4_rc_lap_period) &&
1592
55.2k
                    (!i4_next_i_pic);
1593
55.2k
                    i4_loop++)
1594
55.2k
                {
1595
55.2k
                    if(!i4_stop_count)
1596
55.2k
                    {
1597
55.2k
                        ps_rc_lap_out->i4_next_sc_i_in_rc_look_ahead++;
1598
55.2k
                    }
1599
1600
55.2k
                    if(i4_max_temporal_layer == 0)
1601
54.6k
                    {
1602
54.6k
                        if(ps_lap_struct->i4_is_all_i_pic_in_seq)
1603
6
                        {
1604
6
                            ps_rc_lap_out->ai4_num_pic_type[I_PIC]++;
1605
6
                        }
1606
54.6k
                        else
1607
54.6k
                        {
1608
                            /*second field*/
1609
54.6k
                            if((i4_loop & 1) && i4_field_flag)
1610
0
                            {
1611
0
                                ps_rc_lap_out->ai4_num_pic_type[P1_PIC]++;
1612
0
                            }
1613
54.6k
                            else
1614
54.6k
                            {
1615
54.6k
                                ps_rc_lap_out->ai4_num_pic_type[P_PIC]++;
1616
54.6k
                            }
1617
54.6k
                        }
1618
54.6k
                    }
1619
627
                    else
1620
627
                    {
1621
627
                        ps_rc_lap_out->ai4_num_pic_type
1622
627
                            [gau1_order_insert_pic_type[i4_max_temporal_layer - 1][idx]]++;
1623
627
                        GET_IDX_CIRCULAR_BUF(idx, 1, (8 << i4_field_flag));
1624
627
                    }
1625
55.2k
                }
1626
2.39k
            }
1627
            /*remove one pic type*/
1628
2.52k
            GET_IDX_CIRCULAR_BUF(ps_lap_struct->i4_deq_idx, 1, NUM_LAP2_LOOK_AHEAD);
1629
2.52k
            ps_lap_struct->i4_lap2_counter--;
1630
2.52k
        }
1631
2.52k
    }
1632
1633
2.52k
    {
1634
2.52k
        WORD32 i4_loop;
1635
2.52k
        WORD32 idx = 0;
1636
2.52k
        WORD32 i4_max_temporal_layer = ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
1637
2.52k
        WORD32 i4_num_pictype = 0;
1638
1639
25.2k
        for(i4_loop = 0; i4_loop < MAX_PIC_TYPE; i4_loop++)
1640
22.7k
        {
1641
22.7k
            i4_num_pictype += ps_rc_lap_out->ai4_num_pic_type[i4_loop];
1642
22.7k
        }
1643
1644
2.52k
        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
2.52k
    }
1679
    /*FOR RC : ensure  at least 1 I pic in the gop period at any case*/
1680
2.52k
    if(!ps_rc_lap_out->ai4_num_pic_type[I_PIC])
1681
1.97k
    {
1682
1.97k
        ASSERT(ps_rc_lap_out->ai4_num_pic_type[P_PIC]);
1683
1.97k
        ps_lap_out_buf->s_rc_lap_out.ai4_num_pic_type[P_PIC]--;
1684
1.97k
        ps_lap_out_buf->s_rc_lap_out.ai4_num_pic_type[I_PIC]++;
1685
1.97k
    }
1686
2.52k
    return;
1687
2.52k
}
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
2.52k
{
1697
2.52k
    WORD32 i4_first_field = 1;
1698
2.52k
    WORD32 i4_field = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
1699
1700
2.52k
    if(i4_field)
1701
0
    {
1702
0
        i4_first_field = ps_lap_out_buf->s_lap_out.i4_first_field;
1703
0
    }
1704
1705
2.52k
    ps_lap_out_buf->s_lap_out.i4_used = 0;
1706
1707
2.52k
    rc_update_model_control_by_lap_for_modified_sub_gop(ps_lap_struct, ps_lap_out_buf);
1708
2.52k
    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
2.52k
    ps_lap_out_buf->s_rc_lap_out.ps_rc_lap_out_next_encode = NULL;
1713
2.52k
    if(ps_lap_struct->pv_prev_inp_buf != NULL &&
1714
2.32k
       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
2.52k
    ps_lap_struct->pv_prev_inp_buf = (void *)ps_lap_out_buf;
1721
2.52k
    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
2.52k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_P_FRAME ||
1737
583
       ps_lap_out_buf->s_lap_out.i4_pic_type == IV_B_FRAME)
1738
1.97k
    {
1739
1.97k
        ASSERT(ps_lap_out_buf->s_lap_out.i4_num_ref_pics != 0);
1740
1.97k
    }
1741
1742
    /*assert if poc = 0 and pictype is not an idr*/
1743
2.52k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type != IV_IDR_FRAME &&
1744
2.09k
       ps_lap_out_buf->s_lap_out.i4_poc == 0)
1745
0
    {
1746
0
        ASSERT(0);
1747
0
    }
1748
2.52k
    if(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME &&
1749
430
       ps_lap_out_buf->s_lap_out.i4_poc != 0)
1750
0
    {
1751
0
        ASSERT(0);
1752
0
    }
1753
2.52k
    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
2.52k
    if(!ps_lap_struct->i4_max_cra_period)
1765
768
    {
1766
768
        ASSERT(ps_lap_out_buf->s_lap_out.i4_is_cra_pic != 1);
1767
768
    }
1768
1769
2.52k
    if(ps_lap_out_buf->s_lap_out.i4_force_idr_flag)
1770
76
    {
1771
76
        ASSERT(ps_lap_out_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME);
1772
76
    }
1773
2.52k
    ps_lap_out_buf->s_lap_out.i4_curr_frm_qp = -1;
1774
2.52k
}
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
2.84k
{
1785
2.84k
    ihevce_encode_node_t *ps_encode_node =
1786
2.84k
        (ihevce_encode_node_t *)ps_lap_struct->aps_encode_node[*pi4_tree_num];
1787
1788
2.84k
    WORD32 i4_capture_idx = ps_lap_struct->i4_capture_idx;
1789
1790
    /* Static Lap parameters */
1791
2.84k
    ihevce_lap_static_params_t *ps_lap_static_params =
1792
2.84k
        (ihevce_lap_static_params_t *)&ps_lap_struct->s_lap_static_params;
1793
1794
2.84k
    WORD32 hier_layer = ps_lap_static_params->i4_max_temporal_layers;
1795
2.84k
    WORD32 sub_gop_size = ps_lap_struct->i4_dyn_sub_gop_size;
1796
1797
    /* queue the current input in capture array */
1798
2.84k
    {
1799
2.84k
        WORD32 first_gop_flag;
1800
1801
2.84k
        if(!i4_capture_idx)
1802
2.71k
        {
1803
2.71k
            memset(
1804
2.71k
                &ps_lap_struct->api4_capture_order_array[0],
1805
2.71k
                0,
1806
2.71k
                sizeof(ihevce_lap_enc_buf_t *) * MAX_NUM_ENC_NODES);
1807
2.71k
        }
1808
2.84k
        ps_lap_struct->api4_capture_order_array[i4_capture_idx] = ps_input_lap_enc_buf;
1809
1810
2.84k
        if(ps_input_lap_enc_buf != NULL)
1811
2.72k
        {
1812
2.72k
            if(ps_input_lap_enc_buf->s_lap_out.i4_end_flag == 1)
1813
199
                ps_lap_struct->i4_end_flag_pic_idx = i4_capture_idx;
1814
2.72k
            ps_lap_struct->ai4_capture_order_poc[i4_capture_idx] = ps_lap_struct->i4_curr_poc++;
1815
2.72k
        }
1816
1817
2.84k
        if((1 == ps_lap_struct->i4_num_dummy_pic) && (ps_lap_struct->i4_sub_gop_end == 0))
1818
0
        {
1819
0
            ps_lap_struct->i4_sub_gop_end = i4_capture_idx - 1;
1820
0
        }
1821
2.84k
        i4_capture_idx++;
1822
1823
        /* to take care of buffering 1 extra picture at start or at IDR interval*/
1824
2.84k
        if(!ps_lap_struct->i4_is_all_i_pic_in_seq)
1825
2.69k
        {
1826
2.69k
            if(ps_lap_static_params->i4_src_interlace_field && sub_gop_size <= 2)
1827
0
            {
1828
0
                first_gop_flag = 0;
1829
0
            }
1830
2.69k
            else
1831
2.69k
            {
1832
2.69k
                first_gop_flag = ps_lap_struct->i4_idr_flag
1833
2.69k
                                 << ps_lap_static_params->i4_src_interlace_field;
1834
2.69k
            }
1835
2.69k
        }
1836
152
        else
1837
152
        {
1838
152
            first_gop_flag = ps_lap_struct->i4_idr_flag;
1839
152
        }
1840
1841
        /* For every IDR period, set idr_flag and reset POC value and gop_size to 0*/
1842
2.84k
        if(ps_input_lap_enc_buf != NULL)
1843
2.72k
        {
1844
2.72k
            if((!first_gop_flag) && (ps_input_lap_enc_buf->s_lap_out.i4_pic_type == IV_IDR_FRAME))
1845
247
            {
1846
247
                ps_lap_struct->pi4_encode_poc_ptr = &ps_lap_struct->ai4_encode_order_poc[0];
1847
247
                ps_lap_struct->i4_idr_flag = 1;
1848
247
                ps_lap_struct->i4_curr_poc = 0;
1849
247
                ps_lap_struct->ai4_capture_order_poc[i4_capture_idx - 1] =
1850
247
                    ps_lap_struct->i4_curr_poc++;
1851
247
            }
1852
2.72k
        }
1853
1854
2.84k
        if(first_gop_flag &&
1855
199
           (ps_lap_struct->i4_is_all_i_pic_in_seq || ps_lap_struct->i4_immediate_idr_case))
1856
199
        {
1857
199
            sub_gop_size = 0;
1858
199
        }
1859
1860
2.84k
        if(!first_gop_flag && ps_lap_struct->i4_immediate_idr_case &&
1861
270
           (i4_capture_idx != (sub_gop_size + first_gop_flag)))
1862
3
        {
1863
3
            sub_gop_size = 1 << ps_lap_static_params->i4_src_interlace_field;
1864
3
            ps_lap_struct->i4_dyn_sub_gop_size = 1 << ps_lap_static_params->i4_src_interlace_field;
1865
3
        }
1866
1867
        /* reset the queue idx end of every gop */
1868
2.84k
        if(i4_capture_idx == (sub_gop_size + first_gop_flag))
1869
2.70k
        {
1870
2.70k
            ps_lap_struct->pi4_encode_poc_ptr = &ps_lap_struct->ai4_encode_order_poc[0];
1871
1872
2.70k
            if(ps_lap_struct->i4_end_flag_pic_idx && (1 != sub_gop_size))
1873
5
            {
1874
5
                WORD32 i4_temp_poc = 0;
1875
5
                ihevce_lap_enc_buf_t *ps_temp_lap_enc_buf = NULL;
1876
1877
                /*swap the lap enc buf and poc*/
1878
5
                ps_temp_lap_enc_buf =
1879
5
                    ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx - 1];
1880
5
                ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx - 1] =
1881
5
                    NULL;
1882
5
                ps_lap_struct->api4_capture_order_array[i4_capture_idx - 2] =
1883
5
                    ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx];
1884
1885
5
                if((i4_capture_idx - 2) != ps_lap_struct->i4_end_flag_pic_idx)
1886
5
                    ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_end_flag_pic_idx] =
1887
5
                        NULL;
1888
1889
5
                ps_temp_lap_enc_buf->s_lap_out.i4_pic_type = IV_P_FRAME;
1890
5
                ps_lap_struct->api4_capture_order_array[i4_capture_idx - 1] = ps_temp_lap_enc_buf;
1891
1892
5
                i4_temp_poc =
1893
5
                    ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_end_flag_pic_idx - 1];
1894
5
                ps_lap_struct->ai4_capture_order_poc[i4_capture_idx - 2] =
1895
5
                    ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_end_flag_pic_idx];
1896
1897
5
                ps_lap_struct->ai4_capture_order_poc[i4_capture_idx - 1] = i4_temp_poc;
1898
5
            }
1899
1900
2.70k
            if(ps_lap_struct->i4_num_dummy_pic)
1901
0
            {
1902
0
                WORD32 pic_idx;
1903
0
                ihevce_lap_enc_buf_t *ps_temp_lap_enc_buf = NULL;
1904
0
                static const WORD32 subgop_temporal_layer3[8] = { 7, 3, 1, 0, 2, 5, 4, 6 };
1905
0
                static const WORD32 subgop_temporal_layer2[4] = { 3, 1, 0, 2 };
1906
0
                const WORD32 *subgop_pic_idx = (ps_lap_static_params->i4_max_temporal_layers == 2)
1907
0
                                                   ? &subgop_temporal_layer2[0]
1908
0
                                                   : &subgop_temporal_layer3[0];
1909
0
                WORD32 max_pic_count = ps_lap_struct->i4_sub_gop_end + 1;
1910
1911
0
                for(pic_idx = 0; pic_idx < max_pic_count; pic_idx++)
1912
0
                {
1913
0
                    WORD32 i4_temp_idx = ps_lap_static_params->i4_max_temporal_layers > 1
1914
0
                                             ? subgop_pic_idx[pic_idx]
1915
0
                                             : 1;
1916
1917
0
                    if(NULL == ps_lap_struct->api4_capture_order_array[i4_temp_idx])
1918
0
                    {
1919
0
                        ps_temp_lap_enc_buf =
1920
0
                            ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_sub_gop_end];
1921
0
                        if(pic_idx == 0)
1922
0
                        {
1923
0
                            ps_temp_lap_enc_buf->s_lap_out.i4_pic_type = IV_P_FRAME;
1924
0
                        }
1925
0
                        ps_lap_struct->api4_capture_order_array[i4_temp_idx] = ps_temp_lap_enc_buf;
1926
0
                        ps_lap_struct->api4_capture_order_array[ps_lap_struct->i4_sub_gop_end] =
1927
0
                            NULL;
1928
1929
0
                        ps_lap_struct->ai4_capture_order_poc[i4_temp_idx] =
1930
0
                            ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_sub_gop_end];
1931
0
                        ps_lap_struct->ai4_capture_order_poc[ps_lap_struct->i4_sub_gop_end] = 0;
1932
0
                        ps_lap_struct->i4_sub_gop_end--;
1933
0
                    }
1934
0
                }
1935
0
                ps_lap_struct->i4_sub_gop_end = 0;
1936
0
            }
1937
2.70k
            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
2.70k
            ihevce_ip_pic_population(ps_encode_node, ps_lap_struct, first_gop_flag);
1944
1945
            /* For hierarchical layers, Populate B picture */
1946
2.70k
            if((hier_layer > 0) &&
1947
50
               sub_gop_size > (1 << ps_lap_static_params->i4_src_interlace_field))
1948
35
            {
1949
35
                ihevce_b_pic_population(ps_encode_node, ps_lap_struct);
1950
35
            }
1951
1952
2.70k
            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
2.70k
            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
2.70k
            ps_lap_struct->i4_idr_flag = 0;
2002
2.70k
            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
2.70k
            memcpy(
2006
2.70k
                &ps_lap_struct->api4_lap_out_buf[ps_lap_struct->i4_lap_encode_idx],
2007
2.70k
                &ps_lap_struct->api4_encode_order_array[0],
2008
2.70k
                sizeof(ihevce_lap_enc_buf_t *) * ps_lap_struct->i4_num_bufs_encode_order);
2009
2010
2.70k
            memset(
2011
2.70k
                &ps_lap_struct->api4_encode_order_array[0],
2012
2.70k
                0,
2013
2.70k
                sizeof(ihevce_lap_enc_buf_t *) * ps_lap_struct->i4_num_bufs_encode_order);
2014
2015
2.70k
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_lap_encode_idx] =
2016
2.70k
                ps_lap_struct->i4_num_bufs_encode_order - ps_lap_struct->i4_num_dummy_pic;
2017
2018
2.70k
            ps_lap_struct->i4_lap_encode_idx++;
2019
2.70k
            ps_lap_struct->i4_lap_encode_idx &= (MAX_SUBGOP_IN_ENCODE_QUEUE - 1);
2020
2.70k
        }
2021
2022
        /* store the capture index */
2023
2.84k
        ps_lap_struct->i4_capture_idx = i4_capture_idx;
2024
2.84k
        ps_lap_struct->i4_immediate_idr_case = 0;
2025
2.84k
    }
2026
2.84k
    return;
2027
2.84k
}
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
2.84k
{
2037
2.84k
    lap_intface_t *ps_lap_interface = (lap_intface_t *)pv_interface_ctxt;
2038
2.84k
    lap_struct_t *ps_lap_struct = (lap_struct_t *)ps_lap_interface->pv_lap_module_ctxt;
2039
2.84k
    ihevce_hle_ctxt_t *ps_hle_ctxt = (ihevce_hle_ctxt_t *)ps_lap_interface->pv_hle_ctxt;
2040
2.84k
    ihevce_lap_enc_buf_t *ps_lap_inp_buf = ps_curr_inp;
2041
2.84k
    ihevce_tgt_params_t *ps_tgt_params =
2042
2.84k
        &ps_lap_struct->s_static_cfg_params.s_tgt_lyr_prms.as_tgt_params[0];
2043
2.84k
    WORD32 i4_field_flag = ps_lap_struct->s_lap_static_params.i4_src_interlace_field;
2044
2.84k
    WORD32 i4_flush_check = 0;
2045
2.84k
    WORD32 i4_force_idr_check = 0;
2046
2.84k
    WORD32 i4_tree_num = 0;
2047
2.84k
    iv_input_ctrl_buffs_t *ps_ctrl_buf = NULL;
2048
2.84k
    WORD32 buf_id = 0;
2049
2.84k
    WORD32 i4_lap_window_size = 1 << ps_lap_struct->s_lap_static_params.i4_max_temporal_layers;
2050
2051
2.84k
    ps_lap_interface->i4_ctrl_in_que_blocking_mode = BUFF_QUE_NON_BLOCKING_MODE;
2052
2053
    /* ----------- LAP processing ----------- */
2054
2.84k
    if(ps_lap_struct->end_flag != 1)
2055
2.72k
    {
2056
2.72k
        ASSERT(NULL != ps_curr_inp);
2057
2058
        /* ---------- get the filled control command buffer ------------ */
2059
2.72k
        ps_ctrl_buf = (iv_input_ctrl_buffs_t *)ihevce_q_get_filled_buff(
2060
2.72k
            ps_hle_ctxt->apv_enc_hdl[0],
2061
2.72k
            ps_lap_interface->i4_ctrl_in_que_id,
2062
2.72k
            &buf_id,
2063
2.72k
            ps_lap_interface->i4_ctrl_in_que_blocking_mode);
2064
2065
        /* ----------- check the command ---------------------- */
2066
2.72k
        if(NULL != ps_ctrl_buf)
2067
167
        {
2068
            /* check for async errors */
2069
167
            ihevce_dyn_config_prms_t as_dyn_br[MAX_NUM_DYN_BITRATE_CMDS];
2070
167
            WORD32 i4_num_set_bitrate_cmds = 0;
2071
167
            WORD32 bitrt_ctr = 0;
2072
2073
167
            ihevce_lap_parse_async_cmd(
2074
167
                ps_hle_ctxt,
2075
167
                (WORD32 *)ps_ctrl_buf->pv_asynch_ctrl_bufs,
2076
167
                ps_ctrl_buf->i4_cmd_buf_size,
2077
167
                ps_ctrl_buf->i4_buf_id,
2078
167
                &i4_num_set_bitrate_cmds,
2079
167
                &as_dyn_br[0]);
2080
2081
            /* Call the call back function to register the new bitrate */
2082
334
            for(bitrt_ctr = 0; bitrt_ctr < i4_num_set_bitrate_cmds; bitrt_ctr++)
2083
167
            {
2084
167
                ps_lap_interface->ihevce_dyn_bitrate_cb(
2085
167
                    (void *)ps_hle_ctxt, (void *)&as_dyn_br[bitrt_ctr]);
2086
167
            }
2087
2088
            /* release async ctrl buffer*/
2089
167
            ihevce_q_rel_buf(
2090
167
                ps_hle_ctxt->apv_enc_hdl[0], IHEVCE_INPUT_ASYNCH_CTRL_Q, ps_ctrl_buf->i4_buf_id);
2091
167
        }
2092
2093
2.72k
        {
2094
2.72k
            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
2.72k
            i4_flush_check = 0;
2099
2.72k
            i4_force_idr_check = 0;
2100
2.72k
            ihevce_lap_parse_sync_cmd(
2101
2.72k
                ps_hle_ctxt,
2102
2.72k
                &ps_lap_struct->s_static_cfg_params,
2103
2.72k
                pi4_cmd_buf,
2104
2.72k
                ps_lap_inp_buf,
2105
2.72k
                &i4_flush_check,
2106
2.72k
                &i4_force_idr_check);
2107
2108
2.72k
            if(i4_flush_check)
2109
199
                ps_lap_struct->end_flag = 1;
2110
2111
2.72k
            ps_lap_inp_buf->s_lap_out.i4_out_flush_flag = 0;
2112
2.72k
            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
2.72k
            if(1 == ps_lap_inp_buf->s_input_buf.i4_inp_frm_data_valid_flag)
2116
2.52k
            {
2117
                /* Initialise laps input buffer descriptors */
2118
2.52k
                memset(&ps_lap_inp_buf->s_lap_out, 0, sizeof(ihevce_lap_output_params_t));
2119
2.52k
                memset(&ps_lap_inp_buf->s_rc_lap_out, 0, sizeof(rc_lap_out_params_t));
2120
                /* Default initialization of lapout parameters */
2121
2.52k
                ps_lap_inp_buf->s_lap_out.i4_scene_type = SCENE_TYPE_NORMAL;
2122
2.52k
                ps_lap_inp_buf->s_lap_out.u4_scene_num = 0;
2123
2.52k
                ps_lap_inp_buf->s_lap_out.i4_display_num = ps_lap_struct->i4_display_num;
2124
2.52k
                ps_lap_inp_buf->s_lap_out.i4_quality_preset = ps_tgt_params->i4_quality_preset;
2125
2.52k
                ps_lap_inp_buf->s_lap_out.i1_weighted_pred_flag = 0;
2126
2.52k
                ps_lap_inp_buf->s_lap_out.i1_weighted_bipred_flag = 0;
2127
2.52k
                ps_lap_inp_buf->s_lap_out.i4_log2_luma_wght_denom = DENOM_DEFAULT;
2128
2.52k
                ps_lap_inp_buf->s_lap_out.i4_log2_chroma_wght_denom = DENOM_DEFAULT;
2129
2.52k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0].i4_num_duplicate_entries_in_ref_list = 1;
2130
2.52k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0].i4_used_by_cur_pic_flag = 1;
2131
2.52k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0].as_wght_off[0].u1_luma_weight_enable_flag =
2132
2.52k
                    0;
2133
2.52k
                ps_lap_inp_buf->s_lap_out.as_ref_pics[0]
2134
2.52k
                    .as_wght_off[0]
2135
2.52k
                    .u1_chroma_weight_enable_flag = 0;
2136
2.52k
                ps_lap_inp_buf->s_lap_out.i4_first_field = 1;
2137
2.52k
                ps_lap_inp_buf->s_lap_out.i4_force_idr_flag = 0;
2138
2.52k
                ps_lap_inp_buf->s_lap_out.i4_curr_frm_qp = ps_tgt_params->ai4_frame_qp[0];
2139
2.52k
                ps_lap_inp_buf->s_lap_out.i4_used = 1;
2140
2.52k
                if(i4_force_idr_check)
2141
76
                {
2142
76
                    ps_lap_inp_buf->s_lap_out.i4_force_idr_flag = 1;
2143
76
                }
2144
                /* Populate input params in lap out struct */
2145
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.pv_y_buf =
2146
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.pv_y_buf;
2147
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.pv_u_buf =
2148
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.pv_u_buf;
2149
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.pv_v_buf =
2150
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.pv_v_buf;
2151
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_y_wd =
2152
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_y_wd;
2153
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_y_ht =
2154
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_y_ht;
2155
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_y_strd =
2156
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_y_strd;
2157
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_uv_wd =
2158
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_uv_wd;
2159
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_uv_ht =
2160
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_uv_ht;
2161
2.52k
                ps_lap_inp_buf->s_lap_out.s_input_buf.i4_uv_strd =
2162
2.52k
                    ps_lap_inp_buf->s_input_buf.s_input_buf.i4_uv_strd;
2163
2164
2.52k
                ps_lap_struct->i4_display_num++;
2165
2.52k
                ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_enq_idx] = ps_lap_inp_buf;
2166
                /* update first field flag */
2167
2.52k
                ps_lap_inp_buf->s_lap_out.i4_first_field = 1;
2168
2.52k
                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
2.52k
                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
2.52k
                if((i4_lap_window_size > 1) &&
2182
59
                   (ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr] != PIC_TYPE_IDR))
2183
46
                {
2184
46
                    ps_lap_struct->i4_sub_gop_pic_idx++;
2185
46
                    if(ps_lap_struct->i4_sub_gop_pic_idx > i4_lap_window_size)
2186
7
                    {
2187
7
                        ps_lap_struct->i4_sub_gop_pic_idx =
2188
7
                            ps_lap_struct->i4_sub_gop_pic_idx - i4_lap_window_size;
2189
7
                    }
2190
46
                }
2191
2.48k
                else if(1 == i4_lap_window_size)
2192
2.47k
                {
2193
2.47k
                    ps_lap_struct->i4_sub_gop_pic_idx = 1;
2194
2.47k
                }
2195
2196
2.52k
                if(i4_force_idr_check &&
2197
76
                   (ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr] != PIC_TYPE_IDR))
2198
53
                {
2199
53
                    ps_lap_struct->i4_force_idr_pos = ps_lap_struct->i4_sub_gop_pic_idx;
2200
53
                }
2201
2202
                /* store pictype for next subgop */
2203
2.52k
                if((0 == ps_lap_struct->i4_num_frm_type_decided) &&
2204
2.49k
                   (ps_lap_struct->i4_force_idr_pos == 0))
2205
2.43k
                {
2206
2.43k
                    ps_lap_struct->ai1_pic_type[0] =
2207
2.43k
                        ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
2208
2209
2.43k
                    ihevce_determine_next_sub_gop_state(ps_lap_struct);
2210
2211
2.43k
                    ps_lap_struct->i4_next_start_ctr = 0;
2212
2.43k
                }
2213
91
                else if(
2214
91
                    i4_force_idr_check &&
2215
53
                    (ps_lap_struct->i4_force_idr_pos <= ps_lap_struct->i4_sub_gop_size))
2216
53
                {
2217
                    /*check force idr pos is 1st pic in sub-gop then don't add dummy pics*/
2218
53
                    if(ps_lap_struct->i4_force_idr_pos != 1)
2219
0
                    {
2220
0
                        WORD32 sub_gop_pos = ps_lap_struct->i4_force_idr_pos;
2221
0
                        while(sub_gop_pos <= ps_lap_struct->i4_sub_gop_size)
2222
0
                        {
2223
0
                            ps_lap_struct->i4_num_dummy_pic++;
2224
0
                            ihevce_lap_queue_input(ps_lap_struct, NULL, &i4_tree_num);
2225
0
                            sub_gop_pos++;
2226
0
                        }
2227
0
                        ps_lap_struct->i4_num_dummy_pic = 0;
2228
0
                    }
2229
53
                    ps_lap_struct->ai1_pic_type[0] =
2230
53
                        ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
2231
2232
53
                    ihevce_determine_next_sub_gop_state(ps_lap_struct);
2233
2234
53
                    ps_lap_struct->i4_next_start_ctr = 0;
2235
53
                }
2236
2237
2.52k
                if(/*ps_lap_struct->i4_init_delay_over &&*/ 0 !=
2238
2.52k
                   ps_lap_struct->i4_num_frm_type_decided)
2239
2.52k
                {
2240
2.52k
                    ihevce_assign_pic_type(
2241
2.52k
                        ps_lap_struct,
2242
2.52k
                        ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]);
2243
2244
2.52k
                    ps_lap_struct->i4_num_frm_type_decided--;
2245
2246
2.52k
                    if(NULL != ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx])
2247
2.52k
                    {
2248
                        /*special case of two consequetive idr at the start of encode or due to force idr*/
2249
2.52k
                        ps_lap_struct->i4_immediate_idr_case =
2250
2.52k
                            ps_lap_struct->i4_is_all_i_pic_in_seq;
2251
2.52k
                        if(ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]
2252
2.52k
                               ->s_lap_out.i4_pic_type == IV_IDR_FRAME)
2253
430
                        {
2254
430
                            ps_lap_struct->i4_immediate_idr_case = 1;
2255
430
                        }
2256
2.09k
                        else
2257
2.09k
                        {
2258
2.09k
                            WORD32 i4_prev_idx = ps_lap_struct->i4_buf_deq_idx > 0
2259
2.09k
                                                     ? ps_lap_struct->i4_buf_deq_idx - 1
2260
2.09k
                                                     : ps_lap_struct->i4_buf_deq_idx;
2261
                            /*field case of single IDR field followed by P*/
2262
2.09k
                            if(NULL != ps_lap_struct->aps_lap_inp_buf[i4_prev_idx] &&
2263
2.09k
                               i4_field_flag &&
2264
0
                               ps_lap_struct->aps_lap_inp_buf[i4_prev_idx]->s_lap_out.i4_pic_type ==
2265
0
                                   IV_IDR_FRAME &&
2266
0
                               !ps_lap_struct->i4_num_frm_type_decided)
2267
0
                            {
2268
0
                                ps_lap_struct->i4_immediate_idr_case = 1;
2269
0
                            }
2270
2.09k
                        }
2271
2.52k
                    }
2272
2273
                    /* Queue in the current input Buffer to LAP que */
2274
2.52k
                    ihevce_lap_queue_input(
2275
2.52k
                        ps_lap_struct,
2276
2.52k
                        ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx],
2277
2.52k
                        &i4_tree_num);
2278
2279
2.52k
                    ps_lap_struct->i4_next_start_ctr++;
2280
2.52k
                    ps_lap_struct->i4_buf_deq_idx++;
2281
2.52k
                    if(ps_lap_struct->i4_buf_deq_idx >= MAX_QUEUE_LENGTH)
2282
7
                        ps_lap_struct->i4_buf_deq_idx = 0;
2283
2.52k
                }
2284
2285
2.52k
                ps_lap_struct->i4_buf_enq_idx++;
2286
2.52k
                if(ps_lap_struct->i4_buf_enq_idx >= MAX_QUEUE_LENGTH)
2287
7
                    ps_lap_struct->i4_buf_enq_idx = 0;
2288
2.52k
            } /* end if for valid input buffer check*/
2289
2.72k
        }
2290
2291
        /* source pixel padding if width/height is not aligned to 8 pixel */
2292
2.72k
        if(ps_lap_inp_buf->s_input_buf.i4_inp_frm_data_valid_flag)
2293
2.52k
        {
2294
2.52k
            ihevce_src_params_t *ps_src_prms = &ps_lap_struct->s_static_cfg_params.s_src_prms;
2295
2.52k
            WORD32 i4_align_wd = ps_src_prms->i4_width;
2296
2.52k
            WORD32 i4_align_ht = ps_src_prms->i4_height;
2297
2.52k
            WORD32 min_cu_size =
2298
2.52k
                (1 << ps_lap_struct->s_static_cfg_params.s_config_prms.i4_min_log2_cu_size);
2299
2300
2.52k
            i4_align_wd += SET_CTB_ALIGN(ps_src_prms->i4_width, min_cu_size);
2301
2.52k
            i4_align_ht += SET_CTB_ALIGN(ps_src_prms->i4_height, min_cu_size);
2302
2303
2.52k
            ihevce_lap_pad_input_bufs(ps_lap_inp_buf, i4_align_wd, i4_align_ht);
2304
2.52k
        }
2305
2.72k
        {
2306
2.72k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_width = 0;
2307
2.72k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_height = 0;
2308
2.72k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_x_offset = 0;
2309
2.72k
            ps_lap_inp_buf->s_lap_out.s_logo_ctxt.logo_y_offset = 0;
2310
2.72k
        }
2311
2.72k
    }
2312
2313
2.84k
    if(ps_lap_struct->end_flag == 1)
2314
316
    {
2315
316
        ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_enq_idx] = ps_lap_inp_buf;
2316
2317
        /*to be filed*/
2318
316
        if(0 == ps_lap_struct->i4_num_frm_type_decided)
2319
214
        {
2320
214
            ps_lap_struct->ai1_pic_type[0] =
2321
214
                ps_lap_struct->ai1_pic_type[ps_lap_struct->i4_next_start_ctr];
2322
2323
214
            ihevce_determine_next_sub_gop_state(ps_lap_struct);
2324
2325
214
            ps_lap_struct->i4_next_start_ctr = 0;
2326
214
        }
2327
2328
316
        if(NULL != ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx])
2329
199
        {
2330
199
            ihevce_assign_pic_type(
2331
199
                ps_lap_struct, ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]);
2332
199
        }
2333
2334
316
        ps_lap_struct->i4_num_frm_type_decided--;
2335
2336
316
        if(NULL != ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx])
2337
199
        {
2338
            /*special case of two consequetive idr at the start of encode or due to force idr*/
2339
199
            ps_lap_struct->i4_immediate_idr_case = ps_lap_struct->i4_is_all_i_pic_in_seq;
2340
2341
199
            if(ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx]
2342
199
                   ->s_lap_out.i4_pic_type == IV_IDR_FRAME)
2343
16
            {
2344
16
                ps_lap_struct->i4_immediate_idr_case = 1;
2345
16
            }
2346
183
            else
2347
183
            {
2348
183
                WORD32 i4_prev_idx = ps_lap_struct->i4_buf_deq_idx > 0
2349
183
                                         ? ps_lap_struct->i4_buf_deq_idx - 1
2350
183
                                         : ps_lap_struct->i4_buf_deq_idx;
2351
                /*field case of single IDR field followed by P*/
2352
183
                if(NULL != ps_lap_struct->aps_lap_inp_buf[i4_prev_idx] && i4_field_flag &&
2353
0
                   ps_lap_struct->aps_lap_inp_buf[i4_prev_idx]->s_lap_out.i4_pic_type ==
2354
0
                       IV_IDR_FRAME &&
2355
0
                   !ps_lap_struct->i4_num_frm_type_decided)
2356
0
                {
2357
0
                    ps_lap_struct->i4_immediate_idr_case = 1;
2358
0
                }
2359
183
            }
2360
199
        }
2361
        /* Queue in the current input Buffer to LAP que */
2362
316
        ihevce_lap_queue_input(
2363
316
            ps_lap_struct,
2364
316
            ps_lap_struct->aps_lap_inp_buf[ps_lap_struct->i4_buf_deq_idx],
2365
316
            &i4_tree_num);
2366
316
        ps_lap_struct->i4_max_buf_in_enc_order =
2367
316
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf];
2368
316
        ps_lap_struct->i4_next_start_ctr++;
2369
316
        ps_lap_struct->i4_buf_deq_idx++;
2370
2371
316
        if(ps_lap_struct->i4_buf_deq_idx >= MAX_QUEUE_LENGTH)
2372
0
            ps_lap_struct->i4_buf_deq_idx = 0;
2373
2374
316
        ps_lap_struct->i4_buf_enq_idx++;
2375
316
        if(ps_lap_struct->i4_buf_enq_idx >= MAX_QUEUE_LENGTH)
2376
0
            ps_lap_struct->i4_buf_enq_idx = 0;
2377
316
    }
2378
2379
2.84k
    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
2.84k
    ps_lap_inp_buf = NULL;
2386
2387
2.84k
    if(NULL !=
2388
2.84k
       ps_lap_struct->api4_lap_out_buf[ps_lap_struct->i4_deq_lap_buf][ps_lap_struct->i4_lap_out_idx])
2389
2.72k
    {
2390
2.72k
        ps_lap_inp_buf =
2391
2.72k
            ps_lap_struct
2392
2.72k
                ->api4_lap_out_buf[ps_lap_struct->i4_deq_lap_buf][ps_lap_struct->i4_lap_out_idx];
2393
2.72k
        ps_lap_struct
2394
2.72k
            ->api4_lap_out_buf[ps_lap_struct->i4_deq_lap_buf][ps_lap_struct->i4_lap_out_idx] = NULL;
2395
2.72k
        if(!ps_lap_inp_buf->s_lap_out.i4_end_flag)
2396
2.52k
            ihevce_pre_rel_lapout_update(ps_lap_struct, ps_lap_inp_buf);
2397
2398
2.72k
        ps_lap_struct->i4_max_buf_in_enc_order =
2399
2.72k
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf];
2400
2.72k
    }
2401
2402
2.84k
    ps_lap_struct->i4_lap_out_idx++;
2403
2.84k
    if(ps_lap_struct->i4_lap_out_idx == ps_lap_struct->i4_max_buf_in_enc_order)
2404
2.72k
    {
2405
2.72k
        if(ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf])
2406
2.70k
        {
2407
2.70k
            ps_lap_struct->ai4_num_buffer[ps_lap_struct->i4_deq_lap_buf] = 0;
2408
2.70k
            ps_lap_struct->i4_deq_lap_buf++;
2409
2.70k
            ps_lap_struct->i4_deq_lap_buf &= (MAX_SUBGOP_IN_ENCODE_QUEUE - 1);
2410
2.70k
        }
2411
2412
2.72k
        ps_lap_struct->i4_lap_out_idx = 0;
2413
2.72k
    }
2414
2415
2.84k
    return (ps_lap_inp_buf);
2416
2.84k
}
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
398
{
2426
398
    WORD32 i4_lap_window_size = 1;
2427
398
    WORD32 gop_delay = 1 << ps_lap_stat_prms->i4_max_temporal_layers;
2428
2429
398
    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
398
    gop_delay += (i4_lap_window_size);
2435
398
    return gop_delay;
2436
398
}