Refactor intra block prediction and reconstruction process
[aom.git] / vp9 / encoder / vp9_firstpass.c
blob61279f872deefe39a48b300c0590cd8a3c67a3d7
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_ports/mem.h"
20 #include "vpx_scale/vpx_scale.h"
21 #include "vpx_scale/yv12config.h"
23 #include "vp9/common/vp9_entropymv.h"
24 #include "vp9/common/vp9_quant_common.h"
25 #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes()
26 #include "vp9/common/vp9_systemdependent.h"
27 #include "vp9/encoder/vp9_aq_variance.h"
28 #include "vp9/encoder/vp9_block.h"
29 #include "vp9/encoder/vp9_encodeframe.h"
30 #include "vp9/encoder/vp9_encodemb.h"
31 #include "vp9/encoder/vp9_encodemv.h"
32 #include "vp9/encoder/vp9_encoder.h"
33 #include "vp9/encoder/vp9_extend.h"
34 #include "vp9/encoder/vp9_firstpass.h"
35 #include "vp9/encoder/vp9_mcomp.h"
36 #include "vp9/encoder/vp9_quantize.h"
37 #include "vp9/encoder/vp9_rd.h"
38 #include "vpx_dsp/variance.h"
40 #define OUTPUT_FPF 0
41 #define ARF_STATS_OUTPUT 0
43 #define GROUP_ADAPTIVE_MAXQ 1
45 #define BOOST_BREAKOUT 12.5
46 #define BOOST_FACTOR 12.5
47 #define ERR_DIVISOR 128.0
48 #define FACTOR_PT_LOW 0.70
49 #define FACTOR_PT_HIGH 0.90
50 #define FIRST_PASS_Q 10.0
51 #define GF_MAX_BOOST 96.0
52 #define INTRA_MODE_PENALTY 1024
53 #define KF_MAX_BOOST 128.0
54 #define MIN_ARF_GF_BOOST 240
55 #define MIN_DECAY_FACTOR 0.01
56 #define MIN_KF_BOOST 300
57 #define NEW_MV_MODE_PENALTY 32
58 #define SVC_FACTOR_PT_LOW 0.45
59 #define DARK_THRESH 64
60 #define DEFAULT_GRP_WEIGHT 1.0
61 #define RC_FACTOR_MIN 0.75
62 #define RC_FACTOR_MAX 1.75
65 #define NCOUNT_INTRA_THRESH 8192
66 #define NCOUNT_INTRA_FACTOR 3
67 #define NCOUNT_FRAME_II_THRESH 5.0
69 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
71 #if ARF_STATS_OUTPUT
72 unsigned int arf_count = 0;
73 #endif
75 // Resets the first pass file to the given position using a relative seek from
76 // the current position.
77 static void reset_fpf_position(TWO_PASS *p,
78 const FIRSTPASS_STATS *position) {
79 p->stats_in = position;
82 // Read frame stats at an offset from the current position.
83 static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) {
84 if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) ||
85 (offset < 0 && p->stats_in + offset < p->stats_in_start)) {
86 return NULL;
89 return &p->stats_in[offset];
92 static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) {
93 if (p->stats_in >= p->stats_in_end)
94 return EOF;
96 *fps = *p->stats_in;
97 ++p->stats_in;
98 return 1;
101 static void output_stats(FIRSTPASS_STATS *stats,
102 struct vpx_codec_pkt_list *pktlist) {
103 struct vpx_codec_cx_pkt pkt;
104 pkt.kind = VPX_CODEC_STATS_PKT;
105 pkt.data.twopass_stats.buf = stats;
106 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
107 vpx_codec_pkt_list_add(pktlist, &pkt);
109 // TEMP debug code
110 #if OUTPUT_FPF
112 FILE *fpfile;
113 fpfile = fopen("firstpass.stt", "a");
115 fprintf(fpfile, "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf"
116 "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf"
117 "%12.4lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf\n",
118 stats->frame,
119 stats->weight,
120 stats->intra_error,
121 stats->coded_error,
122 stats->sr_coded_error,
123 stats->pcnt_inter,
124 stats->pcnt_motion,
125 stats->pcnt_second_ref,
126 stats->pcnt_neutral,
127 stats->intra_skip_pct,
128 stats->inactive_zone_rows,
129 stats->inactive_zone_cols,
130 stats->MVr,
131 stats->mvr_abs,
132 stats->MVc,
133 stats->mvc_abs,
134 stats->MVrv,
135 stats->MVcv,
136 stats->mv_in_out_count,
137 stats->new_mv_count,
138 stats->count,
139 stats->duration);
140 fclose(fpfile);
142 #endif
145 #if CONFIG_FP_MB_STATS
146 static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm,
147 struct vpx_codec_pkt_list *pktlist) {
148 struct vpx_codec_cx_pkt pkt;
149 pkt.kind = VPX_CODEC_FPMB_STATS_PKT;
150 pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats;
151 pkt.data.firstpass_mb_stats.sz = cm->initial_mbs * sizeof(uint8_t);
152 vpx_codec_pkt_list_add(pktlist, &pkt);
154 #endif
156 static void zero_stats(FIRSTPASS_STATS *section) {
157 section->frame = 0.0;
158 section->weight = 0.0;
159 section->intra_error = 0.0;
160 section->coded_error = 0.0;
161 section->sr_coded_error = 0.0;
162 section->pcnt_inter = 0.0;
163 section->pcnt_motion = 0.0;
164 section->pcnt_second_ref = 0.0;
165 section->pcnt_neutral = 0.0;
166 section->intra_skip_pct = 0.0;
167 section->inactive_zone_rows = 0.0;
168 section->inactive_zone_cols = 0.0;
169 section->MVr = 0.0;
170 section->mvr_abs = 0.0;
171 section->MVc = 0.0;
172 section->mvc_abs = 0.0;
173 section->MVrv = 0.0;
174 section->MVcv = 0.0;
175 section->mv_in_out_count = 0.0;
176 section->new_mv_count = 0.0;
177 section->count = 0.0;
178 section->duration = 1.0;
179 section->spatial_layer_id = 0;
182 static void accumulate_stats(FIRSTPASS_STATS *section,
183 const FIRSTPASS_STATS *frame) {
184 section->frame += frame->frame;
185 section->weight += frame->weight;
186 section->spatial_layer_id = frame->spatial_layer_id;
187 section->intra_error += frame->intra_error;
188 section->coded_error += frame->coded_error;
189 section->sr_coded_error += frame->sr_coded_error;
190 section->pcnt_inter += frame->pcnt_inter;
191 section->pcnt_motion += frame->pcnt_motion;
192 section->pcnt_second_ref += frame->pcnt_second_ref;
193 section->pcnt_neutral += frame->pcnt_neutral;
194 section->intra_skip_pct += frame->intra_skip_pct;
195 section->inactive_zone_rows += frame->inactive_zone_rows;
196 section->inactive_zone_cols += frame->inactive_zone_cols;
197 section->MVr += frame->MVr;
198 section->mvr_abs += frame->mvr_abs;
199 section->MVc += frame->MVc;
200 section->mvc_abs += frame->mvc_abs;
201 section->MVrv += frame->MVrv;
202 section->MVcv += frame->MVcv;
203 section->mv_in_out_count += frame->mv_in_out_count;
204 section->new_mv_count += frame->new_mv_count;
205 section->count += frame->count;
206 section->duration += frame->duration;
209 static void subtract_stats(FIRSTPASS_STATS *section,
210 const FIRSTPASS_STATS *frame) {
211 section->frame -= frame->frame;
212 section->weight -= frame->weight;
213 section->intra_error -= frame->intra_error;
214 section->coded_error -= frame->coded_error;
215 section->sr_coded_error -= frame->sr_coded_error;
216 section->pcnt_inter -= frame->pcnt_inter;
217 section->pcnt_motion -= frame->pcnt_motion;
218 section->pcnt_second_ref -= frame->pcnt_second_ref;
219 section->pcnt_neutral -= frame->pcnt_neutral;
220 section->intra_skip_pct -= frame->intra_skip_pct;
221 section->inactive_zone_rows -= frame->inactive_zone_rows;
222 section->inactive_zone_cols -= frame->inactive_zone_cols;
223 section->MVr -= frame->MVr;
224 section->mvr_abs -= frame->mvr_abs;
225 section->MVc -= frame->MVc;
226 section->mvc_abs -= frame->mvc_abs;
227 section->MVrv -= frame->MVrv;
228 section->MVcv -= frame->MVcv;
229 section->mv_in_out_count -= frame->mv_in_out_count;
230 section->new_mv_count -= frame->new_mv_count;
231 section->count -= frame->count;
232 section->duration -= frame->duration;
235 // Calculate an active area of the image that discounts formatting
236 // bars and partially discounts other 0 energy areas.
237 #define MIN_ACTIVE_AREA 0.5
238 #define MAX_ACTIVE_AREA 1.0
239 static double calculate_active_area(const VP9_COMP *cpi,
240 const FIRSTPASS_STATS *this_frame)
242 double active_pct;
244 active_pct = 1.0 -
245 ((this_frame->intra_skip_pct / 2) +
246 ((this_frame->inactive_zone_rows * 2) / (double)cpi->common.mb_rows));
247 return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA);
250 // Calculate a modified Error used in distributing bits between easier and
251 // harder frames.
252 #define ACT_AREA_CORRECTION 0.5
253 static double calculate_modified_err(const VP9_COMP *cpi,
254 const TWO_PASS *twopass,
255 const VP9EncoderConfig *oxcf,
256 const FIRSTPASS_STATS *this_frame) {
257 const FIRSTPASS_STATS *const stats = &twopass->total_stats;
258 const double av_weight = stats->weight / stats->count;
259 const double av_err = (stats->coded_error * av_weight) / stats->count;
260 double modified_error =
261 av_err * pow(this_frame->coded_error * this_frame->weight /
262 DOUBLE_DIVIDE_CHECK(av_err), oxcf->two_pass_vbrbias / 100.0);
264 // Correction for active area. Frames with a reduced active area
265 // (eg due to formatting bars) have a higher error per mb for the
266 // remaining active MBs. The correction here assumes that coding
267 // 0.5N blocks of complexity 2X is a little easier than coding N
268 // blocks of complexity X.
269 modified_error *=
270 pow(calculate_active_area(cpi, this_frame), ACT_AREA_CORRECTION);
272 return fclamp(modified_error,
273 twopass->modified_error_min, twopass->modified_error_max);
276 // This function returns the maximum target rate per frame.
277 static int frame_max_bits(const RATE_CONTROL *rc,
278 const VP9EncoderConfig *oxcf) {
279 int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
280 (int64_t)oxcf->two_pass_vbrmax_section) / 100;
281 if (max_bits < 0)
282 max_bits = 0;
283 else if (max_bits > rc->max_frame_bandwidth)
284 max_bits = rc->max_frame_bandwidth;
286 return (int)max_bits;
289 void vp9_init_first_pass(VP9_COMP *cpi) {
290 zero_stats(&cpi->twopass.total_stats);
293 void vp9_end_first_pass(VP9_COMP *cpi) {
294 if (is_two_pass_svc(cpi)) {
295 int i;
296 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
297 output_stats(&cpi->svc.layer_context[i].twopass.total_stats,
298 cpi->output_pkt_list);
300 } else {
301 output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
305 static vpx_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
306 switch (bsize) {
307 case BLOCK_8X8:
308 return vpx_mse8x8;
309 case BLOCK_16X8:
310 return vpx_mse16x8;
311 case BLOCK_8X16:
312 return vpx_mse8x16;
313 default:
314 return vpx_mse16x16;
318 static unsigned int get_prediction_error(BLOCK_SIZE bsize,
319 const struct buf_2d *src,
320 const struct buf_2d *ref) {
321 unsigned int sse;
322 const vpx_variance_fn_t fn = get_block_variance_fn(bsize);
323 fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
324 return sse;
327 #if CONFIG_VP9_HIGHBITDEPTH
328 static vpx_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize,
329 int bd) {
330 switch (bd) {
331 default:
332 switch (bsize) {
333 case BLOCK_8X8:
334 return vpx_highbd_8_mse8x8;
335 case BLOCK_16X8:
336 return vpx_highbd_8_mse16x8;
337 case BLOCK_8X16:
338 return vpx_highbd_8_mse8x16;
339 default:
340 return vpx_highbd_8_mse16x16;
342 break;
343 case 10:
344 switch (bsize) {
345 case BLOCK_8X8:
346 return vpx_highbd_10_mse8x8;
347 case BLOCK_16X8:
348 return vpx_highbd_10_mse16x8;
349 case BLOCK_8X16:
350 return vpx_highbd_10_mse8x16;
351 default:
352 return vpx_highbd_10_mse16x16;
354 break;
355 case 12:
356 switch (bsize) {
357 case BLOCK_8X8:
358 return vpx_highbd_12_mse8x8;
359 case BLOCK_16X8:
360 return vpx_highbd_12_mse16x8;
361 case BLOCK_8X16:
362 return vpx_highbd_12_mse8x16;
363 default:
364 return vpx_highbd_12_mse16x16;
366 break;
370 static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize,
371 const struct buf_2d *src,
372 const struct buf_2d *ref,
373 int bd) {
374 unsigned int sse;
375 const vpx_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
376 fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
377 return sse;
379 #endif // CONFIG_VP9_HIGHBITDEPTH
381 // Refine the motion search range according to the frame dimension
382 // for first pass test.
383 static int get_search_range(const VP9_COMP *cpi) {
384 int sr = 0;
385 const int dim = MIN(cpi->initial_width, cpi->initial_height);
387 while ((dim << sr) < MAX_FULL_PEL_VAL)
388 ++sr;
389 return sr;
392 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
393 const MV *ref_mv, MV *best_mv,
394 int *best_motion_err) {
395 MACROBLOCKD *const xd = &x->e_mbd;
396 MV tmp_mv = {0, 0};
397 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3};
398 int num00, tmp_err, n;
399 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
400 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
401 const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
403 int step_param = 3;
404 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
405 const int sr = get_search_range(cpi);
406 step_param += sr;
407 further_steps -= sr;
409 // Override the default variance function to use MSE.
410 v_fn_ptr.vf = get_block_variance_fn(bsize);
411 #if CONFIG_VP9_HIGHBITDEPTH
412 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
413 v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd);
415 #endif // CONFIG_VP9_HIGHBITDEPTH
417 // Center the initial step/diamond search on best mv.
418 tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
419 step_param,
420 x->sadperbit16, &num00, &v_fn_ptr, ref_mv);
421 if (tmp_err < INT_MAX)
422 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
423 if (tmp_err < INT_MAX - new_mv_mode_penalty)
424 tmp_err += new_mv_mode_penalty;
426 if (tmp_err < *best_motion_err) {
427 *best_motion_err = tmp_err;
428 *best_mv = tmp_mv;
431 // Carry out further step/diamond searches as necessary.
432 n = num00;
433 num00 = 0;
435 while (n < further_steps) {
436 ++n;
438 if (num00) {
439 --num00;
440 } else {
441 tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
442 step_param + n, x->sadperbit16,
443 &num00, &v_fn_ptr, ref_mv);
444 if (tmp_err < INT_MAX)
445 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
446 if (tmp_err < INT_MAX - new_mv_mode_penalty)
447 tmp_err += new_mv_mode_penalty;
449 if (tmp_err < *best_motion_err) {
450 *best_motion_err = tmp_err;
451 *best_mv = tmp_mv;
457 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
458 if (2 * mb_col + 1 < cm->mi_cols) {
459 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16
460 : BLOCK_16X8;
461 } else {
462 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16
463 : BLOCK_8X8;
467 static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
468 int i;
470 for (i = 0; i < QINDEX_RANGE; ++i)
471 if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q)
472 break;
474 if (i == QINDEX_RANGE)
475 i--;
477 return i;
480 static void set_first_pass_params(VP9_COMP *cpi) {
481 VP9_COMMON *const cm = &cpi->common;
482 if (!cpi->refresh_alt_ref_frame &&
483 (cm->current_video_frame == 0 ||
484 (cpi->frame_flags & FRAMEFLAGS_KEY))) {
485 cm->frame_type = KEY_FRAME;
486 } else {
487 cm->frame_type = INTER_FRAME;
489 // Do not use periodic key frames.
490 cpi->rc.frames_to_key = INT_MAX;
493 #define UL_INTRA_THRESH 50
494 #define INVALID_ROW -1
495 void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
496 int mb_row, mb_col;
497 MACROBLOCK *const x = &cpi->td.mb;
498 VP9_COMMON *const cm = &cpi->common;
499 MACROBLOCKD *const xd = &x->e_mbd;
500 TileInfo tile;
501 struct macroblock_plane *const p = x->plane;
502 struct macroblockd_plane *const pd = xd->plane;
503 const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
504 int i;
506 int recon_yoffset, recon_uvoffset;
507 int64_t intra_error = 0;
508 int64_t coded_error = 0;
509 int64_t sr_coded_error = 0;
511 int sum_mvr = 0, sum_mvc = 0;
512 int sum_mvr_abs = 0, sum_mvc_abs = 0;
513 int64_t sum_mvrs = 0, sum_mvcs = 0;
514 int mvcount = 0;
515 int intercount = 0;
516 int second_ref_count = 0;
517 const int intrapenalty = INTRA_MODE_PENALTY;
518 double neutral_count;
519 int intra_skip_count = 0;
520 int image_data_start_row = INVALID_ROW;
521 int new_mv_count = 0;
522 int sum_in_vectors = 0;
523 MV lastmv = {0, 0};
524 TWO_PASS *twopass = &cpi->twopass;
525 const MV zero_mv = {0, 0};
526 int recon_y_stride, recon_uv_stride, uv_mb_height;
528 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
529 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
530 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
531 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
533 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
534 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL;
535 double intra_factor;
536 double brightness_factor;
537 BufferPool *const pool = cm->buffer_pool;
539 // First pass code requires valid last and new frame buffers.
540 assert(new_yv12 != NULL);
541 assert((lc != NULL) || frame_is_intra_only(cm) || (lst_yv12 != NULL));
543 #if CONFIG_FP_MB_STATS
544 if (cpi->use_fp_mb_stats) {
545 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs);
547 #endif
549 vp9_clear_system_state();
551 intra_factor = 0.0;
552 brightness_factor = 0.0;
553 neutral_count = 0.0;
555 set_first_pass_params(cpi);
556 vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
558 if (lc != NULL) {
559 twopass = &lc->twopass;
561 cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
562 cpi->ref_frame_flags = VP9_LAST_FLAG;
564 if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id <
565 REF_FRAMES) {
566 cpi->gld_fb_idx =
567 cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id;
568 cpi->ref_frame_flags |= VP9_GOLD_FLAG;
569 cpi->refresh_golden_frame = (lc->current_video_frame_in_layer == 0);
570 } else {
571 cpi->refresh_golden_frame = 0;
574 if (lc->current_video_frame_in_layer == 0)
575 cpi->ref_frame_flags = 0;
577 vp9_scale_references(cpi);
579 // Use either last frame or alt frame for motion search.
580 if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
581 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
582 if (first_ref_buf == NULL)
583 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME);
586 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
587 gld_yv12 = vp9_get_scaled_ref_frame(cpi, GOLDEN_FRAME);
588 if (gld_yv12 == NULL) {
589 gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
591 } else {
592 gld_yv12 = NULL;
595 set_ref_ptrs(cm, xd,
596 (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME: NONE,
597 (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE);
599 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
600 &cpi->scaled_source);
603 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
605 vp9_setup_src_planes(x, cpi->Source, 0, 0);
606 vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
608 if (!frame_is_intra_only(cm)) {
609 vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
612 xd->mi = cm->mi_grid_visible;
613 xd->mi[0] = cm->mi;
615 vp9_frame_init_quantizer(cpi);
617 for (i = 0; i < MAX_MB_PLANE; ++i) {
618 p[i].coeff = ctx->coeff_pbuf[i][1];
619 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
620 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
621 p[i].eobs = ctx->eobs_pbuf[i][1];
623 x->skip_recode = 0;
625 vp9_init_mv_probs(cm);
626 vp9_initialize_rd_consts(cpi);
628 // Tiling is ignored in the first pass.
629 vp9_tile_init(&tile, cm, 0, 0);
631 recon_y_stride = new_yv12->y_stride;
632 recon_uv_stride = new_yv12->uv_stride;
633 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
635 for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
636 MV best_ref_mv = {0, 0};
638 // Reset above block coeffs.
639 xd->up_available = (mb_row != 0);
640 recon_yoffset = (mb_row * recon_y_stride * 16);
641 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height);
643 // Set up limit values for motion vectors to prevent them extending
644 // outside the UMV borders.
645 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
646 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
647 + BORDER_MV_PIXELS_B16;
649 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
650 int this_error;
651 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
652 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
653 double log_intra;
654 int level_sample;
656 #if CONFIG_FP_MB_STATS
657 const int mb_index = mb_row * cm->mb_cols + mb_col;
658 #endif
660 vp9_clear_system_state();
662 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
663 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
664 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
665 xd->left_available = (mb_col != 0);
666 xd->mi[0]->mbmi.sb_type = bsize;
667 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME;
668 set_mi_row_col(xd, &tile,
669 mb_row << 1, num_8x8_blocks_high_lookup[bsize],
670 mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
671 cm->mi_rows, cm->mi_cols);
673 // Do intra 16x16 prediction.
674 x->skip_encode = 0;
675 xd->mi[0]->mbmi.mode = DC_PRED;
676 xd->mi[0]->mbmi.tx_size = use_dc_pred ?
677 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
678 vp9_encode_intra_block_plane(x, bsize, 0);
679 this_error = vpx_get_mb_ss(x->plane[0].src_diff);
681 // Keep a record of blocks that have almost no intra error residual
682 // (i.e. are in effect completely flat and untextured in the intra
683 // domain). In natural videos this is uncommon, but it is much more
684 // common in animations, graphics and screen content, so may be used
685 // as a signal to detect these types of content.
686 if (this_error < UL_INTRA_THRESH) {
687 ++intra_skip_count;
688 } else if ((mb_col > 0) && (image_data_start_row == INVALID_ROW)) {
689 image_data_start_row = mb_row;
692 #if CONFIG_VP9_HIGHBITDEPTH
693 if (cm->use_highbitdepth) {
694 switch (cm->bit_depth) {
695 case VPX_BITS_8:
696 break;
697 case VPX_BITS_10:
698 this_error >>= 4;
699 break;
700 case VPX_BITS_12:
701 this_error >>= 8;
702 break;
703 default:
704 assert(0 && "cm->bit_depth should be VPX_BITS_8, "
705 "VPX_BITS_10 or VPX_BITS_12");
706 return;
709 #endif // CONFIG_VP9_HIGHBITDEPTH
711 vp9_clear_system_state();
712 log_intra = log(this_error + 1.0);
713 if (log_intra < 10.0)
714 intra_factor += 1.0 + ((10.0 - log_intra) * 0.05);
715 else
716 intra_factor += 1.0;
718 #if CONFIG_VP9_HIGHBITDEPTH
719 if (cm->use_highbitdepth)
720 level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0];
721 else
722 level_sample = x->plane[0].src.buf[0];
723 #else
724 level_sample = x->plane[0].src.buf[0];
725 #endif
726 if ((level_sample < DARK_THRESH) && (log_intra < 9.0))
727 brightness_factor += 1.0 + (0.01 * (DARK_THRESH - level_sample));
728 else
729 brightness_factor += 1.0;
731 // Intrapenalty below deals with situations where the intra and inter
732 // error scores are very low (e.g. a plain black frame).
733 // We do not have special cases in first pass for 0,0 and nearest etc so
734 // all inter modes carry an overhead cost estimate for the mv.
735 // When the error score is very low this causes us to pick all or lots of
736 // INTRA modes and throw lots of key frames.
737 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
738 this_error += intrapenalty;
740 // Accumulate the intra error.
741 intra_error += (int64_t)this_error;
743 #if CONFIG_FP_MB_STATS
744 if (cpi->use_fp_mb_stats) {
745 // initialization
746 cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
748 #endif
750 // Set up limit values for motion vectors to prevent them extending
751 // outside the UMV borders.
752 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
753 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
755 // Other than for the first frame do a motion search.
756 if ((lc == NULL && cm->current_video_frame > 0) ||
757 (lc != NULL && lc->current_video_frame_in_layer > 0)) {
758 int tmp_err, motion_error, raw_motion_error;
759 // Assume 0,0 motion with no mv overhead.
760 MV mv = {0, 0} , tmp_mv = {0, 0};
761 struct buf_2d unscaled_last_source_buf_2d;
763 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
764 #if CONFIG_VP9_HIGHBITDEPTH
765 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
766 motion_error = highbd_get_prediction_error(
767 bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
768 } else {
769 motion_error = get_prediction_error(
770 bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
772 #else
773 motion_error = get_prediction_error(
774 bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
775 #endif // CONFIG_VP9_HIGHBITDEPTH
777 // Compute the motion error of the 0,0 motion using the last source
778 // frame as the reference. Skip the further motion search on
779 // reconstructed frame if this error is small.
780 unscaled_last_source_buf_2d.buf =
781 cpi->unscaled_last_source->y_buffer + recon_yoffset;
782 unscaled_last_source_buf_2d.stride =
783 cpi->unscaled_last_source->y_stride;
784 #if CONFIG_VP9_HIGHBITDEPTH
785 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
786 raw_motion_error = highbd_get_prediction_error(
787 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d, xd->bd);
788 } else {
789 raw_motion_error = get_prediction_error(
790 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d);
792 #else
793 raw_motion_error = get_prediction_error(
794 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d);
795 #endif // CONFIG_VP9_HIGHBITDEPTH
797 // TODO(pengchong): Replace the hard-coded threshold
798 if (raw_motion_error > 25 || lc != NULL) {
799 // Test last reference frame using the previous best mv as the
800 // starting point (best reference) for the search.
801 first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error);
803 // If the current best reference mv is not centered on 0,0 then do a
804 // 0,0 based search as well.
805 if (!is_zero_mv(&best_ref_mv)) {
806 tmp_err = INT_MAX;
807 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
809 if (tmp_err < motion_error) {
810 motion_error = tmp_err;
811 mv = tmp_mv;
815 // Search in an older reference frame.
816 if (((lc == NULL && cm->current_video_frame > 1) ||
817 (lc != NULL && lc->current_video_frame_in_layer > 1))
818 && gld_yv12 != NULL) {
819 // Assume 0,0 motion with no mv overhead.
820 int gf_motion_error;
822 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
823 #if CONFIG_VP9_HIGHBITDEPTH
824 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
825 gf_motion_error = highbd_get_prediction_error(
826 bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
827 } else {
828 gf_motion_error = get_prediction_error(
829 bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
831 #else
832 gf_motion_error = get_prediction_error(
833 bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
834 #endif // CONFIG_VP9_HIGHBITDEPTH
836 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv,
837 &gf_motion_error);
839 if (gf_motion_error < motion_error && gf_motion_error < this_error)
840 ++second_ref_count;
842 // Reset to last frame as reference buffer.
843 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
844 xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
845 xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
847 // In accumulating a score for the older reference frame take the
848 // best of the motion predicted score and the intra coded error
849 // (just as will be done for) accumulation of "coded_error" for
850 // the last frame.
851 if (gf_motion_error < this_error)
852 sr_coded_error += gf_motion_error;
853 else
854 sr_coded_error += this_error;
855 } else {
856 sr_coded_error += motion_error;
858 } else {
859 sr_coded_error += motion_error;
862 // Start by assuming that intra mode is best.
863 best_ref_mv.row = 0;
864 best_ref_mv.col = 0;
866 #if CONFIG_FP_MB_STATS
867 if (cpi->use_fp_mb_stats) {
868 // intra predication statistics
869 cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
870 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK;
871 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
872 if (this_error > FPMB_ERROR_LARGE_TH) {
873 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
874 } else if (this_error < FPMB_ERROR_SMALL_TH) {
875 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
878 #endif
880 if (motion_error <= this_error) {
881 vp9_clear_system_state();
883 // Keep a count of cases where the inter and intra were very close
884 // and very low. This helps with scene cut detection for example in
885 // cropped clips with black bars at the sides or top and bottom.
886 if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
887 (this_error < (2 * intrapenalty))) {
888 neutral_count += 1.0;
889 // Also track cases where the intra is not much worse than the inter
890 // and use this in limiting the GF/arf group length.
891 } else if ((this_error > NCOUNT_INTRA_THRESH) &&
892 (this_error < (NCOUNT_INTRA_FACTOR * motion_error))) {
893 neutral_count += (double)motion_error /
894 DOUBLE_DIVIDE_CHECK((double)this_error);
897 mv.row *= 8;
898 mv.col *= 8;
899 this_error = motion_error;
900 xd->mi[0]->mbmi.mode = NEWMV;
901 xd->mi[0]->mbmi.mv[0].as_mv = mv;
902 xd->mi[0]->mbmi.tx_size = TX_4X4;
903 xd->mi[0]->mbmi.ref_frame[0] = LAST_FRAME;
904 xd->mi[0]->mbmi.ref_frame[1] = NONE;
905 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
906 vp9_encode_sby_pass1(x, bsize);
907 sum_mvr += mv.row;
908 sum_mvr_abs += abs(mv.row);
909 sum_mvc += mv.col;
910 sum_mvc_abs += abs(mv.col);
911 sum_mvrs += mv.row * mv.row;
912 sum_mvcs += mv.col * mv.col;
913 ++intercount;
915 best_ref_mv = mv;
917 #if CONFIG_FP_MB_STATS
918 if (cpi->use_fp_mb_stats) {
919 // inter predication statistics
920 cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
921 cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK;
922 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
923 if (this_error > FPMB_ERROR_LARGE_TH) {
924 cpi->twopass.frame_mb_stats_buf[mb_index] |=
925 FPMB_ERROR_LARGE_MASK;
926 } else if (this_error < FPMB_ERROR_SMALL_TH) {
927 cpi->twopass.frame_mb_stats_buf[mb_index] |=
928 FPMB_ERROR_SMALL_MASK;
931 #endif
933 if (!is_zero_mv(&mv)) {
934 ++mvcount;
936 #if CONFIG_FP_MB_STATS
937 if (cpi->use_fp_mb_stats) {
938 cpi->twopass.frame_mb_stats_buf[mb_index] &=
939 ~FPMB_MOTION_ZERO_MASK;
940 // check estimated motion direction
941 if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) {
942 // right direction
943 cpi->twopass.frame_mb_stats_buf[mb_index] |=
944 FPMB_MOTION_RIGHT_MASK;
945 } else if (mv.as_mv.row < 0 &&
946 abs(mv.as_mv.row) >= abs(mv.as_mv.col)) {
947 // up direction
948 cpi->twopass.frame_mb_stats_buf[mb_index] |=
949 FPMB_MOTION_UP_MASK;
950 } else if (mv.as_mv.col < 0 &&
951 abs(mv.as_mv.col) >= abs(mv.as_mv.row)) {
952 // left direction
953 cpi->twopass.frame_mb_stats_buf[mb_index] |=
954 FPMB_MOTION_LEFT_MASK;
955 } else {
956 // down direction
957 cpi->twopass.frame_mb_stats_buf[mb_index] |=
958 FPMB_MOTION_DOWN_MASK;
961 #endif
963 // Non-zero vector, was it different from the last non zero vector?
964 if (!is_equal_mv(&mv, &lastmv))
965 ++new_mv_count;
966 lastmv = mv;
968 // Does the row vector point inwards or outwards?
969 if (mb_row < cm->mb_rows / 2) {
970 if (mv.row > 0)
971 --sum_in_vectors;
972 else if (mv.row < 0)
973 ++sum_in_vectors;
974 } else if (mb_row > cm->mb_rows / 2) {
975 if (mv.row > 0)
976 ++sum_in_vectors;
977 else if (mv.row < 0)
978 --sum_in_vectors;
981 // Does the col vector point inwards or outwards?
982 if (mb_col < cm->mb_cols / 2) {
983 if (mv.col > 0)
984 --sum_in_vectors;
985 else if (mv.col < 0)
986 ++sum_in_vectors;
987 } else if (mb_col > cm->mb_cols / 2) {
988 if (mv.col > 0)
989 ++sum_in_vectors;
990 else if (mv.col < 0)
991 --sum_in_vectors;
995 } else {
996 sr_coded_error += (int64_t)this_error;
998 coded_error += (int64_t)this_error;
1000 // Adjust to the next column of MBs.
1001 x->plane[0].src.buf += 16;
1002 x->plane[1].src.buf += uv_mb_height;
1003 x->plane[2].src.buf += uv_mb_height;
1005 recon_yoffset += 16;
1006 recon_uvoffset += uv_mb_height;
1009 // Adjust to the next row of MBs.
1010 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
1011 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
1012 uv_mb_height * cm->mb_cols;
1013 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
1014 uv_mb_height * cm->mb_cols;
1016 vp9_clear_system_state();
1019 // Clamp the image start to rows/2. This number of rows is discarded top
1020 // and bottom as dead data so rows / 2 means the frame is blank.
1021 if ((image_data_start_row > cm->mb_rows / 2) ||
1022 (image_data_start_row == INVALID_ROW)) {
1023 image_data_start_row = cm->mb_rows / 2;
1025 // Exclude any image dead zone
1026 if (image_data_start_row > 0) {
1027 intra_skip_count =
1028 MAX(0, intra_skip_count - (image_data_start_row * cm->mb_cols * 2));
1032 FIRSTPASS_STATS fps;
1033 // The minimum error here insures some bit allocation to frames even
1034 // in static regions. The allocation per MB declines for larger formats
1035 // where the typical "real" energy per MB also falls.
1036 // Initial estimate here uses sqrt(mbs) to define the min_err, where the
1037 // number of mbs is proportional to the image area.
1038 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1039 ? cpi->initial_mbs : cpi->common.MBs;
1040 const double min_err = 200 * sqrt(num_mbs);
1042 intra_factor = intra_factor / (double)num_mbs;
1043 brightness_factor = brightness_factor / (double)num_mbs;
1044 fps.weight = intra_factor * brightness_factor;
1046 fps.frame = cm->current_video_frame;
1047 fps.spatial_layer_id = cpi->svc.spatial_layer_id;
1048 fps.coded_error = (double)(coded_error >> 8) + min_err;
1049 fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
1050 fps.intra_error = (double)(intra_error >> 8) + min_err;
1051 fps.count = 1.0;
1052 fps.pcnt_inter = (double)intercount / num_mbs;
1053 fps.pcnt_second_ref = (double)second_ref_count / num_mbs;
1054 fps.pcnt_neutral = (double)neutral_count / num_mbs;
1055 fps.intra_skip_pct = (double)intra_skip_count / num_mbs;
1056 fps.inactive_zone_rows = (double)image_data_start_row;
1057 fps.inactive_zone_cols = (double)0; // TODO(paulwilkins): fix
1059 if (mvcount > 0) {
1060 fps.MVr = (double)sum_mvr / mvcount;
1061 fps.mvr_abs = (double)sum_mvr_abs / mvcount;
1062 fps.MVc = (double)sum_mvc / mvcount;
1063 fps.mvc_abs = (double)sum_mvc_abs / mvcount;
1064 fps.MVrv = ((double)sum_mvrs -
1065 ((double)sum_mvr * sum_mvr / mvcount)) / mvcount;
1066 fps.MVcv = ((double)sum_mvcs -
1067 ((double)sum_mvc * sum_mvc / mvcount)) / mvcount;
1068 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
1069 fps.new_mv_count = new_mv_count;
1070 fps.pcnt_motion = (double)mvcount / num_mbs;
1071 } else {
1072 fps.MVr = 0.0;
1073 fps.mvr_abs = 0.0;
1074 fps.MVc = 0.0;
1075 fps.mvc_abs = 0.0;
1076 fps.MVrv = 0.0;
1077 fps.MVcv = 0.0;
1078 fps.mv_in_out_count = 0.0;
1079 fps.new_mv_count = 0.0;
1080 fps.pcnt_motion = 0.0;
1083 // TODO(paulwilkins): Handle the case when duration is set to 0, or
1084 // something less than the full time between subsequent values of
1085 // cpi->source_time_stamp.
1086 fps.duration = (double)(source->ts_end - source->ts_start);
1088 // Don't want to do output stats with a stack variable!
1089 twopass->this_frame_stats = fps;
1090 output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
1091 accumulate_stats(&twopass->total_stats, &fps);
1093 #if CONFIG_FP_MB_STATS
1094 if (cpi->use_fp_mb_stats) {
1095 output_fpmb_stats(twopass->frame_mb_stats_buf, cm, cpi->output_pkt_list);
1097 #endif
1100 // Copy the previous Last Frame back into gf and and arf buffers if
1101 // the prediction is good enough... but also don't allow it to lag too far.
1102 if ((twopass->sr_update_lag > 3) ||
1103 ((cm->current_video_frame > 0) &&
1104 (twopass->this_frame_stats.pcnt_inter > 0.20) &&
1105 ((twopass->this_frame_stats.intra_error /
1106 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
1107 if (gld_yv12 != NULL) {
1108 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
1109 cm->ref_frame_map[cpi->lst_fb_idx]);
1111 twopass->sr_update_lag = 1;
1112 } else {
1113 ++twopass->sr_update_lag;
1116 vp9_extend_frame_borders(new_yv12);
1118 if (lc != NULL) {
1119 vp9_update_reference_frames(cpi);
1120 } else {
1121 // The frame we just compressed now becomes the last frame.
1122 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
1123 cm->new_fb_idx);
1126 // Special case for the first frame. Copy into the GF buffer as a second
1127 // reference.
1128 if (cm->current_video_frame == 0 && cpi->gld_fb_idx != INVALID_IDX &&
1129 lc == NULL) {
1130 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
1131 cm->ref_frame_map[cpi->lst_fb_idx]);
1134 // Use this to see what the first pass reconstruction looks like.
1135 if (0) {
1136 char filename[512];
1137 FILE *recon_file;
1138 snprintf(filename, sizeof(filename), "enc%04d.yuv",
1139 (int)cm->current_video_frame);
1141 if (cm->current_video_frame == 0)
1142 recon_file = fopen(filename, "wb");
1143 else
1144 recon_file = fopen(filename, "ab");
1146 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
1147 fclose(recon_file);
1150 ++cm->current_video_frame;
1151 if (cpi->use_svc)
1152 vp9_inc_frame_in_layer(cpi);
1155 static double calc_correction_factor(double err_per_mb,
1156 double err_divisor,
1157 double pt_low,
1158 double pt_high,
1159 int q,
1160 vpx_bit_depth_t bit_depth) {
1161 const double error_term = err_per_mb / err_divisor;
1163 // Adjustment based on actual quantizer to power term.
1164 const double power_term =
1165 MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high);
1167 // Calculate correction factor.
1168 if (power_term < 1.0)
1169 assert(error_term >= 0.0);
1171 return fclamp(pow(error_term, power_term), 0.05, 5.0);
1174 // Larger image formats are expected to be a little harder to code relatively
1175 // given the same prediction error score. This in part at least relates to the
1176 // increased size and hence coding cost of motion vectors.
1177 #define EDIV_SIZE_FACTOR 800
1179 static int get_twopass_worst_quality(const VP9_COMP *cpi,
1180 const double section_err,
1181 double inactive_zone,
1182 int section_target_bandwidth,
1183 double group_weight_factor) {
1184 const RATE_CONTROL *const rc = &cpi->rc;
1185 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1187 inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
1189 if (section_target_bandwidth <= 0) {
1190 return rc->worst_quality; // Highest value allowed
1191 } else {
1192 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1193 ? cpi->initial_mbs : cpi->common.MBs;
1194 const int active_mbs = MAX(1, num_mbs - (int)(num_mbs * inactive_zone));
1195 const double av_err_per_mb = section_err / active_mbs;
1196 const double speed_term = 1.0 + 0.04 * oxcf->speed;
1197 const double ediv_size_correction = (double)num_mbs / EDIV_SIZE_FACTOR;
1198 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth <<
1199 BPER_MB_NORMBITS) / active_mbs;
1201 int q;
1202 int is_svc_upper_layer = 0;
1204 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
1205 is_svc_upper_layer = 1;
1208 // Try and pick a max Q that will be high enough to encode the
1209 // content at the given rate.
1210 for (q = rc->best_quality; q < rc->worst_quality; ++q) {
1211 const double factor =
1212 calc_correction_factor(av_err_per_mb,
1213 ERR_DIVISOR - ediv_size_correction,
1214 is_svc_upper_layer ? SVC_FACTOR_PT_LOW :
1215 FACTOR_PT_LOW, FACTOR_PT_HIGH, q,
1216 cpi->common.bit_depth);
1217 const int bits_per_mb =
1218 vp9_rc_bits_per_mb(INTER_FRAME, q,
1219 factor * speed_term * group_weight_factor,
1220 cpi->common.bit_depth);
1221 if (bits_per_mb <= target_norm_bits_per_mb)
1222 break;
1225 // Restriction on active max q for constrained quality mode.
1226 if (cpi->oxcf.rc_mode == VPX_CQ)
1227 q = MAX(q, oxcf->cq_level);
1228 return q;
1232 static void setup_rf_level_maxq(VP9_COMP *cpi) {
1233 int i;
1234 RATE_CONTROL *const rc = &cpi->rc;
1235 for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) {
1236 int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality);
1237 rc->rf_level_maxq[i] = MAX(rc->worst_quality + qdelta, rc->best_quality);
1241 void vp9_init_subsampling(VP9_COMP *cpi) {
1242 const VP9_COMMON *const cm = &cpi->common;
1243 RATE_CONTROL *const rc = &cpi->rc;
1244 const int w = cm->width;
1245 const int h = cm->height;
1246 int i;
1248 for (i = 0; i < FRAME_SCALE_STEPS; ++i) {
1249 // Note: Frames with odd-sized dimensions may result from this scaling.
1250 rc->frame_width[i] = (w * 16) / frame_scale_factor[i];
1251 rc->frame_height[i] = (h * 16) / frame_scale_factor[i];
1254 setup_rf_level_maxq(cpi);
1257 void calculate_coded_size(VP9_COMP *cpi,
1258 int *scaled_frame_width,
1259 int *scaled_frame_height) {
1260 RATE_CONTROL *const rc = &cpi->rc;
1261 *scaled_frame_width = rc->frame_width[rc->frame_size_selector];
1262 *scaled_frame_height = rc->frame_height[rc->frame_size_selector];
1265 void vp9_init_second_pass(VP9_COMP *cpi) {
1266 SVC *const svc = &cpi->svc;
1267 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1268 const int is_two_pass_svc = (svc->number_spatial_layers > 1) ||
1269 (svc->number_temporal_layers > 1);
1270 TWO_PASS *const twopass = is_two_pass_svc ?
1271 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
1272 double frame_rate;
1273 FIRSTPASS_STATS *stats;
1275 zero_stats(&twopass->total_stats);
1276 zero_stats(&twopass->total_left_stats);
1278 if (!twopass->stats_in_end)
1279 return;
1281 stats = &twopass->total_stats;
1283 *stats = *twopass->stats_in_end;
1284 twopass->total_left_stats = *stats;
1286 frame_rate = 10000000.0 * stats->count / stats->duration;
1287 // Each frame can have a different duration, as the frame rate in the source
1288 // isn't guaranteed to be constant. The frame rate prior to the first frame
1289 // encoded in the second pass is a guess. However, the sum duration is not.
1290 // It is calculated based on the actual durations of all frames from the
1291 // first pass.
1293 if (is_two_pass_svc) {
1294 vp9_update_spatial_layer_framerate(cpi, frame_rate);
1295 twopass->bits_left = (int64_t)(stats->duration *
1296 svc->layer_context[svc->spatial_layer_id].target_bandwidth /
1297 10000000.0);
1298 } else {
1299 vp9_new_framerate(cpi, frame_rate);
1300 twopass->bits_left = (int64_t)(stats->duration * oxcf->target_bandwidth /
1301 10000000.0);
1304 // This variable monitors how far behind the second ref update is lagging.
1305 twopass->sr_update_lag = 1;
1307 // Scan the first pass file and calculate a modified total error based upon
1308 // the bias/power function used to allocate bits.
1310 const double avg_error = stats->coded_error /
1311 DOUBLE_DIVIDE_CHECK(stats->count);
1312 const FIRSTPASS_STATS *s = twopass->stats_in;
1313 double modified_error_total = 0.0;
1314 twopass->modified_error_min = (avg_error *
1315 oxcf->two_pass_vbrmin_section) / 100;
1316 twopass->modified_error_max = (avg_error *
1317 oxcf->two_pass_vbrmax_section) / 100;
1318 while (s < twopass->stats_in_end) {
1319 modified_error_total += calculate_modified_err(cpi, twopass, oxcf, s);
1320 ++s;
1322 twopass->modified_error_left = modified_error_total;
1325 // Reset the vbr bits off target counters
1326 cpi->rc.vbr_bits_off_target = 0;
1327 cpi->rc.vbr_bits_off_target_fast = 0;
1329 cpi->rc.rate_error_estimate = 0;
1331 // Static sequence monitor variables.
1332 twopass->kf_zeromotion_pct = 100;
1333 twopass->last_kfgroup_zeromotion_pct = 100;
1335 if (oxcf->resize_mode != RESIZE_NONE) {
1336 vp9_init_subsampling(cpi);
1340 #define SR_DIFF_PART 0.0015
1341 #define MOTION_AMP_PART 0.003
1342 #define INTRA_PART 0.005
1343 #define DEFAULT_DECAY_LIMIT 0.75
1344 #define LOW_SR_DIFF_TRHESH 0.1
1345 #define SR_DIFF_MAX 128.0
1347 static double get_sr_decay_rate(const VP9_COMP *cpi,
1348 const FIRSTPASS_STATS *frame) {
1349 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1350 ? cpi->initial_mbs : cpi->common.MBs;
1351 double sr_diff =
1352 (frame->sr_coded_error - frame->coded_error) / num_mbs;
1353 double sr_decay = 1.0;
1354 double modified_pct_inter;
1355 double modified_pcnt_intra;
1356 const double motion_amplitude_factor =
1357 frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) / 2);
1359 modified_pct_inter = frame->pcnt_inter;
1360 if ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) <
1361 (double)NCOUNT_FRAME_II_THRESH) {
1362 modified_pct_inter = frame->pcnt_inter - frame->pcnt_neutral;
1364 modified_pcnt_intra = 100 * (1.0 - modified_pct_inter);
1367 if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1368 sr_diff = MIN(sr_diff, SR_DIFF_MAX);
1369 sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) -
1370 (MOTION_AMP_PART * motion_amplitude_factor) -
1371 (INTRA_PART * modified_pcnt_intra);
1373 return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, modified_pct_inter));
1376 // This function gives an estimate of how badly we believe the prediction
1377 // quality is decaying from frame to frame.
1378 static double get_zero_motion_factor(const VP9_COMP *cpi,
1379 const FIRSTPASS_STATS *frame) {
1380 const double zero_motion_pct = frame->pcnt_inter -
1381 frame->pcnt_motion;
1382 double sr_decay = get_sr_decay_rate(cpi, frame);
1383 return MIN(sr_decay, zero_motion_pct);
1386 #define ZM_POWER_FACTOR 0.75
1388 static double get_prediction_decay_rate(const VP9_COMP *cpi,
1389 const FIRSTPASS_STATS *next_frame) {
1390 const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame);
1391 const double zero_motion_factor =
1392 (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
1393 ZM_POWER_FACTOR));
1395 return MAX(zero_motion_factor,
1396 (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1399 // Function to test for a condition where a complex transition is followed
1400 // by a static section. For example in slide shows where there is a fade
1401 // between slides. This is to help with more optimal kf and gf positioning.
1402 static int detect_transition_to_still(VP9_COMP *cpi,
1403 int frame_interval, int still_interval,
1404 double loop_decay_rate,
1405 double last_decay_rate) {
1406 TWO_PASS *const twopass = &cpi->twopass;
1407 RATE_CONTROL *const rc = &cpi->rc;
1409 // Break clause to detect very still sections after motion
1410 // For example a static image after a fade or other transition
1411 // instead of a clean scene cut.
1412 if (frame_interval > rc->min_gf_interval &&
1413 loop_decay_rate >= 0.999 &&
1414 last_decay_rate < 0.9) {
1415 int j;
1417 // Look ahead a few frames to see if static condition persists...
1418 for (j = 0; j < still_interval; ++j) {
1419 const FIRSTPASS_STATS *stats = &twopass->stats_in[j];
1420 if (stats >= twopass->stats_in_end)
1421 break;
1423 if (stats->pcnt_inter - stats->pcnt_motion < 0.999)
1424 break;
1427 // Only if it does do we signal a transition to still.
1428 return j == still_interval;
1431 return 0;
1434 // This function detects a flash through the high relative pcnt_second_ref
1435 // score in the frame following a flash frame. The offset passed in should
1436 // reflect this.
1437 static int detect_flash(const TWO_PASS *twopass, int offset) {
1438 const FIRSTPASS_STATS *const next_frame = read_frame_stats(twopass, offset);
1440 // What we are looking for here is a situation where there is a
1441 // brief break in prediction (such as a flash) but subsequent frames
1442 // are reasonably well predicted by an earlier (pre flash) frame.
1443 // The recovery after a flash is indicated by a high pcnt_second_ref
1444 // compared to pcnt_inter.
1445 return next_frame != NULL &&
1446 next_frame->pcnt_second_ref > next_frame->pcnt_inter &&
1447 next_frame->pcnt_second_ref >= 0.5;
1450 // Update the motion related elements to the GF arf boost calculation.
1451 static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats,
1452 double *mv_in_out,
1453 double *mv_in_out_accumulator,
1454 double *abs_mv_in_out_accumulator,
1455 double *mv_ratio_accumulator) {
1456 const double pct = stats->pcnt_motion;
1458 // Accumulate Motion In/Out of frame stats.
1459 *mv_in_out = stats->mv_in_out_count * pct;
1460 *mv_in_out_accumulator += *mv_in_out;
1461 *abs_mv_in_out_accumulator += fabs(*mv_in_out);
1463 // Accumulate a measure of how uniform (or conversely how random) the motion
1464 // field is (a ratio of abs(mv) / mv).
1465 if (pct > 0.05) {
1466 const double mvr_ratio = fabs(stats->mvr_abs) /
1467 DOUBLE_DIVIDE_CHECK(fabs(stats->MVr));
1468 const double mvc_ratio = fabs(stats->mvc_abs) /
1469 DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
1471 *mv_ratio_accumulator += pct * (mvr_ratio < stats->mvr_abs ?
1472 mvr_ratio : stats->mvr_abs);
1473 *mv_ratio_accumulator += pct * (mvc_ratio < stats->mvc_abs ?
1474 mvc_ratio : stats->mvc_abs);
1478 #define BASELINE_ERR_PER_MB 1000.0
1479 static double calc_frame_boost(VP9_COMP *cpi,
1480 const FIRSTPASS_STATS *this_frame,
1481 double this_frame_mv_in_out,
1482 double max_boost) {
1483 double frame_boost;
1484 const double lq =
1485 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME],
1486 cpi->common.bit_depth);
1487 const double boost_q_correction = MIN((0.5 + (lq * 0.015)), 1.5);
1488 int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1489 ? cpi->initial_mbs : cpi->common.MBs;
1491 // Correct for any inactive region in the image
1492 num_mbs = (int)MAX(1, num_mbs * calculate_active_area(cpi, this_frame));
1494 // Underlying boost factor is based on inter error ratio.
1495 frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
1496 DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1497 frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction;
1499 // Increase boost for frames where new data coming into frame (e.g. zoom out).
1500 // Slightly reduce boost if there is a net balance of motion out of the frame
1501 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1502 if (this_frame_mv_in_out > 0.0)
1503 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1504 // In the extreme case the boost is halved.
1505 else
1506 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1508 return MIN(frame_boost, max_boost * boost_q_correction);
1511 static int calc_arf_boost(VP9_COMP *cpi, int offset,
1512 int f_frames, int b_frames,
1513 int *f_boost, int *b_boost) {
1514 TWO_PASS *const twopass = &cpi->twopass;
1515 int i;
1516 double boost_score = 0.0;
1517 double mv_ratio_accumulator = 0.0;
1518 double decay_accumulator = 1.0;
1519 double this_frame_mv_in_out = 0.0;
1520 double mv_in_out_accumulator = 0.0;
1521 double abs_mv_in_out_accumulator = 0.0;
1522 int arf_boost;
1523 int flash_detected = 0;
1525 // Search forward from the proposed arf/next gf position.
1526 for (i = 0; i < f_frames; ++i) {
1527 const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1528 if (this_frame == NULL)
1529 break;
1531 // Update the motion related elements to the boost calculation.
1532 accumulate_frame_motion_stats(this_frame,
1533 &this_frame_mv_in_out, &mv_in_out_accumulator,
1534 &abs_mv_in_out_accumulator,
1535 &mv_ratio_accumulator);
1537 // We want to discount the flash frame itself and the recovery
1538 // frame that follows as both will have poor scores.
1539 flash_detected = detect_flash(twopass, i + offset) ||
1540 detect_flash(twopass, i + offset + 1);
1542 // Accumulate the effect of prediction quality decay.
1543 if (!flash_detected) {
1544 decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
1545 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1546 ? MIN_DECAY_FACTOR : decay_accumulator;
1549 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1550 this_frame_mv_in_out,
1551 GF_MAX_BOOST);
1554 *f_boost = (int)boost_score;
1556 // Reset for backward looking loop.
1557 boost_score = 0.0;
1558 mv_ratio_accumulator = 0.0;
1559 decay_accumulator = 1.0;
1560 this_frame_mv_in_out = 0.0;
1561 mv_in_out_accumulator = 0.0;
1562 abs_mv_in_out_accumulator = 0.0;
1564 // Search backward towards last gf position.
1565 for (i = -1; i >= -b_frames; --i) {
1566 const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1567 if (this_frame == NULL)
1568 break;
1570 // Update the motion related elements to the boost calculation.
1571 accumulate_frame_motion_stats(this_frame,
1572 &this_frame_mv_in_out, &mv_in_out_accumulator,
1573 &abs_mv_in_out_accumulator,
1574 &mv_ratio_accumulator);
1576 // We want to discount the the flash frame itself and the recovery
1577 // frame that follows as both will have poor scores.
1578 flash_detected = detect_flash(twopass, i + offset) ||
1579 detect_flash(twopass, i + offset + 1);
1581 // Cumulative effect of prediction quality decay.
1582 if (!flash_detected) {
1583 decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
1584 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1585 ? MIN_DECAY_FACTOR : decay_accumulator;
1588 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1589 this_frame_mv_in_out,
1590 GF_MAX_BOOST);
1592 *b_boost = (int)boost_score;
1594 arf_boost = (*f_boost + *b_boost);
1595 if (arf_boost < ((b_frames + f_frames) * 20))
1596 arf_boost = ((b_frames + f_frames) * 20);
1597 arf_boost = MAX(arf_boost, MIN_ARF_GF_BOOST);
1599 return arf_boost;
1602 // Calculate a section intra ratio used in setting max loop filter.
1603 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
1604 const FIRSTPASS_STATS *end,
1605 int section_length) {
1606 const FIRSTPASS_STATS *s = begin;
1607 double intra_error = 0.0;
1608 double coded_error = 0.0;
1609 int i = 0;
1611 while (s < end && i < section_length) {
1612 intra_error += s->intra_error;
1613 coded_error += s->coded_error;
1614 ++s;
1615 ++i;
1618 return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error));
1621 // Calculate the total bits to allocate in this GF/ARF group.
1622 static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi,
1623 double gf_group_err) {
1624 const RATE_CONTROL *const rc = &cpi->rc;
1625 const TWO_PASS *const twopass = &cpi->twopass;
1626 const int max_bits = frame_max_bits(rc, &cpi->oxcf);
1627 int64_t total_group_bits;
1629 // Calculate the bits to be allocated to the group as a whole.
1630 if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) {
1631 total_group_bits = (int64_t)(twopass->kf_group_bits *
1632 (gf_group_err / twopass->kf_group_error_left));
1633 } else {
1634 total_group_bits = 0;
1637 // Clamp odd edge cases.
1638 total_group_bits = (total_group_bits < 0) ?
1639 0 : (total_group_bits > twopass->kf_group_bits) ?
1640 twopass->kf_group_bits : total_group_bits;
1642 // Clip based on user supplied data rate variability limit.
1643 if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
1644 total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
1646 return total_group_bits;
1649 // Calculate the number bits extra to assign to boosted frames in a group.
1650 static int calculate_boost_bits(int frame_count,
1651 int boost, int64_t total_group_bits) {
1652 int allocation_chunks;
1654 // return 0 for invalid inputs (could arise e.g. through rounding errors)
1655 if (!boost || (total_group_bits <= 0) || (frame_count <= 0) )
1656 return 0;
1658 allocation_chunks = (frame_count * 100) + boost;
1660 // Prevent overflow.
1661 if (boost > 1023) {
1662 int divisor = boost >> 10;
1663 boost /= divisor;
1664 allocation_chunks /= divisor;
1667 // Calculate the number of extra bits for use in the boosted frame or frames.
1668 return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0);
1671 // Current limit on maximum number of active arfs in a GF/ARF group.
1672 #define MAX_ACTIVE_ARFS 2
1673 #define ARF_SLOT1 2
1674 #define ARF_SLOT2 3
1675 // This function indirects the choice of buffers for arfs.
1676 // At the moment the values are fixed but this may change as part of
1677 // the integration process with other codec features that swap buffers around.
1678 static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) {
1679 arf_buffer_indices[0] = ARF_SLOT1;
1680 arf_buffer_indices[1] = ARF_SLOT2;
1683 static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits,
1684 double group_error, int gf_arf_bits) {
1685 RATE_CONTROL *const rc = &cpi->rc;
1686 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1687 TWO_PASS *const twopass = &cpi->twopass;
1688 GF_GROUP *const gf_group = &twopass->gf_group;
1689 FIRSTPASS_STATS frame_stats;
1690 int i;
1691 int frame_index = 1;
1692 int target_frame_size;
1693 int key_frame;
1694 const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf);
1695 int64_t total_group_bits = gf_group_bits;
1696 double modified_err = 0.0;
1697 double err_fraction;
1698 int mid_boost_bits = 0;
1699 int mid_frame_idx;
1700 unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS];
1701 int alt_frame_index = frame_index;
1702 int has_temporal_layers = is_two_pass_svc(cpi) &&
1703 cpi->svc.number_temporal_layers > 1;
1705 // Only encode alt reference frame in temporal base layer.
1706 if (has_temporal_layers)
1707 alt_frame_index = cpi->svc.number_temporal_layers;
1709 key_frame = cpi->common.frame_type == KEY_FRAME ||
1710 vp9_is_upper_layer_key_frame(cpi);
1712 get_arf_buffer_indices(arf_buffer_indices);
1714 // For key frames the frame target rate is already set and it
1715 // is also the golden frame.
1716 if (!key_frame) {
1717 if (rc->source_alt_ref_active) {
1718 gf_group->update_type[0] = OVERLAY_UPDATE;
1719 gf_group->rf_level[0] = INTER_NORMAL;
1720 gf_group->bit_allocation[0] = 0;
1721 gf_group->arf_update_idx[0] = arf_buffer_indices[0];
1722 gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
1723 } else {
1724 gf_group->update_type[0] = GF_UPDATE;
1725 gf_group->rf_level[0] = GF_ARF_STD;
1726 gf_group->bit_allocation[0] = gf_arf_bits;
1727 gf_group->arf_update_idx[0] = arf_buffer_indices[0];
1728 gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
1731 // Step over the golden frame / overlay frame
1732 if (EOF == input_stats(twopass, &frame_stats))
1733 return;
1736 // Deduct the boost bits for arf (or gf if it is not a key frame)
1737 // from the group total.
1738 if (rc->source_alt_ref_pending || !key_frame)
1739 total_group_bits -= gf_arf_bits;
1741 // Store the bits to spend on the ARF if there is one.
1742 if (rc->source_alt_ref_pending) {
1743 gf_group->update_type[alt_frame_index] = ARF_UPDATE;
1744 gf_group->rf_level[alt_frame_index] = GF_ARF_STD;
1745 gf_group->bit_allocation[alt_frame_index] = gf_arf_bits;
1747 if (has_temporal_layers)
1748 gf_group->arf_src_offset[alt_frame_index] =
1749 (unsigned char)(rc->baseline_gf_interval -
1750 cpi->svc.number_temporal_layers);
1751 else
1752 gf_group->arf_src_offset[alt_frame_index] =
1753 (unsigned char)(rc->baseline_gf_interval - 1);
1755 gf_group->arf_update_idx[alt_frame_index] = arf_buffer_indices[0];
1756 gf_group->arf_ref_idx[alt_frame_index] =
1757 arf_buffer_indices[cpi->multi_arf_last_grp_enabled &&
1758 rc->source_alt_ref_active];
1759 if (!has_temporal_layers)
1760 ++frame_index;
1762 if (cpi->multi_arf_enabled) {
1763 // Set aside a slot for a level 1 arf.
1764 gf_group->update_type[frame_index] = ARF_UPDATE;
1765 gf_group->rf_level[frame_index] = GF_ARF_LOW;
1766 gf_group->arf_src_offset[frame_index] =
1767 (unsigned char)((rc->baseline_gf_interval >> 1) - 1);
1768 gf_group->arf_update_idx[frame_index] = arf_buffer_indices[1];
1769 gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
1770 ++frame_index;
1774 // Define middle frame
1775 mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
1777 // Allocate bits to the other frames in the group.
1778 for (i = 0; i < rc->baseline_gf_interval - rc->source_alt_ref_pending; ++i) {
1779 int arf_idx = 0;
1780 if (EOF == input_stats(twopass, &frame_stats))
1781 break;
1783 if (has_temporal_layers && frame_index == alt_frame_index) {
1784 ++frame_index;
1787 modified_err = calculate_modified_err(cpi, twopass, oxcf, &frame_stats);
1789 if (group_error > 0)
1790 err_fraction = modified_err / DOUBLE_DIVIDE_CHECK(group_error);
1791 else
1792 err_fraction = 0.0;
1794 target_frame_size = (int)((double)total_group_bits * err_fraction);
1796 if (rc->source_alt_ref_pending && cpi->multi_arf_enabled) {
1797 mid_boost_bits += (target_frame_size >> 4);
1798 target_frame_size -= (target_frame_size >> 4);
1800 if (frame_index <= mid_frame_idx)
1801 arf_idx = 1;
1803 gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx];
1804 gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx];
1806 target_frame_size = clamp(target_frame_size, 0,
1807 MIN(max_bits, (int)total_group_bits));
1809 gf_group->update_type[frame_index] = LF_UPDATE;
1810 gf_group->rf_level[frame_index] = INTER_NORMAL;
1812 gf_group->bit_allocation[frame_index] = target_frame_size;
1813 ++frame_index;
1816 // Note:
1817 // We need to configure the frame at the end of the sequence + 1 that will be
1818 // the start frame for the next group. Otherwise prior to the call to
1819 // vp9_rc_get_second_pass_params() the data will be undefined.
1820 gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
1821 gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
1823 if (rc->source_alt_ref_pending) {
1824 gf_group->update_type[frame_index] = OVERLAY_UPDATE;
1825 gf_group->rf_level[frame_index] = INTER_NORMAL;
1827 // Final setup for second arf and its overlay.
1828 if (cpi->multi_arf_enabled) {
1829 gf_group->bit_allocation[2] =
1830 gf_group->bit_allocation[mid_frame_idx] + mid_boost_bits;
1831 gf_group->update_type[mid_frame_idx] = OVERLAY_UPDATE;
1832 gf_group->bit_allocation[mid_frame_idx] = 0;
1834 } else {
1835 gf_group->update_type[frame_index] = GF_UPDATE;
1836 gf_group->rf_level[frame_index] = GF_ARF_STD;
1839 // Note whether multi-arf was enabled this group for next time.
1840 cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled;
1843 // Analyse and define a gf/arf group.
1844 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1845 VP9_COMMON *const cm = &cpi->common;
1846 RATE_CONTROL *const rc = &cpi->rc;
1847 VP9EncoderConfig *const oxcf = &cpi->oxcf;
1848 TWO_PASS *const twopass = &cpi->twopass;
1849 FIRSTPASS_STATS next_frame;
1850 const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
1851 int i;
1853 double boost_score = 0.0;
1854 double old_boost_score = 0.0;
1855 double gf_group_err = 0.0;
1856 #if GROUP_ADAPTIVE_MAXQ
1857 double gf_group_raw_error = 0.0;
1858 #endif
1859 double gf_group_skip_pct = 0.0;
1860 double gf_group_inactive_zone_rows = 0.0;
1861 double gf_first_frame_err = 0.0;
1862 double mod_frame_err = 0.0;
1864 double mv_ratio_accumulator = 0.0;
1865 double decay_accumulator = 1.0;
1866 double zero_motion_accumulator = 1.0;
1868 double loop_decay_rate = 1.00;
1869 double last_loop_decay_rate = 1.00;
1871 double this_frame_mv_in_out = 0.0;
1872 double mv_in_out_accumulator = 0.0;
1873 double abs_mv_in_out_accumulator = 0.0;
1874 double mv_ratio_accumulator_thresh;
1875 unsigned int allow_alt_ref = is_altref_enabled(cpi);
1877 int f_boost = 0;
1878 int b_boost = 0;
1879 int flash_detected;
1880 int active_max_gf_interval;
1881 int active_min_gf_interval;
1882 int64_t gf_group_bits;
1883 double gf_group_error_left;
1884 int gf_arf_bits;
1885 const int is_key_frame = frame_is_intra_only(cm);
1886 const int kf_or_arf_active = is_key_frame || rc->source_alt_ref_active;
1888 // Reset the GF group data structures unless this is a key
1889 // frame in which case it will already have been done.
1890 if (is_key_frame == 0) {
1891 vp9_zero(twopass->gf_group);
1894 vp9_clear_system_state();
1895 vp9_zero(next_frame);
1897 // Load stats for the current frame.
1898 mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
1900 // Note the error of the frame at the start of the group. This will be
1901 // the GF frame error if we code a normal gf.
1902 gf_first_frame_err = mod_frame_err;
1904 // If this is a key frame or the overlay from a previous arf then
1905 // the error score / cost of this frame has already been accounted for.
1906 if (is_key_frame || rc->source_alt_ref_active) {
1907 gf_group_err -= gf_first_frame_err;
1908 #if GROUP_ADAPTIVE_MAXQ
1909 gf_group_raw_error -= this_frame->coded_error;
1910 #endif
1911 gf_group_skip_pct -= this_frame->intra_skip_pct;
1912 gf_group_inactive_zone_rows -= this_frame->inactive_zone_rows;
1915 // Motion breakout threshold for loop below depends on image size.
1916 mv_ratio_accumulator_thresh =
1917 (cpi->initial_height + cpi->initial_width) / 4.0;
1919 // Set a maximum and minimum interval for the GF group.
1920 // If the image appears almost completely static we can extend beyond this.
1922 int int_max_q =
1923 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
1924 cpi->common.bit_depth));
1925 int int_lbq =
1926 (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex,
1927 cpi->common.bit_depth));
1928 active_min_gf_interval = rc->min_gf_interval + MIN(2, int_max_q / 200);
1929 if (active_min_gf_interval > rc->max_gf_interval)
1930 active_min_gf_interval = rc->max_gf_interval;
1932 if (cpi->multi_arf_allowed) {
1933 active_max_gf_interval = rc->max_gf_interval;
1934 } else {
1935 // The value chosen depends on the active Q range. At low Q we have
1936 // bits to spare and are better with a smaller interval and smaller boost.
1937 // At high Q when there are few bits to spare we are better with a longer
1938 // interval to spread the cost of the GF.
1939 active_max_gf_interval = rc->max_gf_interval - 4 + MIN(4, (int_lbq / 6));
1940 if (active_max_gf_interval < active_min_gf_interval)
1941 active_max_gf_interval = active_min_gf_interval;
1943 if (active_max_gf_interval > rc->max_gf_interval)
1944 active_max_gf_interval = rc->max_gf_interval;
1945 if (active_max_gf_interval < active_min_gf_interval)
1946 active_max_gf_interval = active_min_gf_interval;
1950 i = 0;
1951 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
1952 ++i;
1954 // Accumulate error score of frames in this gf group.
1955 mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
1956 gf_group_err += mod_frame_err;
1957 #if GROUP_ADAPTIVE_MAXQ
1958 gf_group_raw_error += this_frame->coded_error;
1959 #endif
1960 gf_group_skip_pct += this_frame->intra_skip_pct;
1961 gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
1963 if (EOF == input_stats(twopass, &next_frame))
1964 break;
1966 // Test for the case where there is a brief flash but the prediction
1967 // quality back to an earlier frame is then restored.
1968 flash_detected = detect_flash(twopass, 0);
1970 // Update the motion related elements to the boost calculation.
1971 accumulate_frame_motion_stats(&next_frame,
1972 &this_frame_mv_in_out, &mv_in_out_accumulator,
1973 &abs_mv_in_out_accumulator,
1974 &mv_ratio_accumulator);
1976 // Accumulate the effect of prediction quality decay.
1977 if (!flash_detected) {
1978 last_loop_decay_rate = loop_decay_rate;
1979 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1981 decay_accumulator = decay_accumulator * loop_decay_rate;
1983 // Monitor for static sections.
1984 zero_motion_accumulator =
1985 MIN(zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
1987 // Break clause to detect very still sections after motion. For example,
1988 // a static image after a fade or other transition.
1989 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
1990 last_loop_decay_rate)) {
1991 allow_alt_ref = 0;
1992 break;
1996 // Calculate a boost number for this frame.
1997 boost_score += decay_accumulator * calc_frame_boost(cpi, &next_frame,
1998 this_frame_mv_in_out,
1999 GF_MAX_BOOST);
2001 // Break out conditions.
2002 if (
2003 // Break at active_max_gf_interval unless almost totally static.
2004 ((i >= active_max_gf_interval + kf_or_arf_active) &&
2005 (zero_motion_accumulator < 0.995)) ||
2007 // Don't break out with a very short interval.
2008 (i >= active_min_gf_interval + kf_or_arf_active) &&
2009 (!flash_detected) &&
2010 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
2011 (abs_mv_in_out_accumulator > 3.0) ||
2012 (mv_in_out_accumulator < -2.0) ||
2013 ((boost_score - old_boost_score) < BOOST_BREAKOUT)))) {
2014 boost_score = old_boost_score;
2015 break;
2018 *this_frame = next_frame;
2019 old_boost_score = boost_score;
2022 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
2024 // Was the group length constrained by the requirement for a new KF?
2025 rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0;
2027 // Should we use the alternate reference frame.
2028 if (allow_alt_ref &&
2029 (i < cpi->oxcf.lag_in_frames) &&
2030 (i >= rc->min_gf_interval)) {
2031 // Calculate the boost for alt ref.
2032 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
2033 &b_boost);
2034 rc->source_alt_ref_pending = 1;
2036 // Test to see if multi arf is appropriate.
2037 cpi->multi_arf_enabled =
2038 (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) &&
2039 (zero_motion_accumulator < 0.995)) ? 1 : 0;
2040 } else {
2041 rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST);
2042 rc->source_alt_ref_pending = 0;
2045 // Set the interval until the next gf.
2046 if (is_key_frame || rc->source_alt_ref_pending)
2047 rc->baseline_gf_interval = i - 1;
2048 else
2049 rc->baseline_gf_interval = i;
2051 // Only encode alt reference frame in temporal base layer. So
2052 // baseline_gf_interval should be multiple of a temporal layer group
2053 // (typically the frame distance between two base layer frames)
2054 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
2055 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
2056 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count);
2057 int j;
2058 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) {
2059 if (EOF == input_stats(twopass, this_frame))
2060 break;
2061 gf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2062 #if GROUP_ADAPTIVE_MAXQ
2063 gf_group_raw_error += this_frame->coded_error;
2064 #endif
2065 gf_group_skip_pct += this_frame->intra_skip_pct;
2066 gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
2068 rc->baseline_gf_interval = new_gf_interval;
2071 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2073 // Reset the file position.
2074 reset_fpf_position(twopass, start_pos);
2076 // Calculate the bits to be allocated to the gf/arf group as a whole
2077 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
2079 #if GROUP_ADAPTIVE_MAXQ
2080 // Calculate an estimate of the maxq needed for the group.
2081 // We are more agressive about correcting for sections
2082 // where there could be significant overshoot than for easier
2083 // sections where we do not wish to risk creating an overshoot
2084 // of the allocated bit budget.
2085 if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) {
2086 const int vbr_group_bits_per_frame =
2087 (int)(gf_group_bits / rc->baseline_gf_interval);
2088 const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval;
2089 const double group_av_skip_pct =
2090 gf_group_skip_pct / rc->baseline_gf_interval;
2091 const double group_av_inactive_zone =
2092 ((gf_group_inactive_zone_rows * 2) /
2093 (rc->baseline_gf_interval * (double)cm->mb_rows));
2095 int tmp_q;
2096 // rc factor is a weight factor that corrects for local rate control drift.
2097 double rc_factor = 1.0;
2098 if (rc->rate_error_estimate > 0) {
2099 rc_factor = MAX(RC_FACTOR_MIN,
2100 (double)(100 - rc->rate_error_estimate) / 100.0);
2101 } else {
2102 rc_factor = MIN(RC_FACTOR_MAX,
2103 (double)(100 - rc->rate_error_estimate) / 100.0);
2105 tmp_q =
2106 get_twopass_worst_quality(cpi, group_av_err,
2107 (group_av_skip_pct + group_av_inactive_zone),
2108 vbr_group_bits_per_frame,
2109 twopass->kfgroup_inter_fraction * rc_factor);
2110 twopass->active_worst_quality =
2111 MAX(tmp_q, twopass->active_worst_quality >> 1);
2113 #endif
2115 // Calculate the extra bits to be used for boosted frame(s)
2116 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval,
2117 rc->gfu_boost, gf_group_bits);
2119 // Adjust KF group bits and error remaining.
2120 twopass->kf_group_error_left -= (int64_t)gf_group_err;
2122 // If this is an arf update we want to remove the score for the overlay
2123 // frame at the end which will usually be very cheap to code.
2124 // The overlay frame has already, in effect, been coded so we want to spread
2125 // the remaining bits among the other frames.
2126 // For normal GFs remove the score for the GF itself unless this is
2127 // also a key frame in which case it has already been accounted for.
2128 if (rc->source_alt_ref_pending) {
2129 gf_group_error_left = gf_group_err - mod_frame_err;
2130 } else if (is_key_frame == 0) {
2131 gf_group_error_left = gf_group_err - gf_first_frame_err;
2132 } else {
2133 gf_group_error_left = gf_group_err;
2136 // Allocate bits to each of the frames in the GF group.
2137 allocate_gf_group_bits(cpi, gf_group_bits, gf_group_error_left, gf_arf_bits);
2139 // Reset the file position.
2140 reset_fpf_position(twopass, start_pos);
2142 // Calculate a section intra ratio used in setting max loop filter.
2143 if (cpi->common.frame_type != KEY_FRAME) {
2144 twopass->section_intra_rating =
2145 calculate_section_intra_ratio(start_pos, twopass->stats_in_end,
2146 rc->baseline_gf_interval);
2149 if (oxcf->resize_mode == RESIZE_DYNAMIC) {
2150 // Default to starting GF groups at normal frame size.
2151 cpi->rc.next_frame_size_selector = UNSCALED;
2155 // Threshold for use of the lagging second reference frame. High second ref
2156 // usage may point to a transient event like a flash or occlusion rather than
2157 // a real scene cut.
2158 #define SECOND_REF_USEAGE_THRESH 0.1
2159 // Minimum % intra coding observed in first pass (1.0 = 100%)
2160 #define MIN_INTRA_LEVEL 0.25
2161 // Minimum ratio between the % of intra coding and inter coding in the first
2162 // pass after discounting neutral blocks (discounting neutral blocks in this
2163 // way helps catch scene cuts in clips with very flat areas or letter box
2164 // format clips with image padding.
2165 #define INTRA_VS_INTER_THRESH 2.0
2166 // Hard threshold where the first pass chooses intra for almost all blocks.
2167 // In such a case even if the frame is not a scene cut coding a key frame
2168 // may be a good option.
2169 #define VERY_LOW_INTER_THRESH 0.05
2170 // Maximum threshold for the relative ratio of intra error score vs best
2171 // inter error score.
2172 #define KF_II_ERR_THRESHOLD 2.5
2173 // In real scene cuts there is almost always a sharp change in the intra
2174 // or inter error score.
2175 #define ERR_CHANGE_THRESHOLD 0.4
2176 // For real scene cuts we expect an improvment in the intra inter error
2177 // ratio in the next frame.
2178 #define II_IMPROVEMENT_THRESHOLD 3.5
2179 #define KF_II_MAX 128.0
2181 static int test_candidate_kf(TWO_PASS *twopass,
2182 const FIRSTPASS_STATS *last_frame,
2183 const FIRSTPASS_STATS *this_frame,
2184 const FIRSTPASS_STATS *next_frame) {
2185 int is_viable_kf = 0;
2186 double pcnt_intra = 1.0 - this_frame->pcnt_inter;
2187 double modified_pcnt_inter =
2188 this_frame->pcnt_inter - this_frame->pcnt_neutral;
2190 // Does the frame satisfy the primary criteria of a key frame?
2191 // See above for an explanation of the test criteria.
2192 // If so, then examine how well it predicts subsequent frames.
2193 if ((this_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
2194 (next_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
2195 ((this_frame->pcnt_inter < VERY_LOW_INTER_THRESH) ||
2196 ((pcnt_intra > MIN_INTRA_LEVEL) &&
2197 (pcnt_intra > (INTRA_VS_INTER_THRESH * modified_pcnt_inter)) &&
2198 ((this_frame->intra_error /
2199 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) <
2200 KF_II_ERR_THRESHOLD) &&
2201 ((fabs(last_frame->coded_error - this_frame->coded_error) /
2202 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
2203 ERR_CHANGE_THRESHOLD) ||
2204 (fabs(last_frame->intra_error - this_frame->intra_error) /
2205 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
2206 ERR_CHANGE_THRESHOLD) ||
2207 ((next_frame->intra_error /
2208 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) >
2209 II_IMPROVEMENT_THRESHOLD))))) {
2210 int i;
2211 const FIRSTPASS_STATS *start_pos = twopass->stats_in;
2212 FIRSTPASS_STATS local_next_frame = *next_frame;
2213 double boost_score = 0.0;
2214 double old_boost_score = 0.0;
2215 double decay_accumulator = 1.0;
2217 // Examine how well the key frame predicts subsequent frames.
2218 for (i = 0; i < 16; ++i) {
2219 double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error /
2220 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
2222 if (next_iiratio > KF_II_MAX)
2223 next_iiratio = KF_II_MAX;
2225 // Cumulative effect of decay in prediction quality.
2226 if (local_next_frame.pcnt_inter > 0.85)
2227 decay_accumulator *= local_next_frame.pcnt_inter;
2228 else
2229 decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
2231 // Keep a running total.
2232 boost_score += (decay_accumulator * next_iiratio);
2234 // Test various breakout clauses.
2235 if ((local_next_frame.pcnt_inter < 0.05) ||
2236 (next_iiratio < 1.5) ||
2237 (((local_next_frame.pcnt_inter -
2238 local_next_frame.pcnt_neutral) < 0.20) &&
2239 (next_iiratio < 3.0)) ||
2240 ((boost_score - old_boost_score) < 3.0) ||
2241 (local_next_frame.intra_error < 200)) {
2242 break;
2245 old_boost_score = boost_score;
2247 // Get the next frame details
2248 if (EOF == input_stats(twopass, &local_next_frame))
2249 break;
2252 // If there is tolerable prediction for at least the next 3 frames then
2253 // break out else discard this potential key frame and move on
2254 if (boost_score > 30.0 && (i > 3)) {
2255 is_viable_kf = 1;
2256 } else {
2257 // Reset the file position
2258 reset_fpf_position(twopass, start_pos);
2260 is_viable_kf = 0;
2264 return is_viable_kf;
2267 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2268 int i, j;
2269 RATE_CONTROL *const rc = &cpi->rc;
2270 TWO_PASS *const twopass = &cpi->twopass;
2271 GF_GROUP *const gf_group = &twopass->gf_group;
2272 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2273 const FIRSTPASS_STATS first_frame = *this_frame;
2274 const FIRSTPASS_STATS *const start_position = twopass->stats_in;
2275 FIRSTPASS_STATS next_frame;
2276 FIRSTPASS_STATS last_frame;
2277 int kf_bits = 0;
2278 int loop_decay_counter = 0;
2279 double decay_accumulator = 1.0;
2280 double av_decay_accumulator = 0.0;
2281 double zero_motion_accumulator = 1.0;
2282 double boost_score = 0.0;
2283 double kf_mod_err = 0.0;
2284 double kf_group_err = 0.0;
2285 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
2287 vp9_zero(next_frame);
2289 cpi->common.frame_type = KEY_FRAME;
2291 // Reset the GF group data structures.
2292 vp9_zero(*gf_group);
2294 // Is this a forced key frame by interval.
2295 rc->this_key_frame_forced = rc->next_key_frame_forced;
2297 // Clear the alt ref active flag and last group multi arf flags as they
2298 // can never be set for a key frame.
2299 rc->source_alt_ref_active = 0;
2300 cpi->multi_arf_last_grp_enabled = 0;
2302 // KF is always a GF so clear frames till next gf counter.
2303 rc->frames_till_gf_update_due = 0;
2305 rc->frames_to_key = 1;
2307 twopass->kf_group_bits = 0; // Total bits available to kf group
2308 twopass->kf_group_error_left = 0; // Group modified error score.
2310 kf_mod_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
2312 // Find the next keyframe.
2313 i = 0;
2314 while (twopass->stats_in < twopass->stats_in_end &&
2315 rc->frames_to_key < cpi->oxcf.key_freq) {
2316 // Accumulate kf group error.
2317 kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2319 // Load the next frame's stats.
2320 last_frame = *this_frame;
2321 input_stats(twopass, this_frame);
2323 // Provided that we are not at the end of the file...
2324 if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) {
2325 double loop_decay_rate;
2327 // Check for a scene cut.
2328 if (test_candidate_kf(twopass, &last_frame, this_frame,
2329 twopass->stats_in))
2330 break;
2332 // How fast is the prediction quality decaying?
2333 loop_decay_rate = get_prediction_decay_rate(cpi, twopass->stats_in);
2335 // We want to know something about the recent past... rather than
2336 // as used elsewhere where we are concerned with decay in prediction
2337 // quality since the last GF or KF.
2338 recent_loop_decay[i % 8] = loop_decay_rate;
2339 decay_accumulator = 1.0;
2340 for (j = 0; j < 8; ++j)
2341 decay_accumulator *= recent_loop_decay[j];
2343 // Special check for transition or high motion followed by a
2344 // static scene.
2345 if (detect_transition_to_still(cpi, i, cpi->oxcf.key_freq - i,
2346 loop_decay_rate, decay_accumulator))
2347 break;
2349 // Step on to the next frame.
2350 ++rc->frames_to_key;
2352 // If we don't have a real key frame within the next two
2353 // key_freq intervals then break out of the loop.
2354 if (rc->frames_to_key >= 2 * cpi->oxcf.key_freq)
2355 break;
2356 } else {
2357 ++rc->frames_to_key;
2359 ++i;
2362 // If there is a max kf interval set by the user we must obey it.
2363 // We already breakout of the loop above at 2x max.
2364 // This code centers the extra kf if the actual natural interval
2365 // is between 1x and 2x.
2366 if (cpi->oxcf.auto_key &&
2367 rc->frames_to_key > cpi->oxcf.key_freq) {
2368 FIRSTPASS_STATS tmp_frame = first_frame;
2370 rc->frames_to_key /= 2;
2372 // Reset to the start of the group.
2373 reset_fpf_position(twopass, start_position);
2375 kf_group_err = 0.0;
2377 // Rescan to get the correct error data for the forced kf group.
2378 for (i = 0; i < rc->frames_to_key; ++i) {
2379 kf_group_err += calculate_modified_err(cpi, twopass, oxcf, &tmp_frame);
2380 input_stats(twopass, &tmp_frame);
2382 rc->next_key_frame_forced = 1;
2383 } else if (twopass->stats_in == twopass->stats_in_end ||
2384 rc->frames_to_key >= cpi->oxcf.key_freq) {
2385 rc->next_key_frame_forced = 1;
2386 } else {
2387 rc->next_key_frame_forced = 0;
2390 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
2391 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
2392 int new_frame_to_key = (rc->frames_to_key + count) & (~count);
2393 int j;
2394 for (j = 0; j < new_frame_to_key - rc->frames_to_key; ++j) {
2395 if (EOF == input_stats(twopass, this_frame))
2396 break;
2397 kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2399 rc->frames_to_key = new_frame_to_key;
2402 // Special case for the last key frame of the file.
2403 if (twopass->stats_in >= twopass->stats_in_end) {
2404 // Accumulate kf group error.
2405 kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame);
2408 // Calculate the number of bits that should be assigned to the kf group.
2409 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) {
2410 // Maximum number of bits for a single normal frame (not key frame).
2411 const int max_bits = frame_max_bits(rc, &cpi->oxcf);
2413 // Maximum number of bits allocated to the key frame group.
2414 int64_t max_grp_bits;
2416 // Default allocation based on bits left and relative
2417 // complexity of the section.
2418 twopass->kf_group_bits = (int64_t)(twopass->bits_left *
2419 (kf_group_err / twopass->modified_error_left));
2421 // Clip based on maximum per frame rate defined by the user.
2422 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
2423 if (twopass->kf_group_bits > max_grp_bits)
2424 twopass->kf_group_bits = max_grp_bits;
2425 } else {
2426 twopass->kf_group_bits = 0;
2428 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits);
2430 // Reset the first pass file position.
2431 reset_fpf_position(twopass, start_position);
2433 // Scan through the kf group collating various stats used to determine
2434 // how many bits to spend on it.
2435 decay_accumulator = 1.0;
2436 boost_score = 0.0;
2437 for (i = 0; i < (rc->frames_to_key - 1); ++i) {
2438 if (EOF == input_stats(twopass, &next_frame))
2439 break;
2441 // Monitor for static sections.
2442 zero_motion_accumulator =
2443 MIN(zero_motion_accumulator,
2444 get_zero_motion_factor(cpi, &next_frame));
2446 // Not all frames in the group are necessarily used in calculating boost.
2447 if ((i <= rc->max_gf_interval) ||
2448 ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) {
2449 const double frame_boost =
2450 calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST);
2452 // How fast is prediction quality decaying.
2453 if (!detect_flash(twopass, 0)) {
2454 const double loop_decay_rate =
2455 get_prediction_decay_rate(cpi, &next_frame);
2456 decay_accumulator *= loop_decay_rate;
2457 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR);
2458 av_decay_accumulator += decay_accumulator;
2459 ++loop_decay_counter;
2461 boost_score += (decay_accumulator * frame_boost);
2464 av_decay_accumulator /= (double)loop_decay_counter;
2466 reset_fpf_position(twopass, start_position);
2468 // Store the zero motion percentage
2469 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2471 // Calculate a section intra ratio used in setting max loop filter.
2472 twopass->section_intra_rating =
2473 calculate_section_intra_ratio(start_position, twopass->stats_in_end,
2474 rc->frames_to_key);
2476 // Apply various clamps for min and max boost
2477 rc->kf_boost = (int)(av_decay_accumulator * boost_score);
2478 rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3));
2479 rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST);
2481 // Work out how many bits to allocate for the key frame itself.
2482 kf_bits = calculate_boost_bits((rc->frames_to_key - 1),
2483 rc->kf_boost, twopass->kf_group_bits);
2485 // Work out the fraction of the kf group bits reserved for the inter frames
2486 // within the group after discounting the bits for the kf itself.
2487 if (twopass->kf_group_bits) {
2488 twopass->kfgroup_inter_fraction =
2489 (double)(twopass->kf_group_bits - kf_bits) /
2490 (double)twopass->kf_group_bits;
2491 } else {
2492 twopass->kfgroup_inter_fraction = 1.0;
2495 twopass->kf_group_bits -= kf_bits;
2497 // Save the bits to spend on the key frame.
2498 gf_group->bit_allocation[0] = kf_bits;
2499 gf_group->update_type[0] = KF_UPDATE;
2500 gf_group->rf_level[0] = KF_STD;
2502 // Note the total error score of the kf group minus the key frame itself.
2503 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2505 // Adjust the count of total modified error left.
2506 // The count of bits left is adjusted elsewhere based on real coded frame
2507 // sizes.
2508 twopass->modified_error_left -= kf_group_err;
2510 if (oxcf->resize_mode == RESIZE_DYNAMIC) {
2511 // Default to normal-sized frame on keyframes.
2512 cpi->rc.next_frame_size_selector = UNSCALED;
2516 // Define the reference buffers that will be updated post encode.
2517 static void configure_buffer_updates(VP9_COMP *cpi) {
2518 TWO_PASS *const twopass = &cpi->twopass;
2520 cpi->rc.is_src_frame_alt_ref = 0;
2521 switch (twopass->gf_group.update_type[twopass->gf_group.index]) {
2522 case KF_UPDATE:
2523 cpi->refresh_last_frame = 1;
2524 cpi->refresh_golden_frame = 1;
2525 cpi->refresh_alt_ref_frame = 1;
2526 break;
2527 case LF_UPDATE:
2528 cpi->refresh_last_frame = 1;
2529 cpi->refresh_golden_frame = 0;
2530 cpi->refresh_alt_ref_frame = 0;
2531 break;
2532 case GF_UPDATE:
2533 cpi->refresh_last_frame = 1;
2534 cpi->refresh_golden_frame = 1;
2535 cpi->refresh_alt_ref_frame = 0;
2536 break;
2537 case OVERLAY_UPDATE:
2538 cpi->refresh_last_frame = 0;
2539 cpi->refresh_golden_frame = 1;
2540 cpi->refresh_alt_ref_frame = 0;
2541 cpi->rc.is_src_frame_alt_ref = 1;
2542 break;
2543 case ARF_UPDATE:
2544 cpi->refresh_last_frame = 0;
2545 cpi->refresh_golden_frame = 0;
2546 cpi->refresh_alt_ref_frame = 1;
2547 break;
2548 default:
2549 assert(0);
2550 break;
2552 if (is_two_pass_svc(cpi)) {
2553 if (cpi->svc.temporal_layer_id > 0) {
2554 cpi->refresh_last_frame = 0;
2555 cpi->refresh_golden_frame = 0;
2557 if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0)
2558 cpi->refresh_golden_frame = 0;
2559 if (cpi->alt_ref_source == NULL)
2560 cpi->refresh_alt_ref_frame = 0;
2564 static int is_skippable_frame(const VP9_COMP *cpi) {
2565 // If the current frame does not have non-zero motion vector detected in the
2566 // first pass, and so do its previous and forward frames, then this frame
2567 // can be skipped for partition check, and the partition size is assigned
2568 // according to the variance
2569 const SVC *const svc = &cpi->svc;
2570 const TWO_PASS *const twopass = is_two_pass_svc(cpi) ?
2571 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
2573 return (!frame_is_intra_only(&cpi->common) &&
2574 twopass->stats_in - 2 > twopass->stats_in_start &&
2575 twopass->stats_in < twopass->stats_in_end &&
2576 (twopass->stats_in - 1)->pcnt_inter - (twopass->stats_in - 1)->pcnt_motion
2577 == 1 &&
2578 (twopass->stats_in - 2)->pcnt_inter - (twopass->stats_in - 2)->pcnt_motion
2579 == 1 &&
2580 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
2583 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2584 VP9_COMMON *const cm = &cpi->common;
2585 RATE_CONTROL *const rc = &cpi->rc;
2586 TWO_PASS *const twopass = &cpi->twopass;
2587 GF_GROUP *const gf_group = &twopass->gf_group;
2588 int frames_left;
2589 FIRSTPASS_STATS this_frame;
2591 int target_rate;
2592 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
2593 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0;
2595 if (lc != NULL) {
2596 frames_left = (int)(twopass->total_stats.count -
2597 lc->current_video_frame_in_layer);
2598 } else {
2599 frames_left = (int)(twopass->total_stats.count -
2600 cm->current_video_frame);
2603 if (!twopass->stats_in)
2604 return;
2606 // If this is an arf frame then we dont want to read the stats file or
2607 // advance the input pointer as we already have what we need.
2608 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2609 int target_rate;
2610 configure_buffer_updates(cpi);
2611 target_rate = gf_group->bit_allocation[gf_group->index];
2612 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2613 rc->base_frame_target = target_rate;
2615 cm->frame_type = INTER_FRAME;
2617 if (lc != NULL) {
2618 if (cpi->svc.spatial_layer_id == 0) {
2619 lc->is_key_frame = 0;
2620 } else {
2621 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2623 if (lc->is_key_frame)
2624 cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2628 // Do the firstpass stats indicate that this frame is skippable for the
2629 // partition search?
2630 if (cpi->sf.allow_partition_search_skip &&
2631 cpi->oxcf.pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
2632 cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
2635 return;
2638 vp9_clear_system_state();
2640 if (cpi->oxcf.rc_mode == VPX_Q) {
2641 twopass->active_worst_quality = cpi->oxcf.cq_level;
2642 } else if (cm->current_video_frame == 0 ||
2643 (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2644 // Special case code for first frame.
2645 const int section_target_bandwidth = (int)(twopass->bits_left /
2646 frames_left);
2647 const double section_length = twopass->total_left_stats.count;
2648 const double section_error =
2649 twopass->total_left_stats.coded_error / section_length;
2650 const double section_intra_skip =
2651 twopass->total_left_stats.intra_skip_pct / section_length;
2652 const double section_inactive_zone =
2653 (twopass->total_left_stats.inactive_zone_rows * 2) /
2654 ((double)cm->mb_rows * section_length);
2655 const int tmp_q =
2656 get_twopass_worst_quality(cpi, section_error,
2657 section_intra_skip + section_inactive_zone,
2658 section_target_bandwidth, DEFAULT_GRP_WEIGHT);
2660 twopass->active_worst_quality = tmp_q;
2661 twopass->baseline_active_worst_quality = tmp_q;
2662 rc->ni_av_qi = tmp_q;
2663 rc->last_q[INTER_FRAME] = tmp_q;
2664 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
2665 rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
2666 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2;
2667 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
2669 vp9_zero(this_frame);
2670 if (EOF == input_stats(twopass, &this_frame))
2671 return;
2673 // Set the frame content type flag.
2674 if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH)
2675 twopass->fr_content_type = FC_GRAPHICS_ANIMATION;
2676 else
2677 twopass->fr_content_type = FC_NORMAL;
2679 // Keyframe and section processing.
2680 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2681 FIRSTPASS_STATS this_frame_copy;
2682 this_frame_copy = this_frame;
2683 // Define next KF group and assign bits to it.
2684 find_next_key_frame(cpi, &this_frame);
2685 this_frame = this_frame_copy;
2686 } else {
2687 cm->frame_type = INTER_FRAME;
2690 if (lc != NULL) {
2691 if (cpi->svc.spatial_layer_id == 0) {
2692 lc->is_key_frame = (cm->frame_type == KEY_FRAME);
2693 if (lc->is_key_frame) {
2694 cpi->ref_frame_flags &=
2695 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2696 lc->frames_from_key_frame = 0;
2697 // Encode an intra only empty frame since we have a key frame.
2698 cpi->svc.encode_intra_empty_frame = 1;
2700 } else {
2701 cm->frame_type = INTER_FRAME;
2702 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2704 if (lc->is_key_frame) {
2705 cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2706 lc->frames_from_key_frame = 0;
2711 // Define a new GF/ARF group. (Should always enter here for key frames).
2712 if (rc->frames_till_gf_update_due == 0) {
2713 define_gf_group(cpi, &this_frame);
2715 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2716 if (lc != NULL)
2717 cpi->refresh_golden_frame = 1;
2719 #if ARF_STATS_OUTPUT
2721 FILE *fpfile;
2722 fpfile = fopen("arf.stt", "a");
2723 ++arf_count;
2724 fprintf(fpfile, "%10d %10ld %10d %10d %10ld\n",
2725 cm->current_video_frame, rc->frames_till_gf_update_due,
2726 rc->kf_boost, arf_count, rc->gfu_boost);
2728 fclose(fpfile);
2730 #endif
2733 configure_buffer_updates(cpi);
2735 // Do the firstpass stats indicate that this frame is skippable for the
2736 // partition search?
2737 if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
2738 (!cpi->use_svc || is_two_pass_svc(cpi))) {
2739 cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
2742 target_rate = gf_group->bit_allocation[gf_group->index];
2743 if (cpi->common.frame_type == KEY_FRAME)
2744 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2745 else
2746 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2748 rc->base_frame_target = target_rate;
2751 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
2752 ? cpi->initial_mbs : cpi->common.MBs;
2753 // The multiplication by 256 reverses a scaling factor of (>> 8)
2754 // applied when combining MB error values for the frame.
2755 twopass->mb_av_energy =
2756 log(((this_frame.intra_error * 256.0) / num_mbs) + 1.0);
2759 // Update the total stats remaining structure.
2760 subtract_stats(&twopass->total_left_stats, &this_frame);
2763 #define MINQ_ADJ_LIMIT 48
2764 #define MINQ_ADJ_LIMIT_CQ 20
2765 #define HIGH_UNDERSHOOT_RATIO 2
2766 void vp9_twopass_postencode_update(VP9_COMP *cpi) {
2767 TWO_PASS *const twopass = &cpi->twopass;
2768 RATE_CONTROL *const rc = &cpi->rc;
2769 const int bits_used = rc->base_frame_target;
2771 // VBR correction is done through rc->vbr_bits_off_target. Based on the
2772 // sign of this value, a limited % adjustment is made to the target rate
2773 // of subsequent frames, to try and push it back towards 0. This method
2774 // is designed to prevent extreme behaviour at the end of a clip
2775 // or group of frames.
2776 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
2777 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0);
2779 // Calculate the pct rc error.
2780 if (rc->total_actual_bits) {
2781 rc->rate_error_estimate =
2782 (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
2783 rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
2784 } else {
2785 rc->rate_error_estimate = 0;
2788 if (cpi->common.frame_type != KEY_FRAME &&
2789 !vp9_is_upper_layer_key_frame(cpi)) {
2790 twopass->kf_group_bits -= bits_used;
2791 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
2793 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0);
2795 // Increment the gf group index ready for the next frame.
2796 ++twopass->gf_group.index;
2798 // If the rate control is drifting consider adjustment to min or maxq.
2799 if ((cpi->oxcf.rc_mode != VPX_Q) &&
2800 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) &&
2801 !cpi->rc.is_src_frame_alt_ref) {
2802 const int maxq_adj_limit =
2803 rc->worst_quality - twopass->active_worst_quality;
2804 const int minq_adj_limit =
2805 (cpi->oxcf.rc_mode == VPX_CQ ? MINQ_ADJ_LIMIT_CQ : MINQ_ADJ_LIMIT);
2807 // Undershoot.
2808 if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) {
2809 --twopass->extend_maxq;
2810 if (rc->rolling_target_bits >= rc->rolling_actual_bits)
2811 ++twopass->extend_minq;
2812 // Overshoot.
2813 } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) {
2814 --twopass->extend_minq;
2815 if (rc->rolling_target_bits < rc->rolling_actual_bits)
2816 ++twopass->extend_maxq;
2817 } else {
2818 // Adjustment for extreme local overshoot.
2819 if (rc->projected_frame_size > (2 * rc->base_frame_target) &&
2820 rc->projected_frame_size > (2 * rc->avg_frame_bandwidth))
2821 ++twopass->extend_maxq;
2823 // Unwind undershoot or overshoot adjustment.
2824 if (rc->rolling_target_bits < rc->rolling_actual_bits)
2825 --twopass->extend_minq;
2826 else if (rc->rolling_target_bits > rc->rolling_actual_bits)
2827 --twopass->extend_maxq;
2830 twopass->extend_minq = clamp(twopass->extend_minq, 0, minq_adj_limit);
2831 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit);
2833 // If there is a big and undexpected undershoot then feed the extra
2834 // bits back in quickly. One situation where this may happen is if a
2835 // frame is unexpectedly almost perfectly predicted by the ARF or GF
2836 // but not very well predcited by the previous frame.
2837 if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) {
2838 int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO;
2839 if (rc->projected_frame_size < fast_extra_thresh) {
2840 rc->vbr_bits_off_target_fast +=
2841 fast_extra_thresh - rc->projected_frame_size;
2842 rc->vbr_bits_off_target_fast =
2843 MIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth));
2845 // Fast adaptation of minQ if necessary to use up the extra bits.
2846 if (rc->avg_frame_bandwidth) {
2847 twopass->extend_minq_fast =
2848 (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth);
2850 twopass->extend_minq_fast = MIN(twopass->extend_minq_fast,
2851 minq_adj_limit - twopass->extend_minq);
2852 } else if (rc->vbr_bits_off_target_fast) {
2853 twopass->extend_minq_fast = MIN(twopass->extend_minq_fast,
2854 minq_adj_limit - twopass->extend_minq);
2855 } else {
2856 twopass->extend_minq_fast = 0;