• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "block.h"
18 #include "onyx_int.h"
19 #include "vpx_dsp/variance.h"
20 #include "encodeintra.h"
21 #include "vp8/common/common.h"
22 #include "vp8/common/setupintrarecon.h"
23 #include "vp8/common/systemdependent.h"
24 #include "mcomp.h"
25 #include "firstpass.h"
26 #include "vpx_scale/vpx_scale.h"
27 #include "encodemb.h"
28 #include "vp8/common/extend.h"
29 #include "vpx_ports/system_state.h"
30 #include "vpx_mem/vpx_mem.h"
31 #include "vp8/common/swapyv12buffer.h"
32 #include "rdopt.h"
33 #include "vp8/common/quant_common.h"
34 #include "encodemv.h"
35 #include "encodeframe.h"
36 
37 #define OUTPUT_FPF 0
38 
39 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
40 
41 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
42 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
43 
44 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
45 
46 #define IIFACTOR 1.5
47 #define IIKFACTOR1 1.40
48 #define IIKFACTOR2 1.5
49 #define RMAX 14.0
50 #define GF_RMAX 48.0
51 
52 #define KF_MB_INTRA_MIN 300
53 #define GF_MB_INTRA_MIN 200
54 
55 #define DOUBLE_DIVIDE_CHECK(X) ((X) < 0 ? (X)-.000001 : (X) + .000001)
56 
57 #define POW1 (double)cpi->oxcf.two_pass_vbrbias / 100.0
58 #define POW2 (double)cpi->oxcf.two_pass_vbrbias / 100.0
59 
60 #define NEW_BOOST 1
61 
62 static int vscale_lookup[7] = { 0, 1, 1, 2, 2, 3, 3 };
63 static int hscale_lookup[7] = { 0, 0, 1, 1, 2, 2, 3 };
64 
65 static const int cq_level[QINDEX_RANGE] = {
66   0,  0,  1,  1,  2,  3,  3,  4,  4,  5,  6,  6,  7,  8,  8,  9,  9,  10, 11,
67   11, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24,
68   24, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38,
69   39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53,
70   54, 55, 55, 56, 57, 58, 59, 60, 60, 61, 62, 63, 64, 65, 66, 67, 67, 68, 69,
71   70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 86,
72   87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
73 };
74 
75 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
76 
77 /* Resets the first pass file to the given position using a relative seek
78  * from the current position
79  */
reset_fpf_position(VP8_COMP * cpi,FIRSTPASS_STATS * Position)80 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) {
81   cpi->twopass.stats_in = Position;
82 }
83 
lookup_next_frame_stats(VP8_COMP * cpi,FIRSTPASS_STATS * next_frame)84 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) {
85   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF;
86 
87   *next_frame = *cpi->twopass.stats_in;
88   return 1;
89 }
90 
91 /* Read frame stats at an offset from the current position */
read_frame_stats(VP8_COMP * cpi,FIRSTPASS_STATS * frame_stats,int offset)92 static int read_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *frame_stats,
93                             int offset) {
94   FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in;
95 
96   /* Check legality of offset */
97   if (offset >= 0) {
98     if (&fps_ptr[offset] >= cpi->twopass.stats_in_end) return EOF;
99   } else if (offset < 0) {
100     if (&fps_ptr[offset] < cpi->twopass.stats_in_start) return EOF;
101   }
102 
103   *frame_stats = fps_ptr[offset];
104   return 1;
105 }
106 
input_stats(VP8_COMP * cpi,FIRSTPASS_STATS * fps)107 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps) {
108   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) return EOF;
109 
110   *fps = *cpi->twopass.stats_in;
111   cpi->twopass.stats_in =
112       (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
113   return 1;
114 }
115 
output_stats(struct vpx_codec_pkt_list * pktlist,FIRSTPASS_STATS * stats)116 static void output_stats(struct vpx_codec_pkt_list *pktlist,
117                          FIRSTPASS_STATS *stats) {
118   struct vpx_codec_cx_pkt pkt;
119   pkt.kind = VPX_CODEC_STATS_PKT;
120   pkt.data.twopass_stats.buf = stats;
121   pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
122   vpx_codec_pkt_list_add(pktlist, &pkt);
123 
124 /* TEMP debug code */
125 #if OUTPUT_FPF
126 
127   {
128     FILE *fpfile;
129     fpfile = fopen("firstpass.stt", "a");
130 
131     fprintf(fpfile,
132             "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
133             " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
134             " %12.0f %12.0f %12.4f\n",
135             stats->frame, stats->intra_error, stats->coded_error,
136             stats->ssim_weighted_pred_err, stats->pcnt_inter,
137             stats->pcnt_motion, stats->pcnt_second_ref, stats->pcnt_neutral,
138             stats->MVr, stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv,
139             stats->MVcv, stats->mv_in_out_count, stats->new_mv_count,
140             stats->count, stats->duration);
141     fclose(fpfile);
142   }
143 #endif
144 }
145 
zero_stats(FIRSTPASS_STATS * section)146 static void zero_stats(FIRSTPASS_STATS *section) {
147   section->frame = 0.0;
148   section->intra_error = 0.0;
149   section->coded_error = 0.0;
150   section->ssim_weighted_pred_err = 0.0;
151   section->pcnt_inter = 0.0;
152   section->pcnt_motion = 0.0;
153   section->pcnt_second_ref = 0.0;
154   section->pcnt_neutral = 0.0;
155   section->MVr = 0.0;
156   section->mvr_abs = 0.0;
157   section->MVc = 0.0;
158   section->mvc_abs = 0.0;
159   section->MVrv = 0.0;
160   section->MVcv = 0.0;
161   section->mv_in_out_count = 0.0;
162   section->new_mv_count = 0.0;
163   section->count = 0.0;
164   section->duration = 1.0;
165 }
166 
accumulate_stats(FIRSTPASS_STATS * section,FIRSTPASS_STATS * frame)167 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
168   section->frame += frame->frame;
169   section->intra_error += frame->intra_error;
170   section->coded_error += frame->coded_error;
171   section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
172   section->pcnt_inter += frame->pcnt_inter;
173   section->pcnt_motion += frame->pcnt_motion;
174   section->pcnt_second_ref += frame->pcnt_second_ref;
175   section->pcnt_neutral += frame->pcnt_neutral;
176   section->MVr += frame->MVr;
177   section->mvr_abs += frame->mvr_abs;
178   section->MVc += frame->MVc;
179   section->mvc_abs += frame->mvc_abs;
180   section->MVrv += frame->MVrv;
181   section->MVcv += frame->MVcv;
182   section->mv_in_out_count += frame->mv_in_out_count;
183   section->new_mv_count += frame->new_mv_count;
184   section->count += frame->count;
185   section->duration += frame->duration;
186 }
187 
subtract_stats(FIRSTPASS_STATS * section,FIRSTPASS_STATS * frame)188 static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
189   section->frame -= frame->frame;
190   section->intra_error -= frame->intra_error;
191   section->coded_error -= frame->coded_error;
192   section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
193   section->pcnt_inter -= frame->pcnt_inter;
194   section->pcnt_motion -= frame->pcnt_motion;
195   section->pcnt_second_ref -= frame->pcnt_second_ref;
196   section->pcnt_neutral -= frame->pcnt_neutral;
197   section->MVr -= frame->MVr;
198   section->mvr_abs -= frame->mvr_abs;
199   section->MVc -= frame->MVc;
200   section->mvc_abs -= frame->mvc_abs;
201   section->MVrv -= frame->MVrv;
202   section->MVcv -= frame->MVcv;
203   section->mv_in_out_count -= frame->mv_in_out_count;
204   section->new_mv_count -= frame->new_mv_count;
205   section->count -= frame->count;
206   section->duration -= frame->duration;
207 }
208 
avg_stats(FIRSTPASS_STATS * section)209 static void avg_stats(FIRSTPASS_STATS *section) {
210   if (section->count < 1.0) return;
211 
212   section->intra_error /= section->count;
213   section->coded_error /= section->count;
214   section->ssim_weighted_pred_err /= section->count;
215   section->pcnt_inter /= section->count;
216   section->pcnt_second_ref /= section->count;
217   section->pcnt_neutral /= section->count;
218   section->pcnt_motion /= section->count;
219   section->MVr /= section->count;
220   section->mvr_abs /= section->count;
221   section->MVc /= section->count;
222   section->mvc_abs /= section->count;
223   section->MVrv /= section->count;
224   section->MVcv /= section->count;
225   section->mv_in_out_count /= section->count;
226   section->duration /= section->count;
227 }
228 
229 /* Calculate a modified Error used in distributing bits between easier
230  * and harder frames
231  */
calculate_modified_err(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame)232 static double calculate_modified_err(VP8_COMP *cpi,
233                                      FIRSTPASS_STATS *this_frame) {
234   double av_err = (cpi->twopass.total_stats.ssim_weighted_pred_err /
235                    cpi->twopass.total_stats.count);
236   double this_err = this_frame->ssim_weighted_pred_err;
237   double modified_err;
238 
239   if (this_err > av_err) {
240     modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
241   } else {
242     modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
243   }
244 
245   return modified_err;
246 }
247 
248 static const double weight_table[256] = {
249   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
250   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
251   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
252   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
253   0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
254   0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
255   0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
256   0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
257   0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
258   0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
259   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
260   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
261   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
262   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
263   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
264   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
265   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
266   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
267   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
268   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
269   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
270   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
271   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
272   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
273   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
274   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
275   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
276   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
277   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
278   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
279   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
280   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
281   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
282   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
283   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
284   1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
285   1.000000, 1.000000, 1.000000, 1.000000
286 };
287 
simple_weight(YV12_BUFFER_CONFIG * source)288 static double simple_weight(YV12_BUFFER_CONFIG *source) {
289   int i, j;
290 
291   unsigned char *src = source->y_buffer;
292   double sum_weights = 0.0;
293 
294   /* Loop throught the Y plane raw examining levels and creating a weight
295    * for the image
296    */
297   i = source->y_height;
298   do {
299     j = source->y_width;
300     do {
301       sum_weights += weight_table[*src];
302       src++;
303     } while (--j);
304     src -= source->y_width;
305     src += source->y_stride;
306   } while (--i);
307 
308   sum_weights /= (source->y_height * source->y_width);
309 
310   return sum_weights;
311 }
312 
313 /* This function returns the current per frame maximum bitrate target */
frame_max_bits(VP8_COMP * cpi)314 static int frame_max_bits(VP8_COMP *cpi) {
315   /* Max allocation for a single frame based on the max section guidelines
316    * passed in and how many bits are left
317    */
318   int max_bits;
319 
320   /* For CBR we need to also consider buffer fullness.
321    * If we are running below the optimal level then we need to gradually
322    * tighten up on max_bits.
323    */
324   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
325     double buffer_fullness_ratio =
326         (double)cpi->buffer_level /
327         DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
328 
329     /* For CBR base this on the target average bits per frame plus the
330      * maximum sedction rate passed in by the user
331      */
332     max_bits = (int)(cpi->av_per_frame_bandwidth *
333                      ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
334 
335     /* If our buffer is below the optimum level */
336     if (buffer_fullness_ratio < 1.0) {
337       /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */
338       int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2))
339                              ? cpi->av_per_frame_bandwidth >> 2
340                              : max_bits >> 2;
341 
342       max_bits = (int)(max_bits * buffer_fullness_ratio);
343 
344       /* Lowest value we will set ... which should allow the buffer to
345        * refill.
346        */
347       if (max_bits < min_max_bits) max_bits = min_max_bits;
348     }
349   }
350   /* VBR */
351   else {
352     /* For VBR base this on the bits and frames left plus the
353      * two_pass_vbrmax_section rate passed in by the user
354      */
355     max_bits = (int)(((double)cpi->twopass.bits_left /
356                       (cpi->twopass.total_stats.count -
357                        (double)cpi->common.current_video_frame)) *
358                      ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
359   }
360 
361   /* Trap case where we are out of bits */
362   if (max_bits < 0) max_bits = 0;
363 
364   return max_bits;
365 }
366 
vp8_init_first_pass(VP8_COMP * cpi)367 void vp8_init_first_pass(VP8_COMP *cpi) {
368   zero_stats(&cpi->twopass.total_stats);
369 }
370 
vp8_end_first_pass(VP8_COMP * cpi)371 void vp8_end_first_pass(VP8_COMP *cpi) {
372   output_stats(cpi->output_pkt_list, &cpi->twopass.total_stats);
373 }
374 
zz_motion_search(MACROBLOCK * x,YV12_BUFFER_CONFIG * raw_buffer,int * raw_motion_err,YV12_BUFFER_CONFIG * recon_buffer,int * best_motion_err,int recon_yoffset)375 static void zz_motion_search(MACROBLOCK *x, YV12_BUFFER_CONFIG *raw_buffer,
376                              int *raw_motion_err,
377                              YV12_BUFFER_CONFIG *recon_buffer,
378                              int *best_motion_err, int recon_yoffset) {
379   MACROBLOCKD *const xd = &x->e_mbd;
380   BLOCK *b = &x->block[0];
381   BLOCKD *d = &x->e_mbd.block[0];
382 
383   unsigned char *src_ptr = (*(b->base_src) + b->src);
384   int src_stride = b->src_stride;
385   unsigned char *raw_ptr;
386   int raw_stride = raw_buffer->y_stride;
387   unsigned char *ref_ptr;
388   int ref_stride = x->e_mbd.pre.y_stride;
389 
390   /* Set up pointers for this macro block raw buffer */
391   raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset + d->offset);
392   vpx_mse16x16(src_ptr, src_stride, raw_ptr, raw_stride,
393                (unsigned int *)(raw_motion_err));
394 
395   /* Set up pointers for this macro block recon buffer */
396   xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
397   ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset);
398   vpx_mse16x16(src_ptr, src_stride, ref_ptr, ref_stride,
399                (unsigned int *)(best_motion_err));
400 }
401 
first_pass_motion_search(VP8_COMP * cpi,MACROBLOCK * x,int_mv * ref_mv,MV * best_mv,YV12_BUFFER_CONFIG * recon_buffer,int * best_motion_err,int recon_yoffset)402 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
403                                      int_mv *ref_mv, MV *best_mv,
404                                      YV12_BUFFER_CONFIG *recon_buffer,
405                                      int *best_motion_err, int recon_yoffset) {
406   MACROBLOCKD *const xd = &x->e_mbd;
407   BLOCK *b = &x->block[0];
408   BLOCKD *d = &x->e_mbd.block[0];
409   int num00;
410 
411   int_mv tmp_mv;
412   int_mv ref_mv_full;
413 
414   int tmp_err;
415   int step_param = 3; /* Dont search over full range for first pass */
416   int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
417   int n;
418   vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
419   int new_mv_mode_penalty = 256;
420 
421   /* override the default variance function to use MSE */
422   v_fn_ptr.vf = vpx_mse16x16;
423 
424   /* Set up pointers for this macro block recon buffer */
425   xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
426 
427   /* Initial step/diamond search centred on best mv */
428   tmp_mv.as_int = 0;
429   ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3;
430   ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3;
431   tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
432                                     x->sadperbit16, &num00, &v_fn_ptr,
433                                     x->mvcost, ref_mv);
434   if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
435 
436   if (tmp_err < *best_motion_err) {
437     *best_motion_err = tmp_err;
438     best_mv->row = tmp_mv.as_mv.row;
439     best_mv->col = tmp_mv.as_mv.col;
440   }
441 
442   /* Further step/diamond searches as necessary */
443   n = num00;
444   num00 = 0;
445 
446   while (n < further_steps) {
447     n++;
448 
449     if (num00) {
450       num00--;
451     } else {
452       tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
453                                         step_param + n, x->sadperbit16, &num00,
454                                         &v_fn_ptr, x->mvcost, ref_mv);
455       if (tmp_err < INT_MAX - new_mv_mode_penalty) {
456         tmp_err += new_mv_mode_penalty;
457       }
458 
459       if (tmp_err < *best_motion_err) {
460         *best_motion_err = tmp_err;
461         best_mv->row = tmp_mv.as_mv.row;
462         best_mv->col = tmp_mv.as_mv.col;
463       }
464     }
465   }
466 }
467 
vp8_first_pass(VP8_COMP * cpi)468 void vp8_first_pass(VP8_COMP *cpi) {
469   int mb_row, mb_col;
470   MACROBLOCK *const x = &cpi->mb;
471   VP8_COMMON *const cm = &cpi->common;
472   MACROBLOCKD *const xd = &x->e_mbd;
473 
474   int recon_yoffset, recon_uvoffset;
475   YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
476   YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
477   YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
478   int recon_y_stride = lst_yv12->y_stride;
479   int recon_uv_stride = lst_yv12->uv_stride;
480   int64_t intra_error = 0;
481   int64_t coded_error = 0;
482 
483   int sum_mvr = 0, sum_mvc = 0;
484   int sum_mvr_abs = 0, sum_mvc_abs = 0;
485   int sum_mvrs = 0, sum_mvcs = 0;
486   int mvcount = 0;
487   int intercount = 0;
488   int second_ref_count = 0;
489   int intrapenalty = 256;
490   int neutral_count = 0;
491   int new_mv_count = 0;
492   int sum_in_vectors = 0;
493   uint32_t lastmv_as_int = 0;
494 
495   int_mv zero_ref_mv;
496 
497   zero_ref_mv.as_int = 0;
498 
499   vpx_clear_system_state();
500 
501   x->src = *cpi->Source;
502   xd->pre = *lst_yv12;
503   xd->dst = *new_yv12;
504 
505   x->partition_info = x->pi;
506 
507   xd->mode_info_context = cm->mi;
508 
509   if (!cm->use_bilinear_mc_filter) {
510     xd->subpixel_predict = vp8_sixtap_predict4x4;
511     xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
512     xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
513     xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
514   } else {
515     xd->subpixel_predict = vp8_bilinear_predict4x4;
516     xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
517     xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
518     xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
519   }
520 
521   vp8_build_block_offsets(x);
522 
523   /* set up frame new frame for intra coded blocks */
524   vp8_setup_intra_recon(new_yv12);
525   vp8cx_frame_init_quantizer(cpi);
526 
527   /* Initialise the MV cost table to the defaults */
528   {
529     int flag[2] = { 1, 1 };
530     vp8_initialize_rd_consts(cpi, x,
531                              vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
532     memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
533     vp8_build_component_cost_table(cpi->mb.mvcost,
534                                    (const MV_CONTEXT *)cm->fc.mvc, flag);
535   }
536 
537   /* for each macroblock row in image */
538   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
539     int_mv best_ref_mv;
540 
541     best_ref_mv.as_int = 0;
542 
543     /* reset above block coeffs */
544     xd->up_available = (mb_row != 0);
545     recon_yoffset = (mb_row * recon_y_stride * 16);
546     recon_uvoffset = (mb_row * recon_uv_stride * 8);
547 
548     /* Set up limit values for motion vectors to prevent them extending
549      * outside the UMV borders
550      */
551     x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
552     x->mv_row_max =
553         ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
554 
555     /* for each macroblock col in image */
556     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
557       int this_error;
558       int gf_motion_error = INT_MAX;
559       int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
560 
561       xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
562       xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
563       xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
564       xd->left_available = (mb_col != 0);
565 
566       /* Copy current mb to a buffer */
567       vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
568 
569       /* do intra 16x16 prediction */
570       this_error = vp8_encode_intra(cpi, x, use_dc_pred);
571 
572       /* "intrapenalty" below deals with situations where the intra
573        * and inter error scores are very low (eg a plain black frame)
574        * We do not have special cases in first pass for 0,0 and
575        * nearest etc so all inter modes carry an overhead cost
576        * estimate fot the mv. When the error score is very low this
577        * causes us to pick all or lots of INTRA modes and throw lots
578        * of key frames. This penalty adds a cost matching that of a
579        * 0,0 mv to the intra case.
580        */
581       this_error += intrapenalty;
582 
583       /* Cumulative intra error total */
584       intra_error += (int64_t)this_error;
585 
586       /* Set up limit values for motion vectors to prevent them
587        * extending outside the UMV borders
588        */
589       x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
590       x->mv_col_max =
591           ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
592 
593       /* Other than for the first frame do a motion search */
594       if (cm->current_video_frame > 0) {
595         BLOCKD *d = &x->e_mbd.block[0];
596         MV tmp_mv = { 0, 0 };
597         int tmp_err;
598         int motion_error = INT_MAX;
599         int raw_motion_error = INT_MAX;
600 
601         /* Simple 0,0 motion with no mv overhead */
602         zz_motion_search(x, cpi->last_frame_unscaled_source, &raw_motion_error,
603                          lst_yv12, &motion_error, recon_yoffset);
604         d->bmi.mv.as_mv.row = 0;
605         d->bmi.mv.as_mv.col = 0;
606 
607         if (raw_motion_error < cpi->oxcf.encode_breakout) {
608           goto skip_motion_search;
609         }
610 
611         /* Test last reference frame using the previous best mv as the
612          * starting point (best reference) for the search
613          */
614         first_pass_motion_search(cpi, x, &best_ref_mv, &d->bmi.mv.as_mv,
615                                  lst_yv12, &motion_error, recon_yoffset);
616 
617         /* If the current best reference mv is not centred on 0,0
618          * then do a 0,0 based search as well
619          */
620         if (best_ref_mv.as_int) {
621           tmp_err = INT_MAX;
622           first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, lst_yv12,
623                                    &tmp_err, recon_yoffset);
624 
625           if (tmp_err < motion_error) {
626             motion_error = tmp_err;
627             d->bmi.mv.as_mv.row = tmp_mv.row;
628             d->bmi.mv.as_mv.col = tmp_mv.col;
629           }
630         }
631 
632         /* Experimental search in a second reference frame ((0,0)
633          * based only)
634          */
635         if (cm->current_video_frame > 1) {
636           first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12,
637                                    &gf_motion_error, recon_yoffset);
638 
639           if ((gf_motion_error < motion_error) &&
640               (gf_motion_error < this_error)) {
641             second_ref_count++;
642           }
643 
644           /* Reset to last frame as reference buffer */
645           xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
646           xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
647           xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
648         }
649 
650       skip_motion_search:
651         /* Intra assumed best */
652         best_ref_mv.as_int = 0;
653 
654         if (motion_error <= this_error) {
655           /* Keep a count of cases where the inter and intra were
656            * very close and very low. This helps with scene cut
657            * detection for example in cropped clips with black bars
658            * at the sides or top and bottom.
659            */
660           if ((((this_error - intrapenalty) * 9) <= (motion_error * 10)) &&
661               (this_error < (2 * intrapenalty))) {
662             neutral_count++;
663           }
664 
665           d->bmi.mv.as_mv.row *= 8;
666           d->bmi.mv.as_mv.col *= 8;
667           this_error = motion_error;
668           vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
669           vp8_encode_inter16x16y(x);
670           sum_mvr += d->bmi.mv.as_mv.row;
671           sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
672           sum_mvc += d->bmi.mv.as_mv.col;
673           sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
674           sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
675           sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
676           intercount++;
677 
678           best_ref_mv.as_int = d->bmi.mv.as_int;
679 
680           /* Was the vector non-zero */
681           if (d->bmi.mv.as_int) {
682             mvcount++;
683 
684             /* Was it different from the last non zero vector */
685             if (d->bmi.mv.as_int != lastmv_as_int) new_mv_count++;
686             lastmv_as_int = d->bmi.mv.as_int;
687 
688             /* Does the Row vector point inwards or outwards */
689             if (mb_row < cm->mb_rows / 2) {
690               if (d->bmi.mv.as_mv.row > 0) {
691                 sum_in_vectors--;
692               } else if (d->bmi.mv.as_mv.row < 0) {
693                 sum_in_vectors++;
694               }
695             } else if (mb_row > cm->mb_rows / 2) {
696               if (d->bmi.mv.as_mv.row > 0) {
697                 sum_in_vectors++;
698               } else if (d->bmi.mv.as_mv.row < 0) {
699                 sum_in_vectors--;
700               }
701             }
702 
703             /* Does the Row vector point inwards or outwards */
704             if (mb_col < cm->mb_cols / 2) {
705               if (d->bmi.mv.as_mv.col > 0) {
706                 sum_in_vectors--;
707               } else if (d->bmi.mv.as_mv.col < 0) {
708                 sum_in_vectors++;
709               }
710             } else if (mb_col > cm->mb_cols / 2) {
711               if (d->bmi.mv.as_mv.col > 0) {
712                 sum_in_vectors++;
713               } else if (d->bmi.mv.as_mv.col < 0) {
714                 sum_in_vectors--;
715               }
716             }
717           }
718         }
719       }
720 
721       coded_error += (int64_t)this_error;
722 
723       /* adjust to the next column of macroblocks */
724       x->src.y_buffer += 16;
725       x->src.u_buffer += 8;
726       x->src.v_buffer += 8;
727 
728       recon_yoffset += 16;
729       recon_uvoffset += 8;
730     }
731 
732     /* adjust to the next row of mbs */
733     x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
734     x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
735     x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
736 
737     /* extend the recon for intra prediction */
738     vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8,
739                       xd->dst.v_buffer + 8);
740     vpx_clear_system_state();
741   }
742 
743   vpx_clear_system_state();
744   {
745     double weight = 0.0;
746 
747     FIRSTPASS_STATS fps;
748 
749     fps.frame = cm->current_video_frame;
750     fps.intra_error = (double)(intra_error >> 8);
751     fps.coded_error = (double)(coded_error >> 8);
752     weight = simple_weight(cpi->Source);
753 
754     if (weight < 0.1) weight = 0.1;
755 
756     fps.ssim_weighted_pred_err = fps.coded_error * weight;
757 
758     fps.pcnt_inter = 0.0;
759     fps.pcnt_motion = 0.0;
760     fps.MVr = 0.0;
761     fps.mvr_abs = 0.0;
762     fps.MVc = 0.0;
763     fps.mvc_abs = 0.0;
764     fps.MVrv = 0.0;
765     fps.MVcv = 0.0;
766     fps.mv_in_out_count = 0.0;
767     fps.new_mv_count = 0.0;
768     fps.count = 1.0;
769 
770     fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
771     fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
772     fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
773 
774     if (mvcount > 0) {
775       fps.MVr = (double)sum_mvr / (double)mvcount;
776       fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
777       fps.MVc = (double)sum_mvc / (double)mvcount;
778       fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
779       fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) /
780                  (double)mvcount;
781       fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) /
782                  (double)mvcount;
783       fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
784       fps.new_mv_count = new_mv_count;
785 
786       fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
787     }
788 
789     /* TODO:  handle the case when duration is set to 0, or something less
790      * than the full time between subsequent cpi->source_time_stamps
791      */
792     fps.duration = (double)(cpi->source->ts_end - cpi->source->ts_start);
793 
794     /* don't want to do output stats with a stack variable! */
795     memcpy(&cpi->twopass.this_frame_stats, &fps, sizeof(FIRSTPASS_STATS));
796     output_stats(cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
797     accumulate_stats(&cpi->twopass.total_stats, &fps);
798   }
799 
800   /* Copy the previous Last Frame into the GF buffer if specific
801    * conditions for doing so are met
802    */
803   if ((cm->current_video_frame > 0) &&
804       (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
805       ((cpi->twopass.this_frame_stats.intra_error /
806         DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
807        2.0)) {
808     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
809   }
810 
811   /* swap frame pointers so last frame refers to the frame we just
812    * compressed
813    */
814   vp8_swap_yv12_buffer(lst_yv12, new_yv12);
815   vp8_yv12_extend_frame_borders(lst_yv12);
816 
817   /* Special case for the first frame. Copy into the GF buffer as a
818    * second reference.
819    */
820   if (cm->current_video_frame == 0) {
821     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
822   }
823 
824   /* use this to see what the first pass reconstruction looks like */
825   if (0) {
826     char filename[512];
827     FILE *recon_file;
828     sprintf(filename, "enc%04d.yuv", (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 
836     (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
837     fclose(recon_file);
838   }
839 
840   cm->current_video_frame++;
841 }
842 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
843 
844 /* Estimate a cost per mb attributable to overheads such as the coding of
845  * modes and motion vectors.
846  * Currently simplistic in its assumptions for testing.
847  */
848 
bitcost(double prob)849 static double bitcost(double prob) {
850   if (prob > 0.000122) {
851     return -log(prob) / log(2.0);
852   } else {
853     return 13.0;
854   }
855 }
estimate_modemvcost(VP8_COMP * cpi,FIRSTPASS_STATS * fpstats)856 static int64_t estimate_modemvcost(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats) {
857   int mv_cost;
858   int64_t mode_cost;
859 
860   double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
861   double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
862   double av_intra = (1.0 - av_pct_inter);
863 
864   double zz_cost;
865   double motion_cost;
866   double intra_cost;
867 
868   zz_cost = bitcost(av_pct_inter - av_pct_motion);
869   motion_cost = bitcost(av_pct_motion);
870   intra_cost = bitcost(av_intra);
871 
872   /* Estimate of extra bits per mv overhead for mbs
873    * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb
874    */
875   mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
876 
877   /* Crude estimate of overhead cost from modes
878    * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
879    */
880   mode_cost =
881       (int64_t)((((av_pct_inter - av_pct_motion) * zz_cost) +
882                  (av_pct_motion * motion_cost) + (av_intra * intra_cost)) *
883                 cpi->common.MBs) *
884       512;
885 
886   return mv_cost + mode_cost;
887 }
888 
calc_correction_factor(double err_per_mb,double err_devisor,double pt_low,double pt_high,int Q)889 static double calc_correction_factor(double err_per_mb, double err_devisor,
890                                      double pt_low, double pt_high, int Q) {
891   double power_term;
892   double error_term = err_per_mb / err_devisor;
893   double correction_factor;
894 
895   /* Adjustment based on Q to power term. */
896   power_term = pt_low + (Q * 0.01);
897   power_term = (power_term > pt_high) ? pt_high : power_term;
898 
899   /* Adjustments to error term */
900   /* TBD */
901 
902   /* Calculate correction factor */
903   correction_factor = pow(error_term, power_term);
904 
905   /* Clip range */
906   correction_factor = (correction_factor < 0.05)
907                           ? 0.05
908                           : (correction_factor > 5.0) ? 5.0 : correction_factor;
909 
910   return correction_factor;
911 }
912 
estimate_max_q(VP8_COMP * cpi,FIRSTPASS_STATS * fpstats,int section_target_bandwitdh,int overhead_bits)913 static int estimate_max_q(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
914                           int section_target_bandwitdh, int overhead_bits) {
915   int Q;
916   int num_mbs = cpi->common.MBs;
917   int target_norm_bits_per_mb;
918 
919   double section_err = (fpstats->coded_error / fpstats->count);
920   double err_per_mb = section_err / num_mbs;
921   double err_correction_factor;
922   double speed_correction = 1.0;
923   int overhead_bits_per_mb;
924 
925   if (section_target_bandwitdh <= 0) {
926     return cpi->twopass.maxq_max_limit; /* Highest value allowed */
927   }
928 
929   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
930                                 ? (512 * section_target_bandwitdh) / num_mbs
931                                 : 512 * (section_target_bandwitdh / num_mbs);
932 
933   /* Calculate a corrective factor based on a rolling ratio of bits spent
934    * vs target bits
935    */
936   if ((cpi->rolling_target_bits > 0) &&
937       (cpi->active_worst_quality < cpi->worst_quality)) {
938     double rolling_ratio;
939 
940     rolling_ratio =
941         (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
942 
943     if (rolling_ratio < 0.95) {
944       cpi->twopass.est_max_qcorrection_factor -= 0.005;
945     } else if (rolling_ratio > 1.05) {
946       cpi->twopass.est_max_qcorrection_factor += 0.005;
947     }
948 
949     cpi->twopass.est_max_qcorrection_factor =
950         (cpi->twopass.est_max_qcorrection_factor < 0.1)
951             ? 0.1
952             : (cpi->twopass.est_max_qcorrection_factor > 10.0)
953                   ? 10.0
954                   : cpi->twopass.est_max_qcorrection_factor;
955   }
956 
957   /* Corrections for higher compression speed settings
958    * (reduced compression expected)
959    */
960   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
961     if (cpi->oxcf.cpu_used <= 5) {
962       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
963     } else {
964       speed_correction = 1.25;
965     }
966   }
967 
968   /* Estimate of overhead bits per mb */
969   /* Correction to overhead bits for min allowed Q. */
970   overhead_bits_per_mb = overhead_bits / num_mbs;
971   overhead_bits_per_mb = (int)(overhead_bits_per_mb *
972                                pow(0.98, (double)cpi->twopass.maxq_min_limit));
973 
974   /* Try and pick a max Q that will be high enough to encode the
975    * content at the given rate.
976    */
977   for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; ++Q) {
978     int bits_per_mb_at_this_q;
979 
980     /* Error per MB based correction factor */
981     err_correction_factor =
982         calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
983 
984     bits_per_mb_at_this_q =
985         vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
986 
987     bits_per_mb_at_this_q =
988         (int)(.5 + err_correction_factor * speed_correction *
989                        cpi->twopass.est_max_qcorrection_factor *
990                        cpi->twopass.section_max_qfactor *
991                        (double)bits_per_mb_at_this_q);
992 
993     /* Mode and motion overhead */
994     /* As Q rises in real encode loop rd code will force overhead down
995      * We make a crude adjustment for this here as *.98 per Q step.
996      */
997     overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
998 
999     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
1000   }
1001 
1002   /* Restriction on active max q for constrained quality mode. */
1003   if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
1004       (Q < cpi->cq_target_quality)) {
1005     Q = cpi->cq_target_quality;
1006   }
1007 
1008   /* Adjust maxq_min_limit and maxq_max_limit limits based on
1009    * average q observed in clip for non kf/gf.arf frames
1010    * Give average a chance to settle though.
1011    */
1012   if ((cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8)) &&
1013       (cpi->ni_frames > 150)) {
1014     cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
1015                                       ? (cpi->ni_av_qi + 32)
1016                                       : cpi->worst_quality;
1017     cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
1018                                       ? (cpi->ni_av_qi - 32)
1019                                       : cpi->best_quality;
1020   }
1021 
1022   return Q;
1023 }
1024 
1025 /* For cq mode estimate a cq level that matches the observed
1026  * complexity and data rate.
1027  */
estimate_cq(VP8_COMP * cpi,FIRSTPASS_STATS * fpstats,int section_target_bandwitdh,int overhead_bits)1028 static int estimate_cq(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
1029                        int section_target_bandwitdh, int overhead_bits) {
1030   int Q;
1031   int num_mbs = cpi->common.MBs;
1032   int target_norm_bits_per_mb;
1033 
1034   double section_err = (fpstats->coded_error / fpstats->count);
1035   double err_per_mb = section_err / num_mbs;
1036   double err_correction_factor;
1037   double speed_correction = 1.0;
1038   double clip_iiratio;
1039   double clip_iifactor;
1040   int overhead_bits_per_mb;
1041 
1042   if (0) {
1043     FILE *f = fopen("epmp.stt", "a");
1044     fprintf(f, "%10.2f\n", err_per_mb);
1045     fclose(f);
1046   }
1047 
1048   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1049                                 ? (512 * section_target_bandwitdh) / num_mbs
1050                                 : 512 * (section_target_bandwitdh / num_mbs);
1051 
1052   /* Estimate of overhead bits per mb */
1053   overhead_bits_per_mb = overhead_bits / num_mbs;
1054 
1055   /* Corrections for higher compression speed settings
1056    * (reduced compression expected)
1057    */
1058   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
1059     if (cpi->oxcf.cpu_used <= 5) {
1060       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1061     } else {
1062       speed_correction = 1.25;
1063     }
1064   }
1065 
1066   /* II ratio correction factor for clip as a whole */
1067   clip_iiratio = cpi->twopass.total_stats.intra_error /
1068                  DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
1069   clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1070   if (clip_iifactor < 0.80) clip_iifactor = 0.80;
1071 
1072   /* Try and pick a Q that can encode the content at the given rate. */
1073   for (Q = 0; Q < MAXQ; ++Q) {
1074     int bits_per_mb_at_this_q;
1075 
1076     /* Error per MB based correction factor */
1077     err_correction_factor =
1078         calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q);
1079 
1080     bits_per_mb_at_this_q =
1081         vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
1082 
1083     bits_per_mb_at_this_q =
1084         (int)(.5 + err_correction_factor * speed_correction * clip_iifactor *
1085                        (double)bits_per_mb_at_this_q);
1086 
1087     /* Mode and motion overhead */
1088     /* As Q rises in real encode loop rd code will force overhead down
1089      * We make a crude adjustment for this here as *.98 per Q step.
1090      */
1091     overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
1092 
1093     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
1094   }
1095 
1096   /* Clip value to range "best allowed to (worst allowed - 1)" */
1097   Q = cq_level[Q];
1098   if (Q >= cpi->worst_quality) Q = cpi->worst_quality - 1;
1099   if (Q < cpi->best_quality) Q = cpi->best_quality;
1100 
1101   return Q;
1102 }
1103 
estimate_q(VP8_COMP * cpi,double section_err,int section_target_bandwitdh)1104 static int estimate_q(VP8_COMP *cpi, double section_err,
1105                       int section_target_bandwitdh) {
1106   int Q;
1107   int num_mbs = cpi->common.MBs;
1108   int target_norm_bits_per_mb;
1109 
1110   double err_per_mb = section_err / num_mbs;
1111   double err_correction_factor;
1112   double speed_correction = 1.0;
1113 
1114   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1115                                 ? (512 * section_target_bandwitdh) / num_mbs
1116                                 : 512 * (section_target_bandwitdh / num_mbs);
1117 
1118   /* Corrections for higher compression speed settings
1119    * (reduced compression expected)
1120    */
1121   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
1122     if (cpi->oxcf.cpu_used <= 5) {
1123       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1124     } else {
1125       speed_correction = 1.25;
1126     }
1127   }
1128 
1129   /* Try and pick a Q that can encode the content at the given rate. */
1130   for (Q = 0; Q < MAXQ; ++Q) {
1131     int bits_per_mb_at_this_q;
1132 
1133     /* Error per MB based correction factor */
1134     err_correction_factor =
1135         calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
1136 
1137     bits_per_mb_at_this_q =
1138         (int)(.5 + (err_correction_factor * speed_correction *
1139                     cpi->twopass.est_max_qcorrection_factor *
1140                     (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0));
1141 
1142     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
1143   }
1144 
1145   return Q;
1146 }
1147 
1148 /* Estimate a worst case Q for a KF group */
estimate_kf_group_q(VP8_COMP * cpi,double section_err,int section_target_bandwitdh,double group_iiratio)1149 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err,
1150                                int section_target_bandwitdh,
1151                                double group_iiratio) {
1152   int Q;
1153   int num_mbs = cpi->common.MBs;
1154   int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1155   int bits_per_mb_at_this_q;
1156 
1157   double err_per_mb = section_err / num_mbs;
1158   double err_correction_factor;
1159   double speed_correction = 1.0;
1160   double current_spend_ratio = 1.0;
1161 
1162   double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1163   double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1164 
1165   double iiratio_correction_factor = 1.0;
1166 
1167   double combined_correction_factor;
1168 
1169   /* Trap special case where the target is <= 0 */
1170   if (target_norm_bits_per_mb <= 0) return MAXQ * 2;
1171 
1172   /* Calculate a corrective factor based on a rolling ratio of bits spent
1173    *  vs target bits
1174    * This is clamped to the range 0.1 to 10.0
1175    */
1176   if (cpi->long_rolling_target_bits <= 0) {
1177     current_spend_ratio = 10.0;
1178   } else {
1179     current_spend_ratio = (double)cpi->long_rolling_actual_bits /
1180                           (double)cpi->long_rolling_target_bits;
1181     current_spend_ratio =
1182         (current_spend_ratio > 10.0)
1183             ? 10.0
1184             : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
1185   }
1186 
1187   /* Calculate a correction factor based on the quality of prediction in
1188    * the sequence as indicated by intra_inter error score ratio (IIRatio)
1189    * The idea here is to favour subsampling in the hardest sections vs
1190    * the easyest.
1191    */
1192   iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1193 
1194   if (iiratio_correction_factor < 0.5) iiratio_correction_factor = 0.5;
1195 
1196   /* Corrections for higher compression speed settings
1197    * (reduced compression expected)
1198    */
1199   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
1200     if (cpi->oxcf.cpu_used <= 5) {
1201       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1202     } else {
1203       speed_correction = 1.25;
1204     }
1205   }
1206 
1207   /* Combine the various factors calculated above */
1208   combined_correction_factor =
1209       speed_correction * iiratio_correction_factor * current_spend_ratio;
1210 
1211   /* Try and pick a Q that should be high enough to encode the content at
1212    * the given rate.
1213    */
1214   for (Q = 0; Q < MAXQ; ++Q) {
1215     /* Error per MB based correction factor */
1216     err_correction_factor =
1217         calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q);
1218 
1219     bits_per_mb_at_this_q =
1220         (int)(.5 + (err_correction_factor * combined_correction_factor *
1221                     (double)vp8_bits_per_mb[INTER_FRAME][Q]));
1222 
1223     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
1224   }
1225 
1226   /* If we could not hit the target even at Max Q then estimate what Q
1227    * would have been required
1228    */
1229   while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) &&
1230          (Q < (MAXQ * 2))) {
1231     bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1232     Q++;
1233   }
1234 
1235   if (0) {
1236     FILE *f = fopen("estkf_q.stt", "a");
1237     fprintf(f, "%8d %8d %8d %8.2f %8.3f %8.2f %8.3f %8.3f %8.3f %8d\n",
1238             cpi->common.current_video_frame, bits_per_mb_at_this_q,
1239             target_norm_bits_per_mb, err_per_mb, err_correction_factor,
1240             current_spend_ratio, group_iiratio, iiratio_correction_factor,
1241             (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level,
1242             Q);
1243     fclose(f);
1244   }
1245 
1246   return Q;
1247 }
1248 
vp8_init_second_pass(VP8_COMP * cpi)1249 void vp8_init_second_pass(VP8_COMP *cpi) {
1250   FIRSTPASS_STATS this_frame;
1251   FIRSTPASS_STATS *start_pos;
1252 
1253   double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
1254                                       cpi->oxcf.two_pass_vbrmin_section / 100);
1255 
1256   zero_stats(&cpi->twopass.total_stats);
1257   zero_stats(&cpi->twopass.total_left_stats);
1258 
1259   if (!cpi->twopass.stats_in_end) return;
1260 
1261   cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1262   cpi->twopass.total_left_stats = cpi->twopass.total_stats;
1263 
1264   /* each frame can have a different duration, as the frame rate in the
1265    * source isn't guaranteed to be constant.   The frame rate prior to
1266    * the first frame encoded in the second pass is a guess.  However the
1267    * sum duration is not. Its calculated based on the actual durations of
1268    * all frames from the first pass.
1269    */
1270   vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count /
1271                              cpi->twopass.total_stats.duration);
1272 
1273   cpi->output_framerate = cpi->framerate;
1274   cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration *
1275                                      cpi->oxcf.target_bandwidth / 10000000.0);
1276   cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration *
1277                                       two_pass_min_rate / 10000000.0);
1278 
1279   /* Calculate a minimum intra value to be used in determining the IIratio
1280    * scores used in the second pass. We have this minimum to make sure
1281    * that clips that are static but "low complexity" in the intra domain
1282    * are still boosted appropriately for KF/GF/ARF
1283    */
1284   cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1285   cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1286 
1287   /* Scan the first pass file and calculate an average Intra / Inter error
1288    * score ratio for the sequence
1289    */
1290   {
1291     double sum_iiratio = 0.0;
1292     double IIRatio;
1293 
1294     start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1295 
1296     while (input_stats(cpi, &this_frame) != EOF) {
1297       IIRatio =
1298           this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1299       IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1300       sum_iiratio += IIRatio;
1301     }
1302 
1303     cpi->twopass.avg_iiratio =
1304         sum_iiratio /
1305         DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
1306 
1307     /* Reset file position */
1308     reset_fpf_position(cpi, start_pos);
1309   }
1310 
1311   /* Scan the first pass file and calculate a modified total error based
1312    * upon the bias/power function used to allocate bits
1313    */
1314   {
1315     start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1316 
1317     cpi->twopass.modified_error_total = 0.0;
1318     cpi->twopass.modified_error_used = 0.0;
1319 
1320     while (input_stats(cpi, &this_frame) != EOF) {
1321       cpi->twopass.modified_error_total +=
1322           calculate_modified_err(cpi, &this_frame);
1323     }
1324     cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1325 
1326     reset_fpf_position(cpi, start_pos); /* Reset file position */
1327   }
1328 }
1329 
vp8_end_second_pass(VP8_COMP * cpi)1330 void vp8_end_second_pass(VP8_COMP *cpi) { (void)cpi; }
1331 
1332 /* This function gives and estimate of how badly we believe the prediction
1333  * quality is decaying from frame to frame.
1334  */
get_prediction_decay_rate(FIRSTPASS_STATS * next_frame)1335 static double get_prediction_decay_rate(FIRSTPASS_STATS *next_frame) {
1336   double prediction_decay_rate;
1337   double motion_decay;
1338   double motion_pct = next_frame->pcnt_motion;
1339 
1340   /* Initial basis is the % mbs inter coded */
1341   prediction_decay_rate = next_frame->pcnt_inter;
1342 
1343   /* High % motion -> somewhat higher decay rate */
1344   motion_decay = (1.0 - (motion_pct / 20.0));
1345   if (motion_decay < prediction_decay_rate) {
1346     prediction_decay_rate = motion_decay;
1347   }
1348 
1349   /* Adjustment to decay rate based on speed of motion */
1350   {
1351     double this_mv_rabs;
1352     double this_mv_cabs;
1353     double distance_factor;
1354 
1355     this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1356     this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1357 
1358     distance_factor =
1359         sqrt((this_mv_rabs * this_mv_rabs) + (this_mv_cabs * this_mv_cabs)) /
1360         250.0;
1361     distance_factor = ((distance_factor > 1.0) ? 0.0 : (1.0 - distance_factor));
1362     if (distance_factor < prediction_decay_rate) {
1363       prediction_decay_rate = distance_factor;
1364     }
1365   }
1366 
1367   return prediction_decay_rate;
1368 }
1369 
1370 /* Function to test for a condition where a complex transition is followed
1371  * by a static section. For example in slide shows where there is a fade
1372  * between slides. This is to help with more optimal kf and gf positioning.
1373  */
detect_transition_to_still(VP8_COMP * cpi,int frame_interval,int still_interval,double loop_decay_rate,double decay_accumulator)1374 static int detect_transition_to_still(VP8_COMP *cpi, int frame_interval,
1375                                       int still_interval,
1376                                       double loop_decay_rate,
1377                                       double decay_accumulator) {
1378   int trans_to_still = 0;
1379 
1380   /* Break clause to detect very still sections after motion
1381    * For example a static image after a fade or other transition
1382    * instead of a clean scene cut.
1383    */
1384   if ((frame_interval > MIN_GF_INTERVAL) && (loop_decay_rate >= 0.999) &&
1385       (decay_accumulator < 0.9)) {
1386     int j;
1387     FIRSTPASS_STATS *position = cpi->twopass.stats_in;
1388     FIRSTPASS_STATS tmp_next_frame;
1389     double decay_rate;
1390 
1391     /* Look ahead a few frames to see if static condition persists... */
1392     for (j = 0; j < still_interval; ++j) {
1393       if (EOF == input_stats(cpi, &tmp_next_frame)) break;
1394 
1395       decay_rate = get_prediction_decay_rate(&tmp_next_frame);
1396       if (decay_rate < 0.999) break;
1397     }
1398     /* Reset file position */
1399     reset_fpf_position(cpi, position);
1400 
1401     /* Only if it does do we signal a transition to still */
1402     if (j == still_interval) trans_to_still = 1;
1403   }
1404 
1405   return trans_to_still;
1406 }
1407 
1408 /* This function detects a flash through the high relative pcnt_second_ref
1409  * score in the frame following a flash frame. The offset passed in should
1410  * reflect this
1411  */
detect_flash(VP8_COMP * cpi,int offset)1412 static int detect_flash(VP8_COMP *cpi, int offset) {
1413   FIRSTPASS_STATS next_frame;
1414 
1415   int flash_detected = 0;
1416 
1417   /* Read the frame data. */
1418   /* The return is 0 (no flash detected) if not a valid frame */
1419   if (read_frame_stats(cpi, &next_frame, offset) != EOF) {
1420     /* What we are looking for here is a situation where there is a
1421      * brief break in prediction (such as a flash) but subsequent frames
1422      * are reasonably well predicted by an earlier (pre flash) frame.
1423      * The recovery after a flash is indicated by a high pcnt_second_ref
1424      * comapred to pcnt_inter.
1425      */
1426     if ((next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
1427         (next_frame.pcnt_second_ref >= 0.5)) {
1428       flash_detected = 1;
1429 
1430       /*if (1)
1431       {
1432           FILE *f = fopen("flash.stt", "a");
1433           fprintf(f, "%8.0f %6.2f %6.2f\n",
1434               next_frame.frame,
1435               next_frame.pcnt_inter,
1436               next_frame.pcnt_second_ref);
1437           fclose(f);
1438       }*/
1439     }
1440   }
1441 
1442   return flash_detected;
1443 }
1444 
1445 /* Update the motion related elements to the GF arf boost calculation */
accumulate_frame_motion_stats(FIRSTPASS_STATS * this_frame,double * this_frame_mv_in_out,double * mv_in_out_accumulator,double * abs_mv_in_out_accumulator,double * mv_ratio_accumulator)1446 static void accumulate_frame_motion_stats(FIRSTPASS_STATS *this_frame,
1447                                           double *this_frame_mv_in_out,
1448                                           double *mv_in_out_accumulator,
1449                                           double *abs_mv_in_out_accumulator,
1450                                           double *mv_ratio_accumulator) {
1451   double this_frame_mvr_ratio;
1452   double this_frame_mvc_ratio;
1453   double motion_pct;
1454 
1455   /* Accumulate motion stats. */
1456   motion_pct = this_frame->pcnt_motion;
1457 
1458   /* Accumulate Motion In/Out of frame stats */
1459   *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1460   *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1461   *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct);
1462 
1463   /* Accumulate a measure of how uniform (or conversely how random)
1464    * the motion field is. (A ratio of absmv / mv)
1465    */
1466   if (motion_pct > 0.05) {
1467     this_frame_mvr_ratio =
1468         fabs(this_frame->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1469 
1470     this_frame_mvc_ratio =
1471         fabs(this_frame->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1472 
1473     *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs)
1474                                  ? (this_frame_mvr_ratio * motion_pct)
1475                                  : this_frame->mvr_abs * motion_pct;
1476 
1477     *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs)
1478                                  ? (this_frame_mvc_ratio * motion_pct)
1479                                  : this_frame->mvc_abs * motion_pct;
1480   }
1481 }
1482 
1483 /* Calculate a baseline boost number for the current frame. */
calc_frame_boost(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame,double this_frame_mv_in_out)1484 static double calc_frame_boost(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame,
1485                                double this_frame_mv_in_out) {
1486   double frame_boost;
1487 
1488   /* Underlying boost factor is based on inter intra error ratio */
1489   if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) {
1490     frame_boost = (IIFACTOR * this_frame->intra_error /
1491                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1492   } else {
1493     frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1494                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1495   }
1496 
1497   /* Increase boost for frames where new data coming into frame
1498    * (eg zoom out). Slightly reduce boost if there is a net balance
1499    * of motion out of the frame (zoom in).
1500    * The range for this_frame_mv_in_out is -1.0 to +1.0
1501    */
1502   if (this_frame_mv_in_out > 0.0) {
1503     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1504     /* In extreme case boost is halved */
1505   } else {
1506     frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1507   }
1508 
1509   /* Clip to maximum */
1510   if (frame_boost > GF_RMAX) frame_boost = GF_RMAX;
1511 
1512   return frame_boost;
1513 }
1514 
1515 #if NEW_BOOST
calc_arf_boost(VP8_COMP * cpi,int offset,int f_frames,int b_frames,int * f_boost,int * b_boost)1516 static int calc_arf_boost(VP8_COMP *cpi, int offset, int f_frames, int b_frames,
1517                           int *f_boost, int *b_boost) {
1518   FIRSTPASS_STATS this_frame;
1519 
1520   int i;
1521   double boost_score = 0.0;
1522   double mv_ratio_accumulator = 0.0;
1523   double decay_accumulator = 1.0;
1524   double this_frame_mv_in_out = 0.0;
1525   double mv_in_out_accumulator = 0.0;
1526   double abs_mv_in_out_accumulator = 0.0;
1527   double r;
1528   int flash_detected = 0;
1529 
1530   /* Search forward from the proposed arf/next gf position */
1531   for (i = 0; i < f_frames; ++i) {
1532     if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
1533 
1534     /* Update the motion related elements to the boost calculation */
1535     accumulate_frame_motion_stats(
1536         &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1537         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1538 
1539     /* Calculate the baseline boost number for this frame */
1540     r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
1541 
1542     /* We want to discount the the flash frame itself and the recovery
1543      * frame that follows as both will have poor scores.
1544      */
1545     flash_detected =
1546         detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
1547 
1548     /* Cumulative effect of prediction quality decay */
1549     if (!flash_detected) {
1550       decay_accumulator =
1551           decay_accumulator * get_prediction_decay_rate(&this_frame);
1552       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1553     }
1554     boost_score += (decay_accumulator * r);
1555 
1556     /* Break out conditions. */
1557     if ((!flash_detected) &&
1558         ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
1559          (mv_in_out_accumulator < -2.0))) {
1560       break;
1561     }
1562   }
1563 
1564   *f_boost = (int)(boost_score * 100.0) >> 4;
1565 
1566   /* Reset for backward looking loop */
1567   boost_score = 0.0;
1568   mv_ratio_accumulator = 0.0;
1569   decay_accumulator = 1.0;
1570   this_frame_mv_in_out = 0.0;
1571   mv_in_out_accumulator = 0.0;
1572   abs_mv_in_out_accumulator = 0.0;
1573 
1574   /* Search forward from the proposed arf/next gf position */
1575   for (i = -1; i >= -b_frames; i--) {
1576     if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
1577 
1578     /* Update the motion related elements to the boost calculation */
1579     accumulate_frame_motion_stats(
1580         &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1581         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1582 
1583     /* Calculate the baseline boost number for this frame */
1584     r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
1585 
1586     /* We want to discount the the flash frame itself and the recovery
1587      * frame that follows as both will have poor scores.
1588      */
1589     flash_detected =
1590         detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
1591 
1592     /* Cumulative effect of prediction quality decay */
1593     if (!flash_detected) {
1594       decay_accumulator =
1595           decay_accumulator * get_prediction_decay_rate(&this_frame);
1596       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1597     }
1598 
1599     boost_score += (decay_accumulator * r);
1600 
1601     /* Break out conditions. */
1602     if ((!flash_detected) &&
1603         ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
1604          (mv_in_out_accumulator < -2.0))) {
1605       break;
1606     }
1607   }
1608   *b_boost = (int)(boost_score * 100.0) >> 4;
1609 
1610   return (*f_boost + *b_boost);
1611 }
1612 #endif
1613 
1614 /* Analyse and define a gf/arf group . */
define_gf_group(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame)1615 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1616   FIRSTPASS_STATS next_frame;
1617   FIRSTPASS_STATS *start_pos;
1618   int i;
1619   double r;
1620   double boost_score = 0.0;
1621   double old_boost_score = 0.0;
1622   double gf_group_err = 0.0;
1623   double gf_first_frame_err = 0.0;
1624   double mod_frame_err = 0.0;
1625 
1626   double mv_ratio_accumulator = 0.0;
1627   double decay_accumulator = 1.0;
1628 
1629   double loop_decay_rate = 1.00; /* Starting decay rate */
1630 
1631   double this_frame_mv_in_out = 0.0;
1632   double mv_in_out_accumulator = 0.0;
1633   double abs_mv_in_out_accumulator = 0.0;
1634   double mod_err_per_mb_accumulator = 0.0;
1635 
1636   int max_bits = frame_max_bits(cpi); /* Max for a single frame */
1637 
1638   unsigned int allow_alt_ref =
1639       cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1640 
1641   int alt_boost = 0;
1642   int f_boost = 0;
1643   int b_boost = 0;
1644   int flash_detected;
1645 
1646   cpi->twopass.gf_group_bits = 0;
1647   cpi->twopass.gf_decay_rate = 0;
1648 
1649   vpx_clear_system_state();
1650 
1651   start_pos = cpi->twopass.stats_in;
1652 
1653   memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */
1654 
1655   /* Load stats for the current frame. */
1656   mod_frame_err = calculate_modified_err(cpi, this_frame);
1657 
1658   /* Note the error of the frame at the start of the group (this will be
1659    * the GF frame error if we code a normal gf
1660    */
1661   gf_first_frame_err = mod_frame_err;
1662 
1663   /* Special treatment if the current frame is a key frame (which is also
1664    * a gf). If it is then its error score (and hence bit allocation) need
1665    * to be subtracted out from the calculation for the GF group
1666    */
1667   if (cpi->common.frame_type == KEY_FRAME) gf_group_err -= gf_first_frame_err;
1668 
1669   /* Scan forward to try and work out how many frames the next gf group
1670    * should contain and what level of boost is appropriate for the GF
1671    * or ARF that will be coded with the group
1672    */
1673   i = 0;
1674 
1675   while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1676           ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1677          (i < cpi->twopass.frames_to_key)) {
1678     i++;
1679 
1680     /* Accumulate error score of frames in this gf group */
1681     mod_frame_err = calculate_modified_err(cpi, this_frame);
1682 
1683     gf_group_err += mod_frame_err;
1684 
1685     mod_err_per_mb_accumulator +=
1686         mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1687 
1688     if (EOF == input_stats(cpi, &next_frame)) break;
1689 
1690     /* Test for the case where there is a brief flash but the prediction
1691      * quality back to an earlier frame is then restored.
1692      */
1693     flash_detected = detect_flash(cpi, 0);
1694 
1695     /* Update the motion related elements to the boost calculation */
1696     accumulate_frame_motion_stats(
1697         &next_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1698         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1699 
1700     /* Calculate a baseline boost number for this frame */
1701     r = calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out);
1702 
1703     /* Cumulative effect of prediction quality decay */
1704     if (!flash_detected) {
1705       loop_decay_rate = get_prediction_decay_rate(&next_frame);
1706       decay_accumulator = decay_accumulator * loop_decay_rate;
1707       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1708     }
1709     boost_score += (decay_accumulator * r);
1710 
1711     /* Break clause to detect very still sections after motion
1712      * For example a staic image after a fade or other transition.
1713      */
1714     if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
1715                                    decay_accumulator)) {
1716       allow_alt_ref = 0;
1717       boost_score = old_boost_score;
1718       break;
1719     }
1720 
1721     /* Break out conditions. */
1722     if (
1723         /* Break at cpi->max_gf_interval unless almost totally static */
1724         (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1725         (
1726             /* Dont break out with a very short interval */
1727             (i > MIN_GF_INTERVAL) &&
1728             /* Dont break out very close to a key frame */
1729             ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1730             ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1731             (!flash_detected) &&
1732             ((mv_ratio_accumulator > 100.0) ||
1733              (abs_mv_in_out_accumulator > 3.0) ||
1734              (mv_in_out_accumulator < -2.0) ||
1735              ((boost_score - old_boost_score) < 2.0)))) {
1736       boost_score = old_boost_score;
1737       break;
1738     }
1739 
1740     memcpy(this_frame, &next_frame, sizeof(*this_frame));
1741 
1742     old_boost_score = boost_score;
1743   }
1744 
1745   cpi->twopass.gf_decay_rate =
1746       (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1747 
1748   /* When using CBR apply additional buffer related upper limits */
1749   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1750     double max_boost;
1751 
1752     /* For cbr apply buffer related limits */
1753     if (cpi->drop_frames_allowed) {
1754       int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1755                                 (cpi->oxcf.optimal_buffer_level / 100);
1756 
1757       if (cpi->buffer_level > df_buffer_level) {
1758         max_boost =
1759             ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
1760             DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1761       } else {
1762         max_boost = 0.0;
1763       }
1764     } else if (cpi->buffer_level > 0) {
1765       max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
1766                   DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1767     } else {
1768       max_boost = 0.0;
1769     }
1770 
1771     if (boost_score > max_boost) boost_score = max_boost;
1772   }
1773 
1774   /* Dont allow conventional gf too near the next kf */
1775   if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) {
1776     while (i < cpi->twopass.frames_to_key) {
1777       i++;
1778 
1779       if (EOF == input_stats(cpi, this_frame)) break;
1780 
1781       if (i < cpi->twopass.frames_to_key) {
1782         mod_frame_err = calculate_modified_err(cpi, this_frame);
1783         gf_group_err += mod_frame_err;
1784       }
1785     }
1786   }
1787 
1788   cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1789 
1790 #if NEW_BOOST
1791   /* Alterrnative boost calculation for alt ref */
1792   alt_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
1793 #endif
1794 
1795   /* Should we use the alternate refernce frame */
1796   if (allow_alt_ref && (i >= MIN_GF_INTERVAL) &&
1797       /* dont use ARF very near next kf */
1798       (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1799 #if NEW_BOOST
1800       ((next_frame.pcnt_inter > 0.75) || (next_frame.pcnt_second_ref > 0.5)) &&
1801       ((mv_in_out_accumulator / (double)i > -0.2) ||
1802        (mv_in_out_accumulator > -2.0)) &&
1803       (b_boost > 100) && (f_boost > 100))
1804 #else
1805       (next_frame.pcnt_inter > 0.75) &&
1806       ((mv_in_out_accumulator / (double)i > -0.2) ||
1807        (mv_in_out_accumulator > -2.0)) &&
1808       (cpi->gfu_boost > 100) &&
1809       (cpi->twopass.gf_decay_rate <=
1810        (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))))
1811 #endif
1812   {
1813     int Boost;
1814     int allocation_chunks;
1815     int Q =
1816         (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1817     int tmp_q;
1818     int arf_frame_bits = 0;
1819     int group_bits;
1820 
1821 #if NEW_BOOST
1822     cpi->gfu_boost = alt_boost;
1823 #endif
1824 
1825     /* Estimate the bits to be allocated to the group as a whole */
1826     if ((cpi->twopass.kf_group_bits > 0) &&
1827         (cpi->twopass.kf_group_error_left > 0)) {
1828       group_bits =
1829           (int)((double)cpi->twopass.kf_group_bits *
1830                 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1831     } else {
1832       group_bits = 0;
1833     }
1834 
1835 /* Boost for arf frame */
1836 #if NEW_BOOST
1837     Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1838 #else
1839     Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1840 #endif
1841     Boost += (i * 50);
1842 
1843     /* Set max and minimum boost and hence minimum allocation */
1844     if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
1845       Boost = ((cpi->baseline_gf_interval + 1) * 200);
1846     } else if (Boost < 125) {
1847       Boost = 125;
1848     }
1849 
1850     allocation_chunks = (i * 100) + Boost;
1851 
1852     /* Normalize Altboost and allocations chunck down to prevent overflow */
1853     while (Boost > 1000) {
1854       Boost /= 2;
1855       allocation_chunks /= 2;
1856     }
1857 
1858     /* Calculate the number of bits to be spent on the arf based on the
1859      * boost number
1860      */
1861     arf_frame_bits =
1862         (int)((double)Boost * (group_bits / (double)allocation_chunks));
1863 
1864     /* Estimate if there are enough bits available to make worthwhile use
1865      * of an arf.
1866      */
1867     tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
1868 
1869     /* Only use an arf if it is likely we will be able to code
1870      * it at a lower Q than the surrounding frames.
1871      */
1872     if (tmp_q < cpi->worst_quality) {
1873       int half_gf_int;
1874       int frames_after_arf;
1875       int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1876       int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1877 
1878       cpi->source_alt_ref_pending = 1;
1879 
1880       /*
1881        * For alt ref frames the error score for the end frame of the
1882        * group (the alt ref frame) should not contribute to the group
1883        * total and hence the number of bit allocated to the group.
1884        * Rather it forms part of the next group (it is the GF at the
1885        * start of the next group)
1886        * gf_group_err -= mod_frame_err;
1887        *
1888        * For alt ref frames alt ref frame is technically part of the
1889        * GF frame for the next group but we always base the error
1890        * calculation and bit allocation on the current group of frames.
1891        *
1892        * Set the interval till the next gf or arf.
1893        * For ARFs this is the number of frames to be coded before the
1894        * future frame that is coded as an ARF.
1895        * The future frame itself is part of the next group
1896        */
1897       cpi->baseline_gf_interval = i;
1898 
1899       /*
1900        * Define the arnr filter width for this group of frames:
1901        * We only filter frames that lie within a distance of half
1902        * the GF interval from the ARF frame. We also have to trap
1903        * cases where the filter extends beyond the end of clip.
1904        * Note: this_frame->frame has been updated in the loop
1905        * so it now points at the ARF frame.
1906        */
1907       half_gf_int = cpi->baseline_gf_interval >> 1;
1908       frames_after_arf =
1909           (int)(cpi->twopass.total_stats.count - this_frame->frame - 1);
1910 
1911       switch (cpi->oxcf.arnr_type) {
1912         case 1: /* Backward filter */
1913           frames_fwd = 0;
1914           if (frames_bwd > half_gf_int) frames_bwd = half_gf_int;
1915           break;
1916 
1917         case 2: /* Forward filter */
1918           if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
1919           if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
1920           frames_bwd = 0;
1921           break;
1922 
1923         case 3: /* Centered filter */
1924         default:
1925           frames_fwd >>= 1;
1926           if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
1927           if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
1928 
1929           frames_bwd = frames_fwd;
1930 
1931           /* For even length filter there is one more frame backward
1932            * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1933            */
1934           if (frames_bwd < half_gf_int) {
1935             frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
1936           }
1937           break;
1938       }
1939 
1940       cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1941     } else {
1942       cpi->source_alt_ref_pending = 0;
1943       cpi->baseline_gf_interval = i;
1944     }
1945   } else {
1946     cpi->source_alt_ref_pending = 0;
1947     cpi->baseline_gf_interval = i;
1948   }
1949 
1950   /*
1951    * Now decide how many bits should be allocated to the GF group as  a
1952    * proportion of those remaining in the kf group.
1953    * The final key frame group in the clip is treated as a special case
1954    * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
1955    * This is also important for short clips where there may only be one
1956    * key frame.
1957    */
1958   if (cpi->twopass.frames_to_key >=
1959       (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame)) {
1960     cpi->twopass.kf_group_bits =
1961         (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
1962   }
1963 
1964   /* Calculate the bits to be allocated to the group as a whole */
1965   if ((cpi->twopass.kf_group_bits > 0) &&
1966       (cpi->twopass.kf_group_error_left > 0)) {
1967     cpi->twopass.gf_group_bits =
1968         (int64_t)(cpi->twopass.kf_group_bits *
1969                   (gf_group_err / cpi->twopass.kf_group_error_left));
1970   } else {
1971     cpi->twopass.gf_group_bits = 0;
1972   }
1973 
1974   cpi->twopass.gf_group_bits =
1975       (cpi->twopass.gf_group_bits < 0)
1976           ? 0
1977           : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
1978                 ? cpi->twopass.kf_group_bits
1979                 : cpi->twopass.gf_group_bits;
1980 
1981   /* Clip cpi->twopass.gf_group_bits based on user supplied data rate
1982    * variability limit (cpi->oxcf.two_pass_vbrmax_section)
1983    */
1984   if (cpi->twopass.gf_group_bits >
1985       (int64_t)max_bits * cpi->baseline_gf_interval) {
1986     cpi->twopass.gf_group_bits = (int64_t)max_bits * cpi->baseline_gf_interval;
1987   }
1988 
1989   /* Reset the file position */
1990   reset_fpf_position(cpi, start_pos);
1991 
1992   /* Update the record of error used so far (only done once per gf group) */
1993   cpi->twopass.modified_error_used += gf_group_err;
1994 
1995   /* Assign  bits to the arf or gf. */
1996   for (i = 0; i <= (cpi->source_alt_ref_pending &&
1997                     cpi->common.frame_type != KEY_FRAME);
1998        i++) {
1999     int Boost;
2000     int allocation_chunks;
2001     int Q =
2002         (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
2003     int gf_bits;
2004 
2005     /* For ARF frames */
2006     if (cpi->source_alt_ref_pending && i == 0) {
2007 #if NEW_BOOST
2008       Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
2009 #else
2010       Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
2011 #endif
2012       Boost += (cpi->baseline_gf_interval * 50);
2013 
2014       /* Set max and minimum boost and hence minimum allocation */
2015       if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
2016         Boost = ((cpi->baseline_gf_interval + 1) * 200);
2017       } else if (Boost < 125) {
2018         Boost = 125;
2019       }
2020 
2021       allocation_chunks = ((cpi->baseline_gf_interval + 1) * 100) + Boost;
2022     }
2023     /* Else for standard golden frames */
2024     else {
2025       /* boost based on inter / intra ratio of subsequent frames */
2026       Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
2027 
2028       /* Set max and minimum boost and hence minimum allocation */
2029       if (Boost > (cpi->baseline_gf_interval * 150)) {
2030         Boost = (cpi->baseline_gf_interval * 150);
2031       } else if (Boost < 125) {
2032         Boost = 125;
2033       }
2034 
2035       allocation_chunks = (cpi->baseline_gf_interval * 100) + (Boost - 100);
2036     }
2037 
2038     /* Normalize Altboost and allocations chunck down to prevent overflow */
2039     while (Boost > 1000) {
2040       Boost /= 2;
2041       allocation_chunks /= 2;
2042     }
2043 
2044     /* Calculate the number of bits to be spent on the gf or arf based on
2045      * the boost number
2046      */
2047     gf_bits = (int)((double)Boost *
2048                     (cpi->twopass.gf_group_bits / (double)allocation_chunks));
2049 
2050     /* If the frame that is to be boosted is simpler than the average for
2051      * the gf/arf group then use an alternative calculation
2052      * based on the error score of the frame itself
2053      */
2054     if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) {
2055       double alt_gf_grp_bits;
2056       int alt_gf_bits;
2057 
2058       alt_gf_grp_bits =
2059           (double)cpi->twopass.kf_group_bits *
2060           (mod_frame_err * (double)cpi->baseline_gf_interval) /
2061           DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
2062 
2063       alt_gf_bits =
2064           (int)((double)Boost * (alt_gf_grp_bits / (double)allocation_chunks));
2065 
2066       if (gf_bits > alt_gf_bits) {
2067         gf_bits = alt_gf_bits;
2068       }
2069     }
2070     /* Else if it is harder than other frames in the group make sure it at
2071      * least receives an allocation in keeping with its relative error
2072      * score, otherwise it may be worse off than an "un-boosted" frame
2073      */
2074     else {
2075       // Avoid division by 0 by clamping cpi->twopass.kf_group_error_left to 1
2076       int alt_gf_bits =
2077           (int)((double)cpi->twopass.kf_group_bits * mod_frame_err /
2078                 (double)VPXMAX(cpi->twopass.kf_group_error_left, 1));
2079 
2080       if (alt_gf_bits > gf_bits) {
2081         gf_bits = alt_gf_bits;
2082       }
2083     }
2084 
2085     /* Apply an additional limit for CBR */
2086     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2087       if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1)) {
2088         cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1);
2089       }
2090     }
2091 
2092     /* Dont allow a negative value for gf_bits */
2093     if (gf_bits < 0) gf_bits = 0;
2094 
2095     /* Add in minimum for a frame */
2096     gf_bits += cpi->min_frame_bandwidth;
2097 
2098     if (i == 0) {
2099       cpi->twopass.gf_bits = gf_bits;
2100     }
2101     if (i == 1 || (!cpi->source_alt_ref_pending &&
2102                    (cpi->common.frame_type != KEY_FRAME))) {
2103       /* Per frame bit target for this frame */
2104       cpi->per_frame_bandwidth = gf_bits;
2105     }
2106   }
2107 
2108   {
2109     /* Adjust KF group bits and error remainin */
2110     cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
2111     cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
2112 
2113     if (cpi->twopass.kf_group_bits < 0) cpi->twopass.kf_group_bits = 0;
2114 
2115     /* Note the error score left in the remaining frames of the group.
2116      * For normal GFs we want to remove the error score for the first
2117      * frame of the group (except in Key frame case where this has
2118      * already happened)
2119      */
2120     if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) {
2121       cpi->twopass.gf_group_error_left =
2122           (int)(gf_group_err - gf_first_frame_err);
2123     } else {
2124       cpi->twopass.gf_group_error_left = (int)gf_group_err;
2125     }
2126 
2127     cpi->twopass.gf_group_bits -=
2128         cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
2129 
2130     if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
2131 
2132     /* This condition could fail if there are two kfs very close together
2133      * despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the
2134      * calculation of cpi->twopass.alt_extra_bits.
2135      */
2136     if (cpi->baseline_gf_interval >= 3) {
2137 #if NEW_BOOST
2138       int boost = (cpi->source_alt_ref_pending) ? b_boost : cpi->gfu_boost;
2139 #else
2140       int boost = cpi->gfu_boost;
2141 #endif
2142       if (boost >= 150) {
2143         int pct_extra;
2144 
2145         pct_extra = (boost - 100) / 50;
2146         pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2147 
2148         cpi->twopass.alt_extra_bits =
2149             (int)(cpi->twopass.gf_group_bits * pct_extra) / 100;
2150         cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
2151         cpi->twopass.alt_extra_bits /= ((cpi->baseline_gf_interval - 1) >> 1);
2152       } else {
2153         cpi->twopass.alt_extra_bits = 0;
2154       }
2155     } else {
2156       cpi->twopass.alt_extra_bits = 0;
2157     }
2158   }
2159 
2160   /* Adjustments based on a measure of complexity of the section */
2161   if (cpi->common.frame_type != KEY_FRAME) {
2162     FIRSTPASS_STATS sectionstats;
2163     double Ratio;
2164 
2165     zero_stats(&sectionstats);
2166     reset_fpf_position(cpi, start_pos);
2167 
2168     for (i = 0; i < cpi->baseline_gf_interval; ++i) {
2169       input_stats(cpi, &next_frame);
2170       accumulate_stats(&sectionstats, &next_frame);
2171     }
2172 
2173     avg_stats(&sectionstats);
2174 
2175     cpi->twopass.section_intra_rating =
2176         (unsigned int)(sectionstats.intra_error /
2177                        DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2178 
2179     Ratio = sectionstats.intra_error /
2180             DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2181     cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2182 
2183     if (cpi->twopass.section_max_qfactor < 0.80) {
2184       cpi->twopass.section_max_qfactor = 0.80;
2185     }
2186 
2187     reset_fpf_position(cpi, start_pos);
2188   }
2189 }
2190 
2191 /* Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2192  */
assign_std_frame_bits(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame)2193 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2194   int target_frame_size;
2195 
2196   double modified_err;
2197   double err_fraction;
2198 
2199   int max_bits = frame_max_bits(cpi); /* Max for a single frame */
2200 
2201   /* Calculate modified prediction error used in bit allocation */
2202   modified_err = calculate_modified_err(cpi, this_frame);
2203 
2204   /* What portion of the remaining GF group error is used by this frame */
2205   if (cpi->twopass.gf_group_error_left > 0) {
2206     err_fraction = modified_err / cpi->twopass.gf_group_error_left;
2207   } else {
2208     err_fraction = 0.0;
2209   }
2210 
2211   /* How many of those bits available for allocation should we give it? */
2212   target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
2213 
2214   /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits)
2215    * at the top end.
2216    */
2217   if (target_frame_size < 0) {
2218     target_frame_size = 0;
2219   } else {
2220     if (target_frame_size > max_bits) target_frame_size = max_bits;
2221 
2222     if (target_frame_size > cpi->twopass.gf_group_bits) {
2223       target_frame_size = (int)cpi->twopass.gf_group_bits;
2224     }
2225   }
2226 
2227   /* Adjust error and bits remaining */
2228   cpi->twopass.gf_group_error_left -= (int)modified_err;
2229   cpi->twopass.gf_group_bits -= target_frame_size;
2230 
2231   if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
2232 
2233   /* Add in the minimum number of bits that is set aside for every frame. */
2234   target_frame_size += cpi->min_frame_bandwidth;
2235 
2236   /* Every other frame gets a few extra bits */
2237   if ((cpi->frames_since_golden & 0x01) &&
2238       (cpi->frames_till_gf_update_due > 0)) {
2239     target_frame_size += cpi->twopass.alt_extra_bits;
2240   }
2241 
2242   /* Per frame bit target for this frame */
2243   cpi->per_frame_bandwidth = target_frame_size;
2244 }
2245 
vp8_second_pass(VP8_COMP * cpi)2246 void vp8_second_pass(VP8_COMP *cpi) {
2247   int tmp_q;
2248   int frames_left =
2249       (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
2250 
2251   FIRSTPASS_STATS this_frame;
2252   FIRSTPASS_STATS this_frame_copy;
2253 
2254   double this_frame_intra_error;
2255   double this_frame_coded_error;
2256 
2257   int overhead_bits;
2258 
2259   vp8_zero(this_frame);
2260 
2261   if (!cpi->twopass.stats_in) {
2262     return;
2263   }
2264 
2265   vpx_clear_system_state();
2266 
2267   if (EOF == input_stats(cpi, &this_frame)) return;
2268 
2269   this_frame_intra_error = this_frame.intra_error;
2270   this_frame_coded_error = this_frame.coded_error;
2271 
2272   /* keyframe and section processing ! */
2273   if (cpi->twopass.frames_to_key == 0) {
2274     /* Define next KF group and assign bits to it */
2275     memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2276     find_next_key_frame(cpi, &this_frame_copy);
2277 
2278     /* Special case: Error error_resilient_mode mode does not make much
2279      * sense for two pass but with its current meaning this code is
2280      * designed to stop outlandish behaviour if someone does set it when
2281      * using two pass. It effectively disables GF groups. This is
2282      * temporary code until we decide what should really happen in this
2283      * case.
2284      */
2285     if (cpi->oxcf.error_resilient_mode) {
2286       cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
2287       cpi->twopass.gf_group_error_left = (int)cpi->twopass.kf_group_error_left;
2288       cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
2289       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2290       cpi->source_alt_ref_pending = 0;
2291     }
2292   }
2293 
2294   /* Is this a GF / ARF (Note that a KF is always also a GF) */
2295   if (cpi->frames_till_gf_update_due == 0) {
2296     /* Define next gf group and assign bits to it */
2297     memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2298     define_gf_group(cpi, &this_frame_copy);
2299 
2300     /* If we are going to code an altref frame at the end of the group
2301      * and the current frame is not a key frame.... If the previous
2302      * group used an arf this frame has already benefited from that arf
2303      * boost and it should not be given extra bits If the previous
2304      * group was NOT coded using arf we may want to apply some boost to
2305      * this GF as well
2306      */
2307     if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) {
2308       /* Assign a standard frames worth of bits from those allocated
2309        * to the GF group
2310        */
2311       int bak = cpi->per_frame_bandwidth;
2312       memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2313       assign_std_frame_bits(cpi, &this_frame_copy);
2314       cpi->per_frame_bandwidth = bak;
2315     }
2316   }
2317 
2318   /* Otherwise this is an ordinary frame */
2319   else {
2320     /* Special case: Error error_resilient_mode mode does not make much
2321      * sense for two pass but with its current meaning but this code is
2322      * designed to stop outlandish behaviour if someone does set it
2323      * when using two pass. It effectively disables GF groups. This is
2324      * temporary code till we decide what should really happen in this
2325      * case.
2326      */
2327     if (cpi->oxcf.error_resilient_mode) {
2328       cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
2329 
2330       if (cpi->common.frame_type != KEY_FRAME) {
2331         /* Assign bits from those allocated to the GF group */
2332         memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2333         assign_std_frame_bits(cpi, &this_frame_copy);
2334       }
2335     } else {
2336       /* Assign bits from those allocated to the GF group */
2337       memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2338       assign_std_frame_bits(cpi, &this_frame_copy);
2339     }
2340   }
2341 
2342   /* Keep a globally available copy of this and the next frame's iiratio. */
2343   cpi->twopass.this_iiratio =
2344       (unsigned int)(this_frame_intra_error /
2345                      DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
2346   {
2347     FIRSTPASS_STATS next_frame;
2348     if (lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2349       cpi->twopass.next_iiratio =
2350           (unsigned int)(next_frame.intra_error /
2351                          DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2352     }
2353   }
2354 
2355   /* Set nominal per second bandwidth for this frame */
2356   cpi->target_bandwidth =
2357       (int)(cpi->per_frame_bandwidth * cpi->output_framerate);
2358   if (cpi->target_bandwidth < 0) cpi->target_bandwidth = 0;
2359 
2360   /* Account for mv, mode and other overheads. */
2361   overhead_bits = (int)estimate_modemvcost(cpi, &cpi->twopass.total_left_stats);
2362 
2363   /* Special case code for first frame. */
2364   if (cpi->common.current_video_frame == 0) {
2365     cpi->twopass.est_max_qcorrection_factor = 1.0;
2366 
2367     /* Set a cq_level in constrained quality mode. */
2368     if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2369       int est_cq;
2370 
2371       est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats,
2372                            (int)(cpi->twopass.bits_left / frames_left),
2373                            overhead_bits);
2374 
2375       cpi->cq_target_quality = cpi->oxcf.cq_level;
2376       if (est_cq > cpi->cq_target_quality) cpi->cq_target_quality = est_cq;
2377     }
2378 
2379     /* guess at maxq needed in 2nd pass */
2380     cpi->twopass.maxq_max_limit = cpi->worst_quality;
2381     cpi->twopass.maxq_min_limit = cpi->best_quality;
2382 
2383     tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
2384                            (int)(cpi->twopass.bits_left / frames_left),
2385                            overhead_bits);
2386 
2387     /* Limit the maxq value returned subsequently.
2388      * This increases the risk of overspend or underspend if the initial
2389      * estimate for the clip is bad, but helps prevent excessive
2390      * variation in Q, especially near the end of a clip
2391      * where for example a small overspend may cause Q to crash
2392      */
2393     cpi->twopass.maxq_max_limit =
2394         ((tmp_q + 32) < cpi->worst_quality) ? (tmp_q + 32) : cpi->worst_quality;
2395     cpi->twopass.maxq_min_limit =
2396         ((tmp_q - 32) > cpi->best_quality) ? (tmp_q - 32) : cpi->best_quality;
2397 
2398     cpi->active_worst_quality = tmp_q;
2399     cpi->ni_av_qi = tmp_q;
2400   }
2401 
2402   /* The last few frames of a clip almost always have to few or too many
2403    * bits and for the sake of over exact rate control we dont want to make
2404    * radical adjustments to the allowed quantizer range just to use up a
2405    * few surplus bits or get beneath the target rate.
2406    */
2407   else if ((cpi->common.current_video_frame <
2408             (((unsigned int)cpi->twopass.total_stats.count * 255) >> 8)) &&
2409            ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2410             (unsigned int)cpi->twopass.total_stats.count)) {
2411     if (frames_left < 1) frames_left = 1;
2412 
2413     tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
2414                            (int)(cpi->twopass.bits_left / frames_left),
2415                            overhead_bits);
2416 
2417     /* Move active_worst_quality but in a damped way */
2418     if (tmp_q > cpi->active_worst_quality) {
2419       cpi->active_worst_quality++;
2420     } else if (tmp_q < cpi->active_worst_quality) {
2421       cpi->active_worst_quality--;
2422     }
2423 
2424     cpi->active_worst_quality =
2425         ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2426   }
2427 
2428   cpi->twopass.frames_to_key--;
2429 
2430   /* Update the total stats remaining sturcture */
2431   subtract_stats(&cpi->twopass.total_left_stats, &this_frame);
2432 }
2433 
test_candidate_kf(VP8_COMP * cpi,FIRSTPASS_STATS * last_frame,FIRSTPASS_STATS * this_frame,FIRSTPASS_STATS * next_frame)2434 static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame,
2435                              FIRSTPASS_STATS *this_frame,
2436                              FIRSTPASS_STATS *next_frame) {
2437   int is_viable_kf = 0;
2438 
2439   /* Does the frame satisfy the primary criteria of a key frame
2440    *      If so, then examine how well it predicts subsequent frames
2441    */
2442   if ((this_frame->pcnt_second_ref < 0.10) &&
2443       (next_frame->pcnt_second_ref < 0.10) &&
2444       ((this_frame->pcnt_inter < 0.05) ||
2445        (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2446         ((this_frame->intra_error /
2447           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2448         ((fabs(last_frame->coded_error - this_frame->coded_error) /
2449               DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
2450           .40) ||
2451          (fabs(last_frame->intra_error - this_frame->intra_error) /
2452               DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
2453           .40) ||
2454          ((next_frame->intra_error /
2455            DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
2456     int i;
2457     FIRSTPASS_STATS *start_pos;
2458 
2459     FIRSTPASS_STATS local_next_frame;
2460 
2461     double boost_score = 0.0;
2462     double old_boost_score = 0.0;
2463     double decay_accumulator = 1.0;
2464     double next_iiratio;
2465 
2466     memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2467 
2468     /* Note the starting file position so we can reset to it */
2469     start_pos = cpi->twopass.stats_in;
2470 
2471     /* Examine how well the key frame predicts subsequent frames */
2472     for (i = 0; i < 16; ++i) {
2473       next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
2474                       DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
2475 
2476       if (next_iiratio > RMAX) next_iiratio = RMAX;
2477 
2478       /* Cumulative effect of decay in prediction quality */
2479       if (local_next_frame.pcnt_inter > 0.85) {
2480         decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2481       } else {
2482         decay_accumulator =
2483             decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2484       }
2485 
2486       /* Keep a running total */
2487       boost_score += (decay_accumulator * next_iiratio);
2488 
2489       /* Test various breakout clauses */
2490       if ((local_next_frame.pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
2491           (((local_next_frame.pcnt_inter - local_next_frame.pcnt_neutral) <
2492             0.20) &&
2493            (next_iiratio < 3.0)) ||
2494           ((boost_score - old_boost_score) < 0.5) ||
2495           (local_next_frame.intra_error < 200)) {
2496         break;
2497       }
2498 
2499       old_boost_score = boost_score;
2500 
2501       /* Get the next frame details */
2502       if (EOF == input_stats(cpi, &local_next_frame)) break;
2503     }
2504 
2505     /* If there is tolerable prediction for at least the next 3 frames
2506      * then break out else discard this pottential key frame and move on
2507      */
2508     if (boost_score > 5.0 && (i > 3)) {
2509       is_viable_kf = 1;
2510     } else {
2511       /* Reset the file position */
2512       reset_fpf_position(cpi, start_pos);
2513 
2514       is_viable_kf = 0;
2515     }
2516   }
2517 
2518   return is_viable_kf;
2519 }
find_next_key_frame(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame)2520 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2521   int i, j;
2522   FIRSTPASS_STATS last_frame;
2523   FIRSTPASS_STATS first_frame;
2524   FIRSTPASS_STATS next_frame;
2525   FIRSTPASS_STATS *start_position;
2526 
2527   double decay_accumulator = 1.0;
2528   double boost_score = 0;
2529   double old_boost_score = 0.0;
2530   double loop_decay_rate;
2531 
2532   double kf_mod_err = 0.0;
2533   double kf_group_err = 0.0;
2534   double kf_group_intra_err = 0.0;
2535   double kf_group_coded_err = 0.0;
2536   double recent_loop_decay[8] = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
2537 
2538   memset(&next_frame, 0, sizeof(next_frame));
2539 
2540   vpx_clear_system_state();
2541   start_position = cpi->twopass.stats_in;
2542 
2543   cpi->common.frame_type = KEY_FRAME;
2544 
2545   /* is this a forced key frame by interval */
2546   cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2547 
2548   /* Clear the alt ref active flag as this can never be active on a key
2549    * frame
2550    */
2551   cpi->source_alt_ref_active = 0;
2552 
2553   /* Kf is always a gf so clear frames till next gf counter */
2554   cpi->frames_till_gf_update_due = 0;
2555 
2556   cpi->twopass.frames_to_key = 1;
2557 
2558   /* Take a copy of the initial frame details */
2559   memcpy(&first_frame, this_frame, sizeof(*this_frame));
2560 
2561   cpi->twopass.kf_group_bits = 0;
2562   cpi->twopass.kf_group_error_left = 0;
2563 
2564   kf_mod_err = calculate_modified_err(cpi, this_frame);
2565 
2566   /* find the next keyframe */
2567   i = 0;
2568   while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) {
2569     /* Accumulate kf group error */
2570     kf_group_err += calculate_modified_err(cpi, this_frame);
2571 
2572     /* These figures keep intra and coded error counts for all frames
2573      * including key frames in the group. The effect of the key frame
2574      * itself can be subtracted out using the first_frame data
2575      * collected above
2576      */
2577     kf_group_intra_err += this_frame->intra_error;
2578     kf_group_coded_err += this_frame->coded_error;
2579 
2580     /* Load the next frame's stats. */
2581     memcpy(&last_frame, this_frame, sizeof(*this_frame));
2582     input_stats(cpi, this_frame);
2583 
2584     /* Provided that we are not at the end of the file... */
2585     if (cpi->oxcf.auto_key &&
2586         lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2587       /* Normal scene cut check */
2588       if ((i >= MIN_GF_INTERVAL) &&
2589           test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) {
2590         break;
2591       }
2592 
2593       /* How fast is prediction quality decaying */
2594       loop_decay_rate = get_prediction_decay_rate(&next_frame);
2595 
2596       /* We want to know something about the recent past... rather than
2597        * as used elsewhere where we are concened with decay in prediction
2598        * quality since the last GF or KF.
2599        */
2600       recent_loop_decay[i % 8] = loop_decay_rate;
2601       decay_accumulator = 1.0;
2602       for (j = 0; j < 8; ++j) {
2603         decay_accumulator = decay_accumulator * recent_loop_decay[j];
2604       }
2605 
2606       /* Special check for transition or high motion followed by a
2607        * static scene.
2608        */
2609       if (detect_transition_to_still(cpi, i,
2610                                      ((int)(cpi->key_frame_frequency) - (int)i),
2611                                      loop_decay_rate, decay_accumulator)) {
2612         break;
2613       }
2614 
2615       /* Step on to the next frame */
2616       cpi->twopass.frames_to_key++;
2617 
2618       /* If we don't have a real key frame within the next two
2619        * forcekeyframeevery intervals then break out of the loop.
2620        */
2621       if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency) {
2622         break;
2623       }
2624     } else {
2625       cpi->twopass.frames_to_key++;
2626     }
2627 
2628     i++;
2629   }
2630 
2631   /* If there is a max kf interval set by the user we must obey it.
2632    * We already breakout of the loop above at 2x max.
2633    * This code centers the extra kf if the actual natural
2634    * interval is between 1x and 2x
2635    */
2636   if (cpi->oxcf.auto_key &&
2637       cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) {
2638     FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2639     FIRSTPASS_STATS tmp_frame;
2640 
2641     cpi->twopass.frames_to_key /= 2;
2642 
2643     /* Copy first frame details */
2644     memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2645 
2646     /* Reset to the start of the group */
2647     reset_fpf_position(cpi, start_position);
2648 
2649     kf_group_err = 0;
2650     kf_group_intra_err = 0;
2651     kf_group_coded_err = 0;
2652 
2653     /* Rescan to get the correct error data for the forced kf group */
2654     for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
2655       /* Accumulate kf group errors */
2656       kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2657       kf_group_intra_err += tmp_frame.intra_error;
2658       kf_group_coded_err += tmp_frame.coded_error;
2659 
2660       /* Load a the next frame's stats */
2661       input_stats(cpi, &tmp_frame);
2662     }
2663 
2664     /* Reset to the start of the group */
2665     reset_fpf_position(cpi, current_pos);
2666 
2667     cpi->next_key_frame_forced = 1;
2668   } else {
2669     cpi->next_key_frame_forced = 0;
2670   }
2671 
2672   /* Special case for the last frame of the file */
2673   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
2674     /* Accumulate kf group error */
2675     kf_group_err += calculate_modified_err(cpi, this_frame);
2676 
2677     /* These figures keep intra and coded error counts for all frames
2678      * including key frames in the group. The effect of the key frame
2679      * itself can be subtracted out using the first_frame data
2680      * collected above
2681      */
2682     kf_group_intra_err += this_frame->intra_error;
2683     kf_group_coded_err += this_frame->coded_error;
2684   }
2685 
2686   /* Calculate the number of bits that should be assigned to the kf group. */
2687   if ((cpi->twopass.bits_left > 0) &&
2688       (cpi->twopass.modified_error_left > 0.0)) {
2689     /* Max for a single normal frame (not key frame) */
2690     int max_bits = frame_max_bits(cpi);
2691 
2692     /* Maximum bits for the kf group */
2693     int64_t max_grp_bits;
2694 
2695     /* Default allocation based on bits left and relative
2696      * complexity of the section
2697      */
2698     cpi->twopass.kf_group_bits =
2699         (int64_t)(cpi->twopass.bits_left *
2700                   (kf_group_err / cpi->twopass.modified_error_left));
2701 
2702     /* Clip based on maximum per frame rate defined by the user. */
2703     max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2704     if (cpi->twopass.kf_group_bits > max_grp_bits) {
2705       cpi->twopass.kf_group_bits = max_grp_bits;
2706     }
2707 
2708     /* Additional special case for CBR if buffer is getting full. */
2709     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2710       int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2711       int64_t buffer_lvl = cpi->buffer_level;
2712 
2713       /* If the buffer is near or above the optimal and this kf group is
2714        * not being allocated much then increase the allocation a bit.
2715        */
2716       if (buffer_lvl >= opt_buffer_lvl) {
2717         int64_t high_water_mark =
2718             (opt_buffer_lvl + cpi->oxcf.maximum_buffer_size) >> 1;
2719 
2720         int64_t av_group_bits;
2721 
2722         /* Av bits per frame * number of frames */
2723         av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
2724                         (int64_t)cpi->twopass.frames_to_key;
2725 
2726         /* We are at or above the maximum. */
2727         if (cpi->buffer_level >= high_water_mark) {
2728           int64_t min_group_bits;
2729 
2730           min_group_bits =
2731               av_group_bits + (int64_t)(buffer_lvl - high_water_mark);
2732 
2733           if (cpi->twopass.kf_group_bits < min_group_bits) {
2734             cpi->twopass.kf_group_bits = min_group_bits;
2735           }
2736         }
2737         /* We are above optimal but below the maximum */
2738         else if (cpi->twopass.kf_group_bits < av_group_bits) {
2739           int64_t bits_below_av = av_group_bits - cpi->twopass.kf_group_bits;
2740 
2741           cpi->twopass.kf_group_bits += (int64_t)(
2742               (double)bits_below_av * (double)(buffer_lvl - opt_buffer_lvl) /
2743               (double)(high_water_mark - opt_buffer_lvl));
2744         }
2745       }
2746     }
2747   } else {
2748     cpi->twopass.kf_group_bits = 0;
2749   }
2750 
2751   /* Reset the first pass file position */
2752   reset_fpf_position(cpi, start_position);
2753 
2754   /* determine how big to make this keyframe based on how well the
2755    * subsequent frames use inter blocks
2756    */
2757   decay_accumulator = 1.0;
2758   boost_score = 0.0;
2759 
2760   for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
2761     double r;
2762 
2763     if (EOF == input_stats(cpi, &next_frame)) break;
2764 
2765     if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) {
2766       r = (IIKFACTOR2 * next_frame.intra_error /
2767            DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2768     } else {
2769       r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2770            DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2771     }
2772 
2773     if (r > RMAX) r = RMAX;
2774 
2775     /* How fast is prediction quality decaying */
2776     loop_decay_rate = get_prediction_decay_rate(&next_frame);
2777 
2778     decay_accumulator = decay_accumulator * loop_decay_rate;
2779     decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2780 
2781     boost_score += (decay_accumulator * r);
2782 
2783     if ((i > MIN_GF_INTERVAL) && ((boost_score - old_boost_score) < 1.0)) {
2784       break;
2785     }
2786 
2787     old_boost_score = boost_score;
2788   }
2789 
2790   if (1) {
2791     FIRSTPASS_STATS sectionstats;
2792     double Ratio;
2793 
2794     zero_stats(&sectionstats);
2795     reset_fpf_position(cpi, start_position);
2796 
2797     for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
2798       input_stats(cpi, &next_frame);
2799       accumulate_stats(&sectionstats, &next_frame);
2800     }
2801 
2802     avg_stats(&sectionstats);
2803 
2804     cpi->twopass.section_intra_rating =
2805         (unsigned int)(sectionstats.intra_error /
2806                        DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2807 
2808     Ratio = sectionstats.intra_error /
2809             DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2810     cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2811 
2812     if (cpi->twopass.section_max_qfactor < 0.80) {
2813       cpi->twopass.section_max_qfactor = 0.80;
2814     }
2815   }
2816 
2817   /* When using CBR apply additional buffer fullness related upper limits */
2818   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2819     double max_boost;
2820 
2821     if (cpi->drop_frames_allowed) {
2822       int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark *
2823                                   (cpi->oxcf.optimal_buffer_level / 100));
2824 
2825       if (cpi->buffer_level > df_buffer_level) {
2826         max_boost =
2827             ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
2828             DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2829       } else {
2830         max_boost = 0.0;
2831       }
2832     } else if (cpi->buffer_level > 0) {
2833       max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
2834                   DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2835     } else {
2836       max_boost = 0.0;
2837     }
2838 
2839     if (boost_score > max_boost) boost_score = max_boost;
2840   }
2841 
2842   /* Reset the first pass file position */
2843   reset_fpf_position(cpi, start_position);
2844 
2845   /* Work out how many bits to allocate for the key frame itself */
2846   if (1) {
2847     int kf_boost = (int)boost_score;
2848     int allocation_chunks;
2849     int Counter = cpi->twopass.frames_to_key;
2850     int alt_kf_bits;
2851     YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2852 /* Min boost based on kf interval */
2853 #if 0
2854 
2855         while ((kf_boost < 48) && (Counter > 0))
2856         {
2857             Counter -= 2;
2858             kf_boost ++;
2859         }
2860 
2861 #endif
2862 
2863     if (kf_boost < 48) {
2864       kf_boost += ((Counter + 1) >> 1);
2865 
2866       if (kf_boost > 48) kf_boost = 48;
2867     }
2868 
2869     /* bigger frame sizes need larger kf boosts, smaller frames smaller
2870      * boosts...
2871      */
2872     if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240)) {
2873       kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2874     } else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240)) {
2875       kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2876     }
2877 
2878     /* Min KF boost */
2879     kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */
2880     if (kf_boost < 250) kf_boost = 250;
2881 
2882     /*
2883      * We do three calculations for kf size.
2884      * The first is based on the error score for the whole kf group.
2885      * The second (optionaly) on the key frames own error if this is
2886      * smaller than the average for the group.
2887      * The final one insures that the frame receives at least the
2888      * allocation it would have received based on its own error score vs
2889      * the error score remaining
2890      * Special case if the sequence appears almost totaly static
2891      * as measured by the decay accumulator. In this case we want to
2892      * spend almost all of the bits on the key frame.
2893      * cpi->twopass.frames_to_key-1 because key frame itself is taken
2894      * care of by kf_boost.
2895      */
2896     if (decay_accumulator >= 0.99) {
2897       allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
2898     } else {
2899       allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
2900     }
2901 
2902     /* Normalize Altboost and allocations chunck down to prevent overflow */
2903     while (kf_boost > 1000) {
2904       kf_boost /= 2;
2905       allocation_chunks /= 2;
2906     }
2907 
2908     cpi->twopass.kf_group_bits =
2909         (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
2910 
2911     /* Calculate the number of bits to be spent on the key frame */
2912     cpi->twopass.kf_bits =
2913         (int)((double)kf_boost *
2914               ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
2915 
2916     /* Apply an additional limit for CBR */
2917     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2918       if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2)) {
2919         cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2);
2920       }
2921     }
2922 
2923     /* If the key frame is actually easier than the average for the
2924      * kf group (which does sometimes happen... eg a blank intro frame)
2925      * Then use an alternate calculation based on the kf error score
2926      * which should give a smaller key frame.
2927      */
2928     if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) {
2929       double alt_kf_grp_bits =
2930           ((double)cpi->twopass.bits_left *
2931            (kf_mod_err * (double)cpi->twopass.frames_to_key) /
2932            DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
2933 
2934       alt_kf_bits = (int)((double)kf_boost *
2935                           (alt_kf_grp_bits / (double)allocation_chunks));
2936 
2937       if (cpi->twopass.kf_bits > alt_kf_bits) {
2938         cpi->twopass.kf_bits = alt_kf_bits;
2939       }
2940     }
2941     /* Else if it is much harder than other frames in the group make sure
2942      * it at least receives an allocation in keeping with its relative
2943      * error score
2944      */
2945     else {
2946       alt_kf_bits = (int)((double)cpi->twopass.bits_left *
2947                           (kf_mod_err / DOUBLE_DIVIDE_CHECK(
2948                                             cpi->twopass.modified_error_left)));
2949 
2950       if (alt_kf_bits > cpi->twopass.kf_bits) {
2951         cpi->twopass.kf_bits = alt_kf_bits;
2952       }
2953     }
2954 
2955     cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
2956     /* Add in the minimum frame allowance */
2957     cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
2958 
2959     /* Peer frame bit target for this frame */
2960     cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
2961 
2962     /* Convert to a per second bitrate */
2963     cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * cpi->output_framerate);
2964   }
2965 
2966   /* Note the total error score of the kf group minus the key frame itself */
2967   cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2968 
2969   /* Adjust the count of total modified error left. The count of bits left
2970    * is adjusted elsewhere based on real coded frame sizes
2971    */
2972   cpi->twopass.modified_error_left -= kf_group_err;
2973 
2974   if (cpi->oxcf.allow_spatial_resampling) {
2975     int resample_trigger = 0;
2976     int last_kf_resampled = 0;
2977     int kf_q;
2978     int scale_val = 0;
2979     int hr, hs, vr, vs;
2980     int new_width = cpi->oxcf.Width;
2981     int new_height = cpi->oxcf.Height;
2982 
2983     int projected_buffer_level;
2984     int tmp_q;
2985 
2986     double projected_bits_perframe;
2987     double group_iiratio = (kf_group_intra_err - first_frame.intra_error) /
2988                            (kf_group_coded_err - first_frame.coded_error);
2989     double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
2990     double bits_per_frame;
2991     double av_bits_per_frame;
2992     double effective_size_ratio;
2993 
2994     if ((cpi->common.Width != cpi->oxcf.Width) ||
2995         (cpi->common.Height != cpi->oxcf.Height)) {
2996       last_kf_resampled = 1;
2997     }
2998 
2999     /* Set back to unscaled by defaults */
3000     cpi->common.horiz_scale = NORMAL;
3001     cpi->common.vert_scale = NORMAL;
3002 
3003     /* Calculate Average bits per frame. */
3004     av_bits_per_frame = cpi->oxcf.target_bandwidth /
3005                         DOUBLE_DIVIDE_CHECK((double)cpi->framerate);
3006 
3007     /* CBR... Use the clip average as the target for deciding resample */
3008     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3009       bits_per_frame = av_bits_per_frame;
3010     }
3011 
3012     /* In VBR we want to avoid downsampling in easy section unless we
3013      * are under extreme pressure So use the larger of target bitrate
3014      * for this section or average bitrate for sequence
3015      */
3016     else {
3017       /* This accounts for how hard the section is... */
3018       bits_per_frame =
3019           (double)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key);
3020 
3021       /* Dont turn to resampling in easy sections just because they
3022        * have been assigned a small number of bits
3023        */
3024       if (bits_per_frame < av_bits_per_frame) {
3025         bits_per_frame = av_bits_per_frame;
3026       }
3027     }
3028 
3029     /* bits_per_frame should comply with our minimum */
3030     if (bits_per_frame < (cpi->oxcf.target_bandwidth *
3031                           cpi->oxcf.two_pass_vbrmin_section / 100)) {
3032       bits_per_frame = (cpi->oxcf.target_bandwidth *
3033                         cpi->oxcf.two_pass_vbrmin_section / 100);
3034     }
3035 
3036     /* Work out if spatial resampling is necessary */
3037     kf_q = estimate_kf_group_q(cpi, err_per_frame, (int)bits_per_frame,
3038                                group_iiratio);
3039 
3040     /* If we project a required Q higher than the maximum allowed Q then
3041      * make a guess at the actual size of frames in this section
3042      */
3043     projected_bits_perframe = bits_per_frame;
3044     tmp_q = kf_q;
3045 
3046     while (tmp_q > cpi->worst_quality) {
3047       projected_bits_perframe *= 1.04;
3048       tmp_q--;
3049     }
3050 
3051     /* Guess at buffer level at the end of the section */
3052     projected_buffer_level =
3053         (int)(cpi->buffer_level -
3054               (int)((projected_bits_perframe - av_bits_per_frame) *
3055                     cpi->twopass.frames_to_key));
3056 
3057     if (0) {
3058       FILE *f = fopen("Subsamle.stt", "a");
3059       fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n",
3060               cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale,
3061               cpi->common.vert_scale, kf_group_err / cpi->twopass.frames_to_key,
3062               (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key),
3063               new_height, new_width);
3064       fclose(f);
3065     }
3066 
3067     /* The trigger for spatial resampling depends on the various
3068      * parameters such as whether we are streaming (CBR) or VBR.
3069      */
3070     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3071       /* Trigger resample if we are projected to fall below down
3072        * sample level or resampled last time and are projected to
3073        * remain below the up sample level
3074        */
3075       if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark *
3076                                      cpi->oxcf.optimal_buffer_level / 100)) ||
3077           (last_kf_resampled &&
3078            (projected_buffer_level < (cpi->oxcf.resample_up_water_mark *
3079                                       cpi->oxcf.optimal_buffer_level / 100)))) {
3080         resample_trigger = 1;
3081       } else {
3082         resample_trigger = 0;
3083       }
3084     } else {
3085       int64_t clip_bits = (int64_t)(
3086           cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth /
3087           DOUBLE_DIVIDE_CHECK((double)cpi->framerate));
3088       int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
3089 
3090       /* If triggered last time the threshold for triggering again is
3091        * reduced:
3092        *
3093        * Projected Q higher than allowed and Overspend > 5% of total
3094        * bits
3095        */
3096       if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||
3097           ((kf_q > cpi->worst_quality) && (over_spend > clip_bits / 20))) {
3098         resample_trigger = 1;
3099       } else {
3100         resample_trigger = 0;
3101       }
3102     }
3103 
3104     if (resample_trigger) {
3105       while ((kf_q >= cpi->worst_quality) && (scale_val < 6)) {
3106         scale_val++;
3107 
3108         cpi->common.vert_scale = vscale_lookup[scale_val];
3109         cpi->common.horiz_scale = hscale_lookup[scale_val];
3110 
3111         Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
3112         Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
3113 
3114         new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3115         new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3116 
3117         /* Reducing the area to 1/4 does not reduce the complexity
3118          * (err_per_frame) to 1/4... effective_sizeratio attempts
3119          * to provide a crude correction for this
3120          */
3121         effective_size_ratio = (double)(new_width * new_height) /
3122                                (double)(cpi->oxcf.Width * cpi->oxcf.Height);
3123         effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
3124 
3125         /* Now try again and see what Q we get with the smaller
3126          * image size
3127          */
3128         kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio,
3129                                    (int)bits_per_frame, group_iiratio);
3130 
3131         if (0) {
3132           FILE *f = fopen("Subsamle.stt", "a");
3133           fprintf(
3134               f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q,
3135               cpi->common.horiz_scale, cpi->common.vert_scale,
3136               kf_group_err / cpi->twopass.frames_to_key,
3137               (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key),
3138               new_height, new_width);
3139           fclose(f);
3140         }
3141       }
3142     }
3143 
3144     if ((cpi->common.Width != new_width) ||
3145         (cpi->common.Height != new_height)) {
3146       cpi->common.Width = new_width;
3147       cpi->common.Height = new_height;
3148       vp8_alloc_compressor_data(cpi);
3149     }
3150   }
3151 }
3152