summaryrefslogtreecommitdiffstats
path: root/encoder/ih264e_rc_mem_interface.c
blob: a74513a65b426af42d012b128856ca69827df4a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/

/**
******************************************************************************
* @file
*  ih264e_rc_mem_interface.c
*
* @brief
*  This file contains api function definitions for rate control memtabs
*
* @author
*  ittiam
*
* List of Functions
*  - fill_memtab()
*  - use_or_fill_base()
*  - ih264e_map_rc_mem_recs_to_itt_api()
*  - ih264e_map_itt_mem_rec_to_rc_mem_rec()
*  - ih264e_get_rate_control_mem_tab()
*
* @remarks
*  None
*
*******************************************************************************
*/


/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/

/* System include files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <math.h>

/* User Include Files */
#include "ih264e_config.h"
#include "ih264_typedefs.h"
#include "ih264_size_defs.h"
#include "iv2.h"
#include "ive2.h"
#include "ime_distortion_metrics.h"
#include "ime_defs.h"
#include "ime_structs.h"
#include "ih264e.h"
#include "ithread.h"
#include "ih264_defs.h"
#include "ih264_debug.h"
#include "ih264_macros.h"
#include "ih264_platform_macros.h"
#include "ih264_error.h"
#include "ih264_structs.h"
#include "ih264_trans_quant_itrans_iquant.h"
#include "ih264_inter_pred_filters.h"
#include "ih264_mem_fns.h"
#include "ih264_padding.h"
#include "ih264_intra_pred_filters.h"
#include "ih264_deblk_edge_filters.h"
#include "ih264_common_tables.h"
#include "ih264_list.h"
#include "ih264_cabac_tables.h"
#include "ih264e_error.h"
#include "ih264e_defs.h"
#include "ih264e_bitstream.h"
#include "irc_cntrl_param.h"
#include "irc_frame_info_collector.h"
#include "ih264e_rate_control.h"
#include "ih264e_cabac_structs.h"
#include "ih264e_structs.h"
#include "ih264e_master.h"
#include "ih264_buf_mgr.h"
#include "ih264_dpb_mgr.h"
#include "ih264e_utils.h"
#include "ih264e_platform_macros.h"
#include "ih264_cavlc_tables.h"
#include "ih264e_statistics.h"
#include "ih264e_trace.h"
#include "ih264e_fmt_conv.h"
#include "ih264e_cavlc.h"
#include "ih264e_rc_mem_interface.h"
#include "ih264e_time_stamp.h"
#include "irc_common.h"
#include "irc_rd_model.h"
#include "irc_est_sad.h"
#include "irc_fixed_point_error_bits.h"
#include "irc_vbr_storage_vbv.h"
#include "irc_picture_type.h"
#include "irc_bit_allocation.h"
#include "irc_mb_model_based.h"
#include "irc_cbr_buffer_control.h"
#include "irc_vbr_str_prms.h"
#include "irc_rate_control_api.h"
#include "irc_rate_control_api_structs.h"
#include "ih264e_modify_frm_rate.h"


/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/

/**
******************************************************************************
*
* @brief This function fills memory record attributes
*
* @par   Description
*  This function fills memory record attributes
*
* @param[in] ps_mem_tab
*  pointer to mem records
*
* @param[in] u4_size
*  size of the record
*
* @param[in] i4_alignment
*  memory alignment size
*
* @param[in] e_usage
*  usage
*
* @param[in] e_mem_region
*  mem region
*
* @return void
*
******************************************************************************
*/
void fill_memtab(itt_memtab_t *ps_mem_tab,
                 WORD32 u4_size,
                 WORD32 i4_alignment,
                 ITT_MEM_USAGE_TYPE_E e_usage,
                 ITT_MEM_REGION_E e_mem_region)
{
    /* Make the size next multiple of alignment */
    WORD32 i4_aligned_size   = (((u4_size) + (i4_alignment-1)) & (~(i4_alignment-1)));

    /* Fill the memtab */
    ps_mem_tab->u4_size      = i4_aligned_size;
    ps_mem_tab->i4_alignment = i4_alignment;
    ps_mem_tab->e_usage      = e_usage;
    ps_mem_tab->e_mem_region = e_mem_region;
}

