| AOMedia AV1 Codec

Functions
Rate Control
Encoder Algorithm » High-level Algorithm

## Functions
aom_codec_err_t | av1_firstpass_info_init (FIRSTPASS_INFO *firstpass_info, FIRSTPASS_STATS *ext_stats_buf, int ext_stats_buf_size)
| Init firstpass_info.
aom_codec_err_t | av1_firstpass_info_move_cur_index (FIRSTPASS_INFO *firstpass_info)
| Move cur_index by 1.
aom_codec_err_t | av1_firstpass_info_pop (FIRSTPASS_INFO *firstpass_info)
| Pop a stats from firstpass_info.
aom_codec_err_t | av1_firstpass_info_move_cur_index_and_pop (FIRSTPASS_INFO *firstpass_info)
| Move cur_index by 1 and pop a stats from firstpass_info.
aom_codec_err_t | av1_firstpass_info_push (FIRSTPASS_INFO *firstpass_info, const FIRSTPASS_STATS *input_stats)
| Push a stats into firstpass_info.
const FIRSTPASS_STATS * | av1_firstpass_info_peek (const FIRSTPASS_INFO *firstpass_info, int offset_from_cur)
| Peek at a stats from firstpass_info.
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 as well. The target index is as follows. (cur_index + offset_from_cur) % firstpass_info->stats_buf_size.
void | av1_first_pass (struct AV1_COMP *cpi, const int64_t ts_duration)
| AV1 first pass encoding.
void | av1_gop_setup_structure (struct AV1_COMP *cpi)
| Set up the Group-Of-Pictures structure for this GF_GROUP.
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.
int | av1_gop_check_forward_keyframe (const GF_GROUP *gf_group, int gf_frame_index)
| Check whether a frame in the GOP is a forward key frame.
int | av1_gop_is_second_arf (const GF_GROUP *gf_group, int gf_frame_index)
| Check whether a frame in the GOP is the second arf.
void | av1_get_second_pass_params (struct AV1_COMP *cpi, struct EncodeFrameParams *const frame_params, unsigned int frame_flags)
| Main per frame entry point for second pass of two pass encode.
void | av1_twopass_postencode_update (struct AV1_COMP *cpi)
| Adjustments to two pass and rate control after each frame.
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.
static int64_t | calculate_total_gf_group_bits (AV1_COMP *cpi, double gf_group_err)
| Calculates the bit target for this GF/ARF group.
static double | get_rate_correction_factor (const AV1_COMP *cpi, int width, int height)
| Gets a rate vs Q correction factor.
static void | set_rate_correction_factor (AV1_COMP *cpi, int is_encode_stage, double factor, int width, int height)
| Sets a rate vs Q correction factor.
static int | find_closest_qindex_by_rate (int desired_bits_per_mb, const AV1_COMP *cpi, double correction_factor, int best_qindex, int worst_qindex)
| Searches for a Q index value predicted to give an average macro block rate closest to the target value.
static int | rc_pick_q_and_bounds_no_stats_cbr (const AV1_COMP *cpi, int width, int height, int *bottom_index, int *top_index)
| Picks q and q bounds given CBR rate control parameters in `cpi->rc`.
static int | rc_pick_q_and_bounds_no_stats (const AV1_COMP *cpi, int width, int height, int *bottom_index, int *top_index)
| Picks q and q bounds given non-CBR rate control params in `cpi->rc`.
static int | get_q (const AV1_COMP *cpi, const int width, const int height, const int active_worst_quality, const int active_best_quality)
| Gets a Q value to use for the current frame.
static int | rc_pick_q_and_bounds (const AV1_COMP *cpi, int width, int height, int gf_index, int *bottom_index, int *top_index)
| Picks q and q bounds given rate control parameters in `cpi->rc`.
void | av1_set_rtc_reference_structure_one_layer (AV1_COMP *cpi, int gf_update)
| Setup the reference prediction structure for 1 pass real-time.
static void | rc_scene_detection_onepass_rt (AV1_COMP *cpi, const EncodeFrameInput *frame_input)
| Check for scene detection, for 1 pass real-time mode.
static void | rc_spatial_act_keyframe_onepass_rt (AV1_COMP *cpi, uint8_t *src_y, int src_ystride)
| Compute spatial activity for keyframe, 1 pass real-time mode.
static int | set_gf_interval_update_onepass_rt (AV1_COMP *cpi, FRAME_TYPE frame_type)
| Set the GF baseline interval for 1 pass real-time mode.
static void | dynamic_resize_one_pass_cbr (AV1_COMP *cpi)
| Check for resize based on Q, for 1 pass real-time mode.
void | av1_rc_update_rate_correction_factors (struct AV1_COMP *cpi, int is_encode_stage, int width, int height)
| Updates the rate correction factor linking Q to output bits.
int | av1_rc_pick_q_and_bounds (struct AV1_COMP *cpi, int width, int height, int gf_index, int *bottom_index, int *top_index)
| Picks q and q bounds given the rate control parameters in `cpi->rc`.
int | av1_rc_regulate_q (const struct AV1_COMP *cpi, int target_bits_per_frame, int active_best_quality, int active_worst_quality, int width, int height)
| Estimates q to achieve a target bits per frame.
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.
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.
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.
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.
void | av1_get_one_pass_rt_params (struct AV1_COMP *cpi, FRAME_TYPE *const frame_type, const struct EncodeFrameInput *frame_input, unsigned int frame_flags)
| Setup the rate control parameters for 1 pass real-time mode.
int | av1_encodedframe_overshoot_cbr (struct AV1_COMP *cpi, int *q)
| Increase q on expected encoder overshoot, for CBR mode.
int | av1_postencode_drop_cbr (struct AV1_COMP *cpi, size_t *size)
| Check if frame should be dropped, for RTC mode.
static int | recode_loop_test (AV1_COMP *cpi, int high_limit, int low_limit, int q, int maxq, int minq)
| Function to test for conditions that indicate we should loop back and recode a frame.
static void | recode_loop_update_q (AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low, int *const q_high, const int top_index, const int bottom_index, int *const undershoot_seen, int *const overshoot_seen, int *const low_cr_seen, const int loop_count)
| Called after encode_with_recode_loop() has just encoded a frame. This function works out whether we undershot or overshot our bitrate target and adjusts q as appropriate. It also decides whether or not we need to recode the frame to get closer to the target rate.
static LAYER_CONTEXT * | get_layer_context (AV1_COMP *const cpi)
| Return layer context for current layer.

