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