1 /******************************************************************************
2 *
3 * Copyright (C) 2022 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20
21 /**
22 ******************************************************************************
23 * @file
24 * isvce_rc_mem_interface.c
25 *
26 * @brief
27 * This file contains api function definitions for rate control memtabs
28 *
29 * @author
30 * ittiam
31 *
32 * List of Functions
33 * - fill_memtab()
34 * - use_or_fill_base()
35 * - isvce_map_rc_mem_recs_to_itt_api()
36 * - isvce_map_itt_mem_rec_to_rc_mem_rec()
37 * - isvce_get_rate_control_mem_tab()
38 *
39 * @remarks
40 * None
41 *
42 *******************************************************************************
43 */
44
45 /*****************************************************************************/
46 /* File Includes */
47 /*****************************************************************************/
48
49 /* System include files */
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <assert.h>
54 #include <stdarg.h>
55 #include <math.h>
56
57 /* User Include Files */
58 #include "ih264e_config.h"
59 #include "ih264_typedefs.h"
60 #include "ih264_size_defs.h"
61 #include "iv2.h"
62 #include "ive2.h"
63 #include "ime_distortion_metrics.h"
64 #include "ime_defs.h"
65 #include "ime_structs.h"
66 #include "isvce.h"
67 #include "ithread.h"
68 #include "isvc_defs.h"
69 #include "ih264_debug.h"
70 #include "isvc_macros.h"
71 #include "ih264_platform_macros.h"
72 #include "ih264_error.h"
73 #include "isvc_structs.h"
74 #include "isvc_trans_quant_itrans_iquant.h"
75 #include "isvc_inter_pred_filters.h"
76 #include "isvc_mem_fns.h"
77 #include "ih264_padding.h"
78 #include "ih264_intra_pred_filters.h"
79 #include "ih264_deblk_edge_filters.h"
80 #include "isvc_common_tables.h"
81 #include "ih264_list.h"
82 #include "isvc_cabac_tables.h"
83 #include "ih264e_error.h"
84 #include "isvce_defs.h"
85 #include "ih264e_bitstream.h"
86 #include "irc_mem_req_and_acq.h"
87 #include "irc_cntrl_param.h"
88 #include "irc_frame_info_collector.h"
89 #include "isvce_rate_control.h"
90 #include "isvce_cabac_structs.h"
91 #include "isvce_structs.h"
92 #include "ih264e_master.h"
93 #include "ih264_buf_mgr.h"
94 #include "ih264_dpb_mgr.h"
95 #include "isvce_utils.h"
96 #include "ih264e_platform_macros.h"
97 #include "ih264_cavlc_tables.h"
98 #include "ih264e_statistics.h"
99 #include "ih264e_trace.h"
100 #include "ih264e_fmt_conv.h"
101 #include "isvce_cavlc.h"
102 #include "ih264e_rc_mem_interface.h"
103 #include "isvce_rc_mem_interface.h"
104 #include "ih264e_time_stamp.h"
105 #include "irc_common.h"
106 #include "irc_rd_model.h"
107 #include "irc_est_sad.h"
108 #include "irc_fixed_point_error_bits.h"
109 #include "irc_vbr_storage_vbv.h"
110 #include "irc_picture_type.h"
111 #include "irc_bit_allocation.h"
112 #include "irc_mb_model_based.h"
113 #include "irc_cbr_buffer_control.h"
114 #include "irc_vbr_str_prms.h"
115 #include "irc_rate_control_api.h"
116 #include "irc_rate_control_api_structs.h"
117 #include "ih264e_modify_frm_rate.h"
118
119 /*****************************************************************************/
120 /* Function Definitions */
121 /*****************************************************************************/
122
123 /**
124 ******************************************************************************
125 *
126 * @brief This function maps rc mem records structure to encoder lib mem records
127 * structure
128 *
129 * @par Description
130 * This function maps rc mem records structure to encoder lib mem records
131 * structure
132 *
133 * @param[in] ps_mem
134 * pointer to encoder lib mem records
135 *
136 * @param[in] rc_memtab
137 * pointer to rc mem records
138 *
139 * @param[in] num_mem_recs
140 * number of memory records
141 *
142 * @return void
143 *
144 ******************************************************************************
145 */
isvce_map_rc_mem_recs_to_itt_api(iv_mem_rec_t * ps_mem,itt_memtab_t * rc_memtab,UWORD32 num_mem_recs)146 static void isvce_map_rc_mem_recs_to_itt_api(iv_mem_rec_t *ps_mem, itt_memtab_t *rc_memtab,
147 UWORD32 num_mem_recs)
148 {
149 UWORD32 j;
150 UWORD32 Size, align;
151
152 for(j = 0; j < num_mem_recs; j++)
153 {
154 Size = rc_memtab->u4_size;
155 align = rc_memtab->i4_alignment;
156
157 /* we always ask for external persistent cacheable memory */
158 FILL_MEMTAB(ps_mem, j, Size, align, IV_EXTERNAL_CACHEABLE_PERSISTENT_MEM);
159
160 rc_memtab++;
161 }
162 }
163
164 /**
165 *******************************************************************************
166 *
167 * @brief This function maps encoder lib mem records structure to RC memory
168 * records structure
169 *
170 * @par Description
171 * This function maps encoder lib mem records structure to RC memory
172 * records structure
173 *
174 * @param[in] ps_mem
175 * pointer to encoder lib mem records
176 *
177 * @param[in] rc_memtab
178 * pointer to rc mem records
179 *
180 * @param[in] num_mem_recs
181 * Number of memory records
182
183 * @returns none
184 *
185 * @remarks
186 *
187 *******************************************************************************
188 */
isvce_map_itt_mem_rec_to_rc_mem_rec(iv_mem_rec_t * ps_mem,itt_memtab_t * rc_memtab,UWORD32 num_mem_recs)189 static void isvce_map_itt_mem_rec_to_rc_mem_rec(iv_mem_rec_t *ps_mem, itt_memtab_t *rc_memtab,
190 UWORD32 num_mem_recs)
191 {
192 UWORD32 i;
193
194 for(i = 0; i < num_mem_recs; i++)
195 {
196 rc_memtab->i4_alignment = ps_mem->u4_mem_alignment;
197 rc_memtab->u4_size = ps_mem->u4_mem_size;
198 rc_memtab->pv_base = ps_mem->pv_base;
199
200 /* only DDR memory is available */
201 rc_memtab->e_mem_region = DDR;
202 rc_memtab->e_usage = PERSISTENT;
203
204 rc_memtab++;
205 ps_mem++;
206 }
207 }
208
209 /**
210 ******************************************************************************
211 *
212 * @brief Get memtabs for rate control
213 *
214 * @par Description
215 * This routine is used to Get/init memtabs for rate control
216 *
217 * @param[in] pv_rate_control
218 * pointer to rate control context (handle)
219 *
220 * @param[in] ps_mem
221 * pointer to encoder lib mem records
222 *
223 * @param[in] e_func_type
224 * enum that dictates fill memory records or Init memory records
225 *
226 * @return total number of mem records
227 *
228 ******************************************************************************
229 */
isvce_get_rate_control_mem_tab(void * pv_rate_control,iv_mem_rec_t * ps_mem,ITT_FUNC_TYPE_E e_func_type)230 WORD32 isvce_get_rate_control_mem_tab(void *pv_rate_control, iv_mem_rec_t *ps_mem,
231 ITT_FUNC_TYPE_E e_func_type)
232 {
233 itt_memtab_t as_itt_memtab[NUM_SVCE_RC_MEMTABS];
234 WORD32 i4_num_memtab = 0, j = 0;
235 void *refptr2[RC_MEM_CNT];
236 void **refptr1[RC_MEM_CNT];
237 isvce_rate_control_ctxt_t *ps_rate_control = pv_rate_control;
238
239 for(j = 0; j < RC_MEM_CNT; j++) refptr1[j] = &(refptr2[j]);
240
241 j = 0;
242
243 if(e_func_type == USE_BASE || e_func_type == FILL_BASE)
244 {
245 refptr1[RC_MEM_FRAME_TIME] = &ps_rate_control->pps_frame_time;
246 refptr1[RC_MEM_TIME_STAMP] = &ps_rate_control->pps_time_stamp;
247 refptr1[RC_MEM_FRAME_RATE] = &ps_rate_control->pps_pd_frm_rate;
248 refptr1[RC_MEM_API_L0] = (void **) &ps_rate_control->apps_rate_control_api[0];
249 refptr1[RC_MEM_API_L1] = (void **) &ps_rate_control->apps_rate_control_api[1];
250 refptr1[RC_MEM_API_L2] = (void **) &ps_rate_control->apps_rate_control_api[2];
251 }
252
253 /* Get the total number of memtabs used by Frame time Module */
254 i4_num_memtab = ih264e_frame_time_get_init_free_memtab(
255 (frame_time_t **) refptr1[RC_MEM_FRAME_TIME], NULL, GET_NUM_MEMTAB);
256 /* Few extra steps during init */
257 isvce_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
258 /* Fill the memtabs used by Frame time Module */
259 i4_num_memtab = ih264e_frame_time_get_init_free_memtab(
260 (frame_time_t **) refptr1[RC_MEM_FRAME_TIME], as_itt_memtab + j, e_func_type);
261 /* Mapping ittiam memtabs to App. memtabs */
262 isvce_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
263 j += i4_num_memtab;
264
265 /* Get the total number of memtabs used by Time stamp Module */
266 i4_num_memtab = ih264e_time_stamp_get_init_free_memtab(
267 (time_stamp_t **) refptr1[RC_MEM_TIME_STAMP], NULL, GET_NUM_MEMTAB);
268 /* Few extra steps during init */
269 isvce_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
270 /* Fill the memtabs used by Time Stamp Module */
271 i4_num_memtab = ih264e_time_stamp_get_init_free_memtab(
272 (time_stamp_t **) refptr1[RC_MEM_TIME_STAMP], as_itt_memtab + j, e_func_type);
273 /* Mapping ittiam memtabs to App. memtabs */
274 isvce_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
275 j += i4_num_memtab;
276
277 /* Get the total number of memtabs used by Frame rate Module */
278 i4_num_memtab = ih264e_pd_frm_rate_get_init_free_memtab(
279 (pd_frm_rate_t **) refptr1[RC_MEM_FRAME_RATE], NULL, GET_NUM_MEMTAB);
280 /* Few extra steps during init */
281 isvce_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
282 /* Fill the memtabs used by Frame Rate Module */
283 i4_num_memtab = ih264e_pd_frm_rate_get_init_free_memtab(
284 (pd_frm_rate_t **) refptr1[RC_MEM_FRAME_RATE], as_itt_memtab + j, e_func_type);
285 /* Mapping ittiam memtabs to App. memtabs */
286 isvce_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
287 j += i4_num_memtab;
288
289 /* Get the total number of memtabs used by Rate Controller */
290 i4_num_memtab = irc_rate_control_num_fill_use_free_memtab(
291 (rate_control_api_t **) refptr1[RC_MEM_API_L0], NULL, GET_NUM_MEMTAB);
292 /* Few extra steps during init */
293 isvce_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
294 /* Fill the memtabs used by Rate Controller */
295 i4_num_memtab = irc_rate_control_num_fill_use_free_memtab(
296 (rate_control_api_t **) refptr1[RC_MEM_API_L0], as_itt_memtab + j, e_func_type);
297 /* Mapping ittiam memtabs to App. memtabs */
298 isvce_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
299 j += i4_num_memtab;
300
301 /* Get the total number of memtabs used by Rate Controller */
302 i4_num_memtab = irc_rate_control_num_fill_use_free_memtab(
303 (rate_control_api_t **) refptr1[RC_MEM_API_L1], NULL, GET_NUM_MEMTAB);
304 /* Few extra steps during init */
305 isvce_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
306 /* Fill the memtabs used by Rate Controller */
307 i4_num_memtab = irc_rate_control_num_fill_use_free_memtab(
308 (rate_control_api_t **) refptr1[RC_MEM_API_L1], as_itt_memtab + j, e_func_type);
309 /* Mapping ittiam memtabs to App. memtabs */
310 isvce_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
311 j += i4_num_memtab;
312
313 /* Get the total number of memtabs used by Rate Controller */
314 i4_num_memtab = irc_rate_control_num_fill_use_free_memtab(
315 (rate_control_api_t **) refptr1[RC_MEM_API_L2], NULL, GET_NUM_MEMTAB);
316 /* Few extra steps during init */
317 isvce_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
318 /* Fill the memtabs used by Rate Controller */
319 i4_num_memtab = irc_rate_control_num_fill_use_free_memtab(
320 (rate_control_api_t **) refptr1[RC_MEM_API_L2], as_itt_memtab + j, e_func_type);
321 /* Mapping ittiam memtabs to App. memtabs */
322 isvce_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab + j, i4_num_memtab);
323 j += i4_num_memtab;
324
325 return j; /* Total MemTabs Needed by Rate Control Module */
326 }
327