vp9_ratectrl.c 53.11 KiB
/*
 *  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_mem/vpx_mem.h"
#include "vp9/common/vp9_alloccommon.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/common/vp9_systemdependent.h"
#include "vp9/encoder/vp9_encodemv.h"
#include "vp9/encoder/vp9_ratectrl.h"
#define DEFAULT_KF_BOOST 2000
#define DEFAULT_GF_BOOST 2000
#define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1
#define MIN_BPB_FACTOR 0.005
#define MAX_BPB_FACTOR 50
// Bits Per MB at different Q (Multiplied by 512)
#define BPER_MB_NORMBITS    9
// Tables relating active max Q to active min Q
static int kf_low_motion_minq[QINDEX_RANGE];
static int kf_high_motion_minq[QINDEX_RANGE];
static int gf_low_motion_minq[QINDEX_RANGE];
static int gf_high_motion_minq[QINDEX_RANGE];
static int inter_minq[QINDEX_RANGE];
static int afq_low_motion_minq[QINDEX_RANGE];
static int afq_high_motion_minq[QINDEX_RANGE];
static int gf_high = 2000;
static int gf_low = 400;
static int kf_high = 5000;
static int kf_low = 400;
// 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 calculate_minq_index(double maxq,
                                double x3, double x2, double x1, double c) {
  int i;
  const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
                                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))
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
return i; } return QINDEX_RANGE - 1; } void vp9_rc_init_minq_luts(void) { int i; for (i = 0; i < QINDEX_RANGE; i++) { const double maxq = vp9_convert_qindex_to_q(i); kf_low_motion_minq[i] = calculate_minq_index(maxq, 0.000001, -0.0004, 0.15, 0.0); kf_high_motion_minq[i] = calculate_minq_index(maxq, 0.000002, -0.0012, 0.50, 0.0); gf_low_motion_minq[i] = calculate_minq_index(maxq, 0.0000015, -0.0009, 0.32, 0.0); gf_high_motion_minq[i] = calculate_minq_index(maxq, 0.0000021, -0.00125, 0.50, 0.0); afq_low_motion_minq[i] = calculate_minq_index(maxq, 0.0000015, -0.0009, 0.33, 0.0); afq_high_motion_minq[i] = calculate_minq_index(maxq, 0.0000021, -0.00125, 0.55, 0.0); inter_minq[i] = calculate_minq_index(maxq, 0.00000271, -0.00113, 0.75, 0.0); } } // 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) { // Convert the index to a real Q value (scaled down to match old Q values) return vp9_ac_quant(qindex, 0) / 4.0; } int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, double correction_factor) { const double q = vp9_convert_qindex_to_q(qindex); int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; // q based adjustment to baseline enumerator enumerator += (int)(enumerator * q) >> 12; return (int)(0.5 + (enumerator * correction_factor / q)); }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
void vp9_save_coding_context(VP9_COMP *cpi) { CODING_CONTEXT *const cc = &cpi->coding_context; VP9_COMMON *cm = &cpi->common; // Stores a snapshot of key state variables which can subsequently be // restored with a call to vp9_restore_coding_context. These functions are // intended for use in a re-code loop in vp9_compress_frame where the // quantizer value is adjusted between loop iterations. vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost); vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts); vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp); vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs); vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols)); vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas); vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas); cc->fc = cm->fc; } void vp9_restore_coding_context(VP9_COMP *cpi) { CODING_CONTEXT *const cc = &cpi->coding_context; VP9_COMMON *cm = &cpi->common; // Restore key state variables to the snapshot state stored in the // previous call to vp9_save_coding_context. vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost); vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts); vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp); vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs); vpx_memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy, (cm->mi_rows * cm->mi_cols)); vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas); vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas); cm->fc = cc->fc; } void vp9_setup_key_frame(VP9_COMP *cpi) { VP9_COMMON *cm = &cpi->common; vp9_setup_past_independence(cm); /* All buffers are implicitly updated on key frames. */ cpi->refresh_golden_frame = 1; cpi->refresh_alt_ref_frame = 1; } void vp9_setup_inter_frame(VP9_COMP *cpi) { VP9_COMMON *cm = &cpi->common; if (cm->error_resilient_mode || cm->intra_only) vp9_setup_past_independence(cm); assert(cm->frame_context_idx < FRAME_CONTEXTS); cm->fc = cm->frame_contexts[cm->frame_context_idx]; } static int estimate_bits_at_q(int frame_kind, int q, int mbs, double correction_factor) { const int bpm = (int)(vp9_rc_bits_per_mb(frame_kind, q, correction_factor)); // Attempt to retain reasonable accuracy without overflow. The cutoff is // chosen such that the maximum product of Bpm and MBs fits 31 bits. The
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
// largest Bpm takes 20 bits. return (mbs > (1 << 11)) ? (bpm >> BPER_MB_NORMBITS) * mbs : (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 int min_frame_target = MAX(rc->min_frame_bandwidth, rc->av_per_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; return target; } int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { const RATE_CONTROL *rc = &cpi->rc; const VP9_CONFIG *oxcf = &cpi->oxcf; if (oxcf->rc_max_intra_bitrate_pct) { const int max_rate = rc->av_per_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100; target = MIN(target, max_rate); } if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; return target; } // Update the buffer level for higher layers, given the encoded current layer. static void update_layer_buffer_level(VP9_COMP *const cpi, int encoded_frame_size) { int temporal_layer = 0; int current_temporal_layer = cpi->svc.temporal_layer_id; for (temporal_layer = current_temporal_layer + 1; temporal_layer < cpi->svc.number_temporal_layers; ++temporal_layer) { LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer]; RATE_CONTROL *lrc = &lc->rc; int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate - encoded_frame_size); lrc->bits_off_target += bits_off_for_this_layer; // Clip buffer level to maximum buffer size for the layer. lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size); lrc->buffer_level = lrc->bits_off_target; } } // Update the buffer level: leaky bucket model. static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { const VP9_COMMON *const cm = &cpi->common; const VP9_CONFIG *oxcf = &cpi->oxcf; RATE_CONTROL *const rc = &cpi->rc; // Non-viewable frames are a special case and are treated as pure overhead. if (!cm->show_frame) { rc->bits_off_target -= encoded_frame_size; } else { rc->bits_off_target += rc->av_per_frame_bandwidth - encoded_frame_size; }
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
// Clip the buffer level to the maximum specified buffer size. rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size); rc->buffer_level = rc->bits_off_target; if (cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { update_layer_buffer_level(cpi, encoded_frame_size); } } int vp9_rc_drop_frame(VP9_COMP *cpi) { const VP9_CONFIG *oxcf = &cpi->oxcf; RATE_CONTROL *const rc = &cpi->rc; if (!oxcf->drop_frames_water_mark) { return 0; } else { if (rc->buffer_level < 0) { // Always drop if buffer is below 0. return 1; } else { // If buffer is below drop_mark, for now just drop every other frame // (starting with the next frame) until it increases back over drop_mark. int drop_mark = (int)(oxcf->drop_frames_water_mark * oxcf->optimal_buffer_level / 100); if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) { --rc->decimation_factor; } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) { rc->decimation_factor = 1; } if (rc->decimation_factor > 0) { if (rc->decimation_count > 0) { --rc->decimation_count; return 1; } else { rc->decimation_count = rc->decimation_factor; return 0; } } else { rc->decimation_count = 0; return 0; } } } } static double get_rate_correction_factor(const VP9_COMP *cpi) { if (cpi->common.frame_type == KEY_FRAME) { return cpi->rc.key_frame_rate_correction_factor; } else { if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) return cpi->rc.gf_rate_correction_factor; else return cpi->rc.rate_correction_factor; } } static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { if (cpi->common.frame_type == KEY_FRAME) { cpi->rc.key_frame_rate_correction_factor = factor; } else { if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && !(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) cpi->rc.gf_rate_correction_factor = factor; else cpi->rc.rate_correction_factor = factor; } }
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { const int q = cpi->common.base_qindex; int correction_factor = 100; double rate_correction_factor = get_rate_correction_factor(cpi); double adjustment_limit; int projected_size_based_on_q = 0; // Clear down mmx registers to allow floating point in what follows vp9_clear_system_state(); // Work out how big we would have expected the frame to be at this Q given // the current correction factor. // Stay in double to avoid int overflow when values are large projected_size_based_on_q = estimate_bits_at_q(cpi->common.frame_type, q, cpi->common.MBs, rate_correction_factor); // Work out a size correction factor. if (projected_size_based_on_q > 0) correction_factor = (100 * cpi->rc.projected_frame_size) / projected_size_based_on_q; // More heavily damped adjustment used if we have been oscillating either side // of target. switch (damp_var) { case 0: adjustment_limit = 0.75; break; case 1: adjustment_limit = 0.375; break; case 2: default: adjustment_limit = 0.25; break; } if (correction_factor > 102) { // We are not already at the worst allowable quality correction_factor = (int)(100 + ((correction_factor - 100) * adjustment_limit)); rate_correction_factor = ((rate_correction_factor * correction_factor) / 100); // Keep rate_correction_factor within limits if (rate_correction_factor > MAX_BPB_FACTOR) rate_correction_factor = MAX_BPB_FACTOR; } else if (correction_factor < 99) { // We are not already at the best allowable quality correction_factor = (int)(100 - ((100 - correction_factor) * adjustment_limit)); rate_correction_factor = ((rate_correction_factor * correction_factor) / 100); // Keep rate_correction_factor within limits if (rate_correction_factor < MIN_BPB_FACTOR) rate_correction_factor = MIN_BPB_FACTOR; } set_rate_correction_factor(cpi, rate_correction_factor); } int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame, int active_best_quality, int active_worst_quality) { const VP9_COMMON *const cm = &cpi->common; int q = active_worst_quality; int last_error = INT_MAX; int i, target_bits_per_mb;
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
const double correction_factor = get_rate_correction_factor(cpi); // Calculate required scaling factor based on target frame size and size of // frame produced using previous Q. if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) // Case where we would overflow int target_bits_per_mb = (target_bits_per_frame / cm->MBs) << BPER_MB_NORMBITS; else target_bits_per_mb = (target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs; i = active_best_quality; do { const int bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i, correction_factor); if (bits_per_mb_at_this_q <= target_bits_per_mb) { if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) q = i; else q = i - 1; break; } else { last_error = bits_per_mb_at_this_q - target_bits_per_mb; } } while (++i <= active_worst_quality); return q; } static int get_active_quality(int q, int gfu_boost, int low, int high, int *low_motion_minq, int *high_motion_minq) { if (gfu_boost > high) { return low_motion_minq[q]; } else if (gfu_boost < low) { return high_motion_minq[q]; } else { const int gap = high - low; const int offset = high - gfu_boost; const int qdiff = high_motion_minq[q] - low_motion_minq[q]; const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; return low_motion_minq[q] + adjustment; } } static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) { const RATE_CONTROL *const rc = &cpi->rc; const unsigned int curr_frame = cpi->common.current_video_frame; int active_worst_quality; if (cpi->common.frame_type == KEY_FRAME) { active_worst_quality = curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] * 2; } else { if (!rc->is_src_frame_alt_ref && (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4 : rc->last_q[INTER_FRAME]; } else { active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2 : rc->last_q[INTER_FRAME] * 2; } } return MIN(active_worst_quality, rc->worst_quality); } // Adjust active_worst_quality level based on buffer level. static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
// Adjust active_worst_quality: If buffer is above the optimal/target level, // bring active_worst_quality down depending on fullness of buffer. // If buffer is below the optimal level, let the active_worst_quality go from // ambient Q (at buffer = optimal level) to worst_quality level // (at buffer = critical level). const VP9_CONFIG *oxcf = &cpi->oxcf; const RATE_CONTROL *rc = &cpi->rc; // Buffer level below which we push active_worst to worst_quality. int64_t critical_level = oxcf->optimal_buffer_level >> 2; int64_t buff_lvl_step = 0; int adjustment = 0; int active_worst_quality; if (cpi->common.frame_type == KEY_FRAME) return rc->worst_quality; if (cpi->common.current_video_frame > 1) active_worst_quality = MIN(rc->worst_quality, rc->avg_frame_qindex[INTER_FRAME] * 5 / 4); else active_worst_quality = MIN(rc->worst_quality, rc->avg_frame_qindex[KEY_FRAME] * 3 / 2); if (rc->buffer_level > oxcf->optimal_buffer_level) { // Adjust down. // Maximum limit for down adjustment, ~30%. int max_adjustment_down = active_worst_quality / 3; if (max_adjustment_down) { buff_lvl_step = ((oxcf->maximum_buffer_size - oxcf->optimal_buffer_level) / max_adjustment_down); if (buff_lvl_step) adjustment = (int)((rc->buffer_level - oxcf->optimal_buffer_level) / buff_lvl_step); active_worst_quality -= adjustment; } } else if (rc->buffer_level > critical_level) { // Adjust up from ambient Q. if (critical_level) { buff_lvl_step = (oxcf->optimal_buffer_level - critical_level); if (buff_lvl_step) { adjustment = (int)((rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) * (oxcf->optimal_buffer_level - rc->buffer_level) / buff_lvl_step); } active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment; } } else { // Set to worst_quality if buffer is below critical level. active_worst_quality = rc->worst_quality; } return active_worst_quality; } static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi, int *bottom_index, int *top_index) { const VP9_COMMON *const cm = &cpi->common; const RATE_CONTROL *const rc = &cpi->rc; int active_best_quality; int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi); int q; if (frame_is_intra_only(cm)) { active_best_quality = rc->best_quality; // Handle the special case for key frames forced when we have75 reached // the maximum key frame interval. Here force the Q to a range // based on the ambient Q to reduce the risk of popping. if (rc->this_key_frame_forced) { int qindex = rc->last_boosted_qindex; double last_boosted_q = vp9_convert_qindex_to_q(qindex); int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, (last_boosted_q * 0.75));
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); } else if (cm->current_video_frame > 0) { // not first frame of one pass and kf_boost is set double q_adj_factor = 1.0; double q_val; active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME], rc->kf_boost, kf_low, kf_high, kf_low_motion_minq, kf_high_motion_minq); // Allow somewhat lower kf minq with small image formats. if ((cm->width * cm->height) <= (352 * 288)) { q_adj_factor -= 0.25; } // Convert the adjustment factor to a qindex delta // on active_best_quality. q_val = vp9_convert_qindex_to_q(active_best_quality); active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * q_adj_factor); } } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc && (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { // Use the lower of active_worst_quality and recent // average Q as basis for GF/ARF best Q limit unless last frame was // a key frame. if (rc->frames_since_key > 1 && rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { q = rc->avg_frame_qindex[INTER_FRAME]; } else { q = active_worst_quality; } active_best_quality = get_active_quality( q, rc->gfu_boost, gf_low, gf_high, gf_low_motion_minq, gf_high_motion_minq); } else { // Use the lower of active_worst_quality and recent/average Q. if (cm->current_video_frame > 1) { if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; else active_best_quality = inter_minq[active_worst_quality]; } else { if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality) active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; else active_best_quality = inter_minq[active_worst_quality]; } } // Clip the active best and worst quality values to limits active_best_quality = clamp(active_best_quality, rc->best_quality, rc->worst_quality); active_worst_quality = clamp(active_worst_quality, active_best_quality, rc->worst_quality); *top_index = active_worst_quality; *bottom_index = active_best_quality; #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY // Limit Q range for the adaptive loop. if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced) { if (!(cm->current_video_frame == 0)) *top_index = (active_worst_quality + active_best_quality * 3) / 4; } #endif // Special case code to try and match quality with forced key frames
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) { q = rc->last_boosted_qindex; } else { q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, active_worst_quality); if (q > *top_index) { // Special case when we are targeting the max allowed rate if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth) *top_index = q; else q = *top_index; } } assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); assert(*bottom_index <= rc->worst_quality && *bottom_index >= rc->best_quality); assert(q <= rc->worst_quality && q >= rc->best_quality); return q; } static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi, int *bottom_index, int *top_index) { const VP9_COMMON *const cm = &cpi->common; const RATE_CONTROL *const rc = &cpi->rc; const VP9_CONFIG *const oxcf = &cpi->oxcf; int active_best_quality; int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); int q; if (frame_is_intra_only(cm)) { active_best_quality = rc->best_quality; #if !CONFIG_MULTIPLE_ARF // Handle the special case for key frames forced when we have75 reached // the maximum key frame interval. Here force the Q to a range // based on the ambient Q to reduce the risk of popping. if (rc->this_key_frame_forced) { int qindex = rc->last_boosted_qindex; double last_boosted_q = vp9_convert_qindex_to_q(qindex); int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, (last_boosted_q * 0.75)); active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); } else if (cm->current_video_frame > 0) { // not first frame of one pass and kf_boost is set double q_adj_factor = 1.0; double q_val; active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME], rc->kf_boost, kf_low, kf_high, kf_low_motion_minq, kf_high_motion_minq); // Allow somewhat lower kf minq with small image formats. if ((cm->width * cm->height) <= (352 * 288)) { q_adj_factor -= 0.25; } // Convert the adjustment factor to a qindex delta // on active_best_quality. q_val = vp9_convert_qindex_to_q(active_best_quality); active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val * q_adj_factor); } #else double current_q; // Force the KF quantizer to be 30% of the active_worst_quality. current_q = vp9_convert_qindex_to_q(active_worst_quality); active_best_quality = active_worst_quality
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
+ vp9_compute_qdelta(cpi, current_q, current_q * 0.3); #endif } else if (!rc->is_src_frame_alt_ref && (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { // Use the lower of active_worst_quality and recent // average Q as basis for GF/ARF best Q limit unless last frame was // a key frame. if (rc->frames_since_key > 1 && rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { q = rc->avg_frame_qindex[INTER_FRAME]; } else { q = rc->avg_frame_qindex[KEY_FRAME]; } // For constrained quality dont allow Q less than the cq level if (oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) { if (q < cpi->cq_target_quality) q = cpi->cq_target_quality; if (rc->frames_since_key > 1) { active_best_quality = get_active_quality(q, rc->gfu_boost, gf_low, gf_high, afq_low_motion_minq, afq_high_motion_minq); } else { active_best_quality = get_active_quality(q, rc->gfu_boost, gf_low, gf_high, gf_low_motion_minq, gf_high_motion_minq); } // Constrained quality use slightly lower active best. active_best_quality = active_best_quality * 15 / 16; } else if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { if (!cpi->refresh_alt_ref_frame) { active_best_quality = cpi->cq_target_quality; } else { if (rc->frames_since_key > 1) { active_best_quality = get_active_quality( q, rc->gfu_boost, gf_low, gf_high, afq_low_motion_minq, afq_high_motion_minq); } else { active_best_quality = get_active_quality( q, rc->gfu_boost, gf_low, gf_high, gf_low_motion_minq, gf_high_motion_minq); } } } else { active_best_quality = get_active_quality( q, rc->gfu_boost, gf_low, gf_high, gf_low_motion_minq, gf_high_motion_minq); } } else { if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { active_best_quality = cpi->cq_target_quality; } else { // Use the lower of active_worst_quality and recent/average Q. if (cm->current_video_frame > 1) active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; else active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; // For the constrained quality mode we don't want // q to fall below the cq level. if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) && (active_best_quality < cpi->cq_target_quality)) { // If we are strongly undershooting the target rate in the last // frames then use the user passed in cq value not the auto // cq value. if (rc->rolling_actual_bits < rc->min_frame_bandwidth) active_best_quality = oxcf->cq_level; else active_best_quality = cpi->cq_target_quality;