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