Coverage Report

Created: 2025-10-13 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_plugin.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2018 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
/*!
21
******************************************************************************
22
* \file ihevce_plugin.c
23
*
24
* \brief
25
*    This file contains wrapper utilities to use hevc encoder library
26
*
27
* \date
28
*    15/04/2014
29
*
30
* \author
31
*    Ittiam
32
*
33
* List of Functions
34
*
35
*
36
******************************************************************************
37
*/
38
39
/*****************************************************************************/
40
/* File Includes                                                             */
41
/*****************************************************************************/
42
/* System include files */
43
#include <stdio.h>
44
#include <string.h>
45
#include <stdlib.h>
46
#include <assert.h>
47
#include <stdarg.h>
48
49
/* User include files */
50
#include "ihevc_typedefs.h"
51
#include "itt_video_api.h"
52
#include "ihevce_api.h"
53
54
#include "rc_cntrl_param.h"
55
#include "rc_frame_info_collector.h"
56
#include "rc_look_ahead_params.h"
57
58
#include "ihevc_defs.h"
59
#include "ihevc_macros.h"
60
#include "ihevc_debug.h"
61
#include "ihevc_structs.h"
62
#include "ihevc_platform_macros.h"
63
#include "ihevc_deblk.h"
64
#include "ihevc_itrans_recon.h"
65
#include "ihevc_chroma_itrans_recon.h"
66
#include "ihevc_chroma_intra_pred.h"
67
#include "ihevc_intra_pred.h"
68
#include "ihevc_inter_pred.h"
69
#include "ihevc_mem_fns.h"
70
#include "ihevc_padding.h"
71
#include "ihevc_weighted_pred.h"
72
#include "ihevc_sao.h"
73
#include "ihevc_resi_trans.h"
74
#include "ihevc_quant_iquant_ssd.h"
75
76
#include "ihevce_defs.h"
77
#include "ihevce_lap_enc_structs.h"
78
#include "ihevce_plugin.h"
79
#include "ihevce_plugin_priv.h"
80
#include "ihevce_hle_interface.h"
81
#include "ihevce_multi_thrd_structs.h"
82
#include "ihevce_me_common_defs.h"
83
#include "ihevce_error_codes.h"
84
#include "ihevce_error_checks.h"
85
#include "ihevce_function_selector.h"
86
#include "ihevce_enc_structs.h"
87
#include "ihevce_global_tables.h"
88
89
#include "cast_types.h"
90
#include "osal.h"
91
#include "osal_defaults.h"
92
93
/*****************************************************************************/
94
/* Constant Macros                                                           */
95
/*****************************************************************************/
96
7.01k
#define CREATE_TIME_ALLOCATION_INPUT 1
97
7.01k
#define CREATE_TIME_ALLOCATION_OUTPUT 0
98
99
#define MAX_NUM_FRM_IN_GOP 600
100
101
/*****************************************************************************/
102
/* Extern variables                                                          */
103
/*****************************************************************************/
104
105
/*****************************************************************************/
106
/* Function Definitions                                                      */
107
/*****************************************************************************/
108
109
/*!
110
******************************************************************************
111
* \if Function name : mem_mngr_alloc \endif
112
*
113
* \brief
114
*    Memory manager specific alloc function
115
*    it expects to reset the allocated memory and provide the zero initialised
116
*    memory whenever this function getting called
117
*
118
* \param[in] pv_handle : handle to memory manager
119
*                        (currently not required can be set to null)
120
* \param[in] ps_memtab : memory descriptor pointer
121
*
122
* \return
123
*    Memory pointer
124
*
125
* \author
126
*  Ittiam
127
*
128
*****************************************************************************
129
*/
130
void mem_mngr_alloc(void *pv_handle, ihevce_sys_api_t *ps_sys_api, iv_mem_rec_t *ps_memtab)
131
6.61M
{
132
6.61M
#ifndef X86_MINGW
133
6.61M
    WORD32 error, mem_alignment;
134
6.61M
#endif
135
136
6.61M
    (void)pv_handle;
137
138
#ifdef X86_MINGW
139
    ps_memtab->pv_base = _aligned_malloc(ps_memtab->i4_mem_size, ps_memtab->i4_mem_alignment);
140
#else
141
6.61M
    mem_alignment = ps_memtab->i4_mem_alignment;
142
6.61M
    mem_alignment = (mem_alignment >> 3) << 3;
143
6.61M
    if(mem_alignment == 0)
144
4.32M
    {
145
4.32M
        error = posix_memalign(&ps_memtab->pv_base, sizeof(void *), ps_memtab->i4_mem_size);
146
4.32M
    }
147
2.29M
    else
148
2.29M
    {
149
2.29M
        error = posix_memalign(&ps_memtab->pv_base, mem_alignment, ps_memtab->i4_mem_size);
150
2.29M
    }
151
6.61M
    if(error != 0)
152
0
    {
153
0
        ps_sys_api->ihevce_printf(ps_sys_api->pv_cb_handle, "posix_memalign error %d\n", error);
154
0
    }
155
6.61M
#endif
156
157
6.61M
    if(ps_memtab->pv_base == NULL)
158
0
    {
159
0
        ps_sys_api->ihevce_printf(
160
0
            ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Unable to allocate memory\n");
161
0
        ASSERT(0);
162
0
    }
163
6.61M
    else
164
6.61M
    {
165
6.61M
        memset(ps_memtab->pv_base, 0, ps_memtab->i4_mem_size);
166
6.61M
    }
167
6.61M
    return;
168
6.61M
}
169
170
/*!
171
******************************************************************************
172
* \if Function name : memory_alloc \endif
173
*
174
* \brief
175
*    common memory allocate function should be used across all threads
176
*    it expects to reset the allocated memory and return the zero initialised
177
*    memory pointer whenever this function getting called
178
*
179
* \param[in] pv_handle : handle to memory manager
180
*                        (currently not required can be set to null)
181
* \param[in] u4_size : size of memory required
182
*
183
* \return
184
*    Memory pointer
185
*
186
* \author
187
*  Ittiam
188
*
189
*****************************************************************************
190
*/
191
void *memory_alloc(void *pv_handle, UWORD32 u4_size)
192
55.9k
{
193
55.9k
    (void)pv_handle;
194
55.9k
    void *pv_buf = malloc(u4_size);
195
55.9k
    if(pv_buf)
196
55.9k
    {
197
55.9k
        memset(pv_buf, 0, u4_size);
198
55.9k
    }
199
55.9k
    return (pv_buf);
200
55.9k
}
201
202
/*!
203
******************************************************************************
204
* \if Function name : mem_mngr_free \endif
205
*
206
* \brief
207
*    Memory manager specific free function
208
*
209
* \param[in] pv_handle : handle to memory manager
210
*                        (currently not required can be set to null)
211
* \param[in] ps_memtab : memory descriptor pointer
212
*
213
* \return
214
*    Memory pointer
215
*
216
* \author
217
*  Ittiam
218
*
219
*****************************************************************************
220
*/
221
void mem_mngr_free(void *pv_handle, iv_mem_rec_t *ps_memtab)
222
6.61M
{
223
6.61M
    (void)pv_handle;
224
#ifdef X86_MINGW
225
    _aligned_free(ps_memtab->pv_base);
226
#else
227
6.61M
    free(ps_memtab->pv_base);
228
6.61M
#endif
229
6.61M
    return;
230
6.61M
}
231
232
/*!
233
******************************************************************************
234
* \if Function name : memory_free \endif
235
*
236
* \brief
237
*    common memory free function should be used across all threads
238
*
239
* \param[in] pv_handle : handle to memory manager
240
*                        (currently not required can be set to null)
241
* \param[in] pv_mem : memory to be freed
242
*
243
* \return
244
*    Memory pointer
245
*
246
* \author
247
*  Ittiam
248
*
249
*****************************************************************************
250
*/
251
void memory_free(void *pv_handle, void *pv_mem)
252
62.9k
{
253
62.9k
    (void)pv_handle;
254
62.9k
    free(pv_mem);
255
62.9k
    return;
256
62.9k
}
257
258
/*!
259
******************************************************************************
260
* \if Function name : ihevce_set_def_params \endif
261
*
262
* \brief
263
*    Set default values
264
*
265
* \param[in] Static params pointer
266
*
267
* \return
268
*    status
269
*
270
* \author
271
*  Ittiam
272
*
273
*****************************************************************************
274
*/
275
IHEVCE_PLUGIN_STATUS_T ihevce_set_def_params(ihevce_static_cfg_params_t *ps_params)
276
7.01k
{
277
7.01k
    WORD32 i, j;
278
    /* sanity checks */
279
7.01k
    if(NULL == ps_params)
280
0
        return (IHEVCE_EFAIL);
281
282
7.01k
    memset(ps_params, 0, sizeof(*ps_params));
283
284
    /* initialsie all the parameters to default values */
285
7.01k
    ps_params->i4_size = sizeof(ihevce_static_cfg_params_t);
286
7.01k
    ps_params->i4_save_recon = 0;
287
7.01k
    ps_params->i4_log_dump_level = 0;
288
7.01k
    ps_params->i4_enable_logo = 0;
289
7.01k
    ps_params->i4_enable_csv_dump = 0;
290
291
    /* Control to free the entropy output buffers   */
292
    /* 1  for non_blocking mode */
293
    /* and 0 for blocking mode */
294
7.01k
    ps_params->i4_outbuf_buf_free_control = 1;
295
296
    /* coding tools parameters */
297
7.01k
    ps_params->s_coding_tools_prms.i4_size = sizeof(ihevce_coding_params_t);
298
7.01k
    ps_params->s_coding_tools_prms.i4_cropping_mode = 1;
299
7.01k
    ps_params->s_coding_tools_prms.i4_deblocking_type = 0;
300
7.01k
    ps_params->s_coding_tools_prms.i4_enable_entropy_sync = 0;
301
    // New IDR/CDR Params
302
7.01k
    ps_params->s_coding_tools_prms.i4_max_closed_gop_period = 0;
303
7.01k
    ps_params->s_coding_tools_prms.i4_min_closed_gop_period = 0;
304
7.01k
    ps_params->s_coding_tools_prms.i4_max_cra_open_gop_period = 60;
305
7.01k
    ps_params->s_coding_tools_prms.i4_max_i_open_gop_period = 0;
306
7.01k
    ps_params->s_coding_tools_prms.i4_max_reference_frames = -1;
307
7.01k
    ps_params->s_coding_tools_prms.i4_max_temporal_layers = 0;
308
7.01k
    ps_params->s_coding_tools_prms.i4_slice_type = 0;
309
7.01k
    ps_params->s_coding_tools_prms.i4_use_default_sc_mtx = 0;
310
7.01k
    ps_params->s_coding_tools_prms.i4_weighted_pred_enable = 0;
311
7.01k
    ps_params->s_coding_tools_prms.i4_vqet = 0;
312
313
7.01k
    ps_params->e_arch_type = ARCH_NA;
314
315
    /* config parameters */
316
7.01k
    ps_params->s_config_prms.i4_size = sizeof(ihevce_config_prms_t);
317
7.01k
    ps_params->s_config_prms.i4_cu_level_rc = 1;
318
7.01k
    ps_params->s_config_prms.i4_init_vbv_fullness = 0;
319
7.01k
    ps_params->s_config_prms.i4_max_frame_qp = 51;
320
7.01k
    ps_params->s_config_prms.i4_max_log2_cu_size = 6;
321
7.01k
    ps_params->s_config_prms.i4_max_log2_tu_size = 5;
322
7.01k
    ps_params->s_config_prms.i4_max_search_range_horz = 512;
323
7.01k
    ps_params->s_config_prms.i4_max_search_range_vert = 256;
324
7.01k
    ps_params->s_config_prms.i4_max_tr_tree_depth_I = 1;
325
7.01k
    ps_params->s_config_prms.i4_max_tr_tree_depth_nI = 3;
326
7.01k
    ps_params->s_config_prms.i4_min_frame_qp = 1;
327
7.01k
    ps_params->s_config_prms.i4_min_log2_cu_size = 3;
328
7.01k
    ps_params->s_config_prms.i4_min_log2_tu_size = 2;
329
7.01k
    ps_params->s_config_prms.i4_num_frms_to_encode = -1;
330
7.01k
    ps_params->s_config_prms.i4_rate_control_mode = 2;
331
7.01k
    ps_params->s_config_prms.i4_stuffing_enable = 0;
332
7.01k
    ps_params->s_config_prms.i4_vbr_max_peak_rate_dur = 2000;
333
334
    /* LAP parameters */
335
7.01k
    ps_params->s_lap_prms.i4_size = sizeof(ihevce_lap_params_t);
336
7.01k
    ps_params->s_lap_prms.i4_deinterlacer_enable = 0;
337
7.01k
    ps_params->s_lap_prms.i4_denoise_enable = 0;
338
7.01k
    ps_params->s_lap_prms.i4_enable_wts_ofsts = 1;
339
7.01k
    ps_params->s_lap_prms.i4_rc_look_ahead_pics = 0;
340
341
    /* Multi Thread parameters */
342
7.01k
    ps_params->s_multi_thrd_prms.i4_size = sizeof(ihevce_static_multi_thread_params_t);
343
7.01k
    ps_params->s_multi_thrd_prms.i4_max_num_cores = 1;
344
7.01k
    ps_params->s_multi_thrd_prms.i4_memory_alloc_ctrl_flag = 0;
345
7.01k
    ps_params->s_multi_thrd_prms.i4_num_proc_groups = 1;
346
7.01k
    ps_params->s_multi_thrd_prms.ai4_num_cores_per_grp[0] = -1;
347
7.01k
    ps_params->s_multi_thrd_prms.i4_use_thrd_affinity = -1;  //0;
348
7.01k
    memset(&ps_params->s_multi_thrd_prms.au8_core_aff_mask[0], 0, sizeof(ULWORD64) * MAX_NUM_CORES);
349
350
    /* Output Streams parameters */
351
7.01k
    ps_params->s_out_strm_prms.i4_size = sizeof(ihevce_out_strm_params_t);
352
7.01k
    ps_params->s_out_strm_prms.i4_aud_enable_flags = 0;
353
7.01k
    ps_params->s_out_strm_prms.i4_eos_enable_flags = 0;
354
7.01k
    ps_params->s_out_strm_prms.i4_codec_profile = 1;
355
7.01k
    ps_params->s_out_strm_prms.i4_codec_tier = 0;
356
7.01k
    ps_params->s_out_strm_prms.i4_codec_type = 0;
357
7.01k
#ifndef DISABLE_SEI
358
7.01k
    ps_params->s_out_strm_prms.i4_sei_buffer_period_flags = 0;
359
7.01k
    ps_params->s_out_strm_prms.i4_sei_enable_flag = 0;
360
7.01k
    ps_params->s_out_strm_prms.i4_sei_payload_enable_flag = 0;
361
7.01k
    ps_params->s_out_strm_prms.i4_sei_pic_timing_flags = 0;
362
7.01k
    ps_params->s_out_strm_prms.i4_sei_cll_enable = 0;
363
7.01k
    ps_params->s_out_strm_prms.u2_sei_avg_cll = 0;
364
7.01k
    ps_params->s_out_strm_prms.u2_sei_max_cll = 0;
365
7.01k
    ps_params->s_out_strm_prms.i4_sei_recovery_point_flags = 0;
366
7.01k
    ps_params->s_out_strm_prms.i4_sei_mastering_disp_colour_vol_flags = 0;
367
7.01k
    ps_params->s_out_strm_prms.i4_decoded_pic_hash_sei_flag = 0;
368
7.01k
#endif
369
7.01k
    ps_params->s_out_strm_prms.i4_sps_at_cdr_enable = 1;
370
7.01k
    ps_params->s_out_strm_prms.i4_vui_enable = 0;
371
    /*Set the interoperability flag to 0*/
372
7.01k
    ps_params->s_out_strm_prms.i4_interop_flags = 0;
373
374
    /* Source parameters */
375
7.01k
    ps_params->s_src_prms.i4_size = sizeof(ihevce_src_params_t);
376
7.01k
    ps_params->s_src_prms.inp_chr_format = 1;
377
7.01k
    ps_params->s_src_prms.i4_chr_format = 11;
378
7.01k
    ps_params->s_src_prms.i4_field_pic = 0;
379
7.01k
    ps_params->s_src_prms.i4_frm_rate_denom = 1000;
380
7.01k
    ps_params->s_src_prms.i4_frm_rate_num = 30000;
381
7.01k
    ps_params->s_src_prms.i4_height = 0;  //1080;
382
7.01k
    ps_params->s_src_prms.i4_input_bit_depth = 8;
383
7.01k
    ps_params->s_src_prms.i4_topfield_first = 1;
384
7.01k
    ps_params->s_src_prms.i4_width = 0;  //1920;
385
7.01k
    ps_params->s_src_prms.i4_orig_width = 0;
386
7.01k
    ps_params->s_src_prms.i4_orig_height = 0;
387
388
    /* Target layer parameters */
389
7.01k
    ps_params->s_tgt_lyr_prms.i4_size = sizeof(ihevce_tgt_layer_params_t);
390
7.01k
    ps_params->s_tgt_lyr_prms.i4_enable_temporal_scalability = 0;
391
7.01k
    ps_params->s_tgt_lyr_prms.i4_internal_bit_depth = 8;
392
7.01k
    ps_params->s_tgt_lyr_prms.i4_mbr_quality_setting = IHEVCE_MBR_HIGH_QUALITY;
393
7.01k
    ps_params->s_tgt_lyr_prms.i4_multi_res_layer_reuse = 0;
394
7.01k
    ps_params->s_tgt_lyr_prms.i4_num_res_layers = 1;
395
7.01k
    ps_params->s_tgt_lyr_prms.i4_mres_single_out = 0;
396
7.01k
    ps_params->s_tgt_lyr_prms.i4_start_res_id = 0;
397
7.01k
    ps_params->s_tgt_lyr_prms.pf_scale_chroma = NULL;
398
7.01k
    ps_params->s_tgt_lyr_prms.pf_scale_luma = NULL;
399
7.01k
    ps_params->s_tgt_lyr_prms.pv_scaler_handle = NULL;
400
401
    /* target parameters */
402
14.0k
    for(i = 0; i < IHEVCE_MAX_NUM_RESOLUTIONS; i++)
403
7.01k
    {
404
7.01k
        ps_params->s_tgt_lyr_prms.as_tgt_params[i].i4_size = sizeof(ihevce_tgt_params_t);
405
14.0k
        for(j = 0; j < IHEVCE_MAX_NUM_BITRATES; j++)
406
7.01k
        {
407
7.01k
            ps_params->s_tgt_lyr_prms.as_tgt_params[i].ai4_frame_qp[j] = 32;
408
7.01k
            ps_params->s_tgt_lyr_prms.as_tgt_params[i].ai4_tgt_bitrate[j] = 5000000;
409
7.01k
            ps_params->s_tgt_lyr_prms.as_tgt_params[i].ai4_peak_bitrate[j] = 10000000;
410
7.01k
            ps_params->s_tgt_lyr_prms.as_tgt_params[i].ai4_max_vbv_buffer_size[j] = -1;
411
7.01k
        }
412
7.01k
        ps_params->s_tgt_lyr_prms.as_tgt_params[i].i4_codec_level = 156;
413
7.01k
        ps_params->s_tgt_lyr_prms.as_tgt_params[i].i4_frm_rate_scale_factor = 1;
414
7.01k
        ps_params->s_tgt_lyr_prms.as_tgt_params[i].i4_height = 0;
415
7.01k
        ps_params->s_tgt_lyr_prms.as_tgt_params[i].i4_num_bitrate_instances = 1;
416
7.01k
        ps_params->s_tgt_lyr_prms.as_tgt_params[i].i4_quality_preset = IHEVCE_QUALITY_P5;
417
7.01k
        ps_params->s_tgt_lyr_prms.as_tgt_params[i].i4_width = 0;
418
7.01k
    }
419
420
    /* SEI VUI parameters */
421
7.01k
    ps_params->s_vui_sei_prms.u1_aspect_ratio_info_present_flag = 0;
422
7.01k
    ps_params->s_vui_sei_prms.au1_aspect_ratio_idc[0] = 255;
423
7.01k
    ps_params->s_vui_sei_prms.au2_sar_width[0] = 4;
424
7.01k
    ps_params->s_vui_sei_prms.au2_sar_height[0] = 3;
425
7.01k
    ps_params->s_vui_sei_prms.u1_overscan_info_present_flag = 0;
426
7.01k
    ps_params->s_vui_sei_prms.u1_overscan_appropriate_flag = 0;
427
7.01k
    ps_params->s_vui_sei_prms.u1_video_signal_type_present_flag = 1;
428
7.01k
    ps_params->s_vui_sei_prms.u1_video_format = 5;
429
7.01k
    ps_params->s_vui_sei_prms.u1_video_full_range_flag = 1;
430
7.01k
    ps_params->s_vui_sei_prms.u1_colour_description_present_flag = 0;
431
7.01k
    ps_params->s_vui_sei_prms.u1_colour_primaries = 2;
432
7.01k
    ps_params->s_vui_sei_prms.u1_transfer_characteristics = 2;
433
7.01k
    ps_params->s_vui_sei_prms.u1_matrix_coefficients = 2;
434
7.01k
    ps_params->s_vui_sei_prms.u1_chroma_loc_info_present_flag = 0;
435
7.01k
    ps_params->s_vui_sei_prms.u1_chroma_sample_loc_type_top_field = 0;
436
7.01k
    ps_params->s_vui_sei_prms.u1_chroma_sample_loc_type_bottom_field = 0;
437
7.01k
    ps_params->s_vui_sei_prms.u1_vui_hrd_parameters_present_flag = 0;
438
7.01k
    ps_params->s_vui_sei_prms.u1_timing_info_present_flag = 0;
439
7.01k
    ps_params->s_vui_sei_prms.u1_nal_hrd_parameters_present_flag = 0;
440
441
    /* Setting sysAPIs to NULL */
442
7.01k
    memset(&ps_params->s_sys_api, 0, sizeof(ihevce_sys_api_t));
443
444
    /* Multi pass parameters */
445
7.01k
    memset(&ps_params->s_pass_prms, 0, sizeof(ihevce_pass_prms_t));
446
7.01k
    ps_params->s_pass_prms.i4_size = sizeof(ihevce_pass_prms_t);
447
448
    /* Tile parameters */
449
7.01k
    ps_params->s_app_tile_params.i4_size = sizeof(ihevce_app_tile_params_t);
450
7.01k
    ps_params->s_app_tile_params.i4_tiles_enabled_flag = 0;
451
7.01k
    ps_params->s_app_tile_params.i4_uniform_spacing_flag = 1;
452
7.01k
    ps_params->s_app_tile_params.i4_num_tile_cols = 1;
453
7.01k
    ps_params->s_app_tile_params.i4_num_tile_rows = 1;
454
455
7.01k
    ps_params->s_slice_params.i4_slice_segment_mode = 0;
456
7.01k
    ps_params->s_slice_params.i4_slice_segment_argument = 1300;
457
458
7.01k
    return (IHEVCE_EOK);
459
7.01k
}
460
461
/*!
462
******************************************************************************
463
* \if Function name : ihevce_cmds_error_report \endif
464
*
465
* \brief
466
*    Call back from encoder to report errors
467
*
468
* \param[in]    pv_error_handling_cb_handle
469
* \param[in]    i4_error_code
470
* \param[in]    i4_cmd_type
471
* \param[in]    i4_id
472
*
473
* \return
474
*    None
475
*
476
* \author
477
*  Ittiam
478
*
479
*****************************************************************************
480
*/
481
IV_API_CALL_STATUS_T ihevce_cmds_error_report(
482
    void *pv_cb_handle, WORD32 i4_error_code, WORD32 i4_cmd_type, WORD32 i4_buf_id)
483
0
{
484
    /*local variables*/
485
0
    plugin_ctxt_t *plugin_ctxt = (plugin_ctxt_t *)pv_cb_handle;
486
0
    ihevce_static_cfg_params_t *ps_static_cfg_params =
487
0
        ((ihevce_hle_ctxt_t *)plugin_ctxt->pv_hle_interface_ctxt)->ps_static_cfg_prms;
488
489
0
    if(i4_cmd_type == 0)
490
0
        ps_static_cfg_params->s_sys_api.ihevce_printf(
491
0
            ps_static_cfg_params->s_sys_api.pv_cb_handle,
492
0
            "PLUGIN ERROR: Asynchronous Buffer Error %d in Buffer Id %d",
493
0
            i4_error_code,
494
0
            i4_buf_id);
495
0
    else
496
0
        ps_static_cfg_params->s_sys_api.ihevce_printf(
497
0
            ps_static_cfg_params->s_sys_api.pv_cb_handle,
498
0
            "PLUGIN ERROR: Synchronous Buffer Error %d in Buffer Id %d",
499
0
            i4_error_code,
500
0
            i4_buf_id);
501
502
0
    return (IV_SUCCESS);
503
0
}
504
505
/*!
506
******************************************************************************
507
* \if Function name : ihevce_strm_fill_done \endif
508
*
509
* \brief
510
*    Call back from encoder when Bitstream is ready to consume
511
*
512
* \param[in]
513
* \param[in]
514
* \param[in]
515
*
516
* \return
517
*    None
518
*
519
* \author
520
*  Ittiam
521
*
522
*****************************************************************************
523
*/
524
IV_API_CALL_STATUS_T
525
    ihevce_strm_fill_done(void *pv_ctxt, void *pv_curr_out, WORD32 i4_br_id, WORD32 i4_res_id)
526
115k
{
527
    /* local variables */
528
115k
    plugin_ctxt_t *ps_ctxt = (plugin_ctxt_t *)pv_ctxt;
529
115k
    app_ctxt_t *ps_app_ctxt = &ps_ctxt->s_app_ctxt;
530
115k
    out_strm_prms_t *ps_out_strm_prms = &ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id];
531
115k
    void *pv_app_out_strm_buf_mutex_hdl = ps_out_strm_prms->pv_app_out_strm_buf_mutex_hdl;
532
115k
    void *pv_app_out_strm_buf_cond_var_hdl = ps_out_strm_prms->pv_app_out_strm_buf_cond_var_hdl;
533
115k
    iv_output_data_buffs_t *ps_curr_out = (iv_output_data_buffs_t *)pv_curr_out;
534
115k
    WORD32 end_flag = ps_curr_out->i4_end_flag;
535
115k
    WORD32 osal_result;
536
537
    /* ------  output dump stream  -- */
538
115k
    if((WORD32)IV_FAIL != ps_curr_out->i4_process_ret_sts)
539
114k
    {
540
114k
        if(0 != ps_curr_out->i4_bytes_generated)
541
107k
        {
542
            /* accumulate the total bits generated */
543
107k
            (ps_out_strm_prms->u8_total_bits) += ps_curr_out->i4_bytes_generated * 8;
544
107k
            (ps_out_strm_prms->u4_num_frms_enc)++;
545
107k
        }
546
114k
    }
547
548
    /****** Lock the critical section ******/
549
115k
    osal_result = osal_mutex_lock(pv_app_out_strm_buf_mutex_hdl);
550
115k
    if(OSAL_SUCCESS != osal_result)
551
0
        return (IV_FAIL);
552
553
    /* Update the end flag to communicate with the o/p thread */
554
115k
    ps_app_ctxt->ai4_out_strm_end_flag[i4_res_id][i4_br_id] = end_flag;
555
556
    /* set the produced status of the buffer */
557
115k
    {
558
115k
        WORD32 idx = ps_curr_out->i4_cb_buf_id;
559
560
115k
        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][idx].i4_timestamp_low =
561
115k
            ps_curr_out->i4_out_timestamp_low;
562
115k
        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][idx].i4_timestamp_high =
