1*77c1e3ccSAndroid Build Coastguard Worker /* 2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2019, 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_PASS2_STRATEGY_H_ 13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_PASS2_STRATEGY_H_ 14*77c1e3ccSAndroid Build Coastguard Worker 15*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus 16*77c1e3ccSAndroid Build Coastguard Worker extern "C" { 17*77c1e3ccSAndroid Build Coastguard Worker #endif 18*77c1e3ccSAndroid Build Coastguard Worker 19*77c1e3ccSAndroid Build Coastguard Worker struct AV1_COMP; 20*77c1e3ccSAndroid Build Coastguard Worker struct EncodeFrameParams; 21*77c1e3ccSAndroid Build Coastguard Worker 22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h" 23*77c1e3ccSAndroid Build Coastguard Worker 24*77c1e3ccSAndroid Build Coastguard Worker /*! 25*77c1e3ccSAndroid Build Coastguard Worker * \brief accumulated stats and features in a gf group 26*77c1e3ccSAndroid Build Coastguard Worker */ 27*77c1e3ccSAndroid Build Coastguard Worker typedef struct { 28*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */ 29*77c1e3ccSAndroid Build Coastguard Worker double gf_group_err; 30*77c1e3ccSAndroid Build Coastguard Worker double gf_group_raw_error; 31*77c1e3ccSAndroid Build Coastguard Worker double gf_group_skip_pct; 32*77c1e3ccSAndroid Build Coastguard Worker double gf_group_inactive_zone_rows; 33*77c1e3ccSAndroid Build Coastguard Worker 34*77c1e3ccSAndroid Build Coastguard Worker double mv_ratio_accumulator; 35*77c1e3ccSAndroid Build Coastguard Worker double decay_accumulator; 36*77c1e3ccSAndroid Build Coastguard Worker double zero_motion_accumulator; 37*77c1e3ccSAndroid Build Coastguard Worker double loop_decay_rate; 38*77c1e3ccSAndroid Build Coastguard Worker double last_loop_decay_rate; 39*77c1e3ccSAndroid Build Coastguard Worker double this_frame_mv_in_out; 40*77c1e3ccSAndroid Build Coastguard Worker double mv_in_out_accumulator; 41*77c1e3ccSAndroid Build Coastguard Worker double abs_mv_in_out_accumulator; 42*77c1e3ccSAndroid Build Coastguard Worker 43*77c1e3ccSAndroid Build Coastguard Worker double avg_sr_coded_error; 44*77c1e3ccSAndroid Build Coastguard Worker double avg_pcnt_second_ref; 45*77c1e3ccSAndroid Build Coastguard Worker double avg_new_mv_count; 46*77c1e3ccSAndroid Build Coastguard Worker double avg_wavelet_energy; 47*77c1e3ccSAndroid Build Coastguard Worker double avg_raw_err_stdev; 48*77c1e3ccSAndroid Build Coastguard Worker int non_zero_stdev_count; 49*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */ 50*77c1e3ccSAndroid Build Coastguard Worker } GF_GROUP_STATS; 51*77c1e3ccSAndroid Build Coastguard Worker 52*77c1e3ccSAndroid Build Coastguard Worker /*! 53*77c1e3ccSAndroid Build Coastguard Worker * \brief accumulated stats and features for a frame 54*77c1e3ccSAndroid Build Coastguard Worker */ 55*77c1e3ccSAndroid Build Coastguard Worker typedef struct { 56*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */ 57*77c1e3ccSAndroid Build Coastguard Worker double frame_err; 58*77c1e3ccSAndroid Build Coastguard Worker double frame_coded_error; 59*77c1e3ccSAndroid Build Coastguard Worker double frame_sr_coded_error; 60*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */ 61*77c1e3ccSAndroid Build Coastguard Worker } GF_FRAME_STATS; 62*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */ 63*77c1e3ccSAndroid Build Coastguard Worker 64*77c1e3ccSAndroid Build Coastguard Worker void av1_init_second_pass(struct AV1_COMP *cpi); 65*77c1e3ccSAndroid Build Coastguard Worker 66*77c1e3ccSAndroid Build Coastguard Worker void av1_init_single_pass_lap(AV1_COMP *cpi); 67*77c1e3ccSAndroid Build Coastguard Worker 68*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */ 69*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Main per frame entry point for second pass of two pass encode 70*77c1e3ccSAndroid Build Coastguard Worker * 71*77c1e3ccSAndroid Build Coastguard Worker *\ingroup rate_control 72*77c1e3ccSAndroid Build Coastguard Worker * 73*77c1e3ccSAndroid Build Coastguard Worker * This function is called for each frame in the second pass of a two pass 74*77c1e3ccSAndroid Build Coastguard Worker * encode. It checks the frame type and if a new KF or GF/ARF is due. 75*77c1e3ccSAndroid Build Coastguard Worker * When a KF is due it calls find_next_key_frame() to work out how long 76*77c1e3ccSAndroid Build Coastguard Worker * this key frame group will be and assign bits to the key frame. 77*77c1e3ccSAndroid Build Coastguard Worker * At the start of a new GF/ARF group it calls calculate_gf_length() 78*77c1e3ccSAndroid Build Coastguard Worker * and define_gf_group() which are the main functions responsible for 79*77c1e3ccSAndroid Build Coastguard Worker * defining the size and structure of the new GF/ARF group. 80*77c1e3ccSAndroid Build Coastguard Worker * 81*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top - level encoder instance structure 82*77c1e3ccSAndroid Build Coastguard Worker * \param[in] frame_params Per frame encoding parameters 83*77c1e3ccSAndroid Build Coastguard Worker * \param[in] frame_flags Frame type and coding flags 84*77c1e3ccSAndroid Build Coastguard Worker * 85*77c1e3ccSAndroid Build Coastguard Worker * \remark No return but analyses first pass stats and assigns a target 86*77c1e3ccSAndroid Build Coastguard Worker * number of bits to the current frame and a target Q range. 87*77c1e3ccSAndroid Build Coastguard Worker */ 88*77c1e3ccSAndroid Build Coastguard Worker void av1_get_second_pass_params(struct AV1_COMP *cpi, 89*77c1e3ccSAndroid Build Coastguard Worker struct EncodeFrameParams *const frame_params, 90*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_flags); 91*77c1e3ccSAndroid Build Coastguard Worker 92*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Adjustments to two pass and rate control after each frame. 93*77c1e3ccSAndroid Build Coastguard Worker * 94*77c1e3ccSAndroid Build Coastguard Worker *\ingroup rate_control 95*77c1e3ccSAndroid Build Coastguard Worker * 96*77c1e3ccSAndroid Build Coastguard Worker * This function is called after each frame to make adjustments to 97*77c1e3ccSAndroid Build Coastguard Worker * heuristics and data structures that relate to rate control. 98*77c1e3ccSAndroid Build Coastguard Worker * 99*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top - level encoder instance structure 100*77c1e3ccSAndroid Build Coastguard Worker * 101*77c1e3ccSAndroid Build Coastguard Worker * \remark No return value but this function updates various rate control 102*77c1e3ccSAndroid Build Coastguard Worker * related data structures that for example track overshoot and 103*77c1e3ccSAndroid Build Coastguard Worker * undershoot. 104*77c1e3ccSAndroid Build Coastguard Worker */ 105*77c1e3ccSAndroid Build Coastguard Worker void av1_twopass_postencode_update(struct AV1_COMP *cpi); 106*77c1e3ccSAndroid Build Coastguard Worker 107*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Distributes bits to frames in a group 108*77c1e3ccSAndroid Build Coastguard Worker * 109*77c1e3ccSAndroid Build Coastguard Worker *\ingroup rate_control 110*77c1e3ccSAndroid Build Coastguard Worker * 111*77c1e3ccSAndroid Build Coastguard Worker * This function decides on the allocation of bits between the different 112*77c1e3ccSAndroid Build Coastguard Worker * frames and types of frame in a GF/ARF group. 113*77c1e3ccSAndroid Build Coastguard Worker * 114*77c1e3ccSAndroid Build Coastguard Worker * \param[in] cpi Top - level encoder instance structure 115*77c1e3ccSAndroid Build Coastguard Worker * \param[in] rc Rate control data 116*77c1e3ccSAndroid Build Coastguard Worker * \param[in] gf_group GF/ARF group data structure 117*77c1e3ccSAndroid Build Coastguard Worker * \param[in] is_key_frame Indicates if the first frame in the group is 118*77c1e3ccSAndroid Build Coastguard Worker * also a key frame. 119*77c1e3ccSAndroid Build Coastguard Worker * \param[in] use_arf Are ARF frames enabled or is this a GF only 120*77c1e3ccSAndroid Build Coastguard Worker * uni-directional group. 121*77c1e3ccSAndroid Build Coastguard Worker * \param[in] gf_group_bits Bits available to be allocated. 122*77c1e3ccSAndroid Build Coastguard Worker * 123*77c1e3ccSAndroid Build Coastguard Worker * \remark No return but updates the rate control and group data structures 124*77c1e3ccSAndroid Build Coastguard Worker * to reflect the allocation of bits. 125*77c1e3ccSAndroid Build Coastguard Worker */ 126*77c1e3ccSAndroid Build Coastguard Worker void av1_gop_bit_allocation(const AV1_COMP *cpi, RATE_CONTROL *const rc, 127*77c1e3ccSAndroid Build Coastguard Worker GF_GROUP *gf_group, int is_key_frame, int use_arf, 128*77c1e3ccSAndroid Build Coastguard Worker int64_t gf_group_bits); 129*77c1e3ccSAndroid Build Coastguard Worker 130*77c1e3ccSAndroid Build Coastguard Worker int av1_calc_arf_boost(const TWO_PASS *twopass, 131*77c1e3ccSAndroid Build Coastguard Worker const TWO_PASS_FRAME *twopass_frame, 132*77c1e3ccSAndroid Build Coastguard Worker const PRIMARY_RATE_CONTROL *p_rc, FRAME_INFO *frame_info, 133*77c1e3ccSAndroid Build Coastguard Worker int offset, int f_frames, int b_frames, 134*77c1e3ccSAndroid Build Coastguard Worker int *num_fpstats_used, int *num_fpstats_required, 135*77c1e3ccSAndroid Build Coastguard Worker int project_gfu_boost); 136*77c1e3ccSAndroid Build Coastguard Worker 137*77c1e3ccSAndroid Build Coastguard Worker void av1_mark_flashes(FIRSTPASS_STATS *first_stats, 138*77c1e3ccSAndroid Build Coastguard Worker FIRSTPASS_STATS *last_stats); 139*77c1e3ccSAndroid Build Coastguard Worker void av1_estimate_noise(FIRSTPASS_STATS *first_stats, 140*77c1e3ccSAndroid Build Coastguard Worker FIRSTPASS_STATS *last_stats, 141*77c1e3ccSAndroid Build Coastguard Worker struct aom_internal_error_info *error_info); 142*77c1e3ccSAndroid Build Coastguard Worker void av1_estimate_coeff(FIRSTPASS_STATS *first_stats, 143*77c1e3ccSAndroid Build Coastguard Worker FIRSTPASS_STATS *last_stats); 144*77c1e3ccSAndroid Build Coastguard Worker 145*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus 146*77c1e3ccSAndroid Build Coastguard Worker } // extern "C" 147*77c1e3ccSAndroid Build Coastguard Worker #endif 148*77c1e3ccSAndroid Build Coastguard Worker 149*77c1e3ccSAndroid Build Coastguard Worker #endif // AOM_AV1_ENCODER_PASS2_STRATEGY_H_ 150