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