563
115k
            ps_curr_out->i4_out_timestamp_high;
564
115k
        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][idx].i4_bytes_gen =
565
115k
            ps_curr_out->i4_bytes_generated;
566
115k
        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][idx].i4_is_key_frame = 0;
567
115k
        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][idx].i4_end_flag = end_flag;
568
569
115k
        if((IV_IDR_FRAME == ps_curr_out->i4_encoded_frame_type) ||
570
95.4k
           (IV_I_FRAME == ps_curr_out->i4_encoded_frame_type))
571
31.3k
        {
572
31.3k
            ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][idx].i4_is_key_frame = 1;
573
31.3k
        }
574
575
        /* set the buffer as produced */
576
115k
        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][idx].i4_is_prod = 1;
577
115k
    }
578
579
    /****** Wake ******/
580
115k
    osal_cond_var_signal(pv_app_out_strm_buf_cond_var_hdl);
581
582
    /****** Unlock the critical section ******/
583
115k
    osal_result = osal_mutex_unlock(pv_app_out_strm_buf_mutex_hdl);
584
115k
    if(OSAL_SUCCESS != osal_result)
585
0
        return (IV_FAIL);
586
587
115k
    return (IV_SUCCESS);
588
115k
}
589
590
/*!
591
******************************************************************************
592
* \if Function name : ihevce_plugin_init \endif
593
*
594
* \brief
595
*    Initialises the encoder context and threads
596
*
597
* \param[in] Static params pointer
598
*
599
* \return
600
*    status
601
*
602
* \author
603
*  Ittiam
604
*
605
*****************************************************************************
606
*/
607
IHEVCE_PLUGIN_STATUS_T ihevce_init(ihevce_static_cfg_params_t *ps_params, void **ppv_ihevce_hdl)
608
7.01k
{
609
    /* local variables */
610
7.01k
    plugin_ctxt_t *ps_ctxt;
611
7.01k
    app_ctxt_t *ps_app_ctxt;
612
7.01k
    ihevce_hle_ctxt_t *ps_interface_ctxt;
613
7.01k
    ihevce_sys_api_t *ps_sys_api;
614
7.01k
    osal_cb_funcs_t s_cb_funcs;
615
7.01k
    WORD32 status = 0;
616
617
    /* sanity checks */
618
7.01k
    if(NULL == ps_params)
619
0
        return (IHEVCE_EFAIL);
620
621
7.01k
    if(NULL == ppv_ihevce_hdl)
622
0
        return (IHEVCE_EFAIL);
623
624
    /* set the handle to null by default */
625
7.01k
    *ppv_ihevce_hdl = NULL;
626
627
    /* Initiallizing system apis */
628
7.01k
    ps_sys_api = &ps_params->s_sys_api;
629
7.01k
    ihevce_init_sys_api(NULL, ps_sys_api);
630
631
    /* --------------------------------------------------------------------- */
632
    /*                   Query and print Encoder version                     */
633
    /* --------------------------------------------------------------------- */
634
7.01k
    ps_sys_api->ihevce_printf(
635
7.01k
        ps_sys_api->pv_cb_handle, "Encoder version %s\n\n", ihevce_get_encoder_version());
636
637
    /* --------------------------------------------------------------------- */
638
    /*                    Plugin Handle create                               */
639
    /* --------------------------------------------------------------------- */
640
7.01k
    ps_ctxt = (plugin_ctxt_t *)memory_alloc(NULL, sizeof(plugin_ctxt_t));
641
7.01k
    if(NULL == ps_ctxt)
642
0
    {
643
0
        ps_sys_api->ihevce_printf(
644
0
            ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in Plugin initialization\n");
645
0
        return (IHEVCE_EFAIL);
646
0
    }
647
648
    /* initialise memory call backs */
649
7.01k
    ps_ctxt->ihevce_mem_alloc = memory_alloc;
650
7.01k
    ps_ctxt->ihevce_mem_free = memory_free;
651
652
7.01k
    ps_ctxt->u8_num_frames_encoded = 0;
653
654
7.01k
    if((0 == ps_params->i4_res_id) && (0 == ps_params->i4_br_id))
655
7.01k
    {
656
        /* --------------------------------------------------------------------- */
657
        /*                      OSAL Handle create                               */
658
        /* --------------------------------------------------------------------- */
659
7.01k
        ps_ctxt->pv_osal_handle = memory_alloc(NULL, OSAL_HANDLE_SIZE);
660
661
        /* Initialize OSAL call back functions */
662
7.01k
        s_cb_funcs.mmr_handle = NULL;
663
7.01k
        s_cb_funcs.osal_alloc = memory_alloc;
664
7.01k
        s_cb_funcs.osal_free = memory_free;
665
666
7.01k
        status = osal_init(ps_ctxt->pv_osal_handle);
667
7.01k
        if(OSAL_SUCCESS != status)
668
0
        {
669
0
            ps_sys_api->ihevce_printf(
670
0
                ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in OSAL initialization\n");
671
0
            return (IHEVCE_EFAIL);
672
0
        }
673
674
7.01k
        status = osal_register_callbacks(ps_ctxt->pv_osal_handle, &s_cb_funcs);
675
7.01k
        if(OSAL_SUCCESS != status)
676
0
        {
677
0
            ps_sys_api->ihevce_printf(
678
0
                ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in OSAL call back registration\n");
679
0
            return (IHEVCE_EFAIL);
680
0
        }
681
682
        /* --------------------------------------------------------------------- */
683
        /*                      Thread affinity  Initialization                  */
684
        /* --------------------------------------------------------------------- */
685
7.01k
        if(ps_params->s_multi_thrd_prms.i4_use_thrd_affinity)
686
7.01k
        {
687
7.01k
            WORD32 i4_ctr;
688
689
            /* loop over all the cores */
690
14.0k
            for(i4_ctr = 0; i4_ctr < ps_params->s_multi_thrd_prms.i4_max_num_cores; i4_ctr++)
691
7.01k
            {
692
                /* All cores are logical cores  */
693
7.01k
                ps_params->s_multi_thrd_prms.au8_core_aff_mask[i4_ctr] = ((ULWORD64)1 << i4_ctr);
694
7.01k
            }
695
7.01k
        }
696
697
        /* --------------------------------------------------------------------- */
698
        /*             Context Initialization                                    */
699
        /* --------------------------------------------------------------------- */
700
7.01k
        ps_app_ctxt = &ps_ctxt->s_app_ctxt;
701
702
7.01k
        ps_ctxt->ps_static_cfg_prms = (ihevce_static_cfg_params_t *)ps_ctxt->ihevce_mem_alloc(
703
7.01k
            NULL, sizeof(ihevce_static_cfg_params_t));
704
7.01k
        if(NULL == ps_ctxt->ps_static_cfg_prms)
705
0
        {
706
0
            ps_sys_api->ihevce_printf(
707
0
                ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in Plugin memory initialization\n");
708
0
            return (IHEVCE_EFAIL);
709
0
        }
710
711
7.01k
        ps_params->apF_csv_file[0][0] = NULL;
712
713
        /* set the memory manager handle to NULL */
714
7.01k
        ps_app_ctxt->pv_mem_mngr_handle = NULL;
715
716
        /* --------------------------------------------------------------------- */
717
        /*            Back up the static params passed by caller                 */
718
        /* --------------------------------------------------------------------- */
719
7.01k
        memcpy(ps_ctxt->ps_static_cfg_prms, ps_params, sizeof(ihevce_static_cfg_params_t));
720
721
7.01k
        ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_orig_width =
722
7.01k
            ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_width;
723
7.01k
        if(HEVCE_MIN_WIDTH > ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_width)
724
4.78k
        {
725
4.78k
            ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_width = HEVCE_MIN_WIDTH;
726
4.78k
        }
727
728
7.01k
        ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_orig_height =
729
7.01k
            ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_height;
730
7.01k
        if(HEVCE_MIN_HEIGHT > ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_height)
731
5.30k
        {
732
5.30k
            ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_height = HEVCE_MIN_HEIGHT;
733
5.30k
        }
734
735
        /* setting tgt width and height same as src width and height */
736
7.01k
        ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[0].i4_width =
737
7.01k
            ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_width;
738
7.01k
        ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[0].i4_height =
739
7.01k
            ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_height;
740
741
        /* setting key frame interval */
742
7.01k
        ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period =
743
7.01k
            MIN(MAX_NUM_FRM_IN_GOP,
744
7.01k
                ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_closed_gop_period);
745
7.01k
        ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_cra_open_gop_period =
746
7.01k
            MIN(MAX_NUM_FRM_IN_GOP,
747
7.01k
                ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_cra_open_gop_period);
748
7.01k
        ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_i_open_gop_period =
749
7.01k
            MIN(MAX_NUM_FRM_IN_GOP,
750
7.01k
                ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_i_open_gop_period);
751
752
        /* entropy sync is disabled if there is only one CTB row */
753
7.01k
        if(ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_height <=
754
7.01k
            (1 << ps_ctxt->ps_static_cfg_prms->s_config_prms.i4_max_log2_cu_size))
755
5.33k
        {
756
5.33k
            ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_enable_entropy_sync = 0;
757
5.33k
        }
758
759
        /* --------------------------------------------------------------------- */
760
        /*            High Level Encoder context init                            */
761
        /* --------------------------------------------------------------------- */
762
7.01k
        ps_interface_ctxt =
763
7.01k
            (ihevce_hle_ctxt_t *)ps_ctxt->ihevce_mem_alloc(NULL, sizeof(ihevce_hle_ctxt_t));
764
7.01k
        if(NULL == ps_interface_ctxt)
765
0
        {
766
0
            ps_sys_api->ihevce_printf(
767
0
                ps_sys_api->pv_cb_handle,
768
0
                "IHEVCE ERROR: Error in Plugin HLE memory initialization\n");
769
0
            return (IHEVCE_EFAIL);
770
0
        }
771
7.01k
        ps_interface_ctxt->i4_size = sizeof(ihevce_hle_ctxt_t);
772
773
7.01k
        ps_ctxt->pv_hle_interface_ctxt = ps_interface_ctxt;
774
775
        /* store the static config parameters pointer */
776
7.01k
        ps_interface_ctxt->ps_static_cfg_prms = ps_ctxt->ps_static_cfg_prms;
777
778
        /* initialise the interface strucure parameters */
779
7.01k
        ps_interface_ctxt->pv_inp_cb_handle = (void *)ps_ctxt;
780
7.01k
        ps_interface_ctxt->pv_out_cb_handle = (void *)ps_ctxt;
781
7.01k
        ps_interface_ctxt->pv_recon_cb_handle = (void *)ps_ctxt;
782
783
7.01k
        ps_interface_ctxt->pv_osal_handle = ps_ctxt->pv_osal_handle;
784
7.01k
        ps_interface_ctxt->ihevce_mem_alloc = mem_mngr_alloc;
785
7.01k
        ps_interface_ctxt->ihevce_mem_free = mem_mngr_free;
786
7.01k
        ps_interface_ctxt->i4_hle_init_done = 0;
787
7.01k
        ps_interface_ctxt->pv_mem_mgr_hdl = ps_app_ctxt->pv_mem_mngr_handle;
788
789
        /* reigter the callbacks */
790
7.01k
        ps_interface_ctxt->ihevce_output_strm_fill_done = ihevce_strm_fill_done;
791
7.01k
        ps_interface_ctxt->ihevce_output_recon_fill_done = NULL;
792
7.01k
        ps_interface_ctxt->ihevce_set_free_input_buff = NULL;
793
794
        /*Added for run time or create time creation*/
795
7.01k
        ps_interface_ctxt->i4_create_time_input_allocation = (WORD32)CREATE_TIME_ALLOCATION_INPUT;
796
7.01k
        ps_interface_ctxt->i4_create_time_output_allocation = (WORD32)CREATE_TIME_ALLOCATION_OUTPUT;
797
798
7.01k
        ps_interface_ctxt->ihevce_cmds_error_report = ihevce_cmds_error_report;
799
7.01k
        ps_interface_ctxt->pv_cmd_err_cb_handle = (void *)ps_ctxt;
800
801
        /* --------------------------------------------------------------------- */
802
        /*           High Level Encoder Instance Creation                        */
803
        /* --------------------------------------------------------------------- */
804
7.01k
        status = ihevce_hle_interface_create(ps_interface_ctxt);
805
7.01k
        if((WORD32)IV_FAIL == status)
806
36
        {
807
36
            ihevce_hle_interface_delete(ps_interface_ctxt);
808
809
36
            memory_free(NULL, ps_interface_ctxt);
810
811
            /* free static config memory */
812
36
            ps_ctxt->ihevce_mem_free(NULL, ps_ctxt->ps_static_cfg_prms);
813
814
            /* free osal handle */
815
36
            memory_free(NULL, ps_ctxt->pv_osal_handle);
816
817
            /* free plugin ctxt memory */
818
36
            memory_free(NULL, ps_ctxt);
819
820
36
            ps_sys_api->ihevce_printf(
821
36
                ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in Plugin HLE create failed\n");
822
36
            return (IHEVCE_EFAIL);
823
36
        }
824
825
        /* --------------------------------------------------------------------- */
826
        /*            Input Output and Command buffer allocation                 */
827
        /* --------------------------------------------------------------------- */
828
6.98k
        {
829
6.98k
            WORD32 ctr;
830
6.98k
            WORD32 buf_size;
831
6.98k
            UWORD8 *pu1_tmp_buf;
832
6.98k
            WORD32 i4_res_id;
833
6.98k
            WORD32 i4_br_id;
834
6.98k
            WORD32 i4_num_resolutions;
835
6.98k
            WORD32 ai4_num_bitrate_instances[IHEVCE_MAX_NUM_RESOLUTIONS] = { 1 };
836
6.98k
            iv_input_bufs_req_t s_input_bufs_req;
837
6.98k
            iv_res_layer_output_bufs_req_t s_res_layer_output_bufs_req;
838
6.98k
            iv_res_layer_recon_bufs_req_t s_res_layer_recon_bufs_req;
839
840
            /* local array of pointers */
841
6.98k
            void *apv_inp_luma_bufs[MAX_NUM_INP_DATA_BUFS];
842
6.98k
            void *apv_inp_cb_bufs[MAX_NUM_INP_DATA_BUFS];
843
6.98k
            void *apv_inp_cr_bufs[MAX_NUM_INP_DATA_BUFS];
844
6.98k
            void *apv_inp_sync_bufs[MAX_NUM_INP_CTRL_SYNC_BUFS];
845
6.98k
            void *apv_inp_async_bufs[MAX_NUM_INP_CTRL_ASYNC_BUFS];
846
6.98k
            void *apv_out_data_bufs[IHEVCE_MAX_NUM_RESOLUTIONS][IHEVCE_MAX_NUM_BITRATES]
847
6.98k
                                   [MAX_NUM_OUT_DATA_BUFS];
848
849
            /* get the number of resolutions */
850
6.98k
            i4_num_resolutions = ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.i4_num_res_layers;
851
852
            /* set the size of the structure */
853
6.98k
            s_input_bufs_req.i4_size = sizeof(iv_input_bufs_req_t);
854
6.98k
            s_res_layer_output_bufs_req.i4_size = sizeof(iv_res_layer_output_bufs_req_t);
855
6.98k
            s_res_layer_recon_bufs_req.i4_size = sizeof(iv_res_layer_recon_bufs_req_t);
856
857
            /* loop over num resolutions */
858
13.9k
            for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
859
6.98k
            {
860
                /* store the number of bitrates */
861
6.98k
                ai4_num_bitrate_instances[i4_res_id] =
862
6.98k
                    ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id]
863
6.98k
                        .i4_num_bitrate_instances;
864
865
                /* loop over num bitrates */
866
13.9k
                for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
867
6.98k
                {
868
6.98k
                    s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id].i4_size =
869
6.98k
                        sizeof(iv_output_bufs_req_t);
870
6.98k
                }
871
6.98k
            }
872
873
            /* call Query I/O buffer */
874
6.98k
            status = ihevce_query_io_buf_req(
875
6.98k
                ps_interface_ctxt,
876
6.98k
                &s_input_bufs_req,
877
6.98k
                &s_res_layer_output_bufs_req,
878
6.98k
                &s_res_layer_recon_bufs_req);
879
880
            /* check on the requirements against the MAX of application */
881
            /* should be present only for debug purpose                 */
882
883
            /* ---------------  Input data buffers init ---------------------- */
884
            /* allocate memory for input buffers  */
885
6.98k
            if(ps_interface_ctxt->i4_create_time_input_allocation == 1)
886
6.98k
            {
887
6.98k
                buf_size = s_input_bufs_req.i4_min_size_uv_buf + s_input_bufs_req.i4_min_size_y_buf;
888
6.98k
                ps_ctxt->s_memtab_inp_data_buf.i4_size = sizeof(iv_mem_rec_t);
889
6.98k
                ps_ctxt->s_memtab_inp_data_buf.i4_mem_alignment = 4;
890
6.98k
                ps_ctxt->s_memtab_inp_data_buf.i4_mem_size =
891
6.98k
                    (s_input_bufs_req.i4_min_num_yuv_bufs + XTRA_INP_DATA_BUFS) * buf_size;
892
6.98k
                ps_ctxt->s_memtab_inp_data_buf.e_mem_type = IV_EXT_CACHEABLE_NUMA_NODE0_MEM;
893
894
6.98k
                mem_mngr_alloc(
895
6.98k
                    ps_app_ctxt->pv_mem_mngr_handle, ps_sys_api, &ps_ctxt->s_memtab_inp_data_buf);
896
897
6.98k
                pu1_tmp_buf = (UWORD8 *)ps_ctxt->s_memtab_inp_data_buf.pv_base;
898
899
6.98k
                if(NULL == pu1_tmp_buf)
900
0
                {
901
0
                    ps_sys_api->ihevce_printf(
902
0
                        ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in allocate memory\n");
903
0
                    return (IHEVCE_EFAIL);
904
0
                }
905
906
                /* loop to initialise the buffer pointer */
907
26.4k
                for(ctr = 0; ctr < s_input_bufs_req.i4_min_num_yuv_bufs + XTRA_INP_DATA_BUFS; ctr++)
908
19.4k
                {
909
19.4k
                    apv_inp_luma_bufs[ctr] = pu1_tmp_buf;
910
19.4k
                    apv_inp_cb_bufs[ctr] = pu1_tmp_buf + s_input_bufs_req.i4_min_size_y_buf;
911
19.4k
                    apv_inp_cr_bufs[ctr] = NULL; /* 420SP case */
912
913
                    /* increment the input buffer pointer to next buffer */
914
19.4k
                    pu1_tmp_buf += buf_size;
915
19.4k
                }
916
6.98k
            }
917
918
            /* ---------------  Output data buffers init ---------------------- */
919
920
            /* loop over num resolutions */
921
13.9k
            for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
922
6.98k
            {
923
13.9k
                for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
924
6.98k
                {
925
6.98k
                    buf_size = s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
926
6.98k
                                   .i4_min_size_bitstream_buf;
927
928
6.98k
                    ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id].i4_size =
929
6.98k
                        sizeof(iv_mem_rec_t);
930
6.98k
                    ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id].i4_mem_alignment = 4;
931
932
6.98k
                    if(!ps_interface_ctxt->i4_create_time_output_allocation)
933
6.98k
                    {
934
6.98k
                        ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id].i4_mem_size =
935
6.98k
                            (s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
936
6.98k
                                 .i4_min_num_out_bufs +
937
6.98k
                             XTRA_OUT_DATA_BUFS) *
938
6.98k
                            buf_size;
939
6.98k
                    }
940
0
                    else
941
0
                    {
942
0
                        ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id].i4_mem_size =
943
0
                            (s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
944
0
                                 .i4_min_num_out_bufs) *
945
0
                            buf_size;
946
0
                    }
947
6.98k
                    ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id].e_mem_type =
948
6.98k
                        IV_EXT_CACHEABLE_NUMA_NODE1_MEM;
949
950
6.98k
                    mem_mngr_alloc(
951
6.98k
                        ps_app_ctxt->pv_mem_mngr_handle,
952
6.98k
                        ps_sys_api,
953
6.98k
                        &ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id]);
954
955
6.98k
                    pu1_tmp_buf =
956
6.98k
                        (UWORD8 *)ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id].pv_base;
957
6.98k
                    if(NULL == pu1_tmp_buf)
958
0
                    {
959
0
                        ps_sys_api->ihevce_printf(
960
0
                            ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in allocate memory\n");
961
0
                        return (IHEVCE_EFAIL);
962
0
                    }
963
964
6.98k
                    if(ps_interface_ctxt->i4_create_time_output_allocation == 1)
965
0
                    {
966
                        /* loop to initialise the buffer pointer */
967
0
                        for(ctr = 0;
968
0
                            ctr < s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
969
0
                                      .i4_min_num_out_bufs;
970
0
                            ctr++)
971
0
                        {
972
0
                            apv_out_data_bufs[i4_res_id][i4_br_id][ctr] = pu1_tmp_buf;
973
0
                            pu1_tmp_buf += buf_size;
974
0
                        }
975
0
                    }
976
6.98k
                    else
977
6.98k
                    {
978
6.98k
                        WORD32 i4_num_out_bufs =
979
6.98k
                            s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
980
6.98k
                                .i4_min_num_out_bufs +
981
6.98k
                            XTRA_OUT_DATA_BUFS;
982
6.98k
                        ps_ctxt->i4_num_out_bufs = i4_num_out_bufs;
983
6.98k
                        ps_ctxt->ai4_free_out_buf_idx[i4_res_id][i4_br_id] = 0;
984
6.98k
                        ps_ctxt->i4_prod_out_buf_idx = 0;
985
986
                        /* Assert to make sure ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][] array
987
                        has more bufs than ps_ctxt->i4_num_out_bufs. Needed to identify
988
                        wrap-around case */
989
6.98k
                        ASSERT(ps_ctxt->i4_num_out_bufs <= MAX_NUM_OUT_DATA_BUFS);
990
991
                        /* loop to initialise the buffer pointer */
992
34.9k
                        for(ctr = 0; ctr < i4_num_out_bufs; ctr++)
993
27.9k
                        {
994
27.9k
                            ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ctr].i4_idx = ctr;
995
27.9k
                            ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ctr].i4_is_free = 1;
996
27.9k
                            ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ctr].i4_is_prod = 0;
997
27.9k
                            ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ctr].i4_bytes_gen = 0;
998
27.9k
                            ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ctr].pu1_buf = pu1_tmp_buf;
