Define RDCOST only once
[libvpx.git] / vp8 / encoder / firstpass.c
blob8f24a11a02799f303b6812a91f8a8ebf7df6a433
1 /*
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.
9 */
11 #include "math.h"
12 #include "limits.h"
13 #include "block.h"
14 #include "onyx_int.h"
15 #include "variance.h"
16 #include "encodeintra.h"
17 #include "vp8/common/setupintrarecon.h"
18 #include "mcomp.h"
19 #include "vpx_scale/vpxscale.h"
20 #include "encodemb.h"
21 #include "vp8/common/extend.h"
22 #include "vp8/common/systemdependent.h"
23 #include "vpx_scale/yv12extend.h"
24 #include "vpx_mem/vpx_mem.h"
25 #include "vp8/common/swapyv12buffer.h"
26 #include <stdio.h>
27 #include "rdopt.h"
28 #include "vp8/common/quant_common.h"
29 #include "encodemv.h"
31 //#define OUTPUT_FPF 1
33 #if CONFIG_RUNTIME_CPU_DETECT
34 #define IF_RTCD(x) (x)
35 #else
36 #define IF_RTCD(x) NULL
37 #endif
39 extern void vp8_build_block_offsets(MACROBLOCK *x);
40 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
41 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
42 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv);
43 extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
45 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10))
46 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10))
47 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
48 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
50 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
52 #define IIFACTOR 1.4
53 #define IIKFACTOR1 1.40
54 #define IIKFACTOR2 1.5
55 #define RMAX 14.0
56 #define GF_RMAX 48.0
58 #define KF_MB_INTRA_MIN 300
59 #define GF_MB_INTRA_MIN 200
61 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
63 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
64 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
66 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
67 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
70 static const int cq_level[QINDEX_RANGE] =
72 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
73 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
74 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
75 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
76 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
77 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
78 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
79 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
82 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
84 static int encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred)
87 int i;
88 int intra_pred_var = 0;
89 (void) cpi;
91 if (use_dc_pred)
93 x->e_mbd.mode_info_context->mbmi.mode = DC_PRED;
94 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
95 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
97 vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
99 else
101 for (i = 0; i < 16; i++)
103 BLOCKD *b = &x->e_mbd.block[i];
104 BLOCK *be = &x->block[i];
106 vp8_encode_intra4x4block(IF_RTCD(&cpi->rtcd), x, be, b, B_DC_PRED);
110 intra_pred_var = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff);
112 return intra_pred_var;
115 // Resets the first pass file to the given position using a relative seek from the current position
116 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
118 cpi->stats_in = Position;
121 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
123 if (cpi->stats_in >= cpi->stats_in_end)
124 return EOF;
126 *next_frame = *cpi->stats_in;
127 return 1;
130 // Calculate a modified Error used in distributing bits between easier and harder frames
131 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
133 double av_err = cpi->total_stats->ssim_weighted_pred_err;
134 double this_err = this_frame->ssim_weighted_pred_err;
135 double modified_err;
137 //double relative_next_iiratio;
138 //double next_iiratio;
139 //double sum_iiratio;
140 //int i;
142 //FIRSTPASS_STATS next_frame;
143 //FIRSTPASS_STATS *start_pos;
145 /*start_pos = cpi->stats_in;
146 sum_iiratio = 0.0;
147 i = 0;
148 while ( (i < 1) && input_stats(cpi,&next_frame) != EOF )
151 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
152 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio;
153 sum_iiratio += next_iiratio;
154 i++;
156 if ( i > 0 )
158 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->avg_iiratio * (double)i);
160 else
162 relative_next_iiratio = 1.0;
164 reset_fpf_position(cpi, start_pos);*/
166 if (this_err > av_err)
167 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
168 else
169 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
172 relative_next_iiratio = pow(relative_next_iiratio,0.25);
173 modified_err = modified_err * relative_next_iiratio;
176 return modified_err;
179 static const double weight_table[256] = {
180 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
181 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
182 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
183 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
184 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
185 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
186 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
187 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
188 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
189 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
190 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
191 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
192 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
193 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
194 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
195 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
196 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
197 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
198 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
199 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
200 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
201 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
202 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
203 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
204 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
205 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
206 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
207 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
208 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
209 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
210 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
211 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
214 static double simple_weight(YV12_BUFFER_CONFIG *source)
216 int i, j;
218 unsigned char *src = source->y_buffer;
219 double sum_weights = 0.0;
221 // Loop throught the Y plane raw examining levels and creating a weight for the image
222 i = source->y_height;
225 j = source->y_width;
228 sum_weights += weight_table[ *src];
229 src++;
230 }while(--j);
231 src -= source->y_width;
232 src += source->y_stride;
233 }while(--i);
235 sum_weights /= (source->y_height * source->y_width);
237 return sum_weights;
241 // This function returns the current per frame maximum bitrate target
242 static int frame_max_bits(VP8_COMP *cpi)
244 // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
245 int max_bits;
247 // For CBR we need to also consider buffer fullness.
248 // If we are running below the optimal level then we need to gradually tighten up on max_bits.
249 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
251 double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
253 // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
254 max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
256 // If our buffer is below the optimum level
257 if (buffer_fullness_ratio < 1.0)
259 // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
260 int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
262 max_bits = (int)(max_bits * buffer_fullness_ratio);
264 if (max_bits < min_max_bits)
265 max_bits = min_max_bits; // Lowest value we will set ... which should allow the buffer to refil.
268 // VBR
269 else
271 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
272 max_bits = (int)(((double)cpi->bits_left / (cpi->total_stats->count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
275 // Trap case where we are out of bits
276 if (max_bits < 0)
277 max_bits = 0;
279 return max_bits;
283 static void output_stats(const VP8_COMP *cpi,
284 struct vpx_codec_pkt_list *pktlist,
285 FIRSTPASS_STATS *stats)
287 struct vpx_codec_cx_pkt pkt;
288 pkt.kind = VPX_CODEC_STATS_PKT;
289 pkt.data.twopass_stats.buf = stats;
290 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
291 vpx_codec_pkt_list_add(pktlist, &pkt);
293 // TEMP debug code
294 #if OUTPUT_FPF
297 FILE *fpfile;
298 fpfile = fopen("firstpass.stt", "a");
300 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
301 " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
302 " %12.0f %12.4f\n",
303 stats->frame,
304 stats->intra_error,
305 stats->coded_error,
306 stats->ssim_weighted_pred_err,
307 stats->pcnt_inter,
308 stats->pcnt_motion,
309 stats->pcnt_second_ref,
310 stats->pcnt_neutral,
311 stats->MVr,
312 stats->mvr_abs,
313 stats->MVc,
314 stats->mvc_abs,
315 stats->MVrv,
316 stats->MVcv,
317 stats->mv_in_out_count,
318 stats->count,
319 stats->duration);
320 fclose(fpfile);
322 #endif
325 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
327 if (cpi->stats_in >= cpi->stats_in_end)
328 return EOF;
330 *fps = *cpi->stats_in;
331 cpi->stats_in = (void*)((char *)cpi->stats_in + sizeof(FIRSTPASS_STATS));
332 return 1;
335 static void zero_stats(FIRSTPASS_STATS *section)
337 section->frame = 0.0;
338 section->intra_error = 0.0;
339 section->coded_error = 0.0;
340 section->ssim_weighted_pred_err = 0.0;
341 section->pcnt_inter = 0.0;
342 section->pcnt_motion = 0.0;
343 section->pcnt_second_ref = 0.0;
344 section->pcnt_neutral = 0.0;
345 section->MVr = 0.0;
346 section->mvr_abs = 0.0;
347 section->MVc = 0.0;
348 section->mvc_abs = 0.0;
349 section->MVrv = 0.0;
350 section->MVcv = 0.0;
351 section->mv_in_out_count = 0.0;
352 section->count = 0.0;
353 section->duration = 1.0;
355 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
357 section->frame += frame->frame;
358 section->intra_error += frame->intra_error;
359 section->coded_error += frame->coded_error;
360 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
361 section->pcnt_inter += frame->pcnt_inter;
362 section->pcnt_motion += frame->pcnt_motion;
363 section->pcnt_second_ref += frame->pcnt_second_ref;
364 section->pcnt_neutral += frame->pcnt_neutral;
365 section->MVr += frame->MVr;
366 section->mvr_abs += frame->mvr_abs;
367 section->MVc += frame->MVc;
368 section->mvc_abs += frame->mvc_abs;
369 section->MVrv += frame->MVrv;
370 section->MVcv += frame->MVcv;
371 section->mv_in_out_count += frame->mv_in_out_count;
372 section->count += frame->count;
373 section->duration += frame->duration;
375 static void avg_stats(FIRSTPASS_STATS *section)
377 if (section->count < 1.0)
378 return;
380 section->intra_error /= section->count;
381 section->coded_error /= section->count;
382 section->ssim_weighted_pred_err /= section->count;
383 section->pcnt_inter /= section->count;
384 section->pcnt_second_ref /= section->count;
385 section->pcnt_neutral /= section->count;
386 section->pcnt_motion /= section->count;
387 section->MVr /= section->count;
388 section->mvr_abs /= section->count;
389 section->MVc /= section->count;
390 section->mvc_abs /= section->count;
391 section->MVrv /= section->count;
392 section->MVcv /= section->count;
393 section->mv_in_out_count /= section->count;
394 section->duration /= section->count;
397 void vp8_init_first_pass(VP8_COMP *cpi)
399 zero_stats(cpi->total_stats);
402 void vp8_end_first_pass(VP8_COMP *cpi)
404 output_stats(cpi, cpi->output_pkt_list, cpi->total_stats);
407 static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset )
409 MACROBLOCKD * const xd = & x->e_mbd;
410 BLOCK *b = &x->block[0];
411 BLOCKD *d = &x->e_mbd.block[0];
413 unsigned char *src_ptr = (*(b->base_src) + b->src);
414 int src_stride = b->src_stride;
415 unsigned char *ref_ptr;
416 int ref_stride=d->pre_stride;
418 // Set up pointers for this macro block recon buffer
419 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
421 ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre );
423 VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_stride, ref_ptr, ref_stride, (unsigned int *)(best_motion_err));
426 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, MV *ref_mv, MV *best_mv, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset )
428 MACROBLOCKD *const xd = & x->e_mbd;
429 BLOCK *b = &x->block[0];
430 BLOCKD *d = &x->e_mbd.block[0];
431 int num00;
433 MV tmp_mv = {0, 0};
435 int tmp_err;
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;
438 int n;
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
449 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param, x->errorperbit, &num00, &v_fn_ptr, x->mvcost, ref_mv);
450 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
451 tmp_err += new_mv_mode_penalty;
453 if (tmp_err < *best_motion_err)
455 *best_motion_err = tmp_err;
456 best_mv->row = tmp_mv.row;
457 best_mv->col = tmp_mv.col;
460 // Further step/diamond searches as necessary
461 n = num00;
462 num00 = 0;
464 while (n < further_steps)
466 n++;
468 if (num00)
469 num00--;
470 else
472 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param + n, x->errorperbit, &num00, &v_fn_ptr, x->mvcost, ref_mv);
473 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
474 tmp_err += new_mv_mode_penalty;
476 if (tmp_err < *best_motion_err)
478 *best_motion_err = tmp_err;
479 best_mv->row = tmp_mv.row;
480 best_mv->col = tmp_mv.col;
486 void vp8_first_pass(VP8_COMP *cpi)
488 int mb_row, mb_col;
489 MACROBLOCK *const x = & cpi->mb;
490 VP8_COMMON *const cm = & cpi->common;
491 MACROBLOCKD *const xd = & x->e_mbd;
493 int recon_yoffset, recon_uvoffset;
494 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
495 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
496 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
497 int recon_y_stride = lst_yv12->y_stride;
498 int recon_uv_stride = lst_yv12->uv_stride;
499 long long intra_error = 0;
500 long long coded_error = 0;
502 int sum_mvr = 0, sum_mvc = 0;
503 int sum_mvr_abs = 0, sum_mvc_abs = 0;
504 int sum_mvrs = 0, sum_mvcs = 0;
505 int mvcount = 0;
506 int intercount = 0;
507 int second_ref_count = 0;
508 int intrapenalty = 256;
509 int neutral_count = 0;
511 int sum_in_vectors = 0;
513 MV zero_ref_mv = {0, 0};
515 vp8_clear_system_state(); //__asm emms;
517 x->src = * cpi->Source;
518 xd->pre = *lst_yv12;
519 xd->dst = *new_yv12;
521 x->partition_info = x->pi;
523 xd->mode_info_context = cm->mi;
525 vp8_build_block_offsets(x);
527 vp8_setup_block_dptrs(&x->e_mbd);
529 vp8_setup_block_ptrs(x);
531 // set up frame new frame for intra coded blocks
532 vp8_setup_intra_recon(new_yv12);
533 vp8cx_frame_init_quantizer(cpi);
535 // Initialise the MV cost table to the defaults
536 //if( cm->current_video_frame == 0)
537 //if ( 0 )
539 int flag[2] = {1, 1};
540 vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
541 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
542 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
545 // for each macroblock row in image
546 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
548 int_mv best_ref_mv;
550 best_ref_mv.as_int = 0;
552 // reset above block coeffs
553 xd->up_available = (mb_row != 0);
554 recon_yoffset = (mb_row * recon_y_stride * 16);
555 recon_uvoffset = (mb_row * recon_uv_stride * 8);
557 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
558 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
559 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
562 // for each macroblock col in image
563 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
565 int this_error;
566 int gf_motion_error = INT_MAX;
567 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
569 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
570 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
571 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
572 xd->left_available = (mb_col != 0);
574 // do intra 16x16 prediction
575 this_error = encode_intra(cpi, x, use_dc_pred);
577 // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
578 // 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.
579 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
580 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
581 this_error += intrapenalty;
583 // Cumulative intra error total
584 intra_error += (long long)this_error;
586 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
587 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
588 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
590 // Other than for the first frame do a motion search
591 if (cm->current_video_frame > 0)
593 BLOCKD *d = &x->e_mbd.block[0];
594 MV tmp_mv = {0, 0};
595 int tmp_err;
596 int motion_error = INT_MAX;
598 // Simple 0,0 motion with no mv overhead
599 zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset );
600 d->bmi.mv.as_mv.row = 0;
601 d->bmi.mv.as_mv.col = 0;
603 // Test last reference frame using the previous best mv as the
604 // starting point (best reference) for the search
605 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv,
606 &d->bmi.mv.as_mv, lst_yv12,
607 &motion_error, recon_yoffset);
609 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
610 if (best_ref_mv.as_int)
612 tmp_err = INT_MAX;
613 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
614 lst_yv12, &tmp_err, recon_yoffset);
616 if ( tmp_err < motion_error )
618 motion_error = tmp_err;
619 d->bmi.mv.as_mv.row = tmp_mv.row;
620 d->bmi.mv.as_mv.col = tmp_mv.col;
624 // Experimental search in a second reference frame ((0,0) based only)
625 if (cm->current_video_frame > 1)
627 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
629 if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
631 second_ref_count++;
632 //motion_error = gf_motion_error;
633 //d->bmi.mv.as_mv.row = tmp_mv.row;
634 //d->bmi.mv.as_mv.col = tmp_mv.col;
636 /*else
638 xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
639 xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
640 xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
644 // Reset to last frame as reference buffer
645 xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
646 xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
647 xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
650 /* Intra assumed best */
651 best_ref_mv.as_int = 0;
653 if (motion_error <= this_error)
655 // Keep a count of cases where the inter and intra were
656 // very close and very low. This helps with scene cut
657 // detection for example in cropped clips with black bars
658 // at the sides or top and bottom.
659 if( (((this_error-intrapenalty) * 9) <=
660 (motion_error*10)) &&
661 (this_error < (2*intrapenalty)) )
663 neutral_count++;
666 d->bmi.mv.as_mv.row <<= 3;
667 d->bmi.mv.as_mv.col <<= 3;
668 this_error = motion_error;
669 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv.as_mv);
670 vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x);
671 sum_mvr += d->bmi.mv.as_mv.row;
672 sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
673 sum_mvc += d->bmi.mv.as_mv.col;
674 sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
675 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
676 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
677 intercount++;
679 best_ref_mv.as_int = d->bmi.mv.as_int;
681 // Was the vector non-zero
682 if (d->bmi.mv.as_int)
684 mvcount++;
686 // Does the Row vector point inwards or outwards
687 if (mb_row < cm->mb_rows / 2)
689 if (d->bmi.mv.as_mv.row > 0)
690 sum_in_vectors--;
691 else if (d->bmi.mv.as_mv.row < 0)
692 sum_in_vectors++;
694 else if (mb_row > cm->mb_rows / 2)
696 if (d->bmi.mv.as_mv.row > 0)
697 sum_in_vectors++;
698 else if (d->bmi.mv.as_mv.row < 0)
699 sum_in_vectors--;
702 // Does the Row vector point inwards or outwards
703 if (mb_col < cm->mb_cols / 2)
705 if (d->bmi.mv.as_mv.col > 0)
706 sum_in_vectors--;
707 else if (d->bmi.mv.as_mv.col < 0)
708 sum_in_vectors++;
710 else if (mb_col > cm->mb_cols / 2)
712 if (d->bmi.mv.as_mv.col > 0)
713 sum_in_vectors++;
714 else if (d->bmi.mv.as_mv.col < 0)
715 sum_in_vectors--;
721 coded_error += (long long)this_error;
723 // adjust to the next column of macroblocks
724 x->src.y_buffer += 16;
725 x->src.u_buffer += 8;
726 x->src.v_buffer += 8;
728 recon_yoffset += 16;
729 recon_uvoffset += 8;
732 // adjust to the next row of mbs
733 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
734 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
735 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
737 //extend the recon for intra prediction
738 vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
739 vp8_clear_system_state(); //__asm emms;
742 vp8_clear_system_state(); //__asm emms;
744 double weight = 0.0;
746 FIRSTPASS_STATS fps;
748 fps.frame = cm->current_video_frame ;
749 fps.intra_error = intra_error >> 8;
750 fps.coded_error = coded_error >> 8;
751 weight = simple_weight(cpi->Source);
754 if (weight < 0.1)
755 weight = 0.1;
757 fps.ssim_weighted_pred_err = fps.coded_error * weight;
759 fps.pcnt_inter = 0.0;
760 fps.pcnt_motion = 0.0;
761 fps.MVr = 0.0;
762 fps.mvr_abs = 0.0;
763 fps.MVc = 0.0;
764 fps.mvc_abs = 0.0;
765 fps.MVrv = 0.0;
766 fps.MVcv = 0.0;
767 fps.mv_in_out_count = 0.0;
768 fps.count = 1.0;
770 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
771 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
772 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
774 if (mvcount > 0)
776 fps.MVr = (double)sum_mvr / (double)mvcount;
777 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
778 fps.MVc = (double)sum_mvc / (double)mvcount;
779 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
780 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
781 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
782 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
784 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
787 // TODO: handle the case when duration is set to 0, or something less
788 // than the full time between subsequent cpi->source_time_stamp s .
789 fps.duration = cpi->source_end_time_stamp - cpi->source_time_stamp;
791 // don't want to do output stats with a stack variable!
792 memcpy(cpi->this_frame_stats,
793 &fps,
794 sizeof(FIRSTPASS_STATS));
795 output_stats(cpi, cpi->output_pkt_list, cpi->this_frame_stats);
796 accumulate_stats(cpi->total_stats, &fps);
799 // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
800 if ((cm->current_video_frame > 0) &&
801 (cpi->this_frame_stats->pcnt_inter > 0.20) &&
802 ((cpi->this_frame_stats->intra_error / cpi->this_frame_stats->coded_error) > 2.0))
804 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
807 // swap frame pointers so last frame refers to the frame we just compressed
808 vp8_swap_yv12_buffer(lst_yv12, new_yv12);
809 vp8_yv12_extend_frame_borders(lst_yv12);
811 // Special case for the first frame. Copy into the GF buffer as a second reference.
812 if (cm->current_video_frame == 0)
814 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
818 // use this to see what the first pass reconstruction looks like
819 if (0)
821 char filename[512];
822 FILE *recon_file;
823 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
825 if (cm->current_video_frame == 0)
826 recon_file = fopen(filename, "wb");
827 else
828 recon_file = fopen(filename, "ab");
830 if(fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file));
831 fclose(recon_file);
834 cm->current_video_frame++;
837 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
839 #define BASE_ERRPERMB 150
840 static int estimate_max_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
842 int Q;
843 int num_mbs = cpi->common.MBs;
844 int target_norm_bits_per_mb;
846 double err_per_mb = section_err / num_mbs;
847 double correction_factor;
848 double corr_high;
849 double speed_correction = 1.0;
850 double rolling_ratio;
852 double pow_highq = 0.90;
853 double pow_lowq = 0.40;
855 if (section_target_bandwitdh <= 0)
856 return cpi->maxq_max_limit; // Highest value allowed
858 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
860 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
861 if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->worst_quality))
863 rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
865 //if ( cpi->est_max_qcorrection_factor > rolling_ratio )
866 if (rolling_ratio < 0.95)
867 //cpi->est_max_qcorrection_factor *= adjustment_rate;
868 cpi->est_max_qcorrection_factor -= 0.005;
869 //else if ( cpi->est_max_qcorrection_factor < rolling_ratio )
870 else if (rolling_ratio > 1.05)
871 cpi->est_max_qcorrection_factor += 0.005;
873 //cpi->est_max_qcorrection_factor /= adjustment_rate;
875 cpi->est_max_qcorrection_factor = (cpi->est_max_qcorrection_factor < 0.1) ? 0.1 : (cpi->est_max_qcorrection_factor > 10.0) ? 10.0 : cpi->est_max_qcorrection_factor;
878 // Corrections for higher compression speed settings (reduced compression expected)
879 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
881 if (cpi->oxcf.cpu_used <= 5)
882 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
883 else
884 speed_correction = 1.25;
887 // Correction factor used for Q values >= 20
888 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
889 corr_high = (corr_high < 0.05)
890 ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
892 // Try and pick a max Q that will be high enough to encode the
893 // content at the given rate.
894 for (Q = cpi->maxq_min_limit; Q < cpi->maxq_max_limit; Q++)
896 int bits_per_mb_at_this_q;
898 if (Q < 50)
900 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
901 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
903 else
904 correction_factor = corr_high;
906 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * cpi->section_max_qfactor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
907 //bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
909 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
910 break;
913 // Restriction on active max q for constrained quality mode.
914 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
915 (Q < cpi->cq_target_quality) )
916 //(Q < cpi->oxcf.cq_level;) )
918 Q = cpi->cq_target_quality;
919 //Q = cpi->oxcf.cq_level;
922 // Adjust maxq_min_limit and maxq_max_limit limits based on
923 // averaga q observed in clip for non kf/gf.arf frames
924 // Give average a chance to settle though.
925 if ( (cpi->ni_frames >
926 ((unsigned int)cpi->total_stats->count >> 8)) &&
927 (cpi->ni_frames > 150) )
929 cpi->maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
930 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
931 cpi->maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
932 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
935 return Q;
937 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
939 int Q;
940 int num_mbs = cpi->common.MBs;
941 int target_norm_bits_per_mb;
943 double err_per_mb = section_err / num_mbs;
944 double correction_factor;
945 double corr_high;
946 double speed_correction = 1.0;
947 double pow_highq = 0.90;
948 double pow_lowq = 0.40;
950 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
952 // Corrections for higher compression speed settings (reduced compression expected)
953 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
955 if (cpi->oxcf.cpu_used <= 5)
956 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
957 else
958 speed_correction = 1.25;
961 // Correction factor used for Q values >= 20
962 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
963 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
965 // Try and pick a Q that can encode the content at the given rate.
966 for (Q = 0; Q < MAXQ; Q++)
968 int bits_per_mb_at_this_q;
970 if (Q < 50)
972 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
973 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
975 else
976 correction_factor = corr_high;
978 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
980 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
981 break;
984 return Q;
987 // Estimate a worst case Q for a KF group
988 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, double group_iiratio)
990 int Q;
991 int num_mbs = cpi->common.MBs;
992 int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
993 int bits_per_mb_at_this_q;
995 double err_per_mb = section_err / num_mbs;
996 double err_correction_factor;
997 double corr_high;
998 double speed_correction = 1.0;
999 double current_spend_ratio = 1.0;
1001 double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1002 double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1004 double iiratio_correction_factor = 1.0;
1006 double combined_correction_factor;
1008 // Trap special case where the target is <= 0
1009 if (target_norm_bits_per_mb <= 0)
1010 return MAXQ * 2;
1012 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
1013 // This is clamped to the range 0.1 to 10.0
1014 if (cpi->long_rolling_target_bits <= 0)
1015 current_spend_ratio = 10.0;
1016 else
1018 current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
1019 current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
1022 // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
1023 // The idea here is to favour subsampling in the hardest sections vs the easyest.
1024 iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1026 if (iiratio_correction_factor < 0.5)
1027 iiratio_correction_factor = 0.5;
1029 // Corrections for higher compression speed settings (reduced compression expected)
1030 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1032 if (cpi->oxcf.cpu_used <= 5)
1033 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1034 else
1035 speed_correction = 1.25;
1038 // Combine the various factors calculated above
1039 combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
1041 // Correction factor used for Q values >= 20
1042 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1043 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1045 // Try and pick a Q that should be high enough to encode the content at the given rate.
1046 for (Q = 0; Q < MAXQ; Q++)
1048 // Q values < 20 treated as a special case
1049 if (Q < 20)
1051 err_correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1052 err_correction_factor = (err_correction_factor < 0.05) ? 0.05 : (err_correction_factor > 5.0) ? 5.0 : err_correction_factor;
1054 else
1055 err_correction_factor = corr_high;
1057 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * combined_correction_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q]);
1059 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1060 break;
1063 // If we could not hit the target even at Max Q then estimate what Q would have bee required
1064 while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2)))
1067 bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1068 Q++;
1071 if (0)
1073 FILE *f = fopen("estkf_q.stt", "a");
1074 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,
1075 target_norm_bits_per_mb, err_per_mb, err_correction_factor,
1076 current_spend_ratio, group_iiratio, iiratio_correction_factor,
1077 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
1078 fclose(f);
1081 return Q;
1084 // For cq mode estimate a cq level that matches the observed
1085 // complexity and data rate.
1086 static int estimate_cq(VP8_COMP *cpi, double section_err, int section_target_bandwitdh)
1088 int Q;
1089 int num_mbs = cpi->common.MBs;
1090 int target_norm_bits_per_mb;
1092 double err_per_mb = section_err / num_mbs;
1093 double correction_factor;
1094 double corr_high;
1095 double speed_correction = 1.0;
1096 double pow_highq = 0.90;
1097 double pow_lowq = 0.40;
1098 double clip_iiratio;
1099 double clip_iifactor;
1101 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1102 ? (512 * section_target_bandwitdh) / num_mbs
1103 : 512 * (section_target_bandwitdh / num_mbs);
1105 // Corrections for higher compression speed settings
1106 // (reduced compression expected)
1107 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1109 if (cpi->oxcf.cpu_used <= 5)
1110 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1111 else
1112 speed_correction = 1.25;
1114 // II ratio correction factor for clip as a whole
1115 clip_iiratio = cpi->total_stats->intra_error /
1116 DOUBLE_DIVIDE_CHECK(cpi->total_stats->coded_error);
1117 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1118 if (clip_iifactor < 0.80)
1119 clip_iifactor = 0.80;
1121 // Correction factor used for Q values >= 20
1122 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1123 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1125 // Try and pick a Q that can encode the content at the given rate.
1126 for (Q = 0; Q < MAXQ; Q++)
1128 int bits_per_mb_at_this_q;
1130 if (Q < 50)
1132 correction_factor =
1133 pow( err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1135 correction_factor = (correction_factor < 0.05) ? 0.05
1136 : (correction_factor > 5.0) ? 5.0
1137 : correction_factor;
1139 else
1140 correction_factor = corr_high;
1142 bits_per_mb_at_this_q =
1143 (int)( .5 + correction_factor *
1144 speed_correction *
1145 clip_iifactor *
1146 (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1148 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1149 break;
1152 return cq_level[Q];
1155 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
1157 void vp8_init_second_pass(VP8_COMP *cpi)
1159 FIRSTPASS_STATS this_frame;
1160 FIRSTPASS_STATS *start_pos;
1162 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1164 zero_stats(cpi->total_stats);
1166 if (!cpi->stats_in_end)
1167 return;
1169 *cpi->total_stats = *cpi->stats_in_end;
1171 cpi->total_error_left = cpi->total_stats->ssim_weighted_pred_err;
1172 cpi->total_intra_error_left = cpi->total_stats->intra_error;
1173 cpi->total_coded_error_left = cpi->total_stats->coded_error;
1174 cpi->start_tot_err_left = cpi->total_error_left;
1176 //cpi->bits_left = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1177 //cpi->bits_left -= (long long)(cpi->total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1179 // each frame can have a different duration, as the frame rate in the source
1180 // isn't guaranteed to be constant. The frame rate prior to the first frame
1181 // encoded in the second pass is a guess. However the sum duration is not.
1182 // Its calculated based on the actual durations of all frames from the first
1183 // pass.
1184 vp8_new_frame_rate(cpi, 10000000.0 * cpi->total_stats->count / cpi->total_stats->duration);
1186 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1187 cpi->bits_left = (long long)(cpi->total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1188 cpi->bits_left -= (long long)(cpi->total_stats->duration * two_pass_min_rate / 10000000.0);
1189 cpi->clip_bits_total = cpi->bits_left;
1191 // Calculate a minimum intra value to be used in determining the IIratio
1192 // scores used in the second pass. We have this minimum to make sure
1193 // that clips that are static but "low complexity" in the intra domain
1194 // are still boosted appropriately for KF/GF/ARF
1195 cpi->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1196 cpi->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1198 avg_stats(cpi->total_stats);
1200 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1202 double sum_iiratio = 0.0;
1203 double IIRatio;
1205 start_pos = cpi->stats_in; // Note starting "file" position
1207 while (input_stats(cpi, &this_frame) != EOF)
1209 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1210 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1211 sum_iiratio += IIRatio;
1214 cpi->avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->total_stats->count);
1216 // Reset file position
1217 reset_fpf_position(cpi, start_pos);
1220 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1221 // used to allocate bits
1223 start_pos = cpi->stats_in; // Note starting "file" position
1225 cpi->modified_error_total = 0.0;
1226 cpi->modified_error_used = 0.0;
1228 while (input_stats(cpi, &this_frame) != EOF)
1230 cpi->modified_error_total += calculate_modified_err(cpi, &this_frame);
1232 cpi->modified_error_left = cpi->modified_error_total;
1234 reset_fpf_position(cpi, start_pos); // Reset file position
1238 // Calculate the clip target modified bits per error
1239 // The observed bpe starts as the same number.
1240 cpi->clip_bpe = cpi->bits_left /
1241 DOUBLE_DIVIDE_CHECK(cpi->modified_error_total);
1242 cpi->observed_bpe = cpi->clip_bpe;
1245 void vp8_end_second_pass(VP8_COMP *cpi)
1249 // This function gives and estimate of how badly we believe
1250 // the prediction quality is decaying from frame to frame.
1251 static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1253 double prediction_decay_rate;
1254 double motion_decay;
1255 double motion_pct = next_frame->pcnt_motion;
1258 // Initial basis is the % mbs inter coded
1259 prediction_decay_rate = next_frame->pcnt_inter;
1261 // High % motion -> somewhat higher decay rate
1262 motion_decay = (1.0 - (motion_pct / 20.0));
1263 if (motion_decay < prediction_decay_rate)
1264 prediction_decay_rate = motion_decay;
1266 // Adjustment to decay rate based on speed of motion
1268 double this_mv_rabs;
1269 double this_mv_cabs;
1270 double distance_factor;
1272 this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1273 this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1275 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
1276 (this_mv_cabs * this_mv_cabs)) / 250.0;
1277 distance_factor = ((distance_factor > 1.0)
1278 ? 0.0 : (1.0 - distance_factor));
1279 if (distance_factor < prediction_decay_rate)
1280 prediction_decay_rate = distance_factor;
1283 return prediction_decay_rate;
1286 // Function to test for a condition where a complex transition is followed
1287 // by a static section. For example in slide shows where there is a fade
1288 // between slides. This is to help with more optimal kf and gf positioning.
1289 static int detect_transition_to_still(
1290 VP8_COMP *cpi,
1291 int frame_interval,
1292 int still_interval,
1293 double loop_decay_rate,
1294 double decay_accumulator )
1296 BOOL trans_to_still = FALSE;
1298 // Break clause to detect very still sections after motion
1299 // For example a static image after a fade or other transition
1300 // instead of a clean scene cut.
1301 if ( (frame_interval > MIN_GF_INTERVAL) &&
1302 (loop_decay_rate >= 0.999) &&
1303 (decay_accumulator < 0.9) )
1305 int j;
1306 FIRSTPASS_STATS * position = cpi->stats_in;
1307 FIRSTPASS_STATS tmp_next_frame;
1308 double decay_rate;
1310 // Look ahead a few frames to see if static condition
1311 // persists...
1312 for ( j = 0; j < still_interval; j++ )
1314 if (EOF == input_stats(cpi, &tmp_next_frame))
1315 break;
1317 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1318 if ( decay_rate < 0.999 )
1319 break;
1321 // Reset file position
1322 reset_fpf_position(cpi, position);
1324 // Only if it does do we signal a transition to still
1325 if ( j == still_interval )
1326 trans_to_still = TRUE;
1329 return trans_to_still;
1332 // Analyse and define a gf/arf group .
1333 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1335 FIRSTPASS_STATS next_frame;
1336 FIRSTPASS_STATS *start_pos;
1337 int i;
1338 double boost_score = 0.0;
1339 double old_boost_score = 0.0;
1340 double gf_group_err = 0.0;
1341 double gf_first_frame_err = 0.0;
1342 double mod_frame_err = 0.0;
1344 double mv_accumulator_rabs = 0.0;
1345 double mv_accumulator_cabs = 0.0;
1346 double mv_ratio_accumulator = 0.0;
1347 double decay_accumulator = 1.0;
1349 double boost_factor = IIFACTOR;
1350 double loop_decay_rate = 1.00; // Starting decay rate
1352 double this_frame_mv_in_out = 0.0;
1353 double mv_in_out_accumulator = 0.0;
1354 double abs_mv_in_out_accumulator = 0.0;
1355 double mod_err_per_mb_accumulator = 0.0;
1357 int max_bits = frame_max_bits(cpi); // Max for a single frame
1359 unsigned int allow_alt_ref =
1360 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1362 cpi->gf_group_bits = 0;
1363 cpi->gf_decay_rate = 0;
1365 vp8_clear_system_state(); //__asm emms;
1367 start_pos = cpi->stats_in;
1369 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1371 // Preload the stats for the next frame.
1372 mod_frame_err = calculate_modified_err(cpi, this_frame);
1374 // Note the error of the frame at the start of the group (this will be
1375 // the GF frame error if we code a normal gf
1376 gf_first_frame_err = mod_frame_err;
1378 // Special treatment if the current frame is a key frame (which is also
1379 // a gf). If it is then its error score (and hence bit allocation) need
1380 // to be subtracted out from the calculation for the GF group
1381 if (cpi->common.frame_type == KEY_FRAME)
1382 gf_group_err -= gf_first_frame_err;
1384 // Scan forward to try and work out how many frames the next gf group
1385 // should contain and what level of boost is appropriate for the GF
1386 // or ARF that will be coded with the group
1387 i = 0;
1389 while (((i < cpi->static_scene_max_gf_interval) ||
1390 ((cpi->frames_to_key - i) < MIN_GF_INTERVAL)) &&
1391 (i < cpi->frames_to_key))
1393 double r;
1394 double this_frame_mvr_ratio;
1395 double this_frame_mvc_ratio;
1396 //double motion_pct = next_frame.pcnt_motion;
1397 double motion_pct;
1399 i++; // Increment the loop counter
1401 // Accumulate error score of frames in this gf group
1402 mod_frame_err = calculate_modified_err(cpi, this_frame);
1404 gf_group_err += mod_frame_err;
1406 mod_err_per_mb_accumulator +=
1407 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1409 if (EOF == input_stats(cpi, &next_frame))
1410 break;
1412 // Accumulate motion stats.
1413 motion_pct = next_frame.pcnt_motion;
1414 mv_accumulator_rabs += fabs(next_frame.mvr_abs * motion_pct);
1415 mv_accumulator_cabs += fabs(next_frame.mvc_abs * motion_pct);
1417 //Accumulate Motion In/Out of frame stats
1418 this_frame_mv_in_out =
1419 next_frame.mv_in_out_count * motion_pct;
1420 mv_in_out_accumulator +=
1421 next_frame.mv_in_out_count * motion_pct;
1422 abs_mv_in_out_accumulator +=
1423 fabs(next_frame.mv_in_out_count * motion_pct);
1425 // If there is a significant amount of motion
1426 if (motion_pct > 0.05)
1428 this_frame_mvr_ratio = fabs(next_frame.mvr_abs) /
1429 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVr));
1431 this_frame_mvc_ratio = fabs(next_frame.mvc_abs) /
1432 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVc));
1434 mv_ratio_accumulator +=
1435 (this_frame_mvr_ratio < next_frame.mvr_abs)
1436 ? (this_frame_mvr_ratio * motion_pct)
1437 : next_frame.mvr_abs * motion_pct;
1439 mv_ratio_accumulator +=
1440 (this_frame_mvc_ratio < next_frame.mvc_abs)
1441 ? (this_frame_mvc_ratio * motion_pct)
1442 : next_frame.mvc_abs * motion_pct;
1444 else
1446 mv_ratio_accumulator += 0.0;
1447 this_frame_mvr_ratio = 1.0;
1448 this_frame_mvc_ratio = 1.0;
1451 // Underlying boost factor is based on inter intra error ratio
1452 r = ( boost_factor *
1453 ( next_frame.intra_error /
1454 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)));
1456 if (next_frame.intra_error > cpi->gf_intra_err_min)
1457 r = (IIKFACTOR2 * next_frame.intra_error /
1458 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1459 else
1460 r = (IIKFACTOR2 * cpi->gf_intra_err_min /
1461 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1463 // Increase boost for frames where new data coming into frame
1464 // (eg zoom out). Slightly reduce boost if there is a net balance
1465 // of motion out of the frame (zoom in).
1466 // The range for this_frame_mv_in_out is -1.0 to +1.0
1467 if (this_frame_mv_in_out > 0.0)
1468 r += r * (this_frame_mv_in_out * 2.0);
1469 // In extreme case boost is halved
1470 else
1471 r += r * (this_frame_mv_in_out / 2.0);
1473 if (r > GF_RMAX)
1474 r = GF_RMAX;
1476 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1478 // Cumulative effect of decay
1479 decay_accumulator = decay_accumulator * loop_decay_rate;
1480 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1482 boost_score += (decay_accumulator * r);
1484 // Break clause to detect very still sections after motion
1485 // For example a staic image after a fade or other transition.
1486 if ( detect_transition_to_still( cpi, i, 5,
1487 loop_decay_rate, decay_accumulator ) )
1489 allow_alt_ref = FALSE;
1490 boost_score = old_boost_score;
1491 break;
1494 // Break out conditions.
1495 if ( /* i>4 || */
1496 // Break at cpi->max_gf_interval unless almost totally static
1497 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1499 // Dont break out with a very short interval
1500 (i > MIN_GF_INTERVAL) &&
1501 // Dont break out very close to a key frame
1502 ((cpi->frames_to_key - i) >= MIN_GF_INTERVAL) &&
1503 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1504 ((mv_ratio_accumulator > 100.0) ||
1505 (abs_mv_in_out_accumulator > 3.0) ||
1506 (mv_in_out_accumulator < -2.0) ||
1507 ((boost_score - old_boost_score) < 2.0))
1510 boost_score = old_boost_score;
1511 break;
1514 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1516 old_boost_score = boost_score;
1519 cpi->gf_decay_rate =
1520 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1522 // When using CBR apply additional buffer related upper limits
1523 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1525 double max_boost;
1527 // For cbr apply buffer related limits
1528 if (cpi->drop_frames_allowed)
1530 int df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1531 (cpi->oxcf.optimal_buffer_level / 100);
1533 if (cpi->buffer_level > df_buffer_level)
1534 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1535 else
1536 max_boost = 0.0;
1538 else if (cpi->buffer_level > 0)
1540 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1542 else
1544 max_boost = 0.0;
1547 if (boost_score > max_boost)
1548 boost_score = max_boost;
1551 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1553 // Should we use the alternate refernce frame
1554 if (allow_alt_ref &&
1555 (i >= MIN_GF_INTERVAL) &&
1556 // dont use ARF very near next kf
1557 (i <= (cpi->frames_to_key - MIN_GF_INTERVAL)) &&
1558 (((next_frame.pcnt_inter > 0.75) &&
1559 ((mv_in_out_accumulator / (double)i > -0.2) || (mv_in_out_accumulator > -2.0)) &&
1560 //(cpi->gfu_boost>150) &&
1561 (cpi->gfu_boost > 100) &&
1562 //(cpi->gfu_boost>AF_THRESH2) &&
1563 //((cpi->gfu_boost/i)>AF_THRESH) &&
1564 //(decay_accumulator > 0.5) &&
1565 (cpi->gf_decay_rate <= (ARF_DECAY_THRESH + (cpi->gfu_boost / 200)))
1570 int Boost;
1571 int allocation_chunks;
1572 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1573 int tmp_q;
1574 int arf_frame_bits = 0;
1575 int group_bits;
1577 // Estimate the bits to be allocated to the group as a whole
1578 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1579 group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1580 else
1581 group_bits = 0;
1583 // Boost for arf frame
1584 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1585 Boost += (i * 50);
1586 allocation_chunks = (i * 100) + Boost;
1588 // Normalize Altboost and allocations chunck down to prevent overflow
1589 while (Boost > 1000)
1591 Boost /= 2;
1592 allocation_chunks /= 2;
1595 // Calculate the number of bits to be spent on the arf based on the boost number
1596 arf_frame_bits = (int)((double)Boost * (group_bits / (double)allocation_chunks));
1598 // Estimate if there are enough bits available to make worthwhile use of an arf.
1599 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
1601 // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames.
1602 if (tmp_q < cpi->worst_quality)
1604 int half_gf_int;
1605 int frames_after_arf;
1606 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1607 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1609 cpi->source_alt_ref_pending = TRUE;
1611 // For alt ref frames the error score for the end frame of the group (the alt ref frame) should not contribute to the group total and hence
1612 // the number of bit allocated to the group. Rather it forms part of the next group (it is the GF at the start of the next group)
1613 gf_group_err -= mod_frame_err;
1615 // Set the interval till the next gf or arf. For ARFs this is the number of frames to be coded before the future frame that is coded as an ARF.
1616 // The future frame itself is part of the next group
1617 cpi->baseline_gf_interval = i - 1;
1619 // Define the arnr filter width for this group of frames:
1620 // We only filter frames that lie within a distance of half
1621 // the GF interval from the ARF frame. We also have to trap
1622 // cases where the filter extends beyond the end of clip.
1623 // Note: this_frame->frame has been updated in the loop
1624 // so it now points at the ARF frame.
1625 half_gf_int = cpi->baseline_gf_interval >> 1;
1626 frames_after_arf = cpi->total_stats->count - this_frame->frame - 1;
1628 switch (cpi->oxcf.arnr_type)
1630 case 1: // Backward filter
1631 frames_fwd = 0;
1632 if (frames_bwd > half_gf_int)
1633 frames_bwd = half_gf_int;
1634 break;
1636 case 2: // Forward filter
1637 if (frames_fwd > half_gf_int)
1638 frames_fwd = half_gf_int;
1639 if (frames_fwd > frames_after_arf)
1640 frames_fwd = frames_after_arf;
1641 frames_bwd = 0;
1642 break;
1644 case 3: // Centered filter
1645 default:
1646 frames_fwd >>= 1;
1647 if (frames_fwd > frames_after_arf)
1648 frames_fwd = frames_after_arf;
1649 if (frames_fwd > half_gf_int)
1650 frames_fwd = half_gf_int;
1652 frames_bwd = frames_fwd;
1654 // For even length filter there is one more frame backward
1655 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1656 if (frames_bwd < half_gf_int)
1657 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
1658 break;
1661 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1663 else
1665 cpi->source_alt_ref_pending = FALSE;
1666 cpi->baseline_gf_interval = i;
1669 else
1671 cpi->source_alt_ref_pending = FALSE;
1672 cpi->baseline_gf_interval = i;
1675 // Conventional GF
1676 if (!cpi->source_alt_ref_pending)
1678 // Dont allow conventional gf too near the next kf
1679 if ((cpi->frames_to_key - cpi->baseline_gf_interval) < MIN_GF_INTERVAL)
1681 while (cpi->baseline_gf_interval < cpi->frames_to_key)
1683 if (EOF == input_stats(cpi, this_frame))
1684 break;
1686 cpi->baseline_gf_interval++;
1688 if (cpi->baseline_gf_interval < cpi->frames_to_key)
1689 gf_group_err += calculate_modified_err(cpi, this_frame);
1694 // Now decide how many bits should be allocated to the GF group as a proportion of those remaining in the kf group.
1695 // The final key frame group in the clip is treated as a special case where cpi->kf_group_bits is tied to cpi->bits_left.
1696 // This is also important for short clips where there may only be one key frame.
1697 if (cpi->frames_to_key >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
1699 cpi->kf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;
1702 // Calculate the bits to be allocated to the group as a whole
1703 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1704 cpi->gf_group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1705 else
1706 cpi->gf_group_bits = 0;
1708 cpi->gf_group_bits = (cpi->gf_group_bits < 0) ? 0 : (cpi->gf_group_bits > cpi->kf_group_bits) ? cpi->kf_group_bits : cpi->gf_group_bits;
1710 // Clip cpi->gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section)
1711 if (cpi->gf_group_bits > max_bits * cpi->baseline_gf_interval)
1712 cpi->gf_group_bits = max_bits * cpi->baseline_gf_interval;
1714 // Reset the file position
1715 reset_fpf_position(cpi, start_pos);
1717 // Update the record of error used so far (only done once per gf group)
1718 cpi->modified_error_used += gf_group_err;
1720 // Assign bits to the arf or gf.
1722 int Boost;
1723 int frames_in_section;
1724 int allocation_chunks;
1725 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1727 // For ARF frames
1728 if (cpi->source_alt_ref_pending)
1730 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1731 //Boost += (cpi->baseline_gf_interval * 25);
1732 Boost += (cpi->baseline_gf_interval * 50);
1734 // Set max and minimum boost and hence minimum allocation
1735 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1736 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1737 else if (Boost < 125)
1738 Boost = 125;
1740 frames_in_section = cpi->baseline_gf_interval + 1;
1741 allocation_chunks = (frames_in_section * 100) + Boost;
1743 // Else for standard golden frames
1744 else
1746 // boost based on inter / intra ratio of subsequent frames
1747 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
1749 // Set max and minimum boost and hence minimum allocation
1750 if (Boost > (cpi->baseline_gf_interval * 150))
1751 Boost = (cpi->baseline_gf_interval * 150);
1752 else if (Boost < 125)
1753 Boost = 125;
1755 frames_in_section = cpi->baseline_gf_interval;
1756 allocation_chunks = (frames_in_section * 100) + (Boost - 100);
1759 // Normalize Altboost and allocations chunck down to prevent overflow
1760 while (Boost > 1000)
1762 Boost /= 2;
1763 allocation_chunks /= 2;
1766 // Calculate the number of bits to be spent on the gf or arf based on the boost number
1767 cpi->gf_bits = (int)((double)Boost * (cpi->gf_group_bits / (double)allocation_chunks));
1769 // If the frame that is to be boosted is simpler than the average for
1770 // the gf/arf group then use an alternative calculation
1771 // based on the error score of the frame itself
1772 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
1774 double alt_gf_grp_bits;
1775 int alt_gf_bits;
1777 alt_gf_grp_bits =
1778 (double)cpi->kf_group_bits *
1779 (mod_frame_err * (double)cpi->baseline_gf_interval) /
1780 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left);
1782 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
1783 (double)allocation_chunks));
1785 if (cpi->gf_bits > alt_gf_bits)
1787 cpi->gf_bits = alt_gf_bits;
1790 // Else if it is harder than other frames in the group make sure it at
1791 // least receives an allocation in keeping with its relative error
1792 // score, otherwise it may be worse off than an "un-boosted" frame
1793 else
1795 int alt_gf_bits =
1796 (int)((double)cpi->kf_group_bits *
1797 mod_frame_err /
1798 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left));
1800 if (alt_gf_bits > cpi->gf_bits)
1802 cpi->gf_bits = alt_gf_bits;
1806 // Apply an additional limit for CBR
1807 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1809 if (cpi->gf_bits > (cpi->buffer_level >> 1))
1810 cpi->gf_bits = cpi->buffer_level >> 1;
1813 // Dont allow a negative value for gf_bits
1814 if (cpi->gf_bits < 0)
1815 cpi->gf_bits = 0;
1817 // Adjust KF group bits and error remainin
1818 cpi->kf_group_error_left -= gf_group_err;
1819 cpi->kf_group_bits -= cpi->gf_group_bits;
1821 if (cpi->kf_group_bits < 0)
1822 cpi->kf_group_bits = 0;
1824 // Note the error score left in the remaining frames of the group.
1825 // 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)
1826 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
1827 cpi->gf_group_error_left = gf_group_err - gf_first_frame_err;
1828 else
1829 cpi->gf_group_error_left = gf_group_err;
1831 cpi->gf_group_bits -= cpi->gf_bits;
1833 if (cpi->gf_group_bits < 0)
1834 cpi->gf_group_bits = 0;
1836 // Set aside some bits for a mid gf sequence boost
1837 if ((cpi->gfu_boost > 150) && (cpi->baseline_gf_interval > 5))
1839 int pct_extra = (cpi->gfu_boost - 100) / 50;
1840 pct_extra = (pct_extra > 10) ? 10 : pct_extra;
1842 cpi->mid_gf_extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
1843 cpi->gf_group_bits -= cpi->mid_gf_extra_bits;
1845 else
1846 cpi->mid_gf_extra_bits = 0;
1848 cpi->gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame
1851 if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) // Normal GF and not a KF
1853 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for this frame
1856 // Adjustment to estimate_max_q based on a measure of complexity of the section
1857 if (cpi->common.frame_type != KEY_FRAME)
1859 FIRSTPASS_STATS sectionstats;
1860 double Ratio;
1862 zero_stats(&sectionstats);
1863 reset_fpf_position(cpi, start_pos);
1865 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
1867 input_stats(cpi, &next_frame);
1868 accumulate_stats(&sectionstats, &next_frame);
1871 avg_stats(&sectionstats);
1873 cpi->section_intra_rating =
1874 sectionstats.intra_error /
1875 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
1877 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
1878 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
1880 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
1882 if (cpi->section_max_qfactor < 0.80)
1883 cpi->section_max_qfactor = 0.80;
1886 //else
1887 // cpi->section_max_qfactor = 1.0;
1889 reset_fpf_position(cpi, start_pos);
1893 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1894 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1896 int target_frame_size; // gf_group_error_left
1898 double modified_err;
1899 double err_fraction; // What portion of the remaining GF group error is used by this frame
1901 int max_bits = frame_max_bits(cpi); // Max for a single frame
1903 // The final few frames have special treatment
1904 if (cpi->frames_till_gf_update_due >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
1906 cpi->gf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;;
1909 // Calculate modified prediction error used in bit allocation
1910 modified_err = calculate_modified_err(cpi, this_frame);
1912 if (cpi->gf_group_error_left > 0)
1913 err_fraction = modified_err / cpi->gf_group_error_left; // What portion of the remaining GF group error is used by this frame
1914 else
1915 err_fraction = 0.0;
1917 target_frame_size = (int)((double)cpi->gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it?
1919 // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top end.
1920 if (target_frame_size < 0)
1921 target_frame_size = 0;
1922 else
1924 if (target_frame_size > max_bits)
1925 target_frame_size = max_bits;
1927 if (target_frame_size > cpi->gf_group_bits)
1928 target_frame_size = cpi->gf_group_bits;
1931 cpi->gf_group_error_left -= modified_err; // Adjust error remaining
1932 cpi->gf_group_bits -= target_frame_size; // Adjust bits remaining
1934 if (cpi->gf_group_bits < 0)
1935 cpi->gf_group_bits = 0;
1937 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame.
1939 // Special case for the frame that lies half way between two gfs
1940 if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2)
1941 target_frame_size += cpi->mid_gf_extra_bits;
1943 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
1946 void vp8_second_pass(VP8_COMP *cpi)
1948 int tmp_q;
1949 int frames_left = (int)(cpi->total_stats->count - cpi->common.current_video_frame);
1951 FIRSTPASS_STATS this_frame;
1952 FIRSTPASS_STATS this_frame_copy;
1954 double this_frame_error;
1955 double this_frame_intra_error;
1956 double this_frame_coded_error;
1958 FIRSTPASS_STATS *start_pos;
1960 if (!cpi->stats_in)
1962 return ;
1965 vp8_clear_system_state();
1967 if (EOF == input_stats(cpi, &this_frame))
1968 return;
1970 this_frame_error = this_frame.ssim_weighted_pred_err;
1971 this_frame_intra_error = this_frame.intra_error;
1972 this_frame_coded_error = this_frame.coded_error;
1974 // Store information regarding level of motion etc for use mode decisions.
1975 cpi->motion_speed = (int)(fabs(this_frame.MVr) + fabs(this_frame.MVc));
1976 cpi->motion_var = (int)(fabs(this_frame.MVrv) + fabs(this_frame.MVcv));
1977 cpi->inter_lvl = (int)(this_frame.pcnt_inter * 100);
1978 cpi->intra_lvl = (int)((1.0 - this_frame.pcnt_inter) * 100);
1979 cpi->motion_lvl = (int)(this_frame.pcnt_motion * 100);
1981 start_pos = cpi->stats_in;
1983 // keyframe and section processing !
1984 if (cpi->frames_to_key == 0)
1986 // Define next KF group and assign bits to it
1987 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
1988 find_next_key_frame(cpi, &this_frame_copy);
1990 // 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
1991 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
1992 // This is temporary code till we decide what should really happen in this case.
1993 if (cpi->oxcf.error_resilient_mode)
1995 cpi->gf_group_bits = cpi->kf_group_bits;
1996 cpi->gf_group_error_left = cpi->kf_group_error_left;
1997 cpi->baseline_gf_interval = cpi->frames_to_key;
1998 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
1999 cpi->source_alt_ref_pending = FALSE;
2004 // Is this a GF / ARF (Note that a KF is always also a GF)
2005 if (cpi->frames_till_gf_update_due == 0)
2007 // Update monitor of the bits per error observed so far.
2008 // Done once per gf group based on what has gone before
2009 // so do nothing if this is the first frame.
2010 if (cpi->common.current_video_frame > 0)
2012 cpi->observed_bpe =
2013 (double)(cpi->clip_bits_total - cpi->bits_left) /
2014 cpi->modified_error_used;
2017 // Define next gf group and assign bits to it
2018 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2019 define_gf_group(cpi, &this_frame_copy);
2021 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
2022 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
2023 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
2024 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2026 // Assign a standard frames worth of bits from those allocated to the GF group
2027 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2028 assign_std_frame_bits(cpi, &this_frame_copy);
2030 // If appropriate (we are switching into ARF active but it was not previously active) apply a boost for the gf at the start of the group.
2031 //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
2032 if (FALSE)
2034 int extra_bits;
2035 int pct_extra = (cpi->gfu_boost - 100) / 50;
2037 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2039 extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
2040 cpi->gf_group_bits -= extra_bits;
2041 cpi->per_frame_bandwidth += extra_bits;
2046 // Otherwise this is an ordinary frame
2047 else
2049 // 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
2050 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2051 // This is temporary code till we decide what should really happen in this case.
2052 if (cpi->oxcf.error_resilient_mode)
2054 cpi->frames_till_gf_update_due = cpi->frames_to_key;
2056 if (cpi->common.frame_type != KEY_FRAME)
2058 // Assign bits from those allocated to the GF group
2059 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2060 assign_std_frame_bits(cpi, &this_frame_copy);
2063 else
2065 // Assign bits from those allocated to the GF group
2066 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2067 assign_std_frame_bits(cpi, &this_frame_copy);
2071 // Keep a globally available copy of this and the next frame's iiratio.
2072 cpi->this_iiratio = this_frame_intra_error /
2073 DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
2075 FIRSTPASS_STATS next_frame;
2076 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2078 cpi->next_iiratio = next_frame.intra_error /
2079 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
2083 // Set nominal per second bandwidth for this frame
2084 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
2085 if (cpi->target_bandwidth < 0)
2086 cpi->target_bandwidth = 0;
2088 if (cpi->common.current_video_frame == 0)
2090 cpi->est_max_qcorrection_factor = 1.0;
2092 // Experimental code to try and set a cq_level in constrained
2093 // quality mode.
2094 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2096 int est_cq;
2098 est_cq =
2099 estimate_cq( cpi,
2100 (cpi->total_coded_error_left / frames_left),
2101 (int)(cpi->bits_left / frames_left));
2103 cpi->cq_target_quality = cpi->oxcf.cq_level;
2104 if ( est_cq > cpi->cq_target_quality )
2105 cpi->cq_target_quality = est_cq;
2108 // guess at maxq needed in 2nd pass
2109 cpi->maxq_max_limit = cpi->worst_quality;
2110 cpi->maxq_min_limit = cpi->best_quality;
2111 tmp_q = estimate_max_q( cpi,
2112 (cpi->total_coded_error_left / frames_left),
2113 (int)(cpi->bits_left / frames_left));
2115 // Limit the maxq value returned subsequently.
2116 // This increases the risk of overspend or underspend if the initial
2117 // estimate for the clip is bad, but helps prevent excessive
2118 // variation in Q, especially near the end of a clip
2119 // where for example a small overspend may cause Q to crash
2120 cpi->maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2121 ? (tmp_q + 32) : cpi->worst_quality;
2122 cpi->maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2123 ? (tmp_q - 32) : cpi->best_quality;
2125 cpi->active_worst_quality = tmp_q;
2126 cpi->ni_av_qi = tmp_q;
2129 // The last few frames of a clip almost always have to few or too many
2130 // bits and for the sake of over exact rate control we dont want to make
2131 // radical adjustments to the allowed quantizer range just to use up a
2132 // few surplus bits or get beneath the target rate.
2133 else if ( (cpi->common.current_video_frame <
2134 (((unsigned int)cpi->total_stats->count * 255)>>8)) &&
2135 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2136 (unsigned int)cpi->total_stats->count) )
2138 if (frames_left < 1)
2139 frames_left = 1;
2141 tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left));
2143 // Move active_worst_quality but in a damped way
2144 if (tmp_q > cpi->active_worst_quality)
2145 cpi->active_worst_quality ++;
2146 else if (tmp_q < cpi->active_worst_quality)
2147 cpi->active_worst_quality --;
2149 cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2152 cpi->frames_to_key --;
2153 cpi->total_error_left -= this_frame_error;
2154 cpi->total_intra_error_left -= this_frame_intra_error;
2155 cpi->total_coded_error_left -= this_frame_coded_error;
2159 static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2161 BOOL is_viable_kf = FALSE;
2163 // Does the frame satisfy the primary criteria of a key frame
2164 // If so, then examine how well it predicts subsequent frames
2165 if ((this_frame->pcnt_second_ref < 0.10) &&
2166 (next_frame->pcnt_second_ref < 0.10) &&
2167 ((this_frame->pcnt_inter < 0.05) ||
2169 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .25) &&
2170 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2171 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2172 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2173 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2179 int i;
2180 FIRSTPASS_STATS *start_pos;
2182 FIRSTPASS_STATS local_next_frame;
2184 double boost_score = 0.0;
2185 double old_boost_score = 0.0;
2186 double decay_accumulator = 1.0;
2187 double next_iiratio;
2189 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2191 // Note the starting file position so we can reset to it
2192 start_pos = cpi->stats_in;
2194 // Examine how well the key frame predicts subsequent frames
2195 for (i = 0 ; i < 16; i++)
2197 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
2199 if (next_iiratio > RMAX)
2200 next_iiratio = RMAX;
2202 // Cumulative effect of decay in prediction quality
2203 if (local_next_frame.pcnt_inter > 0.85)
2204 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2205 else
2206 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2208 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2210 // Keep a running total
2211 boost_score += (decay_accumulator * next_iiratio);
2213 // Test various breakout clauses
2214 if ((local_next_frame.pcnt_inter < 0.05) ||
2215 (next_iiratio < 1.5) ||
2216 (((local_next_frame.pcnt_inter -
2217 local_next_frame.pcnt_neutral) < 0.20) &&
2218 (next_iiratio < 3.0)) ||
2219 ((boost_score - old_boost_score) < 0.5) ||
2220 (local_next_frame.intra_error < 200)
2223 break;
2226 old_boost_score = boost_score;
2228 // Get the next frame details
2229 if (EOF == input_stats(cpi, &local_next_frame))
2230 break;
2233 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2234 if (boost_score > 5.0 && (i > 3))
2235 is_viable_kf = TRUE;
2236 else
2238 // Reset the file position
2239 reset_fpf_position(cpi, start_pos);
2241 is_viable_kf = FALSE;
2245 return is_viable_kf;
2247 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2249 int i,j;
2250 FIRSTPASS_STATS last_frame;
2251 FIRSTPASS_STATS first_frame;
2252 FIRSTPASS_STATS next_frame;
2253 FIRSTPASS_STATS *start_position;
2255 double decay_accumulator = 1.0;
2256 double boost_score = 0;
2257 double old_boost_score = 0.0;
2258 double loop_decay_rate;
2260 double kf_mod_err = 0.0;
2261 double kf_group_err = 0.0;
2262 double kf_group_intra_err = 0.0;
2263 double kf_group_coded_err = 0.0;
2264 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2266 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2268 vp8_clear_system_state(); //__asm emms;
2269 start_position = cpi->stats_in;
2271 cpi->common.frame_type = KEY_FRAME;
2273 // is this a forced key frame by interval
2274 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2276 // Clear the alt ref active flag as this can never be active on a key frame
2277 cpi->source_alt_ref_active = FALSE;
2279 // Kf is always a gf so clear frames till next gf counter
2280 cpi->frames_till_gf_update_due = 0;
2282 cpi->frames_to_key = 1;
2284 // Take a copy of the initial frame details
2285 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2287 cpi->kf_group_bits = 0; // Total bits avaialable to kf group
2288 cpi->kf_group_error_left = 0; // Group modified error score.
2290 kf_mod_err = calculate_modified_err(cpi, this_frame);
2292 // find the next keyframe
2293 i = 0;
2294 while (cpi->stats_in < cpi->stats_in_end)
2296 // Accumulate kf group error
2297 kf_group_err += calculate_modified_err(cpi, this_frame);
2299 // These figures keep intra and coded error counts for all frames including key frames in the group.
2300 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2301 kf_group_intra_err += this_frame->intra_error;
2302 kf_group_coded_err += this_frame->coded_error;
2304 // load a the next frame's stats
2305 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2306 input_stats(cpi, this_frame);
2308 // Provided that we are not at the end of the file...
2309 if (cpi->oxcf.auto_key
2310 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2312 // Normal scene cut check
2313 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
2314 break;
2316 // How fast is prediction quality decaying
2317 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2319 // We want to know something about the recent past... rather than
2320 // as used elsewhere where we are concened with decay in prediction
2321 // quality since the last GF or KF.
2322 recent_loop_decay[i%8] = loop_decay_rate;
2323 decay_accumulator = 1.0;
2324 for (j = 0; j < 8; j++)
2326 decay_accumulator = decay_accumulator * recent_loop_decay[j];
2329 // Special check for transition or high motion followed by a
2330 // to a static scene.
2331 if ( detect_transition_to_still( cpi, i,
2332 (cpi->key_frame_frequency-i),
2333 loop_decay_rate,
2334 decay_accumulator ) )
2336 break;
2340 // Step on to the next frame
2341 cpi->frames_to_key ++;
2343 // If we don't have a real key frame within the next two
2344 // forcekeyframeevery intervals then break out of the loop.
2345 if (cpi->frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2346 break;
2347 } else
2348 cpi->frames_to_key ++;
2350 i++;
2353 // If there is a max kf interval set by the user we must obey it.
2354 // We already breakout of the loop above at 2x max.
2355 // This code centers the extra kf if the actual natural
2356 // interval is between 1x and 2x
2357 if (cpi->oxcf.auto_key
2358 && cpi->frames_to_key > (int)cpi->key_frame_frequency )
2360 FIRSTPASS_STATS *current_pos = cpi->stats_in;
2361 FIRSTPASS_STATS tmp_frame;
2363 cpi->frames_to_key /= 2;
2365 // Copy first frame details
2366 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2368 // Reset to the start of the group
2369 reset_fpf_position(cpi, start_position);
2371 kf_group_err = 0;
2372 kf_group_intra_err = 0;
2373 kf_group_coded_err = 0;
2375 // Rescan to get the correct error data for the forced kf group
2376 for( i = 0; i < cpi->frames_to_key; i++ )
2378 // Accumulate kf group errors
2379 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2380 kf_group_intra_err += tmp_frame.intra_error;
2381 kf_group_coded_err += tmp_frame.coded_error;
2383 // Load a the next frame's stats
2384 input_stats(cpi, &tmp_frame);
2387 // Reset to the start of the group
2388 reset_fpf_position(cpi, current_pos);
2390 cpi->next_key_frame_forced = TRUE;
2392 else
2393 cpi->next_key_frame_forced = FALSE;
2395 // Special case for the last frame of the file
2396 if (cpi->stats_in >= cpi->stats_in_end)
2398 // Accumulate kf group error
2399 kf_group_err += calculate_modified_err(cpi, this_frame);
2401 // These figures keep intra and coded error counts for all frames including key frames in the group.
2402 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2403 kf_group_intra_err += this_frame->intra_error;
2404 kf_group_coded_err += this_frame->coded_error;
2407 // Calculate the number of bits that should be assigned to the kf group.
2408 if ((cpi->bits_left > 0) && (cpi->modified_error_left > 0.0))
2410 // Max for a single normal frame (not key frame)
2411 int max_bits = frame_max_bits(cpi);
2413 // Maximum bits for the kf group
2414 long long max_grp_bits;
2416 // Default allocation based on bits left and relative
2417 // complexity of the section
2418 cpi->kf_group_bits = (long long)( cpi->bits_left *
2419 ( kf_group_err /
2420 cpi->modified_error_left ));
2422 // Clip based on maximum per frame rate defined by the user.
2423 max_grp_bits = (long long)max_bits * (long long)cpi->frames_to_key;
2424 if (cpi->kf_group_bits > max_grp_bits)
2425 cpi->kf_group_bits = max_grp_bits;
2427 // Additional special case for CBR if buffer is getting full.
2428 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2430 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2431 int buffer_lvl = cpi->buffer_level;
2433 // If the buffer is near or above the optimal and this kf group is
2434 // not being allocated much then increase the allocation a bit.
2435 if (buffer_lvl >= opt_buffer_lvl)
2437 int high_water_mark = (opt_buffer_lvl +
2438 cpi->oxcf.maximum_buffer_size) >> 1;
2440 long long av_group_bits;
2442 // Av bits per frame * number of frames
2443 av_group_bits = (long long)cpi->av_per_frame_bandwidth *
2444 (long long)cpi->frames_to_key;
2446 // We are at or above the maximum.
2447 if (cpi->buffer_level >= high_water_mark)
2449 long long min_group_bits;
2451 min_group_bits = av_group_bits +
2452 (long long)(buffer_lvl -
2453 high_water_mark);
2455 if (cpi->kf_group_bits < min_group_bits)
2456 cpi->kf_group_bits = min_group_bits;
2458 // We are above optimal but below the maximum
2459 else if (cpi->kf_group_bits < av_group_bits)
2461 long long bits_below_av = av_group_bits -
2462 cpi->kf_group_bits;
2464 cpi->kf_group_bits +=
2465 (long long)((double)bits_below_av *
2466 (double)(buffer_lvl - opt_buffer_lvl) /
2467 (double)(high_water_mark - opt_buffer_lvl));
2472 else
2473 cpi->kf_group_bits = 0;
2475 // Reset the first pass file position
2476 reset_fpf_position(cpi, start_position);
2478 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2479 decay_accumulator = 1.0;
2480 boost_score = 0.0;
2481 loop_decay_rate = 1.00; // Starting decay rate
2483 for (i = 0 ; i < cpi->frames_to_key ; i++)
2485 double r;
2487 if (EOF == input_stats(cpi, &next_frame))
2488 break;
2490 if (next_frame.intra_error > cpi->kf_intra_err_min)
2491 r = (IIKFACTOR2 * next_frame.intra_error /
2492 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2493 else
2494 r = (IIKFACTOR2 * cpi->kf_intra_err_min /
2495 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2497 if (r > RMAX)
2498 r = RMAX;
2500 // How fast is prediction quality decaying
2501 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2503 decay_accumulator = decay_accumulator * loop_decay_rate;
2504 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2506 boost_score += (decay_accumulator * r);
2508 if ((i > MIN_GF_INTERVAL) &&
2509 ((boost_score - old_boost_score) < 1.0))
2511 break;
2514 old_boost_score = boost_score;
2517 if (1)
2519 FIRSTPASS_STATS sectionstats;
2520 double Ratio;
2522 zero_stats(&sectionstats);
2523 reset_fpf_position(cpi, start_position);
2525 for (i = 0 ; i < cpi->frames_to_key ; i++)
2527 input_stats(cpi, &next_frame);
2528 accumulate_stats(&sectionstats, &next_frame);
2531 avg_stats(&sectionstats);
2533 cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2535 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2536 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2538 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2540 if (cpi->section_max_qfactor < 0.80)
2541 cpi->section_max_qfactor = 0.80;
2544 //else
2545 // cpi->section_max_qfactor = 1.0;
2548 // When using CBR apply additional buffer fullness related upper limits
2549 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2551 double max_boost;
2553 if (cpi->drop_frames_allowed)
2555 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
2557 if (cpi->buffer_level > df_buffer_level)
2558 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2559 else
2560 max_boost = 0.0;
2562 else if (cpi->buffer_level > 0)
2564 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2566 else
2568 max_boost = 0.0;
2571 if (boost_score > max_boost)
2572 boost_score = max_boost;
2575 // Reset the first pass file position
2576 reset_fpf_position(cpi, start_position);
2578 // Work out how many bits to allocate for the key frame itself
2579 if (1)
2581 int kf_boost = boost_score;
2582 int allocation_chunks;
2583 int Counter = cpi->frames_to_key;
2584 int alt_kf_bits;
2585 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2586 // Min boost based on kf interval
2587 #if 0
2589 while ((kf_boost < 48) && (Counter > 0))
2591 Counter -= 2;
2592 kf_boost ++;
2595 #endif
2597 if (kf_boost < 48)
2599 kf_boost += ((Counter + 1) >> 1);
2601 if (kf_boost > 48) kf_boost = 48;
2604 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2605 if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
2606 kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2607 else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
2608 kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2610 kf_boost = (int)((double)kf_boost * 100.0) >> 4; // Scale 16 to 100
2612 // Adjustment to boost based on recent average q
2613 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2615 if (kf_boost < 250) // Min KF boost
2616 kf_boost = 250;
2618 // We do three calculations for kf size.
2619 // The first is based on the error score for the whole kf group.
2620 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2621 // 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
2623 allocation_chunks = ((cpi->frames_to_key - 1) * 100) + kf_boost; // cpi->frames_to_key-1 because key frame itself is taken care of by kf_boost
2625 // Normalize Altboost and allocations chunck down to prevent overflow
2626 while (kf_boost > 1000)
2628 kf_boost /= 2;
2629 allocation_chunks /= 2;
2632 cpi->kf_group_bits = (cpi->kf_group_bits < 0) ? 0 : cpi->kf_group_bits;
2634 // Calculate the number of bits to be spent on the key frame
2635 cpi->kf_bits = (int)((double)kf_boost * ((double)cpi->kf_group_bits / (double)allocation_chunks));
2637 // Apply an additional limit for CBR
2638 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2640 if (cpi->kf_bits > ((3 * cpi->buffer_level) >> 2))
2641 cpi->kf_bits = (3 * cpi->buffer_level) >> 2;
2644 // If the key frame is actually easier than the average for the
2645 // kf group (which does sometimes happen... eg a blank intro frame)
2646 // Then use an alternate calculation based on the kf error score
2647 // which should give a smaller key frame.
2648 if (kf_mod_err < kf_group_err / cpi->frames_to_key)
2650 double alt_kf_grp_bits =
2651 ((double)cpi->bits_left *
2652 (kf_mod_err * (double)cpi->frames_to_key) /
2653 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left));
2655 alt_kf_bits = (int)((double)kf_boost *
2656 (alt_kf_grp_bits / (double)allocation_chunks));
2658 if (cpi->kf_bits > alt_kf_bits)
2660 cpi->kf_bits = alt_kf_bits;
2663 // Else if it is much harder than other frames in the group make sure
2664 // it at least receives an allocation in keeping with its relative
2665 // error score
2666 else
2668 alt_kf_bits =
2669 (int)((double)cpi->bits_left *
2670 (kf_mod_err /
2671 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left)));
2673 if (alt_kf_bits > cpi->kf_bits)
2675 cpi->kf_bits = alt_kf_bits;
2679 cpi->kf_group_bits -= cpi->kf_bits;
2680 cpi->kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance
2682 cpi->per_frame_bandwidth = cpi->kf_bits; // Peer frame bit target for this frame
2683 cpi->target_bandwidth = cpi->kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate
2686 // Note the total error score of the kf group minus the key frame itself
2687 cpi->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2689 // Adjust the count of total modified error left.
2690 // The count of bits left is adjusted elsewhere based on real coded frame sizes
2691 cpi->modified_error_left -= kf_group_err;
2693 if (cpi->oxcf.allow_spatial_resampling)
2695 int resample_trigger = FALSE;
2696 int last_kf_resampled = FALSE;
2697 int kf_q;
2698 int scale_val = 0;
2699 int hr, hs, vr, vs;
2700 int new_width = cpi->oxcf.Width;
2701 int new_height = cpi->oxcf.Height;
2703 int projected_buffer_level = cpi->buffer_level;
2704 int tmp_q;
2706 double projected_bits_perframe;
2707 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
2708 double err_per_frame = kf_group_err / cpi->frames_to_key;
2709 double bits_per_frame;
2710 double av_bits_per_frame;
2711 double effective_size_ratio;
2713 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
2714 last_kf_resampled = TRUE;
2716 // Set back to unscaled by defaults
2717 cpi->common.horiz_scale = NORMAL;
2718 cpi->common.vert_scale = NORMAL;
2720 // Calculate Average bits per frame.
2721 //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats->count - cpi->common.current_video_frame);
2722 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate);
2723 //if ( av_bits_per_frame < 0.0 )
2724 // av_bits_per_frame = 0.0
2726 // CBR... Use the clip average as the target for deciding resample
2727 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2729 bits_per_frame = av_bits_per_frame;
2732 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
2733 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
2734 else
2736 bits_per_frame = cpi->kf_group_bits / cpi->frames_to_key; // This accounts for how hard the section is...
2738 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
2739 bits_per_frame = av_bits_per_frame;
2742 // bits_per_frame should comply with our minimum
2743 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
2744 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2746 // Work out if spatial resampling is necessary
2747 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, group_iiratio);
2749 // 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
2750 projected_bits_perframe = bits_per_frame;
2751 tmp_q = kf_q;
2753 while (tmp_q > cpi->worst_quality)
2755 projected_bits_perframe *= 1.04;
2756 tmp_q--;
2759 // Guess at buffer level at the end of the section
2760 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->frames_to_key);
2762 if (0)
2764 FILE *f = fopen("Subsamle.stt", "a");
2765 fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi->common.current_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->frames_to_key, (int)(cpi->kf_group_bits / cpi->frames_to_key), new_height, new_width);
2766 fclose(f);
2769 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
2770 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2772 // Trigger resample if we are projected to fall below down sample level or
2773 // resampled last time and are projected to remain below the up sample level
2774 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
2775 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
2776 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
2777 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
2778 resample_trigger = TRUE;
2779 else
2780 resample_trigger = FALSE;
2782 else
2784 long long clip_bits = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
2785 long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
2787 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced
2788 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ...
2789 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits
2790 resample_trigger = TRUE;
2791 else
2792 resample_trigger = FALSE;
2796 if (resample_trigger)
2798 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
2800 scale_val ++;
2802 cpi->common.vert_scale = vscale_lookup[scale_val];
2803 cpi->common.horiz_scale = hscale_lookup[scale_val];
2805 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
2806 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
2808 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2809 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2811 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
2812 // effective_sizeratio attempts to provide a crude correction for this
2813 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
2814 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
2816 // Now try again and see what Q we get with the smaller image size
2817 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, group_iiratio);
2819 if (0)
2821 FILE *f = fopen("Subsamle.stt", "a");
2822 fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->frames_to_key, (int)(cpi->kf_group_bits / cpi->frames_to_key), new_height, new_width);
2823 fclose(f);
2828 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
2830 cpi->common.Width = new_width;
2831 cpi->common.Height = new_height;
2832 vp8_alloc_compressor_data(cpi);