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.
15 #include "./vpx_scale_rtcd.h"
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_scale/vpx_scale.h"
19 #include "vpx_scale/yv12config.h"
21 #include "vp9/common/vp9_entropymv.h"
22 #include "vp9/common/vp9_quant_common.h"
23 #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes()
24 #include "vp9/common/vp9_systemdependent.h"
26 #include "vp9/encoder/vp9_aq_variance.h"
27 #include "vp9/encoder/vp9_block.h"
28 #include "vp9/encoder/vp9_encodeframe.h"
29 #include "vp9/encoder/vp9_encodemb.h"
30 #include "vp9/encoder/vp9_encodemv.h"
31 #include "vp9/encoder/vp9_extend.h"
32 #include "vp9/encoder/vp9_firstpass.h"
33 #include "vp9/encoder/vp9_mcomp.h"
34 #include "vp9/encoder/vp9_onyx_int.h"
35 #include "vp9/encoder/vp9_quantize.h"
36 #include "vp9/encoder/vp9_ratectrl.h"
37 #include "vp9/encoder/vp9_rdopt.h"
38 #include "vp9/encoder/vp9_variance.h"
43 #define IIKFACTOR1 12.5
44 #define IIKFACTOR2 15.0
47 #define ERR_DIVISOR 150.0
48 #define MIN_DECAY_FACTOR 0.1
50 #define KF_MB_INTRA_MIN 150
51 #define GF_MB_INTRA_MIN 100
53 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
55 #define MIN_KF_BOOST 300
57 #if CONFIG_MULTIPLE_ARF
58 // Set MIN_GF_INTERVAL to 1 for the full decomposition.
59 #define MIN_GF_INTERVAL 2
61 #define MIN_GF_INTERVAL 4
64 #define DISABLE_RC_LONG_TERM_MEM
66 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
67 YV12_BUFFER_CONFIG temp = *a;
72 static int gfboost_qadjust(int qindex) {
73 const double q = vp9_convert_qindex_to_q(qindex);
74 return (int)((0.00000828 * q * q * q) +
79 // Resets the first pass file to the given position using a relative seek from
80 // the current position.
81 static void reset_fpf_position(struct twopass_rc *p,
82 const FIRSTPASS_STATS *position) {
83 p->stats_in = position;
86 static int lookup_next_frame_stats(const struct twopass_rc *p,
87 FIRSTPASS_STATS *next_frame) {
88 if (p->stats_in >= p->stats_in_end)
91 *next_frame = *p->stats_in;
96 // Read frame stats at an offset from the current position.
97 static int read_frame_stats(const struct twopass_rc *p,
98 FIRSTPASS_STATS *frame_stats, int offset) {
99 const FIRSTPASS_STATS *fps_ptr = p->stats_in;
101 // Check legality of offset.
103 if (&fps_ptr[offset] >= p->stats_in_end)
105 } else if (offset < 0) {
106 if (&fps_ptr[offset] < p->stats_in_start)
110 *frame_stats = fps_ptr[offset];
114 static int input_stats(struct twopass_rc *p, FIRSTPASS_STATS *fps) {
115 if (p->stats_in >= p->stats_in_end)
123 static void output_stats(FIRSTPASS_STATS *stats,
124 struct vpx_codec_pkt_list *pktlist) {
125 struct vpx_codec_cx_pkt pkt;
126 pkt.kind = VPX_CODEC_STATS_PKT;
127 pkt.data.twopass_stats.buf = stats;
128 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
129 vpx_codec_pkt_list_add(pktlist, &pkt);
135 fpfile = fopen("firstpass.stt", "a");
137 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
138 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
139 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
143 stats->sr_coded_error,
144 stats->ssim_weighted_pred_err,
147 stats->pcnt_second_ref,
155 stats->mv_in_out_count,
164 static void zero_stats(FIRSTPASS_STATS *section) {
165 section->frame = 0.0;
166 section->intra_error = 0.0;
167 section->coded_error = 0.0;
168 section->sr_coded_error = 0.0;
169 section->ssim_weighted_pred_err = 0.0;
170 section->pcnt_inter = 0.0;
171 section->pcnt_motion = 0.0;
172 section->pcnt_second_ref = 0.0;
173 section->pcnt_neutral = 0.0;
175 section->mvr_abs = 0.0;
177 section->mvc_abs = 0.0;
180 section->mv_in_out_count = 0.0;
181 section->new_mv_count = 0.0;
182 section->count = 0.0;
183 section->duration = 1.0;
184 section->spatial_layer_id = 0;
187 static void accumulate_stats(FIRSTPASS_STATS *section,
188 const FIRSTPASS_STATS *frame) {
189 section->frame += frame->frame;
190 section->spatial_layer_id = frame->spatial_layer_id;
191 section->intra_error += frame->intra_error;
192 section->coded_error += frame->coded_error;
193 section->sr_coded_error += frame->sr_coded_error;
194 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
195 section->pcnt_inter += frame->pcnt_inter;
196 section->pcnt_motion += frame->pcnt_motion;
197 section->pcnt_second_ref += frame->pcnt_second_ref;
198 section->pcnt_neutral += frame->pcnt_neutral;
199 section->MVr += frame->MVr;
200 section->mvr_abs += frame->mvr_abs;
201 section->MVc += frame->MVc;
202 section->mvc_abs += frame->mvc_abs;
203 section->MVrv += frame->MVrv;
204 section->MVcv += frame->MVcv;
205 section->mv_in_out_count += frame->mv_in_out_count;
206 section->new_mv_count += frame->new_mv_count;
207 section->count += frame->count;
208 section->duration += frame->duration;
211 static void subtract_stats(FIRSTPASS_STATS *section,
212 const FIRSTPASS_STATS *frame) {
213 section->frame -= frame->frame;
214 section->intra_error -= frame->intra_error;
215 section->coded_error -= frame->coded_error;
216 section->sr_coded_error -= frame->sr_coded_error;
217 section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
218 section->pcnt_inter -= frame->pcnt_inter;
219 section->pcnt_motion -= frame->pcnt_motion;
220 section->pcnt_second_ref -= frame->pcnt_second_ref;
221 section->pcnt_neutral -= frame->pcnt_neutral;
222 section->MVr -= frame->MVr;
223 section->mvr_abs -= frame->mvr_abs;
224 section->MVc -= frame->MVc;
225 section->mvc_abs -= frame->mvc_abs;
226 section->MVrv -= frame->MVrv;
227 section->MVcv -= frame->MVcv;
228 section->mv_in_out_count -= frame->mv_in_out_count;
229 section->new_mv_count -= frame->new_mv_count;
230 section->count -= frame->count;
231 section->duration -= frame->duration;
234 static void avg_stats(FIRSTPASS_STATS *section) {
235 if (section->count < 1.0)
238 section->intra_error /= section->count;
239 section->coded_error /= section->count;
240 section->sr_coded_error /= section->count;
241 section->ssim_weighted_pred_err /= section->count;
242 section->pcnt_inter /= section->count;
243 section->pcnt_second_ref /= section->count;
244 section->pcnt_neutral /= section->count;
245 section->pcnt_motion /= section->count;
246 section->MVr /= section->count;
247 section->mvr_abs /= section->count;
248 section->MVc /= section->count;
249 section->mvc_abs /= section->count;
250 section->MVrv /= section->count;
251 section->MVcv /= section->count;
252 section->mv_in_out_count /= section->count;
253 section->duration /= section->count;
256 // Calculate a modified Error used in distributing bits between easier and
258 static double calculate_modified_err(const VP9_COMP *cpi,
259 const FIRSTPASS_STATS *this_frame) {
260 const struct twopass_rc *twopass = &cpi->twopass;
261 const SVC *const svc = &cpi->svc;
262 const FIRSTPASS_STATS *stats;
264 double modified_error;
266 if (svc->number_spatial_layers > 1 &&
267 svc->number_temporal_layers == 1) {
268 twopass = &svc->layer_context[svc->spatial_layer_id].twopass;
271 stats = &twopass->total_stats;
272 av_err = stats->ssim_weighted_pred_err / stats->count;
273 modified_error = av_err * pow(this_frame->ssim_weighted_pred_err /
274 DOUBLE_DIVIDE_CHECK(av_err),
275 cpi->oxcf.two_pass_vbrbias / 100.0);
277 return fclamp(modified_error,
278 twopass->modified_error_min, twopass->modified_error_max);
281 static const double weight_table[256] = {
282 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
283 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
284 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
285 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
286 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
287 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
288 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
289 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
290 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
291 0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
292 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
293 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
294 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
295 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
296 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
297 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
298 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
299 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
300 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
301 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
302 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
303 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
304 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
305 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
306 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
307 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
308 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
309 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
310 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
311 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
312 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
313 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
314 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
315 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
316 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
317 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
318 1.000000, 1.000000, 1.000000, 1.000000
321 static double simple_weight(const YV12_BUFFER_CONFIG *buf) {
324 const int w = buf->y_crop_width;
325 const int h = buf->y_crop_height;
326 const uint8_t *row = buf->y_buffer;
328 for (i = 0; i < h; ++i) {
329 const uint8_t *pixel = row;
330 for (j = 0; j < w; ++j)
331 sum += weight_table[*pixel++];
332 row += buf->y_stride;
335 return MAX(0.1, sum / (w * h));
338 // This function returns the maximum target rate per frame.
339 static int frame_max_bits(const RATE_CONTROL *rc, const VP9_CONFIG *oxcf) {
340 int64_t max_bits = ((int64_t)rc->av_per_frame_bandwidth *
341 (int64_t)oxcf->two_pass_vbrmax_section) / 100;
344 else if (max_bits > rc->max_frame_bandwidth)
345 max_bits = rc->max_frame_bandwidth;
347 return (int)max_bits;
350 void vp9_init_first_pass(VP9_COMP *cpi) {
351 zero_stats(&cpi->twopass.total_stats);
354 void vp9_end_first_pass(VP9_COMP *cpi) {
355 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
357 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
358 output_stats(&cpi->svc.layer_context[i].twopass.total_stats,
359 cpi->output_pkt_list);
362 output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
366 static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
379 static unsigned int zz_motion_search(const MACROBLOCK *x) {
380 const MACROBLOCKD *const xd = &x->e_mbd;
381 const uint8_t *const src = x->plane[0].src.buf;
382 const int src_stride = x->plane[0].src.stride;
383 const uint8_t *const ref = xd->plane[0].pre[0].buf;
384 const int ref_stride = xd->plane[0].pre[0].stride;
386 vp9_variance_fn_t fn = get_block_variance_fn(xd->mi[0]->mbmi.sb_type);
387 fn(src, src_stride, ref, ref_stride, &sse);
391 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
392 const MV *ref_mv, MV *best_mv,
393 int *best_motion_err) {
394 MACROBLOCKD *const xd = &x->e_mbd;
396 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3};
397 int num00, tmp_err, n, sr = 0;
399 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
400 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
401 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
402 int new_mv_mode_penalty = 256;
403 const int quart_frm = MIN(cpi->common.width, cpi->common.height);
405 // Refine the motion search range according to the frame dimension
406 // for first pass test.
407 while ((quart_frm << sr) < MAX_FULL_PEL_VAL)
413 // Override the default variance function to use MSE.
414 v_fn_ptr.vf = get_block_variance_fn(bsize);
416 // Center the initial step/diamond search on best mv.
417 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
419 x->sadperbit16, &num00, &v_fn_ptr,
422 if (tmp_err < INT_MAX)
423 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
424 if (tmp_err < INT_MAX - new_mv_mode_penalty)
425 tmp_err += new_mv_mode_penalty;
427 if (tmp_err < *best_motion_err) {
428 *best_motion_err = tmp_err;
429 best_mv->row = tmp_mv.row;
430 best_mv->col = tmp_mv.col;
433 // Carry out further step/diamond searches as necessary.
437 while (n < further_steps) {
443 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
444 step_param + n, x->sadperbit16,
448 if (tmp_err < INT_MAX)
449 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
450 if (tmp_err < INT_MAX - new_mv_mode_penalty)
451 tmp_err += new_mv_mode_penalty;
453 if (tmp_err < *best_motion_err) {
454 *best_motion_err = tmp_err;
455 best_mv->row = tmp_mv.row;
456 best_mv->col = tmp_mv.col;
462 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
463 if (2 * mb_col + 1 < cm->mi_cols) {
464 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16
467 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16
472 void vp9_first_pass(VP9_COMP *cpi) {
474 MACROBLOCK *const x = &cpi->mb;
475 VP9_COMMON *const cm = &cpi->common;
476 MACROBLOCKD *const xd = &x->e_mbd;
478 struct macroblock_plane *const p = x->plane;
479 struct macroblockd_plane *const pd = xd->plane;
480 const PICK_MODE_CONTEXT *ctx = &x->sb64_context;
483 int recon_yoffset, recon_uvoffset;
484 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
485 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
486 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
487 int recon_y_stride = lst_yv12->y_stride;
488 int recon_uv_stride = lst_yv12->uv_stride;
489 int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height);
490 int64_t intra_error = 0;
491 int64_t coded_error = 0;
492 int64_t sr_coded_error = 0;
494 int sum_mvr = 0, sum_mvc = 0;
495 int sum_mvr_abs = 0, sum_mvc_abs = 0;
496 int64_t sum_mvrs = 0, sum_mvcs = 0;
499 int second_ref_count = 0;
500 int intrapenalty = 256;
501 int neutral_count = 0;
502 int new_mv_count = 0;
503 int sum_in_vectors = 0;
504 uint32_t lastmv_as_int = 0;
505 struct twopass_rc *twopass = &cpi->twopass;
506 const MV zero_mv = {0, 0};
507 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
509 vp9_clear_system_state();
511 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
512 MV_REFERENCE_FRAME ref_frame = LAST_FRAME;
513 const YV12_BUFFER_CONFIG *scaled_ref_buf = NULL;
514 twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass;
516 vp9_scale_references(cpi);
518 // Use either last frame or alt frame for motion search.
519 if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
520 scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
521 ref_frame = LAST_FRAME;
522 } else if (cpi->ref_frame_flags & VP9_ALT_FLAG) {
523 scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, ALTREF_FRAME);
524 ref_frame = ALTREF_FRAME;
527 if (scaled_ref_buf != NULL) {
528 // Update the stride since we are using scaled reference buffer
529 first_ref_buf = scaled_ref_buf;
530 recon_y_stride = first_ref_buf->y_stride;
531 recon_uv_stride = first_ref_buf->uv_stride;
532 uv_mb_height = 16 >> (first_ref_buf->y_height > first_ref_buf->uv_height);
535 // Disable golden frame for svc first pass for now.
537 set_ref_ptrs(cm, xd, ref_frame, NONE);
540 vp9_setup_src_planes(x, cpi->Source, 0, 0);
541 vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
542 vp9_setup_dst_planes(xd, new_yv12, 0, 0);
544 xd->mi = cm->mi_grid_visible;
547 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
549 vp9_frame_init_quantizer(cpi);
551 for (i = 0; i < MAX_MB_PLANE; ++i) {
552 p[i].coeff = ctx->coeff_pbuf[i][1];
553 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
554 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
555 p[i].eobs = ctx->eobs_pbuf[i][1];
559 vp9_init_mv_probs(cm);
560 vp9_initialize_rd_consts(cpi);
562 // Tiling is ignored in the first pass.
563 vp9_tile_init(&tile, cm, 0, 0);
565 for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
568 best_ref_mv.as_int = 0;
570 // Reset above block coeffs.
571 xd->up_available = (mb_row != 0);
572 recon_yoffset = (mb_row * recon_y_stride * 16);
573 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height);
575 // Set up limit values for motion vectors to prevent them extending
576 // outside the UMV borders.
577 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
578 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
579 + BORDER_MV_PIXELS_B16;
581 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
583 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
584 double error_weight = 1.0;
585 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
587 vp9_clear_system_state();
589 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
590 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
591 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
592 xd->left_available = (mb_col != 0);
593 xd->mi[0]->mbmi.sb_type = bsize;
594 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME;
595 set_mi_row_col(xd, &tile,
596 mb_row << 1, num_8x8_blocks_high_lookup[bsize],
597 mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
598 cm->mi_rows, cm->mi_cols);
600 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
601 const int energy = vp9_block_energy(cpi, x, bsize);
602 error_weight = vp9_vaq_inv_q_ratio(energy);
605 // Do intra 16x16 prediction.
606 this_error = vp9_encode_intra(x, use_dc_pred);
607 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
608 vp9_clear_system_state();
609 this_error = (int)(this_error * error_weight);
612 // Intrapenalty below deals with situations where the intra and inter
613 // error scores are very low (e.g. a plain black frame).
614 // We do not have special cases in first pass for 0,0 and nearest etc so
615 // all inter modes carry an overhead cost estimate for the mv.
616 // When the error score is very low this causes us to pick all or lots of
617 // INTRA modes and throw lots of key frames.
618 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
619 this_error += intrapenalty;
621 // Accumulate the intra error.
622 intra_error += (int64_t)this_error;
624 // Set up limit values for motion vectors to prevent them extending
625 // outside the UMV borders.
626 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
627 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
629 // Other than for the first frame do a motion search.
630 if (cm->current_video_frame > 0) {
631 int tmp_err, motion_error;
634 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
635 motion_error = zz_motion_search(x);
636 // Assume 0,0 motion with no mv overhead.
637 mv.as_int = tmp_mv.as_int = 0;
639 // Test last reference frame using the previous best mv as the
640 // starting point (best reference) for the search.
641 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv,
643 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
644 vp9_clear_system_state();
645 motion_error = (int)(motion_error * error_weight);
648 // If the current best reference mv is not centered on 0,0 then do a 0,0
649 // based search as well.
650 if (best_ref_mv.as_int) {
652 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv,
654 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
655 vp9_clear_system_state();
656 tmp_err = (int)(tmp_err * error_weight);
659 if (tmp_err < motion_error) {
660 motion_error = tmp_err;
661 mv.as_int = tmp_mv.as_int;
665 // Search in an older reference frame.
666 if (cm->current_video_frame > 1 && gld_yv12 != NULL) {
667 // Assume 0,0 motion with no mv overhead.
670 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
671 gf_motion_error = zz_motion_search(x);
673 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv,
675 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
676 vp9_clear_system_state();
677 gf_motion_error = (int)(gf_motion_error * error_weight);
680 if (gf_motion_error < motion_error && gf_motion_error < this_error)
683 // Reset to last frame as reference buffer.
684 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
685 xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
686 xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
688 // In accumulating a score for the older reference frame take the
689 // best of the motion predicted score and the intra coded error
690 // (just as will be done for) accumulation of "coded_error" for
692 if (gf_motion_error < this_error)
693 sr_coded_error += gf_motion_error;
695 sr_coded_error += this_error;
697 sr_coded_error += motion_error;
699 // Start by assuming that intra mode is best.
700 best_ref_mv.as_int = 0;
702 if (motion_error <= this_error) {
703 // Keep a count of cases where the inter and intra were very close
704 // and very low. This helps with scene cut detection for example in
705 // cropped clips with black bars at the sides or top and bottom.
706 if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
707 this_error < 2 * intrapenalty)
712 this_error = motion_error;
713 xd->mi[0]->mbmi.mode = NEWMV;
714 xd->mi[0]->mbmi.mv[0] = mv;
715 xd->mi[0]->mbmi.tx_size = TX_4X4;
716 xd->mi[0]->mbmi.ref_frame[0] = LAST_FRAME;
717 xd->mi[0]->mbmi.ref_frame[1] = NONE;
718 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
719 vp9_encode_sby_pass1(x, bsize);
720 sum_mvr += mv.as_mv.row;
721 sum_mvr_abs += abs(mv.as_mv.row);
722 sum_mvc += mv.as_mv.col;
723 sum_mvc_abs += abs(mv.as_mv.col);
724 sum_mvrs += mv.as_mv.row * mv.as_mv.row;
725 sum_mvcs += mv.as_mv.col * mv.as_mv.col;
728 best_ref_mv.as_int = mv.as_int;
733 // Non-zero vector, was it different from the last non zero vector?
734 if (mv.as_int != lastmv_as_int)
736 lastmv_as_int = mv.as_int;
738 // Does the row vector point inwards or outwards?
739 if (mb_row < cm->mb_rows / 2) {
740 if (mv.as_mv.row > 0)
742 else if (mv.as_mv.row < 0)
744 } else if (mb_row > cm->mb_rows / 2) {
745 if (mv.as_mv.row > 0)
747 else if (mv.as_mv.row < 0)
751 // Does the col vector point inwards or outwards?
752 if (mb_col < cm->mb_cols / 2) {
753 if (mv.as_mv.col > 0)
755 else if (mv.as_mv.col < 0)
757 } else if (mb_col > cm->mb_cols / 2) {
758 if (mv.as_mv.col > 0)
760 else if (mv.as_mv.col < 0)
766 sr_coded_error += (int64_t)this_error;
768 coded_error += (int64_t)this_error;
770 // Adjust to the next column of MBs.
771 x->plane[0].src.buf += 16;
772 x->plane[1].src.buf += uv_mb_height;
773 x->plane[2].src.buf += uv_mb_height;
776 recon_uvoffset += uv_mb_height;
779 // Adjust to the next row of MBs.
780 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
781 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
782 uv_mb_height * cm->mb_cols;
783 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
784 uv_mb_height * cm->mb_cols;
786 vp9_clear_system_state();
789 vp9_clear_system_state();
793 fps.frame = cm->current_video_frame;
794 fps.spatial_layer_id = cpi->svc.spatial_layer_id;
795 fps.intra_error = (double)(intra_error >> 8);
796 fps.coded_error = (double)(coded_error >> 8);
797 fps.sr_coded_error = (double)(sr_coded_error >> 8);
798 fps.ssim_weighted_pred_err = fps.coded_error * simple_weight(cpi->Source);
800 fps.pcnt_inter = (double)intercount / cm->MBs;
801 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs;
802 fps.pcnt_neutral = (double)neutral_count / cm->MBs;
805 fps.MVr = (double)sum_mvr / mvcount;
806 fps.mvr_abs = (double)sum_mvr_abs / mvcount;
807 fps.MVc = (double)sum_mvc / mvcount;
808 fps.mvc_abs = (double)sum_mvc_abs / mvcount;
809 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount;
810 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount;
811 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
812 fps.new_mv_count = new_mv_count;
813 fps.pcnt_motion = (double)mvcount / cm->MBs;
821 fps.mv_in_out_count = 0.0;
822 fps.new_mv_count = 0.0;
823 fps.pcnt_motion = 0.0;
826 // TODO(paulwilkins): Handle the case when duration is set to 0, or
827 // something less than the full time between subsequent values of
828 // cpi->source_time_stamp.
829 fps.duration = (double)(cpi->source->ts_end - cpi->source->ts_start);
831 // Don't want to do output stats with a stack variable!
832 twopass->this_frame_stats = fps;
833 output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
834 accumulate_stats(&twopass->total_stats, &fps);
837 // Copy the previous Last Frame back into gf and and arf buffers if
838 // the prediction is good enough... but also don't allow it to lag too far.
839 if ((twopass->sr_update_lag > 3) ||
840 ((cm->current_video_frame > 0) &&
841 (twopass->this_frame_stats.pcnt_inter > 0.20) &&
842 ((twopass->this_frame_stats.intra_error /
843 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
844 if (gld_yv12 != NULL) {
845 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
847 twopass->sr_update_lag = 1;
849 ++twopass->sr_update_lag;
852 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) {
853 vp9_update_reference_frames(cpi);
855 // Swap frame pointers so last frame refers to the frame we just compressed.
856 swap_yv12(lst_yv12, new_yv12);
859 vp9_extend_frame_borders(lst_yv12);
861 // Special case for the first frame. Copy into the GF buffer as a second
863 if (cm->current_video_frame == 0 && gld_yv12 != NULL) {
864 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
867 // Use this to see what the first pass reconstruction looks like.
871 snprintf(filename, sizeof(filename), "enc%04d.yuv",
872 (int)cm->current_video_frame);
874 if (cm->current_video_frame == 0)
875 recon_file = fopen(filename, "wb");
877 recon_file = fopen(filename, "ab");
879 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
883 ++cm->current_video_frame;
886 static double calc_correction_factor(double err_per_mb,
891 const double error_term = err_per_mb / err_divisor;
893 // Adjustment based on actual quantizer to power term.
894 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low,
897 // Calculate correction factor.
898 if (power_term < 1.0)
899 assert(error_term >= 0.0);
901 return fclamp(pow(error_term, power_term), 0.05, 5.0);
904 int vp9_twopass_worst_quality(VP9_COMP *cpi, FIRSTPASS_STATS *fpstats,
905 int section_target_bandwitdh) {
907 const int num_mbs = cpi->common.MBs;
908 int target_norm_bits_per_mb;
909 const RATE_CONTROL *const rc = &cpi->rc;
911 const double section_err = fpstats->coded_error / fpstats->count;
912 const double err_per_mb = section_err / num_mbs;
913 const double speed_term = 1.0 + ((double)cpi->speed * 0.04);
915 if (section_target_bandwitdh <= 0)
916 return rc->worst_quality; // Highest value allowed
918 target_norm_bits_per_mb =
919 ((uint64_t)section_target_bandwitdh << BPER_MB_NORMBITS) / num_mbs;
921 // Try and pick a max Q that will be high enough to encode the
922 // content at the given rate.
923 for (q = rc->best_quality; q < rc->worst_quality; ++q) {
924 const double err_correction_factor = calc_correction_factor(err_per_mb,
925 ERR_DIVISOR, 0.5, 0.90, q);
926 const int bits_per_mb_at_this_q =
927 vp9_rc_bits_per_mb(INTER_FRAME, q, (err_correction_factor * speed_term));
928 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
932 // Restriction on active max q for constrained quality mode.
933 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
934 q = MAX(q, cpi->cq_target_quality);
939 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate);
941 void vp9_init_second_pass(VP9_COMP *cpi) {
942 SVC *const svc = &cpi->svc;
943 FIRSTPASS_STATS this_frame;
944 const FIRSTPASS_STATS *start_pos;
945 struct twopass_rc *twopass = &cpi->twopass;
946 const VP9_CONFIG *const oxcf = &cpi->oxcf;
947 const int is_spatial_svc = (svc->number_spatial_layers > 1) &&
948 (svc->number_temporal_layers == 1);
951 if (is_spatial_svc) {
952 twopass = &svc->layer_context[svc->spatial_layer_id].twopass;
955 zero_stats(&twopass->total_stats);
956 zero_stats(&twopass->total_left_stats);
958 if (!twopass->stats_in_end)
961 twopass->total_stats = *twopass->stats_in_end;
962 twopass->total_left_stats = twopass->total_stats;
964 frame_rate = 10000000.0 * twopass->total_stats.count /
965 twopass->total_stats.duration;
966 // Each frame can have a different duration, as the frame rate in the source
967 // isn't guaranteed to be constant. The frame rate prior to the first frame
968 // encoded in the second pass is a guess. However, the sum duration is not.
969 // It is calculated based on the actual durations of all frames from the
972 if (is_spatial_svc) {
973 vp9_update_spatial_layer_framerate(cpi, frame_rate);
975 (int64_t)(twopass->total_stats.duration *
976 svc->layer_context[svc->spatial_layer_id].target_bandwidth /
979 vp9_new_framerate(cpi, frame_rate);
980 twopass->bits_left = (int64_t)(twopass->total_stats.duration *
981 oxcf->target_bandwidth / 10000000.0);
984 cpi->output_framerate = oxcf->framerate;
986 // Calculate a minimum intra value to be used in determining the IIratio
987 // scores used in the second pass. We have this minimum to make sure
988 // that clips that are static but "low complexity" in the intra domain
989 // are still boosted appropriately for KF/GF/ARF.
990 if (!is_spatial_svc) {
991 // We don't know the number of MBs for each layer at this point.
992 // So we will do it later.
993 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
994 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
997 // This variable monitors how far behind the second ref update is lagging.
998 twopass->sr_update_lag = 1;
1000 // Scan the first pass file and calculate an average Intra / Inter error
1001 // score ratio for the sequence.
1003 double sum_iiratio = 0.0;
1004 start_pos = twopass->stats_in;
1006 while (input_stats(twopass, &this_frame) != EOF) {
1007 const double iiratio = this_frame.intra_error /
1008 DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1009 sum_iiratio += fclamp(iiratio, 1.0, 20.0);
1012 twopass->avg_iiratio = sum_iiratio /
1013 DOUBLE_DIVIDE_CHECK((double)twopass->total_stats.count);
1015 reset_fpf_position(twopass, start_pos);
1018 // Scan the first pass file and calculate a modified total error based upon
1019 // the bias/power function used to allocate bits.
1021 double av_error = twopass->total_stats.ssim_weighted_pred_err /
1022 DOUBLE_DIVIDE_CHECK(twopass->total_stats.count);
1024 start_pos = twopass->stats_in;
1026 twopass->modified_error_total = 0.0;
1027 twopass->modified_error_min =
1028 (av_error * oxcf->two_pass_vbrmin_section) / 100;
1029 twopass->modified_error_max =
1030 (av_error * oxcf->two_pass_vbrmax_section) / 100;
1032 while (input_stats(twopass, &this_frame) != EOF) {
1033 twopass->modified_error_total +=
1034 calculate_modified_err(cpi, &this_frame);
1036 twopass->modified_error_left = twopass->modified_error_total;
1038 reset_fpf_position(twopass, start_pos);
1042 // This function gives an estimate of how badly we believe the prediction
1043 // quality is decaying from frame to frame.
1044 static double get_prediction_decay_rate(const VP9_COMMON *cm,
1045 const FIRSTPASS_STATS *next_frame) {
1046 // Look at the observed drop in prediction quality between the last frame
1047 // and the GF buffer (which contains an older frame).
1048 const double mb_sr_err_diff = (next_frame->sr_coded_error -
1049 next_frame->coded_error) / cm->MBs;
1050 const double second_ref_decay = mb_sr_err_diff <= 512.0
1051 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0)
1054 return MIN(second_ref_decay, next_frame->pcnt_inter);
1057 // Function to test for a condition where a complex transition is followed
1058 // by a static section. For example in slide shows where there is a fade
1059 // between slides. This is to help with more optimal kf and gf positioning.
1060 static int detect_transition_to_still(struct twopass_rc *twopass,
1061 int frame_interval, int still_interval,
1062 double loop_decay_rate,
1063 double last_decay_rate) {
1064 int trans_to_still = 0;
1066 // Break clause to detect very still sections after motion
1067 // For example a static image after a fade or other transition
1068 // instead of a clean scene cut.
1069 if (frame_interval > MIN_GF_INTERVAL &&
1070 loop_decay_rate >= 0.999 &&
1071 last_decay_rate < 0.9) {
1073 const FIRSTPASS_STATS *position = twopass->stats_in;
1074 FIRSTPASS_STATS tmp_next_frame;
1076 // Look ahead a few frames to see if static condition persists...
1077 for (j = 0; j < still_interval; ++j) {
1078 if (EOF == input_stats(twopass, &tmp_next_frame))
1081 if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999)
1085 reset_fpf_position(twopass, position);
1087 // Only if it does do we signal a transition to still.
1088 if (j == still_interval)
1092 return trans_to_still;
1095 // This function detects a flash through the high relative pcnt_second_ref
1096 // score in the frame following a flash frame. The offset passed in should
1098 static int detect_flash(const struct twopass_rc *twopass, int offset) {
1099 FIRSTPASS_STATS next_frame;
1101 int flash_detected = 0;
1103 // Read the frame data.
1104 // The return is FALSE (no flash detected) if not a valid frame
1105 if (read_frame_stats(twopass, &next_frame, offset) != EOF) {
1106 // What we are looking for here is a situation where there is a
1107 // brief break in prediction (such as a flash) but subsequent frames
1108 // are reasonably well predicted by an earlier (pre flash) frame.
1109 // The recovery after a flash is indicated by a high pcnt_second_ref
1110 // compared to pcnt_inter.
1111 if (next_frame.pcnt_second_ref > next_frame.pcnt_inter &&
1112 next_frame.pcnt_second_ref >= 0.5)
1116 return flash_detected;
1119 // Update the motion related elements to the GF arf boost calculation.
1120 static void accumulate_frame_motion_stats(
1121 FIRSTPASS_STATS *this_frame,
1122 double *this_frame_mv_in_out,
1123 double *mv_in_out_accumulator,
1124 double *abs_mv_in_out_accumulator,
1125 double *mv_ratio_accumulator) {
1128 // Accumulate motion stats.
1129 motion_pct = this_frame->pcnt_motion;
1131 // Accumulate Motion In/Out of frame stats.
1132 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1133 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1134 *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct);
1136 // Accumulate a measure of how uniform (or conversely how random)
1137 // the motion field is (a ratio of absmv / mv).
1138 if (motion_pct > 0.05) {
1139 const double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1140 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1142 const double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1143 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1145 *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs)
1146 ? (this_frame_mvr_ratio * motion_pct)
1147 : this_frame->mvr_abs * motion_pct;
1149 *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs)
1150 ? (this_frame_mvc_ratio * motion_pct)
1151 : this_frame->mvc_abs * motion_pct;
1155 // Calculate a baseline boost number for the current frame.
1156 static double calc_frame_boost(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame,
1157 double this_frame_mv_in_out) {
1160 // Underlying boost factor is based on inter intra error ratio.
1161 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1162 frame_boost = (IIFACTOR * this_frame->intra_error /
1163 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1165 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1166 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1168 // Increase boost for frames where new data coming into frame (e.g. zoom out).
1169 // Slightly reduce boost if there is a net balance of motion out of the frame
1170 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1171 if (this_frame_mv_in_out > 0.0)
1172 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1173 // In the extreme case the boost is halved.
1175 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1177 return MIN(frame_boost, GF_RMAX);
1180 static int calc_arf_boost(VP9_COMP *cpi, int offset,
1181 int f_frames, int b_frames,
1182 int *f_boost, int *b_boost) {
1183 FIRSTPASS_STATS this_frame;
1184 struct twopass_rc *const twopass = &cpi->twopass;
1186 double boost_score = 0.0;
1187 double mv_ratio_accumulator = 0.0;
1188 double decay_accumulator = 1.0;
1189 double this_frame_mv_in_out = 0.0;
1190 double mv_in_out_accumulator = 0.0;
1191 double abs_mv_in_out_accumulator = 0.0;
1193 int flash_detected = 0;
1195 // Search forward from the proposed arf/next gf position.
1196 for (i = 0; i < f_frames; ++i) {
1197 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF)
1200 // Update the motion related elements to the boost calculation.
1201 accumulate_frame_motion_stats(&this_frame,
1202 &this_frame_mv_in_out, &mv_in_out_accumulator,
1203 &abs_mv_in_out_accumulator,
1204 &mv_ratio_accumulator);
1206 // We want to discount the flash frame itself and the recovery
1207 // frame that follows as both will have poor scores.
1208 flash_detected = detect_flash(twopass, i + offset) ||
1209 detect_flash(twopass, i + offset + 1);
1211 // Accumulate the effect of prediction quality decay.
1212 if (!flash_detected) {
1213 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame);
1214 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1215 ? MIN_DECAY_FACTOR : decay_accumulator;
1218 boost_score += (decay_accumulator *
1219 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1222 *f_boost = (int)boost_score;
1224 // Reset for backward looking loop.
1226 mv_ratio_accumulator = 0.0;
1227 decay_accumulator = 1.0;
1228 this_frame_mv_in_out = 0.0;
1229 mv_in_out_accumulator = 0.0;
1230 abs_mv_in_out_accumulator = 0.0;
1232 // Search backward towards last gf position.
1233 for (i = -1; i >= -b_frames; --i) {
1234 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF)
1237 // Update the motion related elements to the boost calculation.
1238 accumulate_frame_motion_stats(&this_frame,
1239 &this_frame_mv_in_out, &mv_in_out_accumulator,
1240 &abs_mv_in_out_accumulator,
1241 &mv_ratio_accumulator);
1243 // We want to discount the the flash frame itself and the recovery
1244 // frame that follows as both will have poor scores.
1245 flash_detected = detect_flash(twopass, i + offset) ||
1246 detect_flash(twopass, i + offset + 1);
1248 // Cumulative effect of prediction quality decay.
1249 if (!flash_detected) {
1250 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame);
1251 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1252 ? MIN_DECAY_FACTOR : decay_accumulator;
1255 boost_score += (decay_accumulator *
1256 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1258 *b_boost = (int)boost_score;
1260 arf_boost = (*f_boost + *b_boost);
1261 if (arf_boost < ((b_frames + f_frames) * 20))
1262 arf_boost = ((b_frames + f_frames) * 20);
1267 #if CONFIG_MULTIPLE_ARF
1268 // Work out the frame coding order for a GF or an ARF group.
1269 // The current implementation codes frames in their natural order for a
1270 // GF group, and inserts additional ARFs into an ARF group using a
1271 // binary split approach.
1272 // NOTE: this function is currently implemented recursively.
1273 static void schedule_frames(VP9_COMP *cpi, const int start, const int end,
1274 const int arf_idx, const int gf_or_arf_group,
1276 int i, abs_end, half_range;
1277 int *cfo = cpi->frame_coding_order;
1278 int idx = cpi->new_frame_coding_order_period;
1280 // If (end < 0) an ARF should be coded at position (-end).
1283 // printf("start:%d end:%d\n", start, end);
1285 // GF Group: code frames in logical order.
1286 if (gf_or_arf_group == 0) {
1287 assert(end >= start);
1288 for (i = start; i <= end; ++i) {
1290 cpi->arf_buffer_idx[idx] = arf_idx;
1291 cpi->arf_weight[idx] = -1;
1294 cpi->new_frame_coding_order_period = idx;
1298 // ARF Group: Work out the ARF schedule and mark ARF frames as negative.
1300 // printf("start:%d end:%d\n", -end, -end);
1301 // ARF frame is at the end of the range.
1303 // What ARF buffer does this ARF use as predictor.
1304 cpi->arf_buffer_idx[idx] = (arf_idx > 2) ? (arf_idx - 1) : 2;
1305 cpi->arf_weight[idx] = level;
1312 half_range = (abs_end - start) >> 1;
1314 // ARFs may not be adjacent, they must be separated by at least
1315 // MIN_GF_INTERVAL non-ARF frames.
1316 if ((start + MIN_GF_INTERVAL) >= (abs_end - MIN_GF_INTERVAL)) {
1317 // printf("start:%d end:%d\n", start, abs_end);
1318 // Update the coding order and active ARF.
1319 for (i = start; i <= abs_end; ++i) {
1321 cpi->arf_buffer_idx[idx] = arf_idx;
1322 cpi->arf_weight[idx] = -1;
1325 cpi->new_frame_coding_order_period = idx;
1327 // Place a new ARF at the mid-point of the range.
1328 cpi->new_frame_coding_order_period = idx;
1329 schedule_frames(cpi, start, -(start + half_range), arf_idx + 1,
1330 gf_or_arf_group, level + 1);
1331 schedule_frames(cpi, start + half_range + 1, abs_end, arf_idx,
1332 gf_or_arf_group, level + 1);
1336 #define FIXED_ARF_GROUP_SIZE 16
1338 void define_fixed_arf_period(VP9_COMP *cpi) {
1340 int max_level = INT_MIN;
1342 assert(cpi->multi_arf_enabled);
1343 assert(cpi->oxcf.lag_in_frames >= FIXED_ARF_GROUP_SIZE);
1345 // Save the weight of the last frame in the sequence before next
1346 // sequence pattern overwrites it.
1347 cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
1348 assert(cpi->this_frame_weight >= 0);
1350 cpi->twopass.gf_zeromotion_pct = 0;
1352 // Initialize frame coding order variables.
1353 cpi->new_frame_coding_order_period = 0;
1354 cpi->next_frame_in_order = 0;
1355 cpi->arf_buffered = 0;
1356 vp9_zero(cpi->frame_coding_order);
1357 vp9_zero(cpi->arf_buffer_idx);
1358 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
1360 if (cpi->rc.frames_to_key <= (FIXED_ARF_GROUP_SIZE + 8)) {
1361 // Setup a GF group close to the keyframe.
1362 cpi->rc.source_alt_ref_pending = 0;
1363 cpi->rc.baseline_gf_interval = cpi->rc.frames_to_key;
1364 schedule_frames(cpi, 0, (cpi->rc.baseline_gf_interval - 1), 2, 0, 0);
1366 // Setup a fixed period ARF group.
1367 cpi->rc.source_alt_ref_pending = 1;
1368 cpi->rc.baseline_gf_interval = FIXED_ARF_GROUP_SIZE;
1369 schedule_frames(cpi, 0, -(cpi->rc.baseline_gf_interval - 1), 2, 1, 0);
1372 // Replace level indicator of -1 with correct level.
1373 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1374 if (cpi->arf_weight[i] > max_level) {
1375 max_level = cpi->arf_weight[i];
1379 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1380 if (cpi->arf_weight[i] == -1) {
1381 cpi->arf_weight[i] = max_level;
1384 cpi->max_arf_level = max_level;
1386 printf("\nSchedule: ");
1387 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1388 printf("%4d ", cpi->frame_coding_order[i]);
1392 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1393 printf("%4d ", cpi->arf_buffer_idx[i]);
1397 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1398 printf("%4d ", cpi->arf_weight[i]);
1405 // Analyse and define a gf/arf group.
1406 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1407 RATE_CONTROL *const rc = &cpi->rc;
1408 VP9_CONFIG *const oxcf = &cpi->oxcf;
1409 struct twopass_rc *const twopass = &cpi->twopass;
1410 FIRSTPASS_STATS next_frame = { 0 };
1411 const FIRSTPASS_STATS *start_pos;
1413 double boost_score = 0.0;
1414 double old_boost_score = 0.0;
1415 double gf_group_err = 0.0;
1416 double gf_first_frame_err = 0.0;
1417 double mod_frame_err = 0.0;
1419 double mv_ratio_accumulator = 0.0;
1420 double decay_accumulator = 1.0;
1421 double zero_motion_accumulator = 1.0;
1423 double loop_decay_rate = 1.00;
1424 double last_loop_decay_rate = 1.00;
1426 double this_frame_mv_in_out = 0.0;
1427 double mv_in_out_accumulator = 0.0;
1428 double abs_mv_in_out_accumulator = 0.0;
1429 double mv_ratio_accumulator_thresh;
1430 // Max bits for a single frame.
1431 const int max_bits = frame_max_bits(rc, oxcf);
1432 unsigned int allow_alt_ref = oxcf->play_alternate && oxcf->lag_in_frames;
1437 int active_max_gf_interval;
1439 twopass->gf_group_bits = 0;
1441 vp9_clear_system_state();
1443 start_pos = twopass->stats_in;
1445 // Load stats for the current frame.
1446 mod_frame_err = calculate_modified_err(cpi, this_frame);
1448 // Note the error of the frame at the start of the group. This will be
1449 // the GF frame error if we code a normal gf.
1450 gf_first_frame_err = mod_frame_err;
1452 // If this is a key frame or the overlay from a previous arf then
1453 // the error score / cost of this frame has already been accounted for.
1454 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1455 gf_group_err -= gf_first_frame_err;
1457 // Motion breakout threshold for loop below depends on image size.
1458 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0;
1460 // Work out a maximum interval for the GF.
1461 // If the image appears completely static we can extend beyond this.
1462 // The value chosen depends on the active Q range. At low Q we have
1463 // bits to spare and are better with a smaller interval and smaller boost.
1464 // At high Q when there are few bits to spare we are better with a longer
1465 // interval to spread the cost of the GF.
1467 active_max_gf_interval =
1468 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5);
1470 if (active_max_gf_interval > rc->max_gf_interval)
1471 active_max_gf_interval = rc->max_gf_interval;
1474 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
1477 // Accumulate error score of frames in this gf group.
1478 mod_frame_err = calculate_modified_err(cpi, this_frame);
1479 gf_group_err += mod_frame_err;
1481 if (EOF == input_stats(twopass, &next_frame))
1484 // Test for the case where there is a brief flash but the prediction
1485 // quality back to an earlier frame is then restored.
1486 flash_detected = detect_flash(twopass, 0);
1488 // Update the motion related elements to the boost calculation.
1489 accumulate_frame_motion_stats(&next_frame,
1490 &this_frame_mv_in_out, &mv_in_out_accumulator,
1491 &abs_mv_in_out_accumulator,
1492 &mv_ratio_accumulator);
1494 // Accumulate the effect of prediction quality decay.
1495 if (!flash_detected) {
1496 last_loop_decay_rate = loop_decay_rate;
1497 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
1498 decay_accumulator = decay_accumulator * loop_decay_rate;
1500 // Monitor for static sections.
1501 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
1502 zero_motion_accumulator) {
1503 zero_motion_accumulator = next_frame.pcnt_inter -
1504 next_frame.pcnt_motion;
1507 // Break clause to detect very still sections after motion. For example,
1508 // a static image after a fade or other transition.
1509 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate,
1510 last_loop_decay_rate)) {
1516 // Calculate a boost number for this frame.
1517 boost_score += (decay_accumulator *
1518 calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out));
1520 // Break out conditions.
1522 // Break at cpi->max_gf_interval unless almost totally static.
1523 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
1525 // Don't break out with a very short interval.
1526 (i > MIN_GF_INTERVAL) &&
1527 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) &&
1528 (!flash_detected) &&
1529 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
1530 (abs_mv_in_out_accumulator > 3.0) ||
1531 (mv_in_out_accumulator < -2.0) ||
1532 ((boost_score - old_boost_score) < IIFACTOR)))) {
1533 boost_score = old_boost_score;
1537 *this_frame = next_frame;
1539 old_boost_score = boost_score;
1542 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
1544 // Don't allow a gf too near the next kf.
1545 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) {
1546 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) {
1549 if (EOF == input_stats(twopass, this_frame))
1552 if (i < rc->frames_to_key) {
1553 mod_frame_err = calculate_modified_err(cpi, this_frame);
1554 gf_group_err += mod_frame_err;
1559 #if CONFIG_MULTIPLE_ARF
1560 if (cpi->multi_arf_enabled) {
1561 // Initialize frame coding order variables.
1562 cpi->new_frame_coding_order_period = 0;
1563 cpi->next_frame_in_order = 0;
1564 cpi->arf_buffered = 0;
1565 vp9_zero(cpi->frame_coding_order);
1566 vp9_zero(cpi->arf_buffer_idx);
1567 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
1571 // Set the interval until the next gf.
1572 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1573 rc->baseline_gf_interval = i - 1;
1575 rc->baseline_gf_interval = i;
1577 // Should we use the alternate reference frame.
1578 if (allow_alt_ref &&
1579 (i < cpi->oxcf.lag_in_frames) &&
1580 (i >= MIN_GF_INTERVAL) &&
1581 // For real scene cuts (not forced kfs) don't allow arf very near kf.
1582 (rc->next_key_frame_forced ||
1583 (i <= (rc->frames_to_key - MIN_GF_INTERVAL)))) {
1584 // Calculate the boost for alt ref.
1585 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
1587 rc->source_alt_ref_pending = 1;
1589 #if CONFIG_MULTIPLE_ARF
1590 // Set the ARF schedule.
1591 if (cpi->multi_arf_enabled) {
1592 schedule_frames(cpi, 0, -(rc->baseline_gf_interval - 1), 2, 1, 0);
1596 rc->gfu_boost = (int)boost_score;
1597 rc->source_alt_ref_pending = 0;
1598 #if CONFIG_MULTIPLE_ARF
1599 // Set the GF schedule.
1600 if (cpi->multi_arf_enabled) {
1601 schedule_frames(cpi, 0, rc->baseline_gf_interval - 1, 2, 0, 0);
1602 assert(cpi->new_frame_coding_order_period ==
1603 rc->baseline_gf_interval);
1608 #if CONFIG_MULTIPLE_ARF
1609 if (cpi->multi_arf_enabled && (cpi->common.frame_type != KEY_FRAME)) {
1610 int max_level = INT_MIN;
1611 // Replace level indicator of -1 with correct level.
1612 for (i = 0; i < cpi->frame_coding_order_period; ++i) {
1613 if (cpi->arf_weight[i] > max_level) {
1614 max_level = cpi->arf_weight[i];
1618 for (i = 0; i < cpi->frame_coding_order_period; ++i) {
1619 if (cpi->arf_weight[i] == -1) {
1620 cpi->arf_weight[i] = max_level;
1623 cpi->max_arf_level = max_level;
1626 if (cpi->multi_arf_enabled) {
1627 printf("\nSchedule: ");
1628 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1629 printf("%4d ", cpi->frame_coding_order[i]);
1633 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1634 printf("%4d ", cpi->arf_buffer_idx[i]);
1638 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1639 printf("%4d ", cpi->arf_weight[i]);
1646 // Calculate the bits to be allocated to the group as a whole.
1647 if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) {
1648 twopass->gf_group_bits = (int64_t)(twopass->kf_group_bits *
1649 (gf_group_err / twopass->kf_group_error_left));
1651 twopass->gf_group_bits = 0;
1653 twopass->gf_group_bits = (twopass->gf_group_bits < 0) ?
1654 0 : (twopass->gf_group_bits > twopass->kf_group_bits) ?
1655 twopass->kf_group_bits : twopass->gf_group_bits;
1657 // Clip cpi->twopass.gf_group_bits based on user supplied data rate
1658 // variability limit, cpi->oxcf.two_pass_vbrmax_section.
1659 if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
1660 twopass->gf_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
1662 // Reset the file position.
1663 reset_fpf_position(twopass, start_pos);
1665 // Assign bits to the arf or gf.
1666 for (i = 0; i <= (rc->source_alt_ref_pending &&
1667 cpi->common.frame_type != KEY_FRAME); ++i) {
1668 int allocation_chunks;
1669 int q = rc->last_q[INTER_FRAME];
1672 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100;
1674 // Set max and minimum boost and hence minimum allocation.
1675 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200);
1677 if (rc->source_alt_ref_pending && i == 0)
1678 allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost;
1680 allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100);
1682 // Prevent overflow.
1684 int divisor = boost >> 10;
1686 allocation_chunks /= divisor;
1689 // Calculate the number of bits to be spent on the gf or arf based on
1690 // the boost number.
1691 gf_bits = (int)((double)boost * (twopass->gf_group_bits /
1692 (double)allocation_chunks));
1694 // If the frame that is to be boosted is simpler than the average for
1695 // the gf/arf group then use an alternative calculation
1696 // based on the error score of the frame itself.
1697 if (rc->baseline_gf_interval < 1 ||
1698 mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) {
1699 double alt_gf_grp_bits = (double)twopass->kf_group_bits *
1700 (mod_frame_err * (double)rc->baseline_gf_interval) /
1701 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left);
1703 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits /
1704 (double)allocation_chunks));
1706 if (gf_bits > alt_gf_bits)
1707 gf_bits = alt_gf_bits;
1709 // If it is harder than other frames in the group make sure it at
1710 // least receives an allocation in keeping with its relative error
1711 // score, otherwise it may be worse off than an "un-boosted" frame.
1712 int alt_gf_bits = (int)((double)twopass->kf_group_bits *
1714 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left));
1716 if (alt_gf_bits > gf_bits)
1717 gf_bits = alt_gf_bits;
1720 // Don't allow a negative value for gf_bits.
1725 twopass->gf_bits = gf_bits;
1728 (!rc->source_alt_ref_pending &&
1729 cpi->common.frame_type != KEY_FRAME)) {
1730 // Calculate the per frame bit target for this frame.
1731 vp9_rc_set_frame_target(cpi, gf_bits);
1736 // Adjust KF group bits and error remaining.
1737 twopass->kf_group_error_left -= (int64_t)gf_group_err;
1739 // If this is an arf update we want to remove the score for the overlay
1740 // frame at the end which will usually be very cheap to code.
1741 // The overlay frame has already, in effect, been coded so we want to spread
1742 // the remaining bits among the other frames.
1743 // For normal GFs remove the score for the GF itself unless this is
1744 // also a key frame in which case it has already been accounted for.
1745 if (rc->source_alt_ref_pending) {
1746 twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err);
1747 } else if (cpi->common.frame_type != KEY_FRAME) {
1748 twopass->gf_group_error_left = (int64_t)(gf_group_err
1749 - gf_first_frame_err);
1751 twopass->gf_group_error_left = (int64_t)gf_group_err;
1754 // This condition could fail if there are two kfs very close together
1755 // despite MIN_GF_INTERVAL and would cause a divide by 0 in the
1756 // calculation of alt_extra_bits.
1757 if (rc->baseline_gf_interval >= 3) {
1758 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost;
1761 const int pct_extra = MIN(20, (boost - 100) / 50);
1762 const int alt_extra_bits = (int)((
1763 MAX(twopass->gf_group_bits - twopass->gf_bits, 0) *
1765 twopass->gf_group_bits -= alt_extra_bits;
1770 if (cpi->common.frame_type != KEY_FRAME) {
1771 FIRSTPASS_STATS sectionstats;
1773 zero_stats(§ionstats);
1774 reset_fpf_position(twopass, start_pos);
1776 for (i = 0; i < rc->baseline_gf_interval; ++i) {
1777 input_stats(twopass, &next_frame);
1778 accumulate_stats(§ionstats, &next_frame);
1781 avg_stats(§ionstats);
1783 twopass->section_intra_rating = (int)
1784 (sectionstats.intra_error /
1785 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
1787 reset_fpf_position(twopass, start_pos);
1791 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1792 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1793 struct twopass_rc *twopass = &cpi->twopass;
1794 // For a single frame.
1795 const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf);
1796 // Calculate modified prediction error used in bit allocation.
1797 const double modified_err = calculate_modified_err(cpi, this_frame);
1798 int target_frame_size;
1799 double err_fraction;
1801 if (twopass->gf_group_error_left > 0)
1802 // What portion of the remaining GF group error is used by this frame.
1803 err_fraction = modified_err / twopass->gf_group_error_left;
1807 // How many of those bits available for allocation should we give it?
1808 target_frame_size = (int)((double)twopass->gf_group_bits * err_fraction);
1810 // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at
1812 target_frame_size = clamp(target_frame_size, 0,
1813 MIN(max_bits, (int)twopass->gf_group_bits));
1815 // Adjust error and bits remaining.
1816 twopass->gf_group_error_left -= (int64_t)modified_err;
1818 // Per frame bit target for this frame.
1819 vp9_rc_set_frame_target(cpi, target_frame_size);
1822 static int test_candidate_kf(struct twopass_rc *twopass,
1823 const FIRSTPASS_STATS *last_frame,
1824 const FIRSTPASS_STATS *this_frame,
1825 const FIRSTPASS_STATS *next_frame) {
1826 int is_viable_kf = 0;
1828 // Does the frame satisfy the primary criteria of a key frame?
1829 // If so, then examine how well it predicts subsequent frames.
1830 if ((this_frame->pcnt_second_ref < 0.10) &&
1831 (next_frame->pcnt_second_ref < 0.10) &&
1832 ((this_frame->pcnt_inter < 0.05) ||
1833 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) &&
1834 ((this_frame->intra_error /
1835 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
1836 ((fabs(last_frame->coded_error - this_frame->coded_error) /
1837 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) ||
1838 (fabs(last_frame->intra_error - this_frame->intra_error) /
1839 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) ||
1840 ((next_frame->intra_error /
1841 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
1843 const FIRSTPASS_STATS *start_pos = twopass->stats_in;
1844 FIRSTPASS_STATS local_next_frame = *next_frame;
1845 double boost_score = 0.0;
1846 double old_boost_score = 0.0;
1847 double decay_accumulator = 1.0;
1849 // Examine how well the key frame predicts subsequent frames.
1850 for (i = 0; i < 16; ++i) {
1851 double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
1852 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
1854 if (next_iiratio > RMAX)
1855 next_iiratio = RMAX;
1857 // Cumulative effect of decay in prediction quality.
1858 if (local_next_frame.pcnt_inter > 0.85)
1859 decay_accumulator *= local_next_frame.pcnt_inter;
1861 decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
1863 // Keep a running total.
1864 boost_score += (decay_accumulator * next_iiratio);
1866 // Test various breakout clauses.
1867 if ((local_next_frame.pcnt_inter < 0.05) ||
1868 (next_iiratio < 1.5) ||
1869 (((local_next_frame.pcnt_inter -
1870 local_next_frame.pcnt_neutral) < 0.20) &&
1871 (next_iiratio < 3.0)) ||
1872 ((boost_score - old_boost_score) < 3.0) ||
1873 (local_next_frame.intra_error < 200)) {
1877 old_boost_score = boost_score;
1879 // Get the next frame details
1880 if (EOF == input_stats(twopass, &local_next_frame))
1884 // If there is tolerable prediction for at least the next 3 frames then
1885 // break out else discard this potential key frame and move on
1886 if (boost_score > 30.0 && (i > 3)) {
1889 // Reset the file position
1890 reset_fpf_position(twopass, start_pos);
1896 return is_viable_kf;
1899 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1901 RATE_CONTROL *const rc = &cpi->rc;
1902 struct twopass_rc *const twopass = &cpi->twopass;
1903 const FIRSTPASS_STATS first_frame = *this_frame;
1904 const FIRSTPASS_STATS *start_position = twopass->stats_in;
1905 FIRSTPASS_STATS next_frame;
1906 FIRSTPASS_STATS last_frame;
1907 double decay_accumulator = 1.0;
1908 double zero_motion_accumulator = 1.0;
1909 double boost_score = 0.0;
1910 double kf_mod_err = 0.0;
1911 double kf_group_err = 0.0;
1912 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
1914 vp9_zero(next_frame);
1916 cpi->common.frame_type = KEY_FRAME;
1918 // Is this a forced key frame by interval.
1919 rc->this_key_frame_forced = rc->next_key_frame_forced;
1921 // Clear the alt ref active flag as this can never be active on a key frame.
1922 rc->source_alt_ref_active = 0;
1924 // KF is always a GF so clear frames till next gf counter.
1925 rc->frames_till_gf_update_due = 0;
1927 rc->frames_to_key = 1;
1929 twopass->kf_group_bits = 0; // Total bits available to kf group
1930 twopass->kf_group_error_left = 0; // Group modified error score.
1932 kf_mod_err = calculate_modified_err(cpi, this_frame);
1934 // Find the next keyframe.
1936 while (twopass->stats_in < twopass->stats_in_end) {
1937 // Accumulate kf group error.
1938 kf_group_err += calculate_modified_err(cpi, this_frame);
1940 // Load the next frame's stats.
1941 last_frame = *this_frame;
1942 input_stats(twopass, this_frame);
1944 // Provided that we are not at the end of the file...
1945 if (cpi->oxcf.auto_key &&
1946 lookup_next_frame_stats(twopass, &next_frame) != EOF) {
1947 double loop_decay_rate;
1949 // Check for a scene cut.
1950 if (test_candidate_kf(twopass, &last_frame, this_frame, &next_frame))
1953 // How fast is the prediction quality decaying?
1954 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
1956 // We want to know something about the recent past... rather than
1957 // as used elsewhere where we are concerned with decay in prediction
1958 // quality since the last GF or KF.
1959 recent_loop_decay[i % 8] = loop_decay_rate;
1960 decay_accumulator = 1.0;
1961 for (j = 0; j < 8; ++j)
1962 decay_accumulator *= recent_loop_decay[j];
1964 // Special check for transition or high motion followed by a
1966 if (detect_transition_to_still(twopass, i, cpi->key_frame_frequency - i,
1967 loop_decay_rate, decay_accumulator))
1970 // Step on to the next frame.
1971 ++rc->frames_to_key;
1973 // If we don't have a real key frame within the next two
1974 // key_frame_frequency intervals then break out of the loop.
1975 if (rc->frames_to_key >= 2 * (int)cpi->key_frame_frequency)
1978 ++rc->frames_to_key;
1983 // If there is a max kf interval set by the user we must obey it.
1984 // We already breakout of the loop above at 2x max.
1985 // This code centers the extra kf if the actual natural interval
1986 // is between 1x and 2x.
1987 if (cpi->oxcf.auto_key &&
1988 rc->frames_to_key > (int)cpi->key_frame_frequency) {
1989 FIRSTPASS_STATS tmp_frame = first_frame;
1991 rc->frames_to_key /= 2;
1993 // Reset to the start of the group.
1994 reset_fpf_position(twopass, start_position);
1998 // Rescan to get the correct error data for the forced kf group.
1999 for (i = 0; i < rc->frames_to_key; ++i) {
2000 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2001 input_stats(twopass, &tmp_frame);
2003 rc->next_key_frame_forced = 1;
2004 } else if (twopass->stats_in == twopass->stats_in_end) {
2005 rc->next_key_frame_forced = 1;
2007 rc->next_key_frame_forced = 0;
2010 // Special case for the last key frame of the file.
2011 if (twopass->stats_in >= twopass->stats_in_end) {
2012 // Accumulate kf group error.
2013 kf_group_err += calculate_modified_err(cpi, this_frame);
2016 // Calculate the number of bits that should be assigned to the kf group.
2017 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) {
2018 // Maximum number of bits for a single normal frame (not key frame).
2019 const int max_bits = frame_max_bits(rc, &cpi->oxcf);
2021 // Maximum number of bits allocated to the key frame group.
2022 int64_t max_grp_bits;
2024 // Default allocation based on bits left and relative
2025 // complexity of the section.
2026 twopass->kf_group_bits = (int64_t)(twopass->bits_left *
2027 (kf_group_err / twopass->modified_error_left));
2029 // Clip based on maximum per frame rate defined by the user.
2030 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
2031 if (twopass->kf_group_bits > max_grp_bits)
2032 twopass->kf_group_bits = max_grp_bits;
2034 twopass->kf_group_bits = 0;
2036 // Reset the first pass file position.
2037 reset_fpf_position(twopass, start_position);
2039 // Determine how big to make this keyframe based on how well the subsequent
2040 // frames use inter blocks.
2041 decay_accumulator = 1.0;
2044 // Scan through the kf group collating various stats.
2045 for (i = 0; i < rc->frames_to_key; ++i) {
2046 if (EOF == input_stats(twopass, &next_frame))
2049 // Monitor for static sections.
2050 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
2051 zero_motion_accumulator) {
2052 zero_motion_accumulator = (next_frame.pcnt_inter -
2053 next_frame.pcnt_motion);
2056 // For the first few frames collect data to decide kf boost.
2057 if (i <= (rc->max_gf_interval * 2)) {
2059 if (next_frame.intra_error > twopass->kf_intra_err_min)
2060 r = (IIKFACTOR2 * next_frame.intra_error /
2061 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2063 r = (IIKFACTOR2 * twopass->kf_intra_err_min /
2064 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2069 // How fast is prediction quality decaying.
2070 if (!detect_flash(twopass, 0)) {
2071 const double loop_decay_rate = get_prediction_decay_rate(&cpi->common,
2073 decay_accumulator *= loop_decay_rate;
2074 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR);
2077 boost_score += (decay_accumulator * r);
2082 FIRSTPASS_STATS sectionstats;
2084 zero_stats(§ionstats);
2085 reset_fpf_position(twopass, start_position);
2087 for (i = 0; i < rc->frames_to_key; ++i) {
2088 input_stats(twopass, &next_frame);
2089 accumulate_stats(§ionstats, &next_frame);
2092 avg_stats(§ionstats);
2094 twopass->section_intra_rating = (int) (sectionstats.intra_error /
2095 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2098 // Reset the first pass file position.
2099 reset_fpf_position(twopass, start_position);
2101 // Work out how many bits to allocate for the key frame itself.
2103 int kf_boost = (int)boost_score;
2104 int allocation_chunks;
2106 if (kf_boost < (rc->frames_to_key * 3))
2107 kf_boost = (rc->frames_to_key * 3);
2109 if (kf_boost < MIN_KF_BOOST)
2110 kf_boost = MIN_KF_BOOST;
2112 // Make a note of baseline boost and the zero motion
2113 // accumulator value for use elsewhere.
2114 rc->kf_boost = kf_boost;
2115 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2117 // Key frame size depends on:
2118 // (1) the error score for the whole key frame group,
2119 // (2) the key frames' own error if this is smaller than the
2120 // average for the group (optional),
2121 // (3) insuring that the frame receives at least the allocation it would
2122 // have received based on its own error score vs the error score
2125 // If the sequence appears almost totally static we want to spend almost
2126 // all of the bits on the key frame.
2128 // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is
2129 // taken care of by kf_boost.
2130 if (zero_motion_accumulator >= 0.99) {
2131 allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost;
2133 allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost;
2136 // Prevent overflow.
2137 if (kf_boost > 1028) {
2138 const int divisor = kf_boost >> 10;
2139 kf_boost /= divisor;
2140 allocation_chunks /= divisor;
2143 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits);
2144 // Calculate the number of bits to be spent on the key frame.
2145 twopass->kf_bits = (int)((double)kf_boost *
2146 ((double)twopass->kf_group_bits / allocation_chunks));
2148 // If the key frame is actually easier than the average for the
2149 // kf group (which does sometimes happen, e.g. a blank intro frame)
2150 // then use an alternate calculation based on the kf error score
2151 // which should give a smaller key frame.
2152 if (kf_mod_err < kf_group_err / rc->frames_to_key) {
2153 double alt_kf_grp_bits = ((double)twopass->bits_left *
2154 (kf_mod_err * (double)rc->frames_to_key) /
2155 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left));
2157 const int alt_kf_bits = (int)((double)kf_boost *
2158 (alt_kf_grp_bits / (double)allocation_chunks));
2160 if (twopass->kf_bits > alt_kf_bits)
2161 twopass->kf_bits = alt_kf_bits;
2163 // Else if it is much harder than other frames in the group make sure
2164 // it at least receives an allocation in keeping with its relative
2166 const int alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err /
2167 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)));
2169 if (alt_kf_bits > twopass->kf_bits)
2170 twopass->kf_bits = alt_kf_bits;
2172 twopass->kf_group_bits -= twopass->kf_bits;
2173 // Per frame bit target for this frame.
2174 vp9_rc_set_frame_target(cpi, twopass->kf_bits);
2177 // Note the total error score of the kf group minus the key frame itself.
2178 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2180 // Adjust the count of total modified error left.
2181 // The count of bits left is adjusted elsewhere based on real coded frame
2183 twopass->modified_error_left -= kf_group_err;
2186 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) {
2187 VP9_COMMON *const cm = &cpi->common;
2188 if (!cpi->refresh_alt_ref_frame &&
2189 (cm->current_video_frame == 0 ||
2190 (cpi->frame_flags & FRAMEFLAGS_KEY))) {
2191 cm->frame_type = KEY_FRAME;
2193 cm->frame_type = INTER_FRAME;
2195 // Do not use periodic key frames.
2196 cpi->rc.frames_to_key = INT_MAX;
2199 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2200 VP9_COMMON *const cm = &cpi->common;
2201 RATE_CONTROL *const rc = &cpi->rc;
2202 struct twopass_rc *const twopass = &cpi->twopass;
2204 FIRSTPASS_STATS this_frame;
2205 FIRSTPASS_STATS this_frame_copy;
2207 double this_frame_intra_error;
2208 double this_frame_coded_error;
2210 LAYER_CONTEXT *lc = NULL;
2211 int is_spatial_svc = (cpi->use_svc && cpi->svc.number_temporal_layers == 1);
2213 if (is_spatial_svc) {
2214 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
2215 frames_left = (int)(twopass->total_stats.count -
2216 lc->current_video_frame_in_layer);
2218 frames_left = (int)(twopass->total_stats.count -
2219 cm->current_video_frame);
2222 if (!twopass->stats_in)
2225 if (cpi->refresh_alt_ref_frame) {
2226 cm->frame_type = INTER_FRAME;
2227 vp9_rc_set_frame_target(cpi, twopass->gf_bits);
2231 vp9_clear_system_state();
2233 if (is_spatial_svc && twopass->kf_intra_err_min == 0) {
2234 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
2235 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
2238 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
2239 twopass->active_worst_quality = cpi->oxcf.cq_level;
2240 } else if (cm->current_video_frame == 0 ||
2241 (is_spatial_svc && lc->current_video_frame_in_layer == 0)) {
2242 // Special case code for first frame.
2243 const int section_target_bandwidth = (int)(twopass->bits_left /
2245 const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats,
2246 section_target_bandwidth);
2247 twopass->active_worst_quality = tmp_q;
2248 rc->ni_av_qi = tmp_q;
2249 rc->avg_q = vp9_convert_qindex_to_q(tmp_q);
2251 vp9_zero(this_frame);
2252 if (EOF == input_stats(twopass, &this_frame))
2255 this_frame_intra_error = this_frame.intra_error;
2256 this_frame_coded_error = this_frame.coded_error;
2258 // Keyframe and section processing.
2259 if (rc->frames_to_key == 0 ||
2260 (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2261 // Define next KF group and assign bits to it.
2262 this_frame_copy = this_frame;
2263 find_next_key_frame(cpi, &this_frame_copy);
2264 // Don't place key frame in any enhancement layers in spatial svc
2265 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1 &&
2266 cpi->svc.spatial_layer_id > 0) {
2267 cm->frame_type = INTER_FRAME;
2270 cm->frame_type = INTER_FRAME;
2273 // Is this frame a GF / ARF? (Note: a key frame is always also a GF).
2274 if (rc->frames_till_gf_update_due == 0) {
2275 // Define next gf group and assign bits to it.
2276 this_frame_copy = this_frame;
2278 #if CONFIG_MULTIPLE_ARF
2279 if (cpi->multi_arf_enabled) {
2280 define_fixed_arf_period(cpi);
2283 define_gf_group(cpi, &this_frame_copy);
2284 #if CONFIG_MULTIPLE_ARF
2288 if (twopass->gf_zeromotion_pct > 995) {
2289 // As long as max_thresh for encode breakout is small enough, it is ok
2290 // to enable it for show frame, i.e. set allow_encode_breakout to
2291 // ENCODE_BREAKOUT_LIMITED.
2292 if (!cm->show_frame)
2293 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED;
2295 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED;
2298 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2299 cpi->refresh_golden_frame = 1;
2301 // Otherwise this is an ordinary frame.
2302 // Assign bits from those allocated to the GF group.
2303 this_frame_copy = this_frame;
2304 assign_std_frame_bits(cpi, &this_frame_copy);
2307 // Keep a globally available copy of this and the next frame's iiratio.
2308 twopass->this_iiratio = (int)(this_frame_intra_error /
2309 DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
2311 FIRSTPASS_STATS next_frame;
2312 if (lookup_next_frame_stats(twopass, &next_frame) != EOF) {
2313 twopass->next_iiratio = (int)(next_frame.intra_error /
2314 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2318 if (cpi->common.frame_type == KEY_FRAME)
2319 target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target);
2321 target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target);
2322 vp9_rc_set_frame_target(cpi, target);
2324 // Update the total stats remaining structure.
2325 subtract_stats(&twopass->total_left_stats, &this_frame);
2328 void vp9_twopass_postencode_update(VP9_COMP *cpi) {
2329 #ifdef DISABLE_RC_LONG_TERM_MEM
2330 const uint64_t bits_used = cpi->rc.this_frame_target;
2332 const uint64_t bits_used = cpi->rc.projected_frame_size;
2334 cpi->twopass.bits_left -= bits_used;
2335 cpi->twopass.bits_left = MAX(cpi->twopass.bits_left, 0);
2336 // Update bits left to the kf and gf groups to account for overshoot or
2337 // undershoot on these frames.
2338 if (cpi->common.frame_type == KEY_FRAME) {
2339 // For key frames kf_group_bits already had the target bits subtracted out.
2340 // So now update to the correct value based on the actual bits used.
2341 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - bits_used;
2343 cpi->twopass.kf_group_bits -= bits_used;
2344 cpi->twopass.gf_group_bits -= bits_used;
2345 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0);
2347 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0);