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