## Detailed Description

This module describes rate control algorithm in AV1. See also Rate Control

## Function Documentation

## ◆ av1_firstpass_info_init()

aom_codec_err_t av1_firstpass_info_init | ( | FIRSTPASS_INFO * | firstpass_info,
|  | FIRSTPASS_STATS * | ext_stats_buf,
|  | int | ext_stats_buf_size )

Init firstpass_info.

If using ext_stats_buf, the buffer needs to stay available during encoding process.

Parameters

  [out] | firstpass_info | struct of firstpass_info.
  [in] | ext_stats_buf | external stats buffer. Pass in NULL if choose to use internal static_stats_buf.
  [in] | ext_stats_buf_size | external stats buffer size. Pass in 0 if choose to use internal static_stats_buf.

Returns
  status

## ◆ av1_firstpass_info_move_cur_index()

aom_codec_err_t av1_firstpass_info_move_cur_index | ( | FIRSTPASS_INFO * | firstpass_info | ) |

Move cur_index by 1.

Parameters

  [out] | firstpass_info | struct of firstpass_info.

Returns
  status

## ◆ av1_firstpass_info_pop()

aom_codec_err_t av1_firstpass_info_pop | ( | FIRSTPASS_INFO * | firstpass_info | ) |

Pop a stats from firstpass_info.

Parameters

  [out] | firstpass_info | struct of firstpass_info.

Returns
  status

## ◆ av1_firstpass_info_move_cur_index_and_pop()

aom_codec_err_t av1_firstpass_info_move_cur_index_and_pop | ( | FIRSTPASS_INFO * | firstpass_info | ) |

Move cur_index by 1 and pop a stats from firstpass_info.

Parameters

  [out] | firstpass_info | struct of firstpass_info.

Returns
  status

## ◆ av1_firstpass_info_push()

aom_codec_err_t av1_firstpass_info_push | ( | FIRSTPASS_INFO * | firstpass_info,
|  | const FIRSTPASS_STATS * | input_stats )

Push a stats into firstpass_info.

Note that the input stats will be copied into firstpass_info.

Parameters

  [out] | firstpass_info | struct of firstpass_info.
  [in] | input_stats | input stats

Returns
  status

## ◆ 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.

The target index is as follows. (cur_index + offset_from_cur) % firstpass_info->stats_buf_size

Parameters

  [in] | firstpass_info | struct of firstpass_info.
  [in] | offset_from_cur | index offset from cur_index.

Returns
  pointer to the stats. The pointer will be NULL if stats_index_offset is invalid.

Referenced by define_kf_interval(), and find_next_key_frame().

## ◆ 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 as well. The target index is as follows. (cur_index + offset_from_cur) % firstpass_info->stats_buf_size.

Parameters

  [in] | firstpass_info | struct of firstpass_info.
  [in] | offset_from_cur | target stats's inffset from cur_index.

Returns
  Number of stats in the future after the target stats including itself.

Referenced by define_kf_interval().

## ◆ av1_first_pass()

void av1_first_pass | ( | struct AV1_COMP * | cpi,
|  | const int64_t | ts_duration )

AV1 first pass encoding.

This function is the first encoding pass for the two pass encoding mode. It encodes the whole video and collect essential information. Two pass encoding is an encoding mode in the reference software (libaom) of AV1 for high performance encoding. The first pass is a fast encoding process to collect essential information to help the second pass make encoding decisions and improve coding quality. The collected stats is used in rate control, for example, to determine frame cut, the position of alternative reference frame (ARF), etc.

Parameters

  [in] | cpi | Top-level encoder structure
  [in] | ts_duration | Duration of the frame / collection of frames

Remarks
  Nothing is returned. Instead, the "TWO_PASS" structure inside "cpi" is modified to store information computed in this function.

Referenced by av1_encode().

## ◆ av1_gop_setup_structure()

void av1_gop_setup_structure | ( | struct AV1_COMP * | cpi | ) |

Set up the Group-Of-Pictures structure for this GF_GROUP.

This function defines the Group-Of-Pictures structure for this GF_GROUP. This involves deciding where to place the various FRAME_UPDATE_TYPEs in the group. It does this primarily by updateing entries in cpi->twopass.gf_group.update_type[].

Parameters

  [in] | cpi | Top - level encoder instance structure

