AOMedia AV1 Codec
pass2_strategy.h
1 /*
2  * Copyright (c) 2019, 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_PASS2_STRATEGY_H_
13 #define AOM_AV1_ENCODER_PASS2_STRATEGY_H_
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 struct AV1_COMP;
20 struct EncodeFrameParams;
21 
22 #include "av1/encoder/encoder.h"
23 
28 typedef struct {
30  double gf_group_err;
31  double gf_group_raw_error;
32  double gf_group_skip_pct;
33  double gf_group_inactive_zone_rows;
34 
35  double mv_ratio_accumulator;
36  double decay_accumulator;
37  double zero_motion_accumulator;
38  double loop_decay_rate;
39  double last_loop_decay_rate;
40  double this_frame_mv_in_out;
41  double mv_in_out_accumulator;
42  double abs_mv_in_out_accumulator;
43 
44  double avg_sr_coded_error;
45  double avg_pcnt_second_ref;
46  double avg_new_mv_count;
47  double avg_wavelet_energy;
48  double avg_raw_err_stdev;
49  int non_zero_stdev_count;
52 
56 typedef struct {
58  double frame_err;
59  double frame_coded_error;
60  double frame_sr_coded_error;
65 void av1_init_second_pass(struct AV1_COMP *cpi);
66 
67 void av1_init_single_pass_lap(AV1_COMP *cpi);
68 
89 void av1_get_second_pass_params(struct AV1_COMP *cpi,
90  struct EncodeFrameParams *const frame_params,
91  unsigned int frame_flags);
92 
106 void av1_twopass_postencode_update(struct AV1_COMP *cpi);
107 
127 void av1_gop_bit_allocation(const AV1_COMP *cpi, RATE_CONTROL *const rc,
128  GF_GROUP *gf_group, int is_key_frame, int use_arf,
129  int64_t gf_group_bits);
130 
131 int av1_calc_arf_boost(const TWO_PASS *twopass,
132  const TWO_PASS_FRAME *twopass_frame,
133  const PRIMARY_RATE_CONTROL *p_rc, FRAME_INFO *frame_info,
134  int offset, int f_frames, int b_frames,
135  int *num_fpstats_used, int *num_fpstats_required,
136  int project_gfu_boost);
137 
138 void av1_accumulate_next_frame_stats(const FIRSTPASS_STATS *stats,
139  const int flash_detected,
140  const int frames_since_key,
141  const int cur_idx,
142  GF_GROUP_STATS *gf_stats, int f_w,
143  int f_h);
144 // Identify stable and unstable regions from first pass stats.
145 // stats_start points to the first frame to analyze.
146 // |offset| is the offset from the current frame to the frame stats_start is
147 // pointing to.
148 void av1_identify_regions(const FIRSTPASS_STATS *const stats_start,
149  int total_frames, int offset, REGIONS *regions,
150  int *total_regions);
151 
152 #ifdef __cplusplus
153 } // extern "C"
154 #endif
155 
156 #endif // AOM_AV1_ENCODER_PASS2_STRATEGY_H_
av1_gop_bit_allocation
void av1_gop_bit_allocation(const AV1_COMP *cpi, RATE_CONTROL *const rc, GF_GROUP *gf_group, int is_key_frame, int use_arf, int64_t gf_group_bits)
Distributes bits to frames in a group.
Definition: pass2_strategy.c:2644
InitialDimensions::width
int width
Definition: encoder.h:1965
FIRSTPASS_STATS::pcnt_neutral
double pcnt_neutral
Definition: firstpass.h:88
RATE_CONTROL::projected_frame_size
int projected_frame_size
Definition: ratectrl.h:150
AV1_PRIMARY::frame_probs
FrameProbInfo frame_probs
Definition: encoder.h:2650
CommonModeInfoParams::mb_rows
int mb_rows
Definition: av1_common_int.h:506
encoder.h
Declares top-level encoder structures and functions.
FIRSTPASS_STATS
The stucture of acummulated frame stats in the first pass.
Definition: firstpass.h:41
av1_calc_iframe_target_size_one_pass_cbr
int av1_calc_iframe_target_size_one_pass_cbr(const struct AV1_COMP *cpi)
Calculates how many bits to use for an i frame in one pass cbr.
PRIMARY_RATE_CONTROL::bits_off_target
int64_t bits_off_target
Definition: ratectrl.h:521
PRIMARY_RATE_CONTROL::gf_group_bits
int64_t gf_group_bits
Definition: ratectrl.h:267
RATE_CONTROL
Rate Control parameters and status.
Definition: ratectrl.h:134
PRIMARY_RATE_CONTROL::active_best_quality
int active_best_quality[MAX_ARF_LAYERS+1]
Definition: ratectrl.h:464
GF_GROUP
Data related to the current GF/ARF group and the individual frames within the group.
Definition: firstpass.h:344
define_gf_group_pass0
static void define_gf_group_pass0(AV1_COMP *cpi)
Define a GF group in one pass mode when no look ahead stats are available.
Definition: pass2_strategy.c:2140
av1_get_second_pass_params
void av1_get_second_pass_params(AV1_COMP *cpi, EncodeFrameParams *const frame_params, unsigned int frame_flags)
Main per frame entry point for second pass of two pass encode.
Definition: pass2_strategy.c:3622
RateControlCfg
Encoder rate control configuration parameters.
Definition: encoder.h:520
calculate_gf_length
static void calculate_gf_length(AV1_COMP *cpi, int max_gop_length, int max_intervals)
Determine the length of future GF groups.
Definition: pass2_strategy.c:1874
FIRSTPASS_STATS::is_flash
int64_t is_flash
Definition: firstpass.h:155
get_twopass_worst_quality
static int get_twopass_worst_quality(AV1_COMP *cpi, const double av_frame_err, double inactive_zone, int av_target_bandwidth)
Choose a target maximum Q for a group of frames.
Definition: pass2_strategy.c:328
AlgoCfg::enable_tpl_model
bool enable_tpl_model
Definition: encoder.h:848
RateControlCfg::vbr_corpus_complexity_lap
unsigned int vbr_corpus_complexity_lap
Definition: encoder.h:550
FIRSTPASS_STATS::MVcv
double MVcv
Definition: firstpass.h:128
PRIMARY_RATE_CONTROL
Primary Rate Control parameters and status.
Definition: ratectrl.h:261
AV1_COMP::do_frame_data_update
bool do_frame_data_update
Definition: encoder.h:3272
AV1_PRIMARY::twopass
TWO_PASS twopass
Definition: encoder.h:2525
AV1_COMP::sf
SPEED_FEATURES sf
Definition: encoder.h:2881
PRIMARY_RATE_CONTROL::last_q
int last_q[FRAME_TYPES]
Definition: ratectrl.h:479
RATE_CONTROL::best_quality
int best_quality
Definition: ratectrl.h:216
AV1_COMP::gf_frame_index
unsigned char gf_frame_index
Definition: encoder.h:2912
AV1_COMP::compressor_stage
COMPRESSOR_STAGE compressor_stage
Definition: encoder.h:3168
AV1_COMP::frame_new_probs
FrameProbInfo frame_new_probs[10]
Definition: encoder.h:2998
GF_FRAME_STATS
accumulated stats and features for a frame
Definition: pass2_strategy.h:56
AlgoCfg::arnr_max_frames
int arnr_max_frames
Definition: encoder.h:830
FIRSTPASS_STATS::pcnt_motion
double pcnt_motion
Definition: firstpass.h:76
AV1Common::error
struct aom_internal_error_info * error
Definition: av1_common_int.h:758
define_gf_group
static void define_gf_group(AV1_COMP *cpi, EncodeFrameParams *frame_params, int is_final_pass)
Define a GF group.
Definition: pass2_strategy.c:2429
PRIMARY_RATE_CONTROL::gf_intervals
int gf_intervals[MAX_NUM_GF_INTERVALS]
Definition: ratectrl.h:282
GF_GROUP_STATS
accumulated stats and features in a gf group
Definition: pass2_strategy.h:28
AOM_CQ
@ AOM_CQ
Definition: aom_encoder.h:187
AV1_PRIMARY::gf_group
GF_GROUP gf_group
Definition: encoder.h:2495
FIRSTPASS_STATS::pcnt_second_ref
double pcnt_second_ref
Definition: firstpass.h:82
AV1_COMP::do_update_frame_probs_txtype
int do_update_frame_probs_txtype[10]
Definition: encoder.h:3003
TWO_PASS
Two pass status and control data.
Definition: firstpass.h:412
AV1EncoderConfig::kf_cfg
KeyFrameCfg kf_cfg
Definition: encoder.h:926
AV1Common::spatial_layer_id
int spatial_layer_id
Definition: av1_common_int.h:1049
FIRSTPASS_STATS::sr_coded_error
double sr_coded_error
Definition: firstpass.h:68
AV1_PRIMARY::lap_enabled
int lap_enabled
Definition: encoder.h:2505
AV1_COMP::third_pass_ctx
THIRD_PASS_DEC_CTX * third_pass_ctx
Definition: encoder.h:3328
PRIMARY_RATE_CONTROL::avg_frame_qindex
int avg_frame_qindex[FRAME_TYPES]
Definition: ratectrl.h:356
define_gf_group_pass3
static int define_gf_group_pass3(AV1_COMP *cpi, EncodeFrameParams *frame_params, int is_final_pass)
Define a GF group for the third apss.
Definition: pass2_strategy.c:2586
FIRSTPASS_STATS::MVrv
double MVrv
Definition: firstpass.h:124
aom_codec.h
Describes the codec algorithm interface to applications.
AV1_COMP::twopass_frame
TWO_PASS_FRAME twopass_frame
Definition: encoder.h:3323
AV1EncoderConfig
Main encoder configuration data structure.
Definition: encoder.h:909
FrameProbInfo::tx_type_probs
int tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES]
Definition: encoder.h:1103
av1_calc_pframe_target_size_one_pass_cbr
int av1_calc_pframe_target_size_one_pass_cbr(const struct AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type)
Calculates how many bits to use for a P frame in one pass cbr.
find_next_key_frame
static void find_next_key_frame(AV1_COMP *cpi, FIRSTPASS_STATS *this_frame)
Determine the next key frame group.
Definition: pass2_strategy.c:3145
PRIMARY_RATE_CONTROL::rate_error_estimate
int rate_error_estimate
Definition: ratectrl.h:505
AV1_COMP
Top level encoder structure.
Definition: encoder.h:2664
PRIMARY_RATE_CONTROL::gfu_boost
int gfu_boost
Definition: ratectrl.h:277
AV1_COMP::do_update_vbr_bits_off_target_fast
int do_update_vbr_bits_off_target_fast
Definition: encoder.h:3037
AV1Common::height
int height
Definition: av1_common_int.h:776
AV1_PRIMARY::tpl_data
TplParams tpl_data
Definition: encoder.h:2583
AOM_CBR
@ AOM_CBR
Definition: aom_encoder.h:186
AV1_COMP::initial_mbs
int initial_mbs
Definition: encoder.h:2958
KeyFrameCfg::key_freq_min
int key_freq_min
Definition: encoder.h:466
AV1Common::temporal_layer_id
int temporal_layer_id
Definition: av1_common_int.h:1043
RateControlCfg::mode
enum aom_rc_mode mode
Definition: encoder.h:604
EncodeFrameParams::frame_type
FRAME_TYPE frame_type
Definition: encoder.h:3364
FIRSTPASS_INFO
Data structure used for managing first pass stats.
Definition: firstpass.h:177
AV1_PRIMARY::tf_info
TEMPORAL_FILTER_INFO tf_info
Definition: encoder.h:2535
FIRSTPASS_INFO::total_stats
FIRSTPASS_STATS total_stats
Definition: firstpass.h:231
AV1Common::seq_params
SequenceHeader * seq_params
Definition: av1_common_int.h:976
av1_firstpass_info_peek
const FIRSTPASS_STATS * av1_firstpass_info_peek(const FIRSTPASS_INFO *firstpass_info, int offset_from_cur)
Peek at a stats from firstpass_info.
AV1_PRIMARY::lookahead
struct lookahead_ctx * lookahead
Definition: encoder.h:2467
FrameProbInfo::obmc_probs
int obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL]
Definition: encoder.h:1088
FIRSTPASS_STATS::mv_in_out_count
double mv_in_out_count
Definition: firstpass.h:134
AV1_COMP::common
AV1_COMMON common
Definition: encoder.h:2707
RateControlCfg::under_shoot_pct
int under_shoot_pct
Definition: encoder.h:579
RateControlCfg::cq_level
int cq_level
Definition: encoder.h:599
FIRSTPASS_STATS::mvc_abs
double mvc_abs
Definition: firstpass.h:120
FIRSTPASS_STATS::new_mv_count
double new_mv_count
Definition: firstpass.h:138
AV1_COMP::initial_dimensions
InitialDimensions initial_dimensions
Definition: encoder.h:2950
FIRSTPASS_STATS::pcnt_inter
double pcnt_inter
Definition: firstpass.h:72
encode_strategy.h
Declares frame encoding functions.
av1_tpl_setup_stats
int av1_tpl_setup_stats(struct AV1_COMP *cpi, int gop_eval, const struct EncodeFrameParams *const frame_params)
Implements temporal dependency modelling for a GOP (GF/ARF group) and selects between 16 and 32 frame...
FIRSTPASS_STATS::inactive_zone_rows
double inactive_zone_rows
Definition: firstpass.h:100
RateControlCfg::best_allowed_q
int best_allowed_q
Definition: encoder.h:595
av1_twopass_postencode_update
void av1_twopass_postencode_update(AV1_COMP *cpi)
Adjustments to two pass and rate control after each frame.
Definition: pass2_strategy.c:4002
KeyFrameCfg::auto_key
bool auto_key
Definition: encoder.h:494
RateControlCfg::over_shoot_pct
int over_shoot_pct
Definition: encoder.h:585
RATE_CONTROL::frames_till_gf_update_due
int frames_till_gf_update_due
Definition: ratectrl.h:170
av1_calc_iframe_target_size_one_pass_vbr
int av1_calc_iframe_target_size_one_pass_vbr(const struct AV1_COMP *const cpi)
Calculates how many bits to use for an i frame in one pass vbr.
av1_lookahead_pop_sz
int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage)
Get pop_sz value.
AV1_PRIMARY::internal_altref_allowed
int internal_altref_allowed
Definition: encoder.h:2485
RateControlCfg::vbrmin_section
int vbrmin_section
Definition: encoder.h:616
has_no_stats_stage
static int has_no_stats_stage(const AV1_COMP *const cpi)
Check if the current stage has statistics.
Definition: encoder.h:3761
InitialDimensions::height
int height
Definition: encoder.h:1966
InitialDimensions
Initial frame dimensions.
Definition: encoder.h:1964
KeyFrameCfg::fwd_kf_dist
int fwd_kf_dist
Definition: encoder.h:499
AV1EncoderConfig::pass
enum aom_enc_pass pass
Definition: encoder.h:1034
AV1_PRIMARY::p_rc
PRIMARY_RATE_CONTROL p_rc
Definition: encoder.h:2530
AV1_COMP::second_pass_log_stream
FILE * second_pass_log_stream
Definition: encoder.h:3333
RATE_CONTROL::intervals_till_gf_calculate_due
int intervals_till_gf_calculate_due
Definition: ratectrl.h:175
calculate_total_gf_group_bits
static int64_t calculate_total_gf_group_bits(AV1_COMP *cpi, double gf_group_err)
Calculates the bit target for this GF/ARF group.
Definition: pass2_strategy.c:785
RateControlCfg::vbrbias
int vbrbias
Definition: encoder.h:611
RateControlCfg::target_bandwidth
int64_t target_bandwidth
Definition: encoder.h:544
FrameProbInfo::switchable_interp_probs
int switchable_interp_probs[FRAME_UPDATE_TYPES][((SWITCHABLE_FILTERS+1) *4)][SWITCHABLE_FILTERS]
Definition: encoder.h:1112
RATE_CONTROL::base_frame_target
int base_frame_target
Definition: ratectrl.h:141
AV1Common::show_frame
int show_frame
Definition: av1_common_int.h:885
PRIMARY_RATE_CONTROL::rolling_target_bits
int rolling_target_bits
Definition: ratectrl.h:526
SPEED_FEATURES::tpl_sf
TPL_SPEED_FEATURES tpl_sf
Definition: speed_features.h:1542
AV1Common::quant_params
CommonQuantParams quant_params
Definition: av1_common_int.h:922
FIRSTPASS_STATS::duration
double duration
Definition: firstpass.h:142
PRIMARY_RATE_CONTROL::vbr_bits_off_target
int64_t vbr_bits_off_target
Definition: ratectrl.h:510
PRIMARY_RATE_CONTROL::rolling_actual_bits
int rolling_actual_bits
Definition: ratectrl.h:532
define_kf_interval
static int define_kf_interval(AV1_COMP *cpi, const FIRSTPASS_INFO *firstpass_info, int num_frames_to_detect_scenecut, int search_start_idx)
Determine the location of the next key frame.
Definition: pass2_strategy.c:2882
AOM_RC_ONE_PASS
@ AOM_RC_ONE_PASS
Definition: aom_encoder.h:176
aom_encoder.h
Describes the encoder algorithm interface to applications.
KeyFrameCfg
Encoder config related to the coding of key frames.
Definition: encoder.h:462
RATE_CONTROL::active_worst_quality
int active_worst_quality
Definition: ratectrl.h:233
AV1_COMP::rc
RATE_CONTROL rc
Definition: encoder.h:2861
AV1_COMP::do_update_frame_probs_interpfilter
int do_update_frame_probs_interpfilter[10]
Definition: encoder.h:3018
CommonQuantParams::base_qindex
int base_qindex
Definition: av1_common_int.h:613
RateControlCfg::vbrmax_section
int vbrmax_section
Definition: encoder.h:621
AV1Common
Top level common structure used by both encoder and decoder.
Definition: av1_common_int.h:750
AV1_COMP::new_framerate
double new_framerate
Definition: encoder.h:3032
PRIMARY_RATE_CONTROL::kf_boost
int kf_boost
Definition: ratectrl.h:272
PRIMARY_RATE_CONTROL::total_actual_bits
int64_t total_actual_bits
Definition: ratectrl.h:490
AOM_Q
@ AOM_Q
Definition: aom_encoder.h:188
AV1EncoderConfig::algo_cfg
AlgoCfg algo_cfg
Definition: encoder.h:921
AV1_PRIMARY::level_params
AV1LevelParams level_params
Definition: encoder.h:2510
av1_cyclic_refresh_set_golden_update
void av1_cyclic_refresh_set_golden_update(struct AV1_COMP *const cpi)
Set golden frame update interval nased on cyclic refresh.
FIRSTPASS_STATS::intra_error
double intra_error
Definition: firstpass.h:56
FIRSTPASS_STATS::noise_var
double noise_var
Definition: firstpass.h:159
AV1_COMP::skip_tpl_setup_stats
int skip_tpl_setup_stats
Definition: encoder.h:2772
FIRSTPASS_STATS::inactive_zone_cols
double inactive_zone_cols
Definition: firstpass.h:104
FIRSTPASS_STATS::MVc
double MVc
Definition: firstpass.h:116
AV1_COMP::framerate
double framerate
Definition: encoder.h:2866
CommonModeInfoParams::MBs
int MBs
Definition: av1_common_int.h:516
EncodeFrameParams
contains per-frame encoding parameters decided upon by av1_encode_strategy() and passed down to av1_e...
Definition: encoder.h:3356
aom_rc_mode
aom_rc_mode
Rate control mode.
Definition: aom_encoder.h:184
FIRSTPASS_STATS::intra_skip_pct
double intra_skip_pct
Definition: firstpass.h:96
AV1_COMP::do_update_frame_probs_warp
int do_update_frame_probs_warp[10]
Definition: encoder.h:3013
FIRSTPASS_STATS::raw_error_stdev
double raw_error_stdev
Definition: firstpass.h:151
FIRSTPASS_STATS::mvr_abs
double mvr_abs
Definition: firstpass.h:112
AV1_COMP::ppi
AV1_PRIMARY * ppi
Definition: encoder.h:2668
AV1_COMP::num_frame_recode
int num_frame_recode
Definition: encoder.h:2993
FIRSTPASS_STATS::MVr
double MVr
Definition: firstpass.h:108
av1_firstpass_info_future_count
int av1_firstpass_info_future_count(const FIRSTPASS_INFO *firstpass_info, int offset_from_cur)
Count the future stats from the target in firstpass_info Note that the target stats will be counted a...
FIRSTPASS_STATS::count
double count
Definition: firstpass.h:147
av1_lookahead_depth
unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage)
Get the number of frames currently in the lookahead queue.
PRIMARY_RATE_CONTROL::vbr_bits_off_target_fast
int64_t vbr_bits_off_target_fast
Definition: ratectrl.h:515
FrameProbInfo
Encoder-side probabilities for pruning of various AV1 tools.
Definition: encoder.h:1082
TWO_PASS_FRAME
Frame level Two pass status and control data.
Definition: firstpass.h:455
FrameProbInfo::warped_probs
int warped_probs[FRAME_UPDATE_TYPES]
Definition: encoder.h:1095
AV1Common::width
int width
Definition: av1_common_int.h:775
AV1_PRIMARY::gf_state
GF_STATE gf_state
Definition: encoder.h:2500
AV1_COMP::do_update_frame_probs_obmc
int do_update_frame_probs_obmc[10]
Definition: encoder.h:3008
AV1_PRIMARY::frames_left
int frames_left
Definition: encoder.h:2520
AV1_COMP::oxcf
AV1EncoderConfig oxcf
Definition: encoder.h:2712
av1_calc_pframe_target_size_one_pass_vbr
int av1_calc_pframe_target_size_one_pass_vbr(const struct AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type)
Calculates how many bits to use for a P frame in one pass vbr.
FIRSTPASS_STATS::cor_coeff
double cor_coeff
Definition: firstpass.h:163
FIRSTPASS_STATS::coded_error
double coded_error
Definition: firstpass.h:64
AV1EncoderConfig::rc_cfg
RateControlCfg rc_cfg
Definition: encoder.h:931
aom_bit_depth_t
enum aom_bit_depth aom_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
PRIMARY_RATE_CONTROL::cur_gf_index
int cur_gf_index
Definition: ratectrl.h:287
AV1Common::current_frame
CurrentFrame current_frame
Definition: av1_common_int.h:754
AV1_COMP::frame_info
FRAME_INFO frame_info
Definition: encoder.h:2940
AV1Common::mi_params
CommonModeInfoParams mi_params
Definition: av1_common_int.h:910
FIRSTPASS_STATS::frame_avg_wavelet_energy
double frame_avg_wavelet_energy
Definition: firstpass.h:60
PRIMARY_RATE_CONTROL::avg_q
double avg_q
Definition: ratectrl.h:474
av1_gop_setup_structure
void av1_gop_setup_structure(struct AV1_COMP *cpi)
Set up the Group-Of-Pictures structure for this GF_GROUP.
EncodeFrameParams::show_frame
int show_frame
Definition: encoder.h:3374
RATE_CONTROL::frames_to_key
int frames_to_key
Definition: ratectrl.h:185
RATE_CONTROL::worst_quality
int worst_quality
Definition: ratectrl.h:212
KeyFrameCfg::key_freq_max
int key_freq_max
Definition: encoder.h:471
FIRSTPASS_STATS::weight
double weight
Definition: firstpass.h:52
AOM_RC_THIRD_PASS
@ AOM_RC_THIRD_PASS
Definition: aom_encoder.h:179