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