1 /* 2 * Copyright (c) 2021, Alliance for Open Media. All rights reserved 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 12 #ifndef AOM_AV1_ENCODER_THIRDPASS_H_ 13 #define AOM_AV1_ENCODER_THIRDPASS_H_ 14 15 #include "av1/common/enums.h" 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 #include "av1/encoder/firstpass.h" 21 #include "av1/encoder/ratectrl.h" 22 #include "av1/encoder/tpl_model.h" 23 24 struct AV1_COMP; 25 26 // TODO(bohanli): optimize this number 27 #define MAX_THIRD_PASS_BUF \ 28 (AOMMAX((2 * MAX_GF_INTERVAL + 1), MAX_STATIC_GF_GROUP_LENGTH)) 29 30 // Struct to store useful information related to a GOP, in addition to what is 31 // available in the bitstream 32 typedef struct { 33 int gf_length; 34 int num_frames; 35 int use_arf; 36 } THIRD_PASS_GOP_INFO; 37 38 #if CONFIG_BITRATE_ACCURACY 39 typedef struct TPL_INFO { 40 int gf_length; 41 int tpl_ready; 42 TplTxfmStats txfm_stats_list[MAX_LENGTH_TPL_FRAME_STATS]; 43 double qstep_ratio_ls[MAX_LENGTH_TPL_FRAME_STATS]; 44 FRAME_UPDATE_TYPE update_type_list[MAX_LENGTH_TPL_FRAME_STATS]; 45 } TPL_INFO; 46 #endif // CONFIG_BITRATE_ACCURACY 47 48 typedef struct { 49 BLOCK_SIZE bsize; 50 PARTITION_TYPE partition; 51 int mi_row_start; 52 int mi_col_start; 53 int_mv mv[2]; 54 MV_REFERENCE_FRAME ref_frame[2]; 55 PREDICTION_MODE pred_mode; 56 } THIRD_PASS_MI_INFO; 57 58 // Struct to store useful information about a frame for the third pass. 59 // The members are extracted from the decoder by function get_frame_info. 60 typedef struct { 61 int width; 62 int height; 63 int mi_stride; 64 int mi_rows; 65 int mi_cols; 66 int base_q_idx; 67 int is_show_existing_frame; 68 int is_show_frame; 69 int bits_allocated; 70 int actual_bits; 71 uint64_t sse; 72 double bpm_factor; 73 FRAME_TYPE frame_type; 74 unsigned int order_hint; 75 THIRD_PASS_MI_INFO *mi_info; 76 } THIRD_PASS_FRAME_INFO; 77 78 typedef struct { 79 /* --- Input and decoding related members --- */ 80 // the input file 81 const char *input_file_name; 82 #if CONFIG_THREE_PASS 83 // input context 84 struct AvxInputContext *input_ctx; 85 #endif 86 // decoder codec context 87 aom_codec_ctx_t decoder; 88 // start of the frame in buf 89 const unsigned char *frame; 90 // end of the frame(s) in buf 91 const unsigned char *end_frame; 92 // whether we still have following frames in buf 93 int have_frame; 94 // pointer to buffer for the read frames 95 uint8_t *buf; 96 // size of data in buffer 97 size_t bytes_in_buffer; 98 // current buffer size 99 size_t buffer_size; 100 // error info pointer 101 struct aom_internal_error_info *err_info; 102 103 int this_frame_bits; 104 105 /* --- Members for third pass encoding --- */ 106 // Array to store info about each frame. 107 // frame_info[0] should point to the current frame. 108 THIRD_PASS_FRAME_INFO frame_info[MAX_THIRD_PASS_BUF]; 109 // number of frames available in frame_info 110 int frame_info_count; 111 // the end of the previous GOP (order hint) 112 int prev_gop_end; 113 THIRD_PASS_GOP_INFO gop_info; 114 } THIRD_PASS_DEC_CTX; 115 116 void av1_init_thirdpass_ctx(AV1_COMMON *cm, THIRD_PASS_DEC_CTX **ctx, 117 const char *file); 118 void av1_free_thirdpass_ctx(THIRD_PASS_DEC_CTX *ctx); 119 120 // Set the GOP structure from the twopass bitstream. 121 // TODO(bohanli): this is currently a skeleton and we only return the gop 122 // length. This function also saves all frame information in the array 123 // ctx->frame_info for this GOP. 124 void av1_set_gop_third_pass(THIRD_PASS_DEC_CTX *ctx); 125 126 // Pop one frame out of the array ctx->frame_info. This function is used to make 127 // sure that frame_info[0] always corresponds to the current frame. 128 void av1_pop_third_pass_info(THIRD_PASS_DEC_CTX *ctx); 129 130 void av1_open_second_pass_log(struct AV1_COMP *cpi, int is_read); 131 void av1_close_second_pass_log(struct AV1_COMP *cpi); 132 133 // Write the current GOP information into the second pass log file. 134 void av1_write_second_pass_gop_info(struct AV1_COMP *cpi); 135 // Write the information of the frames in this GOP into the second pass log 136 // file. 137 void av1_write_second_pass_per_frame_info(struct AV1_COMP *cpi, int gf_index); 138 139 // Read the next GOP information from the second pass log file. 140 void av1_read_second_pass_gop_info(FILE *second_pass_log_stream, 141 THIRD_PASS_GOP_INFO *gop_info, 142 struct aom_internal_error_info *error); 143 // read the information of the frames in next GOP from the second pass log file. 144 void av1_read_second_pass_per_frame_info(FILE *second_pass_log_stream, 145 THIRD_PASS_FRAME_INFO *frame_info_arr, 146 int frame_info_count, 147 struct aom_internal_error_info *error); 148 149 int av1_check_use_arf(THIRD_PASS_DEC_CTX *ctx); 150 151 // Calculate the ratio of third pass frame dimensions over second pass frame 152 // dimensions. Return them in ratio_h and ratio_w. 153 void av1_get_third_pass_ratio(THIRD_PASS_DEC_CTX *ctx, int fidx, int fheight, 154 int fwidth, double *ratio_h, double *ratio_w); 155 156 // Get the pointer to a second pass mi info, where mi_row and mi_col are the mi 157 // location in the thirdpass frame. 158 THIRD_PASS_MI_INFO *av1_get_third_pass_mi(THIRD_PASS_DEC_CTX *ctx, int fidx, 159 int mi_row, int mi_col, 160 double ratio_h, double ratio_w); 161 162 // Get the adjusted MVs of this_mi, associated with the reference frame. If no 163 // MV is found with the reference frame, INVALID_MV is returned. 164 int_mv av1_get_third_pass_adjusted_mv(THIRD_PASS_MI_INFO *this_mi, 165 double ratio_h, double ratio_w, 166 MV_REFERENCE_FRAME frame); 167 168 // Get the adjusted block size of this_mi. 169 BLOCK_SIZE av1_get_third_pass_adjusted_blk_size(THIRD_PASS_MI_INFO *this_mi, 170 double ratio_h, double ratio_w); 171 172 // Get the adjusted mi position in the third pass frame, of a given 173 // third_pass_mi. Location is returned in mi_row and mi_col. 174 void av1_third_pass_get_adjusted_mi(THIRD_PASS_MI_INFO *third_pass_mi, 175 double ratio_h, double ratio_w, int *mi_row, 176 int *mi_col); 177 178 PARTITION_TYPE av1_third_pass_get_sb_part_type(THIRD_PASS_DEC_CTX *ctx, 179 THIRD_PASS_MI_INFO *this_mi); 180 181 #if CONFIG_BITRATE_ACCURACY 182 183 void av1_pack_tpl_info(TPL_INFO *tpl_info, const GF_GROUP *gf_group, 184 const TplParams *tpl_data); 185 186 void av1_write_tpl_info(const TPL_INFO *tpl_info, FILE *log_stream, 187 struct aom_internal_error_info *error); 188 189 void av1_read_tpl_info(TPL_INFO *tpl_info, FILE *log_stream, 190 struct aom_internal_error_info *error); 191 192 #endif // CONFIG_BITRATE_ACCURACY 193 #ifdef __cplusplus 194 } // extern "C" 195 #endif 196 197 #endif // AOM_AV1_ENCODER_THIRDPASS_H_ 198