Removed unused vp8_recon_intra4x4mb function
[libvpx.git] / vp8 / encoder / firstpass.c
blobfc6f043c3118a9b883fdd613bf4c48283f37572c
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 "setupintrarecon.h"
18 #include "mcomp.h"
19 #include "vpx_scale/vpxscale.h"
20 #include "encodemb.h"
21 #include "extend.h"
22 #include "systemdependent.h"
23 #include "vpx_scale/yv12extend.h"
24 #include "vpx_mem/vpx_mem.h"
25 #include "swapyv12buffer.h"
26 #include <stdio.h>
27 #include "rdopt.h"
28 #include "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 const int cq_level[QINDEX_RANGE] =
72 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
73 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
74 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
75 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
76 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
77 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
78 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
79 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
82 void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
83 int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps);
85 int vp8_encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred)
88 int i;
89 int intra_pred_var = 0;
90 (void) cpi;
92 if (use_dc_pred)
94 x->e_mbd.mode_info_context->mbmi.mode = DC_PRED;
95 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
96 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
98 vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
100 else
102 for (i = 0; i < 16; i++)
104 BLOCKD *b = &x->e_mbd.block[i];
105 BLOCK *be = &x->block[i];
107 vp8_encode_intra4x4block(IF_RTCD(&cpi->rtcd), x, be, b, B_DC_PRED);
111 intra_pred_var = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff);
113 return intra_pred_var;
116 // Resets the first pass file to the given position using a relative seek from the current position
117 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
119 cpi->stats_in = Position;
122 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
124 if (cpi->stats_in >= cpi->stats_in_end)
125 return EOF;
127 *next_frame = *cpi->stats_in;
128 return 1;
131 // Calculate a modified Error used in distributing bits between easier and harder frames
132 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
134 double av_err = cpi->total_stats->ssim_weighted_pred_err;
135 double this_err = this_frame->ssim_weighted_pred_err;
136 double modified_err;
138 //double relative_next_iiratio;
139 //double next_iiratio;
140 //double sum_iiratio;
141 //int i;
143 //FIRSTPASS_STATS next_frame;
144 //FIRSTPASS_STATS *start_pos;
146 /*start_pos = cpi->stats_in;
147 sum_iiratio = 0.0;
148 i = 0;
149 while ( (i < 1) && vp8_input_stats(cpi,&next_frame) != EOF )
152 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
153 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio;
154 sum_iiratio += next_iiratio;
155 i++;
157 if ( i > 0 )
159 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->avg_iiratio * (double)i);
161 else
163 relative_next_iiratio = 1.0;
165 reset_fpf_position(cpi, start_pos);*/
167 if (this_err > av_err)
168 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
169 else
170 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
173 relative_next_iiratio = pow(relative_next_iiratio,0.25);
174 modified_err = modified_err * relative_next_iiratio;
177 return modified_err;
180 static const double weight_table[256] = {
181 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
182 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
183 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
184 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
185 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
186 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
187 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
188 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
189 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
190 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
191 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
192 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
193 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
194 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
195 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
196 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
197 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
198 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
199 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
200 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
201 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
202 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
203 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
204 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
205 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
206 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
207 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
208 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
209 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
210 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
211 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
212 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
215 double vp8_simple_weight(YV12_BUFFER_CONFIG *source)
217 int i, j;
219 unsigned char *src = source->y_buffer;
220 double sum_weights = 0.0;
222 // Loop throught the Y plane raw examining levels and creating a weight for the image
223 i = source->y_height;
226 j = source->y_width;
229 sum_weights += weight_table[ *src];
230 src++;
231 }while(--j);
232 src -= source->y_width;
233 src += source->y_stride;
234 }while(--i);
236 sum_weights /= (source->y_height * source->y_width);
238 return sum_weights;
242 // This function returns the current per frame maximum bitrate target
243 int frame_max_bits(VP8_COMP *cpi)
245 // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
246 int max_bits;
248 // For CBR we need to also consider buffer fullness.
249 // If we are running below the optimal level then we need to gradually tighten up on max_bits.
250 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
252 double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
254 // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
255 max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
257 // If our buffer is below the optimum level
258 if (buffer_fullness_ratio < 1.0)
260 // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
261 int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
263 max_bits = (int)(max_bits * buffer_fullness_ratio);
265 if (max_bits < min_max_bits)
266 max_bits = min_max_bits; // Lowest value we will set ... which should allow the buffer to refil.
269 // VBR
270 else
272 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
273 max_bits = (int)(((double)cpi->bits_left / (cpi->total_stats->count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
276 // Trap case where we are out of bits
277 if (max_bits < 0)
278 max_bits = 0;
280 return max_bits;
284 extern size_t vp8_firstpass_stats_sz(unsigned int mb_count)
286 /* Calculate the size of a stats packet, which is dependent on the frame
287 * resolution. The FIRSTPASS_STATS struct has a single element array,
288 * motion_map, which is virtually expanded to have one element per
289 * macroblock.
291 size_t stats_sz;
293 stats_sz = sizeof(FIRSTPASS_STATS) + mb_count;
294 stats_sz = (stats_sz + 7) & ~7;
295 return stats_sz;
299 void vp8_output_stats(const VP8_COMP *cpi,
300 struct vpx_codec_pkt_list *pktlist,
301 FIRSTPASS_STATS *stats)
303 struct vpx_codec_cx_pkt pkt;
304 pkt.kind = VPX_CODEC_STATS_PKT;
305 pkt.data.twopass_stats.buf = stats;
306 pkt.data.twopass_stats.sz = vp8_firstpass_stats_sz(cpi->common.MBs);
307 vpx_codec_pkt_list_add(pktlist, &pkt);
309 // TEMP debug code
310 #if OUTPUT_FPF
312 FILE *fpfile;
313 fpfile = fopen("firstpass.stt", "a");
315 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.0f\n",
316 stats->frame,
317 stats->intra_error,
318 stats->coded_error,
319 stats->ssim_weighted_pred_err,
320 stats->pcnt_inter,
321 stats->pcnt_motion,
322 stats->pcnt_second_ref,
323 stats->MVr,
324 stats->mvr_abs,
325 stats->MVc,
326 stats->mvc_abs,
327 stats->MVrv,
328 stats->MVcv,
329 stats->mv_in_out_count,
330 stats->count);
331 fclose(fpfile);
334 fpfile = fopen("fpmotionmap.stt", "a");
335 if(fwrite(cpi->fp_motion_map, 1, cpi->common.MBs, fpfile));
336 fclose(fpfile);
338 #endif
341 int vp8_input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
343 size_t stats_sz = vp8_firstpass_stats_sz(cpi->common.MBs);
345 if (cpi->stats_in >= cpi->stats_in_end)
346 return EOF;
348 *fps = *cpi->stats_in;
349 cpi->stats_in = (void*)((char *)cpi->stats_in + stats_sz);
350 return 1;
353 void vp8_zero_stats(FIRSTPASS_STATS *section)
355 section->frame = 0.0;
356 section->intra_error = 0.0;
357 section->coded_error = 0.0;
358 section->ssim_weighted_pred_err = 0.0;
359 section->pcnt_inter = 0.0;
360 section->pcnt_motion = 0.0;
361 section->pcnt_second_ref = 0.0;
362 section->MVr = 0.0;
363 section->mvr_abs = 0.0;
364 section->MVc = 0.0;
365 section->mvc_abs = 0.0;
366 section->MVrv = 0.0;
367 section->MVcv = 0.0;
368 section->mv_in_out_count = 0.0;
369 section->count = 0.0;
370 section->duration = 1.0;
372 void vp8_accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
374 section->frame += frame->frame;
375 section->intra_error += frame->intra_error;
376 section->coded_error += frame->coded_error;
377 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
378 section->pcnt_inter += frame->pcnt_inter;
379 section->pcnt_motion += frame->pcnt_motion;
380 section->pcnt_second_ref += frame->pcnt_second_ref;
381 section->MVr += frame->MVr;
382 section->mvr_abs += frame->mvr_abs;
383 section->MVc += frame->MVc;
384 section->mvc_abs += frame->mvc_abs;
385 section->MVrv += frame->MVrv;
386 section->MVcv += frame->MVcv;
387 section->mv_in_out_count += frame->mv_in_out_count;
388 section->count += frame->count;
389 section->duration += frame->duration;
391 void vp8_avg_stats(FIRSTPASS_STATS *section)
393 if (section->count < 1.0)
394 return;
396 section->intra_error /= section->count;
397 section->coded_error /= section->count;
398 section->ssim_weighted_pred_err /= section->count;
399 section->pcnt_inter /= section->count;
400 section->pcnt_second_ref /= section->count;
401 section->pcnt_motion /= section->count;
402 section->MVr /= section->count;
403 section->mvr_abs /= section->count;
404 section->MVc /= section->count;
405 section->mvc_abs /= section->count;
406 section->MVrv /= section->count;
407 section->MVcv /= section->count;
408 section->mv_in_out_count /= section->count;
409 section->duration /= section->count;
412 unsigned char *vp8_fpmm_get_pos(VP8_COMP *cpi)
414 return cpi->fp_motion_map_stats;
416 void vp8_fpmm_reset_pos(VP8_COMP *cpi, unsigned char *target_pos)
418 cpi->fp_motion_map_stats = target_pos;
421 void vp8_advance_fpmm(VP8_COMP *cpi, int count)
423 cpi->fp_motion_map_stats = (void*)((char*)cpi->fp_motion_map_stats +
424 count * vp8_firstpass_stats_sz(cpi->common.MBs));
427 void vp8_input_fpmm(VP8_COMP *cpi)
429 unsigned char *fpmm = cpi->fp_motion_map;
430 int MBs = cpi->common.MBs;
431 int max_frames = cpi->active_arnr_frames;
432 int i;
434 for (i=0; i<max_frames; i++)
436 char *motion_map = (char*)cpi->fp_motion_map_stats
437 + sizeof(FIRSTPASS_STATS);
439 memcpy(fpmm, motion_map, MBs);
440 fpmm += MBs;
441 vp8_advance_fpmm(cpi, 1);
444 // Flag the use of weights in the temporal filter
445 cpi->use_weighted_temporal_filter = 1;
448 void vp8_init_first_pass(VP8_COMP *cpi)
450 vp8_zero_stats(cpi->total_stats);
452 // TEMP debug code
453 #ifdef OUTPUT_FPF
455 FILE *fpfile;
456 fpfile = fopen("firstpass.stt", "w");
457 fclose(fpfile);
458 fpfile = fopen("fpmotionmap.stt", "wb");
459 fclose(fpfile);
461 #endif
465 void vp8_end_first_pass(VP8_COMP *cpi)
467 vp8_output_stats(cpi, cpi->output_pkt_list, cpi->total_stats);
470 void vp8_zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset )
472 MACROBLOCKD * const xd = & x->e_mbd;
473 BLOCK *b = &x->block[0];
474 BLOCKD *d = &x->e_mbd.block[0];
476 unsigned char *src_ptr = (*(b->base_src) + b->src);
477 int src_stride = b->src_stride;
478 unsigned char *ref_ptr;
479 int ref_stride=d->pre_stride;
481 // Set up pointers for this macro block recon buffer
482 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
484 ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre );
486 VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_stride, ref_ptr, ref_stride, (unsigned int *)(best_motion_err));
489 void vp8_first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, MV *ref_mv, MV *best_mv, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset )
491 MACROBLOCKD *const xd = & x->e_mbd;
492 BLOCK *b = &x->block[0];
493 BLOCKD *d = &x->e_mbd.block[0];
494 int num00;
496 MV tmp_mv = {0, 0};
498 int tmp_err;
499 int step_param = 3; //3; // Dont search over full range for first pass
500 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3;
501 int n;
502 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
503 int new_mv_mode_penalty = 256;
505 // override the default variance function to use MSE
506 v_fn_ptr.vf = VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16);
508 // Set up pointers for this macro block recon buffer
509 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
511 // Initial step/diamond search centred on best mv
512 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost, ref_mv);
513 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
514 tmp_err += new_mv_mode_penalty;
516 if (tmp_err < *best_motion_err)
518 *best_motion_err = tmp_err;
519 best_mv->row = tmp_mv.row;
520 best_mv->col = tmp_mv.col;
523 // Further step/diamond searches as necessary
524 n = num00;
525 num00 = 0;
527 while (n < further_steps)
529 n++;
531 if (num00)
532 num00--;
533 else
535 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param + n, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost, ref_mv);
536 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
537 tmp_err += new_mv_mode_penalty;
539 if (tmp_err < *best_motion_err)
541 *best_motion_err = tmp_err;
542 best_mv->row = tmp_mv.row;
543 best_mv->col = tmp_mv.col;
549 void vp8_first_pass(VP8_COMP *cpi)
551 int mb_row, mb_col;
552 MACROBLOCK *const x = & cpi->mb;
553 VP8_COMMON *const cm = & cpi->common;
554 MACROBLOCKD *const xd = & x->e_mbd;
556 int col_blocks = 4 * cm->mb_cols;
557 int recon_yoffset, recon_uvoffset;
558 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
559 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
560 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
561 int recon_y_stride = lst_yv12->y_stride;
562 int recon_uv_stride = lst_yv12->uv_stride;
563 int intra_error = 0;
564 int coded_error = 0;
566 int sum_mvr = 0, sum_mvc = 0;
567 int sum_mvr_abs = 0, sum_mvc_abs = 0;
568 int sum_mvrs = 0, sum_mvcs = 0;
569 int mvcount = 0;
570 int intercount = 0;
571 int second_ref_count = 0;
572 int intrapenalty = 256;
574 int sum_in_vectors = 0;
576 MV zero_ref_mv = {0, 0};
578 unsigned char *fp_motion_map_ptr = cpi->fp_motion_map;
580 vp8_clear_system_state(); //__asm emms;
582 x->src = * cpi->Source;
583 xd->pre = *lst_yv12;
584 xd->dst = *new_yv12;
586 x->partition_info = x->pi;
588 xd->mode_info_context = cm->mi;
590 vp8_build_block_offsets(x);
592 vp8_setup_block_dptrs(&x->e_mbd);
594 vp8_setup_block_ptrs(x);
596 // set up frame new frame for intra coded blocks
597 vp8_setup_intra_recon(new_yv12);
598 vp8cx_frame_init_quantizer(cpi);
600 // Initialise the MV cost table to the defaults
601 //if( cm->current_video_frame == 0)
602 //if ( 0 )
604 int flag[2] = {1, 1};
605 vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
606 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
607 vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cm->fc.mvc, flag);
610 // for each macroblock row in image
611 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
613 int_mv best_ref_mv;
615 best_ref_mv.as_int = 0;
617 // reset above block coeffs
618 xd->up_available = (mb_row != 0);
619 recon_yoffset = (mb_row * recon_y_stride * 16);
620 recon_uvoffset = (mb_row * recon_uv_stride * 8);
622 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
623 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
624 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
627 // for each macroblock col in image
628 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
630 int this_error;
631 int zero_error;
632 int zz_to_best_ratio;
633 int gf_motion_error = INT_MAX;
634 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
636 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
637 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
638 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
639 xd->left_available = (mb_col != 0);
641 // do intra 16x16 prediction
642 this_error = vp8_encode_intra(cpi, x, use_dc_pred);
644 // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
645 // 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.
646 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
647 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
648 this_error += intrapenalty;
650 // Cumulative intra error total
651 intra_error += this_error;
653 // Indicate default assumption of intra in the motion map
654 *fp_motion_map_ptr = 0;
656 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
657 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
658 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
660 // Other than for the first frame do a motion search
661 if (cm->current_video_frame > 0)
663 BLOCK *b = &x->block[0];
664 BLOCKD *d = &x->e_mbd.block[0];
665 MV tmp_mv = {0, 0};
666 int tmp_err;
667 int motion_error = INT_MAX;
669 // Simple 0,0 motion with no mv overhead
670 vp8_zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset );
671 d->bmi.mv.as_mv.row = 0;
672 d->bmi.mv.as_mv.col = 0;
674 // Save (0,0) error for later use
675 zero_error = motion_error;
677 // Test last reference frame using the previous best mv as the
678 // starting point (best reference) for the search
679 vp8_first_pass_motion_search(cpi, x, &best_ref_mv.as_mv,
680 &d->bmi.mv.as_mv, lst_yv12,
681 &motion_error, recon_yoffset);
683 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
684 if (best_ref_mv.as_int)
686 tmp_err = INT_MAX;
687 vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
688 lst_yv12, &tmp_err, recon_yoffset);
690 if ( tmp_err < motion_error )
692 motion_error = tmp_err;
693 d->bmi.mv.as_mv.row = tmp_mv.row;
694 d->bmi.mv.as_mv.col = tmp_mv.col;
698 // Experimental search in a second reference frame ((0,0) based only)
699 if (cm->current_video_frame > 1)
701 vp8_first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, gld_yv12, &gf_motion_error, recon_yoffset);
703 if ((gf_motion_error < motion_error) && (gf_motion_error < this_error))
705 second_ref_count++;
706 //motion_error = gf_motion_error;
707 //d->bmi.mv.as_mv.row = tmp_mv.row;
708 //d->bmi.mv.as_mv.col = tmp_mv.col;
710 /*else
712 xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
713 xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
714 xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
718 // Reset to last frame as reference buffer
719 xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
720 xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
721 xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
724 /* Intra assumed best */
725 best_ref_mv.as_int = 0;
727 if (motion_error <= this_error)
729 d->bmi.mv.as_mv.row <<= 3;
730 d->bmi.mv.as_mv.col <<= 3;
731 this_error = motion_error;
732 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv.as_mv);
733 vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x);
734 sum_mvr += d->bmi.mv.as_mv.row;
735 sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
736 sum_mvc += d->bmi.mv.as_mv.col;
737 sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
738 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
739 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
740 intercount++;
742 best_ref_mv.as_int = d->bmi.mv.as_int;
744 // Was the vector non-zero
745 if (d->bmi.mv.as_int)
747 mvcount++;
749 // Does the Row vector point inwards or outwards
750 if (mb_row < cm->mb_rows / 2)
752 if (d->bmi.mv.as_mv.row > 0)
753 sum_in_vectors--;
754 else if (d->bmi.mv.as_mv.row < 0)
755 sum_in_vectors++;
757 else if (mb_row > cm->mb_rows / 2)
759 if (d->bmi.mv.as_mv.row > 0)
760 sum_in_vectors++;
761 else if (d->bmi.mv.as_mv.row < 0)
762 sum_in_vectors--;
765 // Does the Row vector point inwards or outwards
766 if (mb_col < cm->mb_cols / 2)
768 if (d->bmi.mv.as_mv.col > 0)
769 sum_in_vectors--;
770 else if (d->bmi.mv.as_mv.col < 0)
771 sum_in_vectors++;
773 else if (mb_col > cm->mb_cols / 2)
775 if (d->bmi.mv.as_mv.col > 0)
776 sum_in_vectors++;
777 else if (d->bmi.mv.as_mv.col < 0)
778 sum_in_vectors--;
781 // Compute how close (0,0) predictor is to best
782 // predictor in terms of their prediction error
783 zz_to_best_ratio = (10*zero_error + this_error/2)
784 / (this_error+!this_error);
786 if ((zero_error < 50000) &&
787 (zz_to_best_ratio <= 11) )
788 *fp_motion_map_ptr = 1;
789 else
790 *fp_motion_map_ptr = 0;
792 else
794 // 0,0 mv was best
795 if( zero_error<50000 )
796 *fp_motion_map_ptr = 2;
797 else
798 *fp_motion_map_ptr = 1;
803 coded_error += this_error;
805 // adjust to the next column of macroblocks
806 x->src.y_buffer += 16;
807 x->src.u_buffer += 8;
808 x->src.v_buffer += 8;
810 recon_yoffset += 16;
811 recon_uvoffset += 8;
813 // Update the motion map
814 fp_motion_map_ptr++;
817 // adjust to the next row of mbs
818 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
819 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
820 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
822 //extend the recon for intra prediction
823 vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
824 vp8_clear_system_state(); //__asm emms;
827 vp8_clear_system_state(); //__asm emms;
829 double weight = 0.0;
831 FIRSTPASS_STATS fps;
833 fps.frame = cm->current_video_frame ;
834 fps.intra_error = intra_error >> 8;
835 fps.coded_error = coded_error >> 8;
836 weight = vp8_simple_weight(cpi->Source);
839 if (weight < 0.1)
840 weight = 0.1;
842 fps.ssim_weighted_pred_err = fps.coded_error * weight;
844 fps.pcnt_inter = 0.0;
845 fps.pcnt_motion = 0.0;
846 fps.MVr = 0.0;
847 fps.mvr_abs = 0.0;
848 fps.MVc = 0.0;
849 fps.mvc_abs = 0.0;
850 fps.MVrv = 0.0;
851 fps.MVcv = 0.0;
852 fps.mv_in_out_count = 0.0;
853 fps.count = 1.0;
855 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
856 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
858 if (mvcount > 0)
860 fps.MVr = (double)sum_mvr / (double)mvcount;
861 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
862 fps.MVc = (double)sum_mvc / (double)mvcount;
863 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
864 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
865 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
866 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
868 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
871 // TODO: handle the case when duration is set to 0, or something less
872 // than the full time between subsequent cpi->source_time_stamp s .
873 fps.duration = cpi->source_end_time_stamp - cpi->source_time_stamp;
875 // don't want to do outputstats with a stack variable!
876 memcpy(cpi->this_frame_stats,
877 &fps,
878 sizeof(FIRSTPASS_STATS));
879 memcpy((char*)cpi->this_frame_stats + sizeof(FIRSTPASS_STATS),
880 cpi->fp_motion_map,
881 sizeof(cpi->fp_motion_map[0]) * cpi->common.MBs);
882 vp8_output_stats(cpi, cpi->output_pkt_list, cpi->this_frame_stats);
883 vp8_accumulate_stats(cpi->total_stats, &fps);
886 // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
887 if ((cm->current_video_frame > 0) &&
888 (cpi->this_frame_stats->pcnt_inter > 0.20) &&
889 ((cpi->this_frame_stats->intra_error / cpi->this_frame_stats->coded_error) > 2.0))
891 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
894 // swap frame pointers so last frame refers to the frame we just compressed
895 vp8_swap_yv12_buffer(lst_yv12, new_yv12);
896 vp8_yv12_extend_frame_borders(lst_yv12);
898 // Special case for the first frame. Copy into the GF buffer as a second reference.
899 if (cm->current_video_frame == 0)
901 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
905 // use this to see what the first pass reconstruction looks like
906 if (0)
908 char filename[512];
909 FILE *recon_file;
910 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
912 if (cm->current_video_frame == 0)
913 recon_file = fopen(filename, "wb");
914 else
915 recon_file = fopen(filename, "ab");
917 if(fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file));
918 fclose(recon_file);
921 cm->current_video_frame++;
924 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
926 #define BASE_ERRPERMB 150
927 static int estimate_max_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width)
929 int Q;
930 int num_mbs = ((Height * Width) / (16 * 16));
931 int target_norm_bits_per_mb;
933 double err_per_mb = section_err / num_mbs;
934 double correction_factor;
935 double corr_high;
936 double speed_correction = 1.0;
937 double rolling_ratio;
939 double pow_highq = 0.90;
940 double pow_lowq = 0.40;
942 if (section_target_bandwitdh <= 0)
943 return cpi->maxq_max_limit; // Highest value allowed
945 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
947 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
948 if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->worst_quality))
950 //double adjustment_rate = 0.985 + (0.00005 * cpi->active_worst_quality);
951 double adjustment_rate = 0.99;
953 rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_target_bits;
955 //if ( cpi->est_max_qcorrection_factor > rolling_ratio )
956 if (rolling_ratio < 0.95)
957 //cpi->est_max_qcorrection_factor *= adjustment_rate;
958 cpi->est_max_qcorrection_factor -= 0.005;
959 //else if ( cpi->est_max_qcorrection_factor < rolling_ratio )
960 else if (rolling_ratio > 1.05)
961 cpi->est_max_qcorrection_factor += 0.005;
963 //cpi->est_max_qcorrection_factor /= adjustment_rate;
965 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;
968 // Corrections for higher compression speed settings (reduced compression expected)
969 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
971 if (cpi->oxcf.cpu_used <= 5)
972 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
973 else
974 speed_correction = 1.25;
977 // Correction factor used for Q values >= 20
978 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
979 corr_high = (corr_high < 0.05)
980 ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
982 // Try and pick a max Q that will be high enough to encode the
983 // content at the given rate.
984 for (Q = cpi->maxq_min_limit; Q < cpi->maxq_max_limit; Q++)
986 int bits_per_mb_at_this_q;
988 if (Q < 50)
990 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
991 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
993 else
994 correction_factor = corr_high;
996 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);
997 //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);
999 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1000 break;
1003 // Restriction on active max q for constrained quality mode.
1004 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
1005 (Q < cpi->cq_target_quality) )
1006 //(Q < cpi->oxcf.cq_level;) )
1008 Q = cpi->cq_target_quality;
1009 //Q = cpi->oxcf.cq_level;
1012 // Adjust maxq_min_limit and maxq_max_limit limits based on
1013 // averaga q observed in clip for non kf/gf.arf frames
1014 // Give average a chance to settle though.
1015 if ( (cpi->ni_frames >
1016 ((unsigned int)cpi->total_stats->count >> 8)) &&
1017 (cpi->ni_frames > 150) )
1019 cpi->maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality)
1020 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
1021 cpi->maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
1022 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
1025 return Q;
1027 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width)
1029 int Q;
1030 int num_mbs = ((Height * Width) / (16 * 16));
1031 int target_norm_bits_per_mb;
1033 double err_per_mb = section_err / num_mbs;
1034 double correction_factor;
1035 double corr_high;
1036 double speed_correction = 1.0;
1037 double pow_highq = 0.90;
1038 double pow_lowq = 0.40;
1040 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * section_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
1042 // Corrections for higher compression speed settings (reduced compression expected)
1043 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1045 if (cpi->oxcf.cpu_used <= 5)
1046 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1047 else
1048 speed_correction = 1.25;
1051 // Correction factor used for Q values >= 20
1052 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1053 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1055 // Try and pick a Q that can encode the content at the given rate.
1056 for (Q = 0; Q < MAXQ; Q++)
1058 int bits_per_mb_at_this_q;
1060 if (Q < 50)
1062 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1063 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_factor > 5.0) ? 5.0 : correction_factor;
1065 else
1066 correction_factor = corr_high;
1068 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);
1070 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1071 break;
1074 return Q;
1077 // Estimate a worst case Q for a KF group
1078 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_target_bandwitdh, int Height, int Width, double group_iiratio)
1080 int Q;
1081 int num_mbs = ((Height * Width) / (16 * 16));
1082 int target_norm_bits_per_mb = (512 * section_target_bandwitdh) / num_mbs;
1083 int bits_per_mb_at_this_q;
1085 double err_per_mb = section_err / num_mbs;
1086 double err_correction_factor;
1087 double corr_high;
1088 double speed_correction = 1.0;
1089 double current_spend_ratio = 1.0;
1091 double pow_highq = (POW1 < 0.6) ? POW1 + 0.3 : 0.90;
1092 double pow_lowq = (POW1 < 0.7) ? POW1 + 0.1 : 0.80;
1094 double iiratio_correction_factor = 1.0;
1096 double combined_correction_factor;
1098 // Trap special case where the target is <= 0
1099 if (target_norm_bits_per_mb <= 0)
1100 return MAXQ * 2;
1102 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
1103 // This is clamped to the range 0.1 to 10.0
1104 if (cpi->long_rolling_target_bits <= 0)
1105 current_spend_ratio = 10.0;
1106 else
1108 current_spend_ratio = (double)cpi->long_rolling_actual_bits / (double)cpi->long_rolling_target_bits;
1109 current_spend_ratio = (current_spend_ratio > 10.0) ? 10.0 : (current_spend_ratio < 0.1) ? 0.1 : current_spend_ratio;
1112 // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
1113 // The idea here is to favour subsampling in the hardest sections vs the easyest.
1114 iiratio_correction_factor = 1.0 - ((group_iiratio - 6.0) * 0.1);
1116 if (iiratio_correction_factor < 0.5)
1117 iiratio_correction_factor = 0.5;
1119 // Corrections for higher compression speed settings (reduced compression expected)
1120 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1122 if (cpi->oxcf.cpu_used <= 5)
1123 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1124 else
1125 speed_correction = 1.25;
1128 // Combine the various factors calculated above
1129 combined_correction_factor = speed_correction * iiratio_correction_factor * current_spend_ratio;
1131 // Correction factor used for Q values >= 20
1132 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1133 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1135 // Try and pick a Q that should be high enough to encode the content at the given rate.
1136 for (Q = 0; Q < MAXQ; Q++)
1138 // Q values < 20 treated as a special case
1139 if (Q < 20)
1141 err_correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1142 err_correction_factor = (err_correction_factor < 0.05) ? 0.05 : (err_correction_factor > 5.0) ? 5.0 : err_correction_factor;
1144 else
1145 err_correction_factor = corr_high;
1147 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor * combined_correction_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q]);
1149 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1150 break;
1153 // If we could not hit the target even at Max Q then estimate what Q would have bee required
1154 while ((bits_per_mb_at_this_q > target_norm_bits_per_mb) && (Q < (MAXQ * 2)))
1157 bits_per_mb_at_this_q = (int)(0.96 * bits_per_mb_at_this_q);
1158 Q++;
1161 if (0)
1163 FILE *f = fopen("estkf_q.stt", "a");
1164 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,
1165 target_norm_bits_per_mb, err_per_mb, err_correction_factor,
1166 current_spend_ratio, group_iiratio, iiratio_correction_factor,
1167 (double)cpi->buffer_level / (double)cpi->oxcf.optimal_buffer_level, Q);
1168 fclose(f);
1171 return Q;
1174 // For cq mode estimate a cq level that matches the observed
1175 // complexity and data rate.
1176 static int estimate_cq(VP8_COMP *cpi, double section_err,
1177 int section_target_bandwitdh, int Height, int Width)
1179 int Q;
1180 int num_mbs = ((Height * Width) / (16 * 16));
1181 int target_norm_bits_per_mb;
1183 double err_per_mb = section_err / num_mbs;
1184 double correction_factor;
1185 double corr_high;
1186 double speed_correction = 1.0;
1187 double pow_highq = 0.90;
1188 double pow_lowq = 0.40;
1189 double clip_iiratio;
1190 double clip_iifactor;
1192 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1193 ? (512 * section_target_bandwitdh) / num_mbs
1194 : 512 * (section_target_bandwitdh / num_mbs);
1196 // Corrections for higher compression speed settings
1197 // (reduced compression expected)
1198 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1200 if (cpi->oxcf.cpu_used <= 5)
1201 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1202 else
1203 speed_correction = 1.25;
1205 // II ratio correction factor for clip as a whole
1206 clip_iiratio = cpi->total_stats->intra_error /
1207 DOUBLE_DIVIDE_CHECK(cpi->total_stats->coded_error);
1208 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1209 if (clip_iifactor < 0.80)
1210 clip_iifactor = 0.80;
1212 // Correction factor used for Q values >= 20
1213 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1214 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1216 // Try and pick a Q that can encode the content at the given rate.
1217 for (Q = 0; Q < MAXQ; Q++)
1219 int bits_per_mb_at_this_q;
1221 if (Q < 50)
1223 correction_factor =
1224 pow( err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
1226 correction_factor = (correction_factor < 0.05) ? 0.05
1227 : (correction_factor > 5.0) ? 5.0
1228 : correction_factor;
1230 else
1231 correction_factor = corr_high;
1233 bits_per_mb_at_this_q =
1234 (int)( .5 + correction_factor *
1235 speed_correction *
1236 clip_iifactor *
1237 (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
1239 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1240 break;
1243 return cq_level[Q];
1246 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
1248 void vp8_init_second_pass(VP8_COMP *cpi)
1250 FIRSTPASS_STATS this_frame;
1251 FIRSTPASS_STATS *start_pos;
1253 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1255 vp8_zero_stats(cpi->total_stats);
1257 if (!cpi->stats_in_end)
1258 return;
1260 *cpi->total_stats = *cpi->stats_in_end;
1262 cpi->total_error_left = cpi->total_stats->ssim_weighted_pred_err;
1263 cpi->total_intra_error_left = cpi->total_stats->intra_error;
1264 cpi->total_coded_error_left = cpi->total_stats->coded_error;
1265 cpi->start_tot_err_left = cpi->total_error_left;
1267 //cpi->bits_left = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1268 //cpi->bits_left -= (long long)(cpi->total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1270 // each frame can have a different duration, as the frame rate in the source
1271 // isn't guaranteed to be constant. The frame rate prior to the first frame
1272 // encoded in the second pass is a guess. However the sum duration is not.
1273 // Its calculated based on the actual durations of all frames from the first
1274 // pass.
1275 vp8_new_frame_rate(cpi, 10000000.0 * cpi->total_stats->count / cpi->total_stats->duration);
1277 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1278 cpi->bits_left = (long long)(cpi->total_stats->duration * cpi->oxcf.target_bandwidth / 10000000.0) ;
1279 cpi->bits_left -= (long long)(cpi->total_stats->duration * two_pass_min_rate / 10000000.0);
1280 cpi->clip_bits_total = cpi->bits_left;
1282 // Calculate a minimum intra value to be used in determining the IIratio
1283 // scores used in the second pass. We have this minimum to make sure
1284 // that clips that are static but "low complexity" in the intra domain
1285 // are still boosted appropriately for KF/GF/ARF
1286 cpi->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1287 cpi->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1289 vp8_avg_stats(cpi->total_stats);
1291 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1293 double sum_iiratio = 0.0;
1294 double IIRatio;
1296 start_pos = cpi->stats_in; // Note starting "file" position
1298 while (vp8_input_stats(cpi, &this_frame) != EOF)
1300 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
1301 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1302 sum_iiratio += IIRatio;
1305 cpi->avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->total_stats->count);
1307 // Reset file position
1308 reset_fpf_position(cpi, start_pos);
1311 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1312 // used to allocate bits
1314 start_pos = cpi->stats_in; // Note starting "file" position
1316 cpi->modified_error_total = 0.0;
1317 cpi->modified_error_used = 0.0;
1319 while (vp8_input_stats(cpi, &this_frame) != EOF)
1321 cpi->modified_error_total += calculate_modified_err(cpi, &this_frame);
1323 cpi->modified_error_left = cpi->modified_error_total;
1325 reset_fpf_position(cpi, start_pos); // Reset file position
1329 // Calculate the clip target modified bits per error
1330 // The observed bpe starts as the same number.
1331 cpi->clip_bpe = cpi->bits_left /
1332 DOUBLE_DIVIDE_CHECK(cpi->modified_error_total);
1333 cpi->observed_bpe = cpi->clip_bpe;
1335 cpi->fp_motion_map_stats = (unsigned char *)cpi->stats_in;
1338 void vp8_end_second_pass(VP8_COMP *cpi)
1342 // This function gives and estimate of how badly we believe
1343 // the predicition quality is decaying from frame to frame.
1344 double gf_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
1346 double prediction_decay_rate;
1347 double motion_decay;
1348 double motion_pct = next_frame->pcnt_motion;
1351 // Initial basis is the % mbs inter coded
1352 prediction_decay_rate = next_frame->pcnt_inter;
1354 // High % motion -> somewhat higher decay rate
1355 motion_decay = (1.0 - (motion_pct / 20.0));
1356 if (motion_decay < prediction_decay_rate)
1357 prediction_decay_rate = motion_decay;
1359 // Adjustment to decay rate based on speed of motion
1361 double this_mv_rabs;
1362 double this_mv_cabs;
1363 double distance_factor;
1365 this_mv_rabs = fabs(next_frame->mvr_abs * motion_pct);
1366 this_mv_cabs = fabs(next_frame->mvc_abs * motion_pct);
1368 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
1369 (this_mv_cabs * this_mv_cabs)) / 250.0;
1370 distance_factor = ((distance_factor > 1.0)
1371 ? 0.0 : (1.0 - distance_factor));
1372 if (distance_factor < prediction_decay_rate)
1373 prediction_decay_rate = distance_factor;
1376 return prediction_decay_rate;
1379 // Analyse and define a gf/arf group .
1380 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1382 FIRSTPASS_STATS next_frame;
1383 FIRSTPASS_STATS *start_pos;
1384 int i;
1385 int y_width = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_width;
1386 int y_height = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_height;
1387 int image_size = y_width * y_height;
1388 double boost_score = 0.0;
1389 double old_boost_score = 0.0;
1390 double gf_group_err = 0.0;
1391 double gf_first_frame_err = 0.0;
1392 double mod_frame_err = 0.0;
1394 double mv_accumulator_rabs = 0.0;
1395 double mv_accumulator_cabs = 0.0;
1396 double mv_ratio_accumulator = 0.0;
1397 double decay_accumulator = 1.0;
1399 double boost_factor = IIFACTOR;
1400 double loop_decay_rate = 1.00; // Starting decay rate
1402 double this_frame_mv_in_out = 0.0;
1403 double mv_in_out_accumulator = 0.0;
1404 double abs_mv_in_out_accumulator = 0.0;
1405 double mod_err_per_mb_accumulator = 0.0;
1407 int max_bits = frame_max_bits(cpi); // Max for a single frame
1409 unsigned char *fpmm_pos;
1411 unsigned int allow_alt_ref =
1412 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1414 cpi->gf_group_bits = 0;
1415 cpi->gf_decay_rate = 0;
1417 vp8_clear_system_state(); //__asm emms;
1419 fpmm_pos = vp8_fpmm_get_pos(cpi);
1421 start_pos = cpi->stats_in;
1423 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1425 // Preload the stats for the next frame.
1426 mod_frame_err = calculate_modified_err(cpi, this_frame);
1428 // Note the error of the frame at the start of the group (this will be
1429 // the GF frame error if we code a normal gf
1430 gf_first_frame_err = mod_frame_err;
1432 // Special treatment if the current frame is a key frame (which is also
1433 // a gf). If it is then its error score (and hence bit allocation) need
1434 // to be subtracted out from the calculation for the GF group
1435 if (cpi->common.frame_type == KEY_FRAME)
1436 gf_group_err -= gf_first_frame_err;
1438 // Scan forward to try and work out how many frames the next gf group
1439 // should contain and what level of boost is appropriate for the GF
1440 // or ARF that will be coded with the group
1441 i = 0;
1443 while (((i < cpi->static_scene_max_gf_interval) ||
1444 ((cpi->frames_to_key - i) < MIN_GF_INTERVAL)) &&
1445 (i < cpi->frames_to_key))
1447 double r;
1448 double this_frame_mvr_ratio;
1449 double this_frame_mvc_ratio;
1450 double motion_decay;
1451 //double motion_pct = next_frame.pcnt_motion;
1452 double motion_pct;
1454 i++; // Increment the loop counter
1456 // Accumulate error score of frames in this gf group
1457 mod_frame_err = calculate_modified_err(cpi, this_frame);
1459 gf_group_err += mod_frame_err;
1461 mod_err_per_mb_accumulator +=
1462 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1464 if (EOF == vp8_input_stats(cpi, &next_frame))
1465 break;
1467 // Accumulate motion stats.
1468 motion_pct = next_frame.pcnt_motion;
1469 mv_accumulator_rabs += fabs(next_frame.mvr_abs * motion_pct);
1470 mv_accumulator_cabs += fabs(next_frame.mvc_abs * motion_pct);
1472 //Accumulate Motion In/Out of frame stats
1473 this_frame_mv_in_out =
1474 next_frame.mv_in_out_count * motion_pct;
1475 mv_in_out_accumulator +=
1476 next_frame.mv_in_out_count * motion_pct;
1477 abs_mv_in_out_accumulator +=
1478 fabs(next_frame.mv_in_out_count * motion_pct);
1480 // If there is a significant amount of motion
1481 if (motion_pct > 0.05)
1483 this_frame_mvr_ratio = fabs(next_frame.mvr_abs) /
1484 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVr));
1486 this_frame_mvc_ratio = fabs(next_frame.mvc_abs) /
1487 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVc));
1489 mv_ratio_accumulator +=
1490 (this_frame_mvr_ratio < next_frame.mvr_abs)
1491 ? (this_frame_mvr_ratio * motion_pct)
1492 : next_frame.mvr_abs * motion_pct;
1494 mv_ratio_accumulator +=
1495 (this_frame_mvc_ratio < next_frame.mvc_abs)
1496 ? (this_frame_mvc_ratio * motion_pct)
1497 : next_frame.mvc_abs * motion_pct;
1499 else
1501 mv_ratio_accumulator += 0.0;
1502 this_frame_mvr_ratio = 1.0;
1503 this_frame_mvc_ratio = 1.0;
1506 // Underlying boost factor is based on inter intra error ratio
1507 r = ( boost_factor *
1508 ( next_frame.intra_error /
1509 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)));
1511 if (next_frame.intra_error > cpi->gf_intra_err_min)
1512 r = (IIKFACTOR2 * next_frame.intra_error /
1513 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1514 else
1515 r = (IIKFACTOR2 * cpi->gf_intra_err_min /
1516 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1518 // Increase boost for frames where new data coming into frame
1519 // (eg zoom out). Slightly reduce boost if there is a net balance
1520 // of motion out of the frame (zoom in).
1521 // The range for this_frame_mv_in_out is -1.0 to +1.0
1522 if (this_frame_mv_in_out > 0.0)
1523 r += r * (this_frame_mv_in_out * 2.0);
1524 // In extreme case boost is halved
1525 else
1526 r += r * (this_frame_mv_in_out / 2.0);
1528 if (r > GF_RMAX)
1529 r = GF_RMAX;
1531 loop_decay_rate = gf_prediction_decay_rate(cpi, &next_frame);
1533 // Cumulative effect of decay
1534 decay_accumulator = decay_accumulator * loop_decay_rate;
1535 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1537 boost_score += (decay_accumulator * r);
1539 // Break clause to detect very still sections after motion
1540 // For example a staic image after a fade or other transition
1541 // instead of a clean key frame.
1542 if ( (i > MIN_GF_INTERVAL) &&
1543 (loop_decay_rate >= 0.999) &&
1544 (decay_accumulator < 0.9) )
1546 int j;
1547 FIRSTPASS_STATS * position = cpi->stats_in;
1548 FIRSTPASS_STATS tmp_next_frame;
1549 double decay_rate;
1551 // Look ahead a few frames to see if static condition
1552 // persists...
1553 for ( j = 0; j < 4; j++ )
1555 if (EOF == vp8_input_stats(cpi, &tmp_next_frame))
1556 break;
1558 decay_rate = gf_prediction_decay_rate(cpi, &tmp_next_frame);
1559 if ( decay_rate < 0.999 )
1560 break;
1562 reset_fpf_position(cpi, position); // Reset file position
1564 // Force GF not alt ref
1565 if ( j == 4 )
1567 if (0)
1569 FILE *f = fopen("fadegf.stt", "a");
1570 fprintf(f, " %8d %8d %10.4f %10.4f %10.4f\n",
1571 cpi->common.current_video_frame+i, i,
1572 loop_decay_rate, decay_accumulator,
1573 boost_score );
1574 fclose(f);
1577 allow_alt_ref = FALSE;
1579 boost_score = old_boost_score;
1580 break;
1584 // Break out conditions.
1585 if ( /* i>4 || */
1586 // Break at cpi->max_gf_interval unless almost totally static
1587 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1589 // Dont break out with a very short interval
1590 (i > MIN_GF_INTERVAL) &&
1591 // Dont break out very close to a key frame
1592 ((cpi->frames_to_key - i) >= MIN_GF_INTERVAL) &&
1593 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1594 ((mv_ratio_accumulator > 100.0) ||
1595 (abs_mv_in_out_accumulator > 3.0) ||
1596 (mv_in_out_accumulator < -2.0) ||
1597 ((boost_score - old_boost_score) < 2.0))
1600 boost_score = old_boost_score;
1601 break;
1604 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1606 old_boost_score = boost_score;
1609 cpi->gf_decay_rate =
1610 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1612 // When using CBR apply additional buffer related upper limits
1613 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1615 double max_boost;
1617 // For cbr apply buffer related limits
1618 if (cpi->drop_frames_allowed)
1620 int df_buffer_level = cpi->oxcf.drop_frames_water_mark *
1621 (cpi->oxcf.optimal_buffer_level / 100);
1623 if (cpi->buffer_level > df_buffer_level)
1624 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1625 else
1626 max_boost = 0.0;
1628 else if (cpi->buffer_level > 0)
1630 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
1632 else
1634 max_boost = 0.0;
1637 if (boost_score > max_boost)
1638 boost_score = max_boost;
1641 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1643 // Should we use the alternate refernce frame
1644 if (allow_alt_ref &&
1645 (i >= MIN_GF_INTERVAL) &&
1646 // dont use ARF very near next kf
1647 (i <= (cpi->frames_to_key - MIN_GF_INTERVAL)) &&
1648 (((next_frame.pcnt_inter > 0.75) &&
1649 ((mv_in_out_accumulator / (double)i > -0.2) || (mv_in_out_accumulator > -2.0)) &&
1650 //(cpi->gfu_boost>150) &&
1651 (cpi->gfu_boost > 100) &&
1652 //(cpi->gfu_boost>AF_THRESH2) &&
1653 //((cpi->gfu_boost/i)>AF_THRESH) &&
1654 //(decay_accumulator > 0.5) &&
1655 (cpi->gf_decay_rate <= (ARF_DECAY_THRESH + (cpi->gfu_boost / 200)))
1660 int Boost;
1661 int allocation_chunks;
1662 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1663 int tmp_q;
1664 int arf_frame_bits = 0;
1665 int group_bits;
1667 // Estimate the bits to be allocated to the group as a whole
1668 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1669 group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1670 else
1671 group_bits = 0;
1673 // Boost for arf frame
1674 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1675 Boost += (i * 50);
1676 allocation_chunks = (i * 100) + Boost;
1678 // Normalize Altboost and allocations chunck down to prevent overflow
1679 while (Boost > 1000)
1681 Boost /= 2;
1682 allocation_chunks /= 2;
1685 // Calculate the number of bits to be spent on the arf based on the boost number
1686 arf_frame_bits = (int)((double)Boost * (group_bits / (double)allocation_chunks));
1688 // Estimate if there are enough bits available to make worthwhile use of an arf.
1689 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits, cpi->common.Height, cpi->common.Width);
1691 // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames.
1692 if (tmp_q < cpi->worst_quality)
1694 int half_gf_int;
1695 int frames_after_arf;
1696 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1697 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1699 cpi->source_alt_ref_pending = TRUE;
1701 // 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
1702 // 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)
1703 gf_group_err -= mod_frame_err;
1705 // 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.
1706 // The future frame itself is part of the next group
1707 cpi->baseline_gf_interval = i - 1;
1709 // Define the arnr filter width for this group of frames:
1710 // We only filter frames that lie within a distance of half
1711 // the GF interval from the ARF frame. We also have to trap
1712 // cases where the filter extends beyond the end of clip.
1713 // Note: this_frame->frame has been updated in the loop
1714 // so it now points at the ARF frame.
1715 half_gf_int = cpi->baseline_gf_interval >> 1;
1716 frames_after_arf = cpi->total_stats->count - this_frame->frame - 1;
1718 switch (cpi->oxcf.arnr_type)
1720 case 1: // Backward filter
1721 frames_fwd = 0;
1722 if (frames_bwd > half_gf_int)
1723 frames_bwd = half_gf_int;
1724 break;
1726 case 2: // Forward filter
1727 if (frames_fwd > half_gf_int)
1728 frames_fwd = half_gf_int;
1729 if (frames_fwd > frames_after_arf)
1730 frames_fwd = frames_after_arf;
1731 frames_bwd = 0;
1732 break;
1734 case 3: // Centered filter
1735 default:
1736 frames_fwd >>= 1;
1737 if (frames_fwd > frames_after_arf)
1738 frames_fwd = frames_after_arf;
1739 if (frames_fwd > half_gf_int)
1740 frames_fwd = half_gf_int;
1742 frames_bwd = frames_fwd;
1744 // For even length filter there is one more frame backward
1745 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1746 if (frames_bwd < half_gf_int)
1747 frames_bwd += (cpi->oxcf.arnr_max_frames+1) & 0x1;
1748 break;
1751 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1754 // Advance to & read in the motion map for those frames
1755 // to be considered for filtering based on the position
1756 // of the ARF
1757 vp8_fpmm_reset_pos(cpi, cpi->fp_motion_map_stats_save);
1759 // Position at the 'earliest' frame to be filtered
1760 vp8_advance_fpmm(cpi,
1761 cpi->baseline_gf_interval - frames_bwd);
1763 // Read / create a motion map for the region of interest
1764 vp8_input_fpmm(cpi);
1767 else
1769 cpi->source_alt_ref_pending = FALSE;
1770 cpi->baseline_gf_interval = i;
1773 else
1775 cpi->source_alt_ref_pending = FALSE;
1776 cpi->baseline_gf_interval = i;
1779 // Conventional GF
1780 if (!cpi->source_alt_ref_pending)
1782 // Dont allow conventional gf too near the next kf
1783 if ((cpi->frames_to_key - cpi->baseline_gf_interval) < MIN_GF_INTERVAL)
1785 while (cpi->baseline_gf_interval < cpi->frames_to_key)
1787 if (EOF == vp8_input_stats(cpi, this_frame))
1788 break;
1790 cpi->baseline_gf_interval++;
1792 if (cpi->baseline_gf_interval < cpi->frames_to_key)
1793 gf_group_err += calculate_modified_err(cpi, this_frame);
1798 // Now decide how many bits should be allocated to the GF group as a proportion of those remaining in the kf group.
1799 // 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.
1800 // This is also important for short clips where there may only be one key frame.
1801 if (cpi->frames_to_key >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
1803 cpi->kf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;
1806 // Calculate the bits to be allocated to the group as a whole
1807 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0))
1808 cpi->gf_group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left));
1809 else
1810 cpi->gf_group_bits = 0;
1812 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;
1814 // Clip cpi->gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section)
1815 if (cpi->gf_group_bits > max_bits * cpi->baseline_gf_interval)
1816 cpi->gf_group_bits = max_bits * cpi->baseline_gf_interval;
1818 // Reset the file position
1819 reset_fpf_position(cpi, start_pos);
1821 // Update the record of error used so far (only done once per gf group)
1822 cpi->modified_error_used += gf_group_err;
1824 // Assign bits to the arf or gf.
1826 int Boost;
1827 int frames_in_section;
1828 int allocation_chunks;
1829 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1831 // For ARF frames
1832 if (cpi->source_alt_ref_pending)
1834 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1835 //Boost += (cpi->baseline_gf_interval * 25);
1836 Boost += (cpi->baseline_gf_interval * 50);
1838 // Set max and minimum boost and hence minimum allocation
1839 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1840 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1841 else if (Boost < 125)
1842 Boost = 125;
1844 frames_in_section = cpi->baseline_gf_interval + 1;
1845 allocation_chunks = (frames_in_section * 100) + Boost;
1847 // Else for standard golden frames
1848 else
1850 // boost based on inter / intra ratio of subsequent frames
1851 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
1853 // Set max and minimum boost and hence minimum allocation
1854 if (Boost > (cpi->baseline_gf_interval * 150))
1855 Boost = (cpi->baseline_gf_interval * 150);
1856 else if (Boost < 125)
1857 Boost = 125;
1859 frames_in_section = cpi->baseline_gf_interval;
1860 allocation_chunks = (frames_in_section * 100) + (Boost - 100);
1863 // Normalize Altboost and allocations chunck down to prevent overflow
1864 while (Boost > 1000)
1866 Boost /= 2;
1867 allocation_chunks /= 2;
1870 // Calculate the number of bits to be spent on the gf or arf based on the boost number
1871 cpi->gf_bits = (int)((double)Boost * (cpi->gf_group_bits / (double)allocation_chunks));
1873 // If the frame that is to be boosted is simpler than the average for
1874 // the gf/arf group then use an alternative calculation
1875 // based on the error score of the frame itself
1876 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
1878 double alt_gf_grp_bits;
1879 int alt_gf_bits;
1881 alt_gf_grp_bits =
1882 (double)cpi->kf_group_bits *
1883 (mod_frame_err * (double)cpi->baseline_gf_interval) /
1884 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left);
1886 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
1887 (double)allocation_chunks));
1889 if (cpi->gf_bits > alt_gf_bits)
1891 cpi->gf_bits = alt_gf_bits;
1894 // Else if it is harder than other frames in the group make sure it at
1895 // least receives an allocation in keeping with its relative error
1896 // score, otherwise it may be worse off than an "un-boosted" frame
1897 else
1899 int alt_gf_bits =
1900 (int)((double)cpi->kf_group_bits *
1901 mod_frame_err /
1902 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left));
1904 if (alt_gf_bits > cpi->gf_bits)
1906 cpi->gf_bits = alt_gf_bits;
1910 // Apply an additional limit for CBR
1911 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1913 if (cpi->gf_bits > (cpi->buffer_level >> 1))
1914 cpi->gf_bits = cpi->buffer_level >> 1;
1917 // Dont allow a negative value for gf_bits
1918 if (cpi->gf_bits < 0)
1919 cpi->gf_bits = 0;
1921 // Adjust KF group bits and error remainin
1922 cpi->kf_group_error_left -= gf_group_err;
1923 cpi->kf_group_bits -= cpi->gf_group_bits;
1925 if (cpi->kf_group_bits < 0)
1926 cpi->kf_group_bits = 0;
1928 // Note the error score left in the remaining frames of the group.
1929 // 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)
1930 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
1931 cpi->gf_group_error_left = gf_group_err - gf_first_frame_err;
1932 else
1933 cpi->gf_group_error_left = gf_group_err;
1935 cpi->gf_group_bits -= cpi->gf_bits;
1937 if (cpi->gf_group_bits < 0)
1938 cpi->gf_group_bits = 0;
1940 // Set aside some bits for a mid gf sequence boost
1941 if ((cpi->gfu_boost > 150) && (cpi->baseline_gf_interval > 5))
1943 int pct_extra = (cpi->gfu_boost - 100) / 50;
1944 pct_extra = (pct_extra > 10) ? 10 : pct_extra;
1946 cpi->mid_gf_extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
1947 cpi->gf_group_bits -= cpi->mid_gf_extra_bits;
1949 else
1950 cpi->mid_gf_extra_bits = 0;
1952 cpi->gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame
1955 if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) // Normal GF and not a KF
1957 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for this frame
1960 // Adjustment to estimate_max_q based on a measure of complexity of the section
1961 if (cpi->common.frame_type != KEY_FRAME)
1963 FIRSTPASS_STATS sectionstats;
1964 double Ratio;
1966 vp8_zero_stats(&sectionstats);
1967 reset_fpf_position(cpi, start_pos);
1969 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
1971 vp8_input_stats(cpi, &next_frame);
1972 vp8_accumulate_stats(&sectionstats, &next_frame);
1975 vp8_avg_stats(&sectionstats);
1977 cpi->section_intra_rating =
1978 sectionstats.intra_error /
1979 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
1981 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
1982 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
1984 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
1986 if (cpi->section_max_qfactor < 0.80)
1987 cpi->section_max_qfactor = 0.80;
1990 //else
1991 // cpi->section_max_qfactor = 1.0;
1993 reset_fpf_position(cpi, start_pos);
1996 // Reset the First pass motion map file position
1997 vp8_fpmm_reset_pos(cpi, fpmm_pos);
2000 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
2001 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2003 int target_frame_size; // gf_group_error_left
2005 double modified_err;
2006 double err_fraction; // What portion of the remaining GF group error is used by this frame
2008 int max_bits = frame_max_bits(cpi); // Max for a single frame
2010 // The final few frames have special treatment
2011 if (cpi->frames_till_gf_update_due >= (int)(cpi->total_stats->count - cpi->common.current_video_frame))
2013 cpi->gf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;;
2016 // Calculate modified prediction error used in bit allocation
2017 modified_err = calculate_modified_err(cpi, this_frame);
2019 if (cpi->gf_group_error_left > 0)
2020 err_fraction = modified_err / cpi->gf_group_error_left; // What portion of the remaining GF group error is used by this frame
2021 else
2022 err_fraction = 0.0;
2024 target_frame_size = (int)((double)cpi->gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it?
2026 // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top end.
2027 if (target_frame_size < 0)
2028 target_frame_size = 0;
2029 else
2031 if (target_frame_size > max_bits)
2032 target_frame_size = max_bits;
2034 if (target_frame_size > cpi->gf_group_bits)
2035 target_frame_size = cpi->gf_group_bits;
2038 cpi->gf_group_error_left -= modified_err; // Adjust error remaining
2039 cpi->gf_group_bits -= target_frame_size; // Adjust bits remaining
2041 if (cpi->gf_group_bits < 0)
2042 cpi->gf_group_bits = 0;
2044 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame.
2046 // Special case for the frame that lies half way between two gfs
2047 if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2)
2048 target_frame_size += cpi->mid_gf_extra_bits;
2050 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
2053 void vp8_second_pass(VP8_COMP *cpi)
2055 int tmp_q;
2056 int frames_left = (int)(cpi->total_stats->count - cpi->common.current_video_frame);
2058 FIRSTPASS_STATS this_frame;
2059 FIRSTPASS_STATS this_frame_copy;
2061 VP8_COMMON *cm = &cpi->common;
2063 double this_frame_error;
2064 double this_frame_intra_error;
2065 double this_frame_coded_error;
2067 FIRSTPASS_STATS *start_pos;
2069 if (!cpi->stats_in)
2071 return ;
2074 vp8_clear_system_state();
2076 if (EOF == vp8_input_stats(cpi, &this_frame))
2077 return;
2079 vpx_memset(cpi->fp_motion_map, 0,
2080 cpi->oxcf.arnr_max_frames*cpi->common.MBs);
2081 cpi->fp_motion_map_stats_save = vp8_fpmm_get_pos(cpi);
2083 // Step over this frame's first pass motion map
2084 vp8_advance_fpmm(cpi, 1);
2086 this_frame_error = this_frame.ssim_weighted_pred_err;
2087 this_frame_intra_error = this_frame.intra_error;
2088 this_frame_coded_error = this_frame.coded_error;
2090 // Store information regarding level of motion etc for use mode decisions.
2091 cpi->motion_speed = (int)(fabs(this_frame.MVr) + fabs(this_frame.MVc));
2092 cpi->motion_var = (int)(fabs(this_frame.MVrv) + fabs(this_frame.MVcv));
2093 cpi->inter_lvl = (int)(this_frame.pcnt_inter * 100);
2094 cpi->intra_lvl = (int)((1.0 - this_frame.pcnt_inter) * 100);
2095 cpi->motion_lvl = (int)(this_frame.pcnt_motion * 100);
2097 start_pos = cpi->stats_in;
2099 // keyframe and section processing !
2100 if (cpi->frames_to_key == 0)
2102 // Define next KF group and assign bits to it
2103 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2104 vp8_find_next_key_frame(cpi, &this_frame_copy);
2106 // 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
2107 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2108 // This is temporary code till we decide what should really happen in this case.
2109 if (cpi->oxcf.error_resilient_mode)
2111 cpi->gf_group_bits = cpi->kf_group_bits;
2112 cpi->gf_group_error_left = cpi->kf_group_error_left;
2113 cpi->baseline_gf_interval = cpi->frames_to_key;
2114 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2115 cpi->source_alt_ref_pending = FALSE;
2120 // Is this a GF / ARF (Note that a KF is always also a GF)
2121 if (cpi->frames_till_gf_update_due == 0)
2123 // Update monitor of the bits per error observed so far.
2124 // Done once per gf group based on what has gone before
2125 // so do nothing if this is the first frame.
2126 if (cpi->common.current_video_frame > 0)
2128 cpi->observed_bpe =
2129 (double)(cpi->clip_bits_total - cpi->bits_left) /
2130 cpi->modified_error_used;
2133 // Define next gf group and assign bits to it
2134 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2135 define_gf_group(cpi, &this_frame_copy);
2137 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
2138 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
2139 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
2140 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
2142 // Assign a standard frames worth of bits from those allocated to the GF group
2143 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2144 assign_std_frame_bits(cpi, &this_frame_copy);
2146 // 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.
2147 //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
2148 if (FALSE)
2150 int extra_bits;
2151 int pct_extra = (cpi->gfu_boost - 100) / 50;
2153 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2155 extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
2156 cpi->gf_group_bits -= extra_bits;
2157 cpi->per_frame_bandwidth += extra_bits;
2162 // Otherwise this is an ordinary frame
2163 else
2165 // 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
2166 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2167 // This is temporary code till we decide what should really happen in this case.
2168 if (cpi->oxcf.error_resilient_mode)
2170 cpi->frames_till_gf_update_due = cpi->frames_to_key;
2172 if (cpi->common.frame_type != KEY_FRAME)
2174 // Assign bits from those allocated to the GF group
2175 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2176 assign_std_frame_bits(cpi, &this_frame_copy);
2179 else
2181 // Assign bits from those allocated to the GF group
2182 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2183 assign_std_frame_bits(cpi, &this_frame_copy);
2187 // Keep a globally available copy of this and the next frame's iiratio.
2188 cpi->this_iiratio = this_frame_intra_error /
2189 DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
2191 FIRSTPASS_STATS next_frame;
2192 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2194 cpi->next_iiratio = next_frame.intra_error /
2195 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
2199 // Set nominal per second bandwidth for this frame
2200 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
2201 if (cpi->target_bandwidth < 0)
2202 cpi->target_bandwidth = 0;
2204 if (cpi->common.current_video_frame == 0)
2206 cpi->est_max_qcorrection_factor = 1.0;
2208 // Experimental code to try and set a cq_level in constrained
2209 // quality mode.
2210 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2212 int est_cq;
2214 est_cq =
2215 estimate_cq( cpi,
2216 (cpi->total_coded_error_left / frames_left),
2217 (int)(cpi->bits_left / frames_left),
2218 cpi->common.Height, cpi->common.Width);
2220 cpi->cq_target_quality = cpi->oxcf.cq_level;
2221 if ( est_cq > cpi->cq_target_quality )
2222 cpi->cq_target_quality = est_cq;
2225 // guess at maxq needed in 2nd pass
2226 cpi->maxq_max_limit = cpi->worst_quality;
2227 cpi->maxq_min_limit = cpi->best_quality;
2228 tmp_q = estimate_max_q( cpi,
2229 (cpi->total_coded_error_left / frames_left),
2230 (int)(cpi->bits_left / frames_left),
2231 cpi->common.Height,
2232 cpi->common.Width);
2234 // Limit the maxq value returned subsequently.
2235 // This increases the risk of overspend or underspend if the initial
2236 // estimate for the clip is bad, but helps prevent excessive
2237 // variation in Q, especially near the end of a clip
2238 // where for example a small overspend may cause Q to crash
2239 cpi->maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2240 ? (tmp_q + 32) : cpi->worst_quality;
2241 cpi->maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2242 ? (tmp_q - 32) : cpi->best_quality;
2244 cpi->active_worst_quality = tmp_q;
2245 cpi->ni_av_qi = tmp_q;
2248 // The last few frames of a clip almost always have to few or too many
2249 // bits and for the sake of over exact rate control we dont want to make
2250 // radical adjustments to the allowed quantizer range just to use up a
2251 // few surplus bits or get beneath the target rate.
2252 else if ( (cpi->common.current_video_frame <
2253 (((unsigned int)cpi->total_stats->count * 255)>>8)) &&
2254 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2255 (unsigned int)cpi->total_stats->count) )
2257 if (frames_left < 1)
2258 frames_left = 1;
2260 tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left), cpi->common.Height, cpi->common.Width);
2262 // Move active_worst_quality but in a damped way
2263 if (tmp_q > cpi->active_worst_quality)
2264 cpi->active_worst_quality ++;
2265 else if (tmp_q < cpi->active_worst_quality)
2266 cpi->active_worst_quality --;
2268 cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2) / 4;
2271 cpi->frames_to_key --;
2272 cpi->total_error_left -= this_frame_error;
2273 cpi->total_intra_error_left -= this_frame_intra_error;
2274 cpi->total_coded_error_left -= this_frame_coded_error;
2278 static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRSTPASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2280 BOOL is_viable_kf = FALSE;
2282 // Does the frame satisfy the primary criteria of a key frame
2283 // If so, then examine how well it predicts subsequent frames
2284 if ((this_frame->pcnt_second_ref < 0.10) &&
2285 (next_frame->pcnt_second_ref < 0.10) &&
2286 ((this_frame->pcnt_inter < 0.05) ||
2288 (this_frame->pcnt_inter < .25) &&
2289 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
2290 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
2291 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
2292 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
2298 int i;
2299 FIRSTPASS_STATS *start_pos;
2301 FIRSTPASS_STATS local_next_frame;
2303 double boost_score = 0.0;
2304 double old_boost_score = 0.0;
2305 double decay_accumulator = 1.0;
2306 double next_iiratio;
2308 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2310 // Note the starting file position so we can reset to it
2311 start_pos = cpi->stats_in;
2313 // Examine how well the key frame predicts subsequent frames
2314 for (i = 0 ; i < 16; i++)
2316 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)) ;
2318 if (next_iiratio > RMAX)
2319 next_iiratio = RMAX;
2321 // Cumulative effect of decay in prediction quality
2322 if (local_next_frame.pcnt_inter > 0.85)
2323 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2324 else
2325 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
2327 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2329 // Keep a running total
2330 boost_score += (decay_accumulator * next_iiratio);
2332 // Test various breakout clauses
2333 if ((local_next_frame.pcnt_inter < 0.05) ||
2334 (next_iiratio < 1.5) ||
2335 ((local_next_frame.pcnt_inter < 0.20) && (next_iiratio < 3.0)) ||
2336 ((boost_score - old_boost_score) < 0.5) ||
2337 (local_next_frame.intra_error < 200)
2340 break;
2343 old_boost_score = boost_score;
2345 // Get the next frame details
2346 if (EOF == vp8_input_stats(cpi, &local_next_frame))
2347 break;
2350 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2351 if (boost_score > 5.0 && (i > 3))
2352 is_viable_kf = TRUE;
2353 else
2355 // Reset the file position
2356 reset_fpf_position(cpi, start_pos);
2358 is_viable_kf = FALSE;
2362 return is_viable_kf;
2364 void vp8_find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
2366 int i;
2367 FIRSTPASS_STATS last_frame;
2368 FIRSTPASS_STATS first_frame;
2369 FIRSTPASS_STATS next_frame;
2370 FIRSTPASS_STATS *start_position;
2372 double decay_accumulator = 0;
2373 double boost_score = 0;
2374 double old_boost_score = 0.0;
2375 double loop_decay_rate;
2377 double kf_mod_err = 0.0;
2378 double kf_group_err = 0.0;
2379 double kf_group_intra_err = 0.0;
2380 double kf_group_coded_err = 0.0;
2381 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2383 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2385 vp8_clear_system_state(); //__asm emms;
2386 start_position = cpi->stats_in;
2388 cpi->common.frame_type = KEY_FRAME;
2390 // is this a forced key frame by interval
2391 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2393 // Clear the alt ref active flag as this can never be active on a key frame
2394 cpi->source_alt_ref_active = FALSE;
2396 // Kf is always a gf so clear frames till next gf counter
2397 cpi->frames_till_gf_update_due = 0;
2399 cpi->frames_to_key = 1;
2401 // Take a copy of the initial frame details
2402 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2404 cpi->kf_group_bits = 0; // Total bits avaialable to kf group
2405 cpi->kf_group_error_left = 0; // Group modified error score.
2407 kf_mod_err = calculate_modified_err(cpi, this_frame);
2409 // find the next keyframe
2410 while (cpi->stats_in < cpi->stats_in_end)
2412 // Accumulate kf group error
2413 kf_group_err += calculate_modified_err(cpi, this_frame);
2415 // These figures keep intra and coded error counts for all frames including key frames in the group.
2416 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2417 kf_group_intra_err += this_frame->intra_error;
2418 kf_group_coded_err += this_frame->coded_error;
2420 // load a the next frame's stats
2421 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2422 vp8_input_stats(cpi, this_frame);
2424 // Provided that we are not at the end of the file...
2425 if (cpi->oxcf.auto_key
2426 && lookup_next_frame_stats(cpi, &next_frame) != EOF)
2428 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame))
2429 break;
2431 // Step on to the next frame
2432 cpi->frames_to_key ++;
2434 // If we don't have a real key frame within the next two
2435 // forcekeyframeevery intervals then break out of the loop.
2436 if (cpi->frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2437 break;
2438 } else
2439 cpi->frames_to_key ++;
2442 // If there is a max kf interval set by the user we must obey it.
2443 // We already breakout of the loop above at 2x max.
2444 // This code centers the extra kf if the actual natural
2445 // interval is between 1x and 2x
2446 if (cpi->oxcf.auto_key
2447 && cpi->frames_to_key > (int)cpi->key_frame_frequency )
2449 FIRSTPASS_STATS *current_pos = cpi->stats_in;
2450 FIRSTPASS_STATS tmp_frame;
2452 cpi->frames_to_key /= 2;
2454 // Copy first frame details
2455 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2457 // Reset to the start of the group
2458 reset_fpf_position(cpi, start_position);
2460 kf_group_err = 0;
2461 kf_group_intra_err = 0;
2462 kf_group_coded_err = 0;
2464 // Rescan to get the correct error data for the forced kf group
2465 for( i = 0; i < cpi->frames_to_key; i++ )
2467 // Accumulate kf group errors
2468 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2469 kf_group_intra_err += tmp_frame.intra_error;
2470 kf_group_coded_err += tmp_frame.coded_error;
2472 // Load a the next frame's stats
2473 vp8_input_stats(cpi, &tmp_frame);
2476 // Reset to the start of the group
2477 reset_fpf_position(cpi, current_pos);
2479 cpi->next_key_frame_forced = TRUE;
2481 else
2482 cpi->next_key_frame_forced = FALSE;
2484 // Special case for the last frame of the file
2485 if (cpi->stats_in >= cpi->stats_in_end)
2487 // Accumulate kf group error
2488 kf_group_err += calculate_modified_err(cpi, this_frame);
2490 // These figures keep intra and coded error counts for all frames including key frames in the group.
2491 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2492 kf_group_intra_err += this_frame->intra_error;
2493 kf_group_coded_err += this_frame->coded_error;
2496 // Calculate the number of bits that should be assigned to the kf group.
2497 if ((cpi->bits_left > 0) && ((int)cpi->modified_error_left > 0))
2499 // Max for a single normal frame (not key frame)
2500 int max_bits = frame_max_bits(cpi);
2502 // Maximum bits for the kf group
2503 long long max_grp_bits;
2505 // Default allocation based on bits left and relative
2506 // complexity of the section
2507 cpi->kf_group_bits = (long long)( cpi->bits_left *
2508 ( kf_group_err /
2509 cpi->modified_error_left ));
2511 // Clip based on maximum per frame rate defined by the user.
2512 max_grp_bits = (long long)max_bits * (long long)cpi->frames_to_key;
2513 if (cpi->kf_group_bits > max_grp_bits)
2514 cpi->kf_group_bits = max_grp_bits;
2516 // Additional special case for CBR if buffer is getting full.
2517 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2519 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2520 int buffer_lvl = cpi->buffer_level;
2522 // If the buffer is near or above the optimal and this kf group is
2523 // not being allocated much then increase the allocation a bit.
2524 if (buffer_lvl >= opt_buffer_lvl)
2526 int high_water_mark = (opt_buffer_lvl +
2527 cpi->oxcf.maximum_buffer_size) >> 1;
2529 long long av_group_bits;
2531 // Av bits per frame * number of frames
2532 av_group_bits = (long long)cpi->av_per_frame_bandwidth *
2533 (long long)cpi->frames_to_key;
2535 // We are at or above the maximum.
2536 if (cpi->buffer_level >= high_water_mark)
2538 long long min_group_bits;
2540 min_group_bits = av_group_bits +
2541 (long long)(buffer_lvl -
2542 high_water_mark);
2544 if (cpi->kf_group_bits < min_group_bits)
2545 cpi->kf_group_bits = min_group_bits;
2547 // We are above optimal but below the maximum
2548 else if (cpi->kf_group_bits < av_group_bits)
2550 long long bits_below_av = av_group_bits -
2551 cpi->kf_group_bits;
2553 cpi->kf_group_bits +=
2554 (long long)((double)bits_below_av *
2555 (double)(buffer_lvl - opt_buffer_lvl) /
2556 (double)(high_water_mark - opt_buffer_lvl));
2561 else
2562 cpi->kf_group_bits = 0;
2564 // Reset the first pass file position
2565 reset_fpf_position(cpi, start_position);
2567 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2568 decay_accumulator = 1.0;
2569 boost_score = 0.0;
2570 loop_decay_rate = 1.00; // Starting decay rate
2572 for (i = 0 ; i < cpi->frames_to_key ; i++)
2574 double r;
2575 double motion_decay;
2576 double motion_pct;
2578 if (EOF == vp8_input_stats(cpi, &next_frame))
2579 break;
2581 if (next_frame.intra_error > cpi->kf_intra_err_min)
2582 r = (IIKFACTOR2 * next_frame.intra_error /
2583 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2584 else
2585 r = (IIKFACTOR2 * cpi->kf_intra_err_min /
2586 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2588 if (r > RMAX)
2589 r = RMAX;
2591 // Adjust loop decay rate
2592 //if ( next_frame.pcnt_inter < loop_decay_rate )
2593 loop_decay_rate = next_frame.pcnt_inter;
2595 // High % motion -> somewhat higher decay rate
2596 motion_pct = next_frame.pcnt_motion;
2597 motion_decay = (1.0 - (motion_pct / 20.0));
2598 if (motion_decay < loop_decay_rate)
2599 loop_decay_rate = motion_decay;
2601 // Adjustment to decay rate based on speed of motion
2603 double this_mv_rabs;
2604 double this_mv_cabs;
2605 double distance_factor;
2607 this_mv_rabs = fabs(next_frame.mvr_abs * motion_pct);
2608 this_mv_cabs = fabs(next_frame.mvc_abs * motion_pct);
2610 distance_factor = sqrt((this_mv_rabs * this_mv_rabs) +
2611 (this_mv_cabs * this_mv_cabs)) / 250.0;
2612 distance_factor = ((distance_factor > 1.0)
2613 ? 0.0 : (1.0 - distance_factor));
2614 if (distance_factor < loop_decay_rate)
2615 loop_decay_rate = distance_factor;
2618 decay_accumulator = decay_accumulator * loop_decay_rate;
2619 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2621 boost_score += (decay_accumulator * r);
2623 if ((i > MIN_GF_INTERVAL) &&
2624 ((boost_score - old_boost_score) < 1.0))
2626 break;
2629 old_boost_score = boost_score;
2632 if (1)
2634 FIRSTPASS_STATS sectionstats;
2635 double Ratio;
2637 vp8_zero_stats(&sectionstats);
2638 reset_fpf_position(cpi, start_position);
2640 for (i = 0 ; i < cpi->frames_to_key ; i++)
2642 vp8_input_stats(cpi, &next_frame);
2643 vp8_accumulate_stats(&sectionstats, &next_frame);
2646 vp8_avg_stats(&sectionstats);
2648 cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2650 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2651 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2653 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2655 if (cpi->section_max_qfactor < 0.80)
2656 cpi->section_max_qfactor = 0.80;
2659 //else
2660 // cpi->section_max_qfactor = 1.0;
2663 // When using CBR apply additional buffer fullness related upper limits
2664 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2666 double max_boost;
2668 if (cpi->drop_frames_allowed)
2670 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf.optimal_buffer_level / 100);
2672 if (cpi->buffer_level > df_buffer_level)
2673 max_boost = ((double)((cpi->buffer_level - df_buffer_level) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2674 else
2675 max_boost = 0.0;
2677 else if (cpi->buffer_level > 0)
2679 max_boost = ((double)(cpi->buffer_level * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi->av_per_frame_bandwidth);
2681 else
2683 max_boost = 0.0;
2686 if (boost_score > max_boost)
2687 boost_score = max_boost;
2690 // Reset the first pass file position
2691 reset_fpf_position(cpi, start_position);
2693 // Work out how many bits to allocate for the key frame itself
2694 if (1)
2696 int kf_boost = boost_score;
2697 int allocation_chunks;
2698 int Counter = cpi->frames_to_key;
2699 int alt_kf_bits;
2700 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2701 // Min boost based on kf interval
2702 #if 0
2704 while ((kf_boost < 48) && (Counter > 0))
2706 Counter -= 2;
2707 kf_boost ++;
2710 #endif
2712 if (kf_boost < 48)
2714 kf_boost += ((Counter + 1) >> 1);
2716 if (kf_boost > 48) kf_boost = 48;
2719 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2720 if ((lst_yv12->y_width * lst_yv12->y_height) > (320 * 240))
2721 kf_boost += 2 * (lst_yv12->y_width * lst_yv12->y_height) / (320 * 240);
2722 else if ((lst_yv12->y_width * lst_yv12->y_height) < (320 * 240))
2723 kf_boost -= 4 * (320 * 240) / (lst_yv12->y_width * lst_yv12->y_height);
2725 kf_boost = (int)((double)kf_boost * 100.0) >> 4; // Scale 16 to 100
2727 // Adjustment to boost based on recent average q
2728 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2730 if (kf_boost < 250) // Min KF boost
2731 kf_boost = 250;
2733 // We do three calculations for kf size.
2734 // The first is based on the error score for the whole kf group.
2735 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2736 // 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
2738 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
2740 // Normalize Altboost and allocations chunck down to prevent overflow
2741 while (kf_boost > 1000)
2743 kf_boost /= 2;
2744 allocation_chunks /= 2;
2747 cpi->kf_group_bits = (cpi->kf_group_bits < 0) ? 0 : cpi->kf_group_bits;
2749 // Calculate the number of bits to be spent on the key frame
2750 cpi->kf_bits = (int)((double)kf_boost * ((double)cpi->kf_group_bits / (double)allocation_chunks));
2752 // Apply an additional limit for CBR
2753 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2755 if (cpi->kf_bits > ((3 * cpi->buffer_level) >> 2))
2756 cpi->kf_bits = (3 * cpi->buffer_level) >> 2;
2759 // If the key frame is actually easier than the average for the
2760 // kf group (which does sometimes happen... eg a blank intro frame)
2761 // Then use an alternate calculation based on the kf error score
2762 // which should give a smaller key frame.
2763 if (kf_mod_err < kf_group_err / cpi->frames_to_key)
2765 double alt_kf_grp_bits =
2766 ((double)cpi->bits_left *
2767 (kf_mod_err * (double)cpi->frames_to_key) /
2768 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left));
2770 alt_kf_bits = (int)((double)kf_boost *
2771 (alt_kf_grp_bits / (double)allocation_chunks));
2773 if (cpi->kf_bits > alt_kf_bits)
2775 cpi->kf_bits = alt_kf_bits;
2778 // Else if it is much harder than other frames in the group make sure
2779 // it at least receives an allocation in keeping with its relative
2780 // error score
2781 else
2783 alt_kf_bits =
2784 (int)((double)cpi->bits_left *
2785 (kf_mod_err /
2786 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left)));
2788 if (alt_kf_bits > cpi->kf_bits)
2790 cpi->kf_bits = alt_kf_bits;
2794 cpi->kf_group_bits -= cpi->kf_bits;
2795 cpi->kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance
2797 cpi->per_frame_bandwidth = cpi->kf_bits; // Peer frame bit target for this frame
2798 cpi->target_bandwidth = cpi->kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate
2801 // Note the total error score of the kf group minus the key frame itself
2802 cpi->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2804 // Adjust the count of total modified error left.
2805 // The count of bits left is adjusted elsewhere based on real coded frame sizes
2806 cpi->modified_error_left -= kf_group_err;
2808 if (cpi->oxcf.allow_spatial_resampling)
2810 int resample_trigger = FALSE;
2811 int last_kf_resampled = FALSE;
2812 int kf_q;
2813 int scale_val = 0;
2814 int hr, hs, vr, vs;
2815 int new_width = cpi->oxcf.Width;
2816 int new_height = cpi->oxcf.Height;
2818 int projected_buffer_level = cpi->buffer_level;
2819 int tmp_q;
2821 double projected_bits_perframe;
2822 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
2823 double err_per_frame = kf_group_err / cpi->frames_to_key;
2824 double bits_per_frame;
2825 double av_bits_per_frame;
2826 double effective_size_ratio;
2828 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi->oxcf.Height))
2829 last_kf_resampled = TRUE;
2831 // Set back to unscaled by defaults
2832 cpi->common.horiz_scale = NORMAL;
2833 cpi->common.vert_scale = NORMAL;
2835 // Calculate Average bits per frame.
2836 //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats->count - cpi->common.current_video_frame);
2837 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate);
2838 //if ( av_bits_per_frame < 0.0 )
2839 // av_bits_per_frame = 0.0
2841 // CBR... Use the clip average as the target for deciding resample
2842 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2844 bits_per_frame = av_bits_per_frame;
2847 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
2848 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
2849 else
2851 bits_per_frame = cpi->kf_group_bits / cpi->frames_to_key; // This accounts for how hard the section is...
2853 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
2854 bits_per_frame = av_bits_per_frame;
2857 // bits_per_frame should comply with our minimum
2858 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100))
2859 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
2861 // Work out if spatial resampling is necessary
2862 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, new_height, new_width, group_iiratio);
2864 // 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
2865 projected_bits_perframe = bits_per_frame;
2866 tmp_q = kf_q;
2868 while (tmp_q > cpi->worst_quality)
2870 projected_bits_perframe *= 1.04;
2871 tmp_q--;
2874 // Guess at buffer level at the end of the section
2875 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perframe - av_bits_per_frame) * cpi->frames_to_key);
2877 if (0)
2879 FILE *f = fopen("Subsamle.stt", "a");
2880 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);
2881 fclose(f);
2884 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
2885 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2887 // Trigger resample if we are projected to fall below down sample level or
2888 // resampled last time and are projected to remain below the up sample level
2889 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
2890 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
2891 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
2892 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
2893 resample_trigger = TRUE;
2894 else
2895 resample_trigger = FALSE;
2897 else
2899 long long clip_bits = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
2900 long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_level;
2901 long long over_spend2 = cpi->oxcf.starting_buffer_level - projected_buffer_level;
2903 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced
2904 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ...
2905 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits
2906 resample_trigger = TRUE;
2907 else
2908 resample_trigger = FALSE;
2912 if (resample_trigger)
2914 while ((kf_q >= cpi->worst_quality) && (scale_val < 6))
2916 scale_val ++;
2918 cpi->common.vert_scale = vscale_lookup[scale_val];
2919 cpi->common.horiz_scale = hscale_lookup[scale_val];
2921 Scale2Ratio(cpi->common.horiz_scale, &hr, &hs);
2922 Scale2Ratio(cpi->common.vert_scale, &vr, &vs);
2924 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2925 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2927 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
2928 // effective_sizeratio attempts to provide a crude correction for this
2929 effective_size_ratio = (double)(new_width * new_height) / (double)(cpi->oxcf.Width * cpi->oxcf.Height);
2930 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4.0;
2932 // Now try again and see what Q we get with the smaller image size
2933 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_ratio, bits_per_frame, new_height, new_width, group_iiratio);
2935 if (0)
2937 FILE *f = fopen("Subsamle.stt", "a");
2938 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);
2939 fclose(f);
2944 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_height))
2946 cpi->common.Width = new_width;
2947 cpi->common.Height = new_height;
2948 vp8_alloc_compressor_data(cpi);