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