/src/aom/av1/encoder/ratectrl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
3 | | * |
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. |
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 | | |
19 | | #include "aom_dsp/aom_dsp_common.h" |
20 | | #include "aom_mem/aom_mem.h" |
21 | | #include "aom_ports/mem.h" |
22 | | |
23 | | #include "av1/common/alloccommon.h" |
24 | | #include "av1/encoder/aq_cyclicrefresh.h" |
25 | | #include "av1/common/common.h" |
26 | | #include "av1/common/entropymode.h" |
27 | | #include "av1/common/quant_common.h" |
28 | | #include "av1/common/seg_common.h" |
29 | | |
30 | | #include "av1/encoder/encodemv.h" |
31 | | #include "av1/encoder/encode_strategy.h" |
32 | | #include "av1/encoder/gop_structure.h" |
33 | | #include "av1/encoder/random.h" |
34 | | #include "av1/encoder/ratectrl.h" |
35 | | |
36 | | #define USE_UNRESTRICTED_Q_IN_CQ_MODE 0 |
37 | | |
38 | | // Max rate target for 1080P and below encodes under normal circumstances |
39 | | // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB |
40 | | #define MAX_MB_RATE 250 |
41 | | #define MAXRATE_1080P 2025000 |
42 | | |
43 | 3.64k | #define MIN_BPB_FACTOR 0.005 |
44 | 2.53k | #define MAX_BPB_FACTOR 50 |
45 | | |
46 | 0 | #define SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO 0 |
47 | 0 | #define SUPERRES_QADJ_PER_DENOM_KEYFRAME 2 |
48 | 0 | #define SUPERRES_QADJ_PER_DENOM_ARFFRAME 0 |
49 | | |
50 | 1.26k | #define FRAME_OVERHEAD_BITS 200 |
51 | | #define ASSIGN_MINQ_TABLE(bit_depth, name) \ |
52 | 0 | do { \ |
53 | 0 | switch (bit_depth) { \ |
54 | 0 | case AOM_BITS_8: name = name##_8; break; \ |
55 | 0 | case AOM_BITS_10: name = name##_10; break; \ |
56 | 0 | case AOM_BITS_12: name = name##_12; break; \ |
57 | 0 | default: \ |
58 | 0 | assert(0 && \ |
59 | 0 | "bit_depth should be AOM_BITS_8, AOM_BITS_10" \ |
60 | 0 | " or AOM_BITS_12"); \ |
61 | 0 | name = NULL; \ |
62 | 0 | } \ |
63 | 0 | } while (0) |
64 | | |
65 | | // Tables relating active max Q to active min Q |
66 | | static int kf_low_motion_minq_8[QINDEX_RANGE]; |
67 | | static int kf_high_motion_minq_8[QINDEX_RANGE]; |
68 | | static int arfgf_low_motion_minq_8[QINDEX_RANGE]; |
69 | | static int arfgf_high_motion_minq_8[QINDEX_RANGE]; |
70 | | static int inter_minq_8[QINDEX_RANGE]; |
71 | | static int rtc_minq_8[QINDEX_RANGE]; |
72 | | |
73 | | static int kf_low_motion_minq_10[QINDEX_RANGE]; |
74 | | static int kf_high_motion_minq_10[QINDEX_RANGE]; |
75 | | static int arfgf_low_motion_minq_10[QINDEX_RANGE]; |
76 | | static int arfgf_high_motion_minq_10[QINDEX_RANGE]; |
77 | | static int inter_minq_10[QINDEX_RANGE]; |
78 | | static int rtc_minq_10[QINDEX_RANGE]; |
79 | | static int kf_low_motion_minq_12[QINDEX_RANGE]; |
80 | | static int kf_high_motion_minq_12[QINDEX_RANGE]; |
81 | | static int arfgf_low_motion_minq_12[QINDEX_RANGE]; |
82 | | static int arfgf_high_motion_minq_12[QINDEX_RANGE]; |
83 | | static int inter_minq_12[QINDEX_RANGE]; |
84 | | static int rtc_minq_12[QINDEX_RANGE]; |
85 | | |
86 | | static int gf_high = 2400; |
87 | | static int gf_low = 300; |
88 | | #ifdef STRICT_RC |
89 | | static int kf_high = 3200; |
90 | | #else |
91 | | static int kf_high = 5000; |
92 | | #endif |
93 | | static int kf_low = 400; |
94 | | |
95 | | // How many times less pixels there are to encode given the current scaling. |
96 | | // Temporary replacement for rcf_mult and rate_thresh_mult. |
97 | | static double resize_rate_factor(const FrameDimensionCfg *const frm_dim_cfg, |
98 | 2.52k | int width, int height) { |
99 | 2.52k | return (double)(frm_dim_cfg->width * frm_dim_cfg->height) / (width * height); |
100 | 2.52k | } |
101 | | |
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, |
107 | 4.60k | aom_bit_depth_t bit_depth) { |
108 | 4.60k | const double minqtarget = AOMMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq); |
109 | | |
110 | | // Special case handling to deal with the step from q2.0 |
111 | | // down to lossless mode represented by q 1.0. |
112 | 4.60k | if (minqtarget <= 2.0) return 0; |
113 | | |
114 | 4.20k | return av1_find_qindex(minqtarget, bit_depth, 0, QINDEX_RANGE - 1); |
115 | 4.60k | } |
116 | | |
117 | | static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low, |
118 | | int *arfgf_high, int *inter, int *rtc, |
119 | 3 | aom_bit_depth_t bit_depth) { |
120 | 3 | int i; |
121 | 771 | for (i = 0; i < QINDEX_RANGE; i++) { |
122 | 768 | const double maxq = av1_convert_qindex_to_q(i, bit_depth); |
123 | 768 | kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth); |
124 | 768 | kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth); |
125 | 768 | arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth); |
126 | 768 | arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); |
127 | 768 | inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth); |
128 | 768 | rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); |
129 | 768 | } |
130 | 3 | } |
131 | | |
132 | 1 | void av1_rc_init_minq_luts(void) { |
133 | 1 | init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8, |
134 | 1 | arfgf_low_motion_minq_8, arfgf_high_motion_minq_8, |
135 | 1 | inter_minq_8, rtc_minq_8, AOM_BITS_8); |
136 | 1 | init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10, |
137 | 1 | arfgf_low_motion_minq_10, arfgf_high_motion_minq_10, |
138 | 1 | inter_minq_10, rtc_minq_10, AOM_BITS_10); |
139 | 1 | init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12, |
140 | 1 | arfgf_low_motion_minq_12, arfgf_high_motion_minq_12, |
141 | 1 | inter_minq_12, rtc_minq_12, AOM_BITS_12); |
142 | 1 | } |
143 | | |
144 | | // These functions use formulaic calculations to make playing with the |
145 | | // quantizer tables easier. If necessary they can be replaced by lookup |
146 | | // tables if and when things settle down in the experimental bitstream |
147 | 37.6k | double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth) { |
148 | | // Convert the index to a real Q value (scaled down to match old Q values) |
149 | 37.6k | switch (bit_depth) { |
150 | 14.5k | case AOM_BITS_8: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 4.0; |
151 | 11.6k | case AOM_BITS_10: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 16.0; |
152 | 11.5k | case AOM_BITS_12: return av1_ac_quant_QTX(qindex, 0, bit_depth) / 64.0; |
153 | 0 | default: |
154 | 0 | assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12"); |
155 | 0 | return -1.0; |
156 | 37.6k | } |
157 | 37.6k | } |
158 | | |
159 | | int av1_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
160 | | double correction_factor, aom_bit_depth_t bit_depth, |
161 | 1.26k | const int is_screen_content_type) { |
162 | 1.26k | const double q = av1_convert_qindex_to_q(qindex, bit_depth); |
163 | 1.26k | int enumerator = frame_type == KEY_FRAME ? 2000000 : 1500000; |
164 | 1.26k | if (is_screen_content_type) { |
165 | 0 | enumerator = frame_type == KEY_FRAME ? 1000000 : 750000; |
166 | 0 | } |
167 | | |
168 | 1.26k | assert(correction_factor <= MAX_BPB_FACTOR && |
169 | 1.26k | correction_factor >= MIN_BPB_FACTOR); |
170 | | |
171 | | // q based adjustment to baseline enumerator |
172 | 1.26k | return (int)(enumerator * correction_factor / q); |
173 | 1.26k | } |
174 | | |
175 | | int av1_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, |
176 | | double correction_factor, aom_bit_depth_t bit_depth, |
177 | 1.26k | const int is_screen_content_type) { |
178 | 1.26k | const int bpm = (int)(av1_rc_bits_per_mb(frame_type, q, correction_factor, |
179 | 1.26k | bit_depth, is_screen_content_type)); |
180 | 1.26k | return AOMMAX(FRAME_OVERHEAD_BITS, |
181 | 1.26k | (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS); |
182 | 1.26k | } |
183 | | |
184 | | int av1_rc_clamp_pframe_target_size(const AV1_COMP *const cpi, int target, |
185 | 0 | FRAME_UPDATE_TYPE frame_update_type) { |
186 | 0 | const RATE_CONTROL *rc = &cpi->rc; |
187 | 0 | const AV1EncoderConfig *oxcf = &cpi->oxcf; |
188 | 0 | const int min_frame_target = |
189 | 0 | AOMMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5); |
190 | | // Clip the frame target to the minimum setup value. |
191 | 0 | if (frame_update_type == OVERLAY_UPDATE || |
192 | 0 | frame_update_type == INTNL_OVERLAY_UPDATE) { |
193 | | // If there is an active ARF at this location use the minimum |
194 | | // bits on this frame even if it is a constructed arf. |
195 | | // The active maximum quantizer insures that an appropriate |
196 | | // number of bits will be spent if needed for constructed ARFs. |
197 | 0 | target = min_frame_target; |
198 | 0 | } else if (target < min_frame_target) { |
199 | 0 | target = min_frame_target; |
200 | 0 | } |
201 | | |
202 | | // Clip the frame target to the maximum allowed value. |
203 | 0 | if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; |
204 | 0 | if (oxcf->rc_cfg.max_inter_bitrate_pct) { |
205 | 0 | const int max_rate = |
206 | 0 | rc->avg_frame_bandwidth * oxcf->rc_cfg.max_inter_bitrate_pct / 100; |
207 | 0 | target = AOMMIN(target, max_rate); |
208 | 0 | } |
209 | |
|
210 | 0 | return target; |
211 | 0 | } |
212 | | |
213 | 2.52k | int av1_rc_clamp_iframe_target_size(const AV1_COMP *const cpi, int target) { |
214 | 2.52k | const RATE_CONTROL *rc = &cpi->rc; |
215 | 2.52k | const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg; |
216 | 2.52k | if (rc_cfg->max_intra_bitrate_pct) { |
217 | 0 | const int max_rate = |
218 | 0 | rc->avg_frame_bandwidth * rc_cfg->max_intra_bitrate_pct / 100; |
219 | 0 | target = AOMMIN(target, max_rate); |
220 | 0 | } |
221 | 2.52k | if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; |
222 | 2.52k | return target; |
223 | 2.52k | } |
224 | | |
225 | | // Update the buffer level for higher temporal layers, given the encoded current |
226 | | // temporal layer. |
227 | 0 | static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) { |
228 | 0 | const int current_temporal_layer = svc->temporal_layer_id; |
229 | 0 | for (int i = current_temporal_layer + 1; i < svc->number_temporal_layers; |
230 | 0 | ++i) { |
231 | 0 | const int layer = |
232 | 0 | LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers); |
233 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
234 | 0 | PRIMARY_RATE_CONTROL *lp_rc = &lc->p_rc; |
235 | 0 | lp_rc->bits_off_target += |
236 | 0 | (int)round(lc->target_bandwidth / lc->framerate) - encoded_frame_size; |
237 | | // Clip buffer level to maximum buffer size for the layer. |
238 | 0 | lp_rc->bits_off_target = |
239 | 0 | AOMMIN(lp_rc->bits_off_target, lp_rc->maximum_buffer_size); |
240 | 0 | lp_rc->buffer_level = lp_rc->bits_off_target; |
241 | 0 | } |
242 | 0 | } |
243 | | // Update the buffer level: leaky bucket model. |
244 | 1.26k | static void update_buffer_level(AV1_COMP *cpi, int encoded_frame_size) { |
245 | 1.26k | const AV1_COMMON *const cm = &cpi->common; |
246 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
247 | 1.26k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
248 | | |
249 | | // Non-viewable frames are a special case and are treated as pure overhead. |
250 | 1.26k | if (!cm->show_frame) |
251 | 0 | p_rc->bits_off_target -= encoded_frame_size; |
252 | 1.26k | else |
253 | 1.26k | p_rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size; |
254 | | |
255 | | // Clip the buffer level to the maximum specified buffer size. |
256 | 1.26k | p_rc->bits_off_target = |
257 | 1.26k | AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size); |
258 | 1.26k | p_rc->buffer_level = p_rc->bits_off_target; |
259 | | |
260 | 1.26k | if (cpi->ppi->use_svc) |
261 | 0 | update_layer_buffer_level(&cpi->svc, encoded_frame_size); |
262 | | |
263 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
264 | | /* The variable temp_buffer_level is introduced for quality |
265 | | * simulation purpose, it retains the value previous to the parallel |
266 | | * encode frames. The variable is updated based on the update flag. |
267 | | * |
268 | | * If there exist show_existing_frames between parallel frames, then to |
269 | | * retain the temp state do not update it. */ |
270 | | int show_existing_between_parallel_frames = |
271 | | (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == |
272 | | INTNL_OVERLAY_UPDATE && |
273 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2); |
274 | | |
275 | | if (cpi->do_frame_data_update && !show_existing_between_parallel_frames && |
276 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) { |
277 | | p_rc->temp_buffer_level = p_rc->buffer_level; |
278 | | } |
279 | | #endif |
280 | 1.26k | } |
281 | | |
282 | | int av1_rc_get_default_min_gf_interval(int width, int height, |
283 | 2.52k | double framerate) { |
284 | | // Assume we do not need any constraint lower than 4K 20 fps |
285 | 2.52k | static const double factor_safe = 3840 * 2160 * 20.0; |
286 | 2.52k | const double factor = width * height * framerate; |
287 | 2.52k | const int default_interval = |
288 | 2.52k | clamp((int)(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL); |
289 | | |
290 | 2.52k | if (factor <= factor_safe) |
291 | 2.52k | return default_interval; |
292 | 0 | else |
293 | 0 | return AOMMAX(default_interval, |
294 | 2.52k | (int)(MIN_GF_INTERVAL * factor / factor_safe + 0.5)); |
295 | | // Note this logic makes: |
296 | | // 4K24: 5 |
297 | | // 4K30: 6 |
298 | | // 4K60: 12 |
299 | 2.52k | } |
300 | | |
301 | 2.52k | int av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) { |
302 | 2.52k | int interval = AOMMIN(MAX_GF_INTERVAL, (int)(framerate * 0.75)); |
303 | 2.52k | interval += (interval & 0x01); // Round to even value |
304 | 2.52k | interval = AOMMAX(MAX_GF_INTERVAL, interval); |
305 | 2.52k | return AOMMAX(interval, min_gf_interval); |
306 | 2.52k | } |
307 | | |
308 | | void av1_primary_rc_init(const AV1EncoderConfig *oxcf, |
309 | 1.26k | PRIMARY_RATE_CONTROL *p_rc) { |
310 | 1.26k | const RateControlCfg *const rc_cfg = &oxcf->rc_cfg; |
311 | | |
312 | 1.26k | int worst_allowed_q = rc_cfg->worst_allowed_q; |
313 | | |
314 | 1.26k | int min_gf_interval = oxcf->gf_cfg.min_gf_interval; |
315 | 1.26k | int max_gf_interval = oxcf->gf_cfg.max_gf_interval; |
316 | 1.26k | if (min_gf_interval == 0) |
317 | 1.26k | min_gf_interval = av1_rc_get_default_min_gf_interval( |
318 | 1.26k | oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height, |
319 | 1.26k | oxcf->input_cfg.init_framerate); |
320 | 1.26k | if (max_gf_interval == 0) |
321 | 1.26k | max_gf_interval = av1_rc_get_default_max_gf_interval( |
322 | 1.26k | oxcf->input_cfg.init_framerate, min_gf_interval); |
323 | 1.26k | p_rc->baseline_gf_interval = (min_gf_interval + max_gf_interval) / 2; |
324 | 1.26k | p_rc->this_key_frame_forced = 0; |
325 | 1.26k | p_rc->next_key_frame_forced = 0; |
326 | 1.26k | p_rc->ni_frames = 0; |
327 | | |
328 | 1.26k | p_rc->tot_q = 0.0; |
329 | 1.26k | p_rc->total_actual_bits = 0; |
330 | 1.26k | p_rc->total_target_bits = 0; |
331 | 1.26k | p_rc->buffer_level = p_rc->starting_buffer_level; |
332 | | |
333 | 1.26k | if (oxcf->target_seq_level_idx[0] < SEQ_LEVELS) { |
334 | 0 | worst_allowed_q = 255; |
335 | 0 | } |
336 | 1.26k | if (oxcf->pass == AOM_RC_ONE_PASS && rc_cfg->mode == AOM_CBR) { |
337 | 0 | p_rc->avg_frame_qindex[KEY_FRAME] = worst_allowed_q; |
338 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] = worst_allowed_q; |
339 | 1.26k | } else { |
340 | 1.26k | p_rc->avg_frame_qindex[KEY_FRAME] = |
341 | 1.26k | (worst_allowed_q + rc_cfg->best_allowed_q) / 2; |
342 | 1.26k | p_rc->avg_frame_qindex[INTER_FRAME] = |
343 | 1.26k | (worst_allowed_q + rc_cfg->best_allowed_q) / 2; |
344 | 1.26k | } |
345 | 1.26k | p_rc->avg_q = av1_convert_qindex_to_q(rc_cfg->worst_allowed_q, |
346 | 1.26k | oxcf->tool_cfg.bit_depth); |
347 | 1.26k | p_rc->last_q[KEY_FRAME] = rc_cfg->best_allowed_q; |
348 | 1.26k | p_rc->last_q[INTER_FRAME] = rc_cfg->worst_allowed_q; |
349 | | |
350 | 6.31k | for (int i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
351 | 5.04k | p_rc->rate_correction_factors[i] = 0.7; |
352 | 5.04k | } |
353 | 1.26k | p_rc->rate_correction_factors[KF_STD] = 1.0; |
354 | 1.26k | p_rc->bits_off_target = p_rc->starting_buffer_level; |
355 | | |
356 | 1.26k | p_rc->rolling_target_bits = |
357 | 1.26k | (int)(oxcf->rc_cfg.target_bandwidth / oxcf->input_cfg.init_framerate); |
358 | 1.26k | p_rc->rolling_actual_bits = |
359 | 1.26k | (int)(oxcf->rc_cfg.target_bandwidth / oxcf->input_cfg.init_framerate); |
360 | 1.26k | } |
361 | | |
362 | 1.26k | void av1_rc_init(const AV1EncoderConfig *oxcf, RATE_CONTROL *rc) { |
363 | 1.26k | const RateControlCfg *const rc_cfg = &oxcf->rc_cfg; |
364 | | |
365 | 1.26k | rc->frames_since_key = 8; // Sensible default for first frame. |
366 | | |
367 | 1.26k | rc->frames_till_gf_update_due = 0; |
368 | 1.26k | rc->ni_av_qi = rc_cfg->worst_allowed_q; |
369 | 1.26k | rc->ni_tot_qi = 0; |
370 | | |
371 | 1.26k | rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval; |
372 | 1.26k | rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval; |
373 | 1.26k | if (rc->min_gf_interval == 0) |
374 | 1.26k | rc->min_gf_interval = av1_rc_get_default_min_gf_interval( |
375 | 1.26k | oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height, |
376 | 1.26k | oxcf->input_cfg.init_framerate); |
377 | 1.26k | if (rc->max_gf_interval == 0) |
378 | 1.26k | rc->max_gf_interval = av1_rc_get_default_max_gf_interval( |
379 | 1.26k | oxcf->input_cfg.init_framerate, rc->min_gf_interval); |
380 | 1.26k | rc->avg_frame_low_motion = 0; |
381 | | |
382 | 1.26k | rc->resize_state = ORIG; |
383 | 1.26k | rc->resize_avg_qp = 0; |
384 | 1.26k | rc->resize_buffer_underflow = 0; |
385 | 1.26k | rc->resize_count = 0; |
386 | 1.26k | rc->rtc_external_ratectrl = 0; |
387 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
388 | | rc->frame_level_fast_extra_bits = 0; |
389 | | #endif |
390 | 1.26k | } |
391 | | |
392 | 0 | int av1_rc_drop_frame(AV1_COMP *cpi) { |
393 | 0 | const AV1EncoderConfig *oxcf = &cpi->oxcf; |
394 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
395 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
396 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
397 | | const int simulate_parallel_frame = |
398 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
399 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
400 | | int64_t buffer_level = |
401 | | simulate_parallel_frame ? p_rc->temp_buffer_level : p_rc->buffer_level; |
402 | | #else |
403 | 0 | int64_t buffer_level = p_rc->buffer_level; |
404 | 0 | #endif |
405 | |
|
406 | 0 | if (!oxcf->rc_cfg.drop_frames_water_mark) { |
407 | 0 | return 0; |
408 | 0 | } else { |
409 | 0 | if (buffer_level < 0) { |
410 | | // Always drop if buffer is below 0. |
411 | 0 | return 1; |
412 | 0 | } else { |
413 | | // If buffer is below drop_mark, for now just drop every other frame |
414 | | // (starting with the next frame) until it increases back over drop_mark. |
415 | 0 | int drop_mark = (int)(oxcf->rc_cfg.drop_frames_water_mark * |
416 | 0 | p_rc->optimal_buffer_level / 100); |
417 | 0 | if ((buffer_level > drop_mark) && (rc->decimation_factor > 0)) { |
418 | 0 | --rc->decimation_factor; |
419 | 0 | } else if (buffer_level <= drop_mark && rc->decimation_factor == 0) { |
420 | 0 | rc->decimation_factor = 1; |
421 | 0 | } |
422 | 0 | if (rc->decimation_factor > 0) { |
423 | 0 | if (rc->decimation_count > 0) { |
424 | 0 | --rc->decimation_count; |
425 | 0 | return 1; |
426 | 0 | } else { |
427 | 0 | rc->decimation_count = rc->decimation_factor; |
428 | 0 | return 0; |
429 | 0 | } |
430 | 0 | } else { |
431 | 0 | rc->decimation_count = 0; |
432 | 0 | return 0; |
433 | 0 | } |
434 | 0 | } |
435 | 0 | } |
436 | 0 | } |
437 | | |
438 | 0 | static int adjust_q_cbr(const AV1_COMP *cpi, int q, int active_worst_quality) { |
439 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
440 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
441 | 0 | const AV1_COMMON *const cm = &cpi->common; |
442 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
443 | 0 | const int max_delta_down = 16; |
444 | 0 | const int max_delta_up = 20; |
445 | 0 | const int change_avg_frame_bandwidth = |
446 | 0 | abs(rc->avg_frame_bandwidth - rc->prev_avg_frame_bandwidth) > |
447 | 0 | 0.1 * (rc->avg_frame_bandwidth); |
448 | | // If resolution changes or avg_frame_bandwidth significantly changed, |
449 | | // then set this flag to indicate change in target bits per macroblock. |
450 | 0 | const int change_target_bits_mb = |
451 | 0 | cm->prev_frame && |
452 | 0 | (cm->width != cm->prev_frame->width || |
453 | 0 | cm->height != cm->prev_frame->height || change_avg_frame_bandwidth); |
454 | | // Apply some control/clamp to QP under certain conditions. |
455 | 0 | if (cm->current_frame.frame_type != KEY_FRAME && !cpi->ppi->use_svc && |
456 | 0 | rc->frames_since_key > 1 && !change_target_bits_mb && |
457 | 0 | (!cpi->oxcf.rc_cfg.gf_cbr_boost_pct || |
458 | 0 | !(refresh_frame->alt_ref_frame || refresh_frame->golden_frame))) { |
459 | | // Make sure q is between oscillating Qs to prevent resonance. |
460 | 0 | if (rc->rc_1_frame * rc->rc_2_frame == -1 && |
461 | 0 | rc->q_1_frame != rc->q_2_frame) { |
462 | 0 | int qclamp = clamp(q, AOMMIN(rc->q_1_frame, rc->q_2_frame), |
463 | 0 | AOMMAX(rc->q_1_frame, rc->q_2_frame)); |
464 | | // If the previous frame had overshoot and the current q needs to |
465 | | // increase above the clamped value, reduce the clamp for faster reaction |
466 | | // to overshoot. |
467 | 0 | if (cpi->rc.rc_1_frame == -1 && q > qclamp && rc->frames_since_key > 10) |
468 | 0 | q = (q + qclamp) >> 1; |
469 | 0 | else |
470 | 0 | q = qclamp; |
471 | 0 | } |
472 | | // Adjust Q base on source content change from scene detection. |
473 | 0 | if (cpi->sf.rt_sf.check_scene_detection && rc->prev_avg_source_sad > 0 && |
474 | 0 | rc->frames_since_key > 10 && !cpi->ppi->use_svc) { |
475 | 0 | const int bit_depth = cm->seq_params->bit_depth; |
476 | 0 | double delta = |
477 | 0 | (double)rc->avg_source_sad / (double)rc->prev_avg_source_sad - 1.0; |
478 | | // Push Q downwards if content change is decreasing and buffer level |
479 | | // is stable (at least 1/4-optimal level), so not overshooting. Do so |
480 | | // only for high Q to avoid excess overshoot. |
481 | | // Else reduce decrease in Q from previous frame if content change is |
482 | | // increasing and buffer is below max (so not undershooting). |
483 | 0 | if (delta < 0.0 && |
484 | 0 | p_rc->buffer_level > (p_rc->optimal_buffer_level >> 2) && |
485 | 0 | q > (rc->worst_quality >> 1)) { |
486 | 0 | double q_adj_factor = 1.0 + 0.5 * tanh(4.0 * delta); |
487 | 0 | double q_val = av1_convert_qindex_to_q(q, bit_depth); |
488 | 0 | q += av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth); |
489 | 0 | } else if (rc->q_1_frame - q > 0 && delta > 0.1 && |
490 | 0 | p_rc->buffer_level < AOMMIN(p_rc->maximum_buffer_size, |
491 | 0 | p_rc->optimal_buffer_level << 1)) { |
492 | 0 | q = (3 * q + rc->q_1_frame) >> 2; |
493 | 0 | } |
494 | 0 | } |
495 | | // Limit the decrease in Q from previous frame. |
496 | 0 | if (rc->q_1_frame - q > max_delta_down) q = rc->q_1_frame - max_delta_down; |
497 | | // Limit the increase in Q from previous frame. |
498 | 0 | else if (q - rc->q_1_frame > max_delta_up) |
499 | 0 | q = rc->q_1_frame + max_delta_up; |
500 | 0 | } |
501 | | // For single spatial layer: if resolution has increased push q closer |
502 | | // to the active_worst to avoid excess overshoot. |
503 | 0 | if (cpi->svc.number_spatial_layers <= 1 && cm->prev_frame && |
504 | 0 | (cm->width * cm->height > |
505 | 0 | 1.5 * cm->prev_frame->width * cm->prev_frame->height)) |
506 | 0 | q = (q + active_worst_quality) >> 1; |
507 | 0 | return AOMMAX(AOMMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality); |
508 | 0 | } |
509 | | |
510 | | static const RATE_FACTOR_LEVEL rate_factor_levels[FRAME_UPDATE_TYPES] = { |
511 | | KF_STD, // KF_UPDATE |
512 | | INTER_NORMAL, // LF_UPDATE |
513 | | GF_ARF_STD, // GF_UPDATE |
514 | | GF_ARF_STD, // ARF_UPDATE |
515 | | INTER_NORMAL, // OVERLAY_UPDATE |
516 | | INTER_NORMAL, // INTNL_OVERLAY_UPDATE |
517 | | GF_ARF_LOW, // INTNL_ARF_UPDATE |
518 | | }; |
519 | | |
520 | | static RATE_FACTOR_LEVEL get_rate_factor_level(const GF_GROUP *const gf_group, |
521 | 0 | int gf_frame_index) { |
522 | 0 | const FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_frame_index]; |
523 | 0 | assert(update_type < FRAME_UPDATE_TYPES); |
524 | 0 | return rate_factor_levels[update_type]; |
525 | 0 | } |
526 | | |
527 | | /*!\brief Gets a rate vs Q correction factor |
528 | | * |
529 | | * This function returns the current value of a correction factor used to |
530 | | * dynamilcally adjust the relationship between Q and the expected number |
531 | | * of bits for the frame. |
532 | | * |
533 | | * \ingroup rate_control |
534 | | * \param[in] cpi Top level encoder instance structure |
535 | | * \param[in] width Frame width |
536 | | * \param[in] height Frame height |
537 | | * |
538 | | * \return Returns a correction factor for the current frame |
539 | | */ |
540 | | static double get_rate_correction_factor(const AV1_COMP *cpi, int width, |
541 | 1.26k | int height) { |
542 | 1.26k | const RATE_CONTROL *const rc = &cpi->rc; |
543 | 1.26k | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
544 | 1.26k | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
545 | 1.26k | double rcf; |
546 | 1.26k | double rate_correction_factors_kfstd; |
547 | 1.26k | double rate_correction_factors_gfarfstd; |
548 | 1.26k | double rate_correction_factors_internormal; |
549 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
550 | | rate_correction_factors_kfstd = |
551 | | (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) |
552 | | ? rc->frame_level_rate_correction_factors[KF_STD] |
553 | | : p_rc->rate_correction_factors[KF_STD]; |
554 | | rate_correction_factors_gfarfstd = |
555 | | (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) |
556 | | ? rc->frame_level_rate_correction_factors[GF_ARF_STD] |
557 | | : p_rc->rate_correction_factors[GF_ARF_STD]; |
558 | | rate_correction_factors_internormal = |
559 | | (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) |
560 | | ? rc->frame_level_rate_correction_factors[INTER_NORMAL] |
561 | | : p_rc->rate_correction_factors[INTER_NORMAL]; |
562 | | #else |
563 | 1.26k | rate_correction_factors_kfstd = p_rc->rate_correction_factors[KF_STD]; |
564 | 1.26k | rate_correction_factors_gfarfstd = p_rc->rate_correction_factors[GF_ARF_STD]; |
565 | 1.26k | rate_correction_factors_internormal = |
566 | 1.26k | p_rc->rate_correction_factors[INTER_NORMAL]; |
567 | 1.26k | #endif |
568 | | |
569 | 1.26k | if (cpi->common.current_frame.frame_type == KEY_FRAME) { |
570 | 1.26k | rcf = rate_correction_factors_kfstd; |
571 | 1.26k | } else if (is_stat_consumption_stage(cpi)) { |
572 | 0 | const RATE_FACTOR_LEVEL rf_lvl = |
573 | 0 | get_rate_factor_level(&cpi->ppi->gf_group, cpi->gf_frame_index); |
574 | 0 | double rate_correction_factors_rflvl; |
575 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
576 | | rate_correction_factors_rflvl = |
577 | | (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) |
578 | | ? rc->frame_level_rate_correction_factors[rf_lvl] |
579 | | : p_rc->rate_correction_factors[rf_lvl]; |
580 | | #else |
581 | 0 | rate_correction_factors_rflvl = p_rc->rate_correction_factors[rf_lvl]; |
582 | 0 | #endif |
583 | 0 | rcf = rate_correction_factors_rflvl; |
584 | 0 | } else { |
585 | 0 | if ((refresh_frame->alt_ref_frame || refresh_frame->golden_frame) && |
586 | 0 | !rc->is_src_frame_alt_ref && !cpi->ppi->use_svc && |
587 | 0 | (cpi->oxcf.rc_cfg.mode != AOM_CBR || |
588 | 0 | cpi->oxcf.rc_cfg.gf_cbr_boost_pct > 20)) |
589 | 0 | rcf = rate_correction_factors_gfarfstd; |
590 | 0 | else |
591 | 0 | rcf = rate_correction_factors_internormal; |
592 | 0 | } |
593 | 1.26k | rcf *= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height); |
594 | 1.26k | return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
595 | 1.26k | } |
596 | | |
597 | | /*!\brief Sets a rate vs Q correction factor |
598 | | * |
599 | | * This function updates the current value of a correction factor used to |
600 | | * dynamilcally adjust the relationship between Q and the expected number |
601 | | * of bits for the frame. |
602 | | * |
603 | | * \ingroup rate_control |
604 | | * \param[in] cpi Top level encoder instance structure |
605 | | * \param[in] factor New correction factor |
606 | | * \param[in] width Frame width |
607 | | * \param[in] height Frame height |
608 | | * |
609 | | * \return None but updates the rate correction factor for the |
610 | | * current frame type in cpi->rc. |
611 | | */ |
612 | | static void set_rate_correction_factor(AV1_COMP *cpi, |
613 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
614 | | int is_encode_stage, |
615 | | #endif // CONFIG_FRAME_PARALLEL_ENCODE |
616 | 1.26k | double factor, int width, int height) { |
617 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
618 | 1.26k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
619 | 1.26k | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
620 | 1.26k | int update_default_rcf = 1; |
621 | | // Normalize RCF to account for the size-dependent scaling factor. |
622 | 1.26k | factor /= resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height); |
623 | | |
624 | 1.26k | factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
625 | | |
626 | 1.26k | if (cpi->common.current_frame.frame_type == KEY_FRAME) { |
627 | 1.26k | p_rc->rate_correction_factors[KF_STD] = factor; |
628 | 1.26k | } else if (is_stat_consumption_stage(cpi)) { |
629 | 0 | const RATE_FACTOR_LEVEL rf_lvl = |
630 | 0 | get_rate_factor_level(&cpi->ppi->gf_group, cpi->gf_frame_index); |
631 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
632 | | if (is_encode_stage && |
633 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) { |
634 | | rc->frame_level_rate_correction_factors[rf_lvl] = factor; |
635 | | update_default_rcf = 0; |
636 | | } |
637 | | #endif |
638 | 0 | if (update_default_rcf) p_rc->rate_correction_factors[rf_lvl] = factor; |
639 | 0 | } else { |
640 | 0 | if ((refresh_frame->alt_ref_frame || refresh_frame->golden_frame) && |
641 | 0 | !rc->is_src_frame_alt_ref && !cpi->ppi->use_svc && |
642 | 0 | (cpi->oxcf.rc_cfg.mode != AOM_CBR || |
643 | 0 | cpi->oxcf.rc_cfg.gf_cbr_boost_pct > 20)) { |
644 | 0 | p_rc->rate_correction_factors[GF_ARF_STD] = factor; |
645 | 0 | } else { |
646 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
647 | | if (is_encode_stage && |
648 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) { |
649 | | rc->frame_level_rate_correction_factors[INTER_NORMAL] = factor; |
650 | | update_default_rcf = 0; |
651 | | } |
652 | | #endif |
653 | 0 | if (update_default_rcf) |
654 | 0 | p_rc->rate_correction_factors[INTER_NORMAL] = factor; |
655 | 0 | } |
656 | 0 | } |
657 | 1.26k | } |
658 | | |
659 | | void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, |
660 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
661 | | int is_encode_stage, |
662 | | #endif |
663 | 1.26k | int width, int height) { |
664 | 1.26k | const AV1_COMMON *const cm = &cpi->common; |
665 | 1.26k | int correction_factor = 100; |
666 | 1.26k | double rate_correction_factor = |
667 | 1.26k | get_rate_correction_factor(cpi, width, height); |
668 | 1.26k | double adjustment_limit; |
669 | 1.26k | const int MBs = av1_get_MBs(width, height); |
670 | | |
671 | 1.26k | int projected_size_based_on_q = 0; |
672 | | |
673 | | // Do not update the rate factors for arf overlay frames. |
674 | 1.26k | if (cpi->rc.is_src_frame_alt_ref) return; |
675 | | |
676 | | // Clear down mmx registers to allow floating point in what follows |
677 | | |
678 | | // Work out how big we would have expected the frame to be at this Q given |
679 | | // the current correction factor. |
680 | | // Stay in double to avoid int overflow when values are large |
681 | 1.26k | if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) { |
682 | 0 | projected_size_based_on_q = |
683 | 0 | av1_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor); |
684 | 1.26k | } else { |
685 | 1.26k | projected_size_based_on_q = av1_estimate_bits_at_q( |
686 | 1.26k | cm->current_frame.frame_type, cm->quant_params.base_qindex, MBs, |
687 | 1.26k | rate_correction_factor, cm->seq_params->bit_depth, |
688 | 1.26k | cpi->is_screen_content_type); |
689 | 1.26k | } |
690 | | // Work out a size correction factor. |
691 | 1.26k | if (projected_size_based_on_q > FRAME_OVERHEAD_BITS) |
692 | 1.13k | correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) / |
693 | 1.13k | projected_size_based_on_q); |
694 | | |
695 | | // More heavily damped adjustment used if we have been oscillating either side |
696 | | // of target. |
697 | 1.26k | if (correction_factor > 0) { |
698 | 917 | adjustment_limit = |
699 | 917 | 0.25 + 0.5 * AOMMIN(1, fabs(log10(0.01 * correction_factor))); |
700 | 917 | } else { |
701 | 345 | adjustment_limit = 0.75; |
702 | 345 | } |
703 | | |
704 | 1.26k | cpi->rc.q_2_frame = cpi->rc.q_1_frame; |
705 | 1.26k | cpi->rc.q_1_frame = cm->quant_params.base_qindex; |
706 | 1.26k | cpi->rc.rc_2_frame = cpi->rc.rc_1_frame; |
707 | 1.26k | if (correction_factor > 110) |
708 | 1 | cpi->rc.rc_1_frame = -1; |
709 | 1.26k | else if (correction_factor < 90) |
710 | 1.10k | cpi->rc.rc_1_frame = 1; |
711 | 159 | else |
712 | 159 | cpi->rc.rc_1_frame = 0; |
713 | | |
714 | 1.26k | if (correction_factor > 102) { |
715 | | // We are not already at the worst allowable quality |
716 | 7 | correction_factor = |
717 | 7 | (int)(100 + ((correction_factor - 100) * adjustment_limit)); |
718 | 7 | rate_correction_factor = (rate_correction_factor * correction_factor) / 100; |
719 | | // Keep rate_correction_factor within limits |
720 | 7 | if (rate_correction_factor > MAX_BPB_FACTOR) |
721 | 0 | rate_correction_factor = MAX_BPB_FACTOR; |
722 | 1.25k | } else if (correction_factor < 99) { |
723 | | // We are not already at the best allowable quality |
724 | 1.12k | correction_factor = |
725 | 1.12k | (int)(100 - ((100 - correction_factor) * adjustment_limit)); |
726 | 1.12k | rate_correction_factor = (rate_correction_factor * correction_factor) / 100; |
727 | | |
728 | | // Keep rate_correction_factor within limits |
729 | 1.12k | if (rate_correction_factor < MIN_BPB_FACTOR) |
730 | 0 | rate_correction_factor = MIN_BPB_FACTOR; |
731 | 1.12k | } |
732 | | |
733 | 1.26k | set_rate_correction_factor(cpi, |
734 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
735 | | is_encode_stage, |
736 | | #endif |
737 | 1.26k | rate_correction_factor, width, height); |
738 | 1.26k | } |
739 | | |
740 | | // Calculate rate for the given 'q'. |
741 | | static int get_bits_per_mb(const AV1_COMP *cpi, int use_cyclic_refresh, |
742 | 0 | double correction_factor, int q) { |
743 | 0 | const AV1_COMMON *const cm = &cpi->common; |
744 | 0 | return use_cyclic_refresh |
745 | 0 | ? av1_cyclic_refresh_rc_bits_per_mb(cpi, q, correction_factor) |
746 | 0 | : av1_rc_bits_per_mb(cm->current_frame.frame_type, q, |
747 | 0 | correction_factor, cm->seq_params->bit_depth, |
748 | 0 | cpi->is_screen_content_type); |
749 | 0 | } |
750 | | |
751 | | /*!\brief Searches for a Q index value predicted to give an average macro |
752 | | * block rate closest to the target value. |
753 | | * |
754 | | * Similar to find_qindex_by_rate() function, but returns a q index with a |
755 | | * rate just above or below the desired rate, depending on which of the two |
756 | | * rates is closer to the desired rate. |
757 | | * Also, respects the selected aq_mode when computing the rate. |
758 | | * |
759 | | * \ingroup rate_control |
760 | | * \param[in] desired_bits_per_mb Target bits per mb |
761 | | * \param[in] cpi Top level encoder instance structure |
762 | | * \param[in] correction_factor Current Q to rate correction factor |
763 | | * \param[in] best_qindex Min allowed Q value. |
764 | | * \param[in] worst_qindex Max allowed Q value. |
765 | | * |
766 | | * \return Returns a correction factor for the current frame |
767 | | */ |
768 | | static int find_closest_qindex_by_rate(int desired_bits_per_mb, |
769 | | const AV1_COMP *cpi, |
770 | | double correction_factor, |
771 | 0 | int best_qindex, int worst_qindex) { |
772 | 0 | const int use_cyclic_refresh = cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && |
773 | 0 | cpi->cyclic_refresh->apply_cyclic_refresh; |
774 | | |
775 | | // Find 'qindex' based on 'desired_bits_per_mb'. |
776 | 0 | assert(best_qindex <= worst_qindex); |
777 | 0 | int low = best_qindex; |
778 | 0 | int high = worst_qindex; |
779 | 0 | while (low < high) { |
780 | 0 | const int mid = (low + high) >> 1; |
781 | 0 | const int mid_bits_per_mb = |
782 | 0 | get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, mid); |
783 | 0 | if (mid_bits_per_mb > desired_bits_per_mb) { |
784 | 0 | low = mid + 1; |
785 | 0 | } else { |
786 | 0 | high = mid; |
787 | 0 | } |
788 | 0 | } |
789 | 0 | assert(low == high); |
790 | | |
791 | | // Calculate rate difference of this q index from the desired rate. |
792 | 0 | const int curr_q = low; |
793 | 0 | const int curr_bits_per_mb = |
794 | 0 | get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, curr_q); |
795 | 0 | const int curr_bit_diff = (curr_bits_per_mb <= desired_bits_per_mb) |
796 | 0 | ? desired_bits_per_mb - curr_bits_per_mb |
797 | 0 | : INT_MAX; |
798 | 0 | assert((curr_bit_diff != INT_MAX && curr_bit_diff >= 0) || |
799 | 0 | curr_q == worst_qindex); |
800 | | |
801 | | // Calculate rate difference for previous q index too. |
802 | 0 | const int prev_q = curr_q - 1; |
803 | 0 | int prev_bit_diff; |
804 | 0 | if (curr_bit_diff == INT_MAX || curr_q == best_qindex) { |
805 | 0 | prev_bit_diff = INT_MAX; |
806 | 0 | } else { |
807 | 0 | const int prev_bits_per_mb = |
808 | 0 | get_bits_per_mb(cpi, use_cyclic_refresh, correction_factor, prev_q); |
809 | 0 | assert(prev_bits_per_mb > desired_bits_per_mb); |
810 | 0 | prev_bit_diff = prev_bits_per_mb - desired_bits_per_mb; |
811 | 0 | } |
812 | | |
813 | | // Pick one of the two q indices, depending on which one has rate closer to |
814 | | // the desired rate. |
815 | 0 | return (curr_bit_diff <= prev_bit_diff) ? curr_q : prev_q; |
816 | 0 | } |
817 | | |
818 | | int av1_rc_regulate_q(const AV1_COMP *cpi, int target_bits_per_frame, |
819 | | int active_best_quality, int active_worst_quality, |
820 | 0 | int width, int height) { |
821 | 0 | const int MBs = av1_get_MBs(width, height); |
822 | 0 | const double correction_factor = |
823 | 0 | get_rate_correction_factor(cpi, width, height); |
824 | 0 | const int target_bits_per_mb = |
825 | 0 | (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / MBs); |
826 | |
|
827 | 0 | int q = |
828 | 0 | find_closest_qindex_by_rate(target_bits_per_mb, cpi, correction_factor, |
829 | 0 | active_best_quality, active_worst_quality); |
830 | 0 | if (cpi->oxcf.rc_cfg.mode == AOM_CBR && has_no_stats_stage(cpi)) |
831 | 0 | return adjust_q_cbr(cpi, q, active_worst_quality); |
832 | | |
833 | 0 | return q; |
834 | 0 | } |
835 | | |
836 | | static int get_active_quality(int q, int gfu_boost, int low, int high, |
837 | 0 | int *low_motion_minq, int *high_motion_minq) { |
838 | 0 | if (gfu_boost > high) { |
839 | 0 | return low_motion_minq[q]; |
840 | 0 | } else if (gfu_boost < low) { |
841 | 0 | return high_motion_minq[q]; |
842 | 0 | } else { |
843 | 0 | const int gap = high - low; |
844 | 0 | const int offset = high - gfu_boost; |
845 | 0 | const int qdiff = high_motion_minq[q] - low_motion_minq[q]; |
846 | 0 | const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; |
847 | 0 | return low_motion_minq[q] + adjustment; |
848 | 0 | } |
849 | 0 | } |
850 | | |
851 | | static int get_kf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q, |
852 | 0 | aom_bit_depth_t bit_depth) { |
853 | 0 | int *kf_low_motion_minq; |
854 | 0 | int *kf_high_motion_minq; |
855 | 0 | ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq); |
856 | 0 | ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq); |
857 | 0 | return get_active_quality(q, p_rc->kf_boost, kf_low, kf_high, |
858 | 0 | kf_low_motion_minq, kf_high_motion_minq); |
859 | 0 | } |
860 | | |
861 | | static int get_gf_active_quality_no_rc(int gfu_boost, int q, |
862 | 0 | aom_bit_depth_t bit_depth) { |
863 | 0 | int *arfgf_low_motion_minq; |
864 | 0 | int *arfgf_high_motion_minq; |
865 | 0 | ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq); |
866 | 0 | ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq); |
867 | 0 | return get_active_quality(q, gfu_boost, gf_low, gf_high, |
868 | 0 | arfgf_low_motion_minq, arfgf_high_motion_minq); |
869 | 0 | } |
870 | | |
871 | | static int get_gf_active_quality(const PRIMARY_RATE_CONTROL *const p_rc, int q, |
872 | 0 | aom_bit_depth_t bit_depth) { |
873 | 0 | return get_gf_active_quality_no_rc(p_rc->gfu_boost, q, bit_depth); |
874 | 0 | } |
875 | | |
876 | 0 | static int get_gf_high_motion_quality(int q, aom_bit_depth_t bit_depth) { |
877 | 0 | int *arfgf_high_motion_minq; |
878 | 0 | ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq); |
879 | 0 | return arfgf_high_motion_minq[q]; |
880 | 0 | } |
881 | | |
882 | 0 | static int calc_active_worst_quality_no_stats_vbr(const AV1_COMP *cpi) { |
883 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
884 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
885 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
886 | 0 | const unsigned int curr_frame = cpi->common.current_frame.frame_number; |
887 | 0 | int active_worst_quality; |
888 | 0 | int last_q_key_frame; |
889 | 0 | int last_q_inter_frame; |
890 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
891 | | const int simulate_parallel_frame = |
892 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
893 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
894 | | last_q_key_frame = simulate_parallel_frame ? p_rc->temp_last_q[KEY_FRAME] |
895 | | : p_rc->last_q[KEY_FRAME]; |
896 | | last_q_inter_frame = simulate_parallel_frame ? p_rc->temp_last_q[INTER_FRAME] |
897 | | : p_rc->last_q[INTER_FRAME]; |
898 | | #else |
899 | 0 | last_q_key_frame = p_rc->last_q[KEY_FRAME]; |
900 | 0 | last_q_inter_frame = p_rc->last_q[INTER_FRAME]; |
901 | 0 | #endif |
902 | |
|
903 | 0 | if (cpi->common.current_frame.frame_type == KEY_FRAME) { |
904 | 0 | active_worst_quality = |
905 | 0 | curr_frame == 0 ? rc->worst_quality : last_q_key_frame * 2; |
906 | 0 | } else { |
907 | 0 | if (!rc->is_src_frame_alt_ref && |
908 | 0 | (refresh_frame->golden_frame || refresh_frame->bwd_ref_frame || |
909 | 0 | refresh_frame->alt_ref_frame)) { |
910 | 0 | active_worst_quality = |
911 | 0 | curr_frame == 1 ? last_q_key_frame * 5 / 4 : last_q_inter_frame; |
912 | 0 | } else { |
913 | 0 | active_worst_quality = |
914 | 0 | curr_frame == 1 ? last_q_key_frame * 2 : last_q_inter_frame * 2; |
915 | 0 | } |
916 | 0 | } |
917 | 0 | return AOMMIN(active_worst_quality, rc->worst_quality); |
918 | 0 | } |
919 | | |
920 | | // Adjust active_worst_quality level based on buffer level. |
921 | 0 | static int calc_active_worst_quality_no_stats_cbr(const AV1_COMP *cpi) { |
922 | | // Adjust active_worst_quality: If buffer is above the optimal/target level, |
923 | | // bring active_worst_quality down depending on fullness of buffer. |
924 | | // If buffer is below the optimal level, let the active_worst_quality go from |
925 | | // ambient Q (at buffer = optimal level) to worst_quality level |
926 | | // (at buffer = critical level). |
927 | 0 | const AV1_COMMON *const cm = &cpi->common; |
928 | 0 | const RATE_CONTROL *rc = &cpi->rc; |
929 | 0 | const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc; |
930 | 0 | const SVC *const svc = &cpi->svc; |
931 | 0 | unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers; |
932 | | // Buffer level below which we push active_worst to worst_quality. |
933 | 0 | int64_t critical_level = p_rc->optimal_buffer_level >> 3; |
934 | 0 | int64_t buff_lvl_step = 0; |
935 | 0 | int adjustment = 0; |
936 | 0 | int active_worst_quality; |
937 | 0 | int ambient_qp; |
938 | 0 | if (cm->current_frame.frame_type == KEY_FRAME) return rc->worst_quality; |
939 | | // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME] |
940 | | // for the first few frames following key frame. These are both initialized |
941 | | // to worst_quality and updated with (3/4, 1/4) average in postencode_update. |
942 | | // So for first few frames following key, the qp of that key frame is weighted |
943 | | // into the active_worst_quality setting. For SVC the key frame should |
944 | | // correspond to layer (0, 0), so use that for layer context. |
945 | 0 | int avg_qindex_key = p_rc->avg_frame_qindex[KEY_FRAME]; |
946 | 0 | if (svc->number_temporal_layers > 1) { |
947 | 0 | int layer = LAYER_IDS_TO_IDX(0, 0, svc->number_temporal_layers); |
948 | 0 | const LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
949 | 0 | const PRIMARY_RATE_CONTROL *const lp_rc = &lc->p_rc; |
950 | 0 | avg_qindex_key = lp_rc->avg_frame_qindex[KEY_FRAME]; |
951 | 0 | if (svc->temporal_layer_id == 0) |
952 | 0 | avg_qindex_key = |
953 | 0 | AOMMIN(lp_rc->avg_frame_qindex[KEY_FRAME], lp_rc->last_q[KEY_FRAME]); |
954 | 0 | } |
955 | 0 | ambient_qp = (cm->current_frame.frame_number < num_frames_weight_key) |
956 | 0 | ? AOMMIN(p_rc->avg_frame_qindex[INTER_FRAME], avg_qindex_key) |
957 | 0 | : p_rc->avg_frame_qindex[INTER_FRAME]; |
958 | 0 | active_worst_quality = AOMMIN(rc->worst_quality, ambient_qp * 5 / 4); |
959 | 0 | if (p_rc->buffer_level > p_rc->optimal_buffer_level) { |
960 | | // Adjust down. |
961 | | // Maximum limit for down adjustment, ~30%. |
962 | 0 | int max_adjustment_down = active_worst_quality / 3; |
963 | 0 | if (max_adjustment_down) { |
964 | 0 | buff_lvl_step = |
965 | 0 | ((p_rc->maximum_buffer_size - p_rc->optimal_buffer_level) / |
966 | 0 | max_adjustment_down); |
967 | 0 | if (buff_lvl_step) |
968 | 0 | adjustment = (int)((p_rc->buffer_level - p_rc->optimal_buffer_level) / |
969 | 0 | buff_lvl_step); |
970 | 0 | active_worst_quality -= adjustment; |
971 | 0 | } |
972 | 0 | } else if (p_rc->buffer_level > critical_level) { |
973 | | // Adjust up from ambient Q. |
974 | 0 | if (critical_level) { |
975 | 0 | buff_lvl_step = (p_rc->optimal_buffer_level - critical_level); |
976 | 0 | if (buff_lvl_step) { |
977 | 0 | adjustment = (int)((rc->worst_quality - ambient_qp) * |
978 | 0 | (p_rc->optimal_buffer_level - p_rc->buffer_level) / |
979 | 0 | buff_lvl_step); |
980 | 0 | } |
981 | 0 | active_worst_quality = ambient_qp + adjustment; |
982 | 0 | } |
983 | 0 | } else { |
984 | | // Set to worst_quality if buffer is below critical level. |
985 | 0 | active_worst_quality = rc->worst_quality; |
986 | 0 | } |
987 | 0 | return active_worst_quality; |
988 | 0 | } |
989 | | |
990 | | // Calculate the active_best_quality level. |
991 | | static int calc_active_best_quality_no_stats_cbr(const AV1_COMP *cpi, |
992 | | int active_worst_quality, |
993 | 0 | int width, int height) { |
994 | 0 | const AV1_COMMON *const cm = &cpi->common; |
995 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
996 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
997 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
998 | 0 | const CurrentFrame *const current_frame = &cm->current_frame; |
999 | 0 | int *rtc_minq; |
1000 | 0 | const int bit_depth = cm->seq_params->bit_depth; |
1001 | 0 | int active_best_quality = rc->best_quality; |
1002 | 0 | ASSIGN_MINQ_TABLE(bit_depth, rtc_minq); |
1003 | | |
1004 | 0 | if (frame_is_intra_only(cm)) { |
1005 | | // Handle the special case for key frames forced when we have reached |
1006 | | // the maximum key frame interval. Here force the Q to a range |
1007 | | // based on the ambient Q to reduce the risk of popping. |
1008 | 0 | if (p_rc->this_key_frame_forced) { |
1009 | 0 | int qindex = p_rc->last_boosted_qindex; |
1010 | 0 | double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth); |
1011 | 0 | int delta_qindex = av1_compute_qdelta(rc, last_boosted_q, |
1012 | 0 | (last_boosted_q * 0.75), bit_depth); |
1013 | 0 | active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality); |
1014 | 0 | } else if (current_frame->frame_number > 0) { |
1015 | | // not first frame of one pass and kf_boost is set |
1016 | 0 | double q_adj_factor = 1.0; |
1017 | 0 | double q_val; |
1018 | 0 | active_best_quality = get_kf_active_quality( |
1019 | 0 | p_rc, p_rc->avg_frame_qindex[KEY_FRAME], bit_depth); |
1020 | | // Allow somewhat lower kf minq with small image formats. |
1021 | 0 | if ((width * height) <= (352 * 288)) { |
1022 | 0 | q_adj_factor -= 0.25; |
1023 | 0 | } |
1024 | | // Convert the adjustment factor to a qindex delta |
1025 | | // on active_best_quality. |
1026 | 0 | q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth); |
1027 | 0 | active_best_quality += |
1028 | 0 | av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth); |
1029 | 0 | } |
1030 | 0 | } else if (!rc->is_src_frame_alt_ref && !cpi->ppi->use_svc && |
1031 | 0 | cpi->oxcf.rc_cfg.gf_cbr_boost_pct && |
1032 | 0 | (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) { |
1033 | | // Use the lower of active_worst_quality and recent |
1034 | | // average Q as basis for GF/ARF best Q limit unless last frame was |
1035 | | // a key frame. |
1036 | 0 | int q = active_worst_quality; |
1037 | 0 | if (rc->frames_since_key > 1 && |
1038 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
1039 | 0 | q = p_rc->avg_frame_qindex[INTER_FRAME]; |
1040 | 0 | } |
1041 | 0 | active_best_quality = get_gf_active_quality(p_rc, q, bit_depth); |
1042 | 0 | } else { |
1043 | | // Use the lower of active_worst_quality and recent/average Q. |
1044 | 0 | FRAME_TYPE frame_type = |
1045 | 0 | (current_frame->frame_number > 1) ? INTER_FRAME : KEY_FRAME; |
1046 | 0 | if (p_rc->avg_frame_qindex[frame_type] < active_worst_quality) |
1047 | 0 | active_best_quality = rtc_minq[p_rc->avg_frame_qindex[frame_type]]; |
1048 | 0 | else |
1049 | 0 | active_best_quality = rtc_minq[active_worst_quality]; |
1050 | 0 | } |
1051 | 0 | return active_best_quality; |
1052 | 0 | } |
1053 | | |
1054 | | /*!\brief Picks q and q bounds given CBR rate control parameters in \c cpi->rc. |
1055 | | * |
1056 | | * Handles the special case when using: |
1057 | | * - Constant bit-rate mode: \c cpi->oxcf.rc_cfg.mode == \ref AOM_CBR, and |
1058 | | * - 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are |
1059 | | * NOT available. |
1060 | | * |
1061 | | * \ingroup rate_control |
1062 | | * \param[in] cpi Top level encoder structure |
1063 | | * \param[in] width Coded frame width |
1064 | | * \param[in] height Coded frame height |
1065 | | * \param[out] bottom_index Bottom bound for q index (best quality) |
1066 | | * \param[out] top_index Top bound for q index (worst quality) |
1067 | | * \return Returns selected q index to be used for encoding this frame. |
1068 | | */ |
1069 | | static int rc_pick_q_and_bounds_no_stats_cbr(const AV1_COMP *cpi, int width, |
1070 | | int height, int *bottom_index, |
1071 | 0 | int *top_index) { |
1072 | 0 | const AV1_COMMON *const cm = &cpi->common; |
1073 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1074 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1075 | 0 | const CurrentFrame *const current_frame = &cm->current_frame; |
1076 | 0 | int q; |
1077 | 0 | const int bit_depth = cm->seq_params->bit_depth; |
1078 | 0 | int active_worst_quality = calc_active_worst_quality_no_stats_cbr(cpi); |
1079 | 0 | int active_best_quality = calc_active_best_quality_no_stats_cbr( |
1080 | 0 | cpi, active_worst_quality, width, height); |
1081 | 0 | assert(has_no_stats_stage(cpi)); |
1082 | 0 | assert(cpi->oxcf.rc_cfg.mode == AOM_CBR); |
1083 | | |
1084 | | // Clip the active best and worst quality values to limits |
1085 | 0 | active_best_quality = |
1086 | 0 | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
1087 | 0 | active_worst_quality = |
1088 | 0 | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
1089 | |
|
1090 | 0 | *top_index = active_worst_quality; |
1091 | 0 | *bottom_index = active_best_quality; |
1092 | | |
1093 | | // Limit Q range for the adaptive loop. |
1094 | 0 | if (current_frame->frame_type == KEY_FRAME && !p_rc->this_key_frame_forced && |
1095 | 0 | current_frame->frame_number != 0) { |
1096 | 0 | int qdelta = 0; |
1097 | 0 | qdelta = av1_compute_qdelta_by_rate(&cpi->rc, current_frame->frame_type, |
1098 | 0 | active_worst_quality, 2.0, |
1099 | 0 | cpi->is_screen_content_type, bit_depth); |
1100 | 0 | *top_index = active_worst_quality + qdelta; |
1101 | 0 | *top_index = AOMMAX(*top_index, *bottom_index); |
1102 | 0 | } |
1103 | | |
1104 | | // Special case code to try and match quality with forced key frames |
1105 | 0 | if (current_frame->frame_type == KEY_FRAME && p_rc->this_key_frame_forced) { |
1106 | 0 | q = p_rc->last_boosted_qindex; |
1107 | 0 | } else { |
1108 | 0 | q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
1109 | 0 | active_worst_quality, width, height); |
1110 | 0 | if (q > *top_index) { |
1111 | | // Special case when we are targeting the max allowed rate |
1112 | 0 | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
1113 | 0 | *top_index = q; |
1114 | 0 | else |
1115 | 0 | q = *top_index; |
1116 | 0 | } |
1117 | 0 | } |
1118 | |
|
1119 | 0 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
1120 | 0 | assert(*bottom_index <= rc->worst_quality && |
1121 | 0 | *bottom_index >= rc->best_quality); |
1122 | 0 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
1123 | 0 | return q; |
1124 | 0 | } |
1125 | | |
1126 | 0 | static int gf_group_pyramid_level(const GF_GROUP *gf_group, int gf_index) { |
1127 | 0 | return gf_group->layer_depth[gf_index]; |
1128 | 0 | } |
1129 | | |
1130 | | static int get_active_cq_level(const RATE_CONTROL *rc, |
1131 | | const PRIMARY_RATE_CONTROL *p_rc, |
1132 | | const AV1EncoderConfig *const oxcf, |
1133 | | int intra_only, aom_superres_mode superres_mode, |
1134 | 2.52k | int superres_denom) { |
1135 | 2.52k | const RateControlCfg *const rc_cfg = &oxcf->rc_cfg; |
1136 | 2.52k | static const double cq_adjust_threshold = 0.1; |
1137 | 2.52k | int active_cq_level = rc_cfg->cq_level; |
1138 | 2.52k | if (rc_cfg->mode == AOM_CQ || rc_cfg->mode == AOM_Q) { |
1139 | | // printf("Superres %d %d %d = %d\n", superres_denom, intra_only, |
1140 | | // rc->frames_to_key, !(intra_only && rc->frames_to_key <= 1)); |
1141 | 2.52k | if ((superres_mode == AOM_SUPERRES_QTHRESH || |
1142 | 2.52k | superres_mode == AOM_SUPERRES_AUTO) && |
1143 | 2.52k | superres_denom != SCALE_NUMERATOR) { |
1144 | 0 | int mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME_SOLO; |
1145 | 0 | if (intra_only && rc->frames_to_key <= 1) { |
1146 | 0 | mult = 0; |
1147 | 0 | } else if (intra_only) { |
1148 | 0 | mult = SUPERRES_QADJ_PER_DENOM_KEYFRAME; |
1149 | 0 | } else { |
1150 | 0 | mult = SUPERRES_QADJ_PER_DENOM_ARFFRAME; |
1151 | 0 | } |
1152 | 0 | active_cq_level = AOMMAX( |
1153 | 0 | active_cq_level - ((superres_denom - SCALE_NUMERATOR) * mult), 0); |
1154 | 0 | } |
1155 | 2.52k | } |
1156 | 2.52k | if (rc_cfg->mode == AOM_CQ && p_rc->total_target_bits > 0) { |
1157 | 0 | const double x = (double)p_rc->total_actual_bits / p_rc->total_target_bits; |
1158 | 0 | if (x < cq_adjust_threshold) { |
1159 | 0 | active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold); |
1160 | 0 | } |
1161 | 0 | } |
1162 | 2.52k | return active_cq_level; |
1163 | 2.52k | } |
1164 | | |
1165 | | /*!\brief Picks q and q bounds given non-CBR rate control params in \c cpi->rc. |
1166 | | * |
1167 | | * Handles the special case when using: |
1168 | | * - Any rate control other than constant bit-rate mode: |
1169 | | * \c cpi->oxcf.rc_cfg.mode != \ref AOM_CBR, and |
1170 | | * - 1-pass encoding without LAP (look-ahead processing), so 1st pass stats are |
1171 | | * NOT available. |
1172 | | * |
1173 | | * \ingroup rate_control |
1174 | | * \param[in] cpi Top level encoder structure |
1175 | | * \param[in] width Coded frame width |
1176 | | * \param[in] height Coded frame height |
1177 | | * \param[out] bottom_index Bottom bound for q index (best quality) |
1178 | | * \param[out] top_index Top bound for q index (worst quality) |
1179 | | * \return Returns selected q index to be used for encoding this frame. |
1180 | | */ |
1181 | | static int rc_pick_q_and_bounds_no_stats(const AV1_COMP *cpi, int width, |
1182 | | int height, int *bottom_index, |
1183 | 0 | int *top_index) { |
1184 | 0 | const AV1_COMMON *const cm = &cpi->common; |
1185 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1186 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1187 | 0 | const CurrentFrame *const current_frame = &cm->current_frame; |
1188 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
1189 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
1190 | 0 | const enum aom_rc_mode rc_mode = oxcf->rc_cfg.mode; |
1191 | |
|
1192 | 0 | assert(has_no_stats_stage(cpi)); |
1193 | 0 | assert(rc_mode == AOM_VBR || |
1194 | 0 | (!USE_UNRESTRICTED_Q_IN_CQ_MODE && rc_mode == AOM_CQ) || |
1195 | 0 | rc_mode == AOM_Q); |
1196 | |
|
1197 | 0 | const int cq_level = |
1198 | 0 | get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm), |
1199 | 0 | cpi->superres_mode, cm->superres_scale_denominator); |
1200 | 0 | const int bit_depth = cm->seq_params->bit_depth; |
1201 | |
|
1202 | 0 | int active_best_quality; |
1203 | 0 | int active_worst_quality = calc_active_worst_quality_no_stats_vbr(cpi); |
1204 | 0 | int q; |
1205 | 0 | int *inter_minq; |
1206 | 0 | ASSIGN_MINQ_TABLE(bit_depth, inter_minq); |
1207 | | |
1208 | 0 | if (frame_is_intra_only(cm)) { |
1209 | 0 | if (rc_mode == AOM_Q) { |
1210 | 0 | const int qindex = cq_level; |
1211 | 0 | const double q_val = av1_convert_qindex_to_q(qindex, bit_depth); |
1212 | 0 | const int delta_qindex = |
1213 | 0 | av1_compute_qdelta(rc, q_val, q_val * 0.25, bit_depth); |
1214 | 0 | active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality); |
1215 | 0 | } else if (p_rc->this_key_frame_forced) { |
1216 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1217 | | const int simulate_parallel_frame = |
1218 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
1219 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
1220 | | int qindex = simulate_parallel_frame ? p_rc->temp_last_boosted_qindex |
1221 | | : p_rc->last_boosted_qindex; |
1222 | | #else |
1223 | 0 | int qindex = p_rc->last_boosted_qindex; |
1224 | 0 | #endif |
1225 | 0 | const double last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth); |
1226 | 0 | const int delta_qindex = av1_compute_qdelta( |
1227 | 0 | rc, last_boosted_q, last_boosted_q * 0.75, bit_depth); |
1228 | 0 | active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality); |
1229 | 0 | } else { // not first frame of one pass and kf_boost is set |
1230 | 0 | double q_adj_factor = 1.0; |
1231 | |
|
1232 | 0 | active_best_quality = get_kf_active_quality( |
1233 | 0 | p_rc, p_rc->avg_frame_qindex[KEY_FRAME], bit_depth); |
1234 | | |
1235 | | // Allow somewhat lower kf minq with small image formats. |
1236 | 0 | if ((width * height) <= (352 * 288)) { |
1237 | 0 | q_adj_factor -= 0.25; |
1238 | 0 | } |
1239 | | |
1240 | | // Convert the adjustment factor to a qindex delta on active_best_quality. |
1241 | 0 | { |
1242 | 0 | const double q_val = |
1243 | 0 | av1_convert_qindex_to_q(active_best_quality, bit_depth); |
1244 | 0 | active_best_quality += |
1245 | 0 | av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth); |
1246 | 0 | } |
1247 | 0 | } |
1248 | 0 | } else if (!rc->is_src_frame_alt_ref && |
1249 | 0 | (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) { |
1250 | | // Use the lower of active_worst_quality and recent |
1251 | | // average Q as basis for GF/ARF best Q limit unless last frame was |
1252 | | // a key frame. |
1253 | 0 | q = (rc->frames_since_key > 1 && |
1254 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) |
1255 | 0 | ? p_rc->avg_frame_qindex[INTER_FRAME] |
1256 | 0 | : p_rc->avg_frame_qindex[KEY_FRAME]; |
1257 | | // For constrained quality dont allow Q less than the cq level |
1258 | 0 | if (rc_mode == AOM_CQ) { |
1259 | 0 | if (q < cq_level) q = cq_level; |
1260 | 0 | active_best_quality = get_gf_active_quality(p_rc, q, bit_depth); |
1261 | | // Constrained quality use slightly lower active best. |
1262 | 0 | active_best_quality = active_best_quality * 15 / 16; |
1263 | 0 | } else if (rc_mode == AOM_Q) { |
1264 | 0 | const int qindex = cq_level; |
1265 | 0 | const double q_val = av1_convert_qindex_to_q(qindex, bit_depth); |
1266 | 0 | const int delta_qindex = |
1267 | 0 | (refresh_frame->alt_ref_frame) |
1268 | 0 | ? av1_compute_qdelta(rc, q_val, q_val * 0.40, bit_depth) |
1269 | 0 | : av1_compute_qdelta(rc, q_val, q_val * 0.50, bit_depth); |
1270 | 0 | active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality); |
1271 | 0 | } else { |
1272 | 0 | active_best_quality = get_gf_active_quality(p_rc, q, bit_depth); |
1273 | 0 | } |
1274 | 0 | } else { |
1275 | 0 | if (rc_mode == AOM_Q) { |
1276 | 0 | const int qindex = cq_level; |
1277 | 0 | const double q_val = av1_convert_qindex_to_q(qindex, bit_depth); |
1278 | 0 | const double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0, |
1279 | 0 | 0.70, 1.0, 0.85, 1.0 }; |
1280 | 0 | const int delta_qindex = av1_compute_qdelta( |
1281 | 0 | rc, q_val, |
1282 | 0 | q_val * delta_rate[current_frame->frame_number % FIXED_GF_INTERVAL], |
1283 | 0 | bit_depth); |
1284 | 0 | active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality); |
1285 | 0 | } else { |
1286 | | // Use the lower of active_worst_quality and recent/average Q. |
1287 | 0 | active_best_quality = |
1288 | 0 | (current_frame->frame_number > 1) |
1289 | 0 | ? inter_minq[p_rc->avg_frame_qindex[INTER_FRAME]] |
1290 | 0 | : inter_minq[p_rc->avg_frame_qindex[KEY_FRAME]]; |
1291 | | // For the constrained quality mode we don't want |
1292 | | // q to fall below the cq level. |
1293 | 0 | if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) { |
1294 | 0 | active_best_quality = cq_level; |
1295 | 0 | } |
1296 | 0 | } |
1297 | 0 | } |
1298 | | |
1299 | | // Clip the active best and worst quality values to limits |
1300 | 0 | active_best_quality = |
1301 | 0 | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
1302 | 0 | active_worst_quality = |
1303 | 0 | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
1304 | |
|
1305 | 0 | *top_index = active_worst_quality; |
1306 | 0 | *bottom_index = active_best_quality; |
1307 | | |
1308 | | // Limit Q range for the adaptive loop. |
1309 | 0 | { |
1310 | 0 | int qdelta = 0; |
1311 | 0 | if (current_frame->frame_type == KEY_FRAME && |
1312 | 0 | !p_rc->this_key_frame_forced && current_frame->frame_number != 0) { |
1313 | 0 | qdelta = av1_compute_qdelta_by_rate( |
1314 | 0 | &cpi->rc, current_frame->frame_type, active_worst_quality, 2.0, |
1315 | 0 | cpi->is_screen_content_type, bit_depth); |
1316 | 0 | } else if (!rc->is_src_frame_alt_ref && |
1317 | 0 | (refresh_frame->golden_frame || refresh_frame->alt_ref_frame)) { |
1318 | 0 | qdelta = av1_compute_qdelta_by_rate( |
1319 | 0 | &cpi->rc, current_frame->frame_type, active_worst_quality, 1.75, |
1320 | 0 | cpi->is_screen_content_type, bit_depth); |
1321 | 0 | } |
1322 | 0 | *top_index = active_worst_quality + qdelta; |
1323 | 0 | *top_index = AOMMAX(*top_index, *bottom_index); |
1324 | 0 | } |
1325 | |
|
1326 | 0 | if (rc_mode == AOM_Q) { |
1327 | 0 | q = active_best_quality; |
1328 | | // Special case code to try and match quality with forced key frames |
1329 | 0 | } else if ((current_frame->frame_type == KEY_FRAME) && |
1330 | 0 | p_rc->this_key_frame_forced) { |
1331 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1332 | | const int simulate_parallel_frame = |
1333 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
1334 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
1335 | | q = simulate_parallel_frame ? p_rc->temp_last_boosted_qindex |
1336 | | : p_rc->last_boosted_qindex; |
1337 | | #else |
1338 | 0 | q = p_rc->last_boosted_qindex; |
1339 | 0 | #endif |
1340 | 0 | } else { |
1341 | 0 | q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
1342 | 0 | active_worst_quality, width, height); |
1343 | 0 | if (q > *top_index) { |
1344 | | // Special case when we are targeting the max allowed rate |
1345 | 0 | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
1346 | 0 | *top_index = q; |
1347 | 0 | else |
1348 | 0 | q = *top_index; |
1349 | 0 | } |
1350 | 0 | } |
1351 | |
|
1352 | 0 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
1353 | 0 | assert(*bottom_index <= rc->worst_quality && |
1354 | 0 | *bottom_index >= rc->best_quality); |
1355 | 0 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
1356 | 0 | return q; |
1357 | 0 | } |
1358 | | |
1359 | | static const double arf_layer_deltas[MAX_ARF_LAYERS + 1] = { 2.50, 2.00, 1.75, |
1360 | | 1.50, 1.25, 1.15, |
1361 | | 1.0 }; |
1362 | 0 | int av1_frame_type_qdelta(const AV1_COMP *cpi, int q) { |
1363 | 0 | const GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
1364 | 0 | const RATE_FACTOR_LEVEL rf_lvl = |
1365 | 0 | get_rate_factor_level(gf_group, cpi->gf_frame_index); |
1366 | 0 | const FRAME_TYPE frame_type = gf_group->frame_type[cpi->gf_frame_index]; |
1367 | 0 | const int arf_layer = AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], 6); |
1368 | 0 | const double rate_factor = |
1369 | 0 | (rf_lvl == INTER_NORMAL) ? 1.0 : arf_layer_deltas[arf_layer]; |
1370 | |
|
1371 | 0 | return av1_compute_qdelta_by_rate(&cpi->rc, frame_type, q, rate_factor, |
1372 | 0 | cpi->is_screen_content_type, |
1373 | 0 | cpi->common.seq_params->bit_depth); |
1374 | 0 | } |
1375 | | |
1376 | | // This unrestricted Q selection on CQ mode is useful when testing new features, |
1377 | | // but may lead to Q being out of range on current RC restrictions |
1378 | | #if USE_UNRESTRICTED_Q_IN_CQ_MODE |
1379 | | static int rc_pick_q_and_bounds_no_stats_cq(const AV1_COMP *cpi, int width, |
1380 | | int height, int *bottom_index, |
1381 | | int *top_index) { |
1382 | | const AV1_COMMON *const cm = &cpi->common; |
1383 | | const RATE_CONTROL *const rc = &cpi->rc; |
1384 | | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
1385 | | const int cq_level = |
1386 | | get_active_cq_level(rc, oxcf, frame_is_intra_only(cm), cpi->superres_mode, |
1387 | | cm->superres_scale_denominator); |
1388 | | const int bit_depth = cm->seq_params->bit_depth; |
1389 | | const int q = (int)av1_convert_qindex_to_q(cq_level, bit_depth); |
1390 | | (void)width; |
1391 | | (void)height; |
1392 | | assert(has_no_stats_stage(cpi)); |
1393 | | assert(cpi->oxcf.rc_cfg.mode == AOM_CQ); |
1394 | | |
1395 | | *top_index = q; |
1396 | | *bottom_index = q; |
1397 | | |
1398 | | return q; |
1399 | | } |
1400 | | #endif // USE_UNRESTRICTED_Q_IN_CQ_MODE |
1401 | | |
1402 | 0 | #define STATIC_MOTION_THRESH 95 |
1403 | | static void get_intra_q_and_bounds(const AV1_COMP *cpi, int width, int height, |
1404 | | int *active_best, int *active_worst, |
1405 | 1.26k | int cq_level) { |
1406 | 1.26k | const AV1_COMMON *const cm = &cpi->common; |
1407 | 1.26k | const RATE_CONTROL *const rc = &cpi->rc; |
1408 | 1.26k | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1409 | 1.26k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
1410 | 1.26k | int active_best_quality; |
1411 | 1.26k | int active_worst_quality = *active_worst; |
1412 | 1.26k | const int bit_depth = cm->seq_params->bit_depth; |
1413 | | |
1414 | 1.26k | if (rc->frames_to_key <= 1 && oxcf->rc_cfg.mode == AOM_Q) { |
1415 | | // If the next frame is also a key frame or the current frame is the |
1416 | | // only frame in the sequence in AOM_Q mode, just use the cq_level |
1417 | | // as q. |
1418 | 1.26k | active_best_quality = cq_level; |
1419 | 1.26k | active_worst_quality = cq_level; |
1420 | 1.26k | } else if (p_rc->this_key_frame_forced) { |
1421 | | // Handle the special case for key frames forced when we have reached |
1422 | | // the maximum key frame interval. Here force the Q to a range |
1423 | | // based on the ambient Q to reduce the risk of popping. |
1424 | 0 | double last_boosted_q; |
1425 | 0 | int delta_qindex; |
1426 | 0 | int qindex; |
1427 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1428 | | const int simulate_parallel_frame = |
1429 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
1430 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
1431 | | int last_boosted_qindex = simulate_parallel_frame |
1432 | | ? p_rc->temp_last_boosted_qindex |
1433 | | : p_rc->last_boosted_qindex; |
1434 | | #else |
1435 | 0 | int last_boosted_qindex = p_rc->last_boosted_qindex; |
1436 | 0 | #endif |
1437 | 0 | if (is_stat_consumption_stage_twopass(cpi) && |
1438 | 0 | cpi->ppi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { |
1439 | 0 | qindex = AOMMIN(p_rc->last_kf_qindex, last_boosted_qindex); |
1440 | 0 | active_best_quality = qindex; |
1441 | 0 | last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth); |
1442 | 0 | delta_qindex = av1_compute_qdelta(rc, last_boosted_q, |
1443 | 0 | last_boosted_q * 1.25, bit_depth); |
1444 | 0 | active_worst_quality = |
1445 | 0 | AOMMIN(qindex + delta_qindex, active_worst_quality); |
1446 | 0 | } else { |
1447 | 0 | qindex = last_boosted_qindex; |
1448 | 0 | last_boosted_q = av1_convert_qindex_to_q(qindex, bit_depth); |
1449 | 0 | delta_qindex = av1_compute_qdelta(rc, last_boosted_q, |
1450 | 0 | last_boosted_q * 0.50, bit_depth); |
1451 | 0 | active_best_quality = AOMMAX(qindex + delta_qindex, rc->best_quality); |
1452 | 0 | } |
1453 | 0 | } else { |
1454 | | // Not forced keyframe. |
1455 | 0 | double q_adj_factor = 1.0; |
1456 | 0 | double q_val; |
1457 | | |
1458 | | // Baseline value derived from cpi->active_worst_quality and kf boost. |
1459 | 0 | active_best_quality = |
1460 | 0 | get_kf_active_quality(p_rc, active_worst_quality, bit_depth); |
1461 | 0 | if (cpi->is_screen_content_type) { |
1462 | 0 | active_best_quality /= 2; |
1463 | 0 | } |
1464 | |
|
1465 | 0 | if (is_stat_consumption_stage_twopass(cpi) && |
1466 | 0 | cpi->ppi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) { |
1467 | 0 | active_best_quality /= 3; |
1468 | 0 | } |
1469 | | |
1470 | | // Allow somewhat lower kf minq with small image formats. |
1471 | 0 | if ((width * height) <= (352 * 288)) { |
1472 | 0 | q_adj_factor -= 0.25; |
1473 | 0 | } |
1474 | | |
1475 | | // Make a further adjustment based on the kf zero motion measure. |
1476 | 0 | if (is_stat_consumption_stage_twopass(cpi)) |
1477 | 0 | q_adj_factor += |
1478 | 0 | 0.05 - (0.001 * (double)cpi->ppi->twopass.kf_zeromotion_pct); |
1479 | | |
1480 | | // Convert the adjustment factor to a qindex delta |
1481 | | // on active_best_quality. |
1482 | 0 | q_val = av1_convert_qindex_to_q(active_best_quality, bit_depth); |
1483 | 0 | active_best_quality += |
1484 | 0 | av1_compute_qdelta(rc, q_val, q_val * q_adj_factor, bit_depth); |
1485 | | |
1486 | | // Tweak active_best_quality for AOM_Q mode when superres is on, as this |
1487 | | // will be used directly as 'q' later. |
1488 | 0 | if (oxcf->rc_cfg.mode == AOM_Q && |
1489 | 0 | (cpi->superres_mode == AOM_SUPERRES_QTHRESH || |
1490 | 0 | cpi->superres_mode == AOM_SUPERRES_AUTO) && |
1491 | 0 | cm->superres_scale_denominator != SCALE_NUMERATOR) { |
1492 | 0 | active_best_quality = |
1493 | 0 | AOMMAX(active_best_quality - |
1494 | 0 | ((cm->superres_scale_denominator - SCALE_NUMERATOR) * |
1495 | 0 | SUPERRES_QADJ_PER_DENOM_KEYFRAME), |
1496 | 0 | 0); |
1497 | 0 | } |
1498 | 0 | } |
1499 | 1.26k | *active_best = active_best_quality; |
1500 | 1.26k | *active_worst = active_worst_quality; |
1501 | 1.26k | } |
1502 | | |
1503 | | static void adjust_active_best_and_worst_quality(const AV1_COMP *cpi, |
1504 | | const int is_intrl_arf_boost, |
1505 | | int *active_worst, |
1506 | 0 | int *active_best) { |
1507 | 0 | const AV1_COMMON *const cm = &cpi->common; |
1508 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1509 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1510 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
1511 | 0 | const int bit_depth = cpi->common.seq_params->bit_depth; |
1512 | 0 | int active_best_quality = *active_best; |
1513 | 0 | int active_worst_quality = *active_worst; |
1514 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1515 | | const int simulate_parallel_frame = |
1516 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
1517 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
1518 | | int extend_minq_fast = simulate_parallel_frame |
1519 | | ? p_rc->temp_extend_minq_fast |
1520 | | : cpi->ppi->twopass.extend_minq_fast; |
1521 | | int extend_minq = simulate_parallel_frame ? p_rc->temp_extend_minq |
1522 | | : cpi->ppi->twopass.extend_minq; |
1523 | | int extend_maxq = simulate_parallel_frame ? p_rc->temp_extend_maxq |
1524 | | : cpi->ppi->twopass.extend_maxq; |
1525 | | #endif |
1526 | | // Extension to max or min Q if undershoot or overshoot is outside |
1527 | | // the permitted range. |
1528 | 0 | if (cpi->oxcf.rc_cfg.mode != AOM_Q) { |
1529 | 0 | if (frame_is_intra_only(cm) || |
1530 | 0 | (!rc->is_src_frame_alt_ref && |
1531 | 0 | (refresh_frame->golden_frame || is_intrl_arf_boost || |
1532 | 0 | refresh_frame->alt_ref_frame))) { |
1533 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1534 | | active_best_quality -= (extend_minq + extend_minq_fast); |
1535 | | active_worst_quality += (extend_maxq / 2); |
1536 | | #else |
1537 | 0 | active_best_quality -= |
1538 | 0 | (cpi->ppi->twopass.extend_minq + cpi->ppi->twopass.extend_minq_fast); |
1539 | 0 | active_worst_quality += (cpi->ppi->twopass.extend_maxq / 2); |
1540 | 0 | #endif |
1541 | 0 | } else { |
1542 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1543 | | active_best_quality -= (extend_minq + extend_minq_fast) / 2; |
1544 | | active_worst_quality += extend_maxq; |
1545 | | #else |
1546 | 0 | active_best_quality -= |
1547 | 0 | (cpi->ppi->twopass.extend_minq + cpi->ppi->twopass.extend_minq_fast) / |
1548 | 0 | 2; |
1549 | 0 | active_worst_quality += cpi->ppi->twopass.extend_maxq; |
1550 | 0 | #endif |
1551 | 0 | } |
1552 | 0 | } |
1553 | |
|
1554 | 0 | #ifndef STRICT_RC |
1555 | | // Static forced key frames Q restrictions dealt with elsewhere. |
1556 | 0 | if (!(frame_is_intra_only(cm)) || !p_rc->this_key_frame_forced || |
1557 | 0 | (cpi->ppi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { |
1558 | 0 | const int qdelta = av1_frame_type_qdelta(cpi, active_worst_quality); |
1559 | 0 | active_worst_quality = |
1560 | 0 | AOMMAX(active_worst_quality + qdelta, active_best_quality); |
1561 | 0 | } |
1562 | 0 | #endif |
1563 | | |
1564 | | // Modify active_best_quality for downscaled normal frames. |
1565 | 0 | if (av1_frame_scaled(cm) && !frame_is_kf_gf_arf(cpi)) { |
1566 | 0 | int qdelta = av1_compute_qdelta_by_rate( |
1567 | 0 | rc, cm->current_frame.frame_type, active_best_quality, 2.0, |
1568 | 0 | cpi->is_screen_content_type, bit_depth); |
1569 | 0 | active_best_quality = |
1570 | 0 | AOMMAX(active_best_quality + qdelta, rc->best_quality); |
1571 | 0 | } |
1572 | |
|
1573 | 0 | active_best_quality = |
1574 | 0 | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
1575 | 0 | active_worst_quality = |
1576 | 0 | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
1577 | |
|
1578 | 0 | *active_best = active_best_quality; |
1579 | 0 | *active_worst = active_worst_quality; |
1580 | 0 | } |
1581 | | |
1582 | | /*!\brief Gets a Q value to use for the current frame |
1583 | | * |
1584 | | * |
1585 | | * Selects a Q value from a permitted range that we estimate |
1586 | | * will result in approximately the target number of bits. |
1587 | | * |
1588 | | * \ingroup rate_control |
1589 | | * \param[in] cpi Top level encoder instance structure |
1590 | | * \param[in] width Width of frame |
1591 | | * \param[in] height Height of frame |
1592 | | * \param[in] active_worst_quality Max Q allowed |
1593 | | * \param[in] active_best_quality Min Q allowed |
1594 | | * |
1595 | | * \return The suggested Q for this frame. |
1596 | | */ |
1597 | | static int get_q(const AV1_COMP *cpi, const int width, const int height, |
1598 | | const int active_worst_quality, |
1599 | 0 | const int active_best_quality) { |
1600 | 0 | const AV1_COMMON *const cm = &cpi->common; |
1601 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1602 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1603 | 0 | int q; |
1604 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1605 | | const int simulate_parallel_frame = |
1606 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
1607 | | cpi->ppi->fpmt_unit_test_cfg; |
1608 | | int last_boosted_qindex = simulate_parallel_frame |
1609 | | ? p_rc->temp_last_boosted_qindex |
1610 | | : p_rc->last_boosted_qindex; |
1611 | | #else |
1612 | 0 | int last_boosted_qindex = p_rc->last_boosted_qindex; |
1613 | 0 | #endif |
1614 | |
|
1615 | 0 | if (cpi->oxcf.rc_cfg.mode == AOM_Q || |
1616 | 0 | (frame_is_intra_only(cm) && !p_rc->this_key_frame_forced && |
1617 | 0 | cpi->ppi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH && |
1618 | 0 | rc->frames_to_key > 1)) { |
1619 | 0 | q = active_best_quality; |
1620 | | // Special case code to try and match quality with forced key frames. |
1621 | 0 | } else if (frame_is_intra_only(cm) && p_rc->this_key_frame_forced) { |
1622 | | // If static since last kf use better of last boosted and last kf q. |
1623 | 0 | if (cpi->ppi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { |
1624 | 0 | q = AOMMIN(p_rc->last_kf_qindex, last_boosted_qindex); |
1625 | 0 | } else { |
1626 | 0 | q = AOMMIN(last_boosted_qindex, |
1627 | 0 | (active_best_quality + active_worst_quality) / 2); |
1628 | 0 | } |
1629 | 0 | q = clamp(q, active_best_quality, active_worst_quality); |
1630 | 0 | } else { |
1631 | 0 | q = av1_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
1632 | 0 | active_worst_quality, width, height); |
1633 | 0 | if (q > active_worst_quality) { |
1634 | | // Special case when we are targeting the max allowed rate. |
1635 | 0 | if (rc->this_frame_target < rc->max_frame_bandwidth) { |
1636 | 0 | q = active_worst_quality; |
1637 | 0 | } |
1638 | 0 | } |
1639 | 0 | q = AOMMAX(q, active_best_quality); |
1640 | 0 | } |
1641 | 0 | return q; |
1642 | 0 | } |
1643 | | |
1644 | | // Returns |active_best_quality| for an inter frame. |
1645 | | // The |active_best_quality| depends on different rate control modes: |
1646 | | // VBR, Q, CQ, CBR. |
1647 | | // The returning active_best_quality could further be adjusted in |
1648 | | // adjust_active_best_and_worst_quality(). |
1649 | | static int get_active_best_quality(const AV1_COMP *const cpi, |
1650 | | const int active_worst_quality, |
1651 | 0 | const int cq_level, const int gf_index) { |
1652 | 0 | const AV1_COMMON *const cm = &cpi->common; |
1653 | 0 | const int bit_depth = cm->seq_params->bit_depth; |
1654 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1655 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1656 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
1657 | 0 | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
1658 | 0 | const GF_GROUP *gf_group = &cpi->ppi->gf_group; |
1659 | 0 | const enum aom_rc_mode rc_mode = oxcf->rc_cfg.mode; |
1660 | 0 | int *inter_minq; |
1661 | 0 | ASSIGN_MINQ_TABLE(bit_depth, inter_minq); |
1662 | 0 | int active_best_quality = 0; |
1663 | 0 | const int is_intrl_arf_boost = |
1664 | 0 | gf_group->update_type[gf_index] == INTNL_ARF_UPDATE; |
1665 | 0 | int is_leaf_frame = |
1666 | 0 | !(gf_group->update_type[gf_index] == ARF_UPDATE || |
1667 | 0 | gf_group->update_type[gf_index] == GF_UPDATE || is_intrl_arf_boost); |
1668 | | |
1669 | | // TODO(jingning): Consider to rework this hack that covers issues incurred |
1670 | | // in lightfield setting. |
1671 | 0 | if (cm->tiles.large_scale) { |
1672 | 0 | is_leaf_frame = !(refresh_frame->golden_frame || |
1673 | 0 | refresh_frame->alt_ref_frame || is_intrl_arf_boost); |
1674 | 0 | } |
1675 | 0 | const int is_overlay_frame = rc->is_src_frame_alt_ref; |
1676 | |
|
1677 | 0 | if (is_leaf_frame || is_overlay_frame) { |
1678 | 0 | if (rc_mode == AOM_Q) return cq_level; |
1679 | | |
1680 | 0 | active_best_quality = inter_minq[active_worst_quality]; |
1681 | | // For the constrained quality mode we don't want |
1682 | | // q to fall below the cq level. |
1683 | 0 | if ((rc_mode == AOM_CQ) && (active_best_quality < cq_level)) { |
1684 | 0 | active_best_quality = cq_level; |
1685 | 0 | } |
1686 | 0 | return active_best_quality; |
1687 | 0 | } |
1688 | | |
1689 | | // Determine active_best_quality for frames that are not leaf or overlay. |
1690 | 0 | int q = active_worst_quality; |
1691 | | // Use the lower of active_worst_quality and recent |
1692 | | // average Q as basis for GF/ARF best Q limit unless last frame was |
1693 | | // a key frame. |
1694 | 0 | if (rc->frames_since_key > 1 && |
1695 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
1696 | 0 | q = p_rc->avg_frame_qindex[INTER_FRAME]; |
1697 | 0 | } |
1698 | 0 | if (rc_mode == AOM_CQ && q < cq_level) q = cq_level; |
1699 | 0 | active_best_quality = get_gf_active_quality(p_rc, q, bit_depth); |
1700 | | // Constrained quality use slightly lower active best. |
1701 | 0 | if (rc_mode == AOM_CQ) active_best_quality = active_best_quality * 15 / 16; |
1702 | 0 | const int min_boost = get_gf_high_motion_quality(q, bit_depth); |
1703 | 0 | const int boost = min_boost - active_best_quality; |
1704 | 0 | active_best_quality = min_boost - (int)(boost * p_rc->arf_boost_factor); |
1705 | 0 | if (!is_intrl_arf_boost) return active_best_quality; |
1706 | | |
1707 | 0 | if (rc_mode == AOM_Q || rc_mode == AOM_CQ) active_best_quality = p_rc->arf_q; |
1708 | 0 | int this_height = gf_group_pyramid_level(gf_group, gf_index); |
1709 | 0 | while (this_height > 1) { |
1710 | 0 | active_best_quality = (active_best_quality + active_worst_quality + 1) / 2; |
1711 | 0 | --this_height; |
1712 | 0 | } |
1713 | 0 | return active_best_quality; |
1714 | 0 | } |
1715 | | |
1716 | | // Returns the q_index for a single frame in the GOP. |
1717 | | // This function assumes that rc_mode == AOM_Q mode. |
1718 | | int av1_q_mode_get_q_index(int base_q_index, int gf_update_type, |
1719 | 0 | int gf_pyramid_level, int arf_q) { |
1720 | 0 | const int is_intrl_arf_boost = gf_update_type == INTNL_ARF_UPDATE; |
1721 | 0 | int is_leaf_or_overlay_frame = gf_update_type == LF_UPDATE || |
1722 | 0 | gf_update_type == OVERLAY_UPDATE || |
1723 | 0 | gf_update_type == INTNL_OVERLAY_UPDATE; |
1724 | |
|
1725 | 0 | if (is_leaf_or_overlay_frame) return base_q_index; |
1726 | | |
1727 | 0 | if (!is_intrl_arf_boost) return arf_q; |
1728 | | |
1729 | 0 | int active_best_quality = arf_q; |
1730 | 0 | int active_worst_quality = base_q_index; |
1731 | |
|
1732 | 0 | while (gf_pyramid_level > 1) { |
1733 | 0 | active_best_quality = (active_best_quality + active_worst_quality + 1) / 2; |
1734 | 0 | --gf_pyramid_level; |
1735 | 0 | } |
1736 | 0 | return active_best_quality; |
1737 | 0 | } |
1738 | | |
1739 | | // Returns the q_index for the ARF in the GOP. |
1740 | | int av1_get_arf_q_index(int base_q_index, int gfu_boost, int bit_depth, |
1741 | 0 | double arf_boost_factor) { |
1742 | 0 | int active_best_quality = |
1743 | 0 | get_gf_active_quality_no_rc(gfu_boost, base_q_index, bit_depth); |
1744 | 0 | const int min_boost = get_gf_high_motion_quality(base_q_index, bit_depth); |
1745 | 0 | const int boost = min_boost - active_best_quality; |
1746 | 0 | return min_boost - (int)(boost * arf_boost_factor); |
1747 | 0 | } |
1748 | | |
1749 | | static int rc_pick_q_and_bounds_q_mode(const AV1_COMP *cpi, int width, |
1750 | | int height, int gf_index, |
1751 | 1.26k | int *bottom_index, int *top_index) { |
1752 | 1.26k | const AV1_COMMON *const cm = &cpi->common; |
1753 | 1.26k | const RATE_CONTROL *const rc = &cpi->rc; |
1754 | 1.26k | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1755 | 1.26k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
1756 | 1.26k | const int cq_level = |
1757 | 1.26k | get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm), |
1758 | 1.26k | cpi->superres_mode, cm->superres_scale_denominator); |
1759 | 1.26k | int active_best_quality = 0; |
1760 | 1.26k | int active_worst_quality = rc->active_worst_quality; |
1761 | 1.26k | int q; |
1762 | | |
1763 | 1.26k | if (frame_is_intra_only(cm)) { |
1764 | 1.26k | get_intra_q_and_bounds(cpi, width, height, &active_best_quality, |
1765 | 1.26k | &active_worst_quality, cq_level); |
1766 | 1.26k | } else { |
1767 | | // Active best quality limited by previous layer. |
1768 | 0 | active_best_quality = |
1769 | 0 | get_active_best_quality(cpi, active_worst_quality, cq_level, gf_index); |
1770 | 0 | } |
1771 | | |
1772 | 1.26k | *top_index = active_worst_quality; |
1773 | 1.26k | *bottom_index = active_best_quality; |
1774 | | |
1775 | 1.26k | *top_index = AOMMAX(*top_index, rc->best_quality); |
1776 | 1.26k | *top_index = AOMMIN(*top_index, rc->worst_quality); |
1777 | | |
1778 | 1.26k | *bottom_index = AOMMAX(*bottom_index, rc->best_quality); |
1779 | 1.26k | *bottom_index = AOMMIN(*bottom_index, rc->worst_quality); |
1780 | | |
1781 | 1.26k | q = active_best_quality; |
1782 | | |
1783 | 1.26k | q = AOMMAX(q, rc->best_quality); |
1784 | 1.26k | q = AOMMIN(q, rc->worst_quality); |
1785 | | |
1786 | 1.26k | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
1787 | 1.26k | assert(*bottom_index <= rc->worst_quality && |
1788 | 1.26k | *bottom_index >= rc->best_quality); |
1789 | 1.26k | assert(q <= rc->worst_quality && q >= rc->best_quality); |
1790 | | |
1791 | 1.26k | return q; |
1792 | 1.26k | } |
1793 | | |
1794 | | /*!\brief Picks q and q bounds given rate control parameters in \c cpi->rc. |
1795 | | * |
1796 | | * Handles the the general cases not covered by |
1797 | | * \ref rc_pick_q_and_bounds_no_stats_cbr() and |
1798 | | * \ref rc_pick_q_and_bounds_no_stats() |
1799 | | * |
1800 | | * \ingroup rate_control |
1801 | | * \param[in] cpi Top level encoder structure |
1802 | | * \param[in] width Coded frame width |
1803 | | * \param[in] height Coded frame height |
1804 | | * \param[in] gf_index Index of this frame in the golden frame group |
1805 | | * \param[out] bottom_index Bottom bound for q index (best quality) |
1806 | | * \param[out] top_index Top bound for q index (worst quality) |
1807 | | * \return Returns selected q index to be used for encoding this frame. |
1808 | | */ |
1809 | | static int rc_pick_q_and_bounds(const AV1_COMP *cpi, int width, int height, |
1810 | | int gf_index, int *bottom_index, |
1811 | 1.26k | int *top_index) { |
1812 | 1.26k | const AV1_COMMON *const cm = &cpi->common; |
1813 | 1.26k | const RATE_CONTROL *const rc = &cpi->rc; |
1814 | 1.26k | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1815 | 1.26k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
1816 | 1.26k | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
1817 | 1.26k | const GF_GROUP *gf_group = &cpi->ppi->gf_group; |
1818 | 1.26k | assert(IMPLIES(has_no_stats_stage(cpi), |
1819 | 1.26k | cpi->oxcf.rc_cfg.mode == AOM_Q && |
1820 | 1.26k | gf_group->update_type[gf_index] != ARF_UPDATE)); |
1821 | 1.26k | const int cq_level = |
1822 | 1.26k | get_active_cq_level(rc, p_rc, oxcf, frame_is_intra_only(cm), |
1823 | 1.26k | cpi->superres_mode, cm->superres_scale_denominator); |
1824 | | |
1825 | 1.26k | if (oxcf->rc_cfg.mode == AOM_Q) { |
1826 | 1.26k | return rc_pick_q_and_bounds_q_mode(cpi, width, height, gf_index, |
1827 | 1.26k | bottom_index, top_index); |
1828 | 1.26k | } |
1829 | | |
1830 | 0 | int active_best_quality = 0; |
1831 | 0 | int active_worst_quality = rc->active_worst_quality; |
1832 | 0 | int q; |
1833 | |
|
1834 | 0 | const int is_intrl_arf_boost = |
1835 | 0 | gf_group->update_type[gf_index] == INTNL_ARF_UPDATE; |
1836 | |
|
1837 | 0 | if (frame_is_intra_only(cm)) { |
1838 | 0 | get_intra_q_and_bounds(cpi, width, height, &active_best_quality, |
1839 | 0 | &active_worst_quality, cq_level); |
1840 | | #ifdef STRICT_RC |
1841 | | active_best_quality = 0; |
1842 | | #endif |
1843 | 0 | } else { |
1844 | | // Active best quality limited by previous layer. |
1845 | 0 | const int pyramid_level = gf_group_pyramid_level(gf_group, gf_index); |
1846 | |
|
1847 | 0 | if ((pyramid_level <= 1) || (pyramid_level > MAX_ARF_LAYERS)) { |
1848 | 0 | active_best_quality = get_active_best_quality(cpi, active_worst_quality, |
1849 | 0 | cq_level, gf_index); |
1850 | 0 | } else { |
1851 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
1852 | | const int simulate_parallel_frame = |
1853 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
1854 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
1855 | | int local_active_best_quality = |
1856 | | simulate_parallel_frame |
1857 | | ? p_rc->temp_active_best_quality[pyramid_level - 1] |
1858 | | : p_rc->active_best_quality[pyramid_level - 1]; |
1859 | | active_best_quality = local_active_best_quality + 1; |
1860 | | #else |
1861 | 0 | active_best_quality = p_rc->active_best_quality[pyramid_level - 1] + 1; |
1862 | 0 | #endif |
1863 | |
|
1864 | 0 | active_best_quality = AOMMIN(active_best_quality, active_worst_quality); |
1865 | | #ifdef STRICT_RC |
1866 | | active_best_quality += (active_worst_quality - active_best_quality) / 16; |
1867 | | #else |
1868 | 0 | active_best_quality += (active_worst_quality - active_best_quality) / 2; |
1869 | 0 | #endif |
1870 | 0 | } |
1871 | | |
1872 | | // For alt_ref and GF frames (including internal arf frames) adjust the |
1873 | | // worst allowed quality as well. This insures that even on hard |
1874 | | // sections we dont clamp the Q at the same value for arf frames and |
1875 | | // leaf (non arf) frames. This is important to the TPL model which assumes |
1876 | | // Q drops with each arf level. |
1877 | 0 | if (!(rc->is_src_frame_alt_ref) && |
1878 | 0 | (refresh_frame->golden_frame || refresh_frame->alt_ref_frame || |
1879 | 0 | is_intrl_arf_boost)) { |
1880 | 0 | active_worst_quality = |
1881 | 0 | (active_best_quality + (3 * active_worst_quality) + 2) / 4; |
1882 | 0 | } |
1883 | 0 | } |
1884 | |
|
1885 | 0 | adjust_active_best_and_worst_quality( |
1886 | 0 | cpi, is_intrl_arf_boost, &active_worst_quality, &active_best_quality); |
1887 | 0 | q = get_q(cpi, width, height, active_worst_quality, active_best_quality); |
1888 | | |
1889 | | // Special case when we are targeting the max allowed rate. |
1890 | 0 | if (rc->this_frame_target >= rc->max_frame_bandwidth && |
1891 | 0 | q > active_worst_quality) { |
1892 | 0 | active_worst_quality = q; |
1893 | 0 | } |
1894 | |
|
1895 | 0 | *top_index = active_worst_quality; |
1896 | 0 | *bottom_index = active_best_quality; |
1897 | |
|
1898 | 0 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
1899 | 0 | assert(*bottom_index <= rc->worst_quality && |
1900 | 0 | *bottom_index >= rc->best_quality); |
1901 | 0 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
1902 | |
|
1903 | 0 | return q; |
1904 | 1.26k | } |
1905 | | |
1906 | | int av1_rc_pick_q_and_bounds(const AV1_COMP *cpi, int width, int height, |
1907 | 1.26k | int gf_index, int *bottom_index, int *top_index) { |
1908 | 1.26k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1909 | 1.26k | int q; |
1910 | | // TODO(sarahparker) merge no-stats vbr and altref q computation |
1911 | | // with rc_pick_q_and_bounds(). |
1912 | 1.26k | const GF_GROUP *gf_group = &cpi->ppi->gf_group; |
1913 | 1.26k | if ((cpi->oxcf.rc_cfg.mode != AOM_Q || |
1914 | 1.26k | gf_group->update_type[gf_index] == ARF_UPDATE) && |
1915 | 1.26k | has_no_stats_stage(cpi)) { |
1916 | 0 | if (cpi->oxcf.rc_cfg.mode == AOM_CBR) { |
1917 | 0 | q = rc_pick_q_and_bounds_no_stats_cbr(cpi, width, height, bottom_index, |
1918 | 0 | top_index); |
1919 | | #if USE_UNRESTRICTED_Q_IN_CQ_MODE |
1920 | | } else if (cpi->oxcf.rc_cfg.mode == AOM_CQ) { |
1921 | | q = rc_pick_q_and_bounds_no_stats_cq(cpi, width, height, bottom_index, |
1922 | | top_index); |
1923 | | #endif // USE_UNRESTRICTED_Q_IN_CQ_MODE |
1924 | 0 | } else { |
1925 | 0 | q = rc_pick_q_and_bounds_no_stats(cpi, width, height, bottom_index, |
1926 | 0 | top_index); |
1927 | 0 | } |
1928 | 1.26k | } else { |
1929 | 1.26k | q = rc_pick_q_and_bounds(cpi, width, height, gf_index, bottom_index, |
1930 | 1.26k | top_index); |
1931 | 1.26k | } |
1932 | 1.26k | if (gf_group->update_type[gf_index] == ARF_UPDATE) p_rc->arf_q = q; |
1933 | | |
1934 | 1.26k | return q; |
1935 | 1.26k | } |
1936 | | |
1937 | | void av1_rc_compute_frame_size_bounds(const AV1_COMP *cpi, int frame_target, |
1938 | | int *frame_under_shoot_limit, |
1939 | 0 | int *frame_over_shoot_limit) { |
1940 | 0 | if (cpi->oxcf.rc_cfg.mode == AOM_Q) { |
1941 | 0 | *frame_under_shoot_limit = 0; |
1942 | 0 | *frame_over_shoot_limit = INT_MAX; |
1943 | 0 | } else { |
1944 | | // For very small rate targets where the fractional adjustment |
1945 | | // may be tiny make sure there is at least a minimum range. |
1946 | 0 | assert(cpi->sf.hl_sf.recode_tolerance <= 100); |
1947 | 0 | const int tolerance = (int)AOMMAX( |
1948 | 0 | 100, ((int64_t)cpi->sf.hl_sf.recode_tolerance * frame_target) / 100); |
1949 | 0 | *frame_under_shoot_limit = AOMMAX(frame_target - tolerance, 0); |
1950 | 0 | *frame_over_shoot_limit = |
1951 | 0 | AOMMIN(frame_target + tolerance, cpi->rc.max_frame_bandwidth); |
1952 | 0 | } |
1953 | 0 | } |
1954 | | |
1955 | 1.26k | void av1_rc_set_frame_target(AV1_COMP *cpi, int target, int width, int height) { |
1956 | 1.26k | const AV1_COMMON *const cm = &cpi->common; |
1957 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
1958 | | |
1959 | 1.26k | rc->this_frame_target = target; |
1960 | | |
1961 | | // Modify frame size target when down-scaled. |
1962 | 1.26k | if (av1_frame_scaled(cm) && cpi->oxcf.rc_cfg.mode != AOM_CBR) { |
1963 | 0 | rc->this_frame_target = |
1964 | 0 | (int)(rc->this_frame_target * |
1965 | 0 | resize_rate_factor(&cpi->oxcf.frm_dim_cfg, width, height)); |
1966 | 0 | } |
1967 | | |
1968 | | // Target rate per SB64 (including partial SB64s. |
1969 | 1.26k | rc->sb64_target_rate = |
1970 | 1.26k | (int)(((int64_t)rc->this_frame_target << 12) / (width * height)); |
1971 | 1.26k | } |
1972 | | |
1973 | 0 | static void update_alt_ref_frame_stats(AV1_COMP *cpi) { |
1974 | | // this frame refreshes means next frames don't unless specified by user |
1975 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
1976 | 0 | rc->frames_since_golden = 0; |
1977 | 0 | } |
1978 | | |
1979 | 1.26k | static void update_golden_frame_stats(AV1_COMP *cpi) { |
1980 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
1981 | | |
1982 | | // Update the Golden frame usage counts. |
1983 | 1.26k | if (cpi->refresh_frame.golden_frame || rc->is_src_frame_alt_ref) { |
1984 | 1.26k | rc->frames_since_golden = 0; |
1985 | 1.26k | } else if (cpi->common.show_frame) { |
1986 | 0 | rc->frames_since_golden++; |
1987 | 0 | } |
1988 | 1.26k | } |
1989 | | |
1990 | 1.26k | void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) { |
1991 | 1.26k | const AV1_COMMON *const cm = &cpi->common; |
1992 | 1.26k | const CurrentFrame *const current_frame = &cm->current_frame; |
1993 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
1994 | 1.26k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1995 | 1.26k | const GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
1996 | 1.26k | const RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
1997 | | |
1998 | 1.26k | const int is_intrnl_arf = |
1999 | 1.26k | gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE; |
2000 | | |
2001 | 1.26k | const int qindex = cm->quant_params.base_qindex; |
2002 | | |
2003 | | // Update rate control heuristics |
2004 | 1.26k | rc->projected_frame_size = (int)(bytes_used << 3); |
2005 | | |
2006 | | // Post encode loop adjustment of Q prediction. |
2007 | 1.26k | av1_rc_update_rate_correction_factors(cpi, |
2008 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
2009 | | 0, |
2010 | | #endif |
2011 | 1.26k | cm->width, cm->height); |
2012 | | |
2013 | | // Keep a record of last Q and ambient average Q. |
2014 | 1.26k | if (current_frame->frame_type == KEY_FRAME) { |
2015 | 1.26k | p_rc->last_q[KEY_FRAME] = qindex; |
2016 | 1.26k | p_rc->avg_frame_qindex[KEY_FRAME] = |
2017 | 1.26k | ROUND_POWER_OF_TWO(3 * p_rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); |
2018 | 1.26k | } else { |
2019 | 0 | if ((cpi->ppi->use_svc && cpi->oxcf.rc_cfg.mode == AOM_CBR) || |
2020 | 0 | (!rc->is_src_frame_alt_ref && |
2021 | 0 | !(refresh_frame->golden_frame || is_intrnl_arf || |
2022 | 0 | refresh_frame->alt_ref_frame))) { |
2023 | 0 | p_rc->last_q[INTER_FRAME] = qindex; |
2024 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] = ROUND_POWER_OF_TWO( |
2025 | 0 | 3 * p_rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); |
2026 | 0 | p_rc->ni_frames++; |
2027 | 0 | p_rc->tot_q += av1_convert_qindex_to_q(qindex, cm->seq_params->bit_depth); |
2028 | 0 | p_rc->avg_q = p_rc->tot_q / p_rc->ni_frames; |
2029 | | // Calculate the average Q for normal inter frames (not key or GFU |
2030 | | // frames). |
2031 | 0 | rc->ni_tot_qi += qindex; |
2032 | 0 | rc->ni_av_qi = rc->ni_tot_qi / p_rc->ni_frames; |
2033 | 0 | } |
2034 | 0 | } |
2035 | | // Keep record of last boosted (KF/GF/ARF) Q value. |
2036 | | // If the current frame is coded at a lower Q then we also update it. |
2037 | | // If all mbs in this group are skipped only update if the Q value is |
2038 | | // better than that already stored. |
2039 | | // This is used to help set quality in forced key frames to reduce popping |
2040 | 1.26k | if ((qindex < p_rc->last_boosted_qindex) || |
2041 | 1.26k | (current_frame->frame_type == KEY_FRAME) || |
2042 | 1.26k | (!p_rc->constrained_gf_group && |
2043 | 0 | (refresh_frame->alt_ref_frame || is_intrnl_arf || |
2044 | 1.26k | (refresh_frame->golden_frame && !rc->is_src_frame_alt_ref)))) { |
2045 | 1.26k | p_rc->last_boosted_qindex = qindex; |
2046 | 1.26k | } |
2047 | 1.26k | if (current_frame->frame_type == KEY_FRAME) p_rc->last_kf_qindex = qindex; |
2048 | | |
2049 | 1.26k | update_buffer_level(cpi, rc->projected_frame_size); |
2050 | 1.26k | rc->prev_avg_frame_bandwidth = rc->avg_frame_bandwidth; |
2051 | | |
2052 | | // Rolling monitors of whether we are over or underspending used to help |
2053 | | // regulate min and Max Q in two pass. |
2054 | 1.26k | if (av1_frame_scaled(cm)) |
2055 | 0 | rc->this_frame_target = (int)(rc->this_frame_target / |
2056 | 0 | resize_rate_factor(&cpi->oxcf.frm_dim_cfg, |
2057 | 0 | cm->width, cm->height)); |
2058 | 1.26k | if (current_frame->frame_type != KEY_FRAME) { |
2059 | 0 | p_rc->rolling_target_bits = (int)ROUND_POWER_OF_TWO_64( |
2060 | 0 | p_rc->rolling_target_bits * 3 + rc->this_frame_target, 2); |
2061 | 0 | p_rc->rolling_actual_bits = (int)ROUND_POWER_OF_TWO_64( |
2062 | 0 | p_rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2); |
2063 | 0 | } |
2064 | | |
2065 | | // Actual bits spent |
2066 | 1.26k | p_rc->total_actual_bits += rc->projected_frame_size; |
2067 | 1.26k | p_rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0; |
2068 | | |
2069 | 1.26k | if (is_altref_enabled(cpi->oxcf.gf_cfg.lag_in_frames, |
2070 | 1.26k | cpi->oxcf.gf_cfg.enable_auto_arf) && |
2071 | 1.26k | refresh_frame->alt_ref_frame && |
2072 | 1.26k | (current_frame->frame_type != KEY_FRAME && !frame_is_sframe(cm))) |
2073 | | // Update the alternate reference frame stats as appropriate. |
2074 | 0 | update_alt_ref_frame_stats(cpi); |
2075 | 1.26k | else |
2076 | | // Update the Golden frame stats as appropriate. |
2077 | 1.26k | update_golden_frame_stats(cpi); |
2078 | | |
2079 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
2080 | | /*The variables temp_avg_frame_qindex, temp_last_q, temp_avg_q, |
2081 | | * temp_last_boosted_qindex are introduced only for quality simulation |
2082 | | * purpose, it retains the value previous to the parallel encode frames. The |
2083 | | * variables are updated based on the update flag. |
2084 | | * |
2085 | | * If there exist show_existing_frames between parallel frames, then to |
2086 | | * retain the temp state do not update it. */ |
2087 | | int show_existing_between_parallel_frames = |
2088 | | (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == |
2089 | | INTNL_OVERLAY_UPDATE && |
2090 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2); |
2091 | | |
2092 | | if (cpi->do_frame_data_update && !show_existing_between_parallel_frames && |
2093 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) { |
2094 | | for (int i = 0; i < FRAME_TYPES; i++) { |
2095 | | p_rc->temp_last_q[i] = p_rc->last_q[i]; |
2096 | | } |
2097 | | p_rc->temp_avg_q = p_rc->avg_q; |
2098 | | p_rc->temp_last_boosted_qindex = p_rc->last_boosted_qindex; |
2099 | | p_rc->temp_total_actual_bits = p_rc->total_actual_bits; |
2100 | | p_rc->temp_projected_frame_size = rc->projected_frame_size; |
2101 | | for (int i = 0; i < RATE_FACTOR_LEVELS; i++) |
2102 | | p_rc->temp_rate_correction_factors[i] = p_rc->rate_correction_factors[i]; |
2103 | | } |
2104 | | #endif |
2105 | 1.26k | if (current_frame->frame_type == KEY_FRAME) rc->frames_since_key = 0; |
2106 | | // if (current_frame->frame_number == 1 && cm->show_frame) |
2107 | | /* |
2108 | | rc->this_frame_target = |
2109 | | (int)(rc->this_frame_target / resize_rate_factor(&cpi->oxcf.frm_dim_cfg, |
2110 | | cm->width, cm->height)); |
2111 | | */ |
2112 | 1.26k | } |
2113 | | |
2114 | 0 | void av1_rc_postencode_update_drop_frame(AV1_COMP *cpi) { |
2115 | | // Update buffer level with zero size, update frame counters, and return. |
2116 | 0 | update_buffer_level(cpi, 0); |
2117 | 0 | cpi->rc.frames_since_key++; |
2118 | 0 | cpi->rc.frames_to_key--; |
2119 | 0 | cpi->rc.rc_2_frame = 0; |
2120 | 0 | cpi->rc.rc_1_frame = 0; |
2121 | 0 | cpi->rc.prev_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth; |
2122 | 0 | } |
2123 | | |
2124 | | int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth, |
2125 | 4.20k | int best_qindex, int worst_qindex) { |
2126 | 4.20k | assert(best_qindex <= worst_qindex); |
2127 | 4.20k | int low = best_qindex; |
2128 | 4.20k | int high = worst_qindex; |
2129 | 37.8k | while (low < high) { |
2130 | 33.6k | const int mid = (low + high) >> 1; |
2131 | 33.6k | const double mid_q = av1_convert_qindex_to_q(mid, bit_depth); |
2132 | 33.6k | if (mid_q < desired_q) { |
2133 | 15.0k | low = mid + 1; |
2134 | 18.5k | } else { |
2135 | 18.5k | high = mid; |
2136 | 18.5k | } |
2137 | 33.6k | } |
2138 | 4.20k | assert(low == high); |
2139 | 4.20k | assert(av1_convert_qindex_to_q(low, bit_depth) >= desired_q || |
2140 | 4.20k | low == worst_qindex); |
2141 | 4.20k | return low; |
2142 | 4.20k | } |
2143 | | |
2144 | | int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, |
2145 | 0 | aom_bit_depth_t bit_depth) { |
2146 | 0 | const int start_index = |
2147 | 0 | av1_find_qindex(qstart, bit_depth, rc->best_quality, rc->worst_quality); |
2148 | 0 | const int target_index = |
2149 | 0 | av1_find_qindex(qtarget, bit_depth, rc->best_quality, rc->worst_quality); |
2150 | 0 | return target_index - start_index; |
2151 | 0 | } |
2152 | | |
2153 | | // Find q_index for the desired_bits_per_mb, within [best_qindex, worst_qindex], |
2154 | | // assuming 'correction_factor' is 1.0. |
2155 | | // To be precise, 'q_index' is the smallest integer, for which the corresponding |
2156 | | // bits per mb <= desired_bits_per_mb. |
2157 | | // If no such q index is found, returns 'worst_qindex'. |
2158 | | static int find_qindex_by_rate(int desired_bits_per_mb, |
2159 | | aom_bit_depth_t bit_depth, FRAME_TYPE frame_type, |
2160 | | const int is_screen_content_type, |
2161 | 0 | int best_qindex, int worst_qindex) { |
2162 | 0 | assert(best_qindex <= worst_qindex); |
2163 | 0 | int low = best_qindex; |
2164 | 0 | int high = worst_qindex; |
2165 | 0 | while (low < high) { |
2166 | 0 | const int mid = (low + high) >> 1; |
2167 | 0 | const int mid_bits_per_mb = av1_rc_bits_per_mb( |
2168 | 0 | frame_type, mid, 1.0, bit_depth, is_screen_content_type); |
2169 | 0 | if (mid_bits_per_mb > desired_bits_per_mb) { |
2170 | 0 | low = mid + 1; |
2171 | 0 | } else { |
2172 | 0 | high = mid; |
2173 | 0 | } |
2174 | 0 | } |
2175 | 0 | assert(low == high); |
2176 | 0 | assert(av1_rc_bits_per_mb(frame_type, low, 1.0, bit_depth, |
2177 | 0 | is_screen_content_type) <= desired_bits_per_mb || |
2178 | 0 | low == worst_qindex); |
2179 | 0 | return low; |
2180 | 0 | } |
2181 | | |
2182 | | int av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, |
2183 | | int qindex, double rate_target_ratio, |
2184 | | const int is_screen_content_type, |
2185 | 0 | aom_bit_depth_t bit_depth) { |
2186 | | // Look up the current projected bits per block for the base index |
2187 | 0 | const int base_bits_per_mb = av1_rc_bits_per_mb( |
2188 | 0 | frame_type, qindex, 1.0, bit_depth, is_screen_content_type); |
2189 | | |
2190 | | // Find the target bits per mb based on the base value and given ratio. |
2191 | 0 | const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb); |
2192 | |
|
2193 | 0 | const int target_index = find_qindex_by_rate( |
2194 | 0 | target_bits_per_mb, bit_depth, frame_type, is_screen_content_type, |
2195 | 0 | rc->best_quality, rc->worst_quality); |
2196 | 0 | return target_index - qindex; |
2197 | 0 | } |
2198 | | |
2199 | | void av1_rc_set_gf_interval_range(const AV1_COMP *const cpi, |
2200 | 9.05k | RATE_CONTROL *const rc) { |
2201 | 9.05k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2202 | | |
2203 | | // Special case code for 1 pass fixed Q mode tests |
2204 | 9.05k | if ((has_no_stats_stage(cpi)) && (oxcf->rc_cfg.mode == AOM_Q)) { |
2205 | 9.05k | rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval; |
2206 | 9.05k | rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval; |
2207 | 9.05k | rc->static_scene_max_gf_interval = rc->min_gf_interval + 1; |
2208 | 9.05k | } else { |
2209 | | // Set Maximum gf/arf interval |
2210 | 0 | rc->max_gf_interval = oxcf->gf_cfg.max_gf_interval; |
2211 | 0 | rc->min_gf_interval = oxcf->gf_cfg.min_gf_interval; |
2212 | 0 | if (rc->min_gf_interval == 0) |
2213 | 0 | rc->min_gf_interval = av1_rc_get_default_min_gf_interval( |
2214 | 0 | oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height, cpi->framerate); |
2215 | 0 | if (rc->max_gf_interval == 0) |
2216 | 0 | rc->max_gf_interval = av1_rc_get_default_max_gf_interval( |
2217 | 0 | cpi->framerate, rc->min_gf_interval); |
2218 | | /* |
2219 | | * Extended max interval for genuinely static scenes like slide shows. |
2220 | | * The no.of.stats available in the case of LAP is limited, |
2221 | | * hence setting to max_gf_interval. |
2222 | | */ |
2223 | 0 | if (cpi->ppi->lap_enabled) |
2224 | 0 | rc->static_scene_max_gf_interval = rc->max_gf_interval + 1; |
2225 | 0 | else |
2226 | 0 | rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH; |
2227 | |
|
2228 | 0 | if (rc->max_gf_interval > rc->static_scene_max_gf_interval) |
2229 | 0 | rc->max_gf_interval = rc->static_scene_max_gf_interval; |
2230 | | |
2231 | | // Clamp min to max |
2232 | 0 | rc->min_gf_interval = AOMMIN(rc->min_gf_interval, rc->max_gf_interval); |
2233 | 0 | } |
2234 | 9.05k | } |
2235 | | |
2236 | 9.05k | void av1_rc_update_framerate(AV1_COMP *cpi, int width, int height) { |
2237 | 9.05k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2238 | 9.05k | RATE_CONTROL *const rc = &cpi->rc; |
2239 | 9.05k | int vbr_max_bits; |
2240 | 9.05k | const int MBs = av1_get_MBs(width, height); |
2241 | | |
2242 | 9.05k | rc->avg_frame_bandwidth = |
2243 | 9.05k | (int)round(oxcf->rc_cfg.target_bandwidth / cpi->framerate); |
2244 | 9.05k | rc->min_frame_bandwidth = |
2245 | 9.05k | (int)(rc->avg_frame_bandwidth * oxcf->rc_cfg.vbrmin_section / 100); |
2246 | | |
2247 | 9.05k | rc->min_frame_bandwidth = |
2248 | 9.05k | AOMMAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS); |
2249 | | |
2250 | | // A maximum bitrate for a frame is defined. |
2251 | | // The baseline for this aligns with HW implementations that |
2252 | | // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits |
2253 | | // per 16x16 MB (averaged over a frame). However this limit is extended if |
2254 | | // a very high rate is given on the command line or the the rate cannnot |
2255 | | // be acheived because of a user specificed max q (e.g. when the user |
2256 | | // specifies lossless encode. |
2257 | 9.05k | vbr_max_bits = |
2258 | 9.05k | (int)(((int64_t)rc->avg_frame_bandwidth * oxcf->rc_cfg.vbrmax_section) / |
2259 | 9.05k | 100); |
2260 | 9.05k | rc->max_frame_bandwidth = |
2261 | 9.05k | AOMMAX(AOMMAX((MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits); |
2262 | | |
2263 | 9.05k | av1_rc_set_gf_interval_range(cpi, rc); |
2264 | 9.05k | } |
2265 | | |
2266 | | #define VBR_PCT_ADJUSTMENT_LIMIT 50 |
2267 | | // For VBR...adjustment to the frame target based on error from previous frames |
2268 | 0 | static void vbr_rate_correction(AV1_COMP *cpi, int *this_frame_target) { |
2269 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2270 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2271 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
2272 | | const int simulate_parallel_frame = |
2273 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
2274 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
2275 | | int64_t vbr_bits_off_target = simulate_parallel_frame |
2276 | | ? cpi->ppi->p_rc.temp_vbr_bits_off_target |
2277 | | : p_rc->vbr_bits_off_target; |
2278 | | #else |
2279 | 0 | int64_t vbr_bits_off_target = p_rc->vbr_bits_off_target; |
2280 | 0 | #endif |
2281 | 0 | const int stats_count = |
2282 | 0 | cpi->ppi->twopass.stats_buf_ctx->total_stats != NULL |
2283 | 0 | ? (int)cpi->ppi->twopass.stats_buf_ctx->total_stats->count |
2284 | 0 | : 0; |
2285 | 0 | const int frame_window = AOMMIN( |
2286 | 0 | 16, (int)(stats_count - (int)cpi->common.current_frame.frame_number)); |
2287 | 0 | assert(VBR_PCT_ADJUSTMENT_LIMIT <= 100); |
2288 | 0 | if (frame_window > 0) { |
2289 | 0 | const int max_delta = (int)AOMMIN( |
2290 | 0 | abs((int)(vbr_bits_off_target / frame_window)), |
2291 | 0 | ((int64_t)(*this_frame_target) * VBR_PCT_ADJUSTMENT_LIMIT) / 100); |
2292 | | |
2293 | | // vbr_bits_off_target > 0 means we have extra bits to spend |
2294 | | // vbr_bits_off_target < 0 we are currently overshooting |
2295 | 0 | *this_frame_target += (vbr_bits_off_target >= 0) ? max_delta : -max_delta; |
2296 | 0 | } |
2297 | |
|
2298 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
2299 | | int64_t vbr_bits_off_target_fast = |
2300 | | simulate_parallel_frame ? cpi->ppi->p_rc.temp_vbr_bits_off_target_fast |
2301 | | : p_rc->vbr_bits_off_target_fast; |
2302 | | #endif |
2303 | | // Fast redistribution of bits arising from massive local undershoot. |
2304 | | // Dont do it for kf,arf,gf or overlay frames. |
2305 | 0 | if (!frame_is_kf_gf_arf(cpi) && |
2306 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
2307 | | vbr_bits_off_target_fast && |
2308 | | #else |
2309 | 0 | p_rc->vbr_bits_off_target_fast && |
2310 | 0 | #endif |
2311 | 0 | !rc->is_src_frame_alt_ref) { |
2312 | 0 | int one_frame_bits = AOMMAX(rc->avg_frame_bandwidth, *this_frame_target); |
2313 | 0 | int fast_extra_bits; |
2314 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
2315 | | fast_extra_bits = (int)AOMMIN(vbr_bits_off_target_fast, one_frame_bits); |
2316 | | fast_extra_bits = |
2317 | | (int)AOMMIN(fast_extra_bits, |
2318 | | AOMMAX(one_frame_bits / 8, vbr_bits_off_target_fast / 8)); |
2319 | | #else |
2320 | 0 | fast_extra_bits = |
2321 | 0 | (int)AOMMIN(p_rc->vbr_bits_off_target_fast, one_frame_bits); |
2322 | 0 | fast_extra_bits = (int)AOMMIN( |
2323 | 0 | fast_extra_bits, |
2324 | 0 | AOMMAX(one_frame_bits / 8, p_rc->vbr_bits_off_target_fast / 8)); |
2325 | 0 | #endif |
2326 | 0 | if (fast_extra_bits > 0) { |
2327 | | // Update this_frame_target only if additional bits are available from |
2328 | | // local undershoot. |
2329 | 0 | *this_frame_target += (int)fast_extra_bits; |
2330 | 0 | } |
2331 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
2332 | | // Store the fast_extra_bits of the frame and reduce it from |
2333 | | // vbr_bits_off_target_fast during postencode stage. |
2334 | | rc->frame_level_fast_extra_bits = fast_extra_bits; |
2335 | | // Retaining the condition to udpate during postencode stage since |
2336 | | // fast_extra_bits are calculated based on vbr_bits_off_target_fast. |
2337 | | cpi->do_update_vbr_bits_off_target_fast = 1; |
2338 | | #else |
2339 | 0 | p_rc->vbr_bits_off_target_fast -= fast_extra_bits; |
2340 | 0 | #endif |
2341 | 0 | } |
2342 | 0 | } |
2343 | | |
2344 | 0 | void av1_set_target_rate(AV1_COMP *cpi, int width, int height) { |
2345 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2346 | 0 | int target_rate = rc->base_frame_target; |
2347 | | |
2348 | | // Correction to rate target based on prior over or under shoot. |
2349 | 0 | if (cpi->oxcf.rc_cfg.mode == AOM_VBR || cpi->oxcf.rc_cfg.mode == AOM_CQ) |
2350 | 0 | vbr_rate_correction(cpi, &target_rate); |
2351 | 0 | av1_rc_set_frame_target(cpi, target_rate, width, height); |
2352 | 0 | } |
2353 | | |
2354 | | int av1_calc_pframe_target_size_one_pass_vbr( |
2355 | 0 | const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) { |
2356 | 0 | static const int af_ratio = 10; |
2357 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
2358 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2359 | 0 | int64_t target; |
2360 | 0 | #if USE_ALTREF_FOR_ONE_PASS |
2361 | 0 | if (frame_update_type == KF_UPDATE || frame_update_type == GF_UPDATE || |
2362 | 0 | frame_update_type == ARF_UPDATE) { |
2363 | 0 | target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval * |
2364 | 0 | af_ratio) / |
2365 | 0 | (p_rc->baseline_gf_interval + af_ratio - 1); |
2366 | 0 | } else { |
2367 | 0 | target = ((int64_t)rc->avg_frame_bandwidth * p_rc->baseline_gf_interval) / |
2368 | 0 | (p_rc->baseline_gf_interval + af_ratio - 1); |
2369 | 0 | } |
2370 | 0 | if (target > INT_MAX) target = INT_MAX; |
2371 | | #else |
2372 | | target = rc->avg_frame_bandwidth; |
2373 | | #endif |
2374 | 0 | return av1_rc_clamp_pframe_target_size(cpi, (int)target, frame_update_type); |
2375 | 0 | } |
2376 | | |
2377 | 2.52k | int av1_calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) { |
2378 | 2.52k | static const int kf_ratio = 25; |
2379 | 2.52k | const RATE_CONTROL *rc = &cpi->rc; |
2380 | 2.52k | const int target = rc->avg_frame_bandwidth * kf_ratio; |
2381 | 2.52k | return av1_rc_clamp_iframe_target_size(cpi, target); |
2382 | 2.52k | } |
2383 | | |
2384 | | int av1_calc_pframe_target_size_one_pass_cbr( |
2385 | 0 | const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) { |
2386 | 0 | const AV1EncoderConfig *oxcf = &cpi->oxcf; |
2387 | 0 | const RATE_CONTROL *rc = &cpi->rc; |
2388 | 0 | const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc; |
2389 | 0 | const RateControlCfg *rc_cfg = &oxcf->rc_cfg; |
2390 | 0 | const int64_t diff = p_rc->optimal_buffer_level - p_rc->buffer_level; |
2391 | 0 | const int64_t one_pct_bits = 1 + p_rc->optimal_buffer_level / 100; |
2392 | 0 | int min_frame_target = |
2393 | 0 | AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS); |
2394 | 0 | int target; |
2395 | |
|
2396 | 0 | if (rc_cfg->gf_cbr_boost_pct) { |
2397 | 0 | const int af_ratio_pct = rc_cfg->gf_cbr_boost_pct + 100; |
2398 | 0 | if (frame_update_type == GF_UPDATE || frame_update_type == OVERLAY_UPDATE) { |
2399 | 0 | target = (rc->avg_frame_bandwidth * p_rc->baseline_gf_interval * |
2400 | 0 | af_ratio_pct) / |
2401 | 0 | (p_rc->baseline_gf_interval * 100 + af_ratio_pct - 100); |
2402 | 0 | } else { |
2403 | 0 | target = (rc->avg_frame_bandwidth * p_rc->baseline_gf_interval * 100) / |
2404 | 0 | (p_rc->baseline_gf_interval * 100 + af_ratio_pct - 100); |
2405 | 0 | } |
2406 | 0 | } else { |
2407 | 0 | target = rc->avg_frame_bandwidth; |
2408 | 0 | } |
2409 | 0 | if (cpi->ppi->use_svc) { |
2410 | | // Note that for layers, avg_frame_bandwidth is the cumulative |
2411 | | // per-frame-bandwidth. For the target size of this frame, use the |
2412 | | // layer average frame size (i.e., non-cumulative per-frame-bw). |
2413 | 0 | int layer = |
2414 | 0 | LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id, |
2415 | 0 | cpi->svc.number_temporal_layers); |
2416 | 0 | const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer]; |
2417 | 0 | target = lc->avg_frame_size; |
2418 | 0 | min_frame_target = AOMMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); |
2419 | 0 | } |
2420 | 0 | if (diff > 0) { |
2421 | | // Lower the target bandwidth for this frame. |
2422 | 0 | const int pct_low = |
2423 | 0 | (int)AOMMIN(diff / one_pct_bits, rc_cfg->under_shoot_pct); |
2424 | 0 | target -= (target * pct_low) / 200; |
2425 | 0 | } else if (diff < 0) { |
2426 | | // Increase the target bandwidth for this frame. |
2427 | 0 | const int pct_high = |
2428 | 0 | (int)AOMMIN(-diff / one_pct_bits, rc_cfg->over_shoot_pct); |
2429 | 0 | target += (target * pct_high) / 200; |
2430 | 0 | } |
2431 | 0 | if (rc_cfg->max_inter_bitrate_pct) { |
2432 | 0 | const int max_rate = |
2433 | 0 | rc->avg_frame_bandwidth * rc_cfg->max_inter_bitrate_pct / 100; |
2434 | 0 | target = AOMMIN(target, max_rate); |
2435 | 0 | } |
2436 | 0 | return AOMMAX(min_frame_target, target); |
2437 | 0 | } |
2438 | | |
2439 | 0 | int av1_calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) { |
2440 | 0 | const RATE_CONTROL *rc = &cpi->rc; |
2441 | 0 | const PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc; |
2442 | 0 | int target; |
2443 | 0 | if (cpi->common.current_frame.frame_number == 0) { |
2444 | 0 | target = ((p_rc->starting_buffer_level / 2) > INT_MAX) |
2445 | 0 | ? INT_MAX |
2446 | 0 | : (int)(p_rc->starting_buffer_level / 2); |
2447 | 0 | if (cpi->svc.number_temporal_layers > 1 && target < (INT_MAX >> 2)) { |
2448 | 0 | target = target << AOMMIN(2, (cpi->svc.number_temporal_layers - 1)); |
2449 | 0 | } |
2450 | 0 | } else { |
2451 | 0 | int kf_boost = 32; |
2452 | 0 | double framerate = cpi->framerate; |
2453 | |
|
2454 | 0 | kf_boost = AOMMAX(kf_boost, (int)(2 * framerate - 16)); |
2455 | 0 | if (rc->frames_since_key < framerate / 2) { |
2456 | 0 | kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2)); |
2457 | 0 | } |
2458 | 0 | target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; |
2459 | 0 | } |
2460 | 0 | return av1_rc_clamp_iframe_target_size(cpi, target); |
2461 | 0 | } |
2462 | | |
2463 | 0 | static void set_baseline_gf_interval(AV1_COMP *cpi, FRAME_TYPE frame_type) { |
2464 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2465 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2466 | 0 | GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
2467 | 0 | if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) |
2468 | 0 | av1_cyclic_refresh_set_golden_update(cpi); |
2469 | 0 | else |
2470 | 0 | p_rc->baseline_gf_interval = FIXED_GF_INTERVAL; |
2471 | 0 | if (p_rc->baseline_gf_interval > rc->frames_to_key && |
2472 | 0 | cpi->oxcf.kf_cfg.auto_key) |
2473 | 0 | p_rc->baseline_gf_interval = rc->frames_to_key; |
2474 | 0 | p_rc->gfu_boost = DEFAULT_GF_BOOST_RT; |
2475 | 0 | p_rc->constrained_gf_group = |
2476 | 0 | (p_rc->baseline_gf_interval >= rc->frames_to_key && |
2477 | 0 | cpi->oxcf.kf_cfg.auto_key) |
2478 | 0 | ? 1 |
2479 | 0 | : 0; |
2480 | 0 | rc->frames_till_gf_update_due = p_rc->baseline_gf_interval; |
2481 | 0 | cpi->gf_frame_index = 0; |
2482 | | // SVC does not use GF as periodic boost. |
2483 | | // TODO(marpan): Find better way to disable this for SVC. |
2484 | 0 | if (cpi->ppi->use_svc) { |
2485 | 0 | SVC *const svc = &cpi->svc; |
2486 | 0 | p_rc->baseline_gf_interval = MAX_STATIC_GF_GROUP_LENGTH - 1; |
2487 | 0 | p_rc->gfu_boost = 1; |
2488 | 0 | p_rc->constrained_gf_group = 0; |
2489 | 0 | rc->frames_till_gf_update_due = p_rc->baseline_gf_interval; |
2490 | 0 | for (int layer = 0; |
2491 | 0 | layer < svc->number_spatial_layers * svc->number_temporal_layers; |
2492 | 0 | ++layer) { |
2493 | 0 | LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
2494 | 0 | lc->p_rc.baseline_gf_interval = p_rc->baseline_gf_interval; |
2495 | 0 | lc->p_rc.gfu_boost = p_rc->gfu_boost; |
2496 | 0 | lc->p_rc.constrained_gf_group = p_rc->constrained_gf_group; |
2497 | 0 | lc->rc.frames_till_gf_update_due = rc->frames_till_gf_update_due; |
2498 | 0 | lc->group_index = 0; |
2499 | 0 | } |
2500 | 0 | } |
2501 | 0 | gf_group->size = p_rc->baseline_gf_interval; |
2502 | 0 | gf_group->update_type[0] = (frame_type == KEY_FRAME) ? KF_UPDATE : GF_UPDATE; |
2503 | 0 | gf_group->refbuf_state[cpi->gf_frame_index] = |
2504 | 0 | (frame_type == KEY_FRAME) ? REFBUF_RESET : REFBUF_UPDATE; |
2505 | 0 | } |
2506 | | |
2507 | 0 | void av1_adjust_gf_refresh_qp_one_pass_rt(AV1_COMP *cpi) { |
2508 | 0 | AV1_COMMON *const cm = &cpi->common; |
2509 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2510 | 0 | SVC *const svc = &cpi->svc; |
2511 | 0 | const int resize_pending = is_frame_resize_pending(cpi); |
2512 | 0 | if (!resize_pending && !rc->high_source_sad) { |
2513 | | // Check if we should disable GF refresh (if period is up), |
2514 | | // or force a GF refresh update (if we are at least halfway through |
2515 | | // period) based on QP. Look into add info on segment deltaq. |
2516 | 0 | PRIMARY_RATE_CONTROL *p_rc = &cpi->ppi->p_rc; |
2517 | 0 | const int avg_qp = p_rc->avg_frame_qindex[INTER_FRAME]; |
2518 | 0 | const int allow_gf_update = |
2519 | 0 | rc->frames_till_gf_update_due <= (p_rc->baseline_gf_interval - 10); |
2520 | 0 | int gf_update_changed = 0; |
2521 | 0 | int thresh = 87; |
2522 | 0 | if (rc->frames_till_gf_update_due == 1 && |
2523 | 0 | cm->quant_params.base_qindex > avg_qp) { |
2524 | | // Disable GF refresh since QP is above the runninhg average QP. |
2525 | 0 | svc->refresh[svc->gld_idx_1layer] = 0; |
2526 | 0 | gf_update_changed = 1; |
2527 | 0 | } else if (allow_gf_update && |
2528 | 0 | ((cm->quant_params.base_qindex < thresh * avg_qp / 100) || |
2529 | 0 | (rc->avg_frame_low_motion && rc->avg_frame_low_motion < 20))) { |
2530 | | // Force refresh since QP is well below average QP or this is a high |
2531 | | // motion frame. |
2532 | 0 | svc->refresh[svc->gld_idx_1layer] = 1; |
2533 | 0 | gf_update_changed = 1; |
2534 | 0 | } |
2535 | 0 | if (gf_update_changed) { |
2536 | 0 | set_baseline_gf_interval(cpi, INTER_FRAME); |
2537 | 0 | int refresh_mask = 0; |
2538 | 0 | for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
2539 | 0 | int ref_frame_map_idx = svc->ref_idx[i]; |
2540 | 0 | refresh_mask |= svc->refresh[ref_frame_map_idx] << ref_frame_map_idx; |
2541 | 0 | } |
2542 | 0 | cm->current_frame.refresh_frame_flags = refresh_mask; |
2543 | 0 | } |
2544 | 0 | } |
2545 | 0 | } |
2546 | | |
2547 | | /*!\brief Setup the reference prediction structure for 1 pass real-time |
2548 | | * |
2549 | | * Set the reference prediction structure for 1 layer. |
2550 | | * Current structue is to use 3 references (LAST, GOLDEN, ALTREF), |
2551 | | * where ALT_REF always behind current by lag_alt frames, and GOLDEN is |
2552 | | * either updated on LAST with period baseline_gf_interval (fixed slot) |
2553 | | * or always behind current by lag_gld (gld_fixed_slot = 0, lag_gld <= 7). |
2554 | | * |
2555 | | * \ingroup rate_control |
2556 | | * \param[in] cpi Top level encoder structure |
2557 | | * \param[in] gf_update Flag to indicate if GF is updated |
2558 | | * |
2559 | | * \return Nothing is returned. Instead the settings for the prediction |
2560 | | * structure are set in \c cpi-ext_flags; and the buffer slot index |
2561 | | * (for each of 7 references) and refresh flags (for each of the 8 slots) |
2562 | | * are set in \c cpi->svc.ref_idx[] and \c cpi->svc.refresh[]. |
2563 | | */ |
2564 | 0 | void av1_set_reference_structure_one_pass_rt(AV1_COMP *cpi, int gf_update) { |
2565 | 0 | AV1_COMMON *const cm = &cpi->common; |
2566 | 0 | ExternalFlags *const ext_flags = &cpi->ext_flags; |
2567 | 0 | ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags = |
2568 | 0 | &ext_flags->refresh_frame; |
2569 | 0 | SVC *const svc = &cpi->svc; |
2570 | 0 | const int gld_fixed_slot = 1; |
2571 | 0 | const unsigned int lag_alt = 4; |
2572 | 0 | int last_idx = 0; |
2573 | 0 | int last_idx_refresh = 0; |
2574 | 0 | int gld_idx = 0; |
2575 | 0 | int alt_ref_idx = 0; |
2576 | 0 | int last2_idx = 0; |
2577 | 0 | ext_refresh_frame_flags->update_pending = 1; |
2578 | 0 | svc->set_ref_frame_config = 1; |
2579 | 0 | ext_flags->ref_frame_flags = 0; |
2580 | 0 | ext_refresh_frame_flags->last_frame = 1; |
2581 | 0 | ext_refresh_frame_flags->golden_frame = 0; |
2582 | 0 | ext_refresh_frame_flags->alt_ref_frame = 0; |
2583 | 0 | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) svc->ref_idx[i] = 7; |
2584 | 0 | for (int i = 0; i < REF_FRAMES; ++i) svc->refresh[i] = 0; |
2585 | | // Set the reference frame flags. |
2586 | 0 | ext_flags->ref_frame_flags ^= AOM_LAST_FLAG; |
2587 | 0 | ext_flags->ref_frame_flags ^= AOM_ALT_FLAG; |
2588 | 0 | ext_flags->ref_frame_flags ^= AOM_GOLD_FLAG; |
2589 | 0 | if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1]) |
2590 | 0 | ext_flags->ref_frame_flags ^= AOM_LAST2_FLAG; |
2591 | 0 | const int sh = 7 - gld_fixed_slot; |
2592 | | // Moving index slot for last: 0 - (sh - 1). |
2593 | 0 | if (cm->current_frame.frame_number > 1) |
2594 | 0 | last_idx = ((cm->current_frame.frame_number - 1) % sh); |
2595 | | // Moving index for refresh of last: one ahead for next frame. |
2596 | 0 | last_idx_refresh = (cm->current_frame.frame_number % sh); |
2597 | 0 | gld_idx = 6; |
2598 | 0 | if (!gld_fixed_slot) { |
2599 | 0 | gld_idx = 7; |
2600 | 0 | const unsigned int lag_gld = 7; // Must be <= 7. |
2601 | | // Moving index for gld_ref, lag behind current by gld_interval frames. |
2602 | 0 | if (cm->current_frame.frame_number > lag_gld) |
2603 | 0 | gld_idx = ((cm->current_frame.frame_number - lag_gld) % sh); |
2604 | 0 | } |
2605 | | // Moving index for alt_ref, lag behind LAST by lag_alt frames. |
2606 | 0 | if (cm->current_frame.frame_number > lag_alt) |
2607 | 0 | alt_ref_idx = ((cm->current_frame.frame_number - lag_alt) % sh); |
2608 | 0 | if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1]) { |
2609 | | // Moving index for LAST2, lag behind LAST by 2 frames. |
2610 | 0 | if (cm->current_frame.frame_number > 2) |
2611 | 0 | last2_idx = ((cm->current_frame.frame_number - 2) % sh); |
2612 | 0 | } |
2613 | 0 | svc->ref_idx[0] = last_idx; // LAST |
2614 | 0 | svc->ref_idx[1] = last_idx_refresh; // LAST2 (for refresh of last). |
2615 | 0 | if (cpi->sf.rt_sf.ref_frame_comp_nonrd[1]) { |
2616 | 0 | svc->ref_idx[1] = last2_idx; // LAST2 |
2617 | 0 | svc->ref_idx[2] = last_idx_refresh; // LAST3 (for refresh of last). |
2618 | 0 | } |
2619 | 0 | svc->ref_idx[3] = gld_idx; // GOLDEN |
2620 | 0 | svc->ref_idx[6] = alt_ref_idx; // ALT_REF |
2621 | | // Refresh this slot, which will become LAST on next frame. |
2622 | 0 | svc->refresh[last_idx_refresh] = 1; |
2623 | | // Update GOLDEN on period for fixed slot case. |
2624 | 0 | if (gld_fixed_slot && gf_update) { |
2625 | 0 | ext_refresh_frame_flags->golden_frame = 1; |
2626 | 0 | svc->refresh[gld_idx] = 1; |
2627 | 0 | } |
2628 | 0 | svc->gld_idx_1layer = gld_idx; |
2629 | 0 | } |
2630 | | |
2631 | | /*!\brief Check for scene detection, for 1 pass real-time mode. |
2632 | | * |
2633 | | * Compute average source sad (temporal sad: between current source and |
2634 | | * previous source) over a subset of superblocks. Use this is detect big changes |
2635 | | * in content and set the \c cpi->rc.high_source_sad flag. |
2636 | | * |
2637 | | * \ingroup rate_control |
2638 | | * \param[in] cpi Top level encoder structure |
2639 | | * |
2640 | | * \return Nothing is returned. Instead the flag \c cpi->rc.high_source_sad |
2641 | | * is set if scene change is detected, and \c cpi->rc.avg_source_sad is updated. |
2642 | | */ |
2643 | 0 | static void rc_scene_detection_onepass_rt(AV1_COMP *cpi) { |
2644 | 0 | AV1_COMMON *const cm = &cpi->common; |
2645 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2646 | 0 | YV12_BUFFER_CONFIG const *unscaled_src = cpi->unscaled_source; |
2647 | 0 | YV12_BUFFER_CONFIG const *unscaled_last_src = cpi->unscaled_last_source; |
2648 | 0 | uint8_t *src_y; |
2649 | 0 | int src_ystride; |
2650 | 0 | int src_width; |
2651 | 0 | int src_height; |
2652 | 0 | uint8_t *last_src_y; |
2653 | 0 | int last_src_ystride; |
2654 | 0 | int last_src_width; |
2655 | 0 | int last_src_height; |
2656 | 0 | if (cpi->unscaled_source == NULL || cpi->unscaled_last_source == NULL) return; |
2657 | 0 | src_y = unscaled_src->y_buffer; |
2658 | 0 | src_ystride = unscaled_src->y_stride; |
2659 | 0 | src_width = unscaled_src->y_width; |
2660 | 0 | src_height = unscaled_src->y_height; |
2661 | 0 | last_src_y = unscaled_last_src->y_buffer; |
2662 | 0 | last_src_ystride = unscaled_last_src->y_stride; |
2663 | 0 | last_src_width = unscaled_last_src->y_width; |
2664 | 0 | last_src_height = unscaled_last_src->y_height; |
2665 | 0 | if (src_width != last_src_width || src_height != last_src_height) return; |
2666 | 0 | rc->high_source_sad = 0; |
2667 | 0 | rc->prev_avg_source_sad = rc->avg_source_sad; |
2668 | 0 | if (src_width == last_src_width && src_height == last_src_height) { |
2669 | 0 | const int num_mi_cols = cm->mi_params.mi_cols; |
2670 | 0 | const int num_mi_rows = cm->mi_params.mi_rows; |
2671 | 0 | int num_zero_temp_sad = 0; |
2672 | 0 | uint32_t min_thresh = 10000; |
2673 | 0 | if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN) min_thresh = 100000; |
2674 | 0 | const BLOCK_SIZE bsize = BLOCK_64X64; |
2675 | 0 | int full_sampling = (cm->width * cm->height < 640 * 360) ? 1 : 0; |
2676 | | // Loop over sub-sample of frame, compute average sad over 64x64 blocks. |
2677 | 0 | uint64_t avg_sad = 0; |
2678 | 0 | uint64_t tmp_sad = 0; |
2679 | 0 | int num_samples = 0; |
2680 | 0 | const int thresh = 6; |
2681 | | // SAD is computed on 64x64 blocks |
2682 | 0 | const int sb_size_by_mb = (cm->seq_params->sb_size == BLOCK_128X128) |
2683 | 0 | ? (cm->seq_params->mib_size >> 1) |
2684 | 0 | : cm->seq_params->mib_size; |
2685 | 0 | const int sb_cols = (num_mi_cols + sb_size_by_mb - 1) / sb_size_by_mb; |
2686 | 0 | const int sb_rows = (num_mi_rows + sb_size_by_mb - 1) / sb_size_by_mb; |
2687 | 0 | uint64_t sum_sq_thresh = 10000; // sum = sqrt(thresh / 64*64)) ~1.5 |
2688 | 0 | int num_low_var_high_sumdiff = 0; |
2689 | 0 | int light_change = 0; |
2690 | | // Flag to check light change or not. |
2691 | 0 | const int check_light_change = 0; |
2692 | 0 | for (int sbi_row = 0; sbi_row < sb_rows; ++sbi_row) { |
2693 | 0 | for (int sbi_col = 0; sbi_col < sb_cols; ++sbi_col) { |
2694 | | // Checker-board pattern, ignore boundary. |
2695 | 0 | if (full_sampling || |
2696 | 0 | ((sbi_row > 0 && sbi_col > 0) && |
2697 | 0 | (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) && |
2698 | 0 | ((sbi_row % 2 == 0 && sbi_col % 2 == 0) || |
2699 | 0 | (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) { |
2700 | 0 | tmp_sad = cpi->ppi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y, |
2701 | 0 | last_src_ystride); |
2702 | 0 | if (check_light_change) { |
2703 | 0 | unsigned int sse, variance; |
2704 | 0 | variance = cpi->ppi->fn_ptr[bsize].vf( |
2705 | 0 | src_y, src_ystride, last_src_y, last_src_ystride, &sse); |
2706 | | // Note: sse - variance = ((sum * sum) >> 12) |
2707 | | // Detect large lighting change. |
2708 | 0 | if (variance < (sse >> 1) && (sse - variance) > sum_sq_thresh) { |
2709 | 0 | num_low_var_high_sumdiff++; |
2710 | 0 | } |
2711 | 0 | } |
2712 | 0 | avg_sad += tmp_sad; |
2713 | 0 | num_samples++; |
2714 | 0 | if (tmp_sad == 0) num_zero_temp_sad++; |
2715 | 0 | } |
2716 | 0 | src_y += 64; |
2717 | 0 | last_src_y += 64; |
2718 | 0 | } |
2719 | 0 | src_y += (src_ystride << 6) - (sb_cols << 6); |
2720 | 0 | last_src_y += (last_src_ystride << 6) - (sb_cols << 6); |
2721 | 0 | } |
2722 | 0 | if (check_light_change && num_samples > 0 && |
2723 | 0 | num_low_var_high_sumdiff > (num_samples >> 1)) |
2724 | 0 | light_change = 1; |
2725 | 0 | if (num_samples > 0) avg_sad = avg_sad / num_samples; |
2726 | | // Set high_source_sad flag if we detect very high increase in avg_sad |
2727 | | // between current and previous frame value(s). Use minimum threshold |
2728 | | // for cases where there is small change from content that is completely |
2729 | | // static. |
2730 | 0 | if (!light_change && |
2731 | 0 | avg_sad > |
2732 | 0 | AOMMAX(min_thresh, (unsigned int)(rc->avg_source_sad * thresh)) && |
2733 | 0 | rc->frames_since_key > 1 + cpi->svc.number_spatial_layers && |
2734 | 0 | num_zero_temp_sad < 3 * (num_samples >> 2)) |
2735 | 0 | rc->high_source_sad = 1; |
2736 | 0 | else |
2737 | 0 | rc->high_source_sad = 0; |
2738 | 0 | rc->avg_source_sad = (3 * rc->avg_source_sad + avg_sad) >> 2; |
2739 | 0 | } |
2740 | 0 | cpi->svc.high_source_sad_superframe = rc->high_source_sad; |
2741 | 0 | } |
2742 | | |
2743 | | /*!\brief Set the GF baseline interval for 1 pass real-time mode. |
2744 | | * |
2745 | | * |
2746 | | * \ingroup rate_control |
2747 | | * \param[in] cpi Top level encoder structure |
2748 | | * \param[in] frame_type frame type |
2749 | | * |
2750 | | * \return Return GF update flag, and update the \c cpi->rc with |
2751 | | * the next GF interval settings. |
2752 | | */ |
2753 | | static int set_gf_interval_update_onepass_rt(AV1_COMP *cpi, |
2754 | 0 | FRAME_TYPE frame_type) { |
2755 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2756 | 0 | int gf_update = 0; |
2757 | 0 | const int resize_pending = is_frame_resize_pending(cpi); |
2758 | | // GF update based on frames_till_gf_update_due, also |
2759 | | // force upddate on resize pending frame or for scene change. |
2760 | 0 | if ((resize_pending || rc->high_source_sad || |
2761 | 0 | rc->frames_till_gf_update_due == 0) && |
2762 | 0 | cpi->svc.temporal_layer_id == 0 && cpi->svc.spatial_layer_id == 0) { |
2763 | 0 | set_baseline_gf_interval(cpi, frame_type); |
2764 | 0 | gf_update = 1; |
2765 | 0 | } |
2766 | 0 | return gf_update; |
2767 | 0 | } |
2768 | | |
2769 | | static void resize_reset_rc(AV1_COMP *cpi, int resize_width, int resize_height, |
2770 | 0 | int prev_width, int prev_height) { |
2771 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2772 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2773 | 0 | SVC *const svc = &cpi->svc; |
2774 | 0 | double tot_scale_change = 1.0; |
2775 | 0 | int target_bits_per_frame; |
2776 | 0 | int active_worst_quality; |
2777 | 0 | int qindex; |
2778 | 0 | tot_scale_change = (double)(resize_width * resize_height) / |
2779 | 0 | (double)(prev_width * prev_height); |
2780 | | // Reset buffer level to optimal, update target size. |
2781 | 0 | p_rc->buffer_level = p_rc->optimal_buffer_level; |
2782 | 0 | p_rc->bits_off_target = p_rc->optimal_buffer_level; |
2783 | 0 | rc->this_frame_target = |
2784 | 0 | av1_calc_pframe_target_size_one_pass_cbr(cpi, INTER_FRAME); |
2785 | 0 | target_bits_per_frame = rc->this_frame_target; |
2786 | 0 | if (tot_scale_change > 4.0) |
2787 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality; |
2788 | 0 | else if (tot_scale_change > 1.0) |
2789 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] = |
2790 | 0 | (p_rc->avg_frame_qindex[INTER_FRAME] + rc->worst_quality) >> 1; |
2791 | 0 | active_worst_quality = calc_active_worst_quality_no_stats_cbr(cpi); |
2792 | 0 | qindex = av1_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality, |
2793 | 0 | active_worst_quality, resize_width, resize_height); |
2794 | | // If resize is down, check if projected q index is close to worst_quality, |
2795 | | // and if so, reduce the rate correction factor (since likely can afford |
2796 | | // lower q for resized frame). |
2797 | 0 | if (tot_scale_change < 1.0 && qindex > 90 * cpi->rc.worst_quality / 100) |
2798 | 0 | p_rc->rate_correction_factors[INTER_NORMAL] *= 0.85; |
2799 | | // Apply the same rate control reset to all temporal layers. |
2800 | 0 | for (int tl = 0; tl < svc->number_temporal_layers; tl++) { |
2801 | 0 | LAYER_CONTEXT *lc = NULL; |
2802 | 0 | lc = &svc->layer_context[svc->spatial_layer_id * |
2803 | 0 | svc->number_temporal_layers + |
2804 | 0 | tl]; |
2805 | 0 | lc->rc.resize_state = rc->resize_state; |
2806 | 0 | lc->p_rc.buffer_level = lc->p_rc.optimal_buffer_level; |
2807 | 0 | lc->p_rc.bits_off_target = lc->p_rc.optimal_buffer_level; |
2808 | 0 | lc->p_rc.rate_correction_factors[INTER_FRAME] = |
2809 | 0 | p_rc->rate_correction_factors[INTER_FRAME]; |
2810 | 0 | } |
2811 | | // If resize is back up: check if projected q index is too much above the |
2812 | | // previous index, and if so, reduce the rate correction factor |
2813 | | // (since prefer to keep q for resized frame at least closet to previous q). |
2814 | | // Also check if projected qindex is close to previous qindex, if so |
2815 | | // increase correction factor (to push qindex higher and avoid overshoot). |
2816 | 0 | if (tot_scale_change >= 1.0) { |
2817 | 0 | if (tot_scale_change < 4.0 && |
2818 | 0 | qindex > 130 * p_rc->last_q[INTER_FRAME] / 100) |
2819 | 0 | p_rc->rate_correction_factors[INTER_NORMAL] *= 0.8; |
2820 | 0 | if (qindex <= 120 * p_rc->last_q[INTER_FRAME] / 100) |
2821 | 0 | p_rc->rate_correction_factors[INTER_NORMAL] *= 2.0; |
2822 | 0 | } |
2823 | 0 | } |
2824 | | |
2825 | | /*!\brief ChecK for resize based on Q, for 1 pass real-time mode. |
2826 | | * |
2827 | | * Check if we should resize, based on average QP from past x frames. |
2828 | | * Only allow for resize at most 1/2 scale down for now, Scaling factor |
2829 | | * for each step may be 3/4 or 1/2. |
2830 | | * |
2831 | | * \ingroup rate_control |
2832 | | * \param[in] cpi Top level encoder structure |
2833 | | * |
2834 | | * \return Return resized width/height in \c cpi->resize_pending_params, |
2835 | | * and update some resize counters in \c rc. |
2836 | | */ |
2837 | 0 | static void dynamic_resize_one_pass_cbr(AV1_COMP *cpi) { |
2838 | 0 | const AV1_COMMON *const cm = &cpi->common; |
2839 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2840 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2841 | 0 | RESIZE_ACTION resize_action = NO_RESIZE; |
2842 | 0 | const int avg_qp_thr1 = 70; |
2843 | 0 | const int avg_qp_thr2 = 50; |
2844 | | // Don't allow for resized frame to go below 160x90, resize in steps of 3/4. |
2845 | 0 | const int min_width = (160 * 4) / 3; |
2846 | 0 | const int min_height = (90 * 4) / 3; |
2847 | 0 | int down_size_on = 1; |
2848 | | // Don't resize on key frame; reset the counters on key frame. |
2849 | 0 | if (cm->current_frame.frame_type == KEY_FRAME) { |
2850 | 0 | rc->resize_avg_qp = 0; |
2851 | 0 | rc->resize_count = 0; |
2852 | 0 | rc->resize_buffer_underflow = 0; |
2853 | 0 | return; |
2854 | 0 | } |
2855 | | // No resizing down if frame size is below some limit. |
2856 | 0 | if ((cm->width * cm->height) < min_width * min_height) down_size_on = 0; |
2857 | | |
2858 | | // Resize based on average buffer underflow and QP over some window. |
2859 | | // Ignore samples close to key frame, since QP is usually high after key. |
2860 | 0 | if (cpi->rc.frames_since_key > cpi->framerate) { |
2861 | 0 | const int window = AOMMIN(30, (int)(2 * cpi->framerate)); |
2862 | 0 | rc->resize_avg_qp += p_rc->last_q[INTER_FRAME]; |
2863 | 0 | if (cpi->ppi->p_rc.buffer_level < |
2864 | 0 | (int)(30 * p_rc->optimal_buffer_level / 100)) |
2865 | 0 | ++rc->resize_buffer_underflow; |
2866 | 0 | ++rc->resize_count; |
2867 | | // Check for resize action every "window" frames. |
2868 | 0 | if (rc->resize_count >= window) { |
2869 | 0 | int avg_qp = rc->resize_avg_qp / rc->resize_count; |
2870 | | // Resize down if buffer level has underflowed sufficient amount in past |
2871 | | // window, and we are at original or 3/4 of original resolution. |
2872 | | // Resize back up if average QP is low, and we are currently in a resized |
2873 | | // down state, i.e. 1/2 or 3/4 of original resolution. |
2874 | | // Currently, use a flag to turn 3/4 resizing feature on/off. |
2875 | 0 | if (rc->resize_buffer_underflow > (rc->resize_count >> 2) && |
2876 | 0 | down_size_on) { |
2877 | 0 | if (rc->resize_state == THREE_QUARTER) { |
2878 | 0 | resize_action = DOWN_ONEHALF; |
2879 | 0 | rc->resize_state = ONE_HALF; |
2880 | 0 | } else if (rc->resize_state == ORIG) { |
2881 | 0 | resize_action = DOWN_THREEFOUR; |
2882 | 0 | rc->resize_state = THREE_QUARTER; |
2883 | 0 | } |
2884 | 0 | } else if (rc->resize_state != ORIG && |
2885 | 0 | avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) { |
2886 | 0 | if (rc->resize_state == THREE_QUARTER || |
2887 | 0 | avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100) { |
2888 | 0 | resize_action = UP_ORIG; |
2889 | 0 | rc->resize_state = ORIG; |
2890 | 0 | } else if (rc->resize_state == ONE_HALF) { |
2891 | 0 | resize_action = UP_THREEFOUR; |
2892 | 0 | rc->resize_state = THREE_QUARTER; |
2893 | 0 | } |
2894 | 0 | } |
2895 | | // Reset for next window measurement. |
2896 | 0 | rc->resize_avg_qp = 0; |
2897 | 0 | rc->resize_count = 0; |
2898 | 0 | rc->resize_buffer_underflow = 0; |
2899 | 0 | } |
2900 | 0 | } |
2901 | | // If decision is to resize, reset some quantities, and check is we should |
2902 | | // reduce rate correction factor, |
2903 | 0 | if (resize_action != NO_RESIZE) { |
2904 | 0 | int resize_width = cpi->oxcf.frm_dim_cfg.width; |
2905 | 0 | int resize_height = cpi->oxcf.frm_dim_cfg.height; |
2906 | 0 | int resize_scale_num = 1; |
2907 | 0 | int resize_scale_den = 1; |
2908 | 0 | if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) { |
2909 | 0 | resize_scale_num = 3; |
2910 | 0 | resize_scale_den = 4; |
2911 | 0 | } else if (resize_action == DOWN_ONEHALF) { |
2912 | 0 | resize_scale_num = 1; |
2913 | 0 | resize_scale_den = 2; |
2914 | 0 | } |
2915 | 0 | resize_width = resize_width * resize_scale_num / resize_scale_den; |
2916 | 0 | resize_height = resize_height * resize_scale_num / resize_scale_den; |
2917 | 0 | resize_reset_rc(cpi, resize_width, resize_height, cm->width, cm->height); |
2918 | 0 | } |
2919 | 0 | return; |
2920 | 0 | } |
2921 | | |
2922 | 0 | static INLINE int set_key_frame(AV1_COMP *cpi, unsigned int frame_flags) { |
2923 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2924 | 0 | AV1_COMMON *const cm = &cpi->common; |
2925 | 0 | SVC *const svc = &cpi->svc; |
2926 | | |
2927 | | // Very first frame has to be key frame. |
2928 | 0 | if (cm->current_frame.frame_number == 0) return 1; |
2929 | | // Set key frame if forced by frame flags. |
2930 | 0 | if (frame_flags & FRAMEFLAGS_KEY) return 1; |
2931 | 0 | if (!cpi->ppi->use_svc) { |
2932 | | // Non-SVC |
2933 | 0 | if (cpi->oxcf.kf_cfg.auto_key && rc->frames_to_key == 0) return 1; |
2934 | 0 | } else { |
2935 | | // SVC |
2936 | 0 | if (svc->spatial_layer_id == 0 && |
2937 | 0 | (cpi->oxcf.kf_cfg.auto_key && |
2938 | 0 | (cpi->oxcf.kf_cfg.key_freq_max == 0 || |
2939 | 0 | svc->current_superframe % cpi->oxcf.kf_cfg.key_freq_max == 0))) |
2940 | 0 | return 1; |
2941 | 0 | } |
2942 | | |
2943 | 0 | return 0; |
2944 | 0 | } |
2945 | | |
2946 | | void av1_get_one_pass_rt_params(AV1_COMP *cpi, |
2947 | | EncodeFrameParams *const frame_params, |
2948 | 0 | unsigned int frame_flags) { |
2949 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2950 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2951 | 0 | AV1_COMMON *const cm = &cpi->common; |
2952 | 0 | GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
2953 | 0 | SVC *const svc = &cpi->svc; |
2954 | 0 | ResizePendingParams *const resize_pending_params = |
2955 | 0 | &cpi->resize_pending_params; |
2956 | 0 | int target; |
2957 | 0 | const int layer = |
2958 | 0 | LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id, |
2959 | 0 | svc->number_temporal_layers); |
2960 | | // Turn this on to explicitly set the reference structure rather than |
2961 | | // relying on internal/default structure. |
2962 | 0 | if (cpi->ppi->use_svc) { |
2963 | 0 | av1_update_temporal_layer_framerate(cpi); |
2964 | 0 | av1_restore_layer_context(cpi); |
2965 | 0 | } |
2966 | | // Set frame type. |
2967 | 0 | if (set_key_frame(cpi, frame_flags)) { |
2968 | 0 | frame_params->frame_type = KEY_FRAME; |
2969 | 0 | p_rc->this_key_frame_forced = |
2970 | 0 | cm->current_frame.frame_number != 0 && rc->frames_to_key == 0; |
2971 | 0 | rc->frames_to_key = cpi->oxcf.kf_cfg.key_freq_max; |
2972 | 0 | p_rc->kf_boost = DEFAULT_KF_BOOST_RT; |
2973 | 0 | gf_group->update_type[cpi->gf_frame_index] = KF_UPDATE; |
2974 | 0 | gf_group->frame_type[cpi->gf_frame_index] = KEY_FRAME; |
2975 | 0 | gf_group->refbuf_state[cpi->gf_frame_index] = REFBUF_RESET; |
2976 | 0 | if (cpi->ppi->use_svc) { |
2977 | 0 | if (cm->current_frame.frame_number > 0) |
2978 | 0 | av1_svc_reset_temporal_layers(cpi, 1); |
2979 | 0 | svc->layer_context[layer].is_key_frame = 1; |
2980 | 0 | } |
2981 | 0 | } else { |
2982 | 0 | frame_params->frame_type = INTER_FRAME; |
2983 | 0 | gf_group->update_type[cpi->gf_frame_index] = LF_UPDATE; |
2984 | 0 | gf_group->frame_type[cpi->gf_frame_index] = INTER_FRAME; |
2985 | 0 | gf_group->refbuf_state[cpi->gf_frame_index] = REFBUF_UPDATE; |
2986 | 0 | if (cpi->ppi->use_svc) { |
2987 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
2988 | 0 | lc->is_key_frame = |
2989 | 0 | svc->spatial_layer_id == 0 |
2990 | 0 | ? 0 |
2991 | 0 | : svc->layer_context[svc->temporal_layer_id].is_key_frame; |
2992 | | // If the user is setting the SVC pattern with set_ref_frame_config and |
2993 | | // did not set any references, set the frame type to Intra-only. |
2994 | 0 | if (svc->set_ref_frame_config) { |
2995 | 0 | int no_references_set = 1; |
2996 | 0 | for (int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
2997 | 0 | if (svc->reference[i]) { |
2998 | 0 | no_references_set = 0; |
2999 | 0 | break; |
3000 | 0 | } |
3001 | 0 | } |
3002 | 0 | if (no_references_set) frame_params->frame_type = INTRA_ONLY_FRAME; |
3003 | 0 | } |
3004 | 0 | } |
3005 | 0 | } |
3006 | | // Check for scene change: for SVC check on base spatial layer only. |
3007 | 0 | if (cpi->sf.rt_sf.check_scene_detection && svc->spatial_layer_id == 0) |
3008 | 0 | rc_scene_detection_onepass_rt(cpi); |
3009 | | // Check for dynamic resize, for single spatial layer for now. |
3010 | | // For temporal layers only check on base temporal layer. |
3011 | 0 | if (cpi->oxcf.resize_cfg.resize_mode == RESIZE_DYNAMIC) { |
3012 | 0 | if (svc->number_spatial_layers == 1 && svc->temporal_layer_id == 0) |
3013 | 0 | dynamic_resize_one_pass_cbr(cpi); |
3014 | 0 | if (rc->resize_state == THREE_QUARTER) { |
3015 | 0 | resize_pending_params->width = (3 + cpi->oxcf.frm_dim_cfg.width * 3) >> 2; |
3016 | 0 | resize_pending_params->height = |
3017 | 0 | (3 + cpi->oxcf.frm_dim_cfg.height * 3) >> 2; |
3018 | 0 | } else if (rc->resize_state == ONE_HALF) { |
3019 | 0 | resize_pending_params->width = (1 + cpi->oxcf.frm_dim_cfg.width) >> 1; |
3020 | 0 | resize_pending_params->height = (1 + cpi->oxcf.frm_dim_cfg.height) >> 1; |
3021 | 0 | } else { |
3022 | 0 | resize_pending_params->width = cpi->oxcf.frm_dim_cfg.width; |
3023 | 0 | resize_pending_params->height = cpi->oxcf.frm_dim_cfg.height; |
3024 | 0 | } |
3025 | 0 | } else if (is_frame_resize_pending(cpi)) { |
3026 | 0 | resize_reset_rc(cpi, resize_pending_params->width, |
3027 | 0 | resize_pending_params->height, cm->width, cm->height); |
3028 | 0 | } |
3029 | | // Set the GF interval and update flag. |
3030 | 0 | if (!rc->rtc_external_ratectrl) |
3031 | 0 | set_gf_interval_update_onepass_rt(cpi, frame_params->frame_type); |
3032 | | // Set target size. |
3033 | 0 | if (cpi->oxcf.rc_cfg.mode == AOM_CBR) { |
3034 | 0 | if (frame_params->frame_type == KEY_FRAME || |
3035 | 0 | frame_params->frame_type == INTRA_ONLY_FRAME) { |
3036 | 0 | target = av1_calc_iframe_target_size_one_pass_cbr(cpi); |
3037 | 0 | } else { |
3038 | 0 | target = av1_calc_pframe_target_size_one_pass_cbr( |
3039 | 0 | cpi, gf_group->update_type[cpi->gf_frame_index]); |
3040 | 0 | } |
3041 | 0 | } else { |
3042 | 0 | if (frame_params->frame_type == KEY_FRAME || |
3043 | 0 | frame_params->frame_type == INTRA_ONLY_FRAME) { |
3044 | 0 | target = av1_calc_iframe_target_size_one_pass_vbr(cpi); |
3045 | 0 | } else { |
3046 | 0 | target = av1_calc_pframe_target_size_one_pass_vbr( |
3047 | 0 | cpi, gf_group->update_type[cpi->gf_frame_index]); |
3048 | 0 | } |
3049 | 0 | } |
3050 | 0 | if (cpi->oxcf.rc_cfg.mode == AOM_Q) |
3051 | 0 | rc->active_worst_quality = cpi->oxcf.rc_cfg.cq_level; |
3052 | |
|
3053 | 0 | av1_rc_set_frame_target(cpi, target, cm->width, cm->height); |
3054 | 0 | rc->base_frame_target = target; |
3055 | 0 | cm->current_frame.frame_type = frame_params->frame_type; |
3056 | | // For fixed mode SVC: if KSVC is enabled remove inter layer |
3057 | | // prediction on spatial enhancement layer frames for frames |
3058 | | // whose base is not KEY frame. |
3059 | 0 | if (cpi->ppi->use_svc && !svc->use_flexible_mode && svc->ksvc_fixed_mode && |
3060 | 0 | svc->number_spatial_layers > 1 && |
3061 | 0 | !svc->layer_context[layer].is_key_frame) { |
3062 | 0 | ExternalFlags *const ext_flags = &cpi->ext_flags; |
3063 | 0 | ext_flags->ref_frame_flags ^= AOM_GOLD_FLAG; |
3064 | 0 | } |
3065 | 0 | } |
3066 | | |
3067 | 0 | int av1_encodedframe_overshoot_cbr(AV1_COMP *cpi, int *q) { |
3068 | 0 | AV1_COMMON *const cm = &cpi->common; |
3069 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
3070 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
3071 | 0 | SPEED_FEATURES *const sf = &cpi->sf; |
3072 | 0 | int thresh_qp = 7 * (rc->worst_quality >> 3); |
3073 | | // Lower thresh_qp for video (more overshoot at lower Q) to be |
3074 | | // more conservative for video. |
3075 | 0 | if (cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN) |
3076 | 0 | thresh_qp = 3 * (rc->worst_quality >> 2); |
3077 | 0 | if (sf->rt_sf.overshoot_detection_cbr == FAST_DETECTION_MAXQ && |
3078 | 0 | cm->quant_params.base_qindex < thresh_qp) { |
3079 | 0 | double rate_correction_factor = |
3080 | 0 | cpi->ppi->p_rc.rate_correction_factors[INTER_NORMAL]; |
3081 | 0 | const int target_size = cpi->rc.avg_frame_bandwidth; |
3082 | 0 | double new_correction_factor; |
3083 | 0 | int target_bits_per_mb; |
3084 | 0 | double q2; |
3085 | 0 | int enumerator; |
3086 | 0 | *q = (3 * cpi->rc.worst_quality + *q) >> 2; |
3087 | | // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as |
3088 | | // these parameters will affect QP selection for subsequent frames. If they |
3089 | | // have settled down to a very different (low QP) state, then not adjusting |
3090 | | // them may cause next frame to select low QP and overshoot again. |
3091 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] = *q; |
3092 | 0 | p_rc->buffer_level = p_rc->optimal_buffer_level; |
3093 | 0 | p_rc->bits_off_target = p_rc->optimal_buffer_level; |
3094 | | // Reset rate under/over-shoot flags. |
3095 | 0 | cpi->rc.rc_1_frame = 0; |
3096 | 0 | cpi->rc.rc_2_frame = 0; |
3097 | | // Adjust rate correction factor. |
3098 | 0 | target_bits_per_mb = |
3099 | 0 | (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->mi_params.MBs); |
3100 | | // Rate correction factor based on target_bits_per_mb and qp (==max_QP). |
3101 | | // This comes from the inverse computation of vp9_rc_bits_per_mb(). |
3102 | 0 | q2 = av1_convert_qindex_to_q(*q, cm->seq_params->bit_depth); |
3103 | 0 | enumerator = 1800000; // Factor for inter frame. |
3104 | 0 | enumerator += (int)(enumerator * q2) >> 12; |
3105 | 0 | new_correction_factor = (double)target_bits_per_mb * q2 / enumerator; |
3106 | 0 | if (new_correction_factor > rate_correction_factor) { |
3107 | 0 | rate_correction_factor = |
3108 | 0 | AOMMIN(2.0 * rate_correction_factor, new_correction_factor); |
3109 | 0 | if (rate_correction_factor > MAX_BPB_FACTOR) |
3110 | 0 | rate_correction_factor = MAX_BPB_FACTOR; |
3111 | 0 | cpi->ppi->p_rc.rate_correction_factors[INTER_NORMAL] = |
3112 | 0 | rate_correction_factor; |
3113 | 0 | } |
3114 | | // For temporal layers: reset the rate control parameters across all |
3115 | | // temporal layers. |
3116 | 0 | if (cpi->svc.number_temporal_layers > 1) { |
3117 | 0 | SVC *svc = &cpi->svc; |
3118 | 0 | for (int tl = 0; tl < svc->number_temporal_layers; ++tl) { |
3119 | 0 | int sl = svc->spatial_layer_id; |
3120 | 0 | const int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers); |
3121 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
3122 | 0 | RATE_CONTROL *lrc = &lc->rc; |
3123 | 0 | PRIMARY_RATE_CONTROL *lp_rc = &lc->p_rc; |
3124 | 0 | lp_rc->avg_frame_qindex[INTER_FRAME] = *q; |
3125 | 0 | lp_rc->buffer_level = lp_rc->optimal_buffer_level; |
3126 | 0 | lp_rc->bits_off_target = lp_rc->optimal_buffer_level; |
3127 | 0 | lrc->rc_1_frame = 0; |
3128 | 0 | lrc->rc_2_frame = 0; |
3129 | 0 | lp_rc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor; |
3130 | 0 | } |
3131 | 0 | } |
3132 | 0 | return 1; |
3133 | 0 | } else { |
3134 | 0 | return 0; |
3135 | 0 | } |
3136 | 0 | } |