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 bit_allocation.h 23 * 24 * \brief 25 * This file contain bit processing function declarations 26 * 27 * \date 28 * 29 * \author 30 * ittiam 31 * 32 ****************************************************************************** 33 */ 34 35 #ifndef _BIT_ALLOCATION_H_ 36 #define _BIT_ALLOCATION_H_ 37 38 /*****************************************************************************/ 39 /* Constant Macros */ 40 /*****************************************************************************/ 41 #define MIN_THRESHOLD_VBV_GOP_ERROR (0.30) 42 #define MAX_THRESHOLD_VBV_GOP_ERROR (0.80) 43 #define MAX_THRESHOLD_VBV_FRM_ERROR (0.80) 44 45 /*****************************************************************************/ 46 /* Structure */ 47 /*****************************************************************************/ 48 typedef struct bit_allocation_t *bit_allocation_handle; 49 50 /*****************************************************************************/ 51 /* Function Declarations */ 52 /*****************************************************************************/ 53 WORD32 bit_allocation_num_fill_use_free_memtab( 54 bit_allocation_handle *pps_bit_allocation, 55 itt_memtab_t *ps_memtab, 56 ITT_FUNC_TYPE_E e_func_type); 57 58 void init_bit_allocation( 59 bit_allocation_handle ps_bit_allocation, 60 pic_handling_handle ps_pic_handling, 61 WORD32 i4_num_intra_frm_interval, /* num such intervals */ 62 WORD32 i4_bit_rate, /* num bits per second */ 63 WORD32 i4_frm_rate, /* num frms in 1000 seconds */ 64 WORD32 *i4_peak_bit_rate, 65 WORD32 i4_min_bitrate, /* The minimum bit rate that is to be satisfied for a gop */ 66 WORD32 i4_pels_in_frame, 67 WORD32 i4_is_hbr, 68 WORD32 i4_num_active_pic_type, 69 WORD32 i4_lap_window, 70 WORD32 i4_field_pic, 71 WORD32 rc_pass, 72 WORD32 i4_luma_pels, 73 WORD32 i4_enable_look_ahead); 74 75 LWORD64 ba_get_rbip_and_num_frames( 76 bit_allocation_handle ps_bit_allocation, 77 pic_handling_handle ps_pic_handling, 78 WORD32 *pi4_num_frames); 79 void assign_complexity_coeffs( 80 bit_allocation_handle ps_bit_allocation, float af_sum_weigh[MAX_PIC_TYPE][3]); 81 82 void init_prev_header_bits( 83 bit_allocation_handle ps_bit_allocation, pic_handling_handle ps_pic_handling); 84 /* Estimates the number of texture bits required by the current frame */ 85 WORD32 get_cur_frm_est_texture_bits( 86 bit_allocation_handle ps_bit_allocation, 87 rc_rd_model_handle *pps_rd_model, 88 est_sad_handle ps_est_sad, 89 pic_handling_handle ps_pic_handling, 90 cbr_buffer_handle ps_cbr_buffer, 91 picture_type_e e_pic_type, 92 WORD32 i4_use_model, 93 WORD32 i4_is_scd_frame, 94 WORD32 i4_call_type, 95 float i_to_avg_ratio, 96 WORD32 i4_is_model_valid); 97 98 WORD32 bit_alloc_get_intra_bits( 99 bit_allocation_handle ps_bit_allocation, 100 pic_handling_handle ps_pic_handling, 101 cbr_buffer_handle ps_cbr_buf_handling, 102 picture_type_e e_pic_type, 103 number_t *pvq_complexity_estimate, 104 WORD32 i4_is_scd, 105 float scd_ratio, 106 WORD32 i4_call_type, 107 WORD32 i4_non_I_scd, 108 float f_percent_head_bits); 109 110 /* Estimate the number of header bits required by the current frame */ 111 WORD32 112 get_cur_frm_est_header_bits(bit_allocation_handle ps_bit_allocation, picture_type_e e_pic_type); 113 114 /* Get the remaining bits allocated in the period */ 115 WORD32 get_rem_bits_in_period( 116 bit_allocation_handle ps_bit_allocation, pic_handling_handle ps_pic_handling); 117 118 WORD32 ba_get_frame_rate(bit_allocation_handle ps_bit_allocation); 119 120 WORD32 get_bits_per_frame(bit_allocation_handle ps_bit_allocation); 121 122 WORD32 ba_get_bit_rate(bit_allocation_handle ps_bit_allocation); 123 void ba_get_peak_bit_rate(bit_allocation_handle ps_bit_allocation, WORD32 *pi4_peak_bit_rate); 124 125 LWORD64 ba_get_buffer_play_bits_for_cur_gop(bit_allocation_handle ps_bit_allocation); 126 LWORD64 ba_get_gop_bits(bit_allocation_handle ps_bit_allocation); 127 LWORD64 ba_get_gop_sad(bit_allocation_handle ps_bit_allocation); 128 129 /* Updates the bit allocation module with the actual encoded values */ 130 void update_cur_frm_consumed_bits( 131 bit_allocation_handle ps_bit_allocation, 132 pic_handling_handle ps_pic_handling, 133 cbr_buffer_handle ps_cbr_buf_handle, 134 WORD32 i4_total_frame_bits, 135 WORD32 i4_model_updation_hdr_bits, 136 picture_type_e e_pic_type, 137 UWORD8 u1_is_scd, 138 WORD32 i4_last_frm_in_gop, 139 WORD32 i4_lap_comp_bits_reset, 140 WORD32 i4_suppress_bpic_update, 141 WORD32 i4_buffer_based_bit_error, 142 WORD32 i4_stuff_bits, 143 WORD32 i4_lap_window_comp, 144 rc_type_e e_rc_type, 145 WORD32 i4_num_gop, 146 WORD32 i4_is_pause_to_resume, 147 WORD32 i4_est_text_bits_ctr_update_qp, 148 WORD32 *pi4_gop_correction, 149 WORD32 *pi4_new_correction); 150 151 void check_and_update_bit_allocation( 152 bit_allocation_handle ps_bit_allocation, 153 pic_handling_handle ps_pic_handling, 154 WORD32 i4_max_bits_inflow_per_frm); 155 156 /* Based on the change in frame/bit rate update the remaining bits in period */ 157 void change_remaining_bits_in_period( 158 bit_allocation_handle ps_bit_allocation, 159 WORD32 i4_bit_rate, 160 WORD32 i4_frame_rate, 161 WORD32 *i4_peak_bit_rate); 162 163 /* Change the gop size in the middle of a current gop */ 164 void change_gop_size( 165 bit_allocation_handle ps_bit_allocation, 166 WORD32 i4_intra_frm_interval, 167 WORD32 i4_inter_frm_interval, 168 WORD32 i4_num_intra_frm_interval); 169 170 void update_rem_frms_in_period( 171 bit_allocation_handle ps_bit_allocation, 172 picture_type_e e_pic_type, 173 UWORD8 u1_is_first_frm, 174 WORD32 i4_intra_frm_interval, 175 WORD32 i4_num_intra_frm_interval); 176 177 void change_rem_bits_in_prd_at_force_I_frame( 178 bit_allocation_handle ps_bit_allocation, pic_handling_handle ps_pic_handling); 179 180 void change_ba_peak_bit_rate(bit_allocation_handle ps_bit_allocation, WORD32 *ai4_peak_bit_rate); 181 182 void init_intra_header_bits(bit_allocation_handle ps_bit_allocation, WORD32 i4_intra_header_bits); 183 WORD32 get_prev_header_bits(bit_allocation_handle ps_bit_allocation, WORD32 pic_type); 184 void set_Kp_Kb_for_hi_motion(bit_allocation_handle ps_bit_allocation); 185 186 void ba_get_qp_offset_offline_data( 187 WORD32 ai4_offsets[5], 188 WORD32 i4_ratio, 189 float f_ratio, 190 WORD32 i4_num_active_pic_type, 191 WORD32 *pi4_complexity_bin); 192 193 void reset_Kp_Kb( 194 bit_allocation_handle ps_bit_allocation, 195 float f_i_to_avg_ratio, 196 WORD32 i4_num_active_pic_type, 197 float f_hme_sad_per_pixel, 198 float f_max_hme_sad_per_pixel, 199 WORD32 *pi4_complexity_bin, 200 WORD32 i4_rc_pass); 201 202 WORD32 get_Kp_Kb(bit_allocation_handle ps_bit_allocation, picture_type_e e_pic_type); 203 204 /*get total bits for scene cut frame*/ 205 WORD32 get_scene_change_tot_frm_bits( 206 bit_allocation_handle ps_bit_allocation, 207 pic_handling_handle ps_pic_handling, 208 cbr_buffer_handle ps_cbr_buf_handling, 209 WORD32 i4_num_pixels, 210 WORD32 i4_f_sim_lap, 211 float i_to_avg_rest, 212 WORD32 i4_call_type, 213 WORD32 i4_non_I_scd, 214 WORD32 i4_is_infinite_gop); 215 216 void update_estimate_status( 217 bit_allocation_handle ps_bit_allocation, 218 WORD32 i4_est_texture_bits, 219 WORD32 i4_hdr_bits, 220 WORD32 i4_est_text_bits_ctr_get_qp); 221 222 void bit_allocation_set_num_scd_lap_window( 223 bit_allocation_handle ps_bit_allocation, 224 WORD32 i4_num_scd_in_lap_window, 225 WORD32 i4_next_sc_i_in_rc_look_ahead); 226 227 void bit_allocation_set_sc_i_in_rc_look_ahead( 228 bit_allocation_handle ps_bit_allocation, WORD32 i4_num_scd_in_lap_window); 229 230 /*updates gop based bit error entropy and rdopt estimate*/ 231 void bit_allocation_update_gop_level_bit_error( 232 bit_allocation_handle ps_bit_allocation, WORD32 i4_error_bits); 233 /* 234 The parsing of stat file is done at the end of init (by that time bit allocation init would have already happened, 235 The memory for gop stat data is alocated inside the parse stat file code. Hence the pointer has to be updated again 236 */ 237 238 void ba_init_stat_data( 239 bit_allocation_handle ps_bit_allocation, 240 pic_handling_handle ps_pic_handling, 241 void *pv_gop_stat, 242 WORD32 *pi4_pic_dist_in_cur_gop, 243 WORD32 i4_total_bits_in_period, 244 WORD32 i4_excess_bits); 245 246 void get_prev_frame_total_header_bits( 247 bit_allocation_handle ps_bit_allocation, 248 WORD32 *pi4_prev_frame_total_bits, 249 WORD32 *pi4_prev_frame_header_bits, 250 picture_type_e e_pic_type); 251 252 void rc_update_bit_distribution_gop_level_2pass( 253 bit_allocation_handle ps_bit_allocation, 254 pic_handling_handle ps_pic_handle, 255 void *pv_gop_stat, 256 rc_type_e e_rc_type, 257 WORD32 i4_num_gop, 258 WORD32 i4_start_gop_number, 259 float f_avg_qscale_first_pass, 260 WORD32 i4_max_ebf, 261 WORD32 i4_ebf, 262 LWORD64 i8_tot_bits_sequence, 263 WORD32 i4_comp_error); 264 265 LWORD64 bit_alloc_get_gop_num(bit_allocation_handle ps_bit_allocation); 266 267 float get_cur_peak_factor_2pass(bit_allocation_handle ps_bit_allocation); 268 float get_cur_min_complexity_factor_2pass(bit_allocation_handle ps_bit_allocation); 269 270 void set_2pass_total_gops(bit_allocation_handle ps_bit_allocation, WORD32 i4_num_gop); 271 WORD32 ba_get_min_bits_per_frame(bit_allocation_handle ps_bit_allocation); 272 273 void set_bit_allocation_i_frames( 274 bit_allocation_handle ps_bit_allocation, 275 cbr_buffer_handle ps_cbr_buffer, 276 pic_handling_handle ps_pic_handle, 277 WORD32 i4_lap_window_comp, 278 WORD32 i4_num_frames); 279 280 void bit_alloc_set_curr_i_to_sum_i(bit_allocation_handle ps_bit_allocation, float f_i_to_sum); 281 282 void ba_set_gop_stat_in_bit_alloc( 283 bit_allocation_handle ps_bit_allocation, void *pv_gop_stat_summary); 284 285 WORD32 ba_get_luma_pels(bit_allocation_handle ps_bit_allocation); 286 287 void overflow_avoided_summation(WORD32 *pi4_accumulator, WORD32 i4_input); 288 289 float ba_get_sum_complexity_segment_cross_peak(bit_allocation_handle ps_bit_allocation); 290 291 WORD32 ba_get_prev_frame_tot_est_bits(bit_allocation_handle ps_bit_allocation, WORD32 i4_pic); 292 293 WORD32 ba_get_prev_frame_tot_bits(bit_allocation_handle ps_bit_allocation, WORD32 i4_pic); 294 295 void ba_set_avg_qscale_first_pass( 296 bit_allocation_handle ps_bit_allocation, float f_average_qscale_1st_pass); 297 298 void ba_set_max_avg_qscale_first_pass( 299 bit_allocation_handle ps_bit_allocation, float f_average_qscale_1st_pass); 300 301 float ba_get_max_avg_qscale_first_pass(bit_allocation_handle ps_bit_allocation); 302 303 float ba_get_avg_qscale_first_pass(bit_allocation_handle ps_bit_allocation); 304 305 float ba_get_min_complexity_for_peak_br( 306 WORD32 i4_peak_bit_rate, 307 WORD32 i4_bit_rate, 308 float f_peak_rate_factor, 309 float f_max_val, 310 float f_min_val, 311 WORD32 i4_pass); 312 313 float ba_gop_info_average_qscale_gop_without_offset(bit_allocation_handle ps_bit_allocation); 314 315 float ba_get_qscale_max_clip_in_second_pass(bit_allocation_handle ps_bit_allocation); 316 317 float ba_gop_info_average_qscale_gop(bit_allocation_handle ps_bit_allocation); 318 WORD32 ba_get_frame_number_in_gop(bit_allocation_handle ps_bit_allocation); 319 320 void bit_alloc_set_2pass_total_frames( 321 bit_allocation_handle ps_bit_allocation, WORD32 i4_total_2pass_frames); 322 323 WORD32 ba_get_2pass_total_frames(bit_allocation_handle ps_bit_allocation); 324 325 WORD32 ba_get_2pass_bit_rate(bit_allocation_handle ps_bit_allocation); 326 327 void ba_set_2pass_bit_rate(bit_allocation_handle ps_bit_allocation, WORD32 i4_2pass_bit_rate); 328 329 void ba_set_2pass_avg_bit_rate( 330 bit_allocation_handle ps_bit_allocation, LWORD64 i8_2pass_avg_bit_rate); 331 332 void ba_set_enable_look_ahead(bit_allocation_handle ps_bit_allocation, WORD32 i4_enable_look_ahead); 333 #endif 334