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