999
27.9k
                            ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ctr].i4_buf_size = buf_size;
1000
27.9k
                            pu1_tmp_buf += buf_size;
1001
27.9k
                        }
1002
6.98k
                    }
1003
1004
                    /* create mutex for controlling the out strm buf b/w appln and encoder */
1005
6.98k
                    ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1006
6.98k
                        .pv_app_out_strm_buf_mutex_hdl = osal_mutex_create(ps_ctxt->pv_osal_handle);
1007
6.98k
                    if(NULL == ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1008
6.98k
                                   .pv_app_out_strm_buf_mutex_hdl)
1009
0
                    {
1010
0
                        ps_sys_api->ihevce_printf(
1011
0
                            ps_sys_api->pv_cb_handle,
1012
0
                            "IHEVCE ERROR: Error in Plugin initialization\n");
1013
0
                        return (IHEVCE_EFAIL);
1014
0
                    }
1015
1016
                    /* create mutex for controlling the out strm buf b/w appln and encoder */
1017
6.98k
                    ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1018
6.98k
                        .pv_app_out_strm_buf_cond_var_hdl =
1019
6.98k
                        osal_cond_var_create(ps_ctxt->pv_osal_handle);
1020
6.98k
                    if(NULL == ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1021
6.98k
                                   .pv_app_out_strm_buf_cond_var_hdl)