/**
******************************************************************************
*
* @brief This function fills memory record attributes
*
* @par   Description
*  This function fills memory record attributes
*
* @param[in] ps_mem_tab
*  pointer to mem records
*
* @param[in] ptr_to_be_filled
*  handle to the memory record storage space
*
* @param[in] e_func_type
*  enum that dictates fill memory records or use memory records
*
* @return void
*
******************************************************************************
*/
WORD32 use_or_fill_base(itt_memtab_t *ps_mem_tab,
                        void **ptr_to_be_filled,
                        ITT_FUNC_TYPE_E e_func_type)
{
    /* Fill base for freeing the allocated memory */
    if (e_func_type == FILL_BASE)
    {
        if (ptr_to_be_filled[0] != 0)
        {
            ps_mem_tab->pv_base = ptr_to_be_filled[0];
            return (0);
        }
        else
        {
            return (-1);
        }
    }
    /* obtain the allocated memory from base pointer */
    if (e_func_type == USE_BASE)
    {
        if (ps_mem_tab->pv_base != 0)
        {
            ptr_to_be_filled[0] = ps_mem_tab->pv_base;
            return (0);
        }
        else
        {
            return (-1);
        }
    }
    return (0);
}

/**
******************************************************************************
*
* @brief This function maps rc mem records structure to encoder lib mem records
*  structure
*
* @par   Description
*  This function maps rc mem records structure to encoder lib mem records
*  structure
*
* @param[in]   ps_mem
*  pointer to encoder lib mem records
*
* @param[in]   rc_memtab
*  pointer to rc mem records
*
* @param[in]   num_mem_recs
*  number of memory records
*
* @return      void
*
******************************************************************************
*/
void ih264e_map_rc_mem_recs_to_itt_api(iv_mem_rec_t *ps_mem,
                                       itt_memtab_t *rc_memtab,
                                       UWORD32 num_mem_recs)
{
    UWORD32 j;
    UWORD32 Size, align;

    for (j = 0; j < num_mem_recs; j++)
    {
        Size = rc_memtab->u4_size;
        align = rc_memtab->i4_alignment;

        /* we always ask for external persistent cacheable memory */
        FILL_MEMTAB(ps_mem, j, Size, align, IV_EXTERNAL_CACHEABLE_PERSISTENT_MEM);

        rc_memtab++;
    }
}

/**
*******************************************************************************
*
* @brief This function maps encoder lib mem records structure to RC memory
* records structure
*
* @par   Description
*  This function maps encoder lib mem records structure to RC memory
*  records structure
*
* @param[in] ps_mem
*  pointer to encoder lib mem records
*
* @param[in] rc_memtab
*  pointer to rc mem records
*
* @param[in] num_mem_recs
*  Number of memory records

* @returns none
*
* @remarks
*
*******************************************************************************
*/
void ih264e_map_itt_mem_rec_to_rc_mem_rec(iv_mem_rec_t *ps_mem,
                                          itt_memtab_t *rc_memtab,
                                          UWORD32 num_mem_recs)
{
    UWORD32 i;

    for (i = 0; i < num_mem_recs; i++)
    {
        rc_memtab->i4_alignment = ps_mem->u4_mem_alignment;
        rc_memtab->u4_size = ps_mem->u4_mem_size;
        rc_memtab->pv_base = ps_mem->pv_base;

        /* only DDR memory is available */
        rc_memtab->e_mem_region = DDR;
        rc_memtab->e_usage = PERSISTENT;

        rc_memtab++;
        ps_mem++;
    }
}

