2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
16 #include "encodeintra.h"
17 #include "vp8/common/setupintrarecon.h"
19 #include "firstpass.h"
20 #include "vpx_scale/vpxscale.h"
22 #include "vp8/common/extend.h"
23 #include "vp8/common/systemdependent.h"
24 #include "vpx_scale/yv12extend.h"
25 #include "vpx_mem/vpx_mem.h"
26 #include "vp8/common/swapyv12buffer.h"
29 #include "vp8/common/quant_common.h"
32 //#define OUTPUT_FPF 1
34 #if CONFIG_RUNTIME_CPU_DETECT
35 #define IF_RTCD(x) (x)
37 #define IF_RTCD(x) NULL
40 extern void vp8_build_block_offsets(MACROBLOCK *x);
41 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
42 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
43 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv);
44 extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
46 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10))
47 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10))
48 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
49 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
51 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
54 #define IIKFACTOR1 1.40
55 #define IIKFACTOR2 1.5
59 #define KF_MB_INTRA_MIN 300
60 #define GF_MB_INTRA_MIN 200
62 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
64 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
65 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
69 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
70 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
73 static const int cq_level[QINDEX_RANGE] =
75 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
76 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
77 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
78 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
79 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
80 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
81 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
82 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
85 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
87 // Resets the first pass file to the given position using a relative seek from the current position
88 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
90 cpi->twopass.stats_in = Position;
93 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
95 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
98 *next_frame = *cpi->twopass.stats_in;
102 // Read frame stats at an offset from the current position
103 static int read_frame_stats( VP8_COMP *cpi,
104 FIRSTPASS_STATS *frame_stats,
107 FIRSTPASS_STATS * fps_ptr = cpi->twopass.stats_in;
109 // Check legality of offset
112 if ( &fps_ptr[offset] >= cpi->twopass.stats_in_end )
115 else if ( offset < 0 )
117 if ( &fps_ptr[offset] < cpi->twopass.stats_in_start )
121 *frame_stats = fps_ptr[offset];
125 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
127 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
130 *fps = *cpi->twopass.stats_in;
131 cpi->twopass.stats_in =
132 (void*)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
136 static void output_stats(const VP8_COMP *cpi,
137 struct vpx_codec_pkt_list *pktlist,
138 FIRSTPASS_STATS *stats)
140 struct vpx_codec_cx_pkt pkt;
141 pkt.kind = VPX_CODEC_STATS_PKT;
142 pkt.data.twopass_stats.buf = stats;
143 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
144 vpx_codec_pkt_list_add(pktlist, &pkt);
151 fpfile = fopen("firstpass.stt", "a");
153 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
154 " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
159 stats->ssim_weighted_pred_err,
162 stats->pcnt_second_ref,
170 stats->mv_in_out_count,
178 static void zero_stats(FIRSTPASS_STATS *section)
180 section->frame = 0.0;
181 section->intra_error = 0.0;
182 section->coded_error = 0.0;
183 section->ssim_weighted_pred_err = 0.0;
184 section->pcnt_inter = 0.0;
185 section->pcnt_motion = 0.0;
186 section->pcnt_second_ref = 0.0;
187 section->pcnt_neutral = 0.0;
189 section->mvr_abs = 0.0;
191 section->mvc_abs = 0.0;
194 section->mv_in_out_count = 0.0;
195 section->count = 0.0;
196 section->duration = 1.0;
199 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
201 section->frame += frame->frame;
202 section->intra_error += frame->intra_error;
203 section->coded_error += frame->coded_error;
204 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
205 section->pcnt_inter += frame->pcnt_inter;
206 section->pcnt_motion += frame->pcnt_motion;
207 section->pcnt_second_ref += frame->pcnt_second_ref;
208 section->pcnt_neutral += frame->pcnt_neutral;
209 section->MVr += frame->MVr;
210 section->mvr_abs += frame->mvr_abs;
211 section->MVc += frame->MVc;
212 section->mvc_abs += frame->mvc_abs;
213 section->MVrv += frame->MVrv;
214 section->MVcv += frame->MVcv;
215 section->mv_in_out_count += frame->mv_in_out_count;
216 section->count += frame->count;
217 section->duration += frame->duration;
220 static void avg_stats(FIRSTPASS_STATS *section)
222 if (section->count < 1.0)
225 section->intra_error /= section->count;
226 section->coded_error /= section->count;
227 section->ssim_weighted_pred_err /= section->count;
228 section->pcnt_inter /= section->count;
229 section->pcnt_second_ref /= section->count;
230 section->pcnt_neutral /= section->count;
231 section->pcnt_motion /= section->count;
232 section->MVr /= section->count;
233 section->mvr_abs /= section->count;
234 section->MVc /= section->count;
235 section->mvc_abs /= section->count;
236 section->MVrv /= section->count;
237 section->MVcv /= section->count;
238 section->mv_in_out_count /= section->count;
239 section->duration /= section->count;
242 // Calculate a modified Error used in distributing bits between easier and harder frames
243 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
245 double av_err = cpi->twopass.total_stats->ssim_weighted_pred_err;
246 double this_err = this_frame->ssim_weighted_pred_err;
249 //double relative_next_iiratio;
250 //double next_iiratio;
251 //double sum_iiratio;
254 //FIRSTPASS_STATS next_frame;
255 //FIRSTPASS_STATS *start_pos;
257 /*start_pos = cpi->twopass.stats_in;
260 while ( (i < 1) && input_stats(cpi,&next_frame) != EOF )
263 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
264 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio;
265 sum_iiratio += next_iiratio;
270 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->twopass.avg_iiratio * (double)i);
274 relative_next_iiratio = 1.0;
276 reset_fpf_position(cpi, start_pos);*/
278 if (this_err > av_err)
279 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
281 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
284 relative_next_iiratio = pow(relative_next_iiratio,0.25);
285 modified_err = modified_err * relative_next_iiratio;
291 static const double weight_table[256] = {
292 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
293 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
294 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
295 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
296 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
297 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
298 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
299 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
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,
312 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
313 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
314 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
315 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
316 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
317 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
318 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
319 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
320 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
321 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
322 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
323 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
326 static double simple_weight(YV12_BUFFER_CONFIG *source)
330 unsigned char *src = source->y_buffer;
331 double sum_weights = 0.0;
333 // Loop throught the Y plane raw examining levels and creating a weight for the image
334 i = source->y_height;
340 sum_weights += weight_table[ *src];
343 src -= source->y_width;
344 src += source->y_stride;
347 sum_weights /= (source->y_height * source->y_width);
353 // This function returns the current per frame maximum bitrate target
354 static int frame_max_bits(VP8_COMP *cpi)
356 // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
359 // For CBR we need to also consider buffer fullness.
360 // If we are running below the optimal level then we need to gradually tighten up on max_bits.
361 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
363 double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
365 // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
366 max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
368 // If our buffer is below the optimum level
369 if (buffer_fullness_ratio < 1.0)
371 // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
372 int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
374 max_bits = (int)(max_bits * buffer_fullness_ratio);
376 if (max_bits < min_max_bits)
377 max_bits = min_max_bits; // Lowest value we will set ... which should allow the buffer to refil.
383 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
384 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));
387 // Trap case where we are out of bits
394 void vp8_init_first_pass(VP8_COMP *cpi)
396 zero_stats(cpi->twopass.total_stats);
399 void vp8_end_first_pass(VP8_COMP *cpi)
401 output_stats(cpi, cpi->output_pkt_list, cpi->twopass.total_stats);
404 static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset )
406 MACROBLOCKD * const xd = & x->e_mbd;
407 BLOCK *b = &x->block[0];
408 BLOCKD *d = &x->e_mbd.block[0];
410 unsigned char *src_ptr = (*(b->base_src) + b->src);
411 int src_stride = b->src_stride;
412 unsigned char *ref_ptr;
413 int ref_stride=d->pre_stride;
415 // Set up pointers for this macro block recon buffer
416 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
418 ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre );
420 VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_stride, ref_ptr, ref_stride, (unsigned int *)(best_motion_err));
423 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
424 int_mv *ref_mv, MV *best_mv,
425 YV12_BUFFER_CONFIG *recon_buffer,
426 int *best_motion_err, int recon_yoffset )
428 MACROBLOCKD *const xd = & x->e_mbd;
429 BLOCK *b = &x->block[0];
430 BLOCKD *d = &x->e_mbd.block[0];
436 int step_param = 3; //3; // Dont search over full range for first pass
437 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3;
439 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
440 int new_mv_mode_penalty = 256;
442 // override the default variance function to use MSE
443 v_fn_ptr.vf = VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16);
445 // Set up pointers for this macro block recon buffer
446 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
448 // Initial step/diamond search centred on best mv
450 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param,
451 x->sadperbit16, &num00, &v_fn_ptr,
453 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
454 tmp_err += new_mv_mode_penalty;
456 if (tmp_err < *best_motion_err)
458 *best_motion_err = tmp_err;
459 best_mv->row = tmp_mv.as_mv.row;
460 best_mv->col = tmp_mv.as_mv.col;
463 // Further step/diamond searches as necessary
467 while (n < further_steps)
475 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv,
476 step_param + n, x->sadperbit16,
477 &num00, &v_fn_ptr, x->mvcost,
479 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
480 tmp_err += new_mv_mode_penalty;
482 if (tmp_err < *best_motion_err)
484 *best_motion_err = tmp_err;
485 best_mv->row = tmp_mv.as_mv.row;
486 best_mv->col = tmp_mv.as_mv.col;
492 void vp8_first_pass(VP8_COMP *cpi)
495 MACROBLOCK *const x = & cpi->mb;
496 VP8_COMMON *const cm = & cpi->common;
497 MACROBLOCKD *const xd = & x->e_mbd;
499 int recon_yoffset, recon_uvoffset;
500 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
501 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
502 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
503 int recon_y_stride = lst_yv12->y_stride;
504 int recon_uv_stride = lst_yv12->uv_stride;
505 long long intra_error = 0;
506 long long coded_error = 0;
508 int sum_mvr = 0, sum_mvc = 0;
509 int sum_mvr_abs = 0, sum_mvc_abs = 0;
510 int sum_mvrs = 0, sum_mvcs = 0;
513 int second_ref_count = 0;
514 int intrapenalty = 256;
515 int neutral_count = 0;
517 int sum_in_vectors = 0;
521 zero_ref_mv.as_int = 0;
523 vp8_clear_system_state(); //__asm emms;
525 x->src = * cpi->Source;
529 x->partition_info = x->pi;
531 xd->mode_info_context = cm->mi;
533 vp8_build_block_offsets(x);
535 vp8_setup_block_dptrs(&x->e_mbd);
537 vp8_setup_block_ptrs(x);
539 // set up frame new frame for intra coded blocks
540 vp8_setup_intra_recon(new_yv12);
541 vp8cx_frame_init_quantizer(cpi);
543 // Initialise the MV cost table to the defaults
544 //if( cm->current_video_frame == 0)
547 int flag[2] = {1, 1};
548 vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
549 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
550 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
553 // for each macroblock row in image
554 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
558 best_ref_mv.as_int = 0;
560 // reset above block coeffs
561 xd->up_available = (mb_row != 0);
562 recon_yoffset = (mb_row * recon_y_stride * 16);
563 recon_uvoffset = (mb_row * recon_uv_stride * 8);
565 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
566 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
567 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
570 // for each macroblock col in image
571 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
574 int gf_motion_error = INT_MAX;
575 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
577 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
578 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
579 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
580 xd->left_available = (mb_col != 0);
582 //Copy current mb to a buffer
583 RECON_INVOKE(&xd->rtcd->recon, copy16x16)(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
585 // do intra 16x16 prediction
586 this_error = vp8_encode_intra(cpi, x, use_dc_pred);
588 // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
589 // We do not have special cases in first pass for 0,0 and nearest etc so all inter modes carry an overhead cost estimate fot the mv.
590 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
591 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
592 this_error += intrapenalty;
594 // Cumulative intra error total
595 intra_error += (long long)this_error;
597 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
598 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
599 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
601 // Other than for the first frame do a motion search
602 if (cm->current_video_frame > 0)
604 BLOCKD *d = &x->e_mbd.block[0];
607 int motion_error = INT_MAX;
609 // Simple 0,0 motion with no mv overhead
610 zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset );
611 d->bmi.mv.as_mv.row = 0;
612 d->bmi.mv.as_mv.col = 0;
614 // Test last reference frame using the previous best mv as the
615 // starting point (best reference) for the search
616 first_pass_motion_search(cpi, x, &best_ref_mv,
617 &d->bmi.mv.as_mv, lst_yv12,
618 &motion_error, recon_yoffset);
620 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
621 if (best_ref_mv.as_int)
624 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
625 lst_yv12, &tmp_err, recon_yoffset);
627 if ( tmp_err < motion_error )
629 motion_error = tmp_err;
630 d->bmi.mv.as_mv.row = tmp_mv.row;
631 d->bmi.mv.as_mv.col = tmp_mv.col;
635 // Experimental search in a second reference frame ((0,0) based only)
636 if (cm->current_video_frame > 1)
638 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
640 if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
643 //motion_error = gf_motion_error;
644 //d->bmi.mv.as_mv.row = tmp_mv.row;
645 //d->bmi.mv.as_mv.col = tmp_mv.col;
649 xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
650 xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
651 xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
655 // Reset to last frame as reference buffer
656 xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
657 xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
658 xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
661 /* Intra assumed best */
662 best_ref_mv.as_int = 0;
664 if (motion_error <= this_error)
666 // Keep a count of cases where the inter and intra were
667 // very close and very low. This helps with scene cut
668 // detection for example in cropped clips with black bars
669 // at the sides or top and bottom.
670 if( (((this_error-intrapenalty) * 9) <=
671 (motion_error*10)) &&
672 (this_error < (2*intrapenalty)) )
677 d->bmi.mv.as_mv.row <<= 3;
678 d->bmi.mv.as_mv.col <<= 3;
679 this_error = motion_error;
680 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
681 vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x);
682 sum_mvr += d->bmi.mv.as_mv.row;
683 sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
684 sum_mvc += d->bmi.mv.as_mv.col;
685 sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
686 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
687 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
690 best_ref_mv.as_int = d->bmi.mv.as_int;
692 // Was the vector non-zero
693 if (d->bmi.mv.as_int)
697 // Does the Row vector point inwards or outwards
698 if (mb_row < cm->mb_rows / 2)
700 if (d->bmi.mv.as_mv.row > 0)
702 else if (d->bmi.mv.as_mv.row < 0)
705 else if (mb_row > cm->mb_rows / 2)
707 if (d->bmi.mv.as_mv.row > 0)
709 else if (d->bmi.mv.as_mv.row < 0)
713 // Does the Row vector point inwards or outwards
714 if (mb_col < cm->mb_cols / 2)
716 if (d->bmi.mv.as_mv.col > 0)
718 else if (d->bmi.mv.as_mv.col < 0)
721 else if (mb_col > cm->mb_cols / 2)
723 if (d->bmi.mv.as_mv.col > 0)
725 else if (d->bmi.mv.as_mv.col < 0)
732 coded_error += (long long)this_error;
734 // adjust to the next column of macroblocks
735 x->src.y_buffer += 16;
736 x->src.u_buffer += 8;
737 x->src.v_buffer += 8;
743 // adjust to the next row of mbs
744 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
745 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
746 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
748 //extend the recon for intra prediction
749 vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
750 vp8_clear_system_state(); //__asm emms;
753 vp8_clear_system_state(); //__asm emms;
759 fps.frame = cm->current_video_frame ;
760 fps.intra_error = intra_error >> 8;
761 fps.coded_error = coded_error >> 8;
762 weight = simple_weight(cpi->Source);
768 fps.ssim_weighted_pred_err = fps.coded_error * weight;
770 fps.pcnt_inter = 0.0;
771 fps.pcnt_motion = 0.0;
778 fps.mv_in_out_count = 0.0;
781 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
782 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
783 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
787 fps.MVr = (double)sum_mvr / (double)mvcount;
788 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
789 fps.MVc = (double)sum_mvc / (double)mvcount;
790 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
791 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
792 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
793 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
795 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
798 // TODO: handle the case when duration is set to 0, or something less
799 // than the full time between subsequent cpi->source_time_stamp s .
800 fps.duration = cpi->source->ts_end
801 - cpi->source->ts_start;
803 // don't want to do output stats with a stack variable!
804 memcpy(cpi->twopass.this_frame_stats,
806 sizeof(FIRSTPASS_STATS));
807 output_stats(cpi, cpi->output_pkt_list, cpi->twopass.this_frame_stats);
808 accumulate_stats(cpi->twopass.total_stats, &fps);
811 // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
812 if ((cm->current_video_frame > 0) &&
813 (cpi->twopass.this_frame_stats->pcnt_inter > 0.20) &&
814 ((cpi->twopass.this_frame_stats->intra_error / cpi->twopass.this_frame_stats->coded_error) > 2.0))
816 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
819 // swap frame pointers so last frame refers to the frame we just compressed
820 vp8_swap_yv12_buffer(lst_yv12, new_yv12);
821 vp8_yv12_extend_frame_borders(lst_yv12);
823 // Special case for the first frame. Copy into the GF buffer as a second reference.
824 if (cm->current_video_frame == 0)
826 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
830 // use this to see what the first pass reconstruction looks like
835 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
837 if (cm->current_video_frame == 0)
838 recon_file = fopen(filename, "wb");
840 recon_file = fopen(filename, "ab");
842 if(fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file));
846 cm->current_video_frame++;
849 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
851 #define BASE_ERRPERMB 150
852 static int estimate_max_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
855 int num_mbs = cpi->common.MBs;
856 int target_norm_bits_per_mb;
858 double err_per_mb = section_err / num_mbs;
859 double correction_factor;
861 double speed_correction = 1.0;
862 double rolling_ratio;
864 double pow_highq = 0.90;
865 double pow_lowq = 0.40;
867 if (section_target_bandwitdh <= 0)
868 return cpi->twopass.maxq_max_limit; // Highest value allowed
870 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
872 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
873 if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->worst_quality))
875 rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
877 //if ( cpi->twopass.est_max_qcorrection_factor > rolling_ratio )
878 if (rolling_ratio < 0.95)
879 //cpi->twopass.est_max_qcorrection_factor *= adjustment_rate;
880 cpi->twopass.est_max_qcorrection_factor -= 0.005;
881 //else if ( cpi->twopass.est_max_qcorrection_factor < rolling_ratio )
882 else if (rolling_ratio > 1.05)
883 cpi->twopass.est_max_qcorrection_factor += 0.005;
885 //cpi->twopass.est_max_qcorrection_factor /= adjustment_rate;
887 cpi->twopass.est_max_qcorrection_factor =
888 (cpi->twopass.est_max_qcorrection_factor < 0.1)
890 : (cpi->twopass.est_max_qcorrection_factor > 10.0)
891 ? 10.0 : cpi->twopass.est_max_qcorrection_factor;
894 // Corrections for higher compression speed settings (reduced compression expected)
895 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
897 if (cpi->oxcf.cpu_used <= 5)
898 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
900 speed_correction = 1.25;
903 // Correction factor used for Q values >= 20
904 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
905 corr_high = (corr_high < 0.05)
906 ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
908 // Try and pick a max Q that will be high enough to encode the
909 // content at the given rate.
910 for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++)
912 int bits_per_mb_at_this_q;
916 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
917 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
920 correction_factor = corr_high;
922 bits_per_mb_at_this_q = (int)(.5 + correction_factor
923 * speed_correction * cpi->twopass.est_max_qcorrection_factor
924 * cpi->twopass.section_max_qfactor
925 * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
927 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
931 // Restriction on active max q for constrained quality mode.
932 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
933 (Q < cpi->cq_target_quality) )
934 //(Q < cpi->oxcf.cq_level;) )
936 Q = cpi->cq_target_quality;
937 //Q = cpi->oxcf.cq_level;
940 // Adjust maxq_min_limit and maxq_max_limit limits based on
941 // averaga q observed in clip for non kf/gf.arf frames
942 // Give average a chance to settle though.
943 if ( (cpi->ni_frames >
944 ((unsigned int)cpi->twopass.total_stats->count >> 8)) &&
945 (cpi->ni_frames > 150) )
947 cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
948 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
949 cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
950 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
955 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
958 int num_mbs = cpi->common.MBs;
959 int target_norm_bits_per_mb;
961 double err_per_mb = section_err / num_mbs;
962 double correction_factor;
964 double speed_correction = 1.0;
965 double pow_highq = 0.90;
966 double pow_lowq = 0.40;
968 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
970 // Corrections for higher compression speed settings (reduced compression expected)
971 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
973 if (cpi->oxcf.cpu_used <= 5)
974 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
976 speed_correction = 1.25;
979 // Correction factor used for Q values >= 20
980 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
981 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
983 // Try and pick a Q that can encode the content at the given rate.
984 for (Q = 0; Q < MAXQ; Q++)
986 int bits_per_mb_at_this_q;
990 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
991 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
994 correction_factor = corr_high;
996 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->twopass.est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
998 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1005 // Estimate a worst case Q for a KF group
1006 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio)
1009 int num_mbs = cpi->common.MBs;
1010 int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1011 int bits_per_mb_at_this_q;
1013 double err_per_mb = section_err / num_mbs;
1014 double err_correction_factor;
1016 double speed_correction = 1.0;
1017 double current_spend_ratio = 1.0;
1019 double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1020 double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1022 double iiratio_correction_factor = 1.0;
1024 double combined_correction_factor;
1026 // Trap special case where the target is <= 0
1027 if (target_norm_bits_per_mb <= 0)
1030 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
1031 // This is clamped to the range 0.1 to 10.0
1032 if (cpi->long_rolling_target_bits <= 0)
1033 current_spend_ratio = 10.0;
1036 current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
1037 current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
1040 // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
1041 // The idea here is to favour subsampling in the hardest sections vs the easyest.
1042 iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1044 if (iiratio_correction_factor < 0.5)
1045 iiratio_correction_factor = 0.5;
1047 // Corrections for higher compression speed settings (reduced compression expected)
1048 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1050 if (cpi->oxcf.cpu_used <= 5)
1051 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1053 speed_correction = 1.25;
1056 // Combine the various factors calculated above
1057 combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
1059 // Correction factor used for Q values >= 20
1060 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1061 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1063 // Try and pick a Q that should be high enough to encode the content at the given rate.
1064 for (Q = 0; Q < MAXQ; Q++)
1066 // Q values < 20 treated as a special case
1069 err_correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1070 err_correction_factor = (err_correction_factor < 0.05) ? 0.05 : (err_correction_factor > 5.0) ? 5.0 : err_correction_factor;
1073 err_correction_factor = corr_high;
1075 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * combined_correction_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q]);
1077 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1081 // If we could not hit the target even at Max Q then estimate what Q would have bee required
1082 while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2)))
1085 bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1091 FILE *f = fopen("estkf_q.stt", "a");
1092 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,
1093 target_norm_bits_per_mb, err_per_mb, err_correction_factor,
1094 current_spend_ratio, group_iiratio, iiratio_correction_factor,
1095 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
1102 // For cq mode estimate a cq level that matches the observed
1103 // complexity and data rate.
1104 static int estimate_cq(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
1107 int num_mbs = cpi->common.MBs;
1108 int target_norm_bits_per_mb;
1110 double err_per_mb = section_err / num_mbs;
1111 double correction_factor;
1113 double speed_correction = 1.0;
1114 double pow_highq = 0.90;
1115 double pow_lowq = 0.40;
1116 double clip_iiratio;
1117 double clip_iifactor;
1119 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1120 ? (512 * section_target_bandwitdh) / num_mbs
1121 : 512 * (section_target_bandwitdh / num_mbs);
1123 // Corrections for higher compression speed settings
1124 // (reduced compression expected)
1125 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1127 if (cpi->oxcf.cpu_used <= 5)
1128 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1130 speed_correction = 1.25;
1132 // II ratio correction factor for clip as a whole
1133 clip_iiratio = cpi->twopass.total_stats->intra_error /
1134 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats->coded_error);
1135 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1136 if (clip_iifactor < 0.80)
1137 clip_iifactor = 0.80;
1139 // Correction factor used for Q values >= 20
1140 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1141 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
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;
1151 pow( err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1153 correction_factor = (correction_factor < 0.05) ? 0.05
1154 : (correction_factor > 5.0) ? 5.0
1155 : correction_factor;
1158 correction_factor = corr_high;
1160 bits_per_mb_at_this_q =
1161 (int)( .5 + correction_factor *
1164 (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1166 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1173 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
1175 void vp8_init_second_pass(VP8_COMP *cpi)
1177 FIRSTPASS_STATS this_frame;
1178 FIRSTPASS_STATS *start_pos;
1180 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1182 zero_stats(cpi->twopass.total_stats);
1184 if (!cpi->twopass.stats_in_end)
1187 *cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1189 cpi->twopass.total_error_left = cpi->twopass.total_stats->ssim_weighted_pred_err;
1190 cpi->twopass.total_intra_error_left = cpi->twopass.total_stats->intra_error;
1191 cpi->twopass.total_coded_error_left = cpi->twopass.total_stats->coded_error;
1192 cpi->twopass.start_tot_err_left = cpi->twopass.total_error_left;
1194 //cpi->twopass.bits_left = (long long)(cpi->twopass.total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1195 //cpi->twopass.bits_left -= (long long)(cpi->twopass.total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1197 // each frame can have a different duration, as the frame rate in the source
1198 // isn't guaranteed to be constant. The frame rate prior to the first frame
1199 // encoded in the second pass is a guess. However the sum duration is not.
1200 // Its calculated based on the actual durations of all frames from the first
1202 vp8_new_frame_rate(cpi, 10000000.0 * cpi->twopass.total_stats->count / cpi->twopass.total_stats->duration);
1204 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1205 cpi->twopass.bits_left = (long long)(cpi->twopass.total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1206 cpi->twopass.bits_left -= (long long)(cpi->twopass.total_stats->duration * two_pass_min_rate / 10000000.0);
1207 cpi->twopass.clip_bits_total = cpi->twopass.bits_left;
1209 // Calculate a minimum intra value to be used in determining the IIratio
1210 // scores used in the second pass. We have this minimum to make sure
1211 // that clips that are static but "low complexity" in the intra domain
1212 // are still boosted appropriately for KF/GF/ARF
1213 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1214 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1216 avg_stats(cpi->twopass.total_stats);
1218 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1220 double sum_iiratio = 0.0;
1223 start_pos = cpi->twopass.stats_in; // Note starting "file" position
1225 while (input_stats(cpi, &this_frame) != EOF)
1227 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1228 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1229 sum_iiratio += IIRatio;
1232 cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats->count);
1234 // Reset file position
1235 reset_fpf_position(cpi, start_pos);
1238 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1239 // used to allocate bits
1241 start_pos = cpi->twopass.stats_in; // Note starting "file" position
1243 cpi->twopass.modified_error_total = 0.0;
1244 cpi->twopass.modified_error_used = 0.0;
1246 while (input_stats(cpi, &this_frame) != EOF)
1248 cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame);
1250 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1252 reset_fpf_position(cpi, start_pos); // Reset file position
1257 void vp8_end_second_pass(VP8_COMP *cpi)
1261 // This function gives and estimate of how badly we believe
1262 // the prediction quality is decaying from frame to frame.
1263 static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1265 double prediction_decay_rate;
1266 double motion_decay;
1267 double motion_pct = next_frame->pcnt_motion;
1269 // Initial basis is the % mbs inter coded
1270 prediction_decay_rate = next_frame->pcnt_inter;
1272 // High % motion -> somewhat higher decay rate
1273 motion_decay = (1.0 - (motion_pct / 20.0));
1274 if (motion_decay < prediction_decay_rate)
1275 prediction_decay_rate = motion_decay;
1277 // Adjustment to decay rate based on speed of motion
1279 double this_mv_rabs;
1280 double this_mv_cabs;
1281 double distance_factor;
1283 this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1284 this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1286 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
1287 (this_mv_cabs * this_mv_cabs)) / 250.0;
1288 distance_factor = ((distance_factor > 1.0)
1289 ? 0.0 : (1.0 - distance_factor));
1290 if (distance_factor < prediction_decay_rate)
1291 prediction_decay_rate = distance_factor;
1294 return prediction_decay_rate;
1297 // Function to test for a condition where a complex transition is followed
1298 // by a static section. For example in slide shows where there is a fade
1299 // between slides. This is to help with more optimal kf and gf positioning.
1300 static int detect_transition_to_still(
1304 double loop_decay_rate,
1305 double decay_accumulator )
1307 BOOL trans_to_still = FALSE;
1309 // Break clause to detect very still sections after motion
1310 // For example a static image after a fade or other transition
1311 // instead of a clean scene cut.
1312 if ( (frame_interval > MIN_GF_INTERVAL) &&
1313 (loop_decay_rate >= 0.999) &&
1314 (decay_accumulator < 0.9) )
1317 FIRSTPASS_STATS * position = cpi->twopass.stats_in;
1318 FIRSTPASS_STATS tmp_next_frame;
1321 // Look ahead a few frames to see if static condition
1323 for ( j = 0; j < still_interval; j++ )
1325 if (EOF == input_stats(cpi, &tmp_next_frame))
1328 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1329 if ( decay_rate < 0.999 )
1332 // Reset file position
1333 reset_fpf_position(cpi, position);
1335 // Only if it does do we signal a transition to still
1336 if ( j == still_interval )
1337 trans_to_still = TRUE;
1340 return trans_to_still;
1343 // This function detects a flash through the high relative pcnt_second_ref
1344 // score in the frame following a flash frame. The offset passed in should
1346 static BOOL detect_flash( VP8_COMP *cpi, int offset )
1348 FIRSTPASS_STATS next_frame;
1350 BOOL flash_detected = FALSE;
1352 // Read the frame data.
1353 // The return is FALSE (no flash detected) if not a valid frame
1354 if ( read_frame_stats(cpi, &next_frame, offset) != EOF )
1356 // What we are looking for here is a situation where there is a
1357 // brief break in prediction (such as a flash) but subsequent frames
1358 // are reasonably well predicted by an earlier (pre flash) frame.
1359 // The recovery after a flash is indicated by a high pcnt_second_ref
1360 // comapred to pcnt_inter.
1361 if ( (next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
1362 (next_frame.pcnt_second_ref >= 0.5 ) )
1364 flash_detected = TRUE;
1368 FILE *f = fopen("flash.stt", "a");
1369 fprintf(f, "%8.0f %6.2f %6.2f\n",
1371 next_frame.pcnt_inter,
1372 next_frame.pcnt_second_ref);
1378 return flash_detected;
1381 // Update the motion related elements to the GF arf boost calculation
1382 static void accumulate_frame_motion_stats(
1384 FIRSTPASS_STATS * this_frame,
1385 double * this_frame_mv_in_out,
1386 double * mv_in_out_accumulator,
1387 double * abs_mv_in_out_accumulator,
1388 double * mv_ratio_accumulator )
1390 //double this_frame_mv_in_out;
1391 double this_frame_mvr_ratio;
1392 double this_frame_mvc_ratio;
1395 // Accumulate motion stats.
1396 motion_pct = this_frame->pcnt_motion;
1398 // Accumulate Motion In/Out of frame stats
1399 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1400 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1401 *abs_mv_in_out_accumulator +=
1402 fabs(this_frame->mv_in_out_count * motion_pct);
1404 // Accumulate a measure of how uniform (or conversely how random)
1405 // the motion field is. (A ratio of absmv / mv)
1406 if (motion_pct > 0.05)
1408 this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1409 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1411 this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1412 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1414 *mv_ratio_accumulator +=
1415 (this_frame_mvr_ratio < this_frame->mvr_abs)
1416 ? (this_frame_mvr_ratio * motion_pct)
1417 : this_frame->mvr_abs * motion_pct;
1419 *mv_ratio_accumulator +=
1420 (this_frame_mvc_ratio < this_frame->mvc_abs)
1421 ? (this_frame_mvc_ratio * motion_pct)
1422 : this_frame->mvc_abs * motion_pct;
1427 // Calculate a baseline boost number for the current frame.
1428 static double calc_frame_boost(
1430 FIRSTPASS_STATS * this_frame,
1431 double this_frame_mv_in_out )
1435 // Underlying boost factor is based on inter intra error ratio
1436 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1437 frame_boost = (IIFACTOR * this_frame->intra_error /
1438 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1440 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1441 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1443 // Increase boost for frames where new data coming into frame
1444 // (eg zoom out). Slightly reduce boost if there is a net balance
1445 // of motion out of the frame (zoom in).
1446 // The range for this_frame_mv_in_out is -1.0 to +1.0
1447 if (this_frame_mv_in_out > 0.0)
1448 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1449 // In extreme case boost is halved
1451 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1454 if (frame_boost > GF_RMAX)
1455 frame_boost = GF_RMAX;
1461 static int calc_arf_boost(
1469 FIRSTPASS_STATS this_frame;
1472 double boost_score = 0.0;
1473 double fwd_boost_score = 0.0;
1474 double mv_ratio_accumulator = 0.0;
1475 double decay_accumulator = 1.0;
1476 double this_frame_mv_in_out = 0.0;
1477 double mv_in_out_accumulator = 0.0;
1478 double abs_mv_in_out_accumulator = 0.0;
1480 BOOL flash_detected = FALSE;
1482 // Search forward from the proposed arf/next gf position
1483 for ( i = 0; i < f_frames; i++ )
1485 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1488 // Update the motion related elements to the boost calculation
1489 accumulate_frame_motion_stats( cpi, &this_frame,
1490 &this_frame_mv_in_out, &mv_in_out_accumulator,
1491 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1493 // Calculate the baseline boost number for this frame
1494 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1496 // We want to discount the the flash frame itself and the recovery
1497 // frame that follows as both will have poor scores.
1498 flash_detected = detect_flash(cpi, (i+offset)) ||
1499 detect_flash(cpi, (i+offset+1));
1501 // Cumulative effect of prediction quality decay
1502 if ( !flash_detected )
1506 get_prediction_decay_rate(cpi, &this_frame);
1508 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1510 boost_score += (decay_accumulator * r);
1512 // Break out conditions.
1513 if ( (!flash_detected) &&
1514 ((mv_ratio_accumulator > 100.0) ||
1515 (abs_mv_in_out_accumulator > 3.0) ||
1516 (mv_in_out_accumulator < -2.0) ) )
1522 *f_boost = (int)(boost_score * 100.0) >> 4;
1524 // Reset for backward looking loop
1526 mv_ratio_accumulator = 0.0;
1527 decay_accumulator = 1.0;
1528 this_frame_mv_in_out = 0.0;
1529 mv_in_out_accumulator = 0.0;
1530 abs_mv_in_out_accumulator = 0.0;
1532 // Search forward from the proposed arf/next gf position
1533 for ( i = -1; i >= -b_frames; i-- )
1535 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1538 // Update the motion related elements to the boost calculation
1539 accumulate_frame_motion_stats( cpi, &this_frame,
1540 &this_frame_mv_in_out, &mv_in_out_accumulator,
1541 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1543 // Calculate the baseline boost number for this frame
1544 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1546 // We want to discount the the flash frame itself and the recovery
1547 // frame that follows as both will have poor scores.
1548 flash_detected = detect_flash(cpi, (i+offset)) ||
1549 detect_flash(cpi, (i+offset+1));
1551 // Cumulative effect of prediction quality decay
1552 if ( !flash_detected )
1556 get_prediction_decay_rate(cpi, &this_frame);
1558 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1561 boost_score += (decay_accumulator * r);
1563 // Break out conditions.
1564 if ( (!flash_detected) &&
1565 ((mv_ratio_accumulator > 100.0) ||
1566 (abs_mv_in_out_accumulator > 3.0) ||
1567 (mv_in_out_accumulator < -2.0) ) )
1572 *b_boost = (int)(boost_score * 100.0) >> 4;
1574 return (*f_boost + *b_boost);
1578 // Analyse and define a gf/arf group .
1579 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1581 FIRSTPASS_STATS next_frame;
1582 FIRSTPASS_STATS *start_pos;
1585 double boost_score = 0.0;
1586 double old_boost_score = 0.0;
1587 double gf_group_err = 0.0;
1588 double gf_first_frame_err = 0.0;
1589 double mod_frame_err = 0.0;
1591 double mv_ratio_accumulator = 0.0;
1592 double decay_accumulator = 1.0;
1594 double loop_decay_rate = 1.00; // Starting decay rate
1596 double this_frame_mv_in_out = 0.0;
1597 double mv_in_out_accumulator = 0.0;
1598 double abs_mv_in_out_accumulator = 0.0;
1599 double mod_err_per_mb_accumulator = 0.0;
1601 int max_bits = frame_max_bits(cpi); // Max for a single frame
1603 unsigned int allow_alt_ref =
1604 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1609 BOOL flash_detected;
1611 cpi->twopass.gf_group_bits = 0;
1612 cpi->twopass.gf_decay_rate = 0;
1614 vp8_clear_system_state(); //__asm emms;
1616 start_pos = cpi->twopass.stats_in;
1618 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1620 // Load stats for the current frame.
1621 mod_frame_err = calculate_modified_err(cpi, this_frame);
1623 // Note the error of the frame at the start of the group (this will be
1624 // the GF frame error if we code a normal gf
1625 gf_first_frame_err = mod_frame_err;
1627 // Special treatment if the current frame is a key frame (which is also
1628 // a gf). If it is then its error score (and hence bit allocation) need
1629 // to be subtracted out from the calculation for the GF group
1630 if (cpi->common.frame_type == KEY_FRAME)
1631 gf_group_err -= gf_first_frame_err;
1633 // Scan forward to try and work out how many frames the next gf group
1634 // should contain and what level of boost is appropriate for the GF
1635 // or ARF that will be coded with the group
1638 while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1639 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1640 (i < cpi->twopass.frames_to_key))
1642 i++; // Increment the loop counter
1644 // Accumulate error score of frames in this gf group
1645 mod_frame_err = calculate_modified_err(cpi, this_frame);
1647 gf_group_err += mod_frame_err;
1649 mod_err_per_mb_accumulator +=
1650 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1652 if (EOF == input_stats(cpi, &next_frame))
1655 // Test for the case where there is a brief flash but the prediction
1656 // quality back to an earlier frame is then restored.
1657 flash_detected = detect_flash(cpi, 0);
1659 // Update the motion related elements to the boost calculation
1660 accumulate_frame_motion_stats( cpi, &next_frame,
1661 &this_frame_mv_in_out, &mv_in_out_accumulator,
1662 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1664 // Calculate a baseline boost number for this frame
1665 r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out );
1667 // Cumulative effect of prediction quality decay
1668 if ( !flash_detected )
1670 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1671 decay_accumulator = decay_accumulator * loop_decay_rate;
1673 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1675 boost_score += (decay_accumulator * r);
1677 // Break clause to detect very still sections after motion
1678 // For example a staic image after a fade or other transition.
1679 if ( detect_transition_to_still( cpi, i, 5,
1681 decay_accumulator ) )
1683 allow_alt_ref = FALSE;
1684 boost_score = old_boost_score;
1688 // Break out conditions.
1690 // Break at cpi->max_gf_interval unless almost totally static
1691 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1693 // Dont break out with a very short interval
1694 (i > MIN_GF_INTERVAL) &&
1695 // Dont break out very close to a key frame
1696 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1697 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1698 (!flash_detected) &&
1699 ((mv_ratio_accumulator > 100.0) ||
1700 (abs_mv_in_out_accumulator > 3.0) ||
1701 (mv_in_out_accumulator < -2.0) ||
1702 ((boost_score - old_boost_score) < 2.0))
1705 boost_score = old_boost_score;
1709 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1711 old_boost_score = boost_score;
1714 cpi->twopass.gf_decay_rate =
1715 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1717 // When using CBR apply additional buffer related upper limits
1718 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1722 // For cbr apply buffer related limits
1723 if (cpi->drop_frames_allowed)
1725 int df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1726 (cpi->oxcf.optimal_buffer_level / 100);
1728 if (cpi->buffer_level > df_buffer_level)
1729 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1733 else if (cpi->buffer_level > 0)
1735 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1742 if (boost_score > max_boost)
1743 boost_score = max_boost;
1746 // Dont allow conventional gf too near the next kf
1747 if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)
1749 while (i < cpi->twopass.frames_to_key)
1753 if (EOF == input_stats(cpi, this_frame))
1756 if (i < cpi->twopass.frames_to_key)
1758 mod_frame_err = calculate_modified_err(cpi, this_frame);
1759 gf_group_err += mod_frame_err;
1764 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1767 // Alterrnative boost calculation for alt ref
1768 alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost );
1771 // Should we use the alternate refernce frame
1772 if (allow_alt_ref &&
1773 (i >= MIN_GF_INTERVAL) &&
1774 // dont use ARF very near next kf
1775 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1777 ((next_frame.pcnt_inter > 0.75) ||
1778 (next_frame.pcnt_second_ref > 0.5)) &&
1779 ((mv_in_out_accumulator / (double)i > -0.2) ||
1780 (mv_in_out_accumulator > -2.0)) &&
1784 (next_frame.pcnt_inter > 0.75) &&
1785 ((mv_in_out_accumulator / (double)i > -0.2) ||
1786 (mv_in_out_accumulator > -2.0)) &&
1787 (cpi->gfu_boost > 100) &&
1788 (cpi->twopass.gf_decay_rate <=
1789 (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) )
1793 int allocation_chunks;
1794 int Q = (cpi->oxcf.fixed_q < 0)
1795 ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1797 int arf_frame_bits = 0;
1801 cpi->gfu_boost = alt_boost;
1804 // Estimate the bits to be allocated to the group as a whole
1805 if ((cpi->twopass.kf_group_bits > 0) &&
1806 (cpi->twopass.kf_group_error_left > 0))
1808 group_bits = (int)((double)cpi->twopass.kf_group_bits *
1809 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1814 // Boost for arf frame
1816 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1818 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1822 // Set max and minimum boost and hence minimum allocation
1823 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1824 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1825 else if (Boost < 125)
1828 allocation_chunks = (i * 100) + Boost;
1830 // Normalize Altboost and allocations chunck down to prevent overflow
1831 while (Boost > 1000)
1834 allocation_chunks /= 2;
1837 // Calculate the number of bits to be spent on the arf based on the
1839 arf_frame_bits = (int)((double)Boost * (group_bits /
1840 (double)allocation_chunks));
1842 // Estimate if there are enough bits available to make worthwhile use
1844 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
1846 // Only use an arf if it is likely we will be able to code
1847 // it at a lower Q than the surrounding frames.
1848 if (tmp_q < cpi->worst_quality)
1851 int frames_after_arf;
1852 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1853 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1855 cpi->source_alt_ref_pending = TRUE;
1857 // For alt ref frames the error score for the end frame of the
1858 // group (the alt ref frame) should not contribute to the group
1859 // total and hence the number of bit allocated to the group.
1860 // Rather it forms part of the next group (it is the GF at the
1861 // start of the next group)
1862 // gf_group_err -= mod_frame_err;
1864 // For alt ref frames alt ref frame is technically part of the
1865 // GF frame for the next group but we always base the error
1866 // calculation and bit allocation on the current group of frames.
1868 // Set the interval till the next gf or arf.
1869 // For ARFs this is the number of frames to be coded before the
1870 // future frame that is coded as an ARF.
1871 // The future frame itself is part of the next group
1872 cpi->baseline_gf_interval = i;
1874 // Define the arnr filter width for this group of frames:
1875 // We only filter frames that lie within a distance of half
1876 // the GF interval from the ARF frame. We also have to trap
1877 // cases where the filter extends beyond the end of clip.
1878 // Note: this_frame->frame has been updated in the loop
1879 // so it now points at the ARF frame.
1880 half_gf_int = cpi->baseline_gf_interval >> 1;
1881 frames_after_arf = cpi->twopass.total_stats->count -
1882 this_frame->frame - 1;
1884 switch (cpi->oxcf.arnr_type)
1886 case 1: // Backward filter
1888 if (frames_bwd > half_gf_int)
1889 frames_bwd = half_gf_int;
1892 case 2: // Forward filter
1893 if (frames_fwd > half_gf_int)
1894 frames_fwd = half_gf_int;
1895 if (frames_fwd > frames_after_arf)
1896 frames_fwd = frames_after_arf;
1900 case 3: // Centered filter
1903 if (frames_fwd > frames_after_arf)
1904 frames_fwd = frames_after_arf;
1905 if (frames_fwd > half_gf_int)
1906 frames_fwd = half_gf_int;
1908 frames_bwd = frames_fwd;
1910 // For even length filter there is one more frame backward
1911 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1912 if (frames_bwd < half_gf_int)
1913 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
1917 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1921 cpi->source_alt_ref_pending = FALSE;
1922 cpi->baseline_gf_interval = i;
1927 cpi->source_alt_ref_pending = FALSE;
1928 cpi->baseline_gf_interval = i;
1931 // Now decide how many bits should be allocated to the GF group as a
1932 // proportion of those remaining in the kf group.
1933 // The final key frame group in the clip is treated as a special case
1934 // where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
1935 // This is also important for short clips where there may only be one
1937 if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats->count -
1938 cpi->common.current_video_frame))
1940 cpi->twopass.kf_group_bits =
1941 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
1944 // Calculate the bits to be allocated to the group as a whole
1945 if ((cpi->twopass.kf_group_bits > 0) &&
1946 (cpi->twopass.kf_group_error_left > 0))
1948 cpi->twopass.gf_group_bits =
1949 (int)((double)cpi->twopass.kf_group_bits *
1950 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1953 cpi->twopass.gf_group_bits = 0;
1955 cpi->twopass.gf_group_bits =
1956 (cpi->twopass.gf_group_bits < 0)
1958 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
1959 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
1961 // Clip cpi->twopass.gf_group_bits based on user supplied data rate
1962 // variability limit (cpi->oxcf.two_pass_vbrmax_section)
1963 if (cpi->twopass.gf_group_bits > max_bits * cpi->baseline_gf_interval)
1964 cpi->twopass.gf_group_bits = max_bits * cpi->baseline_gf_interval;
1966 // Reset the file position
1967 reset_fpf_position(cpi, start_pos);
1969 // Update the record of error used so far (only done once per gf group)
1970 cpi->twopass.modified_error_used += gf_group_err;
1972 // Assign bits to the arf or gf.
1973 for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) {
1975 int allocation_chunks;
1976 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1980 if (cpi->source_alt_ref_pending && i == 0)
1983 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1985 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1987 Boost += (cpi->baseline_gf_interval * 50);
1989 // Set max and minimum boost and hence minimum allocation
1990 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1991 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1992 else if (Boost < 125)
1996 ((cpi->baseline_gf_interval + 1) * 100) + Boost;
1998 // Else for standard golden frames
2001 // boost based on inter / intra ratio of subsequent frames
2002 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
2004 // Set max and minimum boost and hence minimum allocation
2005 if (Boost > (cpi->baseline_gf_interval * 150))
2006 Boost = (cpi->baseline_gf_interval * 150);
2007 else if (Boost < 125)
2011 (cpi->baseline_gf_interval * 100) + (Boost - 100);
2014 // Normalize Altboost and allocations chunck down to prevent overflow
2015 while (Boost > 1000)
2018 allocation_chunks /= 2;
2021 // Calculate the number of bits to be spent on the gf or arf based on
2023 gf_bits = (int)((double)Boost *
2024 (cpi->twopass.gf_group_bits /
2025 (double)allocation_chunks));
2027 // If the frame that is to be boosted is simpler than the average for
2028 // the gf/arf group then use an alternative calculation
2029 // based on the error score of the frame itself
2030 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
2032 double alt_gf_grp_bits;
2036 (double)cpi->twopass.kf_group_bits *
2037 (mod_frame_err * (double)cpi->baseline_gf_interval) /
2038 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
2040 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
2041 (double)allocation_chunks));
2043 if (gf_bits > alt_gf_bits)
2045 gf_bits = alt_gf_bits;
2048 // Else if it is harder than other frames in the group make sure it at
2049 // least receives an allocation in keeping with its relative error
2050 // score, otherwise it may be worse off than an "un-boosted" frame
2054 (int)((double)cpi->twopass.kf_group_bits *
2056 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
2058 if (alt_gf_bits > gf_bits)
2060 gf_bits = alt_gf_bits;
2064 // Apply an additional limit for CBR
2065 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2067 if (cpi->twopass.gf_bits > (cpi->buffer_level >> 1))
2068 cpi->twopass.gf_bits = cpi->buffer_level >> 1;
2071 // Dont allow a negative value for gf_bits
2075 gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame
2079 cpi->twopass.gf_bits = gf_bits;
2081 if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)))
2083 cpi->per_frame_bandwidth = gf_bits; // Per frame bit target for this frame
2088 // Adjust KF group bits and error remainin
2089 cpi->twopass.kf_group_error_left -= gf_group_err;
2090 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
2092 if (cpi->twopass.kf_group_bits < 0)
2093 cpi->twopass.kf_group_bits = 0;
2095 // Note the error score left in the remaining frames of the group.
2096 // For normal GFs we want to remove the error score for the first frame of the group (except in Key frame case where this has already happened)
2097 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
2098 cpi->twopass.gf_group_error_left = gf_group_err - gf_first_frame_err;
2100 cpi->twopass.gf_group_error_left = gf_group_err;
2102 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
2104 if (cpi->twopass.gf_group_bits < 0)
2105 cpi->twopass.gf_group_bits = 0;
2109 int boost = (cpi->source_alt_ref_pending)
2110 ? b_boost : cpi->gfu_boost;
2112 int boost = cpi->gfu_boost;
2114 // Set aside some bits for a mid gf sequence boost
2115 if ((boost > 150) && (cpi->baseline_gf_interval > 5))
2117 int pct_extra = (boost - 100) / 50;
2118 pct_extra = (pct_extra > 10) ? 10 : pct_extra;
2120 cpi->twopass.mid_gf_extra_bits =
2121 (cpi->twopass.gf_group_bits * pct_extra) / 100;
2122 cpi->twopass.gf_group_bits -= cpi->twopass.mid_gf_extra_bits;
2125 cpi->twopass.mid_gf_extra_bits = 0;
2129 // Adjustment to estimate_max_q based on a measure of complexity of the section
2130 if (cpi->common.frame_type != KEY_FRAME)
2132 FIRSTPASS_STATS sectionstats;
2135 zero_stats(§ionstats);
2136 reset_fpf_position(cpi, start_pos);
2138 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
2140 input_stats(cpi, &next_frame);
2141 accumulate_stats(§ionstats, &next_frame);
2144 avg_stats(§ionstats);
2146 cpi->twopass.section_intra_rating =
2147 sectionstats.intra_error /
2148 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2150 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2151 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2153 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2155 if (cpi->twopass.section_max_qfactor < 0.80)
2156 cpi->twopass.section_max_qfactor = 0.80;
2160 // cpi->twopass.section_max_qfactor = 1.0;
2162 reset_fpf_position(cpi, start_pos);
2166 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2167 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2169 int target_frame_size; // gf_group_error_left
2171 double modified_err;
2172 double err_fraction; // What portion of the remaining GF group error is used by this frame
2174 int max_bits = frame_max_bits(cpi); // Max for a single frame
2176 // Calculate modified prediction error used in bit allocation
2177 modified_err = calculate_modified_err(cpi, this_frame);
2179 if (cpi->twopass.gf_group_error_left > 0)
2180 err_fraction = modified_err / cpi->twopass.gf_group_error_left; // What portion of the remaining GF group error is used by this frame
2184 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it?
2186 // Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at the top end.
2187 if (target_frame_size < 0)
2188 target_frame_size = 0;
2191 if (target_frame_size > max_bits)
2192 target_frame_size = max_bits;
2194 if (target_frame_size > cpi->twopass.gf_group_bits)
2195 target_frame_size = cpi->twopass.gf_group_bits;
2198 cpi->twopass.gf_group_error_left -= modified_err; // Adjust error remaining
2199 cpi->twopass.gf_group_bits -= target_frame_size; // Adjust bits remaining
2201 if (cpi->twopass.gf_group_bits < 0)
2202 cpi->twopass.gf_group_bits = 0;
2204 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame.
2206 // Special case for the frame that lies half way between two gfs
2207 if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2)
2208 target_frame_size += cpi->twopass.mid_gf_extra_bits;
2210 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
2213 void vp8_second_pass(VP8_COMP *cpi)
2216 int frames_left = (int)(cpi->twopass.total_stats->count - cpi->common.current_video_frame);
2218 FIRSTPASS_STATS this_frame;
2219 FIRSTPASS_STATS this_frame_copy;
2221 double this_frame_error;
2222 double this_frame_intra_error;
2223 double this_frame_coded_error;
2225 FIRSTPASS_STATS *start_pos;
2227 if (!cpi->twopass.stats_in)
2232 vp8_clear_system_state();
2234 if (EOF == input_stats(cpi, &this_frame))
2237 this_frame_error = this_frame.ssim_weighted_pred_err;
2238 this_frame_intra_error = this_frame.intra_error;
2239 this_frame_coded_error = this_frame.coded_error;
2241 start_pos = cpi->twopass.stats_in;
2243 // keyframe and section processing !
2244 if (cpi->twopass.frames_to_key == 0)
2246 // Define next KF group and assign bits to it
2247 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2248 find_next_key_frame(cpi, &this_frame_copy);
2250 // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop
2251 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2252 // This is temporary code till we decide what should really happen in this case.
2253 if (cpi->oxcf.error_resilient_mode)
2255 cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
2256 cpi->twopass.gf_group_error_left = cpi->twopass.kf_group_error_left;
2257 cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
2258 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2259 cpi->source_alt_ref_pending = FALSE;
2264 // Is this a GF / ARF (Note that a KF is always also a GF)
2265 if (cpi->frames_till_gf_update_due == 0)
2267 // Define next gf group and assign bits to it
2268 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2269 define_gf_group(cpi, &this_frame_copy);
2271 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
2272 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
2273 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
2274 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2276 // Assign a standard frames worth of bits from those allocated to the GF group
2277 int bak = cpi->per_frame_bandwidth;
2278 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2279 assign_std_frame_bits(cpi, &this_frame_copy);
2280 cpi->per_frame_bandwidth = bak;
2284 // Otherwise this is an ordinary frame
2287 // Special case: Error error_resilient_mode mode does not make much sense for two pass but with its current meaning but this code is designed to stop
2288 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2289 // This is temporary code till we decide what should really happen in this case.
2290 if (cpi->oxcf.error_resilient_mode)
2292 cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
2294 if (cpi->common.frame_type != KEY_FRAME)
2296 // Assign bits from those allocated to the GF group
2297 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2298 assign_std_frame_bits(cpi, &this_frame_copy);
2303 // Assign bits from those allocated to the GF group
2304 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2305 assign_std_frame_bits(cpi, &this_frame_copy);
2309 // Keep a globally available copy of this and the next frame's iiratio.
2310 cpi->twopass.this_iiratio = this_frame_intra_error /
2311 DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
2313 FIRSTPASS_STATS next_frame;
2314 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2316 cpi->twopass.next_iiratio = next_frame.intra_error /
2317 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
2321 // Set nominal per second bandwidth for this frame
2322 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
2323 if (cpi->target_bandwidth < 0)
2324 cpi->target_bandwidth = 0;
2326 if (cpi->common.current_video_frame == 0)
2328 cpi->twopass.est_max_qcorrection_factor = 1.0;
2330 // Experimental code to try and set a cq_level in constrained
2332 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2338 (cpi->twopass.total_coded_error_left / frames_left),
2339 (int)(cpi->twopass.bits_left / frames_left));
2341 cpi->cq_target_quality = cpi->oxcf.cq_level;
2342 if ( est_cq > cpi->cq_target_quality )
2343 cpi->cq_target_quality = est_cq;
2346 // guess at maxq needed in 2nd pass
2347 cpi->twopass.maxq_max_limit = cpi->worst_quality;
2348 cpi->twopass.maxq_min_limit = cpi->best_quality;
2349 tmp_q = estimate_max_q( cpi,
2350 (cpi->twopass.total_coded_error_left / frames_left),
2351 (int)(cpi->twopass.bits_left / frames_left));
2353 // Limit the maxq value returned subsequently.
2354 // This increases the risk of overspend or underspend if the initial
2355 // estimate for the clip is bad, but helps prevent excessive
2356 // variation in Q, especially near the end of a clip
2357 // where for example a small overspend may cause Q to crash
2358 cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2359 ? (tmp_q + 32) : cpi->worst_quality;
2360 cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2361 ? (tmp_q - 32) : cpi->best_quality;
2363 cpi->active_worst_quality = tmp_q;
2364 cpi->ni_av_qi = tmp_q;
2367 // The last few frames of a clip almost always have to few or too many
2368 // bits and for the sake of over exact rate control we dont want to make
2369 // radical adjustments to the allowed quantizer range just to use up a
2370 // few surplus bits or get beneath the target rate.
2371 else if ( (cpi->common.current_video_frame <
2372 (((unsigned int)cpi->twopass.total_stats->count * 255)>>8)) &&
2373 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2374 (unsigned int)cpi->twopass.total_stats->count) )
2376 if (frames_left < 1)
2379 tmp_q = estimate_max_q(cpi, (cpi->twopass.total_coded_error_left / frames_left), (int)(cpi->twopass.bits_left / frames_left));
2381 // Move active_worst_quality but in a damped way
2382 if (tmp_q > cpi->active_worst_quality)
2383 cpi->active_worst_quality ++;
2384 else if (tmp_q < cpi->active_worst_quality)
2385 cpi->active_worst_quality --;
2387 cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2390 cpi->twopass.frames_to_key --;
2391 cpi->twopass.total_error_left -= this_frame_error;
2392 cpi->twopass.total_intra_error_left -= this_frame_intra_error;
2393 cpi->twopass.total_coded_error_left -= this_frame_coded_error;
2397 static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2399 BOOL is_viable_kf = FALSE;
2401 // Does the frame satisfy the primary criteria of a key frame
2402 // If so, then examine how well it predicts subsequent frames
2403 if ((this_frame->pcnt_second_ref < 0.10) &&
2404 (next_frame->pcnt_second_ref < 0.10) &&
2405 ((this_frame->pcnt_inter < 0.05) ||
2407 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2408 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2409 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2410 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2411 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2418 FIRSTPASS_STATS *start_pos;
2420 FIRSTPASS_STATS local_next_frame;
2422 double boost_score = 0.0;
2423 double old_boost_score = 0.0;
2424 double decay_accumulator = 1.0;
2425 double next_iiratio;
2427 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2429 // Note the starting file position so we can reset to it
2430 start_pos = cpi->twopass.stats_in;
2432 // Examine how well the key frame predicts subsequent frames
2433 for (i = 0 ; i < 16; i++)
2435 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
2437 if (next_iiratio > RMAX)
2438 next_iiratio = RMAX;
2440 // Cumulative effect of decay in prediction quality
2441 if (local_next_frame.pcnt_inter > 0.85)
2442 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2444 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2446 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2448 // Keep a running total
2449 boost_score += (decay_accumulator * next_iiratio);
2451 // Test various breakout clauses
2452 if ((local_next_frame.pcnt_inter < 0.05) ||
2453 (next_iiratio < 1.5) ||
2454 (((local_next_frame.pcnt_inter -
2455 local_next_frame.pcnt_neutral) < 0.20) &&
2456 (next_iiratio < 3.0)) ||
2457 ((boost_score - old_boost_score) < 0.5) ||
2458 (local_next_frame.intra_error < 200)
2464 old_boost_score = boost_score;
2466 // Get the next frame details
2467 if (EOF == input_stats(cpi, &local_next_frame))
2471 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2472 if (boost_score > 5.0 && (i > 3))
2473 is_viable_kf = TRUE;
2476 // Reset the file position
2477 reset_fpf_position(cpi, start_pos);
2479 is_viable_kf = FALSE;
2483 return is_viable_kf;
2485 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2488 FIRSTPASS_STATS last_frame;
2489 FIRSTPASS_STATS first_frame;
2490 FIRSTPASS_STATS next_frame;
2491 FIRSTPASS_STATS *start_position;
2493 double decay_accumulator = 1.0;
2494 double boost_score = 0;
2495 double old_boost_score = 0.0;
2496 double loop_decay_rate;
2498 double kf_mod_err = 0.0;
2499 double kf_group_err = 0.0;
2500 double kf_group_intra_err = 0.0;
2501 double kf_group_coded_err = 0.0;
2502 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2504 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2506 vp8_clear_system_state(); //__asm emms;
2507 start_position = cpi->twopass.stats_in;
2509 cpi->common.frame_type = KEY_FRAME;
2511 // is this a forced key frame by interval
2512 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2514 // Clear the alt ref active flag as this can never be active on a key frame
2515 cpi->source_alt_ref_active = FALSE;
2517 // Kf is always a gf so clear frames till next gf counter
2518 cpi->frames_till_gf_update_due = 0;
2520 cpi->twopass.frames_to_key = 1;
2522 // Take a copy of the initial frame details
2523 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2525 cpi->twopass.kf_group_bits = 0; // Total bits avaialable to kf group
2526 cpi->twopass.kf_group_error_left = 0; // Group modified error score.
2528 kf_mod_err = calculate_modified_err(cpi, this_frame);
2530 // find the next keyframe
2532 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end)
2534 // Accumulate kf group error
2535 kf_group_err += calculate_modified_err(cpi, this_frame);
2537 // These figures keep intra and coded error counts for all frames including key frames in the group.
2538 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2539 kf_group_intra_err += this_frame->intra_error;
2540 kf_group_coded_err += this_frame->coded_error;
2542 // load a the next frame's stats
2543 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2544 input_stats(cpi, this_frame);
2546 // Provided that we are not at the end of the file...
2547 if (cpi->oxcf.auto_key
2548 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2550 // Normal scene cut check
2551 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
2554 // How fast is prediction quality decaying
2555 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2557 // We want to know something about the recent past... rather than
2558 // as used elsewhere where we are concened with decay in prediction
2559 // quality since the last GF or KF.
2560 recent_loop_decay[i%8] = loop_decay_rate;
2561 decay_accumulator = 1.0;
2562 for (j = 0; j < 8; j++)
2564 decay_accumulator = decay_accumulator * recent_loop_decay[j];
2567 // Special check for transition or high motion followed by a
2568 // to a static scene.
2569 if ( detect_transition_to_still( cpi, i,
2570 (cpi->key_frame_frequency-i),
2572 decay_accumulator ) )
2578 // Step on to the next frame
2579 cpi->twopass.frames_to_key ++;
2581 // If we don't have a real key frame within the next two
2582 // forcekeyframeevery intervals then break out of the loop.
2583 if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2586 cpi->twopass.frames_to_key ++;
2591 // If there is a max kf interval set by the user we must obey it.
2592 // We already breakout of the loop above at 2x max.
2593 // This code centers the extra kf if the actual natural
2594 // interval is between 1x and 2x
2595 if (cpi->oxcf.auto_key
2596 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency )
2598 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2599 FIRSTPASS_STATS tmp_frame;
2601 cpi->twopass.frames_to_key /= 2;
2603 // Copy first frame details
2604 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2606 // Reset to the start of the group
2607 reset_fpf_position(cpi, start_position);
2610 kf_group_intra_err = 0;
2611 kf_group_coded_err = 0;
2613 // Rescan to get the correct error data for the forced kf group
2614 for( i = 0; i < cpi->twopass.frames_to_key; i++ )
2616 // Accumulate kf group errors
2617 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2618 kf_group_intra_err += tmp_frame.intra_error;
2619 kf_group_coded_err += tmp_frame.coded_error;
2621 // Load a the next frame's stats
2622 input_stats(cpi, &tmp_frame);
2625 // Reset to the start of the group
2626 reset_fpf_position(cpi, current_pos);
2628 cpi->next_key_frame_forced = TRUE;
2631 cpi->next_key_frame_forced = FALSE;
2633 // Special case for the last frame of the file
2634 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
2636 // Accumulate kf group error
2637 kf_group_err += calculate_modified_err(cpi, this_frame);
2639 // These figures keep intra and coded error counts for all frames including key frames in the group.
2640 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2641 kf_group_intra_err += this_frame->intra_error;
2642 kf_group_coded_err += this_frame->coded_error;
2645 // Calculate the number of bits that should be assigned to the kf group.
2646 if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0))
2648 // Max for a single normal frame (not key frame)
2649 int max_bits = frame_max_bits(cpi);
2651 // Maximum bits for the kf group
2652 long long max_grp_bits;
2654 // Default allocation based on bits left and relative
2655 // complexity of the section
2656 cpi->twopass.kf_group_bits = (long long)( cpi->twopass.bits_left *
2658 cpi->twopass.modified_error_left ));
2660 // Clip based on maximum per frame rate defined by the user.
2661 max_grp_bits = (long long)max_bits * (long long)cpi->twopass.frames_to_key;
2662 if (cpi->twopass.kf_group_bits > max_grp_bits)
2663 cpi->twopass.kf_group_bits = max_grp_bits;
2665 // Additional special case for CBR if buffer is getting full.
2666 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2668 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2669 int buffer_lvl = cpi->buffer_level;
2671 // If the buffer is near or above the optimal and this kf group is
2672 // not being allocated much then increase the allocation a bit.
2673 if (buffer_lvl >= opt_buffer_lvl)
2675 int high_water_mark = (opt_buffer_lvl +
2676 cpi->oxcf.maximum_buffer_size) >> 1;
2678 long long av_group_bits;
2680 // Av bits per frame * number of frames
2681 av_group_bits = (long long)cpi->av_per_frame_bandwidth *
2682 (long long)cpi->twopass.frames_to_key;
2684 // We are at or above the maximum.
2685 if (cpi->buffer_level >= high_water_mark)
2687 long long min_group_bits;
2689 min_group_bits = av_group_bits +
2690 (long long)(buffer_lvl -
2693 if (cpi->twopass.kf_group_bits < min_group_bits)
2694 cpi->twopass.kf_group_bits = min_group_bits;
2696 // We are above optimal but below the maximum
2697 else if (cpi->twopass.kf_group_bits < av_group_bits)
2699 long long bits_below_av = av_group_bits -
2700 cpi->twopass.kf_group_bits;
2702 cpi->twopass.kf_group_bits +=
2703 (long long)((double)bits_below_av *
2704 (double)(buffer_lvl - opt_buffer_lvl) /
2705 (double)(high_water_mark - opt_buffer_lvl));
2711 cpi->twopass.kf_group_bits = 0;
2713 // Reset the first pass file position
2714 reset_fpf_position(cpi, start_position);
2716 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2717 decay_accumulator = 1.0;
2719 loop_decay_rate = 1.00; // Starting decay rate
2721 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2725 if (EOF == input_stats(cpi, &next_frame))
2728 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
2729 r = (IIKFACTOR2 * next_frame.intra_error /
2730 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2732 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2733 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2738 // How fast is prediction quality decaying
2739 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2741 decay_accumulator = decay_accumulator * loop_decay_rate;
2742 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2744 boost_score += (decay_accumulator * r);
2746 if ((i > MIN_GF_INTERVAL) &&
2747 ((boost_score - old_boost_score) < 1.0))
2752 old_boost_score = boost_score;
2757 FIRSTPASS_STATS sectionstats;
2760 zero_stats(§ionstats);
2761 reset_fpf_position(cpi, start_position);
2763 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2765 input_stats(cpi, &next_frame);
2766 accumulate_stats(§ionstats, &next_frame);
2769 avg_stats(§ionstats);
2771 cpi->twopass.section_intra_rating =
2772 sectionstats.intra_error
2773 / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2775 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2776 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2778 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2780 if (cpi->twopass.section_max_qfactor < 0.80)
2781 cpi->twopass.section_max_qfactor = 0.80;
2785 // cpi->twopass.section_max_qfactor = 1.0;
2788 // When using CBR apply additional buffer fullness related upper limits
2789 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2793 if (cpi->drop_frames_allowed)
2795 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
2797 if (cpi->buffer_level > df_buffer_level)
2798 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2802 else if (cpi->buffer_level > 0)
2804 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2811 if (boost_score > max_boost)
2812 boost_score = max_boost;
2815 // Reset the first pass file position
2816 reset_fpf_position(cpi, start_position);
2818 // Work out how many bits to allocate for the key frame itself
2821 int kf_boost = boost_score;
2822 int allocation_chunks;
2823 int Counter = cpi->twopass.frames_to_key;
2825 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2826 // Min boost based on kf interval
2829 while ((kf_boost < 48) && (Counter > 0))
2839 kf_boost += ((Counter + 1) >> 1);
2841 if (kf_boost > 48) kf_boost = 48;
2844 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2845 if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
2846 kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2847 else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
2848 kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2850 kf_boost = (int)((double)kf_boost * 100.0) >> 4; // Scale 16 to 100
2852 // Adjustment to boost based on recent average q
2853 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2855 if (kf_boost < 250) // Min KF boost
2858 // We do three calculations for kf size.
2859 // The first is based on the error score for the whole kf group.
2860 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2861 // The final one insures that the frame receives at least the allocation it would have received based on its own error score vs the error score remaining
2863 allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost; // cpi->twopass.frames_to_key-1 because key frame itself is taken care of by kf_boost
2865 // Normalize Altboost and allocations chunck down to prevent overflow
2866 while (kf_boost > 1000)
2869 allocation_chunks /= 2;
2872 cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
2874 // Calculate the number of bits to be spent on the key frame
2875 cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
2877 // Apply an additional limit for CBR
2878 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2880 if (cpi->twopass.kf_bits > ((3 * cpi->buffer_level) >> 2))
2881 cpi->twopass.kf_bits = (3 * cpi->buffer_level) >> 2;
2884 // If the key frame is actually easier than the average for the
2885 // kf group (which does sometimes happen... eg a blank intro frame)
2886 // Then use an alternate calculation based on the kf error score
2887 // which should give a smaller key frame.
2888 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key)
2890 double alt_kf_grp_bits =
2891 ((double)cpi->twopass.bits_left *
2892 (kf_mod_err * (double)cpi->twopass.frames_to_key) /
2893 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
2895 alt_kf_bits = (int)((double)kf_boost *
2896 (alt_kf_grp_bits / (double)allocation_chunks));
2898 if (cpi->twopass.kf_bits > alt_kf_bits)
2900 cpi->twopass.kf_bits = alt_kf_bits;
2903 // Else if it is much harder than other frames in the group make sure
2904 // it at least receives an allocation in keeping with its relative
2909 (int)((double)cpi->twopass.bits_left *
2911 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
2913 if (alt_kf_bits > cpi->twopass.kf_bits)
2915 cpi->twopass.kf_bits = alt_kf_bits;
2919 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
2920 cpi->twopass.kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance
2922 cpi->per_frame_bandwidth = cpi->twopass.kf_bits; // Peer frame bit target for this frame
2923 cpi->target_bandwidth = cpi->twopass.kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate
2926 // Note the total error score of the kf group minus the key frame itself
2927 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2929 // Adjust the count of total modified error left.
2930 // The count of bits left is adjusted elsewhere based on real coded frame sizes
2931 cpi->twopass.modified_error_left -= kf_group_err;
2933 if (cpi->oxcf.allow_spatial_resampling)
2935 int resample_trigger = FALSE;
2936 int last_kf_resampled = FALSE;
2940 int new_width = cpi->oxcf.Width;
2941 int new_height = cpi->oxcf.Height;
2943 int projected_buffer_level = cpi->buffer_level;
2946 double projected_bits_perframe;
2947 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
2948 double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
2949 double bits_per_frame;
2950 double av_bits_per_frame;
2951 double effective_size_ratio;
2953 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
2954 last_kf_resampled = TRUE;
2956 // Set back to unscaled by defaults
2957 cpi->common.horiz_scale = NORMAL;
2958 cpi->common.vert_scale = NORMAL;
2960 // Calculate Average bits per frame.
2961 //av_bits_per_frame = cpi->twopass.bits_left/(double)(cpi->twopass.total_stats->count - cpi->common.current_video_frame);
2962 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate);
2963 //if ( av_bits_per_frame < 0.0 )
2964 // av_bits_per_frame = 0.0
2966 // CBR... Use the clip average as the target for deciding resample
2967 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2969 bits_per_frame = av_bits_per_frame;
2972 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
2973 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
2976 bits_per_frame = cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key; // This accounts for how hard the section is...
2978 if (bits_per_frame < av_bits_per_frame) // Dont turn to resampling in easy sections just because they have been assigned a small number of bits
2979 bits_per_frame = av_bits_per_frame;
2982 // bits_per_frame should comply with our minimum
2983 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
2984 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2986 // Work out if spatial resampling is necessary
2987 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, group_iiratio);
2989 // If we project a required Q higher than the maximum allowed Q then make a guess at the actual size of frames in this section
2990 projected_bits_perframe = bits_per_frame;
2993 while (tmp_q > cpi->worst_quality)
2995 projected_bits_perframe *= 1.04;
2999 // Guess at buffer level at the end of the section
3000 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->twopass.frames_to_key);
3004 FILE *f = fopen("Subsamle.stt", "a");
3005 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);
3009 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
3010 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3012 // Trigger resample if we are projected to fall below down sample level or
3013 // resampled last time and are projected to remain below the up sample level
3014 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
3015 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
3016 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
3017 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
3018 resample_trigger = TRUE;
3020 resample_trigger = FALSE;
3024 long long clip_bits = (long long)(cpi->twopass.total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
3025 long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
3027 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced
3028 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ...
3029 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits
3030 resample_trigger = TRUE;
3032 resample_trigger = FALSE;
3036 if (resample_trigger)
3038 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
3042 cpi->common.vert_scale = vscale_lookup[scale_val];
3043 cpi->common.horiz_scale = hscale_lookup[scale_val];
3045 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
3046 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
3048 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
3049 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
3051 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
3052 // effective_sizeratio attempts to provide a crude correction for this
3053 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
3054 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
3056 // Now try again and see what Q we get with the smaller image size
3057 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, group_iiratio);
3061 FILE *f = fopen("Subsamle.stt", "a");
3062 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);
3068 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
3070 cpi->common.Width = new_width;
3071 cpi->common.Height = new_height;
3072 vp8_alloc_compressor_data(cpi);