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