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