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