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