2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
16 #include "encodeintra.h"
17 #include "vp8/common/setupintrarecon.h"
19 #include "vpx_scale/vpxscale.h"
21 #include "vp8/common/extend.h"
22 #include "vp8/common/systemdependent.h"
23 #include "vpx_scale/yv12extend.h"
24 #include "vpx_mem/vpx_mem.h"
25 #include "vp8/common/swapyv12buffer.h"
28 #include "vp8/common/quant_common.h"
31 //#define OUTPUT_FPF 1
33 #if CONFIG_RUNTIME_CPU_DETECT
34 #define IF_RTCD(x) (x)
36 #define IF_RTCD(x) NULL
39 extern void vp8_build_block_offsets(MACROBLOCK *x);
40 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
41 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
42 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv);
43 extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
45 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10))
46 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10))
47 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
48 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
50 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
53 #define IIKFACTOR1 1.40
54 #define IIKFACTOR2 1.5
58 #define KF_MB_INTRA_MIN 300
59 #define GF_MB_INTRA_MIN 200
61 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
63 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
64 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
66 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
67 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
70 const int cq_level[QINDEX_RANGE] =
72 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
73 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
74 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
75 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
76 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
77 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
78 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
79 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
82 void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
83 int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps);
85 int vp8_encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred)
89 int intra_pred_var = 0;
94 x->e_mbd.mode_info_context->mbmi.mode = DC_PRED;
95 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
96 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
98 vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
102 for (i = 0; i < 16; i++)
104 BLOCKD *b = &x->e_mbd.block[i];
105 BLOCK *be = &x->block[i];
107 vp8_encode_intra4x4block(IF_RTCD(&cpi->rtcd), x, be, b, B_DC_PRED);
111 intra_pred_var = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff);
113 return intra_pred_var;
116 // Resets the first pass file to the given position using a relative seek from the current position
117 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
119 cpi->stats_in = Position;
122 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
124 if (cpi->stats_in >= cpi->stats_in_end)
127 *next_frame = *cpi->stats_in;
131 // Calculate a modified Error used in distributing bits between easier and harder frames
132 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
134 double av_err = cpi->total_stats->ssim_weighted_pred_err;
135 double this_err = this_frame->ssim_weighted_pred_err;
138 //double relative_next_iiratio;
139 //double next_iiratio;
140 //double sum_iiratio;
143 //FIRSTPASS_STATS next_frame;
144 //FIRSTPASS_STATS *start_pos;
146 /*start_pos = cpi->stats_in;
149 while ( (i < 1) && vp8_input_stats(cpi,&next_frame) != EOF )
152 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
153 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio;
154 sum_iiratio += next_iiratio;
159 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->avg_iiratio * (double)i);
163 relative_next_iiratio = 1.0;
165 reset_fpf_position(cpi, start_pos);*/
167 if (this_err > av_err)
168 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
170 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
173 relative_next_iiratio = pow(relative_next_iiratio,0.25);
174 modified_err = modified_err * relative_next_iiratio;
180 static const double weight_table[256] = {
181 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
182 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
183 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
184 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
185 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
186 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
187 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
188 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
189 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
190 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
191 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
192 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
193 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
194 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
195 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
196 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
197 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
198 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
199 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
200 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
201 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
202 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
203 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
204 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
205 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
206 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
207 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
208 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
209 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
210 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
211 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
212 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
215 double vp8_simple_weight(YV12_BUFFER_CONFIG *source)
219 unsigned char *src = source->y_buffer;
220 double sum_weights = 0.0;
222 // Loop throught the Y plane raw examining levels and creating a weight for the image
223 i = source->y_height;
229 sum_weights += weight_table[ *src];
232 src -= source->y_width;
233 src += source->y_stride;
236 sum_weights /= (source->y_height * source->y_width);
242 // This function returns the current per frame maximum bitrate target
243 int frame_max_bits(VP8_COMP *cpi)
245 // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
248 // For CBR we need to also consider buffer fullness.
249 // If we are running below the optimal level then we need to gradually tighten up on max_bits.
250 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
252 double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
254 // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
255 max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
257 // If our buffer is below the optimum level
258 if (buffer_fullness_ratio < 1.0)
260 // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
261 int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
263 max_bits = (int)(max_bits * buffer_fullness_ratio);
265 if (max_bits < min_max_bits)
266 max_bits = min_max_bits; // Lowest value we will set ... which should allow the buffer to refil.
272 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
273 max_bits = (int)(((double)cpi->bits_left / (cpi->total_stats->count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
276 // Trap case where we are out of bits
284 extern size_t vp8_firstpass_stats_sz(unsigned int mb_count)
286 /* Calculate the size of a stats packet, which is dependent on the frame
287 * resolution. The FIRSTPASS_STATS struct has a single element array,
288 * motion_map, which is virtually expanded to have one element per
293 stats_sz = sizeof(FIRSTPASS_STATS) + mb_count;
294 stats_sz = (stats_sz + 7) & ~7;
299 void vp8_output_stats(const VP8_COMP *cpi,
300 struct vpx_codec_pkt_list *pktlist,
301 FIRSTPASS_STATS *stats)
303 struct vpx_codec_cx_pkt pkt;
304 pkt.kind = VPX_CODEC_STATS_PKT;
305 pkt.data.twopass_stats.buf = stats;
306 pkt.data.twopass_stats.sz = vp8_firstpass_stats_sz(cpi->common.MBs);
307 vpx_codec_pkt_list_add(pktlist, &pkt);
313 fpfile = fopen("firstpass.stt", "a");
315 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
316 " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.0f"
321 stats->ssim_weighted_pred_err,
324 stats->pcnt_second_ref,
332 stats->mv_in_out_count,
338 fpfile = fopen("fpmotionmap.stt", "a");
339 if(fwrite(cpi->fp_motion_map, 1, cpi->common.MBs, fpfile));
345 int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
347 size_t stats_sz = vp8_firstpass_stats_sz(cpi->common.MBs);
349 if (cpi->stats_in >= cpi->stats_in_end)
352 *fps = *cpi->stats_in;
353 cpi->stats_in = (void*)((char *)cpi->stats_in + stats_sz);
357 void vp8_zero_stats(FIRSTPASS_STATS *section)
359 section->frame = 0.0;
360 section->intra_error = 0.0;
361 section->coded_error = 0.0;
362 section->ssim_weighted_pred_err = 0.0;
363 section->pcnt_inter = 0.0;
364 section->pcnt_motion = 0.0;
365 section->pcnt_second_ref = 0.0;
366 section->pcnt_neutral = 0.0;
368 section->mvr_abs = 0.0;
370 section->mvc_abs = 0.0;
373 section->mv_in_out_count = 0.0;
374 section->count = 0.0;
375 section->duration = 1.0;
377 void vp8_accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
379 section->frame += frame->frame;
380 section->intra_error += frame->intra_error;
381 section->coded_error += frame->coded_error;
382 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
383 section->pcnt_inter += frame->pcnt_inter;
384 section->pcnt_motion += frame->pcnt_motion;
385 section->pcnt_second_ref += frame->pcnt_second_ref;
386 section->pcnt_neutral += frame->pcnt_neutral;
387 section->MVr += frame->MVr;
388 section->mvr_abs += frame->mvr_abs;
389 section->MVc += frame->MVc;
390 section->mvc_abs += frame->mvc_abs;
391 section->MVrv += frame->MVrv;
392 section->MVcv += frame->MVcv;
393 section->mv_in_out_count += frame->mv_in_out_count;
394 section->count += frame->count;
395 section->duration += frame->duration;
397 void vp8_avg_stats(FIRSTPASS_STATS *section)
399 if (section->count < 1.0)
402 section->intra_error /= section->count;
403 section->coded_error /= section->count;
404 section->ssim_weighted_pred_err /= section->count;
405 section->pcnt_inter /= section->count;
406 section->pcnt_second_ref /= section->count;
407 section->pcnt_neutral /= section->count;
408 section->pcnt_motion /= section->count;
409 section->MVr /= section->count;
410 section->mvr_abs /= section->count;
411 section->MVc /= section->count;
412 section->mvc_abs /= section->count;
413 section->MVrv /= section->count;
414 section->MVcv /= section->count;
415 section->mv_in_out_count /= section->count;
416 section->duration /= section->count;
419 unsigned char *vp8_fpmm_get_pos(VP8_COMP *cpi)
421 return cpi->fp_motion_map_stats;
423 void vp8_fpmm_reset_pos(VP8_COMP *cpi, unsigned char *target_pos)
425 cpi->fp_motion_map_stats = target_pos;
428 void vp8_advance_fpmm(VP8_COMP *cpi, int count)
430 cpi->fp_motion_map_stats = (void*)((char*)cpi->fp_motion_map_stats +
431 count * vp8_firstpass_stats_sz(cpi->common.MBs));
434 void vp8_input_fpmm(VP8_COMP *cpi)
436 unsigned char *fpmm = cpi->fp_motion_map;
437 int MBs = cpi->common.MBs;
438 int max_frames = cpi->active_arnr_frames;
441 for (i=0; i<max_frames; i++)
443 char *motion_map = (char*)cpi->fp_motion_map_stats
444 + sizeof(FIRSTPASS_STATS);
446 memcpy(fpmm, motion_map, MBs);
448 vp8_advance_fpmm(cpi, 1);
451 // Flag the use of weights in the temporal filter
452 cpi->use_weighted_temporal_filter = 1;
455 void vp8_init_first_pass(VP8_COMP *cpi)
457 vp8_zero_stats(cpi->total_stats);
463 fpfile = fopen("firstpass.stt", "w");
465 fpfile = fopen("fpmotionmap.stt", "wb");
472 void vp8_end_first_pass(VP8_COMP *cpi)
474 vp8_output_stats(cpi, cpi->output_pkt_list, cpi->total_stats);
477 void vp8_zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset )
479 MACROBLOCKD * const xd = & x->e_mbd;
480 BLOCK *b = &x->block[0];
481 BLOCKD *d = &x->e_mbd.block[0];
483 unsigned char *src_ptr = (*(b->base_src) + b->src);
484 int src_stride = b->src_stride;
485 unsigned char *ref_ptr;
486 int ref_stride=d->pre_stride;
488 // Set up pointers for this macro block recon buffer
489 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
491 ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre );
493 VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_stride, ref_ptr, ref_stride, (unsigned int *)(best_motion_err));
496 void vp8_first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, MV *ref_mv, MV *best_mv, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset )
498 MACROBLOCKD *const xd = & x->e_mbd;
499 BLOCK *b = &x->block[0];
500 BLOCKD *d = &x->e_mbd.block[0];
506 int step_param = 3; //3; // Dont search over full range for first pass
507 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3;
509 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
510 int new_mv_mode_penalty = 256;
512 // override the default variance function to use MSE
513 v_fn_ptr.vf = VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16);
515 // Set up pointers for this macro block recon buffer
516 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
518 // Initial step/diamond search centred on best mv
519 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost, ref_mv);
520 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
521 tmp_err += new_mv_mode_penalty;
523 if (tmp_err < *best_motion_err)
525 *best_motion_err = tmp_err;
526 best_mv->row = tmp_mv.row;
527 best_mv->col = tmp_mv.col;
530 // Further step/diamond searches as necessary
534 while (n < further_steps)
542 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param + n, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost, ref_mv);
543 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
544 tmp_err += new_mv_mode_penalty;
546 if (tmp_err < *best_motion_err)
548 *best_motion_err = tmp_err;
549 best_mv->row = tmp_mv.row;
550 best_mv->col = tmp_mv.col;
556 void vp8_first_pass(VP8_COMP *cpi)
559 MACROBLOCK *const x = & cpi->mb;
560 VP8_COMMON *const cm = & cpi->common;
561 MACROBLOCKD *const xd = & x->e_mbd;
563 int col_blocks = 4 * cm->mb_cols;
564 int recon_yoffset, recon_uvoffset;
565 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
566 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
567 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
568 int recon_y_stride = lst_yv12->y_stride;
569 int recon_uv_stride = lst_yv12->uv_stride;
570 long long intra_error = 0;
571 long long coded_error = 0;
573 int sum_mvr = 0, sum_mvc = 0;
574 int sum_mvr_abs = 0, sum_mvc_abs = 0;
575 int sum_mvrs = 0, sum_mvcs = 0;
578 int second_ref_count = 0;
579 int intrapenalty = 256;
580 int neutral_count = 0;
582 int sum_in_vectors = 0;
584 MV zero_ref_mv = {0, 0};
586 unsigned char *fp_motion_map_ptr = cpi->fp_motion_map;
588 vp8_clear_system_state(); //__asm emms;
590 x->src = * cpi->Source;
594 x->partition_info = x->pi;
596 xd->mode_info_context = cm->mi;
598 vp8_build_block_offsets(x);
600 vp8_setup_block_dptrs(&x->e_mbd);
602 vp8_setup_block_ptrs(x);
604 // set up frame new frame for intra coded blocks
605 vp8_setup_intra_recon(new_yv12);
606 vp8cx_frame_init_quantizer(cpi);
608 // Initialise the MV cost table to the defaults
609 //if( cm->current_video_frame == 0)
612 int flag[2] = {1, 1};
613 vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
614 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
615 vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
618 // for each macroblock row in image
619 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
623 best_ref_mv.as_int = 0;
625 // reset above block coeffs
626 xd->up_available = (mb_row != 0);
627 recon_yoffset = (mb_row * recon_y_stride * 16);
628 recon_uvoffset = (mb_row * recon_uv_stride * 8);
630 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
631 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
632 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
635 // for each macroblock col in image
636 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
640 int zz_to_best_ratio;
641 int gf_motion_error = INT_MAX;
642 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
644 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
645 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
646 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
647 xd->left_available = (mb_col != 0);
649 // do intra 16x16 prediction
650 this_error = vp8_encode_intra(cpi, x, use_dc_pred);
652 // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
653 // We do not have special cases in first pass for 0,0 and nearest etc so all inter modes carry an overhead cost estimate fot the mv.
654 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
655 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
656 this_error += intrapenalty;
658 // Cumulative intra error total
659 intra_error += (long long)this_error;
661 // Indicate default assumption of intra in the motion map
662 *fp_motion_map_ptr = 0;
664 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
665 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
666 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
668 // Other than for the first frame do a motion search
669 if (cm->current_video_frame > 0)
671 BLOCK *b = &x->block[0];
672 BLOCKD *d = &x->e_mbd.block[0];
675 int motion_error = INT_MAX;
677 // Simple 0,0 motion with no mv overhead
678 vp8_zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset );
679 d->bmi.mv.as_mv.row = 0;
680 d->bmi.mv.as_mv.col = 0;
682 // Save (0,0) error for later use
683 zero_error = motion_error;
685 // Test last reference frame using the previous best mv as the
686 // starting point (best reference) for the search
687 vp8_first_pass_motion_search(cpi, x, &best_ref_mv.as_mv,
688 &d->bmi.mv.as_mv, lst_yv12,
689 &motion_error, recon_yoffset);
691 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
692 if (best_ref_mv.as_int)
695 vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
696 lst_yv12, &tmp_err, recon_yoffset);
698 if ( tmp_err < motion_error )
700 motion_error = tmp_err;
701 d->bmi.mv.as_mv.row = tmp_mv.row;
702 d->bmi.mv.as_mv.col = tmp_mv.col;
706 // Experimental search in a second reference frame ((0,0) based only)
707 if (cm->current_video_frame > 1)
709 vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
711 if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
714 //motion_error = gf_motion_error;
715 //d->bmi.mv.as_mv.row = tmp_mv.row;
716 //d->bmi.mv.as_mv.col = tmp_mv.col;
720 xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
721 xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
722 xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
726 // Reset to last frame as reference buffer
727 xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
728 xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
729 xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
732 /* Intra assumed best */
733 best_ref_mv.as_int = 0;
735 if (motion_error <= this_error)
737 // Keep a count of cases where the inter and intra were
738 // very close and very low. This helps with scene cut
739 // detection for example in cropped clips with black bars
740 // at the sides or top and bottom.
741 if( (((this_error-intrapenalty) * 9) <=
742 (motion_error*10)) &&
743 (this_error < (2*intrapenalty)) )
748 d->bmi.mv.as_mv.row <<= 3;
749 d->bmi.mv.as_mv.col <<= 3;
750 this_error = motion_error;
751 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv.as_mv);
752 vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x);
753 sum_mvr += d->bmi.mv.as_mv.row;
754 sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
755 sum_mvc += d->bmi.mv.as_mv.col;
756 sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
757 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
758 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
761 best_ref_mv.as_int = d->bmi.mv.as_int;
763 // Was the vector non-zero
764 if (d->bmi.mv.as_int)
768 // Does the Row vector point inwards or outwards
769 if (mb_row < cm->mb_rows / 2)
771 if (d->bmi.mv.as_mv.row > 0)
773 else if (d->bmi.mv.as_mv.row < 0)
776 else if (mb_row > cm->mb_rows / 2)
778 if (d->bmi.mv.as_mv.row > 0)
780 else if (d->bmi.mv.as_mv.row < 0)
784 // Does the Row vector point inwards or outwards
785 if (mb_col < cm->mb_cols / 2)
787 if (d->bmi.mv.as_mv.col > 0)
789 else if (d->bmi.mv.as_mv.col < 0)
792 else if (mb_col > cm->mb_cols / 2)
794 if (d->bmi.mv.as_mv.col > 0)
796 else if (d->bmi.mv.as_mv.col < 0)
800 // Compute how close (0,0) predictor is to best
801 // predictor in terms of their prediction error
802 zz_to_best_ratio = (10*zero_error + this_error/2)
803 / (this_error+!this_error);
805 if ((zero_error < 50000) &&
806 (zz_to_best_ratio <= 11) )
807 *fp_motion_map_ptr = 1;
809 *fp_motion_map_ptr = 0;
814 if( zero_error<50000 )
815 *fp_motion_map_ptr = 2;
817 *fp_motion_map_ptr = 1;
822 coded_error += (long long)this_error;
824 // adjust to the next column of macroblocks
825 x->src.y_buffer += 16;
826 x->src.u_buffer += 8;
827 x->src.v_buffer += 8;
832 // Update the motion map
836 // adjust to the next row of mbs
837 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
838 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
839 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
841 //extend the recon for intra prediction
842 vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
843 vp8_clear_system_state(); //__asm emms;
846 vp8_clear_system_state(); //__asm emms;
852 fps.frame = cm->current_video_frame ;
853 fps.intra_error = intra_error >> 8;
854 fps.coded_error = coded_error >> 8;
855 weight = vp8_simple_weight(cpi->Source);
861 fps.ssim_weighted_pred_err = fps.coded_error * weight;
863 fps.pcnt_inter = 0.0;
864 fps.pcnt_motion = 0.0;
871 fps.mv_in_out_count = 0.0;
874 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
875 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
876 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
880 fps.MVr = (double)sum_mvr / (double)mvcount;
881 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
882 fps.MVc = (double)sum_mvc / (double)mvcount;
883 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
884 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
885 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
886 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
888 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
891 // TODO: handle the case when duration is set to 0, or something less
892 // than the full time between subsequent cpi->source_time_stamp s .
893 fps.duration = cpi->source_end_time_stamp - cpi->source_time_stamp;
895 // don't want to do outputstats with a stack variable!
896 memcpy(cpi->this_frame_stats,
898 sizeof(FIRSTPASS_STATS));
899 memcpy((char*)cpi->this_frame_stats + sizeof(FIRSTPASS_STATS),
901 sizeof(cpi->fp_motion_map[0]) * cpi->common.MBs);
902 vp8_output_stats(cpi, cpi->output_pkt_list, cpi->this_frame_stats);
903 vp8_accumulate_stats(cpi->total_stats, &fps);
906 // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
907 if ((cm->current_video_frame > 0) &&
908 (cpi->this_frame_stats->pcnt_inter > 0.20) &&
909 ((cpi->this_frame_stats->intra_error / cpi->this_frame_stats->coded_error) > 2.0))
911 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
914 // swap frame pointers so last frame refers to the frame we just compressed
915 vp8_swap_yv12_buffer(lst_yv12, new_yv12);
916 vp8_yv12_extend_frame_borders(lst_yv12);
918 // Special case for the first frame. Copy into the GF buffer as a second reference.
919 if (cm->current_video_frame == 0)
921 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
925 // use this to see what the first pass reconstruction looks like
930 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
932 if (cm->current_video_frame == 0)
933 recon_file = fopen(filename, "wb");
935 recon_file = fopen(filename, "ab");
937 if(fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file));
941 cm->current_video_frame++;
944 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
946 #define BASE_ERRPERMB 150
947 static int estimate_max_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width)
950 int num_mbs = ((Height * Width) / (16 * 16));
951 int target_norm_bits_per_mb;
953 double err_per_mb = section_err / num_mbs;
954 double correction_factor;
956 double speed_correction = 1.0;
957 double rolling_ratio;
959 double pow_highq = 0.90;
960 double pow_lowq = 0.40;
962 if (section_target_bandwitdh <= 0)
963 return cpi->maxq_max_limit; // Highest value allowed
965 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
967 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
968 if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->worst_quality))
970 //double adjustment_rate = 0.985 + (0.00005 * cpi->active_worst_quality);
971 double adjustment_rate = 0.99;
973 rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
975 //if ( cpi->est_max_qcorrection_factor > rolling_ratio )
976 if (rolling_ratio < 0.95)
977 //cpi->est_max_qcorrection_factor *= adjustment_rate;
978 cpi->est_max_qcorrection_factor -= 0.005;
979 //else if ( cpi->est_max_qcorrection_factor < rolling_ratio )
980 else if (rolling_ratio > 1.05)
981 cpi->est_max_qcorrection_factor += 0.005;
983 //cpi->est_max_qcorrection_factor /= adjustment_rate;
985 cpi->est_max_qcorrection_factor = (cpi->est_max_qcorrection_factor < 0.1) ? 0.1 : (cpi->est_max_qcorrection_factor > 10.0) ? 10.0 : cpi->est_max_qcorrection_factor;
988 // Corrections for higher compression speed settings (reduced compression expected)
989 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
991 if (cpi->oxcf.cpu_used <= 5)
992 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
994 speed_correction = 1.25;
997 // Correction factor used for Q values >= 20
998 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
999 corr_high = (corr_high < 0.05)
1000 ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1002 // Try and pick a max Q that will be high enough to encode the
1003 // content at the given rate.
1004 for (Q = cpi->maxq_min_limit; Q < cpi->maxq_max_limit; Q++)
1006 int bits_per_mb_at_this_q;
1010 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1011 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
1014 correction_factor = corr_high;
1016 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * cpi->section_max_qfactor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1017 //bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1019 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1023 // Restriction on active max q for constrained quality mode.
1024 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
1025 (Q < cpi->cq_target_quality) )
1026 //(Q < cpi->oxcf.cq_level;) )
1028 Q = cpi->cq_target_quality;
1029 //Q = cpi->oxcf.cq_level;
1032 // Adjust maxq_min_limit and maxq_max_limit limits based on
1033 // averaga q observed in clip for non kf/gf.arf frames
1034 // Give average a chance to settle though.
1035 if ( (cpi->ni_frames >
1036 ((unsigned int)cpi->total_stats->count >> 8)) &&
1037 (cpi->ni_frames > 150) )
1039 cpi->maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
1040 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
1041 cpi->maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
1042 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
1047 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width)
1050 int num_mbs = ((Height * Width) / (16 * 16));
1051 int target_norm_bits_per_mb;
1053 double err_per_mb = section_err / num_mbs;
1054 double correction_factor;
1056 double speed_correction = 1.0;
1057 double pow_highq = 0.90;
1058 double pow_lowq = 0.40;
1060 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
1062 // Corrections for higher compression speed settings (reduced compression expected)
1063 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1065 if (cpi->oxcf.cpu_used <= 5)
1066 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1068 speed_correction = 1.25;
1071 // Correction factor used for Q values >= 20
1072 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1073 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1075 // Try and pick a Q that can encode the content at the given rate.
1076 for (Q = 0; Q < MAXQ; Q++)
1078 int bits_per_mb_at_this_q;
1082 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1083 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
1086 correction_factor = corr_high;
1088 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1090 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1097 // Estimate a worst case Q for a KF group
1098 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width, double group_iiratio)
1101 int num_mbs = ((Height * Width) / (16 * 16));
1102 int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1103 int bits_per_mb_at_this_q;
1105 double err_per_mb = section_err / num_mbs;
1106 double err_correction_factor;
1108 double speed_correction = 1.0;
1109 double current_spend_ratio = 1.0;
1111 double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1112 double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1114 double iiratio_correction_factor = 1.0;
1116 double combined_correction_factor;
1118 // Trap special case where the target is <= 0
1119 if (target_norm_bits_per_mb <= 0)
1122 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
1123 // This is clamped to the range 0.1 to 10.0
1124 if (cpi->long_rolling_target_bits <= 0)
1125 current_spend_ratio = 10.0;
1128 current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
1129 current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
1132 // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
1133 // The idea here is to favour subsampling in the hardest sections vs the easyest.
1134 iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1136 if (iiratio_correction_factor < 0.5)
1137 iiratio_correction_factor = 0.5;
1139 // Corrections for higher compression speed settings (reduced compression expected)
1140 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1142 if (cpi->oxcf.cpu_used <= 5)
1143 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1145 speed_correction = 1.25;
1148 // Combine the various factors calculated above
1149 combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
1151 // Correction factor used for Q values >= 20
1152 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1153 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1155 // Try and pick a Q that should be high enough to encode the content at the given rate.
1156 for (Q = 0; Q < MAXQ; Q++)
1158 // Q values < 20 treated as a special case
1161 err_correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1162 err_correction_factor = (err_correction_factor < 0.05) ? 0.05 : (err_correction_factor > 5.0) ? 5.0 : err_correction_factor;
1165 err_correction_factor = corr_high;
1167 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * combined_correction_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q]);
1169 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1173 // If we could not hit the target even at Max Q then estimate what Q would have bee required
1174 while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2)))
1177 bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1183 FILE *f = fopen("estkf_q.stt", "a");
1184 fprintf(f, "%8d %8d %8d %8.2f %8.3f %8.2f %8.3f %8.3f %8.3f %8d\n", cpi->common.current_video_frame, bits_per_mb_at_this_q,
1185 target_norm_bits_per_mb, err_per_mb, err_correction_factor,
1186 current_spend_ratio, group_iiratio, iiratio_correction_factor,
1187 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
1194 // For cq mode estimate a cq level that matches the observed
1195 // complexity and data rate.
1196 static int estimate_cq(VP8_COMP *cpi, double section_err,
1197 int section_target_bandwitdh, int Height, int Width)
1200 int num_mbs = ((Height * Width) / (16 * 16));
1201 int target_norm_bits_per_mb;
1203 double err_per_mb = section_err / num_mbs;
1204 double correction_factor;
1206 double speed_correction = 1.0;
1207 double pow_highq = 0.90;
1208 double pow_lowq = 0.40;
1209 double clip_iiratio;
1210 double clip_iifactor;
1212 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1213 ? (512 * section_target_bandwitdh) / num_mbs
1214 : 512 * (section_target_bandwitdh / num_mbs);
1216 // Corrections for higher compression speed settings
1217 // (reduced compression expected)
1218 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1220 if (cpi->oxcf.cpu_used <= 5)
1221 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1223 speed_correction = 1.25;
1225 // II ratio correction factor for clip as a whole
1226 clip_iiratio = cpi->total_stats->intra_error /
1227 DOUBLE_DIVIDE_CHECK(cpi->total_stats->coded_error);
1228 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1229 if (clip_iifactor < 0.80)
1230 clip_iifactor = 0.80;
1232 // Correction factor used for Q values >= 20
1233 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1234 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1236 // Try and pick a Q that can encode the content at the given rate.
1237 for (Q = 0; Q < MAXQ; Q++)
1239 int bits_per_mb_at_this_q;
1244 pow( err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1246 correction_factor = (correction_factor < 0.05) ? 0.05
1247 : (correction_factor > 5.0) ? 5.0
1248 : correction_factor;
1251 correction_factor = corr_high;
1253 bits_per_mb_at_this_q =
1254 (int)( .5 + correction_factor *
1257 (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1259 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1266 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
1268 void vp8_init_second_pass(VP8_COMP *cpi)
1270 FIRSTPASS_STATS this_frame;
1271 FIRSTPASS_STATS *start_pos;
1273 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1275 vp8_zero_stats(cpi->total_stats);
1277 if (!cpi->stats_in_end)
1280 *cpi->total_stats = *cpi->stats_in_end;
1282 cpi->total_error_left = cpi->total_stats->ssim_weighted_pred_err;
1283 cpi->total_intra_error_left = cpi->total_stats->intra_error;
1284 cpi->total_coded_error_left = cpi->total_stats->coded_error;
1285 cpi->start_tot_err_left = cpi->total_error_left;
1287 //cpi->bits_left = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1288 //cpi->bits_left -= (long long)(cpi->total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1290 // each frame can have a different duration, as the frame rate in the source
1291 // isn't guaranteed to be constant. The frame rate prior to the first frame
1292 // encoded in the second pass is a guess. However the sum duration is not.
1293 // Its calculated based on the actual durations of all frames from the first
1295 vp8_new_frame_rate(cpi, 10000000.0 * cpi->total_stats->count / cpi->total_stats->duration);
1297 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1298 cpi->bits_left = (long long)(cpi->total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1299 cpi->bits_left -= (long long)(cpi->total_stats->duration * two_pass_min_rate / 10000000.0);
1300 cpi->clip_bits_total = cpi->bits_left;
1302 // Calculate a minimum intra value to be used in determining the IIratio
1303 // scores used in the second pass. We have this minimum to make sure
1304 // that clips that are static but "low complexity" in the intra domain
1305 // are still boosted appropriately for KF/GF/ARF
1306 cpi->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1307 cpi->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1309 vp8_avg_stats(cpi->total_stats);
1311 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1313 double sum_iiratio = 0.0;
1316 start_pos = cpi->stats_in; // Note starting "file" position
1318 while (vp8_input_stats(cpi, &this_frame) != EOF)
1320 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1321 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1322 sum_iiratio += IIRatio;
1325 cpi->avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->total_stats->count);
1327 // Reset file position
1328 reset_fpf_position(cpi, start_pos);
1331 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1332 // used to allocate bits
1334 start_pos = cpi->stats_in; // Note starting "file" position
1336 cpi->modified_error_total = 0.0;
1337 cpi->modified_error_used = 0.0;
1339 while (vp8_input_stats(cpi, &this_frame) != EOF)
1341 cpi->modified_error_total += calculate_modified_err(cpi, &this_frame);
1343 cpi->modified_error_left = cpi->modified_error_total;
1345 reset_fpf_position(cpi, start_pos); // Reset file position
1349 // Calculate the clip target modified bits per error
1350 // The observed bpe starts as the same number.
1351 cpi->clip_bpe = cpi->bits_left /
1352 DOUBLE_DIVIDE_CHECK(cpi->modified_error_total);
1353 cpi->observed_bpe = cpi->clip_bpe;
1355 cpi->fp_motion_map_stats = (unsigned char *)cpi->stats_in;
1358 void vp8_end_second_pass(VP8_COMP *cpi)
1362 // This function gives and estimate of how badly we believe
1363 // the predicition quality is decaying from frame to frame.
1364 double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1366 double prediction_decay_rate;
1367 double motion_decay;
1368 double motion_pct = next_frame->pcnt_motion;
1371 // Initial basis is the % mbs inter coded
1372 prediction_decay_rate = next_frame->pcnt_inter;
1374 // High % motion -> somewhat higher decay rate
1375 motion_decay = (1.0 - (motion_pct / 20.0));
1376 if (motion_decay < prediction_decay_rate)
1377 prediction_decay_rate = motion_decay;
1379 // Adjustment to decay rate based on speed of motion
1381 double this_mv_rabs;
1382 double this_mv_cabs;
1383 double distance_factor;
1385 this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1386 this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1388 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
1389 (this_mv_cabs * this_mv_cabs)) / 250.0;
1390 distance_factor = ((distance_factor > 1.0)
1391 ? 0.0 : (1.0 - distance_factor));
1392 if (distance_factor < prediction_decay_rate)
1393 prediction_decay_rate = distance_factor;
1396 return prediction_decay_rate;
1399 // Function to test for a condition where a complex transition is followed
1400 // by a static section. For example in slide shows where there is a fade
1401 // between slides. This is to help with more optimal kf and gf positioning.
1402 BOOL detect_transition_to_still(
1406 double loop_decay_rate,
1407 double decay_accumulator )
1409 BOOL trans_to_still = FALSE;
1411 // Break clause to detect very still sections after motion
1412 // For example a static image after a fade or other transition
1413 // instead of a clean scene cut.
1414 if ( (frame_interval > MIN_GF_INTERVAL) &&
1415 (loop_decay_rate >= 0.999) &&
1416 (decay_accumulator < 0.9) )
1419 FIRSTPASS_STATS * position = cpi->stats_in;
1420 FIRSTPASS_STATS tmp_next_frame;
1423 // Look ahead a few frames to see if static condition
1425 for ( j = 0; j < still_interval; j++ )
1427 if (EOF == vp8_input_stats(cpi, &tmp_next_frame))
1430 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1431 if ( decay_rate < 0.999 )
1434 // Reset file position
1435 reset_fpf_position(cpi, position);
1437 // Only if it does do we signal a transition to still
1438 if ( j == still_interval )
1439 trans_to_still = TRUE;
1442 return trans_to_still;
1445 // Analyse and define a gf/arf group .
1446 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1448 FIRSTPASS_STATS next_frame;
1449 FIRSTPASS_STATS *start_pos;
1451 int y_width = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_width;
1452 int y_height = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_height;
1453 int image_size = y_width * y_height;
1454 double boost_score = 0.0;
1455 double old_boost_score = 0.0;
1456 double gf_group_err = 0.0;
1457 double gf_first_frame_err = 0.0;
1458 double mod_frame_err = 0.0;
1460 double mv_accumulator_rabs = 0.0;
1461 double mv_accumulator_cabs = 0.0;
1462 double mv_ratio_accumulator = 0.0;
1463 double decay_accumulator = 1.0;
1465 double boost_factor = IIFACTOR;
1466 double loop_decay_rate = 1.00; // Starting decay rate
1468 double this_frame_mv_in_out = 0.0;
1469 double mv_in_out_accumulator = 0.0;
1470 double abs_mv_in_out_accumulator = 0.0;
1471 double mod_err_per_mb_accumulator = 0.0;
1473 int max_bits = frame_max_bits(cpi); // Max for a single frame
1475 unsigned char *fpmm_pos;
1477 unsigned int allow_alt_ref =
1478 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1480 cpi->gf_group_bits = 0;
1481 cpi->gf_decay_rate = 0;
1483 vp8_clear_system_state(); //__asm emms;
1485 fpmm_pos = vp8_fpmm_get_pos(cpi);
1487 start_pos = cpi->stats_in;
1489 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1491 // Preload the stats for the next frame.
1492 mod_frame_err = calculate_modified_err(cpi, this_frame);
1494 // Note the error of the frame at the start of the group (this will be
1495 // the GF frame error if we code a normal gf
1496 gf_first_frame_err = mod_frame_err;
1498 // Special treatment if the current frame is a key frame (which is also
1499 // a gf). If it is then its error score (and hence bit allocation) need
1500 // to be subtracted out from the calculation for the GF group
1501 if (cpi->common.frame_type == KEY_FRAME)
1502 gf_group_err -= gf_first_frame_err;
1504 // Scan forward to try and work out how many frames the next gf group
1505 // should contain and what level of boost is appropriate for the GF
1506 // or ARF that will be coded with the group
1509 while (((i < cpi->static_scene_max_gf_interval) ||
1510 ((cpi->frames_to_key - i) < MIN_GF_INTERVAL)) &&
1511 (i < cpi->frames_to_key))
1514 double this_frame_mvr_ratio;
1515 double this_frame_mvc_ratio;
1516 double motion_decay;
1517 //double motion_pct = next_frame.pcnt_motion;
1520 i++; // Increment the loop counter
1522 // Accumulate error score of frames in this gf group
1523 mod_frame_err = calculate_modified_err(cpi, this_frame);
1525 gf_group_err += mod_frame_err;
1527 mod_err_per_mb_accumulator +=
1528 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1530 if (EOF == vp8_input_stats(cpi, &next_frame))
1533 // Accumulate motion stats.
1534 motion_pct = next_frame.pcnt_motion;
1535 mv_accumulator_rabs += fabs(next_frame.mvr_abs * motion_pct);
1536 mv_accumulator_cabs += fabs(next_frame.mvc_abs * motion_pct);
1538 //Accumulate Motion In/Out of frame stats
1539 this_frame_mv_in_out =
1540 next_frame.mv_in_out_count * motion_pct;
1541 mv_in_out_accumulator +=
1542 next_frame.mv_in_out_count * motion_pct;
1543 abs_mv_in_out_accumulator +=
1544 fabs(next_frame.mv_in_out_count * motion_pct);
1546 // If there is a significant amount of motion
1547 if (motion_pct > 0.05)
1549 this_frame_mvr_ratio = fabs(next_frame.mvr_abs) /
1550 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVr));
1552 this_frame_mvc_ratio = fabs(next_frame.mvc_abs) /
1553 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVc));
1555 mv_ratio_accumulator +=
1556 (this_frame_mvr_ratio < next_frame.mvr_abs)
1557 ? (this_frame_mvr_ratio * motion_pct)
1558 : next_frame.mvr_abs * motion_pct;
1560 mv_ratio_accumulator +=
1561 (this_frame_mvc_ratio < next_frame.mvc_abs)
1562 ? (this_frame_mvc_ratio * motion_pct)
1563 : next_frame.mvc_abs * motion_pct;
1567 mv_ratio_accumulator += 0.0;
1568 this_frame_mvr_ratio = 1.0;
1569 this_frame_mvc_ratio = 1.0;
1572 // Underlying boost factor is based on inter intra error ratio
1573 r = ( boost_factor *
1574 ( next_frame.intra_error /
1575 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)));
1577 if (next_frame.intra_error > cpi->gf_intra_err_min)
1578 r = (IIKFACTOR2 * next_frame.intra_error /
1579 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1581 r = (IIKFACTOR2 * cpi->gf_intra_err_min /
1582 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1584 // Increase boost for frames where new data coming into frame
1585 // (eg zoom out). Slightly reduce boost if there is a net balance
1586 // of motion out of the frame (zoom in).
1587 // The range for this_frame_mv_in_out is -1.0 to +1.0
1588 if (this_frame_mv_in_out > 0.0)
1589 r += r * (this_frame_mv_in_out * 2.0);
1590 // In extreme case boost is halved
1592 r += r * (this_frame_mv_in_out / 2.0);
1597 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1599 // Cumulative effect of decay
1600 decay_accumulator = decay_accumulator * loop_decay_rate;
1601 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1603 boost_score += (decay_accumulator * r);
1605 // Break clause to detect very still sections after motion
1606 // For example a staic image after a fade or other transition.
1607 if ( detect_transition_to_still( cpi, i, 5,
1608 loop_decay_rate, decay_accumulator ) )
1610 allow_alt_ref = FALSE;
1611 boost_score = old_boost_score;
1615 // Break out conditions.
1617 // Break at cpi->max_gf_interval unless almost totally static
1618 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1620 // Dont break out with a very short interval
1621 (i > MIN_GF_INTERVAL) &&
1622 // Dont break out very close to a key frame
1623 ((cpi->frames_to_key - i) >= MIN_GF_INTERVAL) &&
1624 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1625 ((mv_ratio_accumulator > 100.0) ||
1626 (abs_mv_in_out_accumulator > 3.0) ||
1627 (mv_in_out_accumulator < -2.0) ||
1628 ((boost_score - old_boost_score) < 2.0))
1631 boost_score = old_boost_score;
1635 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1637 old_boost_score = boost_score;
1640 cpi->gf_decay_rate =
1641 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1643 // When using CBR apply additional buffer related upper limits
1644 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1648 // For cbr apply buffer related limits
1649 if (cpi->drop_frames_allowed)
1651 int df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1652 (cpi->oxcf.optimal_buffer_level / 100);
1654 if (cpi->buffer_level > df_buffer_level)
1655 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1659 else if (cpi->buffer_level > 0)
1661 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1668 if (boost_score > max_boost)
1669 boost_score = max_boost;
1672 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1674 // Should we use the alternate refernce frame
1675 if (allow_alt_ref &&
1676 (i >= MIN_GF_INTERVAL) &&
1677 // dont use ARF very near next kf
1678 (i <= (cpi->frames_to_key - MIN_GF_INTERVAL)) &&
1679 (((next_frame.pcnt_inter > 0.75) &&
1680 ((mv_in_out_accumulator / (double)i > -0.2) || (mv_in_out_accumulator > -2.0)) &&
1681 //(cpi->gfu_boost>150) &&
1682 (cpi->gfu_boost > 100) &&
1683 //(cpi->gfu_boost>AF_THRESH2) &&
1684 //((cpi->gfu_boost/i)>AF_THRESH) &&
1685 //(decay_accumulator > 0.5) &&
1686 (cpi->gf_decay_rate <= (ARF_DECAY_THRESH + (cpi->gfu_boost / 200)))
1692 int allocation_chunks;
1693 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1695 int arf_frame_bits = 0;
1698 // Estimate the bits to be allocated to the group as a whole
1699 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1700 group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1704 // Boost for arf frame
1705 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1707 allocation_chunks = (i * 100) + Boost;
1709 // Normalize Altboost and allocations chunck down to prevent overflow
1710 while (Boost > 1000)
1713 allocation_chunks /= 2;
1716 // Calculate the number of bits to be spent on the arf based on the boost number
1717 arf_frame_bits = (int)((double)Boost * (group_bits / (double)allocation_chunks));
1719 // Estimate if there are enough bits available to make worthwhile use of an arf.
1720 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits, cpi->common.Height, cpi->common.Width);
1722 // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames.
1723 if (tmp_q < cpi->worst_quality)
1726 int frames_after_arf;
1727 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1728 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1730 cpi->source_alt_ref_pending = TRUE;
1732 // 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
1733 // 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)
1734 gf_group_err -= mod_frame_err;
1736 // 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.
1737 // The future frame itself is part of the next group
1738 cpi->baseline_gf_interval = i - 1;
1740 // Define the arnr filter width for this group of frames:
1741 // We only filter frames that lie within a distance of half
1742 // the GF interval from the ARF frame. We also have to trap
1743 // cases where the filter extends beyond the end of clip.
1744 // Note: this_frame->frame has been updated in the loop
1745 // so it now points at the ARF frame.
1746 half_gf_int = cpi->baseline_gf_interval >> 1;
1747 frames_after_arf = cpi->total_stats->count - this_frame->frame - 1;
1749 switch (cpi->oxcf.arnr_type)
1751 case 1: // Backward filter
1753 if (frames_bwd > half_gf_int)
1754 frames_bwd = half_gf_int;
1757 case 2: // Forward filter
1758 if (frames_fwd > half_gf_int)
1759 frames_fwd = half_gf_int;
1760 if (frames_fwd > frames_after_arf)
1761 frames_fwd = frames_after_arf;
1765 case 3: // Centered filter
1768 if (frames_fwd > frames_after_arf)
1769 frames_fwd = frames_after_arf;
1770 if (frames_fwd > half_gf_int)
1771 frames_fwd = half_gf_int;
1773 frames_bwd = frames_fwd;
1775 // For even length filter there is one more frame backward
1776 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1777 if (frames_bwd < half_gf_int)
1778 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
1782 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1785 // Advance to & read in the motion map for those frames
1786 // to be considered for filtering based on the position
1788 vp8_fpmm_reset_pos(cpi, cpi->fp_motion_map_stats_save);
1790 // Position at the 'earliest' frame to be filtered
1791 vp8_advance_fpmm(cpi,
1792 cpi->baseline_gf_interval - frames_bwd);
1794 // Read / create a motion map for the region of interest
1795 vp8_input_fpmm(cpi);
1800 cpi->source_alt_ref_pending = FALSE;
1801 cpi->baseline_gf_interval = i;
1806 cpi->source_alt_ref_pending = FALSE;
1807 cpi->baseline_gf_interval = i;
1811 if (!cpi->source_alt_ref_pending)
1813 // Dont allow conventional gf too near the next kf
1814 if ((cpi->frames_to_key - cpi->baseline_gf_interval) < MIN_GF_INTERVAL)
1816 while (cpi->baseline_gf_interval < cpi->frames_to_key)
1818 if (EOF == vp8_input_stats(cpi, this_frame))
1821 cpi->baseline_gf_interval++;
1823 if (cpi->baseline_gf_interval < cpi->frames_to_key)
1824 gf_group_err += calculate_modified_err(cpi, this_frame);
1829 // Now decide how many bits should be allocated to the GF group as a proportion of those remaining in the kf group.
1830 // 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.
1831 // This is also important for short clips where there may only be one key frame.
1832 if (cpi->frames_to_key >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
1834 cpi->kf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;
1837 // Calculate the bits to be allocated to the group as a whole
1838 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1839 cpi->gf_group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1841 cpi->gf_group_bits = 0;
1843 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;
1845 // Clip cpi->gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section)
1846 if (cpi->gf_group_bits > max_bits * cpi->baseline_gf_interval)
1847 cpi->gf_group_bits = max_bits * cpi->baseline_gf_interval;
1849 // Reset the file position
1850 reset_fpf_position(cpi, start_pos);
1852 // Update the record of error used so far (only done once per gf group)
1853 cpi->modified_error_used += gf_group_err;
1855 // Assign bits to the arf or gf.
1858 int frames_in_section;
1859 int allocation_chunks;
1860 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1863 if (cpi->source_alt_ref_pending)
1865 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1866 //Boost += (cpi->baseline_gf_interval * 25);
1867 Boost += (cpi->baseline_gf_interval * 50);
1869 // Set max and minimum boost and hence minimum allocation
1870 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1871 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1872 else if (Boost < 125)
1875 frames_in_section = cpi->baseline_gf_interval + 1;
1876 allocation_chunks = (frames_in_section * 100) + Boost;
1878 // Else for standard golden frames
1881 // boost based on inter / intra ratio of subsequent frames
1882 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
1884 // Set max and minimum boost and hence minimum allocation
1885 if (Boost > (cpi->baseline_gf_interval * 150))
1886 Boost = (cpi->baseline_gf_interval * 150);
1887 else if (Boost < 125)
1890 frames_in_section = cpi->baseline_gf_interval;
1891 allocation_chunks = (frames_in_section * 100) + (Boost - 100);
1894 // Normalize Altboost and allocations chunck down to prevent overflow
1895 while (Boost > 1000)
1898 allocation_chunks /= 2;
1901 // Calculate the number of bits to be spent on the gf or arf based on the boost number
1902 cpi->gf_bits = (int)((double)Boost * (cpi->gf_group_bits / (double)allocation_chunks));
1904 // If the frame that is to be boosted is simpler than the average for
1905 // the gf/arf group then use an alternative calculation
1906 // based on the error score of the frame itself
1907 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
1909 double alt_gf_grp_bits;
1913 (double)cpi->kf_group_bits *
1914 (mod_frame_err * (double)cpi->baseline_gf_interval) /
1915 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left);
1917 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
1918 (double)allocation_chunks));
1920 if (cpi->gf_bits > alt_gf_bits)
1922 cpi->gf_bits = alt_gf_bits;
1925 // Else if it is harder than other frames in the group make sure it at
1926 // least receives an allocation in keeping with its relative error
1927 // score, otherwise it may be worse off than an "un-boosted" frame
1931 (int)((double)cpi->kf_group_bits *
1933 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left));
1935 if (alt_gf_bits > cpi->gf_bits)
1937 cpi->gf_bits = alt_gf_bits;
1941 // Apply an additional limit for CBR
1942 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1944 if (cpi->gf_bits > (cpi->buffer_level >> 1))
1945 cpi->gf_bits = cpi->buffer_level >> 1;
1948 // Dont allow a negative value for gf_bits
1949 if (cpi->gf_bits < 0)
1952 // Adjust KF group bits and error remainin
1953 cpi->kf_group_error_left -= gf_group_err;
1954 cpi->kf_group_bits -= cpi->gf_group_bits;
1956 if (cpi->kf_group_bits < 0)
1957 cpi->kf_group_bits = 0;
1959 // Note the error score left in the remaining frames of the group.
1960 // 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)
1961 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
1962 cpi->gf_group_error_left = gf_group_err - gf_first_frame_err;
1964 cpi->gf_group_error_left = gf_group_err;
1966 cpi->gf_group_bits -= cpi->gf_bits;
1968 if (cpi->gf_group_bits < 0)
1969 cpi->gf_group_bits = 0;
1971 // Set aside some bits for a mid gf sequence boost
1972 if ((cpi->gfu_boost > 150) && (cpi->baseline_gf_interval > 5))
1974 int pct_extra = (cpi->gfu_boost - 100) / 50;
1975 pct_extra = (pct_extra > 10) ? 10 : pct_extra;
1977 cpi->mid_gf_extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
1978 cpi->gf_group_bits -= cpi->mid_gf_extra_bits;
1981 cpi->mid_gf_extra_bits = 0;
1983 cpi->gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame
1986 if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) // Normal GF and not a KF
1988 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for this frame
1991 // Adjustment to estimate_max_q based on a measure of complexity of the section
1992 if (cpi->common.frame_type != KEY_FRAME)
1994 FIRSTPASS_STATS sectionstats;
1997 vp8_zero_stats(§ionstats);
1998 reset_fpf_position(cpi, start_pos);
2000 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
2002 vp8_input_stats(cpi, &next_frame);
2003 vp8_accumulate_stats(§ionstats, &next_frame);
2006 vp8_avg_stats(§ionstats);
2008 cpi->section_intra_rating =
2009 sectionstats.intra_error /
2010 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2012 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2013 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2015 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2017 if (cpi->section_max_qfactor < 0.80)
2018 cpi->section_max_qfactor = 0.80;
2022 // cpi->section_max_qfactor = 1.0;
2024 reset_fpf_position(cpi, start_pos);
2027 // Reset the First pass motion map file position
2028 vp8_fpmm_reset_pos(cpi, fpmm_pos);
2031 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2032 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2034 int target_frame_size; // gf_group_error_left
2036 double modified_err;
2037 double err_fraction; // What portion of the remaining GF group error is used by this frame
2039 int max_bits = frame_max_bits(cpi); // Max for a single frame
2041 // The final few frames have special treatment
2042 if (cpi->frames_till_gf_update_due >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
2044 cpi->gf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;;
2047 // Calculate modified prediction error used in bit allocation
2048 modified_err = calculate_modified_err(cpi, this_frame);
2050 if (cpi->gf_group_error_left > 0)
2051 err_fraction = modified_err / cpi->gf_group_error_left; // What portion of the remaining GF group error is used by this frame
2055 target_frame_size = (int)((double)cpi->gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it?
2057 // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top end.
2058 if (target_frame_size < 0)
2059 target_frame_size = 0;
2062 if (target_frame_size > max_bits)
2063 target_frame_size = max_bits;
2065 if (target_frame_size > cpi->gf_group_bits)
2066 target_frame_size = cpi->gf_group_bits;
2069 cpi->gf_group_error_left -= modified_err; // Adjust error remaining
2070 cpi->gf_group_bits -= target_frame_size; // Adjust bits remaining
2072 if (cpi->gf_group_bits < 0)
2073 cpi->gf_group_bits = 0;
2075 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame.
2077 // Special case for the frame that lies half way between two gfs
2078 if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2)
2079 target_frame_size += cpi->mid_gf_extra_bits;
2081 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
2084 void vp8_second_pass(VP8_COMP *cpi)
2087 int frames_left = (int)(cpi->total_stats->count - cpi->common.current_video_frame);
2089 FIRSTPASS_STATS this_frame;
2090 FIRSTPASS_STATS this_frame_copy;
2092 VP8_COMMON *cm = &cpi->common;
2094 double this_frame_error;
2095 double this_frame_intra_error;
2096 double this_frame_coded_error;
2098 FIRSTPASS_STATS *start_pos;
2105 vp8_clear_system_state();
2107 if (EOF == vp8_input_stats(cpi, &this_frame))
2110 vpx_memset(cpi->fp_motion_map, 0,
2111 cpi->oxcf.arnr_max_frames*cpi->common.MBs);
2112 cpi->fp_motion_map_stats_save = vp8_fpmm_get_pos(cpi);
2114 // Step over this frame's first pass motion map
2115 vp8_advance_fpmm(cpi, 1);
2117 this_frame_error = this_frame.ssim_weighted_pred_err;
2118 this_frame_intra_error = this_frame.intra_error;
2119 this_frame_coded_error = this_frame.coded_error;
2121 // Store information regarding level of motion etc for use mode decisions.
2122 cpi->motion_speed = (int)(fabs(this_frame.MVr) + fabs(this_frame.MVc));
2123 cpi->motion_var = (int)(fabs(this_frame.MVrv) + fabs(this_frame.MVcv));
2124 cpi->inter_lvl = (int)(this_frame.pcnt_inter * 100);
2125 cpi->intra_lvl = (int)((1.0 - this_frame.pcnt_inter) * 100);
2126 cpi->motion_lvl = (int)(this_frame.pcnt_motion * 100);
2128 start_pos = cpi->stats_in;
2130 // keyframe and section processing !
2131 if (cpi->frames_to_key == 0)
2133 // Define next KF group and assign bits to it
2134 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2135 vp8_find_next_key_frame(cpi, &this_frame_copy);
2137 // 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
2138 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2139 // This is temporary code till we decide what should really happen in this case.
2140 if (cpi->oxcf.error_resilient_mode)
2142 cpi->gf_group_bits = cpi->kf_group_bits;
2143 cpi->gf_group_error_left = cpi->kf_group_error_left;
2144 cpi->baseline_gf_interval = cpi->frames_to_key;
2145 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2146 cpi->source_alt_ref_pending = FALSE;
2151 // Is this a GF / ARF (Note that a KF is always also a GF)
2152 if (cpi->frames_till_gf_update_due == 0)
2154 // Update monitor of the bits per error observed so far.
2155 // Done once per gf group based on what has gone before
2156 // so do nothing if this is the first frame.
2157 if (cpi->common.current_video_frame > 0)
2160 (double)(cpi->clip_bits_total - cpi->bits_left) /
2161 cpi->modified_error_used;
2164 // Define next gf group and assign bits to it
2165 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2166 define_gf_group(cpi, &this_frame_copy);
2168 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
2169 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
2170 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
2171 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2173 // Assign a standard frames worth of bits from those allocated to the GF group
2174 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2175 assign_std_frame_bits(cpi, &this_frame_copy);
2177 // 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.
2178 //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
2182 int pct_extra = (cpi->gfu_boost - 100) / 50;
2184 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2186 extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
2187 cpi->gf_group_bits -= extra_bits;
2188 cpi->per_frame_bandwidth += extra_bits;
2193 // Otherwise this is an ordinary frame
2196 // 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
2197 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2198 // This is temporary code till we decide what should really happen in this case.
2199 if (cpi->oxcf.error_resilient_mode)
2201 cpi->frames_till_gf_update_due = cpi->frames_to_key;
2203 if (cpi->common.frame_type != KEY_FRAME)
2205 // Assign bits from those allocated to the GF group
2206 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2207 assign_std_frame_bits(cpi, &this_frame_copy);
2212 // Assign bits from those allocated to the GF group
2213 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2214 assign_std_frame_bits(cpi, &this_frame_copy);
2218 // Keep a globally available copy of this and the next frame's iiratio.
2219 cpi->this_iiratio = this_frame_intra_error /
2220 DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
2222 FIRSTPASS_STATS next_frame;
2223 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2225 cpi->next_iiratio = next_frame.intra_error /
2226 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
2230 // Set nominal per second bandwidth for this frame
2231 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
2232 if (cpi->target_bandwidth < 0)
2233 cpi->target_bandwidth = 0;
2235 if (cpi->common.current_video_frame == 0)
2237 cpi->est_max_qcorrection_factor = 1.0;
2239 // Experimental code to try and set a cq_level in constrained
2241 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2247 (cpi->total_coded_error_left / frames_left),
2248 (int)(cpi->bits_left / frames_left),
2249 cpi->common.Height, cpi->common.Width);
2251 cpi->cq_target_quality = cpi->oxcf.cq_level;
2252 if ( est_cq > cpi->cq_target_quality )
2253 cpi->cq_target_quality = est_cq;
2256 // guess at maxq needed in 2nd pass
2257 cpi->maxq_max_limit = cpi->worst_quality;
2258 cpi->maxq_min_limit = cpi->best_quality;
2259 tmp_q = estimate_max_q( cpi,
2260 (cpi->total_coded_error_left / frames_left),
2261 (int)(cpi->bits_left / frames_left),
2265 // Limit the maxq value returned subsequently.
2266 // This increases the risk of overspend or underspend if the initial
2267 // estimate for the clip is bad, but helps prevent excessive
2268 // variation in Q, especially near the end of a clip
2269 // where for example a small overspend may cause Q to crash
2270 cpi->maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2271 ? (tmp_q + 32) : cpi->worst_quality;
2272 cpi->maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2273 ? (tmp_q - 32) : cpi->best_quality;
2275 cpi->active_worst_quality = tmp_q;
2276 cpi->ni_av_qi = tmp_q;
2279 // The last few frames of a clip almost always have to few or too many
2280 // bits and for the sake of over exact rate control we dont want to make
2281 // radical adjustments to the allowed quantizer range just to use up a
2282 // few surplus bits or get beneath the target rate.
2283 else if ( (cpi->common.current_video_frame <
2284 (((unsigned int)cpi->total_stats->count * 255)>>8)) &&
2285 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2286 (unsigned int)cpi->total_stats->count) )
2288 if (frames_left < 1)
2291 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);
2293 // Move active_worst_quality but in a damped way
2294 if (tmp_q > cpi->active_worst_quality)
2295 cpi->active_worst_quality ++;
2296 else if (tmp_q < cpi->active_worst_quality)
2297 cpi->active_worst_quality --;
2299 cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2302 cpi->frames_to_key --;
2303 cpi->total_error_left -= this_frame_error;
2304 cpi->total_intra_error_left -= this_frame_intra_error;
2305 cpi->total_coded_error_left -= this_frame_coded_error;
2309 static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2311 BOOL is_viable_kf = FALSE;
2313 // Does the frame satisfy the primary criteria of a key frame
2314 // If so, then examine how well it predicts subsequent frames
2315 if ((this_frame->pcnt_second_ref < 0.10) &&
2316 (next_frame->pcnt_second_ref < 0.10) &&
2317 ((this_frame->pcnt_inter < 0.05) ||
2319 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2320 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2321 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2322 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2323 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2330 FIRSTPASS_STATS *start_pos;
2332 FIRSTPASS_STATS local_next_frame;
2334 double boost_score = 0.0;
2335 double old_boost_score = 0.0;
2336 double decay_accumulator = 1.0;
2337 double next_iiratio;
2339 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2341 // Note the starting file position so we can reset to it
2342 start_pos = cpi->stats_in;
2344 // Examine how well the key frame predicts subsequent frames
2345 for (i = 0 ; i < 16; i++)
2347 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
2349 if (next_iiratio > RMAX)
2350 next_iiratio = RMAX;
2352 // Cumulative effect of decay in prediction quality
2353 if (local_next_frame.pcnt_inter > 0.85)
2354 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2356 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2358 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2360 // Keep a running total
2361 boost_score += (decay_accumulator * next_iiratio);
2363 // Test various breakout clauses
2364 if ((local_next_frame.pcnt_inter < 0.05) ||
2365 (next_iiratio < 1.5) ||
2366 (((local_next_frame.pcnt_inter -
2367 local_next_frame.pcnt_neutral) < 0.20) &&
2368 (next_iiratio < 3.0)) ||
2369 ((boost_score - old_boost_score) < 0.5) ||
2370 (local_next_frame.intra_error < 200)
2376 old_boost_score = boost_score;
2378 // Get the next frame details
2379 if (EOF == vp8_input_stats(cpi, &local_next_frame))
2383 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2384 if (boost_score > 5.0 && (i > 3))
2385 is_viable_kf = TRUE;
2388 // Reset the file position
2389 reset_fpf_position(cpi, start_pos);
2391 is_viable_kf = FALSE;
2395 return is_viable_kf;
2397 void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2400 FIRSTPASS_STATS last_frame;
2401 FIRSTPASS_STATS first_frame;
2402 FIRSTPASS_STATS next_frame;
2403 FIRSTPASS_STATS *start_position;
2405 double decay_accumulator = 1.0;
2406 double boost_score = 0;
2407 double old_boost_score = 0.0;
2408 double loop_decay_rate;
2410 double kf_mod_err = 0.0;
2411 double kf_group_err = 0.0;
2412 double kf_group_intra_err = 0.0;
2413 double kf_group_coded_err = 0.0;
2414 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2415 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2417 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2419 vp8_clear_system_state(); //__asm emms;
2420 start_position = cpi->stats_in;
2422 cpi->common.frame_type = KEY_FRAME;
2424 // is this a forced key frame by interval
2425 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2427 // Clear the alt ref active flag as this can never be active on a key frame
2428 cpi->source_alt_ref_active = FALSE;
2430 // Kf is always a gf so clear frames till next gf counter
2431 cpi->frames_till_gf_update_due = 0;
2433 cpi->frames_to_key = 1;
2435 // Take a copy of the initial frame details
2436 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2438 cpi->kf_group_bits = 0; // Total bits avaialable to kf group
2439 cpi->kf_group_error_left = 0; // Group modified error score.
2441 kf_mod_err = calculate_modified_err(cpi, this_frame);
2443 // find the next keyframe
2445 while (cpi->stats_in < cpi->stats_in_end)
2447 // Accumulate kf group error
2448 kf_group_err += calculate_modified_err(cpi, this_frame);
2450 // These figures keep intra and coded error counts for all frames including key frames in the group.
2451 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2452 kf_group_intra_err += this_frame->intra_error;
2453 kf_group_coded_err += this_frame->coded_error;
2455 // load a the next frame's stats
2456 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2457 vp8_input_stats(cpi, this_frame);
2459 // Provided that we are not at the end of the file...
2460 if (cpi->oxcf.auto_key
2461 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2463 // Normal scene cut check
2464 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
2467 // How fast is prediction quality decaying
2468 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2470 // We want to know something about the recent past... rather than
2471 // as used elsewhere where we are concened with decay in prediction
2472 // quality since the last GF or KF.
2473 recent_loop_decay[i%8] = loop_decay_rate;
2474 decay_accumulator = 1.0;
2475 for (j = 0; j < 8; j++)
2477 decay_accumulator = decay_accumulator * recent_loop_decay[j];
2480 // Special check for transition or high motion followed by a
2481 // to a static scene.
2482 if ( detect_transition_to_still( cpi, i,
2483 (cpi->key_frame_frequency-i),
2485 decay_accumulator ) )
2491 // Step on to the next frame
2492 cpi->frames_to_key ++;
2494 // If we don't have a real key frame within the next two
2495 // forcekeyframeevery intervals then break out of the loop.
2496 if (cpi->frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2499 cpi->frames_to_key ++;
2504 // If there is a max kf interval set by the user we must obey it.
2505 // We already breakout of the loop above at 2x max.
2506 // This code centers the extra kf if the actual natural
2507 // interval is between 1x and 2x
2508 if (cpi->oxcf.auto_key
2509 && cpi->frames_to_key > (int)cpi->key_frame_frequency )
2511 FIRSTPASS_STATS *current_pos = cpi->stats_in;
2512 FIRSTPASS_STATS tmp_frame;
2514 cpi->frames_to_key /= 2;
2516 // Copy first frame details
2517 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2519 // Reset to the start of the group
2520 reset_fpf_position(cpi, start_position);
2523 kf_group_intra_err = 0;
2524 kf_group_coded_err = 0;
2526 // Rescan to get the correct error data for the forced kf group
2527 for( i = 0; i < cpi->frames_to_key; i++ )
2529 // Accumulate kf group errors
2530 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2531 kf_group_intra_err += tmp_frame.intra_error;
2532 kf_group_coded_err += tmp_frame.coded_error;
2534 // Load a the next frame's stats
2535 vp8_input_stats(cpi, &tmp_frame);
2538 // Reset to the start of the group
2539 reset_fpf_position(cpi, current_pos);
2541 cpi->next_key_frame_forced = TRUE;
2544 cpi->next_key_frame_forced = FALSE;
2546 // Special case for the last frame of the file
2547 if (cpi->stats_in >= cpi->stats_in_end)
2549 // Accumulate kf group error
2550 kf_group_err += calculate_modified_err(cpi, this_frame);
2552 // These figures keep intra and coded error counts for all frames including key frames in the group.
2553 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2554 kf_group_intra_err += this_frame->intra_error;
2555 kf_group_coded_err += this_frame->coded_error;
2558 // Calculate the number of bits that should be assigned to the kf group.
2559 if ((cpi->bits_left > 0) && (cpi->modified_error_left > 0.0))
2561 // Max for a single normal frame (not key frame)
2562 int max_bits = frame_max_bits(cpi);
2564 // Maximum bits for the kf group
2565 long long max_grp_bits;
2567 // Default allocation based on bits left and relative
2568 // complexity of the section
2569 cpi->kf_group_bits = (long long)( cpi->bits_left *
2571 cpi->modified_error_left ));
2573 // Clip based on maximum per frame rate defined by the user.
2574 max_grp_bits = (long long)max_bits * (long long)cpi->frames_to_key;
2575 if (cpi->kf_group_bits > max_grp_bits)
2576 cpi->kf_group_bits = max_grp_bits;
2578 // Additional special case for CBR if buffer is getting full.
2579 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2581 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2582 int buffer_lvl = cpi->buffer_level;
2584 // If the buffer is near or above the optimal and this kf group is
2585 // not being allocated much then increase the allocation a bit.
2586 if (buffer_lvl >= opt_buffer_lvl)
2588 int high_water_mark = (opt_buffer_lvl +
2589 cpi->oxcf.maximum_buffer_size) >> 1;
2591 long long av_group_bits;
2593 // Av bits per frame * number of frames
2594 av_group_bits = (long long)cpi->av_per_frame_bandwidth *
2595 (long long)cpi->frames_to_key;
2597 // We are at or above the maximum.
2598 if (cpi->buffer_level >= high_water_mark)
2600 long long min_group_bits;
2602 min_group_bits = av_group_bits +
2603 (long long)(buffer_lvl -
2606 if (cpi->kf_group_bits < min_group_bits)
2607 cpi->kf_group_bits = min_group_bits;
2609 // We are above optimal but below the maximum
2610 else if (cpi->kf_group_bits < av_group_bits)
2612 long long bits_below_av = av_group_bits -
2615 cpi->kf_group_bits +=
2616 (long long)((double)bits_below_av *
2617 (double)(buffer_lvl - opt_buffer_lvl) /
2618 (double)(high_water_mark - opt_buffer_lvl));
2624 cpi->kf_group_bits = 0;
2626 // Reset the first pass file position
2627 reset_fpf_position(cpi, start_position);
2629 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2630 decay_accumulator = 1.0;
2632 loop_decay_rate = 1.00; // Starting decay rate
2634 for (i = 0 ; i < cpi->frames_to_key ; i++)
2637 double motion_decay;
2640 if (EOF == vp8_input_stats(cpi, &next_frame))
2643 if (next_frame.intra_error > cpi->kf_intra_err_min)
2644 r = (IIKFACTOR2 * next_frame.intra_error /
2645 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2647 r = (IIKFACTOR2 * cpi->kf_intra_err_min /
2648 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2653 // How fast is prediction quality decaying
2654 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2656 decay_accumulator = decay_accumulator * loop_decay_rate;
2657 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2659 boost_score += (decay_accumulator * r);
2661 if ((i > MIN_GF_INTERVAL) &&
2662 ((boost_score - old_boost_score) < 1.0))
2667 old_boost_score = boost_score;
2672 FIRSTPASS_STATS sectionstats;
2675 vp8_zero_stats(§ionstats);
2676 reset_fpf_position(cpi, start_position);
2678 for (i = 0 ; i < cpi->frames_to_key ; i++)
2680 vp8_input_stats(cpi, &next_frame);
2681 vp8_accumulate_stats(§ionstats, &next_frame);
2684 vp8_avg_stats(§ionstats);
2686 cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2688 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2689 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2691 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2693 if (cpi->section_max_qfactor < 0.80)
2694 cpi->section_max_qfactor = 0.80;
2698 // cpi->section_max_qfactor = 1.0;
2701 // When using CBR apply additional buffer fullness related upper limits
2702 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2706 if (cpi->drop_frames_allowed)
2708 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
2710 if (cpi->buffer_level > df_buffer_level)
2711 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2715 else if (cpi->buffer_level > 0)
2717 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2724 if (boost_score > max_boost)
2725 boost_score = max_boost;
2728 // Reset the first pass file position
2729 reset_fpf_position(cpi, start_position);
2731 // Work out how many bits to allocate for the key frame itself
2734 int kf_boost = boost_score;
2735 int allocation_chunks;
2736 int Counter = cpi->frames_to_key;
2738 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2739 // Min boost based on kf interval
2742 while ((kf_boost < 48) && (Counter > 0))
2752 kf_boost += ((Counter + 1) >> 1);
2754 if (kf_boost > 48) kf_boost = 48;
2757 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2758 if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
2759 kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2760 else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
2761 kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2763 kf_boost = (int)((double)kf_boost * 100.0) >> 4; // Scale 16 to 100
2765 // Adjustment to boost based on recent average q
2766 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2768 if (kf_boost < 250) // Min KF boost
2771 // We do three calculations for kf size.
2772 // The first is based on the error score for the whole kf group.
2773 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2774 // 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
2776 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
2778 // Normalize Altboost and allocations chunck down to prevent overflow
2779 while (kf_boost > 1000)
2782 allocation_chunks /= 2;
2785 cpi->kf_group_bits = (cpi->kf_group_bits < 0) ? 0 : cpi->kf_group_bits;
2787 // Calculate the number of bits to be spent on the key frame
2788 cpi->kf_bits = (int)((double)kf_boost * ((double)cpi->kf_group_bits / (double)allocation_chunks));
2790 // Apply an additional limit for CBR
2791 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2793 if (cpi->kf_bits > ((3 * cpi->buffer_level) >> 2))
2794 cpi->kf_bits = (3 * cpi->buffer_level) >> 2;
2797 // If the key frame is actually easier than the average for the
2798 // kf group (which does sometimes happen... eg a blank intro frame)
2799 // Then use an alternate calculation based on the kf error score
2800 // which should give a smaller key frame.
2801 if (kf_mod_err < kf_group_err / cpi->frames_to_key)
2803 double alt_kf_grp_bits =
2804 ((double)cpi->bits_left *
2805 (kf_mod_err * (double)cpi->frames_to_key) /
2806 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left));
2808 alt_kf_bits = (int)((double)kf_boost *
2809 (alt_kf_grp_bits / (double)allocation_chunks));
2811 if (cpi->kf_bits > alt_kf_bits)
2813 cpi->kf_bits = alt_kf_bits;
2816 // Else if it is much harder than other frames in the group make sure
2817 // it at least receives an allocation in keeping with its relative
2822 (int)((double)cpi->bits_left *
2824 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left)));
2826 if (alt_kf_bits > cpi->kf_bits)
2828 cpi->kf_bits = alt_kf_bits;
2832 cpi->kf_group_bits -= cpi->kf_bits;
2833 cpi->kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance
2835 cpi->per_frame_bandwidth = cpi->kf_bits; // Peer frame bit target for this frame
2836 cpi->target_bandwidth = cpi->kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate
2839 // Note the total error score of the kf group minus the key frame itself
2840 cpi->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2842 // Adjust the count of total modified error left.
2843 // The count of bits left is adjusted elsewhere based on real coded frame sizes
2844 cpi->modified_error_left -= kf_group_err;
2846 if (cpi->oxcf.allow_spatial_resampling)
2848 int resample_trigger = FALSE;
2849 int last_kf_resampled = FALSE;
2853 int new_width = cpi->oxcf.Width;
2854 int new_height = cpi->oxcf.Height;
2856 int projected_buffer_level = cpi->buffer_level;
2859 double projected_bits_perframe;
2860 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
2861 double err_per_frame = kf_group_err / cpi->frames_to_key;
2862 double bits_per_frame;
2863 double av_bits_per_frame;
2864 double effective_size_ratio;
2866 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
2867 last_kf_resampled = TRUE;
2869 // Set back to unscaled by defaults
2870 cpi->common.horiz_scale = NORMAL;
2871 cpi->common.vert_scale = NORMAL;
2873 // Calculate Average bits per frame.
2874 //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats->count - cpi->common.current_video_frame);
2875 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate);
2876 //if ( av_bits_per_frame < 0.0 )
2877 // av_bits_per_frame = 0.0
2879 // CBR... Use the clip average as the target for deciding resample
2880 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2882 bits_per_frame = av_bits_per_frame;
2885 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
2886 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
2889 bits_per_frame = cpi->kf_group_bits / cpi->frames_to_key; // This accounts for how hard the section is...
2891 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
2892 bits_per_frame = av_bits_per_frame;
2895 // bits_per_frame should comply with our minimum
2896 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
2897 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2899 // Work out if spatial resampling is necessary
2900 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, new_height, new_width, group_iiratio);
2902 // 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
2903 projected_bits_perframe = bits_per_frame;
2906 while (tmp_q > cpi->worst_quality)
2908 projected_bits_perframe *= 1.04;
2912 // Guess at buffer level at the end of the section
2913 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->frames_to_key);
2917 FILE *f = fopen("Subsamle.stt", "a");
2918 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);
2922 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
2923 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2925 // Trigger resample if we are projected to fall below down sample level or
2926 // resampled last time and are projected to remain below the up sample level
2927 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
2928 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
2929 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
2930 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
2931 resample_trigger = TRUE;
2933 resample_trigger = FALSE;
2937 long long clip_bits = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
2938 long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
2939 long long over_spend2 = cpi->oxcf.starting_buffer_level - projected_buffer_level;
2941 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced
2942 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ...
2943 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits
2944 resample_trigger = TRUE;
2946 resample_trigger = FALSE;
2950 if (resample_trigger)
2952 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
2956 cpi->common.vert_scale = vscale_lookup[scale_val];
2957 cpi->common.horiz_scale = hscale_lookup[scale_val];
2959 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
2960 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
2962 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2963 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2965 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
2966 // effective_sizeratio attempts to provide a crude correction for this
2967 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
2968 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
2970 // Now try again and see what Q we get with the smaller image size
2971 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, new_height, new_width, group_iiratio);
2975 FILE *f = fopen("Subsamle.stt", "a");
2976 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);
2982 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
2984 cpi->common.Width = new_width;
2985 cpi->common.Height = new_height;
2986 vp8_alloc_compressor_data(cpi);