• 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; /* Don't 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(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   cm->current_video_frame++;
825 }
826 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
827 
828 /* Estimate a cost per mb attributable to overheads such as the coding of
829  * modes and motion vectors.
830  * Currently simplistic in its assumptions for testing.
831  */
832 
bitcost(double prob)833 static double bitcost(double prob) {
834   if (prob > 0.000122) {
835     return -log(prob) / log(2.0);
836   } else {
837     return 13.0;
838   }
839 }
estimate_modemvcost(VP8_COMP * cpi,FIRSTPASS_STATS * fpstats)840 static int64_t estimate_modemvcost(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats) {
841   int mv_cost;
842   int64_t mode_cost;
843 
844   double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
845   double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
846   double av_intra = (1.0 - av_pct_inter);
847 
848   double zz_cost;
849   double motion_cost;
850   double intra_cost;
851 
852   zz_cost = bitcost(av_pct_inter - av_pct_motion);
853   motion_cost = bitcost(av_pct_motion);
854   intra_cost = bitcost(av_intra);
855 
856   /* Estimate of extra bits per mv overhead for mbs
857    * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb
858    */
859   mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
860 
861   /* Crude estimate of overhead cost from modes
862    * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
863    */
864   mode_cost =
865       (int64_t)((((av_pct_inter - av_pct_motion) * zz_cost) +
866                  (av_pct_motion * motion_cost) + (av_intra * intra_cost)) *
867                 cpi->common.MBs) *
868       512;
869 
870   return mv_cost + mode_cost;
871 }
872 
calc_correction_factor(double err_per_mb,double err_devisor,double pt_low,double pt_high,int Q)873 static double calc_correction_factor(double err_per_mb, double err_devisor,
874                                      double pt_low, double pt_high, int Q) {
875   double power_term;
876   double error_term = err_per_mb / err_devisor;
877   double correction_factor;
878 
879   /* Adjustment based on Q to power term. */
880   power_term = pt_low + (Q * 0.01);
881   power_term = (power_term > pt_high) ? pt_high : power_term;
882 
883   /* Adjustments to error term */
884   /* TBD */
885 
886   /* Calculate correction factor */
887   correction_factor = pow(error_term, power_term);
888 
889   /* Clip range */
890   correction_factor = (correction_factor < 0.05)  ? 0.05
891                       : (correction_factor > 5.0) ? 5.0
892                                                   : correction_factor;
893 
894   return correction_factor;
895 }
896 
estimate_max_q(VP8_COMP * cpi,FIRSTPASS_STATS * fpstats,int section_target_bandwitdh,int overhead_bits)897 static int estimate_max_q(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
898                           int section_target_bandwitdh, int overhead_bits) {
899   int Q;
900   int num_mbs = cpi->common.MBs;
901   int target_norm_bits_per_mb;
902 
903   double section_err = (fpstats->coded_error / fpstats->count);
904   double err_per_mb = section_err / num_mbs;
905   double err_correction_factor;
906   double speed_correction = 1.0;
907   int overhead_bits_per_mb;
908 
909   if (section_target_bandwitdh <= 0) {
910     return cpi->twopass.maxq_max_limit; /* Highest value allowed */
911   }
912 
913   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
914                                 ? (512 * section_target_bandwitdh) / num_mbs
915                                 : 512 * (section_target_bandwitdh / num_mbs);
916 
917   /* Calculate a corrective factor based on a rolling ratio of bits spent
918    * vs target bits
919    */
920   if ((cpi->rolling_target_bits > 0) &&
921       (cpi->active_worst_quality < cpi->worst_quality)) {
922     double rolling_ratio;
923 
924     rolling_ratio =
925         (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
926 
927     if (rolling_ratio < 0.95) {
928       cpi->twopass.est_max_qcorrection_factor -= 0.005;
929     } else if (rolling_ratio > 1.05) {
930       cpi->twopass.est_max_qcorrection_factor += 0.005;
931     }
932 
933     cpi->twopass.est_max_qcorrection_factor =
934         (cpi->twopass.est_max_qcorrection_factor < 0.1) ? 0.1
935         : (cpi->twopass.est_max_qcorrection_factor > 10.0)
936             ? 10.0
937             : cpi->twopass.est_max_qcorrection_factor;
938   }
939 
940   /* Corrections for higher compression speed settings
941    * (reduced compression expected)
942    */
943   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
944     if (cpi->oxcf.cpu_used <= 5) {
945       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
946     } else {
947       speed_correction = 1.25;
948     }
949   }
950 
951   /* Estimate of overhead bits per mb */
952   /* Correction to overhead bits for min allowed Q. */
953   overhead_bits_per_mb = overhead_bits / num_mbs;
954   overhead_bits_per_mb = (int)(overhead_bits_per_mb *
955                                pow(0.98, (double)cpi->twopass.maxq_min_limit));
956 
957   /* Try and pick a max Q that will be high enough to encode the
958    * content at the given rate.
959    */
960   for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; ++Q) {
961     int bits_per_mb_at_this_q;
962 
963     /* Error per MB based correction factor */
964     err_correction_factor =
965         calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
966 
967     bits_per_mb_at_this_q =
968         vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
969 
970     bits_per_mb_at_this_q =
971         (int)(.5 + err_correction_factor * speed_correction *
972                        cpi->twopass.est_max_qcorrection_factor *
973                        cpi->twopass.section_max_qfactor *
974                        (double)bits_per_mb_at_this_q);
975 
976     /* Mode and motion overhead */
977     /* As Q rises in real encode loop rd code will force overhead down
978      * We make a crude adjustment for this here as *.98 per Q step.
979      */
980     overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
981 
982     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
983   }
984 
985   /* Restriction on active max q for constrained quality mode. */
986   if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
987       (Q < cpi->cq_target_quality)) {
988     Q = cpi->cq_target_quality;
989   }
990 
991   /* Adjust maxq_min_limit and maxq_max_limit limits based on
992    * average q observed in clip for non kf/gf.arf frames
993    * Give average a chance to settle though.
994    */
995   if ((cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8)) &&
996       (cpi->ni_frames > 150)) {
997     cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
998                                       ? (cpi->ni_av_qi + 32)
999                                       : cpi->worst_quality;
1000     cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
1001                                       ? (cpi->ni_av_qi - 32)
1002                                       : cpi->best_quality;
1003   }
1004 
1005   return Q;
1006 }
1007 
1008 /* For cq mode estimate a cq level that matches the observed
1009  * complexity and data rate.
1010  */
estimate_cq(VP8_COMP * cpi,FIRSTPASS_STATS * fpstats,int section_target_bandwitdh,int overhead_bits)1011 static int estimate_cq(VP8_COMP *cpi, FIRSTPASS_STATS *fpstats,
1012                        int section_target_bandwitdh, int overhead_bits) {
1013   int Q;
1014   int num_mbs = cpi->common.MBs;
1015   int target_norm_bits_per_mb;
1016 
1017   double section_err = (fpstats->coded_error / fpstats->count);
1018   double err_per_mb = section_err / num_mbs;
1019   double err_correction_factor;
1020   double speed_correction = 1.0;
1021   double clip_iiratio;
1022   double clip_iifactor;
1023   int overhead_bits_per_mb;
1024 
1025   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1026                                 ? (512 * section_target_bandwitdh) / num_mbs
1027                                 : 512 * (section_target_bandwitdh / num_mbs);
1028 
1029   /* Estimate of overhead bits per mb */
1030   overhead_bits_per_mb = overhead_bits / num_mbs;
1031 
1032   /* Corrections for higher compression speed settings
1033    * (reduced compression expected)
1034    */
1035   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
1036     if (cpi->oxcf.cpu_used <= 5) {
1037       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1038     } else {
1039       speed_correction = 1.25;
1040     }
1041   }
1042 
1043   /* II ratio correction factor for clip as a whole */
1044   clip_iiratio = cpi->twopass.total_stats.intra_error /
1045                  DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
1046   clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1047   if (clip_iifactor < 0.80) clip_iifactor = 0.80;
1048 
1049   /* Try and pick a Q that can encode the content at the given rate. */
1050   for (Q = 0; Q < MAXQ; ++Q) {
1051     int bits_per_mb_at_this_q;
1052 
1053     /* Error per MB based correction factor */
1054     err_correction_factor =
1055         calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q);
1056 
1057     bits_per_mb_at_this_q =
1058         vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
1059 
1060     bits_per_mb_at_this_q =
1061         (int)(.5 + err_correction_factor * speed_correction * clip_iifactor *
1062                        (double)bits_per_mb_at_this_q);
1063 
1064     /* Mode and motion overhead */
1065     /* As Q rises in real encode loop rd code will force overhead down
1066      * We make a crude adjustment for this here as *.98 per Q step.
1067      */
1068     overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
1069 
1070     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
1071   }
1072 
1073   /* Clip value to range "best allowed to (worst allowed - 1)" */
1074   Q = cq_level[Q];
1075   if (Q >= cpi->worst_quality) Q = cpi->worst_quality - 1;
1076   if (Q < cpi->best_quality) Q = cpi->best_quality;
1077 
1078   return Q;
1079 }
1080 
estimate_q(VP8_COMP * cpi,double section_err,int section_target_bandwitdh)1081 static int estimate_q(VP8_COMP *cpi, double section_err,
1082                       int section_target_bandwitdh) {
1083   int Q;
1084   int num_mbs = cpi->common.MBs;
1085   int target_norm_bits_per_mb;
1086 
1087   double err_per_mb = section_err / num_mbs;
1088   double err_correction_factor;
1089   double speed_correction = 1.0;
1090 
1091   target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1092                                 ? (512 * section_target_bandwitdh) / num_mbs
1093                                 : 512 * (section_target_bandwitdh / num_mbs);
1094 
1095   /* Corrections for higher compression speed settings
1096    * (reduced compression expected)
1097    */
1098   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
1099     if (cpi->oxcf.cpu_used <= 5) {
1100       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1101     } else {
1102       speed_correction = 1.25;
1103     }
1104   }
1105 
1106   /* Try and pick a Q that can encode the content at the given rate. */
1107   for (Q = 0; Q < MAXQ; ++Q) {
1108     int bits_per_mb_at_this_q;
1109 
1110     /* Error per MB based correction factor */
1111     err_correction_factor =
1112         calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
1113 
1114     bits_per_mb_at_this_q =
1115         (int)(.5 + (err_correction_factor * speed_correction *
1116                     cpi->twopass.est_max_qcorrection_factor *
1117                     (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0));
1118 
1119     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
1120   }
1121 
1122   return Q;
1123 }
1124 
1125 /* 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)1126 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err,
1127                                int section_target_bandwitdh,
1128                                double group_iiratio) {
1129   int Q;
1130   int num_mbs = cpi->common.MBs;
1131   int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1132   int bits_per_mb_at_this_q;
1133 
1134   double err_per_mb = section_err / num_mbs;
1135   double err_correction_factor;
1136   double speed_correction = 1.0;
1137   double current_spend_ratio = 1.0;
1138 
1139   double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1140   double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1141 
1142   double iiratio_correction_factor = 1.0;
1143 
1144   double combined_correction_factor;
1145 
1146   /* Trap special case where the target is <= 0 */
1147   if (target_norm_bits_per_mb <= 0) return MAXQ * 2;
1148 
1149   /* Calculate a corrective factor based on a rolling ratio of bits spent
1150    *  vs target bits
1151    * This is clamped to the range 0.1 to 10.0
1152    */
1153   if (cpi->long_rolling_target_bits <= 0) {
1154     current_spend_ratio = 10.0;
1155   } else {
1156     current_spend_ratio = (double)cpi->long_rolling_actual_bits /
1157                           (double)cpi->long_rolling_target_bits;
1158     current_spend_ratio = (current_spend_ratio > 10.0)  ? 10.0
1159                           : (current_spend_ratio < 0.1) ? 0.1
1160                                                         : current_spend_ratio;
1161   }
1162 
1163   /* Calculate a correction factor based on the quality of prediction in
1164    * the sequence as indicated by intra_inter error score ratio (IIRatio)
1165    * The idea here is to favour subsampling in the hardest sections vs
1166    * the easyest.
1167    */
1168   iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1169 
1170   if (iiratio_correction_factor < 0.5) iiratio_correction_factor = 0.5;
1171 
1172   /* Corrections for higher compression speed settings
1173    * (reduced compression expected)
1174    */
1175   if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) {
1176     if (cpi->oxcf.cpu_used <= 5) {
1177       speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1178     } else {
1179       speed_correction = 1.25;
1180     }
1181   }
1182 
1183   /* Combine the various factors calculated above */
1184   combined_correction_factor =
1185       speed_correction * iiratio_correction_factor * current_spend_ratio;
1186 
1187   /* Try and pick a Q that should be high enough to encode the content at
1188    * the given rate.
1189    */
1190   for (Q = 0; Q < MAXQ; ++Q) {
1191     /* Error per MB based correction factor */
1192     err_correction_factor =
1193         calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q);
1194 
1195     bits_per_mb_at_this_q =
1196         (int)(.5 + (err_correction_factor * combined_correction_factor *
1197                     (double)vp8_bits_per_mb[INTER_FRAME][Q]));
1198 
1199     if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) break;
1200   }
1201 
1202   /* If we could not hit the target even at Max Q then estimate what Q
1203    * would have been required
1204    */
1205   while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) &&
1206          (Q < (MAXQ * 2))) {
1207     bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1208     Q++;
1209   }
1210 
1211   return Q;
1212 }
1213 
vp8_init_second_pass(VP8_COMP * cpi)1214 void vp8_init_second_pass(VP8_COMP *cpi) {
1215   FIRSTPASS_STATS this_frame;
1216   FIRSTPASS_STATS *start_pos;
1217 
1218   double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
1219                                       cpi->oxcf.two_pass_vbrmin_section / 100);
1220 
1221   zero_stats(&cpi->twopass.total_stats);
1222   zero_stats(&cpi->twopass.total_left_stats);
1223 
1224   if (!cpi->twopass.stats_in_end) return;
1225 
1226   cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1227   cpi->twopass.total_left_stats = cpi->twopass.total_stats;
1228 
1229   /* each frame can have a different duration, as the frame rate in the
1230    * source isn't guaranteed to be constant.   The frame rate prior to
1231    * the first frame encoded in the second pass is a guess.  However the
1232    * sum duration is not. Its calculated based on the actual durations of
1233    * all frames from the first pass.
1234    */
1235   vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count /
1236                              cpi->twopass.total_stats.duration);
1237 
1238   cpi->output_framerate = cpi->framerate;
1239   cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration *
1240                                      cpi->oxcf.target_bandwidth / 10000000.0);
1241   cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration *
1242                                       two_pass_min_rate / 10000000.0);
1243 
1244   /* Calculate a minimum intra value to be used in determining the IIratio
1245    * scores used in the second pass. We have this minimum to make sure
1246    * that clips that are static but "low complexity" in the intra domain
1247    * are still boosted appropriately for KF/GF/ARF
1248    */
1249   cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1250   cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1251 
1252   /* Scan the first pass file and calculate an average Intra / Inter error
1253    * score ratio for the sequence
1254    */
1255   {
1256     double sum_iiratio = 0.0;
1257     double IIRatio;
1258 
1259     start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1260 
1261     while (input_stats(cpi, &this_frame) != EOF) {
1262       IIRatio =
1263           this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1264       IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1265       sum_iiratio += IIRatio;
1266     }
1267 
1268     cpi->twopass.avg_iiratio =
1269         sum_iiratio /
1270         DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
1271 
1272     /* Reset file position */
1273     reset_fpf_position(cpi, start_pos);
1274   }
1275 
1276   /* Scan the first pass file and calculate a modified total error based
1277    * upon the bias/power function used to allocate bits
1278    */
1279   {
1280     start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1281 
1282     cpi->twopass.modified_error_total = 0.0;
1283     cpi->twopass.modified_error_used = 0.0;
1284 
1285     while (input_stats(cpi, &this_frame) != EOF) {
1286       cpi->twopass.modified_error_total +=
1287           calculate_modified_err(cpi, &this_frame);
1288     }
1289     cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1290 
1291     reset_fpf_position(cpi, start_pos); /* Reset file position */
1292   }
1293 }
1294 
vp8_end_second_pass(VP8_COMP * cpi)1295 void vp8_end_second_pass(VP8_COMP *cpi) { (void)cpi; }
1296 
1297 /* This function gives and estimate of how badly we believe the prediction
1298  * quality is decaying from frame to frame.
1299  */
get_prediction_decay_rate(FIRSTPASS_STATS * next_frame)1300 static double get_prediction_decay_rate(FIRSTPASS_STATS *next_frame) {
1301   double prediction_decay_rate;
1302   double motion_decay;
1303   double motion_pct = next_frame->pcnt_motion;
1304 
1305   /* Initial basis is the % mbs inter coded */
1306   prediction_decay_rate = next_frame->pcnt_inter;
1307 
1308   /* High % motion -> somewhat higher decay rate */
1309   motion_decay = (1.0 - (motion_pct / 20.0));
1310   if (motion_decay < prediction_decay_rate) {
1311     prediction_decay_rate = motion_decay;
1312   }
1313 
1314   /* Adjustment to decay rate based on speed of motion */
1315   {
1316     double this_mv_rabs;
1317     double this_mv_cabs;
1318     double distance_factor;
1319 
1320     this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1321     this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1322 
1323     distance_factor =
1324         sqrt((this_mv_rabs * this_mv_rabs) + (this_mv_cabs * this_mv_cabs)) /
1325         250.0;
1326     distance_factor = ((distance_factor > 1.0) ? 0.0 : (1.0 - distance_factor));
1327     if (distance_factor < prediction_decay_rate) {
1328       prediction_decay_rate = distance_factor;
1329     }
1330   }
1331 
1332   return prediction_decay_rate;
1333 }
1334 
1335 /* Function to test for a condition where a complex transition is followed
1336  * by a static section. For example in slide shows where there is a fade
1337  * between slides. This is to help with more optimal kf and gf positioning.
1338  */
detect_transition_to_still(VP8_COMP * cpi,int frame_interval,int still_interval,double loop_decay_rate,double decay_accumulator)1339 static int detect_transition_to_still(VP8_COMP *cpi, int frame_interval,
1340                                       int still_interval,
1341                                       double loop_decay_rate,
1342                                       double decay_accumulator) {
1343   int trans_to_still = 0;
1344 
1345   /* Break clause to detect very still sections after motion
1346    * For example a static image after a fade or other transition
1347    * instead of a clean scene cut.
1348    */
1349   if ((frame_interval > MIN_GF_INTERVAL) && (loop_decay_rate >= 0.999) &&
1350       (decay_accumulator < 0.9)) {
1351     int j;
1352     FIRSTPASS_STATS *position = cpi->twopass.stats_in;
1353     FIRSTPASS_STATS tmp_next_frame;
1354     double decay_rate;
1355 
1356     /* Look ahead a few frames to see if static condition persists... */
1357     for (j = 0; j < still_interval; ++j) {
1358       if (EOF == input_stats(cpi, &tmp_next_frame)) break;
1359 
1360       decay_rate = get_prediction_decay_rate(&tmp_next_frame);
1361       if (decay_rate < 0.999) break;
1362     }
1363     /* Reset file position */
1364     reset_fpf_position(cpi, position);
1365 
1366     /* Only if it does do we signal a transition to still */
1367     if (j == still_interval) trans_to_still = 1;
1368   }
1369 
1370   return trans_to_still;
1371 }
1372 
1373 /* This function detects a flash through the high relative pcnt_second_ref
1374  * score in the frame following a flash frame. The offset passed in should
1375  * reflect this
1376  */
detect_flash(VP8_COMP * cpi,int offset)1377 static int detect_flash(VP8_COMP *cpi, int offset) {
1378   FIRSTPASS_STATS next_frame;
1379 
1380   int flash_detected = 0;
1381 
1382   /* Read the frame data. */
1383   /* The return is 0 (no flash detected) if not a valid frame */
1384   if (read_frame_stats(cpi, &next_frame, offset) != EOF) {
1385     /* What we are looking for here is a situation where there is a
1386      * brief break in prediction (such as a flash) but subsequent frames
1387      * are reasonably well predicted by an earlier (pre flash) frame.
1388      * The recovery after a flash is indicated by a high pcnt_second_ref
1389      * comapred to pcnt_inter.
1390      */
1391     if ((next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
1392         (next_frame.pcnt_second_ref >= 0.5)) {
1393       flash_detected = 1;
1394 
1395       /*if (1)
1396       {
1397           FILE *f = fopen("flash.stt", "a");
1398           fprintf(f, "%8.0f %6.2f %6.2f\n",
1399               next_frame.frame,
1400               next_frame.pcnt_inter,
1401               next_frame.pcnt_second_ref);
1402           fclose(f);
1403       }*/
1404     }
1405   }
1406 
1407   return flash_detected;
1408 }
1409 
1410 /* 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)1411 static void accumulate_frame_motion_stats(FIRSTPASS_STATS *this_frame,
1412                                           double *this_frame_mv_in_out,
1413                                           double *mv_in_out_accumulator,
1414                                           double *abs_mv_in_out_accumulator,
1415                                           double *mv_ratio_accumulator) {
1416   double this_frame_mvr_ratio;
1417   double this_frame_mvc_ratio;
1418   double motion_pct;
1419 
1420   /* Accumulate motion stats. */
1421   motion_pct = this_frame->pcnt_motion;
1422 
1423   /* Accumulate Motion In/Out of frame stats */
1424   *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1425   *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1426   *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct);
1427 
1428   /* Accumulate a measure of how uniform (or conversely how random)
1429    * the motion field is. (A ratio of absmv / mv)
1430    */
1431   if (motion_pct > 0.05) {
1432     this_frame_mvr_ratio =
1433         fabs(this_frame->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1434 
1435     this_frame_mvc_ratio =
1436         fabs(this_frame->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1437 
1438     *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs)
1439                                  ? (this_frame_mvr_ratio * motion_pct)
1440                                  : this_frame->mvr_abs * motion_pct;
1441 
1442     *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs)
1443                                  ? (this_frame_mvc_ratio * motion_pct)
1444                                  : this_frame->mvc_abs * motion_pct;
1445   }
1446 }
1447 
1448 /* 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)1449 static double calc_frame_boost(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame,
1450                                double this_frame_mv_in_out) {
1451   double frame_boost;
1452 
1453   /* Underlying boost factor is based on inter intra error ratio */
1454   if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) {
1455     frame_boost = (IIFACTOR * this_frame->intra_error /
1456                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1457   } else {
1458     frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1459                    DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1460   }
1461 
1462   /* Increase boost for frames where new data coming into frame
1463    * (eg zoom out). Slightly reduce boost if there is a net balance
1464    * of motion out of the frame (zoom in).
1465    * The range for this_frame_mv_in_out is -1.0 to +1.0
1466    */
1467   if (this_frame_mv_in_out > 0.0) {
1468     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1469     /* In extreme case boost is halved */
1470   } else {
1471     frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1472   }
1473 
1474   /* Clip to maximum */
1475   if (frame_boost > GF_RMAX) frame_boost = GF_RMAX;
1476 
1477   return frame_boost;
1478 }
1479 
1480 #if NEW_BOOST
calc_arf_boost(VP8_COMP * cpi,int offset,int f_frames,int b_frames,int * f_boost,int * b_boost)1481 static int calc_arf_boost(VP8_COMP *cpi, int offset, int f_frames, int b_frames,
1482                           int *f_boost, int *b_boost) {
1483   FIRSTPASS_STATS this_frame;
1484 
1485   int i;
1486   double boost_score = 0.0;
1487   double mv_ratio_accumulator = 0.0;
1488   double decay_accumulator = 1.0;
1489   double this_frame_mv_in_out = 0.0;
1490   double mv_in_out_accumulator = 0.0;
1491   double abs_mv_in_out_accumulator = 0.0;
1492   double r;
1493   int flash_detected = 0;
1494 
1495   /* Search forward from the proposed arf/next gf position */
1496   for (i = 0; i < f_frames; ++i) {
1497     if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
1498 
1499     /* Update the motion related elements to the boost calculation */
1500     accumulate_frame_motion_stats(
1501         &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1502         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1503 
1504     /* Calculate the baseline boost number for this frame */
1505     r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
1506 
1507     /* We want to discount the flash frame itself and the recovery
1508      * frame that follows as both will have poor scores.
1509      */
1510     flash_detected =
1511         detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
1512 
1513     /* Cumulative effect of prediction quality decay */
1514     if (!flash_detected) {
1515       decay_accumulator =
1516           decay_accumulator * get_prediction_decay_rate(&this_frame);
1517       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1518     }
1519     boost_score += (decay_accumulator * r);
1520 
1521     /* Break out conditions. */
1522     if ((!flash_detected) &&
1523         ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
1524          (mv_in_out_accumulator < -2.0))) {
1525       break;
1526     }
1527   }
1528 
1529   *f_boost = (int)(boost_score * 100.0) >> 4;
1530 
1531   /* Reset for backward looking loop */
1532   boost_score = 0.0;
1533   mv_ratio_accumulator = 0.0;
1534   decay_accumulator = 1.0;
1535   this_frame_mv_in_out = 0.0;
1536   mv_in_out_accumulator = 0.0;
1537   abs_mv_in_out_accumulator = 0.0;
1538 
1539   /* Search forward from the proposed arf/next gf position */
1540   for (i = -1; i >= -b_frames; i--) {
1541     if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) break;
1542 
1543     /* Update the motion related elements to the boost calculation */
1544     accumulate_frame_motion_stats(
1545         &this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1546         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1547 
1548     /* Calculate the baseline boost number for this frame */
1549     r = calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out);
1550 
1551     /* We want to discount the flash frame itself and the recovery
1552      * frame that follows as both will have poor scores.
1553      */
1554     flash_detected =
1555         detect_flash(cpi, (i + offset)) || detect_flash(cpi, (i + offset + 1));
1556 
1557     /* Cumulative effect of prediction quality decay */
1558     if (!flash_detected) {
1559       decay_accumulator =
1560           decay_accumulator * get_prediction_decay_rate(&this_frame);
1561       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1562     }
1563 
1564     boost_score += (decay_accumulator * r);
1565 
1566     /* Break out conditions. */
1567     if ((!flash_detected) &&
1568         ((mv_ratio_accumulator > 100.0) || (abs_mv_in_out_accumulator > 3.0) ||
1569          (mv_in_out_accumulator < -2.0))) {
1570       break;
1571     }
1572   }
1573   *b_boost = (int)(boost_score * 100.0) >> 4;
1574 
1575   return (*f_boost + *b_boost);
1576 }
1577 #endif
1578 
1579 /* Analyse and define a gf/arf group . */
define_gf_group(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame)1580 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1581   FIRSTPASS_STATS next_frame;
1582   FIRSTPASS_STATS *start_pos;
1583   int i;
1584   double r;
1585   double boost_score = 0.0;
1586   double old_boost_score = 0.0;
1587   double gf_group_err = 0.0;
1588   double gf_first_frame_err = 0.0;
1589   double mod_frame_err = 0.0;
1590 
1591   double mv_ratio_accumulator = 0.0;
1592   double decay_accumulator = 1.0;
1593 
1594   double loop_decay_rate = 1.00; /* Starting decay rate */
1595 
1596   double this_frame_mv_in_out = 0.0;
1597   double mv_in_out_accumulator = 0.0;
1598   double abs_mv_in_out_accumulator = 0.0;
1599 
1600   int max_bits = frame_max_bits(cpi); /* Max for a single frame */
1601 
1602   unsigned int allow_alt_ref =
1603       cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1604 
1605   int alt_boost = 0;
1606   int f_boost = 0;
1607   int b_boost = 0;
1608   int flash_detected;
1609 
1610   cpi->twopass.gf_group_bits = 0;
1611   cpi->twopass.gf_decay_rate = 0;
1612 
1613   vpx_clear_system_state();
1614 
1615   start_pos = cpi->twopass.stats_in;
1616 
1617   memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */
1618 
1619   /* Load stats for the current frame. */
1620   mod_frame_err = calculate_modified_err(cpi, this_frame);
1621 
1622   /* Note the error of the frame at the start of the group (this will be
1623    * the GF frame error if we code a normal gf
1624    */
1625   gf_first_frame_err = mod_frame_err;
1626 
1627   /* Special treatment if the current frame is a key frame (which is also
1628    * a gf). If it is then its error score (and hence bit allocation) need
1629    * to be subtracted out from the calculation for the GF group
1630    */
1631   if (cpi->common.frame_type == KEY_FRAME) gf_group_err -= gf_first_frame_err;
1632 
1633   /* Scan forward to try and work out how many frames the next gf group
1634    * should contain and what level of boost is appropriate for the GF
1635    * or ARF that will be coded with the group
1636    */
1637   i = 0;
1638 
1639   while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1640           ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1641          (i < cpi->twopass.frames_to_key)) {
1642     i++;
1643 
1644     /* Accumulate error score of frames in this gf group */
1645     mod_frame_err = calculate_modified_err(cpi, this_frame);
1646 
1647     gf_group_err += mod_frame_err;
1648 
1649     if (EOF == input_stats(cpi, &next_frame)) break;
1650 
1651     /* Test for the case where there is a brief flash but the prediction
1652      * quality back to an earlier frame is then restored.
1653      */
1654     flash_detected = detect_flash(cpi, 0);
1655 
1656     /* Update the motion related elements to the boost calculation */
1657     accumulate_frame_motion_stats(
1658         &next_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1659         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1660 
1661     /* Calculate a baseline boost number for this frame */
1662     r = calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out);
1663 
1664     /* Cumulative effect of prediction quality decay */
1665     if (!flash_detected) {
1666       loop_decay_rate = get_prediction_decay_rate(&next_frame);
1667       decay_accumulator = decay_accumulator * loop_decay_rate;
1668       decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1669     }
1670     boost_score += (decay_accumulator * r);
1671 
1672     /* Break clause to detect very still sections after motion
1673      * For example a staic image after a fade or other transition.
1674      */
1675     if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
1676                                    decay_accumulator)) {
1677       allow_alt_ref = 0;
1678       boost_score = old_boost_score;
1679       break;
1680     }
1681 
1682     /* Break out conditions. */
1683     if (
1684         /* Break at cpi->max_gf_interval unless almost totally static */
1685         (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1686         (
1687             /* Don't break out with a very short interval */
1688             (i > MIN_GF_INTERVAL) &&
1689             /* Don't break out very close to a key frame */
1690             ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1691             ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1692             (!flash_detected) &&
1693             ((mv_ratio_accumulator > 100.0) ||
1694              (abs_mv_in_out_accumulator > 3.0) ||
1695              (mv_in_out_accumulator < -2.0) ||
1696              ((boost_score - old_boost_score) < 2.0)))) {
1697       boost_score = old_boost_score;
1698       break;
1699     }
1700 
1701     memcpy(this_frame, &next_frame, sizeof(*this_frame));
1702 
1703     old_boost_score = boost_score;
1704   }
1705 
1706   cpi->twopass.gf_decay_rate =
1707       (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1708 
1709   /* When using CBR apply additional buffer related upper limits */
1710   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1711     double max_boost;
1712 
1713     /* For cbr apply buffer related limits */
1714     if (cpi->drop_frames_allowed) {
1715       int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1716                                 (cpi->oxcf.optimal_buffer_level / 100);
1717 
1718       if (cpi->buffer_level > df_buffer_level) {
1719         max_boost =
1720             ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
1721             DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1722       } else {
1723         max_boost = 0.0;
1724       }
1725     } else if (cpi->buffer_level > 0) {
1726       max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
1727                   DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1728     } else {
1729       max_boost = 0.0;
1730     }
1731 
1732     if (boost_score > max_boost) boost_score = max_boost;
1733   }
1734 
1735   /* Don't allow conventional gf too near the next kf */
1736   if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) {
1737     while (i < cpi->twopass.frames_to_key) {
1738       i++;
1739 
1740       if (EOF == input_stats(cpi, this_frame)) break;
1741 
1742       if (i < cpi->twopass.frames_to_key) {
1743         mod_frame_err = calculate_modified_err(cpi, this_frame);
1744         gf_group_err += mod_frame_err;
1745       }
1746     }
1747   }
1748 
1749   cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1750 
1751 #if NEW_BOOST
1752   /* Alterrnative boost calculation for alt ref */
1753   alt_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
1754 #endif
1755 
1756   /* Should we use the alternate reference frame */
1757   if (allow_alt_ref && (i >= MIN_GF_INTERVAL) &&
1758       /* don't use ARF very near next kf */
1759       (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1760 #if NEW_BOOST
1761       ((next_frame.pcnt_inter > 0.75) || (next_frame.pcnt_second_ref > 0.5)) &&
1762       ((mv_in_out_accumulator / (double)i > -0.2) ||
1763        (mv_in_out_accumulator > -2.0)) &&
1764       (b_boost > 100) && (f_boost > 100))
1765 #else
1766       (next_frame.pcnt_inter > 0.75) &&
1767       ((mv_in_out_accumulator / (double)i > -0.2) ||
1768        (mv_in_out_accumulator > -2.0)) &&
1769       (cpi->gfu_boost > 100) &&
1770       (cpi->twopass.gf_decay_rate <=
1771        (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))))
1772 #endif
1773   {
1774     int Boost;
1775     int allocation_chunks;
1776     int Q =
1777         (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1778     int tmp_q;
1779     int arf_frame_bits = 0;
1780     int group_bits;
1781 
1782 #if NEW_BOOST
1783     cpi->gfu_boost = alt_boost;
1784 #endif
1785 
1786     /* Estimate the bits to be allocated to the group as a whole */
1787     if ((cpi->twopass.kf_group_bits > 0) &&
1788         (cpi->twopass.kf_group_error_left > 0)) {
1789       group_bits =
1790           (int)((double)cpi->twopass.kf_group_bits *
1791                 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1792     } else {
1793       group_bits = 0;
1794     }
1795 
1796 /* Boost for arf frame */
1797 #if NEW_BOOST
1798     Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1799 #else
1800     Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1801 #endif
1802     Boost += (i * 50);
1803 
1804     /* Set max and minimum boost and hence minimum allocation */
1805     if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
1806       Boost = ((cpi->baseline_gf_interval + 1) * 200);
1807     } else if (Boost < 125) {
1808       Boost = 125;
1809     }
1810 
1811     allocation_chunks = (i * 100) + Boost;
1812 
1813     /* Normalize Altboost and allocations chunck down to prevent overflow */
1814     while (Boost > 1000) {
1815       Boost /= 2;
1816       allocation_chunks /= 2;
1817     }
1818 
1819     /* Calculate the number of bits to be spent on the arf based on the
1820      * boost number
1821      */
1822     arf_frame_bits =
1823         (int)((double)Boost * (group_bits / (double)allocation_chunks));
1824 
1825     /* Estimate if there are enough bits available to make worthwhile use
1826      * of an arf.
1827      */
1828     tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
1829 
1830     /* Only use an arf if it is likely we will be able to code
1831      * it at a lower Q than the surrounding frames.
1832      */
1833     if (tmp_q < cpi->worst_quality) {
1834       int half_gf_int;
1835       int frames_after_arf;
1836       int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1837       int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1838 
1839       cpi->source_alt_ref_pending = 1;
1840 
1841       /*
1842        * For alt ref frames the error score for the end frame of the
1843        * group (the alt ref frame) should not contribute to the group
1844        * total and hence the number of bit allocated to the group.
1845        * Rather it forms part of the next group (it is the GF at the
1846        * start of the next group)
1847        * gf_group_err -= mod_frame_err;
1848        *
1849        * For alt ref frames alt ref frame is technically part of the
1850        * GF frame for the next group but we always base the error
1851        * calculation and bit allocation on the current group of frames.
1852        *
1853        * Set the interval till the next gf or arf.
1854        * For ARFs this is the number of frames to be coded before the
1855        * future frame that is coded as an ARF.
1856        * The future frame itself is part of the next group
1857        */
1858       cpi->baseline_gf_interval = i;
1859 
1860       /*
1861        * Define the arnr filter width for this group of frames:
1862        * We only filter frames that lie within a distance of half
1863        * the GF interval from the ARF frame. We also have to trap
1864        * cases where the filter extends beyond the end of clip.
1865        * Note: this_frame->frame has been updated in the loop
1866        * so it now points at the ARF frame.
1867        */
1868       half_gf_int = cpi->baseline_gf_interval >> 1;
1869       frames_after_arf =
1870           (int)(cpi->twopass.total_stats.count - this_frame->frame - 1);
1871 
1872       switch (cpi->oxcf.arnr_type) {
1873         case 1: /* Backward filter */
1874           frames_fwd = 0;
1875           if (frames_bwd > half_gf_int) frames_bwd = half_gf_int;
1876           break;
1877 
1878         case 2: /* Forward filter */
1879           if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
1880           if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
1881           frames_bwd = 0;
1882           break;
1883 
1884         case 3: /* Centered filter */
1885         default:
1886           frames_fwd >>= 1;
1887           if (frames_fwd > frames_after_arf) frames_fwd = frames_after_arf;
1888           if (frames_fwd > half_gf_int) frames_fwd = half_gf_int;
1889 
1890           frames_bwd = frames_fwd;
1891 
1892           /* For even length filter there is one more frame backward
1893            * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1894            */
1895           if (frames_bwd < half_gf_int) {
1896             frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
1897           }
1898           break;
1899       }
1900 
1901       cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1902     } else {
1903       cpi->source_alt_ref_pending = 0;
1904       cpi->baseline_gf_interval = i;
1905     }
1906   } else {
1907     cpi->source_alt_ref_pending = 0;
1908     cpi->baseline_gf_interval = i;
1909   }
1910 
1911   /*
1912    * Now decide how many bits should be allocated to the GF group as  a
1913    * proportion of those remaining in the kf group.
1914    * The final key frame group in the clip is treated as a special case
1915    * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
1916    * This is also important for short clips where there may only be one
1917    * key frame.
1918    */
1919   if (cpi->twopass.frames_to_key >=
1920       (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame)) {
1921     cpi->twopass.kf_group_bits =
1922         (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
1923   }
1924 
1925   /* Calculate the bits to be allocated to the group as a whole */
1926   if ((cpi->twopass.kf_group_bits > 0) &&
1927       (cpi->twopass.kf_group_error_left > 0)) {
1928     cpi->twopass.gf_group_bits =
1929         (int64_t)(cpi->twopass.kf_group_bits *
1930                   (gf_group_err / cpi->twopass.kf_group_error_left));
1931   } else {
1932     cpi->twopass.gf_group_bits = 0;
1933   }
1934 
1935   cpi->twopass.gf_group_bits =
1936       (cpi->twopass.gf_group_bits < 0) ? 0
1937       : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
1938           ? cpi->twopass.kf_group_bits
1939           : cpi->twopass.gf_group_bits;
1940 
1941   /* Clip cpi->twopass.gf_group_bits based on user supplied data rate
1942    * variability limit (cpi->oxcf.two_pass_vbrmax_section)
1943    */
1944   if (cpi->twopass.gf_group_bits >
1945       (int64_t)max_bits * cpi->baseline_gf_interval) {
1946     cpi->twopass.gf_group_bits = (int64_t)max_bits * cpi->baseline_gf_interval;
1947   }
1948 
1949   /* Reset the file position */
1950   reset_fpf_position(cpi, start_pos);
1951 
1952   /* Update the record of error used so far (only done once per gf group) */
1953   cpi->twopass.modified_error_used += gf_group_err;
1954 
1955   /* Assign  bits to the arf or gf. */
1956   for (i = 0; i <= (cpi->source_alt_ref_pending &&
1957                     cpi->common.frame_type != KEY_FRAME);
1958        i++) {
1959     int Boost;
1960     int allocation_chunks;
1961     int Q =
1962         (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1963     int gf_bits;
1964 
1965     /* For ARF frames */
1966     if (cpi->source_alt_ref_pending && i == 0) {
1967 #if NEW_BOOST
1968       Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1969 #else
1970       Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1971 #endif
1972       Boost += (cpi->baseline_gf_interval * 50);
1973 
1974       /* Set max and minimum boost and hence minimum allocation */
1975       if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) {
1976         Boost = ((cpi->baseline_gf_interval + 1) * 200);
1977       } else if (Boost < 125) {
1978         Boost = 125;
1979       }
1980 
1981       allocation_chunks = ((cpi->baseline_gf_interval + 1) * 100) + Boost;
1982     }
1983     /* Else for standard golden frames */
1984     else {
1985       /* boost based on inter / intra ratio of subsequent frames */
1986       Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
1987 
1988       /* Set max and minimum boost and hence minimum allocation */
1989       if (Boost > (cpi->baseline_gf_interval * 150)) {
1990         Boost = (cpi->baseline_gf_interval * 150);
1991       } else if (Boost < 125) {
1992         Boost = 125;
1993       }
1994 
1995       allocation_chunks = (cpi->baseline_gf_interval * 100) + (Boost - 100);
1996     }
1997 
1998     /* Normalize Altboost and allocations chunck down to prevent overflow */
1999     while (Boost > 1000) {
2000       Boost /= 2;
2001       allocation_chunks /= 2;
2002     }
2003 
2004     /* Calculate the number of bits to be spent on the gf or arf based on
2005      * the boost number
2006      */
2007     gf_bits = (int)((double)Boost *
2008                     (cpi->twopass.gf_group_bits / (double)allocation_chunks));
2009 
2010     /* If the frame that is to be boosted is simpler than the average for
2011      * the gf/arf group then use an alternative calculation
2012      * based on the error score of the frame itself
2013      */
2014     if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) {
2015       double alt_gf_grp_bits;
2016       int alt_gf_bits;
2017 
2018       alt_gf_grp_bits =
2019           (double)cpi->twopass.kf_group_bits *
2020           (mod_frame_err * (double)cpi->baseline_gf_interval) /
2021           DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
2022 
2023       alt_gf_bits =
2024           (int)((double)Boost * (alt_gf_grp_bits / (double)allocation_chunks));
2025 
2026       if (gf_bits > alt_gf_bits) {
2027         gf_bits = alt_gf_bits;
2028       }
2029     }
2030     /* Else if it is harder than other frames in the group make sure it at
2031      * least receives an allocation in keeping with its relative error
2032      * score, otherwise it may be worse off than an "un-boosted" frame
2033      */
2034     else {
2035       // Avoid division by 0 by clamping cpi->twopass.kf_group_error_left to 1
2036       int alt_gf_bits =
2037           (int)((double)cpi->twopass.kf_group_bits * mod_frame_err /
2038                 (double)VPXMAX(cpi->twopass.kf_group_error_left, 1));
2039 
2040       if (alt_gf_bits > gf_bits) {
2041         gf_bits = alt_gf_bits;
2042       }
2043     }
2044 
2045     /* Apply an additional limit for CBR */
2046     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2047       if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1)) {
2048         cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1);
2049       }
2050     }
2051 
2052     /* Don't allow a negative value for gf_bits */
2053     if (gf_bits < 0) gf_bits = 0;
2054 
2055     /* Add in minimum for a frame */
2056     gf_bits += cpi->min_frame_bandwidth;
2057 
2058     if (i == 0) {
2059       cpi->twopass.gf_bits = gf_bits;
2060     }
2061     if (i == 1 || (!cpi->source_alt_ref_pending &&
2062                    (cpi->common.frame_type != KEY_FRAME))) {
2063       /* Per frame bit target for this frame */
2064       cpi->per_frame_bandwidth = gf_bits;
2065     }
2066   }
2067 
2068   {
2069     /* Adjust KF group bits and error remainin */
2070     cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
2071     cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
2072 
2073     if (cpi->twopass.kf_group_bits < 0) cpi->twopass.kf_group_bits = 0;
2074 
2075     /* Note the error score left in the remaining frames of the group.
2076      * For normal GFs we want to remove the error score for the first
2077      * frame of the group (except in Key frame case where this has
2078      * already happened)
2079      */
2080     if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) {
2081       cpi->twopass.gf_group_error_left =
2082           (int)(gf_group_err - gf_first_frame_err);
2083     } else {
2084       cpi->twopass.gf_group_error_left = (int)gf_group_err;
2085     }
2086 
2087     cpi->twopass.gf_group_bits -=
2088         cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
2089 
2090     if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
2091 
2092     /* This condition could fail if there are two kfs very close together
2093      * despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the
2094      * calculation of cpi->twopass.alt_extra_bits.
2095      */
2096     if (cpi->baseline_gf_interval >= 3) {
2097 #if NEW_BOOST
2098       int boost = (cpi->source_alt_ref_pending) ? b_boost : cpi->gfu_boost;
2099 #else
2100       int boost = cpi->gfu_boost;
2101 #endif
2102       if (boost >= 150) {
2103         int pct_extra;
2104 
2105         pct_extra = (boost - 100) / 50;
2106         pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2107 
2108         cpi->twopass.alt_extra_bits =
2109             (int)(cpi->twopass.gf_group_bits * pct_extra) / 100;
2110         cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
2111         cpi->twopass.alt_extra_bits /= ((cpi->baseline_gf_interval - 1) >> 1);
2112       } else {
2113         cpi->twopass.alt_extra_bits = 0;
2114       }
2115     } else {
2116       cpi->twopass.alt_extra_bits = 0;
2117     }
2118   }
2119 
2120   /* Adjustments based on a measure of complexity of the section */
2121   if (cpi->common.frame_type != KEY_FRAME) {
2122     FIRSTPASS_STATS sectionstats;
2123     double Ratio;
2124 
2125     zero_stats(&sectionstats);
2126     reset_fpf_position(cpi, start_pos);
2127 
2128     for (i = 0; i < cpi->baseline_gf_interval; ++i) {
2129       input_stats(cpi, &next_frame);
2130       accumulate_stats(&sectionstats, &next_frame);
2131     }
2132 
2133     avg_stats(&sectionstats);
2134 
2135     cpi->twopass.section_intra_rating =
2136         (unsigned int)(sectionstats.intra_error /
2137                        DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2138 
2139     Ratio = sectionstats.intra_error /
2140             DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2141     cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2142 
2143     if (cpi->twopass.section_max_qfactor < 0.80) {
2144       cpi->twopass.section_max_qfactor = 0.80;
2145     }
2146 
2147     reset_fpf_position(cpi, start_pos);
2148   }
2149 }
2150 
2151 /* Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2152  */
assign_std_frame_bits(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame)2153 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2154   int target_frame_size;
2155 
2156   double modified_err;
2157   double err_fraction;
2158 
2159   int max_bits = frame_max_bits(cpi); /* Max for a single frame */
2160 
2161   /* Calculate modified prediction error used in bit allocation */
2162   modified_err = calculate_modified_err(cpi, this_frame);
2163 
2164   /* What portion of the remaining GF group error is used by this frame */
2165   if (cpi->twopass.gf_group_error_left > 0) {
2166     err_fraction = modified_err / cpi->twopass.gf_group_error_left;
2167   } else {
2168     err_fraction = 0.0;
2169   }
2170 
2171   /* How many of those bits available for allocation should we give it? */
2172   target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
2173 
2174   /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits)
2175    * at the top end.
2176    */
2177   if (target_frame_size < 0) {
2178     target_frame_size = 0;
2179   } else {
2180     if (target_frame_size > max_bits) target_frame_size = max_bits;
2181 
2182     if (target_frame_size > cpi->twopass.gf_group_bits) {
2183       target_frame_size = (int)cpi->twopass.gf_group_bits;
2184     }
2185   }
2186 
2187   /* Adjust error and bits remaining */
2188   cpi->twopass.gf_group_error_left -= (int)modified_err;
2189   cpi->twopass.gf_group_bits -= target_frame_size;
2190 
2191   if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
2192 
2193   /* Add in the minimum number of bits that is set aside for every frame. */
2194   target_frame_size += cpi->min_frame_bandwidth;
2195 
2196   /* Every other frame gets a few extra bits */
2197   if ((cpi->frames_since_golden & 0x01) &&
2198       (cpi->frames_till_gf_update_due > 0)) {
2199     target_frame_size += cpi->twopass.alt_extra_bits;
2200   }
2201 
2202   /* Per frame bit target for this frame */
2203   cpi->per_frame_bandwidth = target_frame_size;
2204 }
2205 
vp8_second_pass(VP8_COMP * cpi)2206 void vp8_second_pass(VP8_COMP *cpi) {
2207   int tmp_q;
2208   int frames_left =
2209       (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
2210 
2211   FIRSTPASS_STATS this_frame;
2212   FIRSTPASS_STATS this_frame_copy;
2213 
2214   double this_frame_intra_error;
2215   double this_frame_coded_error;
2216 
2217   int overhead_bits;
2218 
2219   vp8_zero(this_frame);
2220 
2221   if (!cpi->twopass.stats_in) {
2222     return;
2223   }
2224 
2225   vpx_clear_system_state();
2226 
2227   if (EOF == input_stats(cpi, &this_frame)) return;
2228 
2229   this_frame_intra_error = this_frame.intra_error;
2230   this_frame_coded_error = this_frame.coded_error;
2231 
2232   /* keyframe and section processing ! */
2233   if (cpi->twopass.frames_to_key == 0) {
2234     /* Define next KF group and assign bits to it */
2235     memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2236     find_next_key_frame(cpi, &this_frame_copy);
2237 
2238     /* Special case: Error error_resilient_mode mode does not make much
2239      * sense for two pass but with its current meaning this code is
2240      * designed to stop outlandish behaviour if someone does set it when
2241      * using two pass. It effectively disables GF groups. This is
2242      * temporary code until we decide what should really happen in this
2243      * case.
2244      */
2245     if (cpi->oxcf.error_resilient_mode) {
2246       cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
2247       cpi->twopass.gf_group_error_left = (int)cpi->twopass.kf_group_error_left;
2248       cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
2249       cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2250       cpi->source_alt_ref_pending = 0;
2251     }
2252   }
2253 
2254   /* Is this a GF / ARF (Note that a KF is always also a GF) */
2255   if (cpi->frames_till_gf_update_due == 0) {
2256     /* Define next gf group and assign bits to it */
2257     memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2258     define_gf_group(cpi, &this_frame_copy);
2259 
2260     /* If we are going to code an altref frame at the end of the group
2261      * and the current frame is not a key frame.... If the previous
2262      * group used an arf this frame has already benefited from that arf
2263      * boost and it should not be given extra bits If the previous
2264      * group was NOT coded using arf we may want to apply some boost to
2265      * this GF as well
2266      */
2267     if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) {
2268       /* Assign a standard frames worth of bits from those allocated
2269        * to the GF group
2270        */
2271       int bak = cpi->per_frame_bandwidth;
2272       memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2273       assign_std_frame_bits(cpi, &this_frame_copy);
2274       cpi->per_frame_bandwidth = bak;
2275     }
2276   }
2277 
2278   /* Otherwise this is an ordinary frame */
2279   else {
2280     /* Special case: Error error_resilient_mode mode does not make much
2281      * sense for two pass but with its current meaning but this code is
2282      * designed to stop outlandish behaviour if someone does set it
2283      * when using two pass. It effectively disables GF groups. This is
2284      * temporary code till we decide what should really happen in this
2285      * case.
2286      */
2287     if (cpi->oxcf.error_resilient_mode) {
2288       cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
2289 
2290       if (cpi->common.frame_type != KEY_FRAME) {
2291         /* Assign bits from those allocated to the GF group */
2292         memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2293         assign_std_frame_bits(cpi, &this_frame_copy);
2294       }
2295     } else {
2296       /* Assign bits from those allocated to the GF group */
2297       memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2298       assign_std_frame_bits(cpi, &this_frame_copy);
2299     }
2300   }
2301 
2302   /* Keep a globally available copy of this and the next frame's iiratio. */
2303   cpi->twopass.this_iiratio =
2304       (unsigned int)(this_frame_intra_error /
2305                      DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
2306   {
2307     FIRSTPASS_STATS next_frame;
2308     if (lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2309       cpi->twopass.next_iiratio =
2310           (unsigned int)(next_frame.intra_error /
2311                          DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2312     }
2313   }
2314 
2315   /* Set nominal per second bandwidth for this frame */
2316   cpi->target_bandwidth =
2317       (int)(cpi->per_frame_bandwidth * cpi->output_framerate);
2318   if (cpi->target_bandwidth < 0) cpi->target_bandwidth = 0;
2319 
2320   /* Account for mv, mode and other overheads. */
2321   overhead_bits = (int)estimate_modemvcost(cpi, &cpi->twopass.total_left_stats);
2322 
2323   /* Special case code for first frame. */
2324   if (cpi->common.current_video_frame == 0) {
2325     cpi->twopass.est_max_qcorrection_factor = 1.0;
2326 
2327     /* Set a cq_level in constrained quality mode. */
2328     if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2329       int est_cq;
2330 
2331       est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats,
2332                            (int)(cpi->twopass.bits_left / frames_left),
2333                            overhead_bits);
2334 
2335       cpi->cq_target_quality = cpi->oxcf.cq_level;
2336       if (est_cq > cpi->cq_target_quality) cpi->cq_target_quality = est_cq;
2337     }
2338 
2339     /* guess at maxq needed in 2nd pass */
2340     cpi->twopass.maxq_max_limit = cpi->worst_quality;
2341     cpi->twopass.maxq_min_limit = cpi->best_quality;
2342 
2343     tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
2344                            (int)(cpi->twopass.bits_left / frames_left),
2345                            overhead_bits);
2346 
2347     /* Limit the maxq value returned subsequently.
2348      * This increases the risk of overspend or underspend if the initial
2349      * estimate for the clip is bad, but helps prevent excessive
2350      * variation in Q, especially near the end of a clip
2351      * where for example a small overspend may cause Q to crash
2352      */
2353     cpi->twopass.maxq_max_limit =
2354         ((tmp_q + 32) < cpi->worst_quality) ? (tmp_q + 32) : cpi->worst_quality;
2355     cpi->twopass.maxq_min_limit =
2356         ((tmp_q - 32) > cpi->best_quality) ? (tmp_q - 32) : cpi->best_quality;
2357 
2358     cpi->active_worst_quality = tmp_q;
2359     cpi->ni_av_qi = tmp_q;
2360   }
2361 
2362   /* The last few frames of a clip almost always have to few or too many
2363    * bits and for the sake of over exact rate control we don't want to make
2364    * radical adjustments to the allowed quantizer range just to use up a
2365    * few surplus bits or get beneath the target rate.
2366    */
2367   else if ((cpi->common.current_video_frame <
2368             (((unsigned int)cpi->twopass.total_stats.count * 255) >> 8)) &&
2369            ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2370             (unsigned int)cpi->twopass.total_stats.count)) {
2371     if (frames_left < 1) frames_left = 1;
2372 
2373     tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats,
2374                            (int)(cpi->twopass.bits_left / frames_left),
2375                            overhead_bits);
2376 
2377     /* Move active_worst_quality but in a damped way */
2378     if (tmp_q > cpi->active_worst_quality) {
2379       cpi->active_worst_quality++;
2380     } else if (tmp_q < cpi->active_worst_quality) {
2381       cpi->active_worst_quality--;
2382     }
2383 
2384     cpi->active_worst_quality =
2385         ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2386   }
2387 
2388   cpi->twopass.frames_to_key--;
2389 
2390   /* Update the total stats remaining sturcture */
2391   subtract_stats(&cpi->twopass.total_left_stats, &this_frame);
2392 }
2393 
test_candidate_kf(VP8_COMP * cpi,FIRSTPASS_STATS * last_frame,FIRSTPASS_STATS * this_frame,FIRSTPASS_STATS * next_frame)2394 static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame,
2395                              FIRSTPASS_STATS *this_frame,
2396                              FIRSTPASS_STATS *next_frame) {
2397   int is_viable_kf = 0;
2398 
2399   /* Does the frame satisfy the primary criteria of a key frame
2400    *      If so, then examine how well it predicts subsequent frames
2401    */
2402   if ((this_frame->pcnt_second_ref < 0.10) &&
2403       (next_frame->pcnt_second_ref < 0.10) &&
2404       ((this_frame->pcnt_inter < 0.05) ||
2405        (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2406         ((this_frame->intra_error /
2407           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2408         ((fabs(last_frame->coded_error - this_frame->coded_error) /
2409               DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
2410           .40) ||
2411          (fabs(last_frame->intra_error - this_frame->intra_error) /
2412               DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
2413           .40) ||
2414          ((next_frame->intra_error /
2415            DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
2416     int i;
2417     FIRSTPASS_STATS *start_pos;
2418 
2419     FIRSTPASS_STATS local_next_frame;
2420 
2421     double boost_score = 0.0;
2422     double old_boost_score = 0.0;
2423     double decay_accumulator = 1.0;
2424     double next_iiratio;
2425 
2426     memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2427 
2428     /* Note the starting file position so we can reset to it */
2429     start_pos = cpi->twopass.stats_in;
2430 
2431     /* Examine how well the key frame predicts subsequent frames */
2432     for (i = 0; i < 16; ++i) {
2433       next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
2434                       DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
2435 
2436       if (next_iiratio > RMAX) next_iiratio = RMAX;
2437 
2438       /* Cumulative effect of decay in prediction quality */
2439       if (local_next_frame.pcnt_inter > 0.85) {
2440         decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2441       } else {
2442         decay_accumulator =
2443             decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2444       }
2445 
2446       /* Keep a running total */
2447       boost_score += (decay_accumulator * next_iiratio);
2448 
2449       /* Test various breakout clauses */
2450       if ((local_next_frame.pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
2451           (((local_next_frame.pcnt_inter - local_next_frame.pcnt_neutral) <
2452             0.20) &&
2453            (next_iiratio < 3.0)) ||
2454           ((boost_score - old_boost_score) < 0.5) ||
2455           (local_next_frame.intra_error < 200)) {
2456         break;
2457       }
2458 
2459       old_boost_score = boost_score;
2460 
2461       /* Get the next frame details */
2462       if (EOF == input_stats(cpi, &local_next_frame)) break;
2463     }
2464 
2465     /* If there is tolerable prediction for at least the next 3 frames
2466      * then break out else discard this pottential key frame and move on
2467      */
2468     if (boost_score > 5.0 && (i > 3)) {
2469       is_viable_kf = 1;
2470     } else {
2471       /* Reset the file position */
2472       reset_fpf_position(cpi, start_pos);
2473 
2474       is_viable_kf = 0;
2475     }
2476   }
2477 
2478   return is_viable_kf;
2479 }
find_next_key_frame(VP8_COMP * cpi,FIRSTPASS_STATS * this_frame)2480 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2481   int i, j;
2482   FIRSTPASS_STATS last_frame;
2483   FIRSTPASS_STATS first_frame;
2484   FIRSTPASS_STATS next_frame;
2485   FIRSTPASS_STATS *start_position;
2486 
2487   double decay_accumulator = 1.0;
2488   double boost_score = 0;
2489   double old_boost_score = 0.0;
2490   double loop_decay_rate;
2491 
2492   double kf_mod_err = 0.0;
2493   double kf_group_err = 0.0;
2494   double kf_group_intra_err = 0.0;
2495   double kf_group_coded_err = 0.0;
2496   double recent_loop_decay[8] = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
2497 
2498   memset(&next_frame, 0, sizeof(next_frame));
2499 
2500   vpx_clear_system_state();
2501   start_position = cpi->twopass.stats_in;
2502 
2503   cpi->common.frame_type = KEY_FRAME;
2504 
2505   /* is this a forced key frame by interval */
2506   cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2507 
2508   /* Clear the alt ref active flag as this can never be active on a key
2509    * frame
2510    */
2511   cpi->source_alt_ref_active = 0;
2512 
2513   /* Kf is always a gf so clear frames till next gf counter */
2514   cpi->frames_till_gf_update_due = 0;
2515 
2516   cpi->twopass.frames_to_key = 1;
2517 
2518   /* Take a copy of the initial frame details */
2519   memcpy(&first_frame, this_frame, sizeof(*this_frame));
2520 
2521   cpi->twopass.kf_group_bits = 0;
2522   cpi->twopass.kf_group_error_left = 0;
2523 
2524   kf_mod_err = calculate_modified_err(cpi, this_frame);
2525 
2526   /* find the next keyframe */
2527   i = 0;
2528   while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) {
2529     /* Accumulate kf group error */
2530     kf_group_err += calculate_modified_err(cpi, this_frame);
2531 
2532     /* These figures keep intra and coded error counts for all frames
2533      * including key frames in the group. The effect of the key frame
2534      * itself can be subtracted out using the first_frame data
2535      * collected above
2536      */
2537     kf_group_intra_err += this_frame->intra_error;
2538     kf_group_coded_err += this_frame->coded_error;
2539 
2540     /* Load the next frame's stats. */
2541     memcpy(&last_frame, this_frame, sizeof(*this_frame));
2542     input_stats(cpi, this_frame);
2543 
2544     /* Provided that we are not at the end of the file... */
2545     if (cpi->oxcf.auto_key &&
2546         lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2547       /* Normal scene cut check */
2548       if ((i >= MIN_GF_INTERVAL) &&
2549           test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) {
2550         break;
2551       }
2552 
2553       /* How fast is prediction quality decaying */
2554       loop_decay_rate = get_prediction_decay_rate(&next_frame);
2555 
2556       /* We want to know something about the recent past... rather than
2557        * as used elsewhere where we are concened with decay in prediction
2558        * quality since the last GF or KF.
2559        */
2560       recent_loop_decay[i % 8] = loop_decay_rate;
2561       decay_accumulator = 1.0;
2562       for (j = 0; j < 8; ++j) {
2563         decay_accumulator = decay_accumulator * recent_loop_decay[j];
2564       }
2565 
2566       /* Special check for transition or high motion followed by a
2567        * static scene.
2568        */
2569       if (detect_transition_to_still(cpi, i,
2570                                      ((int)(cpi->key_frame_frequency) - (int)i),
2571                                      loop_decay_rate, decay_accumulator)) {
2572         break;
2573       }
2574 
2575       /* Step on to the next frame */
2576       cpi->twopass.frames_to_key++;
2577 
2578       /* If we don't have a real key frame within the next two
2579        * forcekeyframeevery intervals then break out of the loop.
2580        */
2581       if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency) {
2582         break;
2583       }
2584     } else {
2585       cpi->twopass.frames_to_key++;
2586     }
2587 
2588     i++;
2589   }
2590 
2591   /* If there is a max kf interval set by the user we must obey it.
2592    * We already breakout of the loop above at 2x max.
2593    * This code centers the extra kf if the actual natural
2594    * interval is between 1x and 2x
2595    */
2596   if (cpi->oxcf.auto_key &&
2597       cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) {
2598     FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2599     FIRSTPASS_STATS tmp_frame;
2600 
2601     cpi->twopass.frames_to_key /= 2;
2602 
2603     /* Copy first frame details */
2604     memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2605 
2606     /* Reset to the start of the group */
2607     reset_fpf_position(cpi, start_position);
2608 
2609     kf_group_err = 0;
2610     kf_group_intra_err = 0;
2611     kf_group_coded_err = 0;
2612 
2613     /* Rescan to get the correct error data for the forced kf group */
2614     for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
2615       /* Accumulate kf group errors */
2616       kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2617       kf_group_intra_err += tmp_frame.intra_error;
2618       kf_group_coded_err += tmp_frame.coded_error;
2619 
2620       /* Load a the next frame's stats */
2621       input_stats(cpi, &tmp_frame);
2622     }
2623 
2624     /* Reset to the start of the group */
2625     reset_fpf_position(cpi, current_pos);
2626 
2627     cpi->next_key_frame_forced = 1;
2628   } else {
2629     cpi->next_key_frame_forced = 0;
2630   }
2631 
2632   /* Special case for the last frame of the file */
2633   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
2634     /* Accumulate kf group error */
2635     kf_group_err += calculate_modified_err(cpi, this_frame);
2636 
2637     /* These figures keep intra and coded error counts for all frames
2638      * including key frames in the group. The effect of the key frame
2639      * itself can be subtracted out using the first_frame data
2640      * collected above
2641      */
2642     kf_group_intra_err += this_frame->intra_error;
2643     kf_group_coded_err += this_frame->coded_error;
2644   }
2645 
2646   /* Calculate the number of bits that should be assigned to the kf group. */
2647   if ((cpi->twopass.bits_left > 0) &&
2648       (cpi->twopass.modified_error_left > 0.0)) {
2649     /* Max for a single normal frame (not key frame) */
2650     int max_bits = frame_max_bits(cpi);
2651 
2652     /* Maximum bits for the kf group */
2653     int64_t max_grp_bits;
2654 
2655     /* Default allocation based on bits left and relative
2656      * complexity of the section
2657      */
2658     cpi->twopass.kf_group_bits =
2659         (int64_t)(cpi->twopass.bits_left *
2660                   (kf_group_err / cpi->twopass.modified_error_left));
2661 
2662     /* Clip based on maximum per frame rate defined by the user. */
2663     max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2664     if (cpi->twopass.kf_group_bits > max_grp_bits) {
2665       cpi->twopass.kf_group_bits = max_grp_bits;
2666     }
2667 
2668     /* Additional special case for CBR if buffer is getting full. */
2669     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2670       int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2671       int64_t buffer_lvl = cpi->buffer_level;
2672 
2673       /* If the buffer is near or above the optimal and this kf group is
2674        * not being allocated much then increase the allocation a bit.
2675        */
2676       if (buffer_lvl >= opt_buffer_lvl) {
2677         int64_t high_water_mark =
2678             (opt_buffer_lvl + cpi->oxcf.maximum_buffer_size) >> 1;
2679 
2680         int64_t av_group_bits;
2681 
2682         /* Av bits per frame * number of frames */
2683         av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
2684                         (int64_t)cpi->twopass.frames_to_key;
2685 
2686         /* We are at or above the maximum. */
2687         if (cpi->buffer_level >= high_water_mark) {
2688           int64_t min_group_bits;
2689 
2690           min_group_bits =
2691               av_group_bits + (int64_t)(buffer_lvl - high_water_mark);
2692 
2693           if (cpi->twopass.kf_group_bits < min_group_bits) {
2694             cpi->twopass.kf_group_bits = min_group_bits;
2695           }
2696         }
2697         /* We are above optimal but below the maximum */
2698         else if (cpi->twopass.kf_group_bits < av_group_bits) {
2699           int64_t bits_below_av = av_group_bits - cpi->twopass.kf_group_bits;
2700 
2701           cpi->twopass.kf_group_bits += (int64_t)(
2702               (double)bits_below_av * (double)(buffer_lvl - opt_buffer_lvl) /
2703               (double)(high_water_mark - opt_buffer_lvl));
2704         }
2705       }
2706     }
2707   } else {
2708     cpi->twopass.kf_group_bits = 0;
2709   }
2710 
2711   /* Reset the first pass file position */
2712   reset_fpf_position(cpi, start_position);
2713 
2714   /* determine how big to make this keyframe based on how well the
2715    * subsequent frames use inter blocks
2716    */
2717   decay_accumulator = 1.0;
2718   boost_score = 0.0;
2719 
2720   for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
2721     double r;
2722 
2723     if (EOF == input_stats(cpi, &next_frame)) break;
2724 
2725     if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) {
2726       r = (IIKFACTOR2 * next_frame.intra_error /
2727            DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2728     } else {
2729       r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2730            DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2731     }
2732 
2733     if (r > RMAX) r = RMAX;
2734 
2735     /* How fast is prediction quality decaying */
2736     loop_decay_rate = get_prediction_decay_rate(&next_frame);
2737 
2738     decay_accumulator = decay_accumulator * loop_decay_rate;
2739     decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2740 
2741     boost_score += (decay_accumulator * r);
2742 
2743     if ((i > MIN_GF_INTERVAL) && ((boost_score - old_boost_score) < 1.0)) {
2744       break;
2745     }
2746 
2747     old_boost_score = boost_score;
2748   }
2749 
2750   if (1) {
2751     FIRSTPASS_STATS sectionstats;
2752     double Ratio;
2753 
2754     zero_stats(&sectionstats);
2755     reset_fpf_position(cpi, start_position);
2756 
2757     for (i = 0; i < cpi->twopass.frames_to_key; ++i) {
2758       input_stats(cpi, &next_frame);
2759       accumulate_stats(&sectionstats, &next_frame);
2760     }
2761 
2762     avg_stats(&sectionstats);
2763 
2764     cpi->twopass.section_intra_rating =
2765         (unsigned int)(sectionstats.intra_error /
2766                        DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2767 
2768     Ratio = sectionstats.intra_error /
2769             DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2770     cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2771 
2772     if (cpi->twopass.section_max_qfactor < 0.80) {
2773       cpi->twopass.section_max_qfactor = 0.80;
2774     }
2775   }
2776 
2777   /* When using CBR apply additional buffer fullness related upper limits */
2778   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2779     double max_boost;
2780 
2781     if (cpi->drop_frames_allowed) {
2782       int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark *
2783                                   (cpi->oxcf.optimal_buffer_level / 100));
2784 
2785       if (cpi->buffer_level > df_buffer_level) {
2786         max_boost =
2787             ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) /
2788             DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2789       } else {
2790         max_boost = 0.0;
2791       }
2792     } else if (cpi->buffer_level > 0) {
2793       max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) /
2794                   DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2795     } else {
2796       max_boost = 0.0;
2797     }
2798 
2799     if (boost_score > max_boost) boost_score = max_boost;
2800   }
2801 
2802   /* Reset the first pass file position */
2803   reset_fpf_position(cpi, start_position);
2804 
2805   /* Work out how many bits to allocate for the key frame itself */
2806   if (1) {
2807     int kf_boost = (int)boost_score;
2808     int allocation_chunks;
2809     int Counter = cpi->twopass.frames_to_key;
2810     int alt_kf_bits;
2811     YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2812 /* Min boost based on kf interval */
2813 #if 0
2814 
2815         while ((kf_boost < 48) && (Counter > 0))
2816         {
2817             Counter -= 2;
2818             kf_boost ++;
2819         }
2820 
2821 #endif
2822 
2823     if (kf_boost < 48) {
2824       kf_boost += ((Counter + 1) >> 1);
2825 
2826       if (kf_boost > 48) kf_boost = 48;
2827     }
2828 
2829     /* bigger frame sizes need larger kf boosts, smaller frames smaller
2830      * boosts...
2831      */
2832     if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240)) {
2833       kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2834     } else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240)) {
2835       kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2836     }
2837 
2838     /* Min KF boost */
2839     kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */
2840     if (kf_boost < 250) kf_boost = 250;
2841 
2842     /*
2843      * We do three calculations for kf size.
2844      * The first is based on the error score for the whole kf group.
2845      * The second (optionaly) on the key frames own error if this is
2846      * smaller than the average for the group.
2847      * The final one insures that the frame receives at least the
2848      * allocation it would have received based on its own error score vs
2849      * the error score remaining
2850      * Special case if the sequence appears almost totaly static
2851      * as measured by the decay accumulator. In this case we want to
2852      * spend almost all of the bits on the key frame.
2853      * cpi->twopass.frames_to_key-1 because key frame itself is taken
2854      * care of by kf_boost.
2855      */
2856     if (decay_accumulator >= 0.99) {
2857       allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
2858     } else {
2859       allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
2860     }
2861 
2862     /* Normalize Altboost and allocations chunck down to prevent overflow */
2863     while (kf_boost > 1000) {
2864       kf_boost /= 2;
2865       allocation_chunks /= 2;
2866     }
2867 
2868     cpi->twopass.kf_group_bits =
2869         (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
2870 
2871     /* Calculate the number of bits to be spent on the key frame */
2872     cpi->twopass.kf_bits =
2873         (int)((double)kf_boost *
2874               ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
2875 
2876     /* Apply an additional limit for CBR */
2877     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2878       if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2)) {
2879         cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2);
2880       }
2881     }
2882 
2883     /* If the key frame is actually easier than the average for the
2884      * kf group (which does sometimes happen... eg a blank intro frame)
2885      * Then use an alternate calculation based on the kf error score
2886      * which should give a smaller key frame.
2887      */
2888     if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) {
2889       double alt_kf_grp_bits =
2890           ((double)cpi->twopass.bits_left *
2891            (kf_mod_err * (double)cpi->twopass.frames_to_key) /
2892            DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
2893 
2894       alt_kf_bits = (int)((double)kf_boost *
2895                           (alt_kf_grp_bits / (double)allocation_chunks));
2896 
2897       if (cpi->twopass.kf_bits > alt_kf_bits) {
2898         cpi->twopass.kf_bits = alt_kf_bits;
2899       }
2900     }
2901     /* Else if it is much harder than other frames in the group make sure
2902      * it at least receives an allocation in keeping with its relative
2903      * error score
2904      */
2905     else {
2906       alt_kf_bits = (int)((double)cpi->twopass.bits_left *
2907                           (kf_mod_err / DOUBLE_DIVIDE_CHECK(
2908                                             cpi->twopass.modified_error_left)));
2909 
2910       if (alt_kf_bits > cpi->twopass.kf_bits) {
2911         cpi->twopass.kf_bits = alt_kf_bits;
2912       }
2913     }
2914 
2915     cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
2916     /* Add in the minimum frame allowance */
2917     cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
2918 
2919     /* Peer frame bit target for this frame */
2920     cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
2921 
2922     /* Convert to a per second bitrate */
2923     cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * cpi->output_framerate);
2924   }
2925 
2926   /* Note the total error score of the kf group minus the key frame itself */
2927   cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2928 
2929   /* Adjust the count of total modified error left. The count of bits left
2930    * is adjusted elsewhere based on real coded frame sizes
2931    */
2932   cpi->twopass.modified_error_left -= kf_group_err;
2933 
2934   if (cpi->oxcf.allow_spatial_resampling) {
2935     int resample_trigger = 0;
2936     int last_kf_resampled = 0;
2937     int kf_q;
2938     int scale_val = 0;
2939     int hr, hs, vr, vs;
2940     int new_width = cpi->oxcf.Width;
2941     int new_height = cpi->oxcf.Height;
2942 
2943     int projected_buffer_level;
2944     int tmp_q;
2945 
2946     double projected_bits_perframe;
2947     double group_iiratio = (kf_group_intra_err - first_frame.intra_error) /
2948                            (kf_group_coded_err - first_frame.coded_error);
2949     double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
2950     double bits_per_frame;
2951     double av_bits_per_frame;
2952     double effective_size_ratio;
2953 
2954     if ((cpi->common.Width != cpi->oxcf.Width) ||
2955         (cpi->common.Height != cpi->oxcf.Height)) {
2956       last_kf_resampled = 1;
2957     }
2958 
2959     /* Set back to unscaled by defaults */
2960     cpi->common.horiz_scale = VP8E_NORMAL;
2961     cpi->common.vert_scale = VP8E_NORMAL;
2962 
2963     /* Calculate Average bits per frame. */
2964     av_bits_per_frame = cpi->oxcf.target_bandwidth /
2965                         DOUBLE_DIVIDE_CHECK((double)cpi->framerate);
2966 
2967     /* CBR... Use the clip average as the target for deciding resample */
2968     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2969       bits_per_frame = av_bits_per_frame;
2970     }
2971 
2972     /* In VBR we want to avoid downsampling in easy section unless we
2973      * are under extreme pressure So use the larger of target bitrate
2974      * for this section or average bitrate for sequence
2975      */
2976     else {
2977       /* This accounts for how hard the section is... */
2978       bits_per_frame =
2979           (double)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key);
2980 
2981       /* Don't turn to resampling in easy sections just because they
2982        * have been assigned a small number of bits
2983        */
2984       if (bits_per_frame < av_bits_per_frame) {
2985         bits_per_frame = av_bits_per_frame;
2986       }
2987     }
2988 
2989     /* bits_per_frame should comply with our minimum */
2990     if (bits_per_frame < (cpi->oxcf.target_bandwidth *
2991                           cpi->oxcf.two_pass_vbrmin_section / 100)) {
2992       bits_per_frame = (cpi->oxcf.target_bandwidth *
2993                         cpi->oxcf.two_pass_vbrmin_section / 100);
2994     }
2995 
2996     /* Work out if spatial resampling is necessary */
2997     kf_q = estimate_kf_group_q(cpi, err_per_frame, (int)bits_per_frame,
2998                                group_iiratio);
2999 
3000     /* If we project a required Q higher than the maximum allowed Q then
3001      * make a guess at the actual size of frames in this section
3002      */
3003     projected_bits_perframe = bits_per_frame;
3004     tmp_q = kf_q;
3005 
3006     while (tmp_q > cpi->worst_quality) {
3007       projected_bits_perframe *= 1.04;
3008       tmp_q--;
3009     }
3010 
3011     /* Guess at buffer level at the end of the section */
3012     projected_buffer_level =
3013         (int)(cpi->buffer_level -
3014               (int)((projected_bits_perframe - av_bits_per_frame) *
3015                     cpi->twopass.frames_to_key));
3016 
3017     /* The trigger for spatial resampling depends on the various
3018      * parameters such as whether we are streaming (CBR) or VBR.
3019      */
3020     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3021       /* Trigger resample if we are projected to fall below down
3022        * sample level or resampled last time and are projected to
3023        * remain below the up sample level
3024        */
3025       if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark *
3026                                      cpi->oxcf.optimal_buffer_level / 100)) ||
3027           (last_kf_resampled &&
3028            (projected_buffer_level < (cpi->oxcf.resample_up_water_mark *
3029                                       cpi->oxcf.optimal_buffer_level / 100)))) {
3030         resample_trigger = 1;
3031       } else {
3032         resample_trigger = 0;
3033       }
3034     } else {
3035       int64_t clip_bits = (int64_t)(
3036           cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth /
3037           DOUBLE_DIVIDE_CHECK((double)cpi->framerate));
3038       int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
3039 
3040       /* If triggered last time the threshold for triggering again is
3041        * reduced:
3042        *
3043        * Projected Q higher than allowed and Overspend > 5% of total
3044        * bits
3045        */
3046       if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||
3047           ((kf_q > cpi->worst_quality) && (over_spend > clip_bits / 20))) {
3048         resample_trigger = 1;
3049       } else {
3050         resample_trigger = 0;
3051       }
3052     }
3053 
3054     if (resample_trigger) {
3055       while ((kf_q >= cpi->worst_quality) && (scale_val < 6)) {
3056         scale_val++;
3057 
3058         cpi->common.vert_scale = vscale_lookup[scale_val];
3059         cpi->common.horiz_scale = hscale_lookup[scale_val];
3060 
3061         Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
3062         Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
3063 
3064         new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3065         new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3066 
3067         /* Reducing the area to 1/4 does not reduce the complexity
3068          * (err_per_frame) to 1/4... effective_sizeratio attempts
3069          * to provide a crude correction for this
3070          */
3071         effective_size_ratio = (double)(new_width * new_height) /
3072                                (double)(cpi->oxcf.Width * cpi->oxcf.Height);
3073         effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
3074 
3075         /* Now try again and see what Q we get with the smaller
3076          * image size
3077          */
3078         kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio,
3079                                    (int)bits_per_frame, group_iiratio);
3080       }
3081     }
3082 
3083     if ((cpi->common.Width != new_width) ||
3084         (cpi->common.Height != new_height)) {
3085       cpi->common.Width = new_width;
3086       cpi->common.Height = new_height;
3087       vp8_alloc_compressor_data(cpi);
3088     }
3089   }
3090 }
3091