1022
0
                    {
1023
0
                        ps_sys_api->ihevce_printf(
1024
0
                            ps_sys_api->pv_cb_handle,
1025
0
                            "IHEVCE ERROR: Error in Plugin initialization\n");
1026
0
                        return (IHEVCE_EFAIL);
1027
0
                    }
1028
6.98k
                }
1029
6.98k
            }
1030
1031
6.98k
            if(ps_interface_ctxt->i4_create_time_input_allocation == 1)
1032
6.98k
            {
1033
                /* ------------- Input sync command buffers init -------------------- */
1034
6.98k
                buf_size = s_input_bufs_req.i4_min_size_synch_ctrl_bufs;
1035
1036
6.98k
                ps_ctxt->s_memtab_inp_sync_ctrl_buf.i4_size = sizeof(iv_mem_rec_t);
1037
6.98k
                ps_ctxt->s_memtab_inp_sync_ctrl_buf.i4_mem_alignment = 4;
1038
6.98k
                ps_ctxt->s_memtab_inp_sync_ctrl_buf.i4_mem_size =
1039
6.98k
                    (s_input_bufs_req.i4_min_num_yuv_bufs + XTRA_INP_DATA_BUFS) * buf_size;
1040
6.98k
                ps_ctxt->s_memtab_inp_sync_ctrl_buf.e_mem_type = IV_EXT_CACHEABLE_NUMA_NODE0_MEM;
1041
1042
6.98k
                mem_mngr_alloc(
1043
6.98k
                    ps_app_ctxt->pv_mem_mngr_handle,
1044
6.98k
                    ps_sys_api,
1045
6.98k
                    &ps_ctxt->s_memtab_inp_sync_ctrl_buf);
1046
1047
6.98k
                pu1_tmp_buf = (UWORD8 *)ps_ctxt->s_memtab_inp_sync_ctrl_buf.pv_base;
1048
1049
6.98k
                if(NULL == pu1_tmp_buf)
1050
0
                {
1051
0
                    ps_sys_api->ihevce_printf(
1052
0
                        ps_sys_api->pv_cb_handle, "IHEVCE ERROR: Error in allocate memory\n");
1053
0
                    return (IHEVCE_EFAIL);
1054
0
                }
1055
1056
                /* loop to initialise the buffer pointer */
1057
26.4k
                for(ctr = 0; ctr < s_input_bufs_req.i4_min_num_yuv_bufs + XTRA_INP_DATA_BUFS; ctr++)
1058
19.4k
                {
1059
19.4k
                    apv_inp_sync_bufs[ctr] = pu1_tmp_buf;
1060
19.4k
                    pu1_tmp_buf += buf_size;
1061
19.4k
                }
1062
6.98k
            }
1063
1064
            /* ------------- Input async command buffers init -------------------- */
1065
6.98k
            buf_size = s_input_bufs_req.i4_min_size_asynch_ctrl_bufs;
1066
1067
            /* allocate memory for output status buffer */
1068
6.98k
            ps_ctxt->pu1_inp_async_ctrl_buf = (UWORD8 *)ps_ctxt->ihevce_mem_alloc(
1069
6.98k
                NULL, s_input_bufs_req.i4_min_num_asynch_ctrl_bufs * buf_size);
1070
6.98k
            if(ps_ctxt->pu1_inp_async_ctrl_buf == NULL)
1071
0
            {
1072
0
                ps_sys_api->ihevce_printf(
1073
0
                    ps_sys_api->pv_cb_handle,
1074
0
                    "IHEVCE ERROR: Error in Plugin memory initialization\n");
1075
0
                return (IHEVCE_EFAIL);
1076
0
            }
1077
1078
6.98k
            pu1_tmp_buf = ps_ctxt->pu1_inp_async_ctrl_buf;
1079
1080
            /* loop to initialise the buffer pointer */
1081
34.9k
            for(ctr = 0; ctr < s_input_bufs_req.i4_min_num_asynch_ctrl_bufs; ctr++)
1082
27.9k
            {
1083
27.9k
                apv_inp_async_bufs[ctr] = pu1_tmp_buf;
1084
27.9k
                pu1_tmp_buf += buf_size;
1085
27.9k
            }
1086
1087
            /* Create IO ports for the buffer allocated */
