OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "math.h" | 11 #include <math.h> |
12 #include "limits.h" | 12 #include <limits.h> |
| 13 #include <stdio.h> |
13 #include "vp9/encoder/vp9_block.h" | 14 #include "vp9/encoder/vp9_block.h" |
14 #include "vp9/encoder/vp9_onyx_int.h" | 15 #include "vp9/encoder/vp9_onyx_int.h" |
15 #include "vp9/encoder/vp9_variance.h" | 16 #include "vp9/encoder/vp9_variance.h" |
16 #include "vp9/encoder/vp9_encodeintra.h" | 17 #include "vp9/encoder/vp9_encodeintra.h" |
17 #include "vp9/encoder/vp9_mcomp.h" | 18 #include "vp9/encoder/vp9_mcomp.h" |
18 #include "vp9/encoder/vp9_firstpass.h" | 19 #include "vp9/encoder/vp9_firstpass.h" |
19 #include "vpx_scale/vpx_scale.h" | 20 #include "vpx_scale/vpx_scale.h" |
20 #include "vp9/encoder/vp9_encodeframe.h" | 21 #include "vp9/encoder/vp9_encodeframe.h" |
21 #include "vp9/encoder/vp9_encodemb.h" | 22 #include "vp9/encoder/vp9_encodemb.h" |
22 #include "vp9/common/vp9_extend.h" | 23 #include "vp9/common/vp9_extend.h" |
23 #include "vp9/common/vp9_systemdependent.h" | 24 #include "vp9/common/vp9_systemdependent.h" |
24 #include "vpx_mem/vpx_mem.h" | 25 #include "vpx_mem/vpx_mem.h" |
25 #include "vpx_scale/yv12config.h" | 26 #include "vpx_scale/yv12config.h" |
26 #include <stdio.h> | |
27 #include "vp9/encoder/vp9_quantize.h" | 27 #include "vp9/encoder/vp9_quantize.h" |
28 #include "vp9/encoder/vp9_rdopt.h" | 28 #include "vp9/encoder/vp9_rdopt.h" |
29 #include "vp9/encoder/vp9_ratectrl.h" | 29 #include "vp9/encoder/vp9_ratectrl.h" |
30 #include "vp9/common/vp9_quant_common.h" | 30 #include "vp9/common/vp9_quant_common.h" |
31 #include "vp9/common/vp9_entropymv.h" | 31 #include "vp9/common/vp9_entropymv.h" |
32 #include "vp9/encoder/vp9_encodemv.h" | 32 #include "vp9/encoder/vp9_encodemv.h" |
| 33 #include "vp9/encoder/vp9_vaq.h" |
33 #include "./vpx_scale_rtcd.h" | 34 #include "./vpx_scale_rtcd.h" |
34 // TODO(jkoleszar): for setup_dst_planes | 35 // TODO(jkoleszar): for setup_dst_planes |
35 #include "vp9/common/vp9_reconinter.h" | 36 #include "vp9/common/vp9_reconinter.h" |
36 | 37 |
37 #define OUTPUT_FPF 0 | 38 #define OUTPUT_FPF 0 |
38 | 39 |
39 #define IIFACTOR 12.5 | 40 #define IIFACTOR 12.5 |
40 #define IIKFACTOR1 12.5 | 41 #define IIKFACTOR1 12.5 |
41 #define IIKFACTOR2 15.0 | 42 #define IIKFACTOR2 15.0 |
42 #define RMAX 512.0 | 43 #define RMAX 512.0 |
(...skipping 27 matching lines...) Expand all Loading... |
70 if (target_q <= vp9_convert_qindex_to_q(i)) { | 71 if (target_q <= vp9_convert_qindex_to_q(i)) { |
71 ret_val = i; | 72 ret_val = i; |
72 break; | 73 break; |
73 } | 74 } |
74 } | 75 } |
75 | 76 |
76 return ret_val; | 77 return ret_val; |
77 } | 78 } |
78 | 79 |
79 | 80 |
80 // Resets the first pass file to the given position using a relative seek from t
he current position | 81 // Resets the first pass file to the given position using a relative seek from |
| 82 // the current position. |
81 static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) { | 83 static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) { |
82 cpi->twopass.stats_in = position; | 84 cpi->twopass.stats_in = position; |
83 } | 85 } |
84 | 86 |
85 static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) { | 87 static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) { |
86 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) | 88 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) |
87 return EOF; | 89 return EOF; |
88 | 90 |
89 *next_frame = *cpi->twopass.stats_in; | 91 *next_frame = *cpi->twopass.stats_in; |
90 return 1; | 92 return 1; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 section->MVr /= section->count; | 245 section->MVr /= section->count; |
244 section->mvr_abs /= section->count; | 246 section->mvr_abs /= section->count; |
245 section->MVc /= section->count; | 247 section->MVc /= section->count; |
246 section->mvc_abs /= section->count; | 248 section->mvc_abs /= section->count; |
247 section->MVrv /= section->count; | 249 section->MVrv /= section->count; |
248 section->MVcv /= section->count; | 250 section->MVcv /= section->count; |
249 section->mv_in_out_count /= section->count; | 251 section->mv_in_out_count /= section->count; |
250 section->duration /= section->count; | 252 section->duration /= section->count; |
251 } | 253 } |
252 | 254 |
253 // Calculate a modified Error used in distributing bits between easier and harde
r frames | 255 // Calculate a modified Error used in distributing bits between easier and |
254 static double calculate_modified_err(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame)
{ | 256 // harder frames. |
| 257 static double calculate_modified_err(VP9_COMP *cpi, |
| 258 FIRSTPASS_STATS *this_frame) { |
255 const FIRSTPASS_STATS *const stats = &cpi->twopass.total_stats; | 259 const FIRSTPASS_STATS *const stats = &cpi->twopass.total_stats; |
256 const double av_err = stats->ssim_weighted_pred_err / stats->count; | 260 const double av_err = stats->ssim_weighted_pred_err / stats->count; |
257 const double this_err = this_frame->ssim_weighted_pred_err; | 261 const double this_err = this_frame->ssim_weighted_pred_err; |
258 return av_err * pow(this_err / DOUBLE_DIVIDE_CHECK(av_err), | 262 return av_err * pow(this_err / DOUBLE_DIVIDE_CHECK(av_err), |
259 this_err > av_err ? POW1 : POW2); | 263 this_err > av_err ? POW1 : POW2); |
260 } | 264 } |
261 | 265 |
262 static const double weight_table[256] = { | 266 static const double weight_table[256] = { |
263 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000
, | 267 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
264 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000
, | 268 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
265 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000
, | 269 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
266 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000
, | 270 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
267 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750
, | 271 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500, |
268 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750
, | 272 0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250, |
269 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750
, | 273 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000, |
270 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750
, | 274 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750, |
271 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 275 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, |
272 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 276 0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
273 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 277 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
274 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 278 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
275 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 279 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
276 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 280 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
277 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 281 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
278 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 282 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
279 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 283 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
280 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 284 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
281 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 285 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
282 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 286 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
283 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 287 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
284 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 288 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
285 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 289 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
286 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 290 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
287 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
, | 291 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, |
288 1.000000, 1.000000, 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, |
289 1.000000, 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, |
290 1.000000, 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, |
291 1.000000, 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, |
292 1.000000, 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, |
293 1.000000, 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, |
294 1.000000, 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 |
295 }; | 304 }; |
296 | 305 |
297 static double simple_weight(YV12_BUFFER_CONFIG *source) { | 306 static double simple_weight(YV12_BUFFER_CONFIG *source) { |
298 int i, j; | 307 int i, j; |
299 | 308 |
300 uint8_t *src = source->y_buffer; | 309 uint8_t *src = source->y_buffer; |
301 double sum_weights = 0.0; | 310 double sum_weights = 0.0; |
302 | 311 |
303 // Loop throught the Y plane raw examining levels and creating a weight for th
e image | 312 // Loop through the Y plane examining levels and creating a weight for |
| 313 // the image. |
304 i = source->y_height; | 314 i = source->y_height; |
305 do { | 315 do { |
306 j = source->y_width; | 316 j = source->y_width; |
307 do { | 317 do { |
308 sum_weights += weight_table[ *src]; | 318 sum_weights += weight_table[ *src]; |
309 src++; | 319 src++; |
310 } while (--j); | 320 } while (--j); |
311 src -= source->y_width; | 321 src -= source->y_width; |
312 src += source->y_stride; | 322 src += source->y_stride; |
313 } while (--i); | 323 } while (--i); |
(...skipping 19 matching lines...) Expand all Loading... |
333 } | 343 } |
334 | 344 |
335 void vp9_init_first_pass(VP9_COMP *cpi) { | 345 void vp9_init_first_pass(VP9_COMP *cpi) { |
336 zero_stats(&cpi->twopass.total_stats); | 346 zero_stats(&cpi->twopass.total_stats); |
337 } | 347 } |
338 | 348 |
339 void vp9_end_first_pass(VP9_COMP *cpi) { | 349 void vp9_end_first_pass(VP9_COMP *cpi) { |
340 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats); | 350 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats); |
341 } | 351 } |
342 | 352 |
343 static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, YV12_BUFFER_CONFIG *r
econ_buffer, int *best_motion_err, int recon_yoffset) { | 353 static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
| 354 YV12_BUFFER_CONFIG *recon_buffer, |
| 355 int *best_motion_err, int recon_yoffset) { |
344 MACROBLOCKD *const xd = &x->e_mbd; | 356 MACROBLOCKD *const xd = &x->e_mbd; |
345 | 357 |
346 // Set up pointers for this macro block recon buffer | 358 // Set up pointers for this macro block recon buffer |
347 xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset; | 359 xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset; |
348 | 360 |
349 switch (xd->this_mi->mbmi.sb_type) { | 361 switch (xd->mi_8x8[0]->mbmi.sb_type) { |
350 case BLOCK_8X8: | 362 case BLOCK_8X8: |
351 vp9_mse8x8(x->plane[0].src.buf, x->plane[0].src.stride, | 363 vp9_mse8x8(x->plane[0].src.buf, x->plane[0].src.stride, |
352 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, | 364 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, |
353 (unsigned int *)(best_motion_err)); | 365 (unsigned int *)(best_motion_err)); |
354 break; | 366 break; |
355 case BLOCK_16X8: | 367 case BLOCK_16X8: |
356 vp9_mse16x8(x->plane[0].src.buf, x->plane[0].src.stride, | 368 vp9_mse16x8(x->plane[0].src.buf, x->plane[0].src.stride, |
357 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, | 369 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, |
358 (unsigned int *)(best_motion_err)); | 370 (unsigned int *)(best_motion_err)); |
359 break; | 371 break; |
(...skipping 18 matching lines...) Expand all Loading... |
378 int num00; | 390 int num00; |
379 | 391 |
380 int_mv tmp_mv; | 392 int_mv tmp_mv; |
381 int_mv ref_mv_full; | 393 int_mv ref_mv_full; |
382 | 394 |
383 int tmp_err; | 395 int tmp_err; |
384 int step_param = 3; | 396 int step_param = 3; |
385 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; | 397 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; |
386 int n; | 398 int n; |
387 vp9_variance_fn_ptr_t v_fn_ptr = | 399 vp9_variance_fn_ptr_t v_fn_ptr = |
388 cpi->fn_ptr[xd->this_mi->mbmi.sb_type]; | 400 cpi->fn_ptr[xd->mi_8x8[0]->mbmi.sb_type]; |
389 int new_mv_mode_penalty = 256; | 401 int new_mv_mode_penalty = 256; |
390 | 402 |
391 int sr = 0; | 403 int sr = 0; |
392 int quart_frm = MIN(cpi->common.width, cpi->common.height); | 404 int quart_frm = MIN(cpi->common.width, cpi->common.height); |
393 | 405 |
394 // refine the motion search range accroding to the frame dimension | 406 // refine the motion search range accroding to the frame dimension |
395 // for first pass test | 407 // for first pass test |
396 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) | 408 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) |
397 sr++; | 409 sr++; |
398 if (sr) | 410 if (sr) |
399 sr--; | 411 sr--; |
400 | 412 |
401 step_param += sr; | 413 step_param += sr; |
402 further_steps -= sr; | 414 further_steps -= sr; |
403 | 415 |
404 // override the default variance function to use MSE | 416 // override the default variance function to use MSE |
405 switch (xd->this_mi->mbmi.sb_type) { | 417 switch (xd->mi_8x8[0]->mbmi.sb_type) { |
406 case BLOCK_8X8: | 418 case BLOCK_8X8: |
407 v_fn_ptr.vf = vp9_mse8x8; | 419 v_fn_ptr.vf = vp9_mse8x8; |
408 break; | 420 break; |
409 case BLOCK_16X8: | 421 case BLOCK_16X8: |
410 v_fn_ptr.vf = vp9_mse16x8; | 422 v_fn_ptr.vf = vp9_mse16x8; |
411 break; | 423 break; |
412 case BLOCK_8X16: | 424 case BLOCK_8X16: |
413 v_fn_ptr.vf = vp9_mse8x16; | 425 v_fn_ptr.vf = vp9_mse8x16; |
414 break; | 426 break; |
415 default: | 427 default: |
(...skipping 21 matching lines...) Expand all Loading... |
437 best_mv->col = tmp_mv.as_mv.col; | 449 best_mv->col = tmp_mv.as_mv.col; |
438 } | 450 } |
439 | 451 |
440 // Further step/diamond searches as necessary | 452 // Further step/diamond searches as necessary |
441 n = num00; | 453 n = num00; |
442 num00 = 0; | 454 num00 = 0; |
443 | 455 |
444 while (n < further_steps) { | 456 while (n < further_steps) { |
445 n++; | 457 n++; |
446 | 458 |
447 if (num00) | 459 if (num00) { |
448 num00--; | 460 num00--; |
449 else { | 461 } else { |
450 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, | 462 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, |
451 step_param + n, x->sadperbit16, | 463 step_param + n, x->sadperbit16, |
452 &num00, &v_fn_ptr, | 464 &num00, &v_fn_ptr, |
453 x->nmvjointcost, | 465 x->nmvjointcost, |
454 x->mvcost, ref_mv); | 466 x->mvcost, ref_mv); |
455 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 467 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
456 tmp_err += new_mv_mode_penalty; | 468 tmp_err += new_mv_mode_penalty; |
457 | 469 |
458 if (tmp_err < *best_motion_err) { | 470 if (tmp_err < *best_motion_err) { |
459 *best_motion_err = tmp_err; | 471 *best_motion_err = tmp_err; |
460 best_mv->row = tmp_mv.as_mv.row; | 472 best_mv->row = tmp_mv.as_mv.row; |
461 best_mv->col = tmp_mv.as_mv.col; | 473 best_mv->col = tmp_mv.as_mv.col; |
462 } | 474 } |
463 } | 475 } |
464 } | 476 } |
465 } | 477 } |
466 | 478 |
467 void vp9_first_pass(VP9_COMP *cpi) { | 479 void vp9_first_pass(VP9_COMP *cpi) { |
468 int mb_row, mb_col; | 480 int mb_row, mb_col; |
469 MACROBLOCK *const x = &cpi->mb; | 481 MACROBLOCK *const x = &cpi->mb; |
470 VP9_COMMON *const cm = &cpi->common; | 482 VP9_COMMON *const cm = &cpi->common; |
471 MACROBLOCKD *const xd = &x->e_mbd; | 483 MACROBLOCKD *const xd = &x->e_mbd; |
| 484 TileInfo tile; |
472 | 485 |
473 int recon_yoffset, recon_uvoffset; | 486 int recon_yoffset, recon_uvoffset; |
474 const int lst_yv12_idx = cm->ref_frame_map[cpi->lst_fb_idx]; | 487 const int lst_yv12_idx = cm->ref_frame_map[cpi->lst_fb_idx]; |
475 const int gld_yv12_idx = cm->ref_frame_map[cpi->gld_fb_idx]; | 488 const int gld_yv12_idx = cm->ref_frame_map[cpi->gld_fb_idx]; |
476 YV12_BUFFER_CONFIG *const lst_yv12 = &cm->yv12_fb[lst_yv12_idx]; | 489 YV12_BUFFER_CONFIG *const lst_yv12 = &cm->yv12_fb[lst_yv12_idx]; |
477 YV12_BUFFER_CONFIG *const new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; | |
478 YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx]; | 490 YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx]; |
| 491 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); |
479 const int recon_y_stride = lst_yv12->y_stride; | 492 const int recon_y_stride = lst_yv12->y_stride; |
480 const int recon_uv_stride = lst_yv12->uv_stride; | 493 const int recon_uv_stride = lst_yv12->uv_stride; |
481 int64_t intra_error = 0; | 494 int64_t intra_error = 0; |
482 int64_t coded_error = 0; | 495 int64_t coded_error = 0; |
483 int64_t sr_coded_error = 0; | 496 int64_t sr_coded_error = 0; |
484 | 497 |
485 int sum_mvr = 0, sum_mvc = 0; | 498 int sum_mvr = 0, sum_mvc = 0; |
486 int sum_mvr_abs = 0, sum_mvc_abs = 0; | 499 int sum_mvr_abs = 0, sum_mvc_abs = 0; |
487 int sum_mvrs = 0, sum_mvcs = 0; | 500 int sum_mvrs = 0, sum_mvcs = 0; |
488 int mvcount = 0; | 501 int mvcount = 0; |
489 int intercount = 0; | 502 int intercount = 0; |
490 int second_ref_count = 0; | 503 int second_ref_count = 0; |
491 int intrapenalty = 256; | 504 int intrapenalty = 256; |
492 int neutral_count = 0; | 505 int neutral_count = 0; |
493 int new_mv_count = 0; | 506 int new_mv_count = 0; |
494 int sum_in_vectors = 0; | 507 int sum_in_vectors = 0; |
495 uint32_t lastmv_as_int = 0; | 508 uint32_t lastmv_as_int = 0; |
496 | 509 |
497 int_mv zero_ref_mv; | 510 int_mv zero_ref_mv; |
498 | 511 |
499 zero_ref_mv.as_int = 0; | 512 zero_ref_mv.as_int = 0; |
500 | 513 |
501 vp9_clear_system_state(); // __asm emms; | 514 vp9_clear_system_state(); // __asm emms; |
502 | 515 |
503 vp9_setup_src_planes(x, cpi->Source, 0, 0); | 516 vp9_setup_src_planes(x, cpi->Source, 0, 0); |
504 setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL); | 517 setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL); |
505 setup_dst_planes(xd, new_yv12, 0, 0); | 518 setup_dst_planes(xd, new_yv12, 0, 0); |
506 | 519 |
507 x->partition_info = x->pi; | |
508 xd->mi_8x8 = cm->mi_grid_visible; | 520 xd->mi_8x8 = cm->mi_grid_visible; |
509 // required for vp9_frame_init_quantizer | 521 // required for vp9_frame_init_quantizer |
510 xd->this_mi = | |
511 xd->mi_8x8[0] = cm->mi; | 522 xd->mi_8x8[0] = cm->mi; |
512 xd->mic_stream_ptr = cm->mi; | |
513 | 523 |
514 setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); | 524 setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); |
515 | 525 |
516 vp9_frame_init_quantizer(cpi); | 526 vp9_frame_init_quantizer(cpi); |
517 | 527 |
518 // Initialise the MV cost table to the defaults | 528 // Initialise the MV cost table to the defaults |
519 // if( cm->current_video_frame == 0) | 529 // if( cm->current_video_frame == 0) |
520 // if ( 0 ) | 530 // if ( 0 ) |
521 { | 531 { |
522 vp9_init_mv_probs(cm); | 532 vp9_init_mv_probs(cm); |
523 vp9_initialize_rd_consts(cpi, cm->base_qindex + cm->y_dc_delta_q); | 533 vp9_initialize_rd_consts(cpi); |
524 } | 534 } |
525 | 535 |
| 536 // tiling is ignored in the first pass |
| 537 vp9_tile_init(&tile, cm, 0, 0); |
| 538 |
526 // for each macroblock row in image | 539 // for each macroblock row in image |
527 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { | 540 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { |
528 int_mv best_ref_mv; | 541 int_mv best_ref_mv; |
529 | 542 |
530 best_ref_mv.as_int = 0; | 543 best_ref_mv.as_int = 0; |
531 | 544 |
532 // reset above block coeffs | 545 // reset above block coeffs |
533 xd->up_available = (mb_row != 0); | 546 xd->up_available = (mb_row != 0); |
534 recon_yoffset = (mb_row * recon_y_stride * 16); | 547 recon_yoffset = (mb_row * recon_y_stride * 16); |
535 recon_uvoffset = (mb_row * recon_uv_stride * 8); | 548 recon_uvoffset = (mb_row * recon_uv_stride * 8); |
536 | 549 |
537 // Set up limit values for motion vectors to prevent them extending outside
the UMV borders | 550 // Set up limit values for motion vectors to prevent them extending |
538 x->mv_row_min = -((mb_row * 16) + (VP9BORDERINPIXELS - 8)); | 551 // outside the UMV borders |
| 552 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); |
539 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) | 553 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) |
540 + (VP9BORDERINPIXELS - 8); | 554 + BORDER_MV_PIXELS_B16; |
541 | 555 |
542 // for each macroblock col in image | 556 // for each macroblock col in image |
543 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { | 557 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { |
544 int this_error; | 558 int this_error; |
545 int gf_motion_error = INT_MAX; | 559 int gf_motion_error = INT_MAX; |
546 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); | 560 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
| 561 double error_weight; |
| 562 |
| 563 vp9_clear_system_state(); // __asm emms; |
| 564 error_weight = 1.0; // avoid uninitialized warnings |
547 | 565 |
548 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; | 566 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; |
549 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; | 567 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; |
550 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; | 568 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; |
551 xd->left_available = (mb_col != 0); | 569 xd->left_available = (mb_col != 0); |
552 | 570 |
553 if (mb_col * 2 + 1 < cm->mi_cols) { | 571 if (mb_col * 2 + 1 < cm->mi_cols) { |
554 if (mb_row * 2 + 1 < cm->mi_rows) { | 572 if (mb_row * 2 + 1 < cm->mi_rows) { |
555 xd->this_mi->mbmi.sb_type = BLOCK_16X16; | 573 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X16; |
556 } else { | 574 } else { |
557 xd->this_mi->mbmi.sb_type = BLOCK_16X8; | 575 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X8; |
558 } | 576 } |
559 } else { | 577 } else { |
560 if (mb_row * 2 + 1 < cm->mi_rows) { | 578 if (mb_row * 2 + 1 < cm->mi_rows) { |
561 xd->this_mi->mbmi.sb_type = BLOCK_8X16; | 579 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X16; |
562 } else { | 580 } else { |
563 xd->this_mi->mbmi.sb_type = BLOCK_8X8; | 581 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X8; |
564 } | 582 } |
565 } | 583 } |
566 xd->this_mi->mbmi.ref_frame[0] = INTRA_FRAME; | 584 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
567 set_mi_row_col(cm, xd, | 585 set_mi_row_col(xd, &tile, |
568 mb_row << 1, | 586 mb_row << 1, |
569 1 << mi_height_log2(xd->this_mi->mbmi.sb_type), | 587 1 << mi_height_log2(xd->mi_8x8[0]->mbmi.sb_type), |
570 mb_col << 1, | 588 mb_col << 1, |
571 1 << mi_height_log2(xd->this_mi->mbmi.sb_type)); | 589 1 << mi_width_log2(xd->mi_8x8[0]->mbmi.sb_type), |
| 590 cm->mi_rows, cm->mi_cols); |
| 591 |
| 592 if (cpi->sf.variance_adaptive_quantization) { |
| 593 int energy = vp9_block_energy(cpi, x, xd->mi_8x8[0]->mbmi.sb_type); |
| 594 error_weight = vp9_vaq_inv_q_ratio(energy); |
| 595 } |
572 | 596 |
573 // do intra 16x16 prediction | 597 // do intra 16x16 prediction |
574 this_error = vp9_encode_intra(x, use_dc_pred); | 598 this_error = vp9_encode_intra(x, use_dc_pred); |
| 599 if (cpi->sf.variance_adaptive_quantization) { |
| 600 vp9_clear_system_state(); // __asm emms; |
| 601 this_error *= error_weight; |
| 602 } |
575 | 603 |
576 // "intrapenalty" below deals with situations where the intra and inter er
ror scores are very low (eg a plain black frame) | 604 // intrapenalty below deals with situations where the intra and inter |
577 // We do not have special cases in first pass for 0,0 and nearest etc so a
ll inter modes carry an overhead cost estimate fot the mv. | 605 // error scores are very low (eg a plain black frame). |
578 // When the error score is very low this causes us to pick all or lots of
INTRA modes and throw lots of key frames. | 606 // We do not have special cases in first pass for 0,0 and nearest etc so |
| 607 // all inter modes carry an overhead cost estimate for the mv. |
| 608 // When the error score is very low this causes us to pick all or lots of |
| 609 // INTRA modes and throw lots of key frames. |
579 // This penalty adds a cost matching that of a 0,0 mv to the intra case. | 610 // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
580 this_error += intrapenalty; | 611 this_error += intrapenalty; |
581 | 612 |
582 // Cumulative intra error total | 613 // Cumulative intra error total |
583 intra_error += (int64_t)this_error; | 614 intra_error += (int64_t)this_error; |
584 | 615 |
585 // Set up limit values for motion vectors to prevent them extending outsid
e the UMV borders | 616 // Set up limit values for motion vectors to prevent them extending |
586 x->mv_col_min = -((mb_col * 16) + (VP9BORDERINPIXELS - 8)); | 617 // outside the UMV borders. |
| 618 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
587 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) | 619 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) |
588 + (VP9BORDERINPIXELS - 8); | 620 + BORDER_MV_PIXELS_B16; |
589 | 621 |
590 // Other than for the first frame do a motion search | 622 // Other than for the first frame do a motion search |
591 if (cm->current_video_frame > 0) { | 623 if (cm->current_video_frame > 0) { |
592 int tmp_err; | 624 int tmp_err; |
593 int motion_error = INT_MAX; | 625 int motion_error = INT_MAX; |
594 int_mv mv, tmp_mv; | 626 int_mv mv, tmp_mv; |
595 | 627 |
596 // Simple 0,0 motion with no mv overhead | 628 // Simple 0,0 motion with no mv overhead |
597 zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset); | 629 zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset); |
598 mv.as_int = tmp_mv.as_int = 0; | 630 mv.as_int = tmp_mv.as_int = 0; |
599 | 631 |
600 // Test last reference frame using the previous best mv as the | 632 // Test last reference frame using the previous best mv as the |
601 // starting point (best reference) for the search | 633 // starting point (best reference) for the search |
602 first_pass_motion_search(cpi, x, &best_ref_mv, | 634 first_pass_motion_search(cpi, x, &best_ref_mv, |
603 &mv.as_mv, lst_yv12, | 635 &mv.as_mv, lst_yv12, |
604 &motion_error, recon_yoffset); | 636 &motion_error, recon_yoffset); |
| 637 if (cpi->sf.variance_adaptive_quantization) { |
| 638 vp9_clear_system_state(); // __asm emms; |
| 639 motion_error *= error_weight; |
| 640 } |
605 | 641 |
606 // If the current best reference mv is not centred on 0,0 then do a 0,0
based search as well | 642 // If the current best reference mv is not centered on 0,0 then do a 0,0 |
| 643 // based search as well. |
607 if (best_ref_mv.as_int) { | 644 if (best_ref_mv.as_int) { |
608 tmp_err = INT_MAX; | 645 tmp_err = INT_MAX; |
609 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv, | 646 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv, |
610 lst_yv12, &tmp_err, recon_yoffset); | 647 lst_yv12, &tmp_err, recon_yoffset); |
| 648 if (cpi->sf.variance_adaptive_quantization) { |
| 649 vp9_clear_system_state(); // __asm emms; |
| 650 tmp_err *= error_weight; |
| 651 } |
611 | 652 |
612 if (tmp_err < motion_error) { | 653 if (tmp_err < motion_error) { |
613 motion_error = tmp_err; | 654 motion_error = tmp_err; |
614 mv.as_int = tmp_mv.as_int; | 655 mv.as_int = tmp_mv.as_int; |
615 } | 656 } |
616 } | 657 } |
617 | 658 |
618 // Experimental search in an older reference frame | 659 // Experimental search in an older reference frame |
619 if (cm->current_video_frame > 1) { | 660 if (cm->current_video_frame > 1) { |
620 // Simple 0,0 motion with no mv overhead | 661 // Simple 0,0 motion with no mv overhead |
621 zz_motion_search(cpi, x, gld_yv12, | 662 zz_motion_search(cpi, x, gld_yv12, |
622 &gf_motion_error, recon_yoffset); | 663 &gf_motion_error, recon_yoffset); |
623 | 664 |
624 first_pass_motion_search(cpi, x, &zero_ref_mv, | 665 first_pass_motion_search(cpi, x, &zero_ref_mv, |
625 &tmp_mv.as_mv, gld_yv12, | 666 &tmp_mv.as_mv, gld_yv12, |
626 &gf_motion_error, recon_yoffset); | 667 &gf_motion_error, recon_yoffset); |
| 668 if (cpi->sf.variance_adaptive_quantization) { |
| 669 vp9_clear_system_state(); // __asm emms; |
| 670 gf_motion_error *= error_weight; |
| 671 } |
627 | 672 |
628 if ((gf_motion_error < motion_error) && | 673 if ((gf_motion_error < motion_error) && |
629 (gf_motion_error < this_error)) { | 674 (gf_motion_error < this_error)) { |
630 second_ref_count++; | 675 second_ref_count++; |
631 } | 676 } |
632 | 677 |
633 // Reset to last frame as reference buffer | 678 // Reset to last frame as reference buffer |
634 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; | 679 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; |
635 xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset; | 680 xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset; |
636 xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset; | 681 xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset; |
637 | 682 |
638 // In accumulating a score for the older reference frame | 683 // In accumulating a score for the older reference frame |
639 // take the best of the motion predicted score and | 684 // take the best of the motion predicted score and |
640 // the intra coded error (just as will be done for) | 685 // the intra coded error (just as will be done for) |
641 // accumulation of "coded_error" for the last frame. | 686 // accumulation of "coded_error" for the last frame. |
642 if (gf_motion_error < this_error) | 687 if (gf_motion_error < this_error) |
643 sr_coded_error += gf_motion_error; | 688 sr_coded_error += gf_motion_error; |
644 else | 689 else |
645 sr_coded_error += this_error; | 690 sr_coded_error += this_error; |
646 } else | 691 } else { |
647 sr_coded_error += motion_error; | 692 sr_coded_error += motion_error; |
648 | 693 } |
649 /* Intra assumed best */ | 694 /* Intra assumed best */ |
650 best_ref_mv.as_int = 0; | 695 best_ref_mv.as_int = 0; |
651 | 696 |
652 if (motion_error <= this_error) { | 697 if (motion_error <= this_error) { |
653 // Keep a count of cases where the inter and intra were | 698 // Keep a count of cases where the inter and intra were |
654 // very close and very low. This helps with scene cut | 699 // very close and very low. This helps with scene cut |
655 // detection for example in cropped clips with black bars | 700 // detection for example in cropped clips with black bars |
656 // at the sides or top and bottom. | 701 // at the sides or top and bottom. |
657 if ((((this_error - intrapenalty) * 9) <= | 702 if ((((this_error - intrapenalty) * 9) <= |
658 (motion_error * 10)) && | 703 (motion_error * 10)) && |
659 (this_error < (2 * intrapenalty))) { | 704 (this_error < (2 * intrapenalty))) { |
660 neutral_count++; | 705 neutral_count++; |
661 } | 706 } |
662 | 707 |
663 mv.as_mv.row <<= 3; | 708 mv.as_mv.row *= 8; |
664 mv.as_mv.col <<= 3; | 709 mv.as_mv.col *= 8; |
665 this_error = motion_error; | 710 this_error = motion_error; |
666 vp9_set_mbmode_and_mvs(x, NEWMV, &mv); | 711 vp9_set_mbmode_and_mvs(x, NEWMV, &mv); |
667 xd->this_mi->mbmi.tx_size = TX_4X4; | 712 xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; |
668 xd->this_mi->mbmi.ref_frame[0] = LAST_FRAME; | 713 xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME; |
669 xd->this_mi->mbmi.ref_frame[1] = NONE; | 714 xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE; |
670 vp9_build_inter_predictors_sby(xd, mb_row << 1, | 715 vp9_build_inter_predictors_sby(xd, mb_row << 1, |
671 mb_col << 1, | 716 mb_col << 1, |
672 xd->this_mi->mbmi.sb_type); | 717 xd->mi_8x8[0]->mbmi.sb_type); |
673 vp9_encode_sby(x, xd->this_mi->mbmi.sb_type); | 718 vp9_encode_sby(x, xd->mi_8x8[0]->mbmi.sb_type); |
674 sum_mvr += mv.as_mv.row; | 719 sum_mvr += mv.as_mv.row; |
675 sum_mvr_abs += abs(mv.as_mv.row); | 720 sum_mvr_abs += abs(mv.as_mv.row); |
676 sum_mvc += mv.as_mv.col; | 721 sum_mvc += mv.as_mv.col; |
677 sum_mvc_abs += abs(mv.as_mv.col); | 722 sum_mvc_abs += abs(mv.as_mv.col); |
678 sum_mvrs += mv.as_mv.row * mv.as_mv.row; | 723 sum_mvrs += mv.as_mv.row * mv.as_mv.row; |
679 sum_mvcs += mv.as_mv.col * mv.as_mv.col; | 724 sum_mvcs += mv.as_mv.col * mv.as_mv.col; |
680 intercount++; | 725 intercount++; |
681 | 726 |
682 best_ref_mv.as_int = mv.as_int; | 727 best_ref_mv.as_int = mv.as_int; |
683 | 728 |
(...skipping 26 matching lines...) Expand all Loading... |
710 else if (mv.as_mv.col < 0) | 755 else if (mv.as_mv.col < 0) |
711 sum_in_vectors++; | 756 sum_in_vectors++; |
712 } else if (mb_col > cm->mb_cols / 2) { | 757 } else if (mb_col > cm->mb_cols / 2) { |
713 if (mv.as_mv.col > 0) | 758 if (mv.as_mv.col > 0) |
714 sum_in_vectors++; | 759 sum_in_vectors++; |
715 else if (mv.as_mv.col < 0) | 760 else if (mv.as_mv.col < 0) |
716 sum_in_vectors--; | 761 sum_in_vectors--; |
717 } | 762 } |
718 } | 763 } |
719 } | 764 } |
720 } else | 765 } else { |
721 sr_coded_error += (int64_t)this_error; | 766 sr_coded_error += (int64_t)this_error; |
722 | 767 } |
723 coded_error += (int64_t)this_error; | 768 coded_error += (int64_t)this_error; |
724 | 769 |
725 // adjust to the next column of macroblocks | 770 // adjust to the next column of macroblocks |
726 x->plane[0].src.buf += 16; | 771 x->plane[0].src.buf += 16; |
727 x->plane[1].src.buf += 8; | 772 x->plane[1].src.buf += 8; |
728 x->plane[2].src.buf += 8; | 773 x->plane[2].src.buf += 8; |
729 | 774 |
730 recon_yoffset += 16; | 775 recon_yoffset += 16; |
731 recon_uvoffset += 8; | 776 recon_uvoffset += 8; |
732 } | 777 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 | 816 |
772 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs; | 817 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs; |
773 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs; | 818 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs; |
774 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs; | 819 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs; |
775 | 820 |
776 if (mvcount > 0) { | 821 if (mvcount > 0) { |
777 fps.MVr = (double)sum_mvr / (double)mvcount; | 822 fps.MVr = (double)sum_mvr / (double)mvcount; |
778 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount; | 823 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount; |
779 fps.MVc = (double)sum_mvc / (double)mvcount; | 824 fps.MVc = (double)sum_mvc / (double)mvcount; |
780 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount; | 825 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount; |
781 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (d
ouble)mvcount; | 826 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / |
782 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (d
ouble)mvcount; | 827 (double)mvcount; |
| 828 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / |
| 829 (double)mvcount; |
783 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2); | 830 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2); |
784 fps.new_mv_count = new_mv_count; | 831 fps.new_mv_count = new_mv_count; |
785 | 832 |
786 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs; | 833 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs; |
787 } | 834 } |
788 | 835 |
789 // TODO: handle the case when duration is set to 0, or something less | 836 // TODO(paulwilkins): Handle the case when duration is set to 0, or |
790 // than the full time between subsequent values of cpi->source_time_stamp. | 837 // something less than the full time between subsequent values of |
| 838 // cpi->source_time_stamp. |
791 fps.duration = (double)(cpi->source->ts_end | 839 fps.duration = (double)(cpi->source->ts_end |
792 - cpi->source->ts_start); | 840 - cpi->source->ts_start); |
793 | 841 |
794 // don't want to do output stats with a stack variable! | 842 // don't want to do output stats with a stack variable! |
795 cpi->twopass.this_frame_stats = fps; | 843 cpi->twopass.this_frame_stats = fps; |
796 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats); | 844 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.this_frame_stats); |
797 accumulate_stats(&cpi->twopass.total_stats, &fps); | 845 accumulate_stats(&cpi->twopass.total_stats, &fps); |
798 } | 846 } |
799 | 847 |
800 // Copy the previous Last Frame back into gf and and arf buffers if | 848 // Copy the previous Last Frame back into gf and and arf buffers if |
801 // the prediction is good enough... but also dont allow it to lag too far | 849 // the prediction is good enough... but also dont allow it to lag too far |
802 if ((cpi->twopass.sr_update_lag > 3) || | 850 if ((cpi->twopass.sr_update_lag > 3) || |
803 ((cm->current_video_frame > 0) && | 851 ((cm->current_video_frame > 0) && |
804 (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) && | 852 (cpi->twopass.this_frame_stats.pcnt_inter > 0.20) && |
805 ((cpi->twopass.this_frame_stats.intra_error / | 853 ((cpi->twopass.this_frame_stats.intra_error / |
806 DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) > | 854 DOUBLE_DIVIDE_CHECK(cpi->twopass.this_frame_stats.coded_error)) > |
807 2.0))) { | 855 2.0))) { |
808 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 856 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
809 cpi->twopass.sr_update_lag = 1; | 857 cpi->twopass.sr_update_lag = 1; |
810 } else | 858 } else { |
811 cpi->twopass.sr_update_lag++; | 859 cpi->twopass.sr_update_lag++; |
812 | 860 } |
813 // swap frame pointers so last frame refers to the frame we just compressed | 861 // swap frame pointers so last frame refers to the frame we just compressed |
814 swap_yv12(lst_yv12, new_yv12); | 862 swap_yv12(lst_yv12, new_yv12); |
815 | 863 |
816 vp9_extend_frame_borders(lst_yv12, cm->subsampling_x, cm->subsampling_y); | 864 vp9_extend_frame_borders(lst_yv12, cm->subsampling_x, cm->subsampling_y); |
817 | 865 |
818 // Special case for the first frame. Copy into the GF buffer as a second refer
ence. | 866 // Special case for the first frame. Copy into the GF buffer as a second |
| 867 // reference. |
819 if (cm->current_video_frame == 0) | 868 if (cm->current_video_frame == 0) |
820 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 869 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
821 | 870 |
822 // use this to see what the first pass reconstruction looks like | 871 // use this to see what the first pass reconstruction looks like |
823 if (0) { | 872 if (0) { |
824 char filename[512]; | 873 char filename[512]; |
825 FILE *recon_file; | 874 FILE *recon_file; |
826 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); | 875 snprintf(filename, sizeof(filename), "enc%04d.yuv", |
| 876 (int)cm->current_video_frame); |
827 | 877 |
828 if (cm->current_video_frame == 0) | 878 if (cm->current_video_frame == 0) |
829 recon_file = fopen(filename, "wb"); | 879 recon_file = fopen(filename, "wb"); |
830 else | 880 else |
831 recon_file = fopen(filename, "ab"); | 881 recon_file = fopen(filename, "ab"); |
832 | 882 |
833 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); | 883 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); |
834 fclose(recon_file); | 884 fclose(recon_file); |
835 } | 885 } |
836 | 886 |
837 cm->current_video_frame++; | 887 cm->current_video_frame++; |
838 | |
839 } | 888 } |
840 | 889 |
841 // Estimate a cost per mb attributable to overheads such as the coding of | 890 // Estimate a cost per mb attributable to overheads such as the coding of |
842 // modes and motion vectors. | 891 // modes and motion vectors. |
843 // Currently simplistic in its assumptions for testing. | 892 // Currently simplistic in its assumptions for testing. |
844 // | 893 // |
845 | 894 |
846 | 895 |
847 static double bitcost(double prob) { | 896 static double bitcost(double prob) { |
848 return -(log(prob) / log(2.0)); | 897 return -(log(prob) / log(2.0)); |
(...skipping 22 matching lines...) Expand all Loading... |
871 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; | 920 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; |
872 | 921 |
873 // Crude estimate of overhead cost from modes | 922 // Crude estimate of overhead cost from modes |
874 // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb | 923 // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb |
875 mode_cost = | 924 mode_cost = |
876 (int)((((av_pct_inter - av_pct_motion) * zz_cost) + | 925 (int)((((av_pct_inter - av_pct_motion) * zz_cost) + |
877 (av_pct_motion * motion_cost) + | 926 (av_pct_motion * motion_cost) + |
878 (av_intra * intra_cost)) * cpi->common.MBs) << 9; | 927 (av_intra * intra_cost)) * cpi->common.MBs) << 9; |
879 | 928 |
880 // return mv_cost + mode_cost; | 929 // return mv_cost + mode_cost; |
881 // TODO PGW Fix overhead costs for extended Q range | 930 // TODO(paulwilkins): Fix overhead costs for extended Q range. |
882 #endif | 931 #endif |
883 return 0; | 932 return 0; |
884 } | 933 } |
885 | 934 |
886 static double calc_correction_factor(double err_per_mb, | 935 static double calc_correction_factor(double err_per_mb, |
887 double err_divisor, | 936 double err_divisor, |
888 double pt_low, | 937 double pt_low, |
889 double pt_high, | 938 double pt_high, |
890 int q) { | 939 int q) { |
891 const double error_term = err_per_mb / err_divisor; | 940 const double error_term = err_per_mb / err_divisor; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1095 return q; | 1144 return q; |
1096 } | 1145 } |
1097 | 1146 |
1098 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); | 1147 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); |
1099 | 1148 |
1100 void vp9_init_second_pass(VP9_COMP *cpi) { | 1149 void vp9_init_second_pass(VP9_COMP *cpi) { |
1101 FIRSTPASS_STATS this_frame; | 1150 FIRSTPASS_STATS this_frame; |
1102 FIRSTPASS_STATS *start_pos; | 1151 FIRSTPASS_STATS *start_pos; |
1103 | 1152 |
1104 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate; | 1153 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate; |
1105 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth | 1154 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * |
1106 * cpi->oxcf.two_pass_vbrmin_section / 100)
; | 1155 cpi->oxcf.two_pass_vbrmin_section / 100); |
1107 | 1156 |
1108 if (two_pass_min_rate < lower_bounds_min_rate) | 1157 if (two_pass_min_rate < lower_bounds_min_rate) |
1109 two_pass_min_rate = lower_bounds_min_rate; | 1158 two_pass_min_rate = lower_bounds_min_rate; |
1110 | 1159 |
1111 zero_stats(&cpi->twopass.total_stats); | 1160 zero_stats(&cpi->twopass.total_stats); |
1112 zero_stats(&cpi->twopass.total_left_stats); | 1161 zero_stats(&cpi->twopass.total_left_stats); |
1113 | 1162 |
1114 if (!cpi->twopass.stats_in_end) | 1163 if (!cpi->twopass.stats_in_end) |
1115 return; | 1164 return; |
1116 | 1165 |
(...skipping 17 matching lines...) Expand all Loading... |
1134 // Calculate a minimum intra value to be used in determining the IIratio | 1183 // Calculate a minimum intra value to be used in determining the IIratio |
1135 // scores used in the second pass. We have this minimum to make sure | 1184 // scores used in the second pass. We have this minimum to make sure |
1136 // that clips that are static but "low complexity" in the intra domain | 1185 // that clips that are static but "low complexity" in the intra domain |
1137 // are still boosted appropriately for KF/GF/ARF | 1186 // are still boosted appropriately for KF/GF/ARF |
1138 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; | 1187 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
1139 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; | 1188 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
1140 | 1189 |
1141 // This variable monitors how far behind the second ref update is lagging | 1190 // This variable monitors how far behind the second ref update is lagging |
1142 cpi->twopass.sr_update_lag = 1; | 1191 cpi->twopass.sr_update_lag = 1; |
1143 | 1192 |
1144 // Scan the first pass file and calculate an average Intra / Inter error score
ratio for the sequence | 1193 // Scan the first pass file and calculate an average Intra / Inter error score |
| 1194 // ratio for the sequence. |
1145 { | 1195 { |
1146 double sum_iiratio = 0.0; | 1196 double sum_iiratio = 0.0; |
1147 double IIRatio; | 1197 double IIRatio; |
1148 | 1198 |
1149 start_pos = cpi->twopass.stats_in; // Note starting "file" pos
ition | 1199 start_pos = cpi->twopass.stats_in; // Note the starting "file" position. |
1150 | 1200 |
1151 while (input_stats(cpi, &this_frame) != EOF) { | 1201 while (input_stats(cpi, &this_frame) != EOF) { |
1152 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_er
ror); | 1202 IIRatio = this_frame.intra_error |
| 1203 / DOUBLE_DIVIDE_CHECK(this_frame.coded_error); |
1153 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio; | 1204 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio; |
1154 sum_iiratio += IIRatio; | 1205 sum_iiratio += IIRatio; |
1155 } | 1206 } |
1156 | 1207 |
1157 cpi->twopass.avg_iiratio = sum_iiratio / | 1208 cpi->twopass.avg_iiratio = sum_iiratio / |
1158 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count); | 1209 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.total_stats.count); |
1159 | 1210 |
1160 // Reset file position | 1211 // Reset file position |
1161 reset_fpf_position(cpi, start_pos); | 1212 reset_fpf_position(cpi, start_pos); |
1162 } | 1213 } |
1163 | 1214 |
1164 // Scan the first pass file and calculate a modified total error based upon th
e bias/power function | 1215 // Scan the first pass file and calculate a modified total error based upon |
1165 // used to allocate bits | 1216 // the bias/power function used to allocate bits. |
1166 { | 1217 { |
1167 start_pos = cpi->twopass.stats_in; // Note starting "file" pos
ition | 1218 start_pos = cpi->twopass.stats_in; // Note starting "file" position |
1168 | 1219 |
1169 cpi->twopass.modified_error_total = 0.0; | 1220 cpi->twopass.modified_error_total = 0.0; |
1170 cpi->twopass.modified_error_used = 0.0; | 1221 cpi->twopass.modified_error_used = 0.0; |
1171 | 1222 |
1172 while (input_stats(cpi, &this_frame) != EOF) { | 1223 while (input_stats(cpi, &this_frame) != EOF) { |
1173 cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_fra
me); | 1224 cpi->twopass.modified_error_total += |
| 1225 calculate_modified_err(cpi, &this_frame); |
1174 } | 1226 } |
1175 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total; | 1227 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total; |
1176 | 1228 |
1177 reset_fpf_position(cpi, start_pos); // Reset file position | 1229 reset_fpf_position(cpi, start_pos); // Reset file position |
1178 | |
1179 } | 1230 } |
1180 } | 1231 } |
1181 | 1232 |
1182 void vp9_end_second_pass(VP9_COMP *cpi) { | 1233 void vp9_end_second_pass(VP9_COMP *cpi) { |
1183 } | 1234 } |
1184 | 1235 |
1185 // This function gives and estimate of how badly we believe | 1236 // This function gives and estimate of how badly we believe |
1186 // the prediction quality is decaying from frame to frame. | 1237 // the prediction quality is decaying from frame to frame. |
1187 static double get_prediction_decay_rate(VP9_COMP *cpi, | 1238 static double get_prediction_decay_rate(VP9_COMP *cpi, |
1188 FIRSTPASS_STATS *next_frame) { | 1239 FIRSTPASS_STATS *next_frame) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1314 | 1365 |
1315 *mv_ratio_accumulator += | 1366 *mv_ratio_accumulator += |
1316 (this_frame_mvr_ratio < this_frame->mvr_abs) | 1367 (this_frame_mvr_ratio < this_frame->mvr_abs) |
1317 ? (this_frame_mvr_ratio * motion_pct) | 1368 ? (this_frame_mvr_ratio * motion_pct) |
1318 : this_frame->mvr_abs * motion_pct; | 1369 : this_frame->mvr_abs * motion_pct; |
1319 | 1370 |
1320 *mv_ratio_accumulator += | 1371 *mv_ratio_accumulator += |
1321 (this_frame_mvc_ratio < this_frame->mvc_abs) | 1372 (this_frame_mvc_ratio < this_frame->mvc_abs) |
1322 ? (this_frame_mvc_ratio * motion_pct) | 1373 ? (this_frame_mvc_ratio * motion_pct) |
1323 : this_frame->mvc_abs * motion_pct; | 1374 : this_frame->mvc_abs * motion_pct; |
1324 | |
1325 } | 1375 } |
1326 } | 1376 } |
1327 | 1377 |
1328 // Calculate a baseline boost number for the current frame. | 1378 // Calculate a baseline boost number for the current frame. |
1329 static double calc_frame_boost( | 1379 static double calc_frame_boost( |
1330 VP9_COMP *cpi, | 1380 VP9_COMP *cpi, |
1331 FIRSTPASS_STATS *this_frame, | 1381 FIRSTPASS_STATS *this_frame, |
1332 double this_frame_mv_in_out) { | 1382 double this_frame_mv_in_out) { |
1333 double frame_boost; | 1383 double frame_boost; |
1334 | 1384 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1373 int flash_detected = 0; | 1423 int flash_detected = 0; |
1374 | 1424 |
1375 // Search forward from the proposed arf/next gf position | 1425 // Search forward from the proposed arf/next gf position |
1376 for (i = 0; i < f_frames; i++) { | 1426 for (i = 0; i < f_frames; i++) { |
1377 if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) | 1427 if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) |
1378 break; | 1428 break; |
1379 | 1429 |
1380 // Update the motion related elements to the boost calculation | 1430 // Update the motion related elements to the boost calculation |
1381 accumulate_frame_motion_stats(&this_frame, | 1431 accumulate_frame_motion_stats(&this_frame, |
1382 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1432 &this_frame_mv_in_out, &mv_in_out_accumulator, |
1383 &abs_mv_in_out_accumulator, &mv_ratio_accumula
tor); | 1433 &abs_mv_in_out_accumulator, |
| 1434 &mv_ratio_accumulator); |
1384 | 1435 |
1385 // We want to discount the flash frame itself and the recovery | 1436 // We want to discount the flash frame itself and the recovery |
1386 // frame that follows as both will have poor scores. | 1437 // frame that follows as both will have poor scores. |
1387 flash_detected = detect_flash(cpi, (i + offset)) || | 1438 flash_detected = detect_flash(cpi, (i + offset)) || |
1388 detect_flash(cpi, (i + offset + 1)); | 1439 detect_flash(cpi, (i + offset + 1)); |
1389 | 1440 |
1390 // Cumulative effect of prediction quality decay | 1441 // Cumulative effect of prediction quality decay |
1391 if (!flash_detected) { | 1442 if (!flash_detected) { |
1392 decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame); | 1443 decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame); |
1393 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1444 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
(...skipping 15 matching lines...) Expand all Loading... |
1409 abs_mv_in_out_accumulator = 0.0; | 1460 abs_mv_in_out_accumulator = 0.0; |
1410 | 1461 |
1411 // Search backward towards last gf position | 1462 // Search backward towards last gf position |
1412 for (i = -1; i >= -b_frames; i--) { | 1463 for (i = -1; i >= -b_frames; i--) { |
1413 if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) | 1464 if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF) |
1414 break; | 1465 break; |
1415 | 1466 |
1416 // Update the motion related elements to the boost calculation | 1467 // Update the motion related elements to the boost calculation |
1417 accumulate_frame_motion_stats(&this_frame, | 1468 accumulate_frame_motion_stats(&this_frame, |
1418 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1469 &this_frame_mv_in_out, &mv_in_out_accumulator, |
1419 &abs_mv_in_out_accumulator, &mv_ratio_accumula
tor); | 1470 &abs_mv_in_out_accumulator, |
| 1471 &mv_ratio_accumulator); |
1420 | 1472 |
1421 // We want to discount the the flash frame itself and the recovery | 1473 // We want to discount the the flash frame itself and the recovery |
1422 // frame that follows as both will have poor scores. | 1474 // frame that follows as both will have poor scores. |
1423 flash_detected = detect_flash(cpi, (i + offset)) || | 1475 flash_detected = detect_flash(cpi, (i + offset)) || |
1424 detect_flash(cpi, (i + offset + 1)); | 1476 detect_flash(cpi, (i + offset + 1)); |
1425 | 1477 |
1426 // Cumulative effect of prediction quality decay | 1478 // Cumulative effect of prediction quality decay |
1427 if (!flash_detected) { | 1479 if (!flash_detected) { |
1428 decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame); | 1480 decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame); |
1429 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1481 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
1430 ? MIN_DECAY_FACTOR : decay_accumulator; | 1482 ? MIN_DECAY_FACTOR : decay_accumulator; |
1431 } | 1483 } |
1432 | 1484 |
1433 boost_score += (decay_accumulator * | 1485 boost_score += (decay_accumulator * |
1434 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); | 1486 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); |
1435 | |
1436 } | 1487 } |
1437 *b_boost = (int)boost_score; | 1488 *b_boost = (int)boost_score; |
1438 | 1489 |
1439 arf_boost = (*f_boost + *b_boost); | 1490 arf_boost = (*f_boost + *b_boost); |
1440 if (arf_boost < ((b_frames + f_frames) * 20)) | 1491 if (arf_boost < ((b_frames + f_frames) * 20)) |
1441 arf_boost = ((b_frames + f_frames) * 20); | 1492 arf_boost = ((b_frames + f_frames) * 20); |
1442 | 1493 |
1443 return arf_boost; | 1494 return arf_boost; |
1444 } | 1495 } |
1445 | 1496 |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1659 if (EOF == input_stats(cpi, &next_frame)) | 1710 if (EOF == input_stats(cpi, &next_frame)) |
1660 break; | 1711 break; |
1661 | 1712 |
1662 // Test for the case where there is a brief flash but the prediction | 1713 // Test for the case where there is a brief flash but the prediction |
1663 // quality back to an earlier frame is then restored. | 1714 // quality back to an earlier frame is then restored. |
1664 flash_detected = detect_flash(cpi, 0); | 1715 flash_detected = detect_flash(cpi, 0); |
1665 | 1716 |
1666 // Update the motion related elements to the boost calculation | 1717 // Update the motion related elements to the boost calculation |
1667 accumulate_frame_motion_stats(&next_frame, | 1718 accumulate_frame_motion_stats(&next_frame, |
1668 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1719 &this_frame_mv_in_out, &mv_in_out_accumulator, |
1669 &abs_mv_in_out_accumulator, &mv_ratio_accumula
tor); | 1720 &abs_mv_in_out_accumulator, |
| 1721 &mv_ratio_accumulator); |
1670 | 1722 |
1671 // Cumulative effect of prediction quality decay | 1723 // Cumulative effect of prediction quality decay |
1672 if (!flash_detected) { | 1724 if (!flash_detected) { |
1673 last_loop_decay_rate = loop_decay_rate; | 1725 last_loop_decay_rate = loop_decay_rate; |
1674 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); | 1726 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); |
1675 decay_accumulator = decay_accumulator * loop_decay_rate; | 1727 decay_accumulator = decay_accumulator * loop_decay_rate; |
1676 | 1728 |
1677 // Monitor for static sections. | 1729 // Monitor for static sections. |
1678 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 1730 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
1679 zero_motion_accumulator) { | 1731 zero_motion_accumulator) { |
(...skipping 22 matching lines...) Expand all Loading... |
1702 ( | 1754 ( |
1703 // Don't break out with a very short interval | 1755 // Don't break out with a very short interval |
1704 (i > MIN_GF_INTERVAL) && | 1756 (i > MIN_GF_INTERVAL) && |
1705 // Don't break out very close to a key frame | 1757 // Don't break out very close to a key frame |
1706 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) && | 1758 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) && |
1707 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) && | 1759 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) && |
1708 (!flash_detected) && | 1760 (!flash_detected) && |
1709 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || | 1761 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || |
1710 (abs_mv_in_out_accumulator > 3.0) || | 1762 (abs_mv_in_out_accumulator > 3.0) || |
1711 (mv_in_out_accumulator < -2.0) || | 1763 (mv_in_out_accumulator < -2.0) || |
1712 ((boost_score - old_boost_score) < IIFACTOR)) | 1764 ((boost_score - old_boost_score) < IIFACTOR)))) { |
1713 )) { | |
1714 boost_score = old_boost_score; | 1765 boost_score = old_boost_score; |
1715 break; | 1766 break; |
1716 } | 1767 } |
1717 | 1768 |
1718 *this_frame = next_frame; | 1769 *this_frame = next_frame; |
1719 | 1770 |
1720 old_boost_score = boost_score; | 1771 old_boost_score = boost_score; |
1721 } | 1772 } |
1722 | 1773 |
1723 cpi->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); | 1774 cpi->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1757 (i < cpi->oxcf.lag_in_frames) && | 1808 (i < cpi->oxcf.lag_in_frames) && |
1758 (i >= MIN_GF_INTERVAL) && | 1809 (i >= MIN_GF_INTERVAL) && |
1759 // dont use ARF very near next kf | 1810 // dont use ARF very near next kf |
1760 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) && | 1811 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) && |
1761 ((next_frame.pcnt_inter > 0.75) || | 1812 ((next_frame.pcnt_inter > 0.75) || |
1762 (next_frame.pcnt_second_ref > 0.5)) && | 1813 (next_frame.pcnt_second_ref > 0.5)) && |
1763 ((mv_in_out_accumulator / (double)i > -0.2) || | 1814 ((mv_in_out_accumulator / (double)i > -0.2) || |
1764 (mv_in_out_accumulator > -2.0)) && | 1815 (mv_in_out_accumulator > -2.0)) && |
1765 (boost_score > 100)) { | 1816 (boost_score > 100)) { |
1766 // Alternative boost calculation for alt ref | 1817 // Alternative boost calculation for alt ref |
1767 cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost
); | 1818 cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, |
| 1819 &b_boost); |
1768 cpi->source_alt_ref_pending = 1; | 1820 cpi->source_alt_ref_pending = 1; |
1769 | 1821 |
1770 #if CONFIG_MULTIPLE_ARF | 1822 #if CONFIG_MULTIPLE_ARF |
1771 // Set the ARF schedule. | 1823 // Set the ARF schedule. |
1772 if (cpi->multi_arf_enabled) { | 1824 if (cpi->multi_arf_enabled) { |
1773 schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0); | 1825 schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0); |
1774 } | 1826 } |
1775 #endif | 1827 #endif |
1776 } else { | 1828 } else { |
1777 cpi->gfu_boost = (int)boost_score; | 1829 cpi->gfu_boost = (int)boost_score; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1834 cpi->twopass.kf_group_bits = | 1886 cpi->twopass.kf_group_bits = |
1835 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0; | 1887 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0; |
1836 } | 1888 } |
1837 | 1889 |
1838 // Calculate the bits to be allocated to the group as a whole | 1890 // Calculate the bits to be allocated to the group as a whole |
1839 if ((cpi->twopass.kf_group_bits > 0) && | 1891 if ((cpi->twopass.kf_group_bits > 0) && |
1840 (cpi->twopass.kf_group_error_left > 0)) { | 1892 (cpi->twopass.kf_group_error_left > 0)) { |
1841 cpi->twopass.gf_group_bits = | 1893 cpi->twopass.gf_group_bits = |
1842 (int64_t)(cpi->twopass.kf_group_bits * | 1894 (int64_t)(cpi->twopass.kf_group_bits * |
1843 (gf_group_err / cpi->twopass.kf_group_error_left)); | 1895 (gf_group_err / cpi->twopass.kf_group_error_left)); |
1844 } else | 1896 } else { |
1845 cpi->twopass.gf_group_bits = 0; | 1897 cpi->twopass.gf_group_bits = 0; |
1846 | 1898 } |
1847 cpi->twopass.gf_group_bits = | 1899 cpi->twopass.gf_group_bits = |
1848 (cpi->twopass.gf_group_bits < 0) | 1900 (cpi->twopass.gf_group_bits < 0) |
1849 ? 0 | 1901 ? 0 |
1850 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits) | 1902 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits) |
1851 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits; | 1903 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits; |
1852 | 1904 |
1853 // Clip cpi->twopass.gf_group_bits based on user supplied data rate | 1905 // Clip cpi->twopass.gf_group_bits based on user supplied data rate |
1854 // variability limit (cpi->oxcf.two_pass_vbrmax_section) | 1906 // variability limit (cpi->oxcf.two_pass_vbrmax_section) |
1855 if (cpi->twopass.gf_group_bits > | 1907 if (cpi->twopass.gf_group_bits > |
1856 (int64_t)max_bits * cpi->baseline_gf_interval) | 1908 (int64_t)max_bits * cpi->baseline_gf_interval) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1900 double alt_gf_grp_bits = | 1952 double alt_gf_grp_bits = |
1901 (double)cpi->twopass.kf_group_bits * | 1953 (double)cpi->twopass.kf_group_bits * |
1902 (mod_frame_err * (double)cpi->baseline_gf_interval) / | 1954 (mod_frame_err * (double)cpi->baseline_gf_interval) / |
1903 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left); | 1955 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left); |
1904 | 1956 |
1905 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / | 1957 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / |
1906 (double)allocation_chunks)); | 1958 (double)allocation_chunks)); |
1907 | 1959 |
1908 if (gf_bits > alt_gf_bits) | 1960 if (gf_bits > alt_gf_bits) |
1909 gf_bits = alt_gf_bits; | 1961 gf_bits = alt_gf_bits; |
1910 } | 1962 } else { |
1911 // Else if it is harder than other frames in the group make sure it at | 1963 // If it is harder than other frames in the group make sure it at |
1912 // least receives an allocation in keeping with its relative error | 1964 // least receives an allocation in keeping with its relative error |
1913 // score, otherwise it may be worse off than an "un-boosted" frame | 1965 // score, otherwise it may be worse off than an "un-boosted" frame. |
1914 else { | |
1915 int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits * | 1966 int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits * |
1916 mod_frame_err / | 1967 mod_frame_err / |
1917 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left)); | 1968 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left)); |
1918 | 1969 |
1919 if (alt_gf_bits > gf_bits) | 1970 if (alt_gf_bits > gf_bits) |
1920 gf_bits = alt_gf_bits; | 1971 gf_bits = alt_gf_bits; |
1921 } | 1972 } |
1922 | 1973 |
1923 // Dont allow a negative value for gf_bits | 1974 // Dont allow a negative value for gf_bits |
1924 if (gf_bits < 0) | 1975 if (gf_bits < 0) |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2016 // What portion of the remaining GF group error is used by this frame. | 2067 // What portion of the remaining GF group error is used by this frame. |
2017 err_fraction = modified_err / cpi->twopass.gf_group_error_left; | 2068 err_fraction = modified_err / cpi->twopass.gf_group_error_left; |
2018 else | 2069 else |
2019 err_fraction = 0.0; | 2070 err_fraction = 0.0; |
2020 | 2071 |
2021 // How many of those bits available for allocation should we give it? | 2072 // How many of those bits available for allocation should we give it? |
2022 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); | 2073 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); |
2023 | 2074 |
2024 // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at | 2075 // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at |
2025 // the top end. | 2076 // the top end. |
2026 if (target_frame_size < 0) | 2077 if (target_frame_size < 0) { |
2027 target_frame_size = 0; | 2078 target_frame_size = 0; |
2028 else { | 2079 } else { |
2029 if (target_frame_size > max_bits) | 2080 if (target_frame_size > max_bits) |
2030 target_frame_size = max_bits; | 2081 target_frame_size = max_bits; |
2031 | 2082 |
2032 if (target_frame_size > cpi->twopass.gf_group_bits) | 2083 if (target_frame_size > cpi->twopass.gf_group_bits) |
2033 target_frame_size = (int)cpi->twopass.gf_group_bits; | 2084 target_frame_size = (int)cpi->twopass.gf_group_bits; |
2034 } | 2085 } |
2035 | 2086 |
2036 // Adjust error and bits remaining. | 2087 // Adjust error and bits remaining. |
2037 cpi->twopass.gf_group_error_left -= (int64_t)modified_err; | 2088 cpi->twopass.gf_group_error_left -= (int64_t)modified_err; |
2038 cpi->twopass.gf_group_bits -= target_frame_size; | 2089 cpi->twopass.gf_group_bits -= target_frame_size; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2086 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | 2137 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
2087 cpi->active_worst_quality = cpi->oxcf.cq_level; | 2138 cpi->active_worst_quality = cpi->oxcf.cq_level; |
2088 } else { | 2139 } else { |
2089 // Special case code for first frame. | 2140 // Special case code for first frame. |
2090 if (cpi->common.current_video_frame == 0) { | 2141 if (cpi->common.current_video_frame == 0) { |
2091 int section_target_bandwidth = | 2142 int section_target_bandwidth = |
2092 (int)(cpi->twopass.bits_left / frames_left); | 2143 (int)(cpi->twopass.bits_left / frames_left); |
2093 cpi->twopass.est_max_qcorrection_factor = 1.0; | 2144 cpi->twopass.est_max_qcorrection_factor = 1.0; |
2094 | 2145 |
2095 // Set a cq_level in constrained quality mode. | 2146 // Set a cq_level in constrained quality mode. |
| 2147 // Commenting this code out for now since it does not seem to be |
| 2148 // working well. |
| 2149 /* |
2096 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { | 2150 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
2097 int est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats, | 2151 int est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats, |
2098 section_target_bandwidth); | 2152 section_target_bandwidth); |
2099 | 2153 |
2100 cpi->cq_target_quality = cpi->oxcf.cq_level; | |
2101 if (est_cq > cpi->cq_target_quality) | 2154 if (est_cq > cpi->cq_target_quality) |
2102 cpi->cq_target_quality = est_cq; | 2155 cpi->cq_target_quality = est_cq; |
| 2156 else |
| 2157 cpi->cq_target_quality = cpi->oxcf.cq_level; |
2103 } | 2158 } |
| 2159 */ |
2104 | 2160 |
2105 // guess at maxq needed in 2nd pass | 2161 // guess at maxq needed in 2nd pass |
2106 cpi->twopass.maxq_max_limit = cpi->worst_quality; | 2162 cpi->twopass.maxq_max_limit = cpi->worst_quality; |
2107 cpi->twopass.maxq_min_limit = cpi->best_quality; | 2163 cpi->twopass.maxq_min_limit = cpi->best_quality; |
2108 | 2164 |
2109 tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats, | 2165 tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats, |
2110 section_target_bandwidth); | 2166 section_target_bandwidth); |
2111 | 2167 |
2112 cpi->active_worst_quality = tmp_q; | 2168 cpi->active_worst_quality = tmp_q; |
2113 cpi->ni_av_qi = tmp_q; | 2169 cpi->ni_av_qi = tmp_q; |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2236 FIRSTPASS_STATS *last_frame, | 2292 FIRSTPASS_STATS *last_frame, |
2237 FIRSTPASS_STATS *this_frame, | 2293 FIRSTPASS_STATS *this_frame, |
2238 FIRSTPASS_STATS *next_frame) { | 2294 FIRSTPASS_STATS *next_frame) { |
2239 int is_viable_kf = 0; | 2295 int is_viable_kf = 0; |
2240 | 2296 |
2241 // Does the frame satisfy the primary criteria of a key frame | 2297 // Does the frame satisfy the primary criteria of a key frame |
2242 // If so, then examine how well it predicts subsequent frames | 2298 // If so, then examine how well it predicts subsequent frames |
2243 if ((this_frame->pcnt_second_ref < 0.10) && | 2299 if ((this_frame->pcnt_second_ref < 0.10) && |
2244 (next_frame->pcnt_second_ref < 0.10) && | 2300 (next_frame->pcnt_second_ref < 0.10) && |
2245 ((this_frame->pcnt_inter < 0.05) || | 2301 ((this_frame->pcnt_inter < 0.05) || |
2246 ( | 2302 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) && |
2247 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) && | 2303 ((this_frame->intra_error / |
2248 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error
)) < 2.5) && | 2304 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && |
2249 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVI
DE_CHECK(this_frame->coded_error) > .40) || | 2305 ((fabs(last_frame->coded_error - this_frame->coded_error) / |
2250 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVI
DE_CHECK(this_frame->intra_error) > .40) || | 2306 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > |
2251 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_erro
r)) > 3.5) | 2307 .40) || |
2252 ) | 2308 (fabs(last_frame->intra_error - this_frame->intra_error) / |
2253 ) | 2309 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > |
2254 ) | 2310 .40) || |
2255 ) { | 2311 ((next_frame->intra_error / |
| 2312 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) { |
2256 int i; | 2313 int i; |
2257 FIRSTPASS_STATS *start_pos; | 2314 FIRSTPASS_STATS *start_pos; |
2258 | 2315 |
2259 FIRSTPASS_STATS local_next_frame; | 2316 FIRSTPASS_STATS local_next_frame; |
2260 | 2317 |
2261 double boost_score = 0.0; | 2318 double boost_score = 0.0; |
2262 double old_boost_score = 0.0; | 2319 double old_boost_score = 0.0; |
2263 double decay_accumulator = 1.0; | 2320 double decay_accumulator = 1.0; |
2264 double next_iiratio; | 2321 double next_iiratio; |
2265 | 2322 |
2266 local_next_frame = *next_frame; | 2323 local_next_frame = *next_frame; |
2267 | 2324 |
2268 // Note the starting file position so we can reset to it | 2325 // Note the starting file position so we can reset to it |
2269 start_pos = cpi->twopass.stats_in; | 2326 start_pos = cpi->twopass.stats_in; |
2270 | 2327 |
2271 // Examine how well the key frame predicts subsequent frames | 2328 // Examine how well the key frame predicts subsequent frames |
2272 for (i = 0; i < 16; i++) { | 2329 for (i = 0; i < 16; i++) { |
2273 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_
CHECK(local_next_frame.coded_error)); | 2330 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / |
| 2331 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)); |
2274 | 2332 |
2275 if (next_iiratio > RMAX) | 2333 if (next_iiratio > RMAX) |
2276 next_iiratio = RMAX; | 2334 next_iiratio = RMAX; |
2277 | 2335 |
2278 // Cumulative effect of decay in prediction quality | 2336 // Cumulative effect of decay in prediction quality |
2279 if (local_next_frame.pcnt_inter > 0.85) | 2337 if (local_next_frame.pcnt_inter > 0.85) |
2280 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; | 2338 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; |
2281 else | 2339 else |
2282 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_i
nter) / 2.0); | 2340 decay_accumulator = |
| 2341 decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0); |
2283 | 2342 |
2284 // decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; | 2343 // decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; |
2285 | 2344 |
2286 // Keep a running total | 2345 // Keep a running total |
2287 boost_score += (decay_accumulator * next_iiratio); | 2346 boost_score += (decay_accumulator * next_iiratio); |
2288 | 2347 |
2289 // Test various breakout clauses | 2348 // Test various breakout clauses |
2290 if ((local_next_frame.pcnt_inter < 0.05) || | 2349 if ((local_next_frame.pcnt_inter < 0.05) || |
2291 (next_iiratio < 1.5) || | 2350 (next_iiratio < 1.5) || |
2292 (((local_next_frame.pcnt_inter - | 2351 (((local_next_frame.pcnt_inter - |
2293 local_next_frame.pcnt_neutral) < 0.20) && | 2352 local_next_frame.pcnt_neutral) < 0.20) && |
2294 (next_iiratio < 3.0)) || | 2353 (next_iiratio < 3.0)) || |
2295 ((boost_score - old_boost_score) < 3.0) || | 2354 ((boost_score - old_boost_score) < 3.0) || |
2296 (local_next_frame.intra_error < 200) | 2355 (local_next_frame.intra_error < 200) |
2297 ) { | 2356 ) { |
2298 break; | 2357 break; |
2299 } | 2358 } |
2300 | 2359 |
2301 old_boost_score = boost_score; | 2360 old_boost_score = boost_score; |
2302 | 2361 |
2303 // Get the next frame details | 2362 // Get the next frame details |
2304 if (EOF == input_stats(cpi, &local_next_frame)) | 2363 if (EOF == input_stats(cpi, &local_next_frame)) |
2305 break; | 2364 break; |
2306 } | 2365 } |
2307 | 2366 |
2308 // If there is tolerable prediction for at least the next 3 frames then | 2367 // If there is tolerable prediction for at least the next 3 frames then |
2309 // break out else discard this potential key frame and move on | 2368 // break out else discard this potential key frame and move on |
2310 if (boost_score > 30.0 && (i > 3)) | 2369 if (boost_score > 30.0 && (i > 3)) { |
2311 is_viable_kf = 1; | 2370 is_viable_kf = 1; |
2312 else { | 2371 } else { |
2313 // Reset the file position | 2372 // Reset the file position |
2314 reset_fpf_position(cpi, start_pos); | 2373 reset_fpf_position(cpi, start_pos); |
2315 | 2374 |
2316 is_viable_kf = 0; | 2375 is_viable_kf = 0; |
2317 } | 2376 } |
2318 } | 2377 } |
2319 | 2378 |
2320 return is_viable_kf; | 2379 return is_viable_kf; |
2321 } | 2380 } |
2322 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 2381 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2362 cpi->twopass.kf_group_error_left = 0; // Group modified error score. | 2421 cpi->twopass.kf_group_error_left = 0; // Group modified error score. |
2363 | 2422 |
2364 kf_mod_err = calculate_modified_err(cpi, this_frame); | 2423 kf_mod_err = calculate_modified_err(cpi, this_frame); |
2365 | 2424 |
2366 // find the next keyframe | 2425 // find the next keyframe |
2367 i = 0; | 2426 i = 0; |
2368 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) { | 2427 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) { |
2369 // Accumulate kf group error | 2428 // Accumulate kf group error |
2370 kf_group_err += calculate_modified_err(cpi, this_frame); | 2429 kf_group_err += calculate_modified_err(cpi, this_frame); |
2371 | 2430 |
2372 // These figures keep intra and coded error counts for all frames including
key frames in the group. | 2431 // These figures keep intra and coded error counts for all frames including |
2373 // The effect of the key frame itself can be subtracted out using the first_
frame data collected above | 2432 // key frames in the group. The effect of the key frame itself can be |
| 2433 // subtracted out using the first_frame data collected above. |
2374 kf_group_intra_err += this_frame->intra_error; | 2434 kf_group_intra_err += this_frame->intra_error; |
2375 kf_group_coded_err += this_frame->coded_error; | 2435 kf_group_coded_err += this_frame->coded_error; |
2376 | 2436 |
2377 // load a the next frame's stats | 2437 // load a the next frame's stats |
2378 last_frame = *this_frame; | 2438 last_frame = *this_frame; |
2379 input_stats(cpi, this_frame); | 2439 input_stats(cpi, this_frame); |
2380 | 2440 |
2381 // Provided that we are not at the end of the file... | 2441 // Provided that we are not at the end of the file... |
2382 if (cpi->oxcf.auto_key | 2442 if (cpi->oxcf.auto_key |
2383 && lookup_next_frame_stats(cpi, &next_frame) != EOF) { | 2443 && lookup_next_frame_stats(cpi, &next_frame) != EOF) { |
(...skipping 19 matching lines...) Expand all Loading... |
2403 loop_decay_rate, decay_accumulator)) | 2463 loop_decay_rate, decay_accumulator)) |
2404 break; | 2464 break; |
2405 | 2465 |
2406 // Step on to the next frame | 2466 // Step on to the next frame |
2407 cpi->twopass.frames_to_key++; | 2467 cpi->twopass.frames_to_key++; |
2408 | 2468 |
2409 // If we don't have a real key frame within the next two | 2469 // If we don't have a real key frame within the next two |
2410 // forcekeyframeevery intervals then break out of the loop. | 2470 // forcekeyframeevery intervals then break out of the loop. |
2411 if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency) | 2471 if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency) |
2412 break; | 2472 break; |
2413 } else | 2473 } else { |
2414 cpi->twopass.frames_to_key++; | 2474 cpi->twopass.frames_to_key++; |
2415 | 2475 } |
2416 i++; | 2476 i++; |
2417 } | 2477 } |
2418 | 2478 |
2419 // If there is a max kf interval set by the user we must obey it. | 2479 // If there is a max kf interval set by the user we must obey it. |
2420 // We already breakout of the loop above at 2x max. | 2480 // We already breakout of the loop above at 2x max. |
2421 // This code centers the extra kf if the actual natural | 2481 // This code centers the extra kf if the actual natural |
2422 // interval is between 1x and 2x | 2482 // interval is between 1x and 2x |
2423 if (cpi->oxcf.auto_key | 2483 if (cpi->oxcf.auto_key |
2424 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) { | 2484 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) { |
2425 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in; | 2485 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in; |
(...skipping 19 matching lines...) Expand all Loading... |
2445 kf_group_coded_err += tmp_frame.coded_error; | 2505 kf_group_coded_err += tmp_frame.coded_error; |
2446 | 2506 |
2447 // Load a the next frame's stats | 2507 // Load a the next frame's stats |
2448 input_stats(cpi, &tmp_frame); | 2508 input_stats(cpi, &tmp_frame); |
2449 } | 2509 } |
2450 | 2510 |
2451 // Reset to the start of the group | 2511 // Reset to the start of the group |
2452 reset_fpf_position(cpi, current_pos); | 2512 reset_fpf_position(cpi, current_pos); |
2453 | 2513 |
2454 cpi->next_key_frame_forced = 1; | 2514 cpi->next_key_frame_forced = 1; |
2455 } else | 2515 } else { |
2456 cpi->next_key_frame_forced = 0; | 2516 cpi->next_key_frame_forced = 0; |
2457 | 2517 } |
2458 // Special case for the last frame of the file | 2518 // Special case for the last frame of the file |
2459 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) { | 2519 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) { |
2460 // Accumulate kf group error | 2520 // Accumulate kf group error |
2461 kf_group_err += calculate_modified_err(cpi, this_frame); | 2521 kf_group_err += calculate_modified_err(cpi, this_frame); |
2462 | 2522 |
2463 // These figures keep intra and coded error counts for all frames including
key frames in the group. | 2523 // These figures keep intra and coded error counts for all frames including |
2464 // The effect of the key frame itself can be subtracted out using the first_
frame data collected above | 2524 // key frames in the group. The effect of the key frame itself can be |
| 2525 // subtracted out using the first_frame data collected above. |
2465 kf_group_intra_err += this_frame->intra_error; | 2526 kf_group_intra_err += this_frame->intra_error; |
2466 kf_group_coded_err += this_frame->coded_error; | 2527 kf_group_coded_err += this_frame->coded_error; |
2467 } | 2528 } |
2468 | 2529 |
2469 // Calculate the number of bits that should be assigned to the kf group. | 2530 // Calculate the number of bits that should be assigned to the kf group. |
2470 if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0))
{ | 2531 if ((cpi->twopass.bits_left > 0) && |
| 2532 (cpi->twopass.modified_error_left > 0.0)) { |
2471 // Max for a single normal frame (not key frame) | 2533 // Max for a single normal frame (not key frame) |
2472 int max_bits = frame_max_bits(cpi); | 2534 int max_bits = frame_max_bits(cpi); |
2473 | 2535 |
2474 // Maximum bits for the kf group | 2536 // Maximum bits for the kf group |
2475 int64_t max_grp_bits; | 2537 int64_t max_grp_bits; |
2476 | 2538 |
2477 // Default allocation based on bits left and relative | 2539 // Default allocation based on bits left and relative |
2478 // complexity of the section | 2540 // complexity of the section |
2479 cpi->twopass.kf_group_bits = (int64_t)(cpi->twopass.bits_left * | 2541 cpi->twopass.kf_group_bits = (int64_t)(cpi->twopass.bits_left * |
2480 (kf_group_err / | 2542 (kf_group_err / |
2481 cpi->twopass.modified_error_left)); | 2543 cpi->twopass.modified_error_left)); |
2482 | 2544 |
2483 // Clip based on maximum per frame rate defined by the user. | 2545 // Clip based on maximum per frame rate defined by the user. |
2484 max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key; | 2546 max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key; |
2485 if (cpi->twopass.kf_group_bits > max_grp_bits) | 2547 if (cpi->twopass.kf_group_bits > max_grp_bits) |
2486 cpi->twopass.kf_group_bits = max_grp_bits; | 2548 cpi->twopass.kf_group_bits = max_grp_bits; |
2487 } else | 2549 } else { |
2488 cpi->twopass.kf_group_bits = 0; | 2550 cpi->twopass.kf_group_bits = 0; |
2489 | 2551 } |
2490 // Reset the first pass file position | 2552 // Reset the first pass file position |
2491 reset_fpf_position(cpi, start_position); | 2553 reset_fpf_position(cpi, start_position); |
2492 | 2554 |
2493 // determine how big to make this keyframe based on how well the subsequent fr
ames use inter blocks | 2555 // Determine how big to make this keyframe based on how well the subsequent |
| 2556 // frames use inter blocks. |
2494 decay_accumulator = 1.0; | 2557 decay_accumulator = 1.0; |
2495 boost_score = 0.0; | 2558 boost_score = 0.0; |
2496 loop_decay_rate = 1.00; // Starting decay rate | 2559 loop_decay_rate = 1.00; // Starting decay rate |
2497 | 2560 |
2498 // Scan through the kf group collating various stats. | 2561 // Scan through the kf group collating various stats. |
2499 for (i = 0; i < cpi->twopass.frames_to_key; i++) { | 2562 for (i = 0; i < cpi->twopass.frames_to_key; i++) { |
2500 double r; | 2563 double r; |
2501 | 2564 |
2502 if (EOF == input_stats(cpi, &next_frame)) | 2565 if (EOF == input_stats(cpi, &next_frame)) |
2503 break; | 2566 break; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2556 | 2619 |
2557 // Work out how many bits to allocate for the key frame itself | 2620 // Work out how many bits to allocate for the key frame itself |
2558 if (1) { | 2621 if (1) { |
2559 int kf_boost = (int)boost_score; | 2622 int kf_boost = (int)boost_score; |
2560 int allocation_chunks; | 2623 int allocation_chunks; |
2561 int alt_kf_bits; | 2624 int alt_kf_bits; |
2562 | 2625 |
2563 if (kf_boost < (cpi->twopass.frames_to_key * 3)) | 2626 if (kf_boost < (cpi->twopass.frames_to_key * 3)) |
2564 kf_boost = (cpi->twopass.frames_to_key * 3); | 2627 kf_boost = (cpi->twopass.frames_to_key * 3); |
2565 | 2628 |
2566 if (kf_boost < 300) // Min KF boost | 2629 if (kf_boost < 300) // Min KF boost |
2567 kf_boost = 300; | 2630 kf_boost = 300; |
2568 | 2631 |
2569 // Make a note of baseline boost and the zero motion | 2632 // Make a note of baseline boost and the zero motion |
2570 // accumulator value for use elsewhere. | 2633 // accumulator value for use elsewhere. |
2571 cpi->kf_boost = kf_boost; | 2634 cpi->kf_boost = kf_boost; |
2572 cpi->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); | 2635 cpi->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
2573 | 2636 |
2574 // We do three calculations for kf size. | 2637 // We do three calculations for kf size. |
2575 // The first is based on the error score for the whole kf group. | 2638 // The first is based on the error score for the whole kf group. |
2576 // The second (optionaly) on the key frames own error if this is | 2639 // The second (optionaly) on the key frames own error if this is |
(...skipping 14 matching lines...) Expand all Loading... |
2591 ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost; | 2654 ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost; |
2592 } | 2655 } |
2593 | 2656 |
2594 // Prevent overflow | 2657 // Prevent overflow |
2595 if (kf_boost > 1028) { | 2658 if (kf_boost > 1028) { |
2596 int divisor = kf_boost >> 10; | 2659 int divisor = kf_boost >> 10; |
2597 kf_boost /= divisor; | 2660 kf_boost /= divisor; |
2598 allocation_chunks /= divisor; | 2661 allocation_chunks /= divisor; |
2599 } | 2662 } |
2600 | 2663 |
2601 cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->two
pass.kf_group_bits; | 2664 cpi->twopass.kf_group_bits = |
| 2665 (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits; |
2602 | 2666 |
2603 // Calculate the number of bits to be spent on the key frame | 2667 // Calculate the number of bits to be spent on the key frame |
2604 cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_gr
oup_bits / (double)allocation_chunks)); | 2668 cpi->twopass.kf_bits = |
| 2669 (int)((double)kf_boost * |
| 2670 ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks)); |
2605 | 2671 |
2606 // If the key frame is actually easier than the average for the | 2672 // If the key frame is actually easier than the average for the |
2607 // kf group (which does sometimes happen... eg a blank intro frame) | 2673 // kf group (which does sometimes happen... eg a blank intro frame) |
2608 // Then use an alternate calculation based on the kf error score | 2674 // Then use an alternate calculation based on the kf error score |
2609 // which should give a smaller key frame. | 2675 // which should give a smaller key frame. |
2610 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) { | 2676 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) { |
2611 double alt_kf_grp_bits = | 2677 double alt_kf_grp_bits = |
2612 ((double)cpi->twopass.bits_left * | 2678 ((double)cpi->twopass.bits_left * |
2613 (kf_mod_err * (double)cpi->twopass.frames_to_key) / | 2679 (kf_mod_err * (double)cpi->twopass.frames_to_key) / |
2614 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)); | 2680 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)); |
2615 | 2681 |
2616 alt_kf_bits = (int)((double)kf_boost * | 2682 alt_kf_bits = (int)((double)kf_boost * |
2617 (alt_kf_grp_bits / (double)allocation_chunks)); | 2683 (alt_kf_grp_bits / (double)allocation_chunks)); |
2618 | 2684 |
2619 if (cpi->twopass.kf_bits > alt_kf_bits) { | 2685 if (cpi->twopass.kf_bits > alt_kf_bits) { |
2620 cpi->twopass.kf_bits = alt_kf_bits; | 2686 cpi->twopass.kf_bits = alt_kf_bits; |
2621 } | 2687 } |
2622 } | 2688 } else { |
2623 // Else if it is much harder than other frames in the group make sure | 2689 // Else if it is much harder than other frames in the group make sure |
2624 // it at least receives an allocation in keeping with its relative | 2690 // it at least receives an allocation in keeping with its relative |
2625 // error score | 2691 // error score |
2626 else { | |
2627 alt_kf_bits = | 2692 alt_kf_bits = |
2628 (int)((double)cpi->twopass.bits_left * | 2693 (int)((double)cpi->twopass.bits_left * |
2629 (kf_mod_err / | 2694 (kf_mod_err / |
2630 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left))); | 2695 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left))); |
2631 | 2696 |
2632 if (alt_kf_bits > cpi->twopass.kf_bits) { | 2697 if (alt_kf_bits > cpi->twopass.kf_bits) { |
2633 cpi->twopass.kf_bits = alt_kf_bits; | 2698 cpi->twopass.kf_bits = alt_kf_bits; |
2634 } | 2699 } |
2635 } | 2700 } |
2636 | 2701 |
2637 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits; | 2702 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits; |
2638 // Add in the minimum frame allowance | 2703 // Add in the minimum frame allowance |
2639 cpi->twopass.kf_bits += cpi->min_frame_bandwidth; | 2704 cpi->twopass.kf_bits += cpi->min_frame_bandwidth; |
2640 | 2705 |
2641 // Peer frame bit target for this frame | 2706 // Peer frame bit target for this frame |
2642 cpi->per_frame_bandwidth = cpi->twopass.kf_bits; | 2707 cpi->per_frame_bandwidth = cpi->twopass.kf_bits; |
2643 // Convert to a per second bitrate | 2708 // Convert to a per second bitrate |
2644 cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * | 2709 cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * |
2645 cpi->output_framerate); | 2710 cpi->output_framerate); |
2646 } | 2711 } |
2647 | 2712 |
2648 // Note the total error score of the kf group minus the key frame itself | 2713 // Note the total error score of the kf group minus the key frame itself |
2649 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2714 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
2650 | 2715 |
2651 // Adjust the count of total modified error left. | 2716 // Adjust the count of total modified error left. |
2652 // The count of bits left is adjusted elsewhere based on real coded frame size
s | 2717 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2718 // sizes. |
2653 cpi->twopass.modified_error_left -= kf_group_err; | 2719 cpi->twopass.modified_error_left -= kf_group_err; |
2654 } | 2720 } |
OLD | NEW |