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.
17 #include "vp8/common/variance.h"
18 #include "encodeintra.h"
19 #include "vp8/common/setupintrarecon.h"
20 #include "vp8/common/systemdependent.h"
22 #include "firstpass.h"
23 #include "vpx_scale/vpxscale.h"
25 #include "vp8/common/extend.h"
26 #include "vpx_mem/vpx_mem.h"
27 #include "vp8/common/swapyv12buffer.h"
29 #include "vp8/common/quant_common.h"
32 //#define OUTPUT_FPF 1
34 extern void vp8_build_block_offsets(MACROBLOCK *x);
35 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
36 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
37 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv);
38 extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
40 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10))
41 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10))
42 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
43 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
45 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
48 #define IIKFACTOR1 1.40
49 #define IIKFACTOR2 1.5
53 #define KF_MB_INTRA_MIN 300
54 #define GF_MB_INTRA_MIN 200
56 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
58 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
59 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
63 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
64 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
67 static const int cq_level[QINDEX_RANGE] =
69 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
70 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
71 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
72 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
73 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
74 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
75 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
76 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
79 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
81 // Resets the first pass file to the given position using a relative seek from the current position
82 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
84 cpi->twopass.stats_in = Position;
87 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
89 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
92 *next_frame = *cpi->twopass.stats_in;
96 // Read frame stats at an offset from the current position
97 static int read_frame_stats( VP8_COMP *cpi,
98 FIRSTPASS_STATS *frame_stats,
101 FIRSTPASS_STATS * fps_ptr = cpi->twopass.stats_in;
103 // Check legality of offset
106 if ( &fps_ptr[offset] >= cpi->twopass.stats_in_end )
109 else if ( offset < 0 )
111 if ( &fps_ptr[offset] < cpi->twopass.stats_in_start )
115 *frame_stats = fps_ptr[offset];
119 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
121 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
124 *fps = *cpi->twopass.stats_in;
125 cpi->twopass.stats_in =
126 (void*)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
130 static void output_stats(const VP8_COMP *cpi,
131 struct vpx_codec_pkt_list *pktlist,
132 FIRSTPASS_STATS *stats)
134 struct vpx_codec_cx_pkt pkt;
135 pkt.kind = VPX_CODEC_STATS_PKT;
136 pkt.data.twopass_stats.buf = stats;
137 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
138 vpx_codec_pkt_list_add(pktlist, &pkt);
145 fpfile = fopen("firstpass.stt", "a");
147 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
148 " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
149 " %12.0f %12.0f %12.4f\n",
153 stats->ssim_weighted_pred_err,
156 stats->pcnt_second_ref,
164 stats->mv_in_out_count,
173 static void zero_stats(FIRSTPASS_STATS *section)
175 section->frame = 0.0;
176 section->intra_error = 0.0;
177 section->coded_error = 0.0;
178 section->ssim_weighted_pred_err = 0.0;
179 section->pcnt_inter = 0.0;
180 section->pcnt_motion = 0.0;
181 section->pcnt_second_ref = 0.0;
182 section->pcnt_neutral = 0.0;
184 section->mvr_abs = 0.0;
186 section->mvc_abs = 0.0;
189 section->mv_in_out_count = 0.0;
190 section->new_mv_count = 0.0;
191 section->count = 0.0;
192 section->duration = 1.0;
195 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
197 section->frame += frame->frame;
198 section->intra_error += frame->intra_error;
199 section->coded_error += frame->coded_error;
200 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
201 section->pcnt_inter += frame->pcnt_inter;
202 section->pcnt_motion += frame->pcnt_motion;
203 section->pcnt_second_ref += frame->pcnt_second_ref;
204 section->pcnt_neutral += frame->pcnt_neutral;
205 section->MVr += frame->MVr;
206 section->mvr_abs += frame->mvr_abs;
207 section->MVc += frame->MVc;
208 section->mvc_abs += frame->mvc_abs;
209 section->MVrv += frame->MVrv;
210 section->MVcv += frame->MVcv;
211 section->mv_in_out_count += frame->mv_in_out_count;
212 section->new_mv_count += frame->new_mv_count;
213 section->count += frame->count;
214 section->duration += frame->duration;
217 static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
219 section->frame -= frame->frame;
220 section->intra_error -= frame->intra_error;
221 section->coded_error -= frame->coded_error;
222 section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
223 section->pcnt_inter -= frame->pcnt_inter;
224 section->pcnt_motion -= frame->pcnt_motion;
225 section->pcnt_second_ref -= frame->pcnt_second_ref;
226 section->pcnt_neutral -= frame->pcnt_neutral;
227 section->MVr -= frame->MVr;
228 section->mvr_abs -= frame->mvr_abs;
229 section->MVc -= frame->MVc;
230 section->mvc_abs -= frame->mvc_abs;
231 section->MVrv -= frame->MVrv;
232 section->MVcv -= frame->MVcv;
233 section->mv_in_out_count -= frame->mv_in_out_count;
234 section->new_mv_count -= frame->new_mv_count;
235 section->count -= frame->count;
236 section->duration -= frame->duration;
239 static void avg_stats(FIRSTPASS_STATS *section)
241 if (section->count < 1.0)
244 section->intra_error /= section->count;
245 section->coded_error /= section->count;
246 section->ssim_weighted_pred_err /= section->count;
247 section->pcnt_inter /= section->count;
248 section->pcnt_second_ref /= section->count;
249 section->pcnt_neutral /= section->count;
250 section->pcnt_motion /= section->count;
251 section->MVr /= section->count;
252 section->mvr_abs /= section->count;
253 section->MVc /= section->count;
254 section->mvc_abs /= section->count;
255 section->MVrv /= section->count;
256 section->MVcv /= section->count;
257 section->mv_in_out_count /= section->count;
258 section->duration /= section->count;
261 // Calculate a modified Error used in distributing bits between easier and harder frames
262 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
264 double av_err = ( cpi->twopass.total_stats.ssim_weighted_pred_err /
265 cpi->twopass.total_stats.count );
266 double this_err = this_frame->ssim_weighted_pred_err;
269 if (this_err > av_err)
270 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
272 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
277 static const double weight_table[256] = {
278 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
279 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
280 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
281 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
282 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
283 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
284 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
285 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
286 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
287 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
288 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
289 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
290 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
291 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
292 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
293 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
294 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
295 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
296 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
297 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
298 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
299 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
300 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
301 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
302 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
303 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
304 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
305 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
306 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
307 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
308 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
309 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
312 static double simple_weight(YV12_BUFFER_CONFIG *source)
316 unsigned char *src = source->y_buffer;
317 double sum_weights = 0.0;
319 // Loop throught the Y plane raw examining levels and creating a weight for the image
320 i = source->y_height;
326 sum_weights += weight_table[ *src];
329 src -= source->y_width;
330 src += source->y_stride;
333 sum_weights /= (source->y_height * source->y_width);
339 // This function returns the current per frame maximum bitrate target
340 static int frame_max_bits(VP8_COMP *cpi)
342 // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
345 // For CBR we need to also consider buffer fullness.
346 // If we are running below the optimal level then we need to gradually tighten up on max_bits.
347 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
349 double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
351 // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
352 max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
354 // If our buffer is below the optimum level
355 if (buffer_fullness_ratio < 1.0)
357 // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
358 int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
360 max_bits = (int)(max_bits * buffer_fullness_ratio);
362 if (max_bits < min_max_bits)
363 max_bits = min_max_bits; // Lowest value we will set ... which should allow the buffer to refil.
369 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
370 max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats.count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
373 // Trap case where we are out of bits
380 void vp8_init_first_pass(VP8_COMP *cpi)
382 zero_stats(&cpi->twopass.total_stats);
385 void vp8_end_first_pass(VP8_COMP *cpi)
387 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
390 static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x,
391 YV12_BUFFER_CONFIG * raw_buffer,
392 int * raw_motion_err,
393 YV12_BUFFER_CONFIG * recon_buffer,
394 int * best_motion_err, int recon_yoffset)
396 MACROBLOCKD * const xd = & x->e_mbd;
397 BLOCK *b = &x->block[0];
398 BLOCKD *d = &x->e_mbd.block[0];
400 unsigned char *src_ptr = (*(b->base_src) + b->src);
401 int src_stride = b->src_stride;
402 unsigned char *raw_ptr;
403 int raw_stride = raw_buffer->y_stride;
404 unsigned char *ref_ptr;
405 int ref_stride = x->e_mbd.pre.y_stride;
407 // Set up pointers for this macro block raw buffer
408 raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset
410 vp8_mse16x16 ( src_ptr, src_stride, raw_ptr, raw_stride,
411 (unsigned int *)(raw_motion_err));
413 // Set up pointers for this macro block recon buffer
414 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
415 ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset );
416 vp8_mse16x16 ( src_ptr, src_stride, ref_ptr, ref_stride,
417 (unsigned int *)(best_motion_err));
420 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
421 int_mv *ref_mv, MV *best_mv,
422 YV12_BUFFER_CONFIG *recon_buffer,
423 int *best_motion_err, int recon_yoffset )
425 MACROBLOCKD *const xd = & x->e_mbd;
426 BLOCK *b = &x->block[0];
427 BLOCKD *d = &x->e_mbd.block[0];
434 int step_param = 3; //3; // Dont search over full range for first pass
435 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3;
437 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
438 int new_mv_mode_penalty = 256;
440 // override the default variance function to use MSE
441 v_fn_ptr.vf = vp8_mse16x16;
443 // Set up pointers for this macro block recon buffer
444 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
446 // Initial step/diamond search centred on best mv
448 ref_mv_full.as_mv.col = ref_mv->as_mv.col>>3;
449 ref_mv_full.as_mv.row = ref_mv->as_mv.row>>3;
450 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
451 x->sadperbit16, &num00, &v_fn_ptr,
453 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
454 tmp_err += new_mv_mode_penalty;
456 if (tmp_err < *best_motion_err)
458 *best_motion_err = tmp_err;
459 best_mv->row = tmp_mv.as_mv.row;
460 best_mv->col = tmp_mv.as_mv.col;
463 // Further step/diamond searches as necessary
467 while (n < further_steps)
475 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
476 step_param + n, x->sadperbit16,
477 &num00, &v_fn_ptr, x->mvcost,
479 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
480 tmp_err += new_mv_mode_penalty;
482 if (tmp_err < *best_motion_err)
484 *best_motion_err = tmp_err;
485 best_mv->row = tmp_mv.as_mv.row;
486 best_mv->col = tmp_mv.as_mv.col;
492 void vp8_first_pass(VP8_COMP *cpi)
495 MACROBLOCK *const x = & cpi->mb;
496 VP8_COMMON *const cm = & cpi->common;
497 MACROBLOCKD *const xd = & x->e_mbd;
499 int recon_yoffset, recon_uvoffset;
500 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
501 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
502 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
503 int recon_y_stride = lst_yv12->y_stride;
504 int recon_uv_stride = lst_yv12->uv_stride;
505 int64_t intra_error = 0;
506 int64_t coded_error = 0;
508 int sum_mvr = 0, sum_mvc = 0;
509 int sum_mvr_abs = 0, sum_mvc_abs = 0;
510 int sum_mvrs = 0, sum_mvcs = 0;
513 int second_ref_count = 0;
514 int intrapenalty = 256;
515 int neutral_count = 0;
516 int new_mv_count = 0;
517 int sum_in_vectors = 0;
518 uint32_t lastmv_as_int = 0;
522 zero_ref_mv.as_int = 0;
524 vp8_clear_system_state(); //__asm emms;
526 x->src = * cpi->Source;
530 x->partition_info = x->pi;
532 xd->mode_info_context = cm->mi;
534 vp8_build_block_offsets(x);
536 vp8_setup_block_dptrs(&x->e_mbd);
538 vp8_setup_block_ptrs(x);
540 // set up frame new frame for intra coded blocks
541 vp8_setup_intra_recon(new_yv12);
542 vp8cx_frame_init_quantizer(cpi);
544 // Initialise the MV cost table to the defaults
545 //if( cm->current_video_frame == 0)
548 int flag[2] = {1, 1};
549 vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
550 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
551 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
554 // for each macroblock row in image
555 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
559 best_ref_mv.as_int = 0;
561 // reset above block coeffs
562 xd->up_available = (mb_row != 0);
563 recon_yoffset = (mb_row * recon_y_stride * 16);
564 recon_uvoffset = (mb_row * recon_uv_stride * 8);
566 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
567 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
568 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
571 // for each macroblock col in image
572 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
575 int gf_motion_error = INT_MAX;
576 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
578 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
579 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
580 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
581 xd->left_available = (mb_col != 0);
583 //Copy current mb to a buffer
584 vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
586 // do intra 16x16 prediction
587 this_error = vp8_encode_intra(cpi, x, use_dc_pred);
589 // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
590 // 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.
591 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
592 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
593 this_error += intrapenalty;
595 // Cumulative intra error total
596 intra_error += (int64_t)this_error;
598 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
599 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
600 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
602 // Other than for the first frame do a motion search
603 if (cm->current_video_frame > 0)
605 BLOCKD *d = &x->e_mbd.block[0];
608 int motion_error = INT_MAX;
609 int raw_motion_error = INT_MAX;
611 // Simple 0,0 motion with no mv overhead
612 zz_motion_search( cpi, x, cpi->last_frame_unscaled_source,
613 &raw_motion_error, lst_yv12, &motion_error,
615 d->bmi.mv.as_mv.row = 0;
616 d->bmi.mv.as_mv.col = 0;
618 if (raw_motion_error < cpi->oxcf.encode_breakout)
619 goto skip_motion_search;
621 // Test last reference frame using the previous best mv as the
622 // starting point (best reference) for the search
623 first_pass_motion_search(cpi, x, &best_ref_mv,
624 &d->bmi.mv.as_mv, lst_yv12,
625 &motion_error, recon_yoffset);
627 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
628 if (best_ref_mv.as_int)
631 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
632 lst_yv12, &tmp_err, recon_yoffset);
634 if ( tmp_err < motion_error )
636 motion_error = tmp_err;
637 d->bmi.mv.as_mv.row = tmp_mv.row;
638 d->bmi.mv.as_mv.col = tmp_mv.col;
642 // Experimental search in a second reference frame ((0,0) based only)
643 if (cm->current_video_frame > 1)
645 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
647 if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
650 //motion_error = gf_motion_error;
651 //d->bmi.mv.as_mv.row = tmp_mv.row;
652 //d->bmi.mv.as_mv.col = tmp_mv.col;
656 xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
657 xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
658 xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
662 // Reset to last frame as reference buffer
663 xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
664 xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
665 xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
669 /* Intra assumed best */
670 best_ref_mv.as_int = 0;
672 if (motion_error <= this_error)
674 // Keep a count of cases where the inter and intra were
675 // very close and very low. This helps with scene cut
676 // detection for example in cropped clips with black bars
677 // at the sides or top and bottom.
678 if( (((this_error-intrapenalty) * 9) <=
679 (motion_error*10)) &&
680 (this_error < (2*intrapenalty)) )
685 d->bmi.mv.as_mv.row <<= 3;
686 d->bmi.mv.as_mv.col <<= 3;
687 this_error = motion_error;
688 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
689 vp8_encode_inter16x16y(x);
690 sum_mvr += d->bmi.mv.as_mv.row;
691 sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
692 sum_mvc += d->bmi.mv.as_mv.col;
693 sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
694 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
695 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
698 best_ref_mv.as_int = d->bmi.mv.as_int;
700 // Was the vector non-zero
701 if (d->bmi.mv.as_int)
705 // Was it different from the last non zero vector
706 if ( d->bmi.mv.as_int != lastmv_as_int )
708 lastmv_as_int = d->bmi.mv.as_int;
710 // Does the Row vector point inwards or outwards
711 if (mb_row < cm->mb_rows / 2)
713 if (d->bmi.mv.as_mv.row > 0)
715 else if (d->bmi.mv.as_mv.row < 0)
718 else if (mb_row > cm->mb_rows / 2)
720 if (d->bmi.mv.as_mv.row > 0)
722 else if (d->bmi.mv.as_mv.row < 0)
726 // Does the Row vector point inwards or outwards
727 if (mb_col < cm->mb_cols / 2)
729 if (d->bmi.mv.as_mv.col > 0)
731 else if (d->bmi.mv.as_mv.col < 0)
734 else if (mb_col > cm->mb_cols / 2)
736 if (d->bmi.mv.as_mv.col > 0)
738 else if (d->bmi.mv.as_mv.col < 0)
745 coded_error += (int64_t)this_error;
747 // adjust to the next column of macroblocks
748 x->src.y_buffer += 16;
749 x->src.u_buffer += 8;
750 x->src.v_buffer += 8;
756 // adjust to the next row of mbs
757 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
758 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
759 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
761 //extend the recon for intra prediction
762 vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
763 vp8_clear_system_state(); //__asm emms;
766 vp8_clear_system_state(); //__asm emms;
772 fps.frame = cm->current_video_frame ;
773 fps.intra_error = intra_error >> 8;
774 fps.coded_error = coded_error >> 8;
775 weight = simple_weight(cpi->Source);
781 fps.ssim_weighted_pred_err = fps.coded_error * weight;
783 fps.pcnt_inter = 0.0;
784 fps.pcnt_motion = 0.0;
791 fps.mv_in_out_count = 0.0;
792 fps.new_mv_count = 0.0;
795 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
796 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
797 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
801 fps.MVr = (double)sum_mvr / (double)mvcount;
802 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
803 fps.MVc = (double)sum_mvc / (double)mvcount;
804 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
805 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
806 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
807 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
808 fps.new_mv_count = new_mv_count;
810 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
813 // TODO: handle the case when duration is set to 0, or something less
814 // than the full time between subsequent cpi->source_time_stamp s .
815 fps.duration = cpi->source->ts_end
816 - cpi->source->ts_start;
818 // don't want to do output stats with a stack variable!
819 memcpy(&cpi->twopass.this_frame_stats,
821 sizeof(FIRSTPASS_STATS));
822 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
823 accumulate_stats(&cpi->twopass.total_stats, &fps);
826 // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
827 if ((cm->current_video_frame > 0) &&
828 (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
829 ((cpi->twopass.this_frame_stats.intra_error / cpi->twopass.this_frame_stats.coded_error) > 2.0))
831 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
834 // swap frame pointers so last frame refers to the frame we just compressed
835 vp8_swap_yv12_buffer(lst_yv12, new_yv12);
836 vp8_yv12_extend_frame_borders(lst_yv12);
838 // Special case for the first frame. Copy into the GF buffer as a second reference.
839 if (cm->current_video_frame == 0)
841 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
845 // use this to see what the first pass reconstruction looks like
850 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
852 if (cm->current_video_frame == 0)
853 recon_file = fopen(filename, "wb");
855 recon_file = fopen(filename, "ab");
857 if(fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file));
861 cm->current_video_frame++;
864 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
866 // Estimate a cost per mb attributable to overheads such as the coding of
867 // modes and motion vectors.
868 // Currently simplistic in its assumptions for testing.
872 double bitcost( double prob )
874 return -(log( prob ) / log( 2.0 ));
876 static int64_t estimate_modemvcost(VP8_COMP *cpi,
877 FIRSTPASS_STATS * fpstats)
882 double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
883 double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
884 double av_intra = (1.0 - av_pct_inter);
890 zz_cost = bitcost(av_pct_inter - av_pct_motion);
891 motion_cost = bitcost(av_pct_motion);
892 intra_cost = bitcost(av_intra);
894 // Estimate of extra bits per mv overhead for mbs
895 // << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb
896 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
898 // Crude estimate of overhead cost from modes
899 // << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
901 (int)( ( ((av_pct_inter - av_pct_motion) * zz_cost) +
902 (av_pct_motion * motion_cost) +
903 (av_intra * intra_cost) ) * cpi->common.MBs ) << 9;
905 return mv_cost + mode_cost;
908 static double calc_correction_factor( double err_per_mb,
915 double error_term = err_per_mb / err_devisor;
916 double correction_factor;
918 // Adjustment based on Q to power term.
919 power_term = pt_low + (Q * 0.01);
920 power_term = (power_term > pt_high) ? pt_high : power_term;
922 // Adjustments to error term
925 // Calculate correction factor
926 correction_factor = pow(error_term, power_term);
930 (correction_factor < 0.05)
931 ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
933 return correction_factor;
936 static int estimate_max_q(VP8_COMP *cpi,
937 FIRSTPASS_STATS * fpstats,
938 int section_target_bandwitdh,
942 int num_mbs = cpi->common.MBs;
943 int target_norm_bits_per_mb;
945 double section_err = (fpstats->coded_error / fpstats->count);
946 double err_per_mb = section_err / num_mbs;
947 double err_correction_factor;
948 double speed_correction = 1.0;
949 int overhead_bits_per_mb;
951 if (section_target_bandwitdh <= 0)
952 return cpi->twopass.maxq_max_limit; // Highest value allowed
954 target_norm_bits_per_mb =
955 (section_target_bandwitdh < (1 << 20))
956 ? (512 * section_target_bandwitdh) / num_mbs
957 : 512 * (section_target_bandwitdh / num_mbs);
959 // Calculate a corrective factor based on a rolling ratio of bits spent
961 if ((cpi->rolling_target_bits > 0) &&
962 (cpi->active_worst_quality < cpi->worst_quality))
964 double rolling_ratio;
966 rolling_ratio = (double)cpi->rolling_actual_bits /
967 (double)cpi->rolling_target_bits;
969 if (rolling_ratio < 0.95)
970 cpi->twopass.est_max_qcorrection_factor -= 0.005;
971 else if (rolling_ratio > 1.05)
972 cpi->twopass.est_max_qcorrection_factor += 0.005;
974 cpi->twopass.est_max_qcorrection_factor =
975 (cpi->twopass.est_max_qcorrection_factor < 0.1)
977 : (cpi->twopass.est_max_qcorrection_factor > 10.0)
978 ? 10.0 : cpi->twopass.est_max_qcorrection_factor;
981 // Corrections for higher compression speed settings
982 // (reduced compression expected)
983 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
985 if (cpi->oxcf.cpu_used <= 5)
986 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
988 speed_correction = 1.25;
991 // Estimate of overhead bits per mb
992 // Correction to overhead bits for min allowed Q.
993 overhead_bits_per_mb = overhead_bits / num_mbs;
994 overhead_bits_per_mb *= pow( 0.98, (double)cpi->twopass.maxq_min_limit );
996 // Try and pick a max Q that will be high enough to encode the
997 // content at the given rate.
998 for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++)
1000 int bits_per_mb_at_this_q;
1002 // Error per MB based correction factor
1003 err_correction_factor =
1004 calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
1006 bits_per_mb_at_this_q =
1007 vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
1009 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor
1010 * speed_correction * cpi->twopass.est_max_qcorrection_factor
1011 * cpi->twopass.section_max_qfactor
1012 * (double)bits_per_mb_at_this_q);
1014 // Mode and motion overhead
1015 // As Q rises in real encode loop rd code will force overhead down
1016 // We make a crude adjustment for this here as *.98 per Q step.
1017 overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
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) )
1027 Q = cpi->cq_target_quality;
1030 // Adjust maxq_min_limit and maxq_max_limit limits based on
1031 // averaga q observed in clip for non kf/gf.arf frames
1032 // Give average a chance to settle though.
1033 if ( (cpi->ni_frames >
1034 ((unsigned int)cpi->twopass.total_stats.count >> 8)) &&
1035 (cpi->ni_frames > 150) )
1037 cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
1038 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
1039 cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
1040 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
1046 // For cq mode estimate a cq level that matches the observed
1047 // complexity and data rate.
1048 static int estimate_cq( VP8_COMP *cpi,
1049 FIRSTPASS_STATS * fpstats,
1050 int section_target_bandwitdh,
1054 int num_mbs = cpi->common.MBs;
1055 int target_norm_bits_per_mb;
1057 double section_err = (fpstats->coded_error / fpstats->count);
1058 double err_per_mb = section_err / num_mbs;
1059 double err_correction_factor;
1060 double speed_correction = 1.0;
1061 double clip_iiratio;
1062 double clip_iifactor;
1063 int overhead_bits_per_mb;
1067 FILE *f = fopen("epmp.stt", "a");
1068 fprintf(f, "%10.2f\n", err_per_mb );
1072 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1073 ? (512 * section_target_bandwitdh) / num_mbs
1074 : 512 * (section_target_bandwitdh / num_mbs);
1076 // Estimate of overhead bits per mb
1077 overhead_bits_per_mb = overhead_bits / num_mbs;
1079 // Corrections for higher compression speed settings
1080 // (reduced compression expected)
1081 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1083 if (cpi->oxcf.cpu_used <= 5)
1084 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1086 speed_correction = 1.25;
1089 // II ratio correction factor for clip as a whole
1090 clip_iiratio = cpi->twopass.total_stats.intra_error /
1091 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
1092 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1093 if (clip_iifactor < 0.80)
1094 clip_iifactor = 0.80;
1096 // Try and pick a Q that can encode the content at the given rate.
1097 for (Q = 0; Q < MAXQ; Q++)
1099 int bits_per_mb_at_this_q;
1101 // Error per MB based correction factor
1102 err_correction_factor =
1103 calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q);
1105 bits_per_mb_at_this_q =
1106 vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
1108 bits_per_mb_at_this_q =
1109 (int)( .5 + err_correction_factor *
1112 (double)bits_per_mb_at_this_q);
1114 // Mode and motion overhead
1115 // As Q rises in real encode loop rd code will force overhead down
1116 // We make a crude adjustment for this here as *.98 per Q step.
1117 overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
1119 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1123 // Clip value to range "best allowed to (worst allowed - 1)"
1125 if ( Q >= cpi->worst_quality )
1126 Q = cpi->worst_quality - 1;
1127 if ( Q < cpi->best_quality )
1128 Q = cpi->best_quality;
1133 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
1136 int num_mbs = cpi->common.MBs;
1137 int target_norm_bits_per_mb;
1139 double err_per_mb = section_err / num_mbs;
1140 double err_correction_factor;
1141 double speed_correction = 1.0;
1143 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
1145 // Corrections for higher compression speed settings (reduced compression expected)
1146 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1148 if (cpi->oxcf.cpu_used <= 5)
1149 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1151 speed_correction = 1.25;
1154 // Try and pick a Q that can encode the content at the given rate.
1155 for (Q = 0; Q < MAXQ; Q++)
1157 int bits_per_mb_at_this_q;
1159 // Error per MB based correction factor
1160 err_correction_factor =
1161 calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
1163 bits_per_mb_at_this_q =
1164 (int)( .5 + ( err_correction_factor *
1166 cpi->twopass.est_max_qcorrection_factor *
1167 (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0 ) );
1169 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1176 // Estimate a worst case Q for a KF group
1177 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio)
1180 int num_mbs = cpi->common.MBs;
1181 int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1182 int bits_per_mb_at_this_q;
1184 double err_per_mb = section_err / num_mbs;
1185 double err_correction_factor;
1186 double speed_correction = 1.0;
1187 double current_spend_ratio = 1.0;
1189 double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1190 double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1192 double iiratio_correction_factor = 1.0;
1194 double combined_correction_factor;
1196 // Trap special case where the target is <= 0
1197 if (target_norm_bits_per_mb <= 0)
1200 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
1201 // This is clamped to the range 0.1 to 10.0
1202 if (cpi->long_rolling_target_bits <= 0)
1203 current_spend_ratio = 10.0;
1206 current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
1207 current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
1210 // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
1211 // The idea here is to favour subsampling in the hardest sections vs the easyest.
1212 iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1214 if (iiratio_correction_factor < 0.5)
1215 iiratio_correction_factor = 0.5;
1217 // Corrections for higher compression speed settings (reduced compression expected)
1218 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1220 if (cpi->oxcf.cpu_used <= 5)
1221 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1223 speed_correction = 1.25;
1226 // Combine the various factors calculated above
1227 combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
1229 // Try and pick a Q that should be high enough to encode the content at the given rate.
1230 for (Q = 0; Q < MAXQ; Q++)
1232 // Error per MB based correction factor
1233 err_correction_factor =
1234 calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q);
1236 bits_per_mb_at_this_q =
1237 (int)(.5 + ( err_correction_factor *
1238 combined_correction_factor *
1239 (double)vp8_bits_per_mb[INTER_FRAME][Q]) );
1241 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1245 // If we could not hit the target even at Max Q then estimate what Q would have bee required
1246 while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2)))
1249 bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1255 FILE *f = fopen("estkf_q.stt", "a");
1256 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,
1257 target_norm_bits_per_mb, err_per_mb, err_correction_factor,
1258 current_spend_ratio, group_iiratio, iiratio_correction_factor,
1259 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
1266 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
1268 void vp8_init_second_pass(VP8_COMP *cpi)
1270 FIRSTPASS_STATS this_frame;
1271 FIRSTPASS_STATS *start_pos;
1273 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1275 zero_stats(&cpi->twopass.total_stats);
1276 zero_stats(&cpi->twopass.total_left_stats);
1278 if (!cpi->twopass.stats_in_end)
1281 cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1282 cpi->twopass.total_left_stats = cpi->twopass.total_stats;
1284 // each frame can have a different duration, as the frame rate in the source
1285 // isn't guaranteed to be constant. The frame rate prior to the first frame
1286 // encoded in the second pass is a guess. However the sum duration is not.
1287 // Its calculated based on the actual durations of all frames from the first
1289 vp8_new_frame_rate(cpi, 10000000.0 * cpi->twopass.total_stats.count / cpi->twopass.total_stats.duration);
1291 cpi->output_frame_rate = cpi->frame_rate;
1292 cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1293 cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration * two_pass_min_rate / 10000000.0);
1295 // Calculate a minimum intra value to be used in determining the IIratio
1296 // scores used in the second pass. We have this minimum to make sure
1297 // that clips that are static but "low complexity" in the intra domain
1298 // are still boosted appropriately for KF/GF/ARF
1299 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1300 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1302 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1304 double sum_iiratio = 0.0;
1307 start_pos = cpi->twopass.stats_in; // Note starting "file" position
1309 while (input_stats(cpi, &this_frame) != EOF)
1311 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1312 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1313 sum_iiratio += IIRatio;
1316 cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
1318 // Reset file position
1319 reset_fpf_position(cpi, start_pos);
1322 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1323 // used to allocate bits
1325 start_pos = cpi->twopass.stats_in; // Note starting "file" position
1327 cpi->twopass.modified_error_total = 0.0;
1328 cpi->twopass.modified_error_used = 0.0;
1330 while (input_stats(cpi, &this_frame) != EOF)
1332 cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame);
1334 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1336 reset_fpf_position(cpi, start_pos); // Reset file position
1341 void vp8_end_second_pass(VP8_COMP *cpi)
1345 // This function gives and estimate of how badly we believe
1346 // the prediction quality is decaying from frame to frame.
1347 static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1349 double prediction_decay_rate;
1350 double motion_decay;
1351 double motion_pct = next_frame->pcnt_motion;
1353 // Initial basis is the % mbs inter coded
1354 prediction_decay_rate = next_frame->pcnt_inter;
1356 // High % motion -> somewhat higher decay rate
1357 motion_decay = (1.0 - (motion_pct / 20.0));
1358 if (motion_decay < prediction_decay_rate)
1359 prediction_decay_rate = motion_decay;
1361 // Adjustment to decay rate based on speed of motion
1363 double this_mv_rabs;
1364 double this_mv_cabs;
1365 double distance_factor;
1367 this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1368 this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1370 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
1371 (this_mv_cabs * this_mv_cabs)) / 250.0;
1372 distance_factor = ((distance_factor > 1.0)
1373 ? 0.0 : (1.0 - distance_factor));
1374 if (distance_factor < prediction_decay_rate)
1375 prediction_decay_rate = distance_factor;
1378 return prediction_decay_rate;
1381 // Function to test for a condition where a complex transition is followed
1382 // by a static section. For example in slide shows where there is a fade
1383 // between slides. This is to help with more optimal kf and gf positioning.
1384 static int detect_transition_to_still(
1388 double loop_decay_rate,
1389 double decay_accumulator )
1391 int trans_to_still = 0;
1393 // Break clause to detect very still sections after motion
1394 // For example a static image after a fade or other transition
1395 // instead of a clean scene cut.
1396 if ( (frame_interval > MIN_GF_INTERVAL) &&
1397 (loop_decay_rate >= 0.999) &&
1398 (decay_accumulator < 0.9) )
1401 FIRSTPASS_STATS * position = cpi->twopass.stats_in;
1402 FIRSTPASS_STATS tmp_next_frame;
1405 // Look ahead a few frames to see if static condition
1407 for ( j = 0; j < still_interval; j++ )
1409 if (EOF == input_stats(cpi, &tmp_next_frame))
1412 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1413 if ( decay_rate < 0.999 )
1416 // Reset file position
1417 reset_fpf_position(cpi, position);
1419 // Only if it does do we signal a transition to still
1420 if ( j == still_interval )
1424 return trans_to_still;
1427 // This function detects a flash through the high relative pcnt_second_ref
1428 // score in the frame following a flash frame. The offset passed in should
1430 static int detect_flash( VP8_COMP *cpi, int offset )
1432 FIRSTPASS_STATS next_frame;
1434 int flash_detected = 0;
1436 // Read the frame data.
1437 // The return is 0 (no flash detected) if not a valid frame
1438 if ( read_frame_stats(cpi, &next_frame, offset) != EOF )
1440 // What we are looking for here is a situation where there is a
1441 // brief break in prediction (such as a flash) but subsequent frames
1442 // are reasonably well predicted by an earlier (pre flash) frame.
1443 // The recovery after a flash is indicated by a high pcnt_second_ref
1444 // comapred to pcnt_inter.
1445 if ( (next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
1446 (next_frame.pcnt_second_ref >= 0.5 ) )
1452 FILE *f = fopen("flash.stt", "a");
1453 fprintf(f, "%8.0f %6.2f %6.2f\n",
1455 next_frame.pcnt_inter,
1456 next_frame.pcnt_second_ref);
1462 return flash_detected;
1465 // Update the motion related elements to the GF arf boost calculation
1466 static void accumulate_frame_motion_stats(
1468 FIRSTPASS_STATS * this_frame,
1469 double * this_frame_mv_in_out,
1470 double * mv_in_out_accumulator,
1471 double * abs_mv_in_out_accumulator,
1472 double * mv_ratio_accumulator )
1474 //double this_frame_mv_in_out;
1475 double this_frame_mvr_ratio;
1476 double this_frame_mvc_ratio;
1479 // Accumulate motion stats.
1480 motion_pct = this_frame->pcnt_motion;
1482 // Accumulate Motion In/Out of frame stats
1483 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1484 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1485 *abs_mv_in_out_accumulator +=
1486 fabs(this_frame->mv_in_out_count * motion_pct);
1488 // Accumulate a measure of how uniform (or conversely how random)
1489 // the motion field is. (A ratio of absmv / mv)
1490 if (motion_pct > 0.05)
1492 this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1493 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1495 this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1496 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1498 *mv_ratio_accumulator +=
1499 (this_frame_mvr_ratio < this_frame->mvr_abs)
1500 ? (this_frame_mvr_ratio * motion_pct)
1501 : this_frame->mvr_abs * motion_pct;
1503 *mv_ratio_accumulator +=
1504 (this_frame_mvc_ratio < this_frame->mvc_abs)
1505 ? (this_frame_mvc_ratio * motion_pct)
1506 : this_frame->mvc_abs * motion_pct;
1511 // Calculate a baseline boost number for the current frame.
1512 static double calc_frame_boost(
1514 FIRSTPASS_STATS * this_frame,
1515 double this_frame_mv_in_out )
1519 // Underlying boost factor is based on inter intra error ratio
1520 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1521 frame_boost = (IIFACTOR * this_frame->intra_error /
1522 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1524 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1525 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1527 // Increase boost for frames where new data coming into frame
1528 // (eg zoom out). Slightly reduce boost if there is a net balance
1529 // of motion out of the frame (zoom in).
1530 // The range for this_frame_mv_in_out is -1.0 to +1.0
1531 if (this_frame_mv_in_out > 0.0)
1532 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1533 // In extreme case boost is halved
1535 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1538 if (frame_boost > GF_RMAX)
1539 frame_boost = GF_RMAX;
1545 static int calc_arf_boost(
1553 FIRSTPASS_STATS this_frame;
1556 double boost_score = 0.0;
1557 double mv_ratio_accumulator = 0.0;
1558 double decay_accumulator = 1.0;
1559 double this_frame_mv_in_out = 0.0;
1560 double mv_in_out_accumulator = 0.0;
1561 double abs_mv_in_out_accumulator = 0.0;
1563 int flash_detected = 0;
1565 // Search forward from the proposed arf/next gf position
1566 for ( i = 0; i < f_frames; i++ )
1568 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1571 // Update the motion related elements to the boost calculation
1572 accumulate_frame_motion_stats( cpi, &this_frame,
1573 &this_frame_mv_in_out, &mv_in_out_accumulator,
1574 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1576 // Calculate the baseline boost number for this frame
1577 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1579 // We want to discount the the flash frame itself and the recovery
1580 // frame that follows as both will have poor scores.
1581 flash_detected = detect_flash(cpi, (i+offset)) ||
1582 detect_flash(cpi, (i+offset+1));
1584 // Cumulative effect of prediction quality decay
1585 if ( !flash_detected )
1589 get_prediction_decay_rate(cpi, &this_frame);
1591 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1593 boost_score += (decay_accumulator * r);
1595 // Break out conditions.
1596 if ( (!flash_detected) &&
1597 ((mv_ratio_accumulator > 100.0) ||
1598 (abs_mv_in_out_accumulator > 3.0) ||
1599 (mv_in_out_accumulator < -2.0) ) )
1605 *f_boost = (int)(boost_score * 100.0) >> 4;
1607 // Reset for backward looking loop
1609 mv_ratio_accumulator = 0.0;
1610 decay_accumulator = 1.0;
1611 this_frame_mv_in_out = 0.0;
1612 mv_in_out_accumulator = 0.0;
1613 abs_mv_in_out_accumulator = 0.0;
1615 // Search forward from the proposed arf/next gf position
1616 for ( i = -1; i >= -b_frames; i-- )
1618 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1621 // Update the motion related elements to the boost calculation
1622 accumulate_frame_motion_stats( cpi, &this_frame,
1623 &this_frame_mv_in_out, &mv_in_out_accumulator,
1624 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1626 // Calculate the baseline boost number for this frame
1627 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1629 // We want to discount the the flash frame itself and the recovery
1630 // frame that follows as both will have poor scores.
1631 flash_detected = detect_flash(cpi, (i+offset)) ||
1632 detect_flash(cpi, (i+offset+1));
1634 // Cumulative effect of prediction quality decay
1635 if ( !flash_detected )
1639 get_prediction_decay_rate(cpi, &this_frame);
1641 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1644 boost_score += (decay_accumulator * r);
1646 // Break out conditions.
1647 if ( (!flash_detected) &&
1648 ((mv_ratio_accumulator > 100.0) ||
1649 (abs_mv_in_out_accumulator > 3.0) ||
1650 (mv_in_out_accumulator < -2.0) ) )
1655 *b_boost = (int)(boost_score * 100.0) >> 4;
1657 return (*f_boost + *b_boost);
1661 // Analyse and define a gf/arf group .
1662 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1664 FIRSTPASS_STATS next_frame;
1665 FIRSTPASS_STATS *start_pos;
1668 double boost_score = 0.0;
1669 double old_boost_score = 0.0;
1670 double gf_group_err = 0.0;
1671 double gf_first_frame_err = 0.0;
1672 double mod_frame_err = 0.0;
1674 double mv_ratio_accumulator = 0.0;
1675 double decay_accumulator = 1.0;
1677 double loop_decay_rate = 1.00; // Starting decay rate
1679 double this_frame_mv_in_out = 0.0;
1680 double mv_in_out_accumulator = 0.0;
1681 double abs_mv_in_out_accumulator = 0.0;
1682 double mod_err_per_mb_accumulator = 0.0;
1684 int max_bits = frame_max_bits(cpi); // Max for a single frame
1686 unsigned int allow_alt_ref =
1687 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1694 cpi->twopass.gf_group_bits = 0;
1695 cpi->twopass.gf_decay_rate = 0;
1697 vp8_clear_system_state(); //__asm emms;
1699 start_pos = cpi->twopass.stats_in;
1701 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1703 // Load stats for the current frame.
1704 mod_frame_err = calculate_modified_err(cpi, this_frame);
1706 // Note the error of the frame at the start of the group (this will be
1707 // the GF frame error if we code a normal gf
1708 gf_first_frame_err = mod_frame_err;
1710 // Special treatment if the current frame is a key frame (which is also
1711 // a gf). If it is then its error score (and hence bit allocation) need
1712 // to be subtracted out from the calculation for the GF group
1713 if (cpi->common.frame_type == KEY_FRAME)
1714 gf_group_err -= gf_first_frame_err;
1716 // Scan forward to try and work out how many frames the next gf group
1717 // should contain and what level of boost is appropriate for the GF
1718 // or ARF that will be coded with the group
1721 while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1722 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1723 (i < cpi->twopass.frames_to_key))
1725 i++; // Increment the loop counter
1727 // Accumulate error score of frames in this gf group
1728 mod_frame_err = calculate_modified_err(cpi, this_frame);
1730 gf_group_err += mod_frame_err;
1732 mod_err_per_mb_accumulator +=
1733 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1735 if (EOF == input_stats(cpi, &next_frame))
1738 // Test for the case where there is a brief flash but the prediction
1739 // quality back to an earlier frame is then restored.
1740 flash_detected = detect_flash(cpi, 0);
1742 // Update the motion related elements to the boost calculation
1743 accumulate_frame_motion_stats( cpi, &next_frame,
1744 &this_frame_mv_in_out, &mv_in_out_accumulator,
1745 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1747 // Calculate a baseline boost number for this frame
1748 r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out );
1750 // Cumulative effect of prediction quality decay
1751 if ( !flash_detected )
1753 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1754 decay_accumulator = decay_accumulator * loop_decay_rate;
1756 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1758 boost_score += (decay_accumulator * r);
1760 // Break clause to detect very still sections after motion
1761 // For example a staic image after a fade or other transition.
1762 if ( detect_transition_to_still( cpi, i, 5,
1764 decay_accumulator ) )
1767 boost_score = old_boost_score;
1771 // Break out conditions.
1773 // Break at cpi->max_gf_interval unless almost totally static
1774 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1776 // Dont break out with a very short interval
1777 (i > MIN_GF_INTERVAL) &&
1778 // Dont break out very close to a key frame
1779 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1780 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1781 (!flash_detected) &&
1782 ((mv_ratio_accumulator > 100.0) ||
1783 (abs_mv_in_out_accumulator > 3.0) ||
1784 (mv_in_out_accumulator < -2.0) ||
1785 ((boost_score - old_boost_score) < 2.0))
1788 boost_score = old_boost_score;
1792 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1794 old_boost_score = boost_score;
1797 cpi->twopass.gf_decay_rate =
1798 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1800 // When using CBR apply additional buffer related upper limits
1801 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1805 // For cbr apply buffer related limits
1806 if (cpi->drop_frames_allowed)
1808 int df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1809 (cpi->oxcf.optimal_buffer_level / 100);
1811 if (cpi->buffer_level > df_buffer_level)
1812 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1816 else if (cpi->buffer_level > 0)
1818 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1825 if (boost_score > max_boost)
1826 boost_score = max_boost;
1829 // Dont allow conventional gf too near the next kf
1830 if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)
1832 while (i < cpi->twopass.frames_to_key)
1836 if (EOF == input_stats(cpi, this_frame))
1839 if (i < cpi->twopass.frames_to_key)
1841 mod_frame_err = calculate_modified_err(cpi, this_frame);
1842 gf_group_err += mod_frame_err;
1847 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1850 // Alterrnative boost calculation for alt ref
1851 alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost );
1854 // Should we use the alternate refernce frame
1855 if (allow_alt_ref &&
1856 (i >= MIN_GF_INTERVAL) &&
1857 // dont use ARF very near next kf
1858 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1860 ((next_frame.pcnt_inter > 0.75) ||
1861 (next_frame.pcnt_second_ref > 0.5)) &&
1862 ((mv_in_out_accumulator / (double)i > -0.2) ||
1863 (mv_in_out_accumulator > -2.0)) &&
1867 (next_frame.pcnt_inter > 0.75) &&
1868 ((mv_in_out_accumulator / (double)i > -0.2) ||
1869 (mv_in_out_accumulator > -2.0)) &&
1870 (cpi->gfu_boost > 100) &&
1871 (cpi->twopass.gf_decay_rate <=
1872 (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) )
1876 int allocation_chunks;
1877 int Q = (cpi->oxcf.fixed_q < 0)
1878 ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1880 int arf_frame_bits = 0;
1884 cpi->gfu_boost = alt_boost;
1887 // Estimate the bits to be allocated to the group as a whole
1888 if ((cpi->twopass.kf_group_bits > 0) &&
1889 (cpi->twopass.kf_group_error_left > 0))
1891 group_bits = (int)((double)cpi->twopass.kf_group_bits *
1892 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1897 // Boost for arf frame
1899 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1901 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1905 // Set max and minimum boost and hence minimum allocation
1906 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1907 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1908 else if (Boost < 125)
1911 allocation_chunks = (i * 100) + Boost;
1913 // Normalize Altboost and allocations chunck down to prevent overflow
1914 while (Boost > 1000)
1917 allocation_chunks /= 2;
1920 // Calculate the number of bits to be spent on the arf based on the
1922 arf_frame_bits = (int)((double)Boost * (group_bits /
1923 (double)allocation_chunks));
1925 // Estimate if there are enough bits available to make worthwhile use
1927 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
1929 // Only use an arf if it is likely we will be able to code
1930 // it at a lower Q than the surrounding frames.
1931 if (tmp_q < cpi->worst_quality)
1934 int frames_after_arf;
1935 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1936 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1938 cpi->source_alt_ref_pending = 1;
1940 // For alt ref frames the error score for the end frame of the
1941 // group (the alt ref frame) should not contribute to the group
1942 // total and hence the number of bit allocated to the group.
1943 // Rather it forms part of the next group (it is the GF at the
1944 // start of the next group)
1945 // gf_group_err -= mod_frame_err;
1947 // For alt ref frames alt ref frame is technically part of the
1948 // GF frame for the next group but we always base the error
1949 // calculation and bit allocation on the current group of frames.
1951 // Set the interval till the next gf or arf.
1952 // For ARFs this is the number of frames to be coded before the
1953 // future frame that is coded as an ARF.
1954 // The future frame itself is part of the next group
1955 cpi->baseline_gf_interval = i;
1957 // Define the arnr filter width for this group of frames:
1958 // We only filter frames that lie within a distance of half
1959 // the GF interval from the ARF frame. We also have to trap
1960 // cases where the filter extends beyond the end of clip.
1961 // Note: this_frame->frame has been updated in the loop
1962 // so it now points at the ARF frame.
1963 half_gf_int = cpi->baseline_gf_interval >> 1;
1964 frames_after_arf = cpi->twopass.total_stats.count -
1965 this_frame->frame - 1;
1967 switch (cpi->oxcf.arnr_type)
1969 case 1: // Backward filter
1971 if (frames_bwd > half_gf_int)
1972 frames_bwd = half_gf_int;
1975 case 2: // Forward filter
1976 if (frames_fwd > half_gf_int)
1977 frames_fwd = half_gf_int;
1978 if (frames_fwd > frames_after_arf)
1979 frames_fwd = frames_after_arf;
1983 case 3: // Centered filter
1986 if (frames_fwd > frames_after_arf)
1987 frames_fwd = frames_after_arf;
1988 if (frames_fwd > half_gf_int)
1989 frames_fwd = half_gf_int;
1991 frames_bwd = frames_fwd;
1993 // For even length filter there is one more frame backward
1994 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1995 if (frames_bwd < half_gf_int)
1996 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
2000 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
2004 cpi->source_alt_ref_pending = 0;
2005 cpi->baseline_gf_interval = i;
2010 cpi->source_alt_ref_pending = 0;
2011 cpi->baseline_gf_interval = i;
2014 // Now decide how many bits should be allocated to the GF group as a
2015 // proportion of those remaining in the kf group.
2016 // The final key frame group in the clip is treated as a special case
2017 // where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
2018 // This is also important for short clips where there may only be one
2020 if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count -
2021 cpi->common.current_video_frame))
2023 cpi->twopass.kf_group_bits =
2024 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
2027 // Calculate the bits to be allocated to the group as a whole
2028 if ((cpi->twopass.kf_group_bits > 0) &&
2029 (cpi->twopass.kf_group_error_left > 0))
2031 cpi->twopass.gf_group_bits =
2032 (int)((double)cpi->twopass.kf_group_bits *
2033 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
2036 cpi->twopass.gf_group_bits = 0;
2038 cpi->twopass.gf_group_bits =
2039 (cpi->twopass.gf_group_bits < 0)
2041 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
2042 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
2044 // Clip cpi->twopass.gf_group_bits based on user supplied data rate
2045 // variability limit (cpi->oxcf.two_pass_vbrmax_section)
2046 if (cpi->twopass.gf_group_bits > max_bits * cpi->baseline_gf_interval)
2047 cpi->twopass.gf_group_bits = max_bits * cpi->baseline_gf_interval;
2049 // Reset the file position
2050 reset_fpf_position(cpi, start_pos);
2052 // Update the record of error used so far (only done once per gf group)
2053 cpi->twopass.modified_error_used += gf_group_err;
2055 // Assign bits to the arf or gf.
2056 for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) {
2058 int allocation_chunks;
2059 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
2063 if (cpi->source_alt_ref_pending && i == 0)
2066 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
2068 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
2070 Boost += (cpi->baseline_gf_interval * 50);
2072 // Set max and minimum boost and hence minimum allocation
2073 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
2074 Boost = ((cpi->baseline_gf_interval + 1) * 200);
2075 else if (Boost < 125)
2079 ((cpi->baseline_gf_interval + 1) * 100) + Boost;
2081 // Else for standard golden frames
2084 // boost based on inter / intra ratio of subsequent frames
2085 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
2087 // Set max and minimum boost and hence minimum allocation
2088 if (Boost > (cpi->baseline_gf_interval * 150))
2089 Boost = (cpi->baseline_gf_interval * 150);
2090 else if (Boost < 125)
2094 (cpi->baseline_gf_interval * 100) + (Boost - 100);
2097 // Normalize Altboost and allocations chunck down to prevent overflow
2098 while (Boost > 1000)
2101 allocation_chunks /= 2;
2104 // Calculate the number of bits to be spent on the gf or arf based on
2106 gf_bits = (int)((double)Boost *
2107 (cpi->twopass.gf_group_bits /
2108 (double)allocation_chunks));
2110 // If the frame that is to be boosted is simpler than the average for
2111 // the gf/arf group then use an alternative calculation
2112 // based on the error score of the frame itself
2113 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
2115 double alt_gf_grp_bits;
2119 (double)cpi->twopass.kf_group_bits *
2120 (mod_frame_err * (double)cpi->baseline_gf_interval) /
2121 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
2123 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
2124 (double)allocation_chunks));
2126 if (gf_bits > alt_gf_bits)
2128 gf_bits = alt_gf_bits;
2131 // Else if it is harder than other frames in the group make sure it at
2132 // least receives an allocation in keeping with its relative error
2133 // score, otherwise it may be worse off than an "un-boosted" frame
2137 (int)((double)cpi->twopass.kf_group_bits *
2139 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
2141 if (alt_gf_bits > gf_bits)
2143 gf_bits = alt_gf_bits;
2147 // Apply an additional limit for CBR
2148 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2150 if (cpi->twopass.gf_bits > (cpi->buffer_level >> 1))
2151 cpi->twopass.gf_bits = cpi->buffer_level >> 1;
2154 // Dont allow a negative value for gf_bits
2158 gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame
2162 cpi->twopass.gf_bits = gf_bits;
2164 if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)))
2166 cpi->per_frame_bandwidth = gf_bits; // Per frame bit target for this frame
2171 // Adjust KF group bits and error remainin
2172 cpi->twopass.kf_group_error_left -= gf_group_err;
2173 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
2175 if (cpi->twopass.kf_group_bits < 0)
2176 cpi->twopass.kf_group_bits = 0;
2178 // Note the error score left in the remaining frames of the group.
2179 // 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)
2180 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
2181 cpi->twopass.gf_group_error_left = gf_group_err - gf_first_frame_err;
2183 cpi->twopass.gf_group_error_left = gf_group_err;
2185 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
2187 if (cpi->twopass.gf_group_bits < 0)
2188 cpi->twopass.gf_group_bits = 0;
2190 // This condition could fail if there are two kfs very close together
2191 // despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the
2192 // calculation of cpi->twopass.alt_extra_bits.
2193 if ( cpi->baseline_gf_interval >= 3 )
2196 int boost = (cpi->source_alt_ref_pending)
2197 ? b_boost : cpi->gfu_boost;
2199 int boost = cpi->gfu_boost;
2205 pct_extra = (boost - 100) / 50;
2206 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2208 cpi->twopass.alt_extra_bits =
2209 (cpi->twopass.gf_group_bits * pct_extra) / 100;
2210 cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
2211 cpi->twopass.alt_extra_bits /=
2212 ((cpi->baseline_gf_interval-1)>>1);
2215 cpi->twopass.alt_extra_bits = 0;
2218 cpi->twopass.alt_extra_bits = 0;
2221 // Adjustments based on a measure of complexity of the section
2222 if (cpi->common.frame_type != KEY_FRAME)
2224 FIRSTPASS_STATS sectionstats;
2227 zero_stats(§ionstats);
2228 reset_fpf_position(cpi, start_pos);
2230 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
2232 input_stats(cpi, &next_frame);
2233 accumulate_stats(§ionstats, &next_frame);
2236 avg_stats(§ionstats);
2238 cpi->twopass.section_intra_rating =
2239 sectionstats.intra_error /
2240 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2242 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2243 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2245 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2247 if (cpi->twopass.section_max_qfactor < 0.80)
2248 cpi->twopass.section_max_qfactor = 0.80;
2252 // cpi->twopass.section_max_qfactor = 1.0;
2254 reset_fpf_position(cpi, start_pos);
2258 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2259 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2261 int target_frame_size; // gf_group_error_left
2263 double modified_err;
2264 double err_fraction; // What portion of the remaining GF group error is used by this frame
2266 int max_bits = frame_max_bits(cpi); // Max for a single frame
2268 // Calculate modified prediction error used in bit allocation
2269 modified_err = calculate_modified_err(cpi, this_frame);
2271 if (cpi->twopass.gf_group_error_left > 0)
2272 err_fraction = modified_err / cpi->twopass.gf_group_error_left; // What portion of the remaining GF group error is used by this frame
2276 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it?
2278 // Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at the top end.
2279 if (target_frame_size < 0)
2280 target_frame_size = 0;
2283 if (target_frame_size > max_bits)
2284 target_frame_size = max_bits;
2286 if (target_frame_size > cpi->twopass.gf_group_bits)
2287 target_frame_size = cpi->twopass.gf_group_bits;
2290 cpi->twopass.gf_group_error_left -= modified_err; // Adjust error remaining
2291 cpi->twopass.gf_group_bits -= target_frame_size; // Adjust bits remaining
2293 if (cpi->twopass.gf_group_bits < 0)
2294 cpi->twopass.gf_group_bits = 0;
2296 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame.
2298 // Every other frame gets a few extra bits
2299 if ( (cpi->common.frames_since_golden & 0x01) &&
2300 (cpi->frames_till_gf_update_due > 0) )
2302 target_frame_size += cpi->twopass.alt_extra_bits;
2305 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
2308 void vp8_second_pass(VP8_COMP *cpi)
2311 int frames_left = (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
2313 FIRSTPASS_STATS this_frame = {0};
2314 FIRSTPASS_STATS this_frame_copy;
2316 double this_frame_intra_error;
2317 double this_frame_coded_error;
2321 if (!cpi->twopass.stats_in)
2326 vp8_clear_system_state();
2328 if (EOF == input_stats(cpi, &this_frame))
2331 this_frame_intra_error = this_frame.intra_error;
2332 this_frame_coded_error = this_frame.coded_error;
2334 // keyframe and section processing !
2335 if (cpi->twopass.frames_to_key == 0)
2337 // Define next KF group and assign bits to it
2338 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2339 find_next_key_frame(cpi, &this_frame_copy);
2341 // 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
2342 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2343 // This is temporary code till we decide what should really happen in this case.
2344 if (cpi->oxcf.error_resilient_mode)
2346 cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
2347 cpi->twopass.gf_group_error_left = cpi->twopass.kf_group_error_left;
2348 cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
2349 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2350 cpi->source_alt_ref_pending = 0;
2355 // Is this a GF / ARF (Note that a KF is always also a GF)
2356 if (cpi->frames_till_gf_update_due == 0)
2358 // Define next gf group and assign bits to it
2359 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2360 define_gf_group(cpi, &this_frame_copy);
2362 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
2363 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
2364 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
2365 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2367 // Assign a standard frames worth of bits from those allocated to the GF group
2368 int bak = cpi->per_frame_bandwidth;
2369 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2370 assign_std_frame_bits(cpi, &this_frame_copy);
2371 cpi->per_frame_bandwidth = bak;
2375 // Otherwise this is an ordinary frame
2378 // 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
2379 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2380 // This is temporary code till we decide what should really happen in this case.
2381 if (cpi->oxcf.error_resilient_mode)
2383 cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
2385 if (cpi->common.frame_type != KEY_FRAME)
2387 // Assign bits from those allocated to the GF group
2388 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2389 assign_std_frame_bits(cpi, &this_frame_copy);
2394 // Assign bits from those allocated to the GF group
2395 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2396 assign_std_frame_bits(cpi, &this_frame_copy);
2400 // Keep a globally available copy of this and the next frame's iiratio.
2401 cpi->twopass.this_iiratio = this_frame_intra_error /
2402 DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
2404 FIRSTPASS_STATS next_frame;
2405 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2407 cpi->twopass.next_iiratio = next_frame.intra_error /
2408 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
2412 // Set nominal per second bandwidth for this frame
2413 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
2414 if (cpi->target_bandwidth < 0)
2415 cpi->target_bandwidth = 0;
2418 // Account for mv, mode and other overheads.
2419 overhead_bits = estimate_modemvcost(
2420 cpi, &cpi->twopass.total_left_stats );
2422 // Special case code for first frame.
2423 if (cpi->common.current_video_frame == 0)
2425 cpi->twopass.est_max_qcorrection_factor = 1.0;
2427 // Set a cq_level in constrained quality mode.
2428 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2434 &cpi->twopass.total_left_stats,
2435 (int)(cpi->twopass.bits_left / frames_left),
2438 cpi->cq_target_quality = cpi->oxcf.cq_level;
2439 if ( est_cq > cpi->cq_target_quality )
2440 cpi->cq_target_quality = est_cq;
2443 // guess at maxq needed in 2nd pass
2444 cpi->twopass.maxq_max_limit = cpi->worst_quality;
2445 cpi->twopass.maxq_min_limit = cpi->best_quality;
2447 tmp_q = estimate_max_q(
2449 &cpi->twopass.total_left_stats,
2450 (int)(cpi->twopass.bits_left / frames_left),
2453 // Limit the maxq value returned subsequently.
2454 // This increases the risk of overspend or underspend if the initial
2455 // estimate for the clip is bad, but helps prevent excessive
2456 // variation in Q, especially near the end of a clip
2457 // where for example a small overspend may cause Q to crash
2458 cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2459 ? (tmp_q + 32) : cpi->worst_quality;
2460 cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2461 ? (tmp_q - 32) : cpi->best_quality;
2463 cpi->active_worst_quality = tmp_q;
2464 cpi->ni_av_qi = tmp_q;
2467 // The last few frames of a clip almost always have to few or too many
2468 // bits and for the sake of over exact rate control we dont want to make
2469 // radical adjustments to the allowed quantizer range just to use up a
2470 // few surplus bits or get beneath the target rate.
2471 else if ( (cpi->common.current_video_frame <
2472 (((unsigned int)cpi->twopass.total_stats.count * 255)>>8)) &&
2473 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2474 (unsigned int)cpi->twopass.total_stats.count) )
2476 if (frames_left < 1)
2479 tmp_q = estimate_max_q(
2481 &cpi->twopass.total_left_stats,
2482 (int)(cpi->twopass.bits_left / frames_left),
2485 // Move active_worst_quality but in a damped way
2486 if (tmp_q > cpi->active_worst_quality)
2487 cpi->active_worst_quality ++;
2488 else if (tmp_q < cpi->active_worst_quality)
2489 cpi->active_worst_quality --;
2491 cpi->active_worst_quality =
2492 ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2495 cpi->twopass.frames_to_key --;
2497 // Update the total stats remaining sturcture
2498 subtract_stats(&cpi->twopass.total_left_stats, &this_frame );
2502 static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2504 int is_viable_kf = 0;
2506 // Does the frame satisfy the primary criteria of a key frame
2507 // If so, then examine how well it predicts subsequent frames
2508 if ((this_frame->pcnt_second_ref < 0.10) &&
2509 (next_frame->pcnt_second_ref < 0.10) &&
2510 ((this_frame->pcnt_inter < 0.05) ||
2512 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2513 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2514 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2515 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2516 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2523 FIRSTPASS_STATS *start_pos;
2525 FIRSTPASS_STATS local_next_frame;
2527 double boost_score = 0.0;
2528 double old_boost_score = 0.0;
2529 double decay_accumulator = 1.0;
2530 double next_iiratio;
2532 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2534 // Note the starting file position so we can reset to it
2535 start_pos = cpi->twopass.stats_in;
2537 // Examine how well the key frame predicts subsequent frames
2538 for (i = 0 ; i < 16; i++)
2540 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
2542 if (next_iiratio > RMAX)
2543 next_iiratio = RMAX;
2545 // Cumulative effect of decay in prediction quality
2546 if (local_next_frame.pcnt_inter > 0.85)
2547 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2549 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2551 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2553 // Keep a running total
2554 boost_score += (decay_accumulator * next_iiratio);
2556 // Test various breakout clauses
2557 if ((local_next_frame.pcnt_inter < 0.05) ||
2558 (next_iiratio < 1.5) ||
2559 (((local_next_frame.pcnt_inter -
2560 local_next_frame.pcnt_neutral) < 0.20) &&
2561 (next_iiratio < 3.0)) ||
2562 ((boost_score - old_boost_score) < 0.5) ||
2563 (local_next_frame.intra_error < 200)
2569 old_boost_score = boost_score;
2571 // Get the next frame details
2572 if (EOF == input_stats(cpi, &local_next_frame))
2576 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2577 if (boost_score > 5.0 && (i > 3))
2581 // Reset the file position
2582 reset_fpf_position(cpi, start_pos);
2588 return is_viable_kf;
2590 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2593 FIRSTPASS_STATS last_frame;
2594 FIRSTPASS_STATS first_frame;
2595 FIRSTPASS_STATS next_frame;
2596 FIRSTPASS_STATS *start_position;
2598 double decay_accumulator = 1.0;
2599 double boost_score = 0;
2600 double old_boost_score = 0.0;
2601 double loop_decay_rate;
2603 double kf_mod_err = 0.0;
2604 double kf_group_err = 0.0;
2605 double kf_group_intra_err = 0.0;
2606 double kf_group_coded_err = 0.0;
2607 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2609 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2611 vp8_clear_system_state(); //__asm emms;
2612 start_position = cpi->twopass.stats_in;
2614 cpi->common.frame_type = KEY_FRAME;
2616 // is this a forced key frame by interval
2617 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2619 // Clear the alt ref active flag as this can never be active on a key frame
2620 cpi->source_alt_ref_active = 0;
2622 // Kf is always a gf so clear frames till next gf counter
2623 cpi->frames_till_gf_update_due = 0;
2625 cpi->twopass.frames_to_key = 1;
2627 // Take a copy of the initial frame details
2628 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2630 cpi->twopass.kf_group_bits = 0; // Total bits avaialable to kf group
2631 cpi->twopass.kf_group_error_left = 0; // Group modified error score.
2633 kf_mod_err = calculate_modified_err(cpi, this_frame);
2635 // find the next keyframe
2637 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end)
2639 // Accumulate kf group error
2640 kf_group_err += calculate_modified_err(cpi, this_frame);
2642 // These figures keep intra and coded error counts for all frames including key frames in the group.
2643 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2644 kf_group_intra_err += this_frame->intra_error;
2645 kf_group_coded_err += this_frame->coded_error;
2647 // load a the next frame's stats
2648 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2649 input_stats(cpi, this_frame);
2651 // Provided that we are not at the end of the file...
2652 if (cpi->oxcf.auto_key
2653 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2655 // Normal scene cut check
2656 if ( ( i >= MIN_GF_INTERVAL ) &&
2657 test_candidate_kf(cpi, &last_frame, this_frame, &next_frame) )
2662 // How fast is prediction quality decaying
2663 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2665 // We want to know something about the recent past... rather than
2666 // as used elsewhere where we are concened with decay in prediction
2667 // quality since the last GF or KF.
2668 recent_loop_decay[i%8] = loop_decay_rate;
2669 decay_accumulator = 1.0;
2670 for (j = 0; j < 8; j++)
2672 decay_accumulator = decay_accumulator * recent_loop_decay[j];
2675 // Special check for transition or high motion followed by a
2676 // to a static scene.
2677 if ( detect_transition_to_still( cpi, i,
2678 (cpi->key_frame_frequency-i),
2680 decay_accumulator ) )
2686 // Step on to the next frame
2687 cpi->twopass.frames_to_key ++;
2689 // If we don't have a real key frame within the next two
2690 // forcekeyframeevery intervals then break out of the loop.
2691 if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2694 cpi->twopass.frames_to_key ++;
2699 // If there is a max kf interval set by the user we must obey it.
2700 // We already breakout of the loop above at 2x max.
2701 // This code centers the extra kf if the actual natural
2702 // interval is between 1x and 2x
2703 if (cpi->oxcf.auto_key
2704 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency )
2706 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2707 FIRSTPASS_STATS tmp_frame;
2709 cpi->twopass.frames_to_key /= 2;
2711 // Copy first frame details
2712 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2714 // Reset to the start of the group
2715 reset_fpf_position(cpi, start_position);
2718 kf_group_intra_err = 0;
2719 kf_group_coded_err = 0;
2721 // Rescan to get the correct error data for the forced kf group
2722 for( i = 0; i < cpi->twopass.frames_to_key; i++ )
2724 // Accumulate kf group errors
2725 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2726 kf_group_intra_err += tmp_frame.intra_error;
2727 kf_group_coded_err += tmp_frame.coded_error;
2729 // Load a the next frame's stats
2730 input_stats(cpi, &tmp_frame);
2733 // Reset to the start of the group
2734 reset_fpf_position(cpi, current_pos);
2736 cpi->next_key_frame_forced = 1;
2739 cpi->next_key_frame_forced = 0;
2741 // Special case for the last frame of the file
2742 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
2744 // Accumulate kf group error
2745 kf_group_err += calculate_modified_err(cpi, this_frame);
2747 // These figures keep intra and coded error counts for all frames including key frames in the group.
2748 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2749 kf_group_intra_err += this_frame->intra_error;
2750 kf_group_coded_err += this_frame->coded_error;
2753 // Calculate the number of bits that should be assigned to the kf group.
2754 if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0))
2756 // Max for a single normal frame (not key frame)
2757 int max_bits = frame_max_bits(cpi);
2759 // Maximum bits for the kf group
2760 int64_t max_grp_bits;
2762 // Default allocation based on bits left and relative
2763 // complexity of the section
2764 cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left *
2766 cpi->twopass.modified_error_left ));
2768 // Clip based on maximum per frame rate defined by the user.
2769 max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2770 if (cpi->twopass.kf_group_bits > max_grp_bits)
2771 cpi->twopass.kf_group_bits = max_grp_bits;
2773 // Additional special case for CBR if buffer is getting full.
2774 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2776 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2777 int buffer_lvl = cpi->buffer_level;
2779 // If the buffer is near or above the optimal and this kf group is
2780 // not being allocated much then increase the allocation a bit.
2781 if (buffer_lvl >= opt_buffer_lvl)
2783 int high_water_mark = (opt_buffer_lvl +
2784 cpi->oxcf.maximum_buffer_size) >> 1;
2786 int64_t av_group_bits;
2788 // Av bits per frame * number of frames
2789 av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
2790 (int64_t)cpi->twopass.frames_to_key;
2792 // We are at or above the maximum.
2793 if (cpi->buffer_level >= high_water_mark)
2795 int64_t min_group_bits;
2797 min_group_bits = av_group_bits +
2798 (int64_t)(buffer_lvl -
2801 if (cpi->twopass.kf_group_bits < min_group_bits)
2802 cpi->twopass.kf_group_bits = min_group_bits;
2804 // We are above optimal but below the maximum
2805 else if (cpi->twopass.kf_group_bits < av_group_bits)
2807 int64_t bits_below_av = av_group_bits -
2808 cpi->twopass.kf_group_bits;
2810 cpi->twopass.kf_group_bits +=
2811 (int64_t)((double)bits_below_av *
2812 (double)(buffer_lvl - opt_buffer_lvl) /
2813 (double)(high_water_mark - opt_buffer_lvl));
2819 cpi->twopass.kf_group_bits = 0;
2821 // Reset the first pass file position
2822 reset_fpf_position(cpi, start_position);
2824 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2825 decay_accumulator = 1.0;
2827 loop_decay_rate = 1.00; // Starting decay rate
2829 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2833 if (EOF == input_stats(cpi, &next_frame))
2836 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
2837 r = (IIKFACTOR2 * next_frame.intra_error /
2838 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2840 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2841 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2846 // How fast is prediction quality decaying
2847 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2849 decay_accumulator = decay_accumulator * loop_decay_rate;
2850 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2852 boost_score += (decay_accumulator * r);
2854 if ((i > MIN_GF_INTERVAL) &&
2855 ((boost_score - old_boost_score) < 1.0))
2860 old_boost_score = boost_score;
2865 FIRSTPASS_STATS sectionstats;
2868 zero_stats(§ionstats);
2869 reset_fpf_position(cpi, start_position);
2871 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2873 input_stats(cpi, &next_frame);
2874 accumulate_stats(§ionstats, &next_frame);
2877 avg_stats(§ionstats);
2879 cpi->twopass.section_intra_rating =
2880 sectionstats.intra_error
2881 / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2883 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2884 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2886 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2888 if (cpi->twopass.section_max_qfactor < 0.80)
2889 cpi->twopass.section_max_qfactor = 0.80;
2893 // cpi->twopass.section_max_qfactor = 1.0;
2896 // When using CBR apply additional buffer fullness related upper limits
2897 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2901 if (cpi->drop_frames_allowed)
2903 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
2905 if (cpi->buffer_level > df_buffer_level)
2906 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2910 else if (cpi->buffer_level > 0)
2912 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2919 if (boost_score > max_boost)
2920 boost_score = max_boost;
2923 // Reset the first pass file position
2924 reset_fpf_position(cpi, start_position);
2926 // Work out how many bits to allocate for the key frame itself
2929 int kf_boost = boost_score;
2930 int allocation_chunks;
2931 int Counter = cpi->twopass.frames_to_key;
2933 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2934 // Min boost based on kf interval
2937 while ((kf_boost < 48) && (Counter > 0))
2947 kf_boost += ((Counter + 1) >> 1);
2949 if (kf_boost > 48) kf_boost = 48;
2952 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2953 if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
2954 kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2955 else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
2956 kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2958 kf_boost = (int)((double)kf_boost * 100.0) >> 4; // Scale 16 to 100
2960 // Adjustment to boost based on recent average q
2961 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2963 if (kf_boost < 250) // Min KF boost
2966 // We do three calculations for kf size.
2967 // The first is based on the error score for the whole kf group.
2968 // The second (optionaly) on the key frames own error if this is
2969 // smaller than the average for the group.
2970 // The final one insures that the frame receives at least the
2971 // allocation it would have received based on its own error score vs
2972 // the error score remaining
2973 // Special case if the sequence appears almost totaly static
2974 // as measured by the decay accumulator. In this case we want to
2975 // spend almost all of the bits on the key frame.
2976 // cpi->twopass.frames_to_key-1 because key frame itself is taken
2977 // care of by kf_boost.
2978 if ( decay_accumulator >= 0.99 )
2981 ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
2986 ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
2989 // Normalize Altboost and allocations chunck down to prevent overflow
2990 while (kf_boost > 1000)
2993 allocation_chunks /= 2;
2996 cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
2998 // Calculate the number of bits to be spent on the key frame
2999 cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
3001 // Apply an additional limit for CBR
3002 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3004 if (cpi->twopass.kf_bits > ((3 * cpi->buffer_level) >> 2))
3005 cpi->twopass.kf_bits = (3 * cpi->buffer_level) >> 2;
3008 // If the key frame is actually easier than the average for the
3009 // kf group (which does sometimes happen... eg a blank intro frame)
3010 // Then use an alternate calculation based on the kf error score
3011 // which should give a smaller key frame.
3012 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key)
3014 double alt_kf_grp_bits =
3015 ((double)cpi->twopass.bits_left *
3016 (kf_mod_err * (double)cpi->twopass.frames_to_key) /
3017 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
3019 alt_kf_bits = (int)((double)kf_boost *
3020 (alt_kf_grp_bits / (double)allocation_chunks));
3022 if (cpi->twopass.kf_bits > alt_kf_bits)
3024 cpi->twopass.kf_bits = alt_kf_bits;
3027 // Else if it is much harder than other frames in the group make sure
3028 // it at least receives an allocation in keeping with its relative
3033 (int)((double)cpi->twopass.bits_left *
3035 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
3037 if (alt_kf_bits > cpi->twopass.kf_bits)
3039 cpi->twopass.kf_bits = alt_kf_bits;
3043 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
3044 cpi->twopass.kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance
3046 cpi->per_frame_bandwidth = cpi->twopass.kf_bits; // Peer frame bit target for this frame
3047 cpi->target_bandwidth = cpi->twopass.kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate
3050 // Note the total error score of the kf group minus the key frame itself
3051 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
3053 // Adjust the count of total modified error left.
3054 // The count of bits left is adjusted elsewhere based on real coded frame sizes
3055 cpi->twopass.modified_error_left -= kf_group_err;
3057 if (cpi->oxcf.allow_spatial_resampling)
3059 int resample_trigger = 0;
3060 int last_kf_resampled = 0;
3064 int new_width = cpi->oxcf.Width;
3065 int new_height = cpi->oxcf.Height;
3067 int projected_buffer_level = cpi->buffer_level;
3070 double projected_bits_perframe;
3071 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
3072 double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
3073 double bits_per_frame;
3074 double av_bits_per_frame;
3075 double effective_size_ratio;
3077 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
3078 last_kf_resampled = 1;
3080 // Set back to unscaled by defaults
3081 cpi->common.horiz_scale = NORMAL;
3082 cpi->common.vert_scale = NORMAL;
3084 // Calculate Average bits per frame.
3085 //av_bits_per_frame = cpi->twopass.bits_left/(double)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
3086 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->frame_rate);
3087 //if ( av_bits_per_frame < 0.0 )
3088 // av_bits_per_frame = 0.0
3090 // CBR... Use the clip average as the target for deciding resample
3091 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3093 bits_per_frame = av_bits_per_frame;
3096 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
3097 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
3100 bits_per_frame = cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key; // This accounts for how hard the section is...
3102 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
3103 bits_per_frame = av_bits_per_frame;
3106 // bits_per_frame should comply with our minimum
3107 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
3108 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
3110 // Work out if spatial resampling is necessary
3111 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, group_iiratio);
3113 // 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
3114 projected_bits_perframe = bits_per_frame;
3117 while (tmp_q > cpi->worst_quality)
3119 projected_bits_perframe *= 1.04;
3123 // Guess at buffer level at the end of the section
3124 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->twopass.frames_to_key);
3128 FILE *f = fopen("Subsamle.stt", "a");
3129 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->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width);
3133 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
3134 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3136 // Trigger resample if we are projected to fall below down sample level or
3137 // resampled last time and are projected to remain below the up sample level
3138 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
3139 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
3140 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
3141 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
3142 resample_trigger = 1;
3144 resample_trigger = 0;
3148 int64_t clip_bits = (int64_t)(cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->frame_rate));
3149 int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
3151 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced
3152 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ...
3153 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits
3154 resample_trigger = 1;
3156 resample_trigger = 0;
3160 if (resample_trigger)
3162 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
3166 cpi->common.vert_scale = vscale_lookup[scale_val];
3167 cpi->common.horiz_scale = hscale_lookup[scale_val];
3169 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
3170 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
3172 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3173 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3175 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
3176 // effective_sizeratio attempts to provide a crude correction for this
3177 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
3178 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
3180 // Now try again and see what Q we get with the smaller image size
3181 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, group_iiratio);
3185 FILE *f = fopen("Subsamle.stt", "a");
3186 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->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width);
3192 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
3194 cpi->common.Width = new_width;
3195 cpi->common.Height = new_height;
3196 vp8_alloc_compressor_data(cpi);