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.
17 #include "encodeintra.h"
18 #include "setupintrarecon.h"
20 #include "vpx_scale/vpxscale.h"
23 #include "systemdependent.h"
24 #include "vpx_scale/yv12extend.h"
25 #include "vpx_mem/vpx_mem.h"
26 #include "swapyv12buffer.h"
29 #include "quant_common.h"
32 //#define OUTPUT_FPF 1
34 #if CONFIG_RUNTIME_CPU_DETECT
35 #define IF_RTCD(x) (x)
37 #define IF_RTCD(x) NULL
40 extern void vp8_build_block_offsets(MACROBLOCK
*x
);
41 extern void vp8_setup_block_ptrs(MACROBLOCK
*x
);
42 extern void vp8cx_frame_init_quantizer(VP8_COMP
*cpi
);
43 extern void vp8_set_mbmode_and_mvs(MACROBLOCK
*x
, MB_PREDICTION_MODE mb
, MV
*mv
);
44 extern void vp8_alloc_compressor_data(VP8_COMP
*cpi
);
46 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10))
47 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10))
48 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
49 extern int vp8_kf_boost_qadjustment
[QINDEX_RANGE
];
51 extern const int vp8_gf_boost_qadjustment
[QINDEX_RANGE
];
54 #define IIKFACTOR1 1.40
55 #define IIKFACTOR2 1.5
57 #define GF_RMAX 48.0 // 128.0
59 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
61 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
62 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
64 static int vscale_lookup
[7] = {0, 1, 1, 2, 2, 3, 3};
65 static int hscale_lookup
[7] = {0, 0, 1, 1, 2, 2, 3};
68 void vp8_find_next_key_frame(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
);
69 int vp8_input_stats(VP8_COMP
*cpi
, FIRSTPASS_STATS
*fps
);
71 int vp8_encode_intra(VP8_COMP
*cpi
, MACROBLOCK
*x
, int use_dc_pred
)
75 int intra_pred_var
= 0;
80 x
->e_mbd
.mode_info_context
->mbmi
.mode
= DC_PRED
;
81 x
->e_mbd
.mode_info_context
->mbmi
.uv_mode
= DC_PRED
;
82 x
->e_mbd
.mode_info_context
->mbmi
.ref_frame
= INTRA_FRAME
;
84 vp8_encode_intra16x16mby(IF_RTCD(&cpi
->rtcd
), x
);
88 for (i
= 0; i
< 16; i
++)
90 BLOCKD
*b
= &x
->e_mbd
.block
[i
];
91 BLOCK
*be
= &x
->block
[i
];
93 vp8_encode_intra4x4block(IF_RTCD(&cpi
->rtcd
), x
, be
, b
, B_DC_PRED
);
97 intra_pred_var
= VARIANCE_INVOKE(&cpi
->rtcd
.variance
, getmbss
)(x
->src_diff
);
99 return intra_pred_var
;
102 // Resets the first pass file to the given position using a relative seek from the current position
103 static void reset_fpf_position(VP8_COMP
*cpi
, FIRSTPASS_STATS
*Position
)
105 cpi
->stats_in
= Position
;
108 static int lookup_next_frame_stats(VP8_COMP
*cpi
, FIRSTPASS_STATS
*next_frame
)
110 if (cpi
->stats_in
>= cpi
->stats_in_end
)
113 *next_frame
= *cpi
->stats_in
;
117 // Calculate a modified Error used in distributing bits between easier and harder frames
118 static double calculate_modified_err(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
120 double av_err
= cpi
->total_stats
->ssim_weighted_pred_err
;
121 double this_err
= this_frame
->ssim_weighted_pred_err
;
124 //double relative_next_iiratio;
125 //double next_iiratio;
126 //double sum_iiratio;
129 //FIRSTPASS_STATS next_frame;
130 //FIRSTPASS_STATS *start_pos;
132 /*start_pos = cpi->stats_in;
135 while ( (i < 1) && vp8_input_stats(cpi,&next_frame) != EOF )
138 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
139 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20.0 : next_iiratio;
140 sum_iiratio += next_iiratio;
145 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->avg_iiratio * (double)i);
149 relative_next_iiratio = 1.0;
151 reset_fpf_position(cpi, start_pos);*/
153 if (this_err
> av_err
)
154 modified_err
= av_err
* pow((this_err
/ DOUBLE_DIVIDE_CHECK(av_err
)), POW1
);
156 modified_err
= av_err
* pow((this_err
/ DOUBLE_DIVIDE_CHECK(av_err
)), POW2
);
159 relative_next_iiratio = pow(relative_next_iiratio,0.25);
160 modified_err = modified_err * relative_next_iiratio;
166 double vp8_simple_weight(YV12_BUFFER_CONFIG
*source
)
170 unsigned char *src
= source
->y_buffer
;
172 double sum_weights
= 0.0;
175 // Loop throught the Y plane raw examining levels and creating a weight for the image
176 for (i
= 0; i
< source
->y_height
; i
++)
178 for (j
= 0; j
< source
->y_width
; j
++)
185 Weight
= (value
- 32.0f
) / 32.0f
;
189 sum_weights
+= Weight
;
192 src
+= source
->y_stride
;
195 sum_weights
/= (source
->y_height
* source
->y_width
);
200 // This function returns the current per frame maximum bitrate target
201 int frame_max_bits(VP8_COMP
*cpi
)
203 // Max allocation for a single frame based on the max section guidelines passed in and how many bits are left
206 // For CBR we need to also consider buffer fullness.
207 // If we are running below the optimal level then we need to gradually tighten up on max_bits.
208 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
210 double buffer_fullness_ratio
= (double)cpi
->buffer_level
/ DOUBLE_DIVIDE_CHECK((double)cpi
->oxcf
.optimal_buffer_level
);
212 // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
213 max_bits
= (int)(cpi
->av_per_frame_bandwidth
* ((double)cpi
->oxcf
.two_pass_vbrmax_section
/ 100.0));
215 // If our buffer is below the optimum level
216 if (buffer_fullness_ratio
< 1.0)
218 // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
219 int min_max_bits
= ((cpi
->av_per_frame_bandwidth
>> 2) < (max_bits
>> 2)) ? cpi
->av_per_frame_bandwidth
>> 2 : max_bits
>> 2;
221 max_bits
= (int)(max_bits
* buffer_fullness_ratio
);
223 if (max_bits
< min_max_bits
)
224 max_bits
= min_max_bits
; // Lowest value we will set ... which should allow the buffer to refil.
230 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
231 max_bits
= (int)(((double)cpi
->bits_left
/ (cpi
->total_stats
->count
- (double)cpi
->common
.current_video_frame
)) * ((double)cpi
->oxcf
.two_pass_vbrmax_section
/ 100.0));
234 // Trap case where we are out of bits
242 extern size_t vp8_firstpass_stats_sz(unsigned int mb_count
)
244 /* Calculate the size of a stats packet, which is dependent on the frame
245 * resolution. The FIRSTPASS_STATS struct has a single element array,
246 * motion_map, which is virtually expanded to have one element per
250 FIRSTPASS_STATS stats
;
252 stats_sz
= sizeof(FIRSTPASS_STATS
) + mb_count
;
253 stats_sz
= (stats_sz
+ 7) & ~7;
258 void vp8_output_stats(const VP8_COMP
*cpi
,
259 struct vpx_codec_pkt_list
*pktlist
,
260 FIRSTPASS_STATS
*stats
)
262 struct vpx_codec_cx_pkt pkt
;
263 pkt
.kind
= VPX_CODEC_STATS_PKT
;
264 pkt
.data
.twopass_stats
.buf
= stats
;
265 pkt
.data
.twopass_stats
.sz
= vp8_firstpass_stats_sz(cpi
->common
.MBs
);
266 vpx_codec_pkt_list_add(pktlist
, &pkt
);
272 fpfile
= fopen("firstpass.stt", "a");
274 fprintf(fpfile
, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.0f\n",
278 stats
->ssim_weighted_pred_err
,
281 stats
->pcnt_second_ref
,
288 stats
->mv_in_out_count
,
293 fpfile
= fopen("fpmotionmap.stt", "a");
294 fwrite(cpi
->fp_motion_map
, 1, cpi
->common
.MBs
, fpfile
);
300 int vp8_input_stats(VP8_COMP
*cpi
, FIRSTPASS_STATS
*fps
)
302 size_t stats_sz
= vp8_firstpass_stats_sz(cpi
->common
.MBs
);
304 if (cpi
->stats_in
>= cpi
->stats_in_end
)
307 *fps
= *cpi
->stats_in
;
308 cpi
->stats_in
= (void*)((char *)cpi
->stats_in
+ stats_sz
);
312 void vp8_zero_stats(FIRSTPASS_STATS
*section
)
314 section
->frame
= 0.0;
315 section
->intra_error
= 0.0;
316 section
->coded_error
= 0.0;
317 section
->ssim_weighted_pred_err
= 0.0;
318 section
->pcnt_inter
= 0.0;
319 section
->pcnt_motion
= 0.0;
320 section
->pcnt_second_ref
= 0.0;
322 section
->mvr_abs
= 0.0;
324 section
->mvc_abs
= 0.0;
327 section
->mv_in_out_count
= 0.0;
328 section
->count
= 0.0;
329 section
->duration
= 1.0;
331 void vp8_accumulate_stats(FIRSTPASS_STATS
*section
, FIRSTPASS_STATS
*frame
)
333 section
->frame
+= frame
->frame
;
334 section
->intra_error
+= frame
->intra_error
;
335 section
->coded_error
+= frame
->coded_error
;
336 section
->ssim_weighted_pred_err
+= frame
->ssim_weighted_pred_err
;
337 section
->pcnt_inter
+= frame
->pcnt_inter
;
338 section
->pcnt_motion
+= frame
->pcnt_motion
;
339 section
->pcnt_second_ref
+= frame
->pcnt_second_ref
;
340 section
->MVr
+= frame
->MVr
;
341 section
->mvr_abs
+= frame
->mvr_abs
;
342 section
->MVc
+= frame
->MVc
;
343 section
->mvc_abs
+= frame
->mvc_abs
;
344 section
->MVrv
+= frame
->MVrv
;
345 section
->MVcv
+= frame
->MVcv
;
346 section
->mv_in_out_count
+= frame
->mv_in_out_count
;
347 section
->count
+= frame
->count
;
348 section
->duration
+= frame
->duration
;
350 void vp8_avg_stats(FIRSTPASS_STATS
*section
)
352 if (section
->count
< 1.0)
355 section
->intra_error
/= section
->count
;
356 section
->coded_error
/= section
->count
;
357 section
->ssim_weighted_pred_err
/= section
->count
;
358 section
->pcnt_inter
/= section
->count
;
359 section
->pcnt_second_ref
/= section
->count
;
360 section
->pcnt_motion
/= section
->count
;
361 section
->MVr
/= section
->count
;
362 section
->mvr_abs
/= section
->count
;
363 section
->MVc
/= section
->count
;
364 section
->mvc_abs
/= section
->count
;
365 section
->MVrv
/= section
->count
;
366 section
->MVcv
/= section
->count
;
367 section
->mv_in_out_count
/= section
->count
;
368 section
->duration
/= section
->count
;
371 unsigned char *vp8_fpmm_get_pos(VP8_COMP
*cpi
)
373 return cpi
->fp_motion_map_stats
;
375 void vp8_fpmm_reset_pos(VP8_COMP
*cpi
, unsigned char *target_pos
)
379 cpi
->fp_motion_map_stats
= target_pos
;
382 void vp8_advance_fpmm(VP8_COMP
*cpi
, int count
)
384 cpi
->fp_motion_map_stats
= (void*)((char*)cpi
->fp_motion_map_stats
+
385 count
* vp8_firstpass_stats_sz(cpi
->common
.MBs
));
388 void vp8_input_fpmm(VP8_COMP
*cpi
)
390 unsigned char *fpmm
= cpi
->fp_motion_map
;
391 int MBs
= cpi
->common
.MBs
;
392 int max_frames
= cpi
->active_arnr_frames
;
395 for (i
=0; i
<max_frames
; i
++)
397 char *motion_map
= (char*)cpi
->fp_motion_map_stats
398 + sizeof(FIRSTPASS_STATS
);
400 memcpy(fpmm
, motion_map
, MBs
);
402 vp8_advance_fpmm(cpi
, 1);
405 // Flag the use of weights in the temporal filter
406 cpi
->use_weighted_temporal_filter
= 1;
409 void vp8_init_first_pass(VP8_COMP
*cpi
)
411 vp8_zero_stats(cpi
->total_stats
);
417 fpfile
= fopen("firstpass.stt", "w");
419 fpfile
= fopen("fpmotionmap.stt", "wb");
426 void vp8_end_first_pass(VP8_COMP
*cpi
)
428 vp8_output_stats(cpi
, cpi
->output_pkt_list
, cpi
->total_stats
);
432 void vp8_zz_motion_search( VP8_COMP
*cpi
, MACROBLOCK
* x
, YV12_BUFFER_CONFIG
* recon_buffer
, int * best_motion_err
, int recon_yoffset
)
434 MACROBLOCKD
* const xd
= & x
->e_mbd
;
435 BLOCK
*b
= &x
->block
[0];
436 BLOCKD
*d
= &x
->e_mbd
.block
[0];
438 unsigned char *src_ptr
= (*(b
->base_src
) + b
->src
);
439 int src_stride
= b
->src_stride
;
440 unsigned char *ref_ptr
;
441 int ref_stride
=d
->pre_stride
;
443 // Set up pointers for this macro block recon buffer
444 xd
->pre
.y_buffer
= recon_buffer
->y_buffer
+ recon_yoffset
;
446 ref_ptr
= (unsigned char *)(*(d
->base_pre
) + d
->pre
);
448 VARIANCE_INVOKE(IF_RTCD(&cpi
->rtcd
.variance
), mse16x16
) ( src_ptr
, src_stride
, ref_ptr
, ref_stride
, (unsigned int *)(best_motion_err
));
452 void vp8_first_pass_motion_search(VP8_COMP
*cpi
, MACROBLOCK
*x
, MV
*ref_mv
, MV
*best_mv
, YV12_BUFFER_CONFIG
*recon_buffer
, int *best_motion_err
, int recon_yoffset
)
454 MACROBLOCKD
*const xd
= & x
->e_mbd
;
455 BLOCK
*b
= &x
->block
[0];
456 BLOCKD
*d
= &x
->e_mbd
.block
[0];
462 int step_param
= 3; //3; // Dont search over full range for first pass
463 int further_steps
= (MAX_MVSEARCH_STEPS
- 1) - step_param
; //3;
465 vp8_variance_fn_ptr_t v_fn_ptr
;
466 int new_mv_mode_penalty
= 256;
468 v_fn_ptr
.vf
= VARIANCE_INVOKE(IF_RTCD(&cpi
->rtcd
.variance
), mse16x16
);
469 v_fn_ptr
.sdf
= cpi
->fn_ptr
.sdf
;
470 v_fn_ptr
.sdx4df
= cpi
->fn_ptr
.sdx4df
;
472 // Set up pointers for this macro block recon buffer
473 xd
->pre
.y_buffer
= recon_buffer
->y_buffer
+ recon_yoffset
;
475 // Initial step/diamond search centred on best mv
476 tmp_err
= cpi
->diamond_search_sad(x
, b
, d
, ref_mv
, &tmp_mv
, step_param
, x
->errorperbit
, &num00
, &v_fn_ptr
, x
->mvsadcost
, x
->mvcost
);
477 if ( tmp_err
< INT_MAX
-new_mv_mode_penalty
)
478 tmp_err
+= new_mv_mode_penalty
;
480 if (tmp_err
< *best_motion_err
)
482 *best_motion_err
= tmp_err
;
483 best_mv
->row
= tmp_mv
.row
;
484 best_mv
->col
= tmp_mv
.col
;
487 // Further step/diamond searches as necessary
491 while (n
< further_steps
)
499 tmp_err
= cpi
->diamond_search_sad(x
, b
, d
, ref_mv
, &tmp_mv
, step_param
+ n
, x
->errorperbit
, &num00
, &v_fn_ptr
, x
->mvsadcost
, x
->mvcost
);
500 if ( tmp_err
< INT_MAX
-new_mv_mode_penalty
)
501 tmp_err
+= new_mv_mode_penalty
;
503 if (tmp_err
< *best_motion_err
)
505 *best_motion_err
= tmp_err
;
506 best_mv
->row
= tmp_mv
.row
;
507 best_mv
->col
= tmp_mv
.col
;
513 void vp8_first_pass(VP8_COMP
*cpi
)
516 MACROBLOCK
*const x
= & cpi
->mb
;
517 VP8_COMMON
*const cm
= & cpi
->common
;
518 MACROBLOCKD
*const xd
= & x
->e_mbd
;
520 int col_blocks
= 4 * cm
->mb_cols
;
521 int recon_yoffset
, recon_uvoffset
;
522 YV12_BUFFER_CONFIG
*lst_yv12
= &cm
->yv12_fb
[cm
->lst_fb_idx
];
523 YV12_BUFFER_CONFIG
*new_yv12
= &cm
->yv12_fb
[cm
->new_fb_idx
];
524 YV12_BUFFER_CONFIG
*gld_yv12
= &cm
->yv12_fb
[cm
->gld_fb_idx
];
525 int recon_y_stride
= lst_yv12
->y_stride
;
526 int recon_uv_stride
= lst_yv12
->uv_stride
;
530 int sum_mvr
= 0, sum_mvc
= 0;
531 int sum_mvr_abs
= 0, sum_mvc_abs
= 0;
532 int sum_mvrs
= 0, sum_mvcs
= 0;
535 int second_ref_count
= 0;
536 int intrapenalty
= 256;
538 int sum_in_vectors
= 0;
540 MV best_ref_mv
= {0, 0};
541 MV zero_ref_mv
= {0, 0};
543 unsigned char *fp_motion_map_ptr
= cpi
->fp_motion_map
;
545 vp8_clear_system_state(); //__asm emms;
547 x
->src
= * cpi
->Source
;
551 x
->partition_info
= x
->pi
;
553 xd
->mode_info_context
= cm
->mi
;
555 vp8_build_block_offsets(x
);
557 vp8_setup_block_dptrs(&x
->e_mbd
);
559 vp8_setup_block_ptrs(x
);
561 // set up frame new frame for intra coded blocks
562 vp8_setup_intra_recon(new_yv12
);
563 vp8cx_frame_init_quantizer(cpi
);
565 // Initialise the MV cost table to the defaults
566 //if( cm->current_video_frame == 0)
569 int flag
[2] = {1, 1};
570 vp8_initialize_rd_consts(cpi
, vp8_dc_quant(cm
->base_qindex
, cm
->y1dc_delta_q
));
571 vpx_memcpy(cm
->fc
.mvc
, vp8_default_mv_context
, sizeof(vp8_default_mv_context
));
572 vp8_build_component_cost_table(cpi
->mb
.mvcost
, cpi
->mb
.mvsadcost
, (const MV_CONTEXT
*) cm
->fc
.mvc
, flag
);
575 // for each macroblock row in image
576 for (mb_row
= 0; mb_row
< cm
->mb_rows
; mb_row
++)
578 MV best_ref_mv
= {0, 0};
580 // reset above block coeffs
581 xd
->up_available
= (mb_row
!= 0);
582 recon_yoffset
= (mb_row
* recon_y_stride
* 16);
583 recon_uvoffset
= (mb_row
* recon_uv_stride
* 8);
585 // for each macroblock col in image
586 for (mb_col
= 0; mb_col
< cm
->mb_cols
; mb_col
++)
590 int zz_to_best_ratio
;
591 int gf_motion_error
= INT_MAX
;
592 int use_dc_pred
= (mb_col
|| mb_row
) && (!mb_col
|| !mb_row
);
594 xd
->dst
.y_buffer
= new_yv12
->y_buffer
+ recon_yoffset
;
595 xd
->dst
.u_buffer
= new_yv12
->u_buffer
+ recon_uvoffset
;
596 xd
->dst
.v_buffer
= new_yv12
->v_buffer
+ recon_uvoffset
;
597 xd
->left_available
= (mb_col
!= 0);
599 // do intra 16x16 prediction
600 this_error
= vp8_encode_intra(cpi
, x
, use_dc_pred
);
602 // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
603 // 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.
604 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
605 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
606 this_error
+= intrapenalty
;
608 // Cumulative intra error total
609 intra_error
+= this_error
;
611 // Indicate default assumption of intra in the motion map
612 *fp_motion_map_ptr
= 0;
614 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
615 x
->mv_col_min
= -((mb_col
* 16) + (VP8BORDERINPIXELS
- 16));
616 x
->mv_col_max
= ((cm
->mb_cols
- 1 - mb_col
) * 16) + (VP8BORDERINPIXELS
- 16);
617 x
->mv_row_min
= -((mb_row
* 16) + (VP8BORDERINPIXELS
- 16));
618 x
->mv_row_max
= ((cm
->mb_rows
- 1 - mb_row
) * 16) + (VP8BORDERINPIXELS
- 16);
620 // Other than for the first frame do a motion search
621 if (cm
->current_video_frame
> 0)
623 BLOCK
*b
= &x
->block
[0];
624 BLOCKD
*d
= &x
->e_mbd
.block
[0];
627 int motion_error
= INT_MAX
;
629 // Simple 0,0 motion with no mv overhead
630 vp8_zz_motion_search( cpi
, x
, lst_yv12
, &motion_error
, recon_yoffset
);
631 d
->bmi
.mv
.as_mv
.row
= 0;
632 d
->bmi
.mv
.as_mv
.col
= 0;
634 // Save (0,0) error for later use
635 zero_error
= motion_error
;
637 // Test last reference frame using the previous best mv as the
638 // starting point (best reference) for the search
639 vp8_first_pass_motion_search(cpi
, x
, &best_ref_mv
,
640 &d
->bmi
.mv
.as_mv
, lst_yv12
,
641 &motion_error
, recon_yoffset
);
643 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
644 if ((best_ref_mv
.col
!= 0) || (best_ref_mv
.row
!= 0))
647 vp8_first_pass_motion_search(cpi
, x
, &zero_ref_mv
, &tmp_mv
,
648 lst_yv12
, &tmp_err
, recon_yoffset
);
650 if ( tmp_err
< motion_error
)
652 motion_error
= tmp_err
;
653 d
->bmi
.mv
.as_mv
.row
= tmp_mv
.row
;
654 d
->bmi
.mv
.as_mv
.col
= tmp_mv
.col
;
659 // Experimental search in a second reference frame ((0,0) based only)
660 if (cm
->current_video_frame
> 1)
662 vp8_first_pass_motion_search(cpi
, x
, &zero_ref_mv
, &tmp_mv
, gld_yv12
, &gf_motion_error
, recon_yoffset
);
664 if ((gf_motion_error
< motion_error
) && (gf_motion_error
< this_error
))
667 //motion_error = gf_motion_error;
668 //d->bmi.mv.as_mv.row = tmp_mv.row;
669 //d->bmi.mv.as_mv.col = tmp_mv.col;
673 xd->pre.y_buffer = cm->last_frame.y_buffer + recon_yoffset;
674 xd->pre.u_buffer = cm->last_frame.u_buffer + recon_uvoffset;
675 xd->pre.v_buffer = cm->last_frame.v_buffer + recon_uvoffset;
679 // Reset to last frame as reference buffer
680 xd
->pre
.y_buffer
= lst_yv12
->y_buffer
+ recon_yoffset
;
681 xd
->pre
.u_buffer
= lst_yv12
->u_buffer
+ recon_uvoffset
;
682 xd
->pre
.v_buffer
= lst_yv12
->v_buffer
+ recon_uvoffset
;
685 if (motion_error
<= this_error
)
687 d
->bmi
.mv
.as_mv
.row
<<= 3;
688 d
->bmi
.mv
.as_mv
.col
<<= 3;
689 this_error
= motion_error
;
690 vp8_set_mbmode_and_mvs(x
, NEWMV
, &d
->bmi
.mv
.as_mv
);
691 vp8_encode_inter16x16y(IF_RTCD(&cpi
->rtcd
), x
);
692 sum_mvr
+= d
->bmi
.mv
.as_mv
.row
;
693 sum_mvr_abs
+= abs(d
->bmi
.mv
.as_mv
.row
);
694 sum_mvc
+= d
->bmi
.mv
.as_mv
.col
;
695 sum_mvc_abs
+= abs(d
->bmi
.mv
.as_mv
.col
);
696 sum_mvrs
+= d
->bmi
.mv
.as_mv
.row
* d
->bmi
.mv
.as_mv
.row
;
697 sum_mvcs
+= d
->bmi
.mv
.as_mv
.col
* d
->bmi
.mv
.as_mv
.col
;
700 best_ref_mv
.row
= d
->bmi
.mv
.as_mv
.row
;
701 best_ref_mv
.col
= d
->bmi
.mv
.as_mv
.col
;
702 //best_ref_mv.row = 0;
703 //best_ref_mv.col = 0;
705 // Was the vector non-zero
706 if (d
->bmi
.mv
.as_mv
.row
|| d
->bmi
.mv
.as_mv
.col
)
710 // Does the Row vector point inwards or outwards
711 if (mb_row
< cm
->mb_rows
/ 2)
713 if (d
->bmi
.mv
.as_mv
.row
> 0)
715 else if (d
->bmi
.mv
.as_mv
.row
< 0)
718 else if (mb_row
> cm
->mb_rows
/ 2)
720 if (d
->bmi
.mv
.as_mv
.row
> 0)
722 else if (d
->bmi
.mv
.as_mv
.row
< 0)
726 // Does the Row vector point inwards or outwards
727 if (mb_col
< cm
->mb_cols
/ 2)
729 if (d
->bmi
.mv
.as_mv
.col
> 0)
731 else if (d
->bmi
.mv
.as_mv
.col
< 0)
734 else if (mb_col
> cm
->mb_cols
/ 2)
736 if (d
->bmi
.mv
.as_mv
.col
> 0)
738 else if (d
->bmi
.mv
.as_mv
.col
< 0)
742 // Compute how close (0,0) predictor is to best
743 // predictor in terms of their prediction error
744 zz_to_best_ratio
= (10*zero_error
+ this_error
/2)
745 / (this_error
+!this_error
);
747 if ((zero_error
< 50000) &&
748 (zz_to_best_ratio
<= 11) )
749 *fp_motion_map_ptr
= 1;
751 *fp_motion_map_ptr
= 0;
756 if( zero_error
<50000 )
757 *fp_motion_map_ptr
= 2;
759 *fp_motion_map_ptr
= 1;
770 coded_error
+= this_error
;
772 // adjust to the next column of macroblocks
773 x
->src
.y_buffer
+= 16;
774 x
->src
.u_buffer
+= 8;
775 x
->src
.v_buffer
+= 8;
780 // Update the motion map
784 // adjust to the next row of mbs
785 x
->src
.y_buffer
+= 16 * x
->src
.y_stride
- 16 * cm
->mb_cols
;
786 x
->src
.u_buffer
+= 8 * x
->src
.uv_stride
- 8 * cm
->mb_cols
;
787 x
->src
.v_buffer
+= 8 * x
->src
.uv_stride
- 8 * cm
->mb_cols
;
789 //extend the recon for intra prediction
790 vp8_extend_mb_row(new_yv12
, xd
->dst
.y_buffer
+ 16, xd
->dst
.u_buffer
+ 8, xd
->dst
.v_buffer
+ 8);
791 vp8_clear_system_state(); //__asm emms;
794 vp8_clear_system_state(); //__asm emms;
800 fps
.frame
= cm
->current_video_frame
;
801 fps
.intra_error
= intra_error
>> 8;
802 fps
.coded_error
= coded_error
>> 8;
803 weight
= vp8_simple_weight(cpi
->Source
);
808 fps
.ssim_weighted_pred_err
= fps
.coded_error
* weight
;
810 fps
.pcnt_inter
= 0.0;
811 fps
.pcnt_motion
= 0.0;
818 fps
.mv_in_out_count
= 0.0;
821 fps
.pcnt_inter
= 1.0 * (double)intercount
/ cm
->MBs
;
822 fps
.pcnt_second_ref
= 1.0 * (double)second_ref_count
/ cm
->MBs
;
826 fps
.MVr
= (double)sum_mvr
/ (double)mvcount
;
827 fps
.mvr_abs
= (double)sum_mvr_abs
/ (double)mvcount
;
828 fps
.MVc
= (double)sum_mvc
/ (double)mvcount
;
829 fps
.mvc_abs
= (double)sum_mvc_abs
/ (double)mvcount
;
830 fps
.MVrv
= ((double)sum_mvrs
- (fps
.MVr
* fps
.MVr
/ (double)mvcount
)) / (double)mvcount
;
831 fps
.MVcv
= ((double)sum_mvcs
- (fps
.MVc
* fps
.MVc
/ (double)mvcount
)) / (double)mvcount
;
832 fps
.mv_in_out_count
= (double)sum_in_vectors
/ (double)(mvcount
* 2);
834 fps
.pcnt_motion
= 1.0 * (double)mvcount
/ cpi
->common
.MBs
;
837 // TODO: handle the case when duration is set to 0, or something less
838 // than the full time between subsequent cpi->source_time_stamp s .
839 fps
.duration
= cpi
->source_end_time_stamp
- cpi
->source_time_stamp
;
841 // don't want to do outputstats with a stack variable!
842 memcpy(cpi
->this_frame_stats
,
844 sizeof(FIRSTPASS_STATS
));
845 memcpy((char*)cpi
->this_frame_stats
+ sizeof(FIRSTPASS_STATS
),
847 sizeof(cpi
->fp_motion_map
[0]) * cpi
->common
.MBs
);
848 vp8_output_stats(cpi
, cpi
->output_pkt_list
, cpi
->this_frame_stats
);
849 vp8_accumulate_stats(cpi
->total_stats
, &fps
);
852 // Copy the previous Last Frame into the GF buffer if specific conditions for doing so are met
853 if ((cm
->current_video_frame
> 0) &&
854 (cpi
->this_frame_stats
->pcnt_inter
> 0.20) &&
855 ((cpi
->this_frame_stats
->intra_error
/ cpi
->this_frame_stats
->coded_error
) > 2.0))
857 vp8_yv12_copy_frame_ptr(lst_yv12
, gld_yv12
);
860 // swap frame pointers so last frame refers to the frame we just compressed
861 vp8_swap_yv12_buffer(lst_yv12
, new_yv12
);
862 vp8_yv12_extend_frame_borders(lst_yv12
);
864 // Special case for the first frame. Copy into the GF buffer as a second reference.
865 if (cm
->current_video_frame
== 0)
867 vp8_yv12_copy_frame_ptr(lst_yv12
, gld_yv12
);
871 // use this to see what the first pass reconstruction looks like
876 sprintf(filename
, "enc%04d.yuv", (int) cm
->current_video_frame
);
878 if (cm
->current_video_frame
== 0)
879 recon_file
= fopen(filename
, "wb");
881 recon_file
= fopen(filename
, "ab");
883 fwrite(lst_yv12
->buffer_alloc
, lst_yv12
->frame_size
, 1, recon_file
);
887 cm
->current_video_frame
++;
890 extern const int vp8_bits_per_mb
[2][QINDEX_RANGE
];
892 #define BASE_ERRPERMB 150
893 static int estimate_max_q(VP8_COMP
*cpi
, double section_err
, int section_target_bandwitdh
, int Height
, int Width
)
896 int num_mbs
= ((Height
* Width
) / (16 * 16));
897 int target_norm_bits_per_mb
;
899 double err_per_mb
= section_err
/ num_mbs
;
900 double correction_factor
;
902 double speed_correction
= 1.0;
903 double rolling_ratio
;
905 double pow_highq
= 0.90;
906 double pow_lowq
= 0.40;
908 if (section_target_bandwitdh
<= 0)
911 target_norm_bits_per_mb
= (section_target_bandwitdh
< (1 << 20)) ? (512 * section_target_bandwitdh
) / num_mbs
: 512 * (section_target_bandwitdh
/ num_mbs
);
913 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
914 if ((cpi
->rolling_target_bits
> 0.0) && (cpi
->active_worst_quality
< cpi
->worst_quality
))
916 //double adjustment_rate = 0.985 + (0.00005 * cpi->active_worst_quality);
917 double adjustment_rate
= 0.99;
919 rolling_ratio
= (double)cpi
->rolling_actual_bits
/ (double)cpi
->rolling_target_bits
;
921 //if ( cpi->est_max_qcorrection_factor > rolling_ratio )
922 if (rolling_ratio
< 0.95)
923 //cpi->est_max_qcorrection_factor *= adjustment_rate;
924 cpi
->est_max_qcorrection_factor
-= 0.005;
925 //else if ( cpi->est_max_qcorrection_factor < rolling_ratio )
926 else if (rolling_ratio
> 1.05)
927 cpi
->est_max_qcorrection_factor
+= 0.005;
929 //cpi->est_max_qcorrection_factor /= adjustment_rate;
931 cpi
->est_max_qcorrection_factor
= (cpi
->est_max_qcorrection_factor
< 0.1) ? 0.1 : (cpi
->est_max_qcorrection_factor
> 10.0) ? 10.0 : cpi
->est_max_qcorrection_factor
;
934 // Corrections for higher compression speed settings (reduced compression expected)
935 if ((cpi
->compressor_speed
== 3) || (cpi
->compressor_speed
== 1))
937 if (cpi
->oxcf
.cpu_used
<= 5)
938 speed_correction
= 1.04 + (cpi
->oxcf
.cpu_used
* 0.04);
940 speed_correction
= 1.25;
943 // Correction factor used for Q values >= 20
944 corr_high
= pow(err_per_mb
/ BASE_ERRPERMB
, pow_highq
);
945 corr_high
= (corr_high
< 0.05) ? 0.05 : (corr_high
> 5.0) ? 5.0 : corr_high
;
947 // Try and pick a Q that should be high enough to encode the content at the given rate.
948 for (Q
= 0; Q
< MAXQ
; Q
++)
950 int bits_per_mb_at_this_q
;
954 correction_factor
= pow(err_per_mb
/ BASE_ERRPERMB
, (pow_lowq
+ Q
* 0.01));
955 correction_factor
= (correction_factor
< 0.05) ? 0.05 : (correction_factor
> 5.0) ? 5.0 : correction_factor
;
958 correction_factor
= corr_high
;
960 bits_per_mb_at_this_q
= (int)(.5 + correction_factor
* speed_correction
* cpi
->est_max_qcorrection_factor
* cpi
->section_max_qfactor
* (double)vp8_bits_per_mb
[INTER_FRAME
][Q
] / 1.0);
961 //bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
963 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
969 static int estimate_q(VP8_COMP
*cpi
, double section_err
, int section_target_bandwitdh
, int Height
, int Width
)
972 int num_mbs
= ((Height
* Width
) / (16 * 16));
973 int target_norm_bits_per_mb
;
975 double err_per_mb
= section_err
/ num_mbs
;
976 double correction_factor
;
978 double speed_correction
= 1.0;
979 double pow_highq
= 0.90;
980 double pow_lowq
= 0.40;
982 target_norm_bits_per_mb
= (section_target_bandwitdh
< (1 << 20)) ? (512 * section_target_bandwitdh
) / num_mbs
: 512 * (section_target_bandwitdh
/ num_mbs
);
984 // Corrections for higher compression speed settings (reduced compression expected)
985 if ((cpi
->compressor_speed
== 3) || (cpi
->compressor_speed
== 1))
987 if (cpi
->oxcf
.cpu_used
<= 5)
988 speed_correction
= 1.04 + (cpi
->oxcf
.cpu_used
* 0.04);
990 speed_correction
= 1.25;
993 // Correction factor used for Q values >= 20
994 corr_high
= pow(err_per_mb
/ BASE_ERRPERMB
, pow_highq
);
995 corr_high
= (corr_high
< 0.05) ? 0.05 : (corr_high
> 5.0) ? 5.0 : corr_high
;
997 // Try and pick a Q that can encode the content at the given rate.
998 for (Q
= 0; Q
< MAXQ
; Q
++)
1000 int bits_per_mb_at_this_q
;
1004 correction_factor
= pow(err_per_mb
/ BASE_ERRPERMB
, (pow_lowq
+ Q
* 0.01));
1005 correction_factor
= (correction_factor
< 0.05) ? 0.05 : (correction_factor
> 5.0) ? 5.0 : correction_factor
;
1008 correction_factor
= corr_high
;
1010 bits_per_mb_at_this_q
= (int)(.5 + correction_factor
* speed_correction
* cpi
->est_max_qcorrection_factor
* (double)vp8_bits_per_mb
[INTER_FRAME
][Q
] / 1.0);
1012 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
1019 // Estimate a worst case Q for a KF group
1020 static int estimate_kf_group_q(VP8_COMP
*cpi
, double section_err
, int section_target_bandwitdh
, int Height
, int Width
, double group_iiratio
)
1023 int num_mbs
= ((Height
* Width
) / (16 * 16));
1024 int target_norm_bits_per_mb
= (512 * section_target_bandwitdh
) / num_mbs
;
1025 int bits_per_mb_at_this_q
;
1027 double err_per_mb
= section_err
/ num_mbs
;
1028 double err_correction_factor
;
1030 double speed_correction
= 1.0;
1031 double current_spend_ratio
= 1.0;
1033 double pow_highq
= (POW1
< 0.6) ? POW1
+ 0.3 : 0.90;
1034 double pow_lowq
= (POW1
< 0.7) ? POW1
+ 0.1 : 0.80;
1036 double iiratio_correction_factor
= 1.0;
1038 double combined_correction_factor
;
1040 // Trap special case where the target is <= 0
1041 if (target_norm_bits_per_mb
<= 0)
1044 // Calculate a corrective factor based on a rolling ratio of bits spent vs target bits
1045 // This is clamped to the range 0.1 to 10.0
1046 if (cpi
->long_rolling_target_bits
<= 0)
1047 current_spend_ratio
= 10.0;
1050 current_spend_ratio
= (double)cpi
->long_rolling_actual_bits
/ (double)cpi
->long_rolling_target_bits
;
1051 current_spend_ratio
= (current_spend_ratio
> 10.0) ? 10.0 : (current_spend_ratio
< 0.1) ? 0.1 : current_spend_ratio
;
1054 // Calculate a correction factor based on the quality of prediction in the sequence as indicated by intra_inter error score ratio (IIRatio)
1055 // The idea here is to favour subsampling in the hardest sections vs the easyest.
1056 iiratio_correction_factor
= 1.0 - ((group_iiratio
- 6.0) * 0.1);
1058 if (iiratio_correction_factor
< 0.5)
1059 iiratio_correction_factor
= 0.5;
1061 // Corrections for higher compression speed settings (reduced compression expected)
1062 if ((cpi
->compressor_speed
== 3) || (cpi
->compressor_speed
== 1))
1064 if (cpi
->oxcf
.cpu_used
<= 5)
1065 speed_correction
= 1.04 + (cpi
->oxcf
.cpu_used
* 0.04);
1067 speed_correction
= 1.25;
1070 // Combine the various factors calculated above
1071 combined_correction_factor
= speed_correction
* iiratio_correction_factor
* current_spend_ratio
;
1073 // Correction factor used for Q values >= 20
1074 corr_high
= pow(err_per_mb
/ BASE_ERRPERMB
, pow_highq
);
1075 corr_high
= (corr_high
< 0.05) ? 0.05 : (corr_high
> 5.0) ? 5.0 : corr_high
;
1077 // Try and pick a Q that should be high enough to encode the content at the given rate.
1078 for (Q
= 0; Q
< MAXQ
; Q
++)
1080 // Q values < 20 treated as a special case
1083 err_correction_factor
= pow(err_per_mb
/ BASE_ERRPERMB
, (pow_lowq
+ Q
* 0.01));
1084 err_correction_factor
= (err_correction_factor
< 0.05) ? 0.05 : (err_correction_factor
> 5.0) ? 5.0 : err_correction_factor
;
1087 err_correction_factor
= corr_high
;
1089 bits_per_mb_at_this_q
= (int)(.5 + err_correction_factor
* combined_correction_factor
* (double)vp8_bits_per_mb
[INTER_FRAME
][Q
]);
1091 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
1095 // If we could not hit the target even at Max Q then estimate what Q would have bee required
1096 while ((bits_per_mb_at_this_q
> target_norm_bits_per_mb
) && (Q
< (MAXQ
* 2)))
1099 bits_per_mb_at_this_q
= (int)(0.96 * bits_per_mb_at_this_q
);
1105 FILE *f
= fopen("estkf_q.stt", "a");
1106 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
,
1107 target_norm_bits_per_mb
, err_per_mb
, err_correction_factor
,
1108 current_spend_ratio
, group_iiratio
, iiratio_correction_factor
,
1109 (double)cpi
->buffer_level
/ (double)cpi
->oxcf
.optimal_buffer_level
, Q
);
1115 extern void vp8_new_frame_rate(VP8_COMP
*cpi
, double framerate
);
1117 void vp8_init_second_pass(VP8_COMP
*cpi
)
1119 FIRSTPASS_STATS this_frame
;
1120 FIRSTPASS_STATS
*start_pos
;
1122 double two_pass_min_rate
= (double)(cpi
->oxcf
.target_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
1124 vp8_zero_stats(cpi
->total_stats
);
1126 if (!cpi
->stats_in_end
)
1129 *cpi
->total_stats
= *cpi
->stats_in_end
;
1131 cpi
->total_error_left
= cpi
->total_stats
->ssim_weighted_pred_err
;
1132 cpi
->total_intra_error_left
= cpi
->total_stats
->intra_error
;
1133 cpi
->total_coded_error_left
= cpi
->total_stats
->coded_error
;
1134 cpi
->start_tot_err_left
= cpi
->total_error_left
;
1136 //cpi->bits_left = (long long)(cpi->total_stats->count * cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1137 //cpi->bits_left -= (long long)(cpi->total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1139 // each frame can have a different duration, as the frame rate in the source
1140 // isn't guaranteed to be constant. The frame rate prior to the first frame
1141 // encoded in the second pass is a guess. However the sum duration is not.
1142 // Its calculated based on the actual durations of all frames from the first
1144 vp8_new_frame_rate(cpi
, 10000000.0 * cpi
->total_stats
->count
/ cpi
->total_stats
->duration
);
1146 cpi
->output_frame_rate
= cpi
->oxcf
.frame_rate
;
1147 cpi
->bits_left
= (long long)(cpi
->total_stats
->duration
* cpi
->oxcf
.target_bandwidth
/ 10000000.0) ;
1148 cpi
->bits_left
-= (long long)(cpi
->total_stats
->duration
* two_pass_min_rate
/ 10000000.0);
1150 vp8_avg_stats(cpi
->total_stats
);
1152 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1154 double sum_iiratio
= 0.0;
1157 start_pos
= cpi
->stats_in
; // Note starting "file" position
1159 while (vp8_input_stats(cpi
, &this_frame
) != EOF
)
1161 IIRatio
= this_frame
.intra_error
/ DOUBLE_DIVIDE_CHECK(this_frame
.coded_error
);
1162 IIRatio
= (IIRatio
< 1.0) ? 1.0 : (IIRatio
> 20.0) ? 20.0 : IIRatio
;
1163 sum_iiratio
+= IIRatio
;
1166 cpi
->avg_iiratio
= sum_iiratio
/ DOUBLE_DIVIDE_CHECK((double)cpi
->total_stats
->count
);
1168 // Reset file position
1169 reset_fpf_position(cpi
, start_pos
);
1172 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1173 // used to allocate bits
1175 start_pos
= cpi
->stats_in
; // Note starting "file" position
1177 cpi
->modified_total_error_left
= 0.0;
1179 while (vp8_input_stats(cpi
, &this_frame
) != EOF
)
1181 cpi
->modified_total_error_left
+= calculate_modified_err(cpi
, &this_frame
);
1184 reset_fpf_position(cpi
, start_pos
); // Reset file position
1188 cpi
->fp_motion_map_stats
= (unsigned char *)cpi
->stats_in
;
1191 void vp8_end_second_pass(VP8_COMP
*cpi
)
1195 // Analyse and define a gf/arf group .
1196 static void define_gf_group(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
1198 FIRSTPASS_STATS next_frame
;
1199 FIRSTPASS_STATS
*start_pos
;
1201 int y_width
= cpi
->common
.yv12_fb
[cpi
->common
.lst_fb_idx
].y_width
;
1202 int y_height
= cpi
->common
.yv12_fb
[cpi
->common
.lst_fb_idx
].y_height
;
1203 int image_size
= y_width
* y_height
;
1204 double boost_score
= 0.0;
1205 double old_boost_score
= 0.0;
1206 double gf_group_err
= 0.0;
1207 double gf_first_frame_err
= 0.0;
1208 double mod_frame_err
= 0.0;
1210 double mv_accumulator_rabs
= 0.0;
1211 double mv_accumulator_cabs
= 0.0;
1212 double mv_ratio_accumulator
= 0.0;
1213 double decay_accumulator
= 1.0;
1215 double boost_factor
= IIFACTOR
;
1216 double loop_decay_rate
= 1.00; // Starting decay rate
1218 double this_frame_mv_in_out
= 0.0;
1219 double mv_in_out_accumulator
= 0.0;
1220 double abs_mv_in_out_accumulator
= 0.0;
1221 double mod_err_per_mb_accumulator
= 0.0;
1223 int max_bits
= frame_max_bits(cpi
); // Max for a single frame
1225 unsigned char *fpmm_pos
;
1227 cpi
->gf_group_bits
= 0;
1228 cpi
->gf_decay_rate
= 0;
1230 vp8_clear_system_state(); //__asm emms;
1232 fpmm_pos
= vp8_fpmm_get_pos(cpi
);
1234 start_pos
= cpi
->stats_in
;
1236 vpx_memset(&next_frame
, 0, sizeof(next_frame
)); // assure clean
1238 // Preload the stats for the next frame.
1239 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1241 // Note the error of the frame at the start of the group (this will be the GF frame error if we code a normal gf
1242 gf_first_frame_err
= mod_frame_err
;
1244 // Special treatment if the current frame is a key frame (which is also a gf).
1245 // If it is then its error score (and hence bit allocation) need to be subtracted out
1246 // from the calculation for the GF group
1247 if (cpi
->common
.frame_type
== KEY_FRAME
)
1248 gf_group_err
-= gf_first_frame_err
;
1250 // Scan forward to try and work out how many frames the next gf group should contain and
1251 // what level of boost is appropriate for the GF or ARF that will be coded with the group
1254 while (((i
< cpi
->max_gf_interval
) || ((cpi
->frames_to_key
- i
) < MIN_GF_INTERVAL
)) && (i
< cpi
->frames_to_key
))
1257 double this_frame_mvr_ratio
;
1258 double this_frame_mvc_ratio
;
1259 double motion_decay
;
1260 double motion_pct
= next_frame
.pcnt_motion
;
1262 i
++; // Increment the loop counter
1264 // Accumulate error score of frames in this gf group
1265 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1267 gf_group_err
+= mod_frame_err
;
1269 mod_err_per_mb_accumulator
+= mod_frame_err
/ DOUBLE_DIVIDE_CHECK((double)cpi
->common
.MBs
);
1271 if (EOF
== vp8_input_stats(cpi
, &next_frame
))
1274 // Accumulate motion stats.
1275 mv_accumulator_rabs
+= fabs(next_frame
.mvr_abs
* motion_pct
);
1276 mv_accumulator_cabs
+= fabs(next_frame
.mvc_abs
* motion_pct
);
1278 //Accumulate Motion In/Out of frame stats
1279 this_frame_mv_in_out
= next_frame
.mv_in_out_count
* next_frame
.pcnt_motion
;
1280 mv_in_out_accumulator
+= next_frame
.mv_in_out_count
* next_frame
.pcnt_motion
;
1281 abs_mv_in_out_accumulator
+= fabs(next_frame
.mv_in_out_count
* next_frame
.pcnt_motion
);
1283 // If there is a significant amount of motion
1284 if (motion_pct
> 0.05)
1286 this_frame_mvr_ratio
= fabs(next_frame
.mvr_abs
) /
1287 DOUBLE_DIVIDE_CHECK(fabs(next_frame
.MVr
));
1289 this_frame_mvc_ratio
= fabs(next_frame
.mvc_abs
) /
1290 DOUBLE_DIVIDE_CHECK(fabs(next_frame
.MVc
));
1292 mv_ratio_accumulator
+=
1293 (this_frame_mvr_ratio
< next_frame
.mvr_abs
)
1294 ? (this_frame_mvr_ratio
* motion_pct
)
1295 : next_frame
.mvr_abs
* motion_pct
;
1297 mv_ratio_accumulator
+=
1298 (this_frame_mvc_ratio
< next_frame
.mvc_abs
)
1299 ? (this_frame_mvc_ratio
* motion_pct
)
1300 : next_frame
.mvc_abs
* motion_pct
;
1304 mv_ratio_accumulator
+= 0.0;
1305 this_frame_mvr_ratio
= 1.0;
1306 this_frame_mvc_ratio
= 1.0;
1309 // Underlying boost factor is based on inter intra error ratio
1310 r
= (boost_factor
* (next_frame
.intra_error
/ DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
)));
1312 // Increase boost for frames where new data coming into frame (eg zoom out)
1313 // Slightly reduce boost if there is a net balance of motion out of the frame (zoom in)
1314 // The range for this_frame_mv_in_out is -1.0 to +1.0
1315 if (this_frame_mv_in_out
> 0.0)
1316 r
+= r
* (this_frame_mv_in_out
* 2.0);
1318 r
+= r
* (this_frame_mv_in_out
/ 2.0); // In extreme case boost is halved
1323 // Adjust loop decay rate
1324 //if ( next_frame.pcnt_inter < loop_decay_rate )
1325 loop_decay_rate
= next_frame
.pcnt_inter
;
1327 // High % motion -> somewhat higher decay rate
1328 motion_decay
= (1.0 - (motion_pct
/ 20.0));
1329 if (motion_decay
< loop_decay_rate
)
1330 loop_decay_rate
= motion_decay
;
1332 // Adjustment to decay rate based on speed of motion
1334 double this_mv_rabs
;
1335 double this_mv_cabs
;
1336 double distance_factor
;
1338 this_mv_rabs
= fabs(next_frame
.mvr_abs
* motion_pct
);
1339 this_mv_cabs
= fabs(next_frame
.mvc_abs
* motion_pct
);
1341 distance_factor
= sqrt((this_mv_rabs
* this_mv_rabs
) +
1342 (this_mv_cabs
* this_mv_cabs
)) / 250.0;
1343 distance_factor
= ((distance_factor
> 1.0)
1344 ? 0.0 : (1.0 - distance_factor
));
1345 if (distance_factor
< loop_decay_rate
)
1346 loop_decay_rate
= distance_factor
;
1349 // Cumulative effect of decay
1350 decay_accumulator
= decay_accumulator
* loop_decay_rate
;
1351 decay_accumulator
= decay_accumulator
< 0.1 ? 0.1 : decay_accumulator
;
1352 //decay_accumulator = ( loop_decay_rate < decay_accumulator ) ? loop_decay_rate : decay_accumulator;
1354 boost_score
+= (decay_accumulator
* r
);
1356 // Break out conditions.
1359 (i
> MIN_GF_INTERVAL
) && // Dont break out with a very short interval
1360 ((cpi
->frames_to_key
- i
) >= MIN_GF_INTERVAL
) && // Dont break out very close to a key frame
1361 ((boost_score
> 20.0) || (next_frame
.pcnt_inter
< 0.75)) &&
1362 ((mv_ratio_accumulator
> 100.0) ||
1363 (abs_mv_in_out_accumulator
> 3.0) ||
1364 (mv_in_out_accumulator
< -2.0) ||
1365 ((boost_score
- old_boost_score
) < 2.0)
1370 boost_score
= old_boost_score
;
1374 vpx_memcpy(this_frame
, &next_frame
, sizeof(*this_frame
));
1376 old_boost_score
= boost_score
;
1379 cpi
->gf_decay_rate
= (i
> 0) ? (int)(100.0 * (1.0 - decay_accumulator
)) / i
: 0;
1381 // When using CBR apply additional buffer related upper limits
1382 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
1386 // For cbr apply buffer related limits
1387 if (cpi
->drop_frames_allowed
)
1389 int df_buffer_level
= cpi
->oxcf
.drop_frames_water_mark
* (cpi
->oxcf
.optimal_buffer_level
/ 100);
1391 if (cpi
->buffer_level
> df_buffer_level
)
1392 max_boost
= ((double)((cpi
->buffer_level
- df_buffer_level
) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
1396 else if (cpi
->buffer_level
> 0)
1398 max_boost
= ((double)(cpi
->buffer_level
* 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
1405 if (boost_score
> max_boost
)
1406 boost_score
= max_boost
;
1409 cpi
->gfu_boost
= (int)(boost_score
* 100.0) >> 4;
1411 // Should we use the alternate refernce frame
1412 if (cpi
->oxcf
.play_alternate
&&
1413 cpi
->oxcf
.lag_in_frames
&&
1414 (i
>= MIN_GF_INTERVAL
) &&
1415 (i
<= (cpi
->frames_to_key
- MIN_GF_INTERVAL
)) && // dont use ARF very near next kf
1416 (((next_frame
.pcnt_inter
> 0.75) &&
1417 ((mv_in_out_accumulator
/ (double)i
> -0.2) || (mv_in_out_accumulator
> -2.0)) &&
1418 //(cpi->gfu_boost>150) &&
1419 (cpi
->gfu_boost
> 100) &&
1420 //(cpi->gfu_boost>AF_THRESH2) &&
1421 //((cpi->gfu_boost/i)>AF_THRESH) &&
1422 //(decay_accumulator > 0.5) &&
1423 (cpi
->gf_decay_rate
<= (ARF_DECAY_THRESH
+ (cpi
->gfu_boost
/ 200)))
1429 int allocation_chunks
;
1430 int Q
= (cpi
->oxcf
.fixed_q
< 0) ? cpi
->last_q
[INTER_FRAME
] : cpi
->oxcf
.fixed_q
;
1432 int arf_frame_bits
= 0;
1435 // Estimate the bits to be allocated to the group as a whole
1436 if ((cpi
->kf_group_bits
> 0) && (cpi
->kf_group_error_left
> 0))
1437 group_bits
= (int)((double)cpi
->kf_group_bits
* (gf_group_err
/ (double)cpi
->kf_group_error_left
));
1441 // Boost for arf frame
1442 Boost
= (cpi
->gfu_boost
* 3 * GFQ_ADJUSTMENT
) / (2 * 100);
1443 Boost
+= (cpi
->baseline_gf_interval
* 50);
1444 allocation_chunks
= (i
* 100) + Boost
;
1446 // Normalize Altboost and allocations chunck down to prevent overflow
1447 while (Boost
> 1000)
1450 allocation_chunks
/= 2;
1453 // Calculate the number of bits to be spent on the arf based on the boost number
1454 arf_frame_bits
= (int)((double)Boost
* (group_bits
/ (double)allocation_chunks
));
1456 // Estimate if there are enough bits available to make worthwhile use of an arf.
1457 tmp_q
= estimate_q(cpi
, mod_frame_err
, (int)arf_frame_bits
, cpi
->common
.Height
, cpi
->common
.Width
);
1459 // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames.
1460 if (tmp_q
< cpi
->worst_quality
)
1463 int frames_after_arf
;
1464 int frames_bwd
= cpi
->oxcf
.arnr_max_frames
- 1;
1465 int frames_fwd
= cpi
->oxcf
.arnr_max_frames
- 1;
1467 cpi
->source_alt_ref_pending
= TRUE
;
1469 // 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
1470 // 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)
1471 gf_group_err
-= mod_frame_err
;
1473 // 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.
1474 // The future frame itself is part of the next group
1475 cpi
->baseline_gf_interval
= i
- 1;
1477 // Define the arnr filter width for this group of frames:
1478 // We only filter frames that lie within a distance of half
1479 // the GF interval from the ARF frame. We also have to trap
1480 // cases where the filter extends beyond the end of clip.
1481 // Note: this_frame->frame has been updated in the loop
1482 // so it now points at the ARF frame.
1483 half_gf_int
= cpi
->baseline_gf_interval
>> 1;
1484 frames_after_arf
= cpi
->total_stats
->count
- this_frame
->frame
- 1;
1486 switch (cpi
->oxcf
.arnr_type
)
1488 case 1: // Backward filter
1490 if (frames_bwd
> half_gf_int
)
1491 frames_bwd
= half_gf_int
;
1494 case 2: // Forward filter
1495 if (frames_fwd
> half_gf_int
)
1496 frames_fwd
= half_gf_int
;
1497 if (frames_fwd
> frames_after_arf
)
1498 frames_fwd
= frames_after_arf
;
1502 case 3: // Centered filter
1505 if (frames_fwd
> frames_after_arf
)
1506 frames_fwd
= frames_after_arf
;
1507 if (frames_fwd
> half_gf_int
)
1508 frames_fwd
= half_gf_int
;
1510 frames_bwd
= frames_fwd
;
1512 // For even length filter there is one more frame backward
1513 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1514 if (frames_bwd
< half_gf_int
)
1515 frames_bwd
+= (cpi
->oxcf
.arnr_max_frames
+1) & 0x1;
1519 cpi
->active_arnr_frames
= frames_bwd
+ 1 + frames_fwd
;
1522 // Advance to & read in the motion map for those frames
1523 // to be considered for filtering based on the position
1525 vp8_fpmm_reset_pos(cpi
, cpi
->fp_motion_map_stats_save
);
1527 // Position at the 'earliest' frame to be filtered
1528 vp8_advance_fpmm(cpi
,
1529 cpi
->baseline_gf_interval
- frames_bwd
);
1531 // Read / create a motion map for the region of interest
1532 vp8_input_fpmm(cpi
);
1537 cpi
->source_alt_ref_pending
= FALSE
;
1538 cpi
->baseline_gf_interval
= i
;
1543 cpi
->source_alt_ref_pending
= FALSE
;
1544 cpi
->baseline_gf_interval
= i
;
1548 if (!cpi
->source_alt_ref_pending
)
1550 // Dont allow conventional gf too near the next kf
1551 if ((cpi
->frames_to_key
- cpi
->baseline_gf_interval
) < MIN_GF_INTERVAL
)
1553 while (cpi
->baseline_gf_interval
< cpi
->frames_to_key
)
1555 if (EOF
== vp8_input_stats(cpi
, this_frame
))
1558 cpi
->baseline_gf_interval
++;
1560 if (cpi
->baseline_gf_interval
< cpi
->frames_to_key
)
1561 gf_group_err
+= calculate_modified_err(cpi
, this_frame
);
1566 // Now decide how many bits should be allocated to the GF group as a proportion of those remaining in the kf group.
1567 // The final key frame group in the clip is treated as a special case where cpi->kf_group_bits is tied to cpi->bits_left.
1568 // This is also important for short clips where there may only be one key frame.
1569 if (cpi
->frames_to_key
>= (int)(cpi
->total_stats
->count
- cpi
->common
.current_video_frame
))
1571 cpi
->kf_group_bits
= (cpi
->bits_left
> 0) ? cpi
->bits_left
: 0;
1574 // Calculate the bits to be allocated to the group as a whole
1575 if ((cpi
->kf_group_bits
> 0) && (cpi
->kf_group_error_left
> 0))
1576 cpi
->gf_group_bits
= (int)((double)cpi
->kf_group_bits
* (gf_group_err
/ (double)cpi
->kf_group_error_left
));
1578 cpi
->gf_group_bits
= 0;
1580 cpi
->gf_group_bits
= (cpi
->gf_group_bits
< 0) ? 0 : (cpi
->gf_group_bits
> cpi
->kf_group_bits
) ? cpi
->kf_group_bits
: cpi
->gf_group_bits
;
1582 // Clip cpi->gf_group_bits based on user supplied data rate variability limit (cpi->oxcf.two_pass_vbrmax_section)
1583 if (cpi
->gf_group_bits
> max_bits
* cpi
->baseline_gf_interval
)
1584 cpi
->gf_group_bits
= max_bits
* cpi
->baseline_gf_interval
;
1586 // Reset the file position
1587 reset_fpf_position(cpi
, start_pos
);
1589 // Assign bits to the arf or gf.
1592 int frames_in_section
;
1593 int allocation_chunks
;
1594 int Q
= (cpi
->oxcf
.fixed_q
< 0) ? cpi
->last_q
[INTER_FRAME
] : cpi
->oxcf
.fixed_q
;
1597 if (cpi
->source_alt_ref_pending
)
1599 Boost
= (cpi
->gfu_boost
* 3 * GFQ_ADJUSTMENT
) / (2 * 100);
1600 //Boost += (cpi->baseline_gf_interval * 25);
1601 Boost
+= (cpi
->baseline_gf_interval
* 50);
1603 // Set max and minimum boost and hence minimum allocation
1604 if (Boost
> ((cpi
->baseline_gf_interval
+ 1) * 200))
1605 Boost
= ((cpi
->baseline_gf_interval
+ 1) * 200);
1606 else if (Boost
< 125)
1609 frames_in_section
= cpi
->baseline_gf_interval
+ 1;
1610 allocation_chunks
= (frames_in_section
* 100) + Boost
;
1612 // Else for standard golden frames
1615 // boost based on inter / intra ratio of subsequent frames
1616 Boost
= (cpi
->gfu_boost
* GFQ_ADJUSTMENT
) / 100;
1618 // Set max and minimum boost and hence minimum allocation
1619 if (Boost
> (cpi
->baseline_gf_interval
* 150))
1620 Boost
= (cpi
->baseline_gf_interval
* 150);
1621 else if (Boost
< 125)
1624 frames_in_section
= cpi
->baseline_gf_interval
;
1625 allocation_chunks
= (frames_in_section
* 100) + (Boost
- 100);
1628 // Normalize Altboost and allocations chunck down to prevent overflow
1629 while (Boost
> 1000)
1632 allocation_chunks
/= 2;
1635 // Calculate the number of bits to be spent on the gf or arf based on the boost number
1636 cpi
->gf_bits
= (int)((double)Boost
* (cpi
->gf_group_bits
/ (double)allocation_chunks
));
1638 // If the frame that is to be boosted is simpler than the average for
1639 // the gf/arf group then use an alternative calculation
1640 // based on the error score of the frame itself
1641 if (mod_frame_err
< gf_group_err
/ (double)cpi
->baseline_gf_interval
)
1643 double alt_gf_grp_bits
;
1647 (double)cpi
->kf_group_bits
*
1648 (mod_frame_err
* (double)cpi
->baseline_gf_interval
) /
1649 DOUBLE_DIVIDE_CHECK((double)cpi
->kf_group_error_left
);
1651 alt_gf_bits
= (int)((double)Boost
* (alt_gf_grp_bits
/
1652 (double)allocation_chunks
));
1654 if (cpi
->gf_bits
> alt_gf_bits
)
1656 cpi
->gf_bits
= alt_gf_bits
;
1659 // Else if it is harder than other frames in the group make sure it at
1660 // least receives an allocation in keeping with its relative error
1661 // score, otherwise it may be worse off than an "un-boosted" frame
1665 (int)((double)cpi
->kf_group_bits
*
1667 DOUBLE_DIVIDE_CHECK((double)cpi
->kf_group_error_left
));
1669 if (alt_gf_bits
> cpi
->gf_bits
)
1671 cpi
->gf_bits
= alt_gf_bits
;
1675 // Apply an additional limit for CBR
1676 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
1678 if (cpi
->gf_bits
> (cpi
->buffer_level
>> 1))
1679 cpi
->gf_bits
= cpi
->buffer_level
>> 1;
1682 // Dont allow a negative value for gf_bits
1683 if (cpi
->gf_bits
< 0)
1686 // Adjust KF group bits and error remainin
1687 cpi
->kf_group_error_left
-= gf_group_err
;
1688 cpi
->kf_group_bits
-= cpi
->gf_group_bits
;
1690 if (cpi
->kf_group_bits
< 0)
1691 cpi
->kf_group_bits
= 0;
1693 // Note the error score left in the remaining frames of the group.
1694 // 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)
1695 if (!cpi
->source_alt_ref_pending
&& cpi
->common
.frame_type
!= KEY_FRAME
)
1696 cpi
->gf_group_error_left
= gf_group_err
- gf_first_frame_err
;
1698 cpi
->gf_group_error_left
= gf_group_err
;
1700 cpi
->gf_group_bits
-= cpi
->gf_bits
;
1702 if (cpi
->gf_group_bits
< 0)
1703 cpi
->gf_group_bits
= 0;
1705 // Set aside some bits for a mid gf sequence boost
1706 if ((cpi
->gfu_boost
> 150) && (cpi
->baseline_gf_interval
> 5))
1708 int pct_extra
= (cpi
->gfu_boost
- 100) / 50;
1709 pct_extra
= (pct_extra
> 10) ? 10 : pct_extra
;
1711 cpi
->mid_gf_extra_bits
= (cpi
->gf_group_bits
* pct_extra
) / 100;
1712 cpi
->gf_group_bits
-= cpi
->mid_gf_extra_bits
;
1715 cpi
->mid_gf_extra_bits
= 0;
1717 cpi
->gf_bits
+= cpi
->min_frame_bandwidth
; // Add in minimum for a frame
1720 if (!cpi
->source_alt_ref_pending
&& (cpi
->common
.frame_type
!= KEY_FRAME
)) // Normal GF and not a KF
1722 cpi
->per_frame_bandwidth
= cpi
->gf_bits
; // Per frame bit target for this frame
1725 // Adjustment to estimate_max_q based on a measure of complexity of the section
1726 if (cpi
->common
.frame_type
!= KEY_FRAME
)
1728 FIRSTPASS_STATS sectionstats
;
1731 vp8_zero_stats(§ionstats
);
1732 reset_fpf_position(cpi
, start_pos
);
1734 for (i
= 0 ; i
< cpi
->baseline_gf_interval
; i
++)
1736 vp8_input_stats(cpi
, &next_frame
);
1737 vp8_accumulate_stats(§ionstats
, &next_frame
);
1740 vp8_avg_stats(§ionstats
);
1742 if (sectionstats
.pcnt_motion
< .17)
1743 cpi
->section_is_low_motion
= 1;
1745 cpi
->section_is_low_motion
= 0;
1747 if (sectionstats
.mvc_abs
+ sectionstats
.mvr_abs
> 45)
1748 cpi
->section_is_fast_motion
= 1;
1750 cpi
->section_is_fast_motion
= 0;
1752 cpi
->section_intra_rating
= sectionstats
.intra_error
/ DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
1754 Ratio
= sectionstats
.intra_error
/ DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
1755 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
1757 cpi
->section_max_qfactor
= 1.0 - ((Ratio
- 10.0) * 0.025);
1759 if (cpi
->section_max_qfactor
< 0.80)
1760 cpi
->section_max_qfactor
= 0.80;
1764 // cpi->section_max_qfactor = 1.0;
1766 reset_fpf_position(cpi
, start_pos
);
1769 // Reset the First pass motion map file position
1770 vp8_fpmm_reset_pos(cpi
, fpmm_pos
);
1773 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1774 static void assign_std_frame_bits(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
1776 int target_frame_size
; // gf_group_error_left
1778 double modified_err
;
1779 double err_fraction
; // What portion of the remaining GF group error is used by this frame
1781 int max_bits
= frame_max_bits(cpi
); // Max for a single frame
1783 // The final few frames have special treatment
1784 if (cpi
->frames_till_gf_update_due
>= (int)(cpi
->total_stats
->count
- cpi
->common
.current_video_frame
))
1786 cpi
->gf_group_bits
= (cpi
->bits_left
> 0) ? cpi
->bits_left
: 0;;
1789 // Calculate modified prediction error used in bit allocation
1790 modified_err
= calculate_modified_err(cpi
, this_frame
);
1792 if (cpi
->gf_group_error_left
> 0)
1793 err_fraction
= modified_err
/ cpi
->gf_group_error_left
; // What portion of the remaining GF group error is used by this frame
1797 target_frame_size
= (int)((double)cpi
->gf_group_bits
* err_fraction
); // How many of those bits available for allocation should we give it?
1799 // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top end.
1800 if (target_frame_size
< 0)
1801 target_frame_size
= 0;
1804 if (target_frame_size
> max_bits
)
1805 target_frame_size
= max_bits
;
1807 if (target_frame_size
> cpi
->gf_group_bits
)
1808 target_frame_size
= cpi
->gf_group_bits
;
1811 cpi
->gf_group_error_left
-= modified_err
; // Adjust error remaining
1812 cpi
->gf_group_bits
-= target_frame_size
; // Adjust bits remaining
1814 if (cpi
->gf_group_bits
< 0)
1815 cpi
->gf_group_bits
= 0;
1817 target_frame_size
+= cpi
->min_frame_bandwidth
; // Add in the minimum number of bits that is set aside for every frame.
1819 // Special case for the frame that lies half way between two gfs
1820 if (cpi
->common
.frames_since_golden
== cpi
->baseline_gf_interval
/ 2)
1821 target_frame_size
+= cpi
->mid_gf_extra_bits
;
1823 cpi
->per_frame_bandwidth
= target_frame_size
; // Per frame bit target for this frame
1826 void vp8_second_pass(VP8_COMP
*cpi
)
1829 int frames_left
= (int)(cpi
->total_stats
->count
- cpi
->common
.current_video_frame
);
1831 FIRSTPASS_STATS this_frame
;
1832 FIRSTPASS_STATS this_frame_copy
;
1834 VP8_COMMON
*cm
= &cpi
->common
;
1836 double this_frame_error
;
1837 double this_frame_intra_error
;
1838 double this_frame_coded_error
;
1840 FIRSTPASS_STATS
*start_pos
;
1847 vp8_clear_system_state();
1849 if (EOF
== vp8_input_stats(cpi
, &this_frame
))
1852 vpx_memset(cpi
->fp_motion_map
, 0,
1853 cpi
->oxcf
.arnr_max_frames
*cpi
->common
.MBs
);
1854 cpi
->fp_motion_map_stats_save
= vp8_fpmm_get_pos(cpi
);
1856 // Step over this frame's first pass motion map
1857 vp8_advance_fpmm(cpi
, 1);
1859 this_frame_error
= this_frame
.ssim_weighted_pred_err
;
1860 this_frame_intra_error
= this_frame
.intra_error
;
1861 this_frame_coded_error
= this_frame
.coded_error
;
1863 // Store information regarding level of motion etc for use mode decisions.
1864 cpi
->motion_speed
= (int)(fabs(this_frame
.MVr
) + fabs(this_frame
.MVc
));
1865 cpi
->motion_var
= (int)(fabs(this_frame
.MVrv
) + fabs(this_frame
.MVcv
));
1866 cpi
->inter_lvl
= (int)(this_frame
.pcnt_inter
* 100);
1867 cpi
->intra_lvl
= (int)((1.0 - this_frame
.pcnt_inter
) * 100);
1868 cpi
->motion_lvl
= (int)(this_frame
.pcnt_motion
* 100);
1870 start_pos
= cpi
->stats_in
;
1872 // keyframe and section processing !
1873 if (cpi
->frames_to_key
== 0)
1875 // Define next KF group and assign bits to it
1876 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1877 vp8_find_next_key_frame(cpi
, &this_frame_copy
);
1879 // 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
1880 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
1881 // This is temporary code till we decide what should really happen in this case.
1882 if (cpi
->oxcf
.error_resilient_mode
)
1884 cpi
->gf_group_bits
= cpi
->kf_group_bits
;
1885 cpi
->gf_group_error_left
= cpi
->kf_group_error_left
;
1886 cpi
->baseline_gf_interval
= cpi
->frames_to_key
;
1887 cpi
->frames_till_gf_update_due
= cpi
->baseline_gf_interval
;
1888 cpi
->source_alt_ref_pending
= FALSE
;
1893 // Is this a GF / ARF (Note that a KF is always also a GF)
1894 if (cpi
->frames_till_gf_update_due
== 0)
1896 // Define next gf group and assign bits to it
1897 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1898 define_gf_group(cpi
, &this_frame_copy
);
1900 // If we are going to code an altref frame at the end of the group and the current frame is not a key frame....
1901 // If the previous group used an arf this frame has already benefited from that arf boost and it should not be given extra bits
1902 // If the previous group was NOT coded using arf we may want to apply some boost to this GF as well
1903 if (cpi
->source_alt_ref_pending
&& (cpi
->common
.frame_type
!= KEY_FRAME
))
1905 // Assign a standard frames worth of bits from those allocated to the GF group
1906 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1907 assign_std_frame_bits(cpi
, &this_frame_copy
);
1909 // If appropriate (we are switching into ARF active but it was not previously active) apply a boost for the gf at the start of the group.
1910 //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
1914 int pct_extra
= (cpi
->gfu_boost
- 100) / 50;
1916 pct_extra
= (pct_extra
> 20) ? 20 : pct_extra
;
1918 extra_bits
= (cpi
->gf_group_bits
* pct_extra
) / 100;
1919 cpi
->gf_group_bits
-= extra_bits
;
1920 cpi
->per_frame_bandwidth
+= extra_bits
;
1925 // Otherwise this is an ordinary frame
1928 // 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
1929 // outlandish behaviour if someone does set it when using two pass. It effectively disables GF groups.
1930 // This is temporary code till we decide what should really happen in this case.
1931 if (cpi
->oxcf
.error_resilient_mode
)
1933 cpi
->frames_till_gf_update_due
= cpi
->frames_to_key
;
1935 if (cpi
->common
.frame_type
!= KEY_FRAME
)
1937 // Assign bits from those allocated to the GF group
1938 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1939 assign_std_frame_bits(cpi
, &this_frame_copy
);
1944 // Assign bits from those allocated to the GF group
1945 vpx_memcpy(&this_frame_copy
, &this_frame
, sizeof(this_frame
));
1946 assign_std_frame_bits(cpi
, &this_frame_copy
);
1950 // Keep a globally available copy of this and the next frame's iiratio.
1951 cpi
->this_iiratio
= this_frame_intra_error
/
1952 DOUBLE_DIVIDE_CHECK(this_frame_coded_error
);
1954 FIRSTPASS_STATS next_frame
;
1955 if ( lookup_next_frame_stats(cpi
, &next_frame
) != EOF
)
1957 cpi
->next_iiratio
= next_frame
.intra_error
/
1958 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
);
1962 // Set nominal per second bandwidth for this frame
1963 cpi
->target_bandwidth
= cpi
->per_frame_bandwidth
* cpi
->output_frame_rate
;
1964 if (cpi
->target_bandwidth
< 0)
1965 cpi
->target_bandwidth
= 0;
1967 if (cpi
->common
.current_video_frame
== 0)
1969 // guess at 2nd pass q
1970 cpi
->est_max_qcorrection_factor
= 1.0;
1971 tmp_q
= estimate_max_q(cpi
, (cpi
->total_coded_error_left
/ frames_left
), (int)(cpi
->bits_left
/ frames_left
), cpi
->common
.Height
, cpi
->common
.Width
);
1973 if (tmp_q
< cpi
->worst_quality
)
1975 cpi
->active_worst_quality
= tmp_q
;
1976 cpi
->ni_av_qi
= tmp_q
;
1980 cpi
->active_worst_quality
= cpi
->worst_quality
;
1981 cpi
->ni_av_qi
= cpi
->worst_quality
;
1986 if (frames_left
< 1)
1989 tmp_q
= estimate_max_q(cpi
, (cpi
->total_coded_error_left
/ frames_left
), (int)(cpi
->bits_left
/ frames_left
), cpi
->common
.Height
, cpi
->common
.Width
);
1991 // Move active_worst_quality but in a damped way
1992 if (tmp_q
> cpi
->active_worst_quality
)
1993 cpi
->active_worst_quality
++;
1994 else if (tmp_q
< cpi
->active_worst_quality
)
1995 cpi
->active_worst_quality
--;
1997 cpi
->active_worst_quality
= ((cpi
->active_worst_quality
* 3) + tmp_q
+ 2) / 4;
1999 // Clamp to user set limits
2000 if (cpi
->active_worst_quality
> cpi
->worst_quality
)
2001 cpi
->active_worst_quality
= cpi
->worst_quality
;
2002 else if (cpi
->active_worst_quality
< cpi
->best_quality
)
2003 cpi
->active_worst_quality
= cpi
->best_quality
;
2007 cpi
->frames_to_key
--;
2008 cpi
->total_error_left
-= this_frame_error
;
2009 cpi
->total_intra_error_left
-= this_frame_intra_error
;
2010 cpi
->total_coded_error_left
-= this_frame_coded_error
;
2014 static BOOL
test_candidate_kf(VP8_COMP
*cpi
, FIRSTPASS_STATS
*last_frame
, FIRSTPASS_STATS
*this_frame
, FIRSTPASS_STATS
*next_frame
)
2016 BOOL is_viable_kf
= FALSE
;
2018 // Does the frame satisfy the primary criteria of a key frame
2019 // If so, then examine how well it predicts subsequent frames
2020 if ((this_frame
->pcnt_second_ref
< 0.10) &&
2021 (next_frame
->pcnt_second_ref
< 0.10) &&
2022 ((this_frame
->pcnt_inter
< 0.05) ||
2024 (this_frame
->pcnt_inter
< .25) &&
2025 ((this_frame
->intra_error
/ DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
)) < 2.5) &&
2026 ((fabs(last_frame
->coded_error
- this_frame
->coded_error
) / DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
) > .40) ||
2027 (fabs(last_frame
->intra_error
- this_frame
->intra_error
) / DOUBLE_DIVIDE_CHECK(this_frame
->intra_error
) > .40) ||
2028 ((next_frame
->intra_error
/ DOUBLE_DIVIDE_CHECK(next_frame
->coded_error
)) > 3.5)
2035 FIRSTPASS_STATS
*start_pos
;
2037 FIRSTPASS_STATS local_next_frame
;
2039 double boost_score
= 0.0;
2040 double old_boost_score
= 0.0;
2041 double decay_accumulator
= 1.0;
2042 double next_iiratio
;
2044 vpx_memcpy(&local_next_frame
, next_frame
, sizeof(*next_frame
));
2046 // Note the starting file position so we can reset to it
2047 start_pos
= cpi
->stats_in
;
2049 // Examine how well the key frame predicts subsequent frames
2050 for (i
= 0 ; i
< 16; i
++)
2052 next_iiratio
= (IIKFACTOR1
* local_next_frame
.intra_error
/ DOUBLE_DIVIDE_CHECK(local_next_frame
.coded_error
)) ;
2054 if (next_iiratio
> RMAX
)
2055 next_iiratio
= RMAX
;
2057 // Cumulative effect of decay in prediction quality
2058 if (local_next_frame
.pcnt_inter
> 0.85)
2059 decay_accumulator
= decay_accumulator
* local_next_frame
.pcnt_inter
;
2061 decay_accumulator
= decay_accumulator
* ((0.85 + local_next_frame
.pcnt_inter
) / 2.0);
2063 //decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2065 // Keep a running total
2066 boost_score
+= (decay_accumulator
* next_iiratio
);
2068 // Test various breakout clauses
2069 if ((local_next_frame
.pcnt_inter
< 0.05) ||
2070 (next_iiratio
< 1.5) ||
2071 ((local_next_frame
.pcnt_inter
< 0.20) && (next_iiratio
< 3.0)) ||
2072 ((boost_score
- old_boost_score
) < 0.5) ||
2073 (local_next_frame
.intra_error
< 200)
2079 old_boost_score
= boost_score
;
2081 // Get the next frame details
2082 if (EOF
== vp8_input_stats(cpi
, &local_next_frame
))
2086 // If there is tolerable prediction for at least the next 3 frames then break out else discard this pottential key frame and move on
2087 if (boost_score
> 5.0 && (i
> 3))
2088 is_viable_kf
= TRUE
;
2091 // Reset the file position
2092 reset_fpf_position(cpi
, start_pos
);
2094 is_viable_kf
= FALSE
;
2098 return is_viable_kf
;
2100 void vp8_find_next_key_frame(VP8_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
)
2103 FIRSTPASS_STATS last_frame
;
2104 FIRSTPASS_STATS first_frame
;
2105 FIRSTPASS_STATS next_frame
;
2106 FIRSTPASS_STATS
*start_position
;
2108 double decay_accumulator
= 0;
2109 double boost_score
= 0;
2110 double old_boost_score
= 0.0;
2111 double loop_decay_rate
;
2113 double kf_mod_err
= 0.0;
2114 double kf_group_err
= 0.0;
2115 double kf_group_intra_err
= 0.0;
2116 double kf_group_coded_err
= 0.0;
2117 double two_pass_min_rate
= (double)(cpi
->oxcf
.target_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
2119 vpx_memset(&next_frame
, 0, sizeof(next_frame
)); // assure clean
2121 vp8_clear_system_state(); //__asm emms;
2122 start_position
= cpi
->stats_in
;
2124 cpi
->common
.frame_type
= KEY_FRAME
;
2126 // Clear the alt ref active flag as this can never be active on a key frame
2127 cpi
->source_alt_ref_active
= FALSE
;
2129 // Kf is always a gf so clear frames till next gf counter
2130 cpi
->frames_till_gf_update_due
= 0;
2132 cpi
->frames_to_key
= 1;
2134 // Take a copy of the initial frame details
2135 vpx_memcpy(&first_frame
, this_frame
, sizeof(*this_frame
));
2137 cpi
->kf_group_bits
= 0; // Total bits avaialable to kf group
2138 cpi
->kf_group_error_left
= 0; // Group modified error score.
2140 kf_mod_err
= calculate_modified_err(cpi
, this_frame
);
2142 // find the next keyframe
2143 while (cpi
->stats_in
< cpi
->stats_in_end
)
2145 // Accumulate kf group error
2146 kf_group_err
+= calculate_modified_err(cpi
, this_frame
);
2148 // These figures keep intra and coded error counts for all frames including key frames in the group.
2149 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2150 kf_group_intra_err
+= this_frame
->intra_error
;
2151 kf_group_coded_err
+= this_frame
->coded_error
;
2153 // load a the next frame's stats
2154 vpx_memcpy(&last_frame
, this_frame
, sizeof(*this_frame
));
2155 vp8_input_stats(cpi
, this_frame
);
2157 // Provided that we are not at the end of the file...
2158 if (cpi
->oxcf
.auto_key
2159 && lookup_next_frame_stats(cpi
, &next_frame
) != EOF
)
2161 if (test_candidate_kf(cpi
, &last_frame
, this_frame
, &next_frame
))
2164 // Step on to the next frame
2165 cpi
->frames_to_key
++;
2167 // If we don't have a real key frame within the next two
2168 // forcekeyframeevery intervals then break out of the loop.
2169 if (cpi
->frames_to_key
>= 2 *(int)cpi
->key_frame_frequency
)
2172 cpi
->frames_to_key
++;
2175 // If there is a max kf interval set by the user we must obey it.
2176 // We already breakout of the loop above at 2x max.
2177 // This code centers the extra kf if the actual natural
2178 // interval is between 1x and 2x
2179 if (cpi
->oxcf
.auto_key
2180 && cpi
->frames_to_key
> (int)cpi
->key_frame_frequency
)
2182 cpi
->frames_to_key
/= 2;
2184 // Estimate corrected kf group error
2185 kf_group_err
/= 2.0;
2186 kf_group_intra_err
/= 2.0;
2187 kf_group_coded_err
/= 2.0;
2190 // Special case for the last frame of the file
2191 if (cpi
->stats_in
>= cpi
->stats_in_end
)
2193 // Accumulate kf group error
2194 kf_group_err
+= calculate_modified_err(cpi
, this_frame
);
2196 // These figures keep intra and coded error counts for all frames including key frames in the group.
2197 // The effect of the key frame itself can be subtracted out using the first_frame data collected above
2198 kf_group_intra_err
+= this_frame
->intra_error
;
2199 kf_group_coded_err
+= this_frame
->coded_error
;
2202 // Calculate the number of bits that should be assigned to the kf group.
2203 if ((cpi
->bits_left
> 0) && ((int)cpi
->modified_total_error_left
> 0))
2205 // Max for a single normal frame (not key frame)
2206 int max_bits
= frame_max_bits(cpi
);
2208 // Maximum bits for the kf group
2209 long long max_grp_bits
;
2211 // Default allocation based on bits left and relative
2212 // complexity of the section
2213 cpi
->kf_group_bits
= (long long)( cpi
->bits_left
*
2215 cpi
->modified_total_error_left
));
2217 // Clip based on maximum per frame rate defined by the user.
2218 max_grp_bits
= (long long)max_bits
* (long long)cpi
->frames_to_key
;
2219 if (cpi
->kf_group_bits
> max_grp_bits
)
2220 cpi
->kf_group_bits
= max_grp_bits
;
2222 // Additional special case for CBR if buffer is getting full.
2223 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2225 int opt_buffer_lvl
= cpi
->oxcf
.optimal_buffer_level
;
2226 int buffer_lvl
= cpi
->buffer_level
;
2228 // If the buffer is near or above the optimal and this kf group is
2229 // not being allocated much then increase the allocation a bit.
2230 if (buffer_lvl
>= opt_buffer_lvl
)
2232 int high_water_mark
= (opt_buffer_lvl
+
2233 cpi
->oxcf
.maximum_buffer_size
) >> 1;
2235 long long av_group_bits
;
2237 // Av bits per frame * number of frames
2238 av_group_bits
= (long long)cpi
->av_per_frame_bandwidth
*
2239 (long long)cpi
->frames_to_key
;
2241 // We are at or above the maximum.
2242 if (cpi
->buffer_level
>= high_water_mark
)
2244 long long min_group_bits
;
2246 min_group_bits
= av_group_bits
+
2247 (long long)(buffer_lvl
-
2250 if (cpi
->kf_group_bits
< min_group_bits
)
2251 cpi
->kf_group_bits
= min_group_bits
;
2253 // We are above optimal but below the maximum
2254 else if (cpi
->kf_group_bits
< av_group_bits
)
2256 long long bits_below_av
= av_group_bits
-
2259 cpi
->kf_group_bits
+=
2260 (long long)((double)bits_below_av
*
2261 (double)(buffer_lvl
- opt_buffer_lvl
) /
2262 (double)(high_water_mark
- opt_buffer_lvl
));
2268 cpi
->kf_group_bits
= 0;
2270 // Reset the first pass file position
2271 reset_fpf_position(cpi
, start_position
);
2273 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2274 decay_accumulator
= 1.0;
2276 loop_decay_rate
= 1.00; // Starting decay rate
2278 for (i
= 0 ; i
< cpi
->frames_to_key
; i
++)
2281 double motion_decay
;
2282 double motion_pct
= next_frame
.pcnt_motion
;
2284 if (EOF
== vp8_input_stats(cpi
, &next_frame
))
2287 r
= (IIKFACTOR2
* next_frame
.intra_error
/ DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
)) ;
2292 // Adjust loop decay rate
2293 //if ( next_frame.pcnt_inter < loop_decay_rate )
2294 loop_decay_rate
= next_frame
.pcnt_inter
;
2296 // High % motion -> somewhat higher decay rate
2297 motion_decay
= (1.0 - (motion_pct
/ 20.0));
2298 if (motion_decay
< loop_decay_rate
)
2299 loop_decay_rate
= motion_decay
;
2301 // Adjustment to decay rate based on speed of motion
2303 double this_mv_rabs
;
2304 double this_mv_cabs
;
2305 double distance_factor
;
2307 this_mv_rabs
= fabs(next_frame
.mvr_abs
* motion_pct
);
2308 this_mv_cabs
= fabs(next_frame
.mvc_abs
* motion_pct
);
2310 distance_factor
= sqrt((this_mv_rabs
* this_mv_rabs
) +
2311 (this_mv_cabs
* this_mv_cabs
)) / 250.0;
2312 distance_factor
= ((distance_factor
> 1.0)
2313 ? 0.0 : (1.0 - distance_factor
));
2314 if (distance_factor
< loop_decay_rate
)
2315 loop_decay_rate
= distance_factor
;
2318 decay_accumulator
= decay_accumulator
* loop_decay_rate
;
2319 decay_accumulator
= decay_accumulator
< 0.1 ? 0.1 : decay_accumulator
;
2321 boost_score
+= (decay_accumulator
* r
);
2323 if ((i
> MIN_GF_INTERVAL
) &&
2324 ((boost_score
- old_boost_score
) < 1.0))
2329 old_boost_score
= boost_score
;
2334 FIRSTPASS_STATS sectionstats
;
2337 vp8_zero_stats(§ionstats
);
2338 reset_fpf_position(cpi
, start_position
);
2340 for (i
= 0 ; i
< cpi
->frames_to_key
; i
++)
2342 vp8_input_stats(cpi
, &next_frame
);
2343 vp8_accumulate_stats(§ionstats
, &next_frame
);
2346 vp8_avg_stats(§ionstats
);
2348 if (sectionstats
.pcnt_motion
< .17)
2349 cpi
->section_is_low_motion
= 1;
2351 cpi
->section_is_low_motion
= 0;
2353 if (sectionstats
.mvc_abs
+ sectionstats
.mvr_abs
> 45)
2354 cpi
->section_is_fast_motion
= 1;
2356 cpi
->section_is_fast_motion
= 0;
2358 cpi
->section_intra_rating
= sectionstats
.intra_error
/ DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
2360 Ratio
= sectionstats
.intra_error
/ DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
);
2361 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2363 cpi
->section_max_qfactor
= 1.0 - ((Ratio
- 10.0) * 0.025);
2365 if (cpi
->section_max_qfactor
< 0.80)
2366 cpi
->section_max_qfactor
= 0.80;
2370 // cpi->section_max_qfactor = 1.0;
2373 // When using CBR apply additional buffer fullness related upper limits
2374 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2378 if (cpi
->drop_frames_allowed
)
2380 int df_buffer_level
= cpi
->oxcf
.drop_frames_water_mark
* (cpi
->oxcf
.optimal_buffer_level
/ 100);
2382 if (cpi
->buffer_level
> df_buffer_level
)
2383 max_boost
= ((double)((cpi
->buffer_level
- df_buffer_level
) * 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
2387 else if (cpi
->buffer_level
> 0)
2389 max_boost
= ((double)(cpi
->buffer_level
* 2 / 3) * 16.0) / DOUBLE_DIVIDE_CHECK((double)cpi
->av_per_frame_bandwidth
);
2396 if (boost_score
> max_boost
)
2397 boost_score
= max_boost
;
2400 // Reset the first pass file position
2401 reset_fpf_position(cpi
, start_position
);
2403 // Work out how many bits to allocate for the key frame itself
2406 int kf_boost
= boost_score
;
2407 int allocation_chunks
;
2408 int Counter
= cpi
->frames_to_key
;
2410 YV12_BUFFER_CONFIG
*lst_yv12
= &cpi
->common
.yv12_fb
[cpi
->common
.lst_fb_idx
];
2411 // Min boost based on kf interval
2414 while ((kf_boost
< 48) && (Counter
> 0))
2424 kf_boost
+= ((Counter
+ 1) >> 1);
2426 if (kf_boost
> 48) kf_boost
= 48;
2429 // bigger frame sizes need larger kf boosts, smaller frames smaller boosts...
2430 if ((lst_yv12
->y_width
* lst_yv12
->y_height
) > (320 * 240))
2431 kf_boost
+= 2 * (lst_yv12
->y_width
* lst_yv12
->y_height
) / (320 * 240);
2432 else if ((lst_yv12
->y_width
* lst_yv12
->y_height
) < (320 * 240))
2433 kf_boost
-= 4 * (320 * 240) / (lst_yv12
->y_width
* lst_yv12
->y_height
);
2435 kf_boost
= (int)((double)kf_boost
* 100.0) >> 4; // Scale 16 to 100
2437 // Adjustment to boost based on recent average q
2438 kf_boost
= kf_boost
* vp8_kf_boost_qadjustment
[cpi
->ni_av_qi
] / 100;
2440 if (kf_boost
< 250) // Min KF boost
2443 // We do three calculations for kf size.
2444 // The first is based on the error score for the whole kf group.
2445 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2446 // 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
2448 allocation_chunks
= ((cpi
->frames_to_key
- 1) * 100) + kf_boost
; // cpi->frames_to_key-1 because key frame itself is taken care of by kf_boost
2450 // Normalize Altboost and allocations chunck down to prevent overflow
2451 while (kf_boost
> 1000)
2454 allocation_chunks
/= 2;
2457 cpi
->kf_group_bits
= (cpi
->kf_group_bits
< 0) ? 0 : cpi
->kf_group_bits
;
2459 // Calculate the number of bits to be spent on the key frame
2460 cpi
->kf_bits
= (int)((double)kf_boost
* ((double)cpi
->kf_group_bits
/ (double)allocation_chunks
));
2462 // Apply an additional limit for CBR
2463 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2465 if (cpi
->kf_bits
> ((3 * cpi
->buffer_level
) >> 2))
2466 cpi
->kf_bits
= (3 * cpi
->buffer_level
) >> 2;
2469 // If the key frame is actually easier than the average for the
2470 // kf group (which does sometimes happen... eg a blank intro frame)
2471 // Then use an alternate calculation based on the kf error score
2472 // which should give a smaller key frame.
2473 if (kf_mod_err
< kf_group_err
/ cpi
->frames_to_key
)
2475 double alt_kf_grp_bits
=
2476 ((double)cpi
->bits_left
*
2477 (kf_mod_err
* (double)cpi
->frames_to_key
) /
2478 DOUBLE_DIVIDE_CHECK(cpi
->modified_total_error_left
));
2480 alt_kf_bits
= (int)((double)kf_boost
*
2481 (alt_kf_grp_bits
/ (double)allocation_chunks
));
2483 if (cpi
->kf_bits
> alt_kf_bits
)
2485 cpi
->kf_bits
= alt_kf_bits
;
2488 // Else if it is much harder than other frames in the group make sure
2489 // it at least receives an allocation in keeping with its relative
2494 (int)((double)cpi
->bits_left
*
2496 DOUBLE_DIVIDE_CHECK(cpi
->modified_total_error_left
)));
2498 if (alt_kf_bits
> cpi
->kf_bits
)
2500 cpi
->kf_bits
= alt_kf_bits
;
2504 cpi
->kf_group_bits
-= cpi
->kf_bits
;
2505 cpi
->kf_bits
+= cpi
->min_frame_bandwidth
; // Add in the minimum frame allowance
2507 cpi
->per_frame_bandwidth
= cpi
->kf_bits
; // Peer frame bit target for this frame
2508 cpi
->target_bandwidth
= cpi
->kf_bits
* cpi
->output_frame_rate
; // Convert to a per second bitrate
2511 // Note the total error score of the kf group minus the key frame itself
2512 cpi
->kf_group_error_left
= (int)(kf_group_err
- kf_mod_err
);
2514 // Adjust the count of total modified error left.
2515 // The count of bits left is adjusted elsewhere based on real coded frame sizes
2516 cpi
->modified_total_error_left
-= kf_group_err
;
2518 if (cpi
->oxcf
.allow_spatial_resampling
)
2520 int resample_trigger
= FALSE
;
2521 int last_kf_resampled
= FALSE
;
2525 int new_width
= cpi
->oxcf
.Width
;
2526 int new_height
= cpi
->oxcf
.Height
;
2528 int projected_buffer_level
= cpi
->buffer_level
;
2531 double projected_bits_perframe
;
2532 double group_iiratio
= (kf_group_intra_err
- first_frame
.intra_error
) / (kf_group_coded_err
- first_frame
.coded_error
);
2533 double err_per_frame
= kf_group_err
/ cpi
->frames_to_key
;
2534 double bits_per_frame
;
2535 double av_bits_per_frame
;
2536 double effective_size_ratio
;
2538 if ((cpi
->common
.Width
!= cpi
->oxcf
.Width
) || (cpi
->common
.Height
!= cpi
->oxcf
.Height
))
2539 last_kf_resampled
= TRUE
;
2541 // Set back to unscaled by defaults
2542 cpi
->common
.horiz_scale
= NORMAL
;
2543 cpi
->common
.vert_scale
= NORMAL
;
2545 // Calculate Average bits per frame.
2546 //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats->count - cpi->common.current_video_frame);
2547 av_bits_per_frame
= cpi
->oxcf
.target_bandwidth
/ DOUBLE_DIVIDE_CHECK((double)cpi
->oxcf
.frame_rate
);
2548 //if ( av_bits_per_frame < 0.0 )
2549 // av_bits_per_frame = 0.0
2551 // CBR... Use the clip average as the target for deciding resample
2552 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2554 bits_per_frame
= av_bits_per_frame
;
2557 // In VBR we want to avoid downsampling in easy section unless we are under extreme pressure
2558 // So use the larger of target bitrate for this sectoion or average bitrate for sequence
2561 bits_per_frame
= cpi
->kf_group_bits
/ cpi
->frames_to_key
; // This accounts for how hard the section is...
2563 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
2564 bits_per_frame
= av_bits_per_frame
;
2567 // bits_per_frame should comply with our minimum
2568 if (bits_per_frame
< (cpi
->oxcf
.target_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100))
2569 bits_per_frame
= (cpi
->oxcf
.target_bandwidth
* cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
2571 // Work out if spatial resampling is necessary
2572 kf_q
= estimate_kf_group_q(cpi
, err_per_frame
, bits_per_frame
, new_height
, new_width
, group_iiratio
);
2574 // 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
2575 projected_bits_perframe
= bits_per_frame
;
2578 while (tmp_q
> cpi
->worst_quality
)
2580 projected_bits_perframe
*= 1.04;
2584 // Guess at buffer level at the end of the section
2585 projected_buffer_level
= cpi
->buffer_level
- (int)((projected_bits_perframe
- av_bits_per_frame
) * cpi
->frames_to_key
);
2589 FILE *f
= fopen("Subsamle.stt", "a");
2590 fprintf(f
, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi
->common
.current_video_frame
, kf_q
, cpi
->common
.horiz_scale
, cpi
->common
.vert_scale
, kf_group_err
/ cpi
->frames_to_key
, cpi
->kf_group_bits
/ cpi
->frames_to_key
, new_height
, new_width
);
2594 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
2595 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
)
2597 // Trigger resample if we are projected to fall below down sample level or
2598 // resampled last time and are projected to remain below the up sample level
2599 if ((projected_buffer_level
< (cpi
->oxcf
.resample_down_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100)) ||
2600 (last_kf_resampled
&& (projected_buffer_level
< (cpi
->oxcf
.resample_up_water_mark
* cpi
->oxcf
.optimal_buffer_level
/ 100))))
2601 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
2602 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
2603 resample_trigger
= TRUE
;
2605 resample_trigger
= FALSE
;
2609 long long clip_bits
= (long long)(cpi
->total_stats
->count
* cpi
->oxcf
.target_bandwidth
/ DOUBLE_DIVIDE_CHECK((double)cpi
->oxcf
.frame_rate
));
2610 long long over_spend
= cpi
->oxcf
.starting_buffer_level
- cpi
->buffer_level
;
2611 long long over_spend2
= cpi
->oxcf
.starting_buffer_level
- projected_buffer_level
;
2613 if ((last_kf_resampled
&& (kf_q
> cpi
->worst_quality
)) || // If triggered last time the threshold for triggering again is reduced
2614 ((kf_q
> cpi
->worst_quality
) && // Projected Q higher than allowed and ...
2615 (over_spend
> clip_bits
/ 20))) // ... Overspend > 5% of total bits
2616 resample_trigger
= TRUE
;
2618 resample_trigger
= FALSE
;
2622 if (resample_trigger
)
2624 while ((kf_q
>= cpi
->worst_quality
) && (scale_val
< 6))
2628 cpi
->common
.vert_scale
= vscale_lookup
[scale_val
];
2629 cpi
->common
.horiz_scale
= hscale_lookup
[scale_val
];
2631 Scale2Ratio(cpi
->common
.horiz_scale
, &hr
, &hs
);
2632 Scale2Ratio(cpi
->common
.vert_scale
, &vr
, &vs
);
2634 new_width
= ((hs
- 1) + (cpi
->oxcf
.Width
* hr
)) / hs
;
2635 new_height
= ((vs
- 1) + (cpi
->oxcf
.Height
* vr
)) / vs
;
2637 // Reducing the area to 1/4 does not reduce the complexity (err_per_frame) to 1/4...
2638 // effective_sizeratio attempts to provide a crude correction for this
2639 effective_size_ratio
= (double)(new_width
* new_height
) / (double)(cpi
->oxcf
.Width
* cpi
->oxcf
.Height
);
2640 effective_size_ratio
= (1.0 + (3.0 * effective_size_ratio
)) / 4.0;
2642 // Now try again and see what Q we get with the smaller image size
2643 kf_q
= estimate_kf_group_q(cpi
, err_per_frame
* effective_size_ratio
, bits_per_frame
, new_height
, new_width
, group_iiratio
);
2647 FILE *f
= fopen("Subsamle.stt", "a");
2648 fprintf(f
, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_q
, cpi
->common
.horiz_scale
, cpi
->common
.vert_scale
, kf_group_err
/ cpi
->frames_to_key
, cpi
->kf_group_bits
/ cpi
->frames_to_key
, new_height
, new_width
);
2654 if ((cpi
->common
.Width
!= new_width
) || (cpi
->common
.Height
!= new_height
))
2656 cpi
->common
.Width
= new_width
;
2657 cpi
->common
.Height
= new_height
;
2658 vp8_alloc_compressor_data(cpi
);