Remarks
  No return value but this function updates group data structures.

Referenced by define_gf_group(), and define_gf_group_pass0().

## ◆ 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.

This function decides on the allocation of bits between the different frames and types of frame in a GF/ARF group.

Parameters

  [in] | cpi | Top - level encoder instance structure
  [in] | rc | Rate control data
  [in] | gf_group | GF/ARF group data structure
  [in] | is_key_frame | Indicates if the first frame in the group is also a key frame.
  [in] | use_arf | Are ARF frames enabled or is this a GF only uni-directional group.
  [in] | gf_group_bits | Bits available to be allocated.

Remarks
  No return but updates the rate control and group data structures to reflect the allocation of bits.

References PRIMARY_RATE_CONTROL::gfu_boost, AV1_PRIMARY::p_rc, and AV1_COMP::ppi.

## ◆ av1_gop_check_forward_keyframe()

int av1_gop_check_forward_keyframe | ( | const GF_GROUP * | gf_group,
|  | int | gf_frame_index )

Check whether a frame in the GOP is a forward key frame.

Parameters

  [in] | gf_group | GF/ARF group data structure
  [in] | gf_frame_index | GOP index

Returns
  Return 1 if it is a forward key frame, otherwise return 0

Referenced by tf_setup_filtering_buffer().

## ◆ av1_gop_is_second_arf()

int av1_gop_is_second_arf | ( | const GF_GROUP * | gf_group,
|  | int | gf_frame_index )

Check whether a frame in the GOP is the second arf.

Parameters

  [in] | gf_group | GF/ARF group data structure
  [in] | gf_frame_index | GOP index

Returns
  Return 1 if it is the second arf

## ◆ av1_get_second_pass_params()

void av1_get_second_pass_params | ( | struct AV1_COMP * | cpi,
|  | struct EncodeFrameParams *const | frame_params,
|  | unsigned int | frame_flags )

Main per frame entry point for second pass of two pass encode.

This function is called for each frame in the second pass of a two pass encode. It checks the frame type and if a new KF or GF/ARF is due. When a KF is due it calls find_next_key_frame() to work out how long this key frame group will be and assign bits to the key frame. At the start of a new GF/ARF group it calls calculate_gf_length() and define_gf_group() which are the main functions responsible for defining the size and structure of the new GF/ARF group.

Parameters

  [in] | cpi | Top - level encoder instance structure
  [in] | frame_params | Per frame encoding parameters
  [in] | frame_flags | Frame type and coding flags

Remarks
  No return but analyses first pass stats and assigns a target number of bits to the current frame and a target Q range.

References RATE_CONTROL::active_worst_quality, AV1EncoderConfig::algo_cfg, AOM_CODEC_MEM_ERROR, AOM_Q, AOM_RC_THIRD_PASS, AlgoCfg::arnr_max_frames, calculate_gf_length(), AV1_COMP::common, RateControlCfg::cq_level, PRIMARY_RATE_CONTROL::cur_gf_index, AV1Common::current_frame, define_gf_group(), define_kf_interval(), AV1_COMP::ducky_encode_info, AlgoCfg::enable_tpl_model, AV1Common::error, find_next_key_frame(), AV1_COMP::frame_info, EncodeFrameParams::frame_type, RATE_CONTROL::frames_till_gf_update_due, RATE_CONTROL::frames_to_key, KeyFrameCfg::fwd_kf_dist, AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, PRIMARY_RATE_CONTROL::gf_intervals, AV1_PRIMARY::gf_state, PRIMARY_RATE_CONTROL::gfu_boost, RATE_CONTROL::intervals_till_gf_calculate_due, PRIMARY_RATE_CONTROL::kf_boost, AV1EncoderConfig::kf_cfg, AV1_PRIMARY::lap_enabled, RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1EncoderConfig::pass, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, AV1_COMP::second_pass_log_stream, AV1_COMP::sf, EncodeFrameParams::show_frame, AV1_PRIMARY::tf_info, SPEED_FEATURES::tpl_sf, AV1_PRIMARY::twopass, AV1_COMP::twopass_frame, and AV1_COMP::use_ducky_encode.

Referenced by av1_encode_strategy().

## ◆ av1_twopass_postencode_update()

void av1_twopass_postencode_update | ( | struct AV1_COMP * | cpi | ) |

Adjustments to two pass and rate control after each frame.

This function is called after each frame to make adjustments to heuristics and data structures that relate to rate control.

Parameters

  [in] | cpi | Top - level encoder instance structure

Remarks
  No return value but this function updates various rate control related data structures that for example track overshoot and undershoot.

