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