/**
******************************************************************************
*
* @brief Get memtabs for rate control
*
* @par   Description
*  This routine is used to Get/init memtabs for rate control
*
* @param[in] pv_rate_control
*  pointer to rate control context (handle)
*
* @param[in] ps_mem
*  pointer to encoder lib mem records
*
* @param[in] e_func_type
*  enum that dictates fill memory records or Init memory records
*
* @return total number of mem records
*
******************************************************************************
*/
WORD32 ih264e_get_rate_control_mem_tab(void *pv_rate_control,
                                       iv_mem_rec_t  *ps_mem,
                                       ITT_FUNC_TYPE_E e_func_type)
{
    itt_memtab_t as_itt_memtab[NUM_RC_MEMTABS];
    WORD32 i4_num_memtab = 0, j = 0;
    void *refptr2[4];
    void **refptr1[4];
    rate_control_ctxt_t *ps_rate_control = pv_rate_control;

    for (j = 0; j < 4; j++)
        refptr1[j] = &(refptr2[j]);

    j = 0;

    if (e_func_type == USE_BASE || e_func_type == FILL_BASE)
    {
        refptr1[1] = &ps_rate_control->pps_frame_time;
        refptr1[2] = &ps_rate_control->pps_time_stamp;
        refptr1[3] = &ps_rate_control->pps_pd_frm_rate;
        refptr1[0] = &ps_rate_control->pps_rate_control_api;
    }

    /* Get the total number of memtabs used by Rate Controller */
    i4_num_memtab = irc_rate_control_num_fill_use_free_memtab((rate_control_api_t **)refptr1[0], NULL, GET_NUM_MEMTAB);
    /* Few extra steps during init */
    ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    /* Fill the memtabs used by Rate Controller */
    i4_num_memtab = irc_rate_control_num_fill_use_free_memtab((rate_control_api_t **)refptr1[0],as_itt_memtab+j,e_func_type);
    /* Mapping ittiam memtabs to App. memtabs */
    ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    j += i4_num_memtab;

    /* Get the total number of memtabs used by Frame time Module */
    i4_num_memtab = ih264e_frame_time_get_init_free_memtab((frame_time_t **)refptr1[1], NULL, GET_NUM_MEMTAB);
    /* Few extra steps during init */
    ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    /* Fill the memtabs used by Frame time Module */
    i4_num_memtab = ih264e_frame_time_get_init_free_memtab((frame_time_t **)refptr1[1], as_itt_memtab+j, e_func_type);
    /* Mapping ittiam memtabs to App. memtabs */
    ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    j += i4_num_memtab;

    /* Get the total number of memtabs used by Time stamp Module */
    i4_num_memtab = ih264e_time_stamp_get_init_free_memtab((time_stamp_t **)refptr1[2], NULL, GET_NUM_MEMTAB);
    /* Few extra steps during init */
    ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    /* Fill the memtabs used by Time Stamp Module */
    i4_num_memtab = ih264e_time_stamp_get_init_free_memtab((time_stamp_t **)refptr1[2], as_itt_memtab+j, e_func_type);
    /* Mapping ittiam memtabs to App. memtabs */
    ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    j += i4_num_memtab;

    /* Get the total number of memtabs used by Frame rate Module */
    i4_num_memtab = ih264e_pd_frm_rate_get_init_free_memtab((pd_frm_rate_t **)refptr1[3], NULL, GET_NUM_MEMTAB);
    /* Few extra steps during init */
    ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    /* Fill the memtabs used by Frame Rate Module */
    i4_num_memtab = ih264e_pd_frm_rate_get_init_free_memtab((pd_frm_rate_t **)refptr1[3], as_itt_memtab+j, e_func_type);
    /* Mapping ittiam memtabs to App. memtabs */
    ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    j += i4_num_memtab;

    return j; /* Total MemTabs Needed by Rate Control Module */
}