Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_firstpass.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14
15 #include "./vpx_scale_rtcd.h"
16
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_scale/vpx_scale.h"
19 #include "vpx_scale/yv12config.h"
20
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"
25 #include "vp9/encoder/vp9_aq_variance.h"
26 #include "vp9/encoder/vp9_block.h"
27 #include "vp9/encoder/vp9_encodeframe.h"
28 #include "vp9/encoder/vp9_encodemb.h"
29 #include "vp9/encoder/vp9_encodemv.h"
30 #include "vp9/encoder/vp9_encoder.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_quantize.h"
35 #include "vp9/encoder/vp9_rd.h"
36 #include "vp9/encoder/vp9_variance.h"
37
38 #define OUTPUT_FPF          0
39 #define ARF_STATS_OUTPUT    0
40
41 #define BOOST_BREAKOUT      12.5
42 #define BOOST_FACTOR        12.5
43 #define ERR_DIVISOR         128.0
44 #define FACTOR_PT_LOW       0.70
45 #define FACTOR_PT_HIGH      0.90
46 #define FIRST_PASS_Q        10.0
47 #define GF_MAX_BOOST        96.0
48 #define INTRA_MODE_PENALTY  1024
49 #define KF_MAX_BOOST        128.0
50 #define MIN_ARF_GF_BOOST    240
51 #define MIN_DECAY_FACTOR    0.01
52 #define MIN_GF_INTERVAL     4
53 #define MIN_KF_BOOST        300
54 #define NEW_MV_MODE_PENALTY 32
55 #define SVC_FACTOR_PT_LOW   0.45
56
57 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
58
59 #if ARF_STATS_OUTPUT
60 unsigned int arf_count = 0;
61 #endif
62
63 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
64   YV12_BUFFER_CONFIG temp = *a;
65   *a = *b;
66   *b = temp;
67 }
68
69 // Resets the first pass file to the given position using a relative seek from
70 // the current position.
71 static void reset_fpf_position(TWO_PASS *p,
72                                const FIRSTPASS_STATS *position) {
73   p->stats_in = position;
74 }
75
76 // Read frame stats at an offset from the current position.
77 static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) {
78   if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) ||
79       (offset < 0 && p->stats_in + offset < p->stats_in_start)) {
80     return NULL;
81   }
82
83   return &p->stats_in[offset];
84 }
85
86 static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) {
87   if (p->stats_in >= p->stats_in_end)
88     return EOF;
89
90   *fps = *p->stats_in;
91   ++p->stats_in;
92   return 1;
93 }
94
95 static void output_stats(FIRSTPASS_STATS *stats,
96                          struct vpx_codec_pkt_list *pktlist) {
97   struct vpx_codec_cx_pkt pkt;
98   pkt.kind = VPX_CODEC_STATS_PKT;
99   pkt.data.twopass_stats.buf = stats;
100   pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
101   vpx_codec_pkt_list_add(pktlist, &pkt);
102
103 // TEMP debug code
104 #if OUTPUT_FPF
105   {
106     FILE *fpfile;
107     fpfile = fopen("firstpass.stt", "a");
108
109     fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
110             "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
111             "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
112             stats->frame,
113             stats->intra_error,
114             stats->coded_error,
115             stats->sr_coded_error,
116             stats->pcnt_inter,
117             stats->pcnt_motion,
118             stats->pcnt_second_ref,
119             stats->pcnt_neutral,
120             stats->MVr,
121             stats->mvr_abs,
122             stats->MVc,
123             stats->mvc_abs,
124             stats->MVrv,
125             stats->MVcv,
126             stats->mv_in_out_count,
127             stats->new_mv_count,
128             stats->count,
129             stats->duration);
130     fclose(fpfile);
131   }
132 #endif
133 }
134
135 #if CONFIG_FP_MB_STATS
136 static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm,
137                          struct vpx_codec_pkt_list *pktlist) {
138   struct vpx_codec_cx_pkt pkt;
139   pkt.kind = VPX_CODEC_FPMB_STATS_PKT;
140   pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats;
141   pkt.data.firstpass_mb_stats.sz = cm->MBs * sizeof(uint8_t);
142   vpx_codec_pkt_list_add(pktlist, &pkt);
143 }
144 #endif
145
146 static void zero_stats(FIRSTPASS_STATS *section) {
147   section->frame      = 0.0;
148   section->intra_error = 0.0;
149   section->coded_error = 0.0;
150   section->sr_coded_error = 0.0;
151   section->pcnt_inter  = 0.0;
152   section->pcnt_motion  = 0.0;
153   section->pcnt_second_ref = 0.0;
154   section->pcnt_neutral = 0.0;
155   section->MVr        = 0.0;
156   section->mvr_abs     = 0.0;
157   section->MVc        = 0.0;
158   section->mvc_abs     = 0.0;
159   section->MVrv       = 0.0;
160   section->MVcv       = 0.0;
161   section->mv_in_out_count  = 0.0;
162   section->new_mv_count = 0.0;
163   section->count      = 0.0;
164   section->duration   = 1.0;
165   section->spatial_layer_id = 0;
166 }
167
168 static void accumulate_stats(FIRSTPASS_STATS *section,
169                              const FIRSTPASS_STATS *frame) {
170   section->frame += frame->frame;
171   section->spatial_layer_id = frame->spatial_layer_id;
172   section->intra_error += frame->intra_error;
173   section->coded_error += frame->coded_error;
174   section->sr_coded_error += frame->sr_coded_error;
175   section->pcnt_inter  += frame->pcnt_inter;
176   section->pcnt_motion += frame->pcnt_motion;
177   section->pcnt_second_ref += frame->pcnt_second_ref;
178   section->pcnt_neutral += frame->pcnt_neutral;
179   section->MVr        += frame->MVr;
180   section->mvr_abs     += frame->mvr_abs;
181   section->MVc        += frame->MVc;
182   section->mvc_abs     += frame->mvc_abs;
183   section->MVrv       += frame->MVrv;
184   section->MVcv       += frame->MVcv;
185   section->mv_in_out_count  += frame->mv_in_out_count;
186   section->new_mv_count += frame->new_mv_count;
187   section->count      += frame->count;
188   section->duration   += frame->duration;
189 }
190
191 static void subtract_stats(FIRSTPASS_STATS *section,
192                            const FIRSTPASS_STATS *frame) {
193   section->frame -= frame->frame;
194   section->intra_error -= frame->intra_error;
195   section->coded_error -= frame->coded_error;
196   section->sr_coded_error -= frame->sr_coded_error;
197   section->pcnt_inter  -= frame->pcnt_inter;
198   section->pcnt_motion -= frame->pcnt_motion;
199   section->pcnt_second_ref -= frame->pcnt_second_ref;
200   section->pcnt_neutral -= frame->pcnt_neutral;
201   section->MVr        -= frame->MVr;
202   section->mvr_abs     -= frame->mvr_abs;
203   section->MVc        -= frame->MVc;
204   section->mvc_abs     -= frame->mvc_abs;
205   section->MVrv       -= frame->MVrv;
206   section->MVcv       -= frame->MVcv;
207   section->mv_in_out_count  -= frame->mv_in_out_count;
208   section->new_mv_count -= frame->new_mv_count;
209   section->count      -= frame->count;
210   section->duration   -= frame->duration;
211 }
212
213
214 // Calculate a modified Error used in distributing bits between easier and
215 // harder frames.
216 static double calculate_modified_err(const TWO_PASS *twopass,
217                                      const VP9EncoderConfig *oxcf,
218                                      const FIRSTPASS_STATS *this_frame) {
219   const FIRSTPASS_STATS *const stats = &twopass->total_stats;
220   const double av_err = stats->coded_error / stats->count;
221   const double modified_error = av_err *
222       pow(this_frame->coded_error / DOUBLE_DIVIDE_CHECK(av_err),
223           oxcf->two_pass_vbrbias / 100.0);
224   return fclamp(modified_error,
225                 twopass->modified_error_min, twopass->modified_error_max);
226 }
227
228 // This function returns the maximum target rate per frame.
229 static int frame_max_bits(const RATE_CONTROL *rc,
230                           const VP9EncoderConfig *oxcf) {
231   int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
232                           (int64_t)oxcf->two_pass_vbrmax_section) / 100;
233   if (max_bits < 0)
234     max_bits = 0;
235   else if (max_bits > rc->max_frame_bandwidth)
236     max_bits = rc->max_frame_bandwidth;
237
238   return (int)max_bits;
239 }
240
241 void vp9_init_first_pass(VP9_COMP *cpi) {
242   zero_stats(&cpi->twopass.total_stats);
243 }
244
245 void vp9_end_first_pass(VP9_COMP *cpi) {
246   if (is_two_pass_svc(cpi)) {
247     int i;
248     for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
249       output_stats(&cpi->svc.layer_context[i].twopass.total_stats,
250                    cpi->output_pkt_list);
251     }
252   } else {
253     output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list);
254   }
255 }
256
257 static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
258   switch (bsize) {
259     case BLOCK_8X8:
260       return vp9_mse8x8;
261     case BLOCK_16X8:
262       return vp9_mse16x8;
263     case BLOCK_8X16:
264       return vp9_mse8x16;
265     default:
266       return vp9_mse16x16;
267   }
268 }
269
270 static unsigned int get_prediction_error(BLOCK_SIZE bsize,
271                                          const struct buf_2d *src,
272                                          const struct buf_2d *ref) {
273   unsigned int sse;
274   const vp9_variance_fn_t fn = get_block_variance_fn(bsize);
275   fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
276   return sse;
277 }
278
279 #if CONFIG_VP9_HIGHBITDEPTH
280 static vp9_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize,
281                                                       int bd) {
282   switch (bd) {
283     default:
284       switch (bsize) {
285         case BLOCK_8X8:
286           return vp9_highbd_mse8x8;
287         case BLOCK_16X8:
288           return vp9_highbd_mse16x8;
289         case BLOCK_8X16:
290           return vp9_highbd_mse8x16;
291         default:
292           return vp9_highbd_mse16x16;
293       }
294       break;
295     case 10:
296       switch (bsize) {
297         case BLOCK_8X8:
298           return vp9_highbd_10_mse8x8;
299         case BLOCK_16X8:
300           return vp9_highbd_10_mse16x8;
301         case BLOCK_8X16:
302           return vp9_highbd_10_mse8x16;
303         default:
304           return vp9_highbd_10_mse16x16;
305       }
306       break;
307     case 12:
308       switch (bsize) {
309         case BLOCK_8X8:
310           return vp9_highbd_12_mse8x8;
311         case BLOCK_16X8:
312           return vp9_highbd_12_mse16x8;
313         case BLOCK_8X16:
314           return vp9_highbd_12_mse8x16;
315         default:
316           return vp9_highbd_12_mse16x16;
317       }
318       break;
319   }
320 }
321
322 static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize,
323                                                 const struct buf_2d *src,
324                                                 const struct buf_2d *ref,
325                                                 int bd) {
326   unsigned int sse;
327   const vp9_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
328   fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
329   return sse;
330 }
331 #endif  // CONFIG_VP9_HIGHBITDEPTH
332
333 // Refine the motion search range according to the frame dimension
334 // for first pass test.
335 static int get_search_range(const VP9_COMMON *cm) {
336   int sr = 0;
337   const int dim = MIN(cm->width, cm->height);
338
339   while ((dim << sr) < MAX_FULL_PEL_VAL)
340     ++sr;
341   return sr;
342 }
343
344 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
345                                      const MV *ref_mv, MV *best_mv,
346                                      int *best_motion_err) {
347   MACROBLOCKD *const xd = &x->e_mbd;
348   MV tmp_mv = {0, 0};
349   MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3};
350   int num00, tmp_err, n;
351   const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
352   vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
353   const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
354
355   int step_param = 3;
356   int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
357   const int sr = get_search_range(&cpi->common);
358   step_param += sr;
359   further_steps -= sr;
360
361   // Override the default variance function to use MSE.
362   v_fn_ptr.vf = get_block_variance_fn(bsize);
363 #if CONFIG_VP9_HIGHBITDEPTH
364   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
365     v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd);
366   }
367 #endif  // CONFIG_VP9_HIGHBITDEPTH
368
369   // Center the initial step/diamond search on best mv.
370   tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
371                                     step_param,
372                                     x->sadperbit16, &num00, &v_fn_ptr, ref_mv);
373   if (tmp_err < INT_MAX)
374     tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
375   if (tmp_err < INT_MAX - new_mv_mode_penalty)
376     tmp_err += new_mv_mode_penalty;
377
378   if (tmp_err < *best_motion_err) {
379     *best_motion_err = tmp_err;
380     *best_mv = tmp_mv;
381   }
382
383   // Carry out further step/diamond searches as necessary.
384   n = num00;
385   num00 = 0;
386
387   while (n < further_steps) {
388     ++n;
389
390     if (num00) {
391       --num00;
392     } else {
393       tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
394                                         step_param + n, x->sadperbit16,
395                                         &num00, &v_fn_ptr, ref_mv);
396       if (tmp_err < INT_MAX)
397         tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
398       if (tmp_err < INT_MAX - new_mv_mode_penalty)
399         tmp_err += new_mv_mode_penalty;
400
401       if (tmp_err < *best_motion_err) {
402         *best_motion_err = tmp_err;
403         *best_mv = tmp_mv;
404       }
405     }
406   }
407 }
408
409 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
410   if (2 * mb_col + 1 < cm->mi_cols) {
411     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16
412                                         : BLOCK_16X8;
413   } else {
414     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16
415                                         : BLOCK_8X8;
416   }
417 }
418
419 static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
420   int i;
421
422   for (i = 0; i < QINDEX_RANGE; ++i)
423     if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q)
424       break;
425
426   if (i == QINDEX_RANGE)
427     i--;
428
429   return i;
430 }
431
432 static void set_first_pass_params(VP9_COMP *cpi) {
433   VP9_COMMON *const cm = &cpi->common;
434   if (!cpi->refresh_alt_ref_frame &&
435       (cm->current_video_frame == 0 ||
436        (cpi->frame_flags & FRAMEFLAGS_KEY))) {
437     cm->frame_type = KEY_FRAME;
438   } else {
439     cm->frame_type = INTER_FRAME;
440   }
441   // Do not use periodic key frames.
442   cpi->rc.frames_to_key = INT_MAX;
443 }
444
445 void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
446   int mb_row, mb_col;
447   MACROBLOCK *const x = &cpi->mb;
448   VP9_COMMON *const cm = &cpi->common;
449   MACROBLOCKD *const xd = &x->e_mbd;
450   TileInfo tile;
451   struct macroblock_plane *const p = x->plane;
452   struct macroblockd_plane *const pd = xd->plane;
453   const PICK_MODE_CONTEXT *ctx = &cpi->pc_root->none;
454   int i;
455
456   int recon_yoffset, recon_uvoffset;
457   YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
458   YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
459   YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
460   int recon_y_stride = lst_yv12->y_stride;
461   int recon_uv_stride = lst_yv12->uv_stride;
462   int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height);
463   int64_t intra_error = 0;
464   int64_t coded_error = 0;
465   int64_t sr_coded_error = 0;
466
467   int sum_mvr = 0, sum_mvc = 0;
468   int sum_mvr_abs = 0, sum_mvc_abs = 0;
469   int64_t sum_mvrs = 0, sum_mvcs = 0;
470   int mvcount = 0;
471   int intercount = 0;
472   int second_ref_count = 0;
473   const int intrapenalty = INTRA_MODE_PENALTY;
474   int neutral_count = 0;
475   int new_mv_count = 0;
476   int sum_in_vectors = 0;
477   MV lastmv = {0, 0};
478   TWO_PASS *twopass = &cpi->twopass;
479   const MV zero_mv = {0, 0};
480   const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
481   LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
482         &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL;
483
484 #if CONFIG_FP_MB_STATS
485   if (cpi->use_fp_mb_stats) {
486     vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->MBs);
487   }
488 #endif
489
490   vp9_clear_system_state();
491
492   set_first_pass_params(cpi);
493   vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
494
495   if (lc != NULL) {
496     twopass = &lc->twopass;
497
498     cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
499     cpi->ref_frame_flags = VP9_LAST_FLAG;
500
501     if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id <
502         REF_FRAMES) {
503       cpi->gld_fb_idx =
504           cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id;
505       cpi->ref_frame_flags |= VP9_GOLD_FLAG;
506       cpi->refresh_golden_frame = (lc->current_video_frame_in_layer == 0);
507     } else {
508       cpi->refresh_golden_frame = 0;
509     }
510
511     if (lc->current_video_frame_in_layer == 0)
512       cpi->ref_frame_flags = 0;
513
514     vp9_scale_references(cpi);
515
516     // Use either last frame or alt frame for motion search.
517     if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
518       first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
519       if (first_ref_buf == NULL)
520         first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME);
521     }
522
523     if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
524       const int ref_idx =
525           cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)];
526       const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1];
527
528       gld_yv12 = (scaled_idx != ref_idx) ? &cm->frame_bufs[scaled_idx].buf :
529                  get_ref_frame_buffer(cpi, GOLDEN_FRAME);
530     } else {
531       gld_yv12 = NULL;
532     }
533
534     recon_y_stride = new_yv12->y_stride;
535     recon_uv_stride = new_yv12->uv_stride;
536     uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
537
538     set_ref_ptrs(cm, xd,
539                  (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME: NONE,
540                  (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE);
541
542     cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
543                                         &cpi->scaled_source);
544   }
545
546   vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
547
548   vp9_setup_src_planes(x, cpi->Source, 0, 0);
549   vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
550   vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
551
552   xd->mi = cm->mi;
553   xd->mi[0].src_mi = &xd->mi[0];
554
555   vp9_frame_init_quantizer(cpi);
556
557   for (i = 0; i < MAX_MB_PLANE; ++i) {
558     p[i].coeff = ctx->coeff_pbuf[i][1];
559     p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
560     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
561     p[i].eobs = ctx->eobs_pbuf[i][1];
562   }
563   x->skip_recode = 0;
564
565   vp9_init_mv_probs(cm);
566   vp9_initialize_rd_consts(cpi);
567
568   // Tiling is ignored in the first pass.
569   vp9_tile_init(&tile, cm, 0, 0);
570
571   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
572     MV best_ref_mv = {0, 0};
573
574     // Reset above block coeffs.
575     xd->up_available = (mb_row != 0);
576     recon_yoffset = (mb_row * recon_y_stride * 16);
577     recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height);
578
579     // Set up limit values for motion vectors to prevent them extending
580     // outside the UMV borders.
581     x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
582     x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
583                     + BORDER_MV_PIXELS_B16;
584
585     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
586       int this_error;
587       const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
588       double error_weight = 1.0;
589       const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
590 #if CONFIG_FP_MB_STATS
591       const int mb_index = mb_row * cm->mb_cols + mb_col;
592 #endif
593
594       vp9_clear_system_state();
595
596       xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
597       xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
598       xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
599       xd->left_available = (mb_col != 0);
600       xd->mi[0].src_mi->mbmi.sb_type = bsize;
601       xd->mi[0].src_mi->mbmi.ref_frame[0] = INTRA_FRAME;
602       set_mi_row_col(xd, &tile,
603                      mb_row << 1, num_8x8_blocks_high_lookup[bsize],
604                      mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
605                      cm->mi_rows, cm->mi_cols);
606
607       if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
608         const int energy = vp9_block_energy(cpi, x, bsize);
609         error_weight = vp9_vaq_inv_q_ratio(energy);
610       }
611
612       // Do intra 16x16 prediction.
613       x->skip_encode = 0;
614       xd->mi[0].src_mi->mbmi.mode = DC_PRED;
615       xd->mi[0].src_mi->mbmi.tx_size = use_dc_pred ?
616          (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
617       vp9_encode_intra_block_plane(x, bsize, 0);
618       this_error = vp9_get_mb_ss(x->plane[0].src_diff);
619 #if CONFIG_VP9_HIGHBITDEPTH
620       if (cm->use_highbitdepth) {
621         switch (cm->bit_depth) {
622           case VPX_BITS_8:
623             break;
624           case VPX_BITS_10:
625             this_error >>= 4;
626             break;
627           case VPX_BITS_12:
628             this_error >>= 8;
629             break;
630           default:
631             assert(0 && "cm->bit_depth should be VPX_BITS_8, "
632                         "VPX_BITS_10 or VPX_BITS_12");
633             return;
634         }
635       }
636 #endif  // CONFIG_VP9_HIGHBITDEPTH
637
638       if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
639         vp9_clear_system_state();
640         this_error = (int)(this_error * error_weight);
641       }
642
643       // Intrapenalty below deals with situations where the intra and inter
644       // error scores are very low (e.g. a plain black frame).
645       // We do not have special cases in first pass for 0,0 and nearest etc so
646       // all inter modes carry an overhead cost estimate for the mv.
647       // When the error score is very low this causes us to pick all or lots of
648       // INTRA modes and throw lots of key frames.
649       // This penalty adds a cost matching that of a 0,0 mv to the intra case.
650       this_error += intrapenalty;
651
652       // Accumulate the intra error.
653       intra_error += (int64_t)this_error;
654
655 #if CONFIG_FP_MB_STATS
656       if (cpi->use_fp_mb_stats) {
657         // initialization
658         cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
659       }
660 #endif
661
662       // Set up limit values for motion vectors to prevent them extending
663       // outside the UMV borders.
664       x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
665       x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
666
667       // Other than for the first frame do a motion search.
668       if ((lc == NULL && cm->current_video_frame > 0) ||
669           (lc != NULL && lc->current_video_frame_in_layer > 0)) {
670         int tmp_err, motion_error, raw_motion_error;
671         // Assume 0,0 motion with no mv overhead.
672         MV mv = {0, 0} , tmp_mv = {0, 0};
673         struct buf_2d unscaled_last_source_buf_2d;
674
675         xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
676 #if CONFIG_VP9_HIGHBITDEPTH
677         if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
678           motion_error = highbd_get_prediction_error(
679               bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
680         } else {
681           motion_error = get_prediction_error(
682               bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
683         }
684 #else
685         motion_error = get_prediction_error(
686             bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
687 #endif  // CONFIG_VP9_HIGHBITDEPTH
688
689         // Compute the motion error of the 0,0 motion using the last source
690         // frame as the reference. Skip the further motion search on
691         // reconstructed frame if this error is small.
692         unscaled_last_source_buf_2d.buf =
693             cpi->unscaled_last_source->y_buffer + recon_yoffset;
694         unscaled_last_source_buf_2d.stride =
695             cpi->unscaled_last_source->y_stride;
696 #if CONFIG_VP9_HIGHBITDEPTH
697         if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
698           raw_motion_error = highbd_get_prediction_error(
699               bsize, &x->plane[0].src, &unscaled_last_source_buf_2d, xd->bd);
700         } else {
701           raw_motion_error = get_prediction_error(
702               bsize, &x->plane[0].src, &unscaled_last_source_buf_2d);
703         }
704 #else
705         raw_motion_error = get_prediction_error(
706             bsize, &x->plane[0].src, &unscaled_last_source_buf_2d);
707 #endif  // CONFIG_VP9_HIGHBITDEPTH
708
709         // TODO(pengchong): Replace the hard-coded threshold
710         if (raw_motion_error > 25 || lc != NULL) {
711           // Test last reference frame using the previous best mv as the
712           // starting point (best reference) for the search.
713           first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error);
714           if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
715             vp9_clear_system_state();
716             motion_error = (int)(motion_error * error_weight);
717           }
718
719           // If the current best reference mv is not centered on 0,0 then do a
720           // 0,0 based search as well.
721           if (!is_zero_mv(&best_ref_mv)) {
722             tmp_err = INT_MAX;
723             first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
724             if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
725               vp9_clear_system_state();
726               tmp_err = (int)(tmp_err * error_weight);
727             }
728
729             if (tmp_err < motion_error) {
730               motion_error = tmp_err;
731               mv = tmp_mv;
732             }
733           }
734
735           // Search in an older reference frame.
736           if (((lc == NULL && cm->current_video_frame > 1) ||
737                (lc != NULL && lc->current_video_frame_in_layer > 1))
738               && gld_yv12 != NULL) {
739             // Assume 0,0 motion with no mv overhead.
740             int gf_motion_error;
741
742             xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
743 #if CONFIG_VP9_HIGHBITDEPTH
744             if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
745               gf_motion_error = highbd_get_prediction_error(
746                   bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
747             } else {
748               gf_motion_error = get_prediction_error(
749                   bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
750             }
751 #else
752             gf_motion_error = get_prediction_error(
753                 bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
754 #endif  // CONFIG_VP9_HIGHBITDEPTH
755
756             first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv,
757                                      &gf_motion_error);
758             if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
759               vp9_clear_system_state();
760               gf_motion_error = (int)(gf_motion_error * error_weight);
761             }
762
763             if (gf_motion_error < motion_error && gf_motion_error < this_error)
764               ++second_ref_count;
765
766             // Reset to last frame as reference buffer.
767             xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
768             xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
769             xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
770
771             // In accumulating a score for the older reference frame take the
772             // best of the motion predicted score and the intra coded error
773             // (just as will be done for) accumulation of "coded_error" for
774             // the last frame.
775             if (gf_motion_error < this_error)
776               sr_coded_error += gf_motion_error;
777             else
778               sr_coded_error += this_error;
779           } else {
780             sr_coded_error += motion_error;
781           }
782         } else {
783           sr_coded_error += motion_error;
784         }
785
786         // Start by assuming that intra mode is best.
787         best_ref_mv.row = 0;
788         best_ref_mv.col = 0;
789
790 #if CONFIG_FP_MB_STATS
791         if (cpi->use_fp_mb_stats) {
792           // intra predication statistics
793           cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
794           cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK;
795           cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
796           if (this_error > FPMB_ERROR_LARGE_TH) {
797             cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
798           } else if (this_error < FPMB_ERROR_SMALL_TH) {
799             cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
800           }
801         }
802 #endif
803
804         if (motion_error <= this_error) {
805           // Keep a count of cases where the inter and intra were very close
806           // and very low. This helps with scene cut detection for example in
807           // cropped clips with black bars at the sides or top and bottom.
808           if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
809               this_error < 2 * intrapenalty)
810             ++neutral_count;
811
812           mv.row *= 8;
813           mv.col *= 8;
814           this_error = motion_error;
815           xd->mi[0].src_mi->mbmi.mode = NEWMV;
816           xd->mi[0].src_mi->mbmi.mv[0].as_mv = mv;
817           xd->mi[0].src_mi->mbmi.tx_size = TX_4X4;
818           xd->mi[0].src_mi->mbmi.ref_frame[0] = LAST_FRAME;
819           xd->mi[0].src_mi->mbmi.ref_frame[1] = NONE;
820           vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
821           vp9_encode_sby_pass1(x, bsize);
822           sum_mvr += mv.row;
823           sum_mvr_abs += abs(mv.row);
824           sum_mvc += mv.col;
825           sum_mvc_abs += abs(mv.col);
826           sum_mvrs += mv.row * mv.row;
827           sum_mvcs += mv.col * mv.col;
828           ++intercount;
829
830           best_ref_mv = mv;
831
832 #if CONFIG_FP_MB_STATS
833           if (cpi->use_fp_mb_stats) {
834             // inter predication statistics
835             cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
836             cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK;
837             cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
838             if (this_error > FPMB_ERROR_LARGE_TH) {
839               cpi->twopass.frame_mb_stats_buf[mb_index] |=
840                   FPMB_ERROR_LARGE_MASK;
841             } else if (this_error < FPMB_ERROR_SMALL_TH) {
842               cpi->twopass.frame_mb_stats_buf[mb_index] |=
843                   FPMB_ERROR_SMALL_MASK;
844             }
845           }
846 #endif
847
848           if (!is_zero_mv(&mv)) {
849             ++mvcount;
850
851 #if CONFIG_FP_MB_STATS
852             if (cpi->use_fp_mb_stats) {
853               cpi->twopass.frame_mb_stats_buf[mb_index] &=
854                   ~FPMB_MOTION_ZERO_MASK;
855               // check estimated motion direction
856               if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) {
857                 // right direction
858                 cpi->twopass.frame_mb_stats_buf[mb_index] |=
859                     FPMB_MOTION_RIGHT_MASK;
860               } else if (mv.as_mv.row < 0 &&
861                          abs(mv.as_mv.row) >= abs(mv.as_mv.col)) {
862                 // up direction
863                 cpi->twopass.frame_mb_stats_buf[mb_index] |=
864                     FPMB_MOTION_UP_MASK;
865               } else if (mv.as_mv.col < 0 &&
866                          abs(mv.as_mv.col) >= abs(mv.as_mv.row)) {
867                 // left direction
868                 cpi->twopass.frame_mb_stats_buf[mb_index] |=
869                     FPMB_MOTION_LEFT_MASK;
870               } else {
871                 // down direction
872                 cpi->twopass.frame_mb_stats_buf[mb_index] |=
873                     FPMB_MOTION_DOWN_MASK;
874               }
875             }
876 #endif
877
878             // Non-zero vector, was it different from the last non zero vector?
879             if (!is_equal_mv(&mv, &lastmv))
880               ++new_mv_count;
881             lastmv = mv;
882
883             // Does the row vector point inwards or outwards?
884             if (mb_row < cm->mb_rows / 2) {
885               if (mv.row > 0)
886                 --sum_in_vectors;
887               else if (mv.row < 0)
888                 ++sum_in_vectors;
889             } else if (mb_row > cm->mb_rows / 2) {
890               if (mv.row > 0)
891                 ++sum_in_vectors;
892               else if (mv.row < 0)
893                 --sum_in_vectors;
894             }
895
896             // Does the col vector point inwards or outwards?
897             if (mb_col < cm->mb_cols / 2) {
898               if (mv.col > 0)
899                 --sum_in_vectors;
900               else if (mv.col < 0)
901                 ++sum_in_vectors;
902             } else if (mb_col > cm->mb_cols / 2) {
903               if (mv.col > 0)
904                 ++sum_in_vectors;
905               else if (mv.col < 0)
906                 --sum_in_vectors;
907             }
908           }
909         }
910       } else {
911         sr_coded_error += (int64_t)this_error;
912       }
913       coded_error += (int64_t)this_error;
914
915       // Adjust to the next column of MBs.
916       x->plane[0].src.buf += 16;
917       x->plane[1].src.buf += uv_mb_height;
918       x->plane[2].src.buf += uv_mb_height;
919
920       recon_yoffset += 16;
921       recon_uvoffset += uv_mb_height;
922     }
923
924     // Adjust to the next row of MBs.
925     x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
926     x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
927                            uv_mb_height * cm->mb_cols;
928     x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
929                            uv_mb_height * cm->mb_cols;
930
931     vp9_clear_system_state();
932   }
933
934   vp9_clear_system_state();
935   {
936     FIRSTPASS_STATS fps;
937     // The minimum error here insures some bit alocation to frames even
938     // in static regions. The allocation per MB declines for larger formats
939     // where the typical "real" energy per MB also falls.
940     // Initial estimate here uses sqrt(mbs) to define the min_err, where the
941     // number of mbs is propotional to image area.
942     const double min_err = 200 * sqrt(cm->MBs);
943
944     fps.frame = cm->current_video_frame;
945     fps.spatial_layer_id = cpi->svc.spatial_layer_id;
946     fps.coded_error = (double)(coded_error >> 8) + min_err;
947     fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
948     fps.intra_error = (double)(intra_error >> 8) + min_err;
949     fps.count = 1.0;
950     fps.pcnt_inter = (double)intercount / cm->MBs;
951     fps.pcnt_second_ref = (double)second_ref_count / cm->MBs;
952     fps.pcnt_neutral = (double)neutral_count / cm->MBs;
953
954     if (mvcount > 0) {
955       fps.MVr = (double)sum_mvr / mvcount;
956       fps.mvr_abs = (double)sum_mvr_abs / mvcount;
957       fps.MVc = (double)sum_mvc / mvcount;
958       fps.mvc_abs = (double)sum_mvc_abs / mvcount;
959       fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount;
960       fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount;
961       fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
962       fps.new_mv_count = new_mv_count;
963       fps.pcnt_motion = (double)mvcount / cm->MBs;
964     } else {
965       fps.MVr = 0.0;
966       fps.mvr_abs = 0.0;
967       fps.MVc = 0.0;
968       fps.mvc_abs = 0.0;
969       fps.MVrv = 0.0;
970       fps.MVcv = 0.0;
971       fps.mv_in_out_count = 0.0;
972       fps.new_mv_count = 0.0;
973       fps.pcnt_motion = 0.0;
974     }
975
976     // TODO(paulwilkins):  Handle the case when duration is set to 0, or
977     // something less than the full time between subsequent values of
978     // cpi->source_time_stamp.
979     fps.duration = (double)(source->ts_end - source->ts_start);
980
981     // Don't want to do output stats with a stack variable!
982     twopass->this_frame_stats = fps;
983     output_stats(&twopass->this_frame_stats, cpi->output_pkt_list);
984     accumulate_stats(&twopass->total_stats, &fps);
985
986 #if CONFIG_FP_MB_STATS
987     if (cpi->use_fp_mb_stats) {
988       output_fpmb_stats(twopass->frame_mb_stats_buf, cm, cpi->output_pkt_list);
989     }
990 #endif
991   }
992
993   // Copy the previous Last Frame back into gf and and arf buffers if
994   // the prediction is good enough... but also don't allow it to lag too far.
995   if ((twopass->sr_update_lag > 3) ||
996       ((cm->current_video_frame > 0) &&
997        (twopass->this_frame_stats.pcnt_inter > 0.20) &&
998        ((twopass->this_frame_stats.intra_error /
999          DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
1000     if (gld_yv12 != NULL) {
1001       vp8_yv12_copy_frame(lst_yv12, gld_yv12);
1002     }
1003     twopass->sr_update_lag = 1;
1004   } else {
1005     ++twopass->sr_update_lag;
1006   }
1007
1008   vp9_extend_frame_borders(new_yv12);
1009
1010   if (lc != NULL) {
1011     vp9_update_reference_frames(cpi);
1012   } else {
1013     // Swap frame pointers so last frame refers to the frame we just compressed.
1014     swap_yv12(lst_yv12, new_yv12);
1015   }
1016
1017   // Special case for the first frame. Copy into the GF buffer as a second
1018   // reference.
1019   if (cm->current_video_frame == 0 && gld_yv12 != NULL && lc == NULL) {
1020     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
1021   }
1022
1023   // Use this to see what the first pass reconstruction looks like.
1024   if (0) {
1025     char filename[512];
1026     FILE *recon_file;
1027     snprintf(filename, sizeof(filename), "enc%04d.yuv",
1028              (int)cm->current_video_frame);
1029
1030     if (cm->current_video_frame == 0)
1031       recon_file = fopen(filename, "wb");
1032     else
1033       recon_file = fopen(filename, "ab");
1034
1035     (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
1036     fclose(recon_file);
1037   }
1038
1039   ++cm->current_video_frame;
1040   if (cpi->use_svc)
1041     vp9_inc_frame_in_layer(cpi);
1042 }
1043
1044 static double calc_correction_factor(double err_per_mb,
1045                                      double err_divisor,
1046                                      double pt_low,
1047                                      double pt_high,
1048                                      int q,
1049                                      vpx_bit_depth_t bit_depth) {
1050   const double error_term = err_per_mb / err_divisor;
1051
1052   // Adjustment based on actual quantizer to power term.
1053   const double power_term =
1054       MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high);
1055
1056   // Calculate correction factor.
1057   if (power_term < 1.0)
1058     assert(error_term >= 0.0);
1059
1060   return fclamp(pow(error_term, power_term), 0.05, 5.0);
1061 }
1062
1063 // Larger image formats are expected to be a little harder to code relatively
1064 // given the same prediction error score. This in part at least relates to the
1065 // increased size and hence coding cost of motion vectors.
1066 #define EDIV_SIZE_FACTOR 800
1067
1068 static int get_twopass_worst_quality(const VP9_COMP *cpi,
1069                                      const FIRSTPASS_STATS *stats,
1070                                      int section_target_bandwidth) {
1071   const RATE_CONTROL *const rc = &cpi->rc;
1072   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1073
1074   if (section_target_bandwidth <= 0) {
1075     return rc->worst_quality;  // Highest value allowed
1076   } else {
1077     const int num_mbs = cpi->common.MBs;
1078     const double section_err = stats->coded_error / stats->count;
1079     const double err_per_mb = section_err / num_mbs;
1080     const double speed_term = 1.0 + 0.04 * oxcf->speed;
1081     const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR;
1082     const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth <<
1083                                          BPER_MB_NORMBITS) / num_mbs;
1084
1085     int q;
1086     int is_svc_upper_layer = 0;
1087     if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
1088       is_svc_upper_layer = 1;
1089
1090     // Try and pick a max Q that will be high enough to encode the
1091     // content at the given rate.
1092     for (q = rc->best_quality; q < rc->worst_quality; ++q) {
1093       const double factor =
1094           calc_correction_factor(err_per_mb, ERR_DIVISOR - ediv_size_correction,
1095                                  is_svc_upper_layer ? SVC_FACTOR_PT_LOW :
1096                                  FACTOR_PT_LOW, FACTOR_PT_HIGH, q,
1097                                  cpi->common.bit_depth);
1098       const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q,
1099                                                  factor * speed_term,
1100                                                  cpi->common.bit_depth);
1101       if (bits_per_mb <= target_norm_bits_per_mb)
1102         break;
1103     }
1104
1105     // Restriction on active max q for constrained quality mode.
1106     if (cpi->oxcf.rc_mode == VPX_CQ)
1107       q = MAX(q, oxcf->cq_level);
1108     return q;
1109   }
1110 }
1111
1112 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate);
1113
1114 void vp9_init_second_pass(VP9_COMP *cpi) {
1115   SVC *const svc = &cpi->svc;
1116   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1117   const int is_two_pass_svc = (svc->number_spatial_layers > 1) ||
1118                               (svc->number_temporal_layers > 1);
1119   TWO_PASS *const twopass = is_two_pass_svc ?
1120       &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
1121   double frame_rate;
1122   FIRSTPASS_STATS *stats;
1123
1124   zero_stats(&twopass->total_stats);
1125   zero_stats(&twopass->total_left_stats);
1126
1127   if (!twopass->stats_in_end)
1128     return;
1129
1130   stats = &twopass->total_stats;
1131
1132   *stats = *twopass->stats_in_end;
1133   twopass->total_left_stats = *stats;
1134
1135   frame_rate = 10000000.0 * stats->count / stats->duration;
1136   // Each frame can have a different duration, as the frame rate in the source
1137   // isn't guaranteed to be constant. The frame rate prior to the first frame
1138   // encoded in the second pass is a guess. However, the sum duration is not.
1139   // It is calculated based on the actual durations of all frames from the
1140   // first pass.
1141
1142   if (is_two_pass_svc) {
1143     vp9_update_spatial_layer_framerate(cpi, frame_rate);
1144     twopass->bits_left = (int64_t)(stats->duration *
1145         svc->layer_context[svc->spatial_layer_id].target_bandwidth /
1146         10000000.0);
1147   } else {
1148     vp9_new_framerate(cpi, frame_rate);
1149     twopass->bits_left = (int64_t)(stats->duration * oxcf->target_bandwidth /
1150                              10000000.0);
1151   }
1152
1153   // This variable monitors how far behind the second ref update is lagging.
1154   twopass->sr_update_lag = 1;
1155
1156   // Scan the first pass file and calculate a modified total error based upon
1157   // the bias/power function used to allocate bits.
1158   {
1159     const double avg_error = stats->coded_error /
1160                              DOUBLE_DIVIDE_CHECK(stats->count);
1161     const FIRSTPASS_STATS *s = twopass->stats_in;
1162     double modified_error_total = 0.0;
1163     twopass->modified_error_min = (avg_error *
1164                                       oxcf->two_pass_vbrmin_section) / 100;
1165     twopass->modified_error_max = (avg_error *
1166                                       oxcf->two_pass_vbrmax_section) / 100;
1167     while (s < twopass->stats_in_end) {
1168       modified_error_total += calculate_modified_err(twopass, oxcf, s);
1169       ++s;
1170     }
1171     twopass->modified_error_left = modified_error_total;
1172   }
1173
1174   // Reset the vbr bits off target counter
1175   cpi->rc.vbr_bits_off_target = 0;
1176
1177   cpi->rc.rate_error_estimate = 0;
1178
1179   // Static sequence monitor variables.
1180   twopass->kf_zeromotion_pct = 100;
1181   twopass->last_kfgroup_zeromotion_pct = 100;
1182 }
1183
1184 #define SR_DIFF_PART 0.0015
1185 #define MOTION_AMP_PART 0.003
1186 #define INTRA_PART 0.005
1187 #define DEFAULT_DECAY_LIMIT 0.75
1188 #define LOW_SR_DIFF_TRHESH 0.1
1189 #define SR_DIFF_MAX 128.0
1190
1191 static double get_sr_decay_rate(const VP9_COMMON *cm,
1192                                 const FIRSTPASS_STATS *frame) {
1193   double sr_diff = (frame->sr_coded_error - frame->coded_error) / cm->MBs;
1194   double sr_decay = 1.0;
1195   const double motion_amplitude_factor =
1196     frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) / 2);
1197   const double pcnt_intra = 100 * (1.0 - frame->pcnt_inter);
1198
1199   if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1200     sr_diff = MIN(sr_diff, SR_DIFF_MAX);
1201     sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) -
1202                (MOTION_AMP_PART * motion_amplitude_factor) -
1203                (INTRA_PART * pcnt_intra);
1204   }
1205   return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, frame->pcnt_inter));
1206 }
1207
1208 // This function gives an estimate of how badly we believe the prediction
1209 // quality is decaying from frame to frame.
1210 static double get_zero_motion_factor(const VP9_COMMON *cm,
1211                                      const FIRSTPASS_STATS *frame) {
1212   const double zero_motion_pct = frame->pcnt_inter -
1213                                  frame->pcnt_motion;
1214   double sr_decay = get_sr_decay_rate(cm, frame);
1215   return MIN(sr_decay, zero_motion_pct);
1216 }
1217
1218 #define ZM_POWER_FACTOR 0.75
1219
1220 static double get_prediction_decay_rate(const VP9_COMMON *cm,
1221                                         const FIRSTPASS_STATS *next_frame) {
1222   const double sr_decay_rate = get_sr_decay_rate(cm, next_frame);
1223   const double zero_motion_factor =
1224     (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
1225                 ZM_POWER_FACTOR));
1226
1227   return MAX(zero_motion_factor,
1228              (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1229 }
1230
1231 // Function to test for a condition where a complex transition is followed
1232 // by a static section. For example in slide shows where there is a fade
1233 // between slides. This is to help with more optimal kf and gf positioning.
1234 static int detect_transition_to_still(const TWO_PASS *twopass,
1235                                       int frame_interval, int still_interval,
1236                                       double loop_decay_rate,
1237                                       double last_decay_rate) {
1238   // Break clause to detect very still sections after motion
1239   // For example a static image after a fade or other transition
1240   // instead of a clean scene cut.
1241   if (frame_interval > MIN_GF_INTERVAL &&
1242       loop_decay_rate >= 0.999 &&
1243       last_decay_rate < 0.9) {
1244     int j;
1245
1246     // Look ahead a few frames to see if static condition persists...
1247     for (j = 0; j < still_interval; ++j) {
1248       const FIRSTPASS_STATS *stats = &twopass->stats_in[j];
1249       if (stats >= twopass->stats_in_end)
1250         break;
1251
1252       if (stats->pcnt_inter - stats->pcnt_motion < 0.999)
1253         break;
1254     }
1255
1256     // Only if it does do we signal a transition to still.
1257     return j == still_interval;
1258   }
1259
1260   return 0;
1261 }
1262
1263 // This function detects a flash through the high relative pcnt_second_ref
1264 // score in the frame following a flash frame. The offset passed in should
1265 // reflect this.
1266 static int detect_flash(const TWO_PASS *twopass, int offset) {
1267   const FIRSTPASS_STATS *const next_frame = read_frame_stats(twopass, offset);
1268
1269   // What we are looking for here is a situation where there is a
1270   // brief break in prediction (such as a flash) but subsequent frames
1271   // are reasonably well predicted by an earlier (pre flash) frame.
1272   // The recovery after a flash is indicated by a high pcnt_second_ref
1273   // compared to pcnt_inter.
1274   return next_frame != NULL &&
1275          next_frame->pcnt_second_ref > next_frame->pcnt_inter &&
1276          next_frame->pcnt_second_ref >= 0.5;
1277 }
1278
1279 // Update the motion related elements to the GF arf boost calculation.
1280 static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats,
1281                                           double *mv_in_out,
1282                                           double *mv_in_out_accumulator,
1283                                           double *abs_mv_in_out_accumulator,
1284                                           double *mv_ratio_accumulator) {
1285   const double pct = stats->pcnt_motion;
1286
1287   // Accumulate Motion In/Out of frame stats.
1288   *mv_in_out = stats->mv_in_out_count * pct;
1289   *mv_in_out_accumulator += *mv_in_out;
1290   *abs_mv_in_out_accumulator += fabs(*mv_in_out);
1291
1292   // Accumulate a measure of how uniform (or conversely how random) the motion
1293   // field is (a ratio of abs(mv) / mv).
1294   if (pct > 0.05) {
1295     const double mvr_ratio = fabs(stats->mvr_abs) /
1296                                  DOUBLE_DIVIDE_CHECK(fabs(stats->MVr));
1297     const double mvc_ratio = fabs(stats->mvc_abs) /
1298                                  DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
1299
1300     *mv_ratio_accumulator += pct * (mvr_ratio < stats->mvr_abs ?
1301                                        mvr_ratio : stats->mvr_abs);
1302     *mv_ratio_accumulator += pct * (mvc_ratio < stats->mvc_abs ?
1303                                        mvc_ratio : stats->mvc_abs);
1304   }
1305 }
1306
1307 #define BASELINE_ERR_PER_MB 1000.0
1308 static double calc_frame_boost(VP9_COMP *cpi,
1309                                const FIRSTPASS_STATS *this_frame,
1310                                double this_frame_mv_in_out,
1311                                double max_boost) {
1312   double frame_boost;
1313   const double lq =
1314     vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME],
1315                             cpi->common.bit_depth);
1316   const double boost_correction = MIN((0.5 + (lq * 0.015)), 1.5);
1317
1318   // Underlying boost factor is based on inter error ratio.
1319   frame_boost = (BASELINE_ERR_PER_MB * cpi->common.MBs) /
1320                 DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1321   frame_boost = frame_boost * BOOST_FACTOR * boost_correction;
1322
1323   // Increase boost for frames where new data coming into frame (e.g. zoom out).
1324   // Slightly reduce boost if there is a net balance of motion out of the frame
1325   // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1326   if (this_frame_mv_in_out > 0.0)
1327     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1328   // In the extreme case the boost is halved.
1329   else
1330     frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1331
1332   return MIN(frame_boost, max_boost * boost_correction);
1333 }
1334
1335 static int calc_arf_boost(VP9_COMP *cpi, int offset,
1336                           int f_frames, int b_frames,
1337                           int *f_boost, int *b_boost) {
1338   TWO_PASS *const twopass = &cpi->twopass;
1339   int i;
1340   double boost_score = 0.0;
1341   double mv_ratio_accumulator = 0.0;
1342   double decay_accumulator = 1.0;
1343   double this_frame_mv_in_out = 0.0;
1344   double mv_in_out_accumulator = 0.0;
1345   double abs_mv_in_out_accumulator = 0.0;
1346   int arf_boost;
1347   int flash_detected = 0;
1348
1349   // Search forward from the proposed arf/next gf position.
1350   for (i = 0; i < f_frames; ++i) {
1351     const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1352     if (this_frame == NULL)
1353       break;
1354
1355     // Update the motion related elements to the boost calculation.
1356     accumulate_frame_motion_stats(this_frame,
1357                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
1358                                   &abs_mv_in_out_accumulator,
1359                                   &mv_ratio_accumulator);
1360
1361     // We want to discount the flash frame itself and the recovery
1362     // frame that follows as both will have poor scores.
1363     flash_detected = detect_flash(twopass, i + offset) ||
1364                      detect_flash(twopass, i + offset + 1);
1365
1366     // Accumulate the effect of prediction quality decay.
1367     if (!flash_detected) {
1368       decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame);
1369       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1370                           ? MIN_DECAY_FACTOR : decay_accumulator;
1371     }
1372
1373     boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1374                                                         this_frame_mv_in_out,
1375                                                         GF_MAX_BOOST);
1376   }
1377
1378   *f_boost = (int)boost_score;
1379
1380   // Reset for backward looking loop.
1381   boost_score = 0.0;
1382   mv_ratio_accumulator = 0.0;
1383   decay_accumulator = 1.0;
1384   this_frame_mv_in_out = 0.0;
1385   mv_in_out_accumulator = 0.0;
1386   abs_mv_in_out_accumulator = 0.0;
1387
1388   // Search backward towards last gf position.
1389   for (i = -1; i >= -b_frames; --i) {
1390     const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset);
1391     if (this_frame == NULL)
1392       break;
1393
1394     // Update the motion related elements to the boost calculation.
1395     accumulate_frame_motion_stats(this_frame,
1396                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
1397                                   &abs_mv_in_out_accumulator,
1398                                   &mv_ratio_accumulator);
1399
1400     // We want to discount the the flash frame itself and the recovery
1401     // frame that follows as both will have poor scores.
1402     flash_detected = detect_flash(twopass, i + offset) ||
1403                      detect_flash(twopass, i + offset + 1);
1404
1405     // Cumulative effect of prediction quality decay.
1406     if (!flash_detected) {
1407       decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame);
1408       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1409                               ? MIN_DECAY_FACTOR : decay_accumulator;
1410     }
1411
1412     boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1413                                                         this_frame_mv_in_out,
1414                                                         GF_MAX_BOOST);
1415   }
1416   *b_boost = (int)boost_score;
1417
1418   arf_boost = (*f_boost + *b_boost);
1419   if (arf_boost < ((b_frames + f_frames) * 20))
1420     arf_boost = ((b_frames + f_frames) * 20);
1421   arf_boost = MAX(arf_boost, MIN_ARF_GF_BOOST);
1422
1423   return arf_boost;
1424 }
1425
1426 // Calculate a section intra ratio used in setting max loop filter.
1427 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
1428                                          const FIRSTPASS_STATS *end,
1429                                          int section_length) {
1430   const FIRSTPASS_STATS *s = begin;
1431   double intra_error = 0.0;
1432   double coded_error = 0.0;
1433   int i = 0;
1434
1435   while (s < end && i < section_length) {
1436     intra_error += s->intra_error;
1437     coded_error += s->coded_error;
1438     ++s;
1439     ++i;
1440   }
1441
1442   return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error));
1443 }
1444
1445 // Calculate the total bits to allocate in this GF/ARF group.
1446 static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi,
1447                                              double gf_group_err) {
1448   const RATE_CONTROL *const rc = &cpi->rc;
1449   const TWO_PASS *const twopass = &cpi->twopass;
1450   const int max_bits = frame_max_bits(rc, &cpi->oxcf);
1451   int64_t total_group_bits;
1452
1453   // Calculate the bits to be allocated to the group as a whole.
1454   if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) {
1455     total_group_bits = (int64_t)(twopass->kf_group_bits *
1456                                  (gf_group_err / twopass->kf_group_error_left));
1457   } else {
1458     total_group_bits = 0;
1459   }
1460
1461   // Clamp odd edge cases.
1462   total_group_bits = (total_group_bits < 0) ?
1463      0 : (total_group_bits > twopass->kf_group_bits) ?
1464      twopass->kf_group_bits : total_group_bits;
1465
1466   // Clip based on user supplied data rate variability limit.
1467   if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
1468     total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
1469
1470   return total_group_bits;
1471 }
1472
1473 // Calculate the number bits extra to assign to boosted frames in a group.
1474 static int calculate_boost_bits(int frame_count,
1475                                 int boost, int64_t total_group_bits) {
1476   int allocation_chunks;
1477
1478   // return 0 for invalid inputs (could arise e.g. through rounding errors)
1479   if (!boost || (total_group_bits <= 0) || (frame_count <= 0) )
1480     return 0;
1481
1482   allocation_chunks = (frame_count * 100) + boost;
1483
1484   // Prevent overflow.
1485   if (boost > 1023) {
1486     int divisor = boost >> 10;
1487     boost /= divisor;
1488     allocation_chunks /= divisor;
1489   }
1490
1491   // Calculate the number of extra bits for use in the boosted frame or frames.
1492   return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0);
1493 }
1494
1495 // Current limit on maximum number of active arfs in a GF/ARF group.
1496 #define MAX_ACTIVE_ARFS 2
1497 #define ARF_SLOT1 2
1498 #define ARF_SLOT2 3
1499 // This function indirects the choice of buffers for arfs.
1500 // At the moment the values are fixed but this may change as part of
1501 // the integration process with other codec features that swap buffers around.
1502 static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) {
1503   arf_buffer_indices[0] = ARF_SLOT1;
1504   arf_buffer_indices[1] = ARF_SLOT2;
1505 }
1506
1507 static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits,
1508                                    double group_error, int gf_arf_bits) {
1509   RATE_CONTROL *const rc = &cpi->rc;
1510   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1511   TWO_PASS *const twopass = &cpi->twopass;
1512   GF_GROUP *const gf_group = &twopass->gf_group;
1513   FIRSTPASS_STATS frame_stats;
1514   int i;
1515   int frame_index = 1;
1516   int target_frame_size;
1517   int key_frame;
1518   const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf);
1519   int64_t total_group_bits = gf_group_bits;
1520   double modified_err = 0.0;
1521   double err_fraction;
1522   int mid_boost_bits = 0;
1523   int mid_frame_idx;
1524   unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS];
1525   int alt_frame_index = frame_index;
1526   int has_temporal_layers = is_two_pass_svc(cpi) &&
1527                             cpi->svc.number_temporal_layers > 1;
1528
1529   // Only encode alt reference frame in temporal base layer.
1530   if (has_temporal_layers)
1531     alt_frame_index = cpi->svc.number_temporal_layers;
1532
1533   key_frame = cpi->common.frame_type == KEY_FRAME ||
1534               vp9_is_upper_layer_key_frame(cpi);
1535
1536   get_arf_buffer_indices(arf_buffer_indices);
1537
1538   // For key frames the frame target rate is already set and it
1539   // is also the golden frame.
1540   if (!key_frame) {
1541     if (rc->source_alt_ref_active) {
1542       gf_group->update_type[0] = OVERLAY_UPDATE;
1543       gf_group->rf_level[0] = INTER_NORMAL;
1544       gf_group->bit_allocation[0] = 0;
1545       gf_group->arf_update_idx[0] = arf_buffer_indices[0];
1546       gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
1547     } else {
1548       gf_group->update_type[0] = GF_UPDATE;
1549       gf_group->rf_level[0] = GF_ARF_STD;
1550       gf_group->bit_allocation[0] = gf_arf_bits;
1551       gf_group->arf_update_idx[0] = arf_buffer_indices[0];
1552       gf_group->arf_ref_idx[0] = arf_buffer_indices[0];
1553     }
1554
1555     // Step over the golden frame / overlay frame
1556     if (EOF == input_stats(twopass, &frame_stats))
1557       return;
1558   }
1559
1560   // Deduct the boost bits for arf (or gf if it is not a key frame)
1561   // from the group total.
1562   if (rc->source_alt_ref_pending || !key_frame)
1563     total_group_bits -= gf_arf_bits;
1564
1565   // Store the bits to spend on the ARF if there is one.
1566   if (rc->source_alt_ref_pending) {
1567     gf_group->update_type[alt_frame_index] = ARF_UPDATE;
1568     gf_group->rf_level[alt_frame_index] = GF_ARF_STD;
1569     gf_group->bit_allocation[alt_frame_index] = gf_arf_bits;
1570
1571     if (has_temporal_layers)
1572       gf_group->arf_src_offset[alt_frame_index] =
1573           (unsigned char)(rc->baseline_gf_interval -
1574                           cpi->svc.number_temporal_layers);
1575     else
1576       gf_group->arf_src_offset[alt_frame_index] =
1577           (unsigned char)(rc->baseline_gf_interval - 1);
1578
1579     gf_group->arf_update_idx[alt_frame_index] = arf_buffer_indices[0];
1580     gf_group->arf_ref_idx[alt_frame_index] =
1581       arf_buffer_indices[cpi->multi_arf_last_grp_enabled &&
1582                          rc->source_alt_ref_active];
1583     if (!has_temporal_layers)
1584       ++frame_index;
1585
1586     if (cpi->multi_arf_enabled) {
1587       // Set aside a slot for a level 1 arf.
1588       gf_group->update_type[frame_index] = ARF_UPDATE;
1589       gf_group->rf_level[frame_index] = GF_ARF_LOW;
1590       gf_group->arf_src_offset[frame_index] =
1591         (unsigned char)((rc->baseline_gf_interval >> 1) - 1);
1592       gf_group->arf_update_idx[frame_index] = arf_buffer_indices[1];
1593       gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
1594       ++frame_index;
1595     }
1596   }
1597
1598   // Define middle frame
1599   mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
1600
1601   // Allocate bits to the other frames in the group.
1602   for (i = 0; i < rc->baseline_gf_interval - 1; ++i) {
1603     int arf_idx = 0;
1604     if (EOF == input_stats(twopass, &frame_stats))
1605       break;
1606
1607     if (has_temporal_layers && frame_index == alt_frame_index) {
1608       ++frame_index;
1609     }
1610
1611     modified_err = calculate_modified_err(twopass, oxcf, &frame_stats);
1612
1613     if (group_error > 0)
1614       err_fraction = modified_err / DOUBLE_DIVIDE_CHECK(group_error);
1615     else
1616       err_fraction = 0.0;
1617
1618     target_frame_size = (int)((double)total_group_bits * err_fraction);
1619
1620     if (rc->source_alt_ref_pending && cpi->multi_arf_enabled) {
1621       mid_boost_bits += (target_frame_size >> 4);
1622       target_frame_size -= (target_frame_size >> 4);
1623
1624       if (frame_index <= mid_frame_idx)
1625         arf_idx = 1;
1626     }
1627     gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx];
1628     gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx];
1629
1630     target_frame_size = clamp(target_frame_size, 0,
1631                               MIN(max_bits, (int)total_group_bits));
1632
1633     gf_group->update_type[frame_index] = LF_UPDATE;
1634     gf_group->rf_level[frame_index] = INTER_NORMAL;
1635
1636     gf_group->bit_allocation[frame_index] = target_frame_size;
1637     ++frame_index;
1638   }
1639
1640   // Note:
1641   // We need to configure the frame at the end of the sequence + 1 that will be
1642   // the start frame for the next group. Otherwise prior to the call to
1643   // vp9_rc_get_second_pass_params() the data will be undefined.
1644   gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0];
1645   gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0];
1646
1647   if (rc->source_alt_ref_pending) {
1648     gf_group->update_type[frame_index] = OVERLAY_UPDATE;
1649     gf_group->rf_level[frame_index] = INTER_NORMAL;
1650
1651     // Final setup for second arf and its overlay.
1652     if (cpi->multi_arf_enabled) {
1653       gf_group->bit_allocation[2] =
1654           gf_group->bit_allocation[mid_frame_idx] + mid_boost_bits;
1655       gf_group->update_type[mid_frame_idx] = OVERLAY_UPDATE;
1656       gf_group->bit_allocation[mid_frame_idx] = 0;
1657     }
1658   } else {
1659     gf_group->update_type[frame_index] = GF_UPDATE;
1660     gf_group->rf_level[frame_index] = GF_ARF_STD;
1661   }
1662
1663   // Note whether multi-arf was enabled this group for next time.
1664   cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled;
1665 }
1666
1667 // Analyse and define a gf/arf group.
1668 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1669   RATE_CONTROL *const rc = &cpi->rc;
1670   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1671   TWO_PASS *const twopass = &cpi->twopass;
1672   FIRSTPASS_STATS next_frame;
1673   const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
1674   int i;
1675
1676   double boost_score = 0.0;
1677   double old_boost_score = 0.0;
1678   double gf_group_err = 0.0;
1679   double gf_first_frame_err = 0.0;
1680   double mod_frame_err = 0.0;
1681
1682   double mv_ratio_accumulator = 0.0;
1683   double decay_accumulator = 1.0;
1684   double zero_motion_accumulator = 1.0;
1685
1686   double loop_decay_rate = 1.00;
1687   double last_loop_decay_rate = 1.00;
1688
1689   double this_frame_mv_in_out = 0.0;
1690   double mv_in_out_accumulator = 0.0;
1691   double abs_mv_in_out_accumulator = 0.0;
1692   double mv_ratio_accumulator_thresh;
1693   unsigned int allow_alt_ref = is_altref_enabled(cpi);
1694
1695   int f_boost = 0;
1696   int b_boost = 0;
1697   int flash_detected;
1698   int active_max_gf_interval;
1699   int active_min_gf_interval;
1700   int64_t gf_group_bits;
1701   double gf_group_error_left;
1702   int gf_arf_bits;
1703
1704   // Reset the GF group data structures unless this is a key
1705   // frame in which case it will already have been done.
1706   if (cpi->common.frame_type != KEY_FRAME) {
1707     vp9_zero(twopass->gf_group);
1708   }
1709
1710   vp9_clear_system_state();
1711   vp9_zero(next_frame);
1712
1713   // Load stats for the current frame.
1714   mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame);
1715
1716   // Note the error of the frame at the start of the group. This will be
1717   // the GF frame error if we code a normal gf.
1718   gf_first_frame_err = mod_frame_err;
1719
1720   // If this is a key frame or the overlay from a previous arf then
1721   // the error score / cost of this frame has already been accounted for.
1722   if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1723     gf_group_err -= gf_first_frame_err;
1724
1725   // Motion breakout threshold for loop below depends on image size.
1726   mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 4.0;
1727
1728   // Set a maximum and minimum interval for the GF group.
1729   // If the image appears almost completely static we can extend beyond this.
1730   {
1731     int int_max_q =
1732       (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
1733                                    cpi->common.bit_depth));
1734     int int_lbq =
1735       (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex,
1736                                    cpi->common.bit_depth));
1737     active_min_gf_interval = MIN_GF_INTERVAL + MIN(2, int_max_q / 200);
1738     if (active_min_gf_interval > rc->max_gf_interval)
1739       active_min_gf_interval = rc->max_gf_interval;
1740
1741     if (cpi->multi_arf_allowed) {
1742       active_max_gf_interval = rc->max_gf_interval;
1743     } else {
1744       // The value chosen depends on the active Q range. At low Q we have
1745       // bits to spare and are better with a smaller interval and smaller boost.
1746       // At high Q when there are few bits to spare we are better with a longer
1747       // interval to spread the cost of the GF.
1748       active_max_gf_interval = 12 + MIN(4, (int_lbq / 6));
1749       if (active_max_gf_interval > rc->max_gf_interval)
1750         active_max_gf_interval = rc->max_gf_interval;
1751     }
1752   }
1753
1754   i = 0;
1755   while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
1756     ++i;
1757
1758     // Accumulate error score of frames in this gf group.
1759     mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame);
1760     gf_group_err += mod_frame_err;
1761
1762     if (EOF == input_stats(twopass, &next_frame))
1763       break;
1764
1765     // Test for the case where there is a brief flash but the prediction
1766     // quality back to an earlier frame is then restored.
1767     flash_detected = detect_flash(twopass, 0);
1768
1769     // Update the motion related elements to the boost calculation.
1770     accumulate_frame_motion_stats(&next_frame,
1771                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
1772                                   &abs_mv_in_out_accumulator,
1773                                   &mv_ratio_accumulator);
1774
1775     // Accumulate the effect of prediction quality decay.
1776     if (!flash_detected) {
1777       last_loop_decay_rate = loop_decay_rate;
1778       loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
1779
1780       decay_accumulator = decay_accumulator * loop_decay_rate;
1781
1782       // Monitor for static sections.
1783       zero_motion_accumulator =
1784         MIN(zero_motion_accumulator,
1785             get_zero_motion_factor(&cpi->common, &next_frame));
1786
1787       // Break clause to detect very still sections after motion. For example,
1788       // a static image after a fade or other transition.
1789       if (detect_transition_to_still(twopass, i, 5, loop_decay_rate,
1790                                      last_loop_decay_rate)) {
1791         allow_alt_ref = 0;
1792         break;
1793       }
1794     }
1795
1796     // Calculate a boost number for this frame.
1797     boost_score += decay_accumulator * calc_frame_boost(cpi, &next_frame,
1798                                                         this_frame_mv_in_out,
1799                                                         GF_MAX_BOOST);
1800
1801     // Break out conditions.
1802     if (
1803       // Break at active_max_gf_interval unless almost totally static.
1804       (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
1805       (
1806         // Don't break out with a very short interval.
1807         (i > active_min_gf_interval) &&
1808         (!flash_detected) &&
1809         ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
1810          (abs_mv_in_out_accumulator > 3.0) ||
1811          (mv_in_out_accumulator < -2.0) ||
1812          ((boost_score - old_boost_score) < BOOST_BREAKOUT)))) {
1813       boost_score = old_boost_score;
1814       break;
1815     }
1816
1817     *this_frame = next_frame;
1818     old_boost_score = boost_score;
1819   }
1820
1821   twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
1822
1823   // Set the interval until the next gf.
1824   if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1825     rc->baseline_gf_interval = i - 1;
1826   else
1827     rc->baseline_gf_interval = i;
1828
1829   // Only encode alt reference frame in temporal base layer. So
1830   // baseline_gf_interval should be multiple of a temporal layer group
1831   // (typically the frame distance between two base layer frames)
1832   if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
1833     int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
1834     int new_gf_interval = (rc->baseline_gf_interval + count) & (~count);
1835     int j;
1836     for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) {
1837       if (EOF == input_stats(twopass, this_frame))
1838         break;
1839       gf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
1840     }
1841     rc->baseline_gf_interval = new_gf_interval;
1842   }
1843
1844   rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1845
1846   // Should we use the alternate reference frame.
1847   if (allow_alt_ref &&
1848       (i < cpi->oxcf.lag_in_frames) &&
1849       (i >= MIN_GF_INTERVAL)) {
1850     // Calculate the boost for alt ref.
1851     rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
1852                                    &b_boost);
1853     rc->source_alt_ref_pending = 1;
1854
1855     // Test to see if multi arf is appropriate.
1856     cpi->multi_arf_enabled =
1857       (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) &&
1858       (zero_motion_accumulator < 0.995)) ? 1 : 0;
1859   } else {
1860     rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST);
1861     rc->source_alt_ref_pending = 0;
1862   }
1863
1864   // Reset the file position.
1865   reset_fpf_position(twopass, start_pos);
1866
1867   // Calculate the bits to be allocated to the gf/arf group as a whole
1868   gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
1869
1870   // Calculate the extra bits to be used for boosted frame(s)
1871   gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval,
1872                                      rc->gfu_boost, gf_group_bits);
1873
1874   // Adjust KF group bits and error remaining.
1875   twopass->kf_group_error_left -= (int64_t)gf_group_err;
1876
1877   // If this is an arf update we want to remove the score for the overlay
1878   // frame at the end which will usually be very cheap to code.
1879   // The overlay frame has already, in effect, been coded so we want to spread
1880   // the remaining bits among the other frames.
1881   // For normal GFs remove the score for the GF itself unless this is
1882   // also a key frame in which case it has already been accounted for.
1883   if (rc->source_alt_ref_pending) {
1884     gf_group_error_left = gf_group_err - mod_frame_err;
1885   } else if (cpi->common.frame_type != KEY_FRAME) {
1886     gf_group_error_left = gf_group_err - gf_first_frame_err;
1887   } else {
1888     gf_group_error_left = gf_group_err;
1889   }
1890
1891   // Allocate bits to each of the frames in the GF group.
1892   allocate_gf_group_bits(cpi, gf_group_bits, gf_group_error_left, gf_arf_bits);
1893
1894   // Reset the file position.
1895   reset_fpf_position(twopass, start_pos);
1896
1897   // Calculate a section intra ratio used in setting max loop filter.
1898   if (cpi->common.frame_type != KEY_FRAME) {
1899     twopass->section_intra_rating =
1900         calculate_section_intra_ratio(start_pos, twopass->stats_in_end,
1901                                       rc->baseline_gf_interval);
1902   }
1903 }
1904
1905 // TODO(PGW) Re-examine the use of II ration in this code in the light of#
1906 // changes elsewhere
1907 #define KF_II_MAX 128.0
1908 static int test_candidate_kf(TWO_PASS *twopass,
1909                              const FIRSTPASS_STATS *last_frame,
1910                              const FIRSTPASS_STATS *this_frame,
1911                              const FIRSTPASS_STATS *next_frame) {
1912   int is_viable_kf = 0;
1913
1914   // Does the frame satisfy the primary criteria of a key frame?
1915   // If so, then examine how well it predicts subsequent frames.
1916   if ((this_frame->pcnt_second_ref < 0.10) &&
1917       (next_frame->pcnt_second_ref < 0.10) &&
1918       ((this_frame->pcnt_inter < 0.05) ||
1919        (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) &&
1920         ((this_frame->intra_error /
1921           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
1922         ((fabs(last_frame->coded_error - this_frame->coded_error) /
1923               DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) ||
1924          (fabs(last_frame->intra_error - this_frame->intra_error) /
1925               DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) ||
1926          ((next_frame->intra_error /
1927            DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
1928     int i;
1929     const FIRSTPASS_STATS *start_pos = twopass->stats_in;
1930     FIRSTPASS_STATS local_next_frame = *next_frame;
1931     double boost_score = 0.0;
1932     double old_boost_score = 0.0;
1933     double decay_accumulator = 1.0;
1934
1935     // Examine how well the key frame predicts subsequent frames.
1936     for (i = 0; i < 16; ++i) {
1937       double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error /
1938                              DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
1939
1940       if (next_iiratio > KF_II_MAX)
1941         next_iiratio = KF_II_MAX;
1942
1943       // Cumulative effect of decay in prediction quality.
1944       if (local_next_frame.pcnt_inter > 0.85)
1945         decay_accumulator *= local_next_frame.pcnt_inter;
1946       else
1947         decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
1948
1949       // Keep a running total.
1950       boost_score += (decay_accumulator * next_iiratio);
1951
1952       // Test various breakout clauses.
1953       if ((local_next_frame.pcnt_inter < 0.05) ||
1954           (next_iiratio < 1.5) ||
1955           (((local_next_frame.pcnt_inter -
1956              local_next_frame.pcnt_neutral) < 0.20) &&
1957            (next_iiratio < 3.0)) ||
1958           ((boost_score - old_boost_score) < 3.0) ||
1959           (local_next_frame.intra_error < 200)) {
1960         break;
1961       }
1962
1963       old_boost_score = boost_score;
1964
1965       // Get the next frame details
1966       if (EOF == input_stats(twopass, &local_next_frame))
1967         break;
1968     }
1969
1970     // If there is tolerable prediction for at least the next 3 frames then
1971     // break out else discard this potential key frame and move on
1972     if (boost_score > 30.0 && (i > 3)) {
1973       is_viable_kf = 1;
1974     } else {
1975       // Reset the file position
1976       reset_fpf_position(twopass, start_pos);
1977
1978       is_viable_kf = 0;
1979     }
1980   }
1981
1982   return is_viable_kf;
1983 }
1984
1985 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1986   int i, j;
1987   RATE_CONTROL *const rc = &cpi->rc;
1988   TWO_PASS *const twopass = &cpi->twopass;
1989   GF_GROUP *const gf_group = &twopass->gf_group;
1990   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1991   const FIRSTPASS_STATS first_frame = *this_frame;
1992   const FIRSTPASS_STATS *const start_position = twopass->stats_in;
1993   FIRSTPASS_STATS next_frame;
1994   FIRSTPASS_STATS last_frame;
1995   int kf_bits = 0;
1996   int loop_decay_counter = 0;
1997   double decay_accumulator = 1.0;
1998   double av_decay_accumulator = 0.0;
1999   double zero_motion_accumulator = 1.0;
2000   double boost_score = 0.0;
2001   double kf_mod_err = 0.0;
2002   double kf_group_err = 0.0;
2003   double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
2004
2005   vp9_zero(next_frame);
2006
2007   cpi->common.frame_type = KEY_FRAME;
2008
2009   // Reset the GF group data structures.
2010   vp9_zero(*gf_group);
2011
2012   // Is this a forced key frame by interval.
2013   rc->this_key_frame_forced = rc->next_key_frame_forced;
2014
2015   // Clear the alt ref active flag and last group multi arf flags as they
2016   // can never be set for a key frame.
2017   rc->source_alt_ref_active = 0;
2018   cpi->multi_arf_last_grp_enabled = 0;
2019
2020   // KF is always a GF so clear frames till next gf counter.
2021   rc->frames_till_gf_update_due = 0;
2022
2023   rc->frames_to_key = 1;
2024
2025   twopass->kf_group_bits = 0;        // Total bits available to kf group
2026   twopass->kf_group_error_left = 0;  // Group modified error score.
2027
2028   kf_mod_err = calculate_modified_err(twopass, oxcf, this_frame);
2029
2030   // Find the next keyframe.
2031   i = 0;
2032   while (twopass->stats_in < twopass->stats_in_end &&
2033          rc->frames_to_key < cpi->oxcf.key_freq) {
2034     // Accumulate kf group error.
2035     kf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
2036
2037     // Load the next frame's stats.
2038     last_frame = *this_frame;
2039     input_stats(twopass, this_frame);
2040
2041     // Provided that we are not at the end of the file...
2042     if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) {
2043       double loop_decay_rate;
2044
2045       // Check for a scene cut.
2046       if (test_candidate_kf(twopass, &last_frame, this_frame,
2047                             twopass->stats_in))
2048         break;
2049
2050       // How fast is the prediction quality decaying?
2051       loop_decay_rate = get_prediction_decay_rate(&cpi->common,
2052                                                   twopass->stats_in);
2053
2054       // We want to know something about the recent past... rather than
2055       // as used elsewhere where we are concerned with decay in prediction
2056       // quality since the last GF or KF.
2057       recent_loop_decay[i % 8] = loop_decay_rate;
2058       decay_accumulator = 1.0;
2059       for (j = 0; j < 8; ++j)
2060         decay_accumulator *= recent_loop_decay[j];
2061
2062       // Special check for transition or high motion followed by a
2063       // static scene.
2064       if (detect_transition_to_still(twopass, i, cpi->oxcf.key_freq - i,
2065                                      loop_decay_rate, decay_accumulator))
2066         break;
2067
2068       // Step on to the next frame.
2069       ++rc->frames_to_key;
2070
2071       // If we don't have a real key frame within the next two
2072       // key_freq intervals then break out of the loop.
2073       if (rc->frames_to_key >= 2 * cpi->oxcf.key_freq)
2074         break;
2075     } else {
2076       ++rc->frames_to_key;
2077     }
2078     ++i;
2079   }
2080
2081   // If there is a max kf interval set by the user we must obey it.
2082   // We already breakout of the loop above at 2x max.
2083   // This code centers the extra kf if the actual natural interval
2084   // is between 1x and 2x.
2085   if (cpi->oxcf.auto_key &&
2086       rc->frames_to_key > cpi->oxcf.key_freq) {
2087     FIRSTPASS_STATS tmp_frame = first_frame;
2088
2089     rc->frames_to_key /= 2;
2090
2091     // Reset to the start of the group.
2092     reset_fpf_position(twopass, start_position);
2093
2094     kf_group_err = 0;
2095
2096     // Rescan to get the correct error data for the forced kf group.
2097     for (i = 0; i < rc->frames_to_key; ++i) {
2098       kf_group_err += calculate_modified_err(twopass, oxcf, &tmp_frame);
2099       input_stats(twopass, &tmp_frame);
2100     }
2101     rc->next_key_frame_forced = 1;
2102   } else if (twopass->stats_in == twopass->stats_in_end ||
2103              rc->frames_to_key >= cpi->oxcf.key_freq) {
2104     rc->next_key_frame_forced = 1;
2105   } else {
2106     rc->next_key_frame_forced = 0;
2107   }
2108
2109   if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
2110     int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
2111     int new_frame_to_key = (rc->frames_to_key + count) & (~count);
2112     int j;
2113     for (j = 0; j < new_frame_to_key - rc->frames_to_key; ++j) {
2114       if (EOF == input_stats(twopass, this_frame))
2115         break;
2116       kf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
2117     }
2118     rc->frames_to_key = new_frame_to_key;
2119   }
2120
2121   // Special case for the last key frame of the file.
2122   if (twopass->stats_in >= twopass->stats_in_end) {
2123     // Accumulate kf group error.
2124     kf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
2125   }
2126
2127   // Calculate the number of bits that should be assigned to the kf group.
2128   if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) {
2129     // Maximum number of bits for a single normal frame (not key frame).
2130     const int max_bits = frame_max_bits(rc, &cpi->oxcf);
2131
2132     // Maximum number of bits allocated to the key frame group.
2133     int64_t max_grp_bits;
2134
2135     // Default allocation based on bits left and relative
2136     // complexity of the section.
2137     twopass->kf_group_bits = (int64_t)(twopass->bits_left *
2138        (kf_group_err / twopass->modified_error_left));
2139
2140     // Clip based on maximum per frame rate defined by the user.
2141     max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
2142     if (twopass->kf_group_bits > max_grp_bits)
2143       twopass->kf_group_bits = max_grp_bits;
2144   } else {
2145     twopass->kf_group_bits = 0;
2146   }
2147   twopass->kf_group_bits = MAX(0, twopass->kf_group_bits);
2148
2149   // Reset the first pass file position.
2150   reset_fpf_position(twopass, start_position);
2151
2152   // Scan through the kf group collating various stats used to determine
2153   // how many bits to spend on it.
2154   decay_accumulator = 1.0;
2155   boost_score = 0.0;
2156   for (i = 0; i < (rc->frames_to_key - 1); ++i) {
2157     if (EOF == input_stats(twopass, &next_frame))
2158       break;
2159
2160     // Monitor for static sections.
2161     zero_motion_accumulator =
2162       MIN(zero_motion_accumulator,
2163           get_zero_motion_factor(&cpi->common, &next_frame));
2164
2165     // Not all frames in the group are necessarily used in calculating boost.
2166     if ((i <= rc->max_gf_interval) ||
2167         ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) {
2168       const double frame_boost =
2169         calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST);
2170
2171       // How fast is prediction quality decaying.
2172       if (!detect_flash(twopass, 0)) {
2173         const double loop_decay_rate =
2174           get_prediction_decay_rate(&cpi->common, &next_frame);
2175         decay_accumulator *= loop_decay_rate;
2176         decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR);
2177         av_decay_accumulator += decay_accumulator;
2178         ++loop_decay_counter;
2179       }
2180       boost_score += (decay_accumulator * frame_boost);
2181     }
2182   }
2183   av_decay_accumulator /= (double)loop_decay_counter;
2184
2185   reset_fpf_position(twopass, start_position);
2186
2187   // Store the zero motion percentage
2188   twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2189
2190   // Calculate a section intra ratio used in setting max loop filter.
2191   twopass->section_intra_rating =
2192       calculate_section_intra_ratio(start_position, twopass->stats_in_end,
2193                                     rc->frames_to_key);
2194
2195   // Apply various clamps for min and max boost
2196   rc->kf_boost = (int)(av_decay_accumulator * boost_score);
2197   rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3));
2198   rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST);
2199
2200   // Work out how many bits to allocate for the key frame itself.
2201   kf_bits = calculate_boost_bits((rc->frames_to_key - 1),
2202                                   rc->kf_boost, twopass->kf_group_bits);
2203
2204   twopass->kf_group_bits -= kf_bits;
2205
2206   // Save the bits to spend on the key frame.
2207   gf_group->bit_allocation[0] = kf_bits;
2208   gf_group->update_type[0] = KF_UPDATE;
2209   gf_group->rf_level[0] = KF_STD;
2210
2211   // Note the total error score of the kf group minus the key frame itself.
2212   twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2213
2214   // Adjust the count of total modified error left.
2215   // The count of bits left is adjusted elsewhere based on real coded frame
2216   // sizes.
2217   twopass->modified_error_left -= kf_group_err;
2218 }
2219
2220 #define VBR_PCT_ADJUSTMENT_LIMIT 50
2221 // For VBR...adjustment to the frame target based on error from previous frames
2222 void vbr_rate_correction(VP9_COMP *cpi,
2223                          int * this_frame_target,
2224                          const int64_t vbr_bits_off_target) {
2225   int max_delta;
2226   double position_factor = 1.0;
2227
2228   // How far through the clip are we.
2229   // This number is used to damp the per frame rate correction.
2230   // Range 0 - 1.0
2231   if (cpi->twopass.total_stats.count) {
2232     position_factor = sqrt((double)cpi->common.current_video_frame /
2233                            cpi->twopass.total_stats.count);
2234   }
2235   max_delta = (int)(position_factor *
2236                     ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
2237
2238   // vbr_bits_off_target > 0 means we have extra bits to spend
2239   if (vbr_bits_off_target > 0) {
2240     *this_frame_target +=
2241       (vbr_bits_off_target > max_delta) ? max_delta
2242                                         : (int)vbr_bits_off_target;
2243   } else {
2244     *this_frame_target -=
2245       (vbr_bits_off_target < -max_delta) ? max_delta
2246                                          : (int)-vbr_bits_off_target;
2247   }
2248 }
2249
2250 // Define the reference buffers that will be updated post encode.
2251 void configure_buffer_updates(VP9_COMP *cpi) {
2252   TWO_PASS *const twopass = &cpi->twopass;
2253
2254   cpi->rc.is_src_frame_alt_ref = 0;
2255   switch (twopass->gf_group.update_type[twopass->gf_group.index]) {
2256     case KF_UPDATE:
2257       cpi->refresh_last_frame = 1;
2258       cpi->refresh_golden_frame = 1;
2259       cpi->refresh_alt_ref_frame = 1;
2260       break;
2261     case LF_UPDATE:
2262       cpi->refresh_last_frame = 1;
2263       cpi->refresh_golden_frame = 0;
2264       cpi->refresh_alt_ref_frame = 0;
2265       break;
2266     case GF_UPDATE:
2267       cpi->refresh_last_frame = 1;
2268       cpi->refresh_golden_frame = 1;
2269       cpi->refresh_alt_ref_frame = 0;
2270       break;
2271     case OVERLAY_UPDATE:
2272       cpi->refresh_last_frame = 0;
2273       cpi->refresh_golden_frame = 1;
2274       cpi->refresh_alt_ref_frame = 0;
2275       cpi->rc.is_src_frame_alt_ref = 1;
2276       break;
2277     case ARF_UPDATE:
2278       cpi->refresh_last_frame = 0;
2279       cpi->refresh_golden_frame = 0;
2280       cpi->refresh_alt_ref_frame = 1;
2281       break;
2282     default:
2283       assert(0);
2284       break;
2285   }
2286   if (is_two_pass_svc(cpi)) {
2287     if (cpi->svc.temporal_layer_id > 0) {
2288       cpi->refresh_last_frame = 0;
2289       cpi->refresh_golden_frame = 0;
2290     }
2291     if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0)
2292       cpi->refresh_golden_frame = 0;
2293     if (cpi->alt_ref_source == NULL)
2294       cpi->refresh_alt_ref_frame = 0;
2295   }
2296 }
2297
2298
2299 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2300   VP9_COMMON *const cm = &cpi->common;
2301   RATE_CONTROL *const rc = &cpi->rc;
2302   TWO_PASS *const twopass = &cpi->twopass;
2303   GF_GROUP *const gf_group = &twopass->gf_group;
2304   int frames_left;
2305   FIRSTPASS_STATS this_frame;
2306   FIRSTPASS_STATS this_frame_copy;
2307
2308   int target_rate;
2309   LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
2310         &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0;
2311
2312   if (lc != NULL) {
2313     frames_left = (int)(twopass->total_stats.count -
2314                   lc->current_video_frame_in_layer);
2315   } else {
2316     frames_left = (int)(twopass->total_stats.count -
2317                   cm->current_video_frame);
2318   }
2319
2320   if (!twopass->stats_in)
2321     return;
2322
2323   // If this is an arf frame then we dont want to read the stats file or
2324   // advance the input pointer as we already have what we need.
2325   if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2326     int target_rate;
2327     configure_buffer_updates(cpi);
2328     target_rate = gf_group->bit_allocation[gf_group->index];
2329     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2330     rc->base_frame_target = target_rate;
2331
2332     // Correction to rate target based on prior over or under shoot.
2333     if (cpi->oxcf.rc_mode == VPX_VBR)
2334       vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target);
2335
2336     vp9_rc_set_frame_target(cpi, target_rate);
2337     cm->frame_type = INTER_FRAME;
2338
2339     if (lc != NULL) {
2340       if (cpi->svc.spatial_layer_id == 0) {
2341         lc->is_key_frame = 0;
2342       } else {
2343         lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2344
2345         if (lc->is_key_frame)
2346           cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2347       }
2348     }
2349
2350     return;
2351   }
2352
2353   vp9_clear_system_state();
2354
2355   if (cpi->oxcf.rc_mode == VPX_Q) {
2356     twopass->active_worst_quality = cpi->oxcf.cq_level;
2357   } else if (cm->current_video_frame == 0 ||
2358              (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2359     // Special case code for first frame.
2360     const int section_target_bandwidth = (int)(twopass->bits_left /
2361                                                frames_left);
2362     const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats,
2363                                                 section_target_bandwidth);
2364     twopass->active_worst_quality = tmp_q;
2365     rc->ni_av_qi = tmp_q;
2366     rc->last_q[INTER_FRAME] = tmp_q;
2367     rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
2368     rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
2369     rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2;
2370     rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
2371   }
2372   vp9_zero(this_frame);
2373   if (EOF == input_stats(twopass, &this_frame))
2374     return;
2375
2376   // Local copy of the current frame's first pass stats.
2377   this_frame_copy = this_frame;
2378
2379   // Keyframe and section processing.
2380   if (rc->frames_to_key == 0 ||
2381       (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2382     // Define next KF group and assign bits to it.
2383     find_next_key_frame(cpi, &this_frame_copy);
2384   } else {
2385     cm->frame_type = INTER_FRAME;
2386   }
2387
2388   if (lc != NULL) {
2389     if (cpi->svc.spatial_layer_id == 0) {
2390       lc->is_key_frame = (cm->frame_type == KEY_FRAME);
2391       if (lc->is_key_frame) {
2392         cpi->ref_frame_flags &=
2393             (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG);
2394         lc->frames_from_key_frame = 0;
2395         // Reset the empty frame resolution since we have a key frame.
2396         cpi->svc.empty_frame_width = cm->width;
2397         cpi->svc.empty_frame_height = cm->height;
2398       }
2399     } else {
2400       cm->frame_type = INTER_FRAME;
2401       lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2402
2403       if (lc->is_key_frame) {
2404         cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2405         lc->frames_from_key_frame = 0;
2406       }
2407     }
2408   }
2409
2410   // Define a new GF/ARF group. (Should always enter here for key frames).
2411   if (rc->frames_till_gf_update_due == 0) {
2412     define_gf_group(cpi, &this_frame_copy);
2413
2414     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2415     if (lc != NULL)
2416       cpi->refresh_golden_frame = 1;
2417
2418 #if ARF_STATS_OUTPUT
2419     {
2420       FILE *fpfile;
2421       fpfile = fopen("arf.stt", "a");
2422       ++arf_count;
2423       fprintf(fpfile, "%10d %10ld %10d %10d %10ld\n",
2424               cm->current_video_frame, rc->frames_till_gf_update_due,
2425               rc->kf_boost, arf_count, rc->gfu_boost);
2426
2427       fclose(fpfile);
2428     }
2429 #endif
2430   }
2431
2432   configure_buffer_updates(cpi);
2433
2434   target_rate = gf_group->bit_allocation[gf_group->index];
2435   if (cpi->common.frame_type == KEY_FRAME)
2436     target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2437   else
2438     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2439
2440   rc->base_frame_target = target_rate;
2441
2442   // Correction to rate target based on prior over or under shoot.
2443   if (cpi->oxcf.rc_mode == VPX_VBR)
2444     vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target);
2445
2446   vp9_rc_set_frame_target(cpi, target_rate);
2447
2448   // Update the total stats remaining structure.
2449   subtract_stats(&twopass->total_left_stats, &this_frame);
2450 }
2451
2452 #define MINQ_ADJ_LIMIT 32
2453 #define Q_LIMIT_STEP 1
2454 void vp9_twopass_postencode_update(VP9_COMP *cpi) {
2455   TWO_PASS *const twopass = &cpi->twopass;
2456   RATE_CONTROL *const rc = &cpi->rc;
2457   const int bits_used = rc->base_frame_target;
2458
2459   // VBR correction is done through rc->vbr_bits_off_target. Based on the
2460   // sign of this value, a limited % adjustment is made to the target rate
2461   // of subsequent frames, to try and push it back towards 0. This method
2462   // is designed to prevent extreme behaviour at the end of a clip
2463   // or group of frames.
2464   rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
2465   twopass->bits_left = MAX(twopass->bits_left - bits_used, 0);
2466
2467   // Calculate the pct rc error.
2468   if (rc->total_actual_bits) {
2469     rc->rate_error_estimate =
2470       (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
2471     rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
2472   } else {
2473     rc->rate_error_estimate = 0;
2474   }
2475
2476   if (cpi->common.frame_type != KEY_FRAME &&
2477       !vp9_is_upper_layer_key_frame(cpi)) {
2478     twopass->kf_group_bits -= bits_used;
2479     twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
2480   }
2481   twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0);
2482
2483   // Increment the gf group index ready for the next frame.
2484   ++twopass->gf_group.index;
2485
2486   // If the rate control is drifting consider adjustment ot min or maxq.
2487   // Only make adjustments on gf/arf
2488   if ((cpi->oxcf.rc_mode == VPX_VBR) &&
2489       (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) &&
2490       !cpi->rc.is_src_frame_alt_ref) {
2491     const int maxq_adj_limit =
2492       rc->worst_quality - twopass->active_worst_quality;
2493
2494     // Undershoot.
2495     if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) {
2496       --twopass->extend_maxq;
2497       if (rc->rolling_target_bits >= rc->rolling_actual_bits)
2498         twopass->extend_minq += Q_LIMIT_STEP;
2499     // Overshoot.
2500     } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) {
2501       --twopass->extend_minq;
2502       if (rc->rolling_target_bits < rc->rolling_actual_bits)
2503         twopass->extend_maxq += Q_LIMIT_STEP;
2504     } else {
2505       if (rc->rolling_target_bits < rc->rolling_actual_bits)
2506         --twopass->extend_minq;
2507       if (rc->rolling_target_bits > rc->rolling_actual_bits)
2508         --twopass->extend_maxq;
2509     }
2510     twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT);
2511     twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit);
2512   }
2513 }