/src/aom/av1/encoder/pass2_strategy.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2019, Alliance for Open Media. All rights reserved |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 2 Clause License and |
5 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
6 | | * was not distributed with this source code in the LICENSE file, you can |
7 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
8 | | * Media Patent License 1.0 was not distributed with this source code in the |
9 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
10 | | */ |
11 | | |
12 | | /*!\defgroup gf_group_algo Golden Frame Group |
13 | | * \ingroup high_level_algo |
14 | | * Algorithms regarding determining the length of GF groups and defining GF |
15 | | * group structures. |
16 | | * @{ |
17 | | */ |
18 | | /*! @} - end defgroup gf_group_algo */ |
19 | | |
20 | | #include <stdint.h> |
21 | | |
22 | | #include "config/aom_config.h" |
23 | | #include "config/aom_scale_rtcd.h" |
24 | | |
25 | | #include "aom/aom_codec.h" |
26 | | #include "aom/aom_encoder.h" |
27 | | |
28 | | #include "av1/common/av1_common_int.h" |
29 | | |
30 | | #include "av1/encoder/encoder.h" |
31 | | #include "av1/encoder/firstpass.h" |
32 | | #include "av1/encoder/gop_structure.h" |
33 | | #include "av1/encoder/pass2_strategy.h" |
34 | | #include "av1/encoder/ratectrl.h" |
35 | | #include "av1/encoder/rc_utils.h" |
36 | | #include "av1/encoder/temporal_filter.h" |
37 | | #include "av1/encoder/thirdpass.h" |
38 | | #include "av1/encoder/tpl_model.h" |
39 | | #include "av1/encoder/encode_strategy.h" |
40 | | |
41 | 1.26k | #define DEFAULT_KF_BOOST 2300 |
42 | 2.52k | #define DEFAULT_GF_BOOST 2000 |
43 | | #define GROUP_ADAPTIVE_MAXQ 1 |
44 | | |
45 | | static void init_gf_stats(GF_GROUP_STATS *gf_stats); |
46 | | static int define_gf_group_pass3(AV1_COMP *cpi, EncodeFrameParams *frame_params, |
47 | | int is_final_pass); |
48 | | |
49 | | // Calculate an active area of the image that discounts formatting |
50 | | // bars and partially discounts other 0 energy areas. |
51 | 0 | #define MIN_ACTIVE_AREA 0.5 |
52 | 0 | #define MAX_ACTIVE_AREA 1.0 |
53 | | static double calculate_active_area(const FRAME_INFO *frame_info, |
54 | 0 | const FIRSTPASS_STATS *this_frame) { |
55 | 0 | const double active_pct = |
56 | 0 | 1.0 - |
57 | 0 | ((this_frame->intra_skip_pct / 2) + |
58 | 0 | ((this_frame->inactive_zone_rows * 2) / (double)frame_info->mb_rows)); |
59 | 0 | return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA); |
60 | 0 | } |
61 | | |
62 | | // Calculate a modified Error used in distributing bits between easier and |
63 | | // harder frames. |
64 | 0 | #define ACT_AREA_CORRECTION 0.5 |
65 | | static double calculate_modified_err_new(const FRAME_INFO *frame_info, |
66 | | const FIRSTPASS_STATS *total_stats, |
67 | | const FIRSTPASS_STATS *this_stats, |
68 | | int vbrbias, double modified_error_min, |
69 | 0 | double modified_error_max) { |
70 | 0 | if (total_stats == NULL) { |
71 | 0 | return 0; |
72 | 0 | } |
73 | 0 | const double av_weight = total_stats->weight / total_stats->count; |
74 | 0 | const double av_err = |
75 | 0 | (total_stats->coded_error * av_weight) / total_stats->count; |
76 | 0 | double modified_error = |
77 | 0 | av_err * pow(this_stats->coded_error * this_stats->weight / |
78 | 0 | DOUBLE_DIVIDE_CHECK(av_err), |
79 | 0 | vbrbias / 100.0); |
80 | | |
81 | | // Correction for active area. Frames with a reduced active area |
82 | | // (eg due to formatting bars) have a higher error per mb for the |
83 | | // remaining active MBs. The correction here assumes that coding |
84 | | // 0.5N blocks of complexity 2X is a little easier than coding N |
85 | | // blocks of complexity X. |
86 | 0 | modified_error *= |
87 | 0 | pow(calculate_active_area(frame_info, this_stats), ACT_AREA_CORRECTION); |
88 | |
|
89 | 0 | return fclamp(modified_error, modified_error_min, modified_error_max); |
90 | 0 | } |
91 | | |
92 | | static double calculate_modified_err(const FRAME_INFO *frame_info, |
93 | | const TWO_PASS *twopass, |
94 | | const AV1EncoderConfig *oxcf, |
95 | 0 | const FIRSTPASS_STATS *this_frame) { |
96 | 0 | const FIRSTPASS_STATS *total_stats = twopass->stats_buf_ctx->total_stats; |
97 | 0 | return calculate_modified_err_new( |
98 | 0 | frame_info, total_stats, this_frame, oxcf->rc_cfg.vbrbias, |
99 | 0 | twopass->modified_error_min, twopass->modified_error_max); |
100 | 0 | } |
101 | | |
102 | | // Resets the first pass file to the given position using a relative seek from |
103 | | // the current position. |
104 | | static void reset_fpf_position(TWO_PASS_FRAME *p_frame, |
105 | 0 | const FIRSTPASS_STATS *position) { |
106 | 0 | p_frame->stats_in = position; |
107 | 0 | } |
108 | | |
109 | | static int input_stats(TWO_PASS *p, TWO_PASS_FRAME *p_frame, |
110 | 0 | FIRSTPASS_STATS *fps) { |
111 | 0 | if (p_frame->stats_in >= p->stats_buf_ctx->stats_in_end) return EOF; |
112 | | |
113 | 0 | *fps = *p_frame->stats_in; |
114 | 0 | ++p_frame->stats_in; |
115 | 0 | return 1; |
116 | 0 | } |
117 | | |
118 | | static int input_stats_lap(TWO_PASS *p, TWO_PASS_FRAME *p_frame, |
119 | 0 | FIRSTPASS_STATS *fps) { |
120 | 0 | if (p_frame->stats_in >= p->stats_buf_ctx->stats_in_end) return EOF; |
121 | | |
122 | 0 | *fps = *p_frame->stats_in; |
123 | | /* Move old stats[0] out to accommodate for next frame stats */ |
124 | 0 | memmove(p->frame_stats_arr[0], p->frame_stats_arr[1], |
125 | 0 | (p->stats_buf_ctx->stats_in_end - p_frame->stats_in - 1) * |
126 | 0 | sizeof(FIRSTPASS_STATS)); |
127 | 0 | p->stats_buf_ctx->stats_in_end--; |
128 | 0 | return 1; |
129 | 0 | } |
130 | | |
131 | | // Read frame stats at an offset from the current position. |
132 | | static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, |
133 | | const TWO_PASS_FRAME *p_frame, |
134 | 0 | int offset) { |
135 | 0 | if ((offset >= 0 && |
136 | 0 | p_frame->stats_in + offset >= p->stats_buf_ctx->stats_in_end) || |
137 | 0 | (offset < 0 && |
138 | 0 | p_frame->stats_in + offset < p->stats_buf_ctx->stats_in_start)) { |
139 | 0 | return NULL; |
140 | 0 | } |
141 | | |
142 | 0 | return &p_frame->stats_in[offset]; |
143 | 0 | } |
144 | | |
145 | | // This function returns the maximum target rate per frame. |
146 | | static int frame_max_bits(const RATE_CONTROL *rc, |
147 | 0 | const AV1EncoderConfig *oxcf) { |
148 | 0 | int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth * |
149 | 0 | (int64_t)oxcf->rc_cfg.vbrmax_section) / |
150 | 0 | 100; |
151 | 0 | if (max_bits < 0) |
152 | 0 | max_bits = 0; |
153 | 0 | else if (max_bits > rc->max_frame_bandwidth) |
154 | 0 | max_bits = rc->max_frame_bandwidth; |
155 | |
|
156 | 0 | return (int)max_bits; |
157 | 0 | } |
158 | | |
159 | | static const double q_pow_term[(QINDEX_RANGE >> 5) + 1] = { 0.65, 0.70, 0.75, |
160 | | 0.80, 0.85, 0.90, |
161 | | 0.95, 0.95, 0.95 }; |
162 | 0 | #define ERR_DIVISOR 96.0 |
163 | 0 | static double calc_correction_factor(double err_per_mb, int q) { |
164 | 0 | const double error_term = err_per_mb / ERR_DIVISOR; |
165 | 0 | const int index = q >> 5; |
166 | | // Adjustment to power term based on qindex |
167 | 0 | const double power_term = |
168 | 0 | q_pow_term[index] + |
169 | 0 | (((q_pow_term[index + 1] - q_pow_term[index]) * (q % 32)) / 32.0); |
170 | 0 | assert(error_term >= 0.0); |
171 | 0 | return fclamp(pow(error_term, power_term), 0.05, 5.0); |
172 | 0 | } |
173 | | |
174 | | // Based on history adjust expectations of bits per macroblock. |
175 | 0 | static void twopass_update_bpm_factor(AV1_COMP *cpi, int rate_err_tol) { |
176 | 0 | TWO_PASS *twopass = &cpi->ppi->twopass; |
177 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
178 | | |
179 | | // Based on recent history adjust expectations of bits per macroblock. |
180 | 0 | double damp_fac = AOMMAX(5.0, rate_err_tol / 10.0); |
181 | 0 | double rate_err_factor = 1.0; |
182 | 0 | const double adj_limit = AOMMAX(0.20, (double)(100 - rate_err_tol) / 200.0); |
183 | 0 | const double min_fac = 1.0 - adj_limit; |
184 | 0 | const double max_fac = 1.0 + adj_limit; |
185 | |
|
186 | 0 | if (cpi->third_pass_ctx && cpi->third_pass_ctx->frame_info_count > 0) { |
187 | 0 | int64_t actual_bits = 0; |
188 | 0 | int64_t target_bits = 0; |
189 | 0 | double factor = 0.0; |
190 | 0 | int count = 0; |
191 | 0 | for (int i = 0; i < cpi->third_pass_ctx->frame_info_count; i++) { |
192 | 0 | actual_bits += cpi->third_pass_ctx->frame_info[i].actual_bits; |
193 | 0 | target_bits += cpi->third_pass_ctx->frame_info[i].bits_allocated; |
194 | 0 | factor += cpi->third_pass_ctx->frame_info[i].bpm_factor; |
195 | 0 | count++; |
196 | 0 | } |
197 | |
|
198 | 0 | if (count == 0) { |
199 | 0 | factor = 1.0; |
200 | 0 | } else { |
201 | 0 | factor /= (double)count; |
202 | 0 | } |
203 | |
|
204 | 0 | factor *= (double)actual_bits / DOUBLE_DIVIDE_CHECK((double)target_bits); |
205 | |
|
206 | 0 | if ((twopass->bpm_factor <= 1 && factor < twopass->bpm_factor) || |
207 | 0 | (twopass->bpm_factor >= 1 && factor > twopass->bpm_factor)) { |
208 | 0 | twopass->bpm_factor = factor; |
209 | 0 | twopass->bpm_factor = |
210 | 0 | AOMMAX(min_fac, AOMMIN(max_fac, twopass->bpm_factor)); |
211 | 0 | } |
212 | 0 | } |
213 | |
|
214 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
215 | | const int is_parallel_frame = |
216 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 ? 1 : 0; |
217 | | const int simulate_parallel_frame = |
218 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE |
219 | | ? is_parallel_frame |
220 | | : 0; |
221 | | int64_t local_total_actual_bits = simulate_parallel_frame |
222 | | ? p_rc->temp_total_actual_bits |
223 | | : p_rc->total_actual_bits; |
224 | | int64_t local_vbr_bits_off_target = simulate_parallel_frame |
225 | | ? p_rc->temp_vbr_bits_off_target |
226 | | : p_rc->vbr_bits_off_target; |
227 | | int64_t local_bits_left = simulate_parallel_frame |
228 | | ? p_rc->temp_bits_left |
229 | | : cpi->ppi->twopass.bits_left; |
230 | | double local_rolling_arf_group_target_bits = |
231 | | (double)(simulate_parallel_frame |
232 | | ? p_rc->temp_rolling_arf_group_target_bits |
233 | | : twopass->rolling_arf_group_target_bits); |
234 | | double local_rolling_arf_group_actual_bits = |
235 | | (double)(simulate_parallel_frame |
236 | | ? p_rc->temp_rolling_arf_group_actual_bits |
237 | | : twopass->rolling_arf_group_actual_bits); |
238 | | int err_estimate = simulate_parallel_frame ? p_rc->temp_rate_error_estimate |
239 | | : p_rc->rate_error_estimate; |
240 | | if (local_vbr_bits_off_target && local_total_actual_bits > 0) { |
241 | | if (cpi->ppi->lap_enabled) { |
242 | | rate_err_factor = |
243 | | local_rolling_arf_group_actual_bits / |
244 | | DOUBLE_DIVIDE_CHECK(local_rolling_arf_group_target_bits); |
245 | | } else { |
246 | | rate_err_factor = |
247 | | 1.0 - ((double)(local_vbr_bits_off_target) / |
248 | | AOMMAX(local_total_actual_bits, local_bits_left)); |
249 | | } |
250 | | #else |
251 | 0 | int err_estimate = p_rc->rate_error_estimate; |
252 | |
|
253 | 0 | if (p_rc->vbr_bits_off_target && p_rc->total_actual_bits > 0) { |
254 | 0 | if (cpi->ppi->lap_enabled) { |
255 | 0 | rate_err_factor = |
256 | 0 | (double)twopass->rolling_arf_group_actual_bits / |
257 | 0 | DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits); |
258 | 0 | } else { |
259 | 0 | rate_err_factor = |
260 | 0 | 1.0 - ((double)(p_rc->vbr_bits_off_target) / |
261 | 0 | AOMMAX(p_rc->total_actual_bits, cpi->ppi->twopass.bits_left)); |
262 | 0 | } |
263 | 0 | #endif |
264 | 0 | rate_err_factor = AOMMAX(min_fac, AOMMIN(max_fac, rate_err_factor)); |
265 | | |
266 | | // Adjustment is damped if this is 1 pass with look ahead processing |
267 | | // (as there are only ever a few frames of data) and for all but the first |
268 | | // GOP in normal two pass. |
269 | 0 | if ((twopass->bpm_factor != 1.0) || cpi->ppi->lap_enabled) { |
270 | 0 | rate_err_factor = 1.0 + ((rate_err_factor - 1.0) / damp_fac); |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | // Is the rate control trending in the right direction. Only make |
275 | | // an adjustment if things are getting worse. |
276 | 0 | if ((rate_err_factor < 1.0 && err_estimate >= 0) || |
277 | 0 | (rate_err_factor > 1.0 && err_estimate <= 0)) { |
278 | 0 | twopass->bpm_factor *= rate_err_factor; |
279 | 0 | twopass->bpm_factor = AOMMAX(min_fac, AOMMIN(max_fac, twopass->bpm_factor)); |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | 0 | static int qbpm_enumerator(int rate_err_tol) { |
284 | 0 | return 1200000 + ((300000 * AOMMIN(75, AOMMAX(rate_err_tol - 25, 0))) / 75); |
285 | 0 | } |
286 | | |
287 | | // Similar to find_qindex_by_rate() function in ratectrl.c, but includes |
288 | | // calculation of a correction_factor. |
289 | | static int find_qindex_by_rate_with_correction( |
290 | | int desired_bits_per_mb, aom_bit_depth_t bit_depth, double error_per_mb, |
291 | | double group_weight_factor, int rate_err_tol, int best_qindex, |
292 | 0 | int worst_qindex) { |
293 | 0 | assert(best_qindex <= worst_qindex); |
294 | 0 | int low = best_qindex; |
295 | 0 | int high = worst_qindex; |
296 | |
|
297 | 0 | while (low < high) { |
298 | 0 | const int mid = (low + high) >> 1; |
299 | 0 | const double mid_factor = calc_correction_factor(error_per_mb, mid); |
300 | 0 | const double q = av1_convert_qindex_to_q(mid, bit_depth); |
301 | 0 | const int enumerator = qbpm_enumerator(rate_err_tol); |
302 | 0 | const int mid_bits_per_mb = |
303 | 0 | (int)((enumerator * mid_factor * group_weight_factor) / q); |
304 | |
|
305 | 0 | if (mid_bits_per_mb > desired_bits_per_mb) { |
306 | 0 | low = mid + 1; |
307 | 0 | } else { |
308 | 0 | high = mid; |
309 | 0 | } |
310 | 0 | } |
311 | 0 | return low; |
312 | 0 | } |
313 | | |
314 | | /*!\brief Choose a target maximum Q for a group of frames |
315 | | * |
316 | | * \ingroup rate_control |
317 | | * |
318 | | * This function is used to estimate a suitable maximum Q for a |
319 | | * group of frames. Inititally it is called to get a crude estimate |
320 | | * for the whole clip. It is then called for each ARF/GF group to get |
321 | | * a revised estimate for that group. |
322 | | * |
323 | | * \param[in] cpi Top-level encoder structure |
324 | | * \param[in] av_frame_err The average per frame coded error score |
325 | | * for frames making up this section/group. |
326 | | * \param[in] inactive_zone Used to mask off /ignore part of the |
327 | | * frame. The most common use case is where |
328 | | * a wide format video (e.g. 16:9) is |
329 | | * letter-boxed into a more square format. |
330 | | * Here we want to ignore the bands at the |
331 | | * top and bottom. |
332 | | * \param[in] av_target_bandwidth The target bits per frame |
333 | | * |
334 | | * \return The maximum Q for frames in the group. |
335 | | */ |
336 | | static int get_twopass_worst_quality(AV1_COMP *cpi, const double av_frame_err, |
337 | | double inactive_zone, |
338 | 0 | int av_target_bandwidth) { |
339 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
340 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
341 | 0 | const RateControlCfg *const rc_cfg = &oxcf->rc_cfg; |
342 | 0 | inactive_zone = fclamp(inactive_zone, 0.0, 0.9999); |
343 | |
|
344 | 0 | if (av_target_bandwidth <= 0) { |
345 | 0 | return rc->worst_quality; // Highest value allowed |
346 | 0 | } else { |
347 | 0 | const int num_mbs = (oxcf->resize_cfg.resize_mode != RESIZE_NONE) |
348 | 0 | ? cpi->initial_mbs |
349 | 0 | : cpi->common.mi_params.MBs; |
350 | 0 | const int active_mbs = AOMMAX(1, num_mbs - (int)(num_mbs * inactive_zone)); |
351 | 0 | const double av_err_per_mb = av_frame_err / (1.0 - inactive_zone); |
352 | 0 | const int target_norm_bits_per_mb = |
353 | 0 | (int)((uint64_t)av_target_bandwidth << BPER_MB_NORMBITS) / active_mbs; |
354 | 0 | int rate_err_tol = AOMMIN(rc_cfg->under_shoot_pct, rc_cfg->over_shoot_pct); |
355 | | |
356 | | // Update bpm correction factor based on previous GOP rate error. |
357 | 0 | twopass_update_bpm_factor(cpi, rate_err_tol); |
358 | | |
359 | | // Try and pick a max Q that will be high enough to encode the |
360 | | // content at the given rate. |
361 | 0 | int q = find_qindex_by_rate_with_correction( |
362 | 0 | target_norm_bits_per_mb, cpi->common.seq_params->bit_depth, |
363 | 0 | av_err_per_mb, cpi->ppi->twopass.bpm_factor, rate_err_tol, |
364 | 0 | rc->best_quality, rc->worst_quality); |
365 | | |
366 | | // Restriction on active max q for constrained quality mode. |
367 | 0 | if (rc_cfg->mode == AOM_CQ) q = AOMMAX(q, rc_cfg->cq_level); |
368 | 0 | return q; |
369 | 0 | } |
370 | 0 | } |
371 | | |
372 | 0 | #define INTRA_PART 0.005 |
373 | | #define DEFAULT_DECAY_LIMIT 0.75 |
374 | 0 | #define LOW_SR_DIFF_TRHESH 0.01 |
375 | 0 | #define NCOUNT_FRAME_II_THRESH 5.0 |
376 | 0 | #define LOW_CODED_ERR_PER_MB 0.01 |
377 | | |
378 | | /* This function considers how the quality of prediction may be deteriorating |
379 | | * with distance. It comapres the coded error for the last frame and the |
380 | | * second reference frame (usually two frames old) and also applies a factor |
381 | | * based on the extent of INTRA coding. |
382 | | * |
383 | | * The decay factor is then used to reduce the contribution of frames further |
384 | | * from the alt-ref or golden frame, to the bitframe boost calculation for that |
385 | | * alt-ref or golden frame. |
386 | | */ |
387 | 0 | static double get_sr_decay_rate(const FIRSTPASS_STATS *frame) { |
388 | 0 | double sr_diff = (frame->sr_coded_error - frame->coded_error); |
389 | 0 | double sr_decay = 1.0; |
390 | 0 | double modified_pct_inter; |
391 | 0 | double modified_pcnt_intra; |
392 | |
|
393 | 0 | modified_pct_inter = frame->pcnt_inter; |
394 | 0 | if ((frame->coded_error > LOW_CODED_ERR_PER_MB) && |
395 | 0 | ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) < |
396 | 0 | (double)NCOUNT_FRAME_II_THRESH)) { |
397 | 0 | modified_pct_inter = frame->pcnt_inter - frame->pcnt_neutral; |
398 | 0 | } |
399 | 0 | modified_pcnt_intra = 100 * (1.0 - modified_pct_inter); |
400 | |
|
401 | 0 | if ((sr_diff > LOW_SR_DIFF_TRHESH)) { |
402 | 0 | double sr_diff_part = ((sr_diff * 0.25) / frame->intra_error); |
403 | 0 | sr_decay = 1.0 - sr_diff_part - (INTRA_PART * modified_pcnt_intra); |
404 | 0 | } |
405 | 0 | return AOMMAX(sr_decay, DEFAULT_DECAY_LIMIT); |
406 | 0 | } |
407 | | |
408 | | // This function gives an estimate of how badly we believe the prediction |
409 | | // quality is decaying from frame to frame. |
410 | 0 | static double get_zero_motion_factor(const FIRSTPASS_STATS *frame) { |
411 | 0 | const double zero_motion_pct = frame->pcnt_inter - frame->pcnt_motion; |
412 | 0 | double sr_decay = get_sr_decay_rate(frame); |
413 | 0 | return AOMMIN(sr_decay, zero_motion_pct); |
414 | 0 | } |
415 | | |
416 | 0 | #define DEFAULT_ZM_FACTOR 0.5 |
417 | 0 | static double get_prediction_decay_rate(const FIRSTPASS_STATS *frame_stats) { |
418 | 0 | const double sr_decay_rate = get_sr_decay_rate(frame_stats); |
419 | 0 | double zero_motion_factor = |
420 | 0 | DEFAULT_ZM_FACTOR * (frame_stats->pcnt_inter - frame_stats->pcnt_motion); |
421 | | |
422 | | // Clamp value to range 0.0 to 1.0 |
423 | | // This should happen anyway if input values are sensibly clamped but checked |
424 | | // here just in case. |
425 | 0 | if (zero_motion_factor > 1.0) |
426 | 0 | zero_motion_factor = 1.0; |
427 | 0 | else if (zero_motion_factor < 0.0) |
428 | 0 | zero_motion_factor = 0.0; |
429 | |
|
430 | 0 | return AOMMAX(zero_motion_factor, |
431 | 0 | (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor))); |
432 | 0 | } |
433 | | |
434 | | // Function to test for a condition where a complex transition is followed |
435 | | // by a static section. For example in slide shows where there is a fade |
436 | | // between slides. This is to help with more optimal kf and gf positioning. |
437 | | static int detect_transition_to_still(const FIRSTPASS_INFO *firstpass_info, |
438 | | int next_stats_index, |
439 | | const int min_gf_interval, |
440 | | const int frame_interval, |
441 | | const int still_interval, |
442 | | const double loop_decay_rate, |
443 | 0 | const double last_decay_rate) { |
444 | | // Break clause to detect very still sections after motion |
445 | | // For example a static image after a fade or other transition |
446 | | // instead of a clean scene cut. |
447 | 0 | if (frame_interval > min_gf_interval && loop_decay_rate >= 0.999 && |
448 | 0 | last_decay_rate < 0.9) { |
449 | 0 | int stats_left = |
450 | 0 | av1_firstpass_info_future_count(firstpass_info, next_stats_index); |
451 | 0 | if (stats_left >= still_interval) { |
452 | 0 | int j; |
453 | | // Look ahead a few frames to see if static condition persists... |
454 | 0 | for (j = 0; j < still_interval; ++j) { |
455 | 0 | const FIRSTPASS_STATS *stats = |
456 | 0 | av1_firstpass_info_peek(firstpass_info, next_stats_index + j); |
457 | 0 | if (stats->pcnt_inter - stats->pcnt_motion < 0.999) break; |
458 | 0 | } |
459 | | // Only if it does do we signal a transition to still. |
460 | 0 | return j == still_interval; |
461 | 0 | } |
462 | 0 | } |
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | | // This function detects a flash through the high relative pcnt_second_ref |
467 | | // score in the frame following a flash frame. The offset passed in should |
468 | | // reflect this. |
469 | | static int detect_flash(const TWO_PASS *twopass, |
470 | 0 | const TWO_PASS_FRAME *twopass_frame, const int offset) { |
471 | 0 | const FIRSTPASS_STATS *const next_frame = |
472 | 0 | read_frame_stats(twopass, twopass_frame, offset); |
473 | | |
474 | | // What we are looking for here is a situation where there is a |
475 | | // brief break in prediction (such as a flash) but subsequent frames |
476 | | // are reasonably well predicted by an earlier (pre flash) frame. |
477 | | // The recovery after a flash is indicated by a high pcnt_second_ref |
478 | | // compared to pcnt_inter. |
479 | 0 | return next_frame != NULL && |
480 | 0 | next_frame->pcnt_second_ref > next_frame->pcnt_inter && |
481 | 0 | next_frame->pcnt_second_ref >= 0.5; |
482 | 0 | } |
483 | | |
484 | | // Update the motion related elements to the GF arf boost calculation. |
485 | | static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats, |
486 | | GF_GROUP_STATS *gf_stats, double f_w, |
487 | 0 | double f_h) { |
488 | 0 | const double pct = stats->pcnt_motion; |
489 | | |
490 | | // Accumulate Motion In/Out of frame stats. |
491 | 0 | gf_stats->this_frame_mv_in_out = stats->mv_in_out_count * pct; |
492 | 0 | gf_stats->mv_in_out_accumulator += gf_stats->this_frame_mv_in_out; |
493 | 0 | gf_stats->abs_mv_in_out_accumulator += fabs(gf_stats->this_frame_mv_in_out); |
494 | | |
495 | | // Accumulate a measure of how uniform (or conversely how random) the motion |
496 | | // field is (a ratio of abs(mv) / mv). |
497 | 0 | if (pct > 0.05) { |
498 | 0 | const double mvr_ratio = |
499 | 0 | fabs(stats->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVr)); |
500 | 0 | const double mvc_ratio = |
501 | 0 | fabs(stats->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVc)); |
502 | |
|
503 | 0 | gf_stats->mv_ratio_accumulator += |
504 | 0 | pct * |
505 | 0 | (mvr_ratio < stats->mvr_abs * f_h ? mvr_ratio : stats->mvr_abs * f_h); |
506 | 0 | gf_stats->mv_ratio_accumulator += |
507 | 0 | pct * |
508 | 0 | (mvc_ratio < stats->mvc_abs * f_w ? mvc_ratio : stats->mvc_abs * f_w); |
509 | 0 | } |
510 | 0 | } |
511 | | |
512 | | static void accumulate_this_frame_stats(const FIRSTPASS_STATS *stats, |
513 | | const double mod_frame_err, |
514 | 0 | GF_GROUP_STATS *gf_stats) { |
515 | 0 | gf_stats->gf_group_err += mod_frame_err; |
516 | 0 | #if GROUP_ADAPTIVE_MAXQ |
517 | 0 | gf_stats->gf_group_raw_error += stats->coded_error; |
518 | 0 | #endif |
519 | 0 | gf_stats->gf_group_skip_pct += stats->intra_skip_pct; |
520 | 0 | gf_stats->gf_group_inactive_zone_rows += stats->inactive_zone_rows; |
521 | 0 | } |
522 | | |
523 | | static void accumulate_next_frame_stats(const FIRSTPASS_STATS *stats, |
524 | | const int flash_detected, |
525 | | const int frames_since_key, |
526 | | const int cur_idx, |
527 | | GF_GROUP_STATS *gf_stats, int f_w, |
528 | 0 | int f_h) { |
529 | 0 | accumulate_frame_motion_stats(stats, gf_stats, f_w, f_h); |
530 | | // sum up the metric values of current gf group |
531 | 0 | gf_stats->avg_sr_coded_error += stats->sr_coded_error; |
532 | 0 | gf_stats->avg_pcnt_second_ref += stats->pcnt_second_ref; |
533 | 0 | gf_stats->avg_new_mv_count += stats->new_mv_count; |
534 | 0 | gf_stats->avg_wavelet_energy += stats->frame_avg_wavelet_energy; |
535 | 0 | if (fabs(stats->raw_error_stdev) > 0.000001) { |
536 | 0 | gf_stats->non_zero_stdev_count++; |
537 | 0 | gf_stats->avg_raw_err_stdev += stats->raw_error_stdev; |
538 | 0 | } |
539 | | |
540 | | // Accumulate the effect of prediction quality decay |
541 | 0 | if (!flash_detected) { |
542 | 0 | gf_stats->last_loop_decay_rate = gf_stats->loop_decay_rate; |
543 | 0 | gf_stats->loop_decay_rate = get_prediction_decay_rate(stats); |
544 | |
|
545 | 0 | gf_stats->decay_accumulator = |
546 | 0 | gf_stats->decay_accumulator * gf_stats->loop_decay_rate; |
547 | | |
548 | | // Monitor for static sections. |
549 | 0 | if ((frames_since_key + cur_idx - 1) > 1) { |
550 | 0 | gf_stats->zero_motion_accumulator = AOMMIN( |
551 | 0 | gf_stats->zero_motion_accumulator, get_zero_motion_factor(stats)); |
552 | 0 | } |
553 | 0 | } |
554 | 0 | } |
555 | | |
556 | 0 | static void average_gf_stats(const int total_frame, GF_GROUP_STATS *gf_stats) { |
557 | 0 | if (total_frame) { |
558 | 0 | gf_stats->avg_sr_coded_error /= total_frame; |
559 | 0 | gf_stats->avg_pcnt_second_ref /= total_frame; |
560 | 0 | gf_stats->avg_new_mv_count /= total_frame; |
561 | 0 | gf_stats->avg_wavelet_energy /= total_frame; |
562 | 0 | } |
563 | |
|
564 | 0 | if (gf_stats->non_zero_stdev_count) |
565 | 0 | gf_stats->avg_raw_err_stdev /= gf_stats->non_zero_stdev_count; |
566 | 0 | } |
567 | | |
568 | 0 | #define BOOST_FACTOR 12.5 |
569 | 0 | static double baseline_err_per_mb(const FRAME_INFO *frame_info) { |
570 | 0 | unsigned int screen_area = frame_info->frame_height * frame_info->frame_width; |
571 | | |
572 | | // Use a different error per mb factor for calculating boost for |
573 | | // different formats. |
574 | 0 | if (screen_area <= 640 * 360) { |
575 | 0 | return 500.0; |
576 | 0 | } else { |
577 | 0 | return 1000.0; |
578 | 0 | } |
579 | 0 | } |
580 | | |
581 | | static double calc_frame_boost(const PRIMARY_RATE_CONTROL *p_rc, |
582 | | const FRAME_INFO *frame_info, |
583 | | const FIRSTPASS_STATS *this_frame, |
584 | 0 | double this_frame_mv_in_out, double max_boost) { |
585 | 0 | double frame_boost; |
586 | 0 | const double lq = av1_convert_qindex_to_q(p_rc->avg_frame_qindex[INTER_FRAME], |
587 | 0 | frame_info->bit_depth); |
588 | 0 | const double boost_q_correction = AOMMIN((0.5 + (lq * 0.015)), 1.5); |
589 | 0 | const double active_area = calculate_active_area(frame_info, this_frame); |
590 | | |
591 | | // Underlying boost factor is based on inter error ratio. |
592 | 0 | frame_boost = AOMMAX(baseline_err_per_mb(frame_info) * active_area, |
593 | 0 | this_frame->intra_error * active_area) / |
594 | 0 | DOUBLE_DIVIDE_CHECK(this_frame->coded_error); |
595 | 0 | frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction; |
596 | | |
597 | | // Increase boost for frames where new data coming into frame (e.g. zoom out). |
598 | | // Slightly reduce boost if there is a net balance of motion out of the frame |
599 | | // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. |
600 | 0 | if (this_frame_mv_in_out > 0.0) |
601 | 0 | frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); |
602 | | // In the extreme case the boost is halved. |
603 | 0 | else |
604 | 0 | frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); |
605 | |
|
606 | 0 | return AOMMIN(frame_boost, max_boost * boost_q_correction); |
607 | 0 | } |
608 | | |
609 | | static double calc_kf_frame_boost(const PRIMARY_RATE_CONTROL *p_rc, |
610 | | const FRAME_INFO *frame_info, |
611 | | const FIRSTPASS_STATS *this_frame, |
612 | 0 | double *sr_accumulator, double max_boost) { |
613 | 0 | double frame_boost; |
614 | 0 | const double lq = av1_convert_qindex_to_q(p_rc->avg_frame_qindex[INTER_FRAME], |
615 | 0 | frame_info->bit_depth); |
616 | 0 | const double boost_q_correction = AOMMIN((0.50 + (lq * 0.015)), 2.00); |
617 | 0 | const double active_area = calculate_active_area(frame_info, this_frame); |
618 | | |
619 | | // Underlying boost factor is based on inter error ratio. |
620 | 0 | frame_boost = AOMMAX(baseline_err_per_mb(frame_info) * active_area, |
621 | 0 | this_frame->intra_error * active_area) / |
622 | 0 | DOUBLE_DIVIDE_CHECK( |
623 | 0 | (this_frame->coded_error + *sr_accumulator) * active_area); |
624 | | |
625 | | // Update the accumulator for second ref error difference. |
626 | | // This is intended to give an indication of how much the coded error is |
627 | | // increasing over time. |
628 | 0 | *sr_accumulator += (this_frame->sr_coded_error - this_frame->coded_error); |
629 | 0 | *sr_accumulator = AOMMAX(0.0, *sr_accumulator); |
630 | | |
631 | | // Q correction and scaling |
632 | | // The 40.0 value here is an experimentally derived baseline minimum. |
633 | | // This value is in line with the minimum per frame boost in the alt_ref |
634 | | // boost calculation. |
635 | 0 | frame_boost = ((frame_boost + 40.0) * boost_q_correction); |
636 | |
|
637 | 0 | return AOMMIN(frame_boost, max_boost * boost_q_correction); |
638 | 0 | } |
639 | | |
640 | | static int get_projected_gfu_boost(const PRIMARY_RATE_CONTROL *p_rc, |
641 | | int gfu_boost, int frames_to_project, |
642 | 0 | int num_stats_used_for_gfu_boost) { |
643 | | /* |
644 | | * If frames_to_project is equal to num_stats_used_for_gfu_boost, |
645 | | * it means that gfu_boost was calculated over frames_to_project to |
646 | | * begin with(ie; all stats required were available), hence return |
647 | | * the original boost. |
648 | | */ |
649 | 0 | if (num_stats_used_for_gfu_boost >= frames_to_project) return gfu_boost; |
650 | | |
651 | 0 | double min_boost_factor = sqrt(p_rc->baseline_gf_interval); |
652 | | // Get the current tpl factor (number of frames = frames_to_project). |
653 | 0 | double tpl_factor = av1_get_gfu_boost_projection_factor( |
654 | 0 | min_boost_factor, MAX_GFUBOOST_FACTOR, frames_to_project); |
655 | | // Get the tpl factor when number of frames = num_stats_used_for_prior_boost. |
656 | 0 | double tpl_factor_num_stats = av1_get_gfu_boost_projection_factor( |
657 | 0 | min_boost_factor, MAX_GFUBOOST_FACTOR, num_stats_used_for_gfu_boost); |
658 | 0 | int projected_gfu_boost = |
659 | 0 | (int)rint((tpl_factor * gfu_boost) / tpl_factor_num_stats); |
660 | 0 | return projected_gfu_boost; |
661 | 0 | } |
662 | | |
663 | 0 | #define GF_MAX_BOOST 90.0 |
664 | 0 | #define GF_MIN_BOOST 50 |
665 | 0 | #define MIN_DECAY_FACTOR 0.01 |
666 | | int av1_calc_arf_boost(const TWO_PASS *twopass, |
667 | | const TWO_PASS_FRAME *twopass_frame, |
668 | | const PRIMARY_RATE_CONTROL *p_rc, FRAME_INFO *frame_info, |
669 | | int offset, int f_frames, int b_frames, |
670 | | int *num_fpstats_used, int *num_fpstats_required, |
671 | 0 | int project_gfu_boost) { |
672 | 0 | int i; |
673 | 0 | GF_GROUP_STATS gf_stats; |
674 | 0 | init_gf_stats(&gf_stats); |
675 | 0 | double boost_score = (double)NORMAL_BOOST; |
676 | 0 | int arf_boost; |
677 | 0 | int flash_detected = 0; |
678 | 0 | if (num_fpstats_used) *num_fpstats_used = 0; |
679 | | |
680 | | // Search forward from the proposed arf/next gf position. |
681 | 0 | for (i = 0; i < f_frames; ++i) { |
682 | 0 | const FIRSTPASS_STATS *this_frame = |
683 | 0 | read_frame_stats(twopass, twopass_frame, i + offset); |
684 | 0 | if (this_frame == NULL) break; |
685 | | |
686 | | // Update the motion related elements to the boost calculation. |
687 | 0 | accumulate_frame_motion_stats(this_frame, &gf_stats, |
688 | 0 | frame_info->frame_width, |
689 | 0 | frame_info->frame_height); |
690 | | |
691 | | // We want to discount the flash frame itself and the recovery |
692 | | // frame that follows as both will have poor scores. |
693 | 0 | flash_detected = detect_flash(twopass, twopass_frame, i + offset) || |
694 | 0 | detect_flash(twopass, twopass_frame, i + offset + 1); |
695 | | |
696 | | // Accumulate the effect of prediction quality decay. |
697 | 0 | if (!flash_detected) { |
698 | 0 | gf_stats.decay_accumulator *= get_prediction_decay_rate(this_frame); |
699 | 0 | gf_stats.decay_accumulator = gf_stats.decay_accumulator < MIN_DECAY_FACTOR |
700 | 0 | ? MIN_DECAY_FACTOR |
701 | 0 | : gf_stats.decay_accumulator; |
702 | 0 | } |
703 | |
|
704 | 0 | boost_score += |
705 | 0 | gf_stats.decay_accumulator * |
706 | 0 | calc_frame_boost(p_rc, frame_info, this_frame, |
707 | 0 | gf_stats.this_frame_mv_in_out, GF_MAX_BOOST); |
708 | 0 | if (num_fpstats_used) (*num_fpstats_used)++; |
709 | 0 | } |
710 | |
|
711 | 0 | arf_boost = (int)boost_score; |
712 | | |
713 | | // Reset for backward looking loop. |
714 | 0 | boost_score = 0.0; |
715 | 0 | init_gf_stats(&gf_stats); |
716 | | // Search backward towards last gf position. |
717 | 0 | for (i = -1; i >= -b_frames; --i) { |
718 | 0 | const FIRSTPASS_STATS *this_frame = |
719 | 0 | read_frame_stats(twopass, twopass_frame, i + offset); |
720 | 0 | if (this_frame == NULL) break; |
721 | | |
722 | | // Update the motion related elements to the boost calculation. |
723 | 0 | accumulate_frame_motion_stats(this_frame, &gf_stats, |
724 | 0 | frame_info->frame_width, |
725 | 0 | frame_info->frame_height); |
726 | | |
727 | | // We want to discount the the flash frame itself and the recovery |
728 | | // frame that follows as both will have poor scores. |
729 | 0 | flash_detected = detect_flash(twopass, twopass_frame, i + offset) || |
730 | 0 | detect_flash(twopass, twopass_frame, i + offset + 1); |
731 | | |
732 | | // Cumulative effect of prediction quality decay. |
733 | 0 | if (!flash_detected) { |
734 | 0 | gf_stats.decay_accumulator *= get_prediction_decay_rate(this_frame); |
735 | 0 | gf_stats.decay_accumulator = gf_stats.decay_accumulator < MIN_DECAY_FACTOR |
736 | 0 | ? MIN_DECAY_FACTOR |
737 | 0 | : gf_stats.decay_accumulator; |
738 | 0 | } |
739 | |
|
740 | 0 | boost_score += |
741 | 0 | gf_stats.decay_accumulator * |
742 | 0 | calc_frame_boost(p_rc, frame_info, this_frame, |
743 | 0 | gf_stats.this_frame_mv_in_out, GF_MAX_BOOST); |
744 | 0 | if (num_fpstats_used) (*num_fpstats_used)++; |
745 | 0 | } |
746 | 0 | arf_boost += (int)boost_score; |
747 | |
|
748 | 0 | if (project_gfu_boost) { |
749 | 0 | assert(num_fpstats_required != NULL); |
750 | 0 | assert(num_fpstats_used != NULL); |
751 | 0 | *num_fpstats_required = f_frames + b_frames; |
752 | 0 | arf_boost = get_projected_gfu_boost(p_rc, arf_boost, *num_fpstats_required, |
753 | 0 | *num_fpstats_used); |
754 | 0 | } |
755 | |
|
756 | 0 | if (arf_boost < ((b_frames + f_frames) * GF_MIN_BOOST)) |
757 | 0 | arf_boost = ((b_frames + f_frames) * GF_MIN_BOOST); |
758 | |
|
759 | 0 | return arf_boost; |
760 | 0 | } |
761 | | |
762 | | // Calculate a section intra ratio used in setting max loop filter. |
763 | | static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin, |
764 | | const FIRSTPASS_STATS *end, |
765 | 0 | int section_length) { |
766 | 0 | const FIRSTPASS_STATS *s = begin; |
767 | 0 | double intra_error = 0.0; |
768 | 0 | double coded_error = 0.0; |
769 | 0 | int i = 0; |
770 | |
|
771 | 0 | while (s < end && i < section_length) { |
772 | 0 | intra_error += s->intra_error; |
773 | 0 | coded_error += s->coded_error; |
774 | 0 | ++s; |
775 | 0 | ++i; |
776 | 0 | } |
777 | |
|
778 | 0 | return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error)); |
779 | 0 | } |
780 | | |
781 | | /*!\brief Calculates the bit target for this GF/ARF group |
782 | | * |
783 | | * \ingroup rate_control |
784 | | * |
785 | | * Calculates the total bits to allocate in this GF/ARF group. |
786 | | * |
787 | | * \param[in] cpi Top-level encoder structure |
788 | | * \param[in] gf_group_err Cumulative coded error score for the |
789 | | * frames making up this group. |
790 | | * |
791 | | * \return The target total number of bits for this GF/ARF group. |
792 | | */ |
793 | | static int64_t calculate_total_gf_group_bits(AV1_COMP *cpi, |
794 | 0 | double gf_group_err) { |
795 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
796 | 0 | const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
797 | 0 | const TWO_PASS *const twopass = &cpi->ppi->twopass; |
798 | 0 | const int max_bits = frame_max_bits(rc, &cpi->oxcf); |
799 | 0 | int64_t total_group_bits; |
800 | | |
801 | | // Calculate the bits to be allocated to the group as a whole. |
802 | 0 | if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) { |
803 | 0 | total_group_bits = (int64_t)(twopass->kf_group_bits * |
804 | 0 | (gf_group_err / twopass->kf_group_error_left)); |
805 | 0 | } else { |
806 | 0 | total_group_bits = 0; |
807 | 0 | } |
808 | | |
809 | | // Clamp odd edge cases. |
810 | 0 | total_group_bits = (total_group_bits < 0) |
811 | 0 | ? 0 |
812 | 0 | : (total_group_bits > twopass->kf_group_bits) |
813 | 0 | ? twopass->kf_group_bits |
814 | 0 | : total_group_bits; |
815 | | |
816 | | // Clip based on user supplied data rate variability limit. |
817 | 0 | if (total_group_bits > (int64_t)max_bits * p_rc->baseline_gf_interval) |
818 | 0 | total_group_bits = (int64_t)max_bits * p_rc->baseline_gf_interval; |
819 | |
|
820 | 0 | return total_group_bits; |
821 | 0 | } |
822 | | |
823 | | // Calculate the number of bits to assign to boosted frames in a group. |
824 | | static int calculate_boost_bits(int frame_count, int boost, |
825 | 0 | int64_t total_group_bits) { |
826 | 0 | int allocation_chunks; |
827 | | |
828 | | // return 0 for invalid inputs (could arise e.g. through rounding errors) |
829 | 0 | if (!boost || (total_group_bits <= 0)) return 0; |
830 | | |
831 | 0 | if (frame_count <= 0) return (int)(AOMMIN(total_group_bits, INT_MAX)); |
832 | | |
833 | 0 | allocation_chunks = (frame_count * 100) + boost; |
834 | | |
835 | | // Prevent overflow. |
836 | 0 | if (boost > 1023) { |
837 | 0 | int divisor = boost >> 10; |
838 | 0 | boost /= divisor; |
839 | 0 | allocation_chunks /= divisor; |
840 | 0 | } |
841 | | |
842 | | // Calculate the number of extra bits for use in the boosted frame or frames. |
843 | 0 | return AOMMAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), |
844 | 0 | 0); |
845 | 0 | } |
846 | | |
847 | | // Calculate the boost factor based on the number of bits assigned, i.e. the |
848 | | // inverse of calculate_boost_bits(). |
849 | | static int calculate_boost_factor(int frame_count, int bits, |
850 | 0 | int64_t total_group_bits) { |
851 | 0 | return (int)(100.0 * frame_count * bits / (total_group_bits - bits)); |
852 | 0 | } |
853 | | |
854 | | // Reduce the number of bits assigned to keyframe or arf if necessary, to |
855 | | // prevent bitrate spikes that may break level constraints. |
856 | | // frame_type: 0: keyframe; 1: arf. |
857 | | static int adjust_boost_bits_for_target_level(const AV1_COMP *const cpi, |
858 | | RATE_CONTROL *const rc, |
859 | | int bits_assigned, |
860 | | int64_t group_bits, |
861 | 0 | int frame_type) { |
862 | 0 | const AV1_COMMON *const cm = &cpi->common; |
863 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
864 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
865 | 0 | const int temporal_layer_id = cm->temporal_layer_id; |
866 | 0 | const int spatial_layer_id = cm->spatial_layer_id; |
867 | 0 | for (int index = 0; index < seq_params->operating_points_cnt_minus_1 + 1; |
868 | 0 | ++index) { |
869 | 0 | if (!is_in_operating_point(seq_params->operating_point_idc[index], |
870 | 0 | temporal_layer_id, spatial_layer_id)) { |
871 | 0 | continue; |
872 | 0 | } |
873 | | |
874 | 0 | const AV1_LEVEL target_level = |
875 | 0 | cpi->ppi->level_params.target_seq_level_idx[index]; |
876 | 0 | if (target_level >= SEQ_LEVELS) continue; |
877 | | |
878 | 0 | assert(is_valid_seq_level_idx(target_level)); |
879 | |
|
880 | 0 | const double level_bitrate_limit = av1_get_max_bitrate_for_level( |
881 | 0 | target_level, seq_params->tier[0], seq_params->profile); |
882 | 0 | const int target_bits_per_frame = |
883 | 0 | (int)(level_bitrate_limit / cpi->framerate); |
884 | 0 | if (frame_type == 0) { |
885 | | // Maximum bits for keyframe is 8 times the target_bits_per_frame. |
886 | 0 | const int level_enforced_max_kf_bits = target_bits_per_frame * 8; |
887 | 0 | if (bits_assigned > level_enforced_max_kf_bits) { |
888 | 0 | const int frames = rc->frames_to_key - 1; |
889 | 0 | p_rc->kf_boost = calculate_boost_factor( |
890 | 0 | frames, level_enforced_max_kf_bits, group_bits); |
891 | 0 | bits_assigned = |
892 | 0 | calculate_boost_bits(frames, p_rc->kf_boost, group_bits); |
893 | 0 | } |
894 | 0 | } else if (frame_type == 1) { |
895 | | // Maximum bits for arf is 4 times the target_bits_per_frame. |
896 | 0 | const int level_enforced_max_arf_bits = target_bits_per_frame * 4; |
897 | 0 | if (bits_assigned > level_enforced_max_arf_bits) { |
898 | 0 | p_rc->gfu_boost = |
899 | 0 | calculate_boost_factor(p_rc->baseline_gf_interval, |
900 | 0 | level_enforced_max_arf_bits, group_bits); |
901 | 0 | bits_assigned = calculate_boost_bits(p_rc->baseline_gf_interval, |
902 | 0 | p_rc->gfu_boost, group_bits); |
903 | 0 | } |
904 | 0 | } else { |
905 | 0 | assert(0); |
906 | 0 | } |
907 | 0 | } |
908 | |
|
909 | 0 | return bits_assigned; |
910 | 0 | } |
911 | | |
912 | | // Allocate bits to each frame in a GF / ARF group |
913 | | double layer_fraction[MAX_ARF_LAYERS + 1] = { 1.0, 0.70, 0.55, 0.60, |
914 | | 0.60, 1.0, 1.0 }; |
915 | | static void allocate_gf_group_bits(GF_GROUP *gf_group, |
916 | | PRIMARY_RATE_CONTROL *const p_rc, |
917 | | RATE_CONTROL *const rc, |
918 | | int64_t gf_group_bits, int gf_arf_bits, |
919 | 0 | int key_frame, int use_arf) { |
920 | 0 | int64_t total_group_bits = gf_group_bits; |
921 | 0 | int base_frame_bits; |
922 | 0 | const int gf_group_size = gf_group->size; |
923 | 0 | int layer_frames[MAX_ARF_LAYERS + 1] = { 0 }; |
924 | | |
925 | | // For key frames the frame target rate is already set and it |
926 | | // is also the golden frame. |
927 | | // === [frame_index == 0] === |
928 | 0 | int frame_index = !!key_frame; |
929 | | |
930 | | // Subtract the extra bits set aside for ARF frames from the Group Total |
931 | 0 | if (use_arf) total_group_bits -= gf_arf_bits; |
932 | |
|
933 | 0 | int num_frames = |
934 | 0 | AOMMAX(1, p_rc->baseline_gf_interval - (rc->frames_since_key == 0)); |
935 | 0 | base_frame_bits = (int)(total_group_bits / num_frames); |
936 | | |
937 | | // Check the number of frames in each layer in case we have a |
938 | | // non standard group length. |
939 | 0 | int max_arf_layer = gf_group->max_layer_depth - 1; |
940 | 0 | for (int idx = frame_index; idx < gf_group_size; ++idx) { |
941 | 0 | if ((gf_group->update_type[idx] == ARF_UPDATE) || |
942 | 0 | (gf_group->update_type[idx] == INTNL_ARF_UPDATE)) { |
943 | 0 | layer_frames[gf_group->layer_depth[idx]]++; |
944 | 0 | } |
945 | 0 | } |
946 | | |
947 | | // Allocate extra bits to each ARF layer |
948 | 0 | int i; |
949 | 0 | int layer_extra_bits[MAX_ARF_LAYERS + 1] = { 0 }; |
950 | 0 | for (i = 1; i <= max_arf_layer; ++i) { |
951 | 0 | double fraction = (i == max_arf_layer) ? 1.0 : layer_fraction[i]; |
952 | 0 | layer_extra_bits[i] = |
953 | 0 | (int)((gf_arf_bits * fraction) / AOMMAX(1, layer_frames[i])); |
954 | 0 | gf_arf_bits -= (int)(gf_arf_bits * fraction); |
955 | 0 | } |
956 | | |
957 | | // Now combine ARF layer and baseline bits to give total bits for each frame. |
958 | 0 | int arf_extra_bits; |
959 | 0 | for (int idx = frame_index; idx < gf_group_size; ++idx) { |
960 | 0 | switch (gf_group->update_type[idx]) { |
961 | 0 | case ARF_UPDATE: |
962 | 0 | case INTNL_ARF_UPDATE: |
963 | 0 | arf_extra_bits = layer_extra_bits[gf_group->layer_depth[idx]]; |
964 | 0 | gf_group->bit_allocation[idx] = base_frame_bits + arf_extra_bits; |
965 | 0 | break; |
966 | 0 | case INTNL_OVERLAY_UPDATE: |
967 | 0 | case OVERLAY_UPDATE: gf_group->bit_allocation[idx] = 0; break; |
968 | 0 | default: gf_group->bit_allocation[idx] = base_frame_bits; break; |
969 | 0 | } |
970 | 0 | } |
971 | | |
972 | | // Set the frame following the current GOP to 0 bit allocation. For ARF |
973 | | // groups, this next frame will be overlay frame, which is the first frame |
974 | | // in the next GOP. For GF group, next GOP will overwrite the rate allocation. |
975 | | // Setting this frame to use 0 bit (of out the current GOP budget) will |
976 | | // simplify logics in reference frame management. |
977 | 0 | if (gf_group_size < MAX_STATIC_GF_GROUP_LENGTH) |
978 | 0 | gf_group->bit_allocation[gf_group_size] = 0; |
979 | 0 | } |
980 | | |
981 | | // Returns true if KF group and GF group both are almost completely static. |
982 | | static INLINE int is_almost_static(double gf_zero_motion, int kf_zero_motion, |
983 | 0 | int is_lap_enabled) { |
984 | 0 | if (is_lap_enabled) { |
985 | | /* |
986 | | * when LAP enabled kf_zero_motion is not reliable, so use strict |
987 | | * constraint on gf_zero_motion. |
988 | | */ |
989 | 0 | return (gf_zero_motion >= 0.999); |
990 | 0 | } else { |
991 | 0 | return (gf_zero_motion >= 0.995) && |
992 | 0 | (kf_zero_motion >= STATIC_KF_GROUP_THRESH); |
993 | 0 | } |
994 | 0 | } |
995 | | |
996 | 0 | #define ARF_ABS_ZOOM_THRESH 4.4 |
997 | | static INLINE int detect_gf_cut(AV1_COMP *cpi, int frame_index, int cur_start, |
998 | | int flash_detected, int active_max_gf_interval, |
999 | | int active_min_gf_interval, |
1000 | 0 | GF_GROUP_STATS *gf_stats) { |
1001 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
1002 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
1003 | 0 | InitialDimensions *const initial_dimensions = &cpi->initial_dimensions; |
1004 | | // Motion breakout threshold for loop below depends on image size. |
1005 | 0 | const double mv_ratio_accumulator_thresh = |
1006 | 0 | (initial_dimensions->height + initial_dimensions->width) / 4.0; |
1007 | |
|
1008 | 0 | if (!flash_detected) { |
1009 | | // Break clause to detect very still sections after motion. For example, |
1010 | | // a static image after a fade or other transition. |
1011 | | |
1012 | | // TODO(angiebird): This is a temporary change, we will avoid using |
1013 | | // twopass_frame.stats_in in the follow-up CL |
1014 | 0 | int index = (int)(cpi->twopass_frame.stats_in - |
1015 | 0 | twopass->stats_buf_ctx->stats_in_start); |
1016 | 0 | if (detect_transition_to_still(&twopass->firstpass_info, index, |
1017 | 0 | rc->min_gf_interval, frame_index - cur_start, |
1018 | 0 | 5, gf_stats->loop_decay_rate, |
1019 | 0 | gf_stats->last_loop_decay_rate)) { |
1020 | 0 | return 1; |
1021 | 0 | } |
1022 | 0 | } |
1023 | | |
1024 | | // Some conditions to breakout after min interval. |
1025 | 0 | if (frame_index - cur_start >= active_min_gf_interval && |
1026 | | // If possible don't break very close to a kf |
1027 | 0 | (rc->frames_to_key - frame_index >= rc->min_gf_interval) && |
1028 | 0 | ((frame_index - cur_start) & 0x01) && !flash_detected && |
1029 | 0 | (gf_stats->mv_ratio_accumulator > mv_ratio_accumulator_thresh || |
1030 | 0 | gf_stats->abs_mv_in_out_accumulator > ARF_ABS_ZOOM_THRESH)) { |
1031 | 0 | return 1; |
1032 | 0 | } |
1033 | | |
1034 | | // If almost totally static, we will not use the the max GF length later, |
1035 | | // so we can continue for more frames. |
1036 | 0 | if (((frame_index - cur_start) >= active_max_gf_interval + 1) && |
1037 | 0 | !is_almost_static(gf_stats->zero_motion_accumulator, |
1038 | 0 | twopass->kf_zeromotion_pct, cpi->ppi->lap_enabled)) { |
1039 | 0 | return 1; |
1040 | 0 | } |
1041 | 0 | return 0; |
1042 | 0 | } |
1043 | | |
1044 | | static int is_shorter_gf_interval_better(AV1_COMP *cpi, |
1045 | 0 | EncodeFrameParams *frame_params) { |
1046 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
1047 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1048 | 0 | int gop_length_decision_method = cpi->sf.tpl_sf.gop_length_decision_method; |
1049 | 0 | int shorten_gf_interval; |
1050 | |
|
1051 | 0 | av1_tpl_preload_rc_estimate(cpi, frame_params); |
1052 | |
|
1053 | 0 | if (gop_length_decision_method == 2) { |
1054 | | // GF group length is decided based on GF boost and tpl stats of ARFs from |
1055 | | // base layer, (base+1) layer. |
1056 | 0 | shorten_gf_interval = |
1057 | 0 | (p_rc->gfu_boost < |
1058 | 0 | p_rc->num_stats_used_for_gfu_boost * GF_MIN_BOOST * 1.4) && |
1059 | 0 | !av1_tpl_setup_stats(cpi, 3, frame_params); |
1060 | 0 | } else { |
1061 | 0 | int do_complete_tpl = 1; |
1062 | 0 | GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
1063 | 0 | int is_temporal_filter_enabled = |
1064 | 0 | (rc->frames_since_key > 0 && gf_group->arf_index > -1); |
1065 | |
|
1066 | 0 | if (gop_length_decision_method == 1) { |
1067 | | // Check if tpl stats of ARFs from base layer, (base+1) layer, |
1068 | | // (base+2) layer can decide the GF group length. |
1069 | 0 | int gop_length_eval = av1_tpl_setup_stats(cpi, 2, frame_params); |
1070 | |
|
1071 | 0 | if (gop_length_eval != 2) { |
1072 | 0 | do_complete_tpl = 0; |
1073 | 0 | shorten_gf_interval = !gop_length_eval; |
1074 | 0 | } |
1075 | 0 | } |
1076 | |
|
1077 | 0 | if (do_complete_tpl) { |
1078 | | // Decide GF group length based on complete tpl stats. |
1079 | 0 | shorten_gf_interval = !av1_tpl_setup_stats(cpi, 1, frame_params); |
1080 | | // Tpl stats is reused when the ARF is temporally filtered and GF |
1081 | | // interval is not shortened. |
1082 | 0 | if (is_temporal_filter_enabled && !shorten_gf_interval) { |
1083 | 0 | cpi->skip_tpl_setup_stats = 1; |
1084 | | #if CONFIG_BITRATE_ACCURACY |
1085 | | assert(cpi->gf_frame_index == 0); |
1086 | | av1_vbr_rc_update_q_index_list(&cpi->vbr_rc_info, &cpi->ppi->tpl_data, |
1087 | | gf_group, |
1088 | | cpi->common.seq_params->bit_depth); |
1089 | | #endif // CONFIG_BITRATE_ACCURACY |
1090 | 0 | } |
1091 | 0 | } |
1092 | 0 | } |
1093 | 0 | return shorten_gf_interval; |
1094 | 0 | } |
1095 | | |
1096 | | #define MIN_SHRINK_LEN 6 // the minimum length of gf if we are shrinking |
1097 | 0 | #define SMOOTH_FILT_LEN 7 |
1098 | 0 | #define HALF_FILT_LEN (SMOOTH_FILT_LEN / 2) |
1099 | 0 | #define WINDOW_SIZE 7 |
1100 | 0 | #define HALF_WIN (WINDOW_SIZE / 2) |
1101 | | // A 7-tap gaussian smooth filter |
1102 | | const double smooth_filt[SMOOTH_FILT_LEN] = { 0.006, 0.061, 0.242, 0.383, |
1103 | | 0.242, 0.061, 0.006 }; |
1104 | | |
1105 | | // Smooth filter intra_error and coded_error in firstpass stats. |
1106 | | // If stats[i].is_flash==1, the ith element should not be used in the filtering. |
1107 | | static void smooth_filter_stats(const FIRSTPASS_STATS *stats, int start_idx, |
1108 | | int last_idx, double *filt_intra_err, |
1109 | 0 | double *filt_coded_err) { |
1110 | 0 | int i, j; |
1111 | 0 | for (i = start_idx; i <= last_idx; i++) { |
1112 | 0 | double total_wt = 0; |
1113 | 0 | for (j = -HALF_FILT_LEN; j <= HALF_FILT_LEN; j++) { |
1114 | 0 | int idx = AOMMIN(AOMMAX(i + j, start_idx), last_idx); |
1115 | 0 | if (stats[idx].is_flash) continue; |
1116 | | |
1117 | 0 | filt_intra_err[i] += |
1118 | 0 | smooth_filt[j + HALF_FILT_LEN] * stats[idx].intra_error; |
1119 | 0 | total_wt += smooth_filt[j + HALF_FILT_LEN]; |
1120 | 0 | } |
1121 | 0 | if (total_wt > 0.01) { |
1122 | 0 | filt_intra_err[i] /= total_wt; |
1123 | 0 | } else { |
1124 | 0 | filt_intra_err[i] = stats[i].intra_error; |
1125 | 0 | } |
1126 | 0 | } |
1127 | 0 | for (i = start_idx; i <= last_idx; i++) { |
1128 | 0 | double total_wt = 0; |
1129 | 0 | for (j = -HALF_FILT_LEN; j <= HALF_FILT_LEN; j++) { |
1130 | 0 | int idx = AOMMIN(AOMMAX(i + j, start_idx), last_idx); |
1131 | | // Coded error involves idx and idx - 1. |
1132 | 0 | if (stats[idx].is_flash || (idx > 0 && stats[idx - 1].is_flash)) continue; |
1133 | | |
1134 | 0 | filt_coded_err[i] += |
1135 | 0 | smooth_filt[j + HALF_FILT_LEN] * stats[idx].coded_error; |
1136 | 0 | total_wt += smooth_filt[j + HALF_FILT_LEN]; |
1137 | 0 | } |
1138 | 0 | if (total_wt > 0.01) { |
1139 | 0 | filt_coded_err[i] /= total_wt; |
1140 | 0 | } else { |
1141 | 0 | filt_coded_err[i] = stats[i].coded_error; |
1142 | 0 | } |
1143 | 0 | } |
1144 | 0 | } |
1145 | | |
1146 | | // Calculate gradient |
1147 | | static void get_gradient(const double *values, int start, int last, |
1148 | 0 | double *grad) { |
1149 | 0 | if (start == last) { |
1150 | 0 | grad[start] = 0; |
1151 | 0 | return; |
1152 | 0 | } |
1153 | 0 | for (int i = start; i <= last; i++) { |
1154 | 0 | int prev = AOMMAX(i - 1, start); |
1155 | 0 | int next = AOMMIN(i + 1, last); |
1156 | 0 | grad[i] = (values[next] - values[prev]) / (next - prev); |
1157 | 0 | } |
1158 | 0 | } |
1159 | | |
1160 | | static int find_next_scenecut(const FIRSTPASS_STATS *const stats_start, |
1161 | 0 | int first, int last) { |
1162 | | // Identify unstable areas caused by scenecuts. |
1163 | | // Find the max and 2nd max coded error, and the average of the rest frames. |
1164 | | // If there is only one frame that yields a huge coded error, it is likely a |
1165 | | // scenecut. |
1166 | 0 | double this_ratio, max_prev_ratio, max_next_ratio, max_prev_coded, |
1167 | 0 | max_next_coded; |
1168 | |
|
1169 | 0 | if (last - first == 0) return -1; |
1170 | | |
1171 | 0 | for (int i = first; i <= last; i++) { |
1172 | 0 | if (stats_start[i].is_flash || (i > 0 && stats_start[i - 1].is_flash)) |
1173 | 0 | continue; |
1174 | 0 | double temp_intra = AOMMAX(stats_start[i].intra_error, 0.01); |
1175 | 0 | this_ratio = stats_start[i].coded_error / temp_intra; |
1176 | | // find the avg ratio in the preceding neighborhood |
1177 | 0 | max_prev_ratio = 0; |
1178 | 0 | max_prev_coded = 0; |
1179 | 0 | for (int j = AOMMAX(first, i - HALF_WIN); j < i; j++) { |
1180 | 0 | if (stats_start[j].is_flash || (j > 0 && stats_start[j - 1].is_flash)) |
1181 | 0 | continue; |
1182 | 0 | temp_intra = AOMMAX(stats_start[j].intra_error, 0.01); |
1183 | 0 | double temp_ratio = stats_start[j].coded_error / temp_intra; |
1184 | 0 | if (temp_ratio > max_prev_ratio) { |
1185 | 0 | max_prev_ratio = temp_ratio; |
1186 | 0 | } |
1187 | 0 | if (stats_start[j].coded_error > max_prev_coded) { |
1188 | 0 | max_prev_coded = stats_start[j].coded_error; |
1189 | 0 | } |
1190 | 0 | } |
1191 | | // find the avg ratio in the following neighborhood |
1192 | 0 | max_next_ratio = 0; |
1193 | 0 | max_next_coded = 0; |
1194 | 0 | for (int j = i + 1; j <= AOMMIN(i + HALF_WIN, last); j++) { |
1195 | 0 | if (stats_start[i].is_flash || (i > 0 && stats_start[i - 1].is_flash)) |
1196 | 0 | continue; |
1197 | 0 | temp_intra = AOMMAX(stats_start[j].intra_error, 0.01); |
1198 | 0 | double temp_ratio = stats_start[j].coded_error / temp_intra; |
1199 | 0 | if (temp_ratio > max_next_ratio) { |
1200 | 0 | max_next_ratio = temp_ratio; |
1201 | 0 | } |
1202 | 0 | if (stats_start[j].coded_error > max_next_coded) { |
1203 | 0 | max_next_coded = stats_start[j].coded_error; |
1204 | 0 | } |
1205 | 0 | } |
1206 | |
|
1207 | 0 | if (max_prev_ratio < 0.001 && max_next_ratio < 0.001) { |
1208 | | // the ratios are very small, only check a small fixed threshold |
1209 | 0 | if (this_ratio < 0.02) continue; |
1210 | 0 | } else { |
1211 | | // check if this frame has a larger ratio than the neighborhood |
1212 | 0 | double max_sr = stats_start[i].sr_coded_error; |
1213 | 0 | if (i < last) max_sr = AOMMAX(max_sr, stats_start[i + 1].sr_coded_error); |
1214 | 0 | double max_sr_fr_ratio = |
1215 | 0 | max_sr / AOMMAX(stats_start[i].coded_error, 0.01); |
1216 | |
|
1217 | 0 | if (max_sr_fr_ratio > 1.2) continue; |
1218 | 0 | if (this_ratio < 2 * AOMMAX(max_prev_ratio, max_next_ratio) && |
1219 | 0 | stats_start[i].coded_error < |
1220 | 0 | 2 * AOMMAX(max_prev_coded, max_next_coded)) { |
1221 | 0 | continue; |
1222 | 0 | } |
1223 | 0 | } |
1224 | 0 | return i; |
1225 | 0 | } |
1226 | 0 | return -1; |
1227 | 0 | } |
1228 | | |
1229 | | // Remove the region with index next_region. |
1230 | | // parameter merge: 0: merge with previous; 1: merge with next; 2: |
1231 | | // merge with both, take type from previous if possible |
1232 | | // After removing, next_region will be the index of the next region. |
1233 | | static void remove_region(int merge, REGIONS *regions, int *num_regions, |
1234 | 0 | int *next_region) { |
1235 | 0 | int k = *next_region; |
1236 | 0 | assert(k < *num_regions); |
1237 | 0 | if (*num_regions == 1) { |
1238 | 0 | *num_regions = 0; |
1239 | 0 | return; |
1240 | 0 | } |
1241 | 0 | if (k == 0) { |
1242 | 0 | merge = 1; |
1243 | 0 | } else if (k == *num_regions - 1) { |
1244 | 0 | merge = 0; |
1245 | 0 | } |
1246 | 0 | int num_merge = (merge == 2) ? 2 : 1; |
1247 | 0 | switch (merge) { |
1248 | 0 | case 0: |
1249 | 0 | regions[k - 1].last = regions[k].last; |
1250 | 0 | *next_region = k; |
1251 | 0 | break; |
1252 | 0 | case 1: |
1253 | 0 | regions[k + 1].start = regions[k].start; |
1254 | 0 | *next_region = k + 1; |
1255 | 0 | break; |
1256 | 0 | case 2: |
1257 | 0 | regions[k - 1].last = regions[k + 1].last; |
1258 | 0 | *next_region = k; |
1259 | 0 | break; |
1260 | 0 | default: assert(0); |
1261 | 0 | } |
1262 | 0 | *num_regions -= num_merge; |
1263 | 0 | for (k = *next_region - (merge == 1); k < *num_regions; k++) { |
1264 | 0 | regions[k] = regions[k + num_merge]; |
1265 | 0 | } |
1266 | 0 | } |
1267 | | |
1268 | | // Insert a region in the cur_region_idx. The start and last should both be in |
1269 | | // the current region. After insertion, the cur_region_idx will point to the |
1270 | | // last region that was splitted from the original region. |
1271 | | static void insert_region(int start, int last, REGION_TYPES type, |
1272 | | REGIONS *regions, int *num_regions, |
1273 | 0 | int *cur_region_idx) { |
1274 | 0 | int k = *cur_region_idx; |
1275 | 0 | REGION_TYPES this_region_type = regions[k].type; |
1276 | 0 | int this_region_last = regions[k].last; |
1277 | 0 | int num_add = (start != regions[k].start) + (last != regions[k].last); |
1278 | | // move the following regions further to the back |
1279 | 0 | for (int r = *num_regions - 1; r > k; r--) { |
1280 | 0 | regions[r + num_add] = regions[r]; |
1281 | 0 | } |
1282 | 0 | *num_regions += num_add; |
1283 | 0 | if (start > regions[k].start) { |
1284 | 0 | regions[k].last = start - 1; |
1285 | 0 | k++; |
1286 | 0 | regions[k].start = start; |
1287 | 0 | } |
1288 | 0 | regions[k].type = type; |
1289 | 0 | if (last < this_region_last) { |
1290 | 0 | regions[k].last = last; |
1291 | 0 | k++; |
1292 | 0 | regions[k].start = last + 1; |
1293 | 0 | regions[k].last = this_region_last; |
1294 | 0 | regions[k].type = this_region_type; |
1295 | 0 | } else { |
1296 | 0 | regions[k].last = this_region_last; |
1297 | 0 | } |
1298 | 0 | *cur_region_idx = k; |
1299 | 0 | } |
1300 | | |
1301 | | // Get the average of stats inside a region. |
1302 | | static void analyze_region(const FIRSTPASS_STATS *stats, int k, |
1303 | 0 | REGIONS *regions) { |
1304 | 0 | int i; |
1305 | 0 | regions[k].avg_cor_coeff = 0; |
1306 | 0 | regions[k].avg_sr_fr_ratio = 0; |
1307 | 0 | regions[k].avg_intra_err = 0; |
1308 | 0 | regions[k].avg_coded_err = 0; |
1309 | |
|
1310 | 0 | int check_first_sr = (k != 0); |
1311 | |
|
1312 | 0 | for (i = regions[k].start; i <= regions[k].last; i++) { |
1313 | 0 | if (i > regions[k].start || check_first_sr) { |
1314 | 0 | double num_frames = |
1315 | 0 | (double)(regions[k].last - regions[k].start + check_first_sr); |
1316 | 0 | double max_coded_error = |
1317 | 0 | AOMMAX(stats[i].coded_error, stats[i - 1].coded_error); |
1318 | 0 | double this_ratio = |
1319 | 0 | stats[i].sr_coded_error / AOMMAX(max_coded_error, 0.001); |
1320 | 0 | regions[k].avg_sr_fr_ratio += this_ratio / num_frames; |
1321 | 0 | } |
1322 | |
|
1323 | 0 | regions[k].avg_intra_err += |
1324 | 0 | stats[i].intra_error / (double)(regions[k].last - regions[k].start + 1); |
1325 | 0 | regions[k].avg_coded_err += |
1326 | 0 | stats[i].coded_error / (double)(regions[k].last - regions[k].start + 1); |
1327 | |
|
1328 | 0 | regions[k].avg_cor_coeff += |
1329 | 0 | AOMMAX(stats[i].cor_coeff, 0.001) / |
1330 | 0 | (double)(regions[k].last - regions[k].start + 1); |
1331 | 0 | regions[k].avg_noise_var += |
1332 | 0 | AOMMAX(stats[i].noise_var, 0.001) / |
1333 | 0 | (double)(regions[k].last - regions[k].start + 1); |
1334 | 0 | } |
1335 | 0 | } |
1336 | | |
1337 | | // Calculate the regions stats of every region. |
1338 | | static void get_region_stats(const FIRSTPASS_STATS *stats, REGIONS *regions, |
1339 | 0 | int num_regions) { |
1340 | 0 | for (int k = 0; k < num_regions; k++) { |
1341 | 0 | analyze_region(stats, k, regions); |
1342 | 0 | } |
1343 | 0 | } |
1344 | | |
1345 | | // Find tentative stable regions |
1346 | | static int find_stable_regions(const FIRSTPASS_STATS *stats, |
1347 | | const double *grad_coded, int this_start, |
1348 | 0 | int this_last, REGIONS *regions) { |
1349 | 0 | int i, j, k = 0; |
1350 | 0 | regions[k].start = this_start; |
1351 | 0 | for (i = this_start; i <= this_last; i++) { |
1352 | | // Check mean and variance of stats in a window |
1353 | 0 | double mean_intra = 0.001, var_intra = 0.001; |
1354 | 0 | double mean_coded = 0.001, var_coded = 0.001; |
1355 | 0 | int count = 0; |
1356 | 0 | for (j = -HALF_WIN; j <= HALF_WIN; j++) { |
1357 | 0 | int idx = AOMMIN(AOMMAX(i + j, this_start), this_last); |
1358 | 0 | if (stats[idx].is_flash || (idx > 0 && stats[idx - 1].is_flash)) continue; |
1359 | 0 | mean_intra += stats[idx].intra_error; |
1360 | 0 | var_intra += stats[idx].intra_error * stats[idx].intra_error; |
1361 | 0 | mean_coded += stats[idx].coded_error; |
1362 | 0 | var_coded += stats[idx].coded_error * stats[idx].coded_error; |
1363 | 0 | count++; |
1364 | 0 | } |
1365 | |
|
1366 | 0 | REGION_TYPES cur_type; |
1367 | 0 | if (count > 0) { |
1368 | 0 | mean_intra /= (double)count; |
1369 | 0 | var_intra /= (double)count; |
1370 | 0 | mean_coded /= (double)count; |
1371 | 0 | var_coded /= (double)count; |
1372 | 0 | int is_intra_stable = (var_intra / (mean_intra * mean_intra) < 1.03); |
1373 | 0 | int is_coded_stable = (var_coded / (mean_coded * mean_coded) < 1.04 && |
1374 | 0 | fabs(grad_coded[i]) / mean_coded < 0.05) || |
1375 | 0 | mean_coded / mean_intra < 0.05; |
1376 | 0 | int is_coded_small = mean_coded < 0.5 * mean_intra; |
1377 | 0 | cur_type = (is_intra_stable && is_coded_stable && is_coded_small) |
1378 | 0 | ? STABLE_REGION |
1379 | 0 | : HIGH_VAR_REGION; |
1380 | 0 | } else { |
1381 | 0 | cur_type = HIGH_VAR_REGION; |
1382 | 0 | } |
1383 | | |
1384 | | // mark a new region if type changes |
1385 | 0 | if (i == regions[k].start) { |
1386 | | // first frame in the region |
1387 | 0 | regions[k].type = cur_type; |
1388 | 0 | } else if (cur_type != regions[k].type) { |
1389 | | // Append a new region |
1390 | 0 | regions[k].last = i - 1; |
1391 | 0 | regions[k + 1].start = i; |
1392 | 0 | regions[k + 1].type = cur_type; |
1393 | 0 | k++; |
1394 | 0 | } |
1395 | 0 | } |
1396 | 0 | regions[k].last = this_last; |
1397 | 0 | return k + 1; |
1398 | 0 | } |
1399 | | |
1400 | | // Clean up regions that should be removed or merged. |
1401 | 0 | static void cleanup_regions(REGIONS *regions, int *num_regions) { |
1402 | 0 | int k = 0; |
1403 | 0 | while (k < *num_regions) { |
1404 | 0 | if ((k > 0 && regions[k - 1].type == regions[k].type && |
1405 | 0 | regions[k].type != SCENECUT_REGION) || |
1406 | 0 | regions[k].last < regions[k].start) { |
1407 | 0 | remove_region(0, regions, num_regions, &k); |
1408 | 0 | } else { |
1409 | 0 | k++; |
1410 | 0 | } |
1411 | 0 | } |
1412 | 0 | } |
1413 | | |
1414 | | // Remove regions that are of type and shorter than length. |
1415 | | // Merge it with its neighboring regions. |
1416 | | static void remove_short_regions(REGIONS *regions, int *num_regions, |
1417 | 0 | REGION_TYPES type, int length) { |
1418 | 0 | int k = 0; |
1419 | 0 | while (k < *num_regions && (*num_regions) > 1) { |
1420 | 0 | if ((regions[k].last - regions[k].start + 1 < length && |
1421 | 0 | regions[k].type == type)) { |
1422 | | // merge current region with the previous and next regions |
1423 | 0 | remove_region(2, regions, num_regions, &k); |
1424 | 0 | } else { |
1425 | 0 | k++; |
1426 | 0 | } |
1427 | 0 | } |
1428 | 0 | cleanup_regions(regions, num_regions); |
1429 | 0 | } |
1430 | | |
1431 | | static void adjust_unstable_region_bounds(const FIRSTPASS_STATS *stats, |
1432 | 0 | REGIONS *regions, int *num_regions) { |
1433 | 0 | int i, j, k; |
1434 | | // Remove regions that are too short. Likely noise. |
1435 | 0 | remove_short_regions(regions, num_regions, STABLE_REGION, HALF_WIN); |
1436 | 0 | remove_short_regions(regions, num_regions, HIGH_VAR_REGION, HALF_WIN); |
1437 | |
|
1438 | 0 | get_region_stats(stats, regions, *num_regions); |
1439 | | |
1440 | | // Adjust region boundaries. The thresholds are empirically obtained, but |
1441 | | // overall the performance is not very sensitive to small changes to them. |
1442 | 0 | for (k = 0; k < *num_regions; k++) { |
1443 | 0 | if (regions[k].type == STABLE_REGION) continue; |
1444 | 0 | if (k > 0) { |
1445 | | // Adjust previous boundary. |
1446 | | // First find the average intra/coded error in the previous |
1447 | | // neighborhood. |
1448 | 0 | double avg_intra_err = 0; |
1449 | 0 | const int starti = AOMMAX(regions[k - 1].last - WINDOW_SIZE + 1, |
1450 | 0 | regions[k - 1].start + 1); |
1451 | 0 | const int lasti = regions[k - 1].last; |
1452 | 0 | int counti = 0; |
1453 | 0 | for (i = starti; i <= lasti; i++) { |
1454 | 0 | avg_intra_err += stats[i].intra_error; |
1455 | 0 | counti++; |
1456 | 0 | } |
1457 | 0 | if (counti > 0) { |
1458 | 0 | avg_intra_err = AOMMAX(avg_intra_err / (double)counti, 0.001); |
1459 | 0 | int count_coded = 0, count_grad = 0; |
1460 | 0 | for (j = lasti + 1; j <= regions[k].last; j++) { |
1461 | 0 | const int intra_close = |
1462 | 0 | fabs(stats[j].intra_error - avg_intra_err) / avg_intra_err < 0.1; |
1463 | 0 | const int coded_small = stats[j].coded_error / avg_intra_err < 0.1; |
1464 | 0 | const int coeff_close = stats[j].cor_coeff > 0.995; |
1465 | 0 | if (!coeff_close || !coded_small) count_coded--; |
1466 | 0 | if (intra_close && count_coded >= 0 && count_grad >= 0) { |
1467 | | // this frame probably belongs to the previous stable region |
1468 | 0 | regions[k - 1].last = j; |
1469 | 0 | regions[k].start = j + 1; |
1470 | 0 | } else { |
1471 | 0 | break; |
1472 | 0 | } |
1473 | 0 | } |
1474 | 0 | } |
1475 | 0 | } // if k > 0 |
1476 | 0 | if (k < *num_regions - 1) { |
1477 | | // Adjust next boundary. |
1478 | | // First find the average intra/coded error in the next neighborhood. |
1479 | 0 | double avg_intra_err = 0; |
1480 | 0 | const int starti = regions[k + 1].start; |
1481 | 0 | const int lasti = AOMMIN(regions[k + 1].last - 1, |
1482 | 0 | regions[k + 1].start + WINDOW_SIZE - 1); |
1483 | 0 | int counti = 0; |
1484 | 0 | for (i = starti; i <= lasti; i++) { |
1485 | 0 | avg_intra_err += stats[i].intra_error; |
1486 | 0 | counti++; |
1487 | 0 | } |
1488 | 0 | if (counti > 0) { |
1489 | 0 | avg_intra_err = AOMMAX(avg_intra_err / (double)counti, 0.001); |
1490 | | // At the boundary, coded error is large, but still the frame is stable |
1491 | 0 | int count_coded = 1, count_grad = 1; |
1492 | 0 | for (j = starti - 1; j >= regions[k].start; j--) { |
1493 | 0 | const int intra_close = |
1494 | 0 | fabs(stats[j].intra_error - avg_intra_err) / avg_intra_err < 0.1; |
1495 | 0 | const int coded_small = |
1496 | 0 | stats[j + 1].coded_error / avg_intra_err < 0.1; |
1497 | 0 | const int coeff_close = stats[j].cor_coeff > 0.995; |
1498 | 0 | if (!coeff_close || !coded_small) count_coded--; |
1499 | 0 | if (intra_close && count_coded >= 0 && count_grad >= 0) { |
1500 | | // this frame probably belongs to the next stable region |
1501 | 0 | regions[k + 1].start = j; |
1502 | 0 | regions[k].last = j - 1; |
1503 | 0 | } else { |
1504 | 0 | break; |
1505 | 0 | } |
1506 | 0 | } |
1507 | 0 | } |
1508 | 0 | } // if k < *num_regions - 1 |
1509 | 0 | } // end of loop over all regions |
1510 | |
|
1511 | 0 | cleanup_regions(regions, num_regions); |
1512 | 0 | remove_short_regions(regions, num_regions, HIGH_VAR_REGION, HALF_WIN); |
1513 | 0 | get_region_stats(stats, regions, *num_regions); |
1514 | | |
1515 | | // If a stable regions has higher error than neighboring high var regions, |
1516 | | // or if the stable region has a lower average correlation, |
1517 | | // then it should be merged with them |
1518 | 0 | k = 0; |
1519 | 0 | while (k < *num_regions && (*num_regions) > 1) { |
1520 | 0 | if (regions[k].type == STABLE_REGION && |
1521 | 0 | (regions[k].last - regions[k].start + 1) < 2 * WINDOW_SIZE && |
1522 | 0 | ((k > 0 && // previous regions |
1523 | 0 | (regions[k].avg_coded_err > regions[k - 1].avg_coded_err * 1.01 || |
1524 | 0 | regions[k].avg_cor_coeff < regions[k - 1].avg_cor_coeff * 0.999)) && |
1525 | 0 | (k < *num_regions - 1 && // next region |
1526 | 0 | (regions[k].avg_coded_err > regions[k + 1].avg_coded_err * 1.01 || |
1527 | 0 | regions[k].avg_cor_coeff < regions[k + 1].avg_cor_coeff * 0.999)))) { |
1528 | | // merge current region with the previous and next regions |
1529 | 0 | remove_region(2, regions, num_regions, &k); |
1530 | 0 | analyze_region(stats, k - 1, regions); |
1531 | 0 | } else if (regions[k].type == HIGH_VAR_REGION && |
1532 | 0 | (regions[k].last - regions[k].start + 1) < 2 * WINDOW_SIZE && |
1533 | 0 | ((k > 0 && // previous regions |
1534 | 0 | (regions[k].avg_coded_err < |
1535 | 0 | regions[k - 1].avg_coded_err * 0.99 || |
1536 | 0 | regions[k].avg_cor_coeff > |
1537 | 0 | regions[k - 1].avg_cor_coeff * 1.001)) && |
1538 | 0 | (k < *num_regions - 1 && // next region |
1539 | 0 | (regions[k].avg_coded_err < |
1540 | 0 | regions[k + 1].avg_coded_err * 0.99 || |
1541 | 0 | regions[k].avg_cor_coeff > |
1542 | 0 | regions[k + 1].avg_cor_coeff * 1.001)))) { |
1543 | | // merge current region with the previous and next regions |
1544 | 0 | remove_region(2, regions, num_regions, &k); |
1545 | 0 | analyze_region(stats, k - 1, regions); |
1546 | 0 | } else { |
1547 | 0 | k++; |
1548 | 0 | } |
1549 | 0 | } |
1550 | |
|
1551 | 0 | remove_short_regions(regions, num_regions, STABLE_REGION, WINDOW_SIZE); |
1552 | 0 | remove_short_regions(regions, num_regions, HIGH_VAR_REGION, HALF_WIN); |
1553 | 0 | } |
1554 | | |
1555 | | // Identify blending regions. |
1556 | | static void find_blending_regions(const FIRSTPASS_STATS *stats, |
1557 | 0 | REGIONS *regions, int *num_regions) { |
1558 | 0 | int i, k = 0; |
1559 | | // Blending regions will have large content change, therefore will have a |
1560 | | // large consistent change in intra error. |
1561 | 0 | int count_stable = 0; |
1562 | 0 | while (k < *num_regions) { |
1563 | 0 | if (regions[k].type == STABLE_REGION) { |
1564 | 0 | k++; |
1565 | 0 | count_stable++; |
1566 | 0 | continue; |
1567 | 0 | } |
1568 | 0 | int dir = 0; |
1569 | 0 | int start = 0, last; |
1570 | 0 | for (i = regions[k].start; i <= regions[k].last; i++) { |
1571 | | // First mark the regions that has consistent large change of intra error. |
1572 | 0 | if (k == 0 && i == regions[k].start) continue; |
1573 | 0 | if (stats[i].is_flash || (i > 0 && stats[i - 1].is_flash)) continue; |
1574 | 0 | double grad = stats[i].intra_error - stats[i - 1].intra_error; |
1575 | 0 | int large_change = fabs(grad) / AOMMAX(stats[i].intra_error, 0.01) > 0.05; |
1576 | 0 | int this_dir = 0; |
1577 | 0 | if (large_change) { |
1578 | 0 | this_dir = (grad > 0) ? 1 : -1; |
1579 | 0 | } |
1580 | | // the current trend continues |
1581 | 0 | if (dir == this_dir) continue; |
1582 | 0 | if (dir != 0) { |
1583 | | // Mark the end of a new large change group and add it |
1584 | 0 | last = i - 1; |
1585 | 0 | insert_region(start, last, BLENDING_REGION, regions, num_regions, &k); |
1586 | 0 | } |
1587 | 0 | dir = this_dir; |
1588 | 0 | if (k == 0 && i == regions[k].start + 1) { |
1589 | 0 | start = i - 1; |
1590 | 0 | } else { |
1591 | 0 | start = i; |
1592 | 0 | } |
1593 | 0 | } |
1594 | 0 | if (dir != 0) { |
1595 | 0 | last = regions[k].last; |
1596 | 0 | insert_region(start, last, BLENDING_REGION, regions, num_regions, &k); |
1597 | 0 | } |
1598 | 0 | k++; |
1599 | 0 | } |
1600 | | |
1601 | | // If the blending region has very low correlation, mark it as high variance |
1602 | | // since we probably cannot benefit from it anyways. |
1603 | 0 | get_region_stats(stats, regions, *num_regions); |
1604 | 0 | for (k = 0; k < *num_regions; k++) { |
1605 | 0 | if (regions[k].type != BLENDING_REGION) continue; |
1606 | 0 | if (regions[k].last == regions[k].start || regions[k].avg_cor_coeff < 0.6 || |
1607 | 0 | count_stable == 0) |
1608 | 0 | regions[k].type = HIGH_VAR_REGION; |
1609 | 0 | } |
1610 | 0 | get_region_stats(stats, regions, *num_regions); |
1611 | | |
1612 | | // It is possible for blending to result in a "dip" in intra error (first |
1613 | | // decrease then increase). Therefore we need to find the dip and combine the |
1614 | | // two regions. |
1615 | 0 | k = 1; |
1616 | 0 | while (k < *num_regions) { |
1617 | 0 | if (k < *num_regions - 1 && regions[k].type == HIGH_VAR_REGION) { |
1618 | | // Check if this short high variance regions is actually in the middle of |
1619 | | // a blending region. |
1620 | 0 | if (regions[k - 1].type == BLENDING_REGION && |
1621 | 0 | regions[k + 1].type == BLENDING_REGION && |
1622 | 0 | regions[k].last - regions[k].start < 3) { |
1623 | 0 | int prev_dir = (stats[regions[k - 1].last].intra_error - |
1624 | 0 | stats[regions[k - 1].last - 1].intra_error) > 0 |
1625 | 0 | ? 1 |
1626 | 0 | : -1; |
1627 | 0 | int next_dir = (stats[regions[k + 1].last].intra_error - |
1628 | 0 | stats[regions[k + 1].last - 1].intra_error) > 0 |
1629 | 0 | ? 1 |
1630 | 0 | : -1; |
1631 | 0 | if (prev_dir < 0 && next_dir > 0) { |
1632 | | // This is possibly a mid region of blending. Check the ratios |
1633 | 0 | double ratio_thres = AOMMIN(regions[k - 1].avg_sr_fr_ratio, |
1634 | 0 | regions[k + 1].avg_sr_fr_ratio) * |
1635 | 0 | 0.95; |
1636 | 0 | if (regions[k].avg_sr_fr_ratio > ratio_thres) { |
1637 | 0 | regions[k].type = BLENDING_REGION; |
1638 | 0 | remove_region(2, regions, num_regions, &k); |
1639 | 0 | analyze_region(stats, k - 1, regions); |
1640 | 0 | continue; |
1641 | 0 | } |
1642 | 0 | } |
1643 | 0 | } |
1644 | 0 | } |
1645 | | // Check if we have a pair of consecutive blending regions. |
1646 | 0 | if (regions[k - 1].type == BLENDING_REGION && |
1647 | 0 | regions[k].type == BLENDING_REGION) { |
1648 | 0 | int prev_dir = (stats[regions[k - 1].last].intra_error - |
1649 | 0 | stats[regions[k - 1].last - 1].intra_error) > 0 |
1650 | 0 | ? 1 |
1651 | 0 | : -1; |
1652 | 0 | int next_dir = (stats[regions[k].last].intra_error - |
1653 | 0 | stats[regions[k].last - 1].intra_error) > 0 |
1654 | 0 | ? 1 |
1655 | 0 | : -1; |
1656 | | |
1657 | | // if both are too short, no need to check |
1658 | 0 | int total_length = regions[k].last - regions[k - 1].start + 1; |
1659 | 0 | if (total_length < 4) { |
1660 | 0 | regions[k - 1].type = HIGH_VAR_REGION; |
1661 | 0 | k++; |
1662 | 0 | continue; |
1663 | 0 | } |
1664 | | |
1665 | 0 | int to_merge = 0; |
1666 | 0 | if (prev_dir < 0 && next_dir > 0) { |
1667 | | // In this case we check the last frame in the previous region. |
1668 | 0 | double prev_length = |
1669 | 0 | (double)(regions[k - 1].last - regions[k - 1].start + 1); |
1670 | 0 | double last_ratio, ratio_thres; |
1671 | 0 | if (prev_length < 2.01) { |
1672 | | // if the previous region is very short |
1673 | 0 | double max_coded_error = |
1674 | 0 | AOMMAX(stats[regions[k - 1].last].coded_error, |
1675 | 0 | stats[regions[k - 1].last - 1].coded_error); |
1676 | 0 | last_ratio = stats[regions[k - 1].last].sr_coded_error / |
1677 | 0 | AOMMAX(max_coded_error, 0.001); |
1678 | 0 | ratio_thres = regions[k].avg_sr_fr_ratio * 0.95; |
1679 | 0 | } else { |
1680 | 0 | double max_coded_error = |
1681 | 0 | AOMMAX(stats[regions[k - 1].last].coded_error, |
1682 | 0 | stats[regions[k - 1].last - 1].coded_error); |
1683 | 0 | last_ratio = stats[regions[k - 1].last].sr_coded_error / |
1684 | 0 | AOMMAX(max_coded_error, 0.001); |
1685 | 0 | double prev_ratio = |
1686 | 0 | (regions[k - 1].avg_sr_fr_ratio * prev_length - last_ratio) / |
1687 | 0 | (prev_length - 1.0); |
1688 | 0 | ratio_thres = AOMMIN(prev_ratio, regions[k].avg_sr_fr_ratio) * 0.95; |
1689 | 0 | } |
1690 | 0 | if (last_ratio > ratio_thres) { |
1691 | 0 | to_merge = 1; |
1692 | 0 | } |
1693 | 0 | } |
1694 | |
|
1695 | 0 | if (to_merge) { |
1696 | 0 | remove_region(0, regions, num_regions, &k); |
1697 | 0 | analyze_region(stats, k - 1, regions); |
1698 | 0 | continue; |
1699 | 0 | } else { |
1700 | | // These are possibly two separate blending regions. Mark the boundary |
1701 | | // frame as HIGH_VAR_REGION to separate the two. |
1702 | 0 | int prev_k = k - 1; |
1703 | 0 | insert_region(regions[prev_k].last, regions[prev_k].last, |
1704 | 0 | HIGH_VAR_REGION, regions, num_regions, &prev_k); |
1705 | 0 | analyze_region(stats, prev_k, regions); |
1706 | 0 | k = prev_k + 1; |
1707 | 0 | analyze_region(stats, k, regions); |
1708 | 0 | } |
1709 | 0 | } |
1710 | 0 | k++; |
1711 | 0 | } |
1712 | 0 | cleanup_regions(regions, num_regions); |
1713 | 0 | } |
1714 | | |
1715 | | // Clean up decision for blendings. Remove blending regions that are too short. |
1716 | | // Also if a very short high var region is between a blending and a stable |
1717 | | // region, just merge it with one of them. |
1718 | 0 | static void cleanup_blendings(REGIONS *regions, int *num_regions) { |
1719 | 0 | int k = 0; |
1720 | 0 | while (k<*num_regions && * num_regions> 1) { |
1721 | 0 | int is_short_blending = regions[k].type == BLENDING_REGION && |
1722 | 0 | regions[k].last - regions[k].start + 1 < 5; |
1723 | 0 | int is_short_hv = regions[k].type == HIGH_VAR_REGION && |
1724 | 0 | regions[k].last - regions[k].start + 1 < 5; |
1725 | 0 | int has_stable_neighbor = |
1726 | 0 | ((k > 0 && regions[k - 1].type == STABLE_REGION) || |
1727 | 0 | (k < *num_regions - 1 && regions[k + 1].type == STABLE_REGION)); |
1728 | 0 | int has_blend_neighbor = |
1729 | 0 | ((k > 0 && regions[k - 1].type == BLENDING_REGION) || |
1730 | 0 | (k < *num_regions - 1 && regions[k + 1].type == BLENDING_REGION)); |
1731 | 0 | int total_neighbors = (k > 0) + (k < *num_regions - 1); |
1732 | |
|
1733 | 0 | if (is_short_blending || |
1734 | 0 | (is_short_hv && |
1735 | 0 | has_stable_neighbor + has_blend_neighbor >= total_neighbors)) { |
1736 | | // Remove this region.Try to determine whether to combine it with the |
1737 | | // previous or next region. |
1738 | 0 | int merge; |
1739 | 0 | double prev_diff = |
1740 | 0 | (k > 0) |
1741 | 0 | ? fabs(regions[k].avg_cor_coeff - regions[k - 1].avg_cor_coeff) |
1742 | 0 | : 1; |
1743 | 0 | double next_diff = |
1744 | 0 | (k < *num_regions - 1) |
1745 | 0 | ? fabs(regions[k].avg_cor_coeff - regions[k + 1].avg_cor_coeff) |
1746 | 0 | : 1; |
1747 | | // merge == 0 means to merge with previous, 1 means to merge with next |
1748 | 0 | merge = prev_diff > next_diff; |
1749 | 0 | remove_region(merge, regions, num_regions, &k); |
1750 | 0 | } else { |
1751 | 0 | k++; |
1752 | 0 | } |
1753 | 0 | } |
1754 | 0 | cleanup_regions(regions, num_regions); |
1755 | 0 | } |
1756 | | |
1757 | | // Identify stable and unstable regions from first pass stats. |
1758 | | // Stats_start points to the first frame to analyze. |
1759 | | // Offset is the offset from the current frame to the frame stats_start is |
1760 | | // pointing to. |
1761 | | static void identify_regions(const FIRSTPASS_STATS *const stats_start, |
1762 | | int total_frames, int offset, REGIONS *regions, |
1763 | 1.26k | int *total_regions) { |
1764 | 1.26k | int k; |
1765 | 1.26k | if (total_frames <= 1) return; |
1766 | | |
1767 | | // store the initial decisions |
1768 | 0 | REGIONS temp_regions[MAX_FIRSTPASS_ANALYSIS_FRAMES]; |
1769 | 0 | av1_zero_array(temp_regions, MAX_FIRSTPASS_ANALYSIS_FRAMES); |
1770 | | // buffers for filtered stats |
1771 | 0 | double filt_intra_err[MAX_FIRSTPASS_ANALYSIS_FRAMES] = { 0 }; |
1772 | 0 | double filt_coded_err[MAX_FIRSTPASS_ANALYSIS_FRAMES] = { 0 }; |
1773 | 0 | double grad_coded[MAX_FIRSTPASS_ANALYSIS_FRAMES] = { 0 }; |
1774 | |
|
1775 | 0 | int cur_region = 0, this_start = 0, this_last; |
1776 | |
|
1777 | 0 | int next_scenecut = -1; |
1778 | 0 | do { |
1779 | | // first get the obvious scenecuts |
1780 | 0 | next_scenecut = |
1781 | 0 | find_next_scenecut(stats_start, this_start, total_frames - 1); |
1782 | 0 | this_last = (next_scenecut >= 0) ? (next_scenecut - 1) : total_frames - 1; |
1783 | | |
1784 | | // low-pass filter the needed stats |
1785 | 0 | smooth_filter_stats(stats_start, this_start, this_last, filt_intra_err, |
1786 | 0 | filt_coded_err); |
1787 | 0 | get_gradient(filt_coded_err, this_start, this_last, grad_coded); |
1788 | | |
1789 | | // find tentative stable regions and unstable regions |
1790 | 0 | int num_regions = find_stable_regions(stats_start, grad_coded, this_start, |
1791 | 0 | this_last, temp_regions); |
1792 | |
|
1793 | 0 | adjust_unstable_region_bounds(stats_start, temp_regions, &num_regions); |
1794 | |
|
1795 | 0 | get_region_stats(stats_start, temp_regions, num_regions); |
1796 | | |
1797 | | // Try to identify blending regions in the unstable regions |
1798 | 0 | find_blending_regions(stats_start, temp_regions, &num_regions); |
1799 | 0 | cleanup_blendings(temp_regions, &num_regions); |
1800 | | |
1801 | | // The flash points should all be considered high variance points |
1802 | 0 | k = 0; |
1803 | 0 | while (k < num_regions) { |
1804 | 0 | if (temp_regions[k].type != STABLE_REGION) { |
1805 | 0 | k++; |
1806 | 0 | continue; |
1807 | 0 | } |
1808 | 0 | int start = temp_regions[k].start; |
1809 | 0 | int last = temp_regions[k].last; |
1810 | 0 | for (int i = start; i <= last; i++) { |
1811 | 0 | if (stats_start[i].is_flash) { |
1812 | 0 | insert_region(i, i, HIGH_VAR_REGION, temp_regions, &num_regions, &k); |
1813 | 0 | } |
1814 | 0 | } |
1815 | 0 | k++; |
1816 | 0 | } |
1817 | 0 | cleanup_regions(temp_regions, &num_regions); |
1818 | | |
1819 | | // copy the regions in the scenecut group |
1820 | 0 | for (k = 0; k < num_regions; k++) { |
1821 | 0 | if (temp_regions[k].last < temp_regions[k].start && |
1822 | 0 | k == num_regions - 1) { |
1823 | 0 | num_regions--; |
1824 | 0 | break; |
1825 | 0 | } |
1826 | 0 | regions[k + cur_region] = temp_regions[k]; |
1827 | 0 | } |
1828 | 0 | cur_region += num_regions; |
1829 | | |
1830 | | // add the scenecut region |
1831 | 0 | if (next_scenecut > -1) { |
1832 | | // add the scenecut region, and find the next scenecut |
1833 | 0 | regions[cur_region].type = SCENECUT_REGION; |
1834 | 0 | regions[cur_region].start = next_scenecut; |
1835 | 0 | regions[cur_region].last = next_scenecut; |
1836 | 0 | cur_region++; |
1837 | 0 | this_start = next_scenecut + 1; |
1838 | 0 | } |
1839 | 0 | } while (next_scenecut >= 0); |
1840 | |
|
1841 | 0 | *total_regions = cur_region; |
1842 | 0 | get_region_stats(stats_start, regions, *total_regions); |
1843 | |
|
1844 | 0 | for (k = 0; k < *total_regions; k++) { |
1845 | | // If scenecuts are very minor, mark them as high variance. |
1846 | 0 | if (regions[k].type != SCENECUT_REGION || |
1847 | 0 | regions[k].avg_cor_coeff * |
1848 | 0 | (1 - stats_start[regions[k].start].noise_var / |
1849 | 0 | regions[k].avg_intra_err) < |
1850 | 0 | 0.8) { |
1851 | 0 | continue; |
1852 | 0 | } |
1853 | 0 | regions[k].type = HIGH_VAR_REGION; |
1854 | 0 | } |
1855 | 0 | cleanup_regions(regions, total_regions); |
1856 | 0 | get_region_stats(stats_start, regions, *total_regions); |
1857 | |
|
1858 | 0 | for (k = 0; k < *total_regions; k++) { |
1859 | 0 | regions[k].start += offset; |
1860 | 0 | regions[k].last += offset; |
1861 | 0 | } |
1862 | 0 | } |
1863 | | |
1864 | | static int find_regions_index(const REGIONS *regions, int num_regions, |
1865 | 1.26k | int frame_idx) { |
1866 | 1.26k | for (int k = 0; k < num_regions; k++) { |
1867 | 0 | if (regions[k].start <= frame_idx && regions[k].last >= frame_idx) { |
1868 | 0 | return k; |
1869 | 0 | } |
1870 | 0 | } |
1871 | 1.26k | return -1; |
1872 | 1.26k | } |
1873 | | |
1874 | | /*!\brief Determine the length of future GF groups. |
1875 | | * |
1876 | | * \ingroup gf_group_algo |
1877 | | * This function decides the gf group length of future frames in batch |
1878 | | * |
1879 | | * \param[in] cpi Top-level encoder structure |
1880 | | * \param[in] max_gop_length Maximum length of the GF group |
1881 | | * \param[in] max_intervals Maximum number of intervals to decide |
1882 | | * |
1883 | | * \return Nothing is returned. Instead, cpi->ppi->rc.gf_intervals is |
1884 | | * changed to store the decided GF group lengths. |
1885 | | */ |
1886 | | static void calculate_gf_length(AV1_COMP *cpi, int max_gop_length, |
1887 | 1.26k | int max_intervals) { |
1888 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
1889 | 1.26k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
1890 | 1.26k | TWO_PASS *const twopass = &cpi->ppi->twopass; |
1891 | 1.26k | FIRSTPASS_STATS next_frame; |
1892 | 1.26k | const FIRSTPASS_STATS *const start_pos = cpi->twopass_frame.stats_in; |
1893 | 1.26k | const FIRSTPASS_STATS *const stats = start_pos - (rc->frames_since_key == 0); |
1894 | | |
1895 | 1.26k | const int f_w = cpi->common.width; |
1896 | 1.26k | const int f_h = cpi->common.height; |
1897 | 1.26k | int i; |
1898 | | |
1899 | 1.26k | int flash_detected; |
1900 | | |
1901 | 1.26k | av1_zero(next_frame); |
1902 | | |
1903 | 1.26k | if (has_no_stats_stage(cpi)) { |
1904 | 20.1k | for (i = 0; i < MAX_NUM_GF_INTERVALS; i++) { |
1905 | 18.9k | p_rc->gf_intervals[i] = AOMMIN(rc->max_gf_interval, max_gop_length); |
1906 | 18.9k | } |
1907 | 1.26k | p_rc->cur_gf_index = 0; |
1908 | 1.26k | rc->intervals_till_gf_calculate_due = MAX_NUM_GF_INTERVALS; |
1909 | 1.26k | return; |
1910 | 1.26k | } |
1911 | | |
1912 | | // TODO(urvang): Try logic to vary min and max interval based on q. |
1913 | 0 | const int active_min_gf_interval = rc->min_gf_interval; |
1914 | 0 | const int active_max_gf_interval = |
1915 | 0 | AOMMIN(rc->max_gf_interval, max_gop_length); |
1916 | 0 | const int min_shrink_int = AOMMAX(MIN_SHRINK_LEN, active_min_gf_interval); |
1917 | |
|
1918 | 0 | i = (rc->frames_since_key == 0); |
1919 | 0 | max_intervals = cpi->ppi->lap_enabled ? 1 : max_intervals; |
1920 | 0 | int count_cuts = 1; |
1921 | | // If cpi->gf_state.arf_gf_boost_lst is 0, we are starting with a KF or GF. |
1922 | 0 | int cur_start = -1 + !cpi->ppi->gf_state.arf_gf_boost_lst, cur_last; |
1923 | 0 | int cut_pos[MAX_NUM_GF_INTERVALS + 1] = { -1 }; |
1924 | 0 | int cut_here; |
1925 | 0 | GF_GROUP_STATS gf_stats; |
1926 | 0 | init_gf_stats(&gf_stats); |
1927 | 0 | while (count_cuts < max_intervals + 1) { |
1928 | | // reaches next key frame, break here |
1929 | 0 | if (i >= rc->frames_to_key) { |
1930 | 0 | cut_here = 2; |
1931 | 0 | } else if (i - cur_start >= rc->static_scene_max_gf_interval) { |
1932 | | // reached maximum len, but nothing special yet (almost static) |
1933 | | // let's look at the next interval |
1934 | 0 | cut_here = 1; |
1935 | 0 | } else if (EOF == input_stats(twopass, &cpi->twopass_frame, &next_frame)) { |
1936 | | // reaches last frame, break |
1937 | 0 | cut_here = 2; |
1938 | 0 | } else { |
1939 | | // Test for the case where there is a brief flash but the prediction |
1940 | | // quality back to an earlier frame is then restored. |
1941 | 0 | flash_detected = detect_flash(twopass, &cpi->twopass_frame, 0); |
1942 | | // TODO(bohanli): remove redundant accumulations here, or unify |
1943 | | // this and the ones in define_gf_group |
1944 | 0 | accumulate_next_frame_stats(&next_frame, flash_detected, |
1945 | 0 | rc->frames_since_key, i, &gf_stats, f_w, f_h); |
1946 | |
|
1947 | 0 | cut_here = detect_gf_cut(cpi, i, cur_start, flash_detected, |
1948 | 0 | active_max_gf_interval, active_min_gf_interval, |
1949 | 0 | &gf_stats); |
1950 | 0 | } |
1951 | 0 | if (cut_here) { |
1952 | 0 | cur_last = i - 1; // the current last frame in the gf group |
1953 | 0 | int ori_last = cur_last; |
1954 | | // The region frame idx does not start from the same frame as cur_start |
1955 | | // and cur_last. Need to offset them. |
1956 | 0 | int offset = rc->frames_since_key - p_rc->regions_offset; |
1957 | 0 | REGIONS *regions = p_rc->regions; |
1958 | 0 | int num_regions = p_rc->num_regions; |
1959 | |
|
1960 | 0 | int scenecut_idx = -1; |
1961 | | // only try shrinking if interval smaller than active_max_gf_interval |
1962 | 0 | if (cur_last - cur_start <= active_max_gf_interval && |
1963 | 0 | cur_last > cur_start) { |
1964 | | // find the region indices of where the first and last frame belong. |
1965 | 0 | int k_start = |
1966 | 0 | find_regions_index(regions, num_regions, cur_start + offset); |
1967 | 0 | int k_last = |
1968 | 0 | find_regions_index(regions, num_regions, cur_last + offset); |
1969 | 0 | if (cur_start + offset == 0) k_start = 0; |
1970 | | |
1971 | | // See if we have a scenecut in between |
1972 | 0 | for (int r = k_start + 1; r <= k_last; r++) { |
1973 | 0 | if (regions[r].type == SCENECUT_REGION && |
1974 | 0 | regions[r].last - offset - cur_start > active_min_gf_interval) { |
1975 | 0 | scenecut_idx = r; |
1976 | 0 | break; |
1977 | 0 | } |
1978 | 0 | } |
1979 | | |
1980 | | // if the found scenecut is very close to the end, ignore it. |
1981 | 0 | if (regions[num_regions - 1].last - regions[scenecut_idx].last < 4) { |
1982 | 0 | scenecut_idx = -1; |
1983 | 0 | } |
1984 | |
|
1985 | 0 | if (scenecut_idx != -1) { |
1986 | | // If we have a scenecut, then stop at it. |
1987 | | // TODO(bohanli): add logic here to stop before the scenecut and for |
1988 | | // the next gop start from the scenecut with GF |
1989 | 0 | int is_minor_sc = |
1990 | 0 | (regions[scenecut_idx].avg_cor_coeff * |
1991 | 0 | (1 - stats[regions[scenecut_idx].start - offset].noise_var / |
1992 | 0 | regions[scenecut_idx].avg_intra_err) > |
1993 | 0 | 0.6); |
1994 | 0 | cur_last = regions[scenecut_idx].last - offset - !is_minor_sc; |
1995 | 0 | } else { |
1996 | 0 | int is_last_analysed = (k_last == num_regions - 1) && |
1997 | 0 | (cur_last + offset == regions[k_last].last); |
1998 | 0 | int not_enough_regions = |
1999 | 0 | k_last - k_start <= |
2000 | 0 | 1 + (regions[k_start].type == SCENECUT_REGION); |
2001 | | // if we are very close to the end, then do not shrink since it may |
2002 | | // introduce intervals that are too short |
2003 | 0 | if (!(is_last_analysed && not_enough_regions)) { |
2004 | 0 | const double arf_length_factor = 0.1; |
2005 | 0 | double best_score = 0; |
2006 | 0 | int best_j = -1; |
2007 | 0 | const int first_frame = regions[0].start - offset; |
2008 | 0 | const int last_frame = regions[num_regions - 1].last - offset; |
2009 | | // score of how much the arf helps the whole GOP |
2010 | 0 | double base_score = 0.0; |
2011 | | // Accumulate base_score in |
2012 | 0 | for (int j = cur_start + 1; j < cur_start + min_shrink_int; j++) { |
2013 | 0 | if (stats + j >= twopass->stats_buf_ctx->stats_in_end) break; |
2014 | 0 | base_score = (base_score + 1.0) * stats[j].cor_coeff; |
2015 | 0 | } |
2016 | 0 | int met_blending = 0; // Whether we have met blending areas before |
2017 | 0 | int last_blending = 0; // Whether the previous frame if blending |
2018 | 0 | for (int j = cur_start + min_shrink_int; j <= cur_last; j++) { |
2019 | 0 | if (stats + j >= twopass->stats_buf_ctx->stats_in_end) break; |
2020 | 0 | base_score = (base_score + 1.0) * stats[j].cor_coeff; |
2021 | 0 | int this_reg = |
2022 | 0 | find_regions_index(regions, num_regions, j + offset); |
2023 | 0 | if (this_reg < 0) continue; |
2024 | | // A GOP should include at most 1 blending region. |
2025 | 0 | if (regions[this_reg].type == BLENDING_REGION) { |
2026 | 0 | last_blending = 1; |
2027 | 0 | if (met_blending) { |
2028 | 0 | break; |
2029 | 0 | } else { |
2030 | 0 | base_score = 0; |
2031 | 0 | continue; |
2032 | 0 | } |
2033 | 0 | } else { |
2034 | 0 | if (last_blending) met_blending = 1; |
2035 | 0 | last_blending = 0; |
2036 | 0 | } |
2037 | | |
2038 | | // Add the factor of how good the neighborhood is for this |
2039 | | // candidate arf. |
2040 | 0 | double this_score = arf_length_factor * base_score; |
2041 | 0 | double temp_accu_coeff = 1.0; |
2042 | | // following frames |
2043 | 0 | int count_f = 0; |
2044 | 0 | for (int n = j + 1; n <= j + 3 && n <= last_frame; n++) { |
2045 | 0 | if (stats + n >= twopass->stats_buf_ctx->stats_in_end) break; |
2046 | 0 | temp_accu_coeff *= stats[n].cor_coeff; |
2047 | 0 | this_score += |
2048 | 0 | temp_accu_coeff * |
2049 | 0 | (1 - stats[n].noise_var / |
2050 | 0 | AOMMAX(regions[this_reg].avg_intra_err, 0.001)); |
2051 | 0 | count_f++; |
2052 | 0 | } |
2053 | | // preceding frames |
2054 | 0 | temp_accu_coeff = 1.0; |
2055 | 0 | for (int n = j; n > j - 3 * 2 + count_f && n > first_frame; n--) { |
2056 | 0 | if (stats + n < twopass->stats_buf_ctx->stats_in_start) break; |
2057 | 0 | temp_accu_coeff *= stats[n].cor_coeff; |
2058 | 0 | this_score += |
2059 | 0 | temp_accu_coeff * |
2060 | 0 | (1 - stats[n].noise_var / |
2061 | 0 | AOMMAX(regions[this_reg].avg_intra_err, 0.001)); |
2062 | 0 | } |
2063 | |
|
2064 | 0 | if (this_score > best_score) { |
2065 | 0 | best_score = this_score; |
2066 | 0 | best_j = j; |
2067 | 0 | } |
2068 | 0 | } |
2069 | | |
2070 | | // For blending areas, move one more frame in case we missed the |
2071 | | // first blending frame. |
2072 | 0 | int best_reg = |
2073 | 0 | find_regions_index(regions, num_regions, best_j + offset); |
2074 | 0 | if (best_reg < num_regions - 1 && best_reg > 0) { |
2075 | 0 | if (regions[best_reg - 1].type == BLENDING_REGION && |
2076 | 0 | regions[best_reg + 1].type == BLENDING_REGION) { |
2077 | 0 | if (best_j + offset == regions[best_reg].start && |
2078 | 0 | best_j + offset < regions[best_reg].last) { |
2079 | 0 | best_j += 1; |
2080 | 0 | } else if (best_j + offset == regions[best_reg].last && |
2081 | 0 | best_j + offset > regions[best_reg].start) { |
2082 | 0 | best_j -= 1; |
2083 | 0 | } |
2084 | 0 | } |
2085 | 0 | } |
2086 | |
|
2087 | 0 | if (cur_last - best_j < 2) best_j = cur_last; |
2088 | 0 | if (best_j > 0 && best_score > 0.1) cur_last = best_j; |
2089 | | // if cannot find anything, just cut at the original place. |
2090 | 0 | } |
2091 | 0 | } |
2092 | 0 | } |
2093 | 0 | cut_pos[count_cuts] = cur_last; |
2094 | 0 | count_cuts++; |
2095 | | |
2096 | | // reset pointers to the shrinked location |
2097 | 0 | cpi->twopass_frame.stats_in = start_pos + cur_last; |
2098 | 0 | cur_start = cur_last; |
2099 | 0 | int cur_region_idx = |
2100 | 0 | find_regions_index(regions, num_regions, cur_start + 1 + offset); |
2101 | 0 | if (cur_region_idx >= 0) |
2102 | 0 | if (regions[cur_region_idx].type == SCENECUT_REGION) cur_start++; |
2103 | |
|
2104 | 0 | i = cur_last; |
2105 | |
|
2106 | 0 | if (cut_here > 1 && cur_last == ori_last) break; |
2107 | | |
2108 | | // reset accumulators |
2109 | 0 | init_gf_stats(&gf_stats); |
2110 | 0 | } |
2111 | 0 | ++i; |
2112 | 0 | } |
2113 | | |
2114 | | // save intervals |
2115 | 0 | rc->intervals_till_gf_calculate_due = count_cuts - 1; |
2116 | 0 | for (int n = 1; n < count_cuts; n++) { |
2117 | 0 | p_rc->gf_intervals[n - 1] = cut_pos[n] - cut_pos[n - 1]; |
2118 | 0 | } |
2119 | 0 | p_rc->cur_gf_index = 0; |
2120 | 0 | cpi->twopass_frame.stats_in = start_pos; |
2121 | 0 | } |
2122 | | |
2123 | 3.78k | static void correct_frames_to_key(AV1_COMP *cpi) { |
2124 | 3.78k | int lookahead_size = |
2125 | 3.78k | (int)av1_lookahead_depth(cpi->ppi->lookahead, cpi->compressor_stage); |
2126 | 3.78k | if (lookahead_size < |
2127 | 3.78k | av1_lookahead_pop_sz(cpi->ppi->lookahead, cpi->compressor_stage)) { |
2128 | 0 | assert( |
2129 | 0 | IMPLIES(cpi->oxcf.pass != AOM_RC_ONE_PASS && cpi->ppi->frames_left > 0, |
2130 | 0 | lookahead_size == cpi->ppi->frames_left)); |
2131 | 0 | cpi->rc.frames_to_key = AOMMIN(cpi->rc.frames_to_key, lookahead_size); |
2132 | 3.78k | } else if (cpi->ppi->frames_left > 0) { |
2133 | | // Correct frames to key based on limit |
2134 | 3.78k | cpi->rc.frames_to_key = |
2135 | 3.78k | AOMMIN(cpi->rc.frames_to_key, cpi->ppi->frames_left); |
2136 | 3.78k | } |
2137 | 3.78k | } |
2138 | | |
2139 | | /*!\brief Define a GF group in one pass mode when no look ahead stats are |
2140 | | * available. |
2141 | | * |
2142 | | * \ingroup gf_group_algo |
2143 | | * This function defines the structure of a GF group, along with various |
2144 | | * parameters regarding bit-allocation and quality setup in the special |
2145 | | * case of one pass encoding where no lookahead stats are avialable. |
2146 | | * |
2147 | | * \param[in] cpi Top-level encoder structure |
2148 | | * |
2149 | | * \return Nothing is returned. Instead, cpi->ppi->gf_group is changed. |
2150 | | */ |
2151 | 2.52k | static void define_gf_group_pass0(AV1_COMP *cpi) { |
2152 | 2.52k | RATE_CONTROL *const rc = &cpi->rc; |
2153 | 2.52k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2154 | 2.52k | GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
2155 | 2.52k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2156 | 2.52k | const GFConfig *const gf_cfg = &oxcf->gf_cfg; |
2157 | 2.52k | int target; |
2158 | | |
2159 | 2.52k | if (oxcf->q_cfg.aq_mode == CYCLIC_REFRESH_AQ) { |
2160 | 0 | av1_cyclic_refresh_set_golden_update(cpi); |
2161 | 2.52k | } else { |
2162 | 2.52k | p_rc->baseline_gf_interval = p_rc->gf_intervals[p_rc->cur_gf_index]; |
2163 | 2.52k | rc->intervals_till_gf_calculate_due--; |
2164 | 2.52k | p_rc->cur_gf_index++; |
2165 | 2.52k | } |
2166 | | |
2167 | | // correct frames_to_key when lookahead queue is flushing |
2168 | 2.52k | correct_frames_to_key(cpi); |
2169 | | |
2170 | 2.52k | if (p_rc->baseline_gf_interval > rc->frames_to_key) |
2171 | 0 | p_rc->baseline_gf_interval = rc->frames_to_key; |
2172 | | |
2173 | 2.52k | p_rc->gfu_boost = DEFAULT_GF_BOOST; |
2174 | 2.52k | p_rc->constrained_gf_group = |
2175 | 2.52k | (p_rc->baseline_gf_interval >= rc->frames_to_key) ? 1 : 0; |
2176 | | |
2177 | 2.52k | gf_group->max_layer_depth_allowed = oxcf->gf_cfg.gf_max_pyr_height; |
2178 | | |
2179 | | // Rare case when the look-ahead is less than the target GOP length, can't |
2180 | | // generate ARF frame. |
2181 | 2.52k | if (p_rc->baseline_gf_interval > gf_cfg->lag_in_frames || |
2182 | 2.52k | !is_altref_enabled(gf_cfg->lag_in_frames, gf_cfg->enable_auto_arf) || |
2183 | 2.52k | p_rc->baseline_gf_interval < rc->min_gf_interval) |
2184 | 2.52k | gf_group->max_layer_depth_allowed = 0; |
2185 | | |
2186 | | // Set up the structure of this Group-Of-Pictures (same as GF_GROUP) |
2187 | 2.52k | av1_gop_setup_structure(cpi); |
2188 | | |
2189 | | // Allocate bits to each of the frames in the GF group. |
2190 | | // TODO(sarahparker) Extend this to work with pyramid structure. |
2191 | 5.04k | for (int cur_index = 0; cur_index < gf_group->size; ++cur_index) { |
2192 | 2.52k | const FRAME_UPDATE_TYPE cur_update_type = gf_group->update_type[cur_index]; |
2193 | 2.52k | if (oxcf->rc_cfg.mode == AOM_CBR) { |
2194 | 0 | if (cur_update_type == KF_UPDATE) { |
2195 | 0 | target = av1_calc_iframe_target_size_one_pass_cbr(cpi); |
2196 | 0 | } else { |
2197 | 0 | target = av1_calc_pframe_target_size_one_pass_cbr(cpi, cur_update_type); |
2198 | 0 | } |
2199 | 2.52k | } else { |
2200 | 2.52k | if (cur_update_type == KF_UPDATE) { |
2201 | 2.52k | target = av1_calc_iframe_target_size_one_pass_vbr(cpi); |
2202 | 2.52k | } else { |
2203 | 0 | target = av1_calc_pframe_target_size_one_pass_vbr(cpi, cur_update_type); |
2204 | 0 | } |
2205 | 2.52k | } |
2206 | 2.52k | gf_group->bit_allocation[cur_index] = target; |
2207 | 2.52k | } |
2208 | 2.52k | } |
2209 | | |
2210 | | static INLINE void set_baseline_gf_interval(PRIMARY_RATE_CONTROL *p_rc, |
2211 | 0 | int arf_position) { |
2212 | 0 | p_rc->baseline_gf_interval = arf_position; |
2213 | 0 | } |
2214 | | |
2215 | | // initialize GF_GROUP_STATS |
2216 | 0 | static void init_gf_stats(GF_GROUP_STATS *gf_stats) { |
2217 | 0 | gf_stats->gf_group_err = 0.0; |
2218 | 0 | gf_stats->gf_group_raw_error = 0.0; |
2219 | 0 | gf_stats->gf_group_skip_pct = 0.0; |
2220 | 0 | gf_stats->gf_group_inactive_zone_rows = 0.0; |
2221 | |
|
2222 | 0 | gf_stats->mv_ratio_accumulator = 0.0; |
2223 | 0 | gf_stats->decay_accumulator = 1.0; |
2224 | 0 | gf_stats->zero_motion_accumulator = 1.0; |
2225 | 0 | gf_stats->loop_decay_rate = 1.0; |
2226 | 0 | gf_stats->last_loop_decay_rate = 1.0; |
2227 | 0 | gf_stats->this_frame_mv_in_out = 0.0; |
2228 | 0 | gf_stats->mv_in_out_accumulator = 0.0; |
2229 | 0 | gf_stats->abs_mv_in_out_accumulator = 0.0; |
2230 | |
|
2231 | 0 | gf_stats->avg_sr_coded_error = 0.0; |
2232 | 0 | gf_stats->avg_pcnt_second_ref = 0.0; |
2233 | 0 | gf_stats->avg_new_mv_count = 0.0; |
2234 | 0 | gf_stats->avg_wavelet_energy = 0.0; |
2235 | 0 | gf_stats->avg_raw_err_stdev = 0.0; |
2236 | 0 | gf_stats->non_zero_stdev_count = 0; |
2237 | 0 | } |
2238 | | |
2239 | | static void accumulate_gop_stats(AV1_COMP *cpi, int is_intra_only, int f_w, |
2240 | | int f_h, FIRSTPASS_STATS *next_frame, |
2241 | | const FIRSTPASS_STATS *start_pos, |
2242 | 0 | GF_GROUP_STATS *gf_stats, int *idx) { |
2243 | 0 | int i, flash_detected; |
2244 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
2245 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2246 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2247 | 0 | FRAME_INFO *frame_info = &cpi->frame_info; |
2248 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2249 | |
|
2250 | 0 | init_gf_stats(gf_stats); |
2251 | 0 | av1_zero(*next_frame); |
2252 | | |
2253 | | // If this is a key frame or the overlay from a previous arf then |
2254 | | // the error score / cost of this frame has already been accounted for. |
2255 | 0 | i = is_intra_only; |
2256 | | // get the determined gf group length from p_rc->gf_intervals |
2257 | 0 | while (i < p_rc->gf_intervals[p_rc->cur_gf_index]) { |
2258 | | // read in the next frame |
2259 | 0 | if (EOF == input_stats(twopass, &cpi->twopass_frame, next_frame)) break; |
2260 | | // Accumulate error score of frames in this gf group. |
2261 | 0 | double mod_frame_err = |
2262 | 0 | calculate_modified_err(frame_info, twopass, oxcf, next_frame); |
2263 | | // accumulate stats for this frame |
2264 | 0 | accumulate_this_frame_stats(next_frame, mod_frame_err, gf_stats); |
2265 | 0 | ++i; |
2266 | 0 | } |
2267 | |
|
2268 | 0 | reset_fpf_position(&cpi->twopass_frame, start_pos); |
2269 | |
|
2270 | 0 | i = is_intra_only; |
2271 | 0 | input_stats(twopass, &cpi->twopass_frame, next_frame); |
2272 | 0 | while (i < p_rc->gf_intervals[p_rc->cur_gf_index]) { |
2273 | | // read in the next frame |
2274 | 0 | if (EOF == input_stats(twopass, &cpi->twopass_frame, next_frame)) break; |
2275 | | |
2276 | | // Test for the case where there is a brief flash but the prediction |
2277 | | // quality back to an earlier frame is then restored. |
2278 | 0 | flash_detected = detect_flash(twopass, &cpi->twopass_frame, 0); |
2279 | | |
2280 | | // accumulate stats for next frame |
2281 | 0 | accumulate_next_frame_stats(next_frame, flash_detected, |
2282 | 0 | rc->frames_since_key, i, gf_stats, f_w, f_h); |
2283 | |
|
2284 | 0 | ++i; |
2285 | 0 | } |
2286 | |
|
2287 | 0 | i = p_rc->gf_intervals[p_rc->cur_gf_index]; |
2288 | 0 | average_gf_stats(i, gf_stats); |
2289 | |
|
2290 | 0 | *idx = i; |
2291 | 0 | } |
2292 | | |
2293 | | static void update_gop_length(RATE_CONTROL *rc, PRIMARY_RATE_CONTROL *p_rc, |
2294 | 0 | int idx, int is_final_pass) { |
2295 | 0 | if (is_final_pass) { |
2296 | 0 | rc->intervals_till_gf_calculate_due--; |
2297 | 0 | p_rc->cur_gf_index++; |
2298 | 0 | } |
2299 | | |
2300 | | // Was the group length constrained by the requirement for a new KF? |
2301 | 0 | p_rc->constrained_gf_group = (idx >= rc->frames_to_key) ? 1 : 0; |
2302 | |
|
2303 | 0 | set_baseline_gf_interval(p_rc, idx); |
2304 | 0 | rc->frames_till_gf_update_due = p_rc->baseline_gf_interval; |
2305 | 0 | } |
2306 | | |
2307 | | #define MAX_GF_BOOST 5400 |
2308 | 0 | #define REDUCE_GF_LENGTH_THRESH 4 |
2309 | 0 | #define REDUCE_GF_LENGTH_TO_KEY_THRESH 9 |
2310 | 0 | #define REDUCE_GF_LENGTH_BY 1 |
2311 | | static void set_gop_bits_boost(AV1_COMP *cpi, int i, int is_intra_only, |
2312 | | int is_final_pass, int use_alt_ref, |
2313 | | int alt_offset, const FIRSTPASS_STATS *start_pos, |
2314 | 0 | GF_GROUP_STATS *gf_stats) { |
2315 | | // Should we use the alternate reference frame. |
2316 | 0 | AV1_COMMON *const cm = &cpi->common; |
2317 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2318 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2319 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
2320 | 0 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
2321 | 0 | FRAME_INFO *frame_info = &cpi->frame_info; |
2322 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2323 | 0 | const RateControlCfg *const rc_cfg = &oxcf->rc_cfg; |
2324 | |
|
2325 | 0 | int ext_len = i - is_intra_only; |
2326 | 0 | if (use_alt_ref) { |
2327 | 0 | const int forward_frames = (rc->frames_to_key - i >= ext_len) |
2328 | 0 | ? ext_len |
2329 | 0 | : AOMMAX(0, rc->frames_to_key - i); |
2330 | | |
2331 | | // Calculate the boost for alt ref. |
2332 | 0 | p_rc->gfu_boost = av1_calc_arf_boost( |
2333 | 0 | twopass, &cpi->twopass_frame, p_rc, frame_info, alt_offset, |
2334 | 0 | forward_frames, ext_len, &p_rc->num_stats_used_for_gfu_boost, |
2335 | 0 | &p_rc->num_stats_required_for_gfu_boost, cpi->ppi->lap_enabled); |
2336 | 0 | } else { |
2337 | 0 | reset_fpf_position(&cpi->twopass_frame, start_pos); |
2338 | 0 | p_rc->gfu_boost = AOMMIN( |
2339 | 0 | MAX_GF_BOOST, |
2340 | 0 | av1_calc_arf_boost( |
2341 | 0 | twopass, &cpi->twopass_frame, p_rc, frame_info, alt_offset, ext_len, |
2342 | 0 | 0, &p_rc->num_stats_used_for_gfu_boost, |
2343 | 0 | &p_rc->num_stats_required_for_gfu_boost, cpi->ppi->lap_enabled)); |
2344 | 0 | } |
2345 | |
|
2346 | 0 | #define LAST_ALR_BOOST_FACTOR 0.2f |
2347 | 0 | p_rc->arf_boost_factor = 1.0; |
2348 | 0 | if (use_alt_ref && !is_lossless_requested(rc_cfg)) { |
2349 | | // Reduce the boost of altref in the last gf group |
2350 | 0 | if (rc->frames_to_key - ext_len == REDUCE_GF_LENGTH_BY || |
2351 | 0 | rc->frames_to_key - ext_len == 0) { |
2352 | 0 | p_rc->arf_boost_factor = LAST_ALR_BOOST_FACTOR; |
2353 | 0 | } |
2354 | 0 | } |
2355 | | |
2356 | | // Reset the file position. |
2357 | 0 | reset_fpf_position(&cpi->twopass_frame, start_pos); |
2358 | 0 | if (cpi->ppi->lap_enabled) { |
2359 | | // Since we don't have enough stats to know the actual error of the |
2360 | | // gf group, we assume error of each frame to be equal to 1 and set |
2361 | | // the error of the group as baseline_gf_interval. |
2362 | 0 | gf_stats->gf_group_err = p_rc->baseline_gf_interval; |
2363 | 0 | } |
2364 | | // Calculate the bits to be allocated to the gf/arf group as a whole |
2365 | 0 | p_rc->gf_group_bits = |
2366 | 0 | calculate_total_gf_group_bits(cpi, gf_stats->gf_group_err); |
2367 | |
|
2368 | 0 | #if GROUP_ADAPTIVE_MAXQ |
2369 | | // Calculate an estimate of the maxq needed for the group. |
2370 | | // We are more agressive about correcting for sections |
2371 | | // where there could be significant overshoot than for easier |
2372 | | // sections where we do not wish to risk creating an overshoot |
2373 | | // of the allocated bit budget. |
2374 | 0 | if ((rc_cfg->mode != AOM_Q) && (p_rc->baseline_gf_interval > 1) && |
2375 | 0 | is_final_pass) { |
2376 | 0 | const int vbr_group_bits_per_frame = |
2377 | 0 | (int)(p_rc->gf_group_bits / p_rc->baseline_gf_interval); |
2378 | 0 | const double group_av_err = |
2379 | 0 | gf_stats->gf_group_raw_error / p_rc->baseline_gf_interval; |
2380 | 0 | const double group_av_skip_pct = |
2381 | 0 | gf_stats->gf_group_skip_pct / p_rc->baseline_gf_interval; |
2382 | 0 | const double group_av_inactive_zone = |
2383 | 0 | ((gf_stats->gf_group_inactive_zone_rows * 2) / |
2384 | 0 | (p_rc->baseline_gf_interval * (double)cm->mi_params.mb_rows)); |
2385 | |
|
2386 | 0 | int tmp_q; |
2387 | 0 | tmp_q = get_twopass_worst_quality( |
2388 | 0 | cpi, group_av_err, (group_av_skip_pct + group_av_inactive_zone), |
2389 | 0 | vbr_group_bits_per_frame); |
2390 | 0 | rc->active_worst_quality = AOMMAX(tmp_q, rc->active_worst_quality >> 1); |
2391 | 0 | } |
2392 | 0 | #endif |
2393 | | |
2394 | | // Adjust KF group bits and error remaining. |
2395 | 0 | if (is_final_pass) twopass->kf_group_error_left -= gf_stats->gf_group_err; |
2396 | | |
2397 | | // Reset the file position. |
2398 | 0 | reset_fpf_position(&cpi->twopass_frame, start_pos); |
2399 | | |
2400 | | // Calculate a section intra ratio used in setting max loop filter. |
2401 | 0 | if (rc->frames_since_key != 0) { |
2402 | 0 | twopass->section_intra_rating = calculate_section_intra_ratio( |
2403 | 0 | start_pos, twopass->stats_buf_ctx->stats_in_end, |
2404 | 0 | p_rc->baseline_gf_interval); |
2405 | 0 | } |
2406 | |
|
2407 | 0 | av1_gop_bit_allocation(cpi, rc, gf_group, rc->frames_since_key == 0, |
2408 | 0 | use_alt_ref, p_rc->gf_group_bits); |
2409 | | |
2410 | | // TODO(jingning): Generalize this condition. |
2411 | 0 | if (is_final_pass) { |
2412 | 0 | cpi->ppi->gf_state.arf_gf_boost_lst = use_alt_ref; |
2413 | | |
2414 | | // Reset rolling actual and target bits counters for ARF groups. |
2415 | 0 | twopass->rolling_arf_group_target_bits = 1; |
2416 | 0 | twopass->rolling_arf_group_actual_bits = 1; |
2417 | 0 | } |
2418 | | #if CONFIG_BITRATE_ACCURACY |
2419 | | if (is_final_pass) { |
2420 | | av1_vbr_rc_set_gop_bit_budget(&cpi->vbr_rc_info, |
2421 | | p_rc->baseline_gf_interval); |
2422 | | } |
2423 | | #endif |
2424 | 0 | } |
2425 | | |
2426 | | /*!\brief Define a GF group. |
2427 | | * |
2428 | | * \ingroup gf_group_algo |
2429 | | * This function defines the structure of a GF group, along with various |
2430 | | * parameters regarding bit-allocation and quality setup. |
2431 | | * |
2432 | | * \param[in] cpi Top-level encoder structure |
2433 | | * \param[in] frame_params Structure with frame parameters |
2434 | | * \param[in] is_final_pass Whether this is the final pass for the |
2435 | | * GF group, or a trial (non-zero) |
2436 | | * |
2437 | | * \return Nothing is returned. Instead, cpi->ppi->gf_group is changed. |
2438 | | */ |
2439 | | static void define_gf_group(AV1_COMP *cpi, EncodeFrameParams *frame_params, |
2440 | 2.52k | int is_final_pass) { |
2441 | 2.52k | AV1_COMMON *const cm = &cpi->common; |
2442 | 2.52k | RATE_CONTROL *const rc = &cpi->rc; |
2443 | 2.52k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2444 | 2.52k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2445 | 2.52k | TWO_PASS *const twopass = &cpi->ppi->twopass; |
2446 | 2.52k | FIRSTPASS_STATS next_frame; |
2447 | 2.52k | const FIRSTPASS_STATS *const start_pos = cpi->twopass_frame.stats_in; |
2448 | 2.52k | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
2449 | 2.52k | const GFConfig *const gf_cfg = &oxcf->gf_cfg; |
2450 | 2.52k | const RateControlCfg *const rc_cfg = &oxcf->rc_cfg; |
2451 | 2.52k | const int f_w = cm->width; |
2452 | 2.52k | const int f_h = cm->height; |
2453 | 2.52k | int i; |
2454 | 2.52k | const int is_intra_only = rc->frames_since_key == 0; |
2455 | | |
2456 | 2.52k | cpi->ppi->internal_altref_allowed = (gf_cfg->gf_max_pyr_height > 1); |
2457 | | |
2458 | | // Reset the GF group data structures unless this is a key |
2459 | | // frame in which case it will already have been done. |
2460 | 2.52k | if (!is_intra_only) { |
2461 | 0 | av1_zero(cpi->ppi->gf_group); |
2462 | 0 | cpi->gf_frame_index = 0; |
2463 | 0 | } |
2464 | | |
2465 | 2.52k | if (has_no_stats_stage(cpi)) { |
2466 | 2.52k | define_gf_group_pass0(cpi); |
2467 | 2.52k | return; |
2468 | 2.52k | } |
2469 | | |
2470 | 0 | if (cpi->third_pass_ctx && oxcf->pass == AOM_RC_THIRD_PASS) { |
2471 | 0 | int ret = define_gf_group_pass3(cpi, frame_params, is_final_pass); |
2472 | 0 | if (ret == 0) return; |
2473 | | |
2474 | 0 | av1_free_thirdpass_ctx(cpi->third_pass_ctx); |
2475 | 0 | cpi->third_pass_ctx = NULL; |
2476 | 0 | } |
2477 | | |
2478 | | // correct frames_to_key when lookahead queue is emptying |
2479 | 0 | if (cpi->ppi->lap_enabled) { |
2480 | 0 | correct_frames_to_key(cpi); |
2481 | 0 | } |
2482 | |
|
2483 | 0 | GF_GROUP_STATS gf_stats; |
2484 | 0 | accumulate_gop_stats(cpi, is_intra_only, f_w, f_h, &next_frame, start_pos, |
2485 | 0 | &gf_stats, &i); |
2486 | |
|
2487 | 0 | const int can_disable_arf = !gf_cfg->gf_min_pyr_height; |
2488 | | |
2489 | | // If this is a key frame or the overlay from a previous arf then |
2490 | | // the error score / cost of this frame has already been accounted for. |
2491 | 0 | const int active_min_gf_interval = rc->min_gf_interval; |
2492 | | |
2493 | | // Disable internal ARFs for "still" gf groups. |
2494 | | // zero_motion_accumulator: minimum percentage of (0,0) motion; |
2495 | | // avg_sr_coded_error: average of the SSE per pixel of each frame; |
2496 | | // avg_raw_err_stdev: average of the standard deviation of (0,0) |
2497 | | // motion error per block of each frame. |
2498 | 0 | const int can_disable_internal_arfs = gf_cfg->gf_min_pyr_height <= 1; |
2499 | 0 | if (can_disable_internal_arfs && |
2500 | 0 | gf_stats.zero_motion_accumulator > MIN_ZERO_MOTION && |
2501 | 0 | gf_stats.avg_sr_coded_error < MAX_SR_CODED_ERROR && |
2502 | 0 | gf_stats.avg_raw_err_stdev < MAX_RAW_ERR_VAR) { |
2503 | 0 | cpi->ppi->internal_altref_allowed = 0; |
2504 | 0 | } |
2505 | |
|
2506 | 0 | int use_alt_ref; |
2507 | 0 | if (can_disable_arf) { |
2508 | 0 | use_alt_ref = |
2509 | 0 | !is_almost_static(gf_stats.zero_motion_accumulator, |
2510 | 0 | twopass->kf_zeromotion_pct, cpi->ppi->lap_enabled) && |
2511 | 0 | p_rc->use_arf_in_this_kf_group && (i < gf_cfg->lag_in_frames) && |
2512 | 0 | (i >= MIN_GF_INTERVAL); |
2513 | 0 | } else { |
2514 | 0 | use_alt_ref = p_rc->use_arf_in_this_kf_group && |
2515 | 0 | (i < gf_cfg->lag_in_frames) && (i > 2); |
2516 | 0 | } |
2517 | 0 | if (use_alt_ref) { |
2518 | 0 | gf_group->max_layer_depth_allowed = gf_cfg->gf_max_pyr_height; |
2519 | 0 | } else { |
2520 | 0 | gf_group->max_layer_depth_allowed = 0; |
2521 | 0 | } |
2522 | |
|
2523 | 0 | int alt_offset = 0; |
2524 | | // The length reduction strategy is tweaked for certain cases, and doesn't |
2525 | | // work well for certain other cases. |
2526 | 0 | const int allow_gf_length_reduction = |
2527 | 0 | ((rc_cfg->mode == AOM_Q && rc_cfg->cq_level <= 128) || |
2528 | 0 | !cpi->ppi->internal_altref_allowed) && |
2529 | 0 | !is_lossless_requested(rc_cfg); |
2530 | |
|
2531 | 0 | if (allow_gf_length_reduction && use_alt_ref) { |
2532 | | // adjust length of this gf group if one of the following condition met |
2533 | | // 1: only one overlay frame left and this gf is too long |
2534 | | // 2: next gf group is too short to have arf compared to the current gf |
2535 | | |
2536 | | // maximum length of next gf group |
2537 | 0 | const int next_gf_len = rc->frames_to_key - i; |
2538 | 0 | const int single_overlay_left = |
2539 | 0 | next_gf_len == 0 && i > REDUCE_GF_LENGTH_THRESH; |
2540 | | // the next gf is probably going to have a ARF but it will be shorter than |
2541 | | // this gf |
2542 | 0 | const int unbalanced_gf = |
2543 | 0 | i > REDUCE_GF_LENGTH_TO_KEY_THRESH && |
2544 | 0 | next_gf_len + 1 < REDUCE_GF_LENGTH_TO_KEY_THRESH && |
2545 | 0 | next_gf_len + 1 >= rc->min_gf_interval; |
2546 | |
|
2547 | 0 | if (single_overlay_left || unbalanced_gf) { |
2548 | 0 | const int roll_back = REDUCE_GF_LENGTH_BY; |
2549 | | // Reduce length only if active_min_gf_interval will be respected later. |
2550 | 0 | if (i - roll_back >= active_min_gf_interval + 1) { |
2551 | 0 | alt_offset = -roll_back; |
2552 | 0 | i -= roll_back; |
2553 | 0 | if (is_final_pass) rc->intervals_till_gf_calculate_due = 0; |
2554 | 0 | p_rc->gf_intervals[p_rc->cur_gf_index] -= roll_back; |
2555 | 0 | reset_fpf_position(&cpi->twopass_frame, start_pos); |
2556 | 0 | accumulate_gop_stats(cpi, is_intra_only, f_w, f_h, &next_frame, |
2557 | 0 | start_pos, &gf_stats, &i); |
2558 | 0 | } |
2559 | 0 | } |
2560 | 0 | } |
2561 | |
|
2562 | 0 | update_gop_length(rc, p_rc, i, is_final_pass); |
2563 | | |
2564 | | // Set up the structure of this Group-Of-Pictures (same as GF_GROUP) |
2565 | 0 | av1_gop_setup_structure(cpi); |
2566 | |
|
2567 | 0 | set_gop_bits_boost(cpi, i, is_intra_only, is_final_pass, use_alt_ref, |
2568 | 0 | alt_offset, start_pos, &gf_stats); |
2569 | |
|
2570 | 0 | frame_params->frame_type = |
2571 | 0 | rc->frames_since_key == 0 ? KEY_FRAME : INTER_FRAME; |
2572 | 0 | frame_params->show_frame = |
2573 | 0 | !(gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE || |
2574 | 0 | gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE); |
2575 | 0 | } |
2576 | | |
2577 | | /*!\brief Define a GF group for the third apss. |
2578 | | * |
2579 | | * \ingroup gf_group_algo |
2580 | | * This function defines the structure of a GF group for the third pass, along |
2581 | | * with various parameters regarding bit-allocation and quality setup based on |
2582 | | * the two-pass bitstream. |
2583 | | * Much of the function still uses the strategies used for the second pass and |
2584 | | * relies on first pass statistics. It is expected that over time these portions |
2585 | | * would be replaced with strategies specific to the third pass. |
2586 | | * |
2587 | | * \param[in] cpi Top-level encoder structure |
2588 | | * \param[in] frame_params Structure with frame parameters |
2589 | | * \param[in] is_final_pass Whether this is the final pass for the |
2590 | | * GF group, or a trial (non-zero) |
2591 | | * |
2592 | | * \return 0: Success; |
2593 | | * -1: There are conflicts between the bitstream and current config |
2594 | | * The values in cpi->ppi->gf_group are also changed. |
2595 | | */ |
2596 | | static int define_gf_group_pass3(AV1_COMP *cpi, EncodeFrameParams *frame_params, |
2597 | 0 | int is_final_pass) { |
2598 | 0 | if (!cpi->third_pass_ctx) return -1; |
2599 | 0 | AV1_COMMON *const cm = &cpi->common; |
2600 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2601 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2602 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2603 | 0 | FIRSTPASS_STATS next_frame; |
2604 | 0 | const FIRSTPASS_STATS *const start_pos = cpi->twopass_frame.stats_in; |
2605 | 0 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
2606 | 0 | const GFConfig *const gf_cfg = &oxcf->gf_cfg; |
2607 | 0 | const int f_w = cm->width; |
2608 | 0 | const int f_h = cm->height; |
2609 | 0 | int i; |
2610 | 0 | const int is_intra_only = rc->frames_since_key == 0; |
2611 | |
|
2612 | 0 | cpi->ppi->internal_altref_allowed = (gf_cfg->gf_max_pyr_height > 1); |
2613 | | |
2614 | | // Reset the GF group data structures unless this is a key |
2615 | | // frame in which case it will already have been done. |
2616 | 0 | if (!is_intra_only) { |
2617 | 0 | av1_zero(cpi->ppi->gf_group); |
2618 | 0 | cpi->gf_frame_index = 0; |
2619 | 0 | } |
2620 | |
|
2621 | 0 | GF_GROUP_STATS gf_stats; |
2622 | 0 | accumulate_gop_stats(cpi, is_intra_only, f_w, f_h, &next_frame, start_pos, |
2623 | 0 | &gf_stats, &i); |
2624 | |
|
2625 | 0 | const int can_disable_arf = !gf_cfg->gf_min_pyr_height; |
2626 | | |
2627 | | // TODO(any): set cpi->ppi->internal_altref_allowed accordingly; |
2628 | |
|
2629 | 0 | int use_alt_ref = av1_check_use_arf(cpi->third_pass_ctx); |
2630 | 0 | if (use_alt_ref == 0 && !can_disable_arf) return -1; |
2631 | 0 | if (use_alt_ref) { |
2632 | 0 | gf_group->max_layer_depth_allowed = gf_cfg->gf_max_pyr_height; |
2633 | 0 | } else { |
2634 | 0 | gf_group->max_layer_depth_allowed = 0; |
2635 | 0 | } |
2636 | |
|
2637 | 0 | update_gop_length(rc, p_rc, i, is_final_pass); |
2638 | | |
2639 | | // Set up the structure of this Group-Of-Pictures (same as GF_GROUP) |
2640 | 0 | av1_gop_setup_structure(cpi); |
2641 | |
|
2642 | 0 | set_gop_bits_boost(cpi, i, is_intra_only, is_final_pass, use_alt_ref, 0, |
2643 | 0 | start_pos, &gf_stats); |
2644 | |
|
2645 | 0 | frame_params->frame_type = cpi->third_pass_ctx->frame_info[0].frame_type; |
2646 | 0 | frame_params->show_frame = cpi->third_pass_ctx->frame_info[0].is_show_frame; |
2647 | 0 | return 0; |
2648 | 0 | } |
2649 | | |
2650 | | // #define FIXED_ARF_BITS |
2651 | | #ifdef FIXED_ARF_BITS |
2652 | | #define ARF_BITS_FRACTION 0.75 |
2653 | | #endif |
2654 | | void av1_gop_bit_allocation(const AV1_COMP *cpi, RATE_CONTROL *const rc, |
2655 | | GF_GROUP *gf_group, int is_key_frame, int use_arf, |
2656 | 0 | int64_t gf_group_bits) { |
2657 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2658 | | // Calculate the extra bits to be used for boosted frame(s) |
2659 | | #ifdef FIXED_ARF_BITS |
2660 | | int gf_arf_bits = (int)(ARF_BITS_FRACTION * gf_group_bits); |
2661 | | #else |
2662 | 0 | int gf_arf_bits = calculate_boost_bits( |
2663 | 0 | p_rc->baseline_gf_interval - (rc->frames_since_key == 0), p_rc->gfu_boost, |
2664 | 0 | gf_group_bits); |
2665 | 0 | #endif |
2666 | |
|
2667 | 0 | gf_arf_bits = adjust_boost_bits_for_target_level(cpi, rc, gf_arf_bits, |
2668 | 0 | gf_group_bits, 1); |
2669 | | |
2670 | | // Allocate bits to each of the frames in the GF group. |
2671 | 0 | allocate_gf_group_bits(gf_group, p_rc, rc, gf_group_bits, gf_arf_bits, |
2672 | 0 | is_key_frame, use_arf); |
2673 | 0 | } |
2674 | | |
2675 | | // Minimum % intra coding observed in first pass (1.0 = 100%) |
2676 | 0 | #define MIN_INTRA_LEVEL 0.25 |
2677 | | // Minimum ratio between the % of intra coding and inter coding in the first |
2678 | | // pass after discounting neutral blocks (discounting neutral blocks in this |
2679 | | // way helps catch scene cuts in clips with very flat areas or letter box |
2680 | | // format clips with image padding. |
2681 | 0 | #define INTRA_VS_INTER_THRESH 2.0 |
2682 | | // Hard threshold where the first pass chooses intra for almost all blocks. |
2683 | | // In such a case even if the frame is not a scene cut coding a key frame |
2684 | | // may be a good option. |
2685 | 0 | #define VERY_LOW_INTER_THRESH 0.05 |
2686 | | // Maximum threshold for the relative ratio of intra error score vs best |
2687 | | // inter error score. |
2688 | 0 | #define KF_II_ERR_THRESHOLD 1.9 |
2689 | | // In real scene cuts there is almost always a sharp change in the intra |
2690 | | // or inter error score. |
2691 | 0 | #define ERR_CHANGE_THRESHOLD 0.4 |
2692 | | // For real scene cuts we expect an improvment in the intra inter error |
2693 | | // ratio in the next frame. |
2694 | 0 | #define II_IMPROVEMENT_THRESHOLD 3.5 |
2695 | 0 | #define KF_II_MAX 128.0 |
2696 | | // Intra / Inter threshold very low |
2697 | 0 | #define VERY_LOW_II 1.5 |
2698 | | // Clean slide transitions we expect a sharp single frame spike in error. |
2699 | 0 | #define ERROR_SPIKE 5.0 |
2700 | | |
2701 | | // Slide show transition detection. |
2702 | | // Tests for case where there is very low error either side of the current frame |
2703 | | // but much higher just for this frame. This can help detect key frames in |
2704 | | // slide shows even where the slides are pictures of different sizes. |
2705 | | // Also requires that intra and inter errors are very similar to help eliminate |
2706 | | // harmful false positives. |
2707 | | // It will not help if the transition is a fade or other multi-frame effect. |
2708 | | static int slide_transition(const FIRSTPASS_STATS *this_frame, |
2709 | | const FIRSTPASS_STATS *last_frame, |
2710 | 0 | const FIRSTPASS_STATS *next_frame) { |
2711 | 0 | return (this_frame->intra_error < (this_frame->coded_error * VERY_LOW_II)) && |
2712 | 0 | (this_frame->coded_error > (last_frame->coded_error * ERROR_SPIKE)) && |
2713 | 0 | (this_frame->coded_error > (next_frame->coded_error * ERROR_SPIKE)); |
2714 | 0 | } |
2715 | | |
2716 | | // Threshold for use of the lagging second reference frame. High second ref |
2717 | | // usage may point to a transient event like a flash or occlusion rather than |
2718 | | // a real scene cut. |
2719 | | // We adapt the threshold based on number of frames in this key-frame group so |
2720 | | // far. |
2721 | 0 | static double get_second_ref_usage_thresh(int frame_count_so_far) { |
2722 | 0 | const int adapt_upto = 32; |
2723 | 0 | const double min_second_ref_usage_thresh = 0.085; |
2724 | 0 | const double second_ref_usage_thresh_max_delta = 0.035; |
2725 | 0 | if (frame_count_so_far >= adapt_upto) { |
2726 | 0 | return min_second_ref_usage_thresh + second_ref_usage_thresh_max_delta; |
2727 | 0 | } |
2728 | 0 | return min_second_ref_usage_thresh + |
2729 | 0 | ((double)frame_count_so_far / (adapt_upto - 1)) * |
2730 | 0 | second_ref_usage_thresh_max_delta; |
2731 | 0 | } |
2732 | | |
2733 | | static int test_candidate_kf(const FIRSTPASS_INFO *firstpass_info, |
2734 | | int this_stats_index, int frame_count_so_far, |
2735 | | enum aom_rc_mode rc_mode, int scenecut_mode, |
2736 | 0 | int num_mbs) { |
2737 | 0 | const FIRSTPASS_STATS *last_stats = |
2738 | 0 | av1_firstpass_info_peek(firstpass_info, this_stats_index - 1); |
2739 | 0 | const FIRSTPASS_STATS *this_stats = |
2740 | 0 | av1_firstpass_info_peek(firstpass_info, this_stats_index); |
2741 | 0 | const FIRSTPASS_STATS *next_stats = |
2742 | 0 | av1_firstpass_info_peek(firstpass_info, this_stats_index + 1); |
2743 | 0 | if (last_stats == NULL || this_stats == NULL || next_stats == NULL) { |
2744 | 0 | return 0; |
2745 | 0 | } |
2746 | | |
2747 | 0 | int is_viable_kf = 0; |
2748 | 0 | double pcnt_intra = 1.0 - this_stats->pcnt_inter; |
2749 | 0 | double modified_pcnt_inter = |
2750 | 0 | this_stats->pcnt_inter - this_stats->pcnt_neutral; |
2751 | 0 | const double second_ref_usage_thresh = |
2752 | 0 | get_second_ref_usage_thresh(frame_count_so_far); |
2753 | 0 | int frames_to_test_after_candidate_key = SCENE_CUT_KEY_TEST_INTERVAL; |
2754 | 0 | int count_for_tolerable_prediction = 3; |
2755 | | |
2756 | | // We do "-1" because the candidate key is not counted. |
2757 | 0 | int stats_after_this_stats = |
2758 | 0 | av1_firstpass_info_future_count(firstpass_info, this_stats_index) - 1; |
2759 | |
|
2760 | 0 | if (scenecut_mode == ENABLE_SCENECUT_MODE_1) { |
2761 | 0 | if (stats_after_this_stats < 3) { |
2762 | 0 | return 0; |
2763 | 0 | } else { |
2764 | 0 | frames_to_test_after_candidate_key = 3; |
2765 | 0 | count_for_tolerable_prediction = 1; |
2766 | 0 | } |
2767 | 0 | } |
2768 | | // Make sure we have enough stats after the candidate key. |
2769 | 0 | frames_to_test_after_candidate_key = |
2770 | 0 | AOMMIN(frames_to_test_after_candidate_key, stats_after_this_stats); |
2771 | | |
2772 | | // Does the frame satisfy the primary criteria of a key frame? |
2773 | | // See above for an explanation of the test criteria. |
2774 | | // If so, then examine how well it predicts subsequent frames. |
2775 | 0 | if (IMPLIES(rc_mode == AOM_Q, frame_count_so_far >= 3) && |
2776 | 0 | (this_stats->pcnt_second_ref < second_ref_usage_thresh) && |
2777 | 0 | (next_stats->pcnt_second_ref < second_ref_usage_thresh) && |
2778 | 0 | ((this_stats->pcnt_inter < VERY_LOW_INTER_THRESH) || |
2779 | 0 | slide_transition(this_stats, last_stats, next_stats) || |
2780 | 0 | ((pcnt_intra > MIN_INTRA_LEVEL) && |
2781 | 0 | (pcnt_intra > (INTRA_VS_INTER_THRESH * modified_pcnt_inter)) && |
2782 | 0 | ((this_stats->intra_error / |
2783 | 0 | DOUBLE_DIVIDE_CHECK(this_stats->coded_error)) < |
2784 | 0 | KF_II_ERR_THRESHOLD) && |
2785 | 0 | ((fabs(last_stats->coded_error - this_stats->coded_error) / |
2786 | 0 | DOUBLE_DIVIDE_CHECK(this_stats->coded_error) > |
2787 | 0 | ERR_CHANGE_THRESHOLD) || |
2788 | 0 | (fabs(last_stats->intra_error - this_stats->intra_error) / |
2789 | 0 | DOUBLE_DIVIDE_CHECK(this_stats->intra_error) > |
2790 | 0 | ERR_CHANGE_THRESHOLD) || |
2791 | 0 | ((next_stats->intra_error / |
2792 | 0 | DOUBLE_DIVIDE_CHECK(next_stats->coded_error)) > |
2793 | 0 | II_IMPROVEMENT_THRESHOLD))))) { |
2794 | 0 | int i; |
2795 | 0 | double boost_score = 0.0; |
2796 | 0 | double old_boost_score = 0.0; |
2797 | 0 | double decay_accumulator = 1.0; |
2798 | | |
2799 | | // Examine how well the key frame predicts subsequent frames. |
2800 | 0 | for (i = 1; i <= frames_to_test_after_candidate_key; ++i) { |
2801 | | // Get the next frame details |
2802 | 0 | const FIRSTPASS_STATS *local_next_frame = |
2803 | 0 | av1_firstpass_info_peek(firstpass_info, this_stats_index + i); |
2804 | 0 | double next_iiratio = |
2805 | 0 | (BOOST_FACTOR * local_next_frame->intra_error / |
2806 | 0 | DOUBLE_DIVIDE_CHECK(local_next_frame->coded_error)); |
2807 | |
|
2808 | 0 | if (next_iiratio > KF_II_MAX) next_iiratio = KF_II_MAX; |
2809 | | |
2810 | | // Cumulative effect of decay in prediction quality. |
2811 | 0 | if (local_next_frame->pcnt_inter > 0.85) |
2812 | 0 | decay_accumulator *= local_next_frame->pcnt_inter; |
2813 | 0 | else |
2814 | 0 | decay_accumulator *= (0.85 + local_next_frame->pcnt_inter) / 2.0; |
2815 | | |
2816 | | // Keep a running total. |
2817 | 0 | boost_score += (decay_accumulator * next_iiratio); |
2818 | | |
2819 | | // Test various breakout clauses. |
2820 | | // TODO(any): Test of intra error should be normalized to an MB. |
2821 | 0 | if ((local_next_frame->pcnt_inter < 0.05) || (next_iiratio < 1.5) || |
2822 | 0 | (((local_next_frame->pcnt_inter - local_next_frame->pcnt_neutral) < |
2823 | 0 | 0.20) && |
2824 | 0 | (next_iiratio < 3.0)) || |
2825 | 0 | ((boost_score - old_boost_score) < 3.0) || |
2826 | 0 | (local_next_frame->intra_error < (200.0 / (double)num_mbs))) { |
2827 | 0 | break; |
2828 | 0 | } |
2829 | | |
2830 | 0 | old_boost_score = boost_score; |
2831 | 0 | } |
2832 | | |
2833 | | // If there is tolerable prediction for at least the next 3 frames then |
2834 | | // break out else discard this potential key frame and move on |
2835 | 0 | if (boost_score > 30.0 && (i > count_for_tolerable_prediction)) { |
2836 | 0 | is_viable_kf = 1; |
2837 | 0 | } else { |
2838 | 0 | is_viable_kf = 0; |
2839 | 0 | } |
2840 | 0 | } |
2841 | 0 | return is_viable_kf; |
2842 | 0 | } |
2843 | | |
2844 | 0 | #define FRAMES_TO_CHECK_DECAY 8 |
2845 | | #define KF_MIN_FRAME_BOOST 80.0 |
2846 | 0 | #define KF_MAX_FRAME_BOOST 128.0 |
2847 | | #define MIN_KF_BOOST 600 // Minimum boost for non-static KF interval |
2848 | | #define MAX_KF_BOOST 3200 |
2849 | | #define MIN_STATIC_KF_BOOST 5400 // Minimum boost for static KF interval |
2850 | | |
2851 | 1.26k | static int detect_app_forced_key(AV1_COMP *cpi) { |
2852 | 1.26k | int num_frames_to_app_forced_key = is_forced_keyframe_pending( |
2853 | 1.26k | cpi->ppi->lookahead, cpi->ppi->lookahead->max_sz, cpi->compressor_stage); |
2854 | 1.26k | return num_frames_to_app_forced_key; |
2855 | 1.26k | } |
2856 | | |
2857 | 0 | static int get_projected_kf_boost(AV1_COMP *cpi) { |
2858 | | /* |
2859 | | * If num_stats_used_for_kf_boost >= frames_to_key, then |
2860 | | * all stats needed for prior boost calculation are available. |
2861 | | * Hence projecting the prior boost is not needed in this cases. |
2862 | | */ |
2863 | 0 | if (cpi->ppi->p_rc.num_stats_used_for_kf_boost >= cpi->rc.frames_to_key) |
2864 | 0 | return cpi->ppi->p_rc.kf_boost; |
2865 | | |
2866 | | // Get the current tpl factor (number of frames = frames_to_key). |
2867 | 0 | double tpl_factor = av1_get_kf_boost_projection_factor(cpi->rc.frames_to_key); |
2868 | | // Get the tpl factor when number of frames = num_stats_used_for_kf_boost. |
2869 | 0 | double tpl_factor_num_stats = av1_get_kf_boost_projection_factor( |
2870 | 0 | cpi->ppi->p_rc.num_stats_used_for_kf_boost); |
2871 | 0 | int projected_kf_boost = |
2872 | 0 | (int)rint((tpl_factor * cpi->ppi->p_rc.kf_boost) / tpl_factor_num_stats); |
2873 | 0 | return projected_kf_boost; |
2874 | 0 | } |
2875 | | |
2876 | | /*!\brief Determine the location of the next key frame |
2877 | | * |
2878 | | * \ingroup gf_group_algo |
2879 | | * This function decides the placement of the next key frame when a |
2880 | | * scenecut is detected or the maximum key frame distance is reached. |
2881 | | * |
2882 | | * \param[in] cpi Top-level encoder structure |
2883 | | * \param[in] firstpass_info struct for firstpass info |
2884 | | * \param[in] num_frames_to_detect_scenecut Maximum lookahead frames. |
2885 | | * \param[in] search_start_idx the start index for searching key frame. |
2886 | | * Set it to one if we already know the |
2887 | | * current frame is key frame. Otherwise, |
2888 | | * set it to zero. |
2889 | | * |
2890 | | * \return Number of frames to the next key including the current frame. |
2891 | | */ |
2892 | | static int define_kf_interval(AV1_COMP *cpi, |
2893 | | const FIRSTPASS_INFO *firstpass_info, |
2894 | | int num_frames_to_detect_scenecut, |
2895 | 0 | int search_start_idx) { |
2896 | 0 | const TWO_PASS *const twopass = &cpi->ppi->twopass; |
2897 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
2898 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
2899 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2900 | 0 | const KeyFrameCfg *const kf_cfg = &oxcf->kf_cfg; |
2901 | 0 | double recent_loop_decay[FRAMES_TO_CHECK_DECAY]; |
2902 | 0 | double decay_accumulator = 1.0; |
2903 | 0 | int i = 0, j; |
2904 | 0 | int frames_to_key = search_start_idx; |
2905 | 0 | int frames_since_key = rc->frames_since_key + 1; |
2906 | 0 | int num_stats_used_for_kf_boost = 1; |
2907 | 0 | int scenecut_detected = 0; |
2908 | |
|
2909 | 0 | int num_frames_to_next_key = detect_app_forced_key(cpi); |
2910 | |
|
2911 | 0 | if (num_frames_to_detect_scenecut == 0) { |
2912 | 0 | if (num_frames_to_next_key != -1) |
2913 | 0 | return num_frames_to_next_key; |
2914 | 0 | else |
2915 | 0 | return rc->frames_to_key; |
2916 | 0 | } |
2917 | | |
2918 | 0 | if (num_frames_to_next_key != -1) |
2919 | 0 | num_frames_to_detect_scenecut = |
2920 | 0 | AOMMIN(num_frames_to_detect_scenecut, num_frames_to_next_key); |
2921 | | |
2922 | | // Initialize the decay rates for the recent frames to check |
2923 | 0 | for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j) recent_loop_decay[j] = 1.0; |
2924 | |
|
2925 | 0 | i = 0; |
2926 | 0 | const int num_mbs = (oxcf->resize_cfg.resize_mode != RESIZE_NONE) |
2927 | 0 | ? cpi->initial_mbs |
2928 | 0 | : cpi->common.mi_params.MBs; |
2929 | 0 | const int future_stats_count = |
2930 | 0 | av1_firstpass_info_future_count(firstpass_info, 0); |
2931 | 0 | while (frames_to_key < future_stats_count && |
2932 | 0 | frames_to_key < num_frames_to_detect_scenecut) { |
2933 | | // Accumulate total number of stats available till next key frame |
2934 | 0 | num_stats_used_for_kf_boost++; |
2935 | | |
2936 | | // Provided that we are not at the end of the file... |
2937 | 0 | if ((cpi->ppi->p_rc.enable_scenecut_detection > 0) && kf_cfg->auto_key && |
2938 | 0 | frames_to_key + 1 < future_stats_count) { |
2939 | 0 | double loop_decay_rate; |
2940 | | |
2941 | | // Check for a scene cut. |
2942 | 0 | if (frames_since_key >= kf_cfg->key_freq_min) { |
2943 | 0 | scenecut_detected = test_candidate_kf( |
2944 | 0 | &twopass->firstpass_info, frames_to_key, frames_since_key, |
2945 | 0 | oxcf->rc_cfg.mode, cpi->ppi->p_rc.enable_scenecut_detection, |
2946 | 0 | num_mbs); |
2947 | 0 | if (scenecut_detected) { |
2948 | 0 | break; |
2949 | 0 | } |
2950 | 0 | } |
2951 | | |
2952 | | // How fast is the prediction quality decaying? |
2953 | 0 | const FIRSTPASS_STATS *next_stats = |
2954 | 0 | av1_firstpass_info_peek(firstpass_info, frames_to_key + 1); |
2955 | 0 | loop_decay_rate = get_prediction_decay_rate(next_stats); |
2956 | | |
2957 | | // We want to know something about the recent past... rather than |
2958 | | // as used elsewhere where we are concerned with decay in prediction |
2959 | | // quality since the last GF or KF. |
2960 | 0 | recent_loop_decay[i % FRAMES_TO_CHECK_DECAY] = loop_decay_rate; |
2961 | 0 | decay_accumulator = 1.0; |
2962 | 0 | for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j) |
2963 | 0 | decay_accumulator *= recent_loop_decay[j]; |
2964 | | |
2965 | | // Special check for transition or high motion followed by a |
2966 | | // static scene. |
2967 | 0 | if (frames_since_key >= kf_cfg->key_freq_min) { |
2968 | 0 | scenecut_detected = detect_transition_to_still( |
2969 | 0 | firstpass_info, frames_to_key + 1, rc->min_gf_interval, i, |
2970 | 0 | kf_cfg->key_freq_max - i, loop_decay_rate, decay_accumulator); |
2971 | 0 | if (scenecut_detected) { |
2972 | | // In the case of transition followed by a static scene, the key frame |
2973 | | // could be a good predictor for the following frames, therefore we |
2974 | | // do not use an arf. |
2975 | 0 | p_rc->use_arf_in_this_kf_group = 0; |
2976 | 0 | break; |
2977 | 0 | } |
2978 | 0 | } |
2979 | | |
2980 | | // Step on to the next frame. |
2981 | 0 | ++frames_to_key; |
2982 | 0 | ++frames_since_key; |
2983 | | |
2984 | | // If we don't have a real key frame within the next two |
2985 | | // key_freq_max intervals then break out of the loop. |
2986 | 0 | if (frames_to_key >= 2 * kf_cfg->key_freq_max) { |
2987 | 0 | break; |
2988 | 0 | } |
2989 | 0 | } else { |
2990 | 0 | ++frames_to_key; |
2991 | 0 | ++frames_since_key; |
2992 | 0 | } |
2993 | 0 | ++i; |
2994 | 0 | } |
2995 | 0 | if (cpi->ppi->lap_enabled && !scenecut_detected) |
2996 | 0 | frames_to_key = num_frames_to_next_key; |
2997 | |
|
2998 | 0 | return frames_to_key; |
2999 | 0 | } |
3000 | | |
3001 | | static double get_kf_group_avg_error(TWO_PASS *twopass, |
3002 | | TWO_PASS_FRAME *twopass_frame, |
3003 | | const FIRSTPASS_STATS *first_frame, |
3004 | | const FIRSTPASS_STATS *start_position, |
3005 | 0 | int frames_to_key) { |
3006 | 0 | FIRSTPASS_STATS cur_frame = *first_frame; |
3007 | 0 | int num_frames, i; |
3008 | 0 | double kf_group_avg_error = 0.0; |
3009 | |
|
3010 | 0 | reset_fpf_position(twopass_frame, start_position); |
3011 | |
|
3012 | 0 | for (i = 0; i < frames_to_key; ++i) { |
3013 | 0 | kf_group_avg_error += cur_frame.coded_error; |
3014 | 0 | if (EOF == input_stats(twopass, twopass_frame, &cur_frame)) break; |
3015 | 0 | } |
3016 | 0 | num_frames = i + 1; |
3017 | 0 | num_frames = AOMMIN(num_frames, frames_to_key); |
3018 | 0 | kf_group_avg_error = kf_group_avg_error / num_frames; |
3019 | |
|
3020 | 0 | return (kf_group_avg_error); |
3021 | 0 | } |
3022 | | |
3023 | | static int64_t get_kf_group_bits(AV1_COMP *cpi, double kf_group_err, |
3024 | 0 | double kf_group_avg_error) { |
3025 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
3026 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3027 | 0 | int64_t kf_group_bits; |
3028 | 0 | if (cpi->ppi->lap_enabled) { |
3029 | 0 | kf_group_bits = (int64_t)rc->frames_to_key * rc->avg_frame_bandwidth; |
3030 | 0 | if (cpi->oxcf.rc_cfg.vbr_corpus_complexity_lap) { |
3031 | 0 | double vbr_corpus_complexity_lap = |
3032 | 0 | cpi->oxcf.rc_cfg.vbr_corpus_complexity_lap / 10.0; |
3033 | | /* Get the average corpus complexity of the frame */ |
3034 | 0 | kf_group_bits = (int64_t)( |
3035 | 0 | kf_group_bits * (kf_group_avg_error / vbr_corpus_complexity_lap)); |
3036 | 0 | } |
3037 | 0 | } else { |
3038 | 0 | kf_group_bits = (int64_t)(twopass->bits_left * |
3039 | 0 | (kf_group_err / twopass->modified_error_left)); |
3040 | 0 | } |
3041 | |
|
3042 | 0 | return kf_group_bits; |
3043 | 0 | } |
3044 | | |
3045 | 0 | static int calc_avg_stats(AV1_COMP *cpi, FIRSTPASS_STATS *avg_frame_stat) { |
3046 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
3047 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3048 | 0 | FIRSTPASS_STATS cur_frame; |
3049 | 0 | av1_zero(cur_frame); |
3050 | 0 | int num_frames = 0; |
3051 | | // Accumulate total stat using available number of stats. |
3052 | 0 | for (num_frames = 0; num_frames < (rc->frames_to_key - 1); ++num_frames) { |
3053 | 0 | if (EOF == input_stats(twopass, &cpi->twopass_frame, &cur_frame)) break; |
3054 | 0 | av1_accumulate_stats(avg_frame_stat, &cur_frame); |
3055 | 0 | } |
3056 | |
|
3057 | 0 | if (num_frames < 2) { |
3058 | 0 | return num_frames; |
3059 | 0 | } |
3060 | | // Average the total stat |
3061 | 0 | avg_frame_stat->weight = avg_frame_stat->weight / num_frames; |
3062 | 0 | avg_frame_stat->intra_error = avg_frame_stat->intra_error / num_frames; |
3063 | 0 | avg_frame_stat->frame_avg_wavelet_energy = |
3064 | 0 | avg_frame_stat->frame_avg_wavelet_energy / num_frames; |
3065 | 0 | avg_frame_stat->coded_error = avg_frame_stat->coded_error / num_frames; |
3066 | 0 | avg_frame_stat->sr_coded_error = avg_frame_stat->sr_coded_error / num_frames; |
3067 | 0 | avg_frame_stat->pcnt_inter = avg_frame_stat->pcnt_inter / num_frames; |
3068 | 0 | avg_frame_stat->pcnt_motion = avg_frame_stat->pcnt_motion / num_frames; |
3069 | 0 | avg_frame_stat->pcnt_second_ref = |
3070 | 0 | avg_frame_stat->pcnt_second_ref / num_frames; |
3071 | 0 | avg_frame_stat->pcnt_neutral = avg_frame_stat->pcnt_neutral / num_frames; |
3072 | 0 | avg_frame_stat->intra_skip_pct = avg_frame_stat->intra_skip_pct / num_frames; |
3073 | 0 | avg_frame_stat->inactive_zone_rows = |
3074 | 0 | avg_frame_stat->inactive_zone_rows / num_frames; |
3075 | 0 | avg_frame_stat->inactive_zone_cols = |
3076 | 0 | avg_frame_stat->inactive_zone_cols / num_frames; |
3077 | 0 | avg_frame_stat->MVr = avg_frame_stat->MVr / num_frames; |
3078 | 0 | avg_frame_stat->mvr_abs = avg_frame_stat->mvr_abs / num_frames; |
3079 | 0 | avg_frame_stat->MVc = avg_frame_stat->MVc / num_frames; |
3080 | 0 | avg_frame_stat->mvc_abs = avg_frame_stat->mvc_abs / num_frames; |
3081 | 0 | avg_frame_stat->MVrv = avg_frame_stat->MVrv / num_frames; |
3082 | 0 | avg_frame_stat->MVcv = avg_frame_stat->MVcv / num_frames; |
3083 | 0 | avg_frame_stat->mv_in_out_count = |
3084 | 0 | avg_frame_stat->mv_in_out_count / num_frames; |
3085 | 0 | avg_frame_stat->new_mv_count = avg_frame_stat->new_mv_count / num_frames; |
3086 | 0 | avg_frame_stat->count = avg_frame_stat->count / num_frames; |
3087 | 0 | avg_frame_stat->duration = avg_frame_stat->duration / num_frames; |
3088 | |
|
3089 | 0 | return num_frames; |
3090 | 0 | } |
3091 | | |
3092 | | static double get_kf_boost_score(AV1_COMP *cpi, double kf_raw_err, |
3093 | | double *zero_motion_accumulator, |
3094 | 0 | double *sr_accumulator, int use_avg_stat) { |
3095 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
3096 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3097 | 0 | FRAME_INFO *const frame_info = &cpi->frame_info; |
3098 | 0 | FIRSTPASS_STATS frame_stat; |
3099 | 0 | av1_zero(frame_stat); |
3100 | 0 | int i = 0, num_stat_used = 0; |
3101 | 0 | double boost_score = 0.0; |
3102 | 0 | const double kf_max_boost = |
3103 | 0 | cpi->oxcf.rc_cfg.mode == AOM_Q |
3104 | 0 | ? AOMMIN(AOMMAX(rc->frames_to_key * 2.0, KF_MIN_FRAME_BOOST), |
3105 | 0 | KF_MAX_FRAME_BOOST) |
3106 | 0 | : KF_MAX_FRAME_BOOST; |
3107 | | |
3108 | | // Calculate the average using available number of stats. |
3109 | 0 | if (use_avg_stat) num_stat_used = calc_avg_stats(cpi, &frame_stat); |
3110 | |
|
3111 | 0 | for (i = num_stat_used; i < (rc->frames_to_key - 1); ++i) { |
3112 | 0 | if (!use_avg_stat && |
3113 | 0 | EOF == input_stats(twopass, &cpi->twopass_frame, &frame_stat)) |
3114 | 0 | break; |
3115 | | |
3116 | | // Monitor for static sections. |
3117 | | // For the first frame in kf group, the second ref indicator is invalid. |
3118 | 0 | if (i > 0) { |
3119 | 0 | *zero_motion_accumulator = |
3120 | 0 | AOMMIN(*zero_motion_accumulator, get_zero_motion_factor(&frame_stat)); |
3121 | 0 | } else { |
3122 | 0 | *zero_motion_accumulator = frame_stat.pcnt_inter - frame_stat.pcnt_motion; |
3123 | 0 | } |
3124 | | |
3125 | | // Not all frames in the group are necessarily used in calculating boost. |
3126 | 0 | if ((*sr_accumulator < (kf_raw_err * 1.50)) && |
3127 | 0 | (i <= rc->max_gf_interval * 2)) { |
3128 | 0 | double frame_boost; |
3129 | 0 | double zm_factor; |
3130 | | |
3131 | | // Factor 0.75-1.25 based on how much of frame is static. |
3132 | 0 | zm_factor = (0.75 + (*zero_motion_accumulator / 2.0)); |
3133 | |
|
3134 | 0 | if (i < 2) *sr_accumulator = 0.0; |
3135 | 0 | frame_boost = |
3136 | 0 | calc_kf_frame_boost(&cpi->ppi->p_rc, frame_info, &frame_stat, |
3137 | 0 | sr_accumulator, kf_max_boost); |
3138 | 0 | boost_score += frame_boost * zm_factor; |
3139 | 0 | } |
3140 | 0 | } |
3141 | 0 | return boost_score; |
3142 | 0 | } |
3143 | | |
3144 | | /*!\brief Interval(in seconds) to clip key-frame distance to in LAP. |
3145 | | */ |
3146 | 0 | #define MAX_KF_BITS_INTERVAL_SINGLE_PASS 5 |
3147 | | |
3148 | | /*!\brief Determine the next key frame group |
3149 | | * |
3150 | | * \ingroup gf_group_algo |
3151 | | * This function decides the placement of the next key frame, and |
3152 | | * calculates the bit allocation of the KF group and the keyframe itself. |
3153 | | * |
3154 | | * \param[in] cpi Top-level encoder structure |
3155 | | * \param[in] this_frame Pointer to first pass stats |
3156 | | * |
3157 | | * \return Nothing is returned. |
3158 | | */ |
3159 | 1.26k | static void find_next_key_frame(AV1_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
3160 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
3161 | 1.26k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
3162 | 1.26k | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3163 | 1.26k | GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
3164 | 1.26k | FRAME_INFO *const frame_info = &cpi->frame_info; |
3165 | 1.26k | AV1_COMMON *const cm = &cpi->common; |
3166 | 1.26k | CurrentFrame *const current_frame = &cm->current_frame; |
3167 | 1.26k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
3168 | 1.26k | const KeyFrameCfg *const kf_cfg = &oxcf->kf_cfg; |
3169 | 1.26k | const FIRSTPASS_STATS first_frame = *this_frame; |
3170 | 1.26k | FIRSTPASS_STATS next_frame; |
3171 | 1.26k | const FIRSTPASS_INFO *firstpass_info = &twopass->firstpass_info; |
3172 | 1.26k | av1_zero(next_frame); |
3173 | | |
3174 | 1.26k | rc->frames_since_key = 0; |
3175 | | // Use arfs if possible. |
3176 | 1.26k | p_rc->use_arf_in_this_kf_group = is_altref_enabled( |
3177 | 1.26k | oxcf->gf_cfg.lag_in_frames, oxcf->gf_cfg.enable_auto_arf); |
3178 | | |
3179 | | // Reset the GF group data structures. |
3180 | 1.26k | av1_zero(*gf_group); |
3181 | 1.26k | cpi->gf_frame_index = 0; |
3182 | | |
3183 | | // KF is always a GF so clear frames till next gf counter. |
3184 | 1.26k | rc->frames_till_gf_update_due = 0; |
3185 | | |
3186 | 1.26k | if (has_no_stats_stage(cpi)) { |
3187 | 1.26k | int num_frames_to_app_forced_key = detect_app_forced_key(cpi); |
3188 | 1.26k | p_rc->this_key_frame_forced = |
3189 | 1.26k | current_frame->frame_number != 0 && rc->frames_to_key == 0; |
3190 | 1.26k | if (num_frames_to_app_forced_key != -1) |
3191 | 0 | rc->frames_to_key = num_frames_to_app_forced_key; |
3192 | 1.26k | else |
3193 | 1.26k | rc->frames_to_key = AOMMAX(1, kf_cfg->key_freq_max); |
3194 | 1.26k | correct_frames_to_key(cpi); |
3195 | 1.26k | p_rc->kf_boost = DEFAULT_KF_BOOST; |
3196 | 1.26k | gf_group->update_type[0] = KF_UPDATE; |
3197 | 1.26k | return; |
3198 | 1.26k | } |
3199 | 0 | int i; |
3200 | 0 | const FIRSTPASS_STATS *const start_position = cpi->twopass_frame.stats_in; |
3201 | 0 | int kf_bits = 0; |
3202 | 0 | double zero_motion_accumulator = 1.0; |
3203 | 0 | double boost_score = 0.0; |
3204 | 0 | double kf_raw_err = 0.0; |
3205 | 0 | double kf_mod_err = 0.0; |
3206 | 0 | double sr_accumulator = 0.0; |
3207 | 0 | double kf_group_avg_error = 0.0; |
3208 | 0 | int frames_to_key, frames_to_key_clipped = INT_MAX; |
3209 | 0 | int64_t kf_group_bits_clipped = INT64_MAX; |
3210 | | |
3211 | | // Is this a forced key frame by interval. |
3212 | 0 | p_rc->this_key_frame_forced = p_rc->next_key_frame_forced; |
3213 | |
|
3214 | 0 | twopass->kf_group_bits = 0; // Total bits available to kf group |
3215 | 0 | twopass->kf_group_error_left = 0; // Group modified error score. |
3216 | |
|
3217 | 0 | kf_raw_err = this_frame->intra_error; |
3218 | 0 | kf_mod_err = calculate_modified_err(frame_info, twopass, oxcf, this_frame); |
3219 | | |
3220 | | // We assume the current frame is a key frame and we are looking for the next |
3221 | | // key frame. Therefore search_start_idx = 1 |
3222 | 0 | frames_to_key = define_kf_interval(cpi, firstpass_info, kf_cfg->key_freq_max, |
3223 | 0 | /*search_start_idx=*/1); |
3224 | |
|
3225 | 0 | if (frames_to_key != -1) { |
3226 | 0 | rc->frames_to_key = AOMMIN(kf_cfg->key_freq_max, frames_to_key); |
3227 | 0 | } else { |
3228 | 0 | rc->frames_to_key = kf_cfg->key_freq_max; |
3229 | 0 | } |
3230 | |
|
3231 | 0 | rc->frames_to_fwd_kf = kf_cfg->fwd_kf_dist; |
3232 | |
|
3233 | 0 | if (cpi->ppi->lap_enabled) correct_frames_to_key(cpi); |
3234 | | |
3235 | | // If there is a max kf interval set by the user we must obey it. |
3236 | | // We already breakout of the loop above at 2x max. |
3237 | | // This code centers the extra kf if the actual natural interval |
3238 | | // is between 1x and 2x. |
3239 | 0 | if (kf_cfg->auto_key && rc->frames_to_key > kf_cfg->key_freq_max) { |
3240 | 0 | FIRSTPASS_STATS tmp_frame = first_frame; |
3241 | |
|
3242 | 0 | rc->frames_to_key /= 2; |
3243 | | |
3244 | | // Reset to the start of the group. |
3245 | 0 | reset_fpf_position(&cpi->twopass_frame, start_position); |
3246 | | // Rescan to get the correct error data for the forced kf group. |
3247 | 0 | for (i = 0; i < rc->frames_to_key; ++i) { |
3248 | 0 | if (EOF == input_stats(twopass, &cpi->twopass_frame, &tmp_frame)) break; |
3249 | 0 | } |
3250 | 0 | p_rc->next_key_frame_forced = 1; |
3251 | 0 | } else if ((cpi->twopass_frame.stats_in == |
3252 | 0 | twopass->stats_buf_ctx->stats_in_end && |
3253 | 0 | is_stat_consumption_stage_twopass(cpi)) || |
3254 | 0 | rc->frames_to_key >= kf_cfg->key_freq_max) { |
3255 | 0 | p_rc->next_key_frame_forced = 1; |
3256 | 0 | } else { |
3257 | 0 | p_rc->next_key_frame_forced = 0; |
3258 | 0 | } |
3259 | |
|
3260 | 0 | double kf_group_err = 0; |
3261 | 0 | for (i = 0; i < rc->frames_to_key; ++i) { |
3262 | 0 | const FIRSTPASS_STATS *this_stats = |
3263 | 0 | av1_firstpass_info_peek(&twopass->firstpass_info, i); |
3264 | 0 | if (this_stats != NULL) { |
3265 | | // Accumulate kf group error. |
3266 | 0 | kf_group_err += calculate_modified_err_new( |
3267 | 0 | frame_info, &firstpass_info->total_stats, this_stats, |
3268 | 0 | oxcf->rc_cfg.vbrbias, twopass->modified_error_min, |
3269 | 0 | twopass->modified_error_max); |
3270 | 0 | ++p_rc->num_stats_used_for_kf_boost; |
3271 | 0 | } |
3272 | 0 | } |
3273 | | |
3274 | | // Calculate the number of bits that should be assigned to the kf group. |
3275 | 0 | if ((twopass->bits_left > 0 && twopass->modified_error_left > 0.0) || |
3276 | 0 | (cpi->ppi->lap_enabled && oxcf->rc_cfg.mode != AOM_Q)) { |
3277 | | // Maximum number of bits for a single normal frame (not key frame). |
3278 | 0 | const int max_bits = frame_max_bits(rc, oxcf); |
3279 | | |
3280 | | // Maximum number of bits allocated to the key frame group. |
3281 | 0 | int64_t max_grp_bits; |
3282 | |
|
3283 | 0 | if (oxcf->rc_cfg.vbr_corpus_complexity_lap) { |
3284 | 0 | kf_group_avg_error = |
3285 | 0 | get_kf_group_avg_error(twopass, &cpi->twopass_frame, &first_frame, |
3286 | 0 | start_position, rc->frames_to_key); |
3287 | 0 | } |
3288 | | |
3289 | | // Default allocation based on bits left and relative |
3290 | | // complexity of the section. |
3291 | 0 | twopass->kf_group_bits = |
3292 | 0 | get_kf_group_bits(cpi, kf_group_err, kf_group_avg_error); |
3293 | | // Clip based on maximum per frame rate defined by the user. |
3294 | 0 | max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; |
3295 | 0 | if (twopass->kf_group_bits > max_grp_bits) |
3296 | 0 | twopass->kf_group_bits = max_grp_bits; |
3297 | 0 | } else { |
3298 | 0 | twopass->kf_group_bits = 0; |
3299 | 0 | } |
3300 | 0 | twopass->kf_group_bits = AOMMAX(0, twopass->kf_group_bits); |
3301 | |
|
3302 | 0 | if (cpi->ppi->lap_enabled) { |
3303 | | // In the case of single pass based on LAP, frames to key may have an |
3304 | | // inaccurate value, and hence should be clipped to an appropriate |
3305 | | // interval. |
3306 | 0 | frames_to_key_clipped = |
3307 | 0 | (int)(MAX_KF_BITS_INTERVAL_SINGLE_PASS * cpi->framerate); |
3308 | | |
3309 | | // This variable calculates the bits allocated to kf_group with a clipped |
3310 | | // frames_to_key. |
3311 | 0 | if (rc->frames_to_key > frames_to_key_clipped) { |
3312 | 0 | kf_group_bits_clipped = |
3313 | 0 | (int64_t)((double)twopass->kf_group_bits * frames_to_key_clipped / |
3314 | 0 | rc->frames_to_key); |
3315 | 0 | } |
3316 | 0 | } |
3317 | | |
3318 | | // Reset the first pass file position. |
3319 | 0 | reset_fpf_position(&cpi->twopass_frame, start_position); |
3320 | | |
3321 | | // Scan through the kf group collating various stats used to determine |
3322 | | // how many bits to spend on it. |
3323 | 0 | boost_score = get_kf_boost_score(cpi, kf_raw_err, &zero_motion_accumulator, |
3324 | 0 | &sr_accumulator, 0); |
3325 | 0 | reset_fpf_position(&cpi->twopass_frame, start_position); |
3326 | | // Store the zero motion percentage |
3327 | 0 | twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
3328 | | |
3329 | | // Calculate a section intra ratio used in setting max loop filter. |
3330 | 0 | twopass->section_intra_rating = calculate_section_intra_ratio( |
3331 | 0 | start_position, twopass->stats_buf_ctx->stats_in_end, rc->frames_to_key); |
3332 | |
|
3333 | 0 | p_rc->kf_boost = (int)boost_score; |
3334 | |
|
3335 | 0 | if (cpi->ppi->lap_enabled) { |
3336 | 0 | if (oxcf->rc_cfg.mode == AOM_Q) { |
3337 | 0 | p_rc->kf_boost = get_projected_kf_boost(cpi); |
3338 | 0 | } else { |
3339 | | // TODO(any): Explore using average frame stats for AOM_Q as well. |
3340 | 0 | boost_score = get_kf_boost_score( |
3341 | 0 | cpi, kf_raw_err, &zero_motion_accumulator, &sr_accumulator, 1); |
3342 | 0 | reset_fpf_position(&cpi->twopass_frame, start_position); |
3343 | 0 | p_rc->kf_boost += (int)boost_score; |
3344 | 0 | } |
3345 | 0 | } |
3346 | | |
3347 | | // Special case for static / slide show content but don't apply |
3348 | | // if the kf group is very short. |
3349 | 0 | if ((zero_motion_accumulator > STATIC_KF_GROUP_FLOAT_THRESH) && |
3350 | 0 | (rc->frames_to_key > 8)) { |
3351 | 0 | p_rc->kf_boost = AOMMAX(p_rc->kf_boost, MIN_STATIC_KF_BOOST); |
3352 | 0 | } else { |
3353 | | // Apply various clamps for min and max boost |
3354 | 0 | p_rc->kf_boost = AOMMAX(p_rc->kf_boost, (rc->frames_to_key * 3)); |
3355 | 0 | p_rc->kf_boost = AOMMAX(p_rc->kf_boost, MIN_KF_BOOST); |
3356 | | #ifdef STRICT_RC |
3357 | | p_rc->kf_boost = AOMMIN(p_rc->kf_boost, MAX_KF_BOOST); |
3358 | | #endif |
3359 | 0 | } |
3360 | | |
3361 | | // Work out how many bits to allocate for the key frame itself. |
3362 | | // In case of LAP enabled for VBR, if the frames_to_key value is |
3363 | | // very high, we calculate the bits based on a clipped value of |
3364 | | // frames_to_key. |
3365 | 0 | kf_bits = calculate_boost_bits( |
3366 | 0 | AOMMIN(rc->frames_to_key, frames_to_key_clipped) - 1, p_rc->kf_boost, |
3367 | 0 | AOMMIN(twopass->kf_group_bits, kf_group_bits_clipped)); |
3368 | | // printf("kf boost = %d kf_bits = %d kf_zeromotion_pct = %d\n", |
3369 | | // p_rc->kf_boost, |
3370 | | // kf_bits, twopass->kf_zeromotion_pct); |
3371 | 0 | kf_bits = adjust_boost_bits_for_target_level(cpi, rc, kf_bits, |
3372 | 0 | twopass->kf_group_bits, 0); |
3373 | |
|
3374 | 0 | twopass->kf_group_bits -= kf_bits; |
3375 | | |
3376 | | // Save the bits to spend on the key frame. |
3377 | 0 | gf_group->bit_allocation[0] = kf_bits; |
3378 | 0 | gf_group->update_type[0] = KF_UPDATE; |
3379 | | |
3380 | | // Note the total error score of the kf group minus the key frame itself. |
3381 | 0 | if (cpi->ppi->lap_enabled) |
3382 | | // As we don't have enough stats to know the actual error of the group, |
3383 | | // we assume the complexity of each frame to be equal to 1, and set the |
3384 | | // error as the number of frames in the group(minus the keyframe). |
3385 | 0 | twopass->kf_group_error_left = (double)(rc->frames_to_key - 1); |
3386 | 0 | else |
3387 | 0 | twopass->kf_group_error_left = kf_group_err - kf_mod_err; |
3388 | | |
3389 | | // Adjust the count of total modified error left. |
3390 | | // The count of bits left is adjusted elsewhere based on real coded frame |
3391 | | // sizes. |
3392 | 0 | twopass->modified_error_left -= kf_group_err; |
3393 | 0 | } |
3394 | | |
3395 | | #define ARF_STATS_OUTPUT 0 |
3396 | | #if ARF_STATS_OUTPUT |
3397 | | unsigned int arf_count = 0; |
3398 | | #endif |
3399 | | |
3400 | 0 | static int get_section_target_bandwidth(AV1_COMP *cpi) { |
3401 | 0 | AV1_COMMON *const cm = &cpi->common; |
3402 | 0 | CurrentFrame *const current_frame = &cm->current_frame; |
3403 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
3404 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3405 | 0 | int section_target_bandwidth; |
3406 | 0 | const int frames_left = (int)(twopass->stats_buf_ctx->total_stats->count - |
3407 | 0 | current_frame->frame_number); |
3408 | 0 | if (cpi->ppi->lap_enabled) |
3409 | 0 | section_target_bandwidth = (int)rc->avg_frame_bandwidth; |
3410 | 0 | else |
3411 | 0 | section_target_bandwidth = (int)(twopass->bits_left / frames_left); |
3412 | 0 | return section_target_bandwidth; |
3413 | 0 | } |
3414 | | |
3415 | | static INLINE void set_twopass_params_based_on_fp_stats( |
3416 | 0 | AV1_COMP *cpi, const FIRSTPASS_STATS *this_frame_ptr) { |
3417 | 0 | if (this_frame_ptr == NULL) return; |
3418 | | |
3419 | 0 | TWO_PASS_FRAME *twopass_frame = &cpi->twopass_frame; |
3420 | | // The multiplication by 256 reverses a scaling factor of (>> 8) |
3421 | | // applied when combining MB error values for the frame. |
3422 | 0 | twopass_frame->mb_av_energy = log((this_frame_ptr->intra_error) + 1.0); |
3423 | |
|
3424 | 0 | const FIRSTPASS_STATS *const total_stats = |
3425 | 0 | cpi->ppi->twopass.stats_buf_ctx->total_stats; |
3426 | 0 | if (is_fp_wavelet_energy_invalid(total_stats) == 0) { |
3427 | 0 | twopass_frame->frame_avg_haar_energy = |
3428 | 0 | log((this_frame_ptr->frame_avg_wavelet_energy) + 1.0); |
3429 | 0 | } |
3430 | | |
3431 | | // Set the frame content type flag. |
3432 | 0 | if (this_frame_ptr->intra_skip_pct >= FC_ANIMATION_THRESH) |
3433 | 0 | twopass_frame->fr_content_type = FC_GRAPHICS_ANIMATION; |
3434 | 0 | else |
3435 | 0 | twopass_frame->fr_content_type = FC_NORMAL; |
3436 | 0 | } |
3437 | | |
3438 | | static void process_first_pass_stats(AV1_COMP *cpi, |
3439 | 0 | FIRSTPASS_STATS *this_frame) { |
3440 | 0 | AV1_COMMON *const cm = &cpi->common; |
3441 | 0 | CurrentFrame *const current_frame = &cm->current_frame; |
3442 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
3443 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
3444 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3445 | 0 | FIRSTPASS_STATS *total_stats = twopass->stats_buf_ctx->total_stats; |
3446 | |
|
3447 | 0 | if (cpi->oxcf.rc_cfg.mode != AOM_Q && current_frame->frame_number == 0 && |
3448 | 0 | cpi->gf_frame_index == 0 && total_stats && |
3449 | 0 | cpi->ppi->twopass.stats_buf_ctx->total_left_stats) { |
3450 | 0 | if (cpi->ppi->lap_enabled) { |
3451 | | /* |
3452 | | * Accumulate total_stats using available limited number of stats, |
3453 | | * and assign it to total_left_stats. |
3454 | | */ |
3455 | 0 | *cpi->ppi->twopass.stats_buf_ctx->total_left_stats = *total_stats; |
3456 | 0 | } |
3457 | | // Special case code for first frame. |
3458 | 0 | const int section_target_bandwidth = get_section_target_bandwidth(cpi); |
3459 | 0 | const double section_length = |
3460 | 0 | twopass->stats_buf_ctx->total_left_stats->count; |
3461 | 0 | const double section_error = |
3462 | 0 | twopass->stats_buf_ctx->total_left_stats->coded_error / section_length; |
3463 | 0 | const double section_intra_skip = |
3464 | 0 | twopass->stats_buf_ctx->total_left_stats->intra_skip_pct / |
3465 | 0 | section_length; |
3466 | 0 | const double section_inactive_zone = |
3467 | 0 | (twopass->stats_buf_ctx->total_left_stats->inactive_zone_rows * 2) / |
3468 | 0 | ((double)cm->mi_params.mb_rows * section_length); |
3469 | 0 | const int tmp_q = get_twopass_worst_quality( |
3470 | 0 | cpi, section_error, section_intra_skip + section_inactive_zone, |
3471 | 0 | section_target_bandwidth); |
3472 | |
|
3473 | 0 | rc->active_worst_quality = tmp_q; |
3474 | 0 | rc->ni_av_qi = tmp_q; |
3475 | 0 | p_rc->last_q[INTER_FRAME] = tmp_q; |
3476 | 0 | p_rc->avg_q = av1_convert_qindex_to_q(tmp_q, cm->seq_params->bit_depth); |
3477 | 0 | p_rc->avg_frame_qindex[INTER_FRAME] = tmp_q; |
3478 | 0 | p_rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.rc_cfg.best_allowed_q) / 2; |
3479 | 0 | p_rc->avg_frame_qindex[KEY_FRAME] = p_rc->last_q[KEY_FRAME]; |
3480 | 0 | } |
3481 | |
|
3482 | 0 | if (cpi->twopass_frame.stats_in < |
3483 | 0 | cpi->ppi->twopass.stats_buf_ctx->stats_in_end) { |
3484 | 0 | *this_frame = *cpi->twopass_frame.stats_in; |
3485 | 0 | ++cpi->twopass_frame.stats_in; |
3486 | 0 | } |
3487 | 0 | set_twopass_params_based_on_fp_stats(cpi, this_frame); |
3488 | 0 | } |
3489 | | |
3490 | 1.26k | static void setup_target_rate(AV1_COMP *cpi) { |
3491 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
3492 | 1.26k | GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
3493 | | |
3494 | 1.26k | int target_rate = gf_group->bit_allocation[cpi->gf_frame_index]; |
3495 | | |
3496 | 1.26k | if (has_no_stats_stage(cpi)) { |
3497 | 1.26k | av1_rc_set_frame_target(cpi, target_rate, cpi->common.width, |
3498 | 1.26k | cpi->common.height); |
3499 | 1.26k | } |
3500 | | |
3501 | 1.26k | rc->base_frame_target = target_rate; |
3502 | 1.26k | } |
3503 | | |
3504 | | static void mark_flashes(FIRSTPASS_STATS *first_stats, |
3505 | 0 | FIRSTPASS_STATS *last_stats) { |
3506 | 0 | FIRSTPASS_STATS *this_stats = first_stats, *next_stats; |
3507 | 0 | while (this_stats < last_stats - 1) { |
3508 | 0 | next_stats = this_stats + 1; |
3509 | 0 | if (next_stats->pcnt_second_ref > next_stats->pcnt_inter && |
3510 | 0 | next_stats->pcnt_second_ref >= 0.5) { |
3511 | 0 | this_stats->is_flash = 1; |
3512 | 0 | } else { |
3513 | 0 | this_stats->is_flash = 0; |
3514 | 0 | } |
3515 | 0 | this_stats = next_stats; |
3516 | 0 | } |
3517 | | // We always treat the last one as none flash. |
3518 | 0 | if (last_stats - 1 >= first_stats) { |
3519 | 0 | (last_stats - 1)->is_flash = 0; |
3520 | 0 | } |
3521 | 0 | } |
3522 | | |
3523 | | // Estimate the noise variance of each frame from the first pass stats |
3524 | | static void estimate_noise(FIRSTPASS_STATS *first_stats, |
3525 | 0 | FIRSTPASS_STATS *last_stats) { |
3526 | 0 | FIRSTPASS_STATS *this_stats, *next_stats; |
3527 | 0 | double C1, C2, C3, noise; |
3528 | 0 | int count = 0; |
3529 | 0 | for (this_stats = first_stats + 2; this_stats < last_stats; this_stats++) { |
3530 | 0 | this_stats->noise_var = 0.0; |
3531 | | // flashes tend to have high correlation of innovations, so ignore them. |
3532 | 0 | if (this_stats->is_flash || (this_stats - 1)->is_flash || |
3533 | 0 | (this_stats - 2)->is_flash) |
3534 | 0 | continue; |
3535 | | |
3536 | 0 | C1 = (this_stats - 1)->intra_error * |
3537 | 0 | (this_stats->intra_error - this_stats->coded_error); |
3538 | 0 | C2 = (this_stats - 2)->intra_error * |
3539 | 0 | ((this_stats - 1)->intra_error - (this_stats - 1)->coded_error); |
3540 | 0 | C3 = (this_stats - 2)->intra_error * |
3541 | 0 | (this_stats->intra_error - this_stats->sr_coded_error); |
3542 | 0 | if (C1 <= 0 || C2 <= 0 || C3 <= 0) continue; |
3543 | 0 | C1 = sqrt(C1); |
3544 | 0 | C2 = sqrt(C2); |
3545 | 0 | C3 = sqrt(C3); |
3546 | |
|
3547 | 0 | noise = (this_stats - 1)->intra_error - C1 * C2 / C3; |
3548 | 0 | noise = AOMMAX(noise, 0.01); |
3549 | 0 | this_stats->noise_var = noise; |
3550 | 0 | count++; |
3551 | 0 | } |
3552 | | |
3553 | | // Copy noise from the neighbor if the noise value is not trustworthy |
3554 | 0 | for (this_stats = first_stats + 2; this_stats < last_stats; this_stats++) { |
3555 | 0 | if (this_stats->is_flash || (this_stats - 1)->is_flash || |
3556 | 0 | (this_stats - 2)->is_flash) |
3557 | 0 | continue; |
3558 | 0 | if (this_stats->noise_var < 1.0) { |
3559 | 0 | int found = 0; |
3560 | | // TODO(bohanli): consider expanding to two directions at the same time |
3561 | 0 | for (next_stats = this_stats + 1; next_stats < last_stats; next_stats++) { |
3562 | 0 | if (next_stats->is_flash || (next_stats - 1)->is_flash || |
3563 | 0 | (next_stats - 2)->is_flash || next_stats->noise_var < 1.0) |
3564 | 0 | continue; |
3565 | 0 | found = 1; |
3566 | 0 | this_stats->noise_var = next_stats->noise_var; |
3567 | 0 | break; |
3568 | 0 | } |
3569 | 0 | if (found) continue; |
3570 | 0 | for (next_stats = this_stats - 1; next_stats >= first_stats + 2; |
3571 | 0 | next_stats--) { |
3572 | 0 | if (next_stats->is_flash || (next_stats - 1)->is_flash || |
3573 | 0 | (next_stats - 2)->is_flash || next_stats->noise_var < 1.0) |
3574 | 0 | continue; |
3575 | 0 | this_stats->noise_var = next_stats->noise_var; |
3576 | 0 | break; |
3577 | 0 | } |
3578 | 0 | } |
3579 | 0 | } |
3580 | | |
3581 | | // copy the noise if this is a flash |
3582 | 0 | for (this_stats = first_stats + 2; this_stats < last_stats; this_stats++) { |
3583 | 0 | if (this_stats->is_flash || (this_stats - 1)->is_flash || |
3584 | 0 | (this_stats - 2)->is_flash) { |
3585 | 0 | int found = 0; |
3586 | 0 | for (next_stats = this_stats + 1; next_stats < last_stats; next_stats++) { |
3587 | 0 | if (next_stats->is_flash || (next_stats - 1)->is_flash || |
3588 | 0 | (next_stats - 2)->is_flash) |
3589 | 0 | continue; |
3590 | 0 | found = 1; |
3591 | 0 | this_stats->noise_var = next_stats->noise_var; |
3592 | 0 | break; |
3593 | 0 | } |
3594 | 0 | if (found) continue; |
3595 | 0 | for (next_stats = this_stats - 1; next_stats >= first_stats + 2; |
3596 | 0 | next_stats--) { |
3597 | 0 | if (next_stats->is_flash || (next_stats - 1)->is_flash || |
3598 | 0 | (next_stats - 2)->is_flash) |
3599 | 0 | continue; |
3600 | 0 | this_stats->noise_var = next_stats->noise_var; |
3601 | 0 | break; |
3602 | 0 | } |
3603 | 0 | } |
3604 | 0 | } |
3605 | | |
3606 | | // if we are at the first 2 frames, copy the noise |
3607 | 0 | for (this_stats = first_stats; |
3608 | 0 | this_stats < first_stats + 2 && (first_stats + 2) < last_stats; |
3609 | 0 | this_stats++) { |
3610 | 0 | this_stats->noise_var = (first_stats + 2)->noise_var; |
3611 | 0 | } |
3612 | 0 | } |
3613 | | |
3614 | | // Estimate correlation coefficient of each frame with its previous frame. |
3615 | | static void estimate_coeff(FIRSTPASS_STATS *first_stats, |
3616 | 0 | FIRSTPASS_STATS *last_stats) { |
3617 | 0 | FIRSTPASS_STATS *this_stats; |
3618 | 0 | for (this_stats = first_stats + 1; this_stats < last_stats; this_stats++) { |
3619 | 0 | const double C = |
3620 | 0 | sqrt(AOMMAX((this_stats - 1)->intra_error * |
3621 | 0 | (this_stats->intra_error - this_stats->coded_error), |
3622 | 0 | 0.001)); |
3623 | 0 | const double cor_coeff = |
3624 | 0 | C / |
3625 | 0 | AOMMAX((this_stats - 1)->intra_error - this_stats->noise_var, 0.001); |
3626 | |
|
3627 | 0 | this_stats->cor_coeff = |
3628 | 0 | cor_coeff * |
3629 | 0 | sqrt(AOMMAX((this_stats - 1)->intra_error - this_stats->noise_var, |
3630 | 0 | 0.001) / |
3631 | 0 | AOMMAX(this_stats->intra_error - this_stats->noise_var, 0.001)); |
3632 | | // clip correlation coefficient. |
3633 | 0 | this_stats->cor_coeff = AOMMIN(AOMMAX(this_stats->cor_coeff, 0), 1); |
3634 | 0 | } |
3635 | 0 | first_stats->cor_coeff = 1.0; |
3636 | 0 | } |
3637 | | |
3638 | | void av1_get_second_pass_params(AV1_COMP *cpi, |
3639 | | EncodeFrameParams *const frame_params, |
3640 | 1.26k | unsigned int frame_flags) { |
3641 | 1.26k | RATE_CONTROL *const rc = &cpi->rc; |
3642 | 1.26k | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
3643 | 1.26k | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3644 | 1.26k | GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
3645 | 1.26k | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
3646 | | |
3647 | 1.26k | const FIRSTPASS_STATS *const start_pos = cpi->twopass_frame.stats_in; |
3648 | 1.26k | int update_total_stats = 0; |
3649 | | |
3650 | 1.26k | if (is_stat_consumption_stage(cpi) && !cpi->twopass_frame.stats_in) return; |
3651 | | |
3652 | 1.26k | assert(cpi->twopass_frame.stats_in != NULL); |
3653 | 1.26k | const int update_type = gf_group->update_type[cpi->gf_frame_index]; |
3654 | 1.26k | frame_params->frame_type = gf_group->frame_type[cpi->gf_frame_index]; |
3655 | | |
3656 | 1.26k | if (cpi->gf_frame_index < gf_group->size && !(frame_flags & FRAMEFLAGS_KEY)) { |
3657 | 0 | assert(cpi->gf_frame_index < gf_group->size); |
3658 | |
|
3659 | 0 | setup_target_rate(cpi); |
3660 | | |
3661 | | // If this is an arf frame then we dont want to read the stats file or |
3662 | | // advance the input pointer as we already have what we need. |
3663 | 0 | if (update_type == ARF_UPDATE || update_type == INTNL_ARF_UPDATE) { |
3664 | 0 | const FIRSTPASS_STATS *const this_frame_ptr = |
3665 | 0 | read_frame_stats(twopass, &cpi->twopass_frame, |
3666 | 0 | gf_group->arf_src_offset[cpi->gf_frame_index]); |
3667 | 0 | set_twopass_params_based_on_fp_stats(cpi, this_frame_ptr); |
3668 | 0 | return; |
3669 | 0 | } |
3670 | 0 | } |
3671 | | |
3672 | 1.26k | if (oxcf->rc_cfg.mode == AOM_Q) |
3673 | 1.26k | rc->active_worst_quality = oxcf->rc_cfg.cq_level; |
3674 | 1.26k | FIRSTPASS_STATS this_frame; |
3675 | 1.26k | av1_zero(this_frame); |
3676 | | // call above fn |
3677 | 1.26k | if (is_stat_consumption_stage(cpi)) { |
3678 | 0 | if (cpi->gf_frame_index < gf_group->size || rc->frames_to_key == 0) { |
3679 | 0 | process_first_pass_stats(cpi, &this_frame); |
3680 | 0 | update_total_stats = 1; |
3681 | 0 | } |
3682 | 1.26k | } else { |
3683 | 1.26k | rc->active_worst_quality = oxcf->rc_cfg.cq_level; |
3684 | 1.26k | } |
3685 | | |
3686 | 1.26k | if (cpi->gf_frame_index == gf_group->size) { |
3687 | 1.26k | if (cpi->ppi->lap_enabled && cpi->ppi->p_rc.enable_scenecut_detection) { |
3688 | 0 | const int num_frames_to_detect_scenecut = MAX_GF_LENGTH_LAP + 1; |
3689 | 0 | const int frames_to_key = define_kf_interval( |
3690 | 0 | cpi, &twopass->firstpass_info, num_frames_to_detect_scenecut, |
3691 | 0 | /*search_start_idx=*/0); |
3692 | 0 | if (frames_to_key != -1) |
3693 | 0 | rc->frames_to_key = AOMMIN(rc->frames_to_key, frames_to_key); |
3694 | 0 | } |
3695 | 1.26k | } |
3696 | | |
3697 | | // Keyframe and section processing. |
3698 | 1.26k | FIRSTPASS_STATS this_frame_copy; |
3699 | 1.26k | this_frame_copy = this_frame; |
3700 | 1.26k | if (rc->frames_to_key <= 0) { |
3701 | 1.26k | assert(rc->frames_to_key == 0); |
3702 | | // Define next KF group and assign bits to it. |
3703 | 1.26k | frame_params->frame_type = KEY_FRAME; |
3704 | 1.26k | find_next_key_frame(cpi, &this_frame); |
3705 | 1.26k | this_frame = this_frame_copy; |
3706 | 1.26k | } |
3707 | | |
3708 | 1.26k | if (rc->frames_to_fwd_kf <= 0) |
3709 | 1.26k | rc->frames_to_fwd_kf = oxcf->kf_cfg.fwd_kf_dist; |
3710 | | |
3711 | | // Define a new GF/ARF group. (Should always enter here for key frames). |
3712 | 1.26k | if (cpi->gf_frame_index == gf_group->size) { |
3713 | 1.26k | av1_tf_info_reset(&cpi->ppi->tf_info); |
3714 | | #if CONFIG_BITRATE_ACCURACY |
3715 | | vbr_rc_reset_gop_data(&cpi->vbr_rc_info); |
3716 | | #endif // CONFIG_BITRATE_ACCURACY |
3717 | 1.26k | int max_gop_length = |
3718 | 1.26k | (oxcf->gf_cfg.lag_in_frames >= 32) |
3719 | 1.26k | ? AOMMIN(MAX_GF_INTERVAL, oxcf->gf_cfg.lag_in_frames - |
3720 | 1.26k | oxcf->algo_cfg.arnr_max_frames / 2) |
3721 | 1.26k | : MAX_GF_LENGTH_LAP; |
3722 | | |
3723 | | // Use the provided gop size in low delay setting |
3724 | 1.26k | if (oxcf->gf_cfg.lag_in_frames == 0) max_gop_length = rc->max_gf_interval; |
3725 | | |
3726 | | // Identify regions if needed. |
3727 | | // TODO(bohanli): identify regions for all stats available. |
3728 | 1.26k | if (rc->frames_since_key == 0 || rc->frames_since_key == 1 || |
3729 | 1.26k | (p_rc->frames_till_regions_update - rc->frames_since_key < |
3730 | 0 | rc->frames_to_key && |
3731 | 0 | p_rc->frames_till_regions_update - rc->frames_since_key < |
3732 | 1.26k | max_gop_length + 1)) { |
3733 | | // how many frames we can analyze from this frame |
3734 | 1.26k | int rest_frames = |
3735 | 1.26k | AOMMIN(rc->frames_to_key, MAX_FIRSTPASS_ANALYSIS_FRAMES); |
3736 | 1.26k | rest_frames = |
3737 | 1.26k | AOMMIN(rest_frames, (int)(twopass->stats_buf_ctx->stats_in_end - |
3738 | 1.26k | cpi->twopass_frame.stats_in + |
3739 | 1.26k | (rc->frames_since_key == 0))); |
3740 | 1.26k | p_rc->frames_till_regions_update = rest_frames; |
3741 | | |
3742 | 1.26k | if (cpi->ppi->lap_enabled) { |
3743 | 0 | mark_flashes(twopass->stats_buf_ctx->stats_in_start, |
3744 | 0 | twopass->stats_buf_ctx->stats_in_end); |
3745 | 0 | estimate_noise(twopass->stats_buf_ctx->stats_in_start, |
3746 | 0 | twopass->stats_buf_ctx->stats_in_end); |
3747 | 0 | estimate_coeff(twopass->stats_buf_ctx->stats_in_start, |
3748 | 0 | twopass->stats_buf_ctx->stats_in_end); |
3749 | 0 | identify_regions(cpi->twopass_frame.stats_in, rest_frames, |
3750 | 0 | (rc->frames_since_key == 0), p_rc->regions, |
3751 | 0 | &p_rc->num_regions); |
3752 | 1.26k | } else { |
3753 | 1.26k | identify_regions( |
3754 | 1.26k | cpi->twopass_frame.stats_in - (rc->frames_since_key == 0), |
3755 | 1.26k | rest_frames, 0, p_rc->regions, &p_rc->num_regions); |
3756 | 1.26k | } |
3757 | 1.26k | } |
3758 | | |
3759 | 1.26k | int cur_region_idx = |
3760 | 1.26k | find_regions_index(p_rc->regions, p_rc->num_regions, |
3761 | 1.26k | rc->frames_since_key - p_rc->regions_offset); |
3762 | 1.26k | if ((cur_region_idx >= 0 && |
3763 | 1.26k | p_rc->regions[cur_region_idx].type == SCENECUT_REGION) || |
3764 | 1.26k | rc->frames_since_key == 0) { |
3765 | | // If we start from a scenecut, then the last GOP's arf boost is not |
3766 | | // needed for this GOP. |
3767 | 1.26k | cpi->ppi->gf_state.arf_gf_boost_lst = 0; |
3768 | 1.26k | } |
3769 | | |
3770 | 1.26k | int need_gf_len = 1; |
3771 | 1.26k | if (cpi->third_pass_ctx && oxcf->pass == AOM_RC_THIRD_PASS) { |
3772 | | // set up bitstream to read |
3773 | 0 | if (!cpi->third_pass_ctx->input_file_name && oxcf->two_pass_output) { |
3774 | 0 | cpi->third_pass_ctx->input_file_name = oxcf->two_pass_output; |
3775 | 0 | } |
3776 | 0 | av1_open_second_pass_log(cpi, 1); |
3777 | 0 | THIRD_PASS_GOP_INFO *gop_info = &cpi->third_pass_ctx->gop_info; |
3778 | | // Read in GOP information from the second pass file. |
3779 | 0 | av1_read_second_pass_gop_info(cpi->second_pass_log_stream, gop_info, |
3780 | 0 | cpi->common.error); |
3781 | | // Read in third_pass_info from the bitstream. |
3782 | 0 | av1_set_gop_third_pass(cpi->third_pass_ctx); |
3783 | | // Read in per-frame info from second-pass encoding |
3784 | 0 | av1_read_second_pass_per_frame_info( |
3785 | 0 | cpi->second_pass_log_stream, cpi->third_pass_ctx->frame_info, |
3786 | 0 | gop_info->num_frames, cpi->common.error); |
3787 | |
|
3788 | 0 | p_rc->cur_gf_index = 0; |
3789 | 0 | p_rc->gf_intervals[0] = cpi->third_pass_ctx->gop_info.gf_length; |
3790 | 0 | need_gf_len = 0; |
3791 | 0 | } |
3792 | | |
3793 | 1.26k | if (need_gf_len) { |
3794 | | // If we cannot obtain GF group length from second_pass_file |
3795 | | // TODO(jingning): Resolve the redundant calls here. |
3796 | 1.26k | if (rc->intervals_till_gf_calculate_due == 0 || 1) { |
3797 | 1.26k | calculate_gf_length(cpi, max_gop_length, MAX_NUM_GF_INTERVALS); |
3798 | 1.26k | } |
3799 | | |
3800 | 1.26k | if (max_gop_length > 16 && oxcf->algo_cfg.enable_tpl_model && |
3801 | 1.26k | oxcf->gf_cfg.lag_in_frames >= 32 && |
3802 | 1.26k | cpi->sf.tpl_sf.gop_length_decision_method != 3) { |
3803 | 0 | int this_idx = rc->frames_since_key + |
3804 | 0 | p_rc->gf_intervals[p_rc->cur_gf_index] - |
3805 | 0 | p_rc->regions_offset - 1; |
3806 | 0 | int this_region = |
3807 | 0 | find_regions_index(p_rc->regions, p_rc->num_regions, this_idx); |
3808 | 0 | int next_region = |
3809 | 0 | find_regions_index(p_rc->regions, p_rc->num_regions, this_idx + 1); |
3810 | | // TODO(angiebird): Figure out why this_region and next_region are -1 in |
3811 | | // unit test like AltRefFramePresenceTestLarge (aomedia:3134) |
3812 | 0 | int is_last_scenecut = |
3813 | 0 | p_rc->gf_intervals[p_rc->cur_gf_index] >= rc->frames_to_key || |
3814 | 0 | (this_region != -1 && |
3815 | 0 | p_rc->regions[this_region].type == SCENECUT_REGION) || |
3816 | 0 | (next_region != -1 && |
3817 | 0 | p_rc->regions[next_region].type == SCENECUT_REGION); |
3818 | |
|
3819 | 0 | int ori_gf_int = p_rc->gf_intervals[p_rc->cur_gf_index]; |
3820 | |
|
3821 | 0 | if (p_rc->gf_intervals[p_rc->cur_gf_index] > 16 && |
3822 | 0 | rc->min_gf_interval <= 16) { |
3823 | | // The calculate_gf_length function is previously used with |
3824 | | // max_gop_length = 32 with look-ahead gf intervals. |
3825 | 0 | define_gf_group(cpi, frame_params, 0); |
3826 | 0 | av1_tf_info_filtering(&cpi->ppi->tf_info, cpi, gf_group); |
3827 | 0 | this_frame = this_frame_copy; |
3828 | |
|
3829 | 0 | if (is_shorter_gf_interval_better(cpi, frame_params)) { |
3830 | | // A shorter gf interval is better. |
3831 | | // TODO(jingning): Remove redundant computations here. |
3832 | 0 | max_gop_length = 16; |
3833 | 0 | calculate_gf_length(cpi, max_gop_length, 1); |
3834 | 0 | if (is_last_scenecut && |
3835 | 0 | (ori_gf_int - p_rc->gf_intervals[p_rc->cur_gf_index] < 4)) { |
3836 | 0 | p_rc->gf_intervals[p_rc->cur_gf_index] = ori_gf_int; |
3837 | 0 | } |
3838 | 0 | } |
3839 | 0 | } |
3840 | 0 | } |
3841 | 1.26k | } |
3842 | | |
3843 | 1.26k | define_gf_group(cpi, frame_params, 0); |
3844 | | |
3845 | 1.26k | if (gf_group->update_type[cpi->gf_frame_index] != ARF_UPDATE && |
3846 | 1.26k | rc->frames_since_key > 0) |
3847 | 0 | process_first_pass_stats(cpi, &this_frame); |
3848 | | |
3849 | 1.26k | define_gf_group(cpi, frame_params, 1); |
3850 | | |
3851 | | // write gop info if needed for third pass. Per-frame info is written after |
3852 | | // each frame is encoded. |
3853 | 1.26k | av1_write_second_pass_gop_info(cpi); |
3854 | | |
3855 | 1.26k | av1_tf_info_filtering(&cpi->ppi->tf_info, cpi, gf_group); |
3856 | | |
3857 | 1.26k | rc->frames_till_gf_update_due = p_rc->baseline_gf_interval; |
3858 | 1.26k | assert(cpi->gf_frame_index == 0); |
3859 | | #if ARF_STATS_OUTPUT |
3860 | | { |
3861 | | FILE *fpfile; |
3862 | | fpfile = fopen("arf.stt", "a"); |
3863 | | ++arf_count; |
3864 | | fprintf(fpfile, "%10d %10d %10d %10d %10d\n", |
3865 | | cpi->common.current_frame.frame_number, |
3866 | | rc->frames_till_gf_update_due, cpi->ppi->p_rc.kf_boost, arf_count, |
3867 | | p_rc->gfu_boost); |
3868 | | |
3869 | | fclose(fpfile); |
3870 | | } |
3871 | | #endif |
3872 | 1.26k | } |
3873 | 1.26k | assert(cpi->gf_frame_index < gf_group->size); |
3874 | | |
3875 | 1.26k | if (gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE || |
3876 | 1.26k | gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) { |
3877 | 0 | reset_fpf_position(&cpi->twopass_frame, start_pos); |
3878 | |
|
3879 | 0 | const FIRSTPASS_STATS *const this_frame_ptr = |
3880 | 0 | read_frame_stats(twopass, &cpi->twopass_frame, |
3881 | 0 | gf_group->arf_src_offset[cpi->gf_frame_index]); |
3882 | 0 | set_twopass_params_based_on_fp_stats(cpi, this_frame_ptr); |
3883 | 1.26k | } else { |
3884 | | // Back up this frame's stats for updating total stats during post encode. |
3885 | 1.26k | cpi->twopass_frame.this_frame = update_total_stats ? start_pos : NULL; |
3886 | 1.26k | } |
3887 | | |
3888 | 1.26k | frame_params->frame_type = gf_group->frame_type[cpi->gf_frame_index]; |
3889 | 1.26k | setup_target_rate(cpi); |
3890 | 1.26k | } |
3891 | | |
3892 | 0 | void av1_init_second_pass(AV1_COMP *cpi) { |
3893 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
3894 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3895 | 0 | FRAME_INFO *const frame_info = &cpi->frame_info; |
3896 | 0 | double frame_rate; |
3897 | 0 | FIRSTPASS_STATS *stats; |
3898 | |
|
3899 | 0 | if (!twopass->stats_buf_ctx->stats_in_end) return; |
3900 | | |
3901 | 0 | mark_flashes(twopass->stats_buf_ctx->stats_in_start, |
3902 | 0 | twopass->stats_buf_ctx->stats_in_end); |
3903 | 0 | estimate_noise(twopass->stats_buf_ctx->stats_in_start, |
3904 | 0 | twopass->stats_buf_ctx->stats_in_end); |
3905 | 0 | estimate_coeff(twopass->stats_buf_ctx->stats_in_start, |
3906 | 0 | twopass->stats_buf_ctx->stats_in_end); |
3907 | |
|
3908 | 0 | stats = twopass->stats_buf_ctx->total_stats; |
3909 | |
|
3910 | 0 | *stats = *twopass->stats_buf_ctx->stats_in_end; |
3911 | 0 | *twopass->stats_buf_ctx->total_left_stats = *stats; |
3912 | |
|
3913 | 0 | frame_rate = 10000000.0 * stats->count / stats->duration; |
3914 | | // Each frame can have a different duration, as the frame rate in the source |
3915 | | // isn't guaranteed to be constant. The frame rate prior to the first frame |
3916 | | // encoded in the second pass is a guess. However, the sum duration is not. |
3917 | | // It is calculated based on the actual durations of all frames from the |
3918 | | // first pass. |
3919 | 0 | av1_new_framerate(cpi, frame_rate); |
3920 | 0 | twopass->bits_left = |
3921 | 0 | (int64_t)(stats->duration * oxcf->rc_cfg.target_bandwidth / 10000000.0); |
3922 | |
|
3923 | | #if CONFIG_BITRATE_ACCURACY |
3924 | | av1_vbr_rc_init(&cpi->vbr_rc_info, cpi->ppi->twopass.bits_left, |
3925 | | (int)round(stats->count)); |
3926 | | #endif |
3927 | | |
3928 | | // This variable monitors how far behind the second ref update is lagging. |
3929 | 0 | twopass->sr_update_lag = 1; |
3930 | | |
3931 | | // Scan the first pass file and calculate a modified total error based upon |
3932 | | // the bias/power function used to allocate bits. |
3933 | 0 | { |
3934 | 0 | const double avg_error = |
3935 | 0 | stats->coded_error / DOUBLE_DIVIDE_CHECK(stats->count); |
3936 | 0 | const FIRSTPASS_STATS *s = cpi->twopass_frame.stats_in; |
3937 | 0 | double modified_error_total = 0.0; |
3938 | 0 | twopass->modified_error_min = |
3939 | 0 | (avg_error * oxcf->rc_cfg.vbrmin_section) / 100; |
3940 | 0 | twopass->modified_error_max = |
3941 | 0 | (avg_error * oxcf->rc_cfg.vbrmax_section) / 100; |
3942 | 0 | while (s < twopass->stats_buf_ctx->stats_in_end) { |
3943 | 0 | modified_error_total += |
3944 | 0 | calculate_modified_err(frame_info, twopass, oxcf, s); |
3945 | 0 | ++s; |
3946 | 0 | } |
3947 | 0 | twopass->modified_error_left = modified_error_total; |
3948 | 0 | } |
3949 | | |
3950 | | // Reset the vbr bits off target counters |
3951 | 0 | cpi->ppi->p_rc.vbr_bits_off_target = 0; |
3952 | 0 | cpi->ppi->p_rc.vbr_bits_off_target_fast = 0; |
3953 | |
|
3954 | 0 | cpi->ppi->p_rc.rate_error_estimate = 0; |
3955 | | |
3956 | | // Static sequence monitor variables. |
3957 | 0 | twopass->kf_zeromotion_pct = 100; |
3958 | 0 | twopass->last_kfgroup_zeromotion_pct = 100; |
3959 | | |
3960 | | // Initialize bits per macro_block estimate correction factor. |
3961 | 0 | twopass->bpm_factor = 1.0; |
3962 | | // Initialize actual and target bits counters for ARF groups so that |
3963 | | // at the start we have a neutral bpm adjustment. |
3964 | 0 | twopass->rolling_arf_group_target_bits = 1; |
3965 | 0 | twopass->rolling_arf_group_actual_bits = 1; |
3966 | 0 | } |
3967 | | |
3968 | 0 | void av1_init_single_pass_lap(AV1_COMP *cpi) { |
3969 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3970 | |
|
3971 | 0 | if (!twopass->stats_buf_ctx->stats_in_end) return; |
3972 | | |
3973 | | // This variable monitors how far behind the second ref update is lagging. |
3974 | 0 | twopass->sr_update_lag = 1; |
3975 | |
|
3976 | 0 | twopass->bits_left = 0; |
3977 | 0 | twopass->modified_error_min = 0.0; |
3978 | 0 | twopass->modified_error_max = 0.0; |
3979 | 0 | twopass->modified_error_left = 0.0; |
3980 | | |
3981 | | // Reset the vbr bits off target counters |
3982 | 0 | cpi->ppi->p_rc.vbr_bits_off_target = 0; |
3983 | 0 | cpi->ppi->p_rc.vbr_bits_off_target_fast = 0; |
3984 | |
|
3985 | 0 | cpi->ppi->p_rc.rate_error_estimate = 0; |
3986 | | |
3987 | | // Static sequence monitor variables. |
3988 | 0 | twopass->kf_zeromotion_pct = 100; |
3989 | 0 | twopass->last_kfgroup_zeromotion_pct = 100; |
3990 | | |
3991 | | // Initialize bits per macro_block estimate correction factor. |
3992 | 0 | twopass->bpm_factor = 1.0; |
3993 | | // Initialize actual and target bits counters for ARF groups so that |
3994 | | // at the start we have a neutral bpm adjustment. |
3995 | 0 | twopass->rolling_arf_group_target_bits = 1; |
3996 | 0 | twopass->rolling_arf_group_actual_bits = 1; |
3997 | 0 | } |
3998 | | |
3999 | 0 | #define MINQ_ADJ_LIMIT 48 |
4000 | 0 | #define MINQ_ADJ_LIMIT_CQ 20 |
4001 | 0 | #define HIGH_UNDERSHOOT_RATIO 2 |
4002 | 0 | void av1_twopass_postencode_update(AV1_COMP *cpi) { |
4003 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
4004 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
4005 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
4006 | 0 | const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg; |
4007 | | |
4008 | | // Increment the stats_in pointer. |
4009 | 0 | if (is_stat_consumption_stage(cpi) && |
4010 | 0 | (cpi->gf_frame_index < cpi->ppi->gf_group.size || |
4011 | 0 | rc->frames_to_key == 0)) { |
4012 | 0 | const int update_type = cpi->ppi->gf_group.update_type[cpi->gf_frame_index]; |
4013 | 0 | if (update_type != ARF_UPDATE && update_type != INTNL_ARF_UPDATE) { |
4014 | 0 | FIRSTPASS_STATS this_frame; |
4015 | 0 | --cpi->twopass_frame.stats_in; |
4016 | 0 | if (cpi->ppi->lap_enabled) { |
4017 | 0 | input_stats_lap(twopass, &cpi->twopass_frame, &this_frame); |
4018 | 0 | } else { |
4019 | 0 | input_stats(twopass, &cpi->twopass_frame, &this_frame); |
4020 | 0 | } |
4021 | 0 | } else if (cpi->ppi->lap_enabled) { |
4022 | 0 | cpi->twopass_frame.stats_in = |
4023 | 0 | cpi->ppi->twopass.stats_buf_ctx->stats_in_start; |
4024 | 0 | } |
4025 | 0 | } |
4026 | | |
4027 | | // VBR correction is done through rc->vbr_bits_off_target. Based on the |
4028 | | // sign of this value, a limited % adjustment is made to the target rate |
4029 | | // of subsequent frames, to try and push it back towards 0. This method |
4030 | | // is designed to prevent extreme behaviour at the end of a clip |
4031 | | // or group of frames. |
4032 | 0 | p_rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; |
4033 | 0 | twopass->bits_left = AOMMAX(twopass->bits_left - rc->base_frame_target, 0); |
4034 | |
|
4035 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
4036 | | if (cpi->do_update_vbr_bits_off_target_fast) { |
4037 | | // Subtract current frame's fast_extra_bits. |
4038 | | p_rc->vbr_bits_off_target_fast -= rc->frame_level_fast_extra_bits; |
4039 | | rc->frame_level_fast_extra_bits = 0; |
4040 | | } |
4041 | | #endif |
4042 | | |
4043 | | // Target vs actual bits for this arf group. |
4044 | 0 | twopass->rolling_arf_group_target_bits += rc->base_frame_target; |
4045 | 0 | twopass->rolling_arf_group_actual_bits += rc->projected_frame_size; |
4046 | | |
4047 | | // Calculate the pct rc error. |
4048 | 0 | if (p_rc->total_actual_bits) { |
4049 | 0 | p_rc->rate_error_estimate = |
4050 | 0 | (int)((p_rc->vbr_bits_off_target * 100) / p_rc->total_actual_bits); |
4051 | 0 | p_rc->rate_error_estimate = clamp(p_rc->rate_error_estimate, -100, 100); |
4052 | 0 | } else { |
4053 | 0 | p_rc->rate_error_estimate = 0; |
4054 | 0 | } |
4055 | |
|
4056 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
4057 | | /* The variables temp_vbr_bits_off_target, temp_bits_left, |
4058 | | * temp_rolling_arf_group_target_bits, temp_rolling_arf_group_actual_bits |
4059 | | * temp_rate_error_estimate are introduced for quality simulation purpose, |
4060 | | * it retains the value previous to the parallel encode frames. The |
4061 | | * variables are updated based on the update flag. |
4062 | | * |
4063 | | * If there exist show_existing_frames between parallel frames, then to |
4064 | | * retain the temp state do not update it. */ |
4065 | | const int simulate_parallel_frame = |
4066 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE; |
4067 | | int show_existing_between_parallel_frames = |
4068 | | (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == |
4069 | | INTNL_OVERLAY_UPDATE && |
4070 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2); |
4071 | | |
4072 | | if (cpi->do_frame_data_update && !show_existing_between_parallel_frames && |
4073 | | simulate_parallel_frame) { |
4074 | | cpi->ppi->p_rc.temp_vbr_bits_off_target = p_rc->vbr_bits_off_target; |
4075 | | cpi->ppi->p_rc.temp_bits_left = twopass->bits_left; |
4076 | | cpi->ppi->p_rc.temp_rolling_arf_group_target_bits = |
4077 | | twopass->rolling_arf_group_target_bits; |
4078 | | cpi->ppi->p_rc.temp_rolling_arf_group_actual_bits = |
4079 | | twopass->rolling_arf_group_actual_bits; |
4080 | | cpi->ppi->p_rc.temp_rate_error_estimate = p_rc->rate_error_estimate; |
4081 | | } |
4082 | | #endif |
4083 | | // Update the active best quality pyramid. |
4084 | 0 | if (!rc->is_src_frame_alt_ref) { |
4085 | 0 | const int pyramid_level = |
4086 | 0 | cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index]; |
4087 | 0 | int i; |
4088 | 0 | for (i = pyramid_level; i <= MAX_ARF_LAYERS; ++i) { |
4089 | 0 | p_rc->active_best_quality[i] = cpi->common.quant_params.base_qindex; |
4090 | | #if CONFIG_TUNE_VMAF |
4091 | | if (cpi->vmaf_info.original_qindex != -1 && |
4092 | | (cpi->oxcf.tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING && |
4093 | | cpi->oxcf.tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN)) { |
4094 | | p_rc->active_best_quality[i] = cpi->vmaf_info.original_qindex; |
4095 | | } |
4096 | | #endif |
4097 | 0 | } |
4098 | 0 | } |
4099 | |
|
4100 | | #if 0 |
4101 | | { |
4102 | | AV1_COMMON *cm = &cpi->common; |
4103 | | FILE *fpfile; |
4104 | | fpfile = fopen("details.stt", "a"); |
4105 | | fprintf(fpfile, |
4106 | | "%10d %10d %10d %10" PRId64 " %10" PRId64 |
4107 | | " %10d %10d %10d %10.4lf %10.4lf %10.4lf %10.4lf\n", |
4108 | | cm->current_frame.frame_number, rc->base_frame_target, |
4109 | | rc->projected_frame_size, rc->total_actual_bits, |
4110 | | rc->vbr_bits_off_target, p_rc->rate_error_estimate, |
4111 | | twopass->rolling_arf_group_target_bits, |
4112 | | twopass->rolling_arf_group_actual_bits, |
4113 | | (double)twopass->rolling_arf_group_actual_bits / |
4114 | | (double)twopass->rolling_arf_group_target_bits, |
4115 | | twopass->bpm_factor, |
4116 | | av1_convert_qindex_to_q(cpi->common.quant_params.base_qindex, |
4117 | | cm->seq_params->bit_depth), |
4118 | | av1_convert_qindex_to_q(rc->active_worst_quality, |
4119 | | cm->seq_params->bit_depth)); |
4120 | | fclose(fpfile); |
4121 | | } |
4122 | | #endif |
4123 | |
|
4124 | 0 | if (cpi->common.current_frame.frame_type != KEY_FRAME) { |
4125 | 0 | twopass->kf_group_bits -= rc->base_frame_target; |
4126 | 0 | twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct; |
4127 | 0 | } |
4128 | 0 | twopass->kf_group_bits = AOMMAX(twopass->kf_group_bits, 0); |
4129 | | |
4130 | | // If the rate control is drifting consider adjustment to min or maxq. |
4131 | 0 | if ((rc_cfg->mode != AOM_Q) && !cpi->rc.is_src_frame_alt_ref) { |
4132 | 0 | int maxq_adj_limit; |
4133 | 0 | int minq_adj_limit; |
4134 | 0 | maxq_adj_limit = rc->worst_quality - rc->active_worst_quality; |
4135 | 0 | minq_adj_limit = |
4136 | 0 | (rc_cfg->mode == AOM_CQ ? MINQ_ADJ_LIMIT_CQ : MINQ_ADJ_LIMIT); |
4137 | | // Undershoot. |
4138 | 0 | if (p_rc->rate_error_estimate > rc_cfg->under_shoot_pct) { |
4139 | 0 | --twopass->extend_maxq; |
4140 | 0 | if (p_rc->rolling_target_bits >= p_rc->rolling_actual_bits) |
4141 | 0 | ++twopass->extend_minq; |
4142 | | // Overshoot. |
4143 | 0 | } else if (p_rc->rate_error_estimate < -rc_cfg->over_shoot_pct) { |
4144 | 0 | --twopass->extend_minq; |
4145 | 0 | if (p_rc->rolling_target_bits < p_rc->rolling_actual_bits) |
4146 | 0 | ++twopass->extend_maxq; |
4147 | 0 | } else { |
4148 | | // Adjustment for extreme local overshoot. |
4149 | 0 | if (rc->projected_frame_size > (2 * rc->base_frame_target) && |
4150 | 0 | rc->projected_frame_size > (2 * rc->avg_frame_bandwidth)) |
4151 | 0 | ++twopass->extend_maxq; |
4152 | | // Unwind undershoot or overshoot adjustment. |
4153 | 0 | if (p_rc->rolling_target_bits < p_rc->rolling_actual_bits) |
4154 | 0 | --twopass->extend_minq; |
4155 | 0 | else if (p_rc->rolling_target_bits > p_rc->rolling_actual_bits) |
4156 | 0 | --twopass->extend_maxq; |
4157 | 0 | } |
4158 | 0 | twopass->extend_minq = clamp(twopass->extend_minq, 0, minq_adj_limit); |
4159 | 0 | twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); |
4160 | |
|
4161 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
4162 | | int update_fast_extra_bits = 1; |
4163 | | #if CONFIG_FPMT_TEST |
4164 | | update_fast_extra_bits = simulate_parallel_frame ? 0 : 1; |
4165 | | #endif |
4166 | | if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref && |
4167 | | p_rc->vbr_bits_off_target_fast && update_fast_extra_bits) { |
4168 | | // Subtract current frame's fast_extra_bits. |
4169 | | p_rc->vbr_bits_off_target_fast -= rc->frame_level_fast_extra_bits; |
4170 | | } |
4171 | | #endif |
4172 | | |
4173 | | // If there is a big and undexpected undershoot then feed the extra |
4174 | | // bits back in quickly. One situation where this may happen is if a |
4175 | | // frame is unexpectedly almost perfectly predicted by the ARF or GF |
4176 | | // but not very well predcited by the previous frame. |
4177 | 0 | if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) { |
4178 | 0 | int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO; |
4179 | 0 | if (rc->projected_frame_size < fast_extra_thresh) { |
4180 | 0 | p_rc->vbr_bits_off_target_fast += |
4181 | 0 | fast_extra_thresh - rc->projected_frame_size; |
4182 | 0 | p_rc->vbr_bits_off_target_fast = AOMMIN(p_rc->vbr_bits_off_target_fast, |
4183 | 0 | (4 * rc->avg_frame_bandwidth)); |
4184 | | |
4185 | | // Fast adaptation of minQ if necessary to use up the extra bits. |
4186 | 0 | if (rc->avg_frame_bandwidth) { |
4187 | 0 | twopass->extend_minq_fast = (int)(p_rc->vbr_bits_off_target_fast * 8 / |
4188 | 0 | rc->avg_frame_bandwidth); |
4189 | 0 | } |
4190 | 0 | twopass->extend_minq_fast = AOMMIN( |
4191 | 0 | twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq); |
4192 | 0 | } else if (p_rc->vbr_bits_off_target_fast) { |
4193 | 0 | twopass->extend_minq_fast = AOMMIN( |
4194 | 0 | twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq); |
4195 | 0 | } else { |
4196 | 0 | twopass->extend_minq_fast = 0; |
4197 | 0 | } |
4198 | 0 | } |
4199 | |
|
4200 | | #if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST |
4201 | | if (cpi->do_frame_data_update && !show_existing_between_parallel_frames && |
4202 | | simulate_parallel_frame) { |
4203 | | cpi->ppi->p_rc.temp_vbr_bits_off_target_fast = |
4204 | | p_rc->vbr_bits_off_target_fast; |
4205 | | cpi->ppi->p_rc.temp_extend_minq = twopass->extend_minq; |
4206 | | cpi->ppi->p_rc.temp_extend_maxq = twopass->extend_maxq; |
4207 | | cpi->ppi->p_rc.temp_extend_minq_fast = twopass->extend_minq_fast; |
4208 | | } |
4209 | | #endif |
4210 | 0 | } |
4211 | |
|
4212 | | #if CONFIG_FRAME_PARALLEL_ENCODE |
4213 | | // Update the frame probabilities obtained from parallel encode frames |
4214 | | FrameProbInfo *const frame_probs = &cpi->ppi->frame_probs; |
4215 | | #if CONFIG_FPMT_TEST |
4216 | | /* The variable temp_active_best_quality is introduced only for quality |
4217 | | * simulation purpose, it retains the value previous to the parallel |
4218 | | * encode frames. The variable is updated based on the update flag. |
4219 | | * |
4220 | | * If there exist show_existing_frames between parallel frames, then to |
4221 | | * retain the temp state do not update it. */ |
4222 | | if (cpi->do_frame_data_update && !show_existing_between_parallel_frames && |
4223 | | simulate_parallel_frame) { |
4224 | | int i; |
4225 | | const int pyramid_level = |
4226 | | cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index]; |
4227 | | if (!rc->is_src_frame_alt_ref) { |
4228 | | for (i = pyramid_level; i <= MAX_ARF_LAYERS; ++i) |
4229 | | cpi->ppi->p_rc.temp_active_best_quality[i] = |
4230 | | p_rc->active_best_quality[i]; |
4231 | | } |
4232 | | } |
4233 | | |
4234 | | // Update the frame probabilities obtained from parallel encode frames |
4235 | | FrameProbInfo *const temp_frame_probs_simulation = |
4236 | | simulate_parallel_frame ? &cpi->ppi->temp_frame_probs_simulation |
4237 | | : frame_probs; |
4238 | | FrameProbInfo *const temp_frame_probs = |
4239 | | simulate_parallel_frame ? &cpi->ppi->temp_frame_probs : NULL; |
4240 | | #endif |
4241 | | int i, j, loop; |
4242 | | // Sequentially do average on temp_frame_probs_simulation which holds |
4243 | | // probabilities of last frame before parallel encode |
4244 | | for (loop = 0; loop <= cpi->num_frame_recode; loop++) { |
4245 | | // Sequentially update tx_type_probs |
4246 | | if (cpi->do_update_frame_probs_txtype[loop] && |
4247 | | (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0)) { |
4248 | | const FRAME_UPDATE_TYPE update_type = |
4249 | | get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index); |
4250 | | for (i = 0; i < TX_SIZES_ALL; i++) { |
4251 | | int left = 1024; |
4252 | | |
4253 | | for (j = TX_TYPES - 1; j >= 0; j--) { |
4254 | | const int new_prob = |
4255 | | cpi->frame_new_probs[loop].tx_type_probs[update_type][i][j]; |
4256 | | #if CONFIG_FPMT_TEST |
4257 | | int prob = |
4258 | | (temp_frame_probs_simulation->tx_type_probs[update_type][i][j] + |
4259 | | new_prob) >> |
4260 | | 1; |
4261 | | left -= prob; |
4262 | | if (j == 0) prob += left; |
4263 | | temp_frame_probs_simulation->tx_type_probs[update_type][i][j] = prob; |
4264 | | #else |
4265 | | int prob = |
4266 | | (frame_probs->tx_type_probs[update_type][i][j] + new_prob) >> 1; |
4267 | | left -= prob; |
4268 | | if (j == 0) prob += left; |
4269 | | frame_probs->tx_type_probs[update_type][i][j] = prob; |
4270 | | #endif |
4271 | | } |
4272 | | } |
4273 | | } |
4274 | | |
4275 | | // Sequentially update obmc_probs |
4276 | | if (cpi->do_update_frame_probs_obmc[loop] && |
4277 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) { |
4278 | | const FRAME_UPDATE_TYPE update_type = |
4279 | | get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index); |
4280 | | |
4281 | | for (i = 0; i < BLOCK_SIZES_ALL; i++) { |
4282 | | const int new_prob = |
4283 | | cpi->frame_new_probs[loop].obmc_probs[update_type][i]; |
4284 | | #if CONFIG_FPMT_TEST |
4285 | | temp_frame_probs_simulation->obmc_probs[update_type][i] = |
4286 | | (temp_frame_probs_simulation->obmc_probs[update_type][i] + |
4287 | | new_prob) >> |
4288 | | 1; |
4289 | | #else |
4290 | | frame_probs->obmc_probs[update_type][i] = |
4291 | | (frame_probs->obmc_probs[update_type][i] + new_prob) >> 1; |
4292 | | #endif |
4293 | | } |
4294 | | } |
4295 | | |
4296 | | // Sequentially update warped_probs |
4297 | | if (cpi->do_update_frame_probs_warp[loop] && |
4298 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) { |
4299 | | const FRAME_UPDATE_TYPE update_type = |
4300 | | get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index); |
4301 | | const int new_prob = cpi->frame_new_probs[loop].warped_probs[update_type]; |
4302 | | #if CONFIG_FPMT_TEST |
4303 | | temp_frame_probs_simulation->warped_probs[update_type] = |
4304 | | (temp_frame_probs_simulation->warped_probs[update_type] + new_prob) >> |
4305 | | 1; |
4306 | | #else |
4307 | | frame_probs->warped_probs[update_type] = |
4308 | | (frame_probs->warped_probs[update_type] + new_prob) >> 1; |
4309 | | #endif |
4310 | | } |
4311 | | |
4312 | | // Sequentially update switchable_interp_probs |
4313 | | if (cpi->do_update_frame_probs_interpfilter[loop] && |
4314 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) { |
4315 | | const FRAME_UPDATE_TYPE update_type = |
4316 | | get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index); |
4317 | | |
4318 | | for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) { |
4319 | | int left = 1536; |
4320 | | |
4321 | | for (j = SWITCHABLE_FILTERS - 1; j >= 0; j--) { |
4322 | | const int new_prob = cpi->frame_new_probs[loop] |
4323 | | .switchable_interp_probs[update_type][i][j]; |
4324 | | #if CONFIG_FPMT_TEST |
4325 | | int prob = (temp_frame_probs_simulation |
4326 | | ->switchable_interp_probs[update_type][i][j] + |
4327 | | new_prob) >> |
4328 | | 1; |
4329 | | left -= prob; |
4330 | | if (j == 0) prob += left; |
4331 | | |
4332 | | temp_frame_probs_simulation |
4333 | | ->switchable_interp_probs[update_type][i][j] = prob; |
4334 | | #else |
4335 | | int prob = (frame_probs->switchable_interp_probs[update_type][i][j] + |
4336 | | new_prob) >> |
4337 | | 1; |
4338 | | left -= prob; |
4339 | | if (j == 0) prob += left; |
4340 | | frame_probs->switchable_interp_probs[update_type][i][j] = prob; |
4341 | | #endif |
4342 | | } |
4343 | | } |
4344 | | } |
4345 | | } |
4346 | | |
4347 | | #if CONFIG_FPMT_TEST |
4348 | | // Copying temp_frame_probs_simulation to temp_frame_probs based on |
4349 | | // the flag |
4350 | | if (cpi->do_frame_data_update && |
4351 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0 && |
4352 | | simulate_parallel_frame) { |
4353 | | for (int update_type_idx = 0; update_type_idx < FRAME_UPDATE_TYPES; |
4354 | | update_type_idx++) { |
4355 | | for (i = 0; i < BLOCK_SIZES_ALL; i++) { |
4356 | | temp_frame_probs->obmc_probs[update_type_idx][i] = |
4357 | | temp_frame_probs_simulation->obmc_probs[update_type_idx][i]; |
4358 | | } |
4359 | | temp_frame_probs->warped_probs[update_type_idx] = |
4360 | | temp_frame_probs_simulation->warped_probs[update_type_idx]; |
4361 | | for (i = 0; i < TX_SIZES_ALL; i++) { |
4362 | | for (j = 0; j < TX_TYPES; j++) { |
4363 | | temp_frame_probs->tx_type_probs[update_type_idx][i][j] = |
4364 | | temp_frame_probs_simulation->tx_type_probs[update_type_idx][i][j]; |
4365 | | } |
4366 | | } |
4367 | | for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) { |
4368 | | for (j = 0; j < SWITCHABLE_FILTERS; j++) { |
4369 | | temp_frame_probs->switchable_interp_probs[update_type_idx][i][j] = |
4370 | | temp_frame_probs_simulation |
4371 | | ->switchable_interp_probs[update_type_idx][i][j]; |
4372 | | } |
4373 | | } |
4374 | | } |
4375 | | } |
4376 | | #endif |
4377 | | // Update framerate obtained from parallel encode frames |
4378 | | if (cpi->common.show_frame && |
4379 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) |
4380 | | cpi->framerate = cpi->new_framerate; |
4381 | | #if CONFIG_FPMT_TEST |
4382 | | // SIMULATION PURPOSE |
4383 | | int show_existing_between_parallel_frames_cndn = |
4384 | | (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == |
4385 | | INTNL_OVERLAY_UPDATE && |
4386 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2); |
4387 | | if (cpi->common.show_frame && !show_existing_between_parallel_frames_cndn && |
4388 | | cpi->do_frame_data_update && simulate_parallel_frame) |
4389 | | cpi->temp_framerate = cpi->framerate; |
4390 | | #endif |
4391 | | #endif |
4392 | 0 | } |