1088
6.98k
            {
1089
6.98k
                iv_input_data_ctrl_buffs_desc_t s_inp_desc;
1090
6.98k
                iv_input_asynch_ctrl_buffs_desc_t s_inp_ctrl_desc;
1091
6.98k
                iv_res_layer_output_data_buffs_desc_t s_mres_out_desc;
1092
6.98k
                iv_res_layer_recon_data_buffs_desc_t s_mres_recon_desc;
1093
1094
                /* set the parameters of the input data control desc */
1095
6.98k
                s_inp_desc.i4_size = sizeof(iv_input_data_ctrl_buffs_desc_t);
1096
6.98k
                s_inp_desc.i4_num_synch_ctrl_bufs = s_input_bufs_req.i4_min_num_synch_ctrl_bufs;
1097
6.98k
                s_inp_desc.i4_num_yuv_bufs =
1098
6.98k
                    s_input_bufs_req.i4_min_num_yuv_bufs + XTRA_INP_DATA_BUFS;
1099
6.98k
                s_inp_desc.i4_size_y_buf = s_input_bufs_req.i4_min_size_y_buf;
1100
6.98k
                s_inp_desc.i4_size_uv_buf = s_input_bufs_req.i4_min_size_uv_buf;
1101
6.98k
                s_inp_desc.i4_size_synch_ctrl_bufs = s_input_bufs_req.i4_min_size_synch_ctrl_bufs;
1102
6.98k
                s_inp_desc.ppv_synch_ctrl_bufs = &apv_inp_sync_bufs[0];
1103
6.98k
                s_inp_desc.ppv_y_buf = &apv_inp_luma_bufs[0];
1104
6.98k
                s_inp_desc.ppv_u_buf = &apv_inp_cb_bufs[0];
1105
6.98k
                s_inp_desc.ppv_v_buf = &apv_inp_cr_bufs[0];
1106
1107
                /* set the parameters of the input async control desc */
1108
6.98k
                s_inp_ctrl_desc.i4_size = sizeof(iv_input_asynch_ctrl_buffs_desc_t);
1109
6.98k
                s_inp_ctrl_desc.i4_num_asynch_ctrl_bufs =
1110
6.98k
                    s_input_bufs_req.i4_min_num_asynch_ctrl_bufs;
1111
6.98k
                s_inp_ctrl_desc.i4_size_asynch_ctrl_bufs =
1112
6.98k
                    s_input_bufs_req.i4_min_size_asynch_ctrl_bufs;
1113
6.98k
                s_inp_ctrl_desc.ppv_asynch_ctrl_bufs = &apv_inp_async_bufs[0];
1114
1115
13.9k
                for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1116
6.98k
                {
1117
                    /* set the parameters of the output data desc */
1118
13.9k
                    for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
1119
6.98k
                    {
1120
6.98k
                        s_mres_out_desc.s_output_data_buffs[i4_res_id][i4_br_id].i4_size =
1121
6.98k
                            sizeof(iv_output_data_buffs_desc_t);
1122
1123
6.98k
                        if(!ps_interface_ctxt->i4_create_time_output_allocation)
1124
6.98k
                        {
1125
6.98k
                            s_mres_out_desc.s_output_data_buffs[i4_res_id][i4_br_id]
1126
6.98k
                                .i4_num_bitstream_bufs =
1127
6.98k
                                s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
1128
6.98k
                                    .i4_min_num_out_bufs +
1129
6.98k
                                XTRA_OUT_DATA_BUFS;
1130
6.98k
                        }
1131
0
                        else
1132
0
                        {
1133
0
                            s_mres_out_desc.s_output_data_buffs[i4_res_id][i4_br_id]
1134
0
                                .i4_num_bitstream_bufs =
1135
0
                                s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
1136
0
                                    .i4_min_num_out_bufs;
1137
0
                        }
1138
1139
6.98k
                        s_mres_out_desc.s_output_data_buffs[i4_res_id][i4_br_id]
1140
6.98k
                            .i4_size_bitstream_buf =
1141
6.98k
                            s_res_layer_output_bufs_req.s_output_buf_req[i4_res_id][i4_br_id]
1142
6.98k
                                .i4_min_size_bitstream_buf;
1143
6.98k
                        s_mres_out_desc.s_output_data_buffs[i4_res_id][i4_br_id].ppv_bitstream_bufs =
1144
6.98k
                            &apv_out_data_bufs[i4_res_id][i4_br_id][0];
1145
6.98k
                    }
1146
6.98k
                }
1147
1148
6.98k
                s_mres_recon_desc.i4_size = sizeof(iv_res_layer_recon_data_buffs_desc_t);
1149
                /* call create I/O ports */
1150
6.98k
                status = ihevce_create_ports(
1151
6.98k
                    ps_interface_ctxt,
1152
6.98k
                    &s_inp_desc,
1153
6.98k
                    &s_inp_ctrl_desc,
1154
6.98k
                    &s_mres_out_desc,
1155
6.98k
                    &s_mres_recon_desc);
1156
6.98k
            }
1157
6.98k
        }
1158
1159
        /* --------------------------------------------------------------------- */
1160
        /*            Create a High level encoder  thread                        */
1161
        /* --------------------------------------------------------------------- */
1162
0
        {
1163
6.98k
            osal_thread_attr_t s_thread_attr = OSAL_DEFAULT_THREAD_ATTR;
1164
1165
            /* Initialize application thread attributes */
1166
6.98k
            s_thread_attr.exit_code = 0;
1167
6.98k
            s_thread_attr.name = 0;
1168
6.98k
            s_thread_attr.priority_map_flag = 1;
1169
6.98k
            s_thread_attr.priority = OSAL_PRIORITY_DEFAULT;
1170
6.98k
            s_thread_attr.stack_addr = 0;
1171
6.98k
            s_thread_attr.stack_size = THREAD_STACK_SIZE;
1172
6.98k
            s_thread_attr.thread_func = ihevce_hle_interface_thrd;
1173
6.98k
            s_thread_attr.thread_param = (void *)(ps_interface_ctxt);
1174
6.98k
            s_thread_attr.core_affinity_mask = 0;
1175
6.98k
            s_thread_attr.group_num = 0;
1176
1177
            /* Create High level encoder thread */
1178
6.98k
            ps_ctxt->pv_hle_thread_hdl =
1179
6.98k
                osal_thread_create(ps_ctxt->pv_osal_handle, &s_thread_attr);
1180
6.98k
            if(NULL == ps_ctxt->pv_hle_thread_hdl)
1181
0
            {
1182
0
                return IHEVCE_EFAIL;
1183
0
            }
1184
6.98k
        }
1185
1186
        /* --------------------------------------------------------------------- */
1187
        /*                 Wait until HLE init is done                           */
1188
        /* --------------------------------------------------------------------- */
1189
6.98k
        {
1190
6.98k
            volatile WORD32 hle_init_done;
1191
6.98k
            volatile WORD32 *pi4_hle_init_done;
1192
1193
6.98k
            pi4_hle_init_done = (volatile WORD32 *)&ps_interface_ctxt->i4_hle_init_done;
1194
1195
6.98k
            do
1196
451M
            {
1197
451M
                hle_init_done = *pi4_hle_init_done;
1198
1199
451M
            } while(0 == hle_init_done);
1200
6.98k
        }
1201
1202
        /* reset flush mode */
1203
6.98k
        ps_ctxt->i4_flush_mode_on = 0;
1204
1205
6.98k
        {
1206
6.98k
            WORD32 i4_res_id;
1207
6.98k
            WORD32 i4_br_id;
1208
13.9k
            for(i4_res_id = 0; i4_res_id < IHEVCE_MAX_NUM_RESOLUTIONS; i4_res_id++)
1209
6.98k
            {
1210
13.9k
                for(i4_br_id = 0; i4_br_id < IHEVCE_MAX_NUM_BITRATES; i4_br_id++)
1211
6.98k
                {
1212
                    /* reset out end flag */
1213
6.98k
                    ps_ctxt->ai4_out_end_flag[i4_res_id][i4_br_id] = 0;
1214
6.98k
                }
1215
6.98k
            }
1216
6.98k
        }
1217
1218
        /* reset the field id */
1219
6.98k
        ps_ctxt->i4_field_id = 0;
1220
1221
        /* based on number of B pics set the DTS value */
1222
6.98k
        ps_ctxt->i8_dts = -1;
1223
1224
6.98k
        if(0 != ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_temporal_layers)
1225
1.82k
        {
1226
1.82k
            ps_ctxt->i8_dts =
1227
1.82k
                (-1) *
1228
1.82k
                (1 << ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_temporal_layers);
1229
1.82k
        }
1230
1231
        /* initialsie the buffer stride */
1232
6.98k
        {
1233
6.98k
            WORD32 max_cu_size;
1234
1235
6.98k
            max_cu_size = (1 << ps_ctxt->ps_static_cfg_prms->s_config_prms.i4_max_log2_cu_size);
1236
6.98k
            ps_ctxt->i4_frm_stride =
1237
6.98k
                ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_width +
1238
6.98k
                SET_CTB_ALIGN(ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_width, max_cu_size);
1239
6.98k
        }
1240
6.98k
    }
1241
0
    else
1242
0
    {
1243
        /* free plugin ctxt memory */
1244
0
        memory_free(NULL, ps_ctxt);
1245
1246
0
        return (IHEVCE_EFAIL);
1247
0
    }
1248
1249
    /* reset the place holders of old bitrate */
1250
6.98k
    memset(&ps_ctxt->ai4_old_bitrate[0][0], 0, sizeof(ps_ctxt->ai4_old_bitrate));
1251
1252
6.98k
    ps_ctxt->ai4_old_bitrate[0][0] = ps_params->s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0];
1253
1254
    /* store the plugin handle before returning */
1255
6.98k
    *ppv_ihevce_hdl = (void *)ps_ctxt;
1256
1257
6.98k
    return (IHEVCE_EOK);
1258
7.01k
}
1259
1260
static IHEVCE_PLUGIN_STATUS_T
1261
    ihevce_receive_out_buffer(plugin_ctxt_t *ps_ctxt, ihevce_out_buf_t *ps_out)
1262
104k
{
1263
104k
    app_ctxt_t *ps_app_ctxt = &ps_ctxt->s_app_ctxt;
1264
104k
    WORD32 i4_res_id, i4_br_id;
1265
104k
    WORD32 i4_num_resolutions;
1266
104k
    WORD32 ai4_num_bitrate_instances[IHEVCE_MAX_NUM_RESOLUTIONS] = { 1 };
1267
1268
104k
    i4_num_resolutions = ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.i4_num_res_layers;
1269
208k
    for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1270
104k
    {
1271
104k
        ai4_num_bitrate_instances[i4_res_id] =
1272
104k
            ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id]
1273
104k
                .i4_num_bitrate_instances;
1274
104k
    }
1275
    /* default init */
1276
104k
    ps_out->pu1_output_buf = NULL;
1277
104k
    ps_out->i4_bytes_generated = 0;
1278
1279
    /* ---------------- if any output buffer is available return the buffer back ------------- */
1280
104k
    while(1)
1281
104k
    {
1282
104k
        WORD32 osal_result;
1283
104k
        WORD32 buf_present = 0;
1284
104k
        WORD32 i4_is_prod = 1;
1285
104k
        WORD32 i4_atleast_one_br_prod = 0;
1286
        /****** Lock the critical section ******/
1287
104k
        osal_result =
1288
104k
            osal_mutex_lock(ps_app_ctxt->as_out_strm_prms[0][0].pv_app_out_strm_buf_mutex_hdl);
1289
1290
104k
        if(OSAL_SUCCESS != osal_result)
1291
0
            return IHEVCE_EFAIL;
1292
1293
        /* wait until entropy sends an output */
1294
201k
        while(1)
1295
201k
        {
1296
201k
            i4_is_prod = 1;
1297
402k
            for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1298
201k
            {
1299
402k
                for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
1300
201k
                {
1301
201k
                    i4_is_prod &=
1302
201k
                        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ps_ctxt->i4_prod_out_buf_idx]
1303
201k
                            .i4_is_prod;
1304
201k
                    i4_atleast_one_br_prod |=
1305
201k
                        ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ps_ctxt->i4_prod_out_buf_idx]
1306
201k
                            .i4_is_prod;
1307
201k
                }
1308
201k
            }
1309
201k
            if(!i4_is_prod)
1310
97.0k
            {
1311
194k
                for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1312
97.0k
                {
1313
194k
                    for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
1314
97.0k
                    {
1315
97.0k
                        osal_cond_var_wait(
1316
97.0k
                            ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1317
97.0k
                                .pv_app_out_strm_buf_cond_var_hdl,
1318
97.0k
                            ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1319
97.0k
                                .pv_app_out_strm_buf_mutex_hdl);
1320
97.0k
                    }
1321
97.0k
                }
1322
97.0k
            }
1323
104k
            else
1324
104k
            {
1325
104k
                break;
1326
104k
            }
1327
201k
        }
1328
1329
104k
        ASSERT(i4_is_prod == 1);
1330
1331
        /* check if the current buffer for all bitrates and resolutions have been produced */
1332
104k
        if(1 == i4_is_prod)
1333
104k
        {
1334
104k
            buf_present = 1;
1335
1336
208k
            for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1337
104k
            {
1338
208k
                for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
1339
104k
                {
1340
                    /* set the buffer to free status */
1341
104k
                    ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][ps_ctxt->i4_prod_out_buf_idx]
1342
104k
                        .i4_is_free = 1;
1343
104k
                    if((0 == i4_res_id) && (0 == i4_br_id))
1344
104k
                    {
1345
104k
                        ps_out->i4_bytes_generated =
1346
104k
                            ps_ctxt->aaas_out_bufs[0][0][ps_ctxt->i4_prod_out_buf_idx].i4_bytes_gen;
1347
104k
                        ps_out->pu1_output_buf =
1348
104k
                            ps_ctxt->aaas_out_bufs[0][0][ps_ctxt->i4_prod_out_buf_idx].pu1_buf;
1349
104k
                    }
1350
104k
                }
1351
104k
            }
1352
1353
            /* copy the contents to output buffer */
1354
104k
            ps_out->i4_is_key_frame =
1355
104k
                ps_ctxt->aaas_out_bufs[0][0][ps_ctxt->i4_prod_out_buf_idx].i4_is_key_frame;
1356
104k
            ps_out->u8_pts =
