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.
14 #include "vp9/common/vp9_systemdependent.h"
15 #include "vp9/encoder/vp9_block.h"
16 #include "vp9/encoder/vp9_encodeframe.h"
17 #include "vp9/encoder/vp9_encodemb.h"
18 #include "vp9/encoder/vp9_extend.h"
19 #include "vp9/encoder/vp9_firstpass.h"
20 #include "vp9/encoder/vp9_mcomp.h"
21 #include "vp9/encoder/vp9_onyx_int.h"
22 #include "vp9/encoder/vp9_variance.h"
23 #include "vpx_scale/vpx_scale.h"
24 #include "vpx_mem/vpx_mem.h"
25 #include "vpx_scale/yv12config.h"
26 #include "vp9/encoder/vp9_quantize.h"
27 #include "vp9/encoder/vp9_rdopt.h"
28 #include "vp9/encoder/vp9_ratectrl.h"
29 #include "vp9/common/vp9_quant_common.h"
30 #include "vp9/common/vp9_entropymv.h"
31 #include "vp9/encoder/vp9_encodemv.h"
32 #include "vp9/encoder/vp9_vaq.h"
33 #include "./vpx_scale_rtcd.h"
34 // TODO(jkoleszar): for setup_dst_planes
35 #include "vp9/common/vp9_reconinter.h"
40 #define IIKFACTOR1 12.5
41 #define IIKFACTOR2 15.0
44 #define ERR_DIVISOR 150.0
45 #define MIN_DECAY_FACTOR 0.1
47 #define KF_MB_INTRA_MIN 150
48 #define GF_MB_INTRA_MIN 100
50 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
52 #define MIN_KF_BOOST 300
54 #define DISABLE_RC_LONG_TERM_MEM 0
56 static void swap_yv12(YV12_BUFFER_CONFIG
*a
, YV12_BUFFER_CONFIG
*b
) {
57 YV12_BUFFER_CONFIG temp
= *a
;
62 static int select_cq_level(int qindex
) {
63 int ret_val
= QINDEX_RANGE
- 1;
66 double target_q
= (vp9_convert_qindex_to_q(qindex
) * 0.5847) + 1.0;
68 for (i
= 0; i
< QINDEX_RANGE
; ++i
) {
69 if (target_q
<= vp9_convert_qindex_to_q(i
)) {
78 static int gfboost_qadjust(int qindex
) {
79 const double q
= vp9_convert_qindex_to_q(qindex
);
80 return (int)((0.00000828 * q
* q
* q
) +
85 static int kfboost_qadjust(int qindex
) {
86 const double q
= vp9_convert_qindex_to_q(qindex
);
87 return (int)((0.00000973 * q
* q
* q
) +
92 // Resets the first pass file to the given position using a relative seek from
93 // the current position.
94 static void reset_fpf_position(struct twopass_rc
*p
,
95 FIRSTPASS_STATS
*position
) {
96 p
->stats_in
= position
;
99 static int lookup_next_frame_stats(const struct twopass_rc
*p
,
100 FIRSTPASS_STATS
*next_frame
) {
101 if (p
->stats_in
>= p
->stats_in_end
)
104 *next_frame
= *p
->stats_in
;
109 // Read frame stats at an offset from the current position.
110 static int read_frame_stats(const struct twopass_rc
*p
,
111 FIRSTPASS_STATS
*frame_stats
, int offset
) {
112 const FIRSTPASS_STATS
*fps_ptr
= p
->stats_in
;
114 // Check legality of offset.
116 if (&fps_ptr
[offset
] >= p
->stats_in_end
)
118 } else if (offset
< 0) {
119 if (&fps_ptr
[offset
] < p
->stats_in_start
)
123 *frame_stats
= fps_ptr
[offset
];
127 static int input_stats(struct twopass_rc
*p
, FIRSTPASS_STATS
*fps
) {
128 if (p
->stats_in
>= p
->stats_in_end
)
136 static void output_stats(const VP9_COMP
*cpi
,
137 struct vpx_codec_pkt_list
*pktlist
,
138 FIRSTPASS_STATS
*stats
) {
139 struct vpx_codec_cx_pkt pkt
;
140 pkt
.kind
= VPX_CODEC_STATS_PKT
;
141 pkt
.data
.twopass_stats
.buf
= stats
;
142 pkt
.data
.twopass_stats
.sz
= sizeof(FIRSTPASS_STATS
);
143 vpx_codec_pkt_list_add(pktlist
, &pkt
);
149 fpfile
= fopen("firstpass.stt", "a");
151 fprintf(fpfile
, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
152 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
153 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
157 stats
->sr_coded_error
,
158 stats
->ssim_weighted_pred_err
,
161 stats
->pcnt_second_ref
,
169 stats
->mv_in_out_count
,
178 static void zero_stats(FIRSTPASS_STATS
*section
) {
179 section
->frame
= 0.0;
180 section
->intra_error
= 0.0;
181 section
->coded_error
= 0.0;
182 section
->sr_coded_error
= 0.0;
183 section
->ssim_weighted_pred_err
= 0.0;
184 section
->pcnt_inter
= 0.0;
185 section
->pcnt_motion
= 0.0;
186 section
->pcnt_second_ref
= 0.0;
187 section
->pcnt_neutral
= 0.0;
189 section
->mvr_abs
= 0.0;
191 section
->mvc_abs
= 0.0;
194 section
->mv_in_out_count
= 0.0;
195 section
->new_mv_count
= 0.0;
196 section
->count
= 0.0;
197 section
->duration
= 1.0;
200 static void accumulate_stats(FIRSTPASS_STATS
*section
, FIRSTPASS_STATS
*frame
) {
201 section
->frame
+= frame
->frame
;
202 section
->intra_error
+= frame
->intra_error
;
203 section
->coded_error
+= frame
->coded_error
;
204 section
->sr_coded_error
+= frame
->sr_coded_error
;
205 section
->ssim_weighted_pred_err
+= frame
->ssim_weighted_pred_err
;
206 section
->pcnt_inter
+= frame
->pcnt_inter
;
207 section
->pcnt_motion
+= frame
->pcnt_motion
;
208 section
->pcnt_second_ref
+= frame
->pcnt_second_ref
;
209 section
->pcnt_neutral
+= frame
->pcnt_neutral
;
210 section
->MVr
+= frame
->MVr
;
211 section
->mvr_abs
+= frame
->mvr_abs
;
212 section
->MVc
+= frame
->MVc
;
213 section
->mvc_abs
+= frame
->mvc_abs
;
214 section
->MVrv
+= frame
->MVrv
;
215 section
->MVcv
+= frame
->MVcv
;
216 section
->mv_in_out_count
+= frame
->mv_in_out_count
;
217 section
->new_mv_count
+= frame
->new_mv_count
;
218 section
->count
+= frame
->count
;
219 section
->duration
+= frame
->duration
;
222 static void subtract_stats(FIRSTPASS_STATS
*section
, FIRSTPASS_STATS
*frame
) {
223 section
->frame
-= frame
->frame
;
224 section
->intra_error
-= frame
->intra_error
;
225 section
->coded_error
-= frame
->coded_error
;
226 section
->sr_coded_error
-= frame
->sr_coded_error
;
227 section
->ssim_weighted_pred_err
-= frame
->ssim_weighted_pred_err
;
228 section
->pcnt_inter
-= frame
->pcnt_inter
;
229 section
->pcnt_motion
-= frame
->pcnt_motion
;
230 section
->pcnt_second_ref
-= frame
->pcnt_second_ref
;
231 section
->pcnt_neutral
-= frame
->pcnt_neutral
;
232 section
->MVr
-= frame
->MVr
;
233 section
->mvr_abs
-= frame
->mvr_abs
;
234 section
->MVc
-= frame
->MVc
;
235 section
->mvc_abs
-= frame
->mvc_abs
;
236 section
->MVrv
-= frame
->MVrv
;
237 section
->MVcv
-= frame
->MVcv
;
238 section
->mv_in_out_count
-= frame
->mv_in_out_count
;
239 section
->new_mv_count
-= frame
->new_mv_count
;
240 section
->count
-= frame
->count
;
241 section
->duration
-= frame
->duration
;
244 static void avg_stats(FIRSTPASS_STATS
*section
) {
245 if (section
->count
< 1.0)
248 section
->intra_error
/= section
->count
;
249 section
->coded_error
/= section
->count
;
250 section
->sr_coded_error
/= section
->count
;
251 section
->ssim_weighted_pred_err
/= section
->count
;
252 section
->pcnt_inter
/= section
->count
;
253 section
->pcnt_second_ref
/= section
->count
;
254 section
->pcnt_neutral
/= section
->count
;
255 section
->pcnt_motion
/= section
->count
;
256 section
->MVr
/= section
->count
;
257 section
->mvr_abs
/= section
->count
;
258 section
->MVc
/= section
->count
;
259 section
->mvc_abs
/= section
->count
;
260 section
->MVrv
/= section
->count
;
261 section
->MVcv
/= section
->count
;
262 section
->mv_in_out_count
/= section
->count
;
263 section
->duration
/= section
->count
;
266 // Calculate a modified Error used in distributing bits between easier and
268 static double calculate_modified_err(const VP9_COMP
*cpi
,
269 const FIRSTPASS_STATS
*this_frame
) {
270 const struct twopass_rc
*const twopass
= &cpi
->twopass
;
271 const FIRSTPASS_STATS
*const stats
= &twopass
->total_stats
;
272 const double av_err
= stats
->ssim_weighted_pred_err
/ stats
->count
;
273 double modified_error
= av_err
* pow(this_frame
->ssim_weighted_pred_err
/
274 DOUBLE_DIVIDE_CHECK(av_err
),
275 cpi
->oxcf
.two_pass_vbrbias
/ 100.0);
277 return fclamp(modified_error
,
278 twopass
->modified_error_min
, twopass
->modified_error_max
);
281 static const double weight_table
[256] = {
282 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
283 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
284 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
285 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
286 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
287 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
288 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
289 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
290 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
291 0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
292 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
293 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
294 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
295 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
296 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
297 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
298 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
299 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
300 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
301 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
302 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
303 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
304 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
305 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
306 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
307 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
308 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
309 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
310 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
311 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
312 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
313 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
314 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
315 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
316 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
317 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
318 1.000000, 1.000000, 1.000000, 1.000000
321 static double simple_weight(const YV12_BUFFER_CONFIG
*buf
) {
324 const int w
= buf
->y_crop_width
;
325 const int h
= buf
->y_crop_height
;
326 const uint8_t *row
= buf
->y_buffer
;
328 for (i
= 0; i
< h
; ++i
) {
329 const uint8_t *pixel
= row
;
330 for (j
= 0; j
< w
; ++j
)
331 sum
+= weight_table
[*pixel
++];
332 row
+= buf
->y_stride
;
335 return MAX(0.1, sum
/ (w
* h
));
338 // This function returns the maximum target rate per frame.
339 static int frame_max_bits(const VP9_COMP
*cpi
) {
341 ((int64_t)cpi
->rc
.av_per_frame_bandwidth
*
342 (int64_t)cpi
->oxcf
.two_pass_vbrmax_section
) / 100;
346 else if (max_bits
> cpi
->rc
.max_frame_bandwidth
)
347 max_bits
= cpi
->rc
.max_frame_bandwidth
;
349 return (int)max_bits
;
352 void vp9_init_first_pass(VP9_COMP
*cpi
) {
353 zero_stats(&cpi
->twopass
.total_stats
);
356 void vp9_end_first_pass(VP9_COMP
*cpi
) {
357 output_stats(cpi
, cpi
->output_pkt_list
, &cpi
->twopass
.total_stats
);
360 static vp9_variance_fn_t
get_block_variance_fn(BLOCK_SIZE bsize
) {
373 static unsigned int zz_motion_search(const VP9_COMP
*cpi
, const MACROBLOCK
*x
) {
374 const MACROBLOCKD
*const xd
= &x
->e_mbd
;
375 const uint8_t *const src
= x
->plane
[0].src
.buf
;
376 const int src_stride
= x
->plane
[0].src
.stride
;
377 const uint8_t *const ref
= xd
->plane
[0].pre
[0].buf
;
378 const int ref_stride
= xd
->plane
[0].pre
[0].stride
;
380 vp9_variance_fn_t fn
= get_block_variance_fn(xd
->mi_8x8
[0]->mbmi
.sb_type
);
381 fn(src
, src_stride
, ref
, ref_stride
, &sse
);
385 static void first_pass_motion_search(VP9_COMP
*cpi
, MACROBLOCK
*x
,
386 const MV
*ref_mv
, MV
*best_mv
,
387 int *best_motion_err
) {
388 MACROBLOCKD
*const xd
= &x
->e_mbd
;
390 MV ref_mv_full
= {ref_mv
->row
>> 3, ref_mv
->col
>> 3};
391 int num00
, tmp_err
, n
, sr
= 0;
393 int further_steps
= (MAX_MVSEARCH_STEPS
- 1) - step_param
;
394 const BLOCK_SIZE bsize
= xd
->mi_8x8
[0]->mbmi
.sb_type
;
395 vp9_variance_fn_ptr_t v_fn_ptr
= cpi
->fn_ptr
[bsize
];
396 int new_mv_mode_penalty
= 256;
397 const int quart_frm
= MIN(cpi
->common
.width
, cpi
->common
.height
);
399 // Refine the motion search range according to the frame dimension
400 // for first pass test.
401 while ((quart_frm
<< sr
) < MAX_FULL_PEL_VAL
)
407 // Override the default variance function to use MSE.
408 v_fn_ptr
.vf
= get_block_variance_fn(bsize
);
410 // Center the initial step/diamond search on best mv.
411 tmp_err
= cpi
->diamond_search_sad(x
, &ref_mv_full
, &tmp_mv
,
413 x
->sadperbit16
, &num00
, &v_fn_ptr
,
416 if (tmp_err
< INT_MAX
- new_mv_mode_penalty
)
417 tmp_err
+= new_mv_mode_penalty
;
419 if (tmp_err
< *best_motion_err
) {
420 *best_motion_err
= tmp_err
;
421 best_mv
->row
= tmp_mv
.row
;
422 best_mv
->col
= tmp_mv
.col
;
425 // Carry out further step/diamond searches as necessary.
429 while (n
< further_steps
) {
435 tmp_err
= cpi
->diamond_search_sad(x
, &ref_mv_full
, &tmp_mv
,
436 step_param
+ n
, x
->sadperbit16
,
440 if (tmp_err
< INT_MAX
- new_mv_mode_penalty
)
441 tmp_err
+= new_mv_mode_penalty
;
443 if (tmp_err
< *best_motion_err
) {
444 *best_motion_err
= tmp_err
;
445 best_mv
->row
= tmp_mv
.row
;
446 best_mv
->col
= tmp_mv
.col
;
452 static BLOCK_SIZE
get_bsize(const VP9_COMMON
*cm
, int mb_row
, int mb_col
) {
453 if (2 * mb_col
+ 1 < cm
->mi_cols
) {
454 return 2 * mb_row
+ 1 < cm
->mi_rows
? BLOCK_16X16
457 return 2 * mb_row
+ 1 < cm
->mi_rows
? BLOCK_8X16
462 void vp9_first_pass(VP9_COMP
*cpi
) {
464 MACROBLOCK
*const x
= &cpi
->mb
;
465 VP9_COMMON
*const cm
= &cpi
->common
;
466 MACROBLOCKD
*const xd
= &x
->e_mbd
;
468 struct macroblock_plane
*const p
= x
->plane
;
469 struct macroblockd_plane
*const pd
= xd
->plane
;
470 const PICK_MODE_CONTEXT
*ctx
= &x
->sb64_context
;
473 int recon_yoffset
, recon_uvoffset
;
474 YV12_BUFFER_CONFIG
*const lst_yv12
= get_ref_frame_buffer(cpi
, LAST_FRAME
);
475 YV12_BUFFER_CONFIG
*const gld_yv12
= get_ref_frame_buffer(cpi
, GOLDEN_FRAME
);
476 YV12_BUFFER_CONFIG
*const new_yv12
= get_frame_new_buffer(cm
);
477 const int recon_y_stride
= lst_yv12
->y_stride
;
478 const int recon_uv_stride
= lst_yv12
->uv_stride
;
479 const int uv_mb_height
= 16 >> (lst_yv12
->y_height
> lst_yv12
->uv_height
);
480 int64_t intra_error
= 0;
481 int64_t coded_error
= 0;
482 int64_t sr_coded_error
= 0;
484 int sum_mvr
= 0, sum_mvc
= 0;
485 int sum_mvr_abs
= 0, sum_mvc_abs
= 0;
486 int64_t sum_mvrs
= 0, sum_mvcs
= 0;
489 int second_ref_count
= 0;
490 int intrapenalty
= 256;
491 int neutral_count
= 0;
492 int new_mv_count
= 0;
493 int sum_in_vectors
= 0;
494 uint32_t lastmv_as_int
= 0;
495 struct twopass_rc
*const twopass
= &cpi
->twopass
;
496 const MV zero_mv
= {0, 0};
498 vp9_clear_system_state(); // __asm emms;
500 vp9_setup_src_planes(x
, cpi
->Source
, 0, 0);
501 setup_pre_planes(xd
, 0, lst_yv12
, 0, 0, NULL
);
502 setup_dst_planes(xd
, new_yv12
, 0, 0);
504 xd
->mi_8x8
= cm
->mi_grid_visible
;
505 xd
->mi_8x8
[0] = cm
->mi
;
507 vp9_setup_block_planes(&x
->e_mbd
, cm
->subsampling_x
, cm
->subsampling_y
);
509 vp9_frame_init_quantizer(cpi
);
511 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
512 p
[i
].coeff
= ctx
->coeff_pbuf
[i
][1];
513 p
[i
].qcoeff
= ctx
->qcoeff_pbuf
[i
][1];
514 pd
[i
].dqcoeff
= ctx
->dqcoeff_pbuf
[i
][1];
515 p
[i
].eobs
= ctx
->eobs_pbuf
[i
][1];
519 vp9_init_mv_probs(cm
);
520 vp9_initialize_rd_consts(cpi
);
522 // Tiling is ignored in the first pass.
523 vp9_tile_init(&tile
, cm
, 0, 0);
525 for (mb_row
= 0; mb_row
< cm
->mb_rows
; ++mb_row
) {
528 best_ref_mv
.as_int
= 0;
530 // Reset above block coeffs.
531 xd
->up_available
= (mb_row
!= 0);
532 recon_yoffset
= (mb_row
* recon_y_stride
* 16);
533 recon_uvoffset
= (mb_row
* recon_uv_stride
* uv_mb_height
);
535 // Set up limit values for motion vectors to prevent them extending
536 // outside the UMV borders.
537 x
->mv_row_min
= -((mb_row
* 16) + BORDER_MV_PIXELS_B16
);
538 x
->mv_row_max
= ((cm
->mb_rows
- 1 - mb_row
) * 16)
539 + BORDER_MV_PIXELS_B16
;
541 for (mb_col
= 0; mb_col
< cm
->mb_cols
; ++mb_col
) {
543 const int use_dc_pred
= (mb_col
|| mb_row
) && (!mb_col
|| !mb_row
);
544 double error_weight
= 1.0;
545 const BLOCK_SIZE bsize
= get_bsize(cm
, mb_row
, mb_col
);
547 vp9_clear_system_state(); // __asm emms;
549 xd
->plane
[0].dst
.buf
= new_yv12
->y_buffer
+ recon_yoffset
;
550 xd
->plane
[1].dst
.buf
= new_yv12
->u_buffer
+ recon_uvoffset
;
551 xd
->plane
[2].dst
.buf
= new_yv12
->v_buffer
+ recon_uvoffset
;
552 xd
->left_available
= (mb_col
!= 0);
553 xd
->mi_8x8
[0]->mbmi
.sb_type
= bsize
;
554 xd
->mi_8x8
[0]->mbmi
.ref_frame
[0] = INTRA_FRAME
;
555 set_mi_row_col(xd
, &tile
,
556 mb_row
<< 1, num_8x8_blocks_high_lookup
[bsize
],
557 mb_col
<< 1, num_8x8_blocks_wide_lookup
[bsize
],
558 cm
->mi_rows
, cm
->mi_cols
);
560 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
561 const int energy
= vp9_block_energy(cpi
, x
, bsize
);
562 error_weight
= vp9_vaq_inv_q_ratio(energy
);
565 // Do intra 16x16 prediction.
566 this_error
= vp9_encode_intra(x
, use_dc_pred
);
567 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
568 vp9_clear_system_state(); // __asm emms;
569 this_error
= (int)(this_error
* error_weight
);
572 // Intrapenalty below deals with situations where the intra and inter
573 // error scores are very low (e.g. a plain black frame).
574 // We do not have special cases in first pass for 0,0 and nearest etc so
575 // all inter modes carry an overhead cost estimate for the mv.
576 // When the error score is very low this causes us to pick all or lots of
577 // INTRA modes and throw lots of key frames.
578 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
579 this_error
+= intrapenalty
;
581 // Accumulate the intra error.
582 intra_error
+= (int64_t)this_error
;
584 // Set up limit values for motion vectors to prevent them extending
585 // outside the UMV borders.
586 x
->mv_col_min
= -((mb_col
* 16) + BORDER_MV_PIXELS_B16
);
587 x
->mv_col_max
= ((cm
->mb_cols
- 1 - mb_col
) * 16) + BORDER_MV_PIXELS_B16
;
589 // Other than for the first frame do a motion search.
590 if (cm
->current_video_frame
> 0) {
591 int tmp_err
, motion_error
;
594 xd
->plane
[0].pre
[0].buf
= lst_yv12
->y_buffer
+ recon_yoffset
;
595 motion_error
= zz_motion_search(cpi
, x
);
596 // Assume 0,0 motion with no mv overhead.
597 mv
.as_int
= tmp_mv
.as_int
= 0;
599 // Test last reference frame using the previous best mv as the
600 // starting point (best reference) for the search.
601 first_pass_motion_search(cpi
, x
, &best_ref_mv
.as_mv
, &mv
.as_mv
,
603 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
604 vp9_clear_system_state(); // __asm emms;
605 motion_error
= (int)(motion_error
* error_weight
);
608 // If the current best reference mv is not centered on 0,0 then do a 0,0
609 // based search as well.
610 if (best_ref_mv
.as_int
) {
612 first_pass_motion_search(cpi
, x
, &zero_mv
, &tmp_mv
.as_mv
,
614 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
615 vp9_clear_system_state(); // __asm emms;
616 tmp_err
= (int)(tmp_err
* error_weight
);
619 if (tmp_err
< motion_error
) {
620 motion_error
= tmp_err
;
621 mv
.as_int
= tmp_mv
.as_int
;
625 // Search in an older reference frame.
626 if (cm
->current_video_frame
> 1) {
627 // Assume 0,0 motion with no mv overhead.
630 xd
->plane
[0].pre
[0].buf
= gld_yv12
->y_buffer
+ recon_yoffset
;
631 gf_motion_error
= zz_motion_search(cpi
, x
);
633 first_pass_motion_search(cpi
, x
, &zero_mv
, &tmp_mv
.as_mv
,
635 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
636 vp9_clear_system_state(); // __asm emms;
637 gf_motion_error
= (int)(gf_motion_error
* error_weight
);
640 if (gf_motion_error
< motion_error
&& gf_motion_error
< this_error
)
643 // Reset to last frame as reference buffer.
644 xd
->plane
[0].pre
[0].buf
= lst_yv12
->y_buffer
+ recon_yoffset
;
645 xd
->plane
[1].pre
[0].buf
= lst_yv12
->u_buffer
+ recon_uvoffset
;
646 xd
->plane
[2].pre
[0].buf
= lst_yv12
->v_buffer
+ recon_uvoffset
;
648 // In accumulating a score for the older reference frame take the
649 // best of the motion predicted score and the intra coded error
650 // (just as will be done for) accumulation of "coded_error" for
652 if (gf_motion_error
< this_error
)
653 sr_coded_error
+= gf_motion_error
;
655 sr_coded_error
+= this_error
;
657 sr_coded_error
+= motion_error
;
659 // Start by assuming that intra mode is best.
660 best_ref_mv
.as_int
= 0;
662 if (motion_error
<= this_error
) {
663 // Keep a count of cases where the inter and intra were very close
664 // and very low. This helps with scene cut detection for example in
665 // cropped clips with black bars at the sides or top and bottom.
666 if (((this_error
- intrapenalty
) * 9 <= motion_error
* 10) &&
667 this_error
< 2 * intrapenalty
)
672 this_error
= motion_error
;
673 vp9_set_mbmode_and_mvs(xd
, NEWMV
, &mv
.as_mv
);
674 xd
->mi_8x8
[0]->mbmi
.tx_size
= TX_4X4
;
675 xd
->mi_8x8
[0]->mbmi
.ref_frame
[0] = LAST_FRAME
;
676 xd
->mi_8x8
[0]->mbmi
.ref_frame
[1] = NONE
;
677 vp9_build_inter_predictors_sby(xd
, mb_row
<< 1, mb_col
<< 1, bsize
);
678 vp9_encode_sby(x
, bsize
);
679 sum_mvr
+= mv
.as_mv
.row
;
680 sum_mvr_abs
+= abs(mv
.as_mv
.row
);
681 sum_mvc
+= mv
.as_mv
.col
;
682 sum_mvc_abs
+= abs(mv
.as_mv
.col
);
683 sum_mvrs
+= mv
.as_mv
.row
* mv
.as_mv
.row
;
684 sum_mvcs
+= mv
.as_mv
.col
* mv
.as_mv
.col
;
687 best_ref_mv
.as_int
= mv
.as_int
;
692 // Non-zero vector, was it different from the last non zero vector?
693 if (mv
.as_int
!= lastmv_as_int
)
695 lastmv_as_int
= mv
.as_int
;
697 // Does the row vector point inwards or outwards?
698 if (mb_row
< cm
->mb_rows
/ 2) {
699 if (mv
.as_mv
.row
> 0)
701 else if (mv
.as_mv
.row
< 0)
703 } else if (mb_row
> cm
->mb_rows
/ 2) {
704 if (mv
.as_mv
.row
> 0)
706 else if (mv
.as_mv
.row
< 0)
710 // Does the col vector point inwards or outwards?
711 if (mb_col
< cm
->mb_cols
/ 2) {
712 if (mv
.as_mv
.col
> 0)
714 else if (mv
.as_mv
.col
< 0)
716 } else if (mb_col
> cm
->mb_cols
/ 2) {
717 if (mv
.as_mv
.col
> 0)
719 else if (mv
.as_mv
.col
< 0)
725 sr_coded_error
+= (int64_t)this_error
;
727 coded_error
+= (int64_t)this_error
;
729 // Adjust to the next column of MBs.
730 x
->plane
[0].src
.buf
+= 16;
731 x
->plane
[1].src
.buf
+= uv_mb_height
;
732 x
->plane
[2].src
.buf
+= uv_mb_height
;
735 recon_uvoffset
+= uv_mb_height
;
738 // Adjust to the next row of MBs.
739 x
->plane
[0].src
.buf
+= 16 * x
->plane
[0].src
.stride
- 16 * cm
->mb_cols
;
740 x
->plane
[1].src
.buf
+= uv_mb_height
* x
->plane
[1].src
.stride
-
741 uv_mb_height
* cm
->mb_cols
;
742 x
->plane
[2].src
.buf
+= uv_mb_height
* x
->plane
[1].src
.stride
-
743 uv_mb_height
* cm
->mb_cols
;
745 vp9_clear_system_state(); // __asm emms;
748 vp9_clear_system_state(); // __asm emms;
752 fps
.frame
= cm
->current_video_frame
;
753 fps
.intra_error
= (double)(intra_error
>> 8);
754 fps
.coded_error
= (double)(coded_error
>> 8);
755 fps
.sr_coded_error
= (double)(sr_coded_error
>> 8);
756 fps
.ssim_weighted_pred_err
= fps
.coded_error
* simple_weight(cpi
->Source
);
758 fps
.pcnt_inter
= (double)intercount
/ cm
->MBs
;
759 fps
.pcnt_second_ref
= (double)second_ref_count
/ cm
->MBs
;
760 fps
.pcnt_neutral
= (double)neutral_count
/ cm
->MBs
;
763 fps
.MVr
= (double)sum_mvr
/ mvcount
;
764 fps
.mvr_abs
= (double)sum_mvr_abs
/ mvcount
;
765 fps
.MVc
= (double)sum_mvc
/ mvcount
;
766 fps
.mvc_abs
= (double)sum_mvc_abs
/ mvcount
;
767 fps
.MVrv
= ((double)sum_mvrs
- (fps
.MVr
* fps
.MVr
/ mvcount
)) / mvcount
;
768 fps
.MVcv
= ((double)sum_mvcs
- (fps
.MVc
* fps
.MVc
/ mvcount
)) / mvcount
;
769 fps
.mv_in_out_count
= (double)sum_in_vectors
/ (mvcount
* 2);
770 fps
.new_mv_count
= new_mv_count
;
771 fps
.pcnt_motion
= (double)mvcount
/ cm
->MBs
;
779 fps
.mv_in_out_count
= 0.0;
780 fps
.new_mv_count
= 0.0;
781 fps
.pcnt_motion
= 0.0;
784 // TODO(paulwilkins): Handle the case when duration is set to 0, or
785 // something less than the full time between subsequent values of
786 // cpi->source_time_stamp.
787 fps
.duration
= (double)(cpi
->source
->ts_end
- cpi
->source
->ts_start
);
789 // Don't want to do output stats with a stack variable!
790 twopass
->this_frame_stats
= fps
;
791 output_stats(cpi
, cpi
->output_pkt_list
, &twopass
->this_frame_stats
);
792 accumulate_stats(&twopass
->total_stats
, &fps
);
795 // Copy the previous Last Frame back into gf and and arf buffers if
796 // the prediction is good enough... but also don't allow it to lag too far.
797 if ((twopass
->sr_update_lag
> 3) ||
798 ((cm
->current_video_frame
> 0) &&
799 (twopass
->this_frame_stats
.pcnt_inter
> 0.20) &&
800 ((twopass
->this_frame_stats
.intra_error
/
801 DOUBLE_DIVIDE_CHECK(twopass
->this_frame_stats
.coded_error
)) > 2.0))) {
802 vp8_yv12_copy_frame(lst_yv12
, gld_yv12
);
803 twopass
->sr_update_lag
= 1;
805 ++twopass
->sr_update_lag
;
807 // Swap frame pointers so last frame refers to the frame we just compressed.
808 swap_yv12(lst_yv12
, new_yv12
);
810 vp9_extend_frame_borders(lst_yv12
, cm
->subsampling_x
, cm
->subsampling_y
);
812 // Special case for the first frame. Copy into the GF buffer as a second
814 if (cm
->current_video_frame
== 0)
815 vp8_yv12_copy_frame(lst_yv12
, gld_yv12
);
817 // Use this to see what the first pass reconstruction looks like.
821 snprintf(filename
, sizeof(filename
), "enc%04d.yuv",
822 (int)cm
->current_video_frame
);
824 if (cm
->current_video_frame
== 0)
825 recon_file
= fopen(filename
, "wb");
827 recon_file
= fopen(filename
, "ab");
829 (void)fwrite(lst_yv12
->buffer_alloc
, lst_yv12
->frame_size
, 1, recon_file
);
833 ++cm
->current_video_frame
;
836 // Estimate a cost per mb attributable to overheads such as the coding of modes
837 // and motion vectors. This currently makes simplistic assumptions for testing.
838 static double bitcost(double prob
) {
839 return -(log(prob
) / log(2.0));
842 static int64_t estimate_modemvcost(VP9_COMP
*cpi
,
843 FIRSTPASS_STATS
*fpstats
) {
848 double av_pct_inter
= fpstats
->pcnt_inter
/ fpstats
->count
;
849 double av_pct_motion
= fpstats
->pcnt_motion
/ fpstats
->count
;
850 double av_intra
= (1.0 - av_pct_inter
);
856 zz_cost
= bitcost(av_pct_inter
- av_pct_motion
);
857 motion_cost
= bitcost(av_pct_motion
);
858 intra_cost
= bitcost(av_intra
);
860 // Estimate the number of extra bits per mv overhead for mbs. We shift (<< 9)
861 // to match the scaling of number of bits by 512.
862 mv_cost
= ((int)(fpstats
->new_mv_count
/ fpstats
->count
) * 8) << 9;
864 // Produce a crude estimate of the overhead cost from modes. We shift (<< 9)
865 // to match the scaling of number of bits by 512.
867 (int)((((av_pct_inter
- av_pct_motion
) * zz_cost
) +
868 (av_pct_motion
* motion_cost
) +
869 (av_intra
* intra_cost
)) * cpi
->common
.MBs
) << 9;
871 // TODO(paulwilkins): Fix overhead costs for extended Q range.
876 static double calc_correction_factor(double err_per_mb
,
881 const double error_term
= err_per_mb
/ err_divisor
;
883 // Adjustment based on actual quantizer to power term.
884 const double power_term
= MIN(vp9_convert_qindex_to_q(q
) * 0.0125 + pt_low
,
887 // Calculate correction factor.
888 if (power_term
< 1.0)
889 assert(error_term
>= 0.0);
891 return fclamp(pow(error_term
, power_term
), 0.05, 5.0);
894 int vp9_twopass_worst_quality(VP9_COMP
*cpi
, FIRSTPASS_STATS
*fpstats
,
895 int section_target_bandwitdh
) {
897 const int num_mbs
= cpi
->common
.MBs
;
898 int target_norm_bits_per_mb
;
899 const RATE_CONTROL
*const rc
= &cpi
->rc
;
901 const double section_err
= fpstats
->coded_error
/ fpstats
->count
;
902 const double err_per_mb
= section_err
/ num_mbs
;
904 if (section_target_bandwitdh
<= 0)
905 return rc
->worst_quality
; // Highest value allowed
907 target_norm_bits_per_mb
= section_target_bandwitdh
< (1 << 20)
908 ? (512 * section_target_bandwitdh
) / num_mbs
909 : 512 * (section_target_bandwitdh
/ num_mbs
);
911 // Try and pick a max Q that will be high enough to encode the
912 // content at the given rate.
913 for (q
= rc
->best_quality
; q
< rc
->worst_quality
; ++q
) {
914 const double err_correction_factor
= calc_correction_factor(err_per_mb
,
915 ERR_DIVISOR
, 0.5, 0.90, q
);
916 const int bits_per_mb_at_this_q
= vp9_rc_bits_per_mb(INTER_FRAME
, q
,
917 err_correction_factor
);
918 if (bits_per_mb_at_this_q
<= target_norm_bits_per_mb
)
922 // Restriction on active max q for constrained quality mode.
923 if (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
)
924 q
= MAX(q
, cpi
->cq_target_quality
);
929 extern void vp9_new_framerate(VP9_COMP
*cpi
, double framerate
);
931 void vp9_init_second_pass(VP9_COMP
*cpi
) {
932 FIRSTPASS_STATS this_frame
;
933 FIRSTPASS_STATS
*start_pos
;
934 struct twopass_rc
*const twopass
= &cpi
->twopass
;
935 const VP9_CONFIG
*const oxcf
= &cpi
->oxcf
;
937 zero_stats(&twopass
->total_stats
);
938 zero_stats(&twopass
->total_left_stats
);
940 if (!twopass
->stats_in_end
)
943 twopass
->total_stats
= *twopass
->stats_in_end
;
944 twopass
->total_left_stats
= twopass
->total_stats
;
946 // Each frame can have a different duration, as the frame rate in the source
947 // isn't guaranteed to be constant. The frame rate prior to the first frame
948 // encoded in the second pass is a guess. However, the sum duration is not.
949 // It is calculated based on the actual durations of all frames from the
951 vp9_new_framerate(cpi
, 10000000.0 * twopass
->total_stats
.count
/
952 twopass
->total_stats
.duration
);
954 cpi
->output_framerate
= oxcf
->framerate
;
955 twopass
->bits_left
= (int64_t)(twopass
->total_stats
.duration
*
956 oxcf
->target_bandwidth
/ 10000000.0);
958 // Calculate a minimum intra value to be used in determining the IIratio
959 // scores used in the second pass. We have this minimum to make sure
960 // that clips that are static but "low complexity" in the intra domain
961 // are still boosted appropriately for KF/GF/ARF.
962 twopass
->kf_intra_err_min
= KF_MB_INTRA_MIN
* cpi
->common
.MBs
;
963 twopass
->gf_intra_err_min
= GF_MB_INTRA_MIN
* cpi
->common
.MBs
;
965 // This variable monitors how far behind the second ref update is lagging.
966 twopass
->sr_update_lag
= 1;
968 // Scan the first pass file and calculate an average Intra / Inter error score
969 // ratio for the sequence.
971 double sum_iiratio
= 0.0;
972 start_pos
= twopass
->stats_in
;
974 while (input_stats(twopass
, &this_frame
) != EOF
) {
975 const double iiratio
= this_frame
.intra_error
/
976 DOUBLE_DIVIDE_CHECK(this_frame
.coded_error
);
977 sum_iiratio
+= fclamp(iiratio
, 1.0, 20.0);
980 twopass
->avg_iiratio
= sum_iiratio
/
981 DOUBLE_DIVIDE_CHECK((double)twopass
->total_stats
.count
);
983 reset_fpf_position(twopass
, start_pos
);
986 // Scan the first pass file and calculate a modified total error based upon
987 // the bias/power function used to allocate bits.
989 double av_error
= twopass
->total_stats
.ssim_weighted_pred_err
/
990 DOUBLE_DIVIDE_CHECK(twopass
->total_stats
.count
);
992 start_pos
= twopass
->stats_in
;
994 twopass
->modified_error_total
= 0.0;
995 twopass
->modified_error_min
=
996 (av_error
* oxcf
->two_pass_vbrmin_section
) / 100;
997 twopass
->modified_error_max
=
998 (av_error
* oxcf
->two_pass_vbrmax_section
) / 100;
1000 while (input_stats(twopass
, &this_frame
) != EOF
) {
1001 twopass
->modified_error_total
+=
1002 calculate_modified_err(cpi
, &this_frame
);
1004 twopass
->modified_error_left
= twopass
->modified_error_total
;
1006 reset_fpf_position(twopass
, start_pos
);
1010 void vp9_end_second_pass(VP9_COMP
*cpi
) {
1013 // This function gives an estimate of how badly we believe the prediction
1014 // quality is decaying from frame to frame.
1015 static double get_prediction_decay_rate(const VP9_COMMON
*cm
,
1016 const FIRSTPASS_STATS
*next_frame
) {
1017 // Look at the observed drop in prediction quality between the last frame
1018 // and the GF buffer (which contains an older frame).
1019 const double mb_sr_err_diff
= (next_frame
->sr_coded_error
-
1020 next_frame
->coded_error
) / cm
->MBs
;
1021 const double second_ref_decay
= mb_sr_err_diff
<= 512.0
1022 ? fclamp(pow(1.0 - (mb_sr_err_diff
/ 512.0), 0.5), 0.85, 1.0)
1025 return MIN(second_ref_decay
, next_frame
->pcnt_inter
);
1028 // Function to test for a condition where a complex transition is followed
1029 // by a static section. For example in slide shows where there is a fade
1030 // between slides. This is to help with more optimal kf and gf positioning.
1031 static int detect_transition_to_still(VP9_COMP
*cpi
, int frame_interval
,
1033 double loop_decay_rate
,
1034 double last_decay_rate
) {
1035 int trans_to_still
= 0;
1037 // Break clause to detect very still sections after motion
1038 // For example a static image after a fade or other transition
1039 // instead of a clean scene cut.
1040 if (frame_interval
> MIN_GF_INTERVAL
&&
1041 loop_decay_rate
>= 0.999 &&
1042 last_decay_rate
< 0.9) {
1044 FIRSTPASS_STATS
*position
= cpi
->twopass
.stats_in
;
1045 FIRSTPASS_STATS tmp_next_frame
;
1047 // Look ahead a few frames to see if static condition persists...
1048 for (j
= 0; j
< still_interval
; ++j
) {
1049 if (EOF
== input_stats(&cpi
->twopass
, &tmp_next_frame
))
1052 if (tmp_next_frame
.pcnt_inter
- tmp_next_frame
.pcnt_motion
< 0.999)
1056 reset_fpf_position(&cpi
->twopass
, position
);
1058 // Only if it does do we signal a transition to still.
1059 if (j
== still_interval
)
1063 return trans_to_still
;
1066 // This function detects a flash through the high relative pcnt_second_ref
1067 // score in the frame following a flash frame. The offset passed in should
1069 static int detect_flash(const struct twopass_rc
*twopass
, int offset
) {
1070 FIRSTPASS_STATS next_frame
;
1072 int flash_detected
= 0;
1074 // Read the frame data.
1075 // The return is FALSE (no flash detected) if not a valid frame
1076 if (read_frame_stats(twopass
, &next_frame
, offset
) != EOF
) {
1077 // What we are looking for here is a situation where there is a
1078 // brief break in prediction (such as a flash) but subsequent frames
1079 // are reasonably well predicted by an earlier (pre flash) frame.
1080 // The recovery after a flash is indicated by a high pcnt_second_ref
1081 // compared to pcnt_inter.
1082 if (next_frame
.pcnt_second_ref
> next_frame
.pcnt_inter
&&
1083 next_frame
.pcnt_second_ref
>= 0.5)
1087 return flash_detected
;
1090 // Update the motion related elements to the GF arf boost calculation.
1091 static void accumulate_frame_motion_stats(
1092 FIRSTPASS_STATS
*this_frame
,
1093 double *this_frame_mv_in_out
,
1094 double *mv_in_out_accumulator
,
1095 double *abs_mv_in_out_accumulator
,
1096 double *mv_ratio_accumulator
) {
1099 // Accumulate motion stats.
1100 motion_pct
= this_frame
->pcnt_motion
;
1102 // Accumulate Motion In/Out of frame stats.
1103 *this_frame_mv_in_out
= this_frame
->mv_in_out_count
* motion_pct
;
1104 *mv_in_out_accumulator
+= this_frame
->mv_in_out_count
* motion_pct
;
1105 *abs_mv_in_out_accumulator
+= fabs(this_frame
->mv_in_out_count
* motion_pct
);
1107 // Accumulate a measure of how uniform (or conversely how random)
1108 // the motion field is (a ratio of absmv / mv).
1109 if (motion_pct
> 0.05) {
1110 const double this_frame_mvr_ratio
= fabs(this_frame
->mvr_abs
) /
1111 DOUBLE_DIVIDE_CHECK(fabs(this_frame
->MVr
));
1113 const double this_frame_mvc_ratio
= fabs(this_frame
->mvc_abs
) /
1114 DOUBLE_DIVIDE_CHECK(fabs(this_frame
->MVc
));
1116 *mv_ratio_accumulator
+= (this_frame_mvr_ratio
< this_frame
->mvr_abs
)
1117 ? (this_frame_mvr_ratio
* motion_pct
)
1118 : this_frame
->mvr_abs
* motion_pct
;
1120 *mv_ratio_accumulator
+= (this_frame_mvc_ratio
< this_frame
->mvc_abs
)
1121 ? (this_frame_mvc_ratio
* motion_pct
)
1122 : this_frame
->mvc_abs
* motion_pct
;
1126 // Calculate a baseline boost number for the current frame.
1127 static double calc_frame_boost(VP9_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
,
1128 double this_frame_mv_in_out
) {
1131 // Underlying boost factor is based on inter intra error ratio.
1132 if (this_frame
->intra_error
> cpi
->twopass
.gf_intra_err_min
)
1133 frame_boost
= (IIFACTOR
* this_frame
->intra_error
/
1134 DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
));
1136 frame_boost
= (IIFACTOR
* cpi
->twopass
.gf_intra_err_min
/
1137 DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
));
1139 // Increase boost for frames where new data coming into frame (e.g. zoom out).
1140 // Slightly reduce boost if there is a net balance of motion out of the frame
1141 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1142 if (this_frame_mv_in_out
> 0.0)
1143 frame_boost
+= frame_boost
* (this_frame_mv_in_out
* 2.0);
1144 // In the extreme case the boost is halved.
1146 frame_boost
+= frame_boost
* (this_frame_mv_in_out
/ 2.0);
1148 return MIN(frame_boost
, GF_RMAX
);
1151 static int calc_arf_boost(VP9_COMP
*cpi
, int offset
,
1152 int f_frames
, int b_frames
,
1153 int *f_boost
, int *b_boost
) {
1154 FIRSTPASS_STATS this_frame
;
1155 struct twopass_rc
*const twopass
= &cpi
->twopass
;
1157 double boost_score
= 0.0;
1158 double mv_ratio_accumulator
= 0.0;
1159 double decay_accumulator
= 1.0;
1160 double this_frame_mv_in_out
= 0.0;
1161 double mv_in_out_accumulator
= 0.0;
1162 double abs_mv_in_out_accumulator
= 0.0;
1164 int flash_detected
= 0;
1166 // Search forward from the proposed arf/next gf position.
1167 for (i
= 0; i
< f_frames
; ++i
) {
1168 if (read_frame_stats(twopass
, &this_frame
, (i
+ offset
)) == EOF
)
1171 // Update the motion related elements to the boost calculation.
1172 accumulate_frame_motion_stats(&this_frame
,
1173 &this_frame_mv_in_out
, &mv_in_out_accumulator
,
1174 &abs_mv_in_out_accumulator
,
1175 &mv_ratio_accumulator
);
1177 // We want to discount the flash frame itself and the recovery
1178 // frame that follows as both will have poor scores.
1179 flash_detected
= detect_flash(twopass
, i
+ offset
) ||
1180 detect_flash(twopass
, i
+ offset
+ 1);
1182 // Accumulate the effect of prediction quality decay.
1183 if (!flash_detected
) {
1184 decay_accumulator
*= get_prediction_decay_rate(&cpi
->common
, &this_frame
);
1185 decay_accumulator
= decay_accumulator
< MIN_DECAY_FACTOR
1186 ? MIN_DECAY_FACTOR
: decay_accumulator
;
1189 boost_score
+= (decay_accumulator
*
1190 calc_frame_boost(cpi
, &this_frame
, this_frame_mv_in_out
));
1193 *f_boost
= (int)boost_score
;
1195 // Reset for backward looking loop.
1197 mv_ratio_accumulator
= 0.0;
1198 decay_accumulator
= 1.0;
1199 this_frame_mv_in_out
= 0.0;
1200 mv_in_out_accumulator
= 0.0;
1201 abs_mv_in_out_accumulator
= 0.0;
1203 // Search backward towards last gf position.
1204 for (i
= -1; i
>= -b_frames
; --i
) {
1205 if (read_frame_stats(twopass
, &this_frame
, (i
+ offset
)) == EOF
)
1208 // Update the motion related elements to the boost calculation.
1209 accumulate_frame_motion_stats(&this_frame
,
1210 &this_frame_mv_in_out
, &mv_in_out_accumulator
,
1211 &abs_mv_in_out_accumulator
,
1212 &mv_ratio_accumulator
);
1214 // We want to discount the the flash frame itself and the recovery
1215 // frame that follows as both will have poor scores.
1216 flash_detected
= detect_flash(twopass
, i
+ offset
) ||
1217 detect_flash(twopass
, i
+ offset
+ 1);
1219 // Cumulative effect of prediction quality decay.
1220 if (!flash_detected
) {
1221 decay_accumulator
*= get_prediction_decay_rate(&cpi
->common
, &this_frame
);
1222 decay_accumulator
= decay_accumulator
< MIN_DECAY_FACTOR
1223 ? MIN_DECAY_FACTOR
: decay_accumulator
;
1226 boost_score
+= (decay_accumulator
*
1227 calc_frame_boost(cpi
, &this_frame
, this_frame_mv_in_out
));
1229 *b_boost
= (int)boost_score
;
1231 arf_boost
= (*f_boost
+ *b_boost
);
1232 if (arf_boost
< ((b_frames
+ f_frames
) * 20))
1233 arf_boost
= ((b_frames
+ f_frames
) * 20);
1238 #if CONFIG_MULTIPLE_ARF
1239 // Work out the frame coding order for a GF or an ARF group.
1240 // The current implementation codes frames in their natural order for a
1241 // GF group, and inserts additional ARFs into an ARF group using a
1242 // binary split approach.
1243 // NOTE: this function is currently implemented recursively.
1244 static void schedule_frames(VP9_COMP
*cpi
, const int start
, const int end
,
1245 const int arf_idx
, const int gf_or_arf_group
,
1247 int i
, abs_end
, half_range
;
1248 int *cfo
= cpi
->frame_coding_order
;
1249 int idx
= cpi
->new_frame_coding_order_period
;
1251 // If (end < 0) an ARF should be coded at position (-end).
1254 // printf("start:%d end:%d\n", start, end);
1256 // GF Group: code frames in logical order.
1257 if (gf_or_arf_group
== 0) {
1258 assert(end
>= start
);
1259 for (i
= start
; i
<= end
; ++i
) {
1261 cpi
->arf_buffer_idx
[idx
] = arf_idx
;
1262 cpi
->arf_weight
[idx
] = -1;
1265 cpi
->new_frame_coding_order_period
= idx
;
1269 // ARF Group: Work out the ARF schedule and mark ARF frames as negative.
1271 // printf("start:%d end:%d\n", -end, -end);
1272 // ARF frame is at the end of the range.
1274 // What ARF buffer does this ARF use as predictor.
1275 cpi
->arf_buffer_idx
[idx
] = (arf_idx
> 2) ? (arf_idx
- 1) : 2;
1276 cpi
->arf_weight
[idx
] = level
;
1283 half_range
= (abs_end
- start
) >> 1;
1285 // ARFs may not be adjacent, they must be separated by at least
1286 // MIN_GF_INTERVAL non-ARF frames.
1287 if ((start
+ MIN_GF_INTERVAL
) >= (abs_end
- MIN_GF_INTERVAL
)) {
1288 // printf("start:%d end:%d\n", start, abs_end);
1289 // Update the coding order and active ARF.
1290 for (i
= start
; i
<= abs_end
; ++i
) {
1292 cpi
->arf_buffer_idx
[idx
] = arf_idx
;
1293 cpi
->arf_weight
[idx
] = -1;
1296 cpi
->new_frame_coding_order_period
= idx
;
1298 // Place a new ARF at the mid-point of the range.
1299 cpi
->new_frame_coding_order_period
= idx
;
1300 schedule_frames(cpi
, start
, -(start
+ half_range
), arf_idx
+ 1,
1301 gf_or_arf_group
, level
+ 1);
1302 schedule_frames(cpi
, start
+ half_range
+ 1, abs_end
, arf_idx
,
1303 gf_or_arf_group
, level
+ 1);
1307 #define FIXED_ARF_GROUP_SIZE 16
1309 void define_fixed_arf_period(VP9_COMP
*cpi
) {
1311 int max_level
= INT_MIN
;
1313 assert(cpi
->multi_arf_enabled
);
1314 assert(cpi
->oxcf
.lag_in_frames
>= FIXED_ARF_GROUP_SIZE
);
1316 // Save the weight of the last frame in the sequence before next
1317 // sequence pattern overwrites it.
1318 cpi
->this_frame_weight
= cpi
->arf_weight
[cpi
->sequence_number
];
1319 assert(cpi
->this_frame_weight
>= 0);
1321 cpi
->twopass
.gf_zeromotion_pct
= 0;
1323 // Initialize frame coding order variables.
1324 cpi
->new_frame_coding_order_period
= 0;
1325 cpi
->next_frame_in_order
= 0;
1326 cpi
->arf_buffered
= 0;
1327 vp9_zero(cpi
->frame_coding_order
);
1328 vp9_zero(cpi
->arf_buffer_idx
);
1329 vpx_memset(cpi
->arf_weight
, -1, sizeof(cpi
->arf_weight
));
1331 if (cpi
->rc
.frames_to_key
<= (FIXED_ARF_GROUP_SIZE
+ 8)) {
1332 // Setup a GF group close to the keyframe.
1333 cpi
->rc
.source_alt_ref_pending
= 0;
1334 cpi
->rc
.baseline_gf_interval
= cpi
->rc
.frames_to_key
;
1335 schedule_frames(cpi
, 0, (cpi
->rc
.baseline_gf_interval
- 1), 2, 0, 0);
1337 // Setup a fixed period ARF group.
1338 cpi
->rc
.source_alt_ref_pending
= 1;
1339 cpi
->rc
.baseline_gf_interval
= FIXED_ARF_GROUP_SIZE
;
1340 schedule_frames(cpi
, 0, -(cpi
->rc
.baseline_gf_interval
- 1), 2, 1, 0);
1343 // Replace level indicator of -1 with correct level.
1344 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1345 if (cpi
->arf_weight
[i
] > max_level
) {
1346 max_level
= cpi
->arf_weight
[i
];
1350 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1351 if (cpi
->arf_weight
[i
] == -1) {
1352 cpi
->arf_weight
[i
] = max_level
;
1355 cpi
->max_arf_level
= max_level
;
1357 printf("\nSchedule: ");
1358 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1359 printf("%4d ", cpi
->frame_coding_order
[i
]);
1363 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1364 printf("%4d ", cpi
->arf_buffer_idx
[i
]);
1368 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1369 printf("%4d ", cpi
->arf_weight
[i
]);
1376 // Analyse and define a gf/arf group.
1377 static void define_gf_group(VP9_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
) {
1378 FIRSTPASS_STATS next_frame
= { 0 };
1379 FIRSTPASS_STATS
*start_pos
;
1380 struct twopass_rc
*const twopass
= &cpi
->twopass
;
1382 double boost_score
= 0.0;
1383 double old_boost_score
= 0.0;
1384 double gf_group_err
= 0.0;
1385 double gf_first_frame_err
= 0.0;
1386 double mod_frame_err
= 0.0;
1388 double mv_ratio_accumulator
= 0.0;
1389 double decay_accumulator
= 1.0;
1390 double zero_motion_accumulator
= 1.0;
1392 double loop_decay_rate
= 1.00;
1393 double last_loop_decay_rate
= 1.00;
1395 double this_frame_mv_in_out
= 0.0;
1396 double mv_in_out_accumulator
= 0.0;
1397 double abs_mv_in_out_accumulator
= 0.0;
1398 double mv_ratio_accumulator_thresh
;
1399 const int max_bits
= frame_max_bits(cpi
); // Max bits for a single frame.
1401 unsigned int allow_alt_ref
= cpi
->oxcf
.play_alternate
&&
1402 cpi
->oxcf
.lag_in_frames
;
1407 int active_max_gf_interval
;
1408 RATE_CONTROL
*const rc
= &cpi
->rc
;
1410 twopass
->gf_group_bits
= 0;
1412 vp9_clear_system_state(); // __asm emms;
1414 start_pos
= twopass
->stats_in
;
1416 // Load stats for the current frame.
1417 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1419 // Note the error of the frame at the start of the group. This will be
1420 // the GF frame error if we code a normal gf.
1421 gf_first_frame_err
= mod_frame_err
;
1423 // If this is a key frame or the overlay from a previous arf then
1424 // the error score / cost of this frame has already been accounted for.
1425 if (cpi
->common
.frame_type
== KEY_FRAME
|| rc
->source_alt_ref_active
)
1426 gf_group_err
-= gf_first_frame_err
;
1428 // Motion breakout threshold for loop below depends on image size.
1429 mv_ratio_accumulator_thresh
= (cpi
->common
.width
+ cpi
->common
.height
) / 10.0;
1431 // Work out a maximum interval for the GF.
1432 // If the image appears completely static we can extend beyond this.
1433 // The value chosen depends on the active Q range. At low Q we have
1434 // bits to spare and are better with a smaller interval and smaller boost.
1435 // At high Q when there are few bits to spare we are better with a longer
1436 // interval to spread the cost of the GF.
1438 active_max_gf_interval
=
1439 12 + ((int)vp9_convert_qindex_to_q(rc
->last_q
[INTER_FRAME
]) >> 5);
1441 if (active_max_gf_interval
> rc
->max_gf_interval
)
1442 active_max_gf_interval
= rc
->max_gf_interval
;
1445 while (i
< twopass
->static_scene_max_gf_interval
&& i
< rc
->frames_to_key
) {
1448 // Accumulate error score of frames in this gf group.
1449 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1450 gf_group_err
+= mod_frame_err
;
1452 if (EOF
== input_stats(twopass
, &next_frame
))
1455 // Test for the case where there is a brief flash but the prediction
1456 // quality back to an earlier frame is then restored.
1457 flash_detected
= detect_flash(twopass
, 0);
1459 // Update the motion related elements to the boost calculation.
1460 accumulate_frame_motion_stats(&next_frame
,
1461 &this_frame_mv_in_out
, &mv_in_out_accumulator
,
1462 &abs_mv_in_out_accumulator
,
1463 &mv_ratio_accumulator
);
1465 // Accumulate the effect of prediction quality decay.
1466 if (!flash_detected
) {
1467 last_loop_decay_rate
= loop_decay_rate
;
1468 loop_decay_rate
= get_prediction_decay_rate(&cpi
->common
, &next_frame
);
1469 decay_accumulator
= decay_accumulator
* loop_decay_rate
;
1471 // Monitor for static sections.
1472 if ((next_frame
.pcnt_inter
- next_frame
.pcnt_motion
) <
1473 zero_motion_accumulator
) {
1474 zero_motion_accumulator
= next_frame
.pcnt_inter
-
1475 next_frame
.pcnt_motion
;
1478 // Break clause to detect very still sections after motion. For example,
1479 // a static image after a fade or other transition.
1480 if (detect_transition_to_still(cpi
, i
, 5, loop_decay_rate
,
1481 last_loop_decay_rate
)) {
1487 // Calculate a boost number for this frame.
1488 boost_score
+= (decay_accumulator
*
1489 calc_frame_boost(cpi
, &next_frame
, this_frame_mv_in_out
));
1491 // Break out conditions.
1493 // Break at cpi->max_gf_interval unless almost totally static.
1494 (i
>= active_max_gf_interval
&& (zero_motion_accumulator
< 0.995)) ||
1496 // Don't break out with a very short interval.
1497 (i
> MIN_GF_INTERVAL
) &&
1498 ((boost_score
> 125.0) || (next_frame
.pcnt_inter
< 0.75)) &&
1499 (!flash_detected
) &&
1500 ((mv_ratio_accumulator
> mv_ratio_accumulator_thresh
) ||
1501 (abs_mv_in_out_accumulator
> 3.0) ||
1502 (mv_in_out_accumulator
< -2.0) ||
1503 ((boost_score
- old_boost_score
) < IIFACTOR
)))) {
1504 boost_score
= old_boost_score
;
1508 *this_frame
= next_frame
;
1510 old_boost_score
= boost_score
;
1513 twopass
->gf_zeromotion_pct
= (int)(zero_motion_accumulator
* 1000.0);
1515 // Don't allow a gf too near the next kf.
1516 if ((rc
->frames_to_key
- i
) < MIN_GF_INTERVAL
) {
1517 while (i
< (rc
->frames_to_key
+ !rc
->next_key_frame_forced
)) {
1520 if (EOF
== input_stats(twopass
, this_frame
))
1523 if (i
< rc
->frames_to_key
) {
1524 mod_frame_err
= calculate_modified_err(cpi
, this_frame
);
1525 gf_group_err
+= mod_frame_err
;
1530 #if CONFIG_MULTIPLE_ARF
1531 if (cpi
->multi_arf_enabled
) {
1532 // Initialize frame coding order variables.
1533 cpi
->new_frame_coding_order_period
= 0;
1534 cpi
->next_frame_in_order
= 0;
1535 cpi
->arf_buffered
= 0;
1536 vp9_zero(cpi
->frame_coding_order
);
1537 vp9_zero(cpi
->arf_buffer_idx
);
1538 vpx_memset(cpi
->arf_weight
, -1, sizeof(cpi
->arf_weight
));
1542 // Set the interval until the next gf.
1543 if (cpi
->common
.frame_type
== KEY_FRAME
|| rc
->source_alt_ref_active
)
1544 rc
->baseline_gf_interval
= i
- 1;
1546 rc
->baseline_gf_interval
= i
;
1548 // Should we use the alternate reference frame.
1549 if (allow_alt_ref
&&
1550 (i
< cpi
->oxcf
.lag_in_frames
) &&
1551 (i
>= MIN_GF_INTERVAL
) &&
1552 // For real scene cuts (not forced kfs) don't allow arf very near kf.
1553 (rc
->next_key_frame_forced
||
1554 (i
<= (rc
->frames_to_key
- MIN_GF_INTERVAL
)))) {
1555 // Calculate the boost for alt ref.
1556 rc
->gfu_boost
= calc_arf_boost(cpi
, 0, (i
- 1), (i
- 1), &f_boost
,
1558 rc
->source_alt_ref_pending
= 1;
1560 #if CONFIG_MULTIPLE_ARF
1561 // Set the ARF schedule.
1562 if (cpi
->multi_arf_enabled
) {
1563 schedule_frames(cpi
, 0, -(rc
->baseline_gf_interval
- 1), 2, 1, 0);
1567 rc
->gfu_boost
= (int)boost_score
;
1568 rc
->source_alt_ref_pending
= 0;
1569 #if CONFIG_MULTIPLE_ARF
1570 // Set the GF schedule.
1571 if (cpi
->multi_arf_enabled
) {
1572 schedule_frames(cpi
, 0, rc
->baseline_gf_interval
- 1, 2, 0, 0);
1573 assert(cpi
->new_frame_coding_order_period
==
1574 rc
->baseline_gf_interval
);
1579 #if CONFIG_MULTIPLE_ARF
1580 if (cpi
->multi_arf_enabled
&& (cpi
->common
.frame_type
!= KEY_FRAME
)) {
1581 int max_level
= INT_MIN
;
1582 // Replace level indicator of -1 with correct level.
1583 for (i
= 0; i
< cpi
->frame_coding_order_period
; ++i
) {
1584 if (cpi
->arf_weight
[i
] > max_level
) {
1585 max_level
= cpi
->arf_weight
[i
];
1589 for (i
= 0; i
< cpi
->frame_coding_order_period
; ++i
) {
1590 if (cpi
->arf_weight
[i
] == -1) {
1591 cpi
->arf_weight
[i
] = max_level
;
1594 cpi
->max_arf_level
= max_level
;
1597 if (cpi
->multi_arf_enabled
) {
1598 printf("\nSchedule: ");
1599 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1600 printf("%4d ", cpi
->frame_coding_order
[i
]);
1604 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1605 printf("%4d ", cpi
->arf_buffer_idx
[i
]);
1609 for (i
= 0; i
< cpi
->new_frame_coding_order_period
; ++i
) {
1610 printf("%4d ", cpi
->arf_weight
[i
]);
1617 // Calculate the bits to be allocated to the group as a whole.
1618 if (twopass
->kf_group_bits
> 0 && twopass
->kf_group_error_left
> 0) {
1619 twopass
->gf_group_bits
= (int64_t)(cpi
->twopass
.kf_group_bits
*
1620 (gf_group_err
/ cpi
->twopass
.kf_group_error_left
));
1622 twopass
->gf_group_bits
= 0;
1624 twopass
->gf_group_bits
= (twopass
->gf_group_bits
< 0) ?
1625 0 : (twopass
->gf_group_bits
> twopass
->kf_group_bits
) ?
1626 twopass
->kf_group_bits
: twopass
->gf_group_bits
;
1628 // Clip cpi->twopass.gf_group_bits based on user supplied data rate
1629 // variability limit, cpi->oxcf.two_pass_vbrmax_section.
1630 if (twopass
->gf_group_bits
> (int64_t)max_bits
* rc
->baseline_gf_interval
)
1631 twopass
->gf_group_bits
= (int64_t)max_bits
* rc
->baseline_gf_interval
;
1633 // Reset the file position.
1634 reset_fpf_position(twopass
, start_pos
);
1636 // Assign bits to the arf or gf.
1637 for (i
= 0; i
<= (rc
->source_alt_ref_pending
&&
1638 cpi
->common
.frame_type
!= KEY_FRAME
); ++i
) {
1639 int allocation_chunks
;
1640 int q
= rc
->last_q
[INTER_FRAME
];
1643 int boost
= (rc
->gfu_boost
* gfboost_qadjust(q
)) / 100;
1645 // Set max and minimum boost and hence minimum allocation.
1646 boost
= clamp(boost
, 125, (rc
->baseline_gf_interval
+ 1) * 200);
1648 if (rc
->source_alt_ref_pending
&& i
== 0)
1649 allocation_chunks
= ((rc
->baseline_gf_interval
+ 1) * 100) + boost
;
1651 allocation_chunks
= (rc
->baseline_gf_interval
* 100) + (boost
- 100);
1653 // Prevent overflow.
1655 int divisor
= boost
>> 10;
1657 allocation_chunks
/= divisor
;
1660 // Calculate the number of bits to be spent on the gf or arf based on
1661 // the boost number.
1662 gf_bits
= (int)((double)boost
* (twopass
->gf_group_bits
/
1663 (double)allocation_chunks
));
1665 // If the frame that is to be boosted is simpler than the average for
1666 // the gf/arf group then use an alternative calculation
1667 // based on the error score of the frame itself.
1668 if (rc
->baseline_gf_interval
< 1 ||
1669 mod_frame_err
< gf_group_err
/ (double)rc
->baseline_gf_interval
) {
1670 double alt_gf_grp_bits
= (double)twopass
->kf_group_bits
*
1671 (mod_frame_err
* (double)rc
->baseline_gf_interval
) /
1672 DOUBLE_DIVIDE_CHECK(twopass
->kf_group_error_left
);
1674 int alt_gf_bits
= (int)((double)boost
* (alt_gf_grp_bits
/
1675 (double)allocation_chunks
));
1677 if (gf_bits
> alt_gf_bits
)
1678 gf_bits
= alt_gf_bits
;
1680 // If it is harder than other frames in the group make sure it at
1681 // least receives an allocation in keeping with its relative error
1682 // score, otherwise it may be worse off than an "un-boosted" frame.
1683 int alt_gf_bits
= (int)((double)twopass
->kf_group_bits
*
1685 DOUBLE_DIVIDE_CHECK(twopass
->kf_group_error_left
));
1687 if (alt_gf_bits
> gf_bits
)
1688 gf_bits
= alt_gf_bits
;
1691 // Don't allow a negative value for gf_bits.
1696 twopass
->gf_bits
= gf_bits
;
1699 (!rc
->source_alt_ref_pending
&&
1700 cpi
->common
.frame_type
!= KEY_FRAME
)) {
1701 // Calculate the per frame bit target for this frame.
1702 vp9_rc_set_frame_target(cpi
, gf_bits
);
1707 // Adjust KF group bits and error remaining.
1708 twopass
->kf_group_error_left
-= (int64_t)gf_group_err
;
1709 twopass
->kf_group_bits
-= twopass
->gf_group_bits
;
1711 if (twopass
->kf_group_bits
< 0)
1712 twopass
->kf_group_bits
= 0;
1714 // If this is an arf update we want to remove the score for the overlay
1715 // frame at the end which will usually be very cheap to code.
1716 // The overlay frame has already, in effect, been coded so we want to spread
1717 // the remaining bits among the other frames.
1718 // For normal GFs remove the score for the GF itself unless this is
1719 // also a key frame in which case it has already been accounted for.
1720 if (rc
->source_alt_ref_pending
) {
1721 twopass
->gf_group_error_left
= (int64_t)(gf_group_err
- mod_frame_err
);
1722 } else if (cpi
->common
.frame_type
!= KEY_FRAME
) {
1723 twopass
->gf_group_error_left
= (int64_t)(gf_group_err
1724 - gf_first_frame_err
);
1726 twopass
->gf_group_error_left
= (int64_t)gf_group_err
;
1729 twopass
->gf_group_bits
-= twopass
->gf_bits
;
1731 if (twopass
->gf_group_bits
< 0)
1732 twopass
->gf_group_bits
= 0;
1734 // This condition could fail if there are two kfs very close together
1735 // despite MIN_GF_INTERVAL and would cause a divide by 0 in the
1736 // calculation of alt_extra_bits.
1737 if (rc
->baseline_gf_interval
>= 3) {
1738 const int boost
= rc
->source_alt_ref_pending
? b_boost
: rc
->gfu_boost
;
1741 const int pct_extra
= MIN(20, (boost
- 100) / 50);
1742 const int alt_extra_bits
= (int)((twopass
->gf_group_bits
* pct_extra
) /
1744 twopass
->gf_group_bits
-= alt_extra_bits
;
1749 if (cpi
->common
.frame_type
!= KEY_FRAME
) {
1750 FIRSTPASS_STATS sectionstats
;
1752 zero_stats(§ionstats
);
1753 reset_fpf_position(twopass
, start_pos
);
1755 for (i
= 0; i
< rc
->baseline_gf_interval
; ++i
) {
1756 input_stats(twopass
, &next_frame
);
1757 accumulate_stats(§ionstats
, &next_frame
);
1760 avg_stats(§ionstats
);
1762 twopass
->section_intra_rating
= (int)
1763 (sectionstats
.intra_error
/
1764 DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
));
1766 reset_fpf_position(twopass
, start_pos
);
1770 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1771 static void assign_std_frame_bits(VP9_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
) {
1772 int target_frame_size
;
1773 double modified_err
;
1774 double err_fraction
;
1775 const int max_bits
= frame_max_bits(cpi
); // Max for a single frame.
1777 // Calculate modified prediction error used in bit allocation.
1778 modified_err
= calculate_modified_err(cpi
, this_frame
);
1780 if (cpi
->twopass
.gf_group_error_left
> 0)
1781 // What portion of the remaining GF group error is used by this frame.
1782 err_fraction
= modified_err
/ cpi
->twopass
.gf_group_error_left
;
1786 // How many of those bits available for allocation should we give it?
1787 target_frame_size
= (int)((double)cpi
->twopass
.gf_group_bits
* err_fraction
);
1789 // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at
1791 target_frame_size
= clamp(target_frame_size
, 0,
1792 MIN(max_bits
, (int)cpi
->twopass
.gf_group_bits
));
1794 // Adjust error and bits remaining.
1795 cpi
->twopass
.gf_group_error_left
-= (int64_t)modified_err
;
1796 cpi
->twopass
.gf_group_bits
-= target_frame_size
;
1798 if (cpi
->twopass
.gf_group_bits
< 0)
1799 cpi
->twopass
.gf_group_bits
= 0;
1801 // Per frame bit target for this frame.
1802 vp9_rc_set_frame_target(cpi
, target_frame_size
);
1805 static int test_candidate_kf(VP9_COMP
*cpi
,
1806 const FIRSTPASS_STATS
*last_frame
,
1807 const FIRSTPASS_STATS
*this_frame
,
1808 const FIRSTPASS_STATS
*next_frame
) {
1809 int is_viable_kf
= 0;
1811 // Does the frame satisfy the primary criteria of a key frame?
1812 // If so, then examine how well it predicts subsequent frames.
1813 if ((this_frame
->pcnt_second_ref
< 0.10) &&
1814 (next_frame
->pcnt_second_ref
< 0.10) &&
1815 ((this_frame
->pcnt_inter
< 0.05) ||
1816 (((this_frame
->pcnt_inter
- this_frame
->pcnt_neutral
) < 0.35) &&
1817 ((this_frame
->intra_error
/
1818 DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
)) < 2.5) &&
1819 ((fabs(last_frame
->coded_error
- this_frame
->coded_error
) /
1820 DOUBLE_DIVIDE_CHECK(this_frame
->coded_error
) > 0.40) ||
1821 (fabs(last_frame
->intra_error
- this_frame
->intra_error
) /
1822 DOUBLE_DIVIDE_CHECK(this_frame
->intra_error
) > 0.40) ||
1823 ((next_frame
->intra_error
/
1824 DOUBLE_DIVIDE_CHECK(next_frame
->coded_error
)) > 3.5))))) {
1826 FIRSTPASS_STATS
*start_pos
;
1828 FIRSTPASS_STATS local_next_frame
;
1830 double boost_score
= 0.0;
1831 double old_boost_score
= 0.0;
1832 double decay_accumulator
= 1.0;
1834 local_next_frame
= *next_frame
;
1836 // Note the starting file position so we can reset to it.
1837 start_pos
= cpi
->twopass
.stats_in
;
1839 // Examine how well the key frame predicts subsequent frames.
1840 for (i
= 0; i
< 16; ++i
) {
1841 double next_iiratio
= (IIKFACTOR1
* local_next_frame
.intra_error
/
1842 DOUBLE_DIVIDE_CHECK(local_next_frame
.coded_error
));
1844 if (next_iiratio
> RMAX
)
1845 next_iiratio
= RMAX
;
1847 // Cumulative effect of decay in prediction quality.
1848 if (local_next_frame
.pcnt_inter
> 0.85)
1849 decay_accumulator
*= local_next_frame
.pcnt_inter
;
1851 decay_accumulator
*= (0.85 + local_next_frame
.pcnt_inter
) / 2.0;
1853 // Keep a running total.
1854 boost_score
+= (decay_accumulator
* next_iiratio
);
1856 // Test various breakout clauses.
1857 if ((local_next_frame
.pcnt_inter
< 0.05) ||
1858 (next_iiratio
< 1.5) ||
1859 (((local_next_frame
.pcnt_inter
-
1860 local_next_frame
.pcnt_neutral
) < 0.20) &&
1861 (next_iiratio
< 3.0)) ||
1862 ((boost_score
- old_boost_score
) < 3.0) ||
1863 (local_next_frame
.intra_error
< 200)) {
1867 old_boost_score
= boost_score
;
1869 // Get the next frame details
1870 if (EOF
== input_stats(&cpi
->twopass
, &local_next_frame
))
1874 // If there is tolerable prediction for at least the next 3 frames then
1875 // break out else discard this potential key frame and move on
1876 if (boost_score
> 30.0 && (i
> 3)) {
1879 // Reset the file position
1880 reset_fpf_position(&cpi
->twopass
, start_pos
);
1886 return is_viable_kf
;
1889 static void find_next_key_frame(VP9_COMP
*cpi
, FIRSTPASS_STATS
*this_frame
) {
1891 FIRSTPASS_STATS last_frame
;
1892 FIRSTPASS_STATS first_frame
;
1893 FIRSTPASS_STATS next_frame
;
1894 FIRSTPASS_STATS
*start_position
;
1896 double decay_accumulator
= 1.0;
1897 double zero_motion_accumulator
= 1.0;
1898 double boost_score
= 0;
1899 double loop_decay_rate
;
1901 double kf_mod_err
= 0.0;
1902 double kf_group_err
= 0.0;
1903 double recent_loop_decay
[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
1905 RATE_CONTROL
*const rc
= &cpi
->rc
;
1906 struct twopass_rc
*const twopass
= &cpi
->twopass
;
1908 vp9_zero(next_frame
);
1910 vp9_clear_system_state(); // __asm emms;
1912 start_position
= twopass
->stats_in
;
1913 cpi
->common
.frame_type
= KEY_FRAME
;
1915 // Is this a forced key frame by interval.
1916 rc
->this_key_frame_forced
= rc
->next_key_frame_forced
;
1918 // Clear the alt ref active flag as this can never be active on a key frame.
1919 rc
->source_alt_ref_active
= 0;
1921 // KF is always a GF so clear frames till next gf counter.
1922 rc
->frames_till_gf_update_due
= 0;
1924 rc
->frames_to_key
= 1;
1926 // Take a copy of the initial frame details.
1927 first_frame
= *this_frame
;
1929 twopass
->kf_group_bits
= 0; // Total bits available to kf group
1930 twopass
->kf_group_error_left
= 0; // Group modified error score.
1932 kf_mod_err
= calculate_modified_err(cpi
, this_frame
);
1934 // Find the next keyframe.
1936 while (twopass
->stats_in
< twopass
->stats_in_end
) {
1937 // Accumulate kf group error.
1938 kf_group_err
+= calculate_modified_err(cpi
, this_frame
);
1940 // Load the next frame's stats.
1941 last_frame
= *this_frame
;
1942 input_stats(twopass
, this_frame
);
1944 // Provided that we are not at the end of the file...
1945 if (cpi
->oxcf
.auto_key
&&
1946 lookup_next_frame_stats(twopass
, &next_frame
) != EOF
) {
1947 // Check for a scene cut.
1948 if (test_candidate_kf(cpi
, &last_frame
, this_frame
, &next_frame
))
1951 // How fast is the prediction quality decaying?
1952 loop_decay_rate
= get_prediction_decay_rate(&cpi
->common
, &next_frame
);
1954 // We want to know something about the recent past... rather than
1955 // as used elsewhere where we are concerned with decay in prediction
1956 // quality since the last GF or KF.
1957 recent_loop_decay
[i
% 8] = loop_decay_rate
;
1958 decay_accumulator
= 1.0;
1959 for (j
= 0; j
< 8; ++j
)
1960 decay_accumulator
*= recent_loop_decay
[j
];
1962 // Special check for transition or high motion followed by a
1964 if (detect_transition_to_still(cpi
, i
, cpi
->key_frame_frequency
- i
,
1965 loop_decay_rate
, decay_accumulator
))
1968 // Step on to the next frame.
1969 ++rc
->frames_to_key
;
1971 // If we don't have a real key frame within the next two
1972 // key_frame_frequency intervals then break out of the loop.
1973 if (rc
->frames_to_key
>= 2 * (int)cpi
->key_frame_frequency
)
1976 ++rc
->frames_to_key
;
1981 // If there is a max kf interval set by the user we must obey it.
1982 // We already breakout of the loop above at 2x max.
1983 // This code centers the extra kf if the actual natural interval
1984 // is between 1x and 2x.
1985 if (cpi
->oxcf
.auto_key
&&
1986 rc
->frames_to_key
> (int)cpi
->key_frame_frequency
) {
1987 FIRSTPASS_STATS tmp_frame
;
1989 rc
->frames_to_key
/= 2;
1991 // Copy first frame details.
1992 tmp_frame
= first_frame
;
1994 // Reset to the start of the group.
1995 reset_fpf_position(twopass
, start_position
);
1999 // Rescan to get the correct error data for the forced kf group.
2000 for (i
= 0; i
< rc
->frames_to_key
; ++i
) {
2001 // Accumulate kf group errors.
2002 kf_group_err
+= calculate_modified_err(cpi
, &tmp_frame
);
2004 // Load the next frame's stats.
2005 input_stats(twopass
, &tmp_frame
);
2007 rc
->next_key_frame_forced
= 1;
2008 } else if (twopass
->stats_in
== twopass
->stats_in_end
) {
2009 rc
->next_key_frame_forced
= 1;
2011 rc
->next_key_frame_forced
= 0;
2014 // Special case for the last key frame of the file.
2015 if (twopass
->stats_in
>= twopass
->stats_in_end
) {
2016 // Accumulate kf group error.
2017 kf_group_err
+= calculate_modified_err(cpi
, this_frame
);
2020 // Calculate the number of bits that should be assigned to the kf group.
2021 if (twopass
->bits_left
> 0 && twopass
->modified_error_left
> 0.0) {
2022 // Maximum number of bits for a single normal frame (not key frame).
2023 int max_bits
= frame_max_bits(cpi
);
2025 // Maximum number of bits allocated to the key frame group.
2026 int64_t max_grp_bits
;
2028 // Default allocation based on bits left and relative
2029 // complexity of the section.
2030 twopass
->kf_group_bits
= (int64_t)(twopass
->bits_left
*
2031 (kf_group_err
/ twopass
->modified_error_left
));
2033 // Clip based on maximum per frame rate defined by the user.
2034 max_grp_bits
= (int64_t)max_bits
* (int64_t)rc
->frames_to_key
;
2035 if (twopass
->kf_group_bits
> max_grp_bits
)
2036 twopass
->kf_group_bits
= max_grp_bits
;
2038 twopass
->kf_group_bits
= 0;
2040 // Reset the first pass file position.
2041 reset_fpf_position(twopass
, start_position
);
2043 // Determine how big to make this keyframe based on how well the subsequent
2044 // frames use inter blocks.
2045 decay_accumulator
= 1.0;
2048 // Scan through the kf group collating various stats.
2049 for (i
= 0; i
< rc
->frames_to_key
; ++i
) {
2052 if (EOF
== input_stats(twopass
, &next_frame
))
2055 // Monitor for static sections.
2056 if ((next_frame
.pcnt_inter
- next_frame
.pcnt_motion
) <
2057 zero_motion_accumulator
) {
2058 zero_motion_accumulator
=
2059 (next_frame
.pcnt_inter
- next_frame
.pcnt_motion
);
2062 // For the first few frames collect data to decide kf boost.
2063 if (i
<= (rc
->max_gf_interval
* 2)) {
2064 if (next_frame
.intra_error
> twopass
->kf_intra_err_min
)
2065 r
= (IIKFACTOR2
* next_frame
.intra_error
/
2066 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
));
2068 r
= (IIKFACTOR2
* twopass
->kf_intra_err_min
/
2069 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
));
2074 // How fast is prediction quality decaying.
2075 if (!detect_flash(twopass
, 0)) {
2076 loop_decay_rate
= get_prediction_decay_rate(&cpi
->common
, &next_frame
);
2077 decay_accumulator
*= loop_decay_rate
;
2078 decay_accumulator
= decay_accumulator
< MIN_DECAY_FACTOR
2079 ? MIN_DECAY_FACTOR
: decay_accumulator
;
2082 boost_score
+= (decay_accumulator
* r
);
2087 FIRSTPASS_STATS sectionstats
;
2089 zero_stats(§ionstats
);
2090 reset_fpf_position(twopass
, start_position
);
2092 for (i
= 0; i
< rc
->frames_to_key
; ++i
) {
2093 input_stats(twopass
, &next_frame
);
2094 accumulate_stats(§ionstats
, &next_frame
);
2097 avg_stats(§ionstats
);
2099 twopass
->section_intra_rating
= (int) (sectionstats
.intra_error
/
2100 DOUBLE_DIVIDE_CHECK(sectionstats
.coded_error
));
2103 // Reset the first pass file position.
2104 reset_fpf_position(twopass
, start_position
);
2106 // Work out how many bits to allocate for the key frame itself.
2108 int kf_boost
= (int)boost_score
;
2109 int allocation_chunks
;
2112 if (kf_boost
< (rc
->frames_to_key
* 3))
2113 kf_boost
= (rc
->frames_to_key
* 3);
2115 if (kf_boost
< MIN_KF_BOOST
)
2116 kf_boost
= MIN_KF_BOOST
;
2118 // Make a note of baseline boost and the zero motion
2119 // accumulator value for use elsewhere.
2120 rc
->kf_boost
= kf_boost
;
2121 twopass
->kf_zeromotion_pct
= (int)(zero_motion_accumulator
* 100.0);
2123 // Key frame size depends on:
2124 // (1) the error score for the whole key frame group,
2125 // (2) the key frames' own error if this is smaller than the
2126 // average for the group (optional),
2127 // (3) insuring that the frame receives at least the allocation it would
2128 // have received based on its own error score vs the error score
2131 // If the sequence appears almost totally static we want to spend almost
2132 // all of the bits on the key frame.
2134 // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is
2135 // taken care of by kf_boost.
2136 if (zero_motion_accumulator
>= 0.99) {
2137 allocation_chunks
= ((rc
->frames_to_key
- 1) * 10) + kf_boost
;
2139 allocation_chunks
= ((rc
->frames_to_key
- 1) * 100) + kf_boost
;
2142 // Prevent overflow.
2143 if (kf_boost
> 1028) {
2144 int divisor
= kf_boost
>> 10;
2145 kf_boost
/= divisor
;
2146 allocation_chunks
/= divisor
;
2149 twopass
->kf_group_bits
= (twopass
->kf_group_bits
< 0) ? 0
2150 : twopass
->kf_group_bits
;
2152 // Calculate the number of bits to be spent on the key frame.
2153 twopass
->kf_bits
= (int)((double)kf_boost
*
2154 ((double)twopass
->kf_group_bits
/ allocation_chunks
));
2156 // If the key frame is actually easier than the average for the
2157 // kf group (which does sometimes happen, e.g. a blank intro frame)
2158 // then use an alternate calculation based on the kf error score
2159 // which should give a smaller key frame.
2160 if (kf_mod_err
< kf_group_err
/ rc
->frames_to_key
) {
2161 double alt_kf_grp_bits
= ((double)twopass
->bits_left
*
2162 (kf_mod_err
* (double)rc
->frames_to_key
) /
2163 DOUBLE_DIVIDE_CHECK(twopass
->modified_error_left
));
2165 alt_kf_bits
= (int)((double)kf_boost
*
2166 (alt_kf_grp_bits
/ (double)allocation_chunks
));
2168 if (twopass
->kf_bits
> alt_kf_bits
)
2169 twopass
->kf_bits
= alt_kf_bits
;
2171 // Else if it is much harder than other frames in the group make sure
2172 // it at least receives an allocation in keeping with its relative
2174 alt_kf_bits
= (int)((double)twopass
->bits_left
* (kf_mod_err
/
2175 DOUBLE_DIVIDE_CHECK(twopass
->modified_error_left
)));
2177 if (alt_kf_bits
> twopass
->kf_bits
) {
2178 twopass
->kf_bits
= alt_kf_bits
;
2181 twopass
->kf_group_bits
-= twopass
->kf_bits
;
2182 // Per frame bit target for this frame.
2183 vp9_rc_set_frame_target(cpi
, twopass
->kf_bits
);
2186 // Note the total error score of the kf group minus the key frame itself.
2187 twopass
->kf_group_error_left
= (int)(kf_group_err
- kf_mod_err
);
2189 // Adjust the count of total modified error left.
2190 // The count of bits left is adjusted elsewhere based on real coded frame
2192 twopass
->modified_error_left
-= kf_group_err
;
2195 void vp9_rc_get_first_pass_params(VP9_COMP
*cpi
) {
2196 VP9_COMMON
*const cm
= &cpi
->common
;
2197 if (!cpi
->refresh_alt_ref_frame
&&
2198 (cm
->current_video_frame
== 0 ||
2199 cm
->frame_flags
& FRAMEFLAGS_KEY
)) {
2200 cm
->frame_type
= KEY_FRAME
;
2202 cm
->frame_type
= INTER_FRAME
;
2204 // Do not use periodic key frames.
2205 cpi
->rc
.frames_to_key
= INT_MAX
;
2208 void vp9_rc_get_second_pass_params(VP9_COMP
*cpi
) {
2209 VP9_COMMON
*const cm
= &cpi
->common
;
2210 RATE_CONTROL
*const rc
= &cpi
->rc
;
2211 struct twopass_rc
*const twopass
= &cpi
->twopass
;
2212 const int frames_left
= (int)(twopass
->total_stats
.count
-
2213 cm
->current_video_frame
);
2214 FIRSTPASS_STATS this_frame
;
2215 FIRSTPASS_STATS this_frame_copy
;
2217 double this_frame_intra_error
;
2218 double this_frame_coded_error
;
2221 if (!twopass
->stats_in
)
2224 if (cpi
->refresh_alt_ref_frame
) {
2225 cm
->frame_type
= INTER_FRAME
;
2226 vp9_rc_set_frame_target(cpi
, twopass
->gf_bits
);
2230 vp9_clear_system_state();
2232 if (cpi
->oxcf
.end_usage
== USAGE_CONSTANT_QUALITY
) {
2233 twopass
->active_worst_quality
= cpi
->oxcf
.cq_level
;
2234 } else if (cm
->current_video_frame
== 0) {
2235 // Special case code for first frame.
2236 const int section_target_bandwidth
= (int)(twopass
->bits_left
/
2238 const int tmp_q
= vp9_twopass_worst_quality(cpi
, &twopass
->total_left_stats
,
2239 section_target_bandwidth
);
2240 twopass
->active_worst_quality
= tmp_q
;
2241 rc
->ni_av_qi
= tmp_q
;
2242 rc
->avg_q
= vp9_convert_qindex_to_q(tmp_q
);
2244 vp9_zero(this_frame
);
2245 if (EOF
== input_stats(twopass
, &this_frame
))
2248 this_frame_intra_error
= this_frame
.intra_error
;
2249 this_frame_coded_error
= this_frame
.coded_error
;
2251 // Keyframe and section processing.
2252 if (rc
->frames_to_key
== 0 ||
2253 (cm
->frame_flags
& FRAMEFLAGS_KEY
)) {
2254 // Define next KF group and assign bits to it.
2255 this_frame_copy
= this_frame
;
2256 find_next_key_frame(cpi
, &this_frame_copy
);
2258 cm
->frame_type
= INTER_FRAME
;
2261 // Is this frame a GF / ARF? (Note: a key frame is always also a GF).
2262 if (rc
->frames_till_gf_update_due
== 0) {
2263 // Define next gf group and assign bits to it.
2264 this_frame_copy
= this_frame
;
2266 #if CONFIG_MULTIPLE_ARF
2267 if (cpi
->multi_arf_enabled
) {
2268 define_fixed_arf_period(cpi
);
2271 define_gf_group(cpi
, &this_frame_copy
);
2272 #if CONFIG_MULTIPLE_ARF
2276 if (twopass
->gf_zeromotion_pct
> 995) {
2277 // As long as max_thresh for encode breakout is small enough, it is ok
2278 // to enable it for show frame, i.e. set allow_encode_breakout to
2279 // ENCODE_BREAKOUT_LIMITED.
2280 if (!cm
->show_frame
)
2281 cpi
->allow_encode_breakout
= ENCODE_BREAKOUT_DISABLED
;
2283 cpi
->allow_encode_breakout
= ENCODE_BREAKOUT_LIMITED
;
2286 rc
->frames_till_gf_update_due
= rc
->baseline_gf_interval
;
2287 cpi
->refresh_golden_frame
= 1;
2289 // Otherwise this is an ordinary frame.
2290 // Assign bits from those allocated to the GF group.
2291 this_frame_copy
= this_frame
;
2292 assign_std_frame_bits(cpi
, &this_frame_copy
);
2295 // Keep a globally available copy of this and the next frame's iiratio.
2296 twopass
->this_iiratio
= (int)(this_frame_intra_error
/
2297 DOUBLE_DIVIDE_CHECK(this_frame_coded_error
));
2299 FIRSTPASS_STATS next_frame
;
2300 if (lookup_next_frame_stats(twopass
, &next_frame
) != EOF
) {
2301 twopass
->next_iiratio
= (int)(next_frame
.intra_error
/
2302 DOUBLE_DIVIDE_CHECK(next_frame
.coded_error
));
2306 if (cpi
->common
.frame_type
== KEY_FRAME
)
2307 target
= vp9_rc_clamp_iframe_target_size(cpi
, rc
->this_frame_target
);
2309 target
= vp9_rc_clamp_pframe_target_size(cpi
, rc
->this_frame_target
);
2310 vp9_rc_set_frame_target(cpi
, target
);
2312 // Update the total stats remaining structure.
2313 subtract_stats(&twopass
->total_left_stats
, &this_frame
);
2316 void vp9_twopass_postencode_update(VP9_COMP
*cpi
, uint64_t bytes_used
) {
2317 #ifdef DISABLE_RC_LONG_TERM_MEM
2318 cpi
->twopass
.bits_left
-= cpi
->rc
.this_frame_target
;
2320 cpi
->twopass
.bits_left
-= 8 * bytes_used
;
2321 // Update bits left to the kf and gf groups to account for overshoot or
2322 // undershoot on these frames.
2323 if (cm
->frame_type
== KEY_FRAME
) {
2324 cpi
->twopass
.kf_group_bits
+= cpi
->rc
.this_frame_target
-
2325 cpi
->rc
.projected_frame_size
;
2327 cpi
->twopass
.kf_group_bits
= MAX(cpi
->twopass
.kf_group_bits
, 0);
2328 } else if (cpi
->refresh_golden_frame
|| cpi
->refresh_alt_ref_frame
) {
2329 cpi
->twopass
.gf_group_bits
+= cpi
->rc
.this_frame_target
-
2330 cpi
->rc
.projected_frame_size
;
2332 cpi
->twopass
.gf_group_bits
= MAX(cpi
->twopass
.gf_group_bits
, 0);