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