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)
950 int num_mbs = cpi->common.MBs;
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)
1050 int num_mbs = cpi->common.MBs;
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, double group_iiratio)
1101 int num_mbs = cpi->common.MBs;
1102 int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1103 int bits_per_mb_at_this_q;
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, int section_target_bandwitdh)
1199 int num_mbs = cpi->common.MBs;
1200 int target_norm_bits_per_mb;
1202 double err_per_mb = section_err / num_mbs;
1203 double correction_factor;
1205 double speed_correction = 1.0;
1206 double pow_highq = 0.90;
1207 double pow_lowq = 0.40;
1208 double clip_iiratio;
1209 double clip_iifactor;
1211 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1212 ? (512 * section_target_bandwitdh) / num_mbs
1213 : 512 * (section_target_bandwitdh / num_mbs);
1215 // Corrections for higher compression speed settings
1216 // (reduced compression expected)
1217 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1219 if (cpi->oxcf.cpu_used <= 5)
1220 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1222 speed_correction = 1.25;
1224 // II ratio correction factor for clip as a whole
1225 clip_iiratio = cpi->total_stats->intra_error /
1226 DOUBLE_DIVIDE_CHECK(cpi->total_stats->coded_error);
1227 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1228 if (clip_iifactor < 0.80)
1229 clip_iifactor = 0.80;
1231 // Correction factor used for Q values >= 20
1232 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1233 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1235 // Try and pick a Q that can encode the content at the given rate.
1236 for (Q = 0; Q < MAXQ; Q++)
1238 int bits_per_mb_at_this_q;
1243 pow( err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1245 correction_factor = (correction_factor < 0.05) ? 0.05
1246 : (correction_factor > 5.0) ? 5.0
1247 : correction_factor;
1250 correction_factor = corr_high;
1252 bits_per_mb_at_this_q =
1253 (int)( .5 + correction_factor *
1256 (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1258 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1265 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
1267 void vp8_init_second_pass(VP8_COMP *cpi)
1269 FIRSTPASS_STATS this_frame;
1270 FIRSTPASS_STATS *start_pos;
1272 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1274 vp8_zero_stats(cpi->total_stats);
1276 if (!cpi->stats_in_end)
1279 *cpi->total_stats = *cpi->stats_in_end;
1281 cpi->total_error_left = cpi->total_stats->ssim_weighted_pred_err;
1282 cpi->total_intra_error_left = cpi->total_stats->intra_error;
1283 cpi->total_coded_error_left = cpi->total_stats->coded_error;
1284 cpi->start_tot_err_left = cpi->total_error_left;
1286 //cpi->bits_left = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1287 //cpi->bits_left -= (long long)(cpi->total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1289 // each frame can have a different duration, as the frame rate in the source
1290 // isn't guaranteed to be constant. The frame rate prior to the first frame
1291 // encoded in the second pass is a guess. However the sum duration is not.
1292 // Its calculated based on the actual durations of all frames from the first
1294 vp8_new_frame_rate(cpi, 10000000.0 * cpi->total_stats->count / cpi->total_stats->duration);
1296 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1297 cpi->bits_left = (long long)(cpi->total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1298 cpi->bits_left -= (long long)(cpi->total_stats->duration * two_pass_min_rate / 10000000.0);
1299 cpi->clip_bits_total = cpi->bits_left;
1301 // Calculate a minimum intra value to be used in determining the IIratio
1302 // scores used in the second pass. We have this minimum to make sure
1303 // that clips that are static but "low complexity" in the intra domain
1304 // are still boosted appropriately for KF/GF/ARF
1305 cpi->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1306 cpi->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1308 vp8_avg_stats(cpi->total_stats);
1310 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1312 double sum_iiratio = 0.0;
1315 start_pos = cpi->stats_in; // Note starting "file" position
1317 while (vp8_input_stats(cpi, &this_frame) != EOF)
1319 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1320 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1321 sum_iiratio += IIRatio;
1324 cpi->avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->total_stats->count);
1326 // Reset file position
1327 reset_fpf_position(cpi, start_pos);
1330 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1331 // used to allocate bits
1333 start_pos = cpi->stats_in; // Note starting "file" position
1335 cpi->modified_error_total = 0.0;
1336 cpi->modified_error_used = 0.0;
1338 while (vp8_input_stats(cpi, &this_frame) != EOF)
1340 cpi->modified_error_total += calculate_modified_err(cpi, &this_frame);
1342 cpi->modified_error_left = cpi->modified_error_total;
1344 reset_fpf_position(cpi, start_pos); // Reset file position
1348 // Calculate the clip target modified bits per error
1349 // The observed bpe starts as the same number.
1350 cpi->clip_bpe = cpi->bits_left /
1351 DOUBLE_DIVIDE_CHECK(cpi->modified_error_total);
1352 cpi->observed_bpe = cpi->clip_bpe;
1354 cpi->fp_motion_map_stats = (unsigned char *)cpi->stats_in;
1357 void vp8_end_second_pass(VP8_COMP *cpi)
1361 // This function gives and estimate of how badly we believe
1362 // the predicition quality is decaying from frame to frame.
1363 double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1365 double prediction_decay_rate;
1366 double motion_decay;
1367 double motion_pct = next_frame->pcnt_motion;
1370 // Initial basis is the % mbs inter coded
1371 prediction_decay_rate = next_frame->pcnt_inter;
1373 // High % motion -> somewhat higher decay rate
1374 motion_decay = (1.0 - (motion_pct / 20.0));
1375 if (motion_decay < prediction_decay_rate)
1376 prediction_decay_rate = motion_decay;
1378 // Adjustment to decay rate based on speed of motion
1380 double this_mv_rabs;
1381 double this_mv_cabs;
1382 double distance_factor;
1384 this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1385 this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1387 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
1388 (this_mv_cabs * this_mv_cabs)) / 250.0;
1389 distance_factor = ((distance_factor > 1.0)
1390 ? 0.0 : (1.0 - distance_factor));
1391 if (distance_factor < prediction_decay_rate)
1392 prediction_decay_rate = distance_factor;
1395 return prediction_decay_rate;
1398 // Function to test for a condition where a complex transition is followed
1399 // by a static section. For example in slide shows where there is a fade
1400 // between slides. This is to help with more optimal kf and gf positioning.
1401 BOOL detect_transition_to_still(
1405 double loop_decay_rate,
1406 double decay_accumulator )
1408 BOOL trans_to_still = FALSE;
1410 // Break clause to detect very still sections after motion
1411 // For example a static image after a fade or other transition
1412 // instead of a clean scene cut.
1413 if ( (frame_interval > MIN_GF_INTERVAL) &&
1414 (loop_decay_rate >= 0.999) &&
1415 (decay_accumulator < 0.9) )
1418 FIRSTPASS_STATS * position = cpi->stats_in;
1419 FIRSTPASS_STATS tmp_next_frame;
1422 // Look ahead a few frames to see if static condition
1424 for ( j = 0; j < still_interval; j++ )
1426 if (EOF == vp8_input_stats(cpi, &tmp_next_frame))
1429 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1430 if ( decay_rate < 0.999 )
1433 // Reset file position
1434 reset_fpf_position(cpi, position);
1436 // Only if it does do we signal a transition to still
1437 if ( j == still_interval )
1438 trans_to_still = TRUE;
1441 return trans_to_still;
1444 // Analyse and define a gf/arf group .
1445 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1447 FIRSTPASS_STATS next_frame;
1448 FIRSTPASS_STATS *start_pos;
1450 int y_width = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_width;
1451 int y_height = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_height;
1452 int image_size = y_width * y_height;
1453 double boost_score = 0.0;
1454 double old_boost_score = 0.0;
1455 double gf_group_err = 0.0;
1456 double gf_first_frame_err = 0.0;
1457 double mod_frame_err = 0.0;
1459 double mv_accumulator_rabs = 0.0;
1460 double mv_accumulator_cabs = 0.0;
1461 double mv_ratio_accumulator = 0.0;
1462 double decay_accumulator = 1.0;
1464 double boost_factor = IIFACTOR;
1465 double loop_decay_rate = 1.00; // Starting decay rate
1467 double this_frame_mv_in_out = 0.0;
1468 double mv_in_out_accumulator = 0.0;
1469 double abs_mv_in_out_accumulator = 0.0;
1470 double mod_err_per_mb_accumulator = 0.0;
1472 int max_bits = frame_max_bits(cpi); // Max for a single frame
1474 unsigned char *fpmm_pos;
1476 unsigned int allow_alt_ref =
1477 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1479 cpi->gf_group_bits = 0;
1480 cpi->gf_decay_rate = 0;
1482 vp8_clear_system_state(); //__asm emms;
1484 fpmm_pos = vp8_fpmm_get_pos(cpi);
1486 start_pos = cpi->stats_in;
1488 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1490 // Preload the stats for the next frame.
1491 mod_frame_err = calculate_modified_err(cpi, this_frame);
1493 // Note the error of the frame at the start of the group (this will be
1494 // the GF frame error if we code a normal gf
1495 gf_first_frame_err = mod_frame_err;
1497 // Special treatment if the current frame is a key frame (which is also
1498 // a gf). If it is then its error score (and hence bit allocation) need
1499 // to be subtracted out from the calculation for the GF group
1500 if (cpi->common.frame_type == KEY_FRAME)
1501 gf_group_err -= gf_first_frame_err;
1503 // Scan forward to try and work out how many frames the next gf group
1504 // should contain and what level of boost is appropriate for the GF
1505 // or ARF that will be coded with the group
1508 while (((i < cpi->static_scene_max_gf_interval) ||
1509 ((cpi->frames_to_key - i) < MIN_GF_INTERVAL)) &&
1510 (i < cpi->frames_to_key))
1513 double this_frame_mvr_ratio;
1514 double this_frame_mvc_ratio;
1515 double motion_decay;
1516 //double motion_pct = next_frame.pcnt_motion;
1519 i++; // Increment the loop counter
1521 // Accumulate error score of frames in this gf group
1522 mod_frame_err = calculate_modified_err(cpi, this_frame);
1524 gf_group_err += mod_frame_err;
1526 mod_err_per_mb_accumulator +=
1527 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1529 if (EOF == vp8_input_stats(cpi, &next_frame))
1532 // Accumulate motion stats.
1533 motion_pct = next_frame.pcnt_motion;
1534 mv_accumulator_rabs += fabs(next_frame.mvr_abs * motion_pct);
1535 mv_accumulator_cabs += fabs(next_frame.mvc_abs * motion_pct);
1537 //Accumulate Motion In/Out of frame stats
1538 this_frame_mv_in_out =
1539 next_frame.mv_in_out_count * motion_pct;
1540 mv_in_out_accumulator +=
1541 next_frame.mv_in_out_count * motion_pct;
1542 abs_mv_in_out_accumulator +=
1543 fabs(next_frame.mv_in_out_count * motion_pct);
1545 // If there is a significant amount of motion
1546 if (motion_pct > 0.05)
1548 this_frame_mvr_ratio = fabs(next_frame.mvr_abs) /
1549 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVr));
1551 this_frame_mvc_ratio = fabs(next_frame.mvc_abs) /
1552 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVc));
1554 mv_ratio_accumulator +=
1555 (this_frame_mvr_ratio < next_frame.mvr_abs)
1556 ? (this_frame_mvr_ratio * motion_pct)
1557 : next_frame.mvr_abs * motion_pct;
1559 mv_ratio_accumulator +=
1560 (this_frame_mvc_ratio < next_frame.mvc_abs)
1561 ? (this_frame_mvc_ratio * motion_pct)
1562 : next_frame.mvc_abs * motion_pct;
1566 mv_ratio_accumulator += 0.0;
1567 this_frame_mvr_ratio = 1.0;
1568 this_frame_mvc_ratio = 1.0;
1571 // Underlying boost factor is based on inter intra error ratio
1572 r = ( boost_factor *
1573 ( next_frame.intra_error /
1574 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)));
1576 if (next_frame.intra_error > cpi->gf_intra_err_min)
1577 r = (IIKFACTOR2 * next_frame.intra_error /
1578 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1580 r = (IIKFACTOR2 * cpi->gf_intra_err_min /
1581 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1583 // Increase boost for frames where new data coming into frame
1584 // (eg zoom out). Slightly reduce boost if there is a net balance
1585 // of motion out of the frame (zoom in).
1586 // The range for this_frame_mv_in_out is -1.0 to +1.0
1587 if (this_frame_mv_in_out > 0.0)
1588 r += r * (this_frame_mv_in_out * 2.0);
1589 // In extreme case boost is halved
1591 r += r * (this_frame_mv_in_out / 2.0);
1596 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1598 // Cumulative effect of decay
1599 decay_accumulator = decay_accumulator * loop_decay_rate;
1600 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1602 boost_score += (decay_accumulator * r);
1604 // Break clause to detect very still sections after motion
1605 // For example a staic image after a fade or other transition.
1606 if ( detect_transition_to_still( cpi, i, 5,
1607 loop_decay_rate, decay_accumulator ) )
1609 allow_alt_ref = FALSE;
1610 boost_score = old_boost_score;
1614 // Break out conditions.
1616 // Break at cpi->max_gf_interval unless almost totally static
1617 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1619 // Dont break out with a very short interval
1620 (i > MIN_GF_INTERVAL) &&
1621 // Dont break out very close to a key frame
1622 ((cpi->frames_to_key - i) >= MIN_GF_INTERVAL) &&
1623 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1624 ((mv_ratio_accumulator > 100.0) ||
1625 (abs_mv_in_out_accumulator > 3.0) ||
1626 (mv_in_out_accumulator < -2.0) ||
1627 ((boost_score - old_boost_score) < 2.0))
1630 boost_score = old_boost_score;
1634 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1636 old_boost_score = boost_score;
1639 cpi->gf_decay_rate =
1640 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1642 // When using CBR apply additional buffer related upper limits
1643 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1647 // For cbr apply buffer related limits
1648 if (cpi->drop_frames_allowed)
1650 int df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1651 (cpi->oxcf.optimal_buffer_level / 100);
1653 if (cpi->buffer_level > df_buffer_level)
1654 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1658 else if (cpi->buffer_level > 0)
1660 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1667 if (boost_score > max_boost)
1668 boost_score = max_boost;
1671 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1673 // Should we use the alternate refernce frame
1674 if (allow_alt_ref &&
1675 (i >= MIN_GF_INTERVAL) &&
1676 // dont use ARF very near next kf
1677 (i <= (cpi->frames_to_key - MIN_GF_INTERVAL)) &&
1678 (((next_frame.pcnt_inter > 0.75) &&
1679 ((mv_in_out_accumulator / (double)i > -0.2) || (mv_in_out_accumulator > -2.0)) &&
1680 //(cpi->gfu_boost>150) &&
1681 (cpi->gfu_boost > 100) &&
1682 //(cpi->gfu_boost>AF_THRESH2) &&
1683 //((cpi->gfu_boost/i)>AF_THRESH) &&
1684 //(decay_accumulator > 0.5) &&
1685 (cpi->gf_decay_rate <= (ARF_DECAY_THRESH + (cpi->gfu_boost / 200)))
1691 int allocation_chunks;
1692 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1694 int arf_frame_bits = 0;
1697 // Estimate the bits to be allocated to the group as a whole
1698 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1699 group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1703 // Boost for arf frame
1704 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1706 allocation_chunks = (i * 100) + Boost;
1708 // Normalize Altboost and allocations chunck down to prevent overflow
1709 while (Boost > 1000)
1712 allocation_chunks /= 2;
1715 // Calculate the number of bits to be spent on the arf based on the boost number
1716 arf_frame_bits = (int)((double)Boost * (group_bits / (double)allocation_chunks));
1718 // Estimate if there are enough bits available to make worthwhile use of an arf.
1719 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
1721 // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames.
1722 if (tmp_q < cpi->worst_quality)
1725 int frames_after_arf;
1726 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1727 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1729 cpi->source_alt_ref_pending = TRUE;
1731 // For alt ref frames the error score for the end frame of the group (the alt ref frame) should not contribute to the group total and hence
1732 // the number of bit allocated to the group. Rather it forms part of the next group (it is the GF at the start of the next group)
1733 gf_group_err -= mod_frame_err;
1735 // Set the interval till the next gf or arf. For ARFs this is the number of frames to be coded before the future frame that is coded as an ARF.
1736 // The future frame itself is part of the next group
1737 cpi->baseline_gf_interval = i - 1;
1739 // Define the arnr filter width for this group of frames:
1740 // We only filter frames that lie within a distance of half
1741 // the GF interval from the ARF frame. We also have to trap
1742 // cases where the filter extends beyond the end of clip.
1743 // Note: this_frame->frame has been updated in the loop
1744 // so it now points at the ARF frame.
1745 half_gf_int = cpi->baseline_gf_interval >> 1;
1746 frames_after_arf = cpi->total_stats->count - this_frame->frame - 1;
1748 switch (cpi->oxcf.arnr_type)
1750 case 1: // Backward filter
1752 if (frames_bwd > half_gf_int)
1753 frames_bwd = half_gf_int;
1756 case 2: // Forward filter
1757 if (frames_fwd > half_gf_int)
1758 frames_fwd = half_gf_int;
1759 if (frames_fwd > frames_after_arf)
1760 frames_fwd = frames_after_arf;
1764 case 3: // Centered filter
1767 if (frames_fwd > frames_after_arf)
1768 frames_fwd = frames_after_arf;
1769 if (frames_fwd > half_gf_int)
1770 frames_fwd = half_gf_int;
1772 frames_bwd = frames_fwd;
1774 // For even length filter there is one more frame backward
1775 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1776 if (frames_bwd < half_gf_int)
1777 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
1781 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1784 // Advance to & read in the motion map for those frames
1785 // to be considered for filtering based on the position
1787 vp8_fpmm_reset_pos(cpi, cpi->fp_motion_map_stats_save);
1789 // Position at the 'earliest' frame to be filtered
1790 vp8_advance_fpmm(cpi,
1791 cpi->baseline_gf_interval - frames_bwd);
1793 // Read / create a motion map for the region of interest
1794 vp8_input_fpmm(cpi);
1799 cpi->source_alt_ref_pending = FALSE;
1800 cpi->baseline_gf_interval = i;
1805 cpi->source_alt_ref_pending = FALSE;
1806 cpi->baseline_gf_interval = i;
1810 if (!cpi->source_alt_ref_pending)
1812 // Dont allow conventional gf too near the next kf
1813 if ((cpi->frames_to_key - cpi->baseline_gf_interval) < MIN_GF_INTERVAL)
1815 while (cpi->baseline_gf_interval < cpi->frames_to_key)
1817 if (EOF == vp8_input_stats(cpi, this_frame))
1820 cpi->baseline_gf_interval++;
1822 if (cpi->baseline_gf_interval < cpi->frames_to_key)
1823 gf_group_err += calculate_modified_err(cpi, this_frame);
1828 // Now decide how many bits should be allocated to the GF group as a proportion of those remaining in the kf group.
1829 // The final key frame group in the clip is treated as a special case where cpi->kf_group_bits is tied to cpi->bits_left.
1830 // This is also important for short clips where there may only be one key frame.
1831 if (cpi->frames_to_key >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
1833 cpi->kf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;
1836 // Calculate the bits to be allocated to the group as a whole
1837 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1838 cpi->gf_group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1840 cpi->gf_group_bits = 0;
1842 cpi->gf_group_bits = (cpi->gf_group_bits < 0) ? 0 : (cpi->gf_group_bits > cpi->kf_group_bits) ? cpi->kf_group_bits : cpi->gf_group_bits;
1844 // Clip cpi->gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section)
1845 if (cpi->gf_group_bits > max_bits * cpi->baseline_gf_interval)
1846 cpi->gf_group_bits = max_bits * cpi->baseline_gf_interval;
1848 // Reset the file position
1849 reset_fpf_position(cpi, start_pos);
1851 // Update the record of error used so far (only done once per gf group)
1852 cpi->modified_error_used += gf_group_err;
1854 // Assign bits to the arf or gf.
1857 int frames_in_section;
1858 int allocation_chunks;
1859 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1862 if (cpi->source_alt_ref_pending)
1864 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1865 //Boost += (cpi->baseline_gf_interval * 25);
1866 Boost += (cpi->baseline_gf_interval * 50);
1868 // Set max and minimum boost and hence minimum allocation
1869 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1870 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1871 else if (Boost < 125)
1874 frames_in_section = cpi->baseline_gf_interval + 1;
1875 allocation_chunks = (frames_in_section * 100) + Boost;
1877 // Else for standard golden frames
1880 // boost based on inter / intra ratio of subsequent frames
1881 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
1883 // Set max and minimum boost and hence minimum allocation
1884 if (Boost > (cpi->baseline_gf_interval * 150))
1885 Boost = (cpi->baseline_gf_interval * 150);
1886 else if (Boost < 125)
1889 frames_in_section = cpi->baseline_gf_interval;
1890 allocation_chunks = (frames_in_section * 100) + (Boost - 100);
1893 // Normalize Altboost and allocations chunck down to prevent overflow
1894 while (Boost > 1000)
1897 allocation_chunks /= 2;
1900 // Calculate the number of bits to be spent on the gf or arf based on the boost number
1901 cpi->gf_bits = (int)((double)Boost * (cpi->gf_group_bits / (double)allocation_chunks));
1903 // If the frame that is to be boosted is simpler than the average for
1904 // the gf/arf group then use an alternative calculation
1905 // based on the error score of the frame itself
1906 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
1908 double alt_gf_grp_bits;
1912 (double)cpi->kf_group_bits *
1913 (mod_frame_err * (double)cpi->baseline_gf_interval) /
1914 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left);
1916 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
1917 (double)allocation_chunks));
1919 if (cpi->gf_bits > alt_gf_bits)
1921 cpi->gf_bits = alt_gf_bits;
1924 // Else if it is harder than other frames in the group make sure it at
1925 // least receives an allocation in keeping with its relative error
1926 // score, otherwise it may be worse off than an "un-boosted" frame
1930 (int)((double)cpi->kf_group_bits *
1932 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left));
1934 if (alt_gf_bits > cpi->gf_bits)
1936 cpi->gf_bits = alt_gf_bits;
1940 // Apply an additional limit for CBR
1941 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1943 if (cpi->gf_bits > (cpi->buffer_level >> 1))
1944 cpi->gf_bits = cpi->buffer_level >> 1;
1947 // Dont allow a negative value for gf_bits
1948 if (cpi->gf_bits < 0)
1951 // Adjust KF group bits and error remainin
1952 cpi->kf_group_error_left -= gf_group_err;
1953 cpi->kf_group_bits -= cpi->gf_group_bits;
1955 if (cpi->kf_group_bits < 0)
1956 cpi->kf_group_bits = 0;
1958 // Note the error score left in the remaining frames of the group.
1959 // For normal GFs we want to remove the error score for the first frame of the group (except in Key frame case where this has already happened)
1960 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
1961 cpi->gf_group_error_left = gf_group_err - gf_first_frame_err;
1963 cpi->gf_group_error_left = gf_group_err;
1965 cpi->gf_group_bits -= cpi->gf_bits;
1967 if (cpi->gf_group_bits < 0)
1968 cpi->gf_group_bits = 0;
1970 // Set aside some bits for a mid gf sequence boost
1971 if ((cpi->gfu_boost > 150) && (cpi->baseline_gf_interval > 5))
1973 int pct_extra = (cpi->gfu_boost - 100) / 50;
1974 pct_extra = (pct_extra > 10) ? 10 : pct_extra;
1976 cpi->mid_gf_extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
1977 cpi->gf_group_bits -= cpi->mid_gf_extra_bits;
1980 cpi->mid_gf_extra_bits = 0;
1982 cpi->gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame
1985 if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) // Normal GF and not a KF
1987 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for this frame
1990 // Adjustment to estimate_max_q based on a measure of complexity of the section
1991 if (cpi->common.frame_type != KEY_FRAME)
1993 FIRSTPASS_STATS sectionstats;
1996 vp8_zero_stats(§ionstats);
1997 reset_fpf_position(cpi, start_pos);
1999 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
2001 vp8_input_stats(cpi, &next_frame);
2002 vp8_accumulate_stats(§ionstats, &next_frame);
2005 vp8_avg_stats(§ionstats);
2007 cpi->section_intra_rating =
2008 sectionstats.intra_error /
2009 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2011 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2012 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2014 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2016 if (cpi->section_max_qfactor < 0.80)
2017 cpi->section_max_qfactor = 0.80;
2021 // cpi->section_max_qfactor = 1.0;
2023 reset_fpf_position(cpi, start_pos);
2026 // Reset the First pass motion map file position
2027 vp8_fpmm_reset_pos(cpi, fpmm_pos);
2030 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2031 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2033 int target_frame_size; // gf_group_error_left
2035 double modified_err;
2036 double err_fraction; // What portion of the remaining GF group error is used by this frame
2038 int max_bits = frame_max_bits(cpi); // Max for a single frame
2040 // The final few frames have special treatment
2041 if (cpi->frames_till_gf_update_due >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
2043 cpi->gf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;;
2046 // Calculate modified prediction error used in bit allocation
2047 modified_err = calculate_modified_err(cpi, this_frame);
2049 if (cpi->gf_group_error_left > 0)
2050 err_fraction = modified_err / cpi->gf_group_error_left; // What portion of the remaining GF group error is used by this frame
2054 target_frame_size = (int)((double)cpi->gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it?
2056 // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top end.
2057 if (target_frame_size < 0)
2058 target_frame_size = 0;
2061 if (target_frame_size > max_bits)
2062 target_frame_size = max_bits;
2064 if (target_frame_size > cpi->gf_group_bits)
2065 target_frame_size = cpi->gf_group_bits;
2068 cpi->gf_group_error_left -= modified_err; // Adjust error remaining
2069 cpi->gf_group_bits -= target_frame_size; // Adjust bits remaining
2071 if (cpi->gf_group_bits < 0)
2072 cpi->gf_group_bits = 0;
2074 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame.
2076 // Special case for the frame that lies half way between two gfs
2077 if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2)
2078 target_frame_size += cpi->mid_gf_extra_bits;
2080 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
2083 void vp8_second_pass(VP8_COMP *cpi)
2086 int frames_left = (int)(cpi->total_stats->count - cpi->common.current_video_frame);
2088 FIRSTPASS_STATS this_frame;
2089 FIRSTPASS_STATS this_frame_copy;
2091 VP8_COMMON *cm = &cpi->common;
2093 double this_frame_error;
2094 double this_frame_intra_error;
2095 double this_frame_coded_error;
2097 FIRSTPASS_STATS *start_pos;
2104 vp8_clear_system_state();
2106 if (EOF == vp8_input_stats(cpi, &this_frame))
2109 vpx_memset(cpi->fp_motion_map, 0,
2110 cpi->oxcf.arnr_max_frames*cpi->common.MBs);
2111 cpi->fp_motion_map_stats_save = vp8_fpmm_get_pos(cpi);
2113 // Step over this frame's first pass motion map
2114 vp8_advance_fpmm(cpi, 1);
2116 this_frame_error = this_frame.ssim_weighted_pred_err;
2117 this_frame_intra_error = this_frame.intra_error;
2118 this_frame_coded_error = this_frame.coded_error;
2120 // Store information regarding level of motion etc for use mode decisions.
2121 cpi->motion_speed = (int)(fabs(this_frame.MVr) + fabs(this_frame.MVc));
2122 cpi->motion_var = (int)(fabs(this_frame.MVrv) + fabs(this_frame.MVcv));
2123 cpi->inter_lvl = (int)(this_frame.pcnt_inter * 100);
2124 cpi->intra_lvl = (int)((1.0 - this_frame.pcnt_inter) * 100);
2125 cpi->motion_lvl = (int)(this_frame.pcnt_motion * 100);
2127 start_pos = cpi->stats_in;
2129 // keyframe and section processing !
2130 if (cpi->frames_to_key == 0)
2132 // Define next KF group and assign bits to it
2133 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2134 vp8_find_next_key_frame(cpi, &this_frame_copy);
2136 // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop
2137 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2138 // This is temporary code till we decide what should really happen in this case.
2139 if (cpi->oxcf.error_resilient_mode)
2141 cpi->gf_group_bits = cpi->kf_group_bits;
2142 cpi->gf_group_error_left = cpi->kf_group_error_left;
2143 cpi->baseline_gf_interval = cpi->frames_to_key;
2144 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2145 cpi->source_alt_ref_pending = FALSE;
2150 // Is this a GF / ARF (Note that a KF is always also a GF)
2151 if (cpi->frames_till_gf_update_due == 0)
2153 // Update monitor of the bits per error observed so far.
2154 // Done once per gf group based on what has gone before
2155 // so do nothing if this is the first frame.
2156 if (cpi->common.current_video_frame > 0)
2159 (double)(cpi->clip_bits_total - cpi->bits_left) /
2160 cpi->modified_error_used;
2163 // Define next gf group and assign bits to it
2164 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2165 define_gf_group(cpi, &this_frame_copy);
2167 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
2168 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
2169 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
2170 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2172 // Assign a standard frames worth of bits from those allocated to the GF group
2173 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2174 assign_std_frame_bits(cpi, &this_frame_copy);
2176 // If appropriate (we are switching into ARF active but it was not previously active) apply a boost for the gf at the start of the group.
2177 //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
2181 int pct_extra = (cpi->gfu_boost - 100) / 50;
2183 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2185 extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
2186 cpi->gf_group_bits -= extra_bits;
2187 cpi->per_frame_bandwidth += extra_bits;
2192 // Otherwise this is an ordinary frame
2195 // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop
2196 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2197 // This is temporary code till we decide what should really happen in this case.
2198 if (cpi->oxcf.error_resilient_mode)
2200 cpi->frames_till_gf_update_due = cpi->frames_to_key;
2202 if (cpi->common.frame_type != KEY_FRAME)
2204 // Assign bits from those allocated to the GF group
2205 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2206 assign_std_frame_bits(cpi, &this_frame_copy);
2211 // Assign bits from those allocated to the GF group
2212 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2213 assign_std_frame_bits(cpi, &this_frame_copy);
2217 // Keep a globally available copy of this and the next frame's iiratio.
2218 cpi->this_iiratio = this_frame_intra_error /
2219 DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
2221 FIRSTPASS_STATS next_frame;
2222 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2224 cpi->next_iiratio = next_frame.intra_error /
2225 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
2229 // Set nominal per second bandwidth for this frame
2230 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
2231 if (cpi->target_bandwidth < 0)
2232 cpi->target_bandwidth = 0;
2234 if (cpi->common.current_video_frame == 0)
2236 cpi->est_max_qcorrection_factor = 1.0;
2238 // Experimental code to try and set a cq_level in constrained
2240 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2246 (cpi->total_coded_error_left / frames_left),
2247 (int)(cpi->bits_left / frames_left));
2249 cpi->cq_target_quality = cpi->oxcf.cq_level;
2250 if ( est_cq > cpi->cq_target_quality )
2251 cpi->cq_target_quality = est_cq;
2254 // guess at maxq needed in 2nd pass
2255 cpi->maxq_max_limit = cpi->worst_quality;
2256 cpi->maxq_min_limit = cpi->best_quality;
2257 tmp_q = estimate_max_q( cpi,
2258 (cpi->total_coded_error_left / frames_left),
2259 (int)(cpi->bits_left / frames_left));
2261 // Limit the maxq value returned subsequently.
2262 // This increases the risk of overspend or underspend if the initial
2263 // estimate for the clip is bad, but helps prevent excessive
2264 // variation in Q, especially near the end of a clip
2265 // where for example a small overspend may cause Q to crash
2266 cpi->maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2267 ? (tmp_q + 32) : cpi->worst_quality;
2268 cpi->maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2269 ? (tmp_q - 32) : cpi->best_quality;
2271 cpi->active_worst_quality = tmp_q;
2272 cpi->ni_av_qi = tmp_q;
2275 // The last few frames of a clip almost always have to few or too many
2276 // bits and for the sake of over exact rate control we dont want to make
2277 // radical adjustments to the allowed quantizer range just to use up a
2278 // few surplus bits or get beneath the target rate.
2279 else if ( (cpi->common.current_video_frame <
2280 (((unsigned int)cpi->total_stats->count * 255)>>8)) &&
2281 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2282 (unsigned int)cpi->total_stats->count) )
2284 if (frames_left < 1)
2287 tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left));
2289 // Move active_worst_quality but in a damped way
2290 if (tmp_q > cpi->active_worst_quality)
2291 cpi->active_worst_quality ++;
2292 else if (tmp_q < cpi->active_worst_quality)
2293 cpi->active_worst_quality --;
2295 cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2298 cpi->frames_to_key --;
2299 cpi->total_error_left -= this_frame_error;
2300 cpi->total_intra_error_left -= this_frame_intra_error;
2301 cpi->total_coded_error_left -= this_frame_coded_error;
2305 static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2307 BOOL is_viable_kf = FALSE;
2309 // Does the frame satisfy the primary criteria of a key frame
2310 // If so, then examine how well it predicts subsequent frames
2311 if ((this_frame->pcnt_second_ref < 0.10) &&
2312 (next_frame->pcnt_second_ref < 0.10) &&
2313 ((this_frame->pcnt_inter < 0.05) ||
2315 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2316 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2317 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2318 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2319 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2326 FIRSTPASS_STATS *start_pos;
2328 FIRSTPASS_STATS local_next_frame;
2330 double boost_score = 0.0;
2331 double old_boost_score = 0.0;
2332 double decay_accumulator = 1.0;
2333 double next_iiratio;
2335 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2337 // Note the starting file position so we can reset to it
2338 start_pos = cpi->stats_in;
2340 // Examine how well the key frame predicts subsequent frames
2341 for (i = 0 ; i < 16; i++)
2343 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
2345 if (next_iiratio > RMAX)
2346 next_iiratio = RMAX;
2348 // Cumulative effect of decay in prediction quality
2349 if (local_next_frame.pcnt_inter > 0.85)
2350 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2352 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2354 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2356 // Keep a running total
2357 boost_score += (decay_accumulator * next_iiratio);
2359 // Test various breakout clauses
2360 if ((local_next_frame.pcnt_inter < 0.05) ||
2361 (next_iiratio < 1.5) ||
2362 (((local_next_frame.pcnt_inter -
2363 local_next_frame.pcnt_neutral) < 0.20) &&
2364 (next_iiratio < 3.0)) ||
2365 ((boost_score - old_boost_score) < 0.5) ||
2366 (local_next_frame.intra_error < 200)
2372 old_boost_score = boost_score;
2374 // Get the next frame details
2375 if (EOF == vp8_input_stats(cpi, &local_next_frame))
2379 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2380 if (boost_score > 5.0 && (i > 3))
2381 is_viable_kf = TRUE;
2384 // Reset the file position
2385 reset_fpf_position(cpi, start_pos);
2387 is_viable_kf = FALSE;
2391 return is_viable_kf;
2393 void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2396 FIRSTPASS_STATS last_frame;
2397 FIRSTPASS_STATS first_frame;
2398 FIRSTPASS_STATS next_frame;
2399 FIRSTPASS_STATS *start_position;
2401 double decay_accumulator = 1.0;
2402 double boost_score = 0;
2403 double old_boost_score = 0.0;
2404 double loop_decay_rate;
2406 double kf_mod_err = 0.0;
2407 double kf_group_err = 0.0;
2408 double kf_group_intra_err = 0.0;
2409 double kf_group_coded_err = 0.0;
2410 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2411 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2413 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2415 vp8_clear_system_state(); //__asm emms;
2416 start_position = cpi->stats_in;
2418 cpi->common.frame_type = KEY_FRAME;
2420 // is this a forced key frame by interval
2421 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2423 // Clear the alt ref active flag as this can never be active on a key frame
2424 cpi->source_alt_ref_active = FALSE;
2426 // Kf is always a gf so clear frames till next gf counter
2427 cpi->frames_till_gf_update_due = 0;
2429 cpi->frames_to_key = 1;
2431 // Take a copy of the initial frame details
2432 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2434 cpi->kf_group_bits = 0; // Total bits avaialable to kf group
2435 cpi->kf_group_error_left = 0; // Group modified error score.
2437 kf_mod_err = calculate_modified_err(cpi, this_frame);
2439 // find the next keyframe
2441 while (cpi->stats_in < cpi->stats_in_end)
2443 // Accumulate kf group error
2444 kf_group_err += calculate_modified_err(cpi, this_frame);
2446 // These figures keep intra and coded error counts for all frames including key frames in the group.
2447 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2448 kf_group_intra_err += this_frame->intra_error;
2449 kf_group_coded_err += this_frame->coded_error;
2451 // load a the next frame's stats
2452 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2453 vp8_input_stats(cpi, this_frame);
2455 // Provided that we are not at the end of the file...
2456 if (cpi->oxcf.auto_key
2457 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2459 // Normal scene cut check
2460 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
2463 // How fast is prediction quality decaying
2464 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2466 // We want to know something about the recent past... rather than
2467 // as used elsewhere where we are concened with decay in prediction
2468 // quality since the last GF or KF.
2469 recent_loop_decay[i%8] = loop_decay_rate;
2470 decay_accumulator = 1.0;
2471 for (j = 0; j < 8; j++)
2473 decay_accumulator = decay_accumulator * recent_loop_decay[j];
2476 // Special check for transition or high motion followed by a
2477 // to a static scene.
2478 if ( detect_transition_to_still( cpi, i,
2479 (cpi->key_frame_frequency-i),
2481 decay_accumulator ) )
2487 // Step on to the next frame
2488 cpi->frames_to_key ++;
2490 // If we don't have a real key frame within the next two
2491 // forcekeyframeevery intervals then break out of the loop.
2492 if (cpi->frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2495 cpi->frames_to_key ++;
2500 // If there is a max kf interval set by the user we must obey it.
2501 // We already breakout of the loop above at 2x max.
2502 // This code centers the extra kf if the actual natural
2503 // interval is between 1x and 2x
2504 if (cpi->oxcf.auto_key
2505 && cpi->frames_to_key > (int)cpi->key_frame_frequency )
2507 FIRSTPASS_STATS *current_pos = cpi->stats_in;
2508 FIRSTPASS_STATS tmp_frame;
2510 cpi->frames_to_key /= 2;
2512 // Copy first frame details
2513 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2515 // Reset to the start of the group
2516 reset_fpf_position(cpi, start_position);
2519 kf_group_intra_err = 0;
2520 kf_group_coded_err = 0;
2522 // Rescan to get the correct error data for the forced kf group
2523 for( i = 0; i < cpi->frames_to_key; i++ )
2525 // Accumulate kf group errors
2526 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2527 kf_group_intra_err += tmp_frame.intra_error;
2528 kf_group_coded_err += tmp_frame.coded_error;
2530 // Load a the next frame's stats
2531 vp8_input_stats(cpi, &tmp_frame);
2534 // Reset to the start of the group
2535 reset_fpf_position(cpi, current_pos);
2537 cpi->next_key_frame_forced = TRUE;
2540 cpi->next_key_frame_forced = FALSE;
2542 // Special case for the last frame of the file
2543 if (cpi->stats_in >= cpi->stats_in_end)
2545 // Accumulate kf group error
2546 kf_group_err += calculate_modified_err(cpi, this_frame);
2548 // These figures keep intra and coded error counts for all frames including key frames in the group.
2549 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2550 kf_group_intra_err += this_frame->intra_error;
2551 kf_group_coded_err += this_frame->coded_error;
2554 // Calculate the number of bits that should be assigned to the kf group.
2555 if ((cpi->bits_left > 0) && (cpi->modified_error_left > 0.0))
2557 // Max for a single normal frame (not key frame)
2558 int max_bits = frame_max_bits(cpi);
2560 // Maximum bits for the kf group
2561 long long max_grp_bits;
2563 // Default allocation based on bits left and relative
2564 // complexity of the section
2565 cpi->kf_group_bits = (long long)( cpi->bits_left *
2567 cpi->modified_error_left ));
2569 // Clip based on maximum per frame rate defined by the user.
2570 max_grp_bits = (long long)max_bits * (long long)cpi->frames_to_key;
2571 if (cpi->kf_group_bits > max_grp_bits)
2572 cpi->kf_group_bits = max_grp_bits;
2574 // Additional special case for CBR if buffer is getting full.
2575 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2577 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2578 int buffer_lvl = cpi->buffer_level;
2580 // If the buffer is near or above the optimal and this kf group is
2581 // not being allocated much then increase the allocation a bit.
2582 if (buffer_lvl >= opt_buffer_lvl)
2584 int high_water_mark = (opt_buffer_lvl +
2585 cpi->oxcf.maximum_buffer_size) >> 1;
2587 long long av_group_bits;
2589 // Av bits per frame * number of frames
2590 av_group_bits = (long long)cpi->av_per_frame_bandwidth *
2591 (long long)cpi->frames_to_key;
2593 // We are at or above the maximum.
2594 if (cpi->buffer_level >= high_water_mark)
2596 long long min_group_bits;
2598 min_group_bits = av_group_bits +
2599 (long long)(buffer_lvl -
2602 if (cpi->kf_group_bits < min_group_bits)
2603 cpi->kf_group_bits = min_group_bits;
2605 // We are above optimal but below the maximum
2606 else if (cpi->kf_group_bits < av_group_bits)
2608 long long bits_below_av = av_group_bits -
2611 cpi->kf_group_bits +=
2612 (long long)((double)bits_below_av *
2613 (double)(buffer_lvl - opt_buffer_lvl) /
2614 (double)(high_water_mark - opt_buffer_lvl));
2620 cpi->kf_group_bits = 0;
2622 // Reset the first pass file position
2623 reset_fpf_position(cpi, start_position);
2625 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2626 decay_accumulator = 1.0;
2628 loop_decay_rate = 1.00; // Starting decay rate
2630 for (i = 0 ; i < cpi->frames_to_key ; i++)
2633 double motion_decay;
2636 if (EOF == vp8_input_stats(cpi, &next_frame))
2639 if (next_frame.intra_error > cpi->kf_intra_err_min)
2640 r = (IIKFACTOR2 * next_frame.intra_error /
2641 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2643 r = (IIKFACTOR2 * cpi->kf_intra_err_min /
2644 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2649 // How fast is prediction quality decaying
2650 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2652 decay_accumulator = decay_accumulator * loop_decay_rate;
2653 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2655 boost_score += (decay_accumulator * r);
2657 if ((i > MIN_GF_INTERVAL) &&
2658 ((boost_score - old_boost_score) < 1.0))
2663 old_boost_score = boost_score;
2668 FIRSTPASS_STATS sectionstats;
2671 vp8_zero_stats(§ionstats);
2672 reset_fpf_position(cpi, start_position);
2674 for (i = 0 ; i < cpi->frames_to_key ; i++)
2676 vp8_input_stats(cpi, &next_frame);
2677 vp8_accumulate_stats(§ionstats, &next_frame);
2680 vp8_avg_stats(§ionstats);
2682 cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2684 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2685 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2687 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2689 if (cpi->section_max_qfactor < 0.80)
2690 cpi->section_max_qfactor = 0.80;
2694 // cpi->section_max_qfactor = 1.0;
2697 // When using CBR apply additional buffer fullness related upper limits
2698 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2702 if (cpi->drop_frames_allowed)
2704 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
2706 if (cpi->buffer_level > df_buffer_level)
2707 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2711 else if (cpi->buffer_level > 0)
2713 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2720 if (boost_score > max_boost)
2721 boost_score = max_boost;
2724 // Reset the first pass file position
2725 reset_fpf_position(cpi, start_position);
2727 // Work out how many bits to allocate for the key frame itself
2730 int kf_boost = boost_score;
2731 int allocation_chunks;
2732 int Counter = cpi->frames_to_key;
2734 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2735 // Min boost based on kf interval
2738 while ((kf_boost < 48) && (Counter > 0))
2748 kf_boost += ((Counter + 1) >> 1);
2750 if (kf_boost > 48) kf_boost = 48;
2753 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2754 if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
2755 kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2756 else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
2757 kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2759 kf_boost = (int)((double)kf_boost * 100.0) >> 4; // Scale 16 to 100
2761 // Adjustment to boost based on recent average q
2762 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2764 if (kf_boost < 250) // Min KF boost
2767 // We do three calculations for kf size.
2768 // The first is based on the error score for the whole kf group.
2769 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2770 // The final one insures that the frame receives at least the allocation it would have received based on its own error score vs the error score remaining
2772 allocation_chunks = ((cpi->frames_to_key - 1) * 100) + kf_boost; // cpi->frames_to_key-1 because key frame itself is taken care of by kf_boost
2774 // Normalize Altboost and allocations chunck down to prevent overflow
2775 while (kf_boost > 1000)
2778 allocation_chunks /= 2;
2781 cpi->kf_group_bits = (cpi->kf_group_bits < 0) ? 0 : cpi->kf_group_bits;
2783 // Calculate the number of bits to be spent on the key frame
2784 cpi->kf_bits = (int)((double)kf_boost * ((double)cpi->kf_group_bits / (double)allocation_chunks));
2786 // Apply an additional limit for CBR
2787 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2789 if (cpi->kf_bits > ((3 * cpi->buffer_level) >> 2))
2790 cpi->kf_bits = (3 * cpi->buffer_level) >> 2;
2793 // If the key frame is actually easier than the average for the
2794 // kf group (which does sometimes happen... eg a blank intro frame)
2795 // Then use an alternate calculation based on the kf error score
2796 // which should give a smaller key frame.
2797 if (kf_mod_err < kf_group_err / cpi->frames_to_key)
2799 double alt_kf_grp_bits =
2800 ((double)cpi->bits_left *
2801 (kf_mod_err * (double)cpi->frames_to_key) /
2802 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left));
2804 alt_kf_bits = (int)((double)kf_boost *
2805 (alt_kf_grp_bits / (double)allocation_chunks));
2807 if (cpi->kf_bits > alt_kf_bits)
2809 cpi->kf_bits = alt_kf_bits;
2812 // Else if it is much harder than other frames in the group make sure
2813 // it at least receives an allocation in keeping with its relative
2818 (int)((double)cpi->bits_left *
2820 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left)));
2822 if (alt_kf_bits > cpi->kf_bits)
2824 cpi->kf_bits = alt_kf_bits;
2828 cpi->kf_group_bits -= cpi->kf_bits;
2829 cpi->kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance
2831 cpi->per_frame_bandwidth = cpi->kf_bits; // Peer frame bit target for this frame
2832 cpi->target_bandwidth = cpi->kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate
2835 // Note the total error score of the kf group minus the key frame itself
2836 cpi->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2838 // Adjust the count of total modified error left.
2839 // The count of bits left is adjusted elsewhere based on real coded frame sizes
2840 cpi->modified_error_left -= kf_group_err;
2842 if (cpi->oxcf.allow_spatial_resampling)
2844 int resample_trigger = FALSE;
2845 int last_kf_resampled = FALSE;
2849 int new_width = cpi->oxcf.Width;
2850 int new_height = cpi->oxcf.Height;
2852 int projected_buffer_level = cpi->buffer_level;
2855 double projected_bits_perframe;
2856 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
2857 double err_per_frame = kf_group_err / cpi->frames_to_key;
2858 double bits_per_frame;
2859 double av_bits_per_frame;
2860 double effective_size_ratio;
2862 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
2863 last_kf_resampled = TRUE;
2865 // Set back to unscaled by defaults
2866 cpi->common.horiz_scale = NORMAL;
2867 cpi->common.vert_scale = NORMAL;
2869 // Calculate Average bits per frame.
2870 //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats->count - cpi->common.current_video_frame);
2871 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate);
2872 //if ( av_bits_per_frame < 0.0 )
2873 // av_bits_per_frame = 0.0
2875 // CBR... Use the clip average as the target for deciding resample
2876 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2878 bits_per_frame = av_bits_per_frame;
2881 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
2882 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
2885 bits_per_frame = cpi->kf_group_bits / cpi->frames_to_key; // This accounts for how hard the section is...
2887 if (bits_per_frame < av_bits_per_frame) // Dont turn to resampling in easy sections just because they have been assigned a small number of bits
2888 bits_per_frame = av_bits_per_frame;
2891 // bits_per_frame should comply with our minimum
2892 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
2893 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2895 // Work out if spatial resampling is necessary
2896 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, group_iiratio);
2898 // If we project a required Q higher than the maximum allowed Q then make a guess at the actual size of frames in this section
2899 projected_bits_perframe = bits_per_frame;
2902 while (tmp_q > cpi->worst_quality)
2904 projected_bits_perframe *= 1.04;
2908 // Guess at buffer level at the end of the section
2909 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->frames_to_key);
2913 FILE *f = fopen("Subsamle.stt", "a");
2914 fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->frames_to_key, (int)(cpi->kf_group_bits / cpi->frames_to_key), new_height, new_width);
2918 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
2919 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2921 // Trigger resample if we are projected to fall below down sample level or
2922 // resampled last time and are projected to remain below the up sample level
2923 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
2924 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
2925 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
2926 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
2927 resample_trigger = TRUE;
2929 resample_trigger = FALSE;
2933 long long clip_bits = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
2934 long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
2935 long long over_spend2 = cpi->oxcf.starting_buffer_level - projected_buffer_level;
2937 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced
2938 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ...
2939 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits
2940 resample_trigger = TRUE;
2942 resample_trigger = FALSE;
2946 if (resample_trigger)
2948 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
2952 cpi->common.vert_scale = vscale_lookup[scale_val];
2953 cpi->common.horiz_scale = hscale_lookup[scale_val];
2955 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
2956 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
2958 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2959 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2961 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
2962 // effective_sizeratio attempts to provide a crude correction for this
2963 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
2964 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
2966 // Now try again and see what Q we get with the smaller image size
2967 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, group_iiratio);
2971 FILE *f = fopen("Subsamle.stt", "a");
2972 fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->frames_to_key, (int)(cpi->kf_group_bits / cpi->frames_to_key), new_height, new_width);
2978 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
2980 cpi->common.Width = new_width;
2981 cpi->common.Height = new_height;
2982 vp8_alloc_compressor_data(cpi);