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