2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
16 #include "encodeintra.h"
17 #include "vp8/common/setupintrarecon.h"
19 #include "vpx_scale/vpxscale.h"
21 #include "vp8/common/extend.h"
22 #include "vp8/common/systemdependent.h"
23 #include "vpx_scale/yv12extend.h"
24 #include "vpx_mem/vpx_mem.h"
25 #include "vp8/common/swapyv12buffer.h"
28 #include "vp8/common/quant_common.h"
31 //#define OUTPUT_FPF 1
33 #if CONFIG_RUNTIME_CPU_DETECT
34 #define IF_RTCD(x) (x)
36 #define IF_RTCD(x) NULL
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
, int_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
];
53 #define IIKFACTOR1 1.40
54 #define IIKFACTOR2 1.5
58 #define KF_MB_INTRA_MIN 300
59 #define GF_MB_INTRA_MIN 200
61 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
63 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
64 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
66 static int vscale_lookup
[7] = {0, 1, 1, 2, 2, 3, 3};
67 static int hscale_lookup
[7] = {0, 0, 1, 1, 2, 2, 3};
70 static const int cq_level
[QINDEX_RANGE
] =
72 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
73 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
74 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
75 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
76 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
77 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
78 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
79 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
82 static void find_next_key_frame(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
);
84 // Resets the first pass file to the given position using a relative seek from the current position
85 static void reset_fpf_position(VP8_COMP
*cpi
, FIRSTPASS_STATS
*Position
)
87 cpi
->twopass
.stats_in
= Position
;
90 static int lookup_next_frame_stats(VP8_COMP
*cpi
, FIRSTPASS_STATS
*next_frame
)
92 if (cpi
->twopass
.stats_in
>= cpi
->twopass
.stats_in_end
)
95 *next_frame
= *cpi
->twopass
.stats_in
;
99 // Calculate a modified Error used in distributing bits between easier and harder frames
100 static double calculate_modified_err(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
102 double av_err
= cpi
->twopass
.total_stats
->ssim_weighted_pred_err
;
103 double this_err
= this_frame
->ssim_weighted_pred_err
;
106 //double relative_next_iiratio;
107 //double next_iiratio;
108 //double sum_iiratio;
111 //FIRSTPASS_STATS next_frame;
112 //FIRSTPASS_STATS *start_pos;
114 /*start_pos = cpi->twopass.stats_in;
117 while ( (i < 1) && input_stats(cpi,&next_frame) != EOF )
120 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
121 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio;
122 sum_iiratio += next_iiratio;
127 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->twopass.avg_iiratio * (double)i);
131 relative_next_iiratio = 1.0;
133 reset_fpf_position(cpi, start_pos);*/
135 if (this_err
> av_err
)
136 modified_err
= av_err
* pow((this_err
/ DOUBLE_DIVIDE_CHECK(av_err
)), POW1
);
138 modified_err
= av_err
* pow((this_err
/ DOUBLE_DIVIDE_CHECK(av_err
)), POW2
);
141 relative_next_iiratio = pow(relative_next_iiratio,0.25);
142 modified_err = modified_err * relative_next_iiratio;
148 static const double weight_table
[256] = {
149 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
150 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
151 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
152 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
153 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
154 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
155 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
156 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
157 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
158 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
159 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
160 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
161 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
162 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
163 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
164 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
165 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
166 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
167 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
168 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
169 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
170 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
171 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
172 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
173 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
174 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
175 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
176 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
177 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
178 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
179 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
180 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
183 static double simple_weight(YV12_BUFFER_CONFIG
*source
)
187 unsigned char *src
= source
->y_buffer
;
188 double sum_weights
= 0.0;
190 // Loop throught the Y plane raw examining levels and creating a weight for the image
191 i
= source
->y_height
;
197 sum_weights
+= weight_table
[ *src
];
200 src
-= source
->y_width
;
201 src
+= source
->y_stride
;
204 sum_weights
/= (source
->y_height
* source
->y_width
);
210 // This function returns the current per frame maximum bitrate target
211 static int frame_max_bits(VP8_COMP
*cpi
)
213 // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
216 // For CBR we need to also consider buffer fullness.
217 // If we are running below the optimal level then we need to gradually tighten up on max_bits.
218 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
220 double buffer_fullness_ratio
= (double)cpi
->buffer_level
/ DOUBLE_DIVIDE_CHECK((double)cpi
->oxcf
.optimal_buffer_level
);
222 // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
223 max_bits
= (int)(cpi
->av_per_frame_bandwidth
* ((double)cpi
->oxcf
.two_pass_vbrmax_section
/ 100.0));
225 // If our buffer is below the optimum level
226 if (buffer_fullness_ratio
< 1.0)
228 // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
229 int min_max_bits
= ((cpi
->av_per_frame_bandwidth
>> 2) < (max_bits
>> 2)) ? cpi
->av_per_frame_bandwidth
>> 2 : max_bits
>> 2;
231 max_bits
= (int)(max_bits
* buffer_fullness_ratio
);
233 if (max_bits
< min_max_bits
)
234 max_bits
= min_max_bits
; // Lowest value we will set ... which should allow the buffer to refil.
240 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
241 max_bits
= (int)(((double)cpi
->twopass
.bits_left
/ (cpi
->twopass
.total_stats
->count
- (double)cpi
->common
.current_video_frame
)) * ((double)cpi
->oxcf
.two_pass_vbrmax_section
/ 100.0));
244 // Trap case where we are out of bits
252 static void output_stats(const VP8_COMP
*cpi
,
253 struct vpx_codec_pkt_list
*pktlist
,
254 FIRSTPASS_STATS
*stats
)
256 struct vpx_codec_cx_pkt pkt
;
257 pkt
.kind
= VPX_CODEC_STATS_PKT
;
258 pkt
.data
.twopass_stats
.buf
= stats
;
259 pkt
.data
.twopass_stats
.sz
= sizeof(FIRSTPASS_STATS
);
260 vpx_codec_pkt_list_add(pktlist
, &pkt
);
267 fpfile
= fopen("firstpass.stt", "a");
269 fprintf(fpfile
, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
270 " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
275 stats
->ssim_weighted_pred_err
,
278 stats
->pcnt_second_ref
,
286 stats
->mv_in_out_count
,
294 static int input_stats(VP8_COMP
*cpi
, FIRSTPASS_STATS
*fps
)
296 if (cpi
->twopass
.stats_in
>= cpi
->twopass
.stats_in_end
)
299 *fps
= *cpi
->twopass
.stats_in
;
300 cpi
->twopass
.stats_in
=
301 (void*)((char *)cpi
->twopass
.stats_in
+ sizeof(FIRSTPASS_STATS
));
305 static void zero_stats(FIRSTPASS_STATS
*section
)
307 section
->frame
= 0.0;
308 section
->intra_error
= 0.0;
309 section
->coded_error
= 0.0;
310 section
->ssim_weighted_pred_err
= 0.0;
311 section
->pcnt_inter
= 0.0;
312 section
->pcnt_motion
= 0.0;
313 section
->pcnt_second_ref
= 0.0;
314 section
->pcnt_neutral
= 0.0;
316 section
->mvr_abs
= 0.0;
318 section
->mvc_abs
= 0.0;
321 section
->mv_in_out_count
= 0.0;
322 section
->count
= 0.0;
323 section
->duration
= 1.0;
325 static void accumulate_stats(FIRSTPASS_STATS
*section
, FIRSTPASS_STATS
*frame
)
327 section
->frame
+= frame
->frame
;
328 section
->intra_error
+= frame
->intra_error
;
329 section
->coded_error
+= frame
->coded_error
;
330 section
->ssim_weighted_pred_err
+= frame
->ssim_weighted_pred_err
;
331 section
->pcnt_inter
+= frame
->pcnt_inter
;
332 section
->pcnt_motion
+= frame
->pcnt_motion
;
333 section
->pcnt_second_ref
+= frame
->pcnt_second_ref
;
334 section
->pcnt_neutral
+= frame
->pcnt_neutral
;
335 section
->MVr
+= frame
->MVr
;
336 section
->mvr_abs
+= frame
->mvr_abs
;
337 section
->MVc
+= frame
->MVc
;
338 section
->mvc_abs
+= frame
->mvc_abs
;
339 section
->MVrv
+= frame
->MVrv
;
340 section
->MVcv
+= frame
->MVcv
;
341 section
->mv_in_out_count
+= frame
->mv_in_out_count
;
342 section
->count
+= frame
->count
;
343 section
->duration
+= frame
->duration
;
345 static void avg_stats(FIRSTPASS_STATS
*section
)
347 if (section
->count
< 1.0)
350 section
->intra_error
/= section
->count
;
351 section
->coded_error
/= section
->count
;
352 section
->ssim_weighted_pred_err
/= section
->count
;
353 section
->pcnt_inter
/= section
->count
;
354 section
->pcnt_second_ref
/= section
->count
;
355 section
->pcnt_neutral
/= section
->count
;
356 section
->pcnt_motion
/= section
->count
;
357 section
->MVr
/= section
->count
;
358 section
->mvr_abs
/= section
->count
;
359 section
->MVc
/= section
->count
;
360 section
->mvc_abs
/= section
->count
;
361 section
->MVrv
/= section
->count
;
362 section
->MVcv
/= section
->count
;
363 section
->mv_in_out_count
/= section
->count
;
364 section
->duration
/= section
->count
;
367 void vp8_init_first_pass(VP8_COMP
*cpi
)
369 zero_stats(cpi
->twopass
.total_stats
);
372 void vp8_end_first_pass(VP8_COMP
*cpi
)
374 output_stats(cpi
, cpi
->output_pkt_list
, cpi
->twopass
.total_stats
);
377 static void zz_motion_search( VP8_COMP
*cpi
, MACROBLOCK
* x
, YV12_BUFFER_CONFIG
* recon_buffer
, int * best_motion_err
, int recon_yoffset
)
379 MACROBLOCKD
* const xd
= & x
->e_mbd
;
380 BLOCK
*b
= &x
->block
[0];
381 BLOCKD
*d
= &x
->e_mbd
.block
[0];
383 unsigned char *src_ptr
= (*(b
->base_src
) + b
->src
);
384 int src_stride
= b
->src_stride
;
385 unsigned char *ref_ptr
;
386 int ref_stride
=d
->pre_stride
;
388 // Set up pointers for this macro block recon buffer
389 xd
->pre
.y_buffer
= recon_buffer
->y_buffer
+ recon_yoffset
;
391 ref_ptr
= (unsigned char *)(*(d
->base_pre
) + d
->pre
);
393 VARIANCE_INVOKE(IF_RTCD(&cpi
->rtcd
.variance
), mse16x16
) ( src_ptr
, src_stride
, ref_ptr
, ref_stride
, (unsigned int *)(best_motion_err
));
396 static void first_pass_motion_search(VP8_COMP
*cpi
, MACROBLOCK
*x
,
397 int_mv
*ref_mv
, MV
*best_mv
,
398 YV12_BUFFER_CONFIG
*recon_buffer
,
399 int *best_motion_err
, int recon_yoffset
)
401 MACROBLOCKD
*const xd
= & x
->e_mbd
;
402 BLOCK
*b
= &x
->block
[0];
403 BLOCKD
*d
= &x
->e_mbd
.block
[0];
409 int step_param
= 3; //3; // Dont search over full range for first pass
410 int further_steps
= (MAX_MVSEARCH_STEPS
- 1) - step_param
; //3;
412 vp8_variance_fn_ptr_t v_fn_ptr
= cpi
->fn_ptr
[BLOCK_16X16
];
413 int new_mv_mode_penalty
= 256;
415 // override the default variance function to use MSE
416 v_fn_ptr
.vf
= VARIANCE_INVOKE(IF_RTCD(&cpi
->rtcd
.variance
), mse16x16
);
418 // Set up pointers for this macro block recon buffer
419 xd
->pre
.y_buffer
= recon_buffer
->y_buffer
+ recon_yoffset
;
421 // Initial step/diamond search centred on best mv
423 tmp_err
= cpi
->diamond_search_sad(x
, b
, d
, ref_mv
, &tmp_mv
, step_param
,
424 x
->sadperbit16
, &num00
, &v_fn_ptr
,
426 if ( tmp_err
< INT_MAX
-new_mv_mode_penalty
)
427 tmp_err
+= new_mv_mode_penalty
;
429 if (tmp_err
< *best_motion_err
)
431 *best_motion_err
= tmp_err
;
432 best_mv
->row
= tmp_mv
.as_mv
.row
;
433 best_mv
->col
= tmp_mv
.as_mv
.col
;
436 // Further step/diamond searches as necessary
440 while (n
< further_steps
)
448 tmp_err
= cpi
->diamond_search_sad(x
, b
, d
, ref_mv
, &tmp_mv
,
449 step_param
+ n
, x
->sadperbit16
,
450 &num00
, &v_fn_ptr
, x
->mvcost
,
452 if ( tmp_err
< INT_MAX
-new_mv_mode_penalty
)
453 tmp_err
+= new_mv_mode_penalty
;
455 if (tmp_err
< *best_motion_err
)
457 *best_motion_err
= tmp_err
;
458 best_mv
->row
= tmp_mv
.as_mv
.row
;
459 best_mv
->col
= tmp_mv
.as_mv
.col
;
465 void vp8_first_pass(VP8_COMP
*cpi
)
468 MACROBLOCK
*const x
= & cpi
->mb
;
469 VP8_COMMON
*const cm
= & cpi
->common
;
470 MACROBLOCKD
*const xd
= & x
->e_mbd
;
472 int recon_yoffset
, recon_uvoffset
;
473 YV12_BUFFER_CONFIG
*lst_yv12
= &cm
->yv12_fb
[cm
->lst_fb_idx
];
474 YV12_BUFFER_CONFIG
*new_yv12
= &cm
->yv12_fb
[cm
->new_fb_idx
];
475 YV12_BUFFER_CONFIG
*gld_yv12
= &cm
->yv12_fb
[cm
->gld_fb_idx
];
476 int recon_y_stride
= lst_yv12
->y_stride
;
477 int recon_uv_stride
= lst_yv12
->uv_stride
;
478 long long intra_error
= 0;
479 long long coded_error
= 0;
481 int sum_mvr
= 0, sum_mvc
= 0;
482 int sum_mvr_abs
= 0, sum_mvc_abs
= 0;
483 int sum_mvrs
= 0, sum_mvcs
= 0;
486 int second_ref_count
= 0;
487 int intrapenalty
= 256;
488 int neutral_count
= 0;
490 int sum_in_vectors
= 0;
494 zero_ref_mv
.as_int
= 0;
496 vp8_clear_system_state(); //__asm emms;
498 x
->src
= * cpi
->Source
;
502 x
->partition_info
= x
->pi
;
504 xd
->mode_info_context
= cm
->mi
;
506 vp8_build_block_offsets(x
);
508 vp8_setup_block_dptrs(&x
->e_mbd
);
510 vp8_setup_block_ptrs(x
);
512 // set up frame new frame for intra coded blocks
513 vp8_setup_intra_recon(new_yv12
);
514 vp8cx_frame_init_quantizer(cpi
);
516 // Initialise the MV cost table to the defaults
517 //if( cm->current_video_frame == 0)
520 int flag
[2] = {1, 1};
521 vp8_initialize_rd_consts(cpi
, vp8_dc_quant(cm
->base_qindex
, cm
->y1dc_delta_q
));
522 vpx_memcpy(cm
->fc
.mvc
, vp8_default_mv_context
, sizeof(vp8_default_mv_context
));
523 vp8_build_component_cost_table(cpi
->mb
.mvcost
, (const MV_CONTEXT
*) cm
->fc
.mvc
, flag
);
526 // for each macroblock row in image
527 for (mb_row
= 0; mb_row
< cm
->mb_rows
; mb_row
++)
531 best_ref_mv
.as_int
= 0;
533 // reset above block coeffs
534 xd
->up_available
= (mb_row
!= 0);
535 recon_yoffset
= (mb_row
* recon_y_stride
* 16);
536 recon_uvoffset
= (mb_row
* recon_uv_stride
* 8);
538 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
539 x
->mv_row_min
= -((mb_row
* 16) + (VP8BORDERINPIXELS
- 16));
540 x
->mv_row_max
= ((cm
->mb_rows
- 1 - mb_row
) * 16) + (VP8BORDERINPIXELS
- 16);
543 // for each macroblock col in image
544 for (mb_col
= 0; mb_col
< cm
->mb_cols
; mb_col
++)
547 int gf_motion_error
= INT_MAX
;
548 int use_dc_pred
= (mb_col
|| mb_row
) && (!mb_col
|| !mb_row
);
550 xd
->dst
.y_buffer
= new_yv12
->y_buffer
+ recon_yoffset
;
551 xd
->dst
.u_buffer
= new_yv12
->u_buffer
+ recon_uvoffset
;
552 xd
->dst
.v_buffer
= new_yv12
->v_buffer
+ recon_uvoffset
;
553 xd
->left_available
= (mb_col
!= 0);
555 // do intra 16x16 prediction
556 this_error
= vp8_encode_intra(cpi
, x
, use_dc_pred
);
558 // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
559 // 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.
560 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
561 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
562 this_error
+= intrapenalty
;
564 // Cumulative intra error total
565 intra_error
+= (long long)this_error
;
567 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
568 x
->mv_col_min
= -((mb_col
* 16) + (VP8BORDERINPIXELS
- 16));
569 x
->mv_col_max
= ((cm
->mb_cols
- 1 - mb_col
) * 16) + (VP8BORDERINPIXELS
- 16);
571 // Other than for the first frame do a motion search
572 if (cm
->current_video_frame
> 0)
574 BLOCKD
*d
= &x
->e_mbd
.block
[0];
577 int motion_error
= INT_MAX
;
579 // Simple 0,0 motion with no mv overhead
580 zz_motion_search( cpi
, x
, lst_yv12
, &motion_error
, recon_yoffset
);
581 d
->bmi
.mv
.as_mv
.row
= 0;
582 d
->bmi
.mv
.as_mv
.col
= 0;
584 // Test last reference frame using the previous best mv as the
585 // starting point (best reference) for the search
586 first_pass_motion_search(cpi
, x
, &best_ref_mv
,
587 &d
->bmi
.mv
.as_mv
, lst_yv12
,
588 &motion_error
, recon_yoffset
);
590 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
591 if (best_ref_mv
.as_int
)
594 first_pass_motion_search(cpi
, x
, &zero_ref_mv
, &tmp_mv
,
595 lst_yv12
, &tmp_err
, recon_yoffset
);
597 if ( tmp_err
< motion_error
)
599 motion_error
= tmp_err
;
600 d
->bmi
.mv
.as_mv
.row
= tmp_mv
.row
;
601 d
->bmi
.mv
.as_mv
.col
= tmp_mv
.col
;
605 // Experimental search in a second reference frame ((0,0) based only)
606 if (cm
->current_video_frame
> 1)
608 first_pass_motion_search(cpi
, x
, &zero_ref_mv
, &tmp_mv
, gld_yv12
, &gf_motion_error
, recon_yoffset
);
610 if ((gf_motion_error
< motion_error
) && (gf_motion_error
< this_error
))
613 //motion_error = gf_motion_error;
614 //d->bmi.mv.as_mv.row = tmp_mv.row;
615 //d->bmi.mv.as_mv.col = tmp_mv.col;
619 xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
620 xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
621 xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
625 // Reset to last frame as reference buffer
626 xd
->pre
.y_buffer
= lst_yv12
->y_buffer
+ recon_yoffset
;
627 xd
->pre
.u_buffer
= lst_yv12
->u_buffer
+ recon_uvoffset
;
628 xd
->pre
.v_buffer
= lst_yv12
->v_buffer
+ recon_uvoffset
;
631 /* Intra assumed best */
632 best_ref_mv
.as_int
= 0;
634 if (motion_error
<= this_error
)
636 // Keep a count of cases where the inter and intra were
637 // very close and very low. This helps with scene cut
638 // detection for example in cropped clips with black bars
639 // at the sides or top and bottom.
640 if( (((this_error
-intrapenalty
) * 9) <=
641 (motion_error
*10)) &&
642 (this_error
< (2*intrapenalty
)) )
647 d
->bmi
.mv
.as_mv
.row
<<= 3;
648 d
->bmi
.mv
.as_mv
.col
<<= 3;
649 this_error
= motion_error
;
650 vp8_set_mbmode_and_mvs(x
, NEWMV
, &d
->bmi
.mv
);
651 vp8_encode_inter16x16y(IF_RTCD(&cpi
->rtcd
), x
);
652 sum_mvr
+= d
->bmi
.mv
.as_mv
.row
;
653 sum_mvr_abs
+= abs(d
->bmi
.mv
.as_mv
.row
);
654 sum_mvc
+= d
->bmi
.mv
.as_mv
.col
;
655 sum_mvc_abs
+= abs(d
->bmi
.mv
.as_mv
.col
);
656 sum_mvrs
+= d
->bmi
.mv
.as_mv
.row
* d
->bmi
.mv
.as_mv
.row
;
657 sum_mvcs
+= d
->bmi
.mv
.as_mv
.col
* d
->bmi
.mv
.as_mv
.col
;
660 best_ref_mv
.as_int
= d
->bmi
.mv
.as_int
;
662 // Was the vector non-zero
663 if (d
->bmi
.mv
.as_int
)
667 // Does the Row vector point inwards or outwards
668 if (mb_row
< cm
->mb_rows
/ 2)
670 if (d
->bmi
.mv
.as_mv
.row
> 0)
672 else if (d
->bmi
.mv
.as_mv
.row
< 0)
675 else if (mb_row
> cm
->mb_rows
/ 2)
677 if (d
->bmi
.mv
.as_mv
.row
> 0)
679 else if (d
->bmi
.mv
.as_mv
.row
< 0)
683 // Does the Row vector point inwards or outwards
684 if (mb_col
< cm
->mb_cols
/ 2)
686 if (d
->bmi
.mv
.as_mv
.col
> 0)
688 else if (d
->bmi
.mv
.as_mv
.col
< 0)
691 else if (mb_col
> cm
->mb_cols
/ 2)
693 if (d
->bmi
.mv
.as_mv
.col
> 0)
695 else if (d
->bmi
.mv
.as_mv
.col
< 0)
702 coded_error
+= (long long)this_error
;
704 // adjust to the next column of macroblocks
705 x
->src
.y_buffer
+= 16;
706 x
->src
.u_buffer
+= 8;
707 x
->src
.v_buffer
+= 8;
713 // adjust to the next row of mbs
714 x
->src
.y_buffer
+= 16 * x
->src
.y_stride
- 16 * cm
->mb_cols
;
715 x
->src
.u_buffer
+= 8 * x
->src
.uv_stride
- 8 * cm
->mb_cols
;
716 x
->src
.v_buffer
+= 8 * x
->src
.uv_stride
- 8 * cm
->mb_cols
;
718 //extend the recon for intra prediction
719 vp8_extend_mb_row(new_yv12
, xd
->dst
.y_buffer
+ 16, xd
->dst
.u_buffer
+ 8, xd
->dst
.v_buffer
+ 8);
720 vp8_clear_system_state(); //__asm emms;
723 vp8_clear_system_state(); //__asm emms;
729 fps
.frame
= cm
->current_video_frame
;
730 fps
.intra_error
= intra_error
>> 8;
731 fps
.coded_error
= coded_error
>> 8;
732 weight
= simple_weight(cpi
->Source
);
738 fps
.ssim_weighted_pred_err
= fps
.coded_error
* weight
;
740 fps
.pcnt_inter
= 0.0;
741 fps
.pcnt_motion
= 0.0;
748 fps
.mv_in_out_count
= 0.0;
751 fps
.pcnt_inter
= 1.0 * (double)intercount
/ cm
->MBs
;
752 fps
.pcnt_second_ref
= 1.0 * (double)second_ref_count
/ cm
->MBs
;
753 fps
.pcnt_neutral
= 1.0 * (double)neutral_count
/ cm
->MBs
;
757 fps
.MVr
= (double)sum_mvr
/ (double)mvcount
;
758 fps
.mvr_abs
= (double)sum_mvr_abs
/ (double)mvcount
;
759 fps
.MVc
= (double)sum_mvc
/ (double)mvcount
;
760 fps
.mvc_abs
= (double)sum_mvc_abs
/ (double)mvcount
;
761 fps
.MVrv
= ((double)sum_mvrs
- (fps
.MVr
* fps
.MVr
/ (double)mvcount
)) / (double)mvcount
;
762 fps
.MVcv
= ((double)sum_mvcs
- (fps
.MVc
* fps
.MVc
/ (double)mvcount
)) / (double)mvcount
;
763 fps
.mv_in_out_count
= (double)sum_in_vectors
/ (double)(mvcount
* 2);
765 fps
.pcnt_motion
= 1.0 * (double)mvcount
/ cpi
->common
.MBs
;
768 // TODO: handle the case when duration is set to 0, or something less
769 // than the full time between subsequent cpi->source_time_stamp s .
770 fps
.duration
= cpi
->source
->ts_end
771 - cpi
->source
->ts_start
;
773 // don't want to do output stats with a stack variable!
774 memcpy(cpi
->twopass
.this_frame_stats
,
776 sizeof(FIRSTPASS_STATS
));
777 output_stats(cpi
, cpi
->output_pkt_list
, cpi
->twopass
.this_frame_stats
);
778 accumulate_stats(cpi
->twopass
.total_stats
, &fps
);
781 // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
782 if ((cm
->current_video_frame
> 0) &&
783 (cpi
->twopass
.this_frame_stats
->pcnt_inter
> 0.20) &&
784 ((cpi
->twopass
.this_frame_stats
->intra_error
/ cpi
->twopass
.this_frame_stats
->coded_error
) > 2.0))
786 vp8_yv12_copy_frame_ptr(lst_yv12
, gld_yv12
);
789 // swap frame pointers so last frame refers to the frame we just compressed
790 vp8_swap_yv12_buffer(lst_yv12
, new_yv12
);
791 vp8_yv12_extend_frame_borders(lst_yv12
);
793 // Special case for the first frame. Copy into the GF buffer as a second reference.
794 if (cm
->current_video_frame
== 0)
796 vp8_yv12_copy_frame_ptr(lst_yv12
, gld_yv12
);
800 // use this to see what the first pass reconstruction looks like
805 sprintf(filename
, "enc%04d.yuv", (int) cm
->current_video_frame
);
807 if (cm
->current_video_frame
== 0)
808 recon_file
= fopen(filename
, "wb");
810 recon_file
= fopen(filename
, "ab");
812 if(fwrite(lst_yv12
->buffer_alloc
, lst_yv12
->frame_size
, 1, recon_file
));
816 cm
->current_video_frame
++;
819 extern const int vp8_bits_per_mb
[2][QINDEX_RANGE
];
821 #define BASE_ERRPERMB 150
822 static int estimate_max_q(VP8_COMP
*cpi
, double section_err
, int section_target_bandwitdh
)
825 int num_mbs
= cpi
->common
.MBs
;
826 int target_norm_bits_per_mb
;
828 double err_per_mb
= section_err
/ num_mbs
;
829 double correction_factor
;
831 double speed_correction
= 1.0;
832 double rolling_ratio
;
834 double pow_highq
= 0.90;
835 double pow_lowq
= 0.40;
837 if (section_target_bandwitdh
<= 0)
838 return cpi
->twopass
.maxq_max_limit
; // Highest value allowed
840 target_norm_bits_per_mb
= (section_target_bandwitdh
< (1 << 20)) ? (512 * section_target_bandwitdh
) / num_mbs
: 512 * (section_target_bandwitdh
/ num_mbs
);
842 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
843 if ((cpi
->rolling_target_bits
> 0.0) && (cpi
->active_worst_quality
< cpi
->worst_quality
))
845 rolling_ratio
= (double)cpi
->rolling_actual_bits
/ (double)cpi
->rolling_target_bits
;
847 //if ( cpi->twopass.est_max_qcorrection_factor > rolling_ratio )
848 if (rolling_ratio
< 0.95)
849 //cpi->twopass.est_max_qcorrection_factor *= adjustment_rate;
850 cpi
->twopass
.est_max_qcorrection_factor
-= 0.005;
851 //else if ( cpi->twopass.est_max_qcorrection_factor < rolling_ratio )
852 else if (rolling_ratio
> 1.05)
853 cpi
->twopass
.est_max_qcorrection_factor
+= 0.005;
855 //cpi->twopass.est_max_qcorrection_factor /= adjustment_rate;
857 cpi
->twopass
.est_max_qcorrection_factor
= (cpi
->twopass
.est_max_qcorrection_factor
< 0.1) ? 0.1 : (cpi
->twopass
.est_max_qcorrection_factor
> 10.0) ? 10.0 : cpi
->twopass
.est_max_qcorrection_factor
;
860 // Corrections for higher compression speed settings (reduced compression expected)
861 if ((cpi
->compressor_speed
== 3) || (cpi
->compressor_speed
== 1))
863 if (cpi
->oxcf
.cpu_used
<= 5)
864 speed_correction
= 1.04 + (cpi
->oxcf
.cpu_used
* 0.04);
866 speed_correction
= 1.25;
869 // Correction factor used for Q values >= 20
870 corr_high
= pow(err_per_mb
/ BASE_ERRPERMB
, pow_highq
);
871 corr_high
= (corr_high
< 0.05)
872 ? 0.05 : (corr_high
> 5.0) ? 5.0 : corr_high
;
874 // Try and pick a max Q that will be high enough to encode the
875 // content at the given rate.
876 for (Q
= cpi
->twopass
.maxq_min_limit
; Q
< cpi
->twopass
.maxq_max_limit
; Q
++)
878 int bits_per_mb_at_this_q
;
882 correction_factor
= pow(err_per_mb
/ BASE_ERRPERMB
, (pow_lowq
+ Q
* 0.01));
883 correction_factor
= (correction_factor
< 0.05) ? 0.05 : (correction_factor
> 5.0) ? 5.0 : correction_factor
;
886 correction_factor
= corr_high
;
888 bits_per_mb_at_this_q
= (int)(.5 + correction_factor
889 * speed_correction
* cpi
->twopass
.est_max_qcorrection_factor
890 * cpi
->twopass
.section_max_qfactor
891 * (double)vp8_bits_per_mb
[INTER_FRAME
][Q
] / 1.0);
892 //bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->twopass.est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
894 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
898 // Restriction on active max q for constrained quality mode.
899 if ( (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
) &&
900 (Q
< cpi
->cq_target_quality
) )
901 //(Q < cpi->oxcf.cq_level;) )
903 Q
= cpi
->cq_target_quality
;
904 //Q = cpi->oxcf.cq_level;
907 // Adjust maxq_min_limit and maxq_max_limit limits based on
908 // averaga q observed in clip for non kf/gf.arf frames
909 // Give average a chance to settle though.
910 if ( (cpi
->ni_frames
>
911 ((unsigned int)cpi
->twopass
.total_stats
->count
>> 8)) &&
912 (cpi
->ni_frames
> 150) )
914 cpi
->twopass
.maxq_max_limit
= ((cpi
->ni_av_qi
+ 32) < cpi
->worst_quality
)
915 ? (cpi
->ni_av_qi
+ 32) : cpi
->worst_quality
;
916 cpi
->twopass
.maxq_min_limit
= ((cpi
->ni_av_qi
- 32) > cpi
->best_quality
)
917 ? (cpi
->ni_av_qi
- 32) : cpi
->best_quality
;
922 static int estimate_q(VP8_COMP
*cpi
, double section_err
, int section_target_bandwitdh
)
925 int num_mbs
= cpi
->common
.MBs
;
926 int target_norm_bits_per_mb
;
928 double err_per_mb
= section_err
/ num_mbs
;
929 double correction_factor
;
931 double speed_correction
= 1.0;
932 double pow_highq
= 0.90;
933 double pow_lowq
= 0.40;
935 target_norm_bits_per_mb
= (section_target_bandwitdh
< (1 << 20)) ? (512 * section_target_bandwitdh
) / num_mbs
: 512 * (section_target_bandwitdh
/ num_mbs
);
937 // Corrections for higher compression speed settings (reduced compression expected)
938 if ((cpi
->compressor_speed
== 3) || (cpi
->compressor_speed
== 1))
940 if (cpi
->oxcf
.cpu_used
<= 5)
941 speed_correction
= 1.04 + (cpi
->oxcf
.cpu_used
* 0.04);
943 speed_correction
= 1.25;
946 // Correction factor used for Q values >= 20
947 corr_high
= pow(err_per_mb
/ BASE_ERRPERMB
, pow_highq
);
948 corr_high
= (corr_high
< 0.05) ? 0.05 : (corr_high
> 5.0) ? 5.0 : corr_high
;
950 // Try and pick a Q that can encode the content at the given rate.
951 for (Q
= 0; Q
< MAXQ
; Q
++)
953 int bits_per_mb_at_this_q
;
957 correction_factor
= pow(err_per_mb
/ BASE_ERRPERMB
, (pow_lowq
+ Q
* 0.01));
958 correction_factor
= (correction_factor
< 0.05) ? 0.05 : (correction_factor
> 5.0) ? 5.0 : correction_factor
;
961 correction_factor
= corr_high
;
963 bits_per_mb_at_this_q
= (int)(.5 + correction_factor
* speed_correction
* cpi
->twopass
.est_max_qcorrection_factor
* (double)vp8_bits_per_mb
[INTER_FRAME
][Q
] / 1.0);
965 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
972 // Estimate a worst case Q for a KF group
973 static int estimate_kf_group_q(VP8_COMP
*cpi
, double section_err
, int section_target_bandwitdh
, double group_iiratio
)
976 int num_mbs
= cpi
->common
.MBs
;
977 int target_norm_bits_per_mb
= (512 * section_target_bandwitdh
) / num_mbs
;
978 int bits_per_mb_at_this_q
;
980 double err_per_mb
= section_err
/ num_mbs
;
981 double err_correction_factor
;
983 double speed_correction
= 1.0;
984 double current_spend_ratio
= 1.0;
986 double pow_highq
= (POW1
< 0.6) ? POW1
+ 0.3 : 0.90;
987 double pow_lowq
= (POW1
< 0.7) ? POW1
+ 0.1 : 0.80;
989 double iiratio_correction_factor
= 1.0;
991 double combined_correction_factor
;
993 // Trap special case where the target is <= 0
994 if (target_norm_bits_per_mb
<= 0)
997 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
998 // This is clamped to the range 0.1 to 10.0
999 if (cpi
->long_rolling_target_bits
<= 0)
1000 current_spend_ratio
= 10.0;
1003 current_spend_ratio
= (double)cpi
->long_rolling_actual_bits
/ (double)cpi
->long_rolling_target_bits
;
1004 current_spend_ratio
= (current_spend_ratio
> 10.0) ? 10.0 : (current_spend_ratio
< 0.1) ? 0.1 : current_spend_ratio
;
1007 // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
1008 // The idea here is to favour subsampling in the hardest sections vs the easyest.
1009 iiratio_correction_factor
= 1.0 - ((group_iiratio
- 6.0) * 0.1);
1011 if (iiratio_correction_factor
< 0.5)
1012 iiratio_correction_factor
= 0.5;
1014 // Corrections for higher compression speed settings (reduced compression expected)
1015 if ((cpi
->compressor_speed
== 3) || (cpi
->compressor_speed
== 1))
1017 if (cpi
->oxcf
.cpu_used
<= 5)
1018 speed_correction
= 1.04 + (cpi
->oxcf
.cpu_used
* 0.04);
1020 speed_correction
= 1.25;
1023 // Combine the various factors calculated above
1024 combined_correction_factor
= speed_correction
* iiratio_correction_factor
* current_spend_ratio
;
1026 // Correction factor used for Q values >= 20
1027 corr_high
= pow(err_per_mb
/ BASE_ERRPERMB
, pow_highq
);
1028 corr_high
= (corr_high
< 0.05) ? 0.05 : (corr_high
> 5.0) ? 5.0 : corr_high
;
1030 // Try and pick a Q that should be high enough to encode the content at the given rate.
1031 for (Q
= 0; Q
< MAXQ
; Q
++)
1033 // Q values < 20 treated as a special case
1036 err_correction_factor
= pow(err_per_mb
/ BASE_ERRPERMB
, (pow_lowq
+ Q
* 0.01));
1037 err_correction_factor
= (err_correction_factor
< 0.05) ? 0.05 : (err_correction_factor
> 5.0) ? 5.0 : err_correction_factor
;
1040 err_correction_factor
= corr_high
;
1042 bits_per_mb_at_this_q
= (int)(.5 + err_correction_factor
* combined_correction_factor
* (double)vp8_bits_per_mb
[INTER_FRAME
][Q
]);
1044 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
1048 // If we could not hit the target even at Max Q then estimate what Q would have bee required
1049 while ((bits_per_mb_at_this_q
> target_norm_bits_per_mb
) && (Q
< (MAXQ
* 2)))
1052 bits_per_mb_at_this_q
= (int)(0.96 * bits_per_mb_at_this_q
);
1058 FILE *f
= fopen("estkf_q.stt", "a");
1059 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
,
1060 target_norm_bits_per_mb
, err_per_mb
, err_correction_factor
,
1061 current_spend_ratio
, group_iiratio
, iiratio_correction_factor
,
1062 (double)cpi
->buffer_level
/ (double)cpi
->oxcf
.optimal_buffer_level
, Q
);
1069 // For cq mode estimate a cq level that matches the observed
1070 // complexity and data rate.
1071 static int estimate_cq(VP8_COMP
*cpi
, double section_err
, int section_target_bandwitdh
)
1074 int num_mbs
= cpi
->common
.MBs
;
1075 int target_norm_bits_per_mb
;
1077 double err_per_mb
= section_err
/ num_mbs
;
1078 double correction_factor
;
1080 double speed_correction
= 1.0;
1081 double pow_highq
= 0.90;
1082 double pow_lowq
= 0.40;
1083 double clip_iiratio
;
1084 double clip_iifactor
;
1086 target_norm_bits_per_mb
= (section_target_bandwitdh
< (1 << 20))
1087 ? (512 * section_target_bandwitdh
) / num_mbs
1088 : 512 * (section_target_bandwitdh
/ num_mbs
);
1090 // Corrections for higher compression speed settings
1091 // (reduced compression expected)
1092 if ((cpi
->compressor_speed
== 3) || (cpi
->compressor_speed
== 1))
1094 if (cpi
->oxcf
.cpu_used
<= 5)
1095 speed_correction
= 1.04 + (cpi
->oxcf
.cpu_used
* 0.04);
1097 speed_correction
= 1.25;
1099 // II ratio correction factor for clip as a whole
1100 clip_iiratio
= cpi
->twopass
.total_stats
->intra_error
/
1101 DOUBLE_DIVIDE_CHECK(cpi
->twopass
.total_stats
->coded_error
);
1102 clip_iifactor
= 1.0 - ((clip_iiratio
- 10.0) * 0.025);
1103 if (clip_iifactor
< 0.80)
1104 clip_iifactor
= 0.80;
1106 // Correction factor used for Q values >= 20
1107 corr_high
= pow(err_per_mb
/ BASE_ERRPERMB
, pow_highq
);
1108 corr_high
= (corr_high
< 0.05) ? 0.05 : (corr_high
> 5.0) ? 5.0 : corr_high
;
1110 // Try and pick a Q that can encode the content at the given rate.
1111 for (Q
= 0; Q
< MAXQ
; Q
++)
1113 int bits_per_mb_at_this_q
;
1118 pow( err_per_mb
/ BASE_ERRPERMB
, (pow_lowq
+ Q
* 0.01));
1120 correction_factor
= (correction_factor
< 0.05) ? 0.05
1121 : (correction_factor
> 5.0) ? 5.0
1122 : correction_factor
;
1125 correction_factor
= corr_high
;
1127 bits_per_mb_at_this_q
=
1128 (int)( .5 + correction_factor
*
1131 (double)vp8_bits_per_mb
[INTER_FRAME
][Q
] / 1.0);
1133 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
1140 extern void vp8_new_frame_rate(VP8_COMP
*cpi
, double framerate
);
1142 void vp8_init_second_pass(VP8_COMP
*cpi
)
1144 FIRSTPASS_STATS this_frame
;
1145 FIRSTPASS_STATS
*start_pos
;
1147 double two_pass_min_rate
= (double)(cpi
->oxcf
.target_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
1149 zero_stats(cpi
->twopass
.total_stats
);
1151 if (!cpi
->twopass
.stats_in_end
)
1154 *cpi
->twopass
.total_stats
= *cpi
->twopass
.stats_in_end
;
1156 cpi
->twopass
.total_error_left
= cpi
->twopass
.total_stats
->ssim_weighted_pred_err
;
1157 cpi
->twopass
.total_intra_error_left
= cpi
->twopass
.total_stats
->intra_error
;
1158 cpi
->twopass
.total_coded_error_left
= cpi
->twopass
.total_stats
->coded_error
;
1159 cpi
->twopass
.start_tot_err_left
= cpi
->twopass
.total_error_left
;
1161 //cpi->twopass.bits_left = (long long)(cpi->twopass.total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1162 //cpi->twopass.bits_left -= (long long)(cpi->twopass.total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1164 // each frame can have a different duration, as the frame rate in the source
1165 // isn't guaranteed to be constant. The frame rate prior to the first frame
1166 // encoded in the second pass is a guess. However the sum duration is not.
1167 // Its calculated based on the actual durations of all frames from the first
1169 vp8_new_frame_rate(cpi
, 10000000.0 * cpi
->twopass
.total_stats
->count
/ cpi
->twopass
.total_stats
->duration
);
1171 cpi
->output_frame_rate
= cpi
->oxcf
.frame_rate
;
1172 cpi
->twopass
.bits_left
= (long long)(cpi
->twopass
.total_stats
->duration
* cpi
->oxcf
.target_bandwidth
/ 10000000.0) ;
1173 cpi
->twopass
.bits_left
-= (long long)(cpi
->twopass
.total_stats
->duration
* two_pass_min_rate
/ 10000000.0);
1174 cpi
->twopass
.clip_bits_total
= cpi
->twopass
.bits_left
;
1176 // Calculate a minimum intra value to be used in determining the IIratio
1177 // scores used in the second pass. We have this minimum to make sure
1178 // that clips that are static but "low complexity" in the intra domain
1179 // are still boosted appropriately for KF/GF/ARF
1180 cpi
->twopass
.kf_intra_err_min
= KF_MB_INTRA_MIN
* cpi
->common
.MBs
;
1181 cpi
->twopass
.gf_intra_err_min
= GF_MB_INTRA_MIN
* cpi
->common
.MBs
;
1183 avg_stats(cpi
->twopass
.total_stats
);
1185 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1187 double sum_iiratio
= 0.0;
1190 start_pos
= cpi
->twopass
.stats_in
; // Note starting "file" position
1192 while (input_stats(cpi
, &this_frame
) != EOF
)
1194 IIRatio
= this_frame
.intra_error
/ DOUBLE_DIVIDE_CHECK(this_frame
.coded_error
);
1195 IIRatio
= (IIRatio
< 1.0) ? 1.0 : (IIRatio
> 20.0) ? 20.0 : IIRatio
;
1196 sum_iiratio
+= IIRatio
;
1199 cpi
->twopass
.avg_iiratio
= sum_iiratio
/ DOUBLE_DIVIDE_CHECK((double)cpi
->twopass
.total_stats
->count
);
1201 // Reset file position
1202 reset_fpf_position(cpi
, start_pos
);
1205 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1206 // used to allocate bits
1208 start_pos
= cpi
->twopass
.stats_in
; // Note starting "file" position
1210 cpi
->twopass
.modified_error_total
= 0.0;
1211 cpi
->twopass
.modified_error_used
= 0.0;
1213 while (input_stats(cpi
, &this_frame
) != EOF
)
1215 cpi
->twopass
.modified_error_total
+= calculate_modified_err(cpi
, &this_frame
);
1217 cpi
->twopass
.modified_error_left
= cpi
->twopass
.modified_error_total
;
1219 reset_fpf_position(cpi
, start_pos
); // Reset file position
1224 void vp8_end_second_pass(VP8_COMP
*cpi
)
1228 // This function gives and estimate of how badly we believe
1229 // the prediction quality is decaying from frame to frame.
1230 static double get_prediction_decay_rate(VP8_COMP
*cpi
, FIRSTPASS_STATS
*next_frame
)
1232 double prediction_decay_rate
;
1233 double motion_decay
;
1234 double motion_pct
= next_frame
->pcnt_motion
;
1237 // Initial basis is the % mbs inter coded
1238 prediction_decay_rate
= next_frame
->pcnt_inter
;
1240 // High % motion -> somewhat higher decay rate
1241 motion_decay
= (1.0 - (motion_pct
/ 20.0));
1242 if (motion_decay
< prediction_decay_rate
)
1243 prediction_decay_rate
= motion_decay
;
1245 // Adjustment to decay rate based on speed of motion
1247 double this_mv_rabs
;
1248 double this_mv_cabs
;
1249 double distance_factor
;
1251 this_mv_rabs
= fabs(next_frame
->mvr_abs
* motion_pct
);
1252 this_mv_cabs
= fabs(next_frame
->mvc_abs
* motion_pct
);
1254 distance_factor
= sqrt((this_mv_rabs
* this_mv_rabs
) +
1255 (this_mv_cabs
* this_mv_cabs
)) / 250.0;
1256 distance_factor
= ((distance_factor
> 1.0)
1257 ? 0.0 : (1.0 - distance_factor
));
1258 if (distance_factor
< prediction_decay_rate
)
1259 prediction_decay_rate
= distance_factor
;
1262 return prediction_decay_rate
;
1265 // Function to test for a condition where a complex transition is followed
1266 // by a static section. For example in slide shows where there is a fade
1267 // between slides. This is to help with more optimal kf and gf positioning.
1268 static int detect_transition_to_still(
1272 double loop_decay_rate
,
1273 double decay_accumulator
)
1275 BOOL trans_to_still
= FALSE
;
1277 // Break clause to detect very still sections after motion
1278 // For example a static image after a fade or other transition
1279 // instead of a clean scene cut.
1280 if ( (frame_interval
> MIN_GF_INTERVAL
) &&
1281 (loop_decay_rate
>= 0.999) &&
1282 (decay_accumulator
< 0.9) )
1285 FIRSTPASS_STATS
* position
= cpi
->twopass
.stats_in
;
1286 FIRSTPASS_STATS tmp_next_frame
;
1289 // Look ahead a few frames to see if static condition
1291 for ( j
= 0; j
< still_interval
; j
++ )
1293 if (EOF
== input_stats(cpi
, &tmp_next_frame
))
1296 decay_rate
= get_prediction_decay_rate(cpi
, &tmp_next_frame
);
1297 if ( decay_rate
< 0.999 )
1300 // Reset file position
1301 reset_fpf_position(cpi
, position
);
1303 // Only if it does do we signal a transition to still
1304 if ( j
== still_interval
)
1305 trans_to_still
= TRUE
;
1308 return trans_to_still
;
1311 // Analyse and define a gf/arf group .
1312 static void define_gf_group(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
1314 FIRSTPASS_STATS next_frame
;
1315 FIRSTPASS_STATS
*start_pos
;
1317 double boost_score
= 0.0;
1318 double old_boost_score
= 0.0;
1319 double gf_group_err
= 0.0;
1320 double gf_first_frame_err
= 0.0;
1321 double mod_frame_err
= 0.0;
1323 double mv_accumulator_rabs
= 0.0;
1324 double mv_accumulator_cabs
= 0.0;
1325 double mv_ratio_accumulator
= 0.0;
1326 double decay_accumulator
= 1.0;
1328 double boost_factor
= IIFACTOR
;
1329 double loop_decay_rate
= 1.00; // Starting decay rate
1331 double this_frame_mv_in_out
= 0.0;
1332 double mv_in_out_accumulator
= 0.0;
1333 double abs_mv_in_out_accumulator
= 0.0;
1334 double mod_err_per_mb_accumulator
= 0.0;
1336 int max_bits
= frame_max_bits(cpi
); // Max for a single frame
1338 unsigned int allow_alt_ref
=
1339 cpi
->oxcf
.play_alternate
&& cpi
->oxcf
.lag_in_frames
;
1341 cpi
->twopass
.gf_group_bits
= 0;
1342 cpi
->twopass
.gf_decay_rate
= 0;
1344 vp8_clear_system_state(); //__asm emms;
1346 start_pos
= cpi
->twopass
.stats_in
;
1348 vpx_memset(&next_frame
, 0, sizeof(next_frame
)); // assure clean
1350 // Preload the stats for the next frame.
1351 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1353 // Note the error of the frame at the start of the group (this will be
1354 // the GF frame error if we code a normal gf
1355 gf_first_frame_err
= mod_frame_err
;
1357 // Special treatment if the current frame is a key frame (which is also
1358 // a gf). If it is then its error score (and hence bit allocation) need
1359 // to be subtracted out from the calculation for the GF group
1360 if (cpi
->common
.frame_type
== KEY_FRAME
)
1361 gf_group_err
-= gf_first_frame_err
;
1363 // Scan forward to try and work out how many frames the next gf group
1364 // should contain and what level of boost is appropriate for the GF
1365 // or ARF that will be coded with the group
1368 while (((i
< cpi
->twopass
.static_scene_max_gf_interval
) ||
1369 ((cpi
->twopass
.frames_to_key
- i
) < MIN_GF_INTERVAL
)) &&
1370 (i
< cpi
->twopass
.frames_to_key
))
1373 double this_frame_mvr_ratio
;
1374 double this_frame_mvc_ratio
;
1375 //double motion_pct = next_frame.pcnt_motion;
1378 i
++; // Increment the loop counter
1380 // Accumulate error score of frames in this gf group
1381 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1383 gf_group_err
+= mod_frame_err
;
1385 mod_err_per_mb_accumulator
+=
1386 mod_frame_err
/ DOUBLE_DIVIDE_CHECK((double)cpi
->common
.MBs
);
1388 if (EOF
== input_stats(cpi
, &next_frame
))
1391 // Accumulate motion stats.
1392 motion_pct
= next_frame
.pcnt_motion
;
1393 mv_accumulator_rabs
+= fabs(next_frame
.mvr_abs
* motion_pct
);
1394 mv_accumulator_cabs
+= fabs(next_frame
.mvc_abs
* motion_pct
);
1396 //Accumulate Motion In/Out of frame stats
1397 this_frame_mv_in_out
=
1398 next_frame
.mv_in_out_count
* motion_pct
;
1399 mv_in_out_accumulator
+=
1400 next_frame
.mv_in_out_count
* motion_pct
;
1401 abs_mv_in_out_accumulator
+=
1402 fabs(next_frame
.mv_in_out_count
* motion_pct
);
1404 // If there is a significant amount of motion
1405 if (motion_pct
> 0.05)
1407 this_frame_mvr_ratio
= fabs(next_frame
.mvr_abs
) /
1408 DOUBLE_DIVIDE_CHECK(fabs(next_frame
.MVr
));
1410 this_frame_mvc_ratio
= fabs(next_frame
.mvc_abs
) /
1411 DOUBLE_DIVIDE_CHECK(fabs(next_frame
.MVc
));
1413 mv_ratio_accumulator
+=
1414 (this_frame_mvr_ratio
< next_frame
.mvr_abs
)
1415 ? (this_frame_mvr_ratio
* motion_pct
)
1416 : next_frame
.mvr_abs
* motion_pct
;
1418 mv_ratio_accumulator
+=
1419 (this_frame_mvc_ratio
< next_frame
.mvc_abs
)
1420 ? (this_frame_mvc_ratio
* motion_pct
)
1421 : next_frame
.mvc_abs
* motion_pct
;
1425 mv_ratio_accumulator
+= 0.0;
1426 this_frame_mvr_ratio
= 1.0;
1427 this_frame_mvc_ratio
= 1.0;
1430 // Underlying boost factor is based on inter intra error ratio
1431 r
= ( boost_factor
*
1432 ( next_frame
.intra_error
/
1433 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
)));
1435 if (next_frame
.intra_error
> cpi
->twopass
.gf_intra_err_min
)
1436 r
= (IIKFACTOR2
* next_frame
.intra_error
/
1437 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
));
1439 r
= (IIKFACTOR2
* cpi
->twopass
.gf_intra_err_min
/
1440 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
));
1442 // Increase boost for frames where new data coming into frame
1443 // (eg zoom out). Slightly reduce boost if there is a net balance
1444 // of motion out of the frame (zoom in).
1445 // The range for this_frame_mv_in_out is -1.0 to +1.0
1446 if (this_frame_mv_in_out
> 0.0)
1447 r
+= r
* (this_frame_mv_in_out
* 2.0);
1448 // In extreme case boost is halved
1450 r
+= r
* (this_frame_mv_in_out
/ 2.0);
1455 loop_decay_rate
= get_prediction_decay_rate(cpi
, &next_frame
);
1457 // Cumulative effect of decay
1458 decay_accumulator
= decay_accumulator
* loop_decay_rate
;
1459 decay_accumulator
= decay_accumulator
< 0.1 ? 0.1 : decay_accumulator
;
1461 boost_score
+= (decay_accumulator
* r
);
1463 // Break clause to detect very still sections after motion
1464 // For example a staic image after a fade or other transition.
1465 if ( detect_transition_to_still( cpi
, i
, 5,
1466 loop_decay_rate
, decay_accumulator
) )
1468 allow_alt_ref
= FALSE
;
1469 boost_score
= old_boost_score
;
1473 // Break out conditions.
1475 // Break at cpi->max_gf_interval unless almost totally static
1476 (i
>= cpi
->max_gf_interval
&& (decay_accumulator
< 0.995)) ||
1478 // Dont break out with a very short interval
1479 (i
> MIN_GF_INTERVAL
) &&
1480 // Dont break out very close to a key frame
1481 ((cpi
->twopass
.frames_to_key
- i
) >= MIN_GF_INTERVAL
) &&
1482 ((boost_score
> 20.0) || (next_frame
.pcnt_inter
< 0.75)) &&
1483 ((mv_ratio_accumulator
> 100.0) ||
1484 (abs_mv_in_out_accumulator
> 3.0) ||
1485 (mv_in_out_accumulator
< -2.0) ||
1486 ((boost_score
- old_boost_score
) < 2.0))
1489 boost_score
= old_boost_score
;
1493 vpx_memcpy(this_frame
, &next_frame
, sizeof(*this_frame
));
1495 old_boost_score
= boost_score
;
1498 cpi
->twopass
.gf_decay_rate
=
1499 (i
> 0) ? (int)(100.0 * (1.0 - decay_accumulator
)) / i
: 0;
1501 // When using CBR apply additional buffer related upper limits
1502 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
1506 // For cbr apply buffer related limits
1507 if (cpi
->drop_frames_allowed
)
1509 int df_buffer_level
= cpi
->oxcf
.drop_frames_water_mark
*
1510 (cpi
->oxcf
.optimal_buffer_level
/ 100);
1512 if (cpi
->buffer_level
> df_buffer_level
)
1513 max_boost
= ((double)((cpi
->buffer_level
- df_buffer_level
) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
1517 else if (cpi
->buffer_level
> 0)
1519 max_boost
= ((double)(cpi
->buffer_level
* 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
1526 if (boost_score
> max_boost
)
1527 boost_score
= max_boost
;
1530 cpi
->gfu_boost
= (int)(boost_score
* 100.0) >> 4;
1532 // Dont allow conventional gf too near the next kf
1533 if ((cpi
->twopass
.frames_to_key
- i
) < MIN_GF_INTERVAL
)
1535 while (i
< cpi
->twopass
.frames_to_key
)
1539 if (EOF
== input_stats(cpi
, this_frame
))
1542 if (i
< cpi
->twopass
.frames_to_key
)
1544 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1545 gf_group_err
+= mod_frame_err
;
1550 // Should we use the alternate refernce frame
1551 if (allow_alt_ref
&&
1552 (i
>= MIN_GF_INTERVAL
) &&
1553 // dont use ARF very near next kf
1554 (i
<= (cpi
->twopass
.frames_to_key
- MIN_GF_INTERVAL
)) &&
1555 (((next_frame
.pcnt_inter
> 0.75) &&
1556 ((mv_in_out_accumulator
/ (double)i
> -0.2) || (mv_in_out_accumulator
> -2.0)) &&
1557 //(cpi->gfu_boost>150) &&
1558 (cpi
->gfu_boost
> 100) &&
1559 //(cpi->gfu_boost>AF_THRESH2) &&
1560 //((cpi->gfu_boost/i)>AF_THRESH) &&
1561 //(decay_accumulator > 0.5) &&
1562 (cpi
->twopass
.gf_decay_rate
<= (ARF_DECAY_THRESH
+ (cpi
->gfu_boost
/ 200)))
1568 int allocation_chunks
;
1569 int Q
= (cpi
->oxcf
.fixed_q
< 0) ? cpi
->last_q
[INTER_FRAME
] : cpi
->oxcf
.fixed_q
;
1571 int arf_frame_bits
= 0;
1574 // Estimate the bits to be allocated to the group as a whole
1575 if ((cpi
->twopass
.kf_group_bits
> 0) && (cpi
->twopass
.kf_group_error_left
> 0))
1576 group_bits
= (int)((double)cpi
->twopass
.kf_group_bits
* (gf_group_err
/ (double)cpi
->twopass
.kf_group_error_left
));
1580 // Boost for arf frame
1581 Boost
= (cpi
->gfu_boost
* 3 * GFQ_ADJUSTMENT
) / (2 * 100);
1583 allocation_chunks
= (i
* 100) + Boost
;
1585 // Normalize Altboost and allocations chunck down to prevent overflow
1586 while (Boost
> 1000)
1589 allocation_chunks
/= 2;
1592 // Calculate the number of bits to be spent on the arf based on the boost number
1593 arf_frame_bits
= (int)((double)Boost
* (group_bits
/ (double)allocation_chunks
));
1595 // Estimate if there are enough bits available to make worthwhile use of an arf.
1596 tmp_q
= estimate_q(cpi
, mod_frame_err
, (int)arf_frame_bits
);
1598 // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames.
1599 if (tmp_q
< cpi
->worst_quality
)
1602 int frames_after_arf
;
1603 int frames_bwd
= cpi
->oxcf
.arnr_max_frames
- 1;
1604 int frames_fwd
= cpi
->oxcf
.arnr_max_frames
- 1;
1606 cpi
->source_alt_ref_pending
= TRUE
;
1608 // 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
1609 // 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)
1610 gf_group_err
-= mod_frame_err
;
1612 // 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.
1613 // The future frame itself is part of the next group
1614 cpi
->baseline_gf_interval
= i
- 1;
1616 // Define the arnr filter width for this group of frames:
1617 // We only filter frames that lie within a distance of half
1618 // the GF interval from the ARF frame. We also have to trap
1619 // cases where the filter extends beyond the end of clip.
1620 // Note: this_frame->frame has been updated in the loop
1621 // so it now points at the ARF frame.
1622 half_gf_int
= cpi
->baseline_gf_interval
>> 1;
1623 frames_after_arf
= cpi
->twopass
.total_stats
->count
- this_frame
->frame
- 1;
1625 switch (cpi
->oxcf
.arnr_type
)
1627 case 1: // Backward filter
1629 if (frames_bwd
> half_gf_int
)
1630 frames_bwd
= half_gf_int
;
1633 case 2: // Forward filter
1634 if (frames_fwd
> half_gf_int
)
1635 frames_fwd
= half_gf_int
;
1636 if (frames_fwd
> frames_after_arf
)
1637 frames_fwd
= frames_after_arf
;
1641 case 3: // Centered filter
1644 if (frames_fwd
> frames_after_arf
)
1645 frames_fwd
= frames_after_arf
;
1646 if (frames_fwd
> half_gf_int
)
1647 frames_fwd
= half_gf_int
;
1649 frames_bwd
= frames_fwd
;
1651 // For even length filter there is one more frame backward
1652 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1653 if (frames_bwd
< half_gf_int
)
1654 frames_bwd
+= (cpi
->oxcf
.arnr_max_frames
+1) & 0x1;
1658 cpi
->active_arnr_frames
= frames_bwd
+ 1 + frames_fwd
;
1662 cpi
->source_alt_ref_pending
= FALSE
;
1663 cpi
->baseline_gf_interval
= i
;
1668 cpi
->source_alt_ref_pending
= FALSE
;
1669 cpi
->baseline_gf_interval
= i
;
1672 // Now decide how many bits should be allocated to the GF group as a proportion of those remaining in the kf group.
1673 // The final key frame group in the clip is treated as a special case where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
1674 // This is also important for short clips where there may only be one key frame.
1675 if (cpi
->twopass
.frames_to_key
>= (int)(cpi
->twopass
.total_stats
->count
- cpi
->common
.current_video_frame
))
1677 cpi
->twopass
.kf_group_bits
= (cpi
->twopass
.bits_left
> 0) ? cpi
->twopass
.bits_left
: 0;
1680 // Calculate the bits to be allocated to the group as a whole
1681 if ((cpi
->twopass
.kf_group_bits
> 0) && (cpi
->twopass
.kf_group_error_left
> 0))
1682 cpi
->twopass
.gf_group_bits
= (int)((double)cpi
->twopass
.kf_group_bits
* (gf_group_err
/ (double)cpi
->twopass
.kf_group_error_left
));
1684 cpi
->twopass
.gf_group_bits
= 0;
1686 cpi
->twopass
.gf_group_bits
= (cpi
->twopass
.gf_group_bits
< 0) ? 0 : (cpi
->twopass
.gf_group_bits
> cpi
->twopass
.kf_group_bits
) ? cpi
->twopass
.kf_group_bits
: cpi
->twopass
.gf_group_bits
;
1688 // Clip cpi->twopass.gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section)
1689 if (cpi
->twopass
.gf_group_bits
> max_bits
* cpi
->baseline_gf_interval
)
1690 cpi
->twopass
.gf_group_bits
= max_bits
* cpi
->baseline_gf_interval
;
1692 // Reset the file position
1693 reset_fpf_position(cpi
, start_pos
);
1695 // Update the record of error used so far (only done once per gf group)
1696 cpi
->twopass
.modified_error_used
+= gf_group_err
;
1698 // Assign bits to the arf or gf.
1699 for (i
= 0; i
<= (cpi
->source_alt_ref_pending
&& cpi
->common
.frame_type
!= KEY_FRAME
); i
++) {
1701 int frames_in_section
;
1702 int allocation_chunks
;
1703 int Q
= (cpi
->oxcf
.fixed_q
< 0) ? cpi
->last_q
[INTER_FRAME
] : cpi
->oxcf
.fixed_q
;
1707 if (cpi
->source_alt_ref_pending
&& i
== 0)
1709 Boost
= (cpi
->gfu_boost
* 3 * GFQ_ADJUSTMENT
) / (2 * 100);
1710 //Boost += (cpi->baseline_gf_interval * 25);
1711 Boost
+= (cpi
->baseline_gf_interval
* 50);
1713 // Set max and minimum boost and hence minimum allocation
1714 if (Boost
> ((cpi
->baseline_gf_interval
+ 1) * 200))
1715 Boost
= ((cpi
->baseline_gf_interval
+ 1) * 200);
1716 else if (Boost
< 125)
1719 frames_in_section
= cpi
->baseline_gf_interval
+ 1;
1720 allocation_chunks
= (frames_in_section
* 100) + Boost
;
1722 // Else for standard golden frames
1725 // boost based on inter / intra ratio of subsequent frames
1726 Boost
= (cpi
->gfu_boost
* GFQ_ADJUSTMENT
) / 100;
1728 // Set max and minimum boost and hence minimum allocation
1729 if (Boost
> (cpi
->baseline_gf_interval
* 150))
1730 Boost
= (cpi
->baseline_gf_interval
* 150);
1731 else if (Boost
< 125)
1734 frames_in_section
= cpi
->baseline_gf_interval
;
1735 allocation_chunks
= (frames_in_section
* 100) + (Boost
- 100);
1738 // Normalize Altboost and allocations chunck down to prevent overflow
1739 while (Boost
> 1000)
1742 allocation_chunks
/= 2;
1745 // Calculate the number of bits to be spent on the gf or arf based on the boost number
1746 gf_bits
= (int)((double)Boost
* (cpi
->twopass
.gf_group_bits
/ (double)allocation_chunks
));
1748 // If the frame that is to be boosted is simpler than the average for
1749 // the gf/arf group then use an alternative calculation
1750 // based on the error score of the frame itself
1751 if (mod_frame_err
< gf_group_err
/ (double)cpi
->baseline_gf_interval
)
1753 double alt_gf_grp_bits
;
1757 (double)cpi
->twopass
.kf_group_bits
*
1758 (mod_frame_err
* (double)cpi
->baseline_gf_interval
) /
1759 DOUBLE_DIVIDE_CHECK((double)cpi
->twopass
.kf_group_error_left
);
1761 alt_gf_bits
= (int)((double)Boost
* (alt_gf_grp_bits
/
1762 (double)allocation_chunks
));
1764 if (gf_bits
> alt_gf_bits
)
1766 gf_bits
= alt_gf_bits
;
1769 // Else if it is harder than other frames in the group make sure it at
1770 // least receives an allocation in keeping with its relative error
1771 // score, otherwise it may be worse off than an "un-boosted" frame
1775 (int)((double)cpi
->twopass
.kf_group_bits
*
1777 DOUBLE_DIVIDE_CHECK((double)cpi
->twopass
.kf_group_error_left
));
1779 if (alt_gf_bits
> gf_bits
)
1781 gf_bits
= alt_gf_bits
;
1785 // Apply an additional limit for CBR
1786 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
1788 if (cpi
->twopass
.gf_bits
> (cpi
->buffer_level
>> 1))
1789 cpi
->twopass
.gf_bits
= cpi
->buffer_level
>> 1;
1792 // Dont allow a negative value for gf_bits
1796 gf_bits
+= cpi
->min_frame_bandwidth
; // Add in minimum for a frame
1800 cpi
->twopass
.gf_bits
= gf_bits
;
1802 if (i
== 1 || (!cpi
->source_alt_ref_pending
&& (cpi
->common
.frame_type
!= KEY_FRAME
)))
1804 cpi
->per_frame_bandwidth
= gf_bits
; // Per frame bit target for this frame
1809 // Adjust KF group bits and error remainin
1810 cpi
->twopass
.kf_group_error_left
-= gf_group_err
;
1811 cpi
->twopass
.kf_group_bits
-= cpi
->twopass
.gf_group_bits
;
1813 if (cpi
->twopass
.kf_group_bits
< 0)
1814 cpi
->twopass
.kf_group_bits
= 0;
1816 // Note the error score left in the remaining frames of the group.
1817 // 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)
1818 if (!cpi
->source_alt_ref_pending
&& cpi
->common
.frame_type
!= KEY_FRAME
)
1819 cpi
->twopass
.gf_group_error_left
= gf_group_err
- gf_first_frame_err
;
1821 cpi
->twopass
.gf_group_error_left
= gf_group_err
;
1823 cpi
->twopass
.gf_group_bits
-= cpi
->twopass
.gf_bits
- cpi
->min_frame_bandwidth
;
1825 if (cpi
->twopass
.gf_group_bits
< 0)
1826 cpi
->twopass
.gf_group_bits
= 0;
1828 // Set aside some bits for a mid gf sequence boost
1829 if ((cpi
->gfu_boost
> 150) && (cpi
->baseline_gf_interval
> 5))
1831 int pct_extra
= (cpi
->gfu_boost
- 100) / 50;
1832 pct_extra
= (pct_extra
> 10) ? 10 : pct_extra
;
1834 cpi
->twopass
.mid_gf_extra_bits
= (cpi
->twopass
.gf_group_bits
* pct_extra
) / 100;
1835 cpi
->twopass
.gf_group_bits
-= cpi
->twopass
.mid_gf_extra_bits
;
1838 cpi
->twopass
.mid_gf_extra_bits
= 0;
1841 // Adjustment to estimate_max_q based on a measure of complexity of the section
1842 if (cpi
->common
.frame_type
!= KEY_FRAME
)
1844 FIRSTPASS_STATS sectionstats
;
1847 zero_stats(§ionstats
);
1848 reset_fpf_position(cpi
, start_pos
);
1850 for (i
= 0 ; i
< cpi
->baseline_gf_interval
; i
++)
1852 input_stats(cpi
, &next_frame
);
1853 accumulate_stats(§ionstats
, &next_frame
);
1856 avg_stats(§ionstats
);
1858 cpi
->twopass
.section_intra_rating
=
1859 sectionstats
.intra_error
/
1860 DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
1862 Ratio
= sectionstats
.intra_error
/ DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
1863 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
1865 cpi
->twopass
.section_max_qfactor
= 1.0 - ((Ratio
- 10.0) * 0.025);
1867 if (cpi
->twopass
.section_max_qfactor
< 0.80)
1868 cpi
->twopass
.section_max_qfactor
= 0.80;
1872 // cpi->twopass.section_max_qfactor = 1.0;
1874 reset_fpf_position(cpi
, start_pos
);
1878 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1879 static void assign_std_frame_bits(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
1881 int target_frame_size
; // gf_group_error_left
1883 double modified_err
;
1884 double err_fraction
; // What portion of the remaining GF group error is used by this frame
1886 int max_bits
= frame_max_bits(cpi
); // Max for a single frame
1888 // Calculate modified prediction error used in bit allocation
1889 modified_err
= calculate_modified_err(cpi
, this_frame
);
1891 if (cpi
->twopass
.gf_group_error_left
> 0)
1892 err_fraction
= modified_err
/ cpi
->twopass
.gf_group_error_left
; // What portion of the remaining GF group error is used by this frame
1896 target_frame_size
= (int)((double)cpi
->twopass
.gf_group_bits
* err_fraction
); // How many of those bits available for allocation should we give it?
1898 // Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at the top end.
1899 if (target_frame_size
< 0)
1900 target_frame_size
= 0;
1903 if (target_frame_size
> max_bits
)
1904 target_frame_size
= max_bits
;
1906 if (target_frame_size
> cpi
->twopass
.gf_group_bits
)
1907 target_frame_size
= cpi
->twopass
.gf_group_bits
;
1910 cpi
->twopass
.gf_group_error_left
-= modified_err
; // Adjust error remaining
1911 cpi
->twopass
.gf_group_bits
-= target_frame_size
; // Adjust bits remaining
1913 if (cpi
->twopass
.gf_group_bits
< 0)
1914 cpi
->twopass
.gf_group_bits
= 0;
1916 target_frame_size
+= cpi
->min_frame_bandwidth
; // Add in the minimum number of bits that is set aside for every frame.
1918 // Special case for the frame that lies half way between two gfs
1919 if (cpi
->common
.frames_since_golden
== cpi
->baseline_gf_interval
/ 2)
1920 target_frame_size
+= cpi
->twopass
.mid_gf_extra_bits
;
1922 cpi
->per_frame_bandwidth
= target_frame_size
; // Per frame bit target for this frame
1925 void vp8_second_pass(VP8_COMP
*cpi
)
1928 int frames_left
= (int)(cpi
->twopass
.total_stats
->count
- cpi
->common
.current_video_frame
);
1930 FIRSTPASS_STATS this_frame
;
1931 FIRSTPASS_STATS this_frame_copy
;
1933 double this_frame_error
;
1934 double this_frame_intra_error
;
1935 double this_frame_coded_error
;
1937 FIRSTPASS_STATS
*start_pos
;
1939 if (!cpi
->twopass
.stats_in
)
1944 vp8_clear_system_state();
1946 if (EOF
== input_stats(cpi
, &this_frame
))
1949 this_frame_error
= this_frame
.ssim_weighted_pred_err
;
1950 this_frame_intra_error
= this_frame
.intra_error
;
1951 this_frame_coded_error
= this_frame
.coded_error
;
1953 start_pos
= cpi
->twopass
.stats_in
;
1955 // keyframe and section processing !
1956 if (cpi
->twopass
.frames_to_key
== 0)
1958 // Define next KF group and assign bits to it
1959 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1960 find_next_key_frame(cpi
, &this_frame_copy
);
1962 // 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
1963 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
1964 // This is temporary code till we decide what should really happen in this case.
1965 if (cpi
->oxcf
.error_resilient_mode
)
1967 cpi
->twopass
.gf_group_bits
= cpi
->twopass
.kf_group_bits
;
1968 cpi
->twopass
.gf_group_error_left
= cpi
->twopass
.kf_group_error_left
;
1969 cpi
->baseline_gf_interval
= cpi
->twopass
.frames_to_key
;
1970 cpi
->frames_till_gf_update_due
= cpi
->baseline_gf_interval
;
1971 cpi
->source_alt_ref_pending
= FALSE
;
1976 // Is this a GF / ARF (Note that a KF is always also a GF)
1977 if (cpi
->frames_till_gf_update_due
== 0)
1979 // Define next gf group and assign bits to it
1980 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1981 define_gf_group(cpi
, &this_frame_copy
);
1983 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
1984 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
1985 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
1986 if (cpi
->source_alt_ref_pending
&& (cpi
->common
.frame_type
!= KEY_FRAME
))
1988 // Assign a standard frames worth of bits from those allocated to the GF group
1989 int bak
= cpi
->per_frame_bandwidth
;
1990 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1991 assign_std_frame_bits(cpi
, &this_frame_copy
);
1992 cpi
->per_frame_bandwidth
= bak
;
1996 // Otherwise this is an ordinary frame
1999 // 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
2000 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
2001 // This is temporary code till we decide what should really happen in this case.
2002 if (cpi
->oxcf
.error_resilient_mode
)
2004 cpi
->frames_till_gf_update_due
= cpi
->twopass
.frames_to_key
;
2006 if (cpi
->common
.frame_type
!= KEY_FRAME
)
2008 // Assign bits from those allocated to the GF group
2009 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
2010 assign_std_frame_bits(cpi
, &this_frame_copy
);
2015 // Assign bits from those allocated to the GF group
2016 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
2017 assign_std_frame_bits(cpi
, &this_frame_copy
);
2021 // Keep a globally available copy of this and the next frame's iiratio.
2022 cpi
->twopass
.this_iiratio
= this_frame_intra_error
/
2023 DOUBLE_DIVIDE_CHECK(this_frame_coded_error
);
2025 FIRSTPASS_STATS next_frame
;
2026 if ( lookup_next_frame_stats(cpi
, &next_frame
) != EOF
)
2028 cpi
->twopass
.next_iiratio
= next_frame
.intra_error
/
2029 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
);
2033 // Set nominal per second bandwidth for this frame
2034 cpi
->target_bandwidth
= cpi
->per_frame_bandwidth
* cpi
->output_frame_rate
;
2035 if (cpi
->target_bandwidth
< 0)
2036 cpi
->target_bandwidth
= 0;
2038 if (cpi
->common
.current_video_frame
== 0)
2040 cpi
->twopass
.est_max_qcorrection_factor
= 1.0;
2042 // Experimental code to try and set a cq_level in constrained
2044 if ( cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
)
2050 (cpi
->twopass
.total_coded_error_left
/ frames_left
),
2051 (int)(cpi
->twopass
.bits_left
/ frames_left
));
2053 cpi
->cq_target_quality
= cpi
->oxcf
.cq_level
;
2054 if ( est_cq
> cpi
->cq_target_quality
)
2055 cpi
->cq_target_quality
= est_cq
;
2058 // guess at maxq needed in 2nd pass
2059 cpi
->twopass
.maxq_max_limit
= cpi
->worst_quality
;
2060 cpi
->twopass
.maxq_min_limit
= cpi
->best_quality
;
2061 tmp_q
= estimate_max_q( cpi
,
2062 (cpi
->twopass
.total_coded_error_left
/ frames_left
),
2063 (int)(cpi
->twopass
.bits_left
/ frames_left
));
2065 // Limit the maxq value returned subsequently.
2066 // This increases the risk of overspend or underspend if the initial
2067 // estimate for the clip is bad, but helps prevent excessive
2068 // variation in Q, especially near the end of a clip
2069 // where for example a small overspend may cause Q to crash
2070 cpi
->twopass
.maxq_max_limit
= ((tmp_q
+ 32) < cpi
->worst_quality
)
2071 ? (tmp_q
+ 32) : cpi
->worst_quality
;
2072 cpi
->twopass
.maxq_min_limit
= ((tmp_q
- 32) > cpi
->best_quality
)
2073 ? (tmp_q
- 32) : cpi
->best_quality
;
2075 cpi
->active_worst_quality
= tmp_q
;
2076 cpi
->ni_av_qi
= tmp_q
;
2079 // The last few frames of a clip almost always have to few or too many
2080 // bits and for the sake of over exact rate control we dont want to make
2081 // radical adjustments to the allowed quantizer range just to use up a
2082 // few surplus bits or get beneath the target rate.
2083 else if ( (cpi
->common
.current_video_frame
<
2084 (((unsigned int)cpi
->twopass
.total_stats
->count
* 255)>>8)) &&
2085 ((cpi
->common
.current_video_frame
+ cpi
->baseline_gf_interval
) <
2086 (unsigned int)cpi
->twopass
.total_stats
->count
) )
2088 if (frames_left
< 1)
2091 tmp_q
= estimate_max_q(cpi
, (cpi
->twopass
.total_coded_error_left
/ frames_left
), (int)(cpi
->twopass
.bits_left
/ frames_left
));
2093 // Move active_worst_quality but in a damped way
2094 if (tmp_q
> cpi
->active_worst_quality
)
2095 cpi
->active_worst_quality
++;
2096 else if (tmp_q
< cpi
->active_worst_quality
)
2097 cpi
->active_worst_quality
--;
2099 cpi
->active_worst_quality
= ((cpi
->active_worst_quality
* 3) + tmp_q
+ 2) / 4;
2102 cpi
->twopass
.frames_to_key
--;
2103 cpi
->twopass
.total_error_left
-= this_frame_error
;
2104 cpi
->twopass
.total_intra_error_left
-= this_frame_intra_error
;
2105 cpi
->twopass
.total_coded_error_left
-= this_frame_coded_error
;
2109 static BOOL
test_candidate_kf(VP8_COMP
*cpi
, FIRSTPASS_STATS
*last_frame
, FIRSTPASS_STATS
*this_frame
, FIRSTPASS_STATS
*next_frame
)
2111 BOOL is_viable_kf
= FALSE
;
2113 // Does the frame satisfy the primary criteria of a key frame
2114 // If so, then examine how well it predicts subsequent frames
2115 if ((this_frame
->pcnt_second_ref
< 0.10) &&
2116 (next_frame
->pcnt_second_ref
< 0.10) &&
2117 ((this_frame
->pcnt_inter
< 0.05) ||
2119 ((this_frame
->pcnt_inter
- this_frame
->pcnt_neutral
) < .25) &&
2120 ((this_frame
->intra_error
/ DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
)) < 2.5) &&
2121 ((fabs(last_frame
->coded_error
- this_frame
->coded_error
) / DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
) > .40) ||
2122 (fabs(last_frame
->intra_error
- this_frame
->intra_error
) / DOUBLE_DIVIDE_CHECK(this_frame
->intra_error
) > .40) ||
2123 ((next_frame
->intra_error
/ DOUBLE_DIVIDE_CHECK(next_frame
->coded_error
)) > 3.5)
2130 FIRSTPASS_STATS
*start_pos
;
2132 FIRSTPASS_STATS local_next_frame
;
2134 double boost_score
= 0.0;
2135 double old_boost_score
= 0.0;
2136 double decay_accumulator
= 1.0;
2137 double next_iiratio
;
2139 vpx_memcpy(&local_next_frame
, next_frame
, sizeof(*next_frame
));
2141 // Note the starting file position so we can reset to it
2142 start_pos
= cpi
->twopass
.stats_in
;
2144 // Examine how well the key frame predicts subsequent frames
2145 for (i
= 0 ; i
< 16; i
++)
2147 next_iiratio
= (IIKFACTOR1
* local_next_frame
.intra_error
/ DOUBLE_DIVIDE_CHECK(local_next_frame
.coded_error
)) ;
2149 if (next_iiratio
> RMAX
)
2150 next_iiratio
= RMAX
;
2152 // Cumulative effect of decay in prediction quality
2153 if (local_next_frame
.pcnt_inter
> 0.85)
2154 decay_accumulator
= decay_accumulator
* local_next_frame
.pcnt_inter
;
2156 decay_accumulator
= decay_accumulator
* ((0.85 + local_next_frame
.pcnt_inter
) / 2.0);
2158 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2160 // Keep a running total
2161 boost_score
+= (decay_accumulator
* next_iiratio
);
2163 // Test various breakout clauses
2164 if ((local_next_frame
.pcnt_inter
< 0.05) ||
2165 (next_iiratio
< 1.5) ||
2166 (((local_next_frame
.pcnt_inter
-
2167 local_next_frame
.pcnt_neutral
) < 0.20) &&
2168 (next_iiratio
< 3.0)) ||
2169 ((boost_score
- old_boost_score
) < 0.5) ||
2170 (local_next_frame
.intra_error
< 200)
2176 old_boost_score
= boost_score
;
2178 // Get the next frame details
2179 if (EOF
== input_stats(cpi
, &local_next_frame
))
2183 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2184 if (boost_score
> 5.0 && (i
> 3))
2185 is_viable_kf
= TRUE
;
2188 // Reset the file position
2189 reset_fpf_position(cpi
, start_pos
);
2191 is_viable_kf
= FALSE
;
2195 return is_viable_kf
;
2197 static void find_next_key_frame(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
2200 FIRSTPASS_STATS last_frame
;
2201 FIRSTPASS_STATS first_frame
;
2202 FIRSTPASS_STATS next_frame
;
2203 FIRSTPASS_STATS
*start_position
;
2205 double decay_accumulator
= 1.0;
2206 double boost_score
= 0;
2207 double old_boost_score
= 0.0;
2208 double loop_decay_rate
;
2210 double kf_mod_err
= 0.0;
2211 double kf_group_err
= 0.0;
2212 double kf_group_intra_err
= 0.0;
2213 double kf_group_coded_err
= 0.0;
2214 double recent_loop_decay
[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2216 vpx_memset(&next_frame
, 0, sizeof(next_frame
)); // assure clean
2218 vp8_clear_system_state(); //__asm emms;
2219 start_position
= cpi
->twopass
.stats_in
;
2221 cpi
->common
.frame_type
= KEY_FRAME
;
2223 // is this a forced key frame by interval
2224 cpi
->this_key_frame_forced
= cpi
->next_key_frame_forced
;
2226 // Clear the alt ref active flag as this can never be active on a key frame
2227 cpi
->source_alt_ref_active
= FALSE
;
2229 // Kf is always a gf so clear frames till next gf counter
2230 cpi
->frames_till_gf_update_due
= 0;
2232 cpi
->twopass
.frames_to_key
= 1;
2234 // Take a copy of the initial frame details
2235 vpx_memcpy(&first_frame
, this_frame
, sizeof(*this_frame
));
2237 cpi
->twopass
.kf_group_bits
= 0; // Total bits avaialable to kf group
2238 cpi
->twopass
.kf_group_error_left
= 0; // Group modified error score.
2240 kf_mod_err
= calculate_modified_err(cpi
, this_frame
);
2242 // find the next keyframe
2244 while (cpi
->twopass
.stats_in
< cpi
->twopass
.stats_in_end
)
2246 // Accumulate kf group error
2247 kf_group_err
+= calculate_modified_err(cpi
, this_frame
);
2249 // These figures keep intra and coded error counts for all frames including key frames in the group.
2250 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2251 kf_group_intra_err
+= this_frame
->intra_error
;
2252 kf_group_coded_err
+= this_frame
->coded_error
;
2254 // load a the next frame's stats
2255 vpx_memcpy(&last_frame
, this_frame
, sizeof(*this_frame
));
2256 input_stats(cpi
, this_frame
);
2258 // Provided that we are not at the end of the file...
2259 if (cpi
->oxcf
.auto_key
2260 && lookup_next_frame_stats(cpi
, &next_frame
) != EOF
)
2262 // Normal scene cut check
2263 if (test_candidate_kf(cpi
, &last_frame
, this_frame
, &next_frame
))
2266 // How fast is prediction quality decaying
2267 loop_decay_rate
= get_prediction_decay_rate(cpi
, &next_frame
);
2269 // We want to know something about the recent past... rather than
2270 // as used elsewhere where we are concened with decay in prediction
2271 // quality since the last GF or KF.
2272 recent_loop_decay
[i
%8] = loop_decay_rate
;
2273 decay_accumulator
= 1.0;
2274 for (j
= 0; j
< 8; j
++)
2276 decay_accumulator
= decay_accumulator
* recent_loop_decay
[j
];
2279 // Special check for transition or high motion followed by a
2280 // to a static scene.
2281 if ( detect_transition_to_still( cpi
, i
,
2282 (cpi
->key_frame_frequency
-i
),
2284 decay_accumulator
) )
2290 // Step on to the next frame
2291 cpi
->twopass
.frames_to_key
++;
2293 // If we don't have a real key frame within the next two
2294 // forcekeyframeevery intervals then break out of the loop.
2295 if (cpi
->twopass
.frames_to_key
>= 2 *(int)cpi
->key_frame_frequency
)
2298 cpi
->twopass
.frames_to_key
++;
2303 // If there is a max kf interval set by the user we must obey it.
2304 // We already breakout of the loop above at 2x max.
2305 // This code centers the extra kf if the actual natural
2306 // interval is between 1x and 2x
2307 if (cpi
->oxcf
.auto_key
2308 && cpi
->twopass
.frames_to_key
> (int)cpi
->key_frame_frequency
)
2310 FIRSTPASS_STATS
*current_pos
= cpi
->twopass
.stats_in
;
2311 FIRSTPASS_STATS tmp_frame
;
2313 cpi
->twopass
.frames_to_key
/= 2;
2315 // Copy first frame details
2316 vpx_memcpy(&tmp_frame
, &first_frame
, sizeof(first_frame
));
2318 // Reset to the start of the group
2319 reset_fpf_position(cpi
, start_position
);
2322 kf_group_intra_err
= 0;
2323 kf_group_coded_err
= 0;
2325 // Rescan to get the correct error data for the forced kf group
2326 for( i
= 0; i
< cpi
->twopass
.frames_to_key
; i
++ )
2328 // Accumulate kf group errors
2329 kf_group_err
+= calculate_modified_err(cpi
, &tmp_frame
);
2330 kf_group_intra_err
+= tmp_frame
.intra_error
;
2331 kf_group_coded_err
+= tmp_frame
.coded_error
;
2333 // Load a the next frame's stats
2334 input_stats(cpi
, &tmp_frame
);
2337 // Reset to the start of the group
2338 reset_fpf_position(cpi
, current_pos
);
2340 cpi
->next_key_frame_forced
= TRUE
;
2343 cpi
->next_key_frame_forced
= FALSE
;
2345 // Special case for the last frame of the file
2346 if (cpi
->twopass
.stats_in
>= cpi
->twopass
.stats_in_end
)
2348 // Accumulate kf group error
2349 kf_group_err
+= calculate_modified_err(cpi
, this_frame
);
2351 // These figures keep intra and coded error counts for all frames including key frames in the group.
2352 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2353 kf_group_intra_err
+= this_frame
->intra_error
;
2354 kf_group_coded_err
+= this_frame
->coded_error
;
2357 // Calculate the number of bits that should be assigned to the kf group.
2358 if ((cpi
->twopass
.bits_left
> 0) && (cpi
->twopass
.modified_error_left
> 0.0))
2360 // Max for a single normal frame (not key frame)
2361 int max_bits
= frame_max_bits(cpi
);
2363 // Maximum bits for the kf group
2364 long long max_grp_bits
;
2366 // Default allocation based on bits left and relative
2367 // complexity of the section
2368 cpi
->twopass
.kf_group_bits
= (long long)( cpi
->twopass
.bits_left
*
2370 cpi
->twopass
.modified_error_left
));
2372 // Clip based on maximum per frame rate defined by the user.
2373 max_grp_bits
= (long long)max_bits
* (long long)cpi
->twopass
.frames_to_key
;
2374 if (cpi
->twopass
.kf_group_bits
> max_grp_bits
)
2375 cpi
->twopass
.kf_group_bits
= max_grp_bits
;
2377 // Additional special case for CBR if buffer is getting full.
2378 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2380 int opt_buffer_lvl
= cpi
->oxcf
.optimal_buffer_level
;
2381 int buffer_lvl
= cpi
->buffer_level
;
2383 // If the buffer is near or above the optimal and this kf group is
2384 // not being allocated much then increase the allocation a bit.
2385 if (buffer_lvl
>= opt_buffer_lvl
)
2387 int high_water_mark
= (opt_buffer_lvl
+
2388 cpi
->oxcf
.maximum_buffer_size
) >> 1;
2390 long long av_group_bits
;
2392 // Av bits per frame * number of frames
2393 av_group_bits
= (long long)cpi
->av_per_frame_bandwidth
*
2394 (long long)cpi
->twopass
.frames_to_key
;
2396 // We are at or above the maximum.
2397 if (cpi
->buffer_level
>= high_water_mark
)
2399 long long min_group_bits
;
2401 min_group_bits
= av_group_bits
+
2402 (long long)(buffer_lvl
-
2405 if (cpi
->twopass
.kf_group_bits
< min_group_bits
)
2406 cpi
->twopass
.kf_group_bits
= min_group_bits
;
2408 // We are above optimal but below the maximum
2409 else if (cpi
->twopass
.kf_group_bits
< av_group_bits
)
2411 long long bits_below_av
= av_group_bits
-
2412 cpi
->twopass
.kf_group_bits
;
2414 cpi
->twopass
.kf_group_bits
+=
2415 (long long)((double)bits_below_av
*
2416 (double)(buffer_lvl
- opt_buffer_lvl
) /
2417 (double)(high_water_mark
- opt_buffer_lvl
));
2423 cpi
->twopass
.kf_group_bits
= 0;
2425 // Reset the first pass file position
2426 reset_fpf_position(cpi
, start_position
);
2428 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2429 decay_accumulator
= 1.0;
2431 loop_decay_rate
= 1.00; // Starting decay rate
2433 for (i
= 0 ; i
< cpi
->twopass
.frames_to_key
; i
++)
2437 if (EOF
== input_stats(cpi
, &next_frame
))
2440 if (next_frame
.intra_error
> cpi
->twopass
.kf_intra_err_min
)
2441 r
= (IIKFACTOR2
* next_frame
.intra_error
/
2442 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
));
2444 r
= (IIKFACTOR2
* cpi
->twopass
.kf_intra_err_min
/
2445 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
));
2450 // How fast is prediction quality decaying
2451 loop_decay_rate
= get_prediction_decay_rate(cpi
, &next_frame
);
2453 decay_accumulator
= decay_accumulator
* loop_decay_rate
;
2454 decay_accumulator
= decay_accumulator
< 0.1 ? 0.1 : decay_accumulator
;
2456 boost_score
+= (decay_accumulator
* r
);
2458 if ((i
> MIN_GF_INTERVAL
) &&
2459 ((boost_score
- old_boost_score
) < 1.0))
2464 old_boost_score
= boost_score
;
2469 FIRSTPASS_STATS sectionstats
;
2472 zero_stats(§ionstats
);
2473 reset_fpf_position(cpi
, start_position
);
2475 for (i
= 0 ; i
< cpi
->twopass
.frames_to_key
; i
++)
2477 input_stats(cpi
, &next_frame
);
2478 accumulate_stats(§ionstats
, &next_frame
);
2481 avg_stats(§ionstats
);
2483 cpi
->twopass
.section_intra_rating
=
2484 sectionstats
.intra_error
2485 / DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
2487 Ratio
= sectionstats
.intra_error
/ DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
2488 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2490 cpi
->twopass
.section_max_qfactor
= 1.0 - ((Ratio
- 10.0) * 0.025);
2492 if (cpi
->twopass
.section_max_qfactor
< 0.80)
2493 cpi
->twopass
.section_max_qfactor
= 0.80;
2497 // cpi->twopass.section_max_qfactor = 1.0;
2500 // When using CBR apply additional buffer fullness related upper limits
2501 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2505 if (cpi
->drop_frames_allowed
)
2507 int df_buffer_level
= cpi
->oxcf
.drop_frames_water_mark
* (cpi
->oxcf
.optimal_buffer_level
/ 100);
2509 if (cpi
->buffer_level
> df_buffer_level
)
2510 max_boost
= ((double)((cpi
->buffer_level
- df_buffer_level
) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
2514 else if (cpi
->buffer_level
> 0)
2516 max_boost
= ((double)(cpi
->buffer_level
* 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
2523 if (boost_score
> max_boost
)
2524 boost_score
= max_boost
;
2527 // Reset the first pass file position
2528 reset_fpf_position(cpi
, start_position
);
2530 // Work out how many bits to allocate for the key frame itself
2533 int kf_boost
= boost_score
;
2534 int allocation_chunks
;
2535 int Counter
= cpi
->twopass
.frames_to_key
;
2537 YV12_BUFFER_CONFIG
*lst_yv12
= &cpi
->common
.yv12_fb
[cpi
->common
.lst_fb_idx
];
2538 // Min boost based on kf interval
2541 while ((kf_boost
< 48) && (Counter
> 0))
2551 kf_boost
+= ((Counter
+ 1) >> 1);
2553 if (kf_boost
> 48) kf_boost
= 48;
2556 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2557 if ((lst_yv12
->y_width
* lst_yv12
->y_height
) > (320 * 240))
2558 kf_boost
+= 2 * (lst_yv12
->y_width
* lst_yv12
->y_height
) / (320 * 240);
2559 else if ((lst_yv12
->y_width
* lst_yv12
->y_height
) < (320 * 240))
2560 kf_boost
-= 4 * (320 * 240) / (lst_yv12
->y_width
* lst_yv12
->y_height
);
2562 kf_boost
= (int)((double)kf_boost
* 100.0) >> 4; // Scale 16 to 100
2564 // Adjustment to boost based on recent average q
2565 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2567 if (kf_boost
< 250) // Min KF boost
2570 // We do three calculations for kf size.
2571 // The first is based on the error score for the whole kf group.
2572 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2573 // 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
2575 allocation_chunks
= ((cpi
->twopass
.frames_to_key
- 1) * 100) + kf_boost
; // cpi->twopass.frames_to_key-1 because key frame itself is taken care of by kf_boost
2577 // Normalize Altboost and allocations chunck down to prevent overflow
2578 while (kf_boost
> 1000)
2581 allocation_chunks
/= 2;
2584 cpi
->twopass
.kf_group_bits
= (cpi
->twopass
.kf_group_bits
< 0) ? 0 : cpi
->twopass
.kf_group_bits
;
2586 // Calculate the number of bits to be spent on the key frame
2587 cpi
->twopass
.kf_bits
= (int)((double)kf_boost
* ((double)cpi
->twopass
.kf_group_bits
/ (double)allocation_chunks
));
2589 // Apply an additional limit for CBR
2590 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2592 if (cpi
->twopass
.kf_bits
> ((3 * cpi
->buffer_level
) >> 2))
2593 cpi
->twopass
.kf_bits
= (3 * cpi
->buffer_level
) >> 2;
2596 // If the key frame is actually easier than the average for the
2597 // kf group (which does sometimes happen... eg a blank intro frame)
2598 // Then use an alternate calculation based on the kf error score
2599 // which should give a smaller key frame.
2600 if (kf_mod_err
< kf_group_err
/ cpi
->twopass
.frames_to_key
)
2602 double alt_kf_grp_bits
=
2603 ((double)cpi
->twopass
.bits_left
*
2604 (kf_mod_err
* (double)cpi
->twopass
.frames_to_key
) /
2605 DOUBLE_DIVIDE_CHECK(cpi
->twopass
.modified_error_left
));
2607 alt_kf_bits
= (int)((double)kf_boost
*
2608 (alt_kf_grp_bits
/ (double)allocation_chunks
));
2610 if (cpi
->twopass
.kf_bits
> alt_kf_bits
)
2612 cpi
->twopass
.kf_bits
= alt_kf_bits
;
2615 // Else if it is much harder than other frames in the group make sure
2616 // it at least receives an allocation in keeping with its relative
2621 (int)((double)cpi
->twopass
.bits_left
*
2623 DOUBLE_DIVIDE_CHECK(cpi
->twopass
.modified_error_left
)));
2625 if (alt_kf_bits
> cpi
->twopass
.kf_bits
)
2627 cpi
->twopass
.kf_bits
= alt_kf_bits
;
2631 cpi
->twopass
.kf_group_bits
-= cpi
->twopass
.kf_bits
;
2632 cpi
->twopass
.kf_bits
+= cpi
->min_frame_bandwidth
; // Add in the minimum frame allowance
2634 cpi
->per_frame_bandwidth
= cpi
->twopass
.kf_bits
; // Peer frame bit target for this frame
2635 cpi
->target_bandwidth
= cpi
->twopass
.kf_bits
* cpi
->output_frame_rate
; // Convert to a per second bitrate
2638 // Note the total error score of the kf group minus the key frame itself
2639 cpi
->twopass
.kf_group_error_left
= (int)(kf_group_err
- kf_mod_err
);
2641 // Adjust the count of total modified error left.
2642 // The count of bits left is adjusted elsewhere based on real coded frame sizes
2643 cpi
->twopass
.modified_error_left
-= kf_group_err
;
2645 if (cpi
->oxcf
.allow_spatial_resampling
)
2647 int resample_trigger
= FALSE
;
2648 int last_kf_resampled
= FALSE
;
2652 int new_width
= cpi
->oxcf
.Width
;
2653 int new_height
= cpi
->oxcf
.Height
;
2655 int projected_buffer_level
= cpi
->buffer_level
;
2658 double projected_bits_perframe
;
2659 double group_iiratio
= (kf_group_intra_err
- first_frame
.intra_error
) / (kf_group_coded_err
- first_frame
.coded_error
);
2660 double err_per_frame
= kf_group_err
/ cpi
->twopass
.frames_to_key
;
2661 double bits_per_frame
;
2662 double av_bits_per_frame
;
2663 double effective_size_ratio
;
2665 if ((cpi
->common
.Width
!= cpi
->oxcf
.Width
) || (cpi
->common
.Height
!= cpi
->oxcf
.Height
))
2666 last_kf_resampled
= TRUE
;
2668 // Set back to unscaled by defaults
2669 cpi
->common
.horiz_scale
= NORMAL
;
2670 cpi
->common
.vert_scale
= NORMAL
;
2672 // Calculate Average bits per frame.
2673 //av_bits_per_frame = cpi->twopass.bits_left/(double)(cpi->twopass.total_stats->count - cpi->common.current_video_frame);
2674 av_bits_per_frame
= cpi
->oxcf
.target_bandwidth
/ DOUBLE_DIVIDE_CHECK((double)cpi
->oxcf
.frame_rate
);
2675 //if ( av_bits_per_frame < 0.0 )
2676 // av_bits_per_frame = 0.0
2678 // CBR... Use the clip average as the target for deciding resample
2679 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2681 bits_per_frame
= av_bits_per_frame
;
2684 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
2685 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
2688 bits_per_frame
= cpi
->twopass
.kf_group_bits
/ cpi
->twopass
.frames_to_key
; // This accounts for how hard the section is...
2690 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
2691 bits_per_frame
= av_bits_per_frame
;
2694 // bits_per_frame should comply with our minimum
2695 if (bits_per_frame
< (cpi
->oxcf
.target_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100))
2696 bits_per_frame
= (cpi
->oxcf
.target_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
2698 // Work out if spatial resampling is necessary
2699 kf_q
= estimate_kf_group_q(cpi
, err_per_frame
, bits_per_frame
, group_iiratio
);
2701 // 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
2702 projected_bits_perframe
= bits_per_frame
;
2705 while (tmp_q
> cpi
->worst_quality
)
2707 projected_bits_perframe
*= 1.04;
2711 // Guess at buffer level at the end of the section
2712 projected_buffer_level
= cpi
->buffer_level
- (int)((projected_bits_perframe
- av_bits_per_frame
) * cpi
->twopass
.frames_to_key
);
2716 FILE *f
= fopen("Subsamle.stt", "a");
2717 fprintf(f
, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi
->common
.current_video_frame
, kf_q
, cpi
->common
.horiz_scale
, cpi
->common
.vert_scale
, kf_group_err
/ cpi
->twopass
.frames_to_key
, (int)(cpi
->twopass
.kf_group_bits
/ cpi
->twopass
.frames_to_key
), new_height
, new_width
);
2721 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
2722 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2724 // Trigger resample if we are projected to fall below down sample level or
2725 // resampled last time and are projected to remain below the up sample level
2726 if ((projected_buffer_level
< (cpi
->oxcf
.resample_down_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100)) ||
2727 (last_kf_resampled
&& (projected_buffer_level
< (cpi
->oxcf
.resample_up_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100))))
2728 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
2729 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
2730 resample_trigger
= TRUE
;
2732 resample_trigger
= FALSE
;
2736 long long clip_bits
= (long long)(cpi
->twopass
.total_stats
->count
* cpi
->oxcf
.target_bandwidth
/ DOUBLE_DIVIDE_CHECK((double)cpi
->oxcf
.frame_rate
));
2737 long long over_spend
= cpi
->oxcf
.starting_buffer_level
- cpi
->buffer_level
;
2739 if ((last_kf_resampled
&& (kf_q
> cpi
->worst_quality
)) || // If triggered last time the threshold for triggering again is reduced
2740 ((kf_q
> cpi
->worst_quality
) && // Projected Q higher than allowed and ...
2741 (over_spend
> clip_bits
/ 20))) // ... Overspend > 5% of total bits
2742 resample_trigger
= TRUE
;
2744 resample_trigger
= FALSE
;
2748 if (resample_trigger
)
2750 while ((kf_q
>= cpi
->worst_quality
) && (scale_val
< 6))
2754 cpi
->common
.vert_scale
= vscale_lookup
[scale_val
];
2755 cpi
->common
.horiz_scale
= hscale_lookup
[scale_val
];
2757 Scale2Ratio(cpi
->common
.horiz_scale
, &hr
, &hs
);
2758 Scale2Ratio(cpi
->common
.vert_scale
, &vr
, &vs
);
2760 new_width
= ((hs
- 1) + (cpi
->oxcf
.Width
* hr
)) / hs
;
2761 new_height
= ((vs
- 1) + (cpi
->oxcf
.Height
* vr
)) / vs
;
2763 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
2764 // effective_sizeratio attempts to provide a crude correction for this
2765 effective_size_ratio
= (double)(new_width
* new_height
) / (double)(cpi
->oxcf
.Width
* cpi
->oxcf
.Height
);
2766 effective_size_ratio
= (1.0 + (3.0 * effective_size_ratio
)) / 4.0;
2768 // Now try again and see what Q we get with the smaller image size
2769 kf_q
= estimate_kf_group_q(cpi
, err_per_frame
* effective_size_ratio
, bits_per_frame
, group_iiratio
);
2773 FILE *f
= fopen("Subsamle.stt", "a");
2774 fprintf(f
, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q
, cpi
->common
.horiz_scale
, cpi
->common
.vert_scale
, kf_group_err
/ cpi
->twopass
.frames_to_key
, (int)(cpi
->twopass
.kf_group_bits
/ cpi
->twopass
.frames_to_key
), new_height
, new_width
);
2780 if ((cpi
->common
.Width
!= new_width
) || (cpi
->common
.Height
!= new_height
))
2782 cpi
->common
.Width
= new_width
;
2783 cpi
->common
.Height
= new_height
;
2784 vp8_alloc_compressor_data(cpi
);