Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1 | /* |
Adrian Grange | a872b06 | 2016-03-24 11:38:32 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 3 | * |
Adrian Grange | a872b06 | 2016-03-24 11:38:32 -0700 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <limits.h> |
| 14 | #include <math.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 19 | #include "aom_dsp/aom_dsp_common.h" |
| 20 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | bf4202e | 2016-03-21 15:15:19 -0700 | [diff] [blame] | 21 | #include "aom_ports/mem.h" |
| 22 | #include "aom_ports/system_state.h" |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 23 | |
Yaowu Xu | cfea7dd | 2016-03-22 09:52:13 -0700 | [diff] [blame] | 24 | #include "av1/common/alloccommon.h" |
| 25 | #include "av1/encoder/aq_cyclicrefresh.h" |
| 26 | #include "av1/common/common.h" |
| 27 | #include "av1/common/entropymode.h" |
| 28 | #include "av1/common/quant_common.h" |
| 29 | #include "av1/common/seg_common.h" |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 30 | |
Yaowu Xu | cfea7dd | 2016-03-22 09:52:13 -0700 | [diff] [blame] | 31 | #include "av1/encoder/encodemv.h" |
| 32 | #include "av1/encoder/ratectrl.h" |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 33 | |
| 34 | // Max rate target for 1080P and below encodes under normal circumstances |
| 35 | // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB |
| 36 | #define MAX_MB_RATE 250 |
| 37 | #define MAXRATE_1080P 2025000 |
| 38 | |
| 39 | #define DEFAULT_KF_BOOST 2000 |
| 40 | #define DEFAULT_GF_BOOST 2000 |
| 41 | |
| 42 | #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1 |
| 43 | |
| 44 | #define MIN_BPB_FACTOR 0.005 |
| 45 | #define MAX_BPB_FACTOR 50 |
| 46 | |
| 47 | #define FRAME_OVERHEAD_BITS 200 |
| 48 | |
Yaowu Xu | 3246fc0 | 2016-01-20 16:13:04 -0800 | [diff] [blame] | 49 | #if CONFIG_VPX_HIGHBITDEPTH |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 50 | #define ASSIGN_MINQ_TABLE(bit_depth, name) \ |
| 51 | do { \ |
| 52 | switch (bit_depth) { \ |
| 53 | case VPX_BITS_8: name = name##_8; break; \ |
| 54 | case VPX_BITS_10: name = name##_10; break; \ |
| 55 | case VPX_BITS_12: name = name##_12; break; \ |
| 56 | default: \ |
| 57 | assert(0 && \ |
| 58 | "bit_depth should be VPX_BITS_8, VPX_BITS_10" \ |
| 59 | " or VPX_BITS_12"); \ |
| 60 | name = NULL; \ |
| 61 | } \ |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 62 | } while (0) |
| 63 | #else |
| 64 | #define ASSIGN_MINQ_TABLE(bit_depth, name) \ |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 65 | do { \ |
| 66 | (void) bit_depth; \ |
| 67 | name = name##_8; \ |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 68 | } while (0) |
| 69 | #endif |
| 70 | |
| 71 | // Tables relating active max Q to active min Q |
| 72 | static int kf_low_motion_minq_8[QINDEX_RANGE]; |
| 73 | static int kf_high_motion_minq_8[QINDEX_RANGE]; |
| 74 | static int arfgf_low_motion_minq_8[QINDEX_RANGE]; |
| 75 | static int arfgf_high_motion_minq_8[QINDEX_RANGE]; |
| 76 | static int inter_minq_8[QINDEX_RANGE]; |
| 77 | static int rtc_minq_8[QINDEX_RANGE]; |
| 78 | |
Yaowu Xu | 3246fc0 | 2016-01-20 16:13:04 -0800 | [diff] [blame] | 79 | #if CONFIG_VPX_HIGHBITDEPTH |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 80 | static int kf_low_motion_minq_10[QINDEX_RANGE]; |
| 81 | static int kf_high_motion_minq_10[QINDEX_RANGE]; |
| 82 | static int arfgf_low_motion_minq_10[QINDEX_RANGE]; |
| 83 | static int arfgf_high_motion_minq_10[QINDEX_RANGE]; |
| 84 | static int inter_minq_10[QINDEX_RANGE]; |
| 85 | static int rtc_minq_10[QINDEX_RANGE]; |
| 86 | static int kf_low_motion_minq_12[QINDEX_RANGE]; |
| 87 | static int kf_high_motion_minq_12[QINDEX_RANGE]; |
| 88 | static int arfgf_low_motion_minq_12[QINDEX_RANGE]; |
| 89 | static int arfgf_high_motion_minq_12[QINDEX_RANGE]; |
| 90 | static int inter_minq_12[QINDEX_RANGE]; |
| 91 | static int rtc_minq_12[QINDEX_RANGE]; |
| 92 | #endif |
| 93 | |
| 94 | static int gf_high = 2000; |
| 95 | static int gf_low = 400; |
| 96 | static int kf_high = 5000; |
| 97 | static int kf_low = 400; |
| 98 | |
| 99 | // Functions to compute the active minq lookup table entries based on a |
| 100 | // formulaic approach to facilitate easier adjustment of the Q tables. |
| 101 | // The formulae were derived from computing a 3rd order polynomial best |
| 102 | // fit to the original data (after plotting real maxq vs minq (not q index)) |
| 103 | static int get_minq_index(double maxq, double x3, double x2, double x1, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 104 | aom_bit_depth_t bit_depth) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 105 | int i; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 106 | const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 107 | |
| 108 | // Special case handling to deal with the step from q2.0 |
| 109 | // down to lossless mode represented by q 1.0. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 110 | if (minqtarget <= 2.0) return 0; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 111 | |
| 112 | for (i = 0; i < QINDEX_RANGE; i++) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 113 | if (minqtarget <= vp10_convert_qindex_to_q(i, bit_depth)) return i; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return QINDEX_RANGE - 1; |
| 117 | } |
| 118 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 119 | static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low, |
| 120 | int *arfgf_high, int *inter, int *rtc, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 121 | aom_bit_depth_t bit_depth) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 122 | int i; |
| 123 | for (i = 0; i < QINDEX_RANGE; i++) { |
| 124 | const double maxq = vp10_convert_qindex_to_q(i, bit_depth); |
| 125 | kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth); |
| 126 | kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); |
| 127 | arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth); |
| 128 | arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); |
| 129 | inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth); |
| 130 | rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void vp10_rc_init_minq_luts(void) { |
| 135 | init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8, |
| 136 | arfgf_low_motion_minq_8, arfgf_high_motion_minq_8, |
| 137 | inter_minq_8, rtc_minq_8, VPX_BITS_8); |
Yaowu Xu | 3246fc0 | 2016-01-20 16:13:04 -0800 | [diff] [blame] | 138 | #if CONFIG_VPX_HIGHBITDEPTH |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 139 | init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10, |
| 140 | arfgf_low_motion_minq_10, arfgf_high_motion_minq_10, |
| 141 | inter_minq_10, rtc_minq_10, VPX_BITS_10); |
| 142 | init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12, |
| 143 | arfgf_low_motion_minq_12, arfgf_high_motion_minq_12, |
| 144 | inter_minq_12, rtc_minq_12, VPX_BITS_12); |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | // These functions use formulaic calculations to make playing with the |
| 149 | // quantizer tables easier. If necessary they can be replaced by lookup |
| 150 | // tables if and when things settle down in the experimental bitstream |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 151 | double vp10_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 152 | // Convert the index to a real Q value (scaled down to match old Q values) |
Yaowu Xu | 3246fc0 | 2016-01-20 16:13:04 -0800 | [diff] [blame] | 153 | #if CONFIG_VPX_HIGHBITDEPTH |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 154 | switch (bit_depth) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 155 | case VPX_BITS_8: return vp10_ac_quant(qindex, 0, bit_depth) / 4.0; |
| 156 | case VPX_BITS_10: return vp10_ac_quant(qindex, 0, bit_depth) / 16.0; |
| 157 | case VPX_BITS_12: return vp10_ac_quant(qindex, 0, bit_depth) / 64.0; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 158 | default: |
| 159 | assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12"); |
| 160 | return -1.0; |
| 161 | } |
| 162 | #else |
| 163 | return vp10_ac_quant(qindex, 0, bit_depth) / 4.0; |
| 164 | #endif |
| 165 | } |
| 166 | |
| 167 | int vp10_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 168 | double correction_factor, aom_bit_depth_t bit_depth) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 169 | const double q = vp10_convert_qindex_to_q(qindex, bit_depth); |
| 170 | int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000; |
| 171 | |
| 172 | assert(correction_factor <= MAX_BPB_FACTOR && |
| 173 | correction_factor >= MIN_BPB_FACTOR); |
| 174 | |
| 175 | // q based adjustment to baseline enumerator |
| 176 | enumerator += (int)(enumerator * q) >> 12; |
| 177 | return (int)(enumerator * correction_factor / q); |
| 178 | } |
| 179 | |
| 180 | int vp10_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 181 | double correction_factor, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 182 | aom_bit_depth_t bit_depth) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 183 | const int bpm = |
| 184 | (int)(vp10_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth)); |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 185 | return VPXMAX(FRAME_OVERHEAD_BITS, |
| 186 | (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 189 | int vp10_rc_clamp_pframe_target_size(const VP10_COMP *const cpi, int target) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 190 | const RATE_CONTROL *rc = &cpi->rc; |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 191 | const VP10EncoderConfig *oxcf = &cpi->oxcf; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 192 | const int min_frame_target = |
| 193 | VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5); |
| 194 | if (target < min_frame_target) target = min_frame_target; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 195 | if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { |
| 196 | // If there is an active ARF at this location use the minimum |
| 197 | // bits on this frame even if it is a constructed arf. |
| 198 | // The active maximum quantizer insures that an appropriate |
| 199 | // number of bits will be spent if needed for constructed ARFs. |
| 200 | target = min_frame_target; |
| 201 | } |
| 202 | // Clip the frame target to the maximum allowed value. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 203 | if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 204 | if (oxcf->rc_max_inter_bitrate_pct) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 205 | const int max_rate = |
| 206 | rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 207 | target = VPXMIN(target, max_rate); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 208 | } |
| 209 | return target; |
| 210 | } |
| 211 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 212 | int vp10_rc_clamp_iframe_target_size(const VP10_COMP *const cpi, int target) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 213 | const RATE_CONTROL *rc = &cpi->rc; |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 214 | const VP10EncoderConfig *oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 215 | if (oxcf->rc_max_intra_bitrate_pct) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 216 | const int max_rate = |
| 217 | rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 218 | target = VPXMIN(target, max_rate); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 219 | } |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 220 | if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 221 | return target; |
| 222 | } |
| 223 | |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 224 | // Update the buffer level: leaky bucket model. |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 225 | static void update_buffer_level(VP10_COMP *cpi, int encoded_frame_size) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 226 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 227 | RATE_CONTROL *const rc = &cpi->rc; |
| 228 | |
| 229 | // Non-viewable frames are a special case and are treated as pure overhead. |
| 230 | if (!cm->show_frame) { |
| 231 | rc->bits_off_target -= encoded_frame_size; |
| 232 | } else { |
| 233 | rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size; |
| 234 | } |
| 235 | |
| 236 | // Clip the buffer level to the maximum specified buffer size. |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 237 | rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 238 | rc->buffer_level = rc->bits_off_target; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 239 | } |
| 240 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 241 | int vp10_rc_get_default_min_gf_interval(int width, int height, |
| 242 | double framerate) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 243 | // Assume we do not need any constraint lower than 4K 20 fps |
| 244 | static const double factor_safe = 3840 * 2160 * 20.0; |
| 245 | const double factor = width * height * framerate; |
| 246 | const int default_interval = |
| 247 | clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL); |
| 248 | |
| 249 | if (factor <= factor_safe) |
| 250 | return default_interval; |
| 251 | else |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 252 | return VPXMAX(default_interval, |
| 253 | (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5)); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 254 | // Note this logic makes: |
| 255 | // 4K24: 5 |
| 256 | // 4K30: 6 |
| 257 | // 4K60: 12 |
| 258 | } |
| 259 | |
| 260 | int vp10_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) { |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 261 | int interval = VPXMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75)); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 262 | interval += (interval & 0x01); // Round to even value |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 263 | return VPXMAX(interval, min_gf_interval); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 264 | } |
| 265 | |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 266 | void vp10_rc_init(const VP10EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 267 | int i; |
| 268 | |
| 269 | if (pass == 0 && oxcf->rc_mode == VPX_CBR) { |
| 270 | rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; |
| 271 | rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
| 272 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 273 | rc->avg_frame_qindex[KEY_FRAME] = |
| 274 | (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; |
| 275 | rc->avg_frame_qindex[INTER_FRAME] = |
| 276 | (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | rc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
| 280 | rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
| 281 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 282 | rc->buffer_level = rc->starting_buffer_level; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 283 | rc->bits_off_target = rc->starting_buffer_level; |
| 284 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 285 | rc->rolling_target_bits = rc->avg_frame_bandwidth; |
| 286 | rc->rolling_actual_bits = rc->avg_frame_bandwidth; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 287 | rc->long_rolling_target_bits = rc->avg_frame_bandwidth; |
| 288 | rc->long_rolling_actual_bits = rc->avg_frame_bandwidth; |
| 289 | |
| 290 | rc->total_actual_bits = 0; |
| 291 | rc->total_target_bits = 0; |
| 292 | rc->total_target_vs_actual = 0; |
| 293 | |
| 294 | rc->frames_since_key = 8; // Sensible default for first frame. |
| 295 | rc->this_key_frame_forced = 0; |
| 296 | rc->next_key_frame_forced = 0; |
| 297 | rc->source_alt_ref_pending = 0; |
| 298 | rc->source_alt_ref_active = 0; |
| 299 | |
| 300 | rc->frames_till_gf_update_due = 0; |
| 301 | rc->ni_av_qi = oxcf->worst_allowed_q; |
| 302 | rc->ni_tot_qi = 0; |
| 303 | rc->ni_frames = 0; |
| 304 | |
| 305 | rc->tot_q = 0.0; |
| 306 | rc->avg_q = vp10_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth); |
| 307 | |
| 308 | for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
| 309 | rc->rate_correction_factors[i] = 1.0; |
| 310 | } |
| 311 | |
| 312 | rc->min_gf_interval = oxcf->min_gf_interval; |
| 313 | rc->max_gf_interval = oxcf->max_gf_interval; |
| 314 | if (rc->min_gf_interval == 0) |
| 315 | rc->min_gf_interval = vp10_rc_get_default_min_gf_interval( |
| 316 | oxcf->width, oxcf->height, oxcf->init_framerate); |
| 317 | if (rc->max_gf_interval == 0) |
| 318 | rc->max_gf_interval = vp10_rc_get_default_max_gf_interval( |
| 319 | oxcf->init_framerate, rc->min_gf_interval); |
| 320 | rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2; |
| 321 | } |
| 322 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 323 | int vp10_rc_drop_frame(VP10_COMP *cpi) { |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 324 | const VP10EncoderConfig *oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 325 | RATE_CONTROL *const rc = &cpi->rc; |
| 326 | |
| 327 | if (!oxcf->drop_frames_water_mark) { |
| 328 | return 0; |
| 329 | } else { |
| 330 | if (rc->buffer_level < 0) { |
| 331 | // Always drop if buffer is below 0. |
| 332 | return 1; |
| 333 | } else { |
| 334 | // If buffer is below drop_mark, for now just drop every other frame |
| 335 | // (starting with the next frame) until it increases back over drop_mark. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 336 | int drop_mark = |
| 337 | (int)(oxcf->drop_frames_water_mark * rc->optimal_buffer_level / 100); |
| 338 | if ((rc->buffer_level > drop_mark) && (rc->decimation_factor > 0)) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 339 | --rc->decimation_factor; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 340 | } else if (rc->buffer_level <= drop_mark && rc->decimation_factor == 0) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 341 | rc->decimation_factor = 1; |
| 342 | } |
| 343 | if (rc->decimation_factor > 0) { |
| 344 | if (rc->decimation_count > 0) { |
| 345 | --rc->decimation_count; |
| 346 | return 1; |
| 347 | } else { |
| 348 | rc->decimation_count = rc->decimation_factor; |
| 349 | return 0; |
| 350 | } |
| 351 | } else { |
| 352 | rc->decimation_count = 0; |
| 353 | return 0; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 359 | static double get_rate_correction_factor(const VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 360 | const RATE_CONTROL *const rc = &cpi->rc; |
| 361 | double rcf; |
| 362 | |
| 363 | if (cpi->common.frame_type == KEY_FRAME) { |
| 364 | rcf = rc->rate_correction_factors[KF_STD]; |
| 365 | } else if (cpi->oxcf.pass == 2) { |
| 366 | RATE_FACTOR_LEVEL rf_lvl = |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 367 | cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 368 | rcf = rc->rate_correction_factors[rf_lvl]; |
| 369 | } else { |
| 370 | if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 371 | !rc->is_src_frame_alt_ref && |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 372 | (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) |
| 373 | rcf = rc->rate_correction_factors[GF_ARF_STD]; |
| 374 | else |
| 375 | rcf = rc->rate_correction_factors[INTER_NORMAL]; |
| 376 | } |
| 377 | rcf *= rcf_mult[rc->frame_size_selector]; |
| 378 | return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
| 379 | } |
| 380 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 381 | static void set_rate_correction_factor(VP10_COMP *cpi, double factor) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 382 | RATE_CONTROL *const rc = &cpi->rc; |
| 383 | |
| 384 | // Normalize RCF to account for the size-dependent scaling factor. |
| 385 | factor /= rcf_mult[cpi->rc.frame_size_selector]; |
| 386 | |
| 387 | factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
| 388 | |
| 389 | if (cpi->common.frame_type == KEY_FRAME) { |
| 390 | rc->rate_correction_factors[KF_STD] = factor; |
| 391 | } else if (cpi->oxcf.pass == 2) { |
| 392 | RATE_FACTOR_LEVEL rf_lvl = |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 393 | cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 394 | rc->rate_correction_factors[rf_lvl] = factor; |
| 395 | } else { |
| 396 | if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 397 | !rc->is_src_frame_alt_ref && |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 398 | (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) |
| 399 | rc->rate_correction_factors[GF_ARF_STD] = factor; |
| 400 | else |
| 401 | rc->rate_correction_factors[INTER_NORMAL] = factor; |
| 402 | } |
| 403 | } |
| 404 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 405 | void vp10_rc_update_rate_correction_factors(VP10_COMP *cpi) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 406 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 407 | int correction_factor = 100; |
| 408 | double rate_correction_factor = get_rate_correction_factor(cpi); |
| 409 | double adjustment_limit; |
| 410 | |
| 411 | int projected_size_based_on_q = 0; |
| 412 | |
| 413 | // Do not update the rate factors for arf overlay frames. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 414 | if (cpi->rc.is_src_frame_alt_ref) return; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 415 | |
| 416 | // Clear down mmx registers to allow floating point in what follows |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 417 | aom_clear_system_state(); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 418 | |
| 419 | // Work out how big we would have expected the frame to be at this Q given |
| 420 | // the current correction factor. |
| 421 | // Stay in double to avoid int overflow when values are large |
| 422 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) { |
| 423 | projected_size_based_on_q = |
| 424 | vp10_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor); |
| 425 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 426 | projected_size_based_on_q = |
| 427 | vp10_estimate_bits_at_q(cpi->common.frame_type, cm->base_qindex, |
| 428 | cm->MBs, rate_correction_factor, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 429 | } |
| 430 | // Work out a size correction factor. |
| 431 | if (projected_size_based_on_q > FRAME_OVERHEAD_BITS) |
| 432 | correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) / |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 433 | projected_size_based_on_q); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 434 | |
| 435 | // More heavily damped adjustment used if we have been oscillating either side |
| 436 | // of target. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 437 | adjustment_limit = |
| 438 | 0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor))); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 439 | |
| 440 | cpi->rc.q_2_frame = cpi->rc.q_1_frame; |
| 441 | cpi->rc.q_1_frame = cm->base_qindex; |
| 442 | cpi->rc.rc_2_frame = cpi->rc.rc_1_frame; |
| 443 | if (correction_factor > 110) |
| 444 | cpi->rc.rc_1_frame = -1; |
| 445 | else if (correction_factor < 90) |
| 446 | cpi->rc.rc_1_frame = 1; |
| 447 | else |
| 448 | cpi->rc.rc_1_frame = 0; |
| 449 | |
| 450 | if (correction_factor > 102) { |
| 451 | // We are not already at the worst allowable quality |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 452 | correction_factor = |
| 453 | (int)(100 + ((correction_factor - 100) * adjustment_limit)); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 454 | rate_correction_factor = (rate_correction_factor * correction_factor) / 100; |
| 455 | // Keep rate_correction_factor within limits |
| 456 | if (rate_correction_factor > MAX_BPB_FACTOR) |
| 457 | rate_correction_factor = MAX_BPB_FACTOR; |
| 458 | } else if (correction_factor < 99) { |
| 459 | // We are not already at the best allowable quality |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 460 | correction_factor = |
| 461 | (int)(100 - ((100 - correction_factor) * adjustment_limit)); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 462 | rate_correction_factor = (rate_correction_factor * correction_factor) / 100; |
| 463 | |
| 464 | // Keep rate_correction_factor within limits |
| 465 | if (rate_correction_factor < MIN_BPB_FACTOR) |
| 466 | rate_correction_factor = MIN_BPB_FACTOR; |
| 467 | } |
| 468 | |
| 469 | set_rate_correction_factor(cpi, rate_correction_factor); |
| 470 | } |
| 471 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 472 | int vp10_rc_regulate_q(const VP10_COMP *cpi, int target_bits_per_frame, |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 473 | int active_best_quality, int active_worst_quality) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 474 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 475 | int q = active_worst_quality; |
| 476 | int last_error = INT_MAX; |
| 477 | int i, target_bits_per_mb, bits_per_mb_at_this_q; |
| 478 | const double correction_factor = get_rate_correction_factor(cpi); |
| 479 | |
| 480 | // Calculate required scaling factor based on target frame size and size of |
| 481 | // frame produced using previous Q. |
| 482 | target_bits_per_mb = |
| 483 | ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs; |
| 484 | |
| 485 | i = active_best_quality; |
| 486 | |
| 487 | do { |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 488 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 489 | bits_per_mb_at_this_q = |
| 490 | (int)vp10_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor); |
| 491 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 492 | bits_per_mb_at_this_q = (int)vp10_rc_bits_per_mb( |
| 493 | cm->frame_type, i, correction_factor, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (bits_per_mb_at_this_q <= target_bits_per_mb) { |
| 497 | if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) |
| 498 | q = i; |
| 499 | else |
| 500 | q = i - 1; |
| 501 | |
| 502 | break; |
| 503 | } else { |
| 504 | last_error = bits_per_mb_at_this_q - target_bits_per_mb; |
| 505 | } |
| 506 | } while (++i <= active_worst_quality); |
| 507 | |
| 508 | // In CBR mode, this makes sure q is between oscillating Qs to prevent |
| 509 | // resonance. |
| 510 | if (cpi->oxcf.rc_mode == VPX_CBR && |
| 511 | (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) && |
| 512 | cpi->rc.q_1_frame != cpi->rc.q_2_frame) { |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 513 | q = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame), |
| 514 | VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame)); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 515 | } |
| 516 | return q; |
| 517 | } |
| 518 | |
| 519 | static int get_active_quality(int q, int gfu_boost, int low, int high, |
| 520 | int *low_motion_minq, int *high_motion_minq) { |
| 521 | if (gfu_boost > high) { |
| 522 | return low_motion_minq[q]; |
| 523 | } else if (gfu_boost < low) { |
| 524 | return high_motion_minq[q]; |
| 525 | } else { |
| 526 | const int gap = high - low; |
| 527 | const int offset = high - gfu_boost; |
| 528 | const int qdiff = high_motion_minq[q] - low_motion_minq[q]; |
| 529 | const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; |
| 530 | return low_motion_minq[q] + adjustment; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | static int get_kf_active_quality(const RATE_CONTROL *const rc, int q, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 535 | aom_bit_depth_t bit_depth) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 536 | int *kf_low_motion_minq; |
| 537 | int *kf_high_motion_minq; |
| 538 | ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq); |
| 539 | ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq); |
| 540 | return get_active_quality(q, rc->kf_boost, kf_low, kf_high, |
| 541 | kf_low_motion_minq, kf_high_motion_minq); |
| 542 | } |
| 543 | |
| 544 | static int get_gf_active_quality(const RATE_CONTROL *const rc, int q, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 545 | aom_bit_depth_t bit_depth) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 546 | int *arfgf_low_motion_minq; |
| 547 | int *arfgf_high_motion_minq; |
| 548 | ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq); |
| 549 | ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq); |
| 550 | return get_active_quality(q, rc->gfu_boost, gf_low, gf_high, |
| 551 | arfgf_low_motion_minq, arfgf_high_motion_minq); |
| 552 | } |
| 553 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 554 | static int calc_active_worst_quality_one_pass_vbr(const VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 555 | const RATE_CONTROL *const rc = &cpi->rc; |
| 556 | const unsigned int curr_frame = cpi->common.current_video_frame; |
| 557 | int active_worst_quality; |
| 558 | |
| 559 | if (cpi->common.frame_type == KEY_FRAME) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 560 | active_worst_quality = |
| 561 | curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] * 2; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 562 | } else { |
| 563 | if (!rc->is_src_frame_alt_ref && |
| 564 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 565 | active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 5 / 4 |
| 566 | : rc->last_q[INTER_FRAME]; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 567 | } else { |
| 568 | active_worst_quality = curr_frame == 1 ? rc->last_q[KEY_FRAME] * 2 |
| 569 | : rc->last_q[INTER_FRAME] * 2; |
| 570 | } |
| 571 | } |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 572 | return VPXMIN(active_worst_quality, rc->worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // Adjust active_worst_quality level based on buffer level. |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 576 | static int calc_active_worst_quality_one_pass_cbr(const VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 577 | // Adjust active_worst_quality: If buffer is above the optimal/target level, |
| 578 | // bring active_worst_quality down depending on fullness of buffer. |
| 579 | // If buffer is below the optimal level, let the active_worst_quality go from |
| 580 | // ambient Q (at buffer = optimal level) to worst_quality level |
| 581 | // (at buffer = critical level). |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 582 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 583 | const RATE_CONTROL *rc = &cpi->rc; |
| 584 | // Buffer level below which we push active_worst to worst_quality. |
| 585 | int64_t critical_level = rc->optimal_buffer_level >> 3; |
| 586 | int64_t buff_lvl_step = 0; |
| 587 | int adjustment = 0; |
| 588 | int active_worst_quality; |
| 589 | int ambient_qp; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 590 | if (cm->frame_type == KEY_FRAME) return rc->worst_quality; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 591 | // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME] |
| 592 | // for the first few frames following key frame. These are both initialized |
| 593 | // to worst_quality and updated with (3/4, 1/4) average in postencode_update. |
| 594 | // So for first few frames following key, the qp of that key frame is weighted |
| 595 | // into the active_worst_quality setting. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 596 | ambient_qp = (cm->current_video_frame < 5) |
| 597 | ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME], |
| 598 | rc->avg_frame_qindex[KEY_FRAME]) |
| 599 | : rc->avg_frame_qindex[INTER_FRAME]; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 600 | active_worst_quality = VPXMIN(rc->worst_quality, ambient_qp * 5 / 4); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 601 | if (rc->buffer_level > rc->optimal_buffer_level) { |
| 602 | // Adjust down. |
| 603 | // Maximum limit for down adjustment, ~30%. |
| 604 | int max_adjustment_down = active_worst_quality / 3; |
| 605 | if (max_adjustment_down) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 606 | buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) / |
| 607 | max_adjustment_down); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 608 | if (buff_lvl_step) |
| 609 | adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) / |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 610 | buff_lvl_step); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 611 | active_worst_quality -= adjustment; |
| 612 | } |
| 613 | } else if (rc->buffer_level > critical_level) { |
| 614 | // Adjust up from ambient Q. |
| 615 | if (critical_level) { |
| 616 | buff_lvl_step = (rc->optimal_buffer_level - critical_level); |
| 617 | if (buff_lvl_step) { |
| 618 | adjustment = (int)((rc->worst_quality - ambient_qp) * |
| 619 | (rc->optimal_buffer_level - rc->buffer_level) / |
| 620 | buff_lvl_step); |
| 621 | } |
| 622 | active_worst_quality = ambient_qp + adjustment; |
| 623 | } |
| 624 | } else { |
| 625 | // Set to worst_quality if buffer is below critical level. |
| 626 | active_worst_quality = rc->worst_quality; |
| 627 | } |
| 628 | return active_worst_quality; |
| 629 | } |
| 630 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 631 | static int rc_pick_q_and_bounds_one_pass_cbr(const VP10_COMP *cpi, |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 632 | int *bottom_index, |
| 633 | int *top_index) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 634 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 635 | const RATE_CONTROL *const rc = &cpi->rc; |
| 636 | int active_best_quality; |
| 637 | int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi); |
| 638 | int q; |
| 639 | int *rtc_minq; |
| 640 | ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq); |
| 641 | |
| 642 | if (frame_is_intra_only(cm)) { |
| 643 | active_best_quality = rc->best_quality; |
| 644 | // Handle the special case for key frames forced when we have reached |
| 645 | // the maximum key frame interval. Here force the Q to a range |
| 646 | // based on the ambient Q to reduce the risk of popping. |
| 647 | if (rc->this_key_frame_forced) { |
| 648 | int qindex = rc->last_boosted_qindex; |
| 649 | double last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 650 | int delta_qindex = vp10_compute_qdelta( |
| 651 | rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth); |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 652 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 653 | } else if (cm->current_video_frame > 0) { |
| 654 | // not first frame of one pass and kf_boost is set |
| 655 | double q_adj_factor = 1.0; |
| 656 | double q_val; |
| 657 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 658 | active_best_quality = get_kf_active_quality( |
| 659 | rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 660 | |
| 661 | // Allow somewhat lower kf minq with small image formats. |
| 662 | if ((cm->width * cm->height) <= (352 * 288)) { |
| 663 | q_adj_factor -= 0.25; |
| 664 | } |
| 665 | |
| 666 | // Convert the adjustment factor to a qindex delta |
| 667 | // on active_best_quality. |
| 668 | q_val = vp10_convert_qindex_to_q(active_best_quality, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 669 | active_best_quality += |
| 670 | vp10_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 671 | } |
| 672 | } else if (!rc->is_src_frame_alt_ref && |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 673 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 674 | // Use the lower of active_worst_quality and recent |
| 675 | // average Q as basis for GF/ARF best Q limit unless last frame was |
| 676 | // a key frame. |
| 677 | if (rc->frames_since_key > 1 && |
| 678 | rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
| 679 | q = rc->avg_frame_qindex[INTER_FRAME]; |
| 680 | } else { |
| 681 | q = active_worst_quality; |
| 682 | } |
| 683 | active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
| 684 | } else { |
| 685 | // Use the lower of active_worst_quality and recent/average Q. |
| 686 | if (cm->current_video_frame > 1) { |
| 687 | if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) |
| 688 | active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]]; |
| 689 | else |
| 690 | active_best_quality = rtc_minq[active_worst_quality]; |
| 691 | } else { |
| 692 | if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality) |
| 693 | active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]]; |
| 694 | else |
| 695 | active_best_quality = rtc_minq[active_worst_quality]; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // Clip the active best and worst quality values to limits |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 700 | active_best_quality = |
| 701 | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
| 702 | active_worst_quality = |
| 703 | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 704 | |
| 705 | *top_index = active_worst_quality; |
| 706 | *bottom_index = active_best_quality; |
| 707 | |
| 708 | #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
| 709 | // Limit Q range for the adaptive loop. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 710 | if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced && |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 711 | !(cm->current_video_frame == 0)) { |
| 712 | int qdelta = 0; |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 713 | aom_clear_system_state(); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 714 | qdelta = vp10_compute_qdelta_by_rate( |
| 715 | &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 716 | *top_index = active_worst_quality + qdelta; |
| 717 | *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; |
| 718 | } |
| 719 | #endif |
| 720 | |
| 721 | // Special case code to try and match quality with forced key frames |
| 722 | if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) { |
| 723 | q = rc->last_boosted_qindex; |
| 724 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 725 | q = vp10_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
| 726 | active_worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 727 | if (q > *top_index) { |
| 728 | // Special case when we are targeting the max allowed rate |
| 729 | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
| 730 | *top_index = q; |
| 731 | else |
| 732 | q = *top_index; |
| 733 | } |
| 734 | } |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 735 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 736 | assert(*bottom_index <= rc->worst_quality && |
| 737 | *bottom_index >= rc->best_quality); |
| 738 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
| 739 | return q; |
| 740 | } |
| 741 | |
| 742 | static int get_active_cq_level(const RATE_CONTROL *rc, |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 743 | const VP10EncoderConfig *const oxcf) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 744 | static const double cq_adjust_threshold = 0.1; |
| 745 | int active_cq_level = oxcf->cq_level; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 746 | if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 747 | const double x = (double)rc->total_actual_bits / rc->total_target_bits; |
| 748 | if (x < cq_adjust_threshold) { |
| 749 | active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold); |
| 750 | } |
| 751 | } |
| 752 | return active_cq_level; |
| 753 | } |
| 754 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 755 | static int rc_pick_q_and_bounds_one_pass_vbr(const VP10_COMP *cpi, |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 756 | int *bottom_index, |
| 757 | int *top_index) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 758 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 759 | const RATE_CONTROL *const rc = &cpi->rc; |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 760 | const VP10EncoderConfig *const oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 761 | const int cq_level = get_active_cq_level(rc, oxcf); |
| 762 | int active_best_quality; |
| 763 | int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); |
| 764 | int q; |
| 765 | int *inter_minq; |
| 766 | ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq); |
| 767 | |
| 768 | if (frame_is_intra_only(cm)) { |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 769 | if (oxcf->rc_mode == VPX_Q) { |
| 770 | int qindex = cq_level; |
| 771 | double q = vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 772 | int delta_qindex = vp10_compute_qdelta(rc, q, q * 0.25, cm->bit_depth); |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 773 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
| 774 | } else if (rc->this_key_frame_forced) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 775 | int qindex = rc->last_boosted_qindex; |
| 776 | double last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 777 | int delta_qindex = vp10_compute_qdelta( |
| 778 | rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth); |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 779 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 780 | } else { |
| 781 | // not first frame of one pass and kf_boost is set |
| 782 | double q_adj_factor = 1.0; |
| 783 | double q_val; |
| 784 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 785 | active_best_quality = get_kf_active_quality( |
| 786 | rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 787 | |
| 788 | // Allow somewhat lower kf minq with small image formats. |
| 789 | if ((cm->width * cm->height) <= (352 * 288)) { |
| 790 | q_adj_factor -= 0.25; |
| 791 | } |
| 792 | |
| 793 | // Convert the adjustment factor to a qindex delta |
| 794 | // on active_best_quality. |
| 795 | q_val = vp10_convert_qindex_to_q(active_best_quality, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 796 | active_best_quality += |
| 797 | vp10_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 798 | } |
| 799 | } else if (!rc->is_src_frame_alt_ref && |
| 800 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 801 | // Use the lower of active_worst_quality and recent |
| 802 | // average Q as basis for GF/ARF best Q limit unless last frame was |
| 803 | // a key frame. |
| 804 | if (rc->frames_since_key > 1 && |
| 805 | rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
| 806 | q = rc->avg_frame_qindex[INTER_FRAME]; |
| 807 | } else { |
| 808 | q = rc->avg_frame_qindex[KEY_FRAME]; |
| 809 | } |
| 810 | // For constrained quality dont allow Q less than the cq level |
| 811 | if (oxcf->rc_mode == VPX_CQ) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 812 | if (q < cq_level) q = cq_level; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 813 | |
| 814 | active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
| 815 | |
| 816 | // Constrained quality use slightly lower active best. |
| 817 | active_best_quality = active_best_quality * 15 / 16; |
| 818 | |
| 819 | } else if (oxcf->rc_mode == VPX_Q) { |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 820 | int qindex = cq_level; |
| 821 | double q = vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
| 822 | int delta_qindex; |
| 823 | if (cpi->refresh_alt_ref_frame) |
| 824 | delta_qindex = vp10_compute_qdelta(rc, q, q * 0.40, cm->bit_depth); |
| 825 | else |
| 826 | delta_qindex = vp10_compute_qdelta(rc, q, q * 0.50, cm->bit_depth); |
| 827 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 828 | } else { |
| 829 | active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
| 830 | } |
| 831 | } else { |
| 832 | if (oxcf->rc_mode == VPX_Q) { |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 833 | int qindex = cq_level; |
| 834 | double q = vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 835 | double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0, |
| 836 | 0.70, 1.0, 0.85, 1.0 }; |
| 837 | int delta_qindex = vp10_compute_qdelta( |
| 838 | rc, q, q * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL], |
| 839 | cm->bit_depth); |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 840 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 841 | } else { |
| 842 | // Use the lower of active_worst_quality and recent/average Q. |
| 843 | if (cm->current_video_frame > 1) |
| 844 | active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; |
| 845 | else |
| 846 | active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; |
| 847 | // For the constrained quality mode we don't want |
| 848 | // q to fall below the cq level. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 849 | if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 850 | active_best_quality = cq_level; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | // Clip the active best and worst quality values to limits |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 856 | active_best_quality = |
| 857 | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
| 858 | active_worst_quality = |
| 859 | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 860 | |
| 861 | *top_index = active_worst_quality; |
| 862 | *bottom_index = active_best_quality; |
| 863 | |
| 864 | #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
| 865 | { |
| 866 | int qdelta = 0; |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 867 | aom_clear_system_state(); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 868 | |
| 869 | // Limit Q range for the adaptive loop. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 870 | if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced && |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 871 | !(cm->current_video_frame == 0)) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 872 | qdelta = vp10_compute_qdelta_by_rate( |
| 873 | &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 874 | } else if (!rc->is_src_frame_alt_ref && |
| 875 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 876 | qdelta = vp10_compute_qdelta_by_rate( |
| 877 | &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 878 | } |
| 879 | *top_index = active_worst_quality + qdelta; |
| 880 | *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; |
| 881 | } |
| 882 | #endif |
| 883 | |
| 884 | if (oxcf->rc_mode == VPX_Q) { |
| 885 | q = active_best_quality; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 886 | // Special case code to try and match quality with forced key frames |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 887 | } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { |
| 888 | q = rc->last_boosted_qindex; |
| 889 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 890 | q = vp10_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
| 891 | active_worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 892 | if (q > *top_index) { |
| 893 | // Special case when we are targeting the max allowed rate |
| 894 | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
| 895 | *top_index = q; |
| 896 | else |
| 897 | q = *top_index; |
| 898 | } |
| 899 | } |
| 900 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 901 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 902 | assert(*bottom_index <= rc->worst_quality && |
| 903 | *bottom_index >= rc->best_quality); |
| 904 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
| 905 | return q; |
| 906 | } |
| 907 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 908 | int vp10_frame_type_qdelta(const VP10_COMP *cpi, int rf_level, int q) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 909 | static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { |
| 910 | 1.00, // INTER_NORMAL |
| 911 | 1.00, // INTER_HIGH |
| 912 | 1.50, // GF_ARF_LOW |
| 913 | 1.75, // GF_ARF_STD |
| 914 | 2.00, // KF_STD |
| 915 | }; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 916 | static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] = { |
| 917 | INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME |
| 918 | }; |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 919 | const VP10_COMMON *const cm = &cpi->common; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 920 | int qdelta = |
| 921 | vp10_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], q, |
| 922 | rate_factor_deltas[rf_level], cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 923 | return qdelta; |
| 924 | } |
| 925 | |
| 926 | #define STATIC_MOTION_THRESH 95 |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 927 | static int rc_pick_q_and_bounds_two_pass(const VP10_COMP *cpi, |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 928 | int *bottom_index, int *top_index) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 929 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 930 | const RATE_CONTROL *const rc = &cpi->rc; |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 931 | const VP10EncoderConfig *const oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 932 | const GF_GROUP *gf_group = &cpi->twopass.gf_group; |
| 933 | const int cq_level = get_active_cq_level(rc, oxcf); |
| 934 | int active_best_quality; |
| 935 | int active_worst_quality = cpi->twopass.active_worst_quality; |
| 936 | int q; |
| 937 | int *inter_minq; |
| 938 | ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq); |
| 939 | |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 940 | if (frame_is_intra_only(cm)) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 941 | // Handle the special case for key frames forced when we have reached |
| 942 | // the maximum key frame interval. Here force the Q to a range |
| 943 | // based on the ambient Q to reduce the risk of popping. |
| 944 | if (rc->this_key_frame_forced) { |
| 945 | double last_boosted_q; |
| 946 | int delta_qindex; |
| 947 | int qindex; |
| 948 | |
| 949 | if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 950 | qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 951 | active_best_quality = qindex; |
| 952 | last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 953 | delta_qindex = vp10_compute_qdelta( |
| 954 | rc, last_boosted_q, last_boosted_q * 1.25, cm->bit_depth); |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 955 | active_worst_quality = |
| 956 | VPXMIN(qindex + delta_qindex, active_worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 957 | } else { |
| 958 | qindex = rc->last_boosted_qindex; |
| 959 | last_boosted_q = vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 960 | delta_qindex = vp10_compute_qdelta( |
| 961 | rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth); |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 962 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 963 | } |
| 964 | } else { |
| 965 | // Not forced keyframe. |
| 966 | double q_adj_factor = 1.0; |
| 967 | double q_val; |
| 968 | // Baseline value derived from cpi->active_worst_quality and kf boost. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 969 | active_best_quality = |
| 970 | get_kf_active_quality(rc, active_worst_quality, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 971 | |
| 972 | // Allow somewhat lower kf minq with small image formats. |
| 973 | if ((cm->width * cm->height) <= (352 * 288)) { |
| 974 | q_adj_factor -= 0.25; |
| 975 | } |
| 976 | |
| 977 | // Make a further adjustment based on the kf zero motion measure. |
| 978 | q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct); |
| 979 | |
| 980 | // Convert the adjustment factor to a qindex delta |
| 981 | // on active_best_quality. |
| 982 | q_val = vp10_convert_qindex_to_q(active_best_quality, cm->bit_depth); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 983 | active_best_quality += |
| 984 | vp10_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 985 | } |
| 986 | } else if (!rc->is_src_frame_alt_ref && |
| 987 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
| 988 | // Use the lower of active_worst_quality and recent |
| 989 | // average Q as basis for GF/ARF best Q limit unless last frame was |
| 990 | // a key frame. |
| 991 | if (rc->frames_since_key > 1 && |
| 992 | rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
| 993 | q = rc->avg_frame_qindex[INTER_FRAME]; |
| 994 | } else { |
| 995 | q = active_worst_quality; |
| 996 | } |
| 997 | // For constrained quality dont allow Q less than the cq level |
| 998 | if (oxcf->rc_mode == VPX_CQ) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 999 | if (q < cq_level) q = cq_level; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1000 | |
| 1001 | active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
| 1002 | |
| 1003 | // Constrained quality use slightly lower active best. |
| 1004 | active_best_quality = active_best_quality * 15 / 16; |
| 1005 | |
| 1006 | } else if (oxcf->rc_mode == VPX_Q) { |
| 1007 | if (!cpi->refresh_alt_ref_frame) { |
| 1008 | active_best_quality = cq_level; |
| 1009 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1010 | active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1011 | |
| 1012 | // Modify best quality for second level arfs. For mode VPX_Q this |
| 1013 | // becomes the baseline frame q. |
| 1014 | if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW) |
| 1015 | active_best_quality = (active_best_quality + cq_level + 1) / 2; |
| 1016 | } |
| 1017 | } else { |
| 1018 | active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
| 1019 | } |
| 1020 | } else { |
| 1021 | if (oxcf->rc_mode == VPX_Q) { |
| 1022 | active_best_quality = cq_level; |
| 1023 | } else { |
| 1024 | active_best_quality = inter_minq[active_worst_quality]; |
| 1025 | |
| 1026 | // For the constrained quality mode we don't want |
| 1027 | // q to fall below the cq level. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1028 | if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1029 | active_best_quality = cq_level; |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | // Extension to max or min Q if undershoot or overshoot is outside |
| 1035 | // the permitted range. |
| 1036 | if ((cpi->oxcf.rc_mode != VPX_Q) && |
| 1037 | (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { |
| 1038 | if (frame_is_intra_only(cm) || |
| 1039 | (!rc->is_src_frame_alt_ref && |
| 1040 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { |
| 1041 | active_best_quality -= |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1042 | (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1043 | active_worst_quality += (cpi->twopass.extend_maxq / 2); |
| 1044 | } else { |
| 1045 | active_best_quality -= |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1046 | (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1047 | active_worst_quality += cpi->twopass.extend_maxq; |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 1052 | aom_clear_system_state(); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1053 | // Static forced key frames Q restrictions dealt with elsewhere. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1054 | if (!(frame_is_intra_only(cm)) || !rc->this_key_frame_forced || |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1055 | (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1056 | int qdelta = vp10_frame_type_qdelta( |
| 1057 | cpi, gf_group->rf_level[gf_group->index], active_worst_quality); |
| 1058 | active_worst_quality = |
| 1059 | VPXMAX(active_worst_quality + qdelta, active_best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1060 | } |
| 1061 | #endif |
| 1062 | |
| 1063 | // Modify active_best_quality for downscaled normal frames. |
| 1064 | if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1065 | int qdelta = vp10_compute_qdelta_by_rate( |
| 1066 | rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth); |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1067 | active_best_quality = |
| 1068 | VPXMAX(active_best_quality + qdelta, rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1071 | active_best_quality = |
| 1072 | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
| 1073 | active_worst_quality = |
| 1074 | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1075 | |
| 1076 | if (oxcf->rc_mode == VPX_Q) { |
| 1077 | q = active_best_quality; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1078 | // Special case code to try and match quality with forced key frames. |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 1079 | } else if (frame_is_intra_only(cm) && rc->this_key_frame_forced) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1080 | // If static since last kf use better of last boosted and last kf q. |
| 1081 | if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1082 | q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1083 | } else { |
| 1084 | q = rc->last_boosted_qindex; |
| 1085 | } |
| 1086 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1087 | q = vp10_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
| 1088 | active_worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1089 | if (q > active_worst_quality) { |
| 1090 | // Special case when we are targeting the max allowed rate. |
| 1091 | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
| 1092 | active_worst_quality = q; |
| 1093 | else |
| 1094 | q = active_worst_quality; |
| 1095 | } |
| 1096 | } |
| 1097 | clamp(q, active_best_quality, active_worst_quality); |
| 1098 | |
| 1099 | *top_index = active_worst_quality; |
| 1100 | *bottom_index = active_best_quality; |
| 1101 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1102 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1103 | assert(*bottom_index <= rc->worst_quality && |
| 1104 | *bottom_index >= rc->best_quality); |
| 1105 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
| 1106 | return q; |
| 1107 | } |
| 1108 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1109 | int vp10_rc_pick_q_and_bounds(const VP10_COMP *cpi, int *bottom_index, |
| 1110 | int *top_index) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1111 | int q; |
| 1112 | if (cpi->oxcf.pass == 0) { |
| 1113 | if (cpi->oxcf.rc_mode == VPX_CBR) |
| 1114 | q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index); |
| 1115 | else |
| 1116 | q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index); |
| 1117 | } else { |
| 1118 | q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index); |
| 1119 | } |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1120 | |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1121 | return q; |
| 1122 | } |
| 1123 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1124 | void vp10_rc_compute_frame_size_bounds(const VP10_COMP *cpi, int frame_target, |
| 1125 | int *frame_under_shoot_limit, |
| 1126 | int *frame_over_shoot_limit) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1127 | if (cpi->oxcf.rc_mode == VPX_Q) { |
| 1128 | *frame_under_shoot_limit = 0; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1129 | *frame_over_shoot_limit = INT_MAX; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1130 | } else { |
| 1131 | // For very small rate targets where the fractional adjustment |
| 1132 | // may be tiny make sure there is at least a minimum range. |
| 1133 | const int tolerance = (cpi->sf.recode_tolerance * frame_target) / 100; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1134 | *frame_under_shoot_limit = VPXMAX(frame_target - tolerance - 200, 0); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1135 | *frame_over_shoot_limit = |
| 1136 | VPXMIN(frame_target + tolerance + 200, cpi->rc.max_frame_bandwidth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1140 | void vp10_rc_set_frame_target(VP10_COMP *cpi, int target) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 1141 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1142 | RATE_CONTROL *const rc = &cpi->rc; |
| 1143 | |
| 1144 | rc->this_frame_target = target; |
| 1145 | |
| 1146 | // Modify frame size target when down-scaling. |
| 1147 | if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && |
| 1148 | rc->frame_size_selector != UNSCALED) |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1149 | rc->this_frame_target = (int)(rc->this_frame_target * |
| 1150 | rate_thresh_mult[rc->frame_size_selector]); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1151 | |
| 1152 | // Target rate per SB64 (including partial SB64s. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1153 | rc->sb64_target_rate = |
| 1154 | ((int64_t)rc->this_frame_target * 64 * 64) / (cm->width * cm->height); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1157 | static void update_alt_ref_frame_stats(VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1158 | // this frame refreshes means next frames don't unless specified by user |
| 1159 | RATE_CONTROL *const rc = &cpi->rc; |
| 1160 | rc->frames_since_golden = 0; |
| 1161 | |
| 1162 | // Mark the alt ref as done (setting to 0 means no further alt refs pending). |
| 1163 | rc->source_alt_ref_pending = 0; |
| 1164 | |
| 1165 | // Set the alternate reference frame active flag |
| 1166 | rc->source_alt_ref_active = 1; |
| 1167 | } |
| 1168 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1169 | static void update_golden_frame_stats(VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1170 | RATE_CONTROL *const rc = &cpi->rc; |
| 1171 | |
| 1172 | // Update the Golden frame usage counts. |
| 1173 | if (cpi->refresh_golden_frame) { |
| 1174 | // this frame refreshes means next frames don't unless specified by user |
| 1175 | rc->frames_since_golden = 0; |
| 1176 | |
| 1177 | // If we are not using alt ref in the up and coming group clear the arf |
paulwilkins | 9d85ce8 | 2015-12-07 15:23:46 +0000 | [diff] [blame] | 1178 | // active flag. In multi arf group case, if the index is not 0 then |
| 1179 | // we are overlaying a mid group arf so should not reset the flag. |
| 1180 | if (cpi->oxcf.pass == 2) { |
| 1181 | if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0)) |
| 1182 | rc->source_alt_ref_active = 0; |
| 1183 | } else if (!rc->source_alt_ref_pending) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1184 | rc->source_alt_ref_active = 0; |
| 1185 | } |
| 1186 | |
| 1187 | // Decrement count down till next gf |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1188 | if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1189 | |
| 1190 | } else if (!cpi->refresh_alt_ref_frame) { |
| 1191 | // Decrement count down till next gf |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1192 | if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1193 | |
| 1194 | rc->frames_since_golden++; |
| 1195 | } |
| 1196 | } |
| 1197 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1198 | void vp10_rc_postencode_update(VP10_COMP *cpi, uint64_t bytes_used) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 1199 | const VP10_COMMON *const cm = &cpi->common; |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 1200 | const VP10EncoderConfig *const oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1201 | RATE_CONTROL *const rc = &cpi->rc; |
| 1202 | const int qindex = cm->base_qindex; |
| 1203 | |
| 1204 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) { |
| 1205 | vp10_cyclic_refresh_postencode(cpi); |
| 1206 | } |
| 1207 | |
| 1208 | // Update rate control heuristics |
| 1209 | rc->projected_frame_size = (int)(bytes_used << 3); |
| 1210 | |
| 1211 | // Post encode loop adjustment of Q prediction. |
| 1212 | vp10_rc_update_rate_correction_factors(cpi); |
| 1213 | |
| 1214 | // Keep a record of last Q and ambient average Q. |
| 1215 | if (cm->frame_type == KEY_FRAME) { |
| 1216 | rc->last_q[KEY_FRAME] = qindex; |
| 1217 | rc->avg_frame_qindex[KEY_FRAME] = |
| 1218 | ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); |
| 1219 | } else { |
paulwilkins | 9ce611a | 2015-12-15 14:53:44 +0000 | [diff] [blame] | 1220 | if (!rc->is_src_frame_alt_ref && |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 1221 | !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1222 | rc->last_q[INTER_FRAME] = qindex; |
| 1223 | rc->avg_frame_qindex[INTER_FRAME] = |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1224 | ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1225 | rc->ni_frames++; |
| 1226 | rc->tot_q += vp10_convert_qindex_to_q(qindex, cm->bit_depth); |
| 1227 | rc->avg_q = rc->tot_q / rc->ni_frames; |
| 1228 | // Calculate the average Q for normal inter frames (not key or GFU |
| 1229 | // frames). |
| 1230 | rc->ni_tot_qi += qindex; |
| 1231 | rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | // Keep record of last boosted (KF/KF/ARF) Q value. |
| 1236 | // If the current frame is coded at a lower Q then we also update it. |
| 1237 | // If all mbs in this group are skipped only update if the Q value is |
| 1238 | // better than that already stored. |
| 1239 | // This is used to help set quality in forced key frames to reduce popping |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1240 | if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) || |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1241 | (!rc->constrained_gf_group && |
| 1242 | (cpi->refresh_alt_ref_frame || |
| 1243 | (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { |
| 1244 | rc->last_boosted_qindex = qindex; |
| 1245 | } |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1246 | if (cm->frame_type == KEY_FRAME) rc->last_kf_qindex = qindex; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1247 | |
| 1248 | update_buffer_level(cpi, rc->projected_frame_size); |
| 1249 | |
| 1250 | // Rolling monitors of whether we are over or underspending used to help |
| 1251 | // regulate min and Max Q in two pass. |
| 1252 | if (cm->frame_type != KEY_FRAME) { |
| 1253 | rc->rolling_target_bits = ROUND_POWER_OF_TWO( |
| 1254 | rc->rolling_target_bits * 3 + rc->this_frame_target, 2); |
| 1255 | rc->rolling_actual_bits = ROUND_POWER_OF_TWO( |
| 1256 | rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2); |
| 1257 | rc->long_rolling_target_bits = ROUND_POWER_OF_TWO( |
| 1258 | rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5); |
| 1259 | rc->long_rolling_actual_bits = ROUND_POWER_OF_TWO( |
| 1260 | rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, 5); |
| 1261 | } |
| 1262 | |
| 1263 | // Actual bits spent |
| 1264 | rc->total_actual_bits += rc->projected_frame_size; |
| 1265 | rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0; |
| 1266 | |
| 1267 | rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits; |
| 1268 | |
| 1269 | if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame && |
| 1270 | (cm->frame_type != KEY_FRAME)) |
| 1271 | // Update the alternate reference frame stats as appropriate. |
| 1272 | update_alt_ref_frame_stats(cpi); |
| 1273 | else |
| 1274 | // Update the Golden frame stats as appropriate. |
| 1275 | update_golden_frame_stats(cpi); |
| 1276 | |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1277 | if (cm->frame_type == KEY_FRAME) rc->frames_since_key = 0; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1278 | if (cm->show_frame) { |
| 1279 | rc->frames_since_key++; |
| 1280 | rc->frames_to_key--; |
| 1281 | } |
| 1282 | |
| 1283 | // Trigger the resizing of the next frame if it is scaled. |
| 1284 | if (oxcf->pass != 0) { |
| 1285 | cpi->resize_pending = |
| 1286 | rc->next_frame_size_selector != rc->frame_size_selector; |
| 1287 | rc->frame_size_selector = rc->next_frame_size_selector; |
| 1288 | } |
| 1289 | } |
| 1290 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1291 | void vp10_rc_postencode_update_drop_frame(VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1292 | // Update buffer level with zero size, update frame counters, and return. |
| 1293 | update_buffer_level(cpi, 0); |
| 1294 | cpi->rc.frames_since_key++; |
| 1295 | cpi->rc.frames_to_key--; |
| 1296 | cpi->rc.rc_2_frame = 0; |
| 1297 | cpi->rc.rc_1_frame = 0; |
| 1298 | } |
| 1299 | |
| 1300 | // Use this macro to turn on/off use of alt-refs in one-pass mode. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1301 | #define USE_ALTREF_FOR_ONE_PASS 1 |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1302 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1303 | static int calc_pframe_target_size_one_pass_vbr(const VP10_COMP *const cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1304 | static const int af_ratio = 10; |
| 1305 | const RATE_CONTROL *const rc = &cpi->rc; |
| 1306 | int target; |
| 1307 | #if USE_ALTREF_FOR_ONE_PASS |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1308 | target = |
| 1309 | (!rc->is_src_frame_alt_ref && |
| 1310 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) |
| 1311 | ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) / |
| 1312 | (rc->baseline_gf_interval + af_ratio - 1) |
| 1313 | : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) / |
| 1314 | (rc->baseline_gf_interval + af_ratio - 1); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1315 | #else |
| 1316 | target = rc->avg_frame_bandwidth; |
| 1317 | #endif |
| 1318 | return vp10_rc_clamp_pframe_target_size(cpi, target); |
| 1319 | } |
| 1320 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1321 | static int calc_iframe_target_size_one_pass_vbr(const VP10_COMP *const cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1322 | static const int kf_ratio = 25; |
| 1323 | const RATE_CONTROL *rc = &cpi->rc; |
| 1324 | const int target = rc->avg_frame_bandwidth * kf_ratio; |
| 1325 | return vp10_rc_clamp_iframe_target_size(cpi, target); |
| 1326 | } |
| 1327 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1328 | void vp10_rc_get_one_pass_vbr_params(VP10_COMP *cpi) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 1329 | VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1330 | RATE_CONTROL *const rc = &cpi->rc; |
| 1331 | int target; |
| 1332 | // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic. |
| 1333 | if (!cpi->refresh_alt_ref_frame && |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1334 | (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) || |
| 1335 | rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1336 | cm->frame_type = KEY_FRAME; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1337 | rc->this_key_frame_forced = |
| 1338 | cm->current_video_frame != 0 && rc->frames_to_key == 0; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1339 | rc->frames_to_key = cpi->oxcf.key_freq; |
| 1340 | rc->kf_boost = DEFAULT_KF_BOOST; |
| 1341 | rc->source_alt_ref_active = 0; |
| 1342 | } else { |
| 1343 | cm->frame_type = INTER_FRAME; |
| 1344 | } |
| 1345 | if (rc->frames_till_gf_update_due == 0) { |
| 1346 | rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2; |
| 1347 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 1348 | // NOTE: frames_till_gf_update_due must be <= frames_to_key. |
| 1349 | if (rc->frames_till_gf_update_due > rc->frames_to_key) { |
| 1350 | rc->frames_till_gf_update_due = rc->frames_to_key; |
| 1351 | rc->constrained_gf_group = 1; |
| 1352 | } else { |
| 1353 | rc->constrained_gf_group = 0; |
| 1354 | } |
| 1355 | cpi->refresh_golden_frame = 1; |
| 1356 | rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS; |
| 1357 | rc->gfu_boost = DEFAULT_GF_BOOST; |
| 1358 | } |
| 1359 | if (cm->frame_type == KEY_FRAME) |
| 1360 | target = calc_iframe_target_size_one_pass_vbr(cpi); |
| 1361 | else |
| 1362 | target = calc_pframe_target_size_one_pass_vbr(cpi); |
| 1363 | vp10_rc_set_frame_target(cpi, target); |
| 1364 | } |
| 1365 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1366 | static int calc_pframe_target_size_one_pass_cbr(const VP10_COMP *cpi) { |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 1367 | const VP10EncoderConfig *oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1368 | const RATE_CONTROL *rc = &cpi->rc; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1369 | const int64_t diff = rc->optimal_buffer_level - rc->buffer_level; |
| 1370 | const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1371 | int min_frame_target = |
| 1372 | VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1373 | int target; |
| 1374 | |
| 1375 | if (oxcf->gf_cbr_boost_pct) { |
| 1376 | const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1377 | target = cpi->refresh_golden_frame |
| 1378 | ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * |
| 1379 | af_ratio_pct) / |
| 1380 | (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) |
| 1381 | : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) / |
| 1382 | (rc->baseline_gf_interval * 100 + af_ratio_pct - 100); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1383 | } else { |
| 1384 | target = rc->avg_frame_bandwidth; |
| 1385 | } |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 1386 | |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1387 | if (diff > 0) { |
| 1388 | // Lower the target bandwidth for this frame. |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1389 | const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1390 | target -= (target * pct_low) / 200; |
| 1391 | } else if (diff < 0) { |
| 1392 | // Increase the target bandwidth for this frame. |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1393 | const int pct_high = |
| 1394 | (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1395 | target += (target * pct_high) / 200; |
| 1396 | } |
| 1397 | if (oxcf->rc_max_inter_bitrate_pct) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1398 | const int max_rate = |
| 1399 | rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1400 | target = VPXMIN(target, max_rate); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1401 | } |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1402 | return VPXMAX(min_frame_target, target); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1405 | static int calc_iframe_target_size_one_pass_cbr(const VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1406 | const RATE_CONTROL *rc = &cpi->rc; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1407 | int target; |
| 1408 | if (cpi->common.current_video_frame == 0) { |
| 1409 | target = ((rc->starting_buffer_level / 2) > INT_MAX) |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1410 | ? INT_MAX |
| 1411 | : (int)(rc->starting_buffer_level / 2); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1412 | } else { |
| 1413 | int kf_boost = 32; |
| 1414 | double framerate = cpi->framerate; |
Yunqing Wang | c147c4d | 2015-08-27 15:11:38 -0700 | [diff] [blame] | 1415 | |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1416 | kf_boost = VPXMAX(kf_boost, (int)(2 * framerate - 16)); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1417 | if (rc->frames_since_key < framerate / 2) { |
| 1418 | kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2)); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1419 | } |
| 1420 | target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; |
| 1421 | } |
| 1422 | return vp10_rc_clamp_iframe_target_size(cpi, target); |
| 1423 | } |
| 1424 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1425 | void vp10_rc_get_one_pass_cbr_params(VP10_COMP *cpi) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 1426 | VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1427 | RATE_CONTROL *const rc = &cpi->rc; |
| 1428 | int target; |
| 1429 | // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1430 | if ((cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) || |
| 1431 | rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1432 | cm->frame_type = KEY_FRAME; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1433 | rc->this_key_frame_forced = |
| 1434 | cm->current_video_frame != 0 && rc->frames_to_key == 0; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1435 | rc->frames_to_key = cpi->oxcf.key_freq; |
| 1436 | rc->kf_boost = DEFAULT_KF_BOOST; |
| 1437 | rc->source_alt_ref_active = 0; |
| 1438 | } else { |
| 1439 | cm->frame_type = INTER_FRAME; |
| 1440 | } |
| 1441 | if (rc->frames_till_gf_update_due == 0) { |
| 1442 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
| 1443 | vp10_cyclic_refresh_set_golden_update(cpi); |
| 1444 | else |
| 1445 | rc->baseline_gf_interval = |
| 1446 | (rc->min_gf_interval + rc->max_gf_interval) / 2; |
| 1447 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 1448 | // NOTE: frames_till_gf_update_due must be <= frames_to_key. |
| 1449 | if (rc->frames_till_gf_update_due > rc->frames_to_key) |
| 1450 | rc->frames_till_gf_update_due = rc->frames_to_key; |
| 1451 | cpi->refresh_golden_frame = 1; |
| 1452 | rc->gfu_boost = DEFAULT_GF_BOOST; |
| 1453 | } |
| 1454 | |
| 1455 | // Any update/change of global cyclic refresh parameters (amount/delta-qp) |
| 1456 | // should be done here, before the frame qp is selected. |
| 1457 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
| 1458 | vp10_cyclic_refresh_update_parameters(cpi); |
| 1459 | |
| 1460 | if (cm->frame_type == KEY_FRAME) |
| 1461 | target = calc_iframe_target_size_one_pass_cbr(cpi); |
| 1462 | else |
| 1463 | target = calc_pframe_target_size_one_pass_cbr(cpi); |
| 1464 | |
| 1465 | vp10_rc_set_frame_target(cpi, target); |
| 1466 | if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC) |
| 1467 | cpi->resize_pending = vp10_resize_one_pass_cbr(cpi); |
| 1468 | else |
| 1469 | cpi->resize_pending = 0; |
| 1470 | } |
| 1471 | |
| 1472 | int vp10_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 1473 | aom_bit_depth_t bit_depth) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1474 | int start_index = rc->worst_quality; |
| 1475 | int target_index = rc->worst_quality; |
| 1476 | int i; |
| 1477 | |
| 1478 | // Convert the average q value to an index. |
| 1479 | for (i = rc->best_quality; i < rc->worst_quality; ++i) { |
| 1480 | start_index = i; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1481 | if (vp10_convert_qindex_to_q(i, bit_depth) >= qstart) break; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | // Convert the q target to an index |
| 1485 | for (i = rc->best_quality; i < rc->worst_quality; ++i) { |
| 1486 | target_index = i; |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1487 | if (vp10_convert_qindex_to_q(i, bit_depth) >= qtarget) break; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | return target_index - start_index; |
| 1491 | } |
| 1492 | |
| 1493 | int vp10_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1494 | int qindex, double rate_target_ratio, |
Adrian Grange | cebe6f0 | 2016-03-25 12:11:05 -0700 | [diff] [blame^] | 1495 | aom_bit_depth_t bit_depth) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1496 | int target_index = rc->worst_quality; |
| 1497 | int i; |
| 1498 | |
| 1499 | // Look up the current projected bits per block for the base index |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1500 | const int base_bits_per_mb = |
| 1501 | vp10_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1502 | |
| 1503 | // Find the target bits per mb based on the base value and given ratio. |
| 1504 | const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb); |
| 1505 | |
| 1506 | // Convert the q target to an index |
| 1507 | for (i = rc->best_quality; i < rc->worst_quality; ++i) { |
| 1508 | if (vp10_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <= |
| 1509 | target_bits_per_mb) { |
| 1510 | target_index = i; |
| 1511 | break; |
| 1512 | } |
| 1513 | } |
| 1514 | return target_index - qindex; |
| 1515 | } |
| 1516 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1517 | void vp10_rc_set_gf_interval_range(const VP10_COMP *const cpi, |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1518 | RATE_CONTROL *const rc) { |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 1519 | const VP10EncoderConfig *const oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1520 | |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 1521 | // Special case code for 1 pass fixed Q mode tests |
| 1522 | if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) { |
| 1523 | rc->max_gf_interval = FIXED_GF_INTERVAL; |
| 1524 | rc->min_gf_interval = FIXED_GF_INTERVAL; |
| 1525 | rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL; |
| 1526 | } else { |
| 1527 | // Set Maximum gf/arf interval |
| 1528 | rc->max_gf_interval = oxcf->max_gf_interval; |
| 1529 | rc->min_gf_interval = oxcf->min_gf_interval; |
| 1530 | if (rc->min_gf_interval == 0) |
| 1531 | rc->min_gf_interval = vp10_rc_get_default_min_gf_interval( |
| 1532 | oxcf->width, oxcf->height, cpi->framerate); |
| 1533 | if (rc->max_gf_interval == 0) |
| 1534 | rc->max_gf_interval = vp10_rc_get_default_max_gf_interval( |
| 1535 | cpi->framerate, rc->min_gf_interval); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1536 | |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 1537 | // Extended interval for genuinely static scenes |
| 1538 | rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1539 | |
paulwilkins | 9930900 | 2015-12-15 15:23:47 +0000 | [diff] [blame] | 1540 | if (is_altref_enabled(cpi)) { |
| 1541 | if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) |
| 1542 | rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1; |
| 1543 | } |
| 1544 | |
| 1545 | if (rc->max_gf_interval > rc->static_scene_max_gf_interval) |
| 1546 | rc->max_gf_interval = rc->static_scene_max_gf_interval; |
| 1547 | |
| 1548 | // Clamp min to max |
| 1549 | rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1550 | } |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1553 | void vp10_rc_update_framerate(VP10_COMP *cpi) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 1554 | const VP10_COMMON *const cm = &cpi->common; |
hui su | a4c7e92 | 2015-08-16 17:24:35 -0700 | [diff] [blame] | 1555 | const VP10EncoderConfig *const oxcf = &cpi->oxcf; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1556 | RATE_CONTROL *const rc = &cpi->rc; |
| 1557 | int vbr_max_bits; |
| 1558 | |
| 1559 | rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1560 | rc->min_frame_bandwidth = |
| 1561 | (int)(rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1562 | |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1563 | rc->min_frame_bandwidth = |
| 1564 | VPXMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1565 | |
| 1566 | // A maximum bitrate for a frame is defined. |
| 1567 | // The baseline for this aligns with HW implementations that |
| 1568 | // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits |
| 1569 | // per 16x16 MB (averaged over a frame). However this limit is extended if |
| 1570 | // a very high rate is given on the command line or the the rate cannnot |
| 1571 | // be acheived because of a user specificed max q (e.g. when the user |
| 1572 | // specifies lossless encode. |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1573 | vbr_max_bits = |
| 1574 | (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section) / |
| 1575 | 100); |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1576 | rc->max_frame_bandwidth = |
| 1577 | VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1578 | |
| 1579 | vp10_rc_set_gf_interval_range(cpi, rc); |
| 1580 | } |
| 1581 | |
| 1582 | #define VBR_PCT_ADJUSTMENT_LIMIT 50 |
| 1583 | // For VBR...adjustment to the frame target based on error from previous frames |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1584 | static void vbr_rate_correction(VP10_COMP *cpi, int *this_frame_target) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1585 | RATE_CONTROL *const rc = &cpi->rc; |
| 1586 | int64_t vbr_bits_off_target = rc->vbr_bits_off_target; |
| 1587 | int max_delta; |
| 1588 | double position_factor = 1.0; |
| 1589 | |
| 1590 | // How far through the clip are we. |
| 1591 | // This number is used to damp the per frame rate correction. |
| 1592 | // Range 0 - 1.0 |
| 1593 | if (cpi->twopass.total_stats.count) { |
| 1594 | position_factor = sqrt((double)cpi->common.current_video_frame / |
| 1595 | cpi->twopass.total_stats.count); |
| 1596 | } |
| 1597 | max_delta = (int)(position_factor * |
| 1598 | ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100)); |
| 1599 | |
| 1600 | // vbr_bits_off_target > 0 means we have extra bits to spend |
| 1601 | if (vbr_bits_off_target > 0) { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1602 | *this_frame_target += (vbr_bits_off_target > max_delta) |
| 1603 | ? max_delta |
| 1604 | : (int)vbr_bits_off_target; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1605 | } else { |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1606 | *this_frame_target -= (vbr_bits_off_target < -max_delta) |
| 1607 | ? max_delta |
| 1608 | : (int)-vbr_bits_off_target; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | // Fast redistribution of bits arising from massive local undershoot. |
| 1612 | // Dont do it for kf,arf,gf or overlay frames. |
| 1613 | if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref && |
| 1614 | rc->vbr_bits_off_target_fast) { |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1615 | int one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, *this_frame_target); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1616 | int fast_extra_bits; |
James Zern | 5e16d39 | 2015-08-17 18:19:22 -0700 | [diff] [blame] | 1617 | fast_extra_bits = (int)VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits); |
| 1618 | fast_extra_bits = (int)VPXMIN( |
| 1619 | fast_extra_bits, |
| 1620 | VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8)); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1621 | *this_frame_target += (int)fast_extra_bits; |
| 1622 | rc->vbr_bits_off_target_fast -= fast_extra_bits; |
| 1623 | } |
| 1624 | } |
| 1625 | |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1626 | void vp10_set_target_rate(VP10_COMP *cpi) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1627 | RATE_CONTROL *const rc = &cpi->rc; |
| 1628 | int target_rate = rc->base_frame_target; |
| 1629 | |
| 1630 | // Correction to rate target based on prior over or under shoot. |
| 1631 | if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ) |
| 1632 | vbr_rate_correction(cpi, &target_rate); |
| 1633 | vp10_rc_set_frame_target(cpi, target_rate); |
| 1634 | } |
| 1635 | |
| 1636 | // Check if we should resize, based on average QP from past x frames. |
| 1637 | // Only allow for resize at most one scale down for now, scaling factor is 2. |
Yaowu Xu | 26a9afc | 2015-08-13 09:42:27 -0700 | [diff] [blame] | 1638 | int vp10_resize_one_pass_cbr(VP10_COMP *cpi) { |
Yaowu Xu | fc7cbd1 | 2015-08-13 09:36:53 -0700 | [diff] [blame] | 1639 | const VP10_COMMON *const cm = &cpi->common; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1640 | RATE_CONTROL *const rc = &cpi->rc; |
| 1641 | int resize_now = 0; |
| 1642 | cpi->resize_scale_num = 1; |
| 1643 | cpi->resize_scale_den = 1; |
| 1644 | // Don't resize on key frame; reset the counters on key frame. |
| 1645 | if (cm->frame_type == KEY_FRAME) { |
| 1646 | cpi->resize_avg_qp = 0; |
| 1647 | cpi->resize_count = 0; |
| 1648 | return 0; |
| 1649 | } |
| 1650 | // Resize based on average buffer underflow and QP over some window. |
| 1651 | // Ignore samples close to key frame, since QP is usually high after key. |
| 1652 | if (cpi->rc.frames_since_key > 2 * cpi->framerate) { |
| 1653 | const int window = (int)(5 * cpi->framerate); |
| 1654 | cpi->resize_avg_qp += cm->base_qindex; |
| 1655 | if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100)) |
| 1656 | ++cpi->resize_buffer_underflow; |
| 1657 | ++cpi->resize_count; |
| 1658 | // Check for resize action every "window" frames. |
| 1659 | if (cpi->resize_count >= window) { |
| 1660 | int avg_qp = cpi->resize_avg_qp / cpi->resize_count; |
| 1661 | // Resize down if buffer level has underflowed sufficent amount in past |
| 1662 | // window, and we are at original resolution. |
| 1663 | // Resize back up if average QP is low, and we are currently in a resized |
| 1664 | // down state. |
| 1665 | if (cpi->resize_state == 0 && |
| 1666 | cpi->resize_buffer_underflow > (cpi->resize_count >> 2)) { |
| 1667 | resize_now = 1; |
| 1668 | cpi->resize_state = 1; |
| 1669 | } else if (cpi->resize_state == 1 && |
| 1670 | avg_qp < 40 * cpi->rc.worst_quality / 100) { |
| 1671 | resize_now = -1; |
| 1672 | cpi->resize_state = 0; |
| 1673 | } |
| 1674 | // Reset for next window measurement. |
| 1675 | cpi->resize_avg_qp = 0; |
| 1676 | cpi->resize_count = 0; |
| 1677 | cpi->resize_buffer_underflow = 0; |
| 1678 | } |
| 1679 | } |
| 1680 | // If decision is to resize, reset some quantities, and check is we should |
| 1681 | // reduce rate correction factor, |
| 1682 | if (resize_now != 0) { |
| 1683 | int target_bits_per_frame; |
| 1684 | int active_worst_quality; |
| 1685 | int qindex; |
| 1686 | int tot_scale_change; |
| 1687 | // For now, resize is by 1/2 x 1/2. |
| 1688 | cpi->resize_scale_num = 1; |
| 1689 | cpi->resize_scale_den = 2; |
| 1690 | tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) / |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1691 | (cpi->resize_scale_num * cpi->resize_scale_num); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1692 | // Reset buffer level to optimal, update target size. |
| 1693 | rc->buffer_level = rc->optimal_buffer_level; |
| 1694 | rc->bits_off_target = rc->optimal_buffer_level; |
| 1695 | rc->this_frame_target = calc_pframe_target_size_one_pass_cbr(cpi); |
| 1696 | // Reset cyclic refresh parameters. |
| 1697 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) |
| 1698 | vp10_cyclic_refresh_reset_resize(cpi); |
| 1699 | // Get the projected qindex, based on the scaled target frame size (scaled |
| 1700 | // so target_bits_per_mb in vp10_rc_regulate_q will be correct target). |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1701 | target_bits_per_frame = (resize_now == 1) |
| 1702 | ? rc->this_frame_target * tot_scale_change |
| 1703 | : rc->this_frame_target / tot_scale_change; |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1704 | active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi); |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1705 | qindex = vp10_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality, |
| 1706 | active_worst_quality); |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1707 | // If resize is down, check if projected q index is close to worst_quality, |
| 1708 | // and if so, reduce the rate correction factor (since likely can afford |
| 1709 | // lower q for resized frame). |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1710 | if (resize_now == 1 && qindex > 90 * cpi->rc.worst_quality / 100) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1711 | rc->rate_correction_factors[INTER_NORMAL] *= 0.85; |
| 1712 | } |
| 1713 | // If resize is back up, check if projected q index is too much above the |
| 1714 | // current base_qindex, and if so, reduce the rate correction factor |
| 1715 | // (since prefer to keep q for resized frame at least close to previous q). |
clang-format | 99e28b8 | 2016-01-27 12:42:45 -0800 | [diff] [blame] | 1716 | if (resize_now == -1 && qindex > 130 * cm->base_qindex / 100) { |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 1717 | rc->rate_correction_factors[INTER_NORMAL] *= 0.9; |
| 1718 | } |
| 1719 | } |
| 1720 | return resize_now; |
| 1721 | } |