• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 rate_control_api.h
23 *
24 * \brief
25 *    This file should only contain RC API function declarations
26 *
27 * \date
28 *
29 * \author
30 *    ittiam
31 *
32 ******************************************************************************
33 */
34 
35 #ifndef _RATE_CONTROL_API_H_
36 #define _RATE_CONTROL_API_H_
37 
38 /*****************************************************************************/
39 /* Constant Macros                                                           */
40 /*****************************************************************************/
41 #define RC_OK 0
42 #define RC_FAIL -1
43 #define RC_BENIGN_ERR -2
44 
45 /*****************************************************************************/
46 /* Extern Function Declarations                                              */
47 /*****************************************************************************/
48 
49 typedef struct rate_control_api_t *rate_control_handle;
50 
51 WORD32 rate_control_num_fill_use_free_memtab(
52     rate_control_handle *pps_rate_control_api,
53     itt_memtab_t *ps_memtab,
54     ITT_FUNC_TYPE_E e_func_type);
55 
56 void initialise_rate_control(
57     rate_control_handle ps_rate_control_api,
58     rc_type_e e_rate_control_type,
59     UWORD8 u1_is_mb_level_rc_on,
60     UWORD32 u4_avg_bit_rate,
61     UWORD32 *pu4_peak_bit_rate,
62     UWORD32 u4_min_bit_rate,
63     UWORD32 u4_frame_rate,
64     UWORD32 u4_max_delay,
65     UWORD32 u4_intra_frame_interval,
66     UWORD32 u4_idr_period,
67     WORD32 *pi4_init_qp,
68     UWORD32 u4_max_vbv_buff_size,
69     WORD32 i4_max_inter_frm_int,
70     WORD32 i4_is_gop_closed,
71     WORD32 *pi4_min_max_qp,
72     WORD32 i4_use_est_intra_sad,
73     UWORD32 u4_src_ticks,
74     UWORD32 u4_tgt_ticks,
75     WORD32 i4_frame_height,
76     WORD32 i4_frame_width,
77     WORD32 i4_num_active_pic_type,
78     WORD32 i4_field_pic,
79     WORD32 i4_quality_preset,
80     WORD32 i4_lap_window,
81     WORD32 i4_initial_decoder_delay_frames,
82     float f_max_peak_rate_sustain_dur,
83     LWORD64 i8_num_frames_to_encode,
84     UWORD32 u4_min_scd_hevc_qp,
85     UWORD8 u1_bit_depth,
86     FILE *pf_rc_stat_file,
87     WORD32 i4_rc_pass,
88     void *pv_gop_stat,
89     LWORD64 i8_num_gop_mem_alloc,
90     WORD32 i4_is_infinite_gop,
91     WORD32 i4_size_of_lap_out,
92     WORD32 i4_size_of_rc_lap_out,
93     void *pv_sys_api,
94     WORD32 i4_fp_bit_alloc_in_sp,
95     WORD32 i4_num_frame_parallel,
96     WORD32 i4_capped_vbr_flag);
97 
98 /*****************************************************************************
99                          Process level API fuctions (FRAME LEVEL)
100 *****************************************************************************/
101 void flush_buf_frames(rate_control_handle ps_rate_control_api);
102 
103 void post_encode_frame_skip(rate_control_handle ps_rate_control_api, picture_type_e e_pic_type);
104 
105 void add_picture_to_stack(
106     rate_control_handle rate_control_api, WORD32 i4_enc_pic_id, WORD32 i4_rc_in_pic);
107 
108 void add_picture_to_stack_re_enc(
109     rate_control_handle rate_control_api, WORD32 i4_enc_pic_id, picture_type_e e_pic_type);
110 
111 void get_picture_details(
112     rate_control_handle rate_control_api,
113     WORD32 *pi4_pic_id,
114     WORD32 *pi4_pic_disp_order_no,
115     picture_type_e *pe_pic_type,
116     WORD32 *pi4_is_scd);
117 
118 WORD32 ihevce_rc_get_scaled_hevce_qp_q6(WORD32 i4_frame_qp_q6, UWORD8 u1_bit_depth);
119 
120 void get_bits_for_final_qp(
121     rate_control_handle ps_rate_control_api,
122     WORD32 *pi4_modelQP,
123     WORD32 *pi4_maxEbfQP,
124     LWORD64 *pi8_bits_from_finalQP,
125     WORD32 i4_clipQP,
126     WORD32 i4_frame_qp_q6,
127     WORD32 i4_cur_est_header_bits,
128     WORD32 i4_est_tex_bits,
129     WORD32 i4_buf_based_max_bits,
130     picture_type_e e_pic_type,
131     WORD32 i4_display_num);
132 
133 WORD32 model_availability(rate_control_handle rate_control_api, picture_type_e e_pic_type);
134 
135 WORD32 get_est_hdr_bits(rate_control_handle rate_control_api, picture_type_e e_pic_type);
136 
137 /* Gets the frame level Qp (q scale in q6 format)*/
138 WORD32 get_frame_level_qp(
139     rate_control_handle rate_control_api,
140     picture_type_e pic_type,
141     WORD32 i4_max_frm_bits,
142     WORD32 *pi4_cur_est_texture_bits,
143     float af_sum_weigh[MAX_PIC_TYPE][3],
144     WORD32 i4_call_type,
145     float i_to_avg_ratio,
146     frame_info_t *ps_frame_stat,
147     WORD32 i4_complexity_bin,
148     WORD32 i4_scene_num,
149     WORD32 *i4_curr_bits_estimated,
150     WORD32 *pi4_is_model_valid,
151     WORD32 *pi4_vbv_buf_max_bits,
152     WORD32 *pi4_est_tex_bits,
153     WORD32 *pi4_cur_est_header_bits,
154     WORD32 *pi4_maxEbfQP,
155     WORD32 *pi4_modelQP,
156     WORD32 *pi4_estimate_to_calc_frm_error);
157 
158 WORD32 clip_qp_based_on_prev_ref(
159     rate_control_handle rate_control_api,
160     picture_type_e e_pic_type,
161     WORD32 i4_call_type,
162     WORD32 i4_scene_num);
163 
164 /* Obtain the VBV buffer status information */
165 vbv_buf_status_e get_buffer_status(
166     rate_control_handle rate_control_api,
167     WORD32 i4_total_frame_bits, /* Total frame bits consumed */
168     picture_type_e e_pic_type,
169     WORD32 *pi4_num_bits_to_prevent_vbv_underflow);
170 
171 /* Returns previous frame estimated bits for SCD validation*/
172 WORD32 get_prev_frm_est_bits(rate_control_handle ps_rate_control_api);
173 
174 WORD32 rc_set_estimate_status(
175     rate_control_handle ps_rate_control_api,
176     WORD32 i4_tex_bits,
177     WORD32 i4_hdr_bits,
178     WORD32 i4_est_text_bits_ctr_get_qp);
179 
180 void rc_reset_pic_model(rate_control_handle ps_rate_control_api, picture_type_e pic_type);
181 
182 /*reset the flag at qp query stage itself to differentiate scd frame for qp offset*/
183 void rc_reset_first_frame_coded_flag(
184     rate_control_handle ps_rate_control_api, picture_type_e pic_type);
185 
186 /*  get an estimate of total bits to find estimate of header bits after L1 stage in pre-enc*/
187 WORD32 rc_get_scene_change_est_header_bits(
188     rate_control_handle ps_rate_control_api,
189     WORD32 i4_num_pixels,
190     WORD32 i4_fsim_lap_avg,
191     float af_sum_weigh[MAX_PIC_TYPE][3],
192     float i_to_avg_rest_ratio);
193 
194 /* Used in case when picture handling module needs to move to next frame type. This happens
195 when the get frame qp and update frame qp do not happen within a frame and when there can be
196 multiple get frame qps beofre a update. If this function is called then i4_is_pic_handling_done
197 argument in update_frame_level_info should be set to 1 else 0 */
198 void update_pic_handling_state(rate_control_handle ps_rate_control_api, picture_type_e e_pic_type);
199 
200 LWORD64 get_gop_sad(rate_control_handle ps_rate_control_api);
201 
202 LWORD64 get_gop_bits(rate_control_handle ps_rate_control_api);
203 
204 WORD32 check_if_current_GOP_is_simple(rate_control_handle ps_rate_control_api);
205 
206 /* Updates the frame level changes in the Rate control */
207 void update_frame_level_info(
208     rate_control_handle ps_rate_control_api,
209     picture_type_e e_pic_type,
210     LWORD64 *pi8_mb_type_sad, /* Frame level SAD for each type of MB[Intra/Inter] */
211     WORD32 i4_total_frame_bits, /* Total frame bits actually consumed */
212     WORD32 i4_model_updation_hdr_bits, /*header bits for model updation*/
213     WORD32 *
214         pi4_mb_type_tex_bits, /* Total texture bits consumed for each type of MB[Intra/Inter] used for model */
215     LWORD64 *pi8_tot_mb_type_qp, /* Total qp of all MBs based on mb type */
216     WORD32 *pi4_tot_mb_in_type, /* total number of mbs in each mb type */
217     WORD32 i4_avg_activity, /* Average mb activity in frame */
218     UWORD8 u1_is_scd, /* Is a scene change detected at the current frame */
219     WORD32 i4_is_it_a_skip, /* If it's a pre-encode skip */
220     WORD32 i4_intra_frm_cost, /* Sum of Intra cost for each frame */
221     WORD32
222         i4_is_pic_handling_done, /* Is pic handling [update_pic_handling_state] done before update */
223     WORD32 i4_suppress_bpic_update,
224     WORD32 i4_bits_to_be_stuffed,
225     WORD32 i4_is_pause_to_resume,
226     WORD32 i4_lap_window_comp,
227     WORD32 i4_is_end_of_gop,
228     WORD32 i4_lap_based_bits_reset,
229     frame_info_t *ps_frame_info,
230     WORD32 i4_is_rc_model_needs_to_be_updated,
231     WORD8 i1_qp_offset,
232     WORD32 i4_scene_num,
233     WORD32 i4_num_frm_enc_in_scene,
234     WORD32
235         i4_est_text_bits_ctr_update_qp); /*complexity of future lap window used to set target buffer level at end if GOP*/
236 
237 void update_frame_rc_get_frame_qp_info(
238     rate_control_handle ps_rate_control_api,
239     picture_type_e rc_pic_type,
240     WORD32 i4_is_scd,
241     WORD32 i4_is_pause_to_resume,
242     WORD32 i4_avg_frame_qp_q6,
243     WORD32 i4_suppress_bpic_update,
244     WORD32 i4_scene_num,
245     WORD32 i4_num_frm_enc_in_scene);
246 
247 void reset_rc_for_pause_to_play_transition(rate_control_handle ps_rate_control_api);
248 
249 WORD32 is_first_frame_coded(rate_control_handle ps_rate_control_api);
250 
251 void rc_put_sad(
252     rate_control_handle ps_rate_control_api,
253     WORD32 i4_cur_intra_sad,
254     WORD32 i4_cur_sad,
255     WORD32 i4_cur_pic_type);
256 
257 WORD32 rc_get_qp_for_scd_frame(
258     rate_control_handle ps_rate_control_api,
259     picture_type_e e_pic_type,
260     LWORD64 i8_satd_act_accum,
261     WORD32 i4_num_pels_in_frame,
262     WORD32 i4_est_I_pic_head_bits,
263     WORD32 i4_f_sim_lap_avg,
264     void *offline_model_coeff,
265     float i_to_avg_ratio,
266     WORD32 i4_true_scd,
267     float af_sum_weigh[MAX_PIC_TYPE][3],
268     frame_info_t *ps_frame_stat,
269     WORD32 i4_rc_2_pass,
270     WORD32 i4_is_not_an_I_pic,
271     WORD32 i4_ref_first_pass,
272     WORD32 i4_call_type,
273     WORD32 *pi4_total_bits,
274     WORD32 *i4_curr_bits_estimated,
275     WORD32 i4_use_offline_model_2pass,
276     LWORD64 *pi8_i_tex_bits,
277     float *pf_i_qs,
278     WORD32 i4_best_br_id,
279     WORD32 *pi4_estimate_to_calc_frm_error);
280 
281 void rc_set_num_scd_in_lap_window(
282     rate_control_handle ps_rate_control_api,
283     WORD32 i4_num_scd_in_lap_window,
284     WORD32 i4_num_frames_b4_scd);
285 
286 void rc_set_next_sc_i_in_rc_look_ahead(
287     rate_control_handle ps_rate_control_api, WORD32 i4_next_sc_i_in_rc_look_ahead);
288 
289 void rc_update_mismatch_error(rate_control_handle ps_rate_control_api, WORD32 i4_error_bits);
290 
291 /*temp function to verify I only model*/
292 WORD32 rc_get_qp_scene_change_bits(
293     rate_control_handle ps_rate_control_api,
294     WORD32 i4_total_bits,
295     LWORD64 i8_satd_by_act_accum,
296     WORD32 i4_num_pixel,
297     void *offline_model_coeff,
298     float f_i_to_average_rest,
299     WORD32 i4_call_type);
300 
301 WORD32 rc_get_bpp_based_scene_cut_qp(
302     rate_control_handle ps_rate_control_api,
303     picture_type_e e_pic_type,
304     WORD32 i4_num_pels_in_frame,
305     WORD32 i4_f_sim_lap,
306     float af_sum_weigh[MAX_PIC_TYPE][3],
307     WORD32 i4_call_type);
308 
309 /*****************************************************************************
310                         MB LEVEL API (just wrapper fucntions)
311 *****************************************************************************/
312 /* Intitalises frame level information for mb level qp */
313 void init_mb_rc_frame_level(
314     rate_control_handle ps_rate_control_api, UWORD8 u1_frame_qp); /* Current frame qp*/
315 
316 WORD32 get_bits_to_stuff(
317     rate_control_handle ps_rate_control_api,
318     WORD32 i4_tot_consumed_bits,
319     picture_type_e e_pic_type);
320 
321 /******************************************************************************
322                           Control Level API functions
323 Logic: The control call sets the state structure of the rate control api
324 accordingly such that the next process call would implement the same.
325 ******************************************************************************/
326 /* Re-initialise the rate control module with the same old parameters */
327 /* void re_init_rate_control(rate_control_handle ps_rate_control_api); */
328 
329 /* RC API call to change the inter frame interval */
330 void change_inter_frm_int_call(rate_control_handle ps_rate_control_api, WORD32 i4_inter_frm_int);
331 
332 /* RC API call to change the intra frame interval */
333 void change_intra_frm_int_call(rate_control_handle ps_rate_control_api, WORD32 i4_intra_frm_int);
334 
335 /* Sets the necessary changes for the new average bit rate */
336 void change_avg_bit_rate(
337     rate_control_handle ps_rate_control_api, UWORD32 u4_average_bit_rate, UWORD32 u4_peak_bit_rate);
338 
339 /* This is used for SOURCE FRAME RATE change from the application
340    use case. Target frame rate change is taken care using the
341    change_frm_rate_for_bit_alloc interface and modify frame rate
342    module */
343 void change_frame_rate(
344     rate_control_handle ps_rate_control_api,
345     UWORD32 u4_frame_rate,
346     UWORD32 u4_src_ticks,
347     UWORD32 u4_target_ticks);
348 
349 /* When the change in frame should affect only the bit_allocation
350    This makes sense when the target frame rate changes. This change
351    is gradually done with the use of modify frame rate. Refer the
352    test application for beeter usecase */
353 void change_frm_rate_for_bit_alloc(rate_control_handle ps_rate_control_api, UWORD32 u4_frame_rate);
354 
355 /* Set the init Qp values */
356 void change_init_qp(
357     rate_control_handle ps_rate_control_api, WORD32 *pi4_init_qp, WORD32 i4_scene_num);
358 
359 /* Sets the necessary changes for the new peak bit rate */
360 
361 void force_I_frame(rate_control_handle ps_rate_control_api);
362 
363 void change_min_max_qp(rate_control_handle ps_rate_control_api, WORD32 *pi4_min_max_qp);
364 
365 /********************************************************************************
366                             Getter functions
367 For getting the current state of the rate control structures
368 ********************************************************************************/
369 UWORD32 rc_get_frame_rate(rate_control_handle ps_rate_control_api);
370 UWORD32 rc_get_bit_rate(rate_control_handle ps_rate_control_api);
371 UWORD32 rc_get_intra_frame_interval(rate_control_handle ps_rate_control_api);
372 UWORD32 rc_get_inter_frame_interval(rate_control_handle ps_rate_control_api);
373 rc_type_e rc_get_rc_type(rate_control_handle ps_rate_control_api);
374 WORD32 rc_get_bits_per_frame(rate_control_handle ps_rate_control_api);
375 
376 UWORD32 rc_get_peak_bit_rate(rate_control_handle ps_rate_control_api, WORD32 i4_index);
377 UWORD32 rc_get_max_delay(rate_control_handle ps_rate_control_api);
378 UWORD32 rc_get_seq_no(rate_control_handle ps_rate_control_api);
379 
380 WORD32 rc_get_rem_bits_in_period(rate_control_handle ps_rate_control_api);
381 WORD32 rc_get_vbv_buf_fullness(rate_control_handle ps_rate_control_api);
382 WORD32 rc_get_vbv_buf_size(rate_control_handle ps_rate_control_api);
383 WORD32 rc_get_vbv_fulness_with_cur_bits(rate_control_handle ps_rate_control_api, UWORD32 u4_bits);
384 WORD32 get_rc_target_bits(rate_control_handle ps_rate_control_api);
385 WORD32 get_orig_rc_target_bits(rate_control_handle ps_rate_control_api);
386 WORD32 rc_get_prev_header_bits(rate_control_handle ps_rate_control_api, WORD32 pic_type);
387 WORD32 rc_get_prev_P_QP(rate_control_handle ps_rate_control_api, WORD32 i4_scene_num);
388 WORD32 rc_update_ppic_sad(
389     rate_control_handle ps_rate_control_api, WORD32 i4_est_sad, WORD32 i4_prev_ppic_sad);
390 void rc_get_sad(rate_control_handle ps_rate_control_api, WORD32 *pi4_sad);
391 WORD32 rc_get_ebf(rate_control_handle ps_rate_control_api);
392 void rc_init_set_ebf(rate_control_handle ps_rate_control_api, WORD32 i32_init_ebf);
393 void rc_update_prev_frame_intra_sad(
394     rate_control_handle ps_rate_control_api, WORD32 i4_intra_frame_sad);
395 WORD32 rc_get_prev_frame_intra_sad(rate_control_handle ps_rate_control_api);
396 /*TO DO: previous frame intra SAD update function can also be replaced by below function*/
397 void rc_update_prev_frame_sad(
398     rate_control_handle ps_rate_control_api, WORD32 i4_intra_frame_sad, picture_type_e e_pic_type);
399 WORD32 rc_get_prev_frame_sad(rate_control_handle ps_rate_control_api, picture_type_e e_pic_type);
400 
401 /*update fsim of lap whenever fsim is updated in rc context*/
402 void rc_put_temp_comp_lap(
403     rate_control_handle ps_rate_control_api,
404     WORD32 i4_lap_fsim,
405     LWORD64 i8_per_pixel_p_frm_hme_sad_q10,
406     picture_type_e e_pic_type);
407 
408 void rc_get_pic_distribution(
409     rate_control_handle ps_rate_control_api, WORD32 ai4_pic_type[MAX_PIC_TYPE]);
410 
411 void rc_get_actual_pic_distribution(
412     rate_control_handle ps_rate_control_api, WORD32 ai4_pic_type[MAX_PIC_TYPE]);
413 
414 void rc_reset_Kp_Kb(
415     rate_control_handle ps_rate_control_api,
416     float f_i_to_avg_rest,
417     WORD32 i4_num_active_pic_type,
418     float f_curr_hme_sad_per_pixel,
419     WORD32 *pi4_complexity_bin,
420     WORD32 i4_rc_pass);
421 
422 WORD32 rc_get_kp_kb(rate_control_handle ps_rate_control_api, picture_type_e e_pic_type);
423 WORD32 rc_get_ebf(rate_control_handle ps_rate_control_api);
424 
425 float rc_get_cur_peak_factor_2pass(rate_control_handle ps_rate_control_api);
426 float rc_get_offline_normalized_complexity(
427     WORD32 i4_intra_int, WORD32 i4_luma_pels, float f_per_pixel_complexity, WORD32 i4_pass_number);
428 
429 void rc_bit_alloc_detect_ebf_stuff_scenario(
430     rate_control_handle ps_rate_control_api,
431     WORD32 i4_num_frm_bef_scd_lap2,
432     LWORD64 i4_total_bits_est_consu_lap2,
433     WORD32 i4_max_inter_frm_int);
434 
435 LWORD64 rc_get_rbip_and_num_frames(rate_control_handle ps_rate_contro_api, WORD32 *pi4_num_frames);
436 
437 WORD32 bit_alloc_get_estimated_bits_for_pic(
438     rate_control_handle ps_rate_contro_api,
439     WORD32 i4_cur_frm_est_cl_sad,
440     WORD32 i4_prev_frm_cl_sad,
441     picture_type_e e_pic_type);
442 
443 void rc_get_max_hme_sad_per_pixel(rate_control_handle ps_rate_control_api, WORD32 i4_total_pixels);
444 
445 void rc_update_pic_distn_lap_to_rc(
446     rate_control_handle ps_rate_contro_api, WORD32 ai4_num_pic_type[MAX_PIC_TYPE]);
447 
448 void rc_set_bits_based_on_complexity(
449     rate_control_handle ps_rate_contro_api, WORD32 i4_lap_window_comp, WORD32 i4_num_frames);
450 
451 void rc_set_avg_qscale_first_pass(
452     rate_control_handle ps_rate_contro_api, float f_average_qscale_1st_pass);
453 
454 void rc_set_max_avg_qscale_first_pass(
455     rate_control_handle ps_rate_control_api, float f_max_average_qscale_1st_pass);
456 
457 void rc_set_i_to_sum_api_ba(rate_control_handle ps_rate_contro_api, float f_curr_i_to_sum);
458 
459 float rc_get_min_complexity_factor_2pass(rate_control_handle ps_rate_contro_api);
460 
461 void rc_set_p_to_i_complexity_ratio(
462     rate_control_handle ps_rate_contro_api, float f_p_to_i_comp_ratio);
463 
464 void rc_set_scd_in_period(rate_control_handle ps_rate_contro_api, WORD32 i4_scd_in_period);
465 
466 void rc_ba_get_qp_offset_offline_data(
467     rate_control_handle ps_rate_contro_api,
468     WORD32 ai4_offsets[5],
469     float f_hme_sad_per_pixel,
470     WORD32 i4_num_active_pic_type,
471     WORD32 *pi4_complexity_bin);
472 
473 float rc_api_gop_level_averagae_q_scale_without_offset(rate_control_handle ps_rate_control_api);
474 picture_type_e rc_getprev_ref_pic_type(rate_control_handle ps_rate_control_api);
475 WORD32 rc_get_actual_intra_frame_int(rate_control_handle ps_rate_control_api);
476 float rc_get_qscale_max_clip_in_second_pass(rate_control_handle ps_rate_control_api);
477 void rc_set_2pass_total_frames(
478     rate_control_handle ps_rate_control_api, WORD32 i4_total_2pass_frames);
479 void rc_set_2pass_avg_bit_rate(
480     rate_control_handle ps_rate_control_api, LWORD64 i8_2pass_avg_bit_rate);
481 
482 void rc_set_enable_look_ahead(rate_control_handle ps_rate_control_api, WORD32 i4_enable_look_ahead);
483 
484 void rc_add_est_tot(rate_control_handle ps_rate_control_api, WORD32 i4_tot_tex_bits);
485 void rc_init_buffer_info(
486     rate_control_handle ps_rate_control_api,
487     WORD32 *pi4_vbv_buffer_size,
488     WORD32 *pi4_currEbf,
489     WORD32 *pi4_maxEbf,
490     WORD32 *pi4_drain_rate);
491 
492 #endif
493