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