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