References PRIMARY_RATE_CONTROL::active_best_quality, RATE_CONTROL::active_worst_quality, AOM_CQ, AOM_Q, RATE_CONTROL::base_frame_target, CommonQuantParams::base_qindex, AV1_COMP::common, AV1Common::current_frame, AV1_COMP::do_frame_data_update, AV1_COMP::do_update_frame_probs_interpfilter, AV1_COMP::do_update_frame_probs_obmc, AV1_COMP::do_update_frame_probs_txtype, AV1_COMP::do_update_frame_probs_warp, AV1_COMP::do_update_vbr_bits_off_target_fast, AV1_COMP::ducky_encode_info, AV1_COMP::frame_new_probs, AV1_PRIMARY::frame_probs, AV1_COMP::framerate, RATE_CONTROL::frames_to_key, AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, AV1_PRIMARY::lap_enabled, RateControlCfg::mode, AV1_COMP::new_framerate, AV1_COMP::num_frame_recode, FrameProbInfo::obmc_probs, RateControlCfg::over_shoot_pct, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, RATE_CONTROL::projected_frame_size, AV1Common::quant_params, PRIMARY_RATE_CONTROL::rate_error_estimate, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, PRIMARY_RATE_CONTROL::rolling_actual_bits, PRIMARY_RATE_CONTROL::rolling_target_bits, AV1Common::seq_params, AV1Common::show_frame, FrameProbInfo::switchable_interp_probs, PRIMARY_RATE_CONTROL::total_actual_bits, AV1_PRIMARY::twopass, AV1_COMP::twopass_frame, FrameProbInfo::tx_type_probs, RateControlCfg::under_shoot_pct, AV1_COMP::use_ducky_encode, PRIMARY_RATE_CONTROL::vbr_bits_off_target, PRIMARY_RATE_CONTROL::vbr_bits_off_target_fast, FrameProbInfo::warped_probs, and RATE_CONTROL::worst_quality.

## ◆ 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 )
| static

Choose a target maximum Q for a group of frames.

This function is used to estimate a suitable maximum Q for a group of frames. Inititally it is called to get a crude estimate for the whole clip. It is then called for each ARF/GF group to get a revised estimate for that group.

Parameters

  [in] | cpi | Top-level encoder structure
  [in] | av_frame_err | The average per frame coded error score for frames making up this section/group.
  [in] | inactive_zone | Used to mask off /ignore part of the frame. The most common use case is where a wide format video (e.g. 16:9) is letter-boxed into a more square format. Here we want to ignore the bands at the top and bottom.
  [in] | av_target_bandwidth | The target bits per frame

Returns
  The maximum Q for frames in the group.

References AOM_CQ, RATE_CONTROL::best_quality, AV1_COMP::common, RateControlCfg::cq_level, AV1_COMP::initial_mbs, CommonModeInfoParams::MBs, AV1Common::mi_params, RateControlCfg::mode, RateControlCfg::over_shoot_pct, AV1_COMP::oxcf, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, AV1Common::seq_params, AV1_PRIMARY::twopass, RateControlCfg::under_shoot_pct, and RATE_CONTROL::worst_quality.

## ◆ calculate_total_gf_group_bits()

static int64_t calculate_total_gf_group_bits | ( | AV1_COMP * | cpi,
|  | double | gf_group_err )
| static

Calculates the bit target for this GF/ARF group.

Calculates the total bits to allocate in this GF/ARF group.

Parameters

  [in] | cpi | Top-level encoder structure
  [in] | gf_group_err | Cumulative coded error score for the frames making up this group.

Returns
  The target total number of bits for this GF/ARF group.

References AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1_COMP::rc, and AV1_PRIMARY::twopass.

## ◆ get_rate_correction_factor()

static double get_rate_correction_factor | ( | const AV1_COMP * | cpi,
|  | int | width,
|  | int | height )
| static

Gets a rate vs Q correction factor.

This function returns the current value of a correction factor used to dynamically adjust the relationship between Q and the expected number of bits for the frame.

Parameters

  [in] | cpi | Top level encoder instance structure
  [in] | width | Frame width
  [in] | height | Frame height

Returns
  Returns a correction factor for the current frame

References RefreshFrameInfo::alt_ref_frame, AOM_CBR, AV1_COMP::common, AV1Common::current_frame, RateControlCfg::gf_cbr_boost_pct, AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, RefreshFrameInfo::golden_frame, RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, PRIMARY_RATE_CONTROL::rate_correction_factors, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, AV1_COMP::refresh_frame, and AV1_PRIMARY::use_svc.

Referenced by av1_rc_update_rate_correction_factors().

## ◆ set_rate_correction_factor()

static void set_rate_correction_factor | ( | AV1_COMP * | cpi,
|  | int | is_encode_stage,
|  | double | factor,
|  | int | width,
|  | int | height )
| static

Sets a rate vs Q correction factor.

This function updates the current value of a correction factor used to dynamically adjust the relationship between Q and the expected number of bits for the frame.

Parameters

  [in] | cpi | Top level encoder instance structure
  [in] | is_encode_stage | Indicates if recode loop or post-encode
  [in] | factor | New correction factor
  [in] | width | Frame width
  [in] | height | Frame height

Remarks
  Updates the rate correction factor for the current frame type in cpi->rc.

References RefreshFrameInfo::alt_ref_frame, AOM_CBR, AV1_COMP::common, AV1Common::current_frame, RateControlCfg::gf_cbr_boost_pct, AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, RefreshFrameInfo::golden_frame, RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, PRIMARY_RATE_CONTROL::rate_correction_factors, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, AV1_COMP::refresh_frame, and AV1_PRIMARY::use_svc.

Referenced by av1_rc_update_rate_correction_factors().

## ◆ find_closest_qindex_by_rate()

static int find_closest_qindex_by_rate | ( | int | desired_bits_per_mb,
|  | const AV1_COMP * | cpi,
|  | double | correction_factor,
|  | int | best_qindex,
|  | int | worst_qindex )
| static

Searches for a Q index value predicted to give an average macro block rate closest to the target value.

Similar to find_qindex_by_rate() function, but returns a q index with a rate just above or below the desired rate, depending on which of the two rates is closer to the desired rate. Also, respects the selected aq_mode when computing the rate.

