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 ****************************************************************************** 23 * \file ihevce_rc_interface.h 24 * 25 * \brief 26 * This file contains interface defination of HEVC Rate control library 27 * 28 * \date 29 * 18/09/2012 30 * 31 * \author 32 * Ittiam 33 * 34 ******************************************************************************/ 35 36 #ifndef _IHEVCE_RC_INTERFACE_H_ 37 #define _IHEVCE_RC_INTERFACE_H_ 38 39 /*****************************************************************************/ 40 /* Constant Macros */ 41 /*****************************************************************************/ 42 #define ES_TO_PQ_FACTOR (1.0) 43 44 /*****************************************************************************/ 45 /* Function Macros */ 46 /*****************************************************************************/ 47 48 /*****************************************************************************/ 49 /* Typedefs */ 50 /*****************************************************************************/ 51 52 /*****************************************************************************/ 53 /* Enums */ 54 /*****************************************************************************/ 55 /*call type to distinguish call from enc/ preenc stage*/ 56 typedef enum 57 { 58 ENC_GET_QP = 0, 59 PRE_ENC_GET_QP 60 } IHEVCE_RC_CALL_TYPE; 61 62 /*****************************************************************************/ 63 /* Structure */ 64 /*****************************************************************************/ 65 66 /*****************************************************************************/ 67 /* Extern Variable Declarations */ 68 /*****************************************************************************/ 69 70 /*****************************************************************************/ 71 /* Extern Function Declarations */ 72 /*****************************************************************************/ 73 WORD32 ihevce_rc_get_num_mem_recs(void); 74 75 WORD32 ihevce_rc_get_mem_recs( 76 iv_mem_rec_t *ps_mem_tab, 77 ihevce_static_cfg_params_t *ps_init_prms, 78 WORD32 mem_space, 79 ihevce_sys_api_t *ps_sys_api); 80 81 void *ihevce_rc_mem_init( 82 iv_mem_rec_t *ps_mem_tab, 83 ihevce_static_cfg_params_t *ps_init_prms, 84 WORD32 i4_bitrate_instance_id, 85 rc_quant_t *ps_rc_quant, 86 WORD32 i4_resolution_id, 87 WORD32 i4_look_ahead_frames_in_first_pass); 88 89 void ihevce_rc_init( 90 void *pv_ctxt, 91 ihevce_src_params_t *ps_run_time_src_param, 92 ihevce_tgt_params_t *ps_tgt_params, 93 rc_quant_t *ps_rc_quant, 94 ihevce_sys_api_t *ps_sys_api, 95 ihevce_lap_params_t *ps_lap_prms, 96 WORD32 i4_num_frame_parallel); 97 98 void ihevce_rc_update_pic_info( 99 void *pv_ctxt, 100 UWORD32 u4_total_bits_consumed, 101 UWORD32 u4_total_header_bits, 102 UWORD32 u4_frame_sad, 103 UWORD32 u4_frame_intra_sad, 104 IV_PICTURE_CODING_TYPE_T pic_type, 105 WORD32 i4_avg_frame_qp, 106 WORD32 i4_suppress_bpic_update, 107 WORD32 *pi4_qp_normalized_8x8_cu_sum, 108 WORD32 *pi4_8x8_cu_sum, 109 LWORD64 *pi8_sad_by_qscale, 110 ihevce_lap_output_params_t *ps_lap_out, 111 rc_lap_out_params_t *ps_rc_lap_out, 112 WORD32 i4_buf_id, 113 UWORD32 u4_open_loop_intra_sad, 114 LWORD64 i8_total_ssd_frame, 115 WORD32 i4_enc_frm_id); 116 117 WORD32 ihevce_rc_get_pic_quant( 118 void *pv_ctxt, 119 rc_lap_out_params_t *ps_rc_lap_out, 120 IHEVCE_RC_CALL_TYPE call_type, 121 WORD32 i4_enc_frm_id, 122 WORD32 i4_update_delay, 123 WORD32 *pi4_curr_bits_estimated); 124 125 WORD32 ihevce_rc_get_cu_quant(WORD32 i4_frame_qp, WORD32 i4_cu_size); 126 127 void ihevce_rc_update_cur_frm_intra_satd( 128 void *pv_ctxt, LWORD64 i8_cur_frm_intra_satd, WORD32 i4_enc_frm_id); 129 130 WORD32 ihevce_rc_get_scaled_mpeg2_qp(WORD32 i4_frame_qp, rc_quant_t *ps_rc_quant_ctxt); 131 132 WORD32 ihevce_rc_get_scaled_mpeg2_qp_q6(WORD32 i4_frame_qp, UWORD8 u1_bit_depth); 133 134 /*funtion top return hevce qp when input mpeg2 qp is in q6 format*/ 135 WORD32 ihevce_rc_get_scaled_hevce_qp_q6(WORD32 i4_frame_qp_q6, UWORD8 u1_bit_depth); 136 137 WORD32 ihevce_rc_get_scaled_hevce_qp_q3(WORD32 i4_frame_qp, UWORD8 u1_bit_depth); 138 139 WORD32 ihevce_rc_get_scaled_hevc_qp_from_qs_q3(WORD32 i4_frame_qs_q3, rc_quant_t *ps_rc_quant_ctxt); 140 141 /* Functions dependent on lap input*/ 142 WORD32 ihevce_rc_lap_get_scene_type(rc_lap_out_params_t *ps_rc_lap_out); 143 144 /*funciton that calculates scene change qp based on offline stat*/ 145 WORD32 ihevce_rc_get_new_scene_qp( 146 WORD32 i4_est_texture_bits, 147 LWORD64 i8_satd_by_act_accum, 148 WORD32 i4_variance, 149 WORD32 i4_num_pixel); 150 /* Function to be called in entropy thread to account for error between rdopt bits 151 estimate and actual bits generated in entropy thread*/ 152 void ihevce_rc_rdopt_entropy_bit_correct( 153 void *pv_rc_ctxt, WORD32 i4_cur_entropy_consumption, WORD32 i4_cur_time_stamp_low); 154 /*Funciton to get qp after L1 analysis using estimated L0 sadt/act so that L1 can happen with closer qp as that will be used by enc*/ 155 WORD32 ihevce_get_L0_est_satd_based_scd_qp( 156 void *pv_rc_ctxt, 157 rc_lap_out_params_t *ps_rc_lap_out, 158 LWORD64 i8_est_L0_satd_act, 159 float i_to_avg_rest_ratio); 160 161 /*Function to calculate L0 satd using L1 satd based on offline stat*/ 162 LWORD64 ihevce_get_L0_satd_based_on_L1( 163 LWORD64 i8_satd_by_act_L1, WORD32 i4_num_pixel, WORD32 i4_cur_q_scale); 164 165 /*Function to get qp for Lap-1 to get qp for L1 analysis*/ 166 WORD32 ihevce_rc_get_bpp_based_frame_qp(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out); 167 168 /*L1 data is registred so that L1 qp can be computed assuming previous frame data*/ 169 void ihevce_rc_register_L1_analysis_data( 170 void *pv_rc_ctxt, 171 rc_lap_out_params_t *ps_rc_lap_out, 172 LWORD64 i8_est_L0_satd_by_act, 173 LWORD64 i8_pre_intra_sad, 174 LWORD64 i8_l1_hme_sad); 175 176 /*populates qp for future frames for all possible pic type*/ 177 void ihevce_rc_cal_pre_enc_qp(void *pv_rc_ctxt); 178 179 WORD32 ihevce_rc_pre_enc_qp_query( 180 void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out, WORD32 i4_update_delay); 181 /*In flush mode L0 IPE has to wait till encoder populates pre-enc*/ 182 /*THIS FUNCTION IS MEANT TO BE CALLED WITHOUT MUTEX LOCK*/ 183 WORD32 ihevce_rc_check_is_pre_enc_qp_valid(void *pv_rc_ctxt, volatile WORD32 *pi4_force_end_flag); 184 185 float ihevce_get_i_to_avg_ratio( 186 void *pv_rc_ctxt, 187 rc_lap_out_params_t *ps_rc_lap_out, 188 WORD32 i_to_p_qp_offset, 189 WORD32 i4_offset_flag, 190 WORD32 i4_call_type, 191 WORD32 ai4_qp_offsets[4], 192 WORD32 i4_update_delay); 193 194 /*funtion to detect scene change inside LAP*/ 195 void ihevce_rc_check_non_lap_scd(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out); 196 197 void ihevce_get_dbf_buffer_size( 198 void *pv_rc_ctxt, UWORD32 *pi4_buffer_size, UWORD32 *pi4_dbf, UWORD32 *pi4_bit_rate); 199 200 void ihevce_vbv_compliance_frame_level_update( 201 void *pv_rc_ctxt, 202 WORD32 i4_bits_generated, 203 WORD32 i4_resolution_id, 204 WORD32 i4_appln_bitrate_inst, 205 UWORD32 u4_cur_cpb_removal_delay_minus1); 206 207 void ihevce_vbv_complaince_init_level(void *pv_ctxt, vui_t *ps_vui); 208 209 void ihevce_rc_register_dyn_change_bitrate( 210 void *pv_ctxt, 211 LWORD64 i8_new_bitrate, 212 LWORD64 i8_new_peak_bitrate, 213 WORD32 i4_new_rate_factor, 214 WORD32 i4_rate_control_mode); 215 LWORD64 ihevce_rc_get_new_bitrate(void *pv_ctxt); 216 217 LWORD64 ihevce_rc_get_new_peak_bitrate(void *pv_ctxt); 218 219 LWORD64 ihevce_rc_change_avg_bitrate(void *pv_ctxt); 220 221 LWORD64 ihevce_rc_change_rate_factor(void *pv_ctxt); 222 223 void get_avg_bitrate_bufsize(void *pv_ctxt, LWORD64 *pi8_bitrate, LWORD64 *pi8_ebf); 224 225 void change_bitrate_vbv_complaince(void *pv_ctxt, LWORD64 i8_new_bitrate, LWORD64 i8_buffer_size); 226 227 void ihevce_compute_temporal_complexity_reset_Kp_Kb( 228 rc_lap_out_params_t *ps_rc_lap_out, void *pv_rc_ctxt, WORD32 i4_Kp_Kb_reset_flag); 229 230 /* SGI & Enc Loop Parallelism related changes*/ 231 void ihevce_rc_interface_update( 232 void *pv_ctxt, 233 IV_PICTURE_CODING_TYPE_T pic_type, 234 rc_lap_out_params_t *ps_rc_lap_out, 235 WORD32 i4_avg_frame_hevc_qp, 236 WORD32 i4_enc_frm_id); 237 238 void ihevce_rc_store_retrive_update_info( 239 void *pv_ctxt, 240 rc_bits_sad_t *ps_rc_frame_stat, 241 WORD32 i4_enc_frm_id, 242 WORD32 bit_rate_id, 243 WORD32 i4_store_retrive, 244 WORD32 *pout_buf_id, 245 WORD32 *pi4_pic_type, 246 WORD32 *pcur_qp, 247 void *ps_lap_out, 248 void *ps_rc_lap_out); 249 250 void ihevce_set_L0_scd_qp(void *pv_rc_ctxt, WORD32 i4_scd_qp); 251 252 void rc_set_gop_inter_complexity(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out); 253 254 void rc_set_subgop_inter_complexity(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out); 255 256 void rc_set_gop_complexity_for_bit_allocation(void *pv_rc_ctxt, rc_lap_out_params_t *ps_rc_lap_out); 257 258 float rc_get_buffer_level_unclip(void *pv_rc_ctxt); 259 260 void ihevce_rc_populate_common_params( 261 ihevce_lap_output_params_t *ps_lap_out, rc_lap_out_params_t *ps_rc_lap_out); 262 #endif 263