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