Parameters

  [in] | desired_bits_per_mb | Target bits per mb
  [in] | cpi | Top level encoder instance structure
  [in] | correction_factor | Current Q to rate correction factor
  [in] | best_qindex | Min allowed Q value.
  [in] | worst_qindex | Max allowed Q value.

Returns
  Returns a correction factor for the current frame

References AV1_COMP::cyclic_refresh, and AV1_COMP::oxcf.

## ◆ rc_pick_q_and_bounds_no_stats_cbr()

static int rc_pick_q_and_bounds_no_stats_cbr | ( | const AV1_COMP * | cpi,
|  | int | width,
|  | int | height,
|  | int * | bottom_index,
|  | int * | top_index )
| static

Picks q and q bounds given CBR rate control parameters in `cpi->rc`.

Handles the special case when using:

- Constant bit-rate mode: `cpi->oxcf.rc_cfg.mode` == AOM_CBR, and
- 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are NOT available.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | width | Coded frame width
  [in] | height | Coded frame height
  [out] | bottom_index | Bottom bound for q index (best quality)
  [out] | top_index | Top bound for q index (worst quality)

Returns
  Returns selected q index to be used for encoding this frame.

References AOM_CBR, RATE_CONTROL::best_quality, AV1_COMP::common, AV1Common::current_frame, has_no_stats_stage(), RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, RATE_CONTROL::this_frame_target, and RATE_CONTROL::worst_quality.

Referenced by av1_rc_pick_q_and_bounds().

## ◆ rc_pick_q_and_bounds_no_stats()

static int rc_pick_q_and_bounds_no_stats | ( | const AV1_COMP * | cpi,
|  | int | width,
|  | int | height,
|  | int * | bottom_index,
|  | int * | top_index )
| static

Picks q and q bounds given non-CBR rate control params in `cpi->rc`.

Handles the special case when using:

- Any rate control other than constant bit-rate mode: `cpi->oxcf.rc_cfg.mode` != AOM_CBR, and
- 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are NOT available.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | width | Coded frame width
  [in] | height | Coded frame height
  [out] | bottom_index | Bottom bound for q index (best quality)
  [out] | top_index | Top bound for q index (worst quality)

Returns
  Returns selected q index to be used for encoding this frame.

References RefreshFrameInfo::alt_ref_frame, AOM_CQ, AOM_Q, AOM_VBR, PRIMARY_RATE_CONTROL::avg_frame_qindex, RATE_CONTROL::best_quality, AV1_COMP::common, AV1Common::current_frame, AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, RefreshFrameInfo::golden_frame, has_no_stats_stage(), PRIMARY_RATE_CONTROL::last_boosted_qindex, RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, AV1_COMP::refresh_frame, AV1Common::seq_params, AV1_COMP::superres_mode, AV1Common::superres_scale_denominator, RATE_CONTROL::this_frame_target, and RATE_CONTROL::worst_quality.

Referenced by av1_rc_pick_q_and_bounds().

## ◆ get_q()

static int get_q | ( | const AV1_COMP * | cpi,
|  | const int | width,
|  | const int | height,
|  | const int | active_worst_quality,
|  | const int | active_best_quality )
| static

Gets a Q value to use for the current frame.

Selects a Q value from a permitted range that we estimate will result in approximately the target number of bits.

Parameters

  [in] | cpi | Top level encoder instance structure
  [in] | width | Width of frame
  [in] | height | Height of frame
  [in] | active_worst_quality | Max Q allowed
  [in] | active_best_quality | Min Q allowed

Returns
  The suggested Q for this frame.

References AOM_Q, AV1_COMP::common, RATE_CONTROL::frames_to_key, AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, PRIMARY_RATE_CONTROL::last_boosted_qindex, PRIMARY_RATE_CONTROL::last_kf_qindex, RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, RATE_CONTROL::this_frame_target, and AV1_PRIMARY::twopass.

Referenced by rc_pick_q_and_bounds(), tf_motion_search(), and tf_setup_filtering_buffer().

## ◆ rc_pick_q_and_bounds()

static int rc_pick_q_and_bounds | ( | const AV1_COMP * | cpi,
|  | int | width,
|  | int | height,
|  | int | gf_index,
|  | int * | bottom_index,
|  | int * | top_index )
| static

Picks q and q bounds given rate control parameters in `cpi->rc`.

Handles the general cases not covered by rc_pick_q_and_bounds_no_stats_cbr() and rc_pick_q_and_bounds_no_stats()

Parameters

  [in] | cpi | Top level encoder structure
  [in] | width | Coded frame width
  [in] | height | Coded frame height
  [in] | gf_index | Index of this frame in the golden frame group
  [out] | bottom_index | Bottom bound for q index (best quality)
  [out] | top_index | Top bound for q index (worst quality)

Returns
  Returns selected q index to be used for encoding this frame.

References PRIMARY_RATE_CONTROL::active_best_quality, RATE_CONTROL::active_worst_quality, RefreshFrameInfo::alt_ref_frame, AOM_Q, RATE_CONTROL::best_quality, AV1_COMP::common, get_q(), AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, RefreshFrameInfo::golden_frame, has_no_stats_stage(), RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, AV1_COMP::refresh_frame, AV1_COMP::superres_mode, AV1Common::superres_scale_denominator, and RATE_CONTROL::this_frame_target.

