#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aom_dsp/aom_dsp_common.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/mem.h"
#include "aom_ports/aom_once.h"
#include "av1/common/alloccommon.h"
#include "av1/encoder/aq_cyclicrefresh.h"
#include "av1/common/common.h"
#include "av1/common/entropymode.h"
#include "av1/common/quant_common.h"
#include "av1/common/seg_common.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/encoder_utils.h"
#include "av1/encoder/encode_strategy.h"
#include "av1/encoder/gop_structure.h"
#include "av1/encoder/mcomp.h"
#include "av1/encoder/random.h"
#include "av1/encoder/ratectrl.h"
#include "config/aom_dsp_rtcd.h"
#define USE_UNRESTRICTED_Q_IN_CQ_MODE …
#define MAX_MB_RATE …
#define MAXRATE_1080P …
#define MIN_BPB_FACTOR …
#define MAX_BPB_FACTOR …
#define SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO …
#define SUPERRES_QADJ_PER_DENOM_KEYFRAME …
#define SUPERRES_QADJ_PER_DENOM_ARFFRAME …
#define FRAME_OVERHEAD_BITS …
#define ASSIGN_MINQ_TABLE(bit_depth, name) …
static int kf_low_motion_minq_8[QINDEX_RANGE];
static int kf_high_motion_minq_8[QINDEX_RANGE];
static int arfgf_low_motion_minq_8[QINDEX_RANGE];
static int arfgf_high_motion_minq_8[QINDEX_RANGE];
static int inter_minq_8[QINDEX_RANGE];
static int rtc_minq_8[QINDEX_RANGE];
static int kf_low_motion_minq_10[QINDEX_RANGE];
static int kf_high_motion_minq_10[QINDEX_RANGE];
static int arfgf_low_motion_minq_10[QINDEX_RANGE];
static int arfgf_high_motion_minq_10[QINDEX_RANGE];
static int inter_minq_10[QINDEX_RANGE];
static int rtc_minq_10[QINDEX_RANGE];
static int kf_low_motion_minq_12[QINDEX_RANGE];
static int kf_high_motion_minq_12[QINDEX_RANGE];
static int arfgf_low_motion_minq_12[QINDEX_RANGE];
static int arfgf_high_motion_minq_12[QINDEX_RANGE];
static int inter_minq_12[QINDEX_RANGE];
static int rtc_minq_12[QINDEX_RANGE];
static int gf_high = …;
static int gf_low = …;
#ifdef STRICT_RC
static int kf_high = 3200;
#else
static int kf_high = …;
#endif
static int kf_low = …;
static double resize_rate_factor(const FrameDimensionCfg *const frm_dim_cfg,
int width, int height) { … }
static int get_minq_index(double maxq, double x3, double x2, double x1,
aom_bit_depth_t bit_depth) { … }
static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low,
int *arfgf_high, int *inter, int *rtc,
aom_bit_depth_t bit_depth) { … }
static void rc_init_minq_luts(void) { … }
void av1_rc_init_minq_luts(void) { … }
double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) { … }
static int get_bpmb_enumerator(FRAME_TYPE frame_type,
const int is_screen_content_type) { … }
static int get_init_ratio(double sse) { … }
static int adjust_rtc_keyframe(const RATE_CONTROL *rc, int enumerator) { … }
int av1_rc_bits_per_mb(const AV1_COMP *cpi, FRAME_TYPE frame_type, int qindex,
double correction_factor, int accurate_estimate) { … }
int av1_estimate_bits_at_q(const AV1_COMP *cpi, int q,
double correction_factor) { … }
static int clamp_pframe_target_size(const AV1_COMP *const cpi, int64_t target,
FRAME_UPDATE_TYPE frame_update_type) { … }
static int clamp_iframe_target_size(const AV1_COMP *const cpi, int64_t target) { … }
static void update_layer_buffer_level(SVC *svc, int encoded_frame_size,
bool is_screen) { … }
static void update_buffer_level(AV1_COMP *cpi, int encoded_frame_size) { … }
int av1_rc_get_default_min_gf_interval(int width, int height,
double framerate) { … }
static int get_default_max_gf_interval(double framerate, int min_gf_interval) { … }
void av1_primary_rc_init(const AV1EncoderConfig *oxcf,
PRIMARY_RATE_CONTROL *p_rc) { … }
void av1_rc_init(const AV1EncoderConfig *oxcf, RATE_CONTROL *rc) { … }
static bool check_buffer_below_thresh(AV1_COMP *cpi, int64_t buffer_level,
int drop_mark) { … }
int av1_rc_drop_frame(AV1_COMP *cpi) { … }
static int adjust_q_cbr(const AV1_COMP *cpi, int q, int active_worst_quality,
int width, int height) { … }
static const RATE_FACTOR_LEVEL rate_factor_levels[FRAME_UPDATE_TYPES] = …;
static RATE_FACTOR_LEVEL get_rate_factor_level(const GF_GROUP *const gf_group,
int gf_frame_index) { … }
static double get_rate_correction_factor(const AV1_COMP *cpi, int width,
int height) { … }
static void set_rate_correction_factor(AV1_COMP *cpi, int is_encode_stage,
double factor, int width, int height) { … }
void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, int is_encode_stage,
int width, int height) { … }
static int get_bits_per_mb(const AV1_COMP *cpi, int use_cyclic_refresh,
double correction_factor, int q) { … }
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) { … }
int av1_rc_regulate_q(const AV1_COMP *cpi, int target_bits_per_frame,
int active_best_quality, int active_worst_quality,
int width, int height) { … }
static int get_active_quality(int q, int gfu_boost, int low, int high,
int *low_motion_minq, int *high_motion_minq) { … }
static int get_kf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q,
aom_bit_depth_t bit_depth) { … }
static int get_gf_active_quality_no_rc(int gfu_boost, int q,
aom_bit_depth_t bit_depth) { … }
static int get_gf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q,
aom_bit_depth_t bit_depth) { … }
static int get_gf_high_motion_quality(int q, aom_bit_depth_t bit_depth) { … }
static int calc_active_worst_quality_no_stats_vbr(const AV1_COMP *cpi) { … }
static int calc_active_worst_quality_no_stats_cbr(const AV1_COMP *cpi) { … }
static int calc_active_best_quality_no_stats_cbr(const AV1_COMP *cpi,
int active_worst_quality,
int width, int height) { … }
#if RT_PASSIVE_STRATEGY
static int get_q_passive_strategy(const AV1_COMP *const cpi,
const int q_candidate, const int threshold) {
const AV1_COMMON *const cm = &cpi->common;
const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
const CurrentFrame *const current_frame = &cm->current_frame;
int sum = 0;
int count = 0;
int i = 1;
while (i < MAX_Q_HISTORY) {
int frame_id = current_frame->frame_number - i;
if (frame_id <= 0) break;
sum += p_rc->q_history[frame_id % MAX_Q_HISTORY];
++count;
++i;
}
if (count > 0) {
const int avg_q = sum / count;
if (abs(avg_q - q_candidate) <= threshold) return avg_q;
}
return q_candidate;
}
#endif
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 int gf_group_pyramid_level(const GF_GROUP *gf_group, int gf_index) { … }
static int get_active_cq_level(const RATE_CONTROL *rc,
const PRIMARY_RATE_CONTROL *p_rc,
const AV1EncoderConfig *const oxcf,
int intra_only, aom_superres_mode superres_mode,
int superres_denom) { … }
static int rc_pick_q_and_bounds_no_stats(const AV1_COMP *cpi, int width,
int height, int *bottom_index,
int *top_index) { … }
static const double arf_layer_deltas[MAX_ARF_LAYERS + 1] = …;
static int frame_type_qdelta(const AV1_COMP *cpi, int q) { … }
#if USE_UNRESTRICTED_Q_IN_CQ_MODE
static int rc_pick_q_and_bounds_no_stats_cq(const AV1_COMP *cpi, int width,
int height, int *bottom_index,
int *top_index) {
const AV1_COMMON *const cm = &cpi->common;
const RATE_CONTROL *const rc = &cpi->rc;
const AV1EncoderConfig *const oxcf = &cpi->oxcf;
const int cq_level =
get_active_cq_level(rc, oxcf, frame_is_intra_only(cm), cpi->superres_mode,
cm->superres_scale_denominator);
const int bit_depth = cm->seq_params->bit_depth;
const int q = (int)av1_convert_qindex_to_q(cq_level, bit_depth);
(void)width;
(void)height;
assert(has_no_stats_stage(cpi));
assert(cpi->oxcf.rc_cfg.mode == AOM_CQ);
*top_index = q;
*bottom_index = q;
return q;
}
#endif
#define STATIC_MOTION_THRESH …
static void get_intra_q_and_bounds(const AV1_COMP *cpi, int width, int height,
int *active_best, int *active_worst,
int cq_level) { … }
static void adjust_active_best_and_worst_quality(const AV1_COMP *cpi,
const int is_intrl_arf_boost,
int *active_worst,
int *active_best) { … }
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 int get_active_best_quality(const AV1_COMP *const cpi,
const int active_worst_quality,
const int cq_level, const int gf_index) { … }
int av1_q_mode_get_q_index(int base_q_index, int gf_update_type,
int gf_pyramid_level, int arf_q) { … }
static int rc_pick_q_and_bounds_q_mode(const AV1_COMP *cpi, int width,
int height, int gf_index,
int *bottom_index, int *top_index) { … }
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 void rc_compute_variance_onepass_rt(AV1_COMP *cpi) { … }
int av1_rc_pick_q_and_bounds(AV1_COMP *cpi, int width, int height, int gf_index,
int *bottom_index, int *top_index) { … }
void av1_rc_compute_frame_size_bounds(const AV1_COMP *cpi, int frame_target,
int *frame_under_shoot_limit,
int *frame_over_shoot_limit) { … }
void av1_rc_set_frame_target(AV1_COMP *cpi, int target, int width, int height) { … }
static void update_alt_ref_frame_stats(AV1_COMP *cpi) { … }
static void update_golden_frame_stats(AV1_COMP *cpi) { … }
void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) { … }
void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) { … }
int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth,
int best_qindex, int worst_qindex) { … }
int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
aom_bit_depth_t bit_depth) { … }
static int find_qindex_by_rate(const AV1_COMP *const cpi,
int desired_bits_per_mb, FRAME_TYPE frame_type,
int best_qindex, int worst_qindex) { … }
int av1_compute_qdelta_by_rate(const AV1_COMP *cpi, FRAME_TYPE frame_type,
int qindex, double rate_target_ratio) { … }
static void set_gf_interval_range(const AV1_COMP *const cpi,
RATE_CONTROL *const rc) { … }
void av1_rc_update_framerate(AV1_COMP *cpi, int width, int height) { … }
#define VBR_PCT_ADJUSTMENT_LIMIT …
static void vbr_rate_correction(AV1_COMP *cpi, int *this_frame_target) { … }
void av1_set_target_rate(AV1_COMP *cpi, int width, int height) { … }
int av1_calc_pframe_target_size_one_pass_vbr(
const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) { … }
int av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) { … }
int av1_calc_pframe_target_size_one_pass_cbr(
const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) { … }
int av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) { … }
static void set_golden_update(AV1_COMP *const cpi) { … }
static void set_baseline_gf_interval(AV1_COMP *cpi, FRAME_TYPE frame_type) { … }
void av1_adjust_gf_refresh_qp_one_pass_rt(AV1_COMP *cpi) { … }
void av1_set_rtc_reference_structure_one_layer(AV1_COMP *cpi, int gf_update) { … }
static int set_block_is_active(unsigned char *const active_map_4x4, int mi_cols,
int mi_rows, int sbi_col, int sbi_row) { … }
static unsigned int estimate_scroll_motion(
const AV1_COMP *cpi, uint8_t *src_buf, uint8_t *last_src_buf,
int src_stride, int ref_stride, BLOCK_SIZE bsize, int pos_col, int pos_row,
int *best_intmv_col, int *best_intmv_row) { … }
static void rc_scene_detection_onepass_rt(AV1_COMP *cpi,
const EncodeFrameInput *frame_input) { … }
static const uint8_t AV1_VAR_OFFS[MAX_SB_SIZE] = …;
static void rc_spatial_act_keyframe_onepass_rt(AV1_COMP *cpi, uint8_t *src_y,
int src_ystride) { … }
static int set_gf_interval_update_onepass_rt(AV1_COMP *cpi,
FRAME_TYPE frame_type) { … }
static void resize_reset_rc(AV1_COMP *cpi, int resize_width, int resize_height,
int prev_width, int prev_height) { … }
static void dynamic_resize_one_pass_cbr(AV1_COMP *cpi) { … }
static inline int set_key_frame(AV1_COMP *cpi, unsigned int frame_flags) { … }
static bool set_flag_rps_bias_recovery_frame(const AV1_COMP *const cpi) { … }
void av1_get_one_pass_rt_params(AV1_COMP *cpi, FRAME_TYPE *const frame_type,
const EncodeFrameInput *frame_input,
unsigned int frame_flags) { … }
#define CHECK_INTER_LAYER_PRED(ref_frame) …
int av1_encodedframe_overshoot_cbr(AV1_COMP *cpi, int *q) { … }
int av1_postencode_drop_cbr(AV1_COMP *cpi, size_t *size) { … }