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.
15 #include "./vpx_scale_rtcd.h"
18 #include "vp8/common/variance.h"
19 #include "encodeintra.h"
20 #include "vp8/common/setupintrarecon.h"
21 #include "vp8/common/systemdependent.h"
23 #include "firstpass.h"
24 #include "vpx_scale/vpx_scale.h"
26 #include "vp8/common/extend.h"
27 #include "vpx_mem/vpx_mem.h"
28 #include "vp8/common/swapyv12buffer.h"
30 #include "vp8/common/quant_common.h"
32 #include "encodeframe.h"
34 /* #define OUTPUT_FPF 1 */
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 vp8_gf_boost_qadjustment[Q]
41 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
43 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
46 #define IIKFACTOR1 1.40
47 #define IIKFACTOR2 1.5
51 #define KF_MB_INTRA_MIN 300
52 #define GF_MB_INTRA_MIN 200
54 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
56 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
57 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
61 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
62 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
65 static const int cq_level[QINDEX_RANGE] =
67 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
68 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
69 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
70 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
71 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
72 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
73 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
74 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
77 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
79 /* Resets the first pass file to the given position using a relative seek
80 * 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);
140 /* TEMP debug code */
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
264 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
266 double av_err = ( cpi->twopass.total_stats.ssim_weighted_pred_err /
267 cpi->twopass.total_stats.count );
268 double this_err = this_frame->ssim_weighted_pred_err;
271 if (this_err > av_err)
272 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
274 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
279 static const double weight_table[256] = {
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.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
283 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
284 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
285 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
286 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
287 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
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,
310 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
311 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
314 static double simple_weight(YV12_BUFFER_CONFIG *source)
318 unsigned char *src = source->y_buffer;
319 double sum_weights = 0.0;
321 /* Loop throught the Y plane raw examining levels and creating a weight
324 i = source->y_height;
330 sum_weights += weight_table[ *src];
333 src -= source->y_width;
334 src += source->y_stride;
337 sum_weights /= (source->y_height * source->y_width);
343 /* This function returns the current per frame maximum bitrate target */
344 static int frame_max_bits(VP8_COMP *cpi)
346 /* Max allocation for a single frame based on the max section guidelines
347 * passed in and how many bits are left
351 /* For CBR we need to also consider buffer fullness.
352 * If we are running below the optimal level then we need to gradually
353 * tighten up on max_bits.
355 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
357 double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
359 /* For CBR base this on the target average bits per frame plus the
360 * maximum sedction rate passed in by the user
362 max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
364 /* If our buffer is below the optimum level */
365 if (buffer_fullness_ratio < 1.0)
367 /* The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4. */
368 int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
370 max_bits = (int)(max_bits * buffer_fullness_ratio);
372 /* Lowest value we will set ... which should allow the buffer to
375 if (max_bits < min_max_bits)
376 max_bits = min_max_bits;
382 /* For VBR base this on the bits and frames left plus the
383 * two_pass_vbrmax_section rate passed in by the user
385 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));
388 /* Trap case where we are out of bits */
395 void vp8_init_first_pass(VP8_COMP *cpi)
397 zero_stats(&cpi->twopass.total_stats);
400 void vp8_end_first_pass(VP8_COMP *cpi)
402 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
405 static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x,
406 YV12_BUFFER_CONFIG * raw_buffer,
407 int * raw_motion_err,
408 YV12_BUFFER_CONFIG * recon_buffer,
409 int * best_motion_err, int recon_yoffset)
411 MACROBLOCKD * const xd = & x->e_mbd;
412 BLOCK *b = &x->block[0];
413 BLOCKD *d = &x->e_mbd.block[0];
415 unsigned char *src_ptr = (*(b->base_src) + b->src);
416 int src_stride = b->src_stride;
417 unsigned char *raw_ptr;
418 int raw_stride = raw_buffer->y_stride;
419 unsigned char *ref_ptr;
420 int ref_stride = x->e_mbd.pre.y_stride;
422 /* Set up pointers for this macro block raw buffer */
423 raw_ptr = (unsigned char *)(raw_buffer->y_buffer + recon_yoffset
425 vp8_mse16x16 ( src_ptr, src_stride, raw_ptr, raw_stride,
426 (unsigned int *)(raw_motion_err));
428 /* Set up pointers for this macro block recon buffer */
429 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
430 ref_ptr = (unsigned char *)(xd->pre.y_buffer + d->offset );
431 vp8_mse16x16 ( src_ptr, src_stride, ref_ptr, ref_stride,
432 (unsigned int *)(best_motion_err));
435 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
436 int_mv *ref_mv, MV *best_mv,
437 YV12_BUFFER_CONFIG *recon_buffer,
438 int *best_motion_err, int recon_yoffset )
440 MACROBLOCKD *const xd = & x->e_mbd;
441 BLOCK *b = &x->block[0];
442 BLOCKD *d = &x->e_mbd.block[0];
449 int step_param = 3; /* Dont search over full range for first pass */
450 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
452 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
453 int new_mv_mode_penalty = 256;
455 /* override the default variance function to use MSE */
456 v_fn_ptr.vf = vp8_mse16x16;
458 /* Set up pointers for this macro block recon buffer */
459 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
461 /* Initial step/diamond search centred on best mv */
463 ref_mv_full.as_mv.col = ref_mv->as_mv.col>>3;
464 ref_mv_full.as_mv.row = ref_mv->as_mv.row>>3;
465 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
466 x->sadperbit16, &num00, &v_fn_ptr,
468 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
469 tmp_err += new_mv_mode_penalty;
471 if (tmp_err < *best_motion_err)
473 *best_motion_err = tmp_err;
474 best_mv->row = tmp_mv.as_mv.row;
475 best_mv->col = tmp_mv.as_mv.col;
478 /* Further step/diamond searches as necessary */
482 while (n < further_steps)
490 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
491 step_param + n, x->sadperbit16,
492 &num00, &v_fn_ptr, x->mvcost,
494 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
495 tmp_err += new_mv_mode_penalty;
497 if (tmp_err < *best_motion_err)
499 *best_motion_err = tmp_err;
500 best_mv->row = tmp_mv.as_mv.row;
501 best_mv->col = tmp_mv.as_mv.col;
507 void vp8_first_pass(VP8_COMP *cpi)
510 MACROBLOCK *const x = & cpi->mb;
511 VP8_COMMON *const cm = & cpi->common;
512 MACROBLOCKD *const xd = & x->e_mbd;
514 int recon_yoffset, recon_uvoffset;
515 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
516 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
517 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
518 int recon_y_stride = lst_yv12->y_stride;
519 int recon_uv_stride = lst_yv12->uv_stride;
520 int64_t intra_error = 0;
521 int64_t coded_error = 0;
523 int sum_mvr = 0, sum_mvc = 0;
524 int sum_mvr_abs = 0, sum_mvc_abs = 0;
525 int sum_mvrs = 0, sum_mvcs = 0;
528 int second_ref_count = 0;
529 int intrapenalty = 256;
530 int neutral_count = 0;
531 int new_mv_count = 0;
532 int sum_in_vectors = 0;
533 uint32_t lastmv_as_int = 0;
537 zero_ref_mv.as_int = 0;
539 vp8_clear_system_state();
541 x->src = * cpi->Source;
545 x->partition_info = x->pi;
547 xd->mode_info_context = cm->mi;
549 if(!cm->use_bilinear_mc_filter)
551 xd->subpixel_predict = vp8_sixtap_predict4x4;
552 xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
553 xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
554 xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
558 xd->subpixel_predict = vp8_bilinear_predict4x4;
559 xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
560 xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
561 xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
564 vp8_build_block_offsets(x);
566 /* set up frame new frame for intra coded blocks */
567 vp8_setup_intra_recon(new_yv12);
568 vp8cx_frame_init_quantizer(cpi);
570 /* Initialise the MV cost table to the defaults */
572 int flag[2] = {1, 1};
573 vp8_initialize_rd_consts(cpi, x, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
574 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
575 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
578 /* for each macroblock row in image */
579 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
583 best_ref_mv.as_int = 0;
585 /* reset above block coeffs */
586 xd->up_available = (mb_row != 0);
587 recon_yoffset = (mb_row * recon_y_stride * 16);
588 recon_uvoffset = (mb_row * recon_uv_stride * 8);
590 /* Set up limit values for motion vectors to prevent them extending
591 * outside the UMV borders
593 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
594 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
597 /* for each macroblock col in image */
598 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
601 int gf_motion_error = INT_MAX;
602 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
604 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
605 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
606 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
607 xd->left_available = (mb_col != 0);
609 /* Copy current mb to a buffer */
610 vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
612 /* do intra 16x16 prediction */
613 this_error = vp8_encode_intra(cpi, x, use_dc_pred);
615 /* "intrapenalty" below deals with situations where the intra
616 * and inter error scores are very low (eg a plain black frame)
617 * We do not have special cases in first pass for 0,0 and
618 * nearest etc so all inter modes carry an overhead cost
619 * estimate fot the mv. When the error score is very low this
620 * causes us to pick all or lots of INTRA modes and throw lots
621 * of key frames. This penalty adds a cost matching that of a
622 * 0,0 mv to the intra case.
624 this_error += intrapenalty;
626 /* Cumulative intra error total */
627 intra_error += (int64_t)this_error;
629 /* Set up limit values for motion vectors to prevent them
630 * extending outside the UMV borders
632 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
633 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
635 /* Other than for the first frame do a motion search */
636 if (cm->current_video_frame > 0)
638 BLOCKD *d = &x->e_mbd.block[0];
641 int motion_error = INT_MAX;
642 int raw_motion_error = INT_MAX;
644 /* Simple 0,0 motion with no mv overhead */
645 zz_motion_search( cpi, x, cpi->last_frame_unscaled_source,
646 &raw_motion_error, lst_yv12, &motion_error,
648 d->bmi.mv.as_mv.row = 0;
649 d->bmi.mv.as_mv.col = 0;
651 if (raw_motion_error < cpi->oxcf.encode_breakout)
652 goto skip_motion_search;
654 /* Test last reference frame using the previous best mv as the
655 * starting point (best reference) for the search
657 first_pass_motion_search(cpi, x, &best_ref_mv,
658 &d->bmi.mv.as_mv, lst_yv12,
659 &motion_error, recon_yoffset);
661 /* If the current best reference mv is not centred on 0,0
662 * then do a 0,0 based search as well
664 if (best_ref_mv.as_int)
667 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
668 lst_yv12, &tmp_err, recon_yoffset);
670 if ( tmp_err < motion_error )
672 motion_error = tmp_err;
673 d->bmi.mv.as_mv.row = tmp_mv.row;
674 d->bmi.mv.as_mv.col = tmp_mv.col;
678 /* Experimental search in a second reference frame ((0,0)
681 if (cm->current_video_frame > 1)
683 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
685 if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
690 /* Reset to last frame as reference buffer */
691 xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
692 xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
693 xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
697 /* Intra assumed best */
698 best_ref_mv.as_int = 0;
700 if (motion_error <= this_error)
702 /* Keep a count of cases where the inter and intra were
703 * very close and very low. This helps with scene cut
704 * detection for example in cropped clips with black bars
705 * at the sides or top and bottom.
707 if( (((this_error-intrapenalty) * 9) <=
708 (motion_error*10)) &&
709 (this_error < (2*intrapenalty)) )
714 d->bmi.mv.as_mv.row *= 8;
715 d->bmi.mv.as_mv.col *= 8;
716 this_error = motion_error;
717 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
718 vp8_encode_inter16x16y(x);
719 sum_mvr += d->bmi.mv.as_mv.row;
720 sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
721 sum_mvc += d->bmi.mv.as_mv.col;
722 sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
723 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
724 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
727 best_ref_mv.as_int = d->bmi.mv.as_int;
729 /* Was the vector non-zero */
730 if (d->bmi.mv.as_int)
734 /* Was it different from the last non zero vector */
735 if ( d->bmi.mv.as_int != lastmv_as_int )
737 lastmv_as_int = d->bmi.mv.as_int;
739 /* Does the Row vector point inwards or outwards */
740 if (mb_row < cm->mb_rows / 2)
742 if (d->bmi.mv.as_mv.row > 0)
744 else if (d->bmi.mv.as_mv.row < 0)
747 else if (mb_row > cm->mb_rows / 2)
749 if (d->bmi.mv.as_mv.row > 0)
751 else if (d->bmi.mv.as_mv.row < 0)
755 /* Does the Row vector point inwards or outwards */
756 if (mb_col < cm->mb_cols / 2)
758 if (d->bmi.mv.as_mv.col > 0)
760 else if (d->bmi.mv.as_mv.col < 0)
763 else if (mb_col > cm->mb_cols / 2)
765 if (d->bmi.mv.as_mv.col > 0)
767 else if (d->bmi.mv.as_mv.col < 0)
774 coded_error += (int64_t)this_error;
776 /* adjust to the next column of macroblocks */
777 x->src.y_buffer += 16;
778 x->src.u_buffer += 8;
779 x->src.v_buffer += 8;
785 /* adjust to the next row of mbs */
786 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
787 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
788 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
790 /* extend the recon for intra prediction */
791 vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
792 vp8_clear_system_state();
795 vp8_clear_system_state();
801 fps.frame = cm->current_video_frame ;
802 fps.intra_error = (double)(intra_error >> 8);
803 fps.coded_error = (double)(coded_error >> 8);
804 weight = simple_weight(cpi->Source);
810 fps.ssim_weighted_pred_err = fps.coded_error * weight;
812 fps.pcnt_inter = 0.0;
813 fps.pcnt_motion = 0.0;
820 fps.mv_in_out_count = 0.0;
821 fps.new_mv_count = 0.0;
824 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
825 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
826 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
830 fps.MVr = (double)sum_mvr / (double)mvcount;
831 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
832 fps.MVc = (double)sum_mvc / (double)mvcount;
833 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
834 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
835 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
836 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
837 fps.new_mv_count = new_mv_count;
839 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
842 /* TODO: handle the case when duration is set to 0, or something less
843 * than the full time between subsequent cpi->source_time_stamps
845 fps.duration = (double)(cpi->source->ts_end
846 - cpi->source->ts_start);
848 /* don't want to do output stats with a stack variable! */
849 memcpy(&cpi->twopass.this_frame_stats,
851 sizeof(FIRSTPASS_STATS));
852 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats);
853 accumulate_stats(&cpi->twopass.total_stats, &fps);
856 /* Copy the previous Last Frame into the GF buffer if specific
857 * conditions for doing so are met
859 if ((cm->current_video_frame > 0) &&
860 (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) &&
861 ((cpi->twopass.this_frame_stats.intra_error /
862 DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) >
865 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
868 /* swap frame pointers so last frame refers to the frame we just
871 vp8_swap_yv12_buffer(lst_yv12, new_yv12);
872 vp8_yv12_extend_frame_borders(lst_yv12);
874 /* Special case for the first frame. Copy into the GF buffer as a
877 if (cm->current_video_frame == 0)
879 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
883 /* use this to see what the first pass reconstruction looks like */
888 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
890 if (cm->current_video_frame == 0)
891 recon_file = fopen(filename, "wb");
893 recon_file = fopen(filename, "ab");
895 (void) fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1,
900 cm->current_video_frame++;
903 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
905 /* Estimate a cost per mb attributable to overheads such as the coding of
906 * modes and motion vectors.
907 * Currently simplistic in its assumptions for testing.
910 static double bitcost( double prob )
912 return -(log( prob ) / log( 2.0 ));
914 static int64_t estimate_modemvcost(VP8_COMP *cpi,
915 FIRSTPASS_STATS * fpstats)
920 double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
921 double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
922 double av_intra = (1.0 - av_pct_inter);
928 zz_cost = bitcost(av_pct_inter - av_pct_motion);
929 motion_cost = bitcost(av_pct_motion);
930 intra_cost = bitcost(av_intra);
932 /* Estimate of extra bits per mv overhead for mbs
933 * << 9 is the normalization to the (bits * 512) used in vp8_bits_per_mb
935 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
937 /* Crude estimate of overhead cost from modes
938 * << 9 is the normalization to (bits * 512) used in vp8_bits_per_mb
940 mode_cost =((((av_pct_inter - av_pct_motion) * zz_cost) +
941 (av_pct_motion * motion_cost) +
942 (av_intra * intra_cost)) * cpi->common.MBs) * 512;
944 return mv_cost + mode_cost;
947 static double calc_correction_factor( double err_per_mb,
954 double error_term = err_per_mb / err_devisor;
955 double correction_factor;
957 /* Adjustment based on Q to power term. */
958 power_term = pt_low + (Q * 0.01);
959 power_term = (power_term > pt_high) ? pt_high : power_term;
961 /* Adjustments to error term */
964 /* Calculate correction factor */
965 correction_factor = pow(error_term, power_term);
969 (correction_factor < 0.05)
970 ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
972 return correction_factor;
975 static int estimate_max_q(VP8_COMP *cpi,
976 FIRSTPASS_STATS * fpstats,
977 int section_target_bandwitdh,
981 int num_mbs = cpi->common.MBs;
982 int target_norm_bits_per_mb;
984 double section_err = (fpstats->coded_error / fpstats->count);
985 double err_per_mb = section_err / num_mbs;
986 double err_correction_factor;
987 double speed_correction = 1.0;
988 int overhead_bits_per_mb;
990 if (section_target_bandwitdh <= 0)
991 return cpi->twopass.maxq_max_limit; /* Highest value allowed */
993 target_norm_bits_per_mb =
994 (section_target_bandwitdh < (1 << 20))
995 ? (512 * section_target_bandwitdh) / num_mbs
996 : 512 * (section_target_bandwitdh / num_mbs);
998 /* Calculate a corrective factor based on a rolling ratio of bits spent
1001 if ((cpi->rolling_target_bits > 0) &&
1002 (cpi->active_worst_quality < cpi->worst_quality))
1004 double rolling_ratio;
1006 rolling_ratio = (double)cpi->rolling_actual_bits /
1007 (double)cpi->rolling_target_bits;
1009 if (rolling_ratio < 0.95)
1010 cpi->twopass.est_max_qcorrection_factor -= 0.005;
1011 else if (rolling_ratio > 1.05)
1012 cpi->twopass.est_max_qcorrection_factor += 0.005;
1014 cpi->twopass.est_max_qcorrection_factor =
1015 (cpi->twopass.est_max_qcorrection_factor < 0.1)
1017 : (cpi->twopass.est_max_qcorrection_factor > 10.0)
1018 ? 10.0 : cpi->twopass.est_max_qcorrection_factor;
1021 /* Corrections for higher compression speed settings
1022 * (reduced compression expected)
1024 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1026 if (cpi->oxcf.cpu_used <= 5)
1027 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1029 speed_correction = 1.25;
1032 /* Estimate of overhead bits per mb */
1033 /* Correction to overhead bits for min allowed Q. */
1034 overhead_bits_per_mb = overhead_bits / num_mbs;
1035 overhead_bits_per_mb = (int)(overhead_bits_per_mb *
1036 pow( 0.98, (double)cpi->twopass.maxq_min_limit ));
1038 /* Try and pick a max Q that will be high enough to encode the
1039 * content at the given rate.
1041 for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++)
1043 int bits_per_mb_at_this_q;
1045 /* Error per MB based correction factor */
1046 err_correction_factor =
1047 calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
1049 bits_per_mb_at_this_q =
1050 vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
1052 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor
1053 * speed_correction * cpi->twopass.est_max_qcorrection_factor
1054 * cpi->twopass.section_max_qfactor
1055 * (double)bits_per_mb_at_this_q);
1057 /* Mode and motion overhead */
1058 /* As Q rises in real encode loop rd code will force overhead down
1059 * We make a crude adjustment for this here as *.98 per Q step.
1061 overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
1063 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1067 /* Restriction on active max q for constrained quality mode. */
1068 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
1069 (Q < cpi->cq_target_quality) )
1071 Q = cpi->cq_target_quality;
1074 /* Adjust maxq_min_limit and maxq_max_limit limits based on
1075 * average q observed in clip for non kf/gf.arf frames
1076 * Give average a chance to settle though.
1078 if ( (cpi->ni_frames >
1079 ((int)cpi->twopass.total_stats.count >> 8)) &&
1080 (cpi->ni_frames > 150) )
1082 cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
1083 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
1084 cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
1085 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
1091 /* For cq mode estimate a cq level that matches the observed
1092 * complexity and data rate.
1094 static int estimate_cq( VP8_COMP *cpi,
1095 FIRSTPASS_STATS * fpstats,
1096 int section_target_bandwitdh,
1100 int num_mbs = cpi->common.MBs;
1101 int target_norm_bits_per_mb;
1103 double section_err = (fpstats->coded_error / fpstats->count);
1104 double err_per_mb = section_err / num_mbs;
1105 double err_correction_factor;
1106 double speed_correction = 1.0;
1107 double clip_iiratio;
1108 double clip_iifactor;
1109 int overhead_bits_per_mb;
1113 FILE *f = fopen("epmp.stt", "a");
1114 fprintf(f, "%10.2f\n", err_per_mb );
1118 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1119 ? (512 * section_target_bandwitdh) / num_mbs
1120 : 512 * (section_target_bandwitdh / num_mbs);
1122 /* Estimate of overhead bits per mb */
1123 overhead_bits_per_mb = overhead_bits / num_mbs;
1125 /* Corrections for higher compression speed settings
1126 * (reduced compression expected)
1128 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1130 if (cpi->oxcf.cpu_used <= 5)
1131 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1133 speed_correction = 1.25;
1136 /* II ratio correction factor for clip as a whole */
1137 clip_iiratio = cpi->twopass.total_stats.intra_error /
1138 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error);
1139 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1140 if (clip_iifactor < 0.80)
1141 clip_iifactor = 0.80;
1143 /* Try and pick a Q that can encode the content at the given rate. */
1144 for (Q = 0; Q < MAXQ; Q++)
1146 int bits_per_mb_at_this_q;
1148 /* Error per MB based correction factor */
1149 err_correction_factor =
1150 calc_correction_factor(err_per_mb, 100.0, 0.40, 0.90, Q);
1152 bits_per_mb_at_this_q =
1153 vp8_bits_per_mb[INTER_FRAME][Q] + overhead_bits_per_mb;
1155 bits_per_mb_at_this_q =
1156 (int)( .5 + err_correction_factor *
1159 (double)bits_per_mb_at_this_q);
1161 /* Mode and motion overhead */
1162 /* As Q rises in real encode loop rd code will force overhead down
1163 * We make a crude adjustment for this here as *.98 per Q step.
1165 overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
1167 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1171 /* Clip value to range "best allowed to (worst allowed - 1)" */
1173 if ( Q >= cpi->worst_quality )
1174 Q = cpi->worst_quality - 1;
1175 if ( Q < cpi->best_quality )
1176 Q = cpi->best_quality;
1181 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
1184 int num_mbs = cpi->common.MBs;
1185 int target_norm_bits_per_mb;
1187 double err_per_mb = section_err / num_mbs;
1188 double err_correction_factor;
1189 double speed_correction = 1.0;
1191 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
1193 /* Corrections for higher compression speed settings
1194 * (reduced compression expected)
1196 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1198 if (cpi->oxcf.cpu_used <= 5)
1199 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1201 speed_correction = 1.25;
1204 /* Try and pick a Q that can encode the content at the given rate. */
1205 for (Q = 0; Q < MAXQ; Q++)
1207 int bits_per_mb_at_this_q;
1209 /* Error per MB based correction factor */
1210 err_correction_factor =
1211 calc_correction_factor(err_per_mb, 150.0, 0.40, 0.90, Q);
1213 bits_per_mb_at_this_q =
1214 (int)( .5 + ( err_correction_factor *
1216 cpi->twopass.est_max_qcorrection_factor *
1217 (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0 ) );
1219 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1226 /* Estimate a worst case Q for a KF group */
1227 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio)
1230 int num_mbs = cpi->common.MBs;
1231 int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1232 int bits_per_mb_at_this_q;
1234 double err_per_mb = section_err / num_mbs;
1235 double err_correction_factor;
1236 double speed_correction = 1.0;
1237 double current_spend_ratio = 1.0;
1239 double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1240 double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1242 double iiratio_correction_factor = 1.0;
1244 double combined_correction_factor;
1246 /* Trap special case where the target is <= 0 */
1247 if (target_norm_bits_per_mb <= 0)
1250 /* Calculate a corrective factor based on a rolling ratio of bits spent
1252 * This is clamped to the range 0.1 to 10.0
1254 if (cpi->long_rolling_target_bits <= 0)
1255 current_spend_ratio = 10.0;
1258 current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
1259 current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
1262 /* Calculate a correction factor based on the quality of prediction in
1263 * the sequence as indicated by intra_inter error score ratio (IIRatio)
1264 * The idea here is to favour subsampling in the hardest sections vs
1267 iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1269 if (iiratio_correction_factor < 0.5)
1270 iiratio_correction_factor = 0.5;
1272 /* Corrections for higher compression speed settings
1273 * (reduced compression expected)
1275 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1277 if (cpi->oxcf.cpu_used <= 5)
1278 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1280 speed_correction = 1.25;
1283 /* Combine the various factors calculated above */
1284 combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
1286 /* Try and pick a Q that should be high enough to encode the content at
1289 for (Q = 0; Q < MAXQ; Q++)
1291 /* Error per MB based correction factor */
1292 err_correction_factor =
1293 calc_correction_factor(err_per_mb, 150.0, pow_lowq, pow_highq, Q);
1295 bits_per_mb_at_this_q =
1296 (int)(.5 + ( err_correction_factor *
1297 combined_correction_factor *
1298 (double)vp8_bits_per_mb[INTER_FRAME][Q]) );
1300 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1304 /* If we could not hit the target even at Max Q then estimate what Q
1305 * would have been required
1307 while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2)))
1310 bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1316 FILE *f = fopen("estkf_q.stt", "a");
1317 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,
1318 target_norm_bits_per_mb, err_per_mb, err_correction_factor,
1319 current_spend_ratio, group_iiratio, iiratio_correction_factor,
1320 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
1327 extern void vp8_new_framerate(VP8_COMP *cpi, double framerate);
1329 void vp8_init_second_pass(VP8_COMP *cpi)
1331 FIRSTPASS_STATS this_frame;
1332 FIRSTPASS_STATS *start_pos;
1334 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1336 zero_stats(&cpi->twopass.total_stats);
1337 zero_stats(&cpi->twopass.total_left_stats);
1339 if (!cpi->twopass.stats_in_end)
1342 cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1343 cpi->twopass.total_left_stats = cpi->twopass.total_stats;
1345 /* each frame can have a different duration, as the frame rate in the
1346 * source isn't guaranteed to be constant. The frame rate prior to
1347 * the first frame encoded in the second pass is a guess. However the
1348 * sum duration is not. Its calculated based on the actual durations of
1349 * all frames from the first pass.
1351 vp8_new_framerate(cpi, 10000000.0 * cpi->twopass.total_stats.count / cpi->twopass.total_stats.duration);
1353 cpi->output_framerate = cpi->framerate;
1354 cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats.duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1355 cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats.duration * two_pass_min_rate / 10000000.0);
1357 /* Calculate a minimum intra value to be used in determining the IIratio
1358 * scores used in the second pass. We have this minimum to make sure
1359 * that clips that are static but "low complexity" in the intra domain
1360 * are still boosted appropriately for KF/GF/ARF
1362 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1363 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1365 /* Scan the first pass file and calculate an average Intra / Inter error
1366 * score ratio for the sequence
1369 double sum_iiratio = 0.0;
1372 start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1374 while (input_stats(cpi, &this_frame) != EOF)
1376 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1377 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1378 sum_iiratio += IIRatio;
1381 cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count);
1383 /* Reset file position */
1384 reset_fpf_position(cpi, start_pos);
1387 /* Scan the first pass file and calculate a modified total error based
1388 * upon the bias/power function used to allocate bits
1391 start_pos = cpi->twopass.stats_in; /* Note starting "file" position */
1393 cpi->twopass.modified_error_total = 0.0;
1394 cpi->twopass.modified_error_used = 0.0;
1396 while (input_stats(cpi, &this_frame) != EOF)
1398 cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame);
1400 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1402 reset_fpf_position(cpi, start_pos); /* Reset file position */
1407 void vp8_end_second_pass(VP8_COMP *cpi)
1411 /* This function gives and estimate of how badly we believe the prediction
1412 * quality is decaying from frame to frame.
1414 static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1416 double prediction_decay_rate;
1417 double motion_decay;
1418 double motion_pct = next_frame->pcnt_motion;
1420 /* Initial basis is the % mbs inter coded */
1421 prediction_decay_rate = next_frame->pcnt_inter;
1423 /* High % motion -> somewhat higher decay rate */
1424 motion_decay = (1.0 - (motion_pct / 20.0));
1425 if (motion_decay < prediction_decay_rate)
1426 prediction_decay_rate = motion_decay;
1428 /* Adjustment to decay rate based on speed of motion */
1430 double this_mv_rabs;
1431 double this_mv_cabs;
1432 double distance_factor;
1434 this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1435 this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1437 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
1438 (this_mv_cabs * this_mv_cabs)) / 250.0;
1439 distance_factor = ((distance_factor > 1.0)
1440 ? 0.0 : (1.0 - distance_factor));
1441 if (distance_factor < prediction_decay_rate)
1442 prediction_decay_rate = distance_factor;
1445 return prediction_decay_rate;
1448 /* Function to test for a condition where a complex transition is followed
1449 * by a static section. For example in slide shows where there is a fade
1450 * between slides. This is to help with more optimal kf and gf positioning.
1452 static int detect_transition_to_still(
1456 double loop_decay_rate,
1457 double decay_accumulator )
1459 int trans_to_still = 0;
1461 /* Break clause to detect very still sections after motion
1462 * For example a static image after a fade or other transition
1463 * instead of a clean scene cut.
1465 if ( (frame_interval > MIN_GF_INTERVAL) &&
1466 (loop_decay_rate >= 0.999) &&
1467 (decay_accumulator < 0.9) )
1470 FIRSTPASS_STATS * position = cpi->twopass.stats_in;
1471 FIRSTPASS_STATS tmp_next_frame;
1474 /* Look ahead a few frames to see if static condition persists... */
1475 for ( j = 0; j < still_interval; j++ )
1477 if (EOF == input_stats(cpi, &tmp_next_frame))
1480 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1481 if ( decay_rate < 0.999 )
1484 /* Reset file position */
1485 reset_fpf_position(cpi, position);
1487 /* Only if it does do we signal a transition to still */
1488 if ( j == still_interval )
1492 return trans_to_still;
1495 /* This function detects a flash through the high relative pcnt_second_ref
1496 * score in the frame following a flash frame. The offset passed in should
1499 static int detect_flash( VP8_COMP *cpi, int offset )
1501 FIRSTPASS_STATS next_frame;
1503 int flash_detected = 0;
1505 /* Read the frame data. */
1506 /* The return is 0 (no flash detected) if not a valid frame */
1507 if ( read_frame_stats(cpi, &next_frame, offset) != EOF )
1509 /* What we are looking for here is a situation where there is a
1510 * brief break in prediction (such as a flash) but subsequent frames
1511 * are reasonably well predicted by an earlier (pre flash) frame.
1512 * The recovery after a flash is indicated by a high pcnt_second_ref
1513 * comapred to pcnt_inter.
1515 if ( (next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
1516 (next_frame.pcnt_second_ref >= 0.5 ) )
1522 FILE *f = fopen("flash.stt", "a");
1523 fprintf(f, "%8.0f %6.2f %6.2f\n",
1525 next_frame.pcnt_inter,
1526 next_frame.pcnt_second_ref);
1532 return flash_detected;
1535 /* Update the motion related elements to the GF arf boost calculation */
1536 static void accumulate_frame_motion_stats(
1538 FIRSTPASS_STATS * this_frame,
1539 double * this_frame_mv_in_out,
1540 double * mv_in_out_accumulator,
1541 double * abs_mv_in_out_accumulator,
1542 double * mv_ratio_accumulator )
1544 double this_frame_mvr_ratio;
1545 double this_frame_mvc_ratio;
1548 /* Accumulate motion stats. */
1549 motion_pct = this_frame->pcnt_motion;
1551 /* Accumulate Motion In/Out of frame stats */
1552 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1553 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1554 *abs_mv_in_out_accumulator +=
1555 fabs(this_frame->mv_in_out_count * motion_pct);
1557 /* Accumulate a measure of how uniform (or conversely how random)
1558 * the motion field is. (A ratio of absmv / mv)
1560 if (motion_pct > 0.05)
1562 this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1563 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1565 this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1566 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1568 *mv_ratio_accumulator +=
1569 (this_frame_mvr_ratio < this_frame->mvr_abs)
1570 ? (this_frame_mvr_ratio * motion_pct)
1571 : this_frame->mvr_abs * motion_pct;
1573 *mv_ratio_accumulator +=
1574 (this_frame_mvc_ratio < this_frame->mvc_abs)
1575 ? (this_frame_mvc_ratio * motion_pct)
1576 : this_frame->mvc_abs * motion_pct;
1581 /* Calculate a baseline boost number for the current frame. */
1582 static double calc_frame_boost(
1584 FIRSTPASS_STATS * this_frame,
1585 double this_frame_mv_in_out )
1589 /* Underlying boost factor is based on inter intra error ratio */
1590 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1591 frame_boost = (IIFACTOR * this_frame->intra_error /
1592 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1594 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1595 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1597 /* Increase boost for frames where new data coming into frame
1598 * (eg zoom out). Slightly reduce boost if there is a net balance
1599 * of motion out of the frame (zoom in).
1600 * The range for this_frame_mv_in_out is -1.0 to +1.0
1602 if (this_frame_mv_in_out > 0.0)
1603 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1604 /* In extreme case boost is halved */
1606 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1608 /* Clip to maximum */
1609 if (frame_boost > GF_RMAX)
1610 frame_boost = GF_RMAX;
1616 static int calc_arf_boost(
1624 FIRSTPASS_STATS this_frame;
1627 double boost_score = 0.0;
1628 double mv_ratio_accumulator = 0.0;
1629 double decay_accumulator = 1.0;
1630 double this_frame_mv_in_out = 0.0;
1631 double mv_in_out_accumulator = 0.0;
1632 double abs_mv_in_out_accumulator = 0.0;
1634 int flash_detected = 0;
1636 /* Search forward from the proposed arf/next gf position */
1637 for ( i = 0; i < f_frames; i++ )
1639 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1642 /* Update the motion related elements to the boost calculation */
1643 accumulate_frame_motion_stats( cpi, &this_frame,
1644 &this_frame_mv_in_out, &mv_in_out_accumulator,
1645 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1647 /* Calculate the baseline boost number for this frame */
1648 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1650 /* We want to discount the the flash frame itself and the recovery
1651 * frame that follows as both will have poor scores.
1653 flash_detected = detect_flash(cpi, (i+offset)) ||
1654 detect_flash(cpi, (i+offset+1));
1656 /* Cumulative effect of prediction quality decay */
1657 if ( !flash_detected )
1661 get_prediction_decay_rate(cpi, &this_frame);
1663 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1665 boost_score += (decay_accumulator * r);
1667 /* Break out conditions. */
1668 if ( (!flash_detected) &&
1669 ((mv_ratio_accumulator > 100.0) ||
1670 (abs_mv_in_out_accumulator > 3.0) ||
1671 (mv_in_out_accumulator < -2.0) ) )
1677 *f_boost = (int)(boost_score * 100.0) >> 4;
1679 /* Reset for backward looking loop */
1681 mv_ratio_accumulator = 0.0;
1682 decay_accumulator = 1.0;
1683 this_frame_mv_in_out = 0.0;
1684 mv_in_out_accumulator = 0.0;
1685 abs_mv_in_out_accumulator = 0.0;
1687 /* Search forward from the proposed arf/next gf position */
1688 for ( i = -1; i >= -b_frames; i-- )
1690 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1693 /* Update the motion related elements to the boost calculation */
1694 accumulate_frame_motion_stats( cpi, &this_frame,
1695 &this_frame_mv_in_out, &mv_in_out_accumulator,
1696 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1698 /* Calculate the baseline boost number for this frame */
1699 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1701 /* We want to discount the the flash frame itself and the recovery
1702 * frame that follows as both will have poor scores.
1704 flash_detected = detect_flash(cpi, (i+offset)) ||
1705 detect_flash(cpi, (i+offset+1));
1707 /* Cumulative effect of prediction quality decay */
1708 if ( !flash_detected )
1712 get_prediction_decay_rate(cpi, &this_frame);
1714 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1717 boost_score += (decay_accumulator * r);
1719 /* Break out conditions. */
1720 if ( (!flash_detected) &&
1721 ((mv_ratio_accumulator > 100.0) ||
1722 (abs_mv_in_out_accumulator > 3.0) ||
1723 (mv_in_out_accumulator < -2.0) ) )
1728 *b_boost = (int)(boost_score * 100.0) >> 4;
1730 return (*f_boost + *b_boost);
1734 /* Analyse and define a gf/arf group . */
1735 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1737 FIRSTPASS_STATS next_frame;
1738 FIRSTPASS_STATS *start_pos;
1741 double boost_score = 0.0;
1742 double old_boost_score = 0.0;
1743 double gf_group_err = 0.0;
1744 double gf_first_frame_err = 0.0;
1745 double mod_frame_err = 0.0;
1747 double mv_ratio_accumulator = 0.0;
1748 double decay_accumulator = 1.0;
1750 double loop_decay_rate = 1.00; /* Starting decay rate */
1752 double this_frame_mv_in_out = 0.0;
1753 double mv_in_out_accumulator = 0.0;
1754 double abs_mv_in_out_accumulator = 0.0;
1755 double mod_err_per_mb_accumulator = 0.0;
1757 int max_bits = frame_max_bits(cpi); /* Max for a single frame */
1759 unsigned int allow_alt_ref =
1760 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1767 cpi->twopass.gf_group_bits = 0;
1768 cpi->twopass.gf_decay_rate = 0;
1770 vp8_clear_system_state();
1772 start_pos = cpi->twopass.stats_in;
1774 vpx_memset(&next_frame, 0, sizeof(next_frame)); /* assure clean */
1776 /* Load stats for the current frame. */
1777 mod_frame_err = calculate_modified_err(cpi, this_frame);
1779 /* Note the error of the frame at the start of the group (this will be
1780 * the GF frame error if we code a normal gf
1782 gf_first_frame_err = mod_frame_err;
1784 /* Special treatment if the current frame is a key frame (which is also
1785 * a gf). If it is then its error score (and hence bit allocation) need
1786 * to be subtracted out from the calculation for the GF group
1788 if (cpi->common.frame_type == KEY_FRAME)
1789 gf_group_err -= gf_first_frame_err;
1791 /* Scan forward to try and work out how many frames the next gf group
1792 * should contain and what level of boost is appropriate for the GF
1793 * or ARF that will be coded with the group
1797 while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1798 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1799 (i < cpi->twopass.frames_to_key))
1803 /* Accumulate error score of frames in this gf group */
1804 mod_frame_err = calculate_modified_err(cpi, this_frame);
1806 gf_group_err += mod_frame_err;
1808 mod_err_per_mb_accumulator +=
1809 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1811 if (EOF == input_stats(cpi, &next_frame))
1814 /* Test for the case where there is a brief flash but the prediction
1815 * quality back to an earlier frame is then restored.
1817 flash_detected = detect_flash(cpi, 0);
1819 /* Update the motion related elements to the boost calculation */
1820 accumulate_frame_motion_stats( cpi, &next_frame,
1821 &this_frame_mv_in_out, &mv_in_out_accumulator,
1822 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1824 /* Calculate a baseline boost number for this frame */
1825 r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out );
1827 /* Cumulative effect of prediction quality decay */
1828 if ( !flash_detected )
1830 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1831 decay_accumulator = decay_accumulator * loop_decay_rate;
1833 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1835 boost_score += (decay_accumulator * r);
1837 /* Break clause to detect very still sections after motion
1838 * For example a staic image after a fade or other transition.
1840 if ( detect_transition_to_still( cpi, i, 5,
1842 decay_accumulator ) )
1845 boost_score = old_boost_score;
1849 /* Break out conditions. */
1851 /* Break at cpi->max_gf_interval unless almost totally static */
1852 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1854 /* Dont break out with a very short interval */
1855 (i > MIN_GF_INTERVAL) &&
1856 /* Dont break out very close to a key frame */
1857 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1858 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1859 (!flash_detected) &&
1860 ((mv_ratio_accumulator > 100.0) ||
1861 (abs_mv_in_out_accumulator > 3.0) ||
1862 (mv_in_out_accumulator < -2.0) ||
1863 ((boost_score - old_boost_score) < 2.0))
1866 boost_score = old_boost_score;
1870 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1872 old_boost_score = boost_score;
1875 cpi->twopass.gf_decay_rate =
1876 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1878 /* When using CBR apply additional buffer related upper limits */
1879 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1883 /* For cbr apply buffer related limits */
1884 if (cpi->drop_frames_allowed)
1886 int64_t df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1887 (cpi->oxcf.optimal_buffer_level / 100);
1889 if (cpi->buffer_level > df_buffer_level)
1890 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1894 else if (cpi->buffer_level > 0)
1896 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1903 if (boost_score > max_boost)
1904 boost_score = max_boost;
1907 /* Dont allow conventional gf too near the next kf */
1908 if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)
1910 while (i < cpi->twopass.frames_to_key)
1914 if (EOF == input_stats(cpi, this_frame))
1917 if (i < cpi->twopass.frames_to_key)
1919 mod_frame_err = calculate_modified_err(cpi, this_frame);
1920 gf_group_err += mod_frame_err;
1925 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1928 /* Alterrnative boost calculation for alt ref */
1929 alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost );
1932 /* Should we use the alternate refernce frame */
1933 if (allow_alt_ref &&
1934 (i >= MIN_GF_INTERVAL) &&
1935 /* dont use ARF very near next kf */
1936 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1938 ((next_frame.pcnt_inter > 0.75) ||
1939 (next_frame.pcnt_second_ref > 0.5)) &&
1940 ((mv_in_out_accumulator / (double)i > -0.2) ||
1941 (mv_in_out_accumulator > -2.0)) &&
1945 (next_frame.pcnt_inter > 0.75) &&
1946 ((mv_in_out_accumulator / (double)i > -0.2) ||
1947 (mv_in_out_accumulator > -2.0)) &&
1948 (cpi->gfu_boost > 100) &&
1949 (cpi->twopass.gf_decay_rate <=
1950 (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) )
1954 int allocation_chunks;
1955 int Q = (cpi->oxcf.fixed_q < 0)
1956 ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1958 int arf_frame_bits = 0;
1962 cpi->gfu_boost = alt_boost;
1965 /* Estimate the bits to be allocated to the group as a whole */
1966 if ((cpi->twopass.kf_group_bits > 0) &&
1967 (cpi->twopass.kf_group_error_left > 0))
1969 group_bits = (int)((double)cpi->twopass.kf_group_bits *
1970 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1975 /* Boost for arf frame */
1977 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1979 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1983 /* Set max and minimum boost and hence minimum allocation */
1984 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1985 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1986 else if (Boost < 125)
1989 allocation_chunks = (i * 100) + Boost;
1991 /* Normalize Altboost and allocations chunck down to prevent overflow */
1992 while (Boost > 1000)
1995 allocation_chunks /= 2;
1998 /* Calculate the number of bits to be spent on the arf based on the
2001 arf_frame_bits = (int)((double)Boost * (group_bits /
2002 (double)allocation_chunks));
2004 /* Estimate if there are enough bits available to make worthwhile use
2007 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
2009 /* Only use an arf if it is likely we will be able to code
2010 * it at a lower Q than the surrounding frames.
2012 if (tmp_q < cpi->worst_quality)
2015 int frames_after_arf;
2016 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
2017 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
2019 cpi->source_alt_ref_pending = 1;
2022 * For alt ref frames the error score for the end frame of the
2023 * group (the alt ref frame) should not contribute to the group
2024 * total and hence the number of bit allocated to the group.
2025 * Rather it forms part of the next group (it is the GF at the
2026 * start of the next group)
2027 * gf_group_err -= mod_frame_err;
2029 * For alt ref frames alt ref frame is technically part of the
2030 * GF frame for the next group but we always base the error
2031 * calculation and bit allocation on the current group of frames.
2033 * Set the interval till the next gf or arf.
2034 * For ARFs this is the number of frames to be coded before the
2035 * future frame that is coded as an ARF.
2036 * The future frame itself is part of the next group
2038 cpi->baseline_gf_interval = i;
2041 * Define the arnr filter width for this group of frames:
2042 * We only filter frames that lie within a distance of half
2043 * the GF interval from the ARF frame. We also have to trap
2044 * cases where the filter extends beyond the end of clip.
2045 * Note: this_frame->frame has been updated in the loop
2046 * so it now points at the ARF frame.
2048 half_gf_int = cpi->baseline_gf_interval >> 1;
2049 frames_after_arf = (int)(cpi->twopass.total_stats.count -
2050 this_frame->frame - 1);
2052 switch (cpi->oxcf.arnr_type)
2054 case 1: /* Backward filter */
2056 if (frames_bwd > half_gf_int)
2057 frames_bwd = half_gf_int;
2060 case 2: /* Forward filter */
2061 if (frames_fwd > half_gf_int)
2062 frames_fwd = half_gf_int;
2063 if (frames_fwd > frames_after_arf)
2064 frames_fwd = frames_after_arf;
2068 case 3: /* Centered filter */
2071 if (frames_fwd > frames_after_arf)
2072 frames_fwd = frames_after_arf;
2073 if (frames_fwd > half_gf_int)
2074 frames_fwd = half_gf_int;
2076 frames_bwd = frames_fwd;
2078 /* For even length filter there is one more frame backward
2079 * than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
2081 if (frames_bwd < half_gf_int)
2082 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
2086 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
2090 cpi->source_alt_ref_pending = 0;
2091 cpi->baseline_gf_interval = i;
2096 cpi->source_alt_ref_pending = 0;
2097 cpi->baseline_gf_interval = i;
2101 * Now decide how many bits should be allocated to the GF group as a
2102 * proportion of those remaining in the kf group.
2103 * The final key frame group in the clip is treated as a special case
2104 * where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
2105 * This is also important for short clips where there may only be one
2108 if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats.count -
2109 cpi->common.current_video_frame))
2111 cpi->twopass.kf_group_bits =
2112 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
2115 /* Calculate the bits to be allocated to the group as a whole */
2116 if ((cpi->twopass.kf_group_bits > 0) &&
2117 (cpi->twopass.kf_group_error_left > 0))
2119 cpi->twopass.gf_group_bits =
2120 (int64_t)(cpi->twopass.kf_group_bits *
2121 (gf_group_err / cpi->twopass.kf_group_error_left));
2124 cpi->twopass.gf_group_bits = 0;
2126 cpi->twopass.gf_group_bits =
2127 (cpi->twopass.gf_group_bits < 0)
2129 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
2130 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
2132 /* Clip cpi->twopass.gf_group_bits based on user supplied data rate
2133 * variability limit (cpi->oxcf.two_pass_vbrmax_section)
2135 if (cpi->twopass.gf_group_bits >
2136 (int64_t)max_bits * cpi->baseline_gf_interval)
2137 cpi->twopass.gf_group_bits =
2138 (int64_t)max_bits * cpi->baseline_gf_interval;
2140 /* Reset the file position */
2141 reset_fpf_position(cpi, start_pos);
2143 /* Update the record of error used so far (only done once per gf group) */
2144 cpi->twopass.modified_error_used += gf_group_err;
2146 /* Assign bits to the arf or gf. */
2147 for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) {
2149 int allocation_chunks;
2150 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
2153 /* For ARF frames */
2154 if (cpi->source_alt_ref_pending && i == 0)
2157 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
2159 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
2161 Boost += (cpi->baseline_gf_interval * 50);
2163 /* Set max and minimum boost and hence minimum allocation */
2164 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
2165 Boost = ((cpi->baseline_gf_interval + 1) * 200);
2166 else if (Boost < 125)
2170 ((cpi->baseline_gf_interval + 1) * 100) + Boost;
2172 /* Else for standard golden frames */
2175 /* boost based on inter / intra ratio of subsequent frames */
2176 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
2178 /* Set max and minimum boost and hence minimum allocation */
2179 if (Boost > (cpi->baseline_gf_interval * 150))
2180 Boost = (cpi->baseline_gf_interval * 150);
2181 else if (Boost < 125)
2185 (cpi->baseline_gf_interval * 100) + (Boost - 100);
2188 /* Normalize Altboost and allocations chunck down to prevent overflow */
2189 while (Boost > 1000)
2192 allocation_chunks /= 2;
2195 /* Calculate the number of bits to be spent on the gf or arf based on
2198 gf_bits = (int)((double)Boost *
2199 (cpi->twopass.gf_group_bits /
2200 (double)allocation_chunks));
2202 /* If the frame that is to be boosted is simpler than the average for
2203 * the gf/arf group then use an alternative calculation
2204 * based on the error score of the frame itself
2206 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
2208 double alt_gf_grp_bits;
2212 (double)cpi->twopass.kf_group_bits *
2213 (mod_frame_err * (double)cpi->baseline_gf_interval) /
2214 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
2216 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
2217 (double)allocation_chunks));
2219 if (gf_bits > alt_gf_bits)
2221 gf_bits = alt_gf_bits;
2224 /* Else if it is harder than other frames in the group make sure it at
2225 * least receives an allocation in keeping with its relative error
2226 * score, otherwise it may be worse off than an "un-boosted" frame
2231 (int)((double)cpi->twopass.kf_group_bits *
2233 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
2235 if (alt_gf_bits > gf_bits)
2237 gf_bits = alt_gf_bits;
2241 /* Apply an additional limit for CBR */
2242 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2244 if (cpi->twopass.gf_bits > (int)(cpi->buffer_level >> 1))
2245 cpi->twopass.gf_bits = (int)(cpi->buffer_level >> 1);
2248 /* Dont allow a negative value for gf_bits */
2252 /* Add in minimum for a frame */
2253 gf_bits += cpi->min_frame_bandwidth;
2257 cpi->twopass.gf_bits = gf_bits;
2259 if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)))
2261 /* Per frame bit target for this frame */
2262 cpi->per_frame_bandwidth = gf_bits;
2267 /* Adjust KF group bits and error remainin */
2268 cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
2269 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
2271 if (cpi->twopass.kf_group_bits < 0)
2272 cpi->twopass.kf_group_bits = 0;
2274 /* Note the error score left in the remaining frames of the group.
2275 * For normal GFs we want to remove the error score for the first
2276 * frame of the group (except in Key frame case where this has
2279 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
2280 cpi->twopass.gf_group_error_left = (int)(gf_group_err -
2281 gf_first_frame_err);
2283 cpi->twopass.gf_group_error_left = (int) gf_group_err;
2285 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
2287 if (cpi->twopass.gf_group_bits < 0)
2288 cpi->twopass.gf_group_bits = 0;
2290 /* This condition could fail if there are two kfs very close together
2291 * despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the
2292 * calculation of cpi->twopass.alt_extra_bits.
2294 if ( cpi->baseline_gf_interval >= 3 )
2297 int boost = (cpi->source_alt_ref_pending)
2298 ? b_boost : cpi->gfu_boost;
2300 int boost = cpi->gfu_boost;
2306 pct_extra = (boost - 100) / 50;
2307 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2309 cpi->twopass.alt_extra_bits =
2310 (cpi->twopass.gf_group_bits * pct_extra) / 100;
2311 cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
2312 cpi->twopass.alt_extra_bits /=
2313 ((cpi->baseline_gf_interval-1)>>1);
2316 cpi->twopass.alt_extra_bits = 0;
2319 cpi->twopass.alt_extra_bits = 0;
2322 /* Adjustments based on a measure of complexity of the section */
2323 if (cpi->common.frame_type != KEY_FRAME)
2325 FIRSTPASS_STATS sectionstats;
2328 zero_stats(§ionstats);
2329 reset_fpf_position(cpi, start_pos);
2331 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
2333 input_stats(cpi, &next_frame);
2334 accumulate_stats(§ionstats, &next_frame);
2337 avg_stats(§ionstats);
2339 cpi->twopass.section_intra_rating = (unsigned int)
2340 (sectionstats.intra_error /
2341 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2343 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2344 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2346 if (cpi->twopass.section_max_qfactor < 0.80)
2347 cpi->twopass.section_max_qfactor = 0.80;
2349 reset_fpf_position(cpi, start_pos);
2353 /* Allocate bits to a normal frame that is neither a gf an arf or a key frame. */
2354 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2356 int target_frame_size;
2358 double modified_err;
2359 double err_fraction;
2361 int max_bits = frame_max_bits(cpi); /* Max for a single frame */
2363 /* Calculate modified prediction error used in bit allocation */
2364 modified_err = calculate_modified_err(cpi, this_frame);
2366 /* What portion of the remaining GF group error is used by this frame */
2367 if (cpi->twopass.gf_group_error_left > 0)
2368 err_fraction = modified_err / cpi->twopass.gf_group_error_left;
2372 /* How many of those bits available for allocation should we give it? */
2373 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction);
2375 /* Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits)
2378 if (target_frame_size < 0)
2379 target_frame_size = 0;
2382 if (target_frame_size > max_bits)
2383 target_frame_size = max_bits;
2385 if (target_frame_size > cpi->twopass.gf_group_bits)
2386 target_frame_size = cpi->twopass.gf_group_bits;
2389 /* Adjust error and bits remaining */
2390 cpi->twopass.gf_group_error_left -= (int)modified_err;
2391 cpi->twopass.gf_group_bits -= target_frame_size;
2393 if (cpi->twopass.gf_group_bits < 0)
2394 cpi->twopass.gf_group_bits = 0;
2396 /* Add in the minimum number of bits that is set aside for every frame. */
2397 target_frame_size += cpi->min_frame_bandwidth;
2399 /* Every other frame gets a few extra bits */
2400 if ( (cpi->frames_since_golden & 0x01) &&
2401 (cpi->frames_till_gf_update_due > 0) )
2403 target_frame_size += cpi->twopass.alt_extra_bits;
2406 /* Per frame bit target for this frame */
2407 cpi->per_frame_bandwidth = target_frame_size;
2410 void vp8_second_pass(VP8_COMP *cpi)
2413 int frames_left = (int)(cpi->twopass.total_stats.count - cpi->common.current_video_frame);
2415 FIRSTPASS_STATS this_frame = {0};
2416 FIRSTPASS_STATS this_frame_copy;
2418 double this_frame_intra_error;
2419 double this_frame_coded_error;
2423 if (!cpi->twopass.stats_in)
2428 vp8_clear_system_state();
2430 if (EOF == input_stats(cpi, &this_frame))
2433 this_frame_intra_error = this_frame.intra_error;
2434 this_frame_coded_error = this_frame.coded_error;
2436 /* keyframe and section processing ! */
2437 if (cpi->twopass.frames_to_key == 0)
2439 /* Define next KF group and assign bits to it */
2440 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2441 find_next_key_frame(cpi, &this_frame_copy);
2443 /* Special case: Error error_resilient_mode mode does not make much
2444 * sense for two pass but with its current meaning but this code is
2445 * designed to stop outlandish behaviour if someone does set it when
2446 * using two pass. It effectively disables GF groups. This is
2447 * temporary code till we decide what should really happen in this
2450 if (cpi->oxcf.error_resilient_mode)
2452 cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
2453 cpi->twopass.gf_group_error_left =
2454 (int)cpi->twopass.kf_group_error_left;
2455 cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
2456 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2457 cpi->source_alt_ref_pending = 0;
2462 /* Is this a GF / ARF (Note that a KF is always also a GF) */
2463 if (cpi->frames_till_gf_update_due == 0)
2465 /* Define next gf group and assign bits to it */
2466 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2467 define_gf_group(cpi, &this_frame_copy);
2469 /* If we are going to code an altref frame at the end of the group
2470 * and the current frame is not a key frame.... If the previous
2471 * group used an arf this frame has already benefited from that arf
2472 * boost and it should not be given extra bits If the previous
2473 * group was NOT coded using arf we may want to apply some boost to
2476 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2478 /* Assign a standard frames worth of bits from those allocated
2481 int bak = cpi->per_frame_bandwidth;
2482 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2483 assign_std_frame_bits(cpi, &this_frame_copy);
2484 cpi->per_frame_bandwidth = bak;
2488 /* Otherwise this is an ordinary frame */
2491 /* Special case: Error error_resilient_mode mode does not make much
2492 * sense for two pass but with its current meaning but this code is
2493 * designed to stop outlandish behaviour if someone does set it
2494 * when using two pass. It effectively disables GF groups. This is
2495 * temporary code till we decide what should really happen in this
2498 if (cpi->oxcf.error_resilient_mode)
2500 cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
2502 if (cpi->common.frame_type != KEY_FRAME)
2504 /* Assign bits from those allocated to the GF group */
2505 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2506 assign_std_frame_bits(cpi, &this_frame_copy);
2511 /* Assign bits from those allocated to the GF group */
2512 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2513 assign_std_frame_bits(cpi, &this_frame_copy);
2517 /* Keep a globally available copy of this and the next frame's iiratio. */
2518 cpi->twopass.this_iiratio = (unsigned int)(this_frame_intra_error /
2519 DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
2521 FIRSTPASS_STATS next_frame;
2522 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2524 cpi->twopass.next_iiratio = (unsigned int)(next_frame.intra_error /
2525 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2529 /* Set nominal per second bandwidth for this frame */
2530 cpi->target_bandwidth = (int)
2531 (cpi->per_frame_bandwidth * cpi->output_framerate);
2532 if (cpi->target_bandwidth < 0)
2533 cpi->target_bandwidth = 0;
2536 /* Account for mv, mode and other overheads. */
2537 overhead_bits = (int)estimate_modemvcost(
2538 cpi, &cpi->twopass.total_left_stats );
2540 /* Special case code for first frame. */
2541 if (cpi->common.current_video_frame == 0)
2543 cpi->twopass.est_max_qcorrection_factor = 1.0;
2545 /* Set a cq_level in constrained quality mode. */
2546 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2552 &cpi->twopass.total_left_stats,
2553 (int)(cpi->twopass.bits_left / frames_left),
2556 cpi->cq_target_quality = cpi->oxcf.cq_level;
2557 if ( est_cq > cpi->cq_target_quality )
2558 cpi->cq_target_quality = est_cq;
2561 /* guess at maxq needed in 2nd pass */
2562 cpi->twopass.maxq_max_limit = cpi->worst_quality;
2563 cpi->twopass.maxq_min_limit = cpi->best_quality;
2565 tmp_q = estimate_max_q(
2567 &cpi->twopass.total_left_stats,
2568 (int)(cpi->twopass.bits_left / frames_left),
2571 /* Limit the maxq value returned subsequently.
2572 * This increases the risk of overspend or underspend if the initial
2573 * estimate for the clip is bad, but helps prevent excessive
2574 * variation in Q, especially near the end of a clip
2575 * where for example a small overspend may cause Q to crash
2577 cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2578 ? (tmp_q + 32) : cpi->worst_quality;
2579 cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2580 ? (tmp_q - 32) : cpi->best_quality;
2582 cpi->active_worst_quality = tmp_q;
2583 cpi->ni_av_qi = tmp_q;
2586 /* The last few frames of a clip almost always have to few or too many
2587 * bits and for the sake of over exact rate control we dont want to make
2588 * radical adjustments to the allowed quantizer range just to use up a
2589 * few surplus bits or get beneath the target rate.
2591 else if ( (cpi->common.current_video_frame <
2592 (((unsigned int)cpi->twopass.total_stats.count * 255)>>8)) &&
2593 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2594 (unsigned int)cpi->twopass.total_stats.count) )
2596 if (frames_left < 1)
2599 tmp_q = estimate_max_q(
2601 &cpi->twopass.total_left_stats,
2602 (int)(cpi->twopass.bits_left / frames_left),
2605 /* Move active_worst_quality but in a damped way */
2606 if (tmp_q > cpi->active_worst_quality)
2607 cpi->active_worst_quality ++;
2608 else if (tmp_q < cpi->active_worst_quality)
2609 cpi->active_worst_quality --;
2611 cpi->active_worst_quality =
2612 ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2615 cpi->twopass.frames_to_key --;
2617 /* Update the total stats remaining sturcture */
2618 subtract_stats(&cpi->twopass.total_left_stats, &this_frame );
2622 static int test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2624 int is_viable_kf = 0;
2626 /* Does the frame satisfy the primary criteria of a key frame
2627 * If so, then examine how well it predicts subsequent frames
2629 if ((this_frame->pcnt_second_ref < 0.10) &&
2630 (next_frame->pcnt_second_ref < 0.10) &&
2631 ((this_frame->pcnt_inter < 0.05) ||
2633 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2634 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2635 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2636 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2637 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2644 FIRSTPASS_STATS *start_pos;
2646 FIRSTPASS_STATS local_next_frame;
2648 double boost_score = 0.0;
2649 double old_boost_score = 0.0;
2650 double decay_accumulator = 1.0;
2651 double next_iiratio;
2653 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2655 /* Note the starting file position so we can reset to it */
2656 start_pos = cpi->twopass.stats_in;
2658 /* Examine how well the key frame predicts subsequent frames */
2659 for (i = 0 ; i < 16; i++)
2661 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
2663 if (next_iiratio > RMAX)
2664 next_iiratio = RMAX;
2666 /* Cumulative effect of decay in prediction quality */
2667 if (local_next_frame.pcnt_inter > 0.85)
2668 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2670 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2672 /* Keep a running total */
2673 boost_score += (decay_accumulator * next_iiratio);
2675 /* Test various breakout clauses */
2676 if ((local_next_frame.pcnt_inter < 0.05) ||
2677 (next_iiratio < 1.5) ||
2678 (((local_next_frame.pcnt_inter -
2679 local_next_frame.pcnt_neutral) < 0.20) &&
2680 (next_iiratio < 3.0)) ||
2681 ((boost_score - old_boost_score) < 0.5) ||
2682 (local_next_frame.intra_error < 200)
2688 old_boost_score = boost_score;
2690 /* Get the next frame details */
2691 if (EOF == input_stats(cpi, &local_next_frame))
2695 /* If there is tolerable prediction for at least the next 3 frames
2696 * then break out else discard this pottential key frame and move on
2698 if (boost_score > 5.0 && (i > 3))
2702 /* Reset the file position */
2703 reset_fpf_position(cpi, start_pos);
2709 return is_viable_kf;
2711 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2714 FIRSTPASS_STATS last_frame;
2715 FIRSTPASS_STATS first_frame;
2716 FIRSTPASS_STATS next_frame;
2717 FIRSTPASS_STATS *start_position;
2719 double decay_accumulator = 1.0;
2720 double boost_score = 0;
2721 double old_boost_score = 0.0;
2722 double loop_decay_rate;
2724 double kf_mod_err = 0.0;
2725 double kf_group_err = 0.0;
2726 double kf_group_intra_err = 0.0;
2727 double kf_group_coded_err = 0.0;
2728 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2730 vpx_memset(&next_frame, 0, sizeof(next_frame));
2732 vp8_clear_system_state();
2733 start_position = cpi->twopass.stats_in;
2735 cpi->common.frame_type = KEY_FRAME;
2737 /* is this a forced key frame by interval */
2738 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2740 /* Clear the alt ref active flag as this can never be active on a key
2743 cpi->source_alt_ref_active = 0;
2745 /* Kf is always a gf so clear frames till next gf counter */
2746 cpi->frames_till_gf_update_due = 0;
2748 cpi->twopass.frames_to_key = 1;
2750 /* Take a copy of the initial frame details */
2751 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2753 cpi->twopass.kf_group_bits = 0;
2754 cpi->twopass.kf_group_error_left = 0;
2756 kf_mod_err = calculate_modified_err(cpi, this_frame);
2758 /* find the next keyframe */
2760 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end)
2762 /* Accumulate kf group error */
2763 kf_group_err += calculate_modified_err(cpi, this_frame);
2765 /* These figures keep intra and coded error counts for all frames
2766 * including key frames in the group. The effect of the key frame
2767 * itself can be subtracted out using the first_frame data
2770 kf_group_intra_err += this_frame->intra_error;
2771 kf_group_coded_err += this_frame->coded_error;
2773 /* load a the next frame's stats */
2774 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2775 input_stats(cpi, this_frame);
2777 /* Provided that we are not at the end of the file... */
2778 if (cpi->oxcf.auto_key
2779 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2781 /* Normal scene cut check */
2782 if ( ( i >= MIN_GF_INTERVAL ) &&
2783 test_candidate_kf(cpi, &last_frame, this_frame, &next_frame) )
2788 /* How fast is prediction quality decaying */
2789 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2791 /* We want to know something about the recent past... rather than
2792 * as used elsewhere where we are concened with decay in prediction
2793 * quality since the last GF or KF.
2795 recent_loop_decay[i%8] = loop_decay_rate;
2796 decay_accumulator = 1.0;
2797 for (j = 0; j < 8; j++)
2799 decay_accumulator = decay_accumulator * recent_loop_decay[j];
2802 /* Special check for transition or high motion followed by a
2805 if ( detect_transition_to_still( cpi, i,
2806 (cpi->key_frame_frequency-i),
2808 decay_accumulator ) )
2814 /* Step on to the next frame */
2815 cpi->twopass.frames_to_key ++;
2817 /* If we don't have a real key frame within the next two
2818 * forcekeyframeevery intervals then break out of the loop.
2820 if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2823 cpi->twopass.frames_to_key ++;
2828 /* If there is a max kf interval set by the user we must obey it.
2829 * We already breakout of the loop above at 2x max.
2830 * This code centers the extra kf if the actual natural
2831 * interval is between 1x and 2x
2833 if (cpi->oxcf.auto_key
2834 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency )
2836 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2837 FIRSTPASS_STATS tmp_frame;
2839 cpi->twopass.frames_to_key /= 2;
2841 /* Copy first frame details */
2842 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2844 /* Reset to the start of the group */
2845 reset_fpf_position(cpi, start_position);
2848 kf_group_intra_err = 0;
2849 kf_group_coded_err = 0;
2851 /* Rescan to get the correct error data for the forced kf group */
2852 for( i = 0; i < cpi->twopass.frames_to_key; i++ )
2854 /* Accumulate kf group errors */
2855 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2856 kf_group_intra_err += tmp_frame.intra_error;
2857 kf_group_coded_err += tmp_frame.coded_error;
2859 /* Load a the next frame's stats */
2860 input_stats(cpi, &tmp_frame);
2863 /* Reset to the start of the group */
2864 reset_fpf_position(cpi, current_pos);
2866 cpi->next_key_frame_forced = 1;
2869 cpi->next_key_frame_forced = 0;
2871 /* Special case for the last frame of the file */
2872 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
2874 /* Accumulate kf group error */
2875 kf_group_err += calculate_modified_err(cpi, this_frame);
2877 /* These figures keep intra and coded error counts for all frames
2878 * including key frames in the group. The effect of the key frame
2879 * itself can be subtracted out using the first_frame data
2882 kf_group_intra_err += this_frame->intra_error;
2883 kf_group_coded_err += this_frame->coded_error;
2886 /* Calculate the number of bits that should be assigned to the kf group. */
2887 if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0))
2889 /* Max for a single normal frame (not key frame) */
2890 int max_bits = frame_max_bits(cpi);
2892 /* Maximum bits for the kf group */
2893 int64_t max_grp_bits;
2895 /* Default allocation based on bits left and relative
2896 * complexity of the section
2898 cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left *
2900 cpi->twopass.modified_error_left ));
2902 /* Clip based on maximum per frame rate defined by the user. */
2903 max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2904 if (cpi->twopass.kf_group_bits > max_grp_bits)
2905 cpi->twopass.kf_group_bits = max_grp_bits;
2907 /* Additional special case for CBR if buffer is getting full. */
2908 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2910 int64_t opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2911 int64_t buffer_lvl = cpi->buffer_level;
2913 /* If the buffer is near or above the optimal and this kf group is
2914 * not being allocated much then increase the allocation a bit.
2916 if (buffer_lvl >= opt_buffer_lvl)
2918 int64_t high_water_mark = (opt_buffer_lvl +
2919 cpi->oxcf.maximum_buffer_size) >> 1;
2921 int64_t av_group_bits;
2923 /* Av bits per frame * number of frames */
2924 av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
2925 (int64_t)cpi->twopass.frames_to_key;
2927 /* We are at or above the maximum. */
2928 if (cpi->buffer_level >= high_water_mark)
2930 int64_t min_group_bits;
2932 min_group_bits = av_group_bits +
2933 (int64_t)(buffer_lvl -
2936 if (cpi->twopass.kf_group_bits < min_group_bits)
2937 cpi->twopass.kf_group_bits = min_group_bits;
2939 /* We are above optimal but below the maximum */
2940 else if (cpi->twopass.kf_group_bits < av_group_bits)
2942 int64_t bits_below_av = av_group_bits -
2943 cpi->twopass.kf_group_bits;
2945 cpi->twopass.kf_group_bits +=
2946 (int64_t)((double)bits_below_av *
2947 (double)(buffer_lvl - opt_buffer_lvl) /
2948 (double)(high_water_mark - opt_buffer_lvl));
2954 cpi->twopass.kf_group_bits = 0;
2956 /* Reset the first pass file position */
2957 reset_fpf_position(cpi, start_position);
2959 /* determine how big to make this keyframe based on how well the
2960 * subsequent frames use inter blocks
2962 decay_accumulator = 1.0;
2964 loop_decay_rate = 1.00; /* Starting decay rate */
2966 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2970 if (EOF == input_stats(cpi, &next_frame))
2973 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
2974 r = (IIKFACTOR2 * next_frame.intra_error /
2975 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2977 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2978 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2983 /* How fast is prediction quality decaying */
2984 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2986 decay_accumulator = decay_accumulator * loop_decay_rate;
2987 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2989 boost_score += (decay_accumulator * r);
2991 if ((i > MIN_GF_INTERVAL) &&
2992 ((boost_score - old_boost_score) < 1.0))
2997 old_boost_score = boost_score;
3002 FIRSTPASS_STATS sectionstats;
3005 zero_stats(§ionstats);
3006 reset_fpf_position(cpi, start_position);
3008 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
3010 input_stats(cpi, &next_frame);
3011 accumulate_stats(§ionstats, &next_frame);
3014 avg_stats(§ionstats);
3016 cpi->twopass.section_intra_rating = (unsigned int)
3017 (sectionstats.intra_error
3018 / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
3020 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
3021 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
3023 if (cpi->twopass.section_max_qfactor < 0.80)
3024 cpi->twopass.section_max_qfactor = 0.80;
3027 /* When using CBR apply additional buffer fullness related upper limits */
3028 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3032 if (cpi->drop_frames_allowed)
3034 int df_buffer_level = (int)(cpi->oxcf.drop_frames_water_mark
3035 * (cpi->oxcf.optimal_buffer_level / 100));
3037 if (cpi->buffer_level > df_buffer_level)
3038 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
3042 else if (cpi->buffer_level > 0)
3044 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
3051 if (boost_score > max_boost)
3052 boost_score = max_boost;
3055 /* Reset the first pass file position */
3056 reset_fpf_position(cpi, start_position);
3058 /* Work out how many bits to allocate for the key frame itself */
3061 int kf_boost = (int)boost_score;
3062 int allocation_chunks;
3063 int Counter = cpi->twopass.frames_to_key;
3065 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
3066 /* Min boost based on kf interval */
3069 while ((kf_boost < 48) && (Counter > 0))
3079 kf_boost += ((Counter + 1) >> 1);
3081 if (kf_boost > 48) kf_boost = 48;
3084 /* bigger frame sizes need larger kf boosts, smaller frames smaller
3087 if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
3088 kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
3089 else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
3090 kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
3093 kf_boost = (int)((double)kf_boost * 100.0) >> 4; /* Scale 16 to 100 */
3098 * We do three calculations for kf size.
3099 * The first is based on the error score for the whole kf group.
3100 * The second (optionaly) on the key frames own error if this is
3101 * smaller than the average for the group.
3102 * The final one insures that the frame receives at least the
3103 * allocation it would have received based on its own error score vs
3104 * the error score remaining
3105 * Special case if the sequence appears almost totaly static
3106 * as measured by the decay accumulator. In this case we want to
3107 * spend almost all of the bits on the key frame.
3108 * cpi->twopass.frames_to_key-1 because key frame itself is taken
3109 * care of by kf_boost.
3111 if ( decay_accumulator >= 0.99 )
3114 ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
3119 ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
3122 /* Normalize Altboost and allocations chunck down to prevent overflow */
3123 while (kf_boost > 1000)
3126 allocation_chunks /= 2;
3129 cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
3131 /* Calculate the number of bits to be spent on the key frame */
3132 cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
3134 /* Apply an additional limit for CBR */
3135 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3137 if (cpi->twopass.kf_bits > (int)((3 * cpi->buffer_level) >> 2))
3138 cpi->twopass.kf_bits = (int)((3 * cpi->buffer_level) >> 2);
3141 /* If the key frame is actually easier than the average for the
3142 * kf group (which does sometimes happen... eg a blank intro frame)
3143 * Then use an alternate calculation based on the kf error score
3144 * which should give a smaller key frame.
3146 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key)
3148 double alt_kf_grp_bits =
3149 ((double)cpi->twopass.bits_left *
3150 (kf_mod_err * (double)cpi->twopass.frames_to_key) /
3151 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
3153 alt_kf_bits = (int)((double)kf_boost *
3154 (alt_kf_grp_bits / (double)allocation_chunks));
3156 if (cpi->twopass.kf_bits > alt_kf_bits)
3158 cpi->twopass.kf_bits = alt_kf_bits;
3161 /* Else if it is much harder than other frames in the group make sure
3162 * it at least receives an allocation in keeping with its relative
3168 (int)((double)cpi->twopass.bits_left *
3170 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
3172 if (alt_kf_bits > cpi->twopass.kf_bits)
3174 cpi->twopass.kf_bits = alt_kf_bits;
3178 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
3179 /* Add in the minimum frame allowance */
3180 cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
3182 /* Peer frame bit target for this frame */
3183 cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
3185 /* Convert to a per second bitrate */
3186 cpi->target_bandwidth = (int)(cpi->twopass.kf_bits *
3187 cpi->output_framerate);
3190 /* Note the total error score of the kf group minus the key frame itself */
3191 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
3193 /* Adjust the count of total modified error left. The count of bits left
3194 * is adjusted elsewhere based on real coded frame sizes
3196 cpi->twopass.modified_error_left -= kf_group_err;
3198 if (cpi->oxcf.allow_spatial_resampling)
3200 int resample_trigger = 0;
3201 int last_kf_resampled = 0;
3205 int new_width = cpi->oxcf.Width;
3206 int new_height = cpi->oxcf.Height;
3208 int projected_buffer_level = (int)cpi->buffer_level;
3211 double projected_bits_perframe;
3212 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
3213 double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
3214 double bits_per_frame;
3215 double av_bits_per_frame;
3216 double effective_size_ratio;
3218 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
3219 last_kf_resampled = 1;
3221 /* Set back to unscaled by defaults */
3222 cpi->common.horiz_scale = NORMAL;
3223 cpi->common.vert_scale = NORMAL;
3225 /* Calculate Average bits per frame. */
3226 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate);
3228 /* CBR... Use the clip average as the target for deciding resample */
3229 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3231 bits_per_frame = av_bits_per_frame;
3234 /* In VBR we want to avoid downsampling in easy section unless we
3235 * are under extreme pressure So use the larger of target bitrate
3236 * for this section or average bitrate for sequence
3240 /* This accounts for how hard the section is... */
3241 bits_per_frame = (double)
3242 (cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key);
3244 /* Dont turn to resampling in easy sections just because they
3245 * have been assigned a small number of bits
3247 if (bits_per_frame < av_bits_per_frame)
3248 bits_per_frame = av_bits_per_frame;
3251 /* bits_per_frame should comply with our minimum */
3252 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
3253 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
3255 /* Work out if spatial resampling is necessary */
3256 kf_q = estimate_kf_group_q(cpi, err_per_frame,
3257 (int)bits_per_frame, group_iiratio);
3259 /* If we project a required Q higher than the maximum allowed Q then
3260 * make a guess at the actual size of frames in this section
3262 projected_bits_perframe = bits_per_frame;
3265 while (tmp_q > cpi->worst_quality)
3267 projected_bits_perframe *= 1.04;
3271 /* Guess at buffer level at the end of the section */
3272 projected_buffer_level = (int)
3273 (cpi->buffer_level - (int)
3274 ((projected_bits_perframe - av_bits_per_frame) *
3275 cpi->twopass.frames_to_key));
3279 FILE *f = fopen("Subsamle.stt", "a");
3280 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);
3284 /* The trigger for spatial resampling depends on the various
3285 * parameters such as whether we are streaming (CBR) or VBR.
3287 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3289 /* Trigger resample if we are projected to fall below down
3290 * sample level or resampled last time and are projected to
3291 * remain below the up sample level
3293 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
3294 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
3295 resample_trigger = 1;
3297 resample_trigger = 0;
3301 int64_t clip_bits = (int64_t)(cpi->twopass.total_stats.count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->framerate));
3302 int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
3304 /* If triggered last time the threshold for triggering again is
3307 * Projected Q higher than allowed and Overspend > 5% of total
3310 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) ||
3311 ((kf_q > cpi->worst_quality) &&
3312 (over_spend > clip_bits / 20)))
3313 resample_trigger = 1;
3315 resample_trigger = 0;
3319 if (resample_trigger)
3321 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
3325 cpi->common.vert_scale = vscale_lookup[scale_val];
3326 cpi->common.horiz_scale = hscale_lookup[scale_val];
3328 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
3329 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
3331 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3332 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3334 /* Reducing the area to 1/4 does not reduce the complexity
3335 * (err_per_frame) to 1/4... effective_sizeratio attempts
3336 * to provide a crude correction for this
3338 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
3339 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
3341 /* Now try again and see what Q we get with the smaller
3344 kf_q = estimate_kf_group_q(cpi,
3345 err_per_frame * effective_size_ratio,
3346 (int)bits_per_frame, group_iiratio);
3350 FILE *f = fopen("Subsamle.stt", "a");
3351 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);
3357 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
3359 cpi->common.Width = new_width;
3360 cpi->common.Height = new_height;
3361 vp8_alloc_compressor_data(cpi);