Referenced by av1_rc_pick_q_and_bounds().

## ◆ av1_set_rtc_reference_structure_one_layer()

void av1_set_rtc_reference_structure_one_layer | ( | AV1_COMP * | cpi,
|  | int | gf_update )

Setup the reference prediction structure for 1 pass real-time.

Set the reference prediction structure for 1 layer. Current structure is to use 3 references (LAST, GOLDEN, ALTREF), where ALT_REF always behind current by lag_alt frames, and GOLDEN is either updated on LAST with period baseline_gf_interval (fixed slot) or always behind current by lag_gld (gld_fixed_slot = 0, lag_gld <= 7).

Parameters

  [in] | cpi | Top level encoder structure
  [in] | gf_update | Flag to indicate if GF is updated

Remarks
  Nothing is returned. Instead the settings for the prediction structure are set in `cpi-ext_flags`; and the buffer slot index (for each of 7 references) and refresh flags (for each of the 8 slots) are set in `cpi->svc.ref_idx`[] and `cpi->svc.refresh`[].

References ExtRefreshFrameFlagsInfo::alt_ref_frame, AV1_COMP::common, AV1Common::current_frame, RateControlCfg::drop_frames_water_mark, AV1_COMP::ext_flags, ExtRefreshFrameFlagsInfo::golden_frame, ExtRefreshFrameFlagsInfo::last_frame, AV1_COMP::oxcf, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, ExternalFlags::ref_frame_flags, ExternalFlags::refresh_frame, AV1_COMP::rt_reduce_num_ref_buffers, SPEED_FEATURES::rt_sf, AV1_PRIMARY::rtc_ref, AV1_COMP::sf, and ExtRefreshFrameFlagsInfo::update_pending.

Referenced by av1_encode_strategy().

## ◆ rc_scene_detection_onepass_rt()

static void rc_scene_detection_onepass_rt | ( | AV1_COMP * | cpi,
|  | const EncodeFrameInput * | frame_input )
| static

Check for scene detection, for 1 pass real-time mode.

Compute average source sad (temporal sad: between current source and previous source) over a subset of superblocks. Use this is detect big changes in content and set the `cpi->rc.high_source_sad` flag.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | frame_input | Current and last input source frames

Remarks
  Nothing is returned. Instead the flag `cpi->rc.high_source_sad` is set if scene change is detected, and `cpi->rc.avg_source_sad` is updated.

References AV1_COMP::active_map, AV1_COMP::common, AV1_PRIMARY::fn_ptr, AV1_COMP::framerate, AV1Common::height, SVC::layer_context, CommonModeInfoParams::mi_cols, AV1Common::mi_params, CommonModeInfoParams::mi_rows, AV1_COMP::oxcf, AV1_COMP::ppi, AV1_COMP::rc, AV1Common::render_height, AV1Common::render_width, SPEED_FEATURES::rt_sf, AV1Common::seq_params, AV1_COMP::sf, AV1_COMP::src_sad_blk_64x64, AV1_COMP::svc, and AV1Common::width.

## ◆ rc_spatial_act_keyframe_onepass_rt()

static void rc_spatial_act_keyframe_onepass_rt | ( | AV1_COMP * | cpi,
|  | uint8_t * | src_y,
|  | int | src_ystride )
| static

Compute spatial activity for keyframe, 1 pass real-time mode.

Compute average spatial activity/variance for source frame over a subset of superblocks.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | src_y | Input source buffer for y channel.
  [in] | src_ystride | Input source stride for y channel.

Remarks
  Nothing is returned. Instead the average spatial variance computed is stored in flag `cpi->rc.frame_spatial_variance`.

References AV1_COMP::common, AV1_PRIMARY::fn_ptr, CommonModeInfoParams::mi_cols, AV1Common::mi_params, CommonModeInfoParams::mi_rows, AV1_COMP::ppi, AV1_COMP::rc, and AV1Common::seq_params.

## ◆ set_gf_interval_update_onepass_rt()

static int set_gf_interval_update_onepass_rt | ( | AV1_COMP * | cpi,
|  | FRAME_TYPE | frame_type )
| static

Set the GF baseline interval for 1 pass real-time mode.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | frame_type | frame type

Returns
  Return GF update flag, and update the `cpi->rc` with the next GF interval settings.

References RATE_CONTROL::frames_till_gf_update_due, AV1_COMP::rc, and AV1_COMP::svc.

## ◆ dynamic_resize_one_pass_cbr()

static void dynamic_resize_one_pass_cbr | ( | AV1_COMP * | cpi | ) |
| static

Check for resize based on Q, for 1 pass real-time mode.

Check if we should resize, based on average QP from past x frames. Only allow for resize at most 1/2 scale down for now, Scaling factor for each step may be 3/4 or 1/2.

Parameters

  [in] | cpi | Top level encoder structure

Remarks
  Return resized width/height in `cpi->resize_pending_params`, and update some resize counters in `rc`.

References PRIMARY_RATE_CONTROL::buffer_level, AV1_COMP::common, AV1Common::current_frame, AV1_COMP::framerate, AV1Common::height, PRIMARY_RATE_CONTROL::last_q, PRIMARY_RATE_CONTROL::optimal_buffer_level, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1_COMP::rc, AV1Common::width, and RATE_CONTROL::worst_quality.

## ◆ av1_rc_update_rate_correction_factors()