1357
104k
                ps_ctxt->aaas_out_bufs[0][0][ps_ctxt->i4_prod_out_buf_idx].i4_timestamp_low;
1358
104k
            ps_out->u8_pts =
1359
104k
                ps_out->u8_pts |
1360
104k
                ((ULWORD64)(
1361
104k
                     ps_ctxt->aaas_out_bufs[0][0][ps_ctxt->i4_prod_out_buf_idx].i4_timestamp_high)
1362
104k
                 << 32);
1363
104k
            ps_out->i4_end_flag =
1364
104k
                ps_ctxt->aaas_out_bufs[0][0][ps_ctxt->i4_prod_out_buf_idx].i4_end_flag;
1365
104k
            ps_out->i8_dts = ps_ctxt->i8_dts;
1366
1367
            /* increment the DTS */
1368
104k
            ps_ctxt->i8_dts++;
1369
104k
        }
1370
1371
        /* check for buffer present */
1372
104k
        if(1 == buf_present)
1373
104k
        {
1374
104k
            ps_ctxt->i4_prod_out_buf_idx++;
1375
1376
            /* wrap around case */
1377
104k
            if(ps_ctxt->i4_prod_out_buf_idx == ps_ctxt->i4_num_out_bufs)
1378
23.0k
            {
1379
23.0k
                ps_ctxt->i4_prod_out_buf_idx = 0;
1380
23.0k
            }
1381
1382
            /****** Unlock the critical section ******/
1383
104k
            osal_result = osal_mutex_unlock(
1384
104k
                ps_app_ctxt->as_out_strm_prms[0][0].pv_app_out_strm_buf_mutex_hdl);
1385
104k
            if(OSAL_SUCCESS != osal_result)
1386
0
                return IHEVCE_EFAIL;
1387
1388
            /* break while 1 loop */
1389
104k
            break;
1390
104k
        }
1391
0
        else
1392
0
        {
1393
            /* in steady state*/
1394
0
            if(0 == ps_ctxt->i4_flush_mode_on)
1395
0
            {
1396
                /****** Unlock the critical section ******/
1397
0
                osal_result = osal_mutex_unlock(
1398
0
                    ps_app_ctxt->as_out_strm_prms[0][0].pv_app_out_strm_buf_mutex_hdl);
1399
0
                if(OSAL_SUCCESS != osal_result)
1400
0
                    return IHEVCE_EFAIL;
1401
0
                if(!i4_atleast_one_br_prod) /*** If atleast one bitrate is produced do not break from loop **/
1402
0
                { /*** Continue in while loop and Wait for next bitrate ***/
1403
                    /* break while 1 loop */
1404
0
                    break;
1405
0
                }
1406
0
            }
1407
0
            else
1408
0
            {
1409
                /* In flush mode is ON then this function must return output
1410
                buffers. Otherwise assume that encoding is over and return fail */
1411
                /****** Unlock the critical section ******/
1412
0
                osal_result = osal_mutex_unlock(
1413
0
                    ps_app_ctxt->as_out_strm_prms[0][0].pv_app_out_strm_buf_mutex_hdl);
1414
0
                if(OSAL_SUCCESS != osal_result)
1415
0
                    return IHEVCE_EFAIL;
1416
0
            }
1417
0
        }
1418
104k
    }
1419
1420
104k
    return IHEVCE_EOK;
1421
104k
}
1422
1423
static IHEVCE_PLUGIN_STATUS_T
1424
    ihevce_queue_out_buffer(plugin_ctxt_t *ps_ctxt, WORD32 i4_res_id, WORD32 i4_br_id)
1425
115k
{
1426
115k
    app_ctxt_t *ps_app_ctxt = &ps_ctxt->s_app_ctxt;
1427
115k
    ihevce_hle_ctxt_t *ps_interface_ctxt = (ihevce_hle_ctxt_t *)ps_ctxt->pv_hle_interface_ctxt;
1428
1429
    /* --------------------------------------------------------------------- */
1430
    /*           Free Output buffer Queuing                                  */
1431
    /* --------------------------------------------------------------------- */
1432
    /* ------- Que in free output buffer if end flag is not set ------ */
1433
115k
    if(0 == ps_ctxt->ai4_out_end_flag[i4_res_id][i4_br_id])
1434
115k
    {
1435
115k
        WORD32 osal_result;
1436
115k
        iv_output_data_buffs_t *ps_curr_out;
1437
115k
        WORD32 buf_id_strm;
1438
115k
        WORD32 free_idx;
1439
1440
115k
        free_idx = ps_ctxt->ai4_free_out_buf_idx[i4_res_id][i4_br_id];
1441
1442
115k
        if(1 == ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].i4_is_free)
1443
115k
        {
1444
            /* ---------- get a free desc. from output Q ------ */
1445
115k
            ps_curr_out = (iv_output_data_buffs_t *)ihevce_q_get_free_out_strm_buff(
1446
115k
                ps_interface_ctxt, &buf_id_strm, BUFF_QUE_NON_BLOCKING_MODE, i4_br_id, i4_res_id);
1447
1448
            /* if a free buffer is available */
1449
115k
            if(NULL != ps_curr_out)
1450
115k
            {
1451
                /****** Lock the critical section ******/
1452
115k
                osal_result = osal_mutex_lock(ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1453
115k
                                                  .pv_app_out_strm_buf_mutex_hdl);
1454
1455
115k
                if(OSAL_SUCCESS != osal_result)
1456
0
                    return IHEVCE_EFAIL;
1457
1458
115k
                if(1 == ps_app_ctxt->ai4_out_strm_end_flag[i4_res_id][i4_br_id])
1459
0
                {
1460
0
                    ps_curr_out->i4_is_last_buf = 1;
1461
0
                    ps_ctxt->ai4_out_end_flag[i4_res_id][i4_br_id] = 1;
1462
0
                }
1463
115k
                else
1464
115k
                {
1465
115k
                    ps_curr_out->i4_is_last_buf = 0;
1466
115k
                }
1467
115k
                ASSERT(ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].i4_is_free == 1);
1468
115k
                ASSERT(free_idx == ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].i4_idx);
1469
1470
115k
                ps_curr_out->pv_bitstream_bufs =
1471
115k
                    (void *)ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].pu1_buf;
1472
115k
                ps_curr_out->i4_cb_buf_id =
1473
115k
                    ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].i4_idx;
1474
115k
                ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].i4_is_free = 0;
1475
115k
                ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].i4_is_prod = 0;
1476
115k
                ps_ctxt->aaas_out_bufs[i4_res_id][i4_br_id][free_idx].i4_bytes_gen = 0;
1477
1478
115k
                ps_ctxt->ai4_free_out_buf_idx[i4_res_id][i4_br_id]++;
1479
1480
                /* wrap around case */
1481
115k
                if(ps_ctxt->ai4_free_out_buf_idx[i4_res_id][i4_br_id] == ps_ctxt->i4_num_out_bufs)
1482
25.5k
                {
1483
25.5k
                    ps_ctxt->ai4_free_out_buf_idx[i4_res_id][i4_br_id] = 0;
1484
25.5k
                }
1485
1486
                /****** Unlock the critical section ******/
1487
115k
                osal_result = osal_mutex_unlock(ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1488
115k
                                                    .pv_app_out_strm_buf_mutex_hdl);
1489
115k
                if(OSAL_SUCCESS != osal_result)
1490
0
                    return IHEVCE_EFAIL;
1491
1492
                /* ---------- set the buffer as produced ---------- */
1493
115k
                ihevce_q_set_out_strm_buff_prod(
1494
115k
                    ps_interface_ctxt, buf_id_strm, i4_br_id, i4_res_id);
1495
115k
            }
1496
115k
        }
1497
115k
    }
1498
115k
    return IHEVCE_EOK;
1499
115k
}
1500
1501
/*!
1502
******************************************************************************
1503
* \if Function name : ihevce_close \endif
1504
*
1505
* \brief
1506
*    De-Initialises the encoder context and threads
1507
*
1508
* \param[in] Static params pointer
1509
*
1510
* \return
1511
*    status
1512
*
1513
* \author
1514
*  Ittiam
1515
*
1516
*****************************************************************************
1517
*/
1518
IHEVCE_PLUGIN_STATUS_T ihevce_close(void *pv_ihevce_hdl)
1519
6.98k
{
1520
    /* local variables */
1521
6.98k
    plugin_ctxt_t *ps_ctxt;
1522
6.98k
    app_ctxt_t *ps_app_ctxt;
1523
6.98k
    ihevce_hle_ctxt_t *ps_interface_ctxt;
1524
6.98k
    WORD32 i4_num_resolutions;
1525
6.98k
    WORD32 i4_res_id;
1526
6.98k
    WORD32 i4_br_id;
1527
6.98k
    WORD32 ai4_num_bitrate_instances[IHEVCE_MAX_NUM_RESOLUTIONS] = { 1 };
1528
6.98k
    ihevce_sys_api_t *ps_sys_api;
1529
1530
    /* sanity checks */
1531
6.98k
    if(NULL == pv_ihevce_hdl)
1532
0
        return (IHEVCE_EFAIL);
1533
1534
    /* derive local variables */
1535
6.98k
    ps_ctxt = (plugin_ctxt_t *)pv_ihevce_hdl;
1536
1537
6.98k
    ps_sys_api = &ps_ctxt->ps_static_cfg_prms->s_sys_api;
1538
1539
6.98k
    if((0 == ps_ctxt->ps_static_cfg_prms->i4_res_id) &&
1540
6.98k
       (0 == ps_ctxt->ps_static_cfg_prms->i4_br_id))
1541
6.98k
    {
1542
6.98k
        ps_interface_ctxt = (ihevce_hle_ctxt_t *)ps_ctxt->pv_hle_interface_ctxt;
1543
6.98k
        ps_app_ctxt = &ps_ctxt->s_app_ctxt;
1544
6.98k
        i4_num_resolutions = ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.i4_num_res_layers;
1545
1546
6.98k
        if(1 != ps_ctxt->i4_flush_mode_on)
1547
6.98k
        {
1548
13.9k
            for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1549
6.98k
            {
1550
6.98k
                ai4_num_bitrate_instances[i4_res_id] =
1551
6.98k
                    ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id]
1552
6.98k
                        .i4_num_bitrate_instances;
1553
13.9k
                for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
1554
6.98k
                {
1555
                    /* ------- Que in free output buffer if end flag is not set ------ */
1556
6.98k
                    ihevce_queue_out_buffer(ps_ctxt, i4_res_id, i4_br_id);
1557
6.98k
                }
1558
6.98k
            }
1559
            /* --------------------------------------------------------------------- */
1560
            /*            Input Processing                                           */
1561
            /* --------------------------------------------------------------------- */
1562
6.98k
            {
1563
6.98k
                WORD32 buf_id;
1564
1565
6.98k
                iv_input_data_ctrl_buffs_t *ps_curr_inp;
1566
6.98k
                WORD32 *pi4_ctrl_ptr;
1567
1568
                /* ---------- get a free buffer from input Q ------ */
1569
6.98k
                ps_curr_inp = (iv_input_data_ctrl_buffs_t *)ihevce_q_get_free_inp_data_buff(
1570
6.98k
                    ps_interface_ctxt, &buf_id, BUFF_QUE_BLOCKING_MODE);
1571
1572
6.98k
                if(NULL != ps_curr_inp)
1573
6.98k
                {
1574
                    /* flush mode command */
1575
1576
6.98k
                    ps_curr_inp->i4_buf_id = buf_id;
1577
1578
                    /* set the input status to invalid flag */
1579
6.98k
                    ps_curr_inp->i4_inp_frm_data_valid_flag = 0;
1580
1581
6.98k
                    pi4_ctrl_ptr = (WORD32 *)ps_curr_inp->pv_synch_ctrl_bufs;
1582
1583
6.98k
                    *pi4_ctrl_ptr = IHEVCE_SYNCH_API_FLUSH_TAG;
1584
6.98k
                    *(pi4_ctrl_ptr + 1) = 0;
1585
6.98k
                    *(pi4_ctrl_ptr + 2) = IHEVCE_SYNCH_API_END_TAG;
1586
1587
6.98k
                    ps_curr_inp->i4_cmd_buf_size = 4 * 3; /* 4 bytes */
1588
1589
                    /* ---------- set the buffer as produced ---------- */
1590
6.98k
                    ihevce_q_set_inp_data_buff_prod(ps_interface_ctxt, buf_id);
1591
6.98k
                }
1592
0
                else
1593
0
                {
1594
                    /* Enable flush-mode and internal-flush once limit according to
1595
                    Eval-version is reached */
1596
0
                    ps_ctxt->i4_flush_mode_on = 1;
1597
0
                }
1598
6.98k
            }
1599
6.98k
        }
1600
1601
        /* --------------------------------------------------------------------- */
1602
        /*            Wait and destroy Processing threads                        */
1603
        /* --------------------------------------------------------------------- */
1604
1605
        /* Wait for High level encoder thread to complete */
1606
6.98k
        osal_thread_wait(ps_ctxt->pv_hle_thread_hdl);
1607
1608
        /* Destroy Hle thread */
1609
6.98k
        osal_thread_destroy(ps_ctxt->pv_hle_thread_hdl);
1610
1611
        /* --------------------------------------------------------------------- */
1612
        /*            Input Output and Command buffers free                      */
1613
        /* --------------------------------------------------------------------- */
1614
1615
        /* free output data and control buffer */
1616
1617
13.9k
        for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1618
6.98k
        {
1619
6.98k
            ai4_num_bitrate_instances[i4_res_id] =
1620
6.98k
                ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id]
1621
6.98k
                    .i4_num_bitrate_instances;
1622
1623
13.9k
            for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
1624
6.98k
            {
1625
6.98k
                mem_mngr_free(
1626
6.98k
                    ps_app_ctxt->pv_mem_mngr_handle,
1627
6.98k
                    &ps_ctxt->as_memtab_out_data_buf[i4_res_id][i4_br_id]);
1628
1629
                /* free mutex of out strm buf b/w appln and encoder */
1630
6.98k
                osal_mutex_destroy(ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1631
6.98k
                                       .pv_app_out_strm_buf_mutex_hdl);
1632
1633
6.98k
                osal_cond_var_destroy(ps_app_ctxt->as_out_strm_prms[i4_res_id][i4_br_id]
1634
6.98k
                                          .pv_app_out_strm_buf_cond_var_hdl);
1635
6.98k
            }
1636
6.98k
        }
