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