void av1_rc_update_rate_correction_factors | ( | struct AV1_COMP * | cpi,
|  | int | is_encode_stage,
|  | int | width,
|  | int | height )

Updates the rate correction factor linking Q to output bits.

This function updates the Q rate correction factor after an encode cycle depending on whether we overshot or undershot the target rate.

Parameters

  [in] | cpi | Top level encoder instance structure
  [in] | is_encode_stage | Indicates if recode loop or post-encode
  [in] | width | Frame width
  [in] | height | Frame height

Remarks
  Updates the relevant rate correction factor in cpi->rc

References av1_cyclic_refresh_estimate_bits_at_q(), CommonQuantParams::base_qindex, AV1_COMP::common, AV1_COMP::cyclic_refresh, get_rate_correction_factor(), AV1_COMP::is_screen_content_type, AV1_COMP::oxcf, CYCLIC_REFRESH::percent_refresh_adjustment, AV1_COMP::ppi, RATE_CONTROL::projected_frame_size, AV1Common::quant_params, CYCLIC_REFRESH::rate_ratio_qdelta_adjustment, AV1_COMP::rc, AV1Common::seg, set_rate_correction_factor(), RATE_CONTROL::this_frame_target, and AV1_PRIMARY::use_svc.

Referenced by recode_loop_update_q().

## ◆ av1_rc_pick_q_and_bounds()

int av1_rc_pick_q_and_bounds | ( | struct AV1_COMP * | cpi,
|  | int | width,
|  | int | height,
|  | int | gf_index,
|  | int * | bottom_index,
|  | int * | top_index )

Picks q and q bounds given the rate control parameters in `cpi->rc`.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | width | Coded frame width
  [in] | height | Coded frame height
  [in] | gf_index | Index of this frame in the golden frame group
  [out] | bottom_index | Bottom bound for q index (best quality)
  [out] | top_index | Top bound for q index (worst quality)

Returns
  Returns selected q index to be used for encoding this frame. Also, updates `rc->arf_q`.

References HIGH_LEVEL_SPEED_FEATURES::accurate_bit_estimate, RATE_CONTROL::active_worst_quality, AOM_CBR, AOM_CQ, AOM_Q, PRIMARY_RATE_CONTROL::arf_q, AV1_COMP::common, AV1Common::current_frame, AV1_PRIMARY::gf_group, has_no_stats_stage(), SPEED_FEATURES::hl_sf, RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, rc_pick_q_and_bounds(), rc_pick_q_and_bounds_no_stats(), rc_pick_q_and_bounds_no_stats_cbr(), AV1_COMP::rec_sse, and AV1_COMP::sf.

## ◆ av1_rc_regulate_q()

int av1_rc_regulate_q | ( | const struct AV1_COMP * | cpi,
|  | int | target_bits_per_frame,
|  | int | active_best_quality,
|  | int | active_worst_quality,
|  | int | width,
|  | int | height )

Estimates q to achieve a target bits per frame.

Parameters

  [in] | cpi | Top level encoder instance structure
  [in] | target_bits_per_frame | Frame rate target
  [in] | active_worst_quality | Max Q allowed
  [in] | active_best_quality | Min Q allowed
  [in] | width | Frame width
  [in] | height | Frame height

Returns
  Returns a q index value

## ◆ 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.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | frame_update_type | Type of frame

Returns
  Returns the target number of bits for this frame.

## ◆ 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.

Parameters

  [in] | cpi | Top level encoder structure

Returns
  Returns the target number of bits for this frame.

## ◆ 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.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | frame_update_type | Type of frame

Returns
  Returns the target number of bits for this frame.

## ◆ 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.

Parameters

  [in] | cpi | Top level encoder structure

Returns
  Returns the target number of bits for this frame.

## ◆ av1_get_one_pass_rt_params()

void av1_get_one_pass_rt_params | ( | struct AV1_COMP * | cpi,
|  | FRAME_TYPE *const | frame_type,
|  | const struct EncodeFrameInput * | frame_input,
|  | unsigned int | frame_flags )

Setup the rate control parameters for 1 pass real-time mode.

- Sets the frame type and target frame size.
- Sets the GF update.
- Checks for scene change.
- Sets the reference prediction structure for 1 layers (non-SVC).
- Resets and updates are done for SVC.

Parameters

  [in] | cpi | Top level encoder structure
  [in] | frame_type | Encoder frame type
  [in] | frame_input | Current and last input source frames
  [in] | frame_flags | Encoder frame flags

Remarks
  Nothing is returned. Instead the settings computed in this function are set in: `frame_params`, `cpi->common`, `cpi->rc`, `cpi->svc`.

## ◆ av1_encodedframe_overshoot_cbr()

int av1_encodedframe_overshoot_cbr | ( | struct AV1_COMP * | cpi,
|  | int * | q )

Increase q on expected encoder overshoot, for CBR mode.

Handles the case when encoder is expected to create a large frame:

- q is increased to value closer to `cpi->rc.worst_quality`
- avg_frame_qindex is reset
- buffer levels are reset
- rate correction factor is adjusted

Parameters

  [in] | cpi | Top level encoder structure
  [in] | q | Current q index

Returns
  q is returned, and updates are done to `cpi->rc`.