1637
1638
6.98k
        ps_ctxt->ihevce_mem_free(NULL, ps_ctxt->pu1_out_ctrl_buf);
1639
6.98k
        ps_ctxt->ihevce_mem_free(NULL, ps_ctxt->pu1_inp_async_ctrl_buf);
1640
1641
        /* free input data and control buffer */
1642
6.98k
        if(ps_interface_ctxt->i4_create_time_input_allocation == 1)
1643
6.98k
        {
1644
6.98k
            mem_mngr_free(ps_app_ctxt->pv_mem_mngr_handle, &ps_ctxt->s_memtab_inp_data_buf);
1645
6.98k
            mem_mngr_free(ps_app_ctxt->pv_mem_mngr_handle, &ps_ctxt->s_memtab_inp_sync_ctrl_buf);
1646
6.98k
        }
1647
1648
        /* --------------------------------------------------------------------- */
1649
        /*               Encoder Instance Deletion                               */
1650
        /* --------------------------------------------------------------------- */
1651
6.98k
        ihevce_hle_interface_delete(ps_interface_ctxt);
1652
1653
        /* free the high level encoder context memory */
1654
6.98k
        ps_ctxt->ihevce_mem_free(NULL, ps_ctxt->pv_hle_interface_ctxt);
1655
1656
6.98k
        if(ps_ctxt->ps_static_cfg_prms->i4_enable_csv_dump)
1657
0
        {
1658
0
            ps_sys_api->s_file_io_api.ihevce_fclose(
1659
0
                (void *)ps_sys_api->pv_cb_handle, ps_ctxt->ps_static_cfg_prms->apF_csv_file[0][0]);
1660
0
        }
1661
1662
        /* free static config memory */
1663
6.98k
        ps_ctxt->ihevce_mem_free(NULL, ps_ctxt->ps_static_cfg_prms);
1664
1665
        /* free osal handle */
1666
6.98k
        memory_free(NULL, ps_ctxt->pv_osal_handle);
1667
1668
        /* free plugin ctxt memory */
1669
6.98k
        memory_free(NULL, pv_ihevce_hdl);
1670
6.98k
    }
1671
0
    else
1672
0
    {
1673
0
        return (IHEVCE_EFAIL);
1674
0
    }
1675
1676
6.98k
    return (IHEVCE_EOK);
1677
6.98k
}
1678
1679
/*!
1680
******************************************************************************
1681
* \if Function name : ihevce_copy_inp_8bit \endif
1682
*
1683
* \brief
1684
*    Input copy function for 8 bit input
1685
*
1686
* \param[in] Source and desdtination buffer descriptors
1687
*
1688
* \return
1689
*    None
1690
*
1691
* \author
1692
*  Ittiam
1693
*
1694
*****************************************************************************
1695
*/
1696
IV_API_CALL_STATUS_T ihevce_copy_inp_8bit(
1697
    iv_input_data_ctrl_buffs_t *ps_curr_inp,
1698
    ihevce_inp_buf_t *ps_inp,
1699
    WORD32 chroma_format,
1700
    WORD32 i4_orig_wd,
1701
    WORD32 i4_orig_ht)
1702
101k
{
1703
101k
    UWORD8 *pu1_src, *pu1_dst;
1704
101k
    WORD32 src_strd, dst_strd;
1705
101k
    WORD32 frm_height = i4_orig_ht;
1706
101k
    WORD32 frm_width = i4_orig_wd;
1707
101k
    WORD32 buf_height = ps_curr_inp->s_input_buf.i4_y_ht;
1708
101k
    WORD32 buf_width = ps_curr_inp->s_input_buf.i4_y_wd;
1709
101k
    WORD32 rows, cols;
1710
1711
101k
    pu1_src = (UWORD8 *)ps_inp->apv_inp_planes[0];
1712
101k
    src_strd = ps_inp->ai4_inp_strd[0];
1713
101k
    pu1_dst = (UWORD8 *)ps_curr_inp->s_input_buf.pv_y_buf;
1714
101k
    dst_strd = ps_curr_inp->s_input_buf.i4_y_strd;
1715
1716
101k
    if((ps_inp->ai4_inp_size[0] < (src_strd * frm_height)) || (ps_inp->ai4_inp_size[0] <= 0) ||
1717
101k
       (ps_inp->apv_inp_planes[0] == NULL))
1718
0
    {
1719
0
        return (IV_FAIL);
1720
0
    }
1721
    /* copy the input luma data into internal buffer */
1722
1.77M
    for(rows = 0; rows < frm_height; rows++)
1723
1.67M
    {
1724
1.67M
        memcpy(pu1_dst, pu1_src, frm_width);
1725
1.67M
        if(buf_width > frm_width)
1726
1.22M
        {
1727
1.22M
            memset(pu1_dst + frm_width, 0x0, buf_width - frm_width);
1728
1.22M
        }
1729
1.67M
        pu1_src += src_strd;
1730
1.67M
        pu1_dst += dst_strd;
1731
1.67M
    }
1732
5.71M
    while(rows < buf_height)
1733
5.61M
    {
1734
5.61M
        memset(pu1_dst, 0x0, buf_width);
1735
5.61M
        pu1_dst += dst_strd;
1736
5.61M
        rows++;
1737
5.61M
    }
1738
1739
101k
    if(IV_YUV_420P == chroma_format)
1740
101k
    {
1741
101k
        UWORD8 *pu1_src_u, *pu1_src_v;
1742
101k
        WORD32 src_strd_u, src_strd_v;
1743
1744
101k
        pu1_src_u = (UWORD8 *)ps_inp->apv_inp_planes[1];
1745
101k
        src_strd_u = ps_inp->ai4_inp_strd[1];
1746
101k
        pu1_src_v = (UWORD8 *)ps_inp->apv_inp_planes[2];
1747
101k
        src_strd_v = ps_inp->ai4_inp_strd[2];
1748
101k
        pu1_dst = (UWORD8 *)ps_curr_inp->s_input_buf.pv_u_buf;
1749
101k
        dst_strd = ps_curr_inp->s_input_buf.i4_uv_strd;
1750
1751
101k
        frm_width = i4_orig_wd >> 1;
1752
101k
        frm_height = i4_orig_ht >> 1;
1753
101k
        buf_width = ps_curr_inp->s_input_buf.i4_uv_wd;
1754
101k
        buf_height = ps_curr_inp->s_input_buf.i4_uv_ht;
1755
1756
101k
        if((ps_inp->ai4_inp_size[1] < (ps_inp->ai4_inp_strd[1] * frm_height)) ||
1757
101k
           (ps_inp->ai4_inp_size[1] <= 0) || (pu1_src_u == NULL))
1758
0
        {
1759
0
            return (IV_FAIL);
1760
0
        }
1761
101k
        if((ps_inp->ai4_inp_size[2] < (ps_inp->ai4_inp_strd[2] * frm_height)) ||
1762
101k
           (ps_inp->ai4_inp_size[2] <= 0) || (pu1_src_v == NULL))
1763
0
        {
1764
0
            return (IV_FAIL);
1765
0
        }
1766
1767
        /* copy the input chroma data in pixel interleaved format */
1768
936k
        for(rows = 0; rows < frm_height; rows++)
1769
835k
        {
1770
78.8M
            for(cols = 0; cols < frm_width; cols++)
1771
78.0M
            {
1772
                /* U V alternate */
1773
78.0M
                pu1_dst[(cols << 1)] = pu1_src_u[cols];
1774
78.0M
                pu1_dst[(cols << 1) + 1] = pu1_src_v[cols];
1775
78.0M
            }
1776
835k
            if(buf_width > (cols << 1))
1777
610k
            {
1778
610k
                memset(&pu1_dst[(cols << 1)], 0x80, buf_width - (cols << 1));
1779
610k
            }
1780
1781
835k
            pu1_src_u += src_strd_u;
1782
835k
            pu1_src_v += src_strd_v;
1783
835k
            pu1_dst += dst_strd;
1784
835k
        }
1785
2.90M
        while(rows < buf_height)
1786
2.80M
        {
1787
2.80M
            memset(pu1_dst, 0x80, buf_width);
1788
1789
2.80M
            pu1_dst += dst_strd;
1790
2.80M
            rows++;
1791
2.80M
        }
1792
101k
    }
1793
0
    else if(IV_YUV_420SP_UV == chroma_format)
1794
0
    {
1795
0
        pu1_src = (UWORD8 *)ps_inp->apv_inp_planes[1];
1796
0
        src_strd = ps_inp->ai4_inp_strd[1];
1797
0
        pu1_dst = (UWORD8 *)ps_curr_inp->s_input_buf.pv_u_buf;
1798
0
        dst_strd = ps_curr_inp->s_input_buf.i4_uv_strd;
1799
1800
0
        frm_width = i4_orig_wd;
1801
0
        frm_height = i4_orig_ht >> 1;
1802
0
        buf_width = ps_curr_inp->s_input_buf.i4_uv_wd;
1803
0
        buf_height = ps_curr_inp->s_input_buf.i4_uv_ht;
1804
1805
0
        if((ps_inp->ai4_inp_size[1] < (ps_inp->ai4_inp_strd[1] * frm_height)) ||
1806
0
           (ps_inp->ai4_inp_size[1] <= 0) || (pu1_src == NULL))
1807
0
        {
1808
0
            return (IV_FAIL);
1809
0
        }
1810
1811
        /* copy the input luma data into internal buffer */
1812
0
        for(rows = 0; rows < frm_height; rows++)
1813
0
        {
1814
0
            memcpy(pu1_dst, pu1_src, frm_width);
1815
0
            if(buf_width > frm_width)
1816
0
            {
1817
0
                memset(pu1_dst + frm_width, 0x80, buf_width - frm_width);
1818
0
            }
1819
0
            pu1_src += src_strd;
1820
0
            pu1_dst += dst_strd;
1821
0
        }
1822
0
        while(rows < buf_height)
1823
0
        {
1824
0
            memset(pu1_dst, 0x80, buf_width);
1825
0
            pu1_dst += dst_strd;
1826
0
            rows++;
1827
0
        }
1828
0
    }
1829
101k
    return (IV_SUCCESS);
1830
101k
}
1831
1832
/*!
1833
******************************************************************************
1834
* \if Function name : ihevce_encode_header \endif
1835
*
1836
* \brief
1837
*    Receive sps, pps and vps of the encoded sequence
1838
*
1839
* \param[in] Plugin handle, Output buffer
1840
*
1841
* \return
1842
*    Success or Failure
1843
*
1844
* \author
1845
*  Ittiam
1846
*
1847
*****************************************************************************
1848
*/
1849
IHEVCE_PLUGIN_STATUS_T ihevce_encode_header(void *pv_ihevce_hdl, ihevce_out_buf_t *ps_out)
1850
6.98k
{
1851
6.98k
    plugin_ctxt_t *ps_ctxt = (plugin_ctxt_t *)pv_ihevce_hdl;
1852
6.98k
    ihevce_hle_ctxt_t *ps_interface_ctxt;
1853
1854
    /* sanity checks */
1855
6.98k
    if(NULL == pv_ihevce_hdl)
1856
0
        return (IHEVCE_EFAIL);
1857
1858
6.98k
    if(NULL == ps_out)
1859
0
        return (IHEVCE_EFAIL);
1860
1861
6.98k
    if((0 == ps_ctxt->ps_static_cfg_prms->i4_res_id) &&
1862
6.98k
       (0 == ps_ctxt->ps_static_cfg_prms->i4_br_id))
1863
6.98k
    {
1864
6.98k
        WORD32 status;
1865
1866
        /* ------- Que in free output buffer if end flag is not set ------ */
1867
6.98k
        ihevce_queue_out_buffer(ps_ctxt, 0, 0);
1868
1869
        /* ------- API call to encoder header ------- */
1870
6.98k
        ps_interface_ctxt = (ihevce_hle_ctxt_t *)ps_ctxt->pv_hle_interface_ctxt;
1871
6.98k
        status = ihevce_entropy_encode_header(ps_interface_ctxt, 0, 0);
1872
6.98k
        if(status)
1873
0
            return IHEVCE_EFAIL;
1874
1875
        /* ------- receive header ------- */
1876
6.98k
        ihevce_receive_out_buffer(ps_ctxt, ps_out);
1877
6.98k
    }
1878
0
    else
1879
0
    {
1880
0
        return (IHEVCE_EFAIL);
1881
0
    }
1882
1883
6.98k
    return (IHEVCE_EOK);
1884
6.98k
}
1885
1886
/*!
1887
******************************************************************************
1888
* \if Function name : ihevce_encode \endif
1889
*
1890
* \brief
1891
*    Frame level processing function
1892
*
1893
* \param[in] Plugin handle, Input buffer, Output buffer
1894
*
1895
* \return
1896
*    Success or Failure
1897
*
1898
* \author
1899
*  Ittiam
1900
*
1901
*****************************************************************************
1902
*/
1903
IHEVCE_PLUGIN_STATUS_T
1904
    ihevce_encode(void *pv_ihevce_hdl, ihevce_inp_buf_t *ps_inp, ihevce_out_buf_t *ps_out)
