Home | History | Annotate | Download | only in encoder
      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 *
     24 * @file ihevce_rc_structs.h
     25 *
     26 * @brief
     27 *  This file contains rc interface structures and prototypes
     28 *
     29 * @author
     30 *  Ittiam
     31 *
     32 ******************************************************************************
     33 */
     34 
     35 #ifndef _IHEVCE_RC_STRUCTS_H_
     36 #define _IHEVCE_RC_STRUCTS_H_
     37 
     38 /*****************************************************************************/
     39 /* Constant Macros                                                           */
     40 /*****************************************************************************/
     41 
     42 #define MAX_NUM_TEMPORAL_LAYERS 4
     43 #define HALF_MAX_SCENE_ARRAY_QP MAX_SCENE_NUM / 2
     44 
     45 /*moderate value of fsim to be passed when LAP is not enabled*/
     46 #define MODERATE_FSIM_VALUE 110
     47 #define MODERATE_LAP2_COMPLEXITY_Q7 25
     48 
     49 /*also present in RATE CONTROL HEADER FILE with same name*/
     50 #define MAX_LAP_COMPLEXITY_Q7 90
     51 
     52 /*value of maximum variance in content used to generate offline model.*/
     53 #define MAX_LAP_VAR 1000
     54 #define AVG_LAP_VAR 400
     55 
     56 /*buffer to store bit consumption between rdopt and entropy to calculate correction in entropy thread*/
     57 #define NUM_BUF_RDOPT_ENT_CORRECT (NUM_FRMPROC_ENTCOD_BUFS + 1)  //+(1<<FRAME_PARALLEL_LVL))
     58 
     59 /*****************************************************************************/
     60 /* Enums                                                                     */
     61 /*****************************************************************************/
     62 /**
     63 ******************************************************************************
     64  *  @brief      Enumeration for memory records requested by entropy module
     65 ******************************************************************************
     66  */
     67 typedef enum
     68 {
     69     RC_CTXT = 0,
     70     RC_QSCALE_TO_QP,
     71     RC_QP_TO_QSCALE,
     72     RC_QP_TO_QSCALE_Q_FACTOR,
     73     RC_MULTI_PASS_GOP_STAT,
     74 
     75     /* should always be the last entry */
     76     NUM_RC_MEM_RECS
     77 
     78 } IHEVCE_RC_MEM_TABS_T;
     79 
     80 /*****************************************************************************/
     81 /* Structures                                                                */
     82 /*****************************************************************************/
     83 
     84 /**
     85 ******************************************************************************
     86  *  @brief  pre enc qp queue struct
     87 ******************************************************************************
     88  */
     89 typedef struct
     90 {
     91     WORD32 ai4_quant[NUM_RC_PIC_TYPE];
     92     WORD32 i4_scd_qp;
     93     WORD32 i4_is_qp_valid;
     94 } pre_enc_qp_queue;
     95 
     96 typedef struct
     97 {
     98     LWORD64 ai8_L1_prev_I_intra_raw_satd[MAX_PIC_TYPE];
     99     LWORD64 ai8_L1_prev_pic_coarse_me_cost[MAX_PIC_TYPE];
    100     LWORD64 ai8_L1_prev_pic_coarse_me_sad[MAX_PIC_TYPE];
    101     UWORD32 au4_prev_scene_num[MAX_PIC_TYPE];
    102 } rc_L1_state_t;
    103 
    104 /**
    105 ******************************************************************************
    106  * vbv compliance testing struct
    107 ******************************************************************************
    108 */
    109 typedef struct vbv_compliance_t
    110 {
    111     /** frame rate  */
    112     float f_frame_rate;
    113 
    114     /** bit rate    */
    115     float f_bit_rate;
    116 
    117     /** current buffer level */
    118     float f_curr_buffer_level;
    119 
    120     /*current buffer level unclipped for current frame*/
    121     float f_curr_buffer_level_unclip;
    122 
    123     /** total buffer size */
    124     float f_buffer_size;
    125 
    126     /** drain rate */
    127     float f_drain_rate;
    128     /** previous cbp_removal_removal_delay minus 1**/
    129     UWORD32 u4_prev_cpb_removal_delay_minus1;
    130 
    131 } vbv_compliance_t;
    132 
    133 /* structure defined to maintain the qp's of Non reference b pictures based on reference */
    134 /* b pictures of next layer  to handle in steadystate,SCD and Non_I_SCD's. The offset is */
    135 /* based on the temporeal complexities of the sub GOP                                    */
    136 typedef struct
    137 {
    138     WORD32 i4_enc_order_num_rc;
    139 
    140     WORD32 i4_non_ref_B_pic_qp;
    141 
    142     UWORD32 u4_scene_num_rc;
    143 
    144 } non_ref_b_qp_store_t;
    145 /* structure to get high level stat from rc to adjust clip QP in case
    146 if it causes encoder buffer  overflow*/
    147 typedef struct
    148 {
    149     /*online model valid flag*/
    150     WORD32 i4_is_model_valid;
    151 
    152     /*model given QP if model is valid either offline or online
    153     else set it to INVALID_QP*/
    154     WORD32 i4_modelQP;
    155 
    156     /*final RC QP,must be always valid*/
    157     WORD32 i4_finalQP;
    158 
    159     /* QP to reach maxEbf if model is valid*/
    160     WORD32 i4_maxEbfQP;
    161 
    162     /* bits for final QP if model is valid*/
    163     LWORD64 i8_bits_from_finalQP;
    164 
    165     /*offline model flag for I scd, non i scd, I only scd*/
    166     WORD32 i4_is_offline_model_used;
    167 
    168 } rc_high_level_stat_t;
    169 
    170 typedef struct
    171 {
    172     /* START of static parameters*/
    173     rate_control_handle rc_hdl;
    174     rc_type_e e_rate_control_type;
    175     UWORD8 u1_is_mb_level_rc_on;
    176     /* bit rate to achieved across the entire file size */
    177     UWORD32 u4_avg_bit_rate;
    178     /* max possible drain rate */
    179     UWORD32 au4_peak_bit_rate[MAX_PIC_TYPE];
    180     UWORD32 u4_min_bit_rate;
    181     /* frames per 1000 seconds */
    182     UWORD32 u4_max_frame_rate;
    183     /* Buffer delay for CBR */
    184     UWORD32 u4_max_delay;
    185     /* Intraframe interval equal to GOP size */
    186     UWORD32 u4_intra_frame_interval;
    187     /* IDR period which indicates occurance of open GOP */
    188     UWORD32 u4_idr_period;
    189     /* Initial Qp array for I and P frames */
    190     WORD32 ai4_init_qp[MAX_PIC_TYPE];
    191     //0x3fffffff; /* Max VBV buffer size */
    192     UWORD32 u4_max_vbv_buff_size;
    193     /* MAx interval between I and P frame */
    194     WORD32 i4_max_inter_frm_int;
    195     /* Whether GOP is open or closed */
    196     WORD32 i4_is_gop_closed;
    197     WORD32 ai4_min_max_qp[MAX_PIC_TYPE * 2];
    198     /* Whether to use estimated SAD or Previous I frame SAD */
    199     WORD32 i4_use_est_intra_sad;
    200     UWORD32 u4_src_ticks;
    201     UWORD32 u4_tgt_ticks;
    202 
    203     WORD32 i4_auto_generate_init_qp;
    204 
    205     WORD32 i4_frame_width;
    206     WORD32 i4_frame_height;
    207 
    208     WORD32 i4_min_frame_qp;
    209     WORD32 i4_max_frame_qp;
    210 
    211     WORD32 i4_init_vbv_fullness;
    212     /* Num frames in lap window*/
    213     WORD32 i4_num_frame_in_lap_window;
    214     /** Max temporal layer the configured at init time*/
    215     WORD32 i4_max_temporal_lyr;
    216     /*Number of active picture type. Depends on max temporal reference*/
    217     WORD32 i4_num_active_pic_type;
    218     /* User defined constant qp or init qp to be used during scene cut*/
    219     WORD32 i4_init_frame_qp_user;
    220     /* To remember whether the pic type is field:1 or not:0*/
    221     WORD32 i4_field_pic;
    222     /*To convey whether top field is encoded first:1 or bottom field :0*/
    223     WORD32 i4_top_field_first;
    224     /** Quality preset to choose offline model coeff*/
    225     WORD32 i4_quality_preset;
    226     /*populate init pre enc qp based on bpp for all pictype*/
    227     WORD32 ai4_init_pre_enc_qp[MAX_PIC_TYPE];
    228     WORD32 i4_initial_decoder_delay_frames;
    229 
    230     float f_vbr_max_peak_sustain_dur;
    231     LWORD64 i8_num_frms_to_encode;
    232 
    233     WORD32 i4_min_scd_hevc_qp;
    234 
    235     UWORD8 u1_bit_depth;
    236 
    237     rc_quant_t *ps_rc_quant_ctxt;
    238 
    239     WORD32 i4_rc_pass;
    240     /*Memory allocated for storing GOP level stat*/
    241     void *pv_gop_stat;
    242 
    243     LWORD64 i8_num_gop_mem_alloc;
    244 
    245     WORD32 i4_is_infinite_gop;
    246 
    247     WORD32 ai4_offsets[5];
    248     /*End of static parameters */
    249 
    250     /* Start of parameters updated and accessed during pre-enc*/
    251     rc_L1_state_t s_l1_state_metric;
    252     /*estimate of pre-enc header bits*/
    253     LWORD64 i8_est_I_pic_header_bits;
    254     /** previous frame estimated L0 SATD/act predicted using pre-enc intra SAD*/
    255     LWORD64 ai8_prev_frame_est_L0_satd[MAX_PIC_TYPE];
    256 
    257     LWORD64 ai8_prev_frame_pre_intra_sad[MAX_PIC_TYPE];
    258 
    259     LWORD64 ai8_prev_frame_hme_sad[MAX_PIC_TYPE];
    260 
    261     /** Is previous frame intra sad available. set = 1 when atleast one frame of each picture type has been encoded*/
    262     WORD32 i4_is_est_L0_intra_sad_available;
    263 
    264     FILE *pf_stat_file;
    265 
    266     /* END of parameters updated and accessed during pre-enc */
    267 
    268     /* START of parameters updated during update call and accessed in other threads (pre enc/entropy)*/
    269 
    270     /*variables related to creation of pre enc qp queue*/
    271     pre_enc_qp_queue as_pre_enc_qp_queue[MAX_PRE_ENC_RC_DELAY];
    272     /*Remember RDOPT opt concumption, and corresponding time stamp*/
    273     WORD32 ai4_rdopt_bit_consumption_estimate[NUM_BUF_RDOPT_ENT_CORRECT];
    274 
    275     WORD32 ai4_rdopt_bit_consumption_buf_id[NUM_BUF_RDOPT_ENT_CORRECT];
    276 
    277     WORD32 i4_rdopt_bit_count;
    278 
    279     /*Remember entropy bit consumption and corresponding time stamp*/
    280     WORD32 ai4_entropy_bit_consumption[NUM_BUF_RDOPT_ENT_CORRECT];
    281 
    282     WORD32 ai4_entropy_bit_consumption_buf_id[NUM_BUF_RDOPT_ENT_CORRECT];
    283 
    284     WORD32 i4_entropy_bit_count;
    285 
    286     WORD32 i4_pre_enc_qp_read_index;
    287 
    288     WORD32 i4_pre_enc_qp_write_index;
    289 
    290     WORD32 i4_use_qp_offset_pre_enc;
    291 
    292     WORD32 i4_num_frms_from_reset;
    293     /*CAll back functions for print/write operations*/
    294     ihevce_sys_api_t *ps_sys_rc_api;
    295 
    296     LWORD64 i8_num_frame_read;
    297 
    298     LWORD64 i8_num_bit_alloc_period;
    299 
    300     vbv_compliance_t s_vbv_compliance;
    301 
    302     WORD32 i4_next_sc_i_in_rc_look_ahead;
    303 
    304     LWORD64 i8_new_bitrate;
    305         /*Set to -1 when no request. Positive value indicates pending change in bitrate request*/  //FRAME
    306 
    307     LWORD64 i8_new_peak_bitrate;
    308 
    309     WORD32 i4_num_frames_subgop;
    310 
    311     WORD32 i4_is_last_frame_scan;
    312 
    313     LWORD64 i8_total_acc_coarse_me_sad;
    314 
    315     WORD32 i4_L0_frame_qp;
    316 
    317     /** prev pic scene num of same temporal id*/
    318     UWORD32 au4_scene_num_temp_id[MAX_NUM_TEMPORAL_LAYERS];
    319 
    320     /* END of parameters updated during update call and accessed in other threads (pre enc/entropy)*/
    321 
    322     /* START of parameters to be updated at the query QP level(updation) */
    323 
    324     /** Intra frame cost exported by pre-enc IPE for current frame*/
    325     ULWORD64 ai8_cur_frm_intra_cost[MAX_NUM_ENC_LOOP_PARALLEL];
    326     /** remember prev frame intra cost*/
    327     ULWORD64 i8_prev_i_frm_cost;
    328     /* Current frame inter cost from coarse ME*/
    329     LWORD64 ai8_cur_frame_coarse_ME_cost[MAX_NUM_ENC_LOOP_PARALLEL];
    330     /** Flag for first frame so that same logic as scd can be used(offline data)*/
    331     WORD32 i4_is_first_frame_encoded;
    332     /*Flag to remember to reset I model only based on SCD detecton based on open loop SATD
    333       of two consecutive I pic*/
    334     WORD32 ai4_I_model_only_reset[MAX_NUM_ENC_LOOP_PARALLEL];
    335     /** prev pic intra cost for I pic and coarse ME cost for rest of picture types
    336         For intra L0 cost is availbale and HME cost is on L1 layer*/
    337     LWORD64 ai8_prev_frm_pre_enc_cost[MAX_PIC_TYPE];
    338     /*previous qp used encoded*/
    339     WORD32 ai4_prev_pic_hevc_qp[MAX_SCENE_NUM][MAX_PIC_TYPE];
    340 
    341     WORD32 ai4_scene_numbers[MAX_SCENE_NUM];
    342 
    343     /* END of parameters to be updated at the query QP lecvel */
    344 
    345     /* START of parameters to be maintained array for Enc loop parallelism */
    346 
    347     /** is scene cut frame at base layer*/
    348     WORD32 ai4_is_frame_scd[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    349     /*Flag to remember frames that are detected as scene cut but not made I due to another SCD following it immediately*/
    350     WORD32 ai4_is_non_I_scd_pic[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    351     /*Flag to remember pause to resume so that only P and B models can be reset*/
    352     WORD32 ai4_is_pause_to_resume[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    353     /*Frame similarity over look ahead window*/
    354     WORD32 ai4_lap_f_sim[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    355     /*Overall lap complexity including inter and intra in q7 format*/
    356     WORD32 ai4_lap_complexity_q7[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    357 
    358     float af_sum_weigh[MAX_NUM_ENC_LOOP_PARALLEL][MAX_PIC_TYPE][3];
    359 
    360     WORD32 ai4_is_cmplx_change_reset_model[MAX_NUM_ENC_LOOP_PARALLEL];
    361 
    362     WORD32 ai4_is_cmplx_change_reset_bits[MAX_NUM_ENC_LOOP_PARALLEL];
    363 
    364     float ai_to_avg_bit_ratio[MAX_NUM_ENC_LOOP_PARALLEL];
    365 
    366     WORD32 ai4_num_scd_in_lap_window[MAX_NUM_ENC_LOOP_PARALLEL];
    367 
    368     WORD32 ai4_num_frames_b4_scd[MAX_NUM_ENC_LOOP_PARALLEL];
    369 
    370     /* END of parameters to be maintained array for Enc loop parallelism */
    371 
    372     UWORD32 u4_prev_scene_num;
    373 
    374     WORD32 ai4_qp_for_previous_scene[MAX_PIC_TYPE];
    375 
    376     UWORD32 au4_prev_scene_num_pre_enc[MAX_PIC_TYPE];
    377 
    378     WORD32 ai4_qp_for_previous_scene_pre_enc[MAX_PIC_TYPE];
    379 
    380     UWORD32 u4_scene_num_est_L0_intra_sad_available;
    381 
    382     non_ref_b_qp_store_t as_non_ref_b_qp[MAX_NON_REF_B_PICS_IN_QUEUE_SGI];
    383 
    384     UWORD32 au4_prev_scene_num_multi_scene[MAX_NON_REF_B_PICS_IN_QUEUE_SGI];
    385 
    386     WORD32 ai4_qp_for_previous_scene_multi_scene[MAX_NON_REF_B_PICS_IN_QUEUE_SGI][MAX_PIC_TYPE];
    387 
    388     WORD32 i4_prev_qp_ctr;
    389 
    390     WORD32 i4_cur_scene_num;
    391 
    392     WORD32 i4_non_ref_B_ctr;
    393 
    394     float af_sum_weigh_2_pass[MAX_PIC_TYPE][3];
    395 
    396     rc_bits_sad_t as_rc_frame_stat_store[MAX_NUM_ENC_LOOP_PARALLEL]
    397                                         [IHEVCE_MAX_NUM_BITRATES];  //ELP_RC
    398 
    399     WORD32 out_buf_id[MAX_NUM_ENC_LOOP_PARALLEL][IHEVCE_MAX_NUM_BITRATES];  //ELP_RC
    400 
    401     WORD32 i4_pic_type[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    402 
    403     WORD32 cur_qp[MAX_NUM_ENC_LOOP_PARALLEL][IHEVCE_MAX_NUM_BITRATES];  //ELP_RC
    404 
    405     ihevce_lap_output_params_t as_lap_out[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    406 
    407     rc_lap_out_params_t as_rc_lap_out[MAX_NUM_ENC_LOOP_PARALLEL];  //ELP_RC
    408 
    409     WORD32 i4_complexity_bin;
    410 
    411     WORD32 i4_last_p_or_i_frame_gop;
    412 
    413     WORD32 i4_qp_at_I_frame_for_skip_sad;
    414 
    415     WORD32 i4_denominator_i_to_avg;
    416 
    417     WORD32 i4_no_more_set_rbip_for_cur_gop;
    418 
    419     WORD32 i4_num_frm_scnd_fr_alloc;
    420 
    421     WORD32 i4_last_disp_num_scanned;
    422 
    423     LWORD64 i8_l1_analysis_lap_comp;
    424 
    425     WORD32 i4_est_text_bits_ctr_get_qp;  //ELP_RC
    426 
    427     WORD32 i4_est_text_bits_ctr_update_qp;  //ELP_RC
    428 
    429     WORD32 i4_num_frame_parallel;  //ELP_RC
    430 
    431     WORD32 i4_scene_num_latest;
    432 
    433     WORD32 i4_pre_enc_rc_delay;
    434 
    435     /*Enable this falg to do bit allocation within a gop in
    436     in second pass based on first pass data*/
    437     WORD32 i4_fp_bit_alloc_in_sp;
    438 
    439     WORD32 i4_bitrate_changed;
    440 
    441     /* Flag which shows that capped vbr mode is enabled */
    442     WORD32 i4_capped_vbr_flag;
    443 
    444     rc_high_level_stat_t s_rc_high_lvl_stat;
    445 
    446     WORD32 i4_normal_inter_pic;
    447 
    448     WORD32 i4_br_id_for_2pass;
    449 
    450     WORD32 ai4_scene_num_last_pic[MAX_PIC_TYPE];
    451 
    452     WORD32 ai4_last_tw0_lyr0_pic_qp[2];
    453 } rc_context_t;
    454 
    455 /* NOTE:: Please add any new parameters accordin to the categorization as specified in the comments of */
    456 /* the structure definition. strat and end of the category are present in the defifnition*/
    457 
    458 #endif
    459