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