Home | History | Annotate | Download | only in encoder
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2015 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 *  ih264e_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 *  - ih264e_map_rc_mem_recs_to_itt_api()
     36 *  - ih264e_map_itt_mem_rec_to_rc_mem_rec()
     37 *  - ih264e_get_rate_control_mem_tab()
     38 *
     39 * @remarks
     40 *  None
     41 *
     42 *******************************************************************************
     43 */
     44 
     45 
     46 /*****************************************************************************/
     47 /* File Includes                                                             */
     48 /*****************************************************************************/
     49 
     50 /* System include files */
     51 #include <stdio.h>
     52 #include <string.h>
     53 #include <stdlib.h>
     54 #include <assert.h>
     55 #include <stdarg.h>
     56 #include <math.h>
     57 
     58 /* User Include Files */
     59 #include "ih264e_config.h"
     60 #include "ih264_typedefs.h"
     61 #include "ih264_size_defs.h"
     62 #include "iv2.h"
     63 #include "ive2.h"
     64 #include "ime_distortion_metrics.h"
     65 #include "ime_defs.h"
     66 #include "ime_structs.h"
     67 #include "ih264e.h"
     68 #include "ithread.h"
     69 #include "ih264_defs.h"
     70 #include "ih264_debug.h"
     71 #include "ih264_macros.h"
     72 #include "ih264_platform_macros.h"
     73 #include "ih264_error.h"
     74 #include "ih264_structs.h"
     75 #include "ih264_trans_quant_itrans_iquant.h"
     76 #include "ih264_inter_pred_filters.h"
     77 #include "ih264_mem_fns.h"
     78 #include "ih264_padding.h"
     79 #include "ih264_intra_pred_filters.h"
     80 #include "ih264_deblk_edge_filters.h"
     81 #include "ih264_common_tables.h"
     82 #include "ih264_list.h"
     83 #include "ih264_cabac_tables.h"
     84 #include "ih264e_error.h"
     85 #include "ih264e_defs.h"
     86 #include "ih264e_bitstream.h"
     87 #include "irc_cntrl_param.h"
     88 #include "irc_frame_info_collector.h"
     89 #include "ih264e_rate_control.h"
     90 #include "ih264e_cabac_structs.h"
     91 #include "ih264e_structs.h"
     92 #include "ih264e_master.h"
     93 #include "ih264_buf_mgr.h"
     94 #include "ih264_dpb_mgr.h"
     95 #include "ih264e_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 "ih264e_cavlc.h"
    102 #include "ih264e_rc_mem_interface.h"
    103 #include "ih264e_time_stamp.h"
    104 #include "irc_common.h"
    105 #include "irc_rd_model.h"
    106 #include "irc_est_sad.h"
    107 #include "irc_fixed_point_error_bits.h"
    108 #include "irc_vbr_storage_vbv.h"
    109 #include "irc_picture_type.h"
    110 #include "irc_bit_allocation.h"
    111 #include "irc_mb_model_based.h"
    112 #include "irc_cbr_buffer_control.h"
    113 #include "irc_vbr_str_prms.h"
    114 #include "irc_rate_control_api.h"
    115 #include "irc_rate_control_api_structs.h"
    116 #include "ih264e_modify_frm_rate.h"
    117 
    118 
    119 /*****************************************************************************/
    120 /* Function Definitions                                                      */
    121 /*****************************************************************************/
    122 
    123 /**
    124 ******************************************************************************
    125 *
    126 * @brief This function fills memory record attributes
    127 *
    128 * @par   Description
    129 *  This function fills memory record attributes
    130 *
    131 * @param[in] ps_mem_tab
    132 *  pointer to mem records
    133 *
    134 * @param[in] u4_size
    135 *  size of the record
    136 *
    137 * @param[in] i4_alignment
    138 *  memory alignment size
    139 *
    140 * @param[in] e_usage
    141 *  usage
    142 *
    143 * @param[in] e_mem_region
    144 *  mem region
    145 *
    146 * @return void
    147 *
    148 ******************************************************************************
    149 */
    150 void fill_memtab(itt_memtab_t *ps_mem_tab,
    151                  WORD32 u4_size,
    152                  WORD32 i4_alignment,
    153                  ITT_MEM_USAGE_TYPE_E e_usage,
    154                  ITT_MEM_REGION_E e_mem_region)
    155 {
    156     /* Make the size next multiple of alignment */
    157     WORD32 i4_aligned_size   = (((u4_size) + (i4_alignment-1)) & (~(i4_alignment-1)));
    158 
    159     /* Fill the memtab */
    160     ps_mem_tab->u4_size      = i4_aligned_size;
    161     ps_mem_tab->i4_alignment = i4_alignment;
    162     ps_mem_tab->e_usage      = e_usage;
    163     ps_mem_tab->e_mem_region = e_mem_region;
    164 }
    165 
    166 /**
    167 ******************************************************************************
    168 *
    169 * @brief This function fills memory record attributes
    170 *
    171 * @par   Description
    172 *  This function fills memory record attributes
    173 *
    174 * @param[in] ps_mem_tab
    175 *  pointer to mem records
    176 *
    177 * @param[in] ptr_to_be_filled
    178 *  handle to the memory record storage space
    179 *
    180 * @param[in] e_func_type
    181 *  enum that dictates fill memory records or use memory records
    182 *
    183 * @return void
    184 *
    185 ******************************************************************************
    186 */
    187 WORD32 use_or_fill_base(itt_memtab_t *ps_mem_tab,
    188                         void **ptr_to_be_filled,
    189                         ITT_FUNC_TYPE_E e_func_type)
    190 {
    191     /* Fill base for freeing the allocated memory */
    192     if (e_func_type == FILL_BASE)
    193     {
    194         if (ptr_to_be_filled[0] != 0)
    195         {
    196             ps_mem_tab->pv_base = ptr_to_be_filled[0];
    197             return (0);
    198         }
    199         else
    200         {
    201             return (-1);
    202         }
    203     }
    204     /* obtain the allocated memory from base pointer */
    205     if (e_func_type == USE_BASE)
    206     {
    207         if (ps_mem_tab->pv_base != 0)
    208         {
    209             ptr_to_be_filled[0] = ps_mem_tab->pv_base;
    210             return (0);
    211         }
    212         else
    213         {
    214             return (-1);
    215         }
    216     }
    217     return (0);
    218 }
    219 
    220 /**
    221 ******************************************************************************
    222 *
    223 * @brief This function maps rc mem records structure to encoder lib mem records
    224 *  structure
    225 *
    226 * @par   Description
    227 *  This function maps rc mem records structure to encoder lib mem records
    228 *  structure
    229 *
    230 * @param[in]   ps_mem
    231 *  pointer to encoder lib mem records
    232 *
    233 * @param[in]   rc_memtab
    234 *  pointer to rc mem records
    235 *
    236 * @param[in]   num_mem_recs
    237 *  number of memory records
    238 *
    239 * @return      void
    240 *
    241 ******************************************************************************
    242 */
    243 void ih264e_map_rc_mem_recs_to_itt_api(iv_mem_rec_t *ps_mem,
    244                                        itt_memtab_t *rc_memtab,
    245                                        UWORD32 num_mem_recs)
    246 {
    247     UWORD32 j;
    248     UWORD32 Size, align;
    249 
    250     for (j = 0; j < num_mem_recs; j++)
    251     {
    252         Size = rc_memtab->u4_size;
    253         align = rc_memtab->i4_alignment;
    254 
    255         /* we always ask for external persistent cacheable memory */
    256         FILL_MEMTAB(ps_mem, j, Size, align, IV_EXTERNAL_CACHEABLE_PERSISTENT_MEM);
    257 
    258         rc_memtab++;
    259     }
    260 }
    261 
    262 /**
    263 *******************************************************************************
    264 *
    265 * @brief This function maps encoder lib mem records structure to RC memory
    266 * records structure
    267 *
    268 * @par   Description
    269 *  This function maps encoder lib mem records structure to RC memory
    270 *  records structure
    271 *
    272 * @param[in] ps_mem
    273 *  pointer to encoder lib mem records
    274 *
    275 * @param[in] rc_memtab
    276 *  pointer to rc mem records
    277 *
    278 * @param[in] num_mem_recs
    279 *  Number of memory records
    280 
    281 * @returns none
    282 *
    283 * @remarks
    284 *
    285 *******************************************************************************
    286 */
    287 void ih264e_map_itt_mem_rec_to_rc_mem_rec(iv_mem_rec_t *ps_mem,
    288                                           itt_memtab_t *rc_memtab,
    289                                           UWORD32 num_mem_recs)
    290 {
    291     UWORD32 i;
    292 
    293     for (i = 0; i < num_mem_recs; i++)
    294     {
    295         rc_memtab->i4_alignment = ps_mem->u4_mem_alignment;
    296         rc_memtab->u4_size = ps_mem->u4_mem_size;
    297         rc_memtab->pv_base = ps_mem->pv_base;
    298 
    299         /* only DDR memory is available */
    300         rc_memtab->e_mem_region = DDR;
    301         rc_memtab->e_usage = PERSISTENT;
    302 
    303         rc_memtab++;
    304         ps_mem++;
    305     }
    306 }
    307 
    308 /**
    309 ******************************************************************************
    310 *
    311 * @brief Get memtabs for rate control
    312 *
    313 * @par   Description
    314 *  This routine is used to Get/init memtabs for rate control
    315 *
    316 * @param[in] pv_rate_control
    317 *  pointer to rate control context (handle)
    318 *
    319 * @param[in] ps_mem
    320 *  pointer to encoder lib mem records
    321 *
    322 * @param[in] e_func_type
    323 *  enum that dictates fill memory records or Init memory records
    324 *
    325 * @return total number of mem records
    326 *
    327 ******************************************************************************
    328 */
    329 WORD32 ih264e_get_rate_control_mem_tab(void *pv_rate_control,
    330                                        iv_mem_rec_t  *ps_mem,
    331                                        ITT_FUNC_TYPE_E e_func_type)
    332 {
    333     itt_memtab_t as_itt_memtab[NUM_RC_MEMTABS];
    334     WORD32 i4_num_memtab = 0, j = 0;
    335     void *refptr2[4];
    336     void **refptr1[4];
    337     rate_control_ctxt_t *ps_rate_control = pv_rate_control;
    338 
    339     for (j = 0; j < 4; j++)
    340         refptr1[j] = &(refptr2[j]);
    341 
    342     j = 0;
    343 
    344     if (e_func_type == USE_BASE || e_func_type == FILL_BASE)
    345     {
    346         refptr1[1] = &ps_rate_control->pps_frame_time;
    347         refptr1[2] = &ps_rate_control->pps_time_stamp;
    348         refptr1[3] = &ps_rate_control->pps_pd_frm_rate;
    349         refptr1[0] = &ps_rate_control->pps_rate_control_api;
    350     }
    351 
    352     /* Get the total number of memtabs used by Rate Controller */
    353     i4_num_memtab = irc_rate_control_num_fill_use_free_memtab((rate_control_api_t **)refptr1[0], NULL, GET_NUM_MEMTAB);
    354     /* Few extra steps during init */
    355     ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    356     /* Fill the memtabs used by Rate Controller */
    357     i4_num_memtab = irc_rate_control_num_fill_use_free_memtab((rate_control_api_t **)refptr1[0],as_itt_memtab+j,e_func_type);
    358     /* Mapping ittiam memtabs to App. memtabs */
    359     ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    360     j += i4_num_memtab;
    361 
    362     /* Get the total number of memtabs used by Frame time Module */
    363     i4_num_memtab = ih264e_frame_time_get_init_free_memtab((frame_time_t **)refptr1[1], NULL, GET_NUM_MEMTAB);
    364     /* Few extra steps during init */
    365     ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    366     /* Fill the memtabs used by Frame time Module */
    367     i4_num_memtab = ih264e_frame_time_get_init_free_memtab((frame_time_t **)refptr1[1], as_itt_memtab+j, e_func_type);
    368     /* Mapping ittiam memtabs to App. memtabs */
    369     ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    370     j += i4_num_memtab;
    371 
    372     /* Get the total number of memtabs used by Time stamp Module */
    373     i4_num_memtab = ih264e_time_stamp_get_init_free_memtab((time_stamp_t **)refptr1[2], NULL, GET_NUM_MEMTAB);
    374     /* Few extra steps during init */
    375     ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    376     /* Fill the memtabs used by Time Stamp Module */
    377     i4_num_memtab = ih264e_time_stamp_get_init_free_memtab((time_stamp_t **)refptr1[2], as_itt_memtab+j, e_func_type);
    378     /* Mapping ittiam memtabs to App. memtabs */
    379     ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    380     j += i4_num_memtab;
    381 
    382     /* Get the total number of memtabs used by Frame rate Module */
    383     i4_num_memtab = ih264e_pd_frm_rate_get_init_free_memtab((pd_frm_rate_t **)refptr1[3], NULL, GET_NUM_MEMTAB);
    384     /* Few extra steps during init */
    385     ih264e_map_itt_mem_rec_to_rc_mem_rec((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    386     /* Fill the memtabs used by Frame Rate Module */
    387     i4_num_memtab = ih264e_pd_frm_rate_get_init_free_memtab((pd_frm_rate_t **)refptr1[3], as_itt_memtab+j, e_func_type);
    388     /* Mapping ittiam memtabs to App. memtabs */
    389     ih264e_map_rc_mem_recs_to_itt_api((&ps_mem[j]), as_itt_memtab+j, i4_num_memtab);
    390     j += i4_num_memtab;
    391 
    392     return j; /* Total MemTabs Needed by Rate Control Module */
    393 }
    394