/src/libvpx/vp9/encoder/vp9_ratectrl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style license |
5 | | * that can be found in the LICENSE file in the root of the source |
6 | | * tree. An additional intellectual property rights grant can be found |
7 | | * in the file PATENTS. All contributing project authors may |
8 | | * be found in the AUTHORS file in the root of the source tree. |
9 | | */ |
10 | | |
11 | | #include <assert.h> |
12 | | #include <limits.h> |
13 | | #include <math.h> |
14 | | #include <stdint.h> |
15 | | #include <stdio.h> |
16 | | #include <stdlib.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include "./vpx_dsp_rtcd.h" |
20 | | #include "vpx_dsp/vpx_dsp_common.h" |
21 | | #include "vpx_mem/vpx_mem.h" |
22 | | #include "vpx_ports/mem.h" |
23 | | #include "vpx_ports/system_state.h" |
24 | | |
25 | | #include "vp9/common/vp9_alloccommon.h" |
26 | | #include "vp9/common/vp9_blockd.h" |
27 | | #include "vp9/common/vp9_common.h" |
28 | | #include "vp9/common/vp9_entropymode.h" |
29 | | #include "vp9/common/vp9_onyxc_int.h" |
30 | | #include "vp9/common/vp9_quant_common.h" |
31 | | #include "vp9/common/vp9_seg_common.h" |
32 | | |
33 | | #include "vp9/encoder/vp9_aq_cyclicrefresh.h" |
34 | | #include "vp9/encoder/vp9_encodemv.h" |
35 | | #include "vp9/encoder/vp9_encoder.h" |
36 | | #include "vp9/encoder/vp9_ext_ratectrl.h" |
37 | | #include "vp9/encoder/vp9_firstpass.h" |
38 | | #include "vp9/encoder/vp9_ratectrl.h" |
39 | | #include "vp9/encoder/vp9_svc_layercontext.h" |
40 | | |
41 | | #include "vpx/vpx_codec.h" |
42 | | #include "vpx/vpx_ext_ratectrl.h" |
43 | | #include "vpx/internal/vpx_codec_internal.h" |
44 | | |
45 | | // Max rate per frame for 1080P and below encodes if no level requirement given. |
46 | | // For larger formats limit to MAX_MB_RATE bits per MB |
47 | | // 4Mbits is derived from the level requirement for level 4 (1080P 30) which |
48 | | // requires that HW can sustain a rate of 16Mbits over a 4 frame group. |
49 | | // If a lower level requirement is specified then this may over ride this value. |
50 | | #define MAX_MB_RATE 250 |
51 | | #define MAXRATE_1080P 4000000 |
52 | | |
53 | | #define LIMIT_QRANGE_FOR_ALTREF_AND_KEY 1 |
54 | | |
55 | 142k | #define MIN_BPB_FACTOR 0.005 |
56 | 138k | #define MAX_BPB_FACTOR 50 |
57 | | |
58 | | #if CONFIG_VP9_HIGHBITDEPTH |
59 | | #define ASSIGN_MINQ_TABLE(bit_depth, name) \ |
60 | 56.5k | do { \ |
61 | 56.5k | switch (bit_depth) { \ |
62 | 56.5k | case VPX_BITS_8: name = name##_8; break; \ |
63 | 0 | case VPX_BITS_10: name = name##_10; break; \ |
64 | 0 | default: \ |
65 | 0 | assert(bit_depth == VPX_BITS_12); \ |
66 | 0 | name = name##_12; \ |
67 | 0 | break; \ |
68 | 56.5k | } \ |
69 | 56.5k | } while (0) |
70 | | #else |
71 | | #define ASSIGN_MINQ_TABLE(bit_depth, name) \ |
72 | | do { \ |
73 | | (void)bit_depth; \ |
74 | | name = name##_8; \ |
75 | | } while (0) |
76 | | #endif |
77 | | |
78 | | // Tables relating active max Q to active min Q |
79 | | static int kf_low_motion_minq_8[QINDEX_RANGE]; |
80 | | static int kf_high_motion_minq_8[QINDEX_RANGE]; |
81 | | static int arfgf_low_motion_minq_8[QINDEX_RANGE]; |
82 | | static int arfgf_high_motion_minq_8[QINDEX_RANGE]; |
83 | | static int inter_minq_8[QINDEX_RANGE]; |
84 | | static int rtc_minq_8[QINDEX_RANGE]; |
85 | | |
86 | | #if CONFIG_VP9_HIGHBITDEPTH |
87 | | static int kf_low_motion_minq_10[QINDEX_RANGE]; |
88 | | static int kf_high_motion_minq_10[QINDEX_RANGE]; |
89 | | static int arfgf_low_motion_minq_10[QINDEX_RANGE]; |
90 | | static int arfgf_high_motion_minq_10[QINDEX_RANGE]; |
91 | | static int inter_minq_10[QINDEX_RANGE]; |
92 | | static int rtc_minq_10[QINDEX_RANGE]; |
93 | | static int kf_low_motion_minq_12[QINDEX_RANGE]; |
94 | | static int kf_high_motion_minq_12[QINDEX_RANGE]; |
95 | | static int arfgf_low_motion_minq_12[QINDEX_RANGE]; |
96 | | static int arfgf_high_motion_minq_12[QINDEX_RANGE]; |
97 | | static int inter_minq_12[QINDEX_RANGE]; |
98 | | static int rtc_minq_12[QINDEX_RANGE]; |
99 | | #endif |
100 | | |
101 | | #ifdef AGGRESSIVE_VBR |
102 | | static int gf_high = 2400; |
103 | | static int gf_low = 400; |
104 | | static int kf_high = 4000; |
105 | | static int kf_low = 400; |
106 | | #else |
107 | | static int gf_high = 2000; |
108 | | static int gf_low = 400; |
109 | | static int kf_high = 4800; |
110 | | static int kf_low = 300; |
111 | | #endif |
112 | | |
113 | | // Functions to compute the active minq lookup table entries based on a |
114 | | // formulaic approach to facilitate easier adjustment of the Q tables. |
115 | | // The formulae were derived from computing a 3rd order polynomial best |
116 | | // fit to the original data (after plotting real maxq vs minq (not q index)) |
117 | | static int get_minq_index(double maxq, double x3, double x2, double x1, |
118 | 4.60k | vpx_bit_depth_t bit_depth) { |
119 | 4.60k | int i; |
120 | 4.60k | const double minqtarget = VPXMIN(((x3 * maxq + x2) * maxq + x1) * maxq, maxq); |
121 | | |
122 | | // Special case handling to deal with the step from q2.0 |
123 | | // down to lossless mode represented by q 1.0. |
124 | 4.60k | if (minqtarget <= 2.0) return 0; |
125 | | |
126 | 371k | for (i = 0; i < QINDEX_RANGE; i++) { |
127 | 371k | if (minqtarget <= vp9_convert_qindex_to_q(i, bit_depth)) return i; |
128 | 371k | } |
129 | | |
130 | 0 | return QINDEX_RANGE - 1; |
131 | 4.19k | } |
132 | | |
133 | | static void init_minq_luts(int *kf_low_m, int *kf_high_m, int *arfgf_low, |
134 | | int *arfgf_high, int *inter, int *rtc, |
135 | 3 | vpx_bit_depth_t bit_depth) { |
136 | 3 | int i; |
137 | 771 | for (i = 0; i < QINDEX_RANGE; i++) { |
138 | 768 | const double maxq = vp9_convert_qindex_to_q(i, bit_depth); |
139 | 768 | kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth); |
140 | 768 | kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.45, bit_depth); |
141 | | #ifdef AGGRESSIVE_VBR |
142 | | arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.275, bit_depth); |
143 | | inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.80, bit_depth); |
144 | | #else |
145 | 768 | arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth); |
146 | 768 | inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); |
147 | 768 | #endif |
148 | 768 | arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); |
149 | 768 | rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); |
150 | 768 | } |
151 | 3 | } |
152 | | |
153 | 1 | void vp9_rc_init_minq_luts(void) { |
154 | 1 | init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8, |
155 | 1 | arfgf_low_motion_minq_8, arfgf_high_motion_minq_8, |
156 | 1 | inter_minq_8, rtc_minq_8, VPX_BITS_8); |
157 | 1 | #if CONFIG_VP9_HIGHBITDEPTH |
158 | 1 | init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10, |
159 | 1 | arfgf_low_motion_minq_10, arfgf_high_motion_minq_10, |
160 | 1 | inter_minq_10, rtc_minq_10, VPX_BITS_10); |
161 | 1 | init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12, |
162 | 1 | arfgf_low_motion_minq_12, arfgf_high_motion_minq_12, |
163 | 1 | inter_minq_12, rtc_minq_12, VPX_BITS_12); |
164 | 1 | #endif |
165 | 1 | } |
166 | | |
167 | | // These functions use formulaic calculations to make playing with the |
168 | | // quantizer tables easier. If necessary they can be replaced by lookup |
169 | | // tables if and when things settle down in the experimental bitstream |
170 | 3.32M | double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth) { |
171 | | // Convert the index to a real Q value (scaled down to match old Q values) |
172 | 3.32M | #if CONFIG_VP9_HIGHBITDEPTH |
173 | 3.32M | switch (bit_depth) { |
174 | 3.07M | case VPX_BITS_8: return vp9_ac_quant(qindex, 0, bit_depth) / 4.0; |
175 | 125k | case VPX_BITS_10: return vp9_ac_quant(qindex, 0, bit_depth) / 16.0; |
176 | 126k | default: |
177 | 126k | assert(bit_depth == VPX_BITS_12); |
178 | 126k | return vp9_ac_quant(qindex, 0, bit_depth) / 64.0; |
179 | 3.32M | } |
180 | | #else |
181 | | return vp9_ac_quant(qindex, 0, bit_depth) / 4.0; |
182 | | #endif |
183 | 3.32M | } |
184 | | |
185 | 0 | int vp9_convert_q_to_qindex(double q_val, vpx_bit_depth_t bit_depth) { |
186 | 0 | int i; |
187 | |
|
188 | 0 | for (i = 0; i < QINDEX_RANGE; ++i) |
189 | 0 | if (vp9_convert_qindex_to_q(i, bit_depth) >= q_val) break; |
190 | |
|
191 | 0 | if (i == QINDEX_RANGE) i--; |
192 | |
|
193 | 0 | return i; |
194 | 0 | } |
195 | | |
196 | | int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
197 | 1.92M | double correction_factor, vpx_bit_depth_t bit_depth) { |
198 | 1.92M | const double q = vp9_convert_qindex_to_q(qindex, bit_depth); |
199 | 1.92M | int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000; |
200 | | |
201 | 1.92M | assert(correction_factor <= MAX_BPB_FACTOR && |
202 | 1.92M | correction_factor >= MIN_BPB_FACTOR); |
203 | | |
204 | | // q based adjustment to baseline enumerator |
205 | 1.92M | enumerator += (int)(enumerator * q) >> 12; |
206 | 1.92M | return (int)(enumerator * correction_factor / q); |
207 | 1.92M | } |
208 | | |
209 | | int vp9_estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, |
210 | | double correction_factor, |
211 | 42.2k | vpx_bit_depth_t bit_depth) { |
212 | 42.2k | const int bpm = |
213 | 42.2k | (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, bit_depth)); |
214 | 42.2k | return VPXMAX(FRAME_OVERHEAD_BITS, |
215 | 42.2k | (int)(((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS)); |
216 | 42.2k | } |
217 | | |
218 | 35.7k | int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { |
219 | 35.7k | const RATE_CONTROL *rc = &cpi->rc; |
220 | 35.7k | const VP9EncoderConfig *oxcf = &cpi->oxcf; |
221 | | |
222 | 35.7k | const int min_frame_target = |
223 | 35.7k | VPXMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5); |
224 | 35.7k | if (target < min_frame_target) target = min_frame_target; |
225 | 35.7k | if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { |
226 | | // If there is an active ARF at this location use the minimum |
227 | | // bits on this frame even if it is a constructed arf. |
228 | | // The active maximum quantizer insures that an appropriate |
229 | | // number of bits will be spent if needed for constructed ARFs. |
230 | 0 | target = min_frame_target; |
231 | 0 | } |
232 | | |
233 | | // Clip the frame target to the maximum allowed value. |
234 | 35.7k | if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; |
235 | | |
236 | 35.7k | if (oxcf->rc_max_inter_bitrate_pct) { |
237 | 0 | const int64_t max_rate = |
238 | 0 | (int64_t)rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100; |
239 | | // target is of type int and VPXMIN cannot evaluate to larger than target |
240 | 0 | target = (int)VPXMIN(target, max_rate); |
241 | 0 | } |
242 | 35.7k | return target; |
243 | 35.7k | } |
244 | | |
245 | 7.75k | int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { |
246 | 7.75k | const RATE_CONTROL *rc = &cpi->rc; |
247 | 7.75k | const VP9EncoderConfig *oxcf = &cpi->oxcf; |
248 | 7.75k | if (oxcf->rc_max_intra_bitrate_pct) { |
249 | 0 | const int64_t max_rate = |
250 | 0 | (int64_t)rc->avg_frame_bandwidth * oxcf->rc_max_intra_bitrate_pct / 100; |
251 | 0 | target = (int)VPXMIN(target, max_rate); |
252 | 0 | } |
253 | 7.75k | if (target > rc->max_frame_bandwidth) target = rc->max_frame_bandwidth; |
254 | 7.75k | return target; |
255 | 7.75k | } |
256 | | |
257 | | // TODO(marpan/jianj): bits_off_target and buffer_level are used in the same |
258 | | // way for CBR mode, for the buffering updates below. Look into removing one |
259 | | // of these (i.e., bits_off_target). |
260 | | // Update the buffer level before encoding with the per-frame-bandwidth, |
261 | 0 | void vp9_update_buffer_level_preencode(VP9_COMP *cpi) { |
262 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
263 | 0 | rc->bits_off_target += rc->avg_frame_bandwidth; |
264 | | // Clip the buffer level to the maximum specified buffer size. |
265 | 0 | rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); |
266 | 0 | rc->buffer_level = rc->bits_off_target; |
267 | 0 | } |
268 | | |
269 | | // Update the buffer level before encoding with the per-frame-bandwidth |
270 | | // for SVC. The current and all upper temporal layers are updated, needed |
271 | | // for the layered rate control which involves cumulative buffer levels for |
272 | | // the temporal layers. Allow for using the timestamp(pts) delta for the |
273 | | // framerate when the set_ref_frame_config is used. |
274 | 0 | void vp9_update_buffer_level_svc_preencode(VP9_COMP *cpi) { |
275 | 0 | SVC *const svc = &cpi->svc; |
276 | 0 | int i; |
277 | | // Set this to 1 to use timestamp delta for "framerate" under |
278 | | // ref_frame_config usage. |
279 | 0 | int use_timestamp = 1; |
280 | 0 | const int64_t ts_delta = |
281 | 0 | svc->time_stamp_superframe - svc->time_stamp_prev[svc->spatial_layer_id]; |
282 | 0 | for (i = svc->temporal_layer_id; i < svc->number_temporal_layers; ++i) { |
283 | 0 | const int layer = |
284 | 0 | LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers); |
285 | 0 | LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
286 | 0 | RATE_CONTROL *const lrc = &lc->rc; |
287 | 0 | if (use_timestamp && cpi->svc.use_set_ref_frame_config && |
288 | 0 | svc->number_temporal_layers == 1 && ts_delta > 0 && |
289 | 0 | svc->current_superframe > 0) { |
290 | | // TODO(marpan): This may need to be modified for temporal layers. |
291 | 0 | const double framerate_pts = 10000000.0 / ts_delta; |
292 | 0 | lrc->bits_off_target += saturate_cast_double_to_int( |
293 | 0 | round(lc->target_bandwidth / framerate_pts)); |
294 | 0 | } else { |
295 | 0 | lrc->bits_off_target += saturate_cast_double_to_int( |
296 | 0 | round(lc->target_bandwidth / lc->framerate)); |
297 | 0 | } |
298 | | // Clip buffer level to maximum buffer size for the layer. |
299 | 0 | lrc->bits_off_target = |
300 | 0 | VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size); |
301 | 0 | lrc->buffer_level = lrc->bits_off_target; |
302 | 0 | if (i == svc->temporal_layer_id) { |
303 | 0 | cpi->rc.bits_off_target = lrc->bits_off_target; |
304 | 0 | cpi->rc.buffer_level = lrc->buffer_level; |
305 | 0 | } |
306 | 0 | } |
307 | 0 | } |
308 | | |
309 | | // Update the buffer level for higher temporal layers, given the encoded current |
310 | | // temporal layer. |
311 | | static void update_layer_buffer_level_postencode(SVC *svc, |
312 | 0 | int encoded_frame_size) { |
313 | 0 | int i = 0; |
314 | 0 | const int current_temporal_layer = svc->temporal_layer_id; |
315 | 0 | for (i = current_temporal_layer + 1; i < svc->number_temporal_layers; ++i) { |
316 | 0 | const int layer = |
317 | 0 | LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, svc->number_temporal_layers); |
318 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
319 | 0 | RATE_CONTROL *lrc = &lc->rc; |
320 | 0 | lrc->bits_off_target -= encoded_frame_size; |
321 | | // Clip buffer level to maximum buffer size for the layer. |
322 | 0 | lrc->bits_off_target = |
323 | 0 | VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size); |
324 | 0 | lrc->buffer_level = lrc->bits_off_target; |
325 | 0 | } |
326 | 0 | } |
327 | | |
328 | | // Update the buffer level after encoding with encoded frame size. |
329 | | static void update_buffer_level_postencode(VP9_COMP *cpi, |
330 | 42.2k | int encoded_frame_size) { |
331 | 42.2k | RATE_CONTROL *const rc = &cpi->rc; |
332 | 42.2k | rc->bits_off_target -= encoded_frame_size; |
333 | | // Clip the buffer level to the maximum specified buffer size. |
334 | 42.2k | rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size); |
335 | | // For screen-content mode, and if frame-dropper is off, don't let buffer |
336 | | // level go below threshold, given here as -rc->maximum_ buffer_size. |
337 | 42.2k | if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && |
338 | 42.2k | cpi->oxcf.drop_frames_water_mark == 0) |
339 | 0 | rc->bits_off_target = VPXMAX(rc->bits_off_target, -rc->maximum_buffer_size); |
340 | | |
341 | 42.2k | rc->buffer_level = rc->bits_off_target; |
342 | | |
343 | 42.2k | if (is_one_pass_svc(cpi)) { |
344 | 0 | update_layer_buffer_level_postencode(&cpi->svc, encoded_frame_size); |
345 | 0 | } |
346 | 42.2k | } |
347 | | |
348 | | int vp9_rc_get_default_min_gf_interval(int width, int height, |
349 | 67.5k | double framerate) { |
350 | | // Assume we do not need any constraint lower than 4K 20 fps |
351 | 67.5k | static const double factor_safe = 3840 * 2160 * 20.0; |
352 | 67.5k | const double factor = width * height * framerate; |
353 | 67.5k | const int default_interval = |
354 | 67.5k | clamp((int)round(framerate * 0.125), MIN_GF_INTERVAL, MAX_GF_INTERVAL); |
355 | | |
356 | 67.5k | if (factor <= factor_safe) |
357 | 66.0k | return default_interval; |
358 | 1.45k | else |
359 | 1.45k | return VPXMAX(default_interval, |
360 | 67.5k | (int)round(MIN_GF_INTERVAL * factor / factor_safe)); |
361 | | // Note this logic makes: |
362 | | // 4K24: 5 |
363 | | // 4K30: 6 |
364 | | // 4K60: 12 |
365 | 67.5k | } |
366 | | |
367 | 67.5k | int vp9_rc_get_default_max_gf_interval(double framerate, int min_gf_interval) { |
368 | 67.5k | int interval = VPXMIN(MAX_GF_INTERVAL, (int)round(framerate * 0.75)); |
369 | 67.5k | interval += (interval & 0x01); // Round to even value |
370 | 67.5k | return VPXMAX(interval, min_gf_interval); |
371 | 67.5k | } |
372 | | |
373 | 2.98k | void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { |
374 | 2.98k | int i; |
375 | | |
376 | 2.98k | if (pass == 0 && oxcf->rc_mode == VPX_CBR) { |
377 | 0 | rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; |
378 | 0 | rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
379 | 2.98k | } else { |
380 | 2.98k | rc->avg_frame_qindex[KEY_FRAME] = |
381 | 2.98k | (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; |
382 | 2.98k | rc->avg_frame_qindex[INTER_FRAME] = |
383 | 2.98k | (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2; |
384 | 2.98k | } |
385 | | |
386 | 2.98k | rc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
387 | 2.98k | rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
388 | | |
389 | 2.98k | rc->buffer_level = rc->starting_buffer_level; |
390 | 2.98k | rc->bits_off_target = rc->starting_buffer_level; |
391 | | |
392 | 2.98k | rc->rolling_target_bits = rc->avg_frame_bandwidth; |
393 | 2.98k | rc->rolling_actual_bits = rc->avg_frame_bandwidth; |
394 | 2.98k | rc->long_rolling_target_bits = rc->avg_frame_bandwidth; |
395 | 2.98k | rc->long_rolling_actual_bits = rc->avg_frame_bandwidth; |
396 | | |
397 | 2.98k | rc->total_actual_bits = 0; |
398 | 2.98k | rc->total_target_bits = 0; |
399 | 2.98k | rc->total_target_vs_actual = 0; |
400 | 2.98k | rc->avg_frame_low_motion = 0; |
401 | 2.98k | rc->count_last_scene_change = 0; |
402 | 2.98k | rc->af_ratio_onepass_vbr = 10; |
403 | 2.98k | rc->prev_avg_source_sad_lag = 0; |
404 | 2.98k | rc->high_source_sad = 0; |
405 | 2.98k | rc->reset_high_source_sad = 0; |
406 | 2.98k | rc->high_source_sad_lagindex = -1; |
407 | 2.98k | rc->high_num_blocks_with_motion = 0; |
408 | 2.98k | rc->hybrid_intra_scene_change = 0; |
409 | 2.98k | rc->re_encode_maxq_scene_change = 0; |
410 | 2.98k | rc->alt_ref_gf_group = 0; |
411 | 2.98k | rc->last_frame_is_src_altref = 0; |
412 | 2.98k | rc->fac_active_worst_inter = 150; |
413 | 2.98k | rc->fac_active_worst_gf = 100; |
414 | 2.98k | rc->force_qpmin = 0; |
415 | 77.5k | for (i = 0; i < MAX_LAG_BUFFERS; ++i) rc->avg_source_sad[i] = 0; |
416 | 2.98k | rc->frames_to_key = 0; |
417 | 2.98k | rc->frames_since_key = 8; // Sensible default for first frame. |
418 | 2.98k | rc->this_key_frame_forced = 0; |
419 | 2.98k | rc->next_key_frame_forced = 0; |
420 | 2.98k | rc->source_alt_ref_pending = 0; |
421 | 2.98k | rc->source_alt_ref_active = 0; |
422 | | |
423 | 2.98k | rc->frames_till_gf_update_due = 0; |
424 | 2.98k | rc->constrain_gf_key_freq_onepass_vbr = 1; |
425 | 2.98k | rc->ni_av_qi = oxcf->worst_allowed_q; |
426 | 2.98k | rc->ni_tot_qi = 0; |
427 | 2.98k | rc->ni_frames = 0; |
428 | | |
429 | 2.98k | rc->tot_q = 0.0; |
430 | 2.98k | rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth); |
431 | | |
432 | 17.8k | for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
433 | 14.9k | rc->rate_correction_factors[i] = 1.0; |
434 | 14.9k | rc->damped_adjustment[i] = 0; |
435 | 14.9k | } |
436 | | |
437 | 2.98k | rc->min_gf_interval = oxcf->min_gf_interval; |
438 | 2.98k | rc->max_gf_interval = oxcf->max_gf_interval; |
439 | 2.98k | if (rc->min_gf_interval == 0) |
440 | 2.98k | rc->min_gf_interval = vp9_rc_get_default_min_gf_interval( |
441 | 2.98k | oxcf->width, oxcf->height, oxcf->init_framerate); |
442 | 2.98k | if (rc->max_gf_interval == 0) |
443 | 2.98k | rc->max_gf_interval = vp9_rc_get_default_max_gf_interval( |
444 | 2.98k | oxcf->init_framerate, rc->min_gf_interval); |
445 | 2.98k | rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2; |
446 | 2.98k | if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) { |
447 | 96 | rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL; |
448 | 2.88k | } else { |
449 | 2.88k | rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH; |
450 | 2.88k | } |
451 | | |
452 | 2.98k | rc->force_max_q = 0; |
453 | 2.98k | rc->last_post_encode_dropped_scene_change = 0; |
454 | 2.98k | rc->use_post_encode_drop = 0; |
455 | 2.98k | rc->ext_use_post_encode_drop = 0; |
456 | 2.98k | rc->disable_overshoot_maxq_cbr = 0; |
457 | 2.98k | rc->arf_active_best_quality_adjustment_factor = 1.0; |
458 | 2.98k | rc->arf_increase_active_best_quality = 0; |
459 | 2.98k | rc->preserve_arf_as_gld = 0; |
460 | 2.98k | rc->preserve_next_arf_as_gld = 0; |
461 | 2.98k | rc->show_arf_as_gld = 0; |
462 | 2.98k | } |
463 | | |
464 | 0 | static int check_buffer_above_thresh(VP9_COMP *cpi, int drop_mark) { |
465 | 0 | SVC *svc = &cpi->svc; |
466 | 0 | if (!cpi->use_svc || cpi->svc.framedrop_mode != FULL_SUPERFRAME_DROP) { |
467 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
468 | 0 | return (rc->buffer_level > drop_mark); |
469 | 0 | } else { |
470 | 0 | int i; |
471 | | // For SVC in the FULL_SUPERFRAME_DROP): the condition on |
472 | | // buffer (if its above threshold, so no drop) is checked on current and |
473 | | // upper spatial layers. If any spatial layer is not above threshold then |
474 | | // we return 0. |
475 | 0 | for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) { |
476 | 0 | const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, |
477 | 0 | svc->number_temporal_layers); |
478 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
479 | 0 | RATE_CONTROL *lrc = &lc->rc; |
480 | | // Exclude check for layer whose bitrate is 0. |
481 | 0 | if (lc->target_bandwidth > 0) { |
482 | 0 | const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] * |
483 | 0 | lrc->optimal_buffer_level / 100); |
484 | 0 | if (!(lrc->buffer_level > drop_mark_layer)) return 0; |
485 | 0 | } |
486 | 0 | } |
487 | 0 | return 1; |
488 | 0 | } |
489 | 0 | } |
490 | | |
491 | 0 | static int check_buffer_below_thresh(VP9_COMP *cpi, int drop_mark) { |
492 | 0 | SVC *svc = &cpi->svc; |
493 | 0 | if (!cpi->use_svc || cpi->svc.framedrop_mode == LAYER_DROP) { |
494 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
495 | 0 | return (rc->buffer_level <= drop_mark); |
496 | 0 | } else { |
497 | 0 | int i; |
498 | | // For SVC in the constrained framedrop mode (svc->framedrop_mode = |
499 | | // CONSTRAINED_LAYER_DROP or FULL_SUPERFRAME_DROP): the condition on |
500 | | // buffer (if its below threshold, so drop frame) is checked on current |
501 | | // and upper spatial layers. For FULL_SUPERFRAME_DROP mode if any |
502 | | // spatial layer is <= threshold, then we return 1 (drop). |
503 | 0 | for (i = svc->spatial_layer_id; i < svc->number_spatial_layers; ++i) { |
504 | 0 | const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, |
505 | 0 | svc->number_temporal_layers); |
506 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
507 | 0 | RATE_CONTROL *lrc = &lc->rc; |
508 | | // Exclude check for layer whose bitrate is 0. |
509 | 0 | if (lc->target_bandwidth > 0) { |
510 | 0 | const int drop_mark_layer = (int)(cpi->svc.framedrop_thresh[i] * |
511 | 0 | lrc->optimal_buffer_level / 100); |
512 | 0 | if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP) { |
513 | 0 | if (lrc->buffer_level <= drop_mark_layer) return 1; |
514 | 0 | } else { |
515 | 0 | if (!(lrc->buffer_level <= drop_mark_layer)) return 0; |
516 | 0 | } |
517 | 0 | } |
518 | 0 | } |
519 | 0 | if (cpi->svc.framedrop_mode == FULL_SUPERFRAME_DROP) |
520 | 0 | return 0; |
521 | 0 | else |
522 | 0 | return 1; |
523 | 0 | } |
524 | 0 | } |
525 | | |
526 | 0 | int vp9_test_drop(VP9_COMP *cpi) { |
527 | 0 | const VP9EncoderConfig *oxcf = &cpi->oxcf; |
528 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
529 | 0 | SVC *svc = &cpi->svc; |
530 | 0 | int drop_frames_water_mark = oxcf->drop_frames_water_mark; |
531 | 0 | if (cpi->use_svc) { |
532 | | // If we have dropped max_consec_drop frames, then we don't |
533 | | // drop this spatial layer, and reset counter to 0. |
534 | 0 | if (svc->drop_count[svc->spatial_layer_id] == svc->max_consec_drop) { |
535 | 0 | svc->drop_count[svc->spatial_layer_id] = 0; |
536 | 0 | return 0; |
537 | 0 | } else { |
538 | 0 | drop_frames_water_mark = svc->framedrop_thresh[svc->spatial_layer_id]; |
539 | 0 | } |
540 | 0 | } |
541 | 0 | if (!drop_frames_water_mark || |
542 | 0 | (svc->spatial_layer_id > 0 && |
543 | 0 | svc->framedrop_mode == FULL_SUPERFRAME_DROP)) { |
544 | 0 | return 0; |
545 | 0 | } else { |
546 | 0 | if ((rc->buffer_level < 0 && svc->framedrop_mode != FULL_SUPERFRAME_DROP) || |
547 | 0 | (check_buffer_below_thresh(cpi, -1) && |
548 | 0 | svc->framedrop_mode == FULL_SUPERFRAME_DROP)) { |
549 | | // Always drop if buffer is below 0. |
550 | 0 | return 1; |
551 | 0 | } else { |
552 | | // If buffer is below drop_mark, for now just drop every other frame |
553 | | // (starting with the next frame) until it increases back over drop_mark. |
554 | 0 | int drop_mark = |
555 | 0 | (int)(drop_frames_water_mark * rc->optimal_buffer_level / 100); |
556 | 0 | if (check_buffer_above_thresh(cpi, drop_mark) && |
557 | 0 | (rc->decimation_factor > 0)) { |
558 | 0 | --rc->decimation_factor; |
559 | 0 | } else if (check_buffer_below_thresh(cpi, drop_mark) && |
560 | 0 | rc->decimation_factor == 0) { |
561 | 0 | rc->decimation_factor = 1; |
562 | 0 | } |
563 | 0 | if (rc->decimation_factor > 0) { |
564 | 0 | if (rc->decimation_count > 0) { |
565 | 0 | --rc->decimation_count; |
566 | 0 | return 1; |
567 | 0 | } else { |
568 | 0 | rc->decimation_count = rc->decimation_factor; |
569 | 0 | return 0; |
570 | 0 | } |
571 | 0 | } else { |
572 | 0 | rc->decimation_count = 0; |
573 | 0 | return 0; |
574 | 0 | } |
575 | 0 | } |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | 0 | int post_encode_drop_cbr(VP9_COMP *cpi, size_t *size) { |
580 | 0 | size_t frame_size = *size << 3; |
581 | 0 | int64_t new_buffer_level = |
582 | 0 | cpi->rc.buffer_level + cpi->rc.avg_frame_bandwidth - (int64_t)frame_size; |
583 | | |
584 | | // For now we drop if new buffer level (given the encoded frame size) goes |
585 | | // below 0. |
586 | 0 | if (new_buffer_level < 0) { |
587 | 0 | *size = 0; |
588 | 0 | vp9_rc_postencode_update_drop_frame(cpi); |
589 | | // Update flag to use for next frame. |
590 | 0 | if (cpi->rc.high_source_sad || |
591 | 0 | (cpi->use_svc && cpi->svc.high_source_sad_superframe)) |
592 | 0 | cpi->rc.last_post_encode_dropped_scene_change = 1; |
593 | | // Force max_q on next fame. |
594 | 0 | cpi->rc.force_max_q = 1; |
595 | 0 | cpi->rc.avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality; |
596 | 0 | cpi->last_frame_dropped = 1; |
597 | 0 | cpi->ext_refresh_frame_flags_pending = 0; |
598 | 0 | if (cpi->use_svc) { |
599 | 0 | SVC *svc = &cpi->svc; |
600 | 0 | int sl = 0; |
601 | 0 | int tl = 0; |
602 | 0 | svc->last_layer_dropped[svc->spatial_layer_id] = 1; |
603 | 0 | svc->drop_spatial_layer[svc->spatial_layer_id] = 1; |
604 | 0 | svc->drop_count[svc->spatial_layer_id]++; |
605 | 0 | svc->skip_enhancement_layer = 1; |
606 | | // Postencode drop is only checked on base spatial layer, |
607 | | // for now if max-q is set on base we force it on all layers. |
608 | 0 | for (sl = 0; sl < svc->number_spatial_layers; ++sl) { |
609 | 0 | for (tl = 0; tl < svc->number_temporal_layers; ++tl) { |
610 | 0 | const int layer = |
611 | 0 | LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers); |
612 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
613 | 0 | RATE_CONTROL *lrc = &lc->rc; |
614 | 0 | lrc->force_max_q = 1; |
615 | 0 | lrc->avg_frame_qindex[INTER_FRAME] = cpi->rc.worst_quality; |
616 | 0 | } |
617 | 0 | } |
618 | 0 | } |
619 | 0 | return 1; |
620 | 0 | } |
621 | | |
622 | 0 | cpi->rc.force_max_q = 0; |
623 | 0 | cpi->rc.last_post_encode_dropped_scene_change = 0; |
624 | 0 | return 0; |
625 | 0 | } |
626 | | |
627 | 0 | int vp9_rc_drop_frame(VP9_COMP *cpi) { |
628 | 0 | SVC *svc = &cpi->svc; |
629 | 0 | int svc_prev_layer_dropped = 0; |
630 | | // In the constrained or full_superframe framedrop mode for svc |
631 | | // (framedrop_mode != (LAYER_DROP && CONSTRAINED_FROM_ABOVE)), |
632 | | // if the previous spatial layer was dropped, drop the current spatial layer. |
633 | 0 | if (cpi->use_svc && svc->spatial_layer_id > 0 && |
634 | 0 | svc->drop_spatial_layer[svc->spatial_layer_id - 1]) |
635 | 0 | svc_prev_layer_dropped = 1; |
636 | 0 | if ((svc_prev_layer_dropped && svc->framedrop_mode != LAYER_DROP && |
637 | 0 | svc->framedrop_mode != CONSTRAINED_FROM_ABOVE_DROP) || |
638 | 0 | svc->force_drop_constrained_from_above[svc->spatial_layer_id] || |
639 | 0 | vp9_test_drop(cpi)) { |
640 | 0 | vp9_rc_postencode_update_drop_frame(cpi); |
641 | 0 | cpi->ext_refresh_frame_flags_pending = 0; |
642 | 0 | cpi->last_frame_dropped = 1; |
643 | 0 | if (cpi->use_svc) { |
644 | 0 | svc->last_layer_dropped[svc->spatial_layer_id] = 1; |
645 | 0 | svc->drop_spatial_layer[svc->spatial_layer_id] = 1; |
646 | 0 | svc->drop_count[svc->spatial_layer_id]++; |
647 | 0 | svc->skip_enhancement_layer = 1; |
648 | 0 | if (svc->framedrop_mode == LAYER_DROP || |
649 | 0 | (svc->framedrop_mode == CONSTRAINED_FROM_ABOVE_DROP && |
650 | 0 | svc->force_drop_constrained_from_above[svc->number_spatial_layers - |
651 | 0 | 1] == 0) || |
652 | 0 | svc->drop_spatial_layer[0] == 0) { |
653 | | // For the case of constrained drop mode where full superframe is |
654 | | // dropped, we don't increment the svc frame counters. |
655 | | // In particular temporal layer counter (which is incremented in |
656 | | // vp9_inc_frame_in_layer()) won't be incremented, so on a dropped |
657 | | // frame we try the same temporal_layer_id on next incoming frame. |
658 | | // This is to avoid an issue with temporal alignment with full |
659 | | // superframe dropping. |
660 | 0 | vp9_inc_frame_in_layer(cpi); |
661 | 0 | } |
662 | 0 | if (svc->spatial_layer_id == svc->number_spatial_layers - 1) { |
663 | 0 | int i; |
664 | 0 | int all_layers_drop = 1; |
665 | 0 | for (i = 0; i < svc->spatial_layer_id; i++) { |
666 | 0 | if (svc->drop_spatial_layer[i] == 0) { |
667 | 0 | all_layers_drop = 0; |
668 | 0 | break; |
669 | 0 | } |
670 | 0 | } |
671 | 0 | if (all_layers_drop == 1) svc->skip_enhancement_layer = 0; |
672 | 0 | } |
673 | 0 | } |
674 | 0 | return 1; |
675 | 0 | } |
676 | 0 | return 0; |
677 | 0 | } |
678 | | |
679 | 0 | static int adjust_q_cbr(const VP9_COMP *cpi, int q) { |
680 | | // This makes sure q is between oscillating Qs to prevent resonance. |
681 | 0 | if (!cpi->rc.reset_high_source_sad && |
682 | 0 | (!cpi->oxcf.gf_cbr_boost_pct || |
683 | 0 | !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) && |
684 | 0 | (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) && |
685 | 0 | cpi->rc.q_1_frame != cpi->rc.q_2_frame) { |
686 | 0 | int qclamp = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame), |
687 | 0 | VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame)); |
688 | | // If the previous frame had overshoot and the current q needs to increase |
689 | | // above the clamped value, reduce the clamp for faster reaction to |
690 | | // overshoot. |
691 | 0 | if (cpi->rc.rc_1_frame == -1 && q > qclamp) |
692 | 0 | q = (q + qclamp) >> 1; |
693 | 0 | else |
694 | 0 | q = qclamp; |
695 | 0 | } |
696 | 0 | if (cpi->oxcf.content == VP9E_CONTENT_SCREEN && |
697 | 0 | cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
698 | 0 | vp9_cyclic_refresh_limit_q(cpi, &q); |
699 | 0 | return VPXMAX(VPXMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality); |
700 | 0 | } |
701 | | |
702 | 81.0k | static double get_rate_correction_factor(const VP9_COMP *cpi) { |
703 | 81.0k | const RATE_CONTROL *const rc = &cpi->rc; |
704 | 81.0k | const VP9_COMMON *const cm = &cpi->common; |
705 | 81.0k | double rcf; |
706 | | |
707 | 81.0k | if (frame_is_intra_only(cm)) { |
708 | 12.4k | rcf = rc->rate_correction_factors[KF_STD]; |
709 | 68.5k | } else if (cpi->oxcf.pass == 2) { |
710 | 0 | RATE_FACTOR_LEVEL rf_lvl = |
711 | 0 | cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
712 | 0 | rcf = rc->rate_correction_factors[rf_lvl]; |
713 | 68.5k | } else { |
714 | 68.5k | if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
715 | 68.5k | !rc->is_src_frame_alt_ref && !cpi->use_svc && |
716 | 68.5k | (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100)) |
717 | 4.74k | rcf = rc->rate_correction_factors[GF_ARF_STD]; |
718 | 63.8k | else |
719 | 63.8k | rcf = rc->rate_correction_factors[INTER_NORMAL]; |
720 | 68.5k | } |
721 | 81.0k | rcf *= rcf_mult[rc->frame_size_selector]; |
722 | 81.0k | return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
723 | 81.0k | } |
724 | | |
725 | 42.2k | static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { |
726 | 42.2k | RATE_CONTROL *const rc = &cpi->rc; |
727 | 42.2k | const VP9_COMMON *const cm = &cpi->common; |
728 | | |
729 | | // Normalize RCF to account for the size-dependent scaling factor. |
730 | 42.2k | factor /= rcf_mult[cpi->rc.frame_size_selector]; |
731 | | |
732 | 42.2k | factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
733 | | |
734 | 42.2k | if (frame_is_intra_only(cm)) { |
735 | 7.70k | rc->rate_correction_factors[KF_STD] = factor; |
736 | 34.4k | } else if (cpi->oxcf.pass == 2) { |
737 | 0 | RATE_FACTOR_LEVEL rf_lvl = |
738 | 0 | cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
739 | 0 | rc->rate_correction_factors[rf_lvl] = factor; |
740 | 34.4k | } else { |
741 | 34.4k | if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
742 | 34.4k | !rc->is_src_frame_alt_ref && !cpi->use_svc && |
743 | 34.4k | (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 100)) |
744 | 2.38k | rc->rate_correction_factors[GF_ARF_STD] = factor; |
745 | 32.1k | else |
746 | 32.1k | rc->rate_correction_factors[INTER_NORMAL] = factor; |
747 | 34.4k | } |
748 | 42.2k | } |
749 | | |
750 | 42.2k | void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) { |
751 | 42.2k | const VP9_COMMON *const cm = &cpi->common; |
752 | 42.2k | int correction_factor = 100; |
753 | 42.2k | double rate_correction_factor = get_rate_correction_factor(cpi); |
754 | 42.2k | double adjustment_limit; |
755 | 42.2k | RATE_FACTOR_LEVEL rf_lvl = |
756 | 42.2k | cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
757 | | |
758 | 42.2k | int projected_size_based_on_q = 0; |
759 | | |
760 | | // Do not update the rate factors for arf overlay frames. |
761 | 42.2k | if (cpi->rc.is_src_frame_alt_ref) return; |
762 | | |
763 | | // Clear down mmx registers to allow floating point in what follows |
764 | 42.2k | vpx_clear_system_state(); |
765 | | |
766 | | // Work out how big we would have expected the frame to be at this Q given |
767 | | // the current correction factor. |
768 | | // Stay in double to avoid int overflow when values are large |
769 | 42.2k | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->common.seg.enabled) { |
770 | 0 | projected_size_based_on_q = |
771 | 0 | vp9_cyclic_refresh_estimate_bits_at_q(cpi, rate_correction_factor); |
772 | 42.2k | } else { |
773 | 42.2k | FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type; |
774 | 42.2k | projected_size_based_on_q = |
775 | 42.2k | vp9_estimate_bits_at_q(frame_type, cm->base_qindex, cm->MBs, |
776 | 42.2k | rate_correction_factor, cm->bit_depth); |
777 | 42.2k | } |
778 | | // Work out a size correction factor. |
779 | 42.2k | if (projected_size_based_on_q > FRAME_OVERHEAD_BITS) |
780 | 36.1k | correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) / |
781 | 36.1k | projected_size_based_on_q); |
782 | | |
783 | | // Do not use damped adjustment for the first frame of each frame type |
784 | 42.2k | if (!cpi->rc.damped_adjustment[rf_lvl]) { |
785 | 2.87k | adjustment_limit = 1.0; |
786 | 2.87k | cpi->rc.damped_adjustment[rf_lvl] = 1; |
787 | 39.3k | } else { |
788 | | // More heavily damped adjustment used if we have been oscillating either |
789 | | // side of target. |
790 | 39.3k | adjustment_limit = |
791 | 39.3k | 0.25 + 0.5 * VPXMIN(1, fabs(log10(0.01 * correction_factor))); |
792 | 39.3k | } |
793 | | |
794 | 42.2k | cpi->rc.q_2_frame = cpi->rc.q_1_frame; |
795 | 42.2k | cpi->rc.q_1_frame = cm->base_qindex; |
796 | 42.2k | cpi->rc.rc_2_frame = cpi->rc.rc_1_frame; |
797 | 42.2k | if (correction_factor > 110) |
798 | 11.9k | cpi->rc.rc_1_frame = -1; |
799 | 30.2k | else if (correction_factor < 90) |
800 | 15.1k | cpi->rc.rc_1_frame = 1; |
801 | 15.0k | else |
802 | 15.0k | cpi->rc.rc_1_frame = 0; |
803 | | |
804 | | // Turn off oscilation detection in the case of massive overshoot. |
805 | 42.2k | if (cpi->rc.rc_1_frame == -1 && cpi->rc.rc_2_frame == 1 && |
806 | 42.2k | correction_factor > 1000) { |
807 | 223 | cpi->rc.rc_2_frame = 0; |
808 | 223 | } |
809 | | |
810 | 42.2k | if (correction_factor > 102) { |
811 | | // We are not already at the worst allowable quality |
812 | 14.9k | correction_factor = |
813 | 14.9k | (int)(100 + ((correction_factor - 100) * adjustment_limit)); |
814 | 14.9k | rate_correction_factor = (rate_correction_factor * correction_factor) / 100; |
815 | | // Keep rate_correction_factor within limits |
816 | 14.9k | if (rate_correction_factor > MAX_BPB_FACTOR) |
817 | 0 | rate_correction_factor = MAX_BPB_FACTOR; |
818 | 27.2k | } else if (correction_factor < 99) { |
819 | | // We are not already at the best allowable quality |
820 | 19.1k | correction_factor = |
821 | 19.1k | (int)(100 - ((100 - correction_factor) * adjustment_limit)); |
822 | 19.1k | rate_correction_factor = (rate_correction_factor * correction_factor) / 100; |
823 | | |
824 | | // Keep rate_correction_factor within limits |
825 | 19.1k | if (rate_correction_factor < MIN_BPB_FACTOR) |
826 | 350 | rate_correction_factor = MIN_BPB_FACTOR; |
827 | 19.1k | } |
828 | | |
829 | 42.2k | set_rate_correction_factor(cpi, rate_correction_factor); |
830 | 42.2k | } |
831 | | |
832 | | int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame, |
833 | 38.8k | int active_best_quality, int active_worst_quality) { |
834 | 38.8k | const VP9_COMMON *const cm = &cpi->common; |
835 | 38.8k | CYCLIC_REFRESH *const cr = cpi->cyclic_refresh; |
836 | 38.8k | int q = active_worst_quality; |
837 | 38.8k | int last_error = INT_MAX; |
838 | 38.8k | int i, target_bits_per_mb, bits_per_mb_at_this_q; |
839 | 38.8k | const double correction_factor = get_rate_correction_factor(cpi); |
840 | | |
841 | | // Calculate required scaling factor based on target frame size and size of |
842 | | // frame produced using previous Q. |
843 | 38.8k | target_bits_per_mb = |
844 | 38.8k | (int)(((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs); |
845 | | |
846 | 38.8k | i = active_best_quality; |
847 | | |
848 | 757k | do { |
849 | 757k | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cr->apply_cyclic_refresh && |
850 | 757k | (!cpi->oxcf.gf_cbr_boost_pct || !cpi->refresh_golden_frame)) { |
851 | 0 | bits_per_mb_at_this_q = |
852 | 0 | (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor); |
853 | 757k | } else { |
854 | 757k | FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type; |
855 | 757k | bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb( |
856 | 757k | frame_type, i, correction_factor, cm->bit_depth); |
857 | 757k | } |
858 | | |
859 | 757k | if (bits_per_mb_at_this_q <= target_bits_per_mb) { |
860 | 30.2k | if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) |
861 | 28.3k | q = i; |
862 | 1.89k | else |
863 | 1.89k | q = i - 1; |
864 | | |
865 | 30.2k | break; |
866 | 726k | } else { |
867 | 726k | last_error = bits_per_mb_at_this_q - target_bits_per_mb; |
868 | 726k | } |
869 | 757k | } while (++i <= active_worst_quality); |
870 | | |
871 | | // Adjustment to q for CBR mode. |
872 | 38.8k | if (cpi->oxcf.rc_mode == VPX_CBR) return adjust_q_cbr(cpi, q); |
873 | | |
874 | 38.8k | return q; |
875 | 38.8k | } |
876 | | |
877 | | static int get_active_quality(int q, int gfu_boost, int low, int high, |
878 | 7.14k | int *low_motion_minq, int *high_motion_minq) { |
879 | 7.14k | if (gfu_boost > high) { |
880 | 0 | return low_motion_minq[q]; |
881 | 7.14k | } else if (gfu_boost < low) { |
882 | 0 | return high_motion_minq[q]; |
883 | 7.14k | } else { |
884 | 7.14k | const int gap = high - low; |
885 | 7.14k | const int offset = high - gfu_boost; |
886 | 7.14k | const int qdiff = high_motion_minq[q] - low_motion_minq[q]; |
887 | 7.14k | const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; |
888 | 7.14k | return low_motion_minq[q] + adjustment; |
889 | 7.14k | } |
890 | 7.14k | } |
891 | | |
892 | | static int get_kf_active_quality(const RATE_CONTROL *const rc, int q, |
893 | 4.78k | vpx_bit_depth_t bit_depth) { |
894 | 4.78k | int *kf_low_motion_minq; |
895 | 4.78k | int *kf_high_motion_minq; |
896 | 4.78k | ASSIGN_MINQ_TABLE(bit_depth, kf_low_motion_minq); |
897 | 4.78k | ASSIGN_MINQ_TABLE(bit_depth, kf_high_motion_minq); |
898 | 4.78k | return get_active_quality(q, rc->kf_boost, kf_low, kf_high, |
899 | 4.78k | kf_low_motion_minq, kf_high_motion_minq); |
900 | 4.78k | } |
901 | | |
902 | | static int get_gf_active_quality(const VP9_COMP *const cpi, int q, |
903 | 2.35k | vpx_bit_depth_t bit_depth) { |
904 | 2.35k | const GF_GROUP *const gf_group = &cpi->twopass.gf_group; |
905 | 2.35k | const RATE_CONTROL *const rc = &cpi->rc; |
906 | | |
907 | 2.35k | int *arfgf_low_motion_minq; |
908 | 2.35k | int *arfgf_high_motion_minq; |
909 | 2.35k | const int gfu_boost = cpi->multi_layer_arf |
910 | 2.35k | ? gf_group->gfu_boost[gf_group->index] |
911 | 2.35k | : rc->gfu_boost; |
912 | 2.35k | ASSIGN_MINQ_TABLE(bit_depth, arfgf_low_motion_minq); |
913 | 2.35k | ASSIGN_MINQ_TABLE(bit_depth, arfgf_high_motion_minq); |
914 | 2.35k | return get_active_quality(q, gfu_boost, gf_low, gf_high, |
915 | 2.35k | arfgf_low_motion_minq, arfgf_high_motion_minq); |
916 | 2.35k | } |
917 | | |
918 | 42.2k | static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) { |
919 | 42.2k | const RATE_CONTROL *const rc = &cpi->rc; |
920 | 42.2k | const unsigned int curr_frame = cpi->common.current_video_frame; |
921 | 42.2k | int active_worst_quality; |
922 | | |
923 | 42.2k | if (cpi->common.frame_type == KEY_FRAME) { |
924 | 7.75k | active_worst_quality = |
925 | 7.75k | curr_frame == 0 ? rc->worst_quality : rc->last_q[KEY_FRAME] << 1; |
926 | 34.5k | } else { |
927 | 34.5k | if (!rc->is_src_frame_alt_ref && !cpi->use_svc && |
928 | 34.5k | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
929 | 2.39k | active_worst_quality = |
930 | 2.39k | curr_frame == 1 |
931 | 2.39k | ? rc->last_q[KEY_FRAME] * 5 >> 2 |
932 | 2.39k | : rc->last_q[INTER_FRAME] * rc->fac_active_worst_gf / 100; |
933 | 32.1k | } else { |
934 | 32.1k | active_worst_quality = curr_frame == 1 |
935 | 32.1k | ? rc->last_q[KEY_FRAME] << 1 |
936 | 32.1k | : rc->avg_frame_qindex[INTER_FRAME] * |
937 | 30.3k | rc->fac_active_worst_inter / 100; |
938 | 32.1k | } |
939 | 34.5k | } |
940 | 42.2k | return VPXMIN(active_worst_quality, rc->worst_quality); |
941 | 42.2k | } |
942 | | |
943 | | // Adjust active_worst_quality level based on buffer level. |
944 | 0 | static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) { |
945 | | // Adjust active_worst_quality: If buffer is above the optimal/target level, |
946 | | // bring active_worst_quality down depending on fullness of buffer. |
947 | | // If buffer is below the optimal level, let the active_worst_quality go from |
948 | | // ambient Q (at buffer = optimal level) to worst_quality level |
949 | | // (at buffer = critical level). |
950 | 0 | const VP9_COMMON *const cm = &cpi->common; |
951 | 0 | const RATE_CONTROL *rc = &cpi->rc; |
952 | | // Buffer level below which we push active_worst to worst_quality. |
953 | 0 | int64_t critical_level = rc->optimal_buffer_level >> 3; |
954 | 0 | int64_t buff_lvl_step = 0; |
955 | 0 | int adjustment = 0; |
956 | 0 | int active_worst_quality; |
957 | 0 | int ambient_qp; |
958 | 0 | unsigned int num_frames_weight_key = 5 * cpi->svc.number_temporal_layers; |
959 | 0 | if (frame_is_intra_only(cm) || rc->reset_high_source_sad || rc->force_max_q) |
960 | 0 | return rc->worst_quality; |
961 | | // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME] |
962 | | // for the first few frames following key frame. These are both initialized |
963 | | // to worst_quality and updated with (3/4, 1/4) average in postencode_update. |
964 | | // So for first few frames following key, the qp of that key frame is weighted |
965 | | // into the active_worst_quality setting. |
966 | 0 | ambient_qp = (cm->current_video_frame < num_frames_weight_key) |
967 | 0 | ? VPXMIN(rc->avg_frame_qindex[INTER_FRAME], |
968 | 0 | rc->avg_frame_qindex[KEY_FRAME]) |
969 | 0 | : rc->avg_frame_qindex[INTER_FRAME]; |
970 | 0 | active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 5) >> 2); |
971 | | // For SVC if the current base spatial layer was key frame, use the QP from |
972 | | // that base layer for ambient_qp. |
973 | 0 | if (cpi->use_svc && cpi->svc.spatial_layer_id > 0) { |
974 | 0 | int layer = LAYER_IDS_TO_IDX(0, cpi->svc.temporal_layer_id, |
975 | 0 | cpi->svc.number_temporal_layers); |
976 | 0 | const LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer]; |
977 | 0 | if (lc->is_key_frame) { |
978 | 0 | const RATE_CONTROL *lrc = &lc->rc; |
979 | 0 | ambient_qp = VPXMIN(ambient_qp, lrc->last_q[KEY_FRAME]); |
980 | 0 | active_worst_quality = VPXMIN(rc->worst_quality, (ambient_qp * 9) >> 3); |
981 | 0 | } |
982 | 0 | } |
983 | 0 | if (rc->buffer_level > rc->optimal_buffer_level) { |
984 | | // Adjust down. |
985 | | // Maximum limit for down adjustment ~30%; make it lower for screen content. |
986 | 0 | int max_adjustment_down = active_worst_quality / 3; |
987 | 0 | if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) |
988 | 0 | max_adjustment_down = active_worst_quality >> 3; |
989 | 0 | if (max_adjustment_down) { |
990 | 0 | buff_lvl_step = ((rc->maximum_buffer_size - rc->optimal_buffer_level) / |
991 | 0 | max_adjustment_down); |
992 | 0 | if (buff_lvl_step) |
993 | 0 | adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) / |
994 | 0 | buff_lvl_step); |
995 | 0 | active_worst_quality -= adjustment; |
996 | 0 | } |
997 | 0 | } else if (rc->buffer_level > critical_level) { |
998 | | // Adjust up from ambient Q. |
999 | 0 | if (critical_level) { |
1000 | 0 | buff_lvl_step = (rc->optimal_buffer_level - critical_level); |
1001 | 0 | if (buff_lvl_step) { |
1002 | 0 | adjustment = (int)((rc->worst_quality - ambient_qp) * |
1003 | 0 | (rc->optimal_buffer_level - rc->buffer_level) / |
1004 | 0 | buff_lvl_step); |
1005 | 0 | } |
1006 | 0 | active_worst_quality = ambient_qp + adjustment; |
1007 | 0 | } |
1008 | 0 | } else { |
1009 | | // Set to worst_quality if buffer is below critical level. |
1010 | 0 | active_worst_quality = rc->worst_quality; |
1011 | 0 | } |
1012 | 0 | return active_worst_quality; |
1013 | 0 | } |
1014 | | |
1015 | | static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi, |
1016 | | int *bottom_index, |
1017 | 0 | int *top_index) { |
1018 | 0 | const VP9_COMMON *const cm = &cpi->common; |
1019 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1020 | 0 | int active_best_quality; |
1021 | 0 | int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi); |
1022 | 0 | int q; |
1023 | 0 | int *rtc_minq; |
1024 | 0 | ASSIGN_MINQ_TABLE(cm->bit_depth, rtc_minq); |
1025 | | |
1026 | 0 | if (frame_is_intra_only(cm)) { |
1027 | 0 | active_best_quality = rc->best_quality; |
1028 | | // Handle the special case for key frames forced when we have reached |
1029 | | // the maximum key frame interval. Here force the Q to a range |
1030 | | // based on the ambient Q to reduce the risk of popping. |
1031 | 0 | if (rc->this_key_frame_forced) { |
1032 | 0 | int qindex = rc->last_boosted_qindex; |
1033 | 0 | double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1034 | 0 | int delta_qindex = vp9_compute_qdelta( |
1035 | 0 | rc, last_boosted_q, (last_boosted_q * 0.75), cm->bit_depth); |
1036 | 0 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
1037 | 0 | } else if (cm->current_video_frame > 0) { |
1038 | | // not first frame of one pass and kf_boost is set |
1039 | 0 | double q_adj_factor = 1.0; |
1040 | 0 | double q_val; |
1041 | |
|
1042 | 0 | active_best_quality = get_kf_active_quality( |
1043 | 0 | rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth); |
1044 | | |
1045 | | // Allow somewhat lower kf minq with small image formats. |
1046 | 0 | if ((cm->width * cm->height) <= (352 * 288)) { |
1047 | 0 | q_adj_factor -= 0.25; |
1048 | 0 | } |
1049 | | |
1050 | | // Convert the adjustment factor to a qindex delta |
1051 | | // on active_best_quality. |
1052 | 0 | q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth); |
1053 | 0 | active_best_quality += |
1054 | 0 | vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth); |
1055 | 0 | } |
1056 | 0 | } else if (!rc->is_src_frame_alt_ref && !cpi->use_svc && |
1057 | 0 | cpi->oxcf.gf_cbr_boost_pct && |
1058 | 0 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
1059 | | // Use the lower of active_worst_quality and recent |
1060 | | // average Q as basis for GF/ARF best Q limit unless last frame was |
1061 | | // a key frame. |
1062 | 0 | if (rc->frames_since_key > 1 && |
1063 | 0 | rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
1064 | 0 | q = rc->avg_frame_qindex[INTER_FRAME]; |
1065 | 0 | } else { |
1066 | 0 | q = active_worst_quality; |
1067 | 0 | } |
1068 | 0 | active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth); |
1069 | 0 | } else { |
1070 | | // Use the lower of active_worst_quality and recent/average Q. |
1071 | 0 | if (cm->current_video_frame > 1) { |
1072 | 0 | if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) |
1073 | 0 | active_best_quality = rtc_minq[rc->avg_frame_qindex[INTER_FRAME]]; |
1074 | 0 | else |
1075 | 0 | active_best_quality = rtc_minq[active_worst_quality]; |
1076 | 0 | } else { |
1077 | 0 | if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality) |
1078 | 0 | active_best_quality = rtc_minq[rc->avg_frame_qindex[KEY_FRAME]]; |
1079 | 0 | else |
1080 | 0 | active_best_quality = rtc_minq[active_worst_quality]; |
1081 | 0 | } |
1082 | 0 | } |
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 | | // Special case code to try and match quality with forced key frames |
1094 | 0 | if (frame_is_intra_only(cm) && rc->this_key_frame_forced) { |
1095 | 0 | q = rc->last_boosted_qindex; |
1096 | 0 | } else { |
1097 | 0 | q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
1098 | 0 | active_worst_quality); |
1099 | 0 | if (q > *top_index) { |
1100 | | // Special case when we are targeting the max allowed rate |
1101 | 0 | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
1102 | 0 | *top_index = q; |
1103 | 0 | else |
1104 | 0 | q = *top_index; |
1105 | 0 | } |
1106 | 0 | } |
1107 | |
|
1108 | 0 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
1109 | 0 | assert(*bottom_index <= rc->worst_quality && |
1110 | 0 | *bottom_index >= rc->best_quality); |
1111 | 0 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
1112 | 0 | return q; |
1113 | 0 | } |
1114 | | |
1115 | | static int get_active_cq_level_one_pass(const RATE_CONTROL *rc, |
1116 | 42.2k | const VP9EncoderConfig *const oxcf) { |
1117 | 42.2k | static const double cq_adjust_threshold = 0.1; |
1118 | 42.2k | int active_cq_level = oxcf->cq_level; |
1119 | 42.2k | if (oxcf->rc_mode == VPX_CQ && rc->total_target_bits > 0) { |
1120 | 0 | const double x = (double)rc->total_actual_bits / rc->total_target_bits; |
1121 | 0 | if (x < cq_adjust_threshold) { |
1122 | 0 | active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold); |
1123 | 0 | } |
1124 | 0 | } |
1125 | 42.2k | return active_cq_level; |
1126 | 42.2k | } |
1127 | | |
1128 | 0 | #define SMOOTH_PCT_MIN 0.1 |
1129 | 0 | #define SMOOTH_PCT_DIV 0.05 |
1130 | | static int get_active_cq_level_two_pass(const TWO_PASS *twopass, |
1131 | | const RATE_CONTROL *rc, |
1132 | 0 | const VP9EncoderConfig *const oxcf) { |
1133 | 0 | static const double cq_adjust_threshold = 0.1; |
1134 | 0 | int active_cq_level = oxcf->cq_level; |
1135 | 0 | if (oxcf->rc_mode == VPX_CQ) { |
1136 | 0 | if (twopass->mb_smooth_pct > SMOOTH_PCT_MIN) { |
1137 | 0 | active_cq_level -= |
1138 | 0 | (int)((twopass->mb_smooth_pct - SMOOTH_PCT_MIN) / SMOOTH_PCT_DIV); |
1139 | 0 | active_cq_level = VPXMAX(active_cq_level, 0); |
1140 | 0 | } |
1141 | 0 | if (rc->total_target_bits > 0) { |
1142 | 0 | const double x = (double)rc->total_actual_bits / rc->total_target_bits; |
1143 | 0 | if (x < cq_adjust_threshold) { |
1144 | 0 | active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold); |
1145 | 0 | } |
1146 | 0 | } |
1147 | 0 | } |
1148 | 0 | return active_cq_level; |
1149 | 0 | } |
1150 | | |
1151 | | static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi, |
1152 | | int *bottom_index, |
1153 | 42.2k | int *top_index) { |
1154 | 42.2k | const VP9_COMMON *const cm = &cpi->common; |
1155 | 42.2k | const RATE_CONTROL *const rc = &cpi->rc; |
1156 | 42.2k | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
1157 | 42.2k | const int cq_level = get_active_cq_level_one_pass(rc, oxcf); |
1158 | 42.2k | int active_best_quality; |
1159 | 42.2k | int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); |
1160 | 42.2k | int q; |
1161 | 42.2k | int *inter_minq; |
1162 | 42.2k | ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq); |
1163 | | |
1164 | 42.2k | if (frame_is_intra_only(cm)) { |
1165 | 7.75k | if (oxcf->rc_mode == VPX_Q) { |
1166 | 1.79k | int qindex = cq_level; |
1167 | 1.79k | double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1168 | 1.79k | int delta_qindex = |
1169 | 1.79k | vp9_compute_qdelta(rc, qstart, qstart * 0.25, cm->bit_depth); |
1170 | 1.79k | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
1171 | 5.95k | } else if (rc->this_key_frame_forced) { |
1172 | | // Handle the special case for key frames forced when we have reached |
1173 | | // the maximum key frame interval. Here force the Q to a range |
1174 | | // based on the ambient Q to reduce the risk of popping. |
1175 | 1.16k | int qindex = rc->last_boosted_qindex; |
1176 | 1.16k | double last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1177 | 1.16k | int delta_qindex = vp9_compute_qdelta( |
1178 | 1.16k | rc, last_boosted_q, last_boosted_q * 0.75, cm->bit_depth); |
1179 | 1.16k | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
1180 | 4.78k | } else { |
1181 | | // not first frame of one pass and kf_boost is set |
1182 | 4.78k | double q_adj_factor = 1.0; |
1183 | 4.78k | double q_val; |
1184 | | |
1185 | 4.78k | active_best_quality = get_kf_active_quality( |
1186 | 4.78k | rc, rc->avg_frame_qindex[KEY_FRAME], cm->bit_depth); |
1187 | | |
1188 | | // Allow somewhat lower kf minq with small image formats. |
1189 | 4.78k | if ((cm->width * cm->height) <= (352 * 288)) { |
1190 | 4.66k | q_adj_factor -= 0.25; |
1191 | 4.66k | } |
1192 | | |
1193 | | // Convert the adjustment factor to a qindex delta |
1194 | | // on active_best_quality. |
1195 | 4.78k | q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth); |
1196 | 4.78k | active_best_quality += |
1197 | 4.78k | vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth); |
1198 | 4.78k | } |
1199 | 34.5k | } else if (!rc->is_src_frame_alt_ref && |
1200 | 34.5k | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
1201 | | // Use the lower of active_worst_quality and recent |
1202 | | // average Q as basis for GF/ARF best Q limit unless last frame was |
1203 | | // a key frame. |
1204 | 2.39k | if (rc->frames_since_key > 1) { |
1205 | 2.39k | if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
1206 | 519 | q = rc->avg_frame_qindex[INTER_FRAME]; |
1207 | 1.87k | } else { |
1208 | 1.87k | q = active_worst_quality; |
1209 | 1.87k | } |
1210 | 2.39k | } else { |
1211 | 0 | q = rc->avg_frame_qindex[KEY_FRAME]; |
1212 | 0 | } |
1213 | | // For constrained quality don't allow Q less than the cq level |
1214 | 2.39k | if (oxcf->rc_mode == VPX_CQ) { |
1215 | 0 | if (q < cq_level) q = cq_level; |
1216 | |
|
1217 | 0 | active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth); |
1218 | | |
1219 | | // Constrained quality use slightly lower active best. |
1220 | 0 | active_best_quality = active_best_quality * 15 / 16; |
1221 | |
|
1222 | 2.39k | } else if (oxcf->rc_mode == VPX_Q) { |
1223 | 33 | int qindex = cq_level; |
1224 | 33 | double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1225 | 33 | int delta_qindex; |
1226 | 33 | if (cpi->refresh_alt_ref_frame) |
1227 | 0 | delta_qindex = |
1228 | 0 | vp9_compute_qdelta(rc, qstart, qstart * 0.40, cm->bit_depth); |
1229 | 33 | else |
1230 | 33 | delta_qindex = |
1231 | 33 | vp9_compute_qdelta(rc, qstart, qstart * 0.50, cm->bit_depth); |
1232 | 33 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
1233 | 2.35k | } else { |
1234 | 2.35k | active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth); |
1235 | 2.35k | } |
1236 | 32.1k | } else { |
1237 | 32.1k | if (oxcf->rc_mode == VPX_Q) { |
1238 | 410 | int qindex = cq_level; |
1239 | 410 | double qstart = vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1240 | 410 | double delta_rate[FIXED_GF_INTERVAL] = { 0.50, 1.0, 0.85, 1.0, |
1241 | 410 | 0.70, 1.0, 0.85, 1.0 }; |
1242 | 410 | int delta_qindex = vp9_compute_qdelta( |
1243 | 410 | rc, qstart, |
1244 | 410 | qstart * delta_rate[cm->current_video_frame % FIXED_GF_INTERVAL], |
1245 | 410 | cm->bit_depth); |
1246 | 410 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
1247 | 31.7k | } else { |
1248 | | // Use the min of the average Q and active_worst_quality as basis for |
1249 | | // active_best. |
1250 | 31.7k | if (cm->current_video_frame > 1) { |
1251 | 29.9k | q = VPXMIN(rc->avg_frame_qindex[INTER_FRAME], active_worst_quality); |
1252 | 29.9k | active_best_quality = inter_minq[q]; |
1253 | 29.9k | } else { |
1254 | 1.78k | active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]]; |
1255 | 1.78k | } |
1256 | | // For the constrained quality mode we don't want |
1257 | | // q to fall below the cq level. |
1258 | 31.7k | if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) { |
1259 | 0 | active_best_quality = cq_level; |
1260 | 0 | } |
1261 | 31.7k | } |
1262 | 32.1k | } |
1263 | | |
1264 | | // Clip the active best and worst quality values to limits |
1265 | 42.2k | active_best_quality = |
1266 | 42.2k | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
1267 | 42.2k | active_worst_quality = |
1268 | 42.2k | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
1269 | | |
1270 | 42.2k | *top_index = active_worst_quality; |
1271 | 42.2k | *bottom_index = active_best_quality; |
1272 | | |
1273 | 42.2k | #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
1274 | 42.2k | { |
1275 | 42.2k | int qdelta = 0; |
1276 | 42.2k | vpx_clear_system_state(); |
1277 | | |
1278 | | // Limit Q range for the adaptive loop. |
1279 | 42.2k | if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced && |
1280 | 42.2k | !(cm->current_video_frame == 0)) { |
1281 | 3.70k | qdelta = vp9_compute_qdelta_by_rate( |
1282 | 3.70k | &cpi->rc, cm->frame_type, active_worst_quality, 2.0, cm->bit_depth); |
1283 | 38.5k | } else if (!rc->is_src_frame_alt_ref && |
1284 | 38.5k | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
1285 | 6.43k | qdelta = vp9_compute_qdelta_by_rate( |
1286 | 6.43k | &cpi->rc, cm->frame_type, active_worst_quality, 1.75, cm->bit_depth); |
1287 | 6.43k | } |
1288 | 42.2k | if (rc->high_source_sad && cpi->sf.use_altref_onepass) qdelta = 0; |
1289 | 42.2k | *top_index = active_worst_quality + qdelta; |
1290 | 42.2k | *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; |
1291 | 42.2k | } |
1292 | 42.2k | #endif |
1293 | | |
1294 | 42.2k | if (oxcf->rc_mode == VPX_Q) { |
1295 | 2.24k | q = active_best_quality; |
1296 | | // Special case code to try and match quality with forced key frames |
1297 | 40.0k | } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { |
1298 | 1.16k | q = rc->last_boosted_qindex; |
1299 | 38.8k | } else { |
1300 | 38.8k | q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
1301 | 38.8k | active_worst_quality); |
1302 | 38.8k | if (q > *top_index) { |
1303 | | // Special case when we are targeting the max allowed rate |
1304 | 1.91k | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
1305 | 217 | *top_index = q; |
1306 | 1.70k | else |
1307 | 1.70k | q = *top_index; |
1308 | 1.91k | } |
1309 | 38.8k | } |
1310 | | |
1311 | 42.2k | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
1312 | 42.2k | assert(*bottom_index <= rc->worst_quality && |
1313 | 42.2k | *bottom_index >= rc->best_quality); |
1314 | 42.2k | assert(q <= rc->worst_quality && q >= rc->best_quality); |
1315 | 42.2k | return q; |
1316 | 42.2k | } |
1317 | | |
1318 | 0 | int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) { |
1319 | 0 | static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { |
1320 | 0 | 1.00, // INTER_NORMAL |
1321 | 0 | 1.00, // INTER_HIGH |
1322 | 0 | 1.50, // GF_ARF_LOW |
1323 | 0 | 1.75, // GF_ARF_STD |
1324 | 0 | 2.00, // KF_STD |
1325 | 0 | }; |
1326 | 0 | const VP9_COMMON *const cm = &cpi->common; |
1327 | |
|
1328 | 0 | int qdelta = vp9_compute_qdelta_by_rate( |
1329 | 0 | &cpi->rc, cm->frame_type, q, rate_factor_deltas[rf_level], cm->bit_depth); |
1330 | 0 | return qdelta; |
1331 | 0 | } |
1332 | | |
1333 | 0 | #define STATIC_MOTION_THRESH 95 |
1334 | | |
1335 | | static void pick_kf_q_bound_two_pass(const VP9_COMP *cpi, int *bottom_index, |
1336 | 0 | int *top_index) { |
1337 | 0 | const VP9_COMMON *const cm = &cpi->common; |
1338 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1339 | 0 | int active_best_quality; |
1340 | 0 | int active_worst_quality = cpi->twopass.active_worst_quality; |
1341 | |
|
1342 | 0 | if (rc->this_key_frame_forced) { |
1343 | | // Handle the special case for key frames forced when we have reached |
1344 | | // the maximum key frame interval. Here force the Q to a range |
1345 | | // based on the ambient Q to reduce the risk of popping. |
1346 | 0 | double last_boosted_q; |
1347 | 0 | int delta_qindex; |
1348 | 0 | int qindex; |
1349 | |
|
1350 | 0 | if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { |
1351 | 0 | qindex = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex); |
1352 | 0 | active_best_quality = qindex; |
1353 | 0 | last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1354 | 0 | delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, |
1355 | 0 | last_boosted_q * 1.25, cm->bit_depth); |
1356 | 0 | active_worst_quality = |
1357 | 0 | VPXMIN(qindex + delta_qindex, active_worst_quality); |
1358 | 0 | } else { |
1359 | 0 | qindex = rc->last_boosted_qindex; |
1360 | 0 | last_boosted_q = vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1361 | 0 | delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, |
1362 | 0 | last_boosted_q * 0.75, cm->bit_depth); |
1363 | 0 | active_best_quality = VPXMAX(qindex + delta_qindex, rc->best_quality); |
1364 | 0 | } |
1365 | 0 | } else { |
1366 | | // Not forced keyframe. |
1367 | 0 | double q_adj_factor = 1.0; |
1368 | 0 | double q_val; |
1369 | | // Baseline value derived from cpi->active_worst_quality and kf boost. |
1370 | 0 | active_best_quality = |
1371 | 0 | get_kf_active_quality(rc, active_worst_quality, cm->bit_depth); |
1372 | 0 | if (cpi->twopass.kf_zeromotion_pct >= STATIC_KF_GROUP_THRESH) { |
1373 | 0 | active_best_quality /= 4; |
1374 | 0 | } |
1375 | | |
1376 | | // Don't allow the active min to be lossless (q0) unlesss the max q |
1377 | | // already indicates lossless. |
1378 | 0 | active_best_quality = |
1379 | 0 | VPXMIN(active_worst_quality, VPXMAX(1, active_best_quality)); |
1380 | | |
1381 | | // Allow somewhat lower kf minq with small image formats. |
1382 | 0 | if ((cm->width * cm->height) <= (352 * 288)) { |
1383 | 0 | q_adj_factor -= 0.25; |
1384 | 0 | } |
1385 | | |
1386 | | // Make a further adjustment based on the kf zero motion measure. |
1387 | 0 | q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct); |
1388 | | |
1389 | | // Convert the adjustment factor to a qindex delta |
1390 | | // on active_best_quality. |
1391 | 0 | q_val = vp9_convert_qindex_to_q(active_best_quality, cm->bit_depth); |
1392 | 0 | active_best_quality += |
1393 | 0 | vp9_compute_qdelta(rc, q_val, q_val * q_adj_factor, cm->bit_depth); |
1394 | 0 | } |
1395 | 0 | *top_index = active_worst_quality; |
1396 | 0 | *bottom_index = active_best_quality; |
1397 | 0 | } |
1398 | | |
1399 | | static int rc_constant_q(const VP9_COMP *cpi, int *bottom_index, int *top_index, |
1400 | 0 | int gf_group_index) { |
1401 | 0 | const VP9_COMMON *const cm = &cpi->common; |
1402 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1403 | 0 | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
1404 | 0 | const GF_GROUP *gf_group = &cpi->twopass.gf_group; |
1405 | 0 | const int is_intra_frame = frame_is_intra_only(cm); |
1406 | |
|
1407 | 0 | const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf); |
1408 | |
|
1409 | 0 | int q = cq_level; |
1410 | 0 | int active_best_quality = cq_level; |
1411 | 0 | int active_worst_quality = cq_level; |
1412 | | |
1413 | | // Key frame qp decision |
1414 | 0 | if (is_intra_frame && rc->frames_to_key > 1) |
1415 | 0 | pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality); |
1416 | | |
1417 | | // ARF / GF qp decision |
1418 | 0 | if (!is_intra_frame && !rc->is_src_frame_alt_ref && |
1419 | 0 | cpi->refresh_alt_ref_frame) { |
1420 | 0 | active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth); |
1421 | | |
1422 | | // Modify best quality for second level arfs. For mode VPX_Q this |
1423 | | // becomes the baseline frame q. |
1424 | 0 | if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) { |
1425 | 0 | const int layer_depth = gf_group->layer_depth[gf_group_index]; |
1426 | | // linearly fit the frame q depending on the layer depth index from |
1427 | | // the base layer ARF. |
1428 | 0 | active_best_quality = ((layer_depth - 1) * cq_level + |
1429 | 0 | active_best_quality + layer_depth / 2) / |
1430 | 0 | layer_depth; |
1431 | 0 | } |
1432 | 0 | } |
1433 | |
|
1434 | 0 | q = active_best_quality; |
1435 | 0 | *top_index = active_worst_quality; |
1436 | 0 | *bottom_index = active_best_quality; |
1437 | 0 | return q; |
1438 | 0 | } |
1439 | | |
1440 | | int vp9_rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, int *bottom_index, |
1441 | 0 | int *top_index, int gf_group_index) { |
1442 | 0 | const VP9_COMMON *const cm = &cpi->common; |
1443 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
1444 | 0 | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
1445 | 0 | const GF_GROUP *gf_group = &cpi->twopass.gf_group; |
1446 | 0 | const int cq_level = get_active_cq_level_two_pass(&cpi->twopass, rc, oxcf); |
1447 | 0 | int active_best_quality; |
1448 | 0 | int active_worst_quality = cpi->twopass.active_worst_quality; |
1449 | 0 | int q; |
1450 | 0 | int *inter_minq; |
1451 | 0 | int arf_active_best_quality_hl; |
1452 | 0 | int *arfgf_high_motion_minq, *arfgf_low_motion_minq; |
1453 | 0 | const int boost_frame = |
1454 | 0 | !rc->is_src_frame_alt_ref && |
1455 | 0 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame); |
1456 | |
|
1457 | 0 | ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq); |
1458 | | |
1459 | 0 | if (oxcf->rc_mode == VPX_Q) |
1460 | 0 | return rc_constant_q(cpi, bottom_index, top_index, gf_group_index); |
1461 | | |
1462 | 0 | if (frame_is_intra_only(cm)) { |
1463 | 0 | pick_kf_q_bound_two_pass(cpi, &active_best_quality, &active_worst_quality); |
1464 | 0 | } else if (boost_frame) { |
1465 | | // Use the lower of active_worst_quality and recent |
1466 | | // average Q as basis for GF/ARF best Q limit unless last frame was |
1467 | | // a key frame. |
1468 | 0 | if (rc->frames_since_key > 1 && |
1469 | 0 | rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { |
1470 | 0 | q = rc->avg_frame_qindex[INTER_FRAME]; |
1471 | 0 | } else { |
1472 | 0 | q = active_worst_quality; |
1473 | 0 | } |
1474 | | // For constrained quality don't allow Q less than the cq level |
1475 | 0 | if (oxcf->rc_mode == VPX_CQ) { |
1476 | 0 | if (q < cq_level) q = cq_level; |
1477 | 0 | } |
1478 | 0 | active_best_quality = get_gf_active_quality(cpi, q, cm->bit_depth); |
1479 | 0 | arf_active_best_quality_hl = active_best_quality; |
1480 | |
|
1481 | 0 | if (rc->arf_increase_active_best_quality == 1) { |
1482 | 0 | ASSIGN_MINQ_TABLE(cm->bit_depth, arfgf_high_motion_minq); |
1483 | 0 | arf_active_best_quality_hl = arfgf_high_motion_minq[q]; |
1484 | 0 | } else if (rc->arf_increase_active_best_quality == -1) { |
1485 | 0 | ASSIGN_MINQ_TABLE(cm->bit_depth, arfgf_low_motion_minq); |
1486 | 0 | arf_active_best_quality_hl = arfgf_low_motion_minq[q]; |
1487 | 0 | } |
1488 | 0 | active_best_quality = |
1489 | 0 | (int)((double)active_best_quality * |
1490 | 0 | rc->arf_active_best_quality_adjustment_factor + |
1491 | 0 | (double)arf_active_best_quality_hl * |
1492 | 0 | (1.0 - rc->arf_active_best_quality_adjustment_factor)); |
1493 | | |
1494 | | // Modify best quality for second level arfs. For mode VPX_Q this |
1495 | | // becomes the baseline frame q. |
1496 | 0 | if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) { |
1497 | 0 | const int layer_depth = gf_group->layer_depth[gf_group_index]; |
1498 | | // linearly fit the frame q depending on the layer depth index from |
1499 | | // the base layer ARF. |
1500 | 0 | active_best_quality = |
1501 | 0 | ((layer_depth - 1) * q + active_best_quality + layer_depth / 2) / |
1502 | 0 | layer_depth; |
1503 | 0 | } |
1504 | 0 | } else { |
1505 | 0 | active_best_quality = inter_minq[active_worst_quality]; |
1506 | | |
1507 | | // For the constrained quality mode we don't want |
1508 | | // q to fall below the cq level. |
1509 | 0 | if ((oxcf->rc_mode == VPX_CQ) && (active_best_quality < cq_level)) { |
1510 | 0 | active_best_quality = cq_level; |
1511 | 0 | } |
1512 | 0 | } |
1513 | | |
1514 | | // Extension to max or min Q if undershoot or overshoot is outside |
1515 | | // the permitted range. |
1516 | 0 | if (frame_is_intra_only(cm) || boost_frame) { |
1517 | 0 | const int layer_depth = gf_group->layer_depth[gf_group_index]; |
1518 | 0 | active_best_quality -= |
1519 | 0 | (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast); |
1520 | 0 | active_worst_quality += (cpi->twopass.extend_maxq / 2); |
1521 | |
|
1522 | 0 | if (gf_group->rf_level[gf_group_index] == GF_ARF_LOW) { |
1523 | 0 | assert(layer_depth > 1); |
1524 | 0 | active_best_quality = |
1525 | 0 | VPXMAX(active_best_quality, |
1526 | 0 | cpi->twopass.last_qindex_of_arf_layer[layer_depth - 1]); |
1527 | 0 | } |
1528 | 0 | } else { |
1529 | 0 | const int max_layer_depth = gf_group->max_layer_depth; |
1530 | 0 | assert(max_layer_depth > 0); |
1531 | |
|
1532 | 0 | active_best_quality -= |
1533 | 0 | (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2; |
1534 | 0 | active_worst_quality += cpi->twopass.extend_maxq; |
1535 | | |
1536 | | // For normal frames do not allow an active minq lower than the q used for |
1537 | | // the last boosted frame. |
1538 | 0 | active_best_quality = |
1539 | 0 | VPXMAX(active_best_quality, |
1540 | 0 | cpi->twopass.last_qindex_of_arf_layer[max_layer_depth - 1]); |
1541 | 0 | } |
1542 | |
|
1543 | 0 | #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
1544 | 0 | vpx_clear_system_state(); |
1545 | | // Static forced key frames Q restrictions dealt with elsewhere. |
1546 | 0 | if (!frame_is_intra_only(cm) || !rc->this_key_frame_forced || |
1547 | 0 | cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH) { |
1548 | 0 | int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group_index], |
1549 | 0 | active_worst_quality); |
1550 | 0 | active_worst_quality = |
1551 | 0 | VPXMAX(active_worst_quality + qdelta, active_best_quality); |
1552 | 0 | } |
1553 | 0 | #endif |
1554 | | |
1555 | | // Modify active_best_quality for downscaled normal frames. |
1556 | 0 | if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) { |
1557 | 0 | int qdelta = vp9_compute_qdelta_by_rate( |
1558 | 0 | rc, cm->frame_type, active_best_quality, 2.0, cm->bit_depth); |
1559 | 0 | active_best_quality = |
1560 | 0 | VPXMAX(active_best_quality + qdelta, rc->best_quality); |
1561 | 0 | } |
1562 | |
|
1563 | 0 | active_best_quality = |
1564 | 0 | clamp(active_best_quality, rc->best_quality, rc->worst_quality); |
1565 | 0 | active_worst_quality = |
1566 | 0 | clamp(active_worst_quality, active_best_quality, rc->worst_quality); |
1567 | |
|
1568 | 0 | if (frame_is_intra_only(cm) && rc->this_key_frame_forced) { |
1569 | | // If static since last kf use better of last boosted and last kf q. |
1570 | 0 | if (cpi->twopass.last_kfgroup_zeromotion_pct >= STATIC_MOTION_THRESH) { |
1571 | 0 | q = VPXMIN(rc->last_kf_qindex, rc->last_boosted_qindex); |
1572 | 0 | } else { |
1573 | 0 | q = rc->last_boosted_qindex; |
1574 | 0 | } |
1575 | 0 | } else if (frame_is_intra_only(cm) && !rc->this_key_frame_forced) { |
1576 | 0 | q = active_best_quality; |
1577 | 0 | } else { |
1578 | 0 | q = vp9_rc_regulate_q(cpi, rc->this_frame_target, active_best_quality, |
1579 | 0 | active_worst_quality); |
1580 | 0 | if (q > active_worst_quality) { |
1581 | | // Special case when we are targeting the max allowed rate. |
1582 | 0 | if (rc->this_frame_target >= rc->max_frame_bandwidth) |
1583 | 0 | active_worst_quality = q; |
1584 | 0 | else |
1585 | 0 | q = active_worst_quality; |
1586 | 0 | } |
1587 | 0 | } |
1588 | |
|
1589 | 0 | *top_index = active_worst_quality; |
1590 | 0 | *bottom_index = active_best_quality; |
1591 | |
|
1592 | 0 | assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); |
1593 | 0 | assert(*bottom_index <= rc->worst_quality && |
1594 | 0 | *bottom_index >= rc->best_quality); |
1595 | 0 | assert(q <= rc->worst_quality && q >= rc->best_quality); |
1596 | 0 | return q; |
1597 | 0 | } |
1598 | | |
1599 | | int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, int *bottom_index, |
1600 | 42.2k | int *top_index) { |
1601 | 42.2k | int q; |
1602 | 42.2k | const int gf_group_index = cpi->twopass.gf_group.index; |
1603 | 42.2k | if (cpi->oxcf.pass == 0) { |
1604 | 42.2k | if (cpi->oxcf.rc_mode == VPX_CBR) |
1605 | 0 | q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index); |
1606 | 42.2k | else |
1607 | 42.2k | q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index); |
1608 | 42.2k | } else { |
1609 | 0 | q = vp9_rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index, |
1610 | 0 | gf_group_index); |
1611 | 0 | } |
1612 | 42.2k | if (cpi->sf.use_nonrd_pick_mode) { |
1613 | 0 | if (cpi->sf.force_frame_boost == 1) q -= cpi->sf.max_delta_qindex; |
1614 | |
|
1615 | 0 | if (q < *bottom_index) |
1616 | 0 | *bottom_index = q; |
1617 | 0 | else if (q > *top_index) |
1618 | 0 | *top_index = q; |
1619 | 0 | } |
1620 | 42.2k | return q; |
1621 | 42.2k | } |
1622 | | |
1623 | 0 | void vp9_configure_buffer_updates(VP9_COMP *cpi, int gf_group_index) { |
1624 | 0 | VP9_COMMON *cm = &cpi->common; |
1625 | 0 | TWO_PASS *const twopass = &cpi->twopass; |
1626 | |
|
1627 | 0 | cpi->rc.is_src_frame_alt_ref = 0; |
1628 | 0 | cm->show_existing_frame = 0; |
1629 | 0 | cpi->rc.show_arf_as_gld = 0; |
1630 | 0 | switch (twopass->gf_group.update_type[gf_group_index]) { |
1631 | 0 | case KF_UPDATE: |
1632 | 0 | cpi->refresh_last_frame = 1; |
1633 | 0 | cpi->refresh_golden_frame = 1; |
1634 | 0 | cpi->refresh_alt_ref_frame = 1; |
1635 | 0 | break; |
1636 | 0 | case LF_UPDATE: |
1637 | 0 | cpi->refresh_last_frame = 1; |
1638 | 0 | cpi->refresh_golden_frame = 0; |
1639 | 0 | cpi->refresh_alt_ref_frame = 0; |
1640 | 0 | break; |
1641 | 0 | case GF_UPDATE: |
1642 | 0 | cpi->refresh_last_frame = 1; |
1643 | 0 | cpi->refresh_golden_frame = 1; |
1644 | 0 | cpi->refresh_alt_ref_frame = 0; |
1645 | 0 | break; |
1646 | 0 | case OVERLAY_UPDATE: |
1647 | 0 | cpi->refresh_last_frame = 0; |
1648 | 0 | cpi->refresh_golden_frame = 1; |
1649 | 0 | cpi->refresh_alt_ref_frame = 0; |
1650 | 0 | cpi->rc.is_src_frame_alt_ref = 1; |
1651 | 0 | if (cpi->rc.preserve_arf_as_gld) { |
1652 | 0 | cpi->rc.show_arf_as_gld = 1; |
1653 | 0 | cpi->refresh_golden_frame = 0; |
1654 | 0 | cm->show_existing_frame = 1; |
1655 | 0 | cm->refresh_frame_context = 0; |
1656 | 0 | } |
1657 | 0 | break; |
1658 | 0 | case MID_OVERLAY_UPDATE: |
1659 | 0 | cpi->refresh_last_frame = 1; |
1660 | 0 | cpi->refresh_golden_frame = 0; |
1661 | 0 | cpi->refresh_alt_ref_frame = 0; |
1662 | 0 | cpi->rc.is_src_frame_alt_ref = 1; |
1663 | 0 | break; |
1664 | 0 | case USE_BUF_FRAME: |
1665 | 0 | cpi->refresh_last_frame = 0; |
1666 | 0 | cpi->refresh_golden_frame = 0; |
1667 | 0 | cpi->refresh_alt_ref_frame = 0; |
1668 | 0 | cpi->rc.is_src_frame_alt_ref = 1; |
1669 | 0 | cm->show_existing_frame = 1; |
1670 | 0 | cm->refresh_frame_context = 0; |
1671 | 0 | break; |
1672 | 0 | default: |
1673 | 0 | assert(twopass->gf_group.update_type[gf_group_index] == ARF_UPDATE); |
1674 | 0 | cpi->refresh_last_frame = 0; |
1675 | 0 | cpi->refresh_golden_frame = 0; |
1676 | 0 | cpi->refresh_alt_ref_frame = 1; |
1677 | 0 | break; |
1678 | 0 | } |
1679 | 0 | } |
1680 | | |
1681 | | void vp9_rc_compute_frame_size_bounds(const VP9_COMP *cpi, int frame_target, |
1682 | | int *frame_under_shoot_limit, |
1683 | 0 | int *frame_over_shoot_limit) { |
1684 | 0 | if (cpi->oxcf.rc_mode == VPX_Q) { |
1685 | 0 | *frame_under_shoot_limit = 0; |
1686 | 0 | *frame_over_shoot_limit = INT_MAX; |
1687 | 0 | } else { |
1688 | | // For very small rate targets where the fractional adjustment |
1689 | | // may be tiny make sure there is at least a minimum range. |
1690 | 0 | const int tol_low = |
1691 | 0 | (int)(((int64_t)cpi->sf.recode_tolerance_low * frame_target) / 100); |
1692 | 0 | const int tol_high = |
1693 | 0 | (int)(((int64_t)cpi->sf.recode_tolerance_high * frame_target) / 100); |
1694 | 0 | *frame_under_shoot_limit = VPXMAX(frame_target - tol_low - 100, 0); |
1695 | 0 | *frame_over_shoot_limit = |
1696 | 0 | VPXMIN(frame_target + tol_high + 100, cpi->rc.max_frame_bandwidth); |
1697 | 0 | } |
1698 | 0 | } |
1699 | | |
1700 | 43.5k | void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) { |
1701 | 43.5k | const VP9_COMMON *const cm = &cpi->common; |
1702 | 43.5k | RATE_CONTROL *const rc = &cpi->rc; |
1703 | | |
1704 | 43.5k | rc->this_frame_target = target; |
1705 | | |
1706 | | // Modify frame size target when down-scaling. |
1707 | 43.5k | if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && |
1708 | 43.5k | rc->frame_size_selector != UNSCALED) { |
1709 | 0 | rc->this_frame_target = (int)(rc->this_frame_target * |
1710 | 0 | rate_thresh_mult[rc->frame_size_selector]); |
1711 | 0 | } |
1712 | | |
1713 | | #if CONFIG_RATE_CTRL |
1714 | | if (cpi->oxcf.use_simple_encode_api) { |
1715 | | if (cpi->encode_command.use_external_target_frame_bits) { |
1716 | | rc->this_frame_target = cpi->encode_command.target_frame_bits; |
1717 | | } |
1718 | | } |
1719 | | #endif // CONFIG_RATE_CTRL |
1720 | | |
1721 | | // Target rate per SB64 (including partial SB64s. |
1722 | 43.5k | const int64_t sb64_target_rate = |
1723 | 43.5k | ((int64_t)rc->this_frame_target * 64 * 64) / (cm->width * cm->height); |
1724 | 43.5k | rc->sb64_target_rate = (int)VPXMIN(sb64_target_rate, INT_MAX); |
1725 | 43.5k | } |
1726 | | |
1727 | 0 | static void update_alt_ref_frame_stats(VP9_COMP *cpi) { |
1728 | | // this frame refreshes means next frames don't unless specified by user |
1729 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
1730 | 0 | rc->frames_since_golden = 0; |
1731 | | |
1732 | | // Mark the alt ref as done (setting to 0 means no further alt refs pending). |
1733 | 0 | rc->source_alt_ref_pending = 0; |
1734 | | |
1735 | | // Set the alternate reference frame active flag |
1736 | 0 | rc->source_alt_ref_active = 1; |
1737 | 0 | } |
1738 | | |
1739 | 42.2k | static void update_golden_frame_stats(VP9_COMP *cpi) { |
1740 | 42.2k | RATE_CONTROL *const rc = &cpi->rc; |
1741 | | |
1742 | | // Update the Golden frame usage counts. |
1743 | 42.2k | if (cpi->refresh_golden_frame) { |
1744 | | // this frame refreshes means next frames don't unless specified by user |
1745 | 10.0k | rc->frames_since_golden = 0; |
1746 | | |
1747 | | // If we are not using alt ref in the up and coming group clear the arf |
1748 | | // active flag. In multi arf group case, if the index is not 0 then |
1749 | | // we are overlaying a mid group arf so should not reset the flag. |
1750 | 10.0k | if (cpi->oxcf.pass == 2) { |
1751 | 0 | if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0)) |
1752 | 0 | rc->source_alt_ref_active = 0; |
1753 | 10.0k | } else if (!rc->source_alt_ref_pending) { |
1754 | 10.0k | rc->source_alt_ref_active = 0; |
1755 | 10.0k | } |
1756 | | |
1757 | | // Decrement count down till next gf |
1758 | 10.0k | if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--; |
1759 | | |
1760 | 32.1k | } else if (!cpi->refresh_alt_ref_frame) { |
1761 | | // Decrement count down till next gf |
1762 | 32.1k | if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--; |
1763 | | |
1764 | 32.1k | rc->frames_since_golden++; |
1765 | | |
1766 | 32.1k | if (rc->show_arf_as_gld) { |
1767 | 0 | rc->frames_since_golden = 0; |
1768 | | // If we are not using alt ref in the up and coming group clear the arf |
1769 | | // active flag. In multi arf group case, if the index is not 0 then |
1770 | | // we are overlaying a mid group arf so should not reset the flag. |
1771 | 0 | if (!rc->source_alt_ref_pending && (cpi->twopass.gf_group.index == 0)) |
1772 | 0 | rc->source_alt_ref_active = 0; |
1773 | 0 | } |
1774 | 32.1k | } |
1775 | 42.2k | } |
1776 | | |
1777 | 0 | static void update_altref_usage(VP9_COMP *const cpi) { |
1778 | 0 | VP9_COMMON *const cm = &cpi->common; |
1779 | 0 | int sum_ref_frame_usage = 0; |
1780 | 0 | int arf_frame_usage = 0; |
1781 | 0 | int mi_row, mi_col; |
1782 | 0 | if (cpi->rc.alt_ref_gf_group && !cpi->rc.is_src_frame_alt_ref && |
1783 | 0 | !cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame) |
1784 | 0 | for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8) { |
1785 | 0 | for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8) { |
1786 | 0 | int sboffset = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3); |
1787 | 0 | sum_ref_frame_usage += cpi->count_arf_frame_usage[sboffset] + |
1788 | 0 | cpi->count_lastgolden_frame_usage[sboffset]; |
1789 | 0 | arf_frame_usage += cpi->count_arf_frame_usage[sboffset]; |
1790 | 0 | } |
1791 | 0 | } |
1792 | 0 | if (sum_ref_frame_usage > 0) { |
1793 | 0 | double altref_count = 100.0 * arf_frame_usage / sum_ref_frame_usage; |
1794 | 0 | cpi->rc.perc_arf_usage = |
1795 | 0 | 0.75 * cpi->rc.perc_arf_usage + 0.25 * altref_count; |
1796 | 0 | } |
1797 | 0 | } |
1798 | | |
1799 | 34.4k | void vp9_compute_frame_low_motion(VP9_COMP *const cpi) { |
1800 | 34.4k | VP9_COMMON *const cm = &cpi->common; |
1801 | 34.4k | SVC *const svc = &cpi->svc; |
1802 | 34.4k | int mi_row, mi_col; |
1803 | 34.4k | MODE_INFO **mi = cm->mi_grid_visible; |
1804 | 34.4k | RATE_CONTROL *const rc = &cpi->rc; |
1805 | 34.4k | const int rows = cm->mi_rows, cols = cm->mi_cols; |
1806 | 34.4k | int cnt_zeromv = 0; |
1807 | 281k | for (mi_row = 0; mi_row < rows; mi_row++) { |
1808 | 2.35M | for (mi_col = 0; mi_col < cols; mi_col++) { |
1809 | 2.11M | if (mi[0]->ref_frame[0] == LAST_FRAME && |
1810 | 2.11M | abs(mi[0]->mv[0].as_mv.row) < 16 && abs(mi[0]->mv[0].as_mv.col) < 16) |
1811 | 115k | cnt_zeromv++; |
1812 | 2.11M | mi++; |
1813 | 2.11M | } |
1814 | 247k | mi += 8; |
1815 | 247k | } |
1816 | 34.4k | cnt_zeromv = 100 * cnt_zeromv / (rows * cols); |
1817 | 34.4k | rc->avg_frame_low_motion = (3 * rc->avg_frame_low_motion + cnt_zeromv) >> 2; |
1818 | | |
1819 | | // For SVC: set avg_frame_low_motion (only computed on top spatial layer) |
1820 | | // to all lower spatial layers. |
1821 | 34.4k | if (cpi->use_svc && svc->spatial_layer_id == svc->number_spatial_layers - 1) { |
1822 | 0 | int i; |
1823 | 0 | for (i = 0; i < svc->number_spatial_layers - 1; ++i) { |
1824 | 0 | const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, |
1825 | 0 | svc->number_temporal_layers); |
1826 | 0 | LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
1827 | 0 | RATE_CONTROL *const lrc = &lc->rc; |
1828 | 0 | lrc->avg_frame_low_motion = rc->avg_frame_low_motion; |
1829 | 0 | } |
1830 | 0 | } |
1831 | 34.4k | } |
1832 | | |
1833 | 42.2k | void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { |
1834 | 42.2k | const VP9_COMMON *const cm = &cpi->common; |
1835 | 42.2k | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
1836 | 42.2k | RATE_CONTROL *const rc = &cpi->rc; |
1837 | 42.2k | SVC *const svc = &cpi->svc; |
1838 | 42.2k | const int qindex = cm->base_qindex; |
1839 | 42.2k | const GF_GROUP *gf_group = &cpi->twopass.gf_group; |
1840 | 42.2k | const int gf_group_index = cpi->twopass.gf_group.index; |
1841 | 42.2k | const int layer_depth = gf_group->layer_depth[gf_group_index]; |
1842 | | |
1843 | | // Update rate control heuristics |
1844 | 42.2k | rc->projected_frame_size = (int)(bytes_used << 3); |
1845 | | |
1846 | | // Post encode loop adjustment of Q prediction. |
1847 | 42.2k | vp9_rc_update_rate_correction_factors(cpi); |
1848 | | |
1849 | | // Keep a record of last Q and ambient average Q. |
1850 | 42.2k | if (frame_is_intra_only(cm)) { |
1851 | 7.70k | rc->last_q[KEY_FRAME] = qindex; |
1852 | 7.70k | rc->avg_frame_qindex[KEY_FRAME] = |
1853 | 7.70k | ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); |
1854 | 7.70k | if (cpi->use_svc) { |
1855 | 0 | int i; |
1856 | 0 | for (i = 0; i < svc->number_temporal_layers; ++i) { |
1857 | 0 | const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, |
1858 | 0 | svc->number_temporal_layers); |
1859 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
1860 | 0 | RATE_CONTROL *lrc = &lc->rc; |
1861 | 0 | lrc->last_q[KEY_FRAME] = rc->last_q[KEY_FRAME]; |
1862 | 0 | lrc->avg_frame_qindex[KEY_FRAME] = rc->avg_frame_qindex[KEY_FRAME]; |
1863 | 0 | } |
1864 | 0 | } |
1865 | 34.4k | } else { |
1866 | 34.4k | if ((cpi->use_svc) || |
1867 | 34.4k | (!rc->is_src_frame_alt_ref && |
1868 | 34.4k | !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { |
1869 | 32.1k | rc->last_q[INTER_FRAME] = qindex; |
1870 | 32.1k | rc->avg_frame_qindex[INTER_FRAME] = |
1871 | 32.1k | ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); |
1872 | 32.1k | rc->ni_frames++; |
1873 | 32.1k | rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1874 | 32.1k | rc->avg_q = rc->tot_q / rc->ni_frames; |
1875 | | // Calculate the average Q for normal inter frames (not key or GFU |
1876 | | // frames). |
1877 | 32.1k | rc->ni_tot_qi += qindex; |
1878 | 32.1k | rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; |
1879 | 32.1k | } |
1880 | 34.4k | } |
1881 | | |
1882 | 42.2k | if (cpi->use_svc) vp9_svc_adjust_avg_frame_qindex(cpi); |
1883 | | |
1884 | | // Keep record of last boosted (KF/KF/ARF) Q value. |
1885 | | // If the current frame is coded at a lower Q then we also update it. |
1886 | | // If all mbs in this group are skipped only update if the Q value is |
1887 | | // better than that already stored. |
1888 | | // This is used to help set quality in forced key frames to reduce popping |
1889 | 42.2k | if ((qindex < rc->last_boosted_qindex) || (cm->frame_type == KEY_FRAME) || |
1890 | 42.2k | (!rc->constrained_gf_group && |
1891 | 33.1k | (cpi->refresh_alt_ref_frame || |
1892 | 30.5k | (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { |
1893 | 10.6k | rc->last_boosted_qindex = qindex; |
1894 | 10.6k | } |
1895 | | |
1896 | 42.2k | if ((qindex < cpi->twopass.last_qindex_of_arf_layer[layer_depth]) || |
1897 | 42.2k | (cm->frame_type == KEY_FRAME) || |
1898 | 42.2k | (!rc->constrained_gf_group && |
1899 | 33.1k | (cpi->refresh_alt_ref_frame || |
1900 | 30.5k | (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { |
1901 | 10.6k | cpi->twopass.last_qindex_of_arf_layer[layer_depth] = qindex; |
1902 | 10.6k | } |
1903 | | |
1904 | 42.2k | if (frame_is_intra_only(cm)) rc->last_kf_qindex = qindex; |
1905 | | |
1906 | 42.2k | update_buffer_level_postencode(cpi, rc->projected_frame_size); |
1907 | | |
1908 | | // Rolling monitors of whether we are over or underspending used to help |
1909 | | // regulate min and Max Q in two pass. |
1910 | 42.2k | if (!frame_is_intra_only(cm)) { |
1911 | 34.4k | rc->rolling_target_bits = (int)ROUND64_POWER_OF_TWO( |
1912 | 34.4k | (int64_t)rc->rolling_target_bits * 3 + rc->this_frame_target, 2); |
1913 | 34.4k | rc->rolling_actual_bits = (int)ROUND64_POWER_OF_TWO( |
1914 | 34.4k | (int64_t)rc->rolling_actual_bits * 3 + rc->projected_frame_size, 2); |
1915 | 34.4k | rc->long_rolling_target_bits = (int)ROUND64_POWER_OF_TWO( |
1916 | 34.4k | (int64_t)rc->long_rolling_target_bits * 31 + rc->this_frame_target, 5); |
1917 | 34.4k | rc->long_rolling_actual_bits = (int)ROUND64_POWER_OF_TWO( |
1918 | 34.4k | (int64_t)rc->long_rolling_actual_bits * 31 + rc->projected_frame_size, |
1919 | 34.4k | 5); |
1920 | 34.4k | } |
1921 | | |
1922 | | // Actual bits spent |
1923 | 42.2k | rc->total_actual_bits += rc->projected_frame_size; |
1924 | 42.2k | rc->total_target_bits += cm->show_frame ? rc->avg_frame_bandwidth : 0; |
1925 | | |
1926 | 42.2k | rc->total_target_vs_actual = rc->total_actual_bits - rc->total_target_bits; |
1927 | | |
1928 | 42.2k | if (!cpi->use_svc) { |
1929 | 42.2k | if (is_altref_enabled(cpi) && cpi->refresh_alt_ref_frame && |
1930 | 42.2k | (!frame_is_intra_only(cm))) |
1931 | | // Update the alternate reference frame stats as appropriate. |
1932 | 0 | update_alt_ref_frame_stats(cpi); |
1933 | 42.2k | else |
1934 | | // Update the Golden frame stats as appropriate. |
1935 | 42.2k | update_golden_frame_stats(cpi); |
1936 | 42.2k | } |
1937 | | |
1938 | | // If second (long term) temporal reference is used for SVC, |
1939 | | // update the golden frame counter, only for base temporal layer. |
1940 | 42.2k | if (cpi->use_svc && svc->use_gf_temporal_ref_current_layer && |
1941 | 42.2k | svc->temporal_layer_id == 0) { |
1942 | 0 | int i = 0; |
1943 | 0 | if (cpi->refresh_golden_frame) |
1944 | 0 | rc->frames_since_golden = 0; |
1945 | 0 | else |
1946 | 0 | rc->frames_since_golden++; |
1947 | | // Decrement count down till next gf |
1948 | 0 | if (rc->frames_till_gf_update_due > 0) rc->frames_till_gf_update_due--; |
1949 | | // Update the frames_since_golden for all upper temporal layers. |
1950 | 0 | for (i = 1; i < svc->number_temporal_layers; ++i) { |
1951 | 0 | const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, |
1952 | 0 | svc->number_temporal_layers); |
1953 | 0 | LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
1954 | 0 | RATE_CONTROL *const lrc = &lc->rc; |
1955 | 0 | lrc->frames_since_golden = rc->frames_since_golden; |
1956 | 0 | } |
1957 | 0 | } |
1958 | | |
1959 | 42.2k | if (frame_is_intra_only(cm)) rc->frames_since_key = 0; |
1960 | 42.2k | if (cm->show_frame) { |
1961 | 42.2k | rc->frames_since_key++; |
1962 | 42.2k | rc->frames_to_key--; |
1963 | 42.2k | } |
1964 | | |
1965 | | // Trigger the resizing of the next frame if it is scaled. |
1966 | 42.2k | if (oxcf->pass != 0) { |
1967 | 0 | cpi->resize_pending = |
1968 | 0 | rc->next_frame_size_selector != rc->frame_size_selector; |
1969 | 0 | rc->frame_size_selector = rc->next_frame_size_selector; |
1970 | 0 | } |
1971 | | |
1972 | 42.2k | if (oxcf->pass == 0) { |
1973 | 42.2k | if (!frame_is_intra_only(cm)) |
1974 | 34.4k | if (cpi->sf.use_altref_onepass) update_altref_usage(cpi); |
1975 | 42.2k | cpi->rc.last_frame_is_src_altref = cpi->rc.is_src_frame_alt_ref; |
1976 | 42.2k | } |
1977 | | |
1978 | 42.2k | if (!frame_is_intra_only(cm)) rc->reset_high_source_sad = 0; |
1979 | | |
1980 | 42.2k | rc->last_avg_frame_bandwidth = rc->avg_frame_bandwidth; |
1981 | 42.2k | if (cpi->use_svc && svc->spatial_layer_id < svc->number_spatial_layers - 1) |
1982 | 0 | svc->lower_layer_qindex = cm->base_qindex; |
1983 | 42.2k | cpi->deadline_mode_previous_frame = cpi->oxcf.mode; |
1984 | 42.2k | } |
1985 | | |
1986 | 0 | void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) { |
1987 | 0 | cpi->common.current_video_frame++; |
1988 | 0 | cpi->rc.frames_since_key++; |
1989 | 0 | cpi->rc.frames_to_key--; |
1990 | 0 | cpi->rc.rc_2_frame = 0; |
1991 | 0 | cpi->rc.rc_1_frame = 0; |
1992 | 0 | cpi->rc.last_avg_frame_bandwidth = cpi->rc.avg_frame_bandwidth; |
1993 | 0 | cpi->rc.last_q[INTER_FRAME] = cpi->common.base_qindex; |
1994 | | // For SVC on dropped frame when framedrop_mode != LAYER_DROP: |
1995 | | // in this mode the whole superframe may be dropped if only a single layer |
1996 | | // has buffer underflow (below threshold). Since this can then lead to |
1997 | | // increasing buffer levels/overflow for certain layers even though whole |
1998 | | // superframe is dropped, we cap buffer level if its already stable. |
1999 | 0 | if (cpi->use_svc && cpi->svc.framedrop_mode != LAYER_DROP && |
2000 | 0 | cpi->rc.buffer_level > cpi->rc.optimal_buffer_level) { |
2001 | 0 | cpi->rc.buffer_level = cpi->rc.optimal_buffer_level; |
2002 | 0 | cpi->rc.bits_off_target = cpi->rc.optimal_buffer_level; |
2003 | 0 | } |
2004 | 0 | cpi->deadline_mode_previous_frame = cpi->oxcf.mode; |
2005 | 0 | } |
2006 | | |
2007 | 35.7k | int vp9_calc_pframe_target_size_one_pass_vbr(const VP9_COMP *cpi) { |
2008 | 35.7k | const RATE_CONTROL *const rc = &cpi->rc; |
2009 | 35.7k | const int af_ratio = rc->af_ratio_onepass_vbr; |
2010 | 35.7k | int64_t target = |
2011 | 35.7k | (!rc->is_src_frame_alt_ref && |
2012 | 35.7k | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) |
2013 | 35.7k | ? ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval * |
2014 | 3.63k | af_ratio) / |
2015 | 3.63k | (rc->baseline_gf_interval + af_ratio - 1) |
2016 | 35.7k | : ((int64_t)rc->avg_frame_bandwidth * rc->baseline_gf_interval) / |
2017 | 32.1k | (rc->baseline_gf_interval + af_ratio - 1); |
2018 | | // For SVC: refresh flags are used to define the pattern, so we can't |
2019 | | // use that for boosting the target size here. |
2020 | | // TODO(marpan): Consider adding internal boost on TL0 for VBR-SVC. |
2021 | | // For now just use the CBR logic for setting target size. |
2022 | 35.7k | if (cpi->use_svc) target = vp9_calc_pframe_target_size_one_pass_cbr(cpi); |
2023 | 35.7k | if (target > INT_MAX) target = INT_MAX; |
2024 | 35.7k | return vp9_rc_clamp_pframe_target_size(cpi, (int)target); |
2025 | 35.7k | } |
2026 | | |
2027 | 7.75k | int vp9_calc_iframe_target_size_one_pass_vbr(const VP9_COMP *cpi) { |
2028 | 7.75k | static const int kf_ratio = 25; |
2029 | 7.75k | const RATE_CONTROL *rc = &cpi->rc; |
2030 | 7.75k | int target = rc->avg_frame_bandwidth; |
2031 | 7.75k | if (target > INT_MAX / kf_ratio) |
2032 | 19 | target = INT_MAX; |
2033 | 7.73k | else |
2034 | 7.73k | target = rc->avg_frame_bandwidth * kf_ratio; |
2035 | 7.75k | return vp9_rc_clamp_iframe_target_size(cpi, target); |
2036 | 7.75k | } |
2037 | | |
2038 | 11.3k | static void adjust_gfint_frame_constraint(VP9_COMP *cpi, int frame_constraint) { |
2039 | 11.3k | RATE_CONTROL *const rc = &cpi->rc; |
2040 | 11.3k | rc->constrained_gf_group = 0; |
2041 | | // Reset gf interval to make more equal spacing for frame_constraint. |
2042 | 11.3k | if ((frame_constraint <= 7 * rc->baseline_gf_interval >> 2) && |
2043 | 11.3k | (frame_constraint > rc->baseline_gf_interval)) { |
2044 | 108 | rc->baseline_gf_interval = frame_constraint >> 1; |
2045 | 108 | if (rc->baseline_gf_interval < 5) |
2046 | 46 | rc->baseline_gf_interval = frame_constraint; |
2047 | 108 | rc->constrained_gf_group = 1; |
2048 | 11.2k | } else { |
2049 | | // Reset to keep gf_interval <= frame_constraint. |
2050 | 11.2k | if (rc->baseline_gf_interval > frame_constraint) { |
2051 | 5.55k | rc->baseline_gf_interval = frame_constraint; |
2052 | 5.55k | rc->constrained_gf_group = 1; |
2053 | 5.55k | } |
2054 | 11.2k | } |
2055 | 11.3k | } |
2056 | | |
2057 | 42.2k | void vp9_set_gf_update_one_pass_vbr(VP9_COMP *const cpi) { |
2058 | 42.2k | RATE_CONTROL *const rc = &cpi->rc; |
2059 | 42.2k | VP9_COMMON *const cm = &cpi->common; |
2060 | 42.2k | if (rc->frames_till_gf_update_due == 0) { |
2061 | 10.1k | double rate_err = 1.0; |
2062 | 10.1k | rc->gfu_boost = DEFAULT_GF_BOOST; |
2063 | 10.1k | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) { |
2064 | 0 | vp9_cyclic_refresh_set_golden_update(cpi); |
2065 | 10.1k | } else { |
2066 | 10.1k | rc->baseline_gf_interval = VPXMIN( |
2067 | 10.1k | 20, VPXMAX(10, (rc->min_gf_interval + rc->max_gf_interval) / 2)); |
2068 | 10.1k | } |
2069 | 10.1k | rc->af_ratio_onepass_vbr = 10; |
2070 | 10.1k | if (rc->rolling_target_bits > 0) |
2071 | 8.89k | rate_err = |
2072 | 8.89k | (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits; |
2073 | 10.1k | if (cm->current_video_frame > 30) { |
2074 | 2.88k | if (rc->avg_frame_qindex[INTER_FRAME] > (7 * rc->worst_quality) >> 3 && |
2075 | 2.88k | rate_err > 3.5) { |
2076 | 81 | rc->baseline_gf_interval = |
2077 | 81 | VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1); |
2078 | 2.79k | } else if (rc->avg_frame_low_motion > 0 && |
2079 | 2.79k | rc->avg_frame_low_motion < 20) { |
2080 | | // Decrease gf interval for high motion case. |
2081 | 421 | rc->baseline_gf_interval = VPXMAX(6, rc->baseline_gf_interval >> 1); |
2082 | 421 | } |
2083 | | // Adjust boost and af_ratio based on avg_frame_low_motion, which |
2084 | | // varies between 0 and 100 (stationary, 100% zero/small motion). |
2085 | 2.88k | if (rc->avg_frame_low_motion > 0) |
2086 | 722 | rc->gfu_boost = |
2087 | 722 | VPXMAX(500, DEFAULT_GF_BOOST * (rc->avg_frame_low_motion << 1) / |
2088 | 2.88k | (rc->avg_frame_low_motion + 100)); |
2089 | 2.15k | else if (rc->avg_frame_low_motion == 0 && rate_err > 1.0) |
2090 | 220 | rc->gfu_boost = DEFAULT_GF_BOOST >> 1; |
2091 | 2.88k | rc->af_ratio_onepass_vbr = VPXMIN(15, VPXMAX(5, 3 * rc->gfu_boost / 400)); |
2092 | 2.88k | } |
2093 | 10.1k | if (rc->constrain_gf_key_freq_onepass_vbr) |
2094 | 10.1k | adjust_gfint_frame_constraint(cpi, rc->frames_to_key); |
2095 | 10.1k | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
2096 | 10.1k | cpi->refresh_golden_frame = 1; |
2097 | 10.1k | rc->source_alt_ref_pending = 0; |
2098 | 10.1k | rc->alt_ref_gf_group = 0; |
2099 | 10.1k | if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) { |
2100 | 0 | rc->source_alt_ref_pending = 1; |
2101 | 0 | rc->alt_ref_gf_group = 1; |
2102 | 0 | } |
2103 | 10.1k | } |
2104 | 42.2k | } |
2105 | | |
2106 | 42.2k | void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) { |
2107 | 42.2k | VP9_COMMON *const cm = &cpi->common; |
2108 | 42.2k | RATE_CONTROL *const rc = &cpi->rc; |
2109 | 42.2k | int target; |
2110 | 42.2k | if (!cpi->refresh_alt_ref_frame && |
2111 | 42.2k | (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY) || |
2112 | 42.2k | rc->frames_to_key == 0 || |
2113 | 42.2k | (cpi->oxcf.mode != cpi->deadline_mode_previous_frame))) { |
2114 | 7.75k | cm->frame_type = KEY_FRAME; |
2115 | 7.75k | rc->this_key_frame_forced = |
2116 | 7.75k | cm->current_video_frame != 0 && rc->frames_to_key == 0; |
2117 | 7.75k | rc->frames_to_key = cpi->oxcf.key_freq; |
2118 | 7.75k | rc->kf_boost = DEFAULT_KF_BOOST; |
2119 | 7.75k | rc->source_alt_ref_active = 0; |
2120 | 34.5k | } else { |
2121 | 34.5k | cm->frame_type = INTER_FRAME; |
2122 | 34.5k | } |
2123 | 42.2k | vp9_set_gf_update_one_pass_vbr(cpi); |
2124 | 42.2k | if (cm->frame_type == KEY_FRAME) |
2125 | 7.75k | target = vp9_calc_iframe_target_size_one_pass_vbr(cpi); |
2126 | 34.5k | else |
2127 | 34.5k | target = vp9_calc_pframe_target_size_one_pass_vbr(cpi); |
2128 | 42.2k | vp9_rc_set_frame_target(cpi, target); |
2129 | 42.2k | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.pass == 0) |
2130 | 0 | vp9_cyclic_refresh_update_parameters(cpi); |
2131 | 42.2k | } |
2132 | | |
2133 | 0 | int vp9_calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { |
2134 | 0 | const VP9EncoderConfig *oxcf = &cpi->oxcf; |
2135 | 0 | const RATE_CONTROL *rc = &cpi->rc; |
2136 | 0 | const SVC *const svc = &cpi->svc; |
2137 | 0 | const int64_t diff = rc->optimal_buffer_level - rc->buffer_level; |
2138 | 0 | const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100; |
2139 | 0 | int min_frame_target = |
2140 | 0 | VPXMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS); |
2141 | 0 | int target; |
2142 | |
|
2143 | 0 | if (oxcf->gf_cbr_boost_pct) { |
2144 | 0 | const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100; |
2145 | 0 | target = cpi->refresh_golden_frame |
2146 | 0 | ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * |
2147 | 0 | af_ratio_pct) / |
2148 | 0 | (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) |
2149 | 0 | : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) / |
2150 | 0 | (rc->baseline_gf_interval * 100 + af_ratio_pct - 100); |
2151 | 0 | } else { |
2152 | 0 | target = rc->avg_frame_bandwidth; |
2153 | 0 | } |
2154 | 0 | if (is_one_pass_svc(cpi)) { |
2155 | | // Note that for layers, avg_frame_bandwidth is the cumulative |
2156 | | // per-frame-bandwidth. For the target size of this frame, use the |
2157 | | // layer average frame size (i.e., non-cumulative per-frame-bw). |
2158 | 0 | int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id, |
2159 | 0 | svc->number_temporal_layers); |
2160 | 0 | const LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
2161 | 0 | target = lc->avg_frame_size; |
2162 | 0 | min_frame_target = VPXMAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); |
2163 | 0 | } |
2164 | 0 | if (diff > 0) { |
2165 | | // Lower the target bandwidth for this frame. |
2166 | 0 | const int pct_low = (int)VPXMIN(diff / one_pct_bits, oxcf->under_shoot_pct); |
2167 | 0 | target -= (int)(((int64_t)target * pct_low) / 200); |
2168 | 0 | } else if (diff < 0) { |
2169 | | // Increase the target bandwidth for this frame. |
2170 | 0 | const int pct_high = |
2171 | 0 | (int)VPXMIN(-diff / one_pct_bits, oxcf->over_shoot_pct); |
2172 | 0 | target += (int)(((int64_t)target * pct_high) / 200); |
2173 | 0 | } |
2174 | 0 | if (oxcf->rc_max_inter_bitrate_pct) { |
2175 | 0 | const int max_rate = |
2176 | 0 | rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100; |
2177 | 0 | target = VPXMIN(target, max_rate); |
2178 | 0 | } |
2179 | 0 | return VPXMAX(min_frame_target, target); |
2180 | 0 | } |
2181 | | |
2182 | 0 | int vp9_calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { |
2183 | 0 | const RATE_CONTROL *rc = &cpi->rc; |
2184 | 0 | const VP9EncoderConfig *oxcf = &cpi->oxcf; |
2185 | 0 | const SVC *const svc = &cpi->svc; |
2186 | 0 | int64_t target; |
2187 | 0 | if (cpi->common.current_video_frame == 0) { |
2188 | 0 | target = rc->starting_buffer_level / 2; |
2189 | 0 | } else { |
2190 | 0 | int kf_boost = 32; |
2191 | 0 | double framerate = cpi->framerate; |
2192 | 0 | if (svc->number_temporal_layers > 1 && oxcf->rc_mode == VPX_CBR) { |
2193 | | // Use the layer framerate for temporal layers CBR mode. |
2194 | 0 | const int layer = |
2195 | 0 | LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id, |
2196 | 0 | svc->number_temporal_layers); |
2197 | 0 | const LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
2198 | 0 | framerate = lc->framerate; |
2199 | 0 | } |
2200 | 0 | kf_boost = VPXMAX(kf_boost, (int)round(2 * framerate - 16)); |
2201 | 0 | if (rc->frames_since_key < framerate / 2) { |
2202 | 0 | kf_boost = (int)round(kf_boost * rc->frames_since_key / (framerate / 2)); |
2203 | 0 | } |
2204 | |
|
2205 | 0 | target = ((int64_t)(16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; |
2206 | 0 | } |
2207 | 0 | target = VPXMIN(INT_MAX, target); |
2208 | 0 | return vp9_rc_clamp_iframe_target_size(cpi, (int)target); |
2209 | 0 | } |
2210 | | |
2211 | 0 | static void set_intra_only_frame(VP9_COMP *cpi) { |
2212 | 0 | VP9_COMMON *const cm = &cpi->common; |
2213 | 0 | SVC *const svc = &cpi->svc; |
2214 | | // Don't allow intra_only frame for bypass/flexible SVC mode, or if number |
2215 | | // of spatial layers is 1 or if number of spatial or temporal layers > 3. |
2216 | | // Also if intra-only is inserted on very first frame, don't allow if |
2217 | | // if number of temporal layers > 1. This is because on intra-only frame |
2218 | | // only 3 reference buffers can be updated, but for temporal layers > 1 |
2219 | | // we generally need to use buffer slots 4 and 5. |
2220 | 0 | if ((cm->current_video_frame == 0 && svc->number_temporal_layers > 1) || |
2221 | 0 | svc->number_spatial_layers > 3 || svc->number_temporal_layers > 3 || |
2222 | 0 | svc->number_spatial_layers == 1) |
2223 | 0 | return; |
2224 | 0 | cm->show_frame = 0; |
2225 | 0 | cm->intra_only = 1; |
2226 | 0 | cm->frame_type = INTER_FRAME; |
2227 | 0 | cpi->ext_refresh_frame_flags_pending = 1; |
2228 | 0 | cpi->ext_refresh_last_frame = 1; |
2229 | 0 | cpi->ext_refresh_golden_frame = 1; |
2230 | 0 | cpi->ext_refresh_alt_ref_frame = 1; |
2231 | 0 | if (cm->current_video_frame == 0) { |
2232 | 0 | cpi->lst_fb_idx = 0; |
2233 | 0 | cpi->gld_fb_idx = 1; |
2234 | 0 | cpi->alt_fb_idx = 2; |
2235 | 0 | } else { |
2236 | 0 | int i; |
2237 | 0 | int count = 0; |
2238 | 0 | cpi->lst_fb_idx = -1; |
2239 | 0 | cpi->gld_fb_idx = -1; |
2240 | 0 | cpi->alt_fb_idx = -1; |
2241 | 0 | svc->update_buffer_slot[0] = 0; |
2242 | | // For intra-only frame we need to refresh all slots that were |
2243 | | // being used for the base layer (fb_idx_base[i] == 1). |
2244 | | // Start with assigning last first, then golden and then alt. |
2245 | 0 | for (i = 0; i < REF_FRAMES; ++i) { |
2246 | 0 | if (svc->fb_idx_base[i] == 1) { |
2247 | 0 | svc->update_buffer_slot[0] |= 1 << i; |
2248 | 0 | count++; |
2249 | 0 | } |
2250 | 0 | if (count == 1 && cpi->lst_fb_idx == -1) cpi->lst_fb_idx = i; |
2251 | 0 | if (count == 2 && cpi->gld_fb_idx == -1) cpi->gld_fb_idx = i; |
2252 | 0 | if (count == 3 && cpi->alt_fb_idx == -1) cpi->alt_fb_idx = i; |
2253 | 0 | } |
2254 | | // If golden or alt is not being used for base layer, then set them |
2255 | | // to the lst_fb_idx. |
2256 | 0 | if (cpi->gld_fb_idx == -1) cpi->gld_fb_idx = cpi->lst_fb_idx; |
2257 | 0 | if (cpi->alt_fb_idx == -1) cpi->alt_fb_idx = cpi->lst_fb_idx; |
2258 | 0 | if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) { |
2259 | 0 | cpi->ext_refresh_last_frame = 0; |
2260 | 0 | cpi->ext_refresh_golden_frame = 0; |
2261 | 0 | cpi->ext_refresh_alt_ref_frame = 0; |
2262 | 0 | cpi->ref_frame_flags = 0; |
2263 | 0 | } |
2264 | 0 | } |
2265 | 0 | } |
2266 | | |
2267 | 0 | void vp9_rc_get_svc_params(VP9_COMP *cpi) { |
2268 | 0 | VP9_COMMON *const cm = &cpi->common; |
2269 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2270 | 0 | SVC *const svc = &cpi->svc; |
2271 | 0 | int target = rc->avg_frame_bandwidth; |
2272 | 0 | int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id, |
2273 | 0 | svc->number_temporal_layers); |
2274 | 0 | if (svc->first_spatial_layer_to_encode) |
2275 | 0 | svc->layer_context[svc->temporal_layer_id].is_key_frame = 0; |
2276 | | // Periodic key frames is based on the super-frame counter |
2277 | | // (svc.current_superframe), also only base spatial layer is key frame. |
2278 | | // Key frame is set for any of the following: very first frame, frame flags |
2279 | | // indicates key, superframe counter hits key frequency,(non-intra) sync |
2280 | | // flag is set for spatial layer 0, or deadline mode changes. |
2281 | 0 | if ((cm->current_video_frame == 0 && !svc->previous_frame_is_intra_only) || |
2282 | 0 | (cpi->frame_flags & FRAMEFLAGS_KEY) || |
2283 | 0 | (cpi->oxcf.auto_key && |
2284 | 0 | (svc->current_superframe % cpi->oxcf.key_freq == 0) && |
2285 | 0 | !svc->previous_frame_is_intra_only && svc->spatial_layer_id == 0) || |
2286 | 0 | (svc->spatial_layer_sync[0] == 1 && svc->spatial_layer_id == 0) || |
2287 | 0 | (cpi->oxcf.mode != cpi->deadline_mode_previous_frame)) { |
2288 | 0 | cm->frame_type = KEY_FRAME; |
2289 | 0 | rc->source_alt_ref_active = 0; |
2290 | 0 | if (is_one_pass_svc(cpi)) { |
2291 | 0 | if (cm->current_video_frame > 0) vp9_svc_reset_temporal_layers(cpi, 1); |
2292 | 0 | layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id, |
2293 | 0 | svc->number_temporal_layers); |
2294 | 0 | svc->layer_context[layer].is_key_frame = 1; |
2295 | 0 | cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
2296 | | // Assumption here is that LAST_FRAME is being updated for a keyframe. |
2297 | | // Thus no change in update flags. |
2298 | 0 | if (cpi->oxcf.rc_mode == VPX_CBR) |
2299 | 0 | target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); |
2300 | 0 | else |
2301 | 0 | target = vp9_calc_iframe_target_size_one_pass_vbr(cpi); |
2302 | 0 | } |
2303 | 0 | } else { |
2304 | 0 | cm->frame_type = INTER_FRAME; |
2305 | 0 | if (is_one_pass_svc(cpi)) { |
2306 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
2307 | | // Add condition current_video_frame > 0 for the case where first frame |
2308 | | // is intra only followed by overlay/copy frame. In this case we don't |
2309 | | // want to reset is_key_frame to 0 on overlay/copy frame. |
2310 | 0 | lc->is_key_frame = |
2311 | 0 | (svc->spatial_layer_id == 0 && cm->current_video_frame > 0) |
2312 | 0 | ? 0 |
2313 | 0 | : svc->layer_context[svc->temporal_layer_id].is_key_frame; |
2314 | 0 | if (cpi->oxcf.rc_mode == VPX_CBR) { |
2315 | 0 | target = vp9_calc_pframe_target_size_one_pass_cbr(cpi); |
2316 | 0 | } else { |
2317 | 0 | double rate_err = 0.0; |
2318 | 0 | rc->fac_active_worst_inter = 140; |
2319 | 0 | rc->fac_active_worst_gf = 100; |
2320 | 0 | if (rc->rolling_target_bits > 0) { |
2321 | 0 | rate_err = |
2322 | 0 | (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits; |
2323 | 0 | if (rate_err < 1.0) |
2324 | 0 | rc->fac_active_worst_inter = 120; |
2325 | 0 | else if (rate_err > 2.0) |
2326 | | // Increase active_worst faster if rate fluctuation is high. |
2327 | 0 | rc->fac_active_worst_inter = 160; |
2328 | 0 | } |
2329 | 0 | target = vp9_calc_pframe_target_size_one_pass_vbr(cpi); |
2330 | 0 | } |
2331 | 0 | } |
2332 | 0 | } |
2333 | |
|
2334 | 0 | if (svc->simulcast_mode) { |
2335 | 0 | if (svc->spatial_layer_id > 0 && |
2336 | 0 | svc->layer_context[layer].is_key_frame == 1) { |
2337 | 0 | cm->frame_type = KEY_FRAME; |
2338 | 0 | cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
2339 | 0 | if (cpi->oxcf.rc_mode == VPX_CBR) |
2340 | 0 | target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); |
2341 | 0 | else |
2342 | 0 | target = vp9_calc_iframe_target_size_one_pass_vbr(cpi); |
2343 | 0 | } |
2344 | | // Set the buffer idx and refresh flags for key frames in simulcast mode. |
2345 | | // Note the buffer slot for long-term reference is set below (line 2255), |
2346 | | // and alt_ref is used for that on key frame. So use last and golden for |
2347 | | // the other two normal slots. |
2348 | 0 | if (cm->frame_type == KEY_FRAME) { |
2349 | 0 | if (svc->number_spatial_layers == 2) { |
2350 | 0 | if (svc->spatial_layer_id == 0) { |
2351 | 0 | cpi->lst_fb_idx = 0; |
2352 | 0 | cpi->gld_fb_idx = 2; |
2353 | 0 | cpi->alt_fb_idx = 6; |
2354 | 0 | } else if (svc->spatial_layer_id == 1) { |
2355 | 0 | cpi->lst_fb_idx = 1; |
2356 | 0 | cpi->gld_fb_idx = 3; |
2357 | 0 | cpi->alt_fb_idx = 6; |
2358 | 0 | } |
2359 | 0 | } else if (svc->number_spatial_layers == 3) { |
2360 | 0 | if (svc->spatial_layer_id == 0) { |
2361 | 0 | cpi->lst_fb_idx = 0; |
2362 | 0 | cpi->gld_fb_idx = 3; |
2363 | 0 | cpi->alt_fb_idx = 6; |
2364 | 0 | } else if (svc->spatial_layer_id == 1) { |
2365 | 0 | cpi->lst_fb_idx = 1; |
2366 | 0 | cpi->gld_fb_idx = 4; |
2367 | 0 | cpi->alt_fb_idx = 6; |
2368 | 0 | } else if (svc->spatial_layer_id == 2) { |
2369 | 0 | cpi->lst_fb_idx = 2; |
2370 | 0 | cpi->gld_fb_idx = 5; |
2371 | 0 | cpi->alt_fb_idx = 7; |
2372 | 0 | } |
2373 | 0 | } |
2374 | 0 | cpi->ext_refresh_last_frame = 1; |
2375 | 0 | cpi->ext_refresh_golden_frame = 1; |
2376 | 0 | cpi->ext_refresh_alt_ref_frame = 1; |
2377 | 0 | } |
2378 | 0 | } |
2379 | | |
2380 | | // Check if superframe contains a sync layer request. |
2381 | 0 | vp9_svc_check_spatial_layer_sync(cpi); |
2382 | | |
2383 | | // If long term termporal feature is enabled, set the period of the update. |
2384 | | // The update/refresh of this reference frame is always on base temporal |
2385 | | // layer frame. |
2386 | 0 | if (svc->use_gf_temporal_ref_current_layer) { |
2387 | | // Only use gf long-term prediction on non-key superframes. |
2388 | 0 | if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) { |
2389 | | // Use golden for this reference, which will be used for prediction. |
2390 | 0 | int index = svc->spatial_layer_id; |
2391 | 0 | if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1; |
2392 | 0 | assert(index >= 0); |
2393 | 0 | cpi->gld_fb_idx = svc->buffer_gf_temporal_ref[index].idx; |
2394 | | // Enable prediction off LAST (last reference) and golden (which will |
2395 | | // generally be further behind/long-term reference). |
2396 | 0 | cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG; |
2397 | 0 | } |
2398 | | // Check for update/refresh of reference: only refresh on base temporal |
2399 | | // layer. |
2400 | 0 | if (svc->temporal_layer_id == 0) { |
2401 | 0 | if (svc->layer_context[svc->temporal_layer_id].is_key_frame) { |
2402 | | // On key frame we update the buffer index used for long term reference. |
2403 | | // Use the alt_ref since it is not used or updated on key frames. |
2404 | 0 | int index = svc->spatial_layer_id; |
2405 | 0 | if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1; |
2406 | 0 | assert(index >= 0); |
2407 | 0 | cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx; |
2408 | 0 | cpi->ext_refresh_alt_ref_frame = 1; |
2409 | 0 | } else if (rc->frames_till_gf_update_due == 0) { |
2410 | | // Set perdiod of next update. Make it a multiple of 10, as the cyclic |
2411 | | // refresh is typically ~10%, and we'd like the update to happen after |
2412 | | // a few cylces of the refresh (so it better quality frame). Note the |
2413 | | // cyclic refresh for SVC only operates on base temporal layer frames. |
2414 | | // Choose 20 as perdiod for now (2 cycles). |
2415 | 0 | rc->baseline_gf_interval = 20; |
2416 | 0 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
2417 | 0 | cpi->ext_refresh_golden_frame = 1; |
2418 | 0 | rc->gfu_boost = DEFAULT_GF_BOOST; |
2419 | 0 | } |
2420 | 0 | } |
2421 | 0 | } else if (!svc->use_gf_temporal_ref) { |
2422 | 0 | rc->frames_till_gf_update_due = INT_MAX; |
2423 | 0 | rc->baseline_gf_interval = INT_MAX; |
2424 | 0 | } |
2425 | 0 | if (svc->set_intra_only_frame) { |
2426 | 0 | set_intra_only_frame(cpi); |
2427 | 0 | if (cpi->oxcf.rc_mode == VPX_CBR) |
2428 | 0 | target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); |
2429 | 0 | else |
2430 | 0 | target = vp9_calc_iframe_target_size_one_pass_vbr(cpi); |
2431 | 0 | } |
2432 | | // Overlay frame predicts from LAST (intra-only) |
2433 | 0 | if (svc->previous_frame_is_intra_only) cpi->ref_frame_flags |= VP9_LAST_FLAG; |
2434 | | |
2435 | | // Any update/change of global cyclic refresh parameters (amount/delta-qp) |
2436 | | // should be done here, before the frame qp is selected. |
2437 | 0 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
2438 | 0 | vp9_cyclic_refresh_update_parameters(cpi); |
2439 | |
|
2440 | 0 | vp9_rc_set_frame_target(cpi, target); |
2441 | 0 | if (cm->show_frame) vp9_update_buffer_level_svc_preencode(cpi); |
2442 | |
|
2443 | 0 | if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && svc->single_layer_svc == 1 && |
2444 | 0 | svc->spatial_layer_id == svc->first_spatial_layer_to_encode && |
2445 | 0 | svc->temporal_layer_id == 0) { |
2446 | 0 | LAYER_CONTEXT *lc = NULL; |
2447 | 0 | cpi->resize_pending = vp9_resize_one_pass_cbr(cpi); |
2448 | 0 | if (cpi->resize_pending) { |
2449 | 0 | int tl, width, height; |
2450 | | // Apply the same scale to all temporal layers. |
2451 | 0 | for (tl = 0; tl < svc->number_temporal_layers; tl++) { |
2452 | 0 | lc = &svc->layer_context[svc->spatial_layer_id * |
2453 | 0 | svc->number_temporal_layers + |
2454 | 0 | tl]; |
2455 | 0 | lc->scaling_factor_num_resize = |
2456 | 0 | cpi->resize_scale_num * lc->scaling_factor_num; |
2457 | 0 | lc->scaling_factor_den_resize = |
2458 | 0 | cpi->resize_scale_den * lc->scaling_factor_den; |
2459 | | // Reset rate control for all temporal layers. |
2460 | 0 | lc->rc.buffer_level = lc->rc.optimal_buffer_level; |
2461 | 0 | lc->rc.bits_off_target = lc->rc.optimal_buffer_level; |
2462 | 0 | lc->rc.rate_correction_factors[INTER_FRAME] = |
2463 | 0 | rc->rate_correction_factors[INTER_FRAME]; |
2464 | 0 | } |
2465 | | // Set the size for this current temporal layer. |
2466 | 0 | lc = &svc->layer_context[svc->spatial_layer_id * |
2467 | 0 | svc->number_temporal_layers + |
2468 | 0 | svc->temporal_layer_id]; |
2469 | 0 | get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height, |
2470 | 0 | lc->scaling_factor_num_resize, |
2471 | 0 | lc->scaling_factor_den_resize, &width, &height); |
2472 | 0 | vp9_set_size_literal(cpi, width, height); |
2473 | 0 | svc->resize_set = 1; |
2474 | 0 | } |
2475 | 0 | } else { |
2476 | 0 | cpi->resize_pending = 0; |
2477 | 0 | svc->resize_set = 0; |
2478 | 0 | } |
2479 | 0 | } |
2480 | | |
2481 | 0 | void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { |
2482 | 0 | VP9_COMMON *const cm = &cpi->common; |
2483 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2484 | 0 | int target; |
2485 | 0 | if ((cm->current_video_frame == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY) || |
2486 | 0 | (cpi->oxcf.auto_key && rc->frames_to_key == 0) || |
2487 | 0 | (cpi->oxcf.mode != cpi->deadline_mode_previous_frame)) { |
2488 | 0 | cm->frame_type = KEY_FRAME; |
2489 | 0 | rc->frames_to_key = cpi->oxcf.key_freq; |
2490 | 0 | rc->kf_boost = DEFAULT_KF_BOOST; |
2491 | 0 | rc->source_alt_ref_active = 0; |
2492 | 0 | } else { |
2493 | 0 | cm->frame_type = INTER_FRAME; |
2494 | 0 | } |
2495 | 0 | if (rc->frames_till_gf_update_due == 0) { |
2496 | 0 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
2497 | 0 | vp9_cyclic_refresh_set_golden_update(cpi); |
2498 | 0 | else |
2499 | 0 | rc->baseline_gf_interval = |
2500 | 0 | (rc->min_gf_interval + rc->max_gf_interval) / 2; |
2501 | 0 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
2502 | | // NOTE: frames_till_gf_update_due must be <= frames_to_key. |
2503 | 0 | if (rc->frames_till_gf_update_due > rc->frames_to_key) |
2504 | 0 | rc->frames_till_gf_update_due = rc->frames_to_key; |
2505 | 0 | cpi->refresh_golden_frame = 1; |
2506 | 0 | rc->gfu_boost = DEFAULT_GF_BOOST; |
2507 | 0 | } |
2508 | | |
2509 | | // Any update/change of global cyclic refresh parameters (amount/delta-qp) |
2510 | | // should be done here, before the frame qp is selected. |
2511 | 0 | if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
2512 | 0 | vp9_cyclic_refresh_update_parameters(cpi); |
2513 | |
|
2514 | 0 | if (frame_is_intra_only(cm)) |
2515 | 0 | target = vp9_calc_iframe_target_size_one_pass_cbr(cpi); |
2516 | 0 | else |
2517 | 0 | target = vp9_calc_pframe_target_size_one_pass_cbr(cpi); |
2518 | |
|
2519 | 0 | vp9_rc_set_frame_target(cpi, target); |
2520 | |
|
2521 | 0 | if (cm->show_frame) vp9_update_buffer_level_preencode(cpi); |
2522 | |
|
2523 | 0 | if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC) |
2524 | 0 | cpi->resize_pending = vp9_resize_one_pass_cbr(cpi); |
2525 | 0 | else |
2526 | 0 | cpi->resize_pending = 0; |
2527 | 0 | } |
2528 | | |
2529 | | int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, |
2530 | 8.19k | vpx_bit_depth_t bit_depth) { |
2531 | 8.19k | int start_index = rc->worst_quality; |
2532 | 8.19k | int target_index = rc->worst_quality; |
2533 | 8.19k | int i; |
2534 | | |
2535 | | // Convert the average q value to an index. |
2536 | 605k | for (i = rc->best_quality; i < rc->worst_quality; ++i) { |
2537 | 605k | start_index = i; |
2538 | 605k | if (vp9_convert_qindex_to_q(i, bit_depth) >= qstart) break; |
2539 | 605k | } |
2540 | | |
2541 | | // Convert the q target to an index |
2542 | 370k | for (i = rc->best_quality; i < rc->worst_quality; ++i) { |
2543 | 370k | target_index = i; |
2544 | 370k | if (vp9_convert_qindex_to_q(i, bit_depth) >= qtarget) break; |
2545 | 370k | } |
2546 | | |
2547 | 8.19k | return target_index - start_index; |
2548 | 8.19k | } |
2549 | | |
2550 | | int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, |
2551 | | int qindex, double rate_target_ratio, |
2552 | 10.1k | vpx_bit_depth_t bit_depth) { |
2553 | 10.1k | int target_index = rc->worst_quality; |
2554 | 10.1k | int i; |
2555 | | |
2556 | | // Look up the current projected bits per block for the base index |
2557 | 10.1k | const int base_bits_per_mb = |
2558 | 10.1k | vp9_rc_bits_per_mb(frame_type, qindex, 1.0, bit_depth); |
2559 | | |
2560 | | // Find the target bits per mb based on the base value and given ratio. |
2561 | 10.1k | const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb); |
2562 | | |
2563 | | // Convert the q target to an index |
2564 | 1.11M | for (i = rc->best_quality; i < rc->worst_quality; ++i) { |
2565 | 1.11M | if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <= |
2566 | 1.11M | target_bits_per_mb) { |
2567 | 10.1k | target_index = i; |
2568 | 10.1k | break; |
2569 | 10.1k | } |
2570 | 1.11M | } |
2571 | 10.1k | return target_index - qindex; |
2572 | 10.1k | } |
2573 | | |
2574 | | void vp9_rc_set_gf_interval_range(const VP9_COMP *const cpi, |
2575 | 67.6k | RATE_CONTROL *const rc) { |
2576 | 67.6k | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
2577 | | |
2578 | | // Special case code for 1 pass fixed Q mode tests |
2579 | 67.6k | if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) { |
2580 | 3.17k | rc->max_gf_interval = FIXED_GF_INTERVAL; |
2581 | 3.17k | rc->min_gf_interval = FIXED_GF_INTERVAL; |
2582 | 3.17k | rc->static_scene_max_gf_interval = FIXED_GF_INTERVAL; |
2583 | 64.5k | } else { |
2584 | 64.5k | double framerate = cpi->framerate; |
2585 | | // Set Maximum gf/arf interval |
2586 | 64.5k | rc->max_gf_interval = oxcf->max_gf_interval; |
2587 | 64.5k | rc->min_gf_interval = oxcf->min_gf_interval; |
2588 | | #if CONFIG_RATE_CTRL |
2589 | | if (oxcf->use_simple_encode_api) { |
2590 | | // In this experiment, we avoid framerate being changed dynamically during |
2591 | | // encoding. |
2592 | | framerate = oxcf->init_framerate; |
2593 | | } |
2594 | | #endif // CONFIG_RATE_CTRL |
2595 | 64.5k | if (rc->min_gf_interval == 0) { |
2596 | 64.5k | rc->min_gf_interval = vp9_rc_get_default_min_gf_interval( |
2597 | 64.5k | oxcf->width, oxcf->height, framerate); |
2598 | 64.5k | } |
2599 | 64.5k | if (rc->max_gf_interval == 0) { |
2600 | 64.5k | rc->max_gf_interval = |
2601 | 64.5k | vp9_rc_get_default_max_gf_interval(framerate, rc->min_gf_interval); |
2602 | 64.5k | } |
2603 | | |
2604 | | // Extended max interval for genuinely static scenes like slide shows. |
2605 | 64.5k | rc->static_scene_max_gf_interval = MAX_STATIC_GF_GROUP_LENGTH; |
2606 | | |
2607 | 64.5k | if (rc->max_gf_interval > rc->static_scene_max_gf_interval) |
2608 | 90 | rc->max_gf_interval = rc->static_scene_max_gf_interval; |
2609 | | |
2610 | | // Clamp min to max |
2611 | 64.5k | rc->min_gf_interval = VPXMIN(rc->min_gf_interval, rc->max_gf_interval); |
2612 | | |
2613 | 64.5k | if (oxcf->target_level == LEVEL_AUTO) { |
2614 | 0 | const uint32_t pic_size = cpi->common.width * cpi->common.height; |
2615 | 0 | const uint32_t pic_breadth = |
2616 | 0 | VPXMAX(cpi->common.width, cpi->common.height); |
2617 | 0 | int i; |
2618 | 0 | for (i = 0; i < VP9_LEVELS; ++i) { |
2619 | 0 | if (vp9_level_defs[i].max_luma_picture_size >= pic_size && |
2620 | 0 | vp9_level_defs[i].max_luma_picture_breadth >= pic_breadth) { |
2621 | 0 | if (rc->min_gf_interval <= |
2622 | 0 | (int)vp9_level_defs[i].min_altref_distance) { |
2623 | 0 | rc->min_gf_interval = (int)vp9_level_defs[i].min_altref_distance; |
2624 | 0 | rc->max_gf_interval = |
2625 | 0 | VPXMAX(rc->max_gf_interval, rc->min_gf_interval); |
2626 | 0 | } |
2627 | 0 | break; |
2628 | 0 | } |
2629 | 0 | } |
2630 | 0 | } |
2631 | 64.5k | } |
2632 | 67.6k | } |
2633 | | |
2634 | 67.6k | void vp9_rc_update_framerate(VP9_COMP *cpi) { |
2635 | 67.6k | const VP9_COMMON *const cm = &cpi->common; |
2636 | 67.6k | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
2637 | 67.6k | RATE_CONTROL *const rc = &cpi->rc; |
2638 | | |
2639 | 67.6k | rc->avg_frame_bandwidth = saturate_cast_double_to_int( |
2640 | 67.6k | round(oxcf->target_bandwidth / cpi->framerate)); |
2641 | | |
2642 | 67.6k | int64_t vbr_min_bits = |
2643 | 67.6k | (int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100; |
2644 | 67.6k | vbr_min_bits = VPXMIN(vbr_min_bits, INT_MAX); |
2645 | | |
2646 | 67.6k | rc->min_frame_bandwidth = VPXMAX((int)vbr_min_bits, FRAME_OVERHEAD_BITS); |
2647 | | |
2648 | | // A maximum bitrate for a frame is defined. |
2649 | | // However this limit is extended if a very high rate is given on the command |
2650 | | // line or the rate can not be achieved because of a user specified max q |
2651 | | // (e.g. when the user specifies lossless encode). |
2652 | | // |
2653 | | // If a level is specified that requires a lower maximum rate then the level |
2654 | | // value take precedence. |
2655 | 67.6k | int64_t vbr_max_bits = |
2656 | 67.6k | (int64_t)rc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section / 100; |
2657 | 67.6k | vbr_max_bits = VPXMIN(vbr_max_bits, INT_MAX); |
2658 | | |
2659 | 67.6k | rc->max_frame_bandwidth = |
2660 | 67.6k | VPXMAX(VPXMAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), (int)vbr_max_bits); |
2661 | | |
2662 | 67.6k | vp9_rc_set_gf_interval_range(cpi, rc); |
2663 | 67.6k | } |
2664 | | |
2665 | | #define VBR_PCT_ADJUSTMENT_LIMIT 50 |
2666 | | // For VBR...adjustment to the frame target based on error from previous frames |
2667 | 0 | static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) { |
2668 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2669 | 0 | int64_t vbr_bits_off_target = rc->vbr_bits_off_target; |
2670 | 0 | int64_t frame_target = *this_frame_target; |
2671 | 0 | int frame_window = (int)VPXMIN( |
2672 | 0 | 16, cpi->twopass.total_stats.count - cpi->common.current_video_frame); |
2673 | | |
2674 | | // Calcluate the adjustment to rate for this frame. |
2675 | 0 | if (frame_window > 0) { |
2676 | 0 | int64_t max_delta = (vbr_bits_off_target > 0) |
2677 | 0 | ? (vbr_bits_off_target / frame_window) |
2678 | 0 | : (-vbr_bits_off_target / frame_window); |
2679 | |
|
2680 | 0 | max_delta = |
2681 | 0 | VPXMIN(max_delta, ((frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100)); |
2682 | | |
2683 | | // vbr_bits_off_target > 0 means we have extra bits to spend |
2684 | 0 | if (vbr_bits_off_target > 0) { |
2685 | 0 | frame_target += VPXMIN(vbr_bits_off_target, max_delta); |
2686 | 0 | } else { |
2687 | 0 | frame_target -= VPXMIN(-vbr_bits_off_target, max_delta); |
2688 | 0 | } |
2689 | 0 | } |
2690 | | |
2691 | | // Fast redistribution of bits arising from massive local undershoot. |
2692 | | // Don't do it for kf,arf,gf or overlay frames. |
2693 | 0 | if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref && |
2694 | 0 | rc->vbr_bits_off_target_fast) { |
2695 | 0 | int64_t one_frame_bits = VPXMAX(rc->avg_frame_bandwidth, frame_target); |
2696 | 0 | int64_t fast_extra_bits = |
2697 | 0 | VPXMIN(rc->vbr_bits_off_target_fast, one_frame_bits); |
2698 | 0 | fast_extra_bits = |
2699 | 0 | VPXMIN(fast_extra_bits, |
2700 | 0 | VPXMAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8)); |
2701 | 0 | frame_target += fast_extra_bits; |
2702 | 0 | rc->vbr_bits_off_target_fast -= fast_extra_bits; |
2703 | 0 | } |
2704 | | |
2705 | | // Clamp the target for the frame to the maximum allowed for one frame. |
2706 | 0 | *this_frame_target = (int)VPXMIN(frame_target, INT_MAX); |
2707 | 0 | } |
2708 | | |
2709 | 0 | void vp9_set_target_rate(VP9_COMP *cpi) { |
2710 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2711 | 0 | int target_rate = rc->base_frame_target; |
2712 | |
|
2713 | 0 | if (cpi->common.frame_type == KEY_FRAME) |
2714 | 0 | target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); |
2715 | 0 | else |
2716 | 0 | target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
2717 | |
|
2718 | 0 | if (!cpi->oxcf.vbr_corpus_complexity) { |
2719 | | // Correction to rate target based on prior over or under shoot. |
2720 | 0 | if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ) |
2721 | 0 | vbr_rate_correction(cpi, &target_rate); |
2722 | 0 | } |
2723 | 0 | vp9_rc_set_frame_target(cpi, target_rate); |
2724 | 0 | } |
2725 | | |
2726 | | // Check if we should resize, based on average QP from past x frames. |
2727 | | // Only allow for resize at most one scale down for now, scaling factor is 2. |
2728 | 0 | int vp9_resize_one_pass_cbr(VP9_COMP *cpi) { |
2729 | 0 | const VP9_COMMON *const cm = &cpi->common; |
2730 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2731 | 0 | RESIZE_ACTION resize_action = NO_RESIZE; |
2732 | 0 | int avg_qp_thr1 = 70; |
2733 | 0 | int avg_qp_thr2 = 50; |
2734 | | // Don't allow for resized frame to go below 320x180, resize in steps of 3/4. |
2735 | 0 | int min_width = (320 * 4) / 3; |
2736 | 0 | int min_height = (180 * 4) / 3; |
2737 | 0 | int down_size_on = 1; |
2738 | 0 | int force_downsize_rate = 0; |
2739 | 0 | cpi->resize_scale_num = 1; |
2740 | 0 | cpi->resize_scale_den = 1; |
2741 | | // Don't resize on key frame; reset the counters on key frame. |
2742 | 0 | if (cm->frame_type == KEY_FRAME) { |
2743 | 0 | cpi->resize_avg_qp = 0; |
2744 | 0 | cpi->resize_count = 0; |
2745 | 0 | return 0; |
2746 | 0 | } |
2747 | | |
2748 | | // No resizing down if frame size is below some limit. |
2749 | 0 | if ((cm->width * cm->height) < min_width * min_height) down_size_on = 0; |
2750 | |
|
2751 | | #if CONFIG_VP9_TEMPORAL_DENOISING |
2752 | | // If denoiser is on, apply a smaller qp threshold. |
2753 | | if (cpi->oxcf.noise_sensitivity > 0) { |
2754 | | avg_qp_thr1 = 60; |
2755 | | avg_qp_thr2 = 40; |
2756 | | } |
2757 | | #endif |
2758 | | |
2759 | | // Force downsize based on per-frame-bandwidth, for extreme case, |
2760 | | // for HD input. |
2761 | 0 | if (cpi->resize_state == ORIG && cm->width * cm->height >= 1280 * 720) { |
2762 | 0 | if (rc->avg_frame_bandwidth < 300000 / 30) { |
2763 | 0 | resize_action = DOWN_ONEHALF; |
2764 | 0 | cpi->resize_state = ONE_HALF; |
2765 | 0 | force_downsize_rate = 1; |
2766 | 0 | } else if (rc->avg_frame_bandwidth < 400000 / 30) { |
2767 | 0 | resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR; |
2768 | 0 | cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER; |
2769 | 0 | force_downsize_rate = 1; |
2770 | 0 | } |
2771 | 0 | } else if (cpi->resize_state == THREE_QUARTER && |
2772 | 0 | cm->width * cm->height >= 960 * 540) { |
2773 | 0 | if (rc->avg_frame_bandwidth < 300000 / 30) { |
2774 | 0 | resize_action = DOWN_ONEHALF; |
2775 | 0 | cpi->resize_state = ONE_HALF; |
2776 | 0 | force_downsize_rate = 1; |
2777 | 0 | } |
2778 | 0 | } |
2779 | | |
2780 | | // Resize based on average buffer underflow and QP over some window. |
2781 | | // Ignore samples close to key frame, since QP is usually high after key. |
2782 | 0 | if (!force_downsize_rate && cpi->rc.frames_since_key > cpi->framerate) { |
2783 | 0 | const int window = VPXMIN(30, (int)round(2 * cpi->framerate)); |
2784 | 0 | cpi->resize_avg_qp += rc->last_q[INTER_FRAME]; |
2785 | 0 | if (cpi->rc.buffer_level < (int)(30 * rc->optimal_buffer_level / 100)) |
2786 | 0 | ++cpi->resize_buffer_underflow; |
2787 | 0 | ++cpi->resize_count; |
2788 | | // Check for resize action every "window" frames. |
2789 | 0 | if (cpi->resize_count >= window) { |
2790 | 0 | int avg_qp = cpi->resize_avg_qp / cpi->resize_count; |
2791 | | // Resize down if buffer level has underflowed sufficient amount in past |
2792 | | // window, and we are at original or 3/4 of original resolution. |
2793 | | // Resize back up if average QP is low, and we are currently in a resized |
2794 | | // down state, i.e. 1/2 or 3/4 of original resolution. |
2795 | | // Currently, use a flag to turn 3/4 resizing feature on/off. |
2796 | 0 | if (cpi->resize_buffer_underflow > (cpi->resize_count >> 2) && |
2797 | 0 | down_size_on) { |
2798 | 0 | if (cpi->resize_state == THREE_QUARTER) { |
2799 | 0 | resize_action = DOWN_ONEHALF; |
2800 | 0 | cpi->resize_state = ONE_HALF; |
2801 | 0 | } else if (cpi->resize_state == ORIG) { |
2802 | 0 | resize_action = ONEHALFONLY_RESIZE ? DOWN_ONEHALF : DOWN_THREEFOUR; |
2803 | 0 | cpi->resize_state = ONEHALFONLY_RESIZE ? ONE_HALF : THREE_QUARTER; |
2804 | 0 | } |
2805 | 0 | } else if (cpi->resize_state != ORIG && |
2806 | 0 | avg_qp < avg_qp_thr1 * cpi->rc.worst_quality / 100) { |
2807 | 0 | if (cpi->resize_state == THREE_QUARTER || |
2808 | 0 | avg_qp < avg_qp_thr2 * cpi->rc.worst_quality / 100 || |
2809 | 0 | ONEHALFONLY_RESIZE) { |
2810 | 0 | resize_action = UP_ORIG; |
2811 | 0 | cpi->resize_state = ORIG; |
2812 | 0 | } else if (cpi->resize_state == ONE_HALF) { |
2813 | 0 | resize_action = UP_THREEFOUR; |
2814 | 0 | cpi->resize_state = THREE_QUARTER; |
2815 | 0 | } |
2816 | 0 | } |
2817 | | // Reset for next window measurement. |
2818 | 0 | cpi->resize_avg_qp = 0; |
2819 | 0 | cpi->resize_count = 0; |
2820 | 0 | cpi->resize_buffer_underflow = 0; |
2821 | 0 | } |
2822 | 0 | } |
2823 | | // If decision is to resize, reset some quantities, and check is we should |
2824 | | // reduce rate correction factor, |
2825 | 0 | if (resize_action != NO_RESIZE) { |
2826 | 0 | int target_bits_per_frame; |
2827 | 0 | int active_worst_quality; |
2828 | 0 | int qindex; |
2829 | 0 | int tot_scale_change; |
2830 | 0 | if (resize_action == DOWN_THREEFOUR || resize_action == UP_THREEFOUR) { |
2831 | 0 | cpi->resize_scale_num = 3; |
2832 | 0 | cpi->resize_scale_den = 4; |
2833 | 0 | } else if (resize_action == DOWN_ONEHALF) { |
2834 | 0 | cpi->resize_scale_num = 1; |
2835 | 0 | cpi->resize_scale_den = 2; |
2836 | 0 | } else { // UP_ORIG or anything else |
2837 | 0 | cpi->resize_scale_num = 1; |
2838 | 0 | cpi->resize_scale_den = 1; |
2839 | 0 | } |
2840 | 0 | tot_scale_change = (cpi->resize_scale_den * cpi->resize_scale_den) / |
2841 | 0 | (cpi->resize_scale_num * cpi->resize_scale_num); |
2842 | | // Reset buffer level to optimal, update target size. |
2843 | 0 | rc->buffer_level = rc->optimal_buffer_level; |
2844 | 0 | rc->bits_off_target = rc->optimal_buffer_level; |
2845 | 0 | rc->this_frame_target = vp9_calc_pframe_target_size_one_pass_cbr(cpi); |
2846 | | // Get the projected qindex, based on the scaled target frame size (scaled |
2847 | | // so target_bits_per_mb in vp9_rc_regulate_q will be correct target). |
2848 | 0 | target_bits_per_frame = (resize_action >= 0) |
2849 | 0 | ? rc->this_frame_target * tot_scale_change |
2850 | 0 | : rc->this_frame_target / tot_scale_change; |
2851 | 0 | active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi); |
2852 | 0 | qindex = vp9_rc_regulate_q(cpi, target_bits_per_frame, rc->best_quality, |
2853 | 0 | active_worst_quality); |
2854 | | // If resize is down, check if projected q index is close to worst_quality, |
2855 | | // and if so, reduce the rate correction factor (since likely can afford |
2856 | | // lower q for resized frame). |
2857 | 0 | if (resize_action > 0 && qindex > 90 * cpi->rc.worst_quality / 100) { |
2858 | 0 | rc->rate_correction_factors[INTER_NORMAL] *= 0.85; |
2859 | 0 | } |
2860 | | // If resize is back up, check if projected q index is too much above the |
2861 | | // current base_qindex, and if so, reduce the rate correction factor |
2862 | | // (since prefer to keep q for resized frame at least close to previous q). |
2863 | 0 | if (resize_action < 0 && qindex > 130 * cm->base_qindex / 100) { |
2864 | 0 | rc->rate_correction_factors[INTER_NORMAL] *= 0.9; |
2865 | 0 | } |
2866 | 0 | } |
2867 | 0 | return resize_action; |
2868 | 0 | } |
2869 | | |
2870 | | static void adjust_gf_boost_lag_one_pass_vbr(VP9_COMP *cpi, |
2871 | 19.0k | uint64_t avg_sad_current) { |
2872 | 19.0k | VP9_COMMON *const cm = &cpi->common; |
2873 | 19.0k | RATE_CONTROL *const rc = &cpi->rc; |
2874 | 19.0k | int target; |
2875 | 19.0k | int found = 0; |
2876 | 19.0k | int found2 = 0; |
2877 | 19.0k | int frame; |
2878 | 19.0k | int i; |
2879 | 19.0k | uint64_t avg_source_sad_lag = avg_sad_current; |
2880 | 19.0k | int high_source_sad_lagindex = -1; |
2881 | 19.0k | int steady_sad_lagindex = -1; |
2882 | 19.0k | uint32_t sad_thresh1 = 70000; |
2883 | 19.0k | uint32_t sad_thresh2 = 120000; |
2884 | 19.0k | int low_content = 0; |
2885 | 19.0k | int high_content = 0; |
2886 | 19.0k | double rate_err = 1.0; |
2887 | | // Get measure of complexity over the future frames, and get the first |
2888 | | // future frame with high_source_sad/scene-change. |
2889 | 19.0k | int tot_frames = (int)vp9_lookahead_depth(cpi->lookahead) - 1; |
2890 | 305k | for (frame = tot_frames; frame >= 1; --frame) { |
2891 | 286k | const int lagframe_idx = tot_frames - frame + 1; |
2892 | 286k | uint64_t reference_sad = rc->avg_source_sad[0]; |
2893 | 3.09M | for (i = 1; i < lagframe_idx; ++i) { |
2894 | 2.80M | if (rc->avg_source_sad[i] > 0) |
2895 | 424 | reference_sad = (3 * reference_sad + rc->avg_source_sad[i]) >> 2; |
2896 | 2.80M | } |
2897 | | // Detect up-coming scene change. |
2898 | 286k | if (!found && |
2899 | 286k | (rc->avg_source_sad[lagframe_idx] > |
2900 | 286k | VPXMAX(sad_thresh1, (unsigned int)(reference_sad << 1)) || |
2901 | 286k | rc->avg_source_sad[lagframe_idx] > |
2902 | 285k | VPXMAX(3 * sad_thresh1 >> 2, |
2903 | 286k | (unsigned int)(reference_sad << 2)))) { |
2904 | 84 | high_source_sad_lagindex = lagframe_idx; |
2905 | 84 | found = 1; |
2906 | 84 | } |
2907 | | // Detect change from motion to steady. |
2908 | 286k | if (!found2 && lagframe_idx > 1 && lagframe_idx < tot_frames && |
2909 | 286k | rc->avg_source_sad[lagframe_idx - 1] > (sad_thresh1 >> 2)) { |
2910 | 104 | found2 = 1; |
2911 | 211 | for (i = lagframe_idx; i < tot_frames; ++i) { |
2912 | 107 | if (!(rc->avg_source_sad[i] > 0 && |
2913 | 107 | rc->avg_source_sad[i] < (sad_thresh1 >> 2) && |
2914 | 107 | rc->avg_source_sad[i] < |
2915 | 101 | (rc->avg_source_sad[lagframe_idx - 1] >> 1))) { |
2916 | 101 | found2 = 0; |
2917 | 101 | i = tot_frames; |
2918 | 101 | } |
2919 | 107 | } |
2920 | 104 | if (found2) steady_sad_lagindex = lagframe_idx; |
2921 | 104 | } |
2922 | 286k | avg_source_sad_lag += rc->avg_source_sad[lagframe_idx]; |
2923 | 286k | } |
2924 | 19.0k | if (tot_frames > 0) avg_source_sad_lag = avg_source_sad_lag / tot_frames; |
2925 | | // Constrain distance between detected scene cuts. |
2926 | 19.0k | if (high_source_sad_lagindex != -1 && |
2927 | 19.0k | high_source_sad_lagindex != rc->high_source_sad_lagindex - 1 && |
2928 | 19.0k | abs(high_source_sad_lagindex - rc->high_source_sad_lagindex) < 4) |
2929 | 22 | rc->high_source_sad_lagindex = -1; |
2930 | 19.0k | else |
2931 | 19.0k | rc->high_source_sad_lagindex = high_source_sad_lagindex; |
2932 | | // Adjust some factors for the next GF group, ignore initial key frame, |
2933 | | // and only for lag_in_frames not too small. |
2934 | 19.0k | if (cpi->refresh_golden_frame == 1 && cm->current_video_frame > 30 && |
2935 | 19.0k | cpi->oxcf.lag_in_frames > 8) { |
2936 | 1.24k | int frame_constraint; |
2937 | 1.24k | if (rc->rolling_target_bits > 0) |
2938 | 1.10k | rate_err = |
2939 | 1.10k | (double)rc->rolling_actual_bits / (double)rc->rolling_target_bits; |
2940 | 1.24k | high_content = high_source_sad_lagindex != -1 || |
2941 | 1.24k | avg_source_sad_lag > (rc->prev_avg_source_sad_lag << 1) || |
2942 | 1.24k | avg_source_sad_lag > sad_thresh2; |
2943 | 1.24k | low_content = high_source_sad_lagindex == -1 && |
2944 | 1.24k | ((avg_source_sad_lag < (rc->prev_avg_source_sad_lag >> 1)) || |
2945 | 1.24k | (avg_source_sad_lag < sad_thresh1)); |
2946 | 1.24k | if (low_content) { |
2947 | 1.24k | rc->gfu_boost = DEFAULT_GF_BOOST; |
2948 | 1.24k | rc->baseline_gf_interval = |
2949 | 1.24k | VPXMIN(15, (3 * rc->baseline_gf_interval) >> 1); |
2950 | 1.24k | } else if (high_content) { |
2951 | 0 | rc->gfu_boost = DEFAULT_GF_BOOST >> 1; |
2952 | 0 | rc->baseline_gf_interval = (rate_err > 3.0) |
2953 | 0 | ? VPXMAX(10, rc->baseline_gf_interval >> 1) |
2954 | 0 | : VPXMAX(6, rc->baseline_gf_interval >> 1); |
2955 | 0 | } |
2956 | 1.24k | if (rc->baseline_gf_interval > cpi->oxcf.lag_in_frames - 1) |
2957 | 0 | rc->baseline_gf_interval = cpi->oxcf.lag_in_frames - 1; |
2958 | | // Check for constraining gf_interval for up-coming scene/content changes, |
2959 | | // or for up-coming key frame, whichever is closer. |
2960 | 1.24k | frame_constraint = rc->frames_to_key; |
2961 | 1.24k | if (rc->high_source_sad_lagindex > 0 && |
2962 | 1.24k | frame_constraint > rc->high_source_sad_lagindex) |
2963 | 0 | frame_constraint = rc->high_source_sad_lagindex; |
2964 | 1.24k | if (steady_sad_lagindex > 3 && frame_constraint > steady_sad_lagindex) |
2965 | 0 | frame_constraint = steady_sad_lagindex; |
2966 | 1.24k | adjust_gfint_frame_constraint(cpi, frame_constraint); |
2967 | 1.24k | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
2968 | | // Adjust factors for active_worst setting & af_ratio for next gf interval. |
2969 | 1.24k | rc->fac_active_worst_inter = 150; // corresponds to 3/2 (= 150 /100). |
2970 | 1.24k | rc->fac_active_worst_gf = 100; |
2971 | 1.24k | if (rate_err < 2.0 && !high_content) { |
2972 | 1.11k | rc->fac_active_worst_inter = 120; |
2973 | 1.11k | rc->fac_active_worst_gf = 90; |
2974 | 1.11k | } else if (rate_err > 8.0 && rc->avg_frame_qindex[INTER_FRAME] < 16) { |
2975 | | // Increase active_worst faster at low Q if rate fluctuation is high. |
2976 | 0 | rc->fac_active_worst_inter = 200; |
2977 | 0 | if (rc->avg_frame_qindex[INTER_FRAME] < 8) |
2978 | 0 | rc->fac_active_worst_inter = 400; |
2979 | 0 | } |
2980 | 1.24k | if (low_content && rc->avg_frame_low_motion > 80) { |
2981 | 95 | rc->af_ratio_onepass_vbr = 15; |
2982 | 1.14k | } else if (high_content || rc->avg_frame_low_motion < 30) { |
2983 | 1.03k | rc->af_ratio_onepass_vbr = 5; |
2984 | 1.03k | rc->gfu_boost = DEFAULT_GF_BOOST >> 2; |
2985 | 1.03k | } |
2986 | 1.24k | if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) { |
2987 | | // Flag to disable usage of ARF based on past usage, only allow this |
2988 | | // disabling if current frame/group does not start with key frame or |
2989 | | // scene cut. Note perc_arf_usage is only computed for speed >= 5. |
2990 | 0 | int arf_usage_low = |
2991 | 0 | (cm->frame_type != KEY_FRAME && !rc->high_source_sad && |
2992 | 0 | cpi->rc.perc_arf_usage < 15 && cpi->oxcf.speed >= 5); |
2993 | | // Don't use alt-ref for this group under certain conditions. |
2994 | 0 | if (arf_usage_low || |
2995 | 0 | (rc->high_source_sad_lagindex > 0 && |
2996 | 0 | rc->high_source_sad_lagindex <= rc->frames_till_gf_update_due) || |
2997 | 0 | (avg_source_sad_lag > 3 * sad_thresh1 >> 3)) { |
2998 | 0 | rc->source_alt_ref_pending = 0; |
2999 | 0 | rc->alt_ref_gf_group = 0; |
3000 | 0 | } else { |
3001 | 0 | rc->source_alt_ref_pending = 1; |
3002 | 0 | rc->alt_ref_gf_group = 1; |
3003 | | // If alt-ref is used for this gf group, limit the interval. |
3004 | 0 | if (rc->baseline_gf_interval > 12) { |
3005 | 0 | rc->baseline_gf_interval = 12; |
3006 | 0 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
3007 | 0 | } |
3008 | 0 | } |
3009 | 0 | } |
3010 | 1.24k | target = vp9_calc_pframe_target_size_one_pass_vbr(cpi); |
3011 | 1.24k | vp9_rc_set_frame_target(cpi, target); |
3012 | 1.24k | } |
3013 | 19.0k | rc->prev_avg_source_sad_lag = avg_source_sad_lag; |
3014 | 19.0k | } |
3015 | | |
3016 | | // Compute average source sad (temporal sad: between current source and |
3017 | | // previous source) over a subset of superblocks. Use this is detect big changes |
3018 | | // in content and allow rate control to react. |
3019 | | // This function also handles special case of lag_in_frames, to measure content |
3020 | | // level in #future frames set by the lag_in_frames. |
3021 | 20.6k | void vp9_scene_detection_onepass(VP9_COMP *cpi) { |
3022 | 20.6k | VP9_COMMON *const cm = &cpi->common; |
3023 | 20.6k | RATE_CONTROL *const rc = &cpi->rc; |
3024 | 20.6k | YV12_BUFFER_CONFIG const *unscaled_src = cpi->un_scaled_source; |
3025 | 20.6k | YV12_BUFFER_CONFIG const *unscaled_last_src = cpi->unscaled_last_source; |
3026 | 20.6k | uint8_t *src_y; |
3027 | 20.6k | int src_ystride; |
3028 | 20.6k | int src_width; |
3029 | 20.6k | int src_height; |
3030 | 20.6k | uint8_t *last_src_y; |
3031 | 20.6k | int last_src_ystride; |
3032 | 20.6k | int last_src_width; |
3033 | 20.6k | int last_src_height; |
3034 | 20.6k | if (cpi->un_scaled_source == NULL || cpi->unscaled_last_source == NULL || |
3035 | 20.6k | (cpi->use_svc && cpi->svc.current_superframe == 0)) |
3036 | 1.63k | return; |
3037 | 19.0k | src_y = unscaled_src->y_buffer; |
3038 | 19.0k | src_ystride = unscaled_src->y_stride; |
3039 | 19.0k | src_width = unscaled_src->y_width; |
3040 | 19.0k | src_height = unscaled_src->y_height; |
3041 | 19.0k | last_src_y = unscaled_last_src->y_buffer; |
3042 | 19.0k | last_src_ystride = unscaled_last_src->y_stride; |
3043 | 19.0k | last_src_width = unscaled_last_src->y_width; |
3044 | 19.0k | last_src_height = unscaled_last_src->y_height; |
3045 | 19.0k | #if CONFIG_VP9_HIGHBITDEPTH |
3046 | 19.0k | if (cm->use_highbitdepth) return; |
3047 | 19.0k | #endif |
3048 | 19.0k | rc->high_source_sad = 0; |
3049 | 19.0k | rc->high_num_blocks_with_motion = 0; |
3050 | | // For SVC: scene detection is only checked on first spatial layer of |
3051 | | // the superframe using the original/unscaled resolutions. |
3052 | 19.0k | if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode && |
3053 | 19.0k | src_width == last_src_width && src_height == last_src_height) { |
3054 | 19.0k | YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL }; |
3055 | 19.0k | int num_mi_cols = cm->mi_cols; |
3056 | 19.0k | int num_mi_rows = cm->mi_rows; |
3057 | 19.0k | int start_frame = 0; |
3058 | 19.0k | int frames_to_buffer = 1; |
3059 | 19.0k | int frame = 0; |
3060 | 19.0k | int scene_cut_force_key_frame = 0; |
3061 | 19.0k | int num_zero_temp_sad = 0; |
3062 | 19.0k | uint64_t avg_sad_current = 0; |
3063 | 19.0k | uint32_t min_thresh = 20000; // ~5 * 64 * 64 |
3064 | 19.0k | float thresh = 8.0f; |
3065 | 19.0k | uint32_t thresh_key = 140000; |
3066 | 19.0k | if (cpi->oxcf.speed <= 5) thresh_key = 240000; |
3067 | 19.0k | if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) min_thresh = 65000; |
3068 | 19.0k | if (cpi->oxcf.rc_mode == VPX_VBR) thresh = 2.1f; |
3069 | 19.0k | if (cpi->use_svc && cpi->svc.number_spatial_layers > 1) { |
3070 | 0 | const int aligned_width = ALIGN_POWER_OF_TWO(src_width, MI_SIZE_LOG2); |
3071 | 0 | const int aligned_height = ALIGN_POWER_OF_TWO(src_height, MI_SIZE_LOG2); |
3072 | 0 | num_mi_cols = aligned_width >> MI_SIZE_LOG2; |
3073 | 0 | num_mi_rows = aligned_height >> MI_SIZE_LOG2; |
3074 | 0 | } |
3075 | 19.0k | if (cpi->oxcf.lag_in_frames > 0) { |
3076 | 19.0k | frames_to_buffer = (cm->current_video_frame == 1) |
3077 | 19.0k | ? (int)vp9_lookahead_depth(cpi->lookahead) - 1 |
3078 | 19.0k | : 2; |
3079 | 19.0k | start_frame = (int)vp9_lookahead_depth(cpi->lookahead) - 1; |
3080 | 63.0k | for (frame = 0; frame < frames_to_buffer; ++frame) { |
3081 | 44.0k | const int lagframe_idx = start_frame - frame; |
3082 | 44.0k | if (lagframe_idx >= 0) { |
3083 | 41.6k | struct lookahead_entry *buf = |
3084 | 41.6k | vp9_lookahead_peek(cpi->lookahead, lagframe_idx); |
3085 | 41.6k | frames[frame] = &buf->img; |
3086 | 41.6k | } |
3087 | 44.0k | } |
3088 | | // The avg_sad for this current frame is the value of frame#1 |
3089 | | // (first future frame) from previous frame. |
3090 | 19.0k | avg_sad_current = rc->avg_source_sad[1]; |
3091 | 19.0k | if (avg_sad_current > |
3092 | 19.0k | VPXMAX(min_thresh, |
3093 | 19.0k | (unsigned int)(rc->avg_source_sad[0] * thresh)) && |
3094 | 19.0k | cm->current_video_frame > (unsigned int)cpi->oxcf.lag_in_frames) |
3095 | 0 | rc->high_source_sad = 1; |
3096 | 19.0k | else |
3097 | 19.0k | rc->high_source_sad = 0; |
3098 | 19.0k | if (rc->high_source_sad && avg_sad_current > thresh_key) |
3099 | 0 | scene_cut_force_key_frame = 1; |
3100 | | // Update recursive average for current frame. |
3101 | 19.0k | if (avg_sad_current > 0) |
3102 | 84 | rc->avg_source_sad[0] = |
3103 | 84 | (3 * rc->avg_source_sad[0] + avg_sad_current) >> 2; |
3104 | | // Shift back data, starting at frame#1. |
3105 | 456k | for (frame = 1; frame < cpi->oxcf.lag_in_frames - 1; ++frame) |
3106 | 437k | rc->avg_source_sad[frame] = rc->avg_source_sad[frame + 1]; |
3107 | 19.0k | } |
3108 | 63.0k | for (frame = 0; frame < frames_to_buffer; ++frame) { |
3109 | 44.0k | if (cpi->oxcf.lag_in_frames == 0 || |
3110 | 44.0k | (frames[frame] != NULL && frames[frame + 1] != NULL && |
3111 | 44.0k | frames[frame]->y_width == frames[frame + 1]->y_width && |
3112 | 44.0k | frames[frame]->y_height == frames[frame + 1]->y_height)) { |
3113 | 23.8k | int sbi_row, sbi_col; |
3114 | 23.8k | const int lagframe_idx = |
3115 | 23.8k | (cpi->oxcf.lag_in_frames == 0) ? 0 : start_frame - frame + 1; |
3116 | 23.8k | const BLOCK_SIZE bsize = BLOCK_64X64; |
3117 | | // Loop over sub-sample of frame, compute average sad over 64x64 blocks. |
3118 | 23.8k | uint64_t avg_sad = 0; |
3119 | 23.8k | uint64_t tmp_sad = 0; |
3120 | 23.8k | int num_samples = 0; |
3121 | 23.8k | int sb_cols = (num_mi_cols + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; |
3122 | 23.8k | int sb_rows = (num_mi_rows + MI_BLOCK_SIZE - 1) / MI_BLOCK_SIZE; |
3123 | 23.8k | if (cpi->oxcf.lag_in_frames > 0) { |
3124 | 23.8k | src_y = frames[frame]->y_buffer; |
3125 | 23.8k | src_ystride = frames[frame]->y_stride; |
3126 | 23.8k | last_src_y = frames[frame + 1]->y_buffer; |
3127 | 23.8k | last_src_ystride = frames[frame + 1]->y_stride; |
3128 | 23.8k | } |
3129 | 23.8k | num_zero_temp_sad = 0; |
3130 | 48.8k | for (sbi_row = 0; sbi_row < sb_rows; ++sbi_row) { |
3131 | 56.6k | for (sbi_col = 0; sbi_col < sb_cols; ++sbi_col) { |
3132 | | // Checker-board pattern, ignore boundary. |
3133 | 31.5k | if (((sbi_row > 0 && sbi_col > 0) && |
3134 | 31.5k | (sbi_row < sb_rows - 1 && sbi_col < sb_cols - 1) && |
3135 | 31.5k | ((sbi_row % 2 == 0 && sbi_col % 2 == 0) || |
3136 | 770 | (sbi_row % 2 != 0 && sbi_col % 2 != 0)))) { |
3137 | 406 | tmp_sad = cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y, |
3138 | 406 | last_src_ystride); |
3139 | 406 | avg_sad += tmp_sad; |
3140 | 406 | num_samples++; |
3141 | 406 | if (tmp_sad == 0) num_zero_temp_sad++; |
3142 | 406 | } |
3143 | 31.5k | src_y += 64; |
3144 | 31.5k | last_src_y += 64; |
3145 | 31.5k | } |
3146 | 25.0k | src_y += (src_ystride << 6) - (sb_cols << 6); |
3147 | 25.0k | last_src_y += (last_src_ystride << 6) - (sb_cols << 6); |
3148 | 25.0k | } |
3149 | 23.8k | if (num_samples > 0) avg_sad = avg_sad / num_samples; |
3150 | | // Set high_source_sad flag if we detect very high increase in avg_sad |
3151 | | // between current and previous frame value(s). Use minimum threshold |
3152 | | // for cases where there is small change from content that is completely |
3153 | | // static. |
3154 | 23.8k | if (lagframe_idx == 0) { |
3155 | 0 | if (avg_sad > |
3156 | 0 | VPXMAX(min_thresh, |
3157 | 0 | (unsigned int)(rc->avg_source_sad[0] * thresh)) && |
3158 | 0 | rc->frames_since_key > 1 + cpi->svc.number_spatial_layers && |
3159 | 0 | num_zero_temp_sad < 3 * (num_samples >> 2)) |
3160 | 0 | rc->high_source_sad = 1; |
3161 | 0 | else |
3162 | 0 | rc->high_source_sad = 0; |
3163 | 0 | if (rc->high_source_sad && avg_sad > thresh_key) |
3164 | 0 | scene_cut_force_key_frame = 1; |
3165 | 0 | if (avg_sad > 0 || cpi->oxcf.rc_mode == VPX_CBR) |
3166 | 0 | rc->avg_source_sad[0] = (3 * rc->avg_source_sad[0] + avg_sad) >> 2; |
3167 | 23.8k | } else { |
3168 | 23.8k | rc->avg_source_sad[lagframe_idx] = avg_sad; |
3169 | 23.8k | } |
3170 | 23.8k | if (num_zero_temp_sad < (3 * num_samples >> 2)) |
3171 | 72 | rc->high_num_blocks_with_motion = 1; |
3172 | 23.8k | } |
3173 | 44.0k | } |
3174 | | // For CBR non-screen content mode, check if we should reset the rate |
3175 | | // control. Reset is done if high_source_sad is detected and the rate |
3176 | | // control is at very low QP with rate correction factor at min level. |
3177 | 19.0k | if (cpi->oxcf.rc_mode == VPX_CBR && |
3178 | 19.0k | cpi->oxcf.content != VP9E_CONTENT_SCREEN && !cpi->use_svc) { |
3179 | 0 | if (rc->high_source_sad && rc->last_q[INTER_FRAME] == rc->best_quality && |
3180 | 0 | rc->avg_frame_qindex[INTER_FRAME] < (rc->best_quality << 1) && |
3181 | 0 | rc->rate_correction_factors[INTER_NORMAL] == MIN_BPB_FACTOR) { |
3182 | 0 | rc->rate_correction_factors[INTER_NORMAL] = 0.5; |
3183 | 0 | rc->avg_frame_qindex[INTER_FRAME] = rc->worst_quality; |
3184 | 0 | rc->buffer_level = rc->optimal_buffer_level; |
3185 | 0 | rc->bits_off_target = rc->optimal_buffer_level; |
3186 | 0 | rc->reset_high_source_sad = 1; |
3187 | 0 | } |
3188 | 0 | if (cm->frame_type != KEY_FRAME && rc->reset_high_source_sad) |
3189 | 0 | rc->this_frame_target = rc->avg_frame_bandwidth; |
3190 | 0 | } |
3191 | | // For SVC the new (updated) avg_source_sad[0] for the current superframe |
3192 | | // updates the setting for all layers. |
3193 | 19.0k | if (cpi->use_svc) { |
3194 | 0 | int sl, tl; |
3195 | 0 | SVC *const svc = &cpi->svc; |
3196 | 0 | for (sl = 0; sl < svc->number_spatial_layers; ++sl) |
3197 | 0 | for (tl = 0; tl < svc->number_temporal_layers; ++tl) { |
3198 | 0 | int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers); |
3199 | 0 | LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
3200 | 0 | RATE_CONTROL *const lrc = &lc->rc; |
3201 | 0 | lrc->avg_source_sad[0] = rc->avg_source_sad[0]; |
3202 | 0 | } |
3203 | 0 | } |
3204 | | // For VBR, under scene change/high content change, force golden refresh. |
3205 | 19.0k | if (cpi->oxcf.rc_mode == VPX_VBR && cm->frame_type != KEY_FRAME && |
3206 | 19.0k | rc->high_source_sad && rc->frames_to_key > 3 && |
3207 | 19.0k | rc->count_last_scene_change > 4 && |
3208 | 19.0k | cpi->ext_refresh_frame_flags_pending == 0) { |
3209 | 0 | int target; |
3210 | 0 | cpi->refresh_golden_frame = 1; |
3211 | 0 | if (scene_cut_force_key_frame) cm->frame_type = KEY_FRAME; |
3212 | 0 | rc->source_alt_ref_pending = 0; |
3213 | 0 | if (cpi->sf.use_altref_onepass && cpi->oxcf.enable_auto_arf) |
3214 | 0 | rc->source_alt_ref_pending = 1; |
3215 | 0 | rc->gfu_boost = DEFAULT_GF_BOOST >> 1; |
3216 | 0 | rc->baseline_gf_interval = |
3217 | 0 | VPXMIN(20, VPXMAX(10, rc->baseline_gf_interval)); |
3218 | 0 | adjust_gfint_frame_constraint(cpi, rc->frames_to_key); |
3219 | 0 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
3220 | 0 | target = vp9_calc_pframe_target_size_one_pass_vbr(cpi); |
3221 | 0 | vp9_rc_set_frame_target(cpi, target); |
3222 | 0 | rc->count_last_scene_change = 0; |
3223 | 19.0k | } else { |
3224 | 19.0k | rc->count_last_scene_change++; |
3225 | 19.0k | } |
3226 | | // If lag_in_frame is used, set the gf boost and interval. |
3227 | 19.0k | if (cpi->oxcf.lag_in_frames > 0) |
3228 | 19.0k | adjust_gf_boost_lag_one_pass_vbr(cpi, avg_sad_current); |
3229 | 19.0k | } |
3230 | 19.0k | } |
3231 | | |
3232 | | // Test if encoded frame will significantly overshoot the target bitrate, and |
3233 | | // if so, set the QP, reset/adjust some rate control parameters, and return 1. |
3234 | | // frame_size = -1 means frame has not been encoded. |
3235 | 0 | int vp9_encodedframe_overshoot(VP9_COMP *cpi, int frame_size, int *q) { |
3236 | 0 | VP9_COMMON *const cm = &cpi->common; |
3237 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
3238 | 0 | SPEED_FEATURES *const sf = &cpi->sf; |
3239 | 0 | int thresh_qp = 7 * (rc->worst_quality >> 3); |
3240 | 0 | int thresh_rate = rc->avg_frame_bandwidth << 3; |
3241 | | // Lower thresh_qp for video (more overshoot at lower Q) to be |
3242 | | // more conservative for video. |
3243 | 0 | if (cpi->oxcf.content != VP9E_CONTENT_SCREEN) |
3244 | 0 | thresh_qp = 3 * (rc->worst_quality >> 2); |
3245 | | // If this decision is not based on an encoded frame size but just on |
3246 | | // scene/slide change detection (i.e., re_encode_overshoot_cbr_rt == |
3247 | | // FAST_DETECTION_MAXQ), for now skip the (frame_size > thresh_rate) |
3248 | | // condition in this case. |
3249 | | // TODO(marpan): Use a better size/rate condition for this case and |
3250 | | // adjust thresholds. |
3251 | 0 | if ((sf->overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ || |
3252 | 0 | frame_size > thresh_rate) && |
3253 | 0 | cm->base_qindex < thresh_qp) { |
3254 | 0 | double rate_correction_factor = |
3255 | 0 | cpi->rc.rate_correction_factors[INTER_NORMAL]; |
3256 | 0 | const int target_size = cpi->rc.avg_frame_bandwidth; |
3257 | 0 | double new_correction_factor; |
3258 | 0 | int target_bits_per_mb; |
3259 | 0 | double q2; |
3260 | 0 | int enumerator; |
3261 | | // Force a re-encode, and for now use max-QP. |
3262 | 0 | *q = cpi->rc.worst_quality; |
3263 | 0 | cpi->cyclic_refresh->counter_encode_maxq_scene_change = 0; |
3264 | 0 | cpi->rc.re_encode_maxq_scene_change = 1; |
3265 | | // If the frame_size is much larger than the threshold (big content change) |
3266 | | // and the encoded frame used alot of Intra modes, then force hybrid_intra |
3267 | | // encoding for the re-encode on this scene change. hybrid_intra will |
3268 | | // use rd-based intra mode selection for small blocks. |
3269 | 0 | if (sf->overshoot_detection_cbr_rt == RE_ENCODE_MAXQ && |
3270 | 0 | frame_size > (thresh_rate << 1) && cpi->svc.spatial_layer_id == 0) { |
3271 | 0 | MODE_INFO **mi = cm->mi_grid_visible; |
3272 | 0 | int sum_intra_usage = 0; |
3273 | 0 | int mi_row, mi_col; |
3274 | 0 | for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) { |
3275 | 0 | for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) { |
3276 | 0 | if (mi[0]->ref_frame[0] == INTRA_FRAME) sum_intra_usage++; |
3277 | 0 | mi++; |
3278 | 0 | } |
3279 | 0 | mi += 8; |
3280 | 0 | } |
3281 | 0 | sum_intra_usage = 100 * sum_intra_usage / (cm->mi_rows * cm->mi_cols); |
3282 | 0 | if (sum_intra_usage > 60) cpi->rc.hybrid_intra_scene_change = 1; |
3283 | 0 | } |
3284 | | // Adjust avg_frame_qindex, buffer_level, and rate correction factors, as |
3285 | | // these parameters will affect QP selection for subsequent frames. If they |
3286 | | // have settled down to a very different (low QP) state, then not adjusting |
3287 | | // them may cause next frame to select low QP and overshoot again. |
3288 | 0 | cpi->rc.avg_frame_qindex[INTER_FRAME] = *q; |
3289 | 0 | rc->buffer_level = rc->optimal_buffer_level; |
3290 | 0 | rc->bits_off_target = rc->optimal_buffer_level; |
3291 | | // Reset rate under/over-shoot flags. |
3292 | 0 | cpi->rc.rc_1_frame = 0; |
3293 | 0 | cpi->rc.rc_2_frame = 0; |
3294 | | // Adjust rate correction factor. |
3295 | 0 | target_bits_per_mb = |
3296 | 0 | (int)(((uint64_t)target_size << BPER_MB_NORMBITS) / cm->MBs); |
3297 | | // Rate correction factor based on target_bits_per_mb and qp (==max_QP). |
3298 | | // This comes from the inverse computation of vp9_rc_bits_per_mb(). |
3299 | 0 | q2 = vp9_convert_qindex_to_q(*q, cm->bit_depth); |
3300 | 0 | enumerator = 1800000; // Factor for inter frame. |
3301 | 0 | enumerator += (int)(enumerator * q2) >> 12; |
3302 | 0 | new_correction_factor = (double)target_bits_per_mb * q2 / enumerator; |
3303 | 0 | if (new_correction_factor > rate_correction_factor) { |
3304 | 0 | rate_correction_factor = |
3305 | 0 | VPXMIN(2.0 * rate_correction_factor, new_correction_factor); |
3306 | 0 | if (rate_correction_factor > MAX_BPB_FACTOR) |
3307 | 0 | rate_correction_factor = MAX_BPB_FACTOR; |
3308 | 0 | cpi->rc.rate_correction_factors[INTER_NORMAL] = rate_correction_factor; |
3309 | 0 | } |
3310 | | // For temporal layers, reset the rate control parametes across all |
3311 | | // temporal layers. |
3312 | | // If the first_spatial_layer_to_encode > 0, then this superframe has |
3313 | | // skipped lower base layers. So in this case we should also reset and |
3314 | | // force max-q for spatial layers < first_spatial_layer_to_encode. |
3315 | | // For the case of no inter-layer prediction on delta frames: reset and |
3316 | | // force max-q for all spatial layers, to avoid excessive frame drops. |
3317 | 0 | if (cpi->use_svc) { |
3318 | 0 | int tl = 0; |
3319 | 0 | int sl = 0; |
3320 | 0 | SVC *svc = &cpi->svc; |
3321 | 0 | int num_spatial_layers = VPXMAX(1, svc->first_spatial_layer_to_encode); |
3322 | 0 | if (svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON) |
3323 | 0 | num_spatial_layers = svc->number_spatial_layers; |
3324 | 0 | for (sl = 0; sl < num_spatial_layers; ++sl) { |
3325 | 0 | for (tl = 0; tl < svc->number_temporal_layers; ++tl) { |
3326 | 0 | const int layer = |
3327 | 0 | LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers); |
3328 | 0 | LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
3329 | 0 | RATE_CONTROL *lrc = &lc->rc; |
3330 | 0 | lrc->avg_frame_qindex[INTER_FRAME] = *q; |
3331 | 0 | lrc->buffer_level = lrc->optimal_buffer_level; |
3332 | 0 | lrc->bits_off_target = lrc->optimal_buffer_level; |
3333 | 0 | lrc->rc_1_frame = 0; |
3334 | 0 | lrc->rc_2_frame = 0; |
3335 | 0 | lrc->rate_correction_factors[INTER_NORMAL] = rate_correction_factor; |
3336 | 0 | lrc->force_max_q = 1; |
3337 | 0 | } |
3338 | 0 | } |
3339 | 0 | } |
3340 | 0 | return 1; |
3341 | 0 | } else { |
3342 | 0 | return 0; |
3343 | 0 | } |
3344 | 0 | } |