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_hle_interface.h
23 *
24 * \brief
25 *    This file contains infertace prototypes of High level enocder interafce
26 *    structure and interface functions.
27 *
28 * \date
29 *    18/09/2012
30 *
31 * \author
32 *    Ittiam
33 *
34 ******************************************************************************
35 */
36 
37 #ifndef _IHEVCE_HLE_INTERFACE_H_
38 #define _IHEVCE_HLE_INTERFACE_H_
39 
40 #include "ihevce_profile.h"
41 
42 /*****************************************************************************/
43 /* Constant Macros                                                           */
44 /*****************************************************************************/
45 #define IHEVCE_DLL
46 
47 #define DUMP_MBR_MULTI_RES_INFO 0
48 
49 #define DUMP_RC_2_PASS_DATA_BINARY_APP 1
50 /*print attributes */
51 
52 /*print everything on console */
53 #define PRINTF(v, x, y, ...) ps_sys_api->ihevce_printf(v, __VA_ARGS__)
54 
55 #define FPRINTF(v, fp, x, y, ...)                                                                  \
56     if(NULL != fp)                                                                                 \
57     {                                                                                              \
58         ps_sys_api->s_file_io_api.ihevce_fprintf(v, fp, __VA_ARGS__);                              \
59     }
60 
61 /* Semaphore attribute */
62 #define SEM_START_VALUE 1
63 #define THREAD_STACK_SIZE 0x80000
64 
65 /*****************************************************************************/
66 /* Function Macros                                                           */
67 /*****************************************************************************/
68 
69 /*****************************************************************************/
70 /* Typedefs                                                                  */
71 /*****************************************************************************/
72 
73 /*****************************************************************************/
74 /* Enums                                                                     */
75 /*****************************************************************************/
76 typedef enum
77 {
78     BUFF_QUE_NON_BLOCKING_MODE = 0,
79 
80     BUFF_QUE_BLOCKING_MODE
81 
82 } BUFF_QUE_MODES_T;
83 
84 /*****************************************************************************/
85 /* Structure                                                                 */
86 /*****************************************************************************/
87 /**
88  *  @brief  Structure to describe Process interface parameters of Encoder
89  */
90 typedef struct
91 {
92     /**
93      * Size for version tracking purpose
94      */
95     WORD32 i4_size;
96 
97     /**
98      * Flag to communicate that HLE thread int are done
99      */
100     WORD32 i4_hle_init_done;
101 
102     /**
103      * Error code communciate any error during create stage
104      */
105     WORD32 i4_error_code;
106 
107     /**
108     * GPU memory accumalator
109     */
110     WORD32 i4_gpu_mem_size;
111 
112     /**
113      *  OSAL handle
114      */
115     void *pv_osal_handle;
116 
117     /**
118      * Encoder Handle
119      */
120     void *apv_enc_hdl[IHEVCE_MAX_NUM_RESOLUTIONS];
121 
122     /**
123      *  Static parameters structure
124      */
125     ihevce_static_cfg_params_t *ps_static_cfg_prms;
126 
127     /**
128      * Memory Manager handle
129      */
130     void *pv_mem_mgr_hdl;
131 
132     /**
133      *  Input Buffer callback handle
134      */
135     void *pv_inp_cb_handle;
136 
137     /**
138      *  Ouput Buffer callback handle
139      */
140     void *pv_out_cb_handle;
141 
142     /**
143      *  Ouput Recon Buffer callback handle
144      */
145     void *pv_recon_cb_handle;
146 
147     /**
148      * Call back API to be called while the buffer for bitstream filling is done
149      */
150     IV_API_CALL_STATUS_T (*ihevce_output_strm_fill_done)
151     (void *pv_out_cb_handle, void *pv_curr_out, WORD32 i4_bitrate_instance, WORD32 i4_res_instance);
152 
153     /**
154      * Call back API to be called while the buffer for recon filling is done
155      */
156     IV_API_CALL_STATUS_T (*ihevce_output_recon_fill_done)
157     (void *pv_recon_cb_handle,
158      void *pv_curr_out,
159      WORD32 i4_bitrate_instance,
160      WORD32 i4_res_instance);
161 
162     /**
163      * Call back API to be called while freeing the input buffer
164      */
165     IV_API_CALL_STATUS_T (*ihevce_set_free_input_buff)
166     (void *pv_inp_cb_handle, iv_input_data_ctrl_buffs_t *ps_input_buf);
167 
168     /**
169      * Call back API to be called during allocation using memory manager
170      */
171     void (*ihevce_mem_alloc)(
172         void *pv_mem_mgr_hdl, ihevce_sys_api_t *ps_sys_api, iv_mem_rec_t *ps_memtab);
173 
174     /**
175      * Call back API for freeing using memory manager
176      */
177     void (*ihevce_mem_free)(void *pv_mem_mgr_hdl, iv_mem_rec_t *ps_memtab);
178 
179     /* create or run time input buffer allocation, 1: create time 0: run time*/
180     WORD32 i4_create_time_input_allocation;
181 
182     /* create or run time output buffer allocation, 1: create time 0: run time*/
183     WORD32 i4_create_time_output_allocation;
184 
185     /*Cores per resolution*/
186     WORD32 ai4_num_core_per_res[IHEVCE_MAX_NUM_RESOLUTIONS];
187 
188     /**
189     *  Error Handling callback handle
190     */
191     void *pv_cmd_err_cb_handle;
192 
193     /**
194     * Call back API to be called when errors need to be reported
195     */
196     IV_API_CALL_STATUS_T (*ihevce_cmds_error_report)
197     (void *pv_cmd_err_cb_handle, WORD32 i4_error_code, WORD32 i4_cmd_type, WORD32 i4_buf_id);
198 
199     /**
200     * Flag to indicate if ECU is enabled/disabled
201     */
202     WORD32 i4_p6_opt_enabled;
203 
204     /**
205      * profile stats
206      */
207     profile_database_t profile_hle;
208     profile_database_t profile_pre_enc_l1l2[IHEVCE_MAX_NUM_RESOLUTIONS];
209     profile_database_t profile_pre_enc_l0ipe[IHEVCE_MAX_NUM_RESOLUTIONS];
210     profile_database_t profile_enc_me[IHEVCE_MAX_NUM_RESOLUTIONS];
211     profile_database_t profile_enc[IHEVCE_MAX_NUM_RESOLUTIONS][IHEVCE_MAX_NUM_BITRATES];
212     profile_database_t profile_entropy[IHEVCE_MAX_NUM_RESOLUTIONS][IHEVCE_MAX_NUM_BITRATES];
213 
214 } ihevce_hle_ctxt_t;
215 
216 /**
217 ******************************************************************************
218  *  @brief  Indivisual Thread context structure
219 ******************************************************************************
220  */
221 typedef struct
222 {
223     /**  Unique Id associated with every frame processing thread */
224     WORD32 i4_thrd_id;
225 
226     /** pointer to encoder context structure */
227     void *pv_enc_ctxt;
228 
229     /** pointer to the hle context structure */
230     ihevce_hle_ctxt_t *ps_hle_ctxt;
231 
232 } frm_proc_thrd_ctxt_t;
233 
234 /*****************************************************************************/
235 /* Extern Variable Declarations                                              */
236 /*****************************************************************************/
237 
238 /*****************************************************************************/
239 /* Extern Function Declarations                                              */
240 /*****************************************************************************/
241 
242 /** Create API
243  *
244  * ps_hle_ctxt : Pointer to high level encoder context.
245  *    It is assumed that application before calling this API
246  *    has initialized with correct pointers for following
247  *      - pv_osal_handle
248  *      - pv_app_sem_handle
249  *      - ps_static_cfg_prms
250  *      - ihevce_mem_alloc
251  *      - ihevce_mem_free
252  *
253  * Encoder after initilaization would store the encoder handle in
254  *      - pv_enc_hdl
255  *
256  * Create Return status (success or fail) is returned
257  */
258 IHEVCE_DLL IV_API_CALL_STATUS_T ihevce_hle_interface_create(ihevce_hle_ctxt_t *ps_hle_ctxt);
259 
260 /** Query IO buffers requirements API
261  *
262  *  ps_hle_ctxt : Pointer to high level encoder context.
263  *  ps_input_bufs_req : memory to store input buffer requirements
264  *  ps_output_bufs_req : memory to store output buffer requirements
265  *
266  * Should be called only after a sucessfull create of codec instance
267  *
268  * Return status (success or fail) is returned
269  */
270 IHEVCE_DLL IV_API_CALL_STATUS_T ihevce_query_io_buf_req(
271     ihevce_hle_ctxt_t *ps_hle_ctxt,
272     iv_input_bufs_req_t *ps_input_bufs_req,
273     iv_res_layer_output_bufs_req_t *ps_res_layer_output_bufs_req,
274     iv_res_layer_recon_bufs_req_t *ps_res_layer_recon_bufs_req);
275 
276 /** Create buffer ports for procesing API
277  *
278  *  ps_hle_ctxt : Pointer to high level encoder context.
279  *  ps_input_data_ctrl_buffs_desc :
280  *       Pointer to Input (data/control) buffers details memory
281  *  ps_input_asynch_ctrl_buffs_desc :
282  *       Pointer to Input async control buffers details memory
283  *  ps_output_data_buffs_desc :
284  *      Pointer to output data buffers details memory
285  *  ps_output_status_buffs_desc:
286  *      Pointer to outtput async control buffers details memory
287  *
288  * Return status (success or fail) is returned
289  */
290 IHEVCE_DLL IV_API_CALL_STATUS_T ihevce_create_ports(
291     ihevce_hle_ctxt_t *ps_hle_ctxt,
292     iv_input_data_ctrl_buffs_desc_t *ps_input_data_ctrl_buffs_desc,
293     iv_input_asynch_ctrl_buffs_desc_t *ps_input_asynch_ctrl_buffs_desc,
294     iv_res_layer_output_data_buffs_desc_t *ps_mres_output_data_buffs_desc,
295     iv_res_layer_recon_data_buffs_desc_t *ps_mres_recon_data_buffs_desc);
296 
297 /** Processing interface layer thread API
298  *
299  *  This is the entry point for this thread
300  *  pointer to ihevce_hle_ctxt_t has to be passed
301  *  to this function as the argument
302  *
303  *  return should be a exit code (0)
304  */
305 IHEVCE_DLL WORD32 ihevce_hle_interface_thrd(void *pv_proc_intf_ctxt);
306 
307 /** Get version API
308  *
309  *  This is API to return the version number of the encoder
310  *
311  *  returns the version number string
312  */
313 IHEVCE_DLL const char *ihevce_get_encoder_version(void);
314 
315 /** Validate Encoder parameters
316  *
317  *  This is API to return the version number of the encoder
318  *
319  *  returns the version number string
320  */
321 IHEVCE_DLL WORD32 ihevce_validate_encoder_parameters(ihevce_static_cfg_params_t *ps_static_cfg_prms);
322 
323 /** Get free input frame data buffer API
324  *
325  * ps_hle_ctxt : Pointer to high level encoder context.
326  * pi4_buff_id : pointer to store the buffer id of the buffer returned.
327  * i4_blocking_mode : Blocking mode to control if the the API should wait
328  *                    for a free buffer to be available and then
329  *                    return with a valid buffer @sa BUFF_QUE_MODES_T
330  * returns NULL if no free buffer is present in queue (if non blocking mode)
331  */
332 IHEVCE_DLL void *ihevce_q_get_free_inp_data_buff(
333     ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 *pi4_buff_id, WORD32 i4_blocking_mode);
334 
335 /** Get free input control data buffer API
336  *
337  * ps_hle_ctxt : Pointer to high level encoder context.
338  * pi4_buff_id : pointer to store the buffer id of the buffer returned.
339  * i4_blocking_mode : Blocking mode to control if the the API should wait
340  *                    for a free buffer to be available and then
341  *                    return with a valid buffer @sa BUFF_QUE_MODES_T
342  * returns NULL if no free buffer is present in queue (if non blocking mode)
343  */
344 IHEVCE_DLL void *ihevce_q_get_free_inp_ctrl_buff(
345     ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 *pi4_buff_id, WORD32 i4_blocking_mode);
346 
347 IHEVCE_DLL void *ihevce_q_get_free_out_strm_buff(
348     ihevce_hle_ctxt_t *ps_hle_ctxt,
349     WORD32 *pi4_buff_id,
350     WORD32 i4_blocking_mode,
351     WORD32 i4_bitrate_instance,
352     WORD32 i4_res_instance);
353 
354 IHEVCE_DLL void *ihevce_q_get_free_out_recon_buff(
355     ihevce_hle_ctxt_t *ps_hle_ctxt,
356     WORD32 *pi4_buff_id,
357     WORD32 i4_blocking_mode,
358     WORD32 i4_bitrate_instance,
359     WORD32 i4_res_instance);
360 
361 /** Set Input frame data buffer as produced API
362  *
363  * ps_hle_ctxt : Pointer to high level encoder context.
364  * i4_buff_id : buffer id of the buffer returned during get free buf.
365  */
366 IHEVCE_DLL IV_API_CALL_STATUS_T
367     ihevce_q_set_inp_data_buff_prod(ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 i4_buff_id);
368 
369 /** Set Input control data buffer as produced API
370  *
371  * ps_hle_ctxt : Pointer to high level encoder context.
372  * i4_buff_id : buffer id of the buffer returned during get free buf.
373  */
374 IHEVCE_DLL IV_API_CALL_STATUS_T
375     ihevce_q_set_inp_ctrl_buff_prod(ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 i4_buff_id);
376 
377 IHEVCE_DLL IV_API_CALL_STATUS_T ihevce_q_set_out_strm_buff_prod(
378     ihevce_hle_ctxt_t *ps_hle_ctxt,
379     WORD32 i4_buff_id,
380     WORD32 i4_bitrate_instance_id,
381     WORD32 i4_resolution_id);
382 
383 IHEVCE_DLL IV_API_CALL_STATUS_T ihevce_q_set_out_recon_buff_prod(
384     ihevce_hle_ctxt_t *ps_hle_ctxt,
385     WORD32 i4_buff_id,
386     WORD32 i4_bitrate_instance_id,
387     WORD32 i4_resolution_id);
388 
389 /** Get next filled recon data buffer API
390  *
391  * ps_hle_ctxt : Pointer to high level encoder context.
392  * pi4_buff_id : pointer to store the buffer id of the buffer returned.
393  * i4_blocking_mode : Blocking mode to control if the the API should wait
394  *                    for a produced buffer to be available and then
395  *                    return with a valid buffer @sa BUFF_QUE_MODES_T
396  * returns NULL if no produced buffer is present in queue (if non blocking mode)
397  */
398 IHEVCE_DLL void *ihevce_q_get_filled_recon_buff(
399     ihevce_hle_ctxt_t *ps_hle_ctxt,
400     WORD32 *pi4_buff_id,
401     WORD32 i4_blocking_mode,
402     WORD32 i4_bitrate_instance_id,
403     WORD32 i4_resolution_id);
404 
405 /** Release/ Free recon buffer buffer API
406  *
407  * ps_hle_ctxt : Pointer to high level encoder context.
408  * i4_buff_id : buffer id of the buffer returned during get next buf.
409  */
410 IHEVCE_DLL IV_API_CALL_STATUS_T ihevce_q_rel_recon_buf(
411     ihevce_hle_ctxt_t *ps_hle_ctxt,
412     WORD32 i4_buff_id,
413     WORD32 i4_bitrate_instance_id,
414     WORD32 i4_resolution_id);
415 
416 /** Delete API
417  *
418  * Should be called only after the high level encoder thread exits or returns
419  */
420 IHEVCE_DLL IV_API_CALL_STATUS_T ihevce_hle_interface_delete(ihevce_hle_ctxt_t *ps_hle_ctxt);
421 
422 /** Trace API
423  *
424  * Open and Close trace file pointer.
425  */
426 IHEVCE_DLL WORD32 ihevce_trace_init(UWORD8 *pu1_file_name);
427 
428 IHEVCE_DLL WORD32 ihevce_trace_deinit(void);
429 
430 /** Header API
431  *
432  *  Get sequence headers asynchronously
433  */
434 WORD32 ihevce_entropy_encode_header(
435     ihevce_hle_ctxt_t *ps_hle_ctxt, WORD32 i4_bitrate_instance_id, WORD32 i4_resolution_id);
436 
437 #endif /* _IHEVCE_HLE_INTERFACE_H_ */
438