References PRIMARY_RATE_CONTROL::avg_frame_qindex, PRIMARY_RATE_CONTROL::bits_off_target, PRIMARY_RATE_CONTROL::buffer_level, AV1_COMP::common, AV1_COMP::cyclic_refresh, SVC::layer_context, CommonModeInfoParams::MBs, AV1Common::mi_params, PRIMARY_RATE_CONTROL::optimal_buffer_level, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, PRIMARY_RATE_CONTROL::rate_correction_factors, AV1_COMP::rc, SPEED_FEATURES::rt_sf, AV1Common::seq_params, AV1_COMP::sf, AV1_COMP::svc, and RATE_CONTROL::worst_quality.

Referenced by encode_without_recode().

## ◆ av1_postencode_drop_cbr()

int av1_postencode_drop_cbr | ( | struct AV1_COMP * | cpi,
|  | size_t * | size )

Check if frame should be dropped, for RTC mode.

Parameters

  [in] | cpi | Top level encoder structure
  [in,out] | size | Size of encoded frame

Returns
  1 if frame is to be dropped, 0 otherwise (no drop). Set cpi->rc.force_max_q if frame is to be dropped, and updates are made to rate control parameters. *size is set to 0 when this function returns 1 (frame is dropped).

References PRIMARY_RATE_CONTROL::avg_frame_qindex, CommonQuantParams::base_qindex, PRIMARY_RATE_CONTROL::bits_off_target, PRIMARY_RATE_CONTROL::buffer_level, AV1_COMP::common, AV1_COMP::is_dropped_frame, SVC::layer_context, PRIMARY_RATE_CONTROL::optimal_buffer_level, AV1_PRIMARY::p_rc, AV1_COMP::ppi, AV1Common::quant_params, AV1_COMP::rc, AV1_COMP::svc, and RATE_CONTROL::worst_quality.

Referenced by encode_with_recode_loop_and_filter().

## ◆ recode_loop_test()

static int recode_loop_test | ( | AV1_COMP * | cpi,
|  | int | high_limit,
|  | int | low_limit,
|  | int | q,
|  | int | maxq,
|  | int | minq )
| inline static

Function to test for conditions that indicate we should loop back and recode a frame.

Parameters

  [in] | cpi | Top-level encoder structure
  [in] | high_limit | Upper rate threshold
  [in] | low_limit | Lower rate threshold
  [in] | q | Current q index
  [in] | maxq | Maximum allowed q index
  [in] | minq | Minimum allowed q index

Returns
  Indicates if a recode is required.

Return values

  1 | Recode Required
  0 | No Recode required

References AOM_CQ, RateControlCfg::cq_level, SPEED_FEATURES::hl_sf, RateControlCfg::mode, AV1_COMP::oxcf, RATE_CONTROL::projected_frame_size, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, HIGH_LEVEL_SPEED_FEATURES::recode_loop, AV1_COMP::sf, and RATE_CONTROL::this_frame_target.

Referenced by recode_loop_update_q().

## ◆ recode_loop_update_q()

static void recode_loop_update_q | ( | AV1_COMP *const | cpi,
|  | int *const | loop,
|  | int *const | q,
|  | int *const | q_low,
|  | int *const | q_high,
|  | const int | top_index,
|  | const int | bottom_index,
|  | int *const | undershoot_seen,
|  | int *const | overshoot_seen,
|  | int *const | low_cr_seen,
|  | const int | loop_count )
| inline static

Called after encode_with_recode_loop() has just encoded a frame. This function works out whether we undershot or overshot our bitrate target and adjusts q as appropriate. It also decides whether or not we need to recode the frame to get closer to the target rate.

Parameters

  [in] | cpi | Top-level encoder structure
  [out] | loop | Should we go around the recode loop again
  [in,out] | q | New q index value
  [in,out] | q_low | Low q index limit for this loop itteration
  [in,out] | q_high | High q index limit for this loop itteration
  [in] | top_index | Max permited new value for q index
  [in] | bottom_index | Min permited new value for q index
  [in,out] | undershoot_seen | Have we seen undershoot on this frame
  [in,out] | overshoot_seen | Have we seen overshoot on this frame
  [in,out] | low_cr_seen | Have we previously trriggered recode because the compression ration was less than a given minimum threshold.
  [in] | loop_count | Loop itterations so far.

References AV1_COMP::ambient_err, AOM_CQ, AOM_Q, av1_rc_update_rate_correction_factors(), RATE_CONTROL::best_quality, AV1_COMP::common, AV1Common::cur_frame, AV1Common::current_frame, AV1_COMP::gf_frame_index, AV1_PRIMARY::gf_group, AV1Common::height, AV1_PRIMARY::level_params, RateControlCfg::min_cr, RateControlCfg::mode, AV1_COMP::oxcf, AV1_PRIMARY::p_rc, AV1_COMP::ppi, RATE_CONTROL::projected_frame_size, AV1_COMP::rc, AV1EncoderConfig::rc_cfg, recode_loop_test(), AV1Common::seq_params, AV1_COMP::source, RATE_CONTROL::this_frame_target, AV1Common::width, and RATE_CONTROL::worst_quality.

Referenced by encode_with_recode_loop().

## ◆ get_layer_context()

static LAYER_CONTEXT * get_layer_context | ( | AV1_COMP *const | cpi | ) |
| static

Return layer context for current layer.

Parameters

  [in] | cpi | Top level encoder structure

Returns
  LAYER_CONTEXT for current layer.

References SVC::layer_context, and AV1_COMP::svc.

Referenced by av1_restore_layer_context(), av1_save_layer_context(), and av1_update_temporal_layer_framerate().