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