]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_firstpass.c
Removing output_framerate field from 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   // Calculate a minimum intra value to be used in determining the IIratio
985   // scores used in the second pass. We have this minimum to make sure
986   // that clips that are static but "low complexity" in the intra domain
987   // are still boosted appropriately for KF/GF/ARF.
988   if (!is_spatial_svc) {
989     // We don't know the number of MBs for each layer at this point.
990     // So we will do it later.
991     twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
992     twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
993   }
994
995   // This variable monitors how far behind the second ref update is lagging.
996   twopass->sr_update_lag = 1;
997
998   // Scan the first pass file and calculate an average Intra / Inter error
999   // score ratio for the sequence.
1000   {
1001     double sum_iiratio = 0.0;
1002     start_pos = twopass->stats_in;
1003
1004     while (input_stats(twopass, &this_frame) != EOF) {
1005       const double iiratio = this_frame.intra_error /
1006                                  DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1007       sum_iiratio += fclamp(iiratio, 1.0, 20.0);
1008     }
1009
1010     twopass->avg_iiratio = sum_iiratio /
1011         DOUBLE_DIVIDE_CHECK((double)twopass->total_stats.count);
1012
1013     reset_fpf_position(twopass, start_pos);
1014   }
1015
1016   // Scan the first pass file and calculate a modified total error based upon
1017   // the bias/power function used to allocate bits.
1018   {
1019     double av_error = twopass->total_stats.ssim_weighted_pred_err /
1020                       DOUBLE_DIVIDE_CHECK(twopass->total_stats.count);
1021
1022     start_pos = twopass->stats_in;
1023
1024     twopass->modified_error_total = 0.0;
1025     twopass->modified_error_min =
1026       (av_error * oxcf->two_pass_vbrmin_section) / 100;
1027     twopass->modified_error_max =
1028       (av_error * oxcf->two_pass_vbrmax_section) / 100;
1029
1030     while (input_stats(twopass, &this_frame) != EOF) {
1031       twopass->modified_error_total +=
1032           calculate_modified_err(cpi, &this_frame);
1033     }
1034     twopass->modified_error_left = twopass->modified_error_total;
1035
1036     reset_fpf_position(twopass, start_pos);
1037   }
1038 }
1039
1040 // This function gives an estimate of how badly we believe the prediction
1041 // quality is decaying from frame to frame.
1042 static double get_prediction_decay_rate(const VP9_COMMON *cm,
1043                                         const FIRSTPASS_STATS *next_frame) {
1044   // Look at the observed drop in prediction quality between the last frame
1045   // and the GF buffer (which contains an older frame).
1046   const double mb_sr_err_diff = (next_frame->sr_coded_error -
1047                                      next_frame->coded_error) / cm->MBs;
1048   const double second_ref_decay = mb_sr_err_diff <= 512.0
1049       ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0)
1050       : 0.85;
1051
1052   return MIN(second_ref_decay, next_frame->pcnt_inter);
1053 }
1054
1055 // Function to test for a condition where a complex transition is followed
1056 // by a static section. For example in slide shows where there is a fade
1057 // between slides. This is to help with more optimal kf and gf positioning.
1058 static int detect_transition_to_still(struct twopass_rc *twopass,
1059                                       int frame_interval, int still_interval,
1060                                       double loop_decay_rate,
1061                                       double last_decay_rate) {
1062   int trans_to_still = 0;
1063
1064   // Break clause to detect very still sections after motion
1065   // For example a static image after a fade or other transition
1066   // instead of a clean scene cut.
1067   if (frame_interval > MIN_GF_INTERVAL &&
1068       loop_decay_rate >= 0.999 &&
1069       last_decay_rate < 0.9) {
1070     int j;
1071     const FIRSTPASS_STATS *position = twopass->stats_in;
1072     FIRSTPASS_STATS tmp_next_frame;
1073
1074     // Look ahead a few frames to see if static condition persists...
1075     for (j = 0; j < still_interval; ++j) {
1076       if (EOF == input_stats(twopass, &tmp_next_frame))
1077         break;
1078
1079       if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999)
1080         break;
1081     }
1082
1083     reset_fpf_position(twopass, position);
1084
1085     // Only if it does do we signal a transition to still.
1086     if (j == still_interval)
1087       trans_to_still = 1;
1088   }
1089
1090   return trans_to_still;
1091 }
1092
1093 // This function detects a flash through the high relative pcnt_second_ref
1094 // score in the frame following a flash frame. The offset passed in should
1095 // reflect this.
1096 static int detect_flash(const struct twopass_rc *twopass, int offset) {
1097   FIRSTPASS_STATS next_frame;
1098
1099   int flash_detected = 0;
1100
1101   // Read the frame data.
1102   // The return is FALSE (no flash detected) if not a valid frame
1103   if (read_frame_stats(twopass, &next_frame, offset) != EOF) {
1104     // What we are looking for here is a situation where there is a
1105     // brief break in prediction (such as a flash) but subsequent frames
1106     // are reasonably well predicted by an earlier (pre flash) frame.
1107     // The recovery after a flash is indicated by a high pcnt_second_ref
1108     // compared to pcnt_inter.
1109     if (next_frame.pcnt_second_ref > next_frame.pcnt_inter &&
1110         next_frame.pcnt_second_ref >= 0.5)
1111       flash_detected = 1;
1112   }
1113
1114   return flash_detected;
1115 }
1116
1117 // Update the motion related elements to the GF arf boost calculation.
1118 static void accumulate_frame_motion_stats(
1119   FIRSTPASS_STATS *this_frame,
1120   double *this_frame_mv_in_out,
1121   double *mv_in_out_accumulator,
1122   double *abs_mv_in_out_accumulator,
1123   double *mv_ratio_accumulator) {
1124   double motion_pct;
1125
1126   // Accumulate motion stats.
1127   motion_pct = this_frame->pcnt_motion;
1128
1129   // Accumulate Motion In/Out of frame stats.
1130   *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1131   *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1132   *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct);
1133
1134   // Accumulate a measure of how uniform (or conversely how random)
1135   // the motion field is (a ratio of absmv / mv).
1136   if (motion_pct > 0.05) {
1137     const double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1138                            DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1139
1140     const double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1141                            DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1142
1143     *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs)
1144       ? (this_frame_mvr_ratio * motion_pct)
1145       : this_frame->mvr_abs * motion_pct;
1146
1147     *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs)
1148       ? (this_frame_mvc_ratio * motion_pct)
1149       : this_frame->mvc_abs * motion_pct;
1150   }
1151 }
1152
1153 // Calculate a baseline boost number for the current frame.
1154 static double calc_frame_boost(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame,
1155                                double this_frame_mv_in_out) {
1156   double frame_boost;
1157
1158   // Underlying boost factor is based on inter intra error ratio.
1159   if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1160     frame_boost = (IIFACTOR * this_frame->intra_error /
1161                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1162   else
1163     frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1164                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1165
1166   // Increase boost for frames where new data coming into frame (e.g. zoom out).
1167   // Slightly reduce boost if there is a net balance of motion out of the frame
1168   // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1169   if (this_frame_mv_in_out > 0.0)
1170     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1171   // In the extreme case the boost is halved.
1172   else
1173     frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1174
1175   return MIN(frame_boost, GF_RMAX);
1176 }
1177
1178 static int calc_arf_boost(VP9_COMP *cpi, int offset,
1179                           int f_frames, int b_frames,
1180                           int *f_boost, int *b_boost) {
1181   FIRSTPASS_STATS this_frame;
1182   struct twopass_rc *const twopass = &cpi->twopass;
1183   int i;
1184   double boost_score = 0.0;
1185   double mv_ratio_accumulator = 0.0;
1186   double decay_accumulator = 1.0;
1187   double this_frame_mv_in_out = 0.0;
1188   double mv_in_out_accumulator = 0.0;
1189   double abs_mv_in_out_accumulator = 0.0;
1190   int arf_boost;
1191   int flash_detected = 0;
1192
1193   // Search forward from the proposed arf/next gf position.
1194   for (i = 0; i < f_frames; ++i) {
1195     if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF)
1196       break;
1197
1198     // Update the motion related elements to the boost calculation.
1199     accumulate_frame_motion_stats(&this_frame,
1200                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
1201                                   &abs_mv_in_out_accumulator,
1202                                   &mv_ratio_accumulator);
1203
1204     // We want to discount the flash frame itself and the recovery
1205     // frame that follows as both will have poor scores.
1206     flash_detected = detect_flash(twopass, i + offset) ||
1207                      detect_flash(twopass, i + offset + 1);
1208
1209     // Accumulate the effect of prediction quality decay.
1210     if (!flash_detected) {
1211       decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame);
1212       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1213                           ? MIN_DECAY_FACTOR : decay_accumulator;
1214     }
1215
1216     boost_score += (decay_accumulator *
1217                     calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1218   }
1219
1220   *f_boost = (int)boost_score;
1221
1222   // Reset for backward looking loop.
1223   boost_score = 0.0;
1224   mv_ratio_accumulator = 0.0;
1225   decay_accumulator = 1.0;
1226   this_frame_mv_in_out = 0.0;
1227   mv_in_out_accumulator = 0.0;
1228   abs_mv_in_out_accumulator = 0.0;
1229
1230   // Search backward towards last gf position.
1231   for (i = -1; i >= -b_frames; --i) {
1232     if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF)
1233       break;
1234
1235     // Update the motion related elements to the boost calculation.
1236     accumulate_frame_motion_stats(&this_frame,
1237                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
1238                                   &abs_mv_in_out_accumulator,
1239                                   &mv_ratio_accumulator);
1240
1241     // We want to discount the the flash frame itself and the recovery
1242     // frame that follows as both will have poor scores.
1243     flash_detected = detect_flash(twopass, i + offset) ||
1244                      detect_flash(twopass, i + offset + 1);
1245
1246     // Cumulative effect of prediction quality decay.
1247     if (!flash_detected) {
1248       decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame);
1249       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1250                               ? MIN_DECAY_FACTOR : decay_accumulator;
1251     }
1252
1253     boost_score += (decay_accumulator *
1254                     calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1255   }
1256   *b_boost = (int)boost_score;
1257
1258   arf_boost = (*f_boost + *b_boost);
1259   if (arf_boost < ((b_frames + f_frames) * 20))
1260     arf_boost = ((b_frames + f_frames) * 20);
1261
1262   return arf_boost;
1263 }
1264
1265 #if CONFIG_MULTIPLE_ARF
1266 // Work out the frame coding order for a GF or an ARF group.
1267 // The current implementation codes frames in their natural order for a
1268 // GF group, and inserts additional ARFs into an ARF group using a
1269 // binary split approach.
1270 // NOTE: this function is currently implemented recursively.
1271 static void schedule_frames(VP9_COMP *cpi, const int start, const int end,
1272                             const int arf_idx, const int gf_or_arf_group,
1273                             const int level) {
1274   int i, abs_end, half_range;
1275   int *cfo = cpi->frame_coding_order;
1276   int idx = cpi->new_frame_coding_order_period;
1277
1278   // If (end < 0) an ARF should be coded at position (-end).
1279   assert(start >= 0);
1280
1281   // printf("start:%d end:%d\n", start, end);
1282
1283   // GF Group: code frames in logical order.
1284   if (gf_or_arf_group == 0) {
1285     assert(end >= start);
1286     for (i = start; i <= end; ++i) {
1287       cfo[idx] = i;
1288       cpi->arf_buffer_idx[idx] = arf_idx;
1289       cpi->arf_weight[idx] = -1;
1290       ++idx;
1291     }
1292     cpi->new_frame_coding_order_period = idx;
1293     return;
1294   }
1295
1296   // ARF Group: Work out the ARF schedule and mark ARF frames as negative.
1297   if (end < 0) {
1298     // printf("start:%d end:%d\n", -end, -end);
1299     // ARF frame is at the end of the range.
1300     cfo[idx] = end;
1301     // What ARF buffer does this ARF use as predictor.
1302     cpi->arf_buffer_idx[idx] = (arf_idx > 2) ? (arf_idx - 1) : 2;
1303     cpi->arf_weight[idx] = level;
1304     ++idx;
1305     abs_end = -end;
1306   } else {
1307     abs_end = end;
1308   }
1309
1310   half_range = (abs_end - start) >> 1;
1311
1312   // ARFs may not be adjacent, they must be separated by at least
1313   // MIN_GF_INTERVAL non-ARF frames.
1314   if ((start + MIN_GF_INTERVAL) >= (abs_end - MIN_GF_INTERVAL)) {
1315     // printf("start:%d end:%d\n", start, abs_end);
1316     // Update the coding order and active ARF.
1317     for (i = start; i <= abs_end; ++i) {
1318       cfo[idx] = i;
1319       cpi->arf_buffer_idx[idx] = arf_idx;
1320       cpi->arf_weight[idx] = -1;
1321       ++idx;
1322     }
1323     cpi->new_frame_coding_order_period = idx;
1324   } else {
1325     // Place a new ARF at the mid-point of the range.
1326     cpi->new_frame_coding_order_period = idx;
1327     schedule_frames(cpi, start, -(start + half_range), arf_idx + 1,
1328                     gf_or_arf_group, level + 1);
1329     schedule_frames(cpi, start + half_range + 1, abs_end, arf_idx,
1330                     gf_or_arf_group, level + 1);
1331   }
1332 }
1333
1334 #define FIXED_ARF_GROUP_SIZE 16
1335
1336 void define_fixed_arf_period(VP9_COMP *cpi) {
1337   int i;
1338   int max_level = INT_MIN;
1339
1340   assert(cpi->multi_arf_enabled);
1341   assert(cpi->oxcf.lag_in_frames >= FIXED_ARF_GROUP_SIZE);
1342
1343   // Save the weight of the last frame in the sequence before next
1344   // sequence pattern overwrites it.
1345   cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
1346   assert(cpi->this_frame_weight >= 0);
1347
1348   cpi->twopass.gf_zeromotion_pct = 0;
1349
1350   // Initialize frame coding order variables.
1351   cpi->new_frame_coding_order_period = 0;
1352   cpi->next_frame_in_order = 0;
1353   cpi->arf_buffered = 0;
1354   vp9_zero(cpi->frame_coding_order);
1355   vp9_zero(cpi->arf_buffer_idx);
1356   vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
1357
1358   if (cpi->rc.frames_to_key <= (FIXED_ARF_GROUP_SIZE + 8)) {
1359     // Setup a GF group close to the keyframe.
1360     cpi->rc.source_alt_ref_pending = 0;
1361     cpi->rc.baseline_gf_interval = cpi->rc.frames_to_key;
1362     schedule_frames(cpi, 0, (cpi->rc.baseline_gf_interval - 1), 2, 0, 0);
1363   } else {
1364     // Setup a fixed period ARF group.
1365     cpi->rc.source_alt_ref_pending = 1;
1366     cpi->rc.baseline_gf_interval = FIXED_ARF_GROUP_SIZE;
1367     schedule_frames(cpi, 0, -(cpi->rc.baseline_gf_interval - 1), 2, 1, 0);
1368   }
1369
1370   // Replace level indicator of -1 with correct level.
1371   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1372     if (cpi->arf_weight[i] > max_level) {
1373       max_level = cpi->arf_weight[i];
1374     }
1375   }
1376   ++max_level;
1377   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1378     if (cpi->arf_weight[i] == -1) {
1379       cpi->arf_weight[i] = max_level;
1380     }
1381   }
1382   cpi->max_arf_level = max_level;
1383 #if 0
1384   printf("\nSchedule: ");
1385   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1386     printf("%4d ", cpi->frame_coding_order[i]);
1387   }
1388   printf("\n");
1389   printf("ARFref:   ");
1390   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1391     printf("%4d ", cpi->arf_buffer_idx[i]);
1392   }
1393   printf("\n");
1394   printf("Weight:   ");
1395   for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1396     printf("%4d ", cpi->arf_weight[i]);
1397   }
1398   printf("\n");
1399 #endif
1400 }
1401 #endif
1402
1403 // Analyse and define a gf/arf group.
1404 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1405   RATE_CONTROL *const rc = &cpi->rc;
1406   VP9_CONFIG *const oxcf = &cpi->oxcf;
1407   struct twopass_rc *const twopass = &cpi->twopass;
1408   FIRSTPASS_STATS next_frame = { 0 };
1409   const FIRSTPASS_STATS *start_pos;
1410   int i;
1411   double boost_score = 0.0;
1412   double old_boost_score = 0.0;
1413   double gf_group_err = 0.0;
1414   double gf_first_frame_err = 0.0;
1415   double mod_frame_err = 0.0;
1416
1417   double mv_ratio_accumulator = 0.0;
1418   double decay_accumulator = 1.0;
1419   double zero_motion_accumulator = 1.0;
1420
1421   double loop_decay_rate = 1.00;
1422   double last_loop_decay_rate = 1.00;
1423
1424   double this_frame_mv_in_out = 0.0;
1425   double mv_in_out_accumulator = 0.0;
1426   double abs_mv_in_out_accumulator = 0.0;
1427   double mv_ratio_accumulator_thresh;
1428   // Max bits for a single frame.
1429   const int max_bits = frame_max_bits(rc, oxcf);
1430   unsigned int allow_alt_ref = oxcf->play_alternate && oxcf->lag_in_frames;
1431
1432   int f_boost = 0;
1433   int b_boost = 0;
1434   int flash_detected;
1435   int active_max_gf_interval;
1436
1437   twopass->gf_group_bits = 0;
1438
1439   vp9_clear_system_state();
1440
1441   start_pos = twopass->stats_in;
1442
1443   // Load stats for the current frame.
1444   mod_frame_err = calculate_modified_err(cpi, this_frame);
1445
1446   // Note the error of the frame at the start of the group. This will be
1447   // the GF frame error if we code a normal gf.
1448   gf_first_frame_err = mod_frame_err;
1449
1450   // If this is a key frame or the overlay from a previous arf then
1451   // the error score / cost of this frame has already been accounted for.
1452   if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1453     gf_group_err -= gf_first_frame_err;
1454
1455   // Motion breakout threshold for loop below depends on image size.
1456   mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0;
1457
1458   // Work out a maximum interval for the GF.
1459   // If the image appears completely static we can extend beyond this.
1460   // The value chosen depends on the active Q range. At low Q we have
1461   // bits to spare and are better with a smaller interval and smaller boost.
1462   // At high Q when there are few bits to spare we are better with a longer
1463   // interval to spread the cost of the GF.
1464   //
1465   active_max_gf_interval =
1466     12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5);
1467
1468   if (active_max_gf_interval > rc->max_gf_interval)
1469     active_max_gf_interval = rc->max_gf_interval;
1470
1471   i = 0;
1472   while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
1473     ++i;
1474
1475     // Accumulate error score of frames in this gf group.
1476     mod_frame_err = calculate_modified_err(cpi, this_frame);
1477     gf_group_err += mod_frame_err;
1478
1479     if (EOF == input_stats(twopass, &next_frame))
1480       break;
1481
1482     // Test for the case where there is a brief flash but the prediction
1483     // quality back to an earlier frame is then restored.
1484     flash_detected = detect_flash(twopass, 0);
1485
1486     // Update the motion related elements to the boost calculation.
1487     accumulate_frame_motion_stats(&next_frame,
1488                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
1489                                   &abs_mv_in_out_accumulator,
1490                                   &mv_ratio_accumulator);
1491
1492     // Accumulate the effect of prediction quality decay.
1493     if (!flash_detected) {
1494       last_loop_decay_rate = loop_decay_rate;
1495       loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
1496       decay_accumulator = decay_accumulator * loop_decay_rate;
1497
1498       // Monitor for static sections.
1499       if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
1500           zero_motion_accumulator) {
1501         zero_motion_accumulator = next_frame.pcnt_inter -
1502                                       next_frame.pcnt_motion;
1503       }
1504
1505       // Break clause to detect very still sections after motion. For example,
1506       // a static image after a fade or other transition.
1507       if (detect_transition_to_still(twopass, i, 5, loop_decay_rate,
1508                                      last_loop_decay_rate)) {
1509         allow_alt_ref = 0;
1510         break;
1511       }
1512     }
1513
1514     // Calculate a boost number for this frame.
1515     boost_score += (decay_accumulator *
1516        calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out));
1517
1518     // Break out conditions.
1519     if (
1520       // Break at cpi->max_gf_interval unless almost totally static.
1521       (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
1522       (
1523         // Don't break out with a very short interval.
1524         (i > MIN_GF_INTERVAL) &&
1525         ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) &&
1526         (!flash_detected) &&
1527         ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
1528          (abs_mv_in_out_accumulator > 3.0) ||
1529          (mv_in_out_accumulator < -2.0) ||
1530          ((boost_score - old_boost_score) < IIFACTOR)))) {
1531       boost_score = old_boost_score;
1532       break;
1533     }
1534
1535     *this_frame = next_frame;
1536
1537     old_boost_score = boost_score;
1538   }
1539
1540   twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
1541
1542   // Don't allow a gf too near the next kf.
1543   if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) {
1544     while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) {
1545       ++i;
1546
1547       if (EOF == input_stats(twopass, this_frame))
1548         break;
1549
1550       if (i < rc->frames_to_key) {
1551         mod_frame_err = calculate_modified_err(cpi, this_frame);
1552         gf_group_err += mod_frame_err;
1553       }
1554     }
1555   }
1556
1557 #if CONFIG_MULTIPLE_ARF
1558   if (cpi->multi_arf_enabled) {
1559     // Initialize frame coding order variables.
1560     cpi->new_frame_coding_order_period = 0;
1561     cpi->next_frame_in_order = 0;
1562     cpi->arf_buffered = 0;
1563     vp9_zero(cpi->frame_coding_order);
1564     vp9_zero(cpi->arf_buffer_idx);
1565     vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight));
1566   }
1567 #endif
1568
1569   // Set the interval until the next gf.
1570   if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1571     rc->baseline_gf_interval = i - 1;
1572   else
1573     rc->baseline_gf_interval = i;
1574
1575   // Should we use the alternate reference frame.
1576   if (allow_alt_ref &&
1577       (i < cpi->oxcf.lag_in_frames) &&
1578       (i >= MIN_GF_INTERVAL) &&
1579       // For real scene cuts (not forced kfs) don't allow arf very near kf.
1580       (rc->next_key_frame_forced ||
1581       (i <= (rc->frames_to_key - MIN_GF_INTERVAL)))) {
1582     // Calculate the boost for alt ref.
1583     rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
1584                                    &b_boost);
1585     rc->source_alt_ref_pending = 1;
1586
1587 #if CONFIG_MULTIPLE_ARF
1588     // Set the ARF schedule.
1589     if (cpi->multi_arf_enabled) {
1590       schedule_frames(cpi, 0, -(rc->baseline_gf_interval - 1), 2, 1, 0);
1591     }
1592 #endif
1593   } else {
1594     rc->gfu_boost = (int)boost_score;
1595     rc->source_alt_ref_pending = 0;
1596 #if CONFIG_MULTIPLE_ARF
1597     // Set the GF schedule.
1598     if (cpi->multi_arf_enabled) {
1599       schedule_frames(cpi, 0, rc->baseline_gf_interval - 1, 2, 0, 0);
1600       assert(cpi->new_frame_coding_order_period ==
1601              rc->baseline_gf_interval);
1602     }
1603 #endif
1604   }
1605
1606 #if CONFIG_MULTIPLE_ARF
1607   if (cpi->multi_arf_enabled && (cpi->common.frame_type != KEY_FRAME)) {
1608     int max_level = INT_MIN;
1609     // Replace level indicator of -1 with correct level.
1610     for (i = 0; i < cpi->frame_coding_order_period; ++i) {
1611       if (cpi->arf_weight[i] > max_level) {
1612         max_level = cpi->arf_weight[i];
1613       }
1614     }
1615     ++max_level;
1616     for (i = 0; i < cpi->frame_coding_order_period; ++i) {
1617       if (cpi->arf_weight[i] == -1) {
1618         cpi->arf_weight[i] = max_level;
1619       }
1620     }
1621     cpi->max_arf_level = max_level;
1622   }
1623 #if 0
1624   if (cpi->multi_arf_enabled) {
1625     printf("\nSchedule: ");
1626     for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1627       printf("%4d ", cpi->frame_coding_order[i]);
1628     }
1629     printf("\n");
1630     printf("ARFref:   ");
1631     for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1632       printf("%4d ", cpi->arf_buffer_idx[i]);
1633     }
1634     printf("\n");
1635     printf("Weight:   ");
1636     for (i = 0; i < cpi->new_frame_coding_order_period; ++i) {
1637       printf("%4d ", cpi->arf_weight[i]);
1638     }
1639     printf("\n");
1640   }
1641 #endif
1642 #endif
1643
1644   // Calculate the bits to be allocated to the group as a whole.
1645   if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) {
1646     twopass->gf_group_bits = (int64_t)(twopass->kf_group_bits *
1647                 (gf_group_err / twopass->kf_group_error_left));
1648   } else {
1649     twopass->gf_group_bits = 0;
1650   }
1651   twopass->gf_group_bits = (twopass->gf_group_bits < 0) ?
1652      0 : (twopass->gf_group_bits > twopass->kf_group_bits) ?
1653      twopass->kf_group_bits : twopass->gf_group_bits;
1654
1655   // Clip cpi->twopass.gf_group_bits based on user supplied data rate
1656   // variability limit, cpi->oxcf.two_pass_vbrmax_section.
1657   if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval)
1658     twopass->gf_group_bits = (int64_t)max_bits * rc->baseline_gf_interval;
1659
1660   // Reset the file position.
1661   reset_fpf_position(twopass, start_pos);
1662
1663   // Assign  bits to the arf or gf.
1664   for (i = 0; i <= (rc->source_alt_ref_pending &&
1665                     cpi->common.frame_type != KEY_FRAME); ++i) {
1666     int allocation_chunks;
1667     int q = rc->last_q[INTER_FRAME];
1668     int gf_bits;
1669
1670     int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100;
1671
1672     // Set max and minimum boost and hence minimum allocation.
1673     boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200);
1674
1675     if (rc->source_alt_ref_pending && i == 0)
1676       allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost;
1677     else
1678       allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100);
1679
1680     // Prevent overflow.
1681     if (boost > 1023) {
1682       int divisor = boost >> 10;
1683       boost /= divisor;
1684       allocation_chunks /= divisor;
1685     }
1686
1687     // Calculate the number of bits to be spent on the gf or arf based on
1688     // the boost number.
1689     gf_bits = (int)((double)boost * (twopass->gf_group_bits /
1690                   (double)allocation_chunks));
1691
1692     // If the frame that is to be boosted is simpler than the average for
1693     // the gf/arf group then use an alternative calculation
1694     // based on the error score of the frame itself.
1695     if (rc->baseline_gf_interval < 1 ||
1696         mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) {
1697       double alt_gf_grp_bits = (double)twopass->kf_group_bits  *
1698         (mod_frame_err * (double)rc->baseline_gf_interval) /
1699         DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left);
1700
1701       int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits /
1702                                            (double)allocation_chunks));
1703
1704       if (gf_bits > alt_gf_bits)
1705         gf_bits = alt_gf_bits;
1706     } else {
1707       // If it is harder than other frames in the group make sure it at
1708       // least receives an allocation in keeping with its relative error
1709       // score, otherwise it may be worse off than an "un-boosted" frame.
1710       int alt_gf_bits = (int)((double)twopass->kf_group_bits *
1711                         mod_frame_err /
1712                         DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left));
1713
1714       if (alt_gf_bits > gf_bits)
1715         gf_bits = alt_gf_bits;
1716     }
1717
1718     // Don't allow a negative value for gf_bits.
1719     if (gf_bits < 0)
1720       gf_bits = 0;
1721
1722     if (i == 0) {
1723       twopass->gf_bits = gf_bits;
1724     }
1725     if (i == 1 ||
1726         (!rc->source_alt_ref_pending &&
1727          cpi->common.frame_type != KEY_FRAME)) {
1728       // Calculate the per frame bit target for this frame.
1729       vp9_rc_set_frame_target(cpi, gf_bits);
1730     }
1731   }
1732
1733   {
1734     // Adjust KF group bits and error remaining.
1735     twopass->kf_group_error_left -= (int64_t)gf_group_err;
1736
1737     // If this is an arf update we want to remove the score for the overlay
1738     // frame at the end which will usually be very cheap to code.
1739     // The overlay frame has already, in effect, been coded so we want to spread
1740     // the remaining bits among the other frames.
1741     // For normal GFs remove the score for the GF itself unless this is
1742     // also a key frame in which case it has already been accounted for.
1743     if (rc->source_alt_ref_pending) {
1744       twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err);
1745     } else if (cpi->common.frame_type != KEY_FRAME) {
1746       twopass->gf_group_error_left = (int64_t)(gf_group_err
1747                                                    - gf_first_frame_err);
1748     } else {
1749       twopass->gf_group_error_left = (int64_t)gf_group_err;
1750     }
1751
1752     // This condition could fail if there are two kfs very close together
1753     // despite MIN_GF_INTERVAL and would cause a divide by 0 in the
1754     // calculation of alt_extra_bits.
1755     if (rc->baseline_gf_interval >= 3) {
1756       const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost;
1757
1758       if (boost >= 150) {
1759         const int pct_extra = MIN(20, (boost - 100) / 50);
1760         const int alt_extra_bits = (int)((
1761             MAX(twopass->gf_group_bits - twopass->gf_bits, 0) *
1762             pct_extra) / 100);
1763         twopass->gf_group_bits -= alt_extra_bits;
1764       }
1765     }
1766   }
1767
1768   if (cpi->common.frame_type != KEY_FRAME) {
1769     FIRSTPASS_STATS sectionstats;
1770
1771     zero_stats(&sectionstats);
1772     reset_fpf_position(twopass, start_pos);
1773
1774     for (i = 0; i < rc->baseline_gf_interval; ++i) {
1775       input_stats(twopass, &next_frame);
1776       accumulate_stats(&sectionstats, &next_frame);
1777     }
1778
1779     avg_stats(&sectionstats);
1780
1781     twopass->section_intra_rating = (int)
1782       (sectionstats.intra_error /
1783       DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
1784
1785     reset_fpf_position(twopass, start_pos);
1786   }
1787 }
1788
1789 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1790 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1791   struct twopass_rc *twopass = &cpi->twopass;
1792   // For a single frame.
1793   const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf);
1794   // Calculate modified prediction error used in bit allocation.
1795   const double modified_err = calculate_modified_err(cpi, this_frame);
1796   int target_frame_size;
1797   double err_fraction;
1798
1799   if (twopass->gf_group_error_left > 0)
1800     // What portion of the remaining GF group error is used by this frame.
1801     err_fraction = modified_err / twopass->gf_group_error_left;
1802   else
1803     err_fraction = 0.0;
1804
1805   // How many of those bits available for allocation should we give it?
1806   target_frame_size = (int)((double)twopass->gf_group_bits * err_fraction);
1807
1808   // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at
1809   // the top end.
1810   target_frame_size = clamp(target_frame_size, 0,
1811                             MIN(max_bits, (int)twopass->gf_group_bits));
1812
1813   // Adjust error and bits remaining.
1814   twopass->gf_group_error_left -= (int64_t)modified_err;
1815
1816   // Per frame bit target for this frame.
1817   vp9_rc_set_frame_target(cpi, target_frame_size);
1818 }
1819
1820 static int test_candidate_kf(struct twopass_rc *twopass,
1821                              const FIRSTPASS_STATS *last_frame,
1822                              const FIRSTPASS_STATS *this_frame,
1823                              const FIRSTPASS_STATS *next_frame) {
1824   int is_viable_kf = 0;
1825
1826   // Does the frame satisfy the primary criteria of a key frame?
1827   // If so, then examine how well it predicts subsequent frames.
1828   if ((this_frame->pcnt_second_ref < 0.10) &&
1829       (next_frame->pcnt_second_ref < 0.10) &&
1830       ((this_frame->pcnt_inter < 0.05) ||
1831        (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) &&
1832         ((this_frame->intra_error /
1833           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
1834         ((fabs(last_frame->coded_error - this_frame->coded_error) /
1835               DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) ||
1836          (fabs(last_frame->intra_error - this_frame->intra_error) /
1837               DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) ||
1838          ((next_frame->intra_error /
1839            DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
1840     int i;
1841     const FIRSTPASS_STATS *start_pos = twopass->stats_in;
1842     FIRSTPASS_STATS local_next_frame = *next_frame;
1843     double boost_score = 0.0;
1844     double old_boost_score = 0.0;
1845     double decay_accumulator = 1.0;
1846
1847     // Examine how well the key frame predicts subsequent frames.
1848     for (i = 0; i < 16; ++i) {
1849       double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
1850                              DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
1851
1852       if (next_iiratio > RMAX)
1853         next_iiratio = RMAX;
1854
1855       // Cumulative effect of decay in prediction quality.
1856       if (local_next_frame.pcnt_inter > 0.85)
1857         decay_accumulator *= local_next_frame.pcnt_inter;
1858       else
1859         decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
1860
1861       // Keep a running total.
1862       boost_score += (decay_accumulator * next_iiratio);
1863
1864       // Test various breakout clauses.
1865       if ((local_next_frame.pcnt_inter < 0.05) ||
1866           (next_iiratio < 1.5) ||
1867           (((local_next_frame.pcnt_inter -
1868              local_next_frame.pcnt_neutral) < 0.20) &&
1869            (next_iiratio < 3.0)) ||
1870           ((boost_score - old_boost_score) < 3.0) ||
1871           (local_next_frame.intra_error < 200)) {
1872         break;
1873       }
1874
1875       old_boost_score = boost_score;
1876
1877       // Get the next frame details
1878       if (EOF == input_stats(twopass, &local_next_frame))
1879         break;
1880     }
1881
1882     // If there is tolerable prediction for at least the next 3 frames then
1883     // break out else discard this potential key frame and move on
1884     if (boost_score > 30.0 && (i > 3)) {
1885       is_viable_kf = 1;
1886     } else {
1887       // Reset the file position
1888       reset_fpf_position(twopass, start_pos);
1889
1890       is_viable_kf = 0;
1891     }
1892   }
1893
1894   return is_viable_kf;
1895 }
1896
1897 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1898   int i, j;
1899   RATE_CONTROL *const rc = &cpi->rc;
1900   struct twopass_rc *const twopass = &cpi->twopass;
1901   const FIRSTPASS_STATS first_frame = *this_frame;
1902   const FIRSTPASS_STATS *start_position = twopass->stats_in;
1903   FIRSTPASS_STATS next_frame;
1904   FIRSTPASS_STATS last_frame;
1905   double decay_accumulator = 1.0;
1906   double zero_motion_accumulator = 1.0;
1907   double boost_score = 0.0;
1908   double kf_mod_err = 0.0;
1909   double kf_group_err = 0.0;
1910   double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
1911
1912   vp9_zero(next_frame);
1913
1914   cpi->common.frame_type = KEY_FRAME;
1915
1916   // Is this a forced key frame by interval.
1917   rc->this_key_frame_forced = rc->next_key_frame_forced;
1918
1919   // Clear the alt ref active flag as this can never be active on a key frame.
1920   rc->source_alt_ref_active = 0;
1921
1922   // KF is always a GF so clear frames till next gf counter.
1923   rc->frames_till_gf_update_due = 0;
1924
1925   rc->frames_to_key = 1;
1926
1927   twopass->kf_group_bits = 0;        // Total bits available to kf group
1928   twopass->kf_group_error_left = 0;  // Group modified error score.
1929
1930   kf_mod_err = calculate_modified_err(cpi, this_frame);
1931
1932   // Find the next keyframe.
1933   i = 0;
1934   while (twopass->stats_in < twopass->stats_in_end) {
1935     // Accumulate kf group error.
1936     kf_group_err += calculate_modified_err(cpi, this_frame);
1937
1938     // Load the next frame's stats.
1939     last_frame = *this_frame;
1940     input_stats(twopass, this_frame);
1941
1942     // Provided that we are not at the end of the file...
1943     if (cpi->oxcf.auto_key &&
1944         lookup_next_frame_stats(twopass, &next_frame) != EOF) {
1945       double loop_decay_rate;
1946
1947       // Check for a scene cut.
1948       if (test_candidate_kf(twopass, &last_frame, this_frame, &next_frame))
1949         break;
1950
1951       // How fast is the prediction quality decaying?
1952       loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
1953
1954       // We want to know something about the recent past... rather than
1955       // as used elsewhere where we are concerned with decay in prediction
1956       // quality since the last GF or KF.
1957       recent_loop_decay[i % 8] = loop_decay_rate;
1958       decay_accumulator = 1.0;
1959       for (j = 0; j < 8; ++j)
1960         decay_accumulator *= recent_loop_decay[j];
1961
1962       // Special check for transition or high motion followed by a
1963       // static scene.
1964       if (detect_transition_to_still(twopass, i, cpi->key_frame_frequency - i,
1965                                      loop_decay_rate, decay_accumulator))
1966         break;
1967
1968       // Step on to the next frame.
1969       ++rc->frames_to_key;
1970
1971       // If we don't have a real key frame within the next two
1972       // key_frame_frequency intervals then break out of the loop.
1973       if (rc->frames_to_key >= 2 * (int)cpi->key_frame_frequency)
1974         break;
1975     } else {
1976       ++rc->frames_to_key;
1977     }
1978     ++i;
1979   }
1980
1981   // If there is a max kf interval set by the user we must obey it.
1982   // We already breakout of the loop above at 2x max.
1983   // This code centers the extra kf if the actual natural interval
1984   // is between 1x and 2x.
1985   if (cpi->oxcf.auto_key &&
1986       rc->frames_to_key > (int)cpi->key_frame_frequency) {
1987     FIRSTPASS_STATS tmp_frame = first_frame;
1988
1989     rc->frames_to_key /= 2;
1990
1991     // Reset to the start of the group.
1992     reset_fpf_position(twopass, start_position);
1993
1994     kf_group_err = 0;
1995
1996     // Rescan to get the correct error data for the forced kf group.
1997     for (i = 0; i < rc->frames_to_key; ++i) {
1998       kf_group_err += calculate_modified_err(cpi, &tmp_frame);
1999       input_stats(twopass, &tmp_frame);
2000     }
2001     rc->next_key_frame_forced = 1;
2002   } else if (twopass->stats_in == twopass->stats_in_end) {
2003     rc->next_key_frame_forced = 1;
2004   } else {
2005     rc->next_key_frame_forced = 0;
2006   }
2007
2008   // Special case for the last key frame of the file.
2009   if (twopass->stats_in >= twopass->stats_in_end) {
2010     // Accumulate kf group error.
2011     kf_group_err += calculate_modified_err(cpi, this_frame);
2012   }
2013
2014   // Calculate the number of bits that should be assigned to the kf group.
2015   if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) {
2016     // Maximum number of bits for a single normal frame (not key frame).
2017     const int max_bits = frame_max_bits(rc, &cpi->oxcf);
2018
2019     // Maximum number of bits allocated to the key frame group.
2020     int64_t max_grp_bits;
2021
2022     // Default allocation based on bits left and relative
2023     // complexity of the section.
2024     twopass->kf_group_bits = (int64_t)(twopass->bits_left *
2025        (kf_group_err / twopass->modified_error_left));
2026
2027     // Clip based on maximum per frame rate defined by the user.
2028     max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
2029     if (twopass->kf_group_bits > max_grp_bits)
2030       twopass->kf_group_bits = max_grp_bits;
2031   } else {
2032     twopass->kf_group_bits = 0;
2033   }
2034   // Reset the first pass file position.
2035   reset_fpf_position(twopass, start_position);
2036
2037   // Determine how big to make this keyframe based on how well the subsequent
2038   // frames use inter blocks.
2039   decay_accumulator = 1.0;
2040   boost_score = 0.0;
2041
2042   // Scan through the kf group collating various stats.
2043   for (i = 0; i < rc->frames_to_key; ++i) {
2044     if (EOF == input_stats(twopass, &next_frame))
2045       break;
2046
2047     // Monitor for static sections.
2048     if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
2049             zero_motion_accumulator) {
2050       zero_motion_accumulator = (next_frame.pcnt_inter -
2051                                      next_frame.pcnt_motion);
2052     }
2053
2054     // For the first few frames collect data to decide kf boost.
2055     if (i <= (rc->max_gf_interval * 2)) {
2056       double r;
2057       if (next_frame.intra_error > twopass->kf_intra_err_min)
2058         r = (IIKFACTOR2 * next_frame.intra_error /
2059              DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2060       else
2061         r = (IIKFACTOR2 * twopass->kf_intra_err_min /
2062              DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2063
2064       if (r > RMAX)
2065         r = RMAX;
2066
2067       // How fast is prediction quality decaying.
2068       if (!detect_flash(twopass, 0)) {
2069         const double loop_decay_rate = get_prediction_decay_rate(&cpi->common,
2070                                                                  &next_frame);
2071         decay_accumulator *= loop_decay_rate;
2072         decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR);
2073       }
2074
2075       boost_score += (decay_accumulator * r);
2076     }
2077   }
2078
2079   {
2080     FIRSTPASS_STATS sectionstats;
2081
2082     zero_stats(&sectionstats);
2083     reset_fpf_position(twopass, start_position);
2084
2085     for (i = 0; i < rc->frames_to_key; ++i) {
2086       input_stats(twopass, &next_frame);
2087       accumulate_stats(&sectionstats, &next_frame);
2088     }
2089
2090     avg_stats(&sectionstats);
2091
2092     twopass->section_intra_rating = (int) (sectionstats.intra_error /
2093         DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2094   }
2095
2096   // Reset the first pass file position.
2097   reset_fpf_position(twopass, start_position);
2098
2099   // Work out how many bits to allocate for the key frame itself.
2100   if (1) {
2101     int kf_boost = (int)boost_score;
2102     int allocation_chunks;
2103
2104     if (kf_boost < (rc->frames_to_key * 3))
2105       kf_boost = (rc->frames_to_key * 3);
2106
2107     if (kf_boost < MIN_KF_BOOST)
2108       kf_boost = MIN_KF_BOOST;
2109
2110     // Make a note of baseline boost and the zero motion
2111     // accumulator value for use elsewhere.
2112     rc->kf_boost = kf_boost;
2113     twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2114
2115     // Key frame size depends on:
2116     // (1) the error score for the whole key frame group,
2117     // (2) the key frames' own error if this is smaller than the
2118     //     average for the group (optional),
2119     // (3) insuring that the frame receives at least the allocation it would
2120     //     have received based on its own error score vs the error score
2121     //     remaining.
2122     // Special case:
2123     // If the sequence appears almost totally static we want to spend almost
2124     // all of the bits on the key frame.
2125     //
2126     // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is
2127     // taken care of by kf_boost.
2128     if (zero_motion_accumulator >= 0.99) {
2129       allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost;
2130     } else {
2131       allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost;
2132     }
2133
2134     // Prevent overflow.
2135     if (kf_boost > 1028) {
2136       const int divisor = kf_boost >> 10;
2137       kf_boost /= divisor;
2138       allocation_chunks /= divisor;
2139     }
2140
2141     twopass->kf_group_bits = MAX(0, twopass->kf_group_bits);
2142     // Calculate the number of bits to be spent on the key frame.
2143     twopass->kf_bits = (int)((double)kf_boost *
2144         ((double)twopass->kf_group_bits / allocation_chunks));
2145
2146     // If the key frame is actually easier than the average for the
2147     // kf group (which does sometimes happen, e.g. a blank intro frame)
2148     // then use an alternate calculation based on the kf error score
2149     // which should give a smaller key frame.
2150     if (kf_mod_err < kf_group_err / rc->frames_to_key) {
2151       double alt_kf_grp_bits = ((double)twopass->bits_left *
2152          (kf_mod_err * (double)rc->frames_to_key) /
2153          DOUBLE_DIVIDE_CHECK(twopass->modified_error_left));
2154
2155       const int alt_kf_bits = (int)((double)kf_boost *
2156                           (alt_kf_grp_bits / (double)allocation_chunks));
2157
2158       if (twopass->kf_bits > alt_kf_bits)
2159         twopass->kf_bits = alt_kf_bits;
2160     } else {
2161       // Else if it is much harder than other frames in the group make sure
2162       // it at least receives an allocation in keeping with its relative
2163       // error score.
2164       const int alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err /
2165                DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)));
2166
2167       if (alt_kf_bits > twopass->kf_bits)
2168         twopass->kf_bits = alt_kf_bits;
2169     }
2170     twopass->kf_group_bits -= twopass->kf_bits;
2171     // Per frame bit target for this frame.
2172     vp9_rc_set_frame_target(cpi, twopass->kf_bits);
2173   }
2174
2175   // Note the total error score of the kf group minus the key frame itself.
2176   twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2177
2178   // Adjust the count of total modified error left.
2179   // The count of bits left is adjusted elsewhere based on real coded frame
2180   // sizes.
2181   twopass->modified_error_left -= kf_group_err;
2182 }
2183
2184 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) {
2185   VP9_COMMON *const cm = &cpi->common;
2186   if (!cpi->refresh_alt_ref_frame &&
2187       (cm->current_video_frame == 0 ||
2188        (cm->frame_flags & FRAMEFLAGS_KEY))) {
2189     cm->frame_type = KEY_FRAME;
2190   } else {
2191     cm->frame_type = INTER_FRAME;
2192   }
2193   // Do not use periodic key frames.
2194   cpi->rc.frames_to_key = INT_MAX;
2195 }
2196
2197 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2198   VP9_COMMON *const cm = &cpi->common;
2199   RATE_CONTROL *const rc = &cpi->rc;
2200   struct twopass_rc *const twopass = &cpi->twopass;
2201   int frames_left;
2202   FIRSTPASS_STATS this_frame;
2203   FIRSTPASS_STATS this_frame_copy;
2204
2205   double this_frame_intra_error;
2206   double this_frame_coded_error;
2207   int target;
2208   LAYER_CONTEXT *lc = NULL;
2209   int is_spatial_svc = (cpi->use_svc && cpi->svc.number_temporal_layers == 1);
2210
2211   if (is_spatial_svc) {
2212     lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
2213     frames_left = (int)(twopass->total_stats.count -
2214                   lc->current_video_frame_in_layer);
2215   } else {
2216     frames_left = (int)(twopass->total_stats.count -
2217                   cm->current_video_frame);
2218   }
2219
2220   if (!twopass->stats_in)
2221     return;
2222
2223   if (cpi->refresh_alt_ref_frame) {
2224     cm->frame_type = INTER_FRAME;
2225     vp9_rc_set_frame_target(cpi, twopass->gf_bits);
2226     return;
2227   }
2228
2229   vp9_clear_system_state();
2230
2231   if (is_spatial_svc && twopass->kf_intra_err_min == 0) {
2232     twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
2233     twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
2234   }
2235
2236   if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
2237     twopass->active_worst_quality = cpi->oxcf.cq_level;
2238   } else if (cm->current_video_frame == 0 ||
2239              (is_spatial_svc && lc->current_video_frame_in_layer == 0)) {
2240     // Special case code for first frame.
2241     const int section_target_bandwidth = (int)(twopass->bits_left /
2242                                                frames_left);
2243     const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats,
2244                                                 section_target_bandwidth);
2245     twopass->active_worst_quality = tmp_q;
2246     rc->ni_av_qi = tmp_q;
2247     rc->avg_q = vp9_convert_qindex_to_q(tmp_q);
2248   }
2249   vp9_zero(this_frame);
2250   if (EOF == input_stats(twopass, &this_frame))
2251     return;
2252
2253   this_frame_intra_error = this_frame.intra_error;
2254   this_frame_coded_error = this_frame.coded_error;
2255
2256   // Keyframe and section processing.
2257   if (rc->frames_to_key == 0 ||
2258       (cm->frame_flags & FRAMEFLAGS_KEY)) {
2259     // Define next KF group and assign bits to it.
2260     this_frame_copy = this_frame;
2261     find_next_key_frame(cpi, &this_frame_copy);
2262     // Don't place key frame in any enhancement layers in spatial svc
2263     if (cpi->use_svc && cpi->svc.number_temporal_layers == 1 &&
2264         cpi->svc.spatial_layer_id > 0) {
2265       cm->frame_type = INTER_FRAME;
2266     }
2267   } else {
2268     cm->frame_type = INTER_FRAME;
2269   }
2270
2271   // Is this frame a GF / ARF? (Note: a key frame is always also a GF).
2272   if (rc->frames_till_gf_update_due == 0) {
2273     // Define next gf group and assign bits to it.
2274     this_frame_copy = this_frame;
2275
2276 #if CONFIG_MULTIPLE_ARF
2277     if (cpi->multi_arf_enabled) {
2278       define_fixed_arf_period(cpi);
2279     } else {
2280 #endif
2281       define_gf_group(cpi, &this_frame_copy);
2282 #if CONFIG_MULTIPLE_ARF
2283     }
2284 #endif
2285
2286     if (twopass->gf_zeromotion_pct > 995) {
2287       // As long as max_thresh for encode breakout is small enough, it is ok
2288       // to enable it for show frame, i.e. set allow_encode_breakout to
2289       // ENCODE_BREAKOUT_LIMITED.
2290       if (!cm->show_frame)
2291         cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED;
2292       else
2293         cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED;
2294     }
2295
2296     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2297     cpi->refresh_golden_frame = 1;
2298   } else {
2299     // Otherwise this is an ordinary frame.
2300     // Assign bits from those allocated to the GF group.
2301     this_frame_copy =  this_frame;
2302     assign_std_frame_bits(cpi, &this_frame_copy);
2303   }
2304
2305   // Keep a globally available copy of this and the next frame's iiratio.
2306   twopass->this_iiratio = (int)(this_frame_intra_error /
2307                               DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
2308   {
2309     FIRSTPASS_STATS next_frame;
2310     if (lookup_next_frame_stats(twopass, &next_frame) != EOF) {
2311       twopass->next_iiratio = (int)(next_frame.intra_error /
2312                                  DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2313     }
2314   }
2315
2316   if (cpi->common.frame_type == KEY_FRAME)
2317     target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target);
2318   else
2319     target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target);
2320   vp9_rc_set_frame_target(cpi, target);
2321
2322   // Update the total stats remaining structure.
2323   subtract_stats(&twopass->total_left_stats, &this_frame);
2324 }
2325
2326 void vp9_twopass_postencode_update(VP9_COMP *cpi) {
2327 #ifdef DISABLE_RC_LONG_TERM_MEM
2328   const uint64_t bits_used = cpi->rc.this_frame_target;
2329 #else
2330   const uint64_t bits_used = cpi->rc.projected_frame_size;
2331 #endif
2332   cpi->twopass.bits_left -= bits_used;
2333   cpi->twopass.bits_left = MAX(cpi->twopass.bits_left, 0);
2334   // Update bits left to the kf and gf groups to account for overshoot or
2335   // undershoot on these frames.
2336   if (cpi->common.frame_type == KEY_FRAME) {
2337     // For key frames kf_group_bits already had the target bits subtracted out.
2338     // So now update to the correct value based on the actual bits used.
2339     cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - bits_used;
2340   } else {
2341     cpi->twopass.kf_group_bits -= bits_used;
2342     cpi->twopass.gf_group_bits -= bits_used;
2343     cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0);
2344   }
2345   cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0);
2346 }