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