1905
101k
{
1906
    /* local variables */
1907
101k
    plugin_ctxt_t *ps_ctxt;
1908
101k
    app_ctxt_t *ps_app_ctxt;
1909
101k
    ihevce_hle_ctxt_t *ps_interface_ctxt;
1910
1911
101k
    WORD32 i4_res_id, i4_br_id;
1912
101k
    WORD32 i4_num_resolutions;
1913
101k
    WORD32 ai4_num_bitrate_instances[IHEVCE_MAX_NUM_RESOLUTIONS] = { 1 };
1914
101k
    UWORD32 u4_latency = 0;
1915
1916
    /* sanity checks */
1917
101k
    if(NULL == pv_ihevce_hdl)
1918
0
        return (IHEVCE_EFAIL);
1919
1920
101k
    if(NULL == ps_out)
1921
0
        return (IHEVCE_EFAIL);
1922
1923
    /* derive local variables */
1924
101k
    ps_ctxt = (plugin_ctxt_t *)pv_ihevce_hdl;
1925
101k
    if((0 == ps_ctxt->ps_static_cfg_prms->i4_res_id) &&
1926
101k
       (0 == ps_ctxt->ps_static_cfg_prms->i4_br_id))
1927
101k
    {
1928
101k
        ps_interface_ctxt = (ihevce_hle_ctxt_t *)ps_ctxt->pv_hle_interface_ctxt;
1929
101k
        ps_app_ctxt = &ps_ctxt->s_app_ctxt;
1930
101k
        i4_num_resolutions = ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.i4_num_res_layers;
1931
1932
101k
        if(ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_temporal_layers)
1933
20.9k
        {
1934
20.9k
            u4_latency +=
1935
20.9k
                (1 << ps_ctxt->ps_static_cfg_prms->s_coding_tools_prms.i4_max_temporal_layers) - 1;
1936
20.9k
        }
1937
1938
101k
        u4_latency += ps_ctxt->ps_static_cfg_prms->s_lap_prms.i4_rc_look_ahead_pics;
1939
1940
        /* Once the internal-flush-flag has been set and codec has issued
1941
        end flag, exit encoding by returning IHEVCE_EFAIL */
1942
101k
        if(ps_ctxt->i4_internal_flush)
1943
0
        {
1944
0
            if(1 == ps_app_ctxt->ai4_out_strm_end_flag[0][0])
1945
0
                return (IHEVCE_EFAIL);
1946
0
        }
1947
1948
202k
        for(i4_res_id = 0; i4_res_id < i4_num_resolutions; i4_res_id++)
1949
101k
        {
1950
101k
            ai4_num_bitrate_instances[i4_res_id] =
1951
101k
                ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_res_id]
1952
101k
                    .i4_num_bitrate_instances;
1953
202k
            for(i4_br_id = 0; i4_br_id < ai4_num_bitrate_instances[i4_res_id]; i4_br_id++)
1954
101k
            {
1955
                /* ------- Que in free output buffer if end flag is not set ------ */
1956
101k
                ihevce_queue_out_buffer(ps_ctxt, i4_res_id, i4_br_id);
1957
101k
            }
1958
101k
        }
1959
1960
        /* --------------------------------------------------------------------- */
1961
        /*            Input Processing                                           */
1962
        /* --------------------------------------------------------------------- */
1963
101k
        if(0 == ps_ctxt->i4_flush_mode_on)
1964
101k
        {
1965
101k
            WORD32 frm_stride;
1966
101k
            WORD32 frm_width;
1967
101k
            WORD32 frm_height;
1968
101k
            WORD32 buf_id;
1969
1970
101k
            iv_input_data_ctrl_buffs_t *ps_curr_inp;
1971
101k
            WORD32 *pi4_ctrl_ptr;
1972
1973
101k
            frm_width = ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_width;
1974
101k
            frm_height = ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_height;
1975
101k
            frm_stride = ps_ctxt->i4_frm_stride;
1976
1977
            /* ---------- get a free buffer from input Q ------ */
1978
101k
            ps_curr_inp = (iv_input_data_ctrl_buffs_t *)ihevce_q_get_free_inp_data_buff(
1979
101k
                ps_interface_ctxt, &buf_id, BUFF_QUE_BLOCKING_MODE);
1980
1981
101k
            if(NULL != ps_curr_inp)
1982
101k
            {
1983
                /* if input buffer is not NULL */
1984
101k
                if(NULL != ps_inp)
1985
101k
                {
1986
101k
                    WORD32 result;
1987
1988
101k
                    pi4_ctrl_ptr = (WORD32 *)ps_curr_inp->pv_synch_ctrl_bufs;
1989
1990
                    /* ---------- set ip params ---------- */
1991
101k
                    ps_curr_inp->s_input_buf.i4_size = sizeof(iv_yuv_buf_t);
1992
101k
                    ps_curr_inp->s_input_buf.i4_y_wd = frm_width;
1993
101k
                    ps_curr_inp->s_input_buf.i4_y_ht = frm_height;
1994
101k
                    ps_curr_inp->s_input_buf.i4_y_strd = frm_stride;
1995
101k
                    ps_curr_inp->s_input_buf.i4_uv_wd = frm_width;
1996
101k
                    ps_curr_inp->s_input_buf.i4_uv_ht =
1997
101k
                        frm_height >>
1998
101k
                        ((ps_ctxt->ps_static_cfg_prms->s_src_prms.inp_chr_format == 13) ? 0 : 1);
1999
101k
                    ps_curr_inp->s_input_buf.i4_uv_strd = frm_stride;
2000
2001
101k
                    ps_curr_inp->i4_buf_id = buf_id;
2002
101k
                    ps_curr_inp->i4_inp_frm_data_valid_flag = 1;
2003
101k
                    ps_curr_inp->i4_topfield_first = 1; /* set to default */
2004
101k
                    ps_curr_inp->i4_bottom_field = ps_ctxt->i4_field_id;
2005
101k
                    ps_curr_inp->i4_inp_timestamp_low = (WORD32)(ps_inp->u8_pts & 0xFFFFFFFF);
2006
101k
                    ps_curr_inp->i4_inp_timestamp_high = (WORD32)(ps_inp->u8_pts >> 32);
2007
2008
                    /* toggle field id */
2009
101k
                    ps_ctxt->i4_field_id = !ps_ctxt->i4_field_id;
2010
2011
                    /* ---------- Introduction of Force IDR locs   ---------- */
2012
101k
                    if(ps_inp->i4_force_idr_flag)
2013
1.10k
                    {
2014
1.10k
                        *pi4_ctrl_ptr = IHEVCE_SYNCH_API_FORCE_IDR_TAG;
2015
1.10k
                        *(pi4_ctrl_ptr + 1) = 0;
2016
1.10k
                        pi4_ctrl_ptr += 2;
2017
2018
                        /* set the cmd to NA */
2019
1.10k
                        *pi4_ctrl_ptr = IHEVCE_SYNCH_API_END_TAG;
2020
2021
1.10k
                        ps_curr_inp->i4_cmd_buf_size = 4 * 3; /* 12 bytes */
2022
1.10k
                    }
2023
99.9k
                    else
2024
99.9k
                    {
2025
                        /* set the cmd to NA */
2026
99.9k
                        *pi4_ctrl_ptr = IHEVCE_SYNCH_API_END_TAG;
2027
2028
99.9k
                        ps_curr_inp->i4_cmd_buf_size = 4; /* 4 bytes */
2029
99.9k
                    }
2030
                    /* call the input copy function */
2031
101k
                    result = ihevce_copy_inp_8bit(
2032
101k
                        ps_curr_inp,
2033
101k
                        ps_inp,
2034
101k
                        ps_ctxt->ps_static_cfg_prms->s_src_prms.inp_chr_format,
2035
101k
                        ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_orig_width,
2036
101k
                        ps_ctxt->ps_static_cfg_prms->s_src_prms.i4_orig_height);
2037
2038
101k
                    if(IV_SUCCESS != result)
2039
0
                        return (IHEVCE_EFAIL);
2040
2041
101k
                    if(3 != ps_ctxt->ps_static_cfg_prms->s_config_prms.i4_rate_control_mode)
2042
74.5k
                    {
2043
                        /* Dynamic Change in bitrate not supported for multi res/MBR */
2044
                        /*** Check for change in bitrate command ***/
2045
74.5k
                        if(ps_ctxt->ai4_old_bitrate[0][0] != ps_inp->i4_curr_bitrate)
2046
4.55k
                        {
2047
4.55k
                            WORD32 buf_id;
2048
4.55k
                            WORD32 *pi4_cmd_buf;
2049
4.55k
                            iv_input_ctrl_buffs_t *ps_ctrl_buf;
2050
4.55k
                            ihevce_dyn_config_prms_t *ps_dyn_br;
2051
4.55k
                            WORD32 codec_level_index = ihevce_get_level_index(
2052
4.55k
                                ps_ctxt->ps_static_cfg_prms->s_tgt_lyr_prms.as_tgt_params[0]
2053
4.55k
                                    .i4_codec_level);
2054
4.55k
                            WORD32 max_bitrate =
2055
4.55k
                                g_as_level_data[codec_level_index].i4_max_bit_rate
2056
4.55k
                                    [ps_ctxt->ps_static_cfg_prms->s_out_strm_prms.i4_codec_tier] *
2057
4.55k
                                1000;
2058
2059
                            /* ---------- get a free buffer from command Q ------ */
2060
4.55k
                            ps_ctrl_buf = (iv_input_ctrl_buffs_t *)ihevce_q_get_free_inp_ctrl_buff(
2061
4.55k
                                ps_interface_ctxt, &buf_id, BUFF_QUE_BLOCKING_MODE);
2062
                            /* store the buffer id */
2063
4.55k
                            ps_ctrl_buf->i4_buf_id = buf_id;
2064
2065
                            /* get the buffer pointer */
2066
4.55k
                            pi4_cmd_buf = (WORD32 *)ps_ctrl_buf->pv_asynch_ctrl_bufs;
2067
2068
                            /* store the set default command, encoder should use create time prms */
2069
4.55k
                            *pi4_cmd_buf = IHEVCE_ASYNCH_API_SETBITRATE_TAG;
2070
4.55k
                            *(pi4_cmd_buf + 1) = sizeof(ihevce_dyn_config_prms_t);
2071
2072
4.55k
                            ps_dyn_br = (ihevce_dyn_config_prms_t *)(pi4_cmd_buf + 2);
2073
4.55k
                            ps_dyn_br->i4_size = sizeof(ihevce_dyn_config_prms_t);
2074
4.55k
                            ps_dyn_br->i4_tgt_br_id = 0;
2075
4.55k
                            ps_dyn_br->i4_tgt_res_id = 0;
2076
4.55k
                            ps_dyn_br->i4_new_tgt_bitrate =
2077
4.55k
                                MIN(ps_inp->i4_curr_bitrate, max_bitrate);
2078
4.55k
                            ps_dyn_br->i4_new_tgt_bitrate =
2079
4.55k
                                MAX(ps_dyn_br->i4_new_tgt_bitrate, MIN_BITRATE);
2080
4.55k
                            ps_dyn_br->i4_new_peak_bitrate =
2081
4.55k
                                MIN((ps_dyn_br->i4_new_tgt_bitrate << 1), max_bitrate);
2082
4.55k
                            pi4_cmd_buf += 2;
2083
4.55k
                            pi4_cmd_buf += (sizeof(ihevce_dyn_config_prms_t) >> 2);
2084
2085
4.55k
                            *(pi4_cmd_buf) = IHEVCE_ASYNCH_API_END_TAG;
2086
2087
4.55k
                            ps_ctrl_buf->i4_cmd_buf_size = 12 + sizeof(ihevce_dyn_config_prms_t);
2088
2089
                            /* ---------- set the buffer as produced ---------- */
2090
4.55k
                            ihevce_q_set_inp_ctrl_buff_prod(ps_interface_ctxt, buf_id);
2091
2092
4.55k
                            ps_ctxt->ai4_old_bitrate[0][0] = ps_dyn_br->i4_new_tgt_bitrate;
2093
4.55k
                        }
2094
74.5k
                    }
2095
2096
101k
                    ps_ctxt->u8_num_frames_queued++;
2097
101k
                }
2098
0
                else
2099
0
                { /* flush mode command */
2100
2101
0
                    ps_curr_inp->i4_buf_id = buf_id;
2102
2103
                    /* set the input status to invalid flag */
2104
0
                    ps_curr_inp->i4_inp_frm_data_valid_flag = 0;
2105
2106
0
                    pi4_ctrl_ptr = (WORD32 *)ps_curr_inp->pv_synch_ctrl_bufs;
2107
2108
0
                    *pi4_ctrl_ptr = IHEVCE_SYNCH_API_FLUSH_TAG;
2109
0
                    *(pi4_ctrl_ptr + 1) = 0;
2110
0
                    *(pi4_ctrl_ptr + 2) = IHEVCE_SYNCH_API_END_TAG;
2111
2112
0
                    ps_curr_inp->i4_cmd_buf_size = 4 * 3; /* 4 bytes */
2113
0
                }
2114
2115
                /* ---------- set the buffer as produced ---------- */
2116
101k
                ihevce_q_set_inp_data_buff_prod(ps_interface_ctxt, buf_id);
2117
101k
                ps_ctxt->u8_num_frames_encoded++;
2118
101k
            }
2119
0
            else
2120
0
            {
2121
                /* Enable flush-mode and internal-flush once limit according to
2122
                Eval-version is reached */
2123
0
                ps_ctxt->i4_flush_mode_on = 1;
2124
0
                ps_ctxt->i4_internal_flush = 1;
2125
0
            }
2126
101k
        }
2127
2128
        /* set encoder in flush mode if input buffer is NULL */
2129
101k
        if(0 == ps_ctxt->i4_flush_mode_on)
2130
101k
        {
2131
101k
            if(NULL == ps_inp)
2132
0
            {
2133
0
                ps_ctxt->i4_flush_mode_on = 1;
2134
0
            }
2135
101k
        }
2136
2137
101k
        if((u4_latency < ps_ctxt->u8_num_frames_queued) || (1 == ps_ctxt->i4_flush_mode_on))
2138
97.0k
        {
2139
            /* --------------------------------------------------------------------- */
2140
            /*            Output Processing                                          */
2141
            /* --------------------------------------------------------------------- */
2142
97.0k
            ihevce_receive_out_buffer(ps_ctxt, ps_out);
2143
97.0k
        }
2144
101k
    }
2145
0
    else  //Other bitrate and resolution instances
2146
0
    {
2147
0
        return IHEVCE_EFAIL;
2148
0
    }
2149
101k
    return (IHEVCE_EOK);
2150
101k
}
2151