/* * Copyright (c) 2010 The WebM project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include <assert.h>#include <limits.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h> #include "./vpx_dsp_rtcd.h"#include "vpx_dsp/vpx_dsp_common.h"#include "vpx_mem/vpx_mem.h"#include "vpx_ports/mem.h"#include "vpx_ports/system_state.h" #include "vp9/common/vp9_alloccommon.h"#include "vp9/encoder/vp9_aq_cyclicrefresh.h"#include "vp9/common/vp9_common.h"#include "vp9/common/vp9_entropymode.h"#include "vp9/common/vp9_quant_common.h"#include "vp9/common/vp9_seg_common.h" #include "vp9/encoder/vp9_encodemv.h"#include "vp9/encoder/vp9_ratectrl.h" // Max rate per frame for 1080P and below encodes if no level requirement given.// For larger formats limit to MAX_MB_RATE bits per MB// 4Mbits is derived from the level requirement for level 4 (1080P 30) which// requires that HW can sustain a rate of 16Mbits over a 4 frame group.// If a lower level requirement is specified then this may over ride this value.#define MAX_MB_RATE 250#define MAXRATE_1080P 4000000 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1 #define MIN_BPB_FACTOR 0.005#define MAX_BPB_FACTOR 50 #if CONFIG_VP9_HIGHBITDEPTH#define ASSIGN_MINQ_TABLE(bit_depth, name) \ do { \ switch (bit_depth) { \ case VPX_BITS_8: name = name##_8; break; \ case VPX_BITS_10: name = name##_10; break; \ default: \ assert(bit_depth == VPX_BITS_12); \ name = name##_12; \ break; \ } \ } while (0)#else#define ASSIGN_MINQ_TABLE(bit_depth, name) \ do { \ (void)bit_depth; \ name = name##_8; \ } while (0)#endif // Tables relating active max Q to active min Qstatic 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]; #if CONFIG_VP9_HIGHBITDEPTHstatic 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];#endif #ifdef AGGRESSIVE_VBRstatic int gf_high = 2400;static int gf_low = 400;static int kf_high = 4000;static int kf_low = 400;#elsestatic int gf_high = 2000;static int gf_low = 400;static int kf_high = 4800;static int kf_low = 300;#endif // Functions to compute the active minq lookup table entries based on a// formulaic approach to facilitate easier adjustment of the Q tables.// The formulae were derived from computing a 3rd order polynomial best// fit to the original data (after plotting real maxq vs minq (not q index))static int get_minq_index(double maxq, double x3, double x2, double x1, vpx_bit_depth_t bit_depth) { int i; const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq); // Special case handling to deal with the step from q2.0 // down to lossless mode represented by q 1.0. if (minqtarget <= 2.0) return 0; for (i = 0; i < QINDEX_RANGE; i++) { if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) return i; } return QINDEX_RANGE - 1;} static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low, int *arfgf_high, int *inter, int *rtc, vpx_bit_depth_t bit_depth) { int i; for (i = 0; i < QINDEX_RANGE; i++) { const double maxq = vp9_convert_qindex_to_q(i, bit_depth); kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth); kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth);#ifdef AGGRESSIVE_VBR arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth); inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth);#else arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth); inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth);#endif arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); }} void vp9_rc_init_minq_luts(void) { init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8, arfgf_low_motion_minq_8, arfgf_high_motion_minq_8, inter_minq_8, rtc_minq_8, VPX_BITS_8);#if CONFIG_VP9_HIGHBITDEPTH init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10, arfgf_low_motion_minq_10, arfgf_high_motion_minq_10, inter_minq_10, rtc_minq_10, VPX_BITS_10); init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12, arfgf_low_motion_minq_12, arfgf_high_motion_minq_12, inter_minq_12, rtc_minq_12, VPX_BITS_12);#endif} // These functions use formulaic calculations to make playing with the// quantizer tables easier. If necessary they can be replaced by lookup// tables if and when things settle down in the experimental bitstreamdouble vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) {// Convert the index to a real Q value (scaled down to match old Q values)#if CONFIG_VP9_HIGHBITDEPTH switch (bit_depth) { case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0; case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0; default: assert(bit_depth == VPX_BITS_12); return vp9_ac_quant(qindex, 0, bit_depth) / 64.0; }#else return vp9_ac_quant(qindex, 0, bit_depth) / 4.0;#endif} int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) { int i; for (i = 0; i < QINDEX_RANGE; ++i) if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break; if (i == QINDEX_RANGE) i--; return i;} int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, double correction_factor, vpx_bit_depth_t bit_depth) { const double q = vp9_convert_qindex_to_q(qindex, bit_depth); int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000; assert(correction_factor <= MAX_BPB_FACTOR && correction_factor >= MIN_BPB_FACTOR); // q based adjustment to baseline enumerator enumerator += (int)(enumerator * q) >> 12; return (int)(enumerator * correction_factor / q);} int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, double correction_factor, vpx_bit_depth_t bit_depth) { const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth)); return VPXMAX(FRAME_OVERHEAD_BITS, (int)(((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS));} int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { const RATE_CONTROL *rc = &cpi->rc; const VP9EncoderConfig *oxcf = &cpi->oxcf; const int min_frame_target = VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5); if (target < min_frame_target) target = min_frame_target; if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { // If there is an active ARF at this location use the minimum // bits on this frame even if it is a constructed arf. // The active maximum quantizer insures that an appropriate // number of bits will be spent if needed for constructed ARFs. target = min_frame_target; } // Clip the frame target to the maximum allowed value. if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; if (oxcf->rc_max_inter_bitrate_pct) { const int max_rate = rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100; target = VPXMIN(target, max_rate); } return target;} int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { const RATE_CONTROL *rc = &cpi->rc; const VP9EncoderConfig *oxcf = &cpi->oxcf; if (oxcf->rc_max_intra_bitrate_pct) { const int max_rate = rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100; target = VPXMIN(target, max_rate); } if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; return target;} // TODO(marpan/jianj): bits_off_target and buffer_level are used in the same// way for CBR mode, for the buffering updates below. Look into removing one// of these (i.e., bits_off_target).// Update the buffer level before encoding with the per-frame-bandwidth,void vp9_update_buffer_level_preencode(VP9_COMP *cpi) { RATE_CONTROL *const rc = &cpi->rc; rc->bits_off_target += rc->avg_frame_bandwidth; // Clip the buffer level to the maximum specified buffer size. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); rc->buffer_level = rc->bits_off_target;} // Update the buffer level before encoding with the per-frame-bandwidth// for SVC. The current and all upper temporal layers are updated, needed// for the layered rate control which involves cumulative buffer levels for// the temporal layers. Allow for using the timestamp(pts) delta for the// framerate when the set_ref_frame_config is used.static void update_buffer_level_svc_preencode(VP9_COMP *cpi) { SVC *const svc = &cpi->svc; int i; // Set this to 1 to use timestamp delta for "framerate" under // ref_frame_config usage. int use_timestamp = 1; const int64_t ts_delta = svc->time_stamp_superframe - svc->time_stamp_prev[svc->spatial_layer_id]; for (i = svc->temporal_layer_id; i < svc->number_temporal_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers); LAYER_CONTEXT *const lc = &svc->layer_context[layer]; RATE_CONTROL *const lrc = &lc->rc; if (use_timestamp && cpi->svc.use_set_ref_frame_config && svc->number_temporal_layers == 1 && ts_delta > 0 && svc->current_superframe > 0) { // TODO(marpan): This may need to be modified for temporal layers. const double framerate_pts = 10000000.0 / ts_delta; lrc->bits_off_target += (int)round(lc->target_bandwidth / framerate_pts); } else { lrc->bits_off_target += (int)round(lc->target_bandwidth / lc->framerate); } // Clip buffer level to maximum buffer size for the layer. lrc->bits_off_target = VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size); lrc->buffer_level = lrc->bits_off_target; if (i == svc->temporal_layer_id) { cpi->rc.bits_off_target = lrc->bits_off_target; cpi->rc.buffer_level = lrc->buffer_level; } }} // Update the buffer level for higher temporal layers, given the encoded current// temporal layer.static void update_layer_buffer_level_postencode(SVC *svc, int encoded_frame_size) { int i = 0; const int current_temporal_layer = svc->temporal_layer_id; for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers); LAYER_CONTEXT *lc = &svc->layer_context[layer]; RATE_CONTROL *lrc = &lc->rc; lrc->bits_off_target -= encoded_frame_size; // Clip buffer level to maximum buffer size for the layer. lrc->bits_off_target = VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size); lrc->buffer_level = lrc->bits_off_target; }} // Update the buffer level after encoding with encoded frame size.static void update_buffer_level_postencode(VP9_COMP *cpi, int encoded_frame_size) { RATE_CONTROL *const rc = &cpi->rc; rc->bits_off_target -= encoded_frame_size; // Clip the buffer level to the maximum specified buffer size. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); // For screen-content mode, and if frame-dropper is off, don't let buffer // level go below threshold, given here as -rc->maximum_ buffer_size. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && cpi->oxcf.drop_frames_water_mark == 0) rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size); rc->buffer_level = rc->bits_off_target; if (is_one_pass_cbr_svc(cpi)) { update_layer_buffer_level_postencode(&cpi->svc, encoded_frame_size); }} int vp9_rc_get_default_min_gf_interval(int width, int height, double framerate) { // Assume we do not need any constraint lower than 4K 20 fps static const double factor_safe = 3840 * 2160 * 20.0; const double factor = width * height * framerate; const int default_interval = clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL); if (factor <= factor_safe) return default_interval; else return VPXMAX(default_interval, (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5)); // Note this logic makes: // 4K24: 5 // 4K30: 6 // 4K60: 12} int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) { int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75)); interval += (interval & 0x01); // Round to even value return VPXMAX(interval, min_gf_interval);} void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { int i; if (pass == 0 && oxcf->rc_mode == VPX_CBR) { rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; } else { rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; } rc->last_q[KEY_FRAME] = oxcf->best_allowed_q; rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; rc->buffer_level = rc->starting_buffer_level; rc->bits_off_target = rc->starting_buffer_level; rc->rolling_target_bits = rc->avg_frame_bandwidth; rc->rolling_actual_bits = rc->avg_frame_bandwidth; rc->long_rolling_target_bits = rc->avg_frame_bandwidth; rc->long_rolling_actual_bits = rc->avg_frame_bandwidth; rc->total_actual_bits = 0; rc->total_target_bits = 0; rc->total_target_vs_actual = 0; rc->avg_frame_low_motion = 0; rc->count_last_scene_change = 0; rc->af_ratio_onepass_vbr = 10; rc->prev_avg_source_sad_lag = 0; rc->high_source_sad = 0; rc->reset_high_source_sad = 0; rc->high_source_sad_lagindex = -1; rc->high_num_blocks_with_motion = 0; rc->hybrid_intra_scene_change = 0; rc->re_encode_maxq_scene_change = 0; rc->alt_ref_gf_group = 0; rc->last_frame_is_src_altref = 0; rc->fac_active_worst_inter = 150; rc->fac_active_worst_gf = 100; rc->force_qpmin = 0; for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0; rc->frames_to_key = 0; rc->frames_since_key = 8; // Sensible default for first frame. rc->this_key_frame_forced = 0; rc->next_key_frame_forced = 0; rc->source_alt_ref_pending = 0; rc->source_alt_ref_active = 0; rc->frames_till_gf_update_due = 0; rc->constrain_gf_key_freq_onepass_vbr = 1; rc->ni_av_qi = oxcf->worst_allowed_q; rc->ni_tot_qi = 0; rc->ni_frames = 0; rc->tot_q = 0.0; rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth); for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { rc->rate_correction_factors[i] = 1.0; rc->damped_adjustment[i] = 0; } rc->min_gf_interval = oxcf->min_gf_interval; rc->max_gf_interval = oxcf->max_gf_interval; if (rc->min_gf_interval == 0) rc->min_gf_interval = vp9_rc_get_default_min_gf_interval( oxcf->width, oxcf->height, oxcf->init_framerate); if (rc->max_gf_interval == 0) rc->max_gf_interval = vp9_rc_get_default_max_gf_interval( oxcf->init_framerate, rc->min_gf_interval); rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2; if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) { rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL; } else { rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH; } rc->force_max_q = 0; rc->last_post_encode_dropped_scene_change = 0; rc->use_post_encode_drop = 0; rc->ext_use_post_encode_drop = 0; rc->disable_overshoot_maxq_cbr = 0; rc->arf_active_best_quality_adjustment_factor = 1.0; rc->arf_increase_active_best_quality = 0; rc->preserve_arf_as_gld = 0; rc->preserve_next_arf_as_gld = 0; rc->show_arf_as_gld = 0;} static int check_buffer_above_thresh(VP9_COMP *cpi, int drop_mark) { SVC *svc = &cpi->svc; if (!cpi->use_svc || cpi->svc.framedrop_mode != FULL_SUPERFRAME_DROP) { RATE_CONTROL *const rc = &cpi->rc; return (rc->buffer_level > drop_mark); } else { int i; // For SVC in the FULL_SUPERFRAME_DROP): the condition on // buffer (if its above threshold, so no drop) is checked on current and // upper spatial layers. If any spatial layer is not above threshold then // we return 0. for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, svc->number_temporal_layers); LAYER_CONTEXT *lc = &svc->layer_context[layer]; RATE_CONTROL *lrc = &lc->rc; // Exclude check for layer whose bitrate is 0. if (lc->target_bandwidth > 0) { const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] * lrc->optimal_buffer_level / 100); if (!(lrc->buffer_level > drop_mark_layer)) return 0; } } return 1; }} static int check_buffer_below_thresh(VP9_COMP *cpi, int drop_mark) { SVC *svc = &cpi->svc; if (!cpi->use_svc || cpi->svc.framedrop_mode == LAYER_DROP) { RATE_CONTROL *const rc = &cpi->rc; return (rc->buffer_level <= drop_mark); } else { int i; // For SVC in the constrained framedrop mode (svc->framedrop_mode = // CONSTRAINED_LAYER_DROP or FULL_SUPERFRAME_DROP): the condition on // buffer (if its below threshold, so drop frame) is checked on current // and upper spatial layers. For FULL_SUPERFRAME_DROP mode if any // spatial layer is <= threshold, then we return 1 (drop). for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, svc->number_temporal_layers); LAYER_CONTEXT *lc = &svc->layer_context[layer]; RATE_CONTROL *lrc = &lc->rc; // Exclude check for layer whose bitrate is 0. if (lc->target_bandwidth > 0) { const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] * lrc->optimal_buffer_level / 100); if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP) { if (lrc->buffer_level <= drop_mark_layer) return 1; } else {Lines 1–500 of 3296. Load more
/* * Copyright (c) 2010 The WebM project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include <assert.h> #include <limits.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "./vpx_dsp_rtcd.h" #include "vpx_dsp/vpx_dsp_common.h" #include "vpx_mem/vpx_mem.h" #include "vpx_ports/mem.h" #include "vpx_ports/system_state.h" #include "vp9/common/vp9_alloccommon.h" #include "vp9/encoder/vp9_aq_cyclicrefresh.h" #include "vp9/common/vp9_common.h" #include "vp9/common/vp9_entropymode.h" #include "vp9/common/vp9_quant_common.h" #include "vp9/common/vp9_seg_common.h" #include "vp9/encoder/vp9_encodemv.h" #include "vp9/encoder/vp9_ratectrl.h" // Max rate per frame for 1080P and below encodes if no level requirement given. // For larger formats limit to MAX_MB_RATE bits per MB // 4Mbits is derived from the level requirement for level 4 (1080P 30) which // requires that HW can sustain a rate of 16Mbits over a 4 frame group. // If a lower level requirement is specified then this may over ride this value. #define MAX_MB_RATE 250 #define MAXRATE_1080P 4000000 #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1 #define MIN_BPB_FACTOR 0.005 #define MAX_BPB_FACTOR 50 #if CONFIG_VP9_HIGHBITDEPTH #define ASSIGN_MINQ_TABLE(bit_depth, name) \ do { \ switch (bit_depth) { \ case VPX_BITS_8: name = name##_8; break; \ case VPX_BITS_10: name = name##_10; break; \ default: \ assert(bit_depth == VPX_BITS_12); \ name = name##_12; \ break; \ } \ } while (0) #else #define ASSIGN_MINQ_TABLE(bit_depth, name) \ do { \ (void)bit_depth; \ name = name##_8; \ } while (0) #endif // Tables relating active max Q to active min Q 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]; #if CONFIG_VP9_HIGHBITDEPTH 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]; #endif #ifdef AGGRESSIVE_VBR static int gf_high = 2400; static int gf_low = 400; static int kf_high = 4000; static int kf_low = 400; #else static int gf_high = 2000; static int gf_low = 400; static int kf_high = 4800; static int kf_low = 300; #endif // Functions to compute the active minq lookup table entries based on a // formulaic approach to facilitate easier adjustment of the Q tables. // The formulae were derived from computing a 3rd order polynomial best // fit to the original data (after plotting real maxq vs minq (not q index)) static int get_minq_index(double maxq, double x3, double x2, double x1, vpx_bit_depth_t bit_depth) { int i; const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq); // Special case handling to deal with the step from q2.0 // down to lossless mode represented by q 1.0. if (minqtarget <= 2.0) return 0; for (i = 0; i < QINDEX_RANGE; i++) { if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) return i; } return QINDEX_RANGE - 1; } static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low, int *arfgf_high, int *inter, int *rtc, vpx_bit_depth_t bit_depth) { int i; for (i = 0; i < QINDEX_RANGE; i++) { const double maxq = vp9_convert_qindex_to_q(i, bit_depth); kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth); kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth); #ifdef AGGRESSIVE_VBR arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth); inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth); #else arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth); inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); #endif arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); } } void vp9_rc_init_minq_luts(void) { init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8, arfgf_low_motion_minq_8, arfgf_high_motion_minq_8, inter_minq_8, rtc_minq_8, VPX_BITS_8); #if CONFIG_VP9_HIGHBITDEPTH init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10, arfgf_low_motion_minq_10, arfgf_high_motion_minq_10, inter_minq_10, rtc_minq_10, VPX_BITS_10); init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12, arfgf_low_motion_minq_12, arfgf_high_motion_minq_12, inter_minq_12, rtc_minq_12, VPX_BITS_12); #endif } // These functions use formulaic calculations to make playing with the // quantizer tables easier. If necessary they can be replaced by lookup // tables if and when things settle down in the experimental bitstream double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) { // Convert the index to a real Q value (scaled down to match old Q values) #if CONFIG_VP9_HIGHBITDEPTH switch (bit_depth) { case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0; case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0; default: assert(bit_depth == VPX_BITS_12); return vp9_ac_quant(qindex, 0, bit_depth) / 64.0; } #else return vp9_ac_quant(qindex, 0, bit_depth) / 4.0; #endif } int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) { int i; for (i = 0; i < QINDEX_RANGE; ++i) if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break; if (i == QINDEX_RANGE) i--; return i; } int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, double correction_factor, vpx_bit_depth_t bit_depth) { const double q = vp9_convert_qindex_to_q(qindex, bit_depth); int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000; assert(correction_factor <= MAX_BPB_FACTOR && correction_factor >= MIN_BPB_FACTOR); // q based adjustment to baseline enumerator enumerator += (int)(enumerator * q) >> 12; return (int)(enumerator * correction_factor / q); } int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, double correction_factor, vpx_bit_depth_t bit_depth) { const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth)); return VPXMAX(FRAME_OVERHEAD_BITS, (int)(((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS)); } int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { const RATE_CONTROL *rc = &cpi->rc; const VP9EncoderConfig *oxcf = &cpi->oxcf; const int min_frame_target = VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5); if (target < min_frame_target) target = min_frame_target; if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { // If there is an active ARF at this location use the minimum // bits on this frame even if it is a constructed arf. // The active maximum quantizer insures that an appropriate // number of bits will be spent if needed for constructed ARFs. target = min_frame_target; } // Clip the frame target to the maximum allowed value. if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; if (oxcf->rc_max_inter_bitrate_pct) { const int max_rate = rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100; target = VPXMIN(target, max_rate); } return target; } int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { const RATE_CONTROL *rc = &cpi->rc; const VP9EncoderConfig *oxcf = &cpi->oxcf; if (oxcf->rc_max_intra_bitrate_pct) { const int max_rate = rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100; target = VPXMIN(target, max_rate); } if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; return target; } // TODO(marpan/jianj): bits_off_target and buffer_level are used in the same // way for CBR mode, for the buffering updates below. Look into removing one // of these (i.e., bits_off_target). // Update the buffer level before encoding with the per-frame-bandwidth, void vp9_update_buffer_level_preencode(VP9_COMP *cpi) { RATE_CONTROL *const rc = &cpi->rc; rc->bits_off_target += rc->avg_frame_bandwidth; // Clip the buffer level to the maximum specified buffer size. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); rc->buffer_level = rc->bits_off_target; } // Update the buffer level before encoding with the per-frame-bandwidth // for SVC. The current and all upper temporal layers are updated, needed // for the layered rate control which involves cumulative buffer levels for // the temporal layers. Allow for using the timestamp(pts) delta for the // framerate when the set_ref_frame_config is used. static void update_buffer_level_svc_preencode(VP9_COMP *cpi) { SVC *const svc = &cpi->svc; int i; // Set this to 1 to use timestamp delta for "framerate" under // ref_frame_config usage. int use_timestamp = 1; const int64_t ts_delta = svc->time_stamp_superframe - svc->time_stamp_prev[svc->spatial_layer_id]; for (i = svc->temporal_layer_id; i < svc->number_temporal_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers); LAYER_CONTEXT *const lc = &svc->layer_context[layer]; RATE_CONTROL *const lrc = &lc->rc; if (use_timestamp && cpi->svc.use_set_ref_frame_config && svc->number_temporal_layers == 1 && ts_delta > 0 && svc->current_superframe > 0) { // TODO(marpan): This may need to be modified for temporal layers. const double framerate_pts = 10000000.0 / ts_delta; lrc->bits_off_target += (int)round(lc->target_bandwidth / framerate_pts); } else { lrc->bits_off_target += (int)round(lc->target_bandwidth / lc->framerate); } // Clip buffer level to maximum buffer size for the layer. lrc->bits_off_target = VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size); lrc->buffer_level = lrc->bits_off_target; if (i == svc->temporal_layer_id) { cpi->rc.bits_off_target = lrc->bits_off_target; cpi->rc.buffer_level = lrc->buffer_level; } } } // Update the buffer level for higher temporal layers, given the encoded current // temporal layer. static void update_layer_buffer_level_postencode(SVC *svc, int encoded_frame_size) { int i = 0; const int current_temporal_layer = svc->temporal_layer_id; for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers); LAYER_CONTEXT *lc = &svc->layer_context[layer]; RATE_CONTROL *lrc = &lc->rc; lrc->bits_off_target -= encoded_frame_size; // Clip buffer level to maximum buffer size for the layer. lrc->bits_off_target = VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size); lrc->buffer_level = lrc->bits_off_target; } } // Update the buffer level after encoding with encoded frame size. static void update_buffer_level_postencode(VP9_COMP *cpi, int encoded_frame_size) { RATE_CONTROL *const rc = &cpi->rc; rc->bits_off_target -= encoded_frame_size; // Clip the buffer level to the maximum specified buffer size. rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); // For screen-content mode, and if frame-dropper is off, don't let buffer // level go below threshold, given here as -rc->maximum_ buffer_size. if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && cpi->oxcf.drop_frames_water_mark == 0) rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size); rc->buffer_level = rc->bits_off_target; if (is_one_pass_cbr_svc(cpi)) { update_layer_buffer_level_postencode(&cpi->svc, encoded_frame_size); } } int vp9_rc_get_default_min_gf_interval(int width, int height, double framerate) { // Assume we do not need any constraint lower than 4K 20 fps static const double factor_safe = 3840 * 2160 * 20.0; const double factor = width * height * framerate; const int default_interval = clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL); if (factor <= factor_safe) return default_interval; else return VPXMAX(default_interval, (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5)); // Note this logic makes: // 4K24: 5 // 4K30: 6 // 4K60: 12 } int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) { int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75)); interval += (interval & 0x01); // Round to even value return VPXMAX(interval, min_gf_interval); } void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { int i; if (pass == 0 && oxcf->rc_mode == VPX_CBR) { rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; } else { rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; } rc->last_q[KEY_FRAME] = oxcf->best_allowed_q; rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; rc->buffer_level = rc->starting_buffer_level; rc->bits_off_target = rc->starting_buffer_level; rc->rolling_target_bits = rc->avg_frame_bandwidth; rc->rolling_actual_bits = rc->avg_frame_bandwidth; rc->long_rolling_target_bits = rc->avg_frame_bandwidth; rc->long_rolling_actual_bits = rc->avg_frame_bandwidth; rc->total_actual_bits = 0; rc->total_target_bits = 0; rc->total_target_vs_actual = 0; rc->avg_frame_low_motion = 0; rc->count_last_scene_change = 0; rc->af_ratio_onepass_vbr = 10; rc->prev_avg_source_sad_lag = 0; rc->high_source_sad = 0; rc->reset_high_source_sad = 0; rc->high_source_sad_lagindex = -1; rc->high_num_blocks_with_motion = 0; rc->hybrid_intra_scene_change = 0; rc->re_encode_maxq_scene_change = 0; rc->alt_ref_gf_group = 0; rc->last_frame_is_src_altref = 0; rc->fac_active_worst_inter = 150; rc->fac_active_worst_gf = 100; rc->force_qpmin = 0; for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0; rc->frames_to_key = 0; rc->frames_since_key = 8; // Sensible default for first frame. rc->this_key_frame_forced = 0; rc->next_key_frame_forced = 0; rc->source_alt_ref_pending = 0; rc->source_alt_ref_active = 0; rc->frames_till_gf_update_due = 0; rc->constrain_gf_key_freq_onepass_vbr = 1; rc->ni_av_qi = oxcf->worst_allowed_q; rc->ni_tot_qi = 0; rc->ni_frames = 0; rc->tot_q = 0.0; rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth); for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { rc->rate_correction_factors[i] = 1.0; rc->damped_adjustment[i] = 0; } rc->min_gf_interval = oxcf->min_gf_interval; rc->max_gf_interval = oxcf->max_gf_interval; if (rc->min_gf_interval == 0) rc->min_gf_interval = vp9_rc_get_default_min_gf_interval( oxcf->width, oxcf->height, oxcf->init_framerate); if (rc->max_gf_interval == 0) rc->max_gf_interval = vp9_rc_get_default_max_gf_interval( oxcf->init_framerate, rc->min_gf_interval); rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2; if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) { rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL; } else { rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH; } rc->force_max_q = 0; rc->last_post_encode_dropped_scene_change = 0; rc->use_post_encode_drop = 0; rc->ext_use_post_encode_drop = 0; rc->disable_overshoot_maxq_cbr = 0; rc->arf_active_best_quality_adjustment_factor = 1.0; rc->arf_increase_active_best_quality = 0; rc->preserve_arf_as_gld = 0; rc->preserve_next_arf_as_gld = 0; rc->show_arf_as_gld = 0; } static int check_buffer_above_thresh(VP9_COMP *cpi, int drop_mark) { SVC *svc = &cpi->svc; if (!cpi->use_svc || cpi->svc.framedrop_mode != FULL_SUPERFRAME_DROP) { RATE_CONTROL *const rc = &cpi->rc; return (rc->buffer_level > drop_mark); } else { int i; // For SVC in the FULL_SUPERFRAME_DROP): the condition on // buffer (if its above threshold, so no drop) is checked on current and // upper spatial layers. If any spatial layer is not above threshold then // we return 0. for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, svc->number_temporal_layers); LAYER_CONTEXT *lc = &svc->layer_context[layer]; RATE_CONTROL *lrc = &lc->rc; // Exclude check for layer whose bitrate is 0. if (lc->target_bandwidth > 0) { const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] * lrc->optimal_buffer_level / 100); if (!(lrc->buffer_level > drop_mark_layer)) return 0; } } return 1; } } static int check_buffer_below_thresh(VP9_COMP *cpi, int drop_mark) { SVC *svc = &cpi->svc; if (!cpi->use_svc || cpi->svc.framedrop_mode == LAYER_DROP) { RATE_CONTROL *const rc = &cpi->rc; return (rc->buffer_level <= drop_mark); } else { int i; // For SVC in the constrained framedrop mode (svc->framedrop_mode = // CONSTRAINED_LAYER_DROP or FULL_SUPERFRAME_DROP): the condition on // buffer (if its below threshold, so drop frame) is checked on current // and upper spatial layers. For FULL_SUPERFRAME_DROP mode if any // spatial layer is <= threshold, then we return 1 (drop). for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) { const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, svc->number_temporal_layers); LAYER_CONTEXT *lc = &svc->layer_context[layer]; RATE_CONTROL *lrc = &lc->rc; // Exclude check for layer whose bitrate is 0. if (lc->target_bandwidth > 0) { const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] * lrc->optimal_buffer_level / 100); if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP) { if (lrc->buffer_level <= drop_mark_layer) return 1; } else {