| 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 <limits.h> | 11 #include <limits.h> |
| 12 #include <math.h> | 12 #include <math.h> |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 | 14 |
| 15 #include "./vpx_scale_rtcd.h" | 15 #include "./vpx_scale_rtcd.h" |
| 16 | 16 |
| 17 #include "vpx_mem/vpx_mem.h" | 17 #include "vpx_mem/vpx_mem.h" |
| 18 #include "vpx_scale/vpx_scale.h" | 18 #include "vpx_scale/vpx_scale.h" |
| 19 #include "vpx_scale/yv12config.h" | 19 #include "vpx_scale/yv12config.h" |
| 20 | 20 |
| 21 #include "vp9/common/vp9_entropymv.h" | 21 #include "vp9/common/vp9_entropymv.h" |
| 22 #include "vp9/common/vp9_quant_common.h" | 22 #include "vp9/common/vp9_quant_common.h" |
| 23 #include "vp9/common/vp9_reconinter.h" // setup_dst_planes() | 23 #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes() |
| 24 #include "vp9/common/vp9_systemdependent.h" | 24 #include "vp9/common/vp9_systemdependent.h" |
| 25 | 25 |
| 26 #include "vp9/encoder/vp9_aq_variance.h" |
| 26 #include "vp9/encoder/vp9_block.h" | 27 #include "vp9/encoder/vp9_block.h" |
| 27 #include "vp9/encoder/vp9_encodeframe.h" | 28 #include "vp9/encoder/vp9_encodeframe.h" |
| 28 #include "vp9/encoder/vp9_encodemb.h" | 29 #include "vp9/encoder/vp9_encodemb.h" |
| 29 #include "vp9/encoder/vp9_encodemv.h" | 30 #include "vp9/encoder/vp9_encodemv.h" |
| 30 #include "vp9/encoder/vp9_extend.h" | 31 #include "vp9/encoder/vp9_extend.h" |
| 31 #include "vp9/encoder/vp9_firstpass.h" | 32 #include "vp9/encoder/vp9_firstpass.h" |
| 32 #include "vp9/encoder/vp9_mcomp.h" | 33 #include "vp9/encoder/vp9_mcomp.h" |
| 33 #include "vp9/encoder/vp9_onyx_int.h" | 34 #include "vp9/encoder/vp9_onyx_int.h" |
| 34 #include "vp9/encoder/vp9_quantize.h" | 35 #include "vp9/encoder/vp9_quantize.h" |
| 35 #include "vp9/encoder/vp9_ratectrl.h" | 36 #include "vp9/encoder/vp9_ratectrl.h" |
| 36 #include "vp9/encoder/vp9_rdopt.h" | 37 #include "vp9/encoder/vp9_rdopt.h" |
| 37 #include "vp9/encoder/vp9_vaq.h" | |
| 38 #include "vp9/encoder/vp9_variance.h" | 38 #include "vp9/encoder/vp9_variance.h" |
| 39 | 39 |
| 40 #define OUTPUT_FPF 0 | 40 #define OUTPUT_FPF 0 |
| 41 | 41 |
| 42 #define IIFACTOR 12.5 | 42 #define IIFACTOR 12.5 |
| 43 #define IIKFACTOR1 12.5 | 43 #define IIKFACTOR1 12.5 |
| 44 #define IIKFACTOR2 15.0 | 44 #define IIKFACTOR2 15.0 |
| 45 #define RMAX 512.0 | 45 #define RMAX 512.0 |
| 46 #define GF_RMAX 96.0 | 46 #define GF_RMAX 96.0 |
| 47 #define ERR_DIVISOR 150.0 | 47 #define ERR_DIVISOR 150.0 |
| 48 #define MIN_DECAY_FACTOR 0.1 | 48 #define MIN_DECAY_FACTOR 0.1 |
| 49 | 49 |
| 50 #define KF_MB_INTRA_MIN 150 | 50 #define KF_MB_INTRA_MIN 150 |
| 51 #define GF_MB_INTRA_MIN 100 | 51 #define GF_MB_INTRA_MIN 100 |
| 52 | 52 |
| 53 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 53 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 54 | 54 |
| 55 #define MIN_KF_BOOST 300 | 55 #define MIN_KF_BOOST 300 |
| 56 | 56 |
| 57 #define DISABLE_RC_LONG_TERM_MEM 0 | 57 #if CONFIG_MULTIPLE_ARF |
| 58 // Set MIN_GF_INTERVAL to 1 for the full decomposition. |
| 59 #define MIN_GF_INTERVAL 2 |
| 60 #else |
| 61 #define MIN_GF_INTERVAL 4 |
| 62 #endif |
| 63 |
| 64 #define DISABLE_RC_LONG_TERM_MEM |
| 58 | 65 |
| 59 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | 66 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { |
| 60 YV12_BUFFER_CONFIG temp = *a; | 67 YV12_BUFFER_CONFIG temp = *a; |
| 61 *a = *b; | 68 *a = *b; |
| 62 *b = temp; | 69 *b = temp; |
| 63 } | 70 } |
| 64 | 71 |
| 65 static int select_cq_level(int qindex) { | |
| 66 int ret_val = QINDEX_RANGE - 1; | |
| 67 int i; | |
| 68 | |
| 69 double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0; | |
| 70 | |
| 71 for (i = 0; i < QINDEX_RANGE; ++i) { | |
| 72 if (target_q <= vp9_convert_qindex_to_q(i)) { | |
| 73 ret_val = i; | |
| 74 break; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 return ret_val; | |
| 79 } | |
| 80 | |
| 81 static int gfboost_qadjust(int qindex) { | 72 static int gfboost_qadjust(int qindex) { |
| 82 const double q = vp9_convert_qindex_to_q(qindex); | 73 const double q = vp9_convert_qindex_to_q(qindex); |
| 83 return (int)((0.00000828 * q * q * q) + | 74 return (int)((0.00000828 * q * q * q) + |
| 84 (-0.0055 * q * q) + | 75 (-0.0055 * q * q) + |
| 85 (1.32 * q) + 79.3); | 76 (1.32 * q) + 79.3); |
| 86 } | 77 } |
| 87 | 78 |
| 88 static int kfboost_qadjust(int qindex) { | |
| 89 const double q = vp9_convert_qindex_to_q(qindex); | |
| 90 return (int)((0.00000973 * q * q * q) + | |
| 91 (-0.00613 * q * q) + | |
| 92 (1.316 * q) + 121.2); | |
| 93 } | |
| 94 | |
| 95 // Resets the first pass file to the given position using a relative seek from | 79 // Resets the first pass file to the given position using a relative seek from |
| 96 // the current position. | 80 // the current position. |
| 97 static void reset_fpf_position(struct twopass_rc *p, | 81 static void reset_fpf_position(struct twopass_rc *p, |
| 98 FIRSTPASS_STATS *position) { | 82 const FIRSTPASS_STATS *position) { |
| 99 p->stats_in = position; | 83 p->stats_in = position; |
| 100 } | 84 } |
| 101 | 85 |
| 102 static int lookup_next_frame_stats(const struct twopass_rc *p, | 86 static int lookup_next_frame_stats(const struct twopass_rc *p, |
| 103 FIRSTPASS_STATS *next_frame) { | 87 FIRSTPASS_STATS *next_frame) { |
| 104 if (p->stats_in >= p->stats_in_end) | 88 if (p->stats_in >= p->stats_in_end) |
| 105 return EOF; | 89 return EOF; |
| 106 | 90 |
| 107 *next_frame = *p->stats_in; | 91 *next_frame = *p->stats_in; |
| 108 return 1; | 92 return 1; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 section->MVr = 0.0; | 174 section->MVr = 0.0; |
| 191 section->mvr_abs = 0.0; | 175 section->mvr_abs = 0.0; |
| 192 section->MVc = 0.0; | 176 section->MVc = 0.0; |
| 193 section->mvc_abs = 0.0; | 177 section->mvc_abs = 0.0; |
| 194 section->MVrv = 0.0; | 178 section->MVrv = 0.0; |
| 195 section->MVcv = 0.0; | 179 section->MVcv = 0.0; |
| 196 section->mv_in_out_count = 0.0; | 180 section->mv_in_out_count = 0.0; |
| 197 section->new_mv_count = 0.0; | 181 section->new_mv_count = 0.0; |
| 198 section->count = 0.0; | 182 section->count = 0.0; |
| 199 section->duration = 1.0; | 183 section->duration = 1.0; |
| 184 section->spatial_layer_id = 0; |
| 200 } | 185 } |
| 201 | 186 |
| 202 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) { | 187 static void accumulate_stats(FIRSTPASS_STATS *section, |
| 188 const FIRSTPASS_STATS *frame) { |
| 203 section->frame += frame->frame; | 189 section->frame += frame->frame; |
| 190 section->spatial_layer_id = frame->spatial_layer_id; |
| 204 section->intra_error += frame->intra_error; | 191 section->intra_error += frame->intra_error; |
| 205 section->coded_error += frame->coded_error; | 192 section->coded_error += frame->coded_error; |
| 206 section->sr_coded_error += frame->sr_coded_error; | 193 section->sr_coded_error += frame->sr_coded_error; |
| 207 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err; | 194 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err; |
| 208 section->pcnt_inter += frame->pcnt_inter; | 195 section->pcnt_inter += frame->pcnt_inter; |
| 209 section->pcnt_motion += frame->pcnt_motion; | 196 section->pcnt_motion += frame->pcnt_motion; |
| 210 section->pcnt_second_ref += frame->pcnt_second_ref; | 197 section->pcnt_second_ref += frame->pcnt_second_ref; |
| 211 section->pcnt_neutral += frame->pcnt_neutral; | 198 section->pcnt_neutral += frame->pcnt_neutral; |
| 212 section->MVr += frame->MVr; | 199 section->MVr += frame->MVr; |
| 213 section->mvr_abs += frame->mvr_abs; | 200 section->mvr_abs += frame->mvr_abs; |
| 214 section->MVc += frame->MVc; | 201 section->MVc += frame->MVc; |
| 215 section->mvc_abs += frame->mvc_abs; | 202 section->mvc_abs += frame->mvc_abs; |
| 216 section->MVrv += frame->MVrv; | 203 section->MVrv += frame->MVrv; |
| 217 section->MVcv += frame->MVcv; | 204 section->MVcv += frame->MVcv; |
| 218 section->mv_in_out_count += frame->mv_in_out_count; | 205 section->mv_in_out_count += frame->mv_in_out_count; |
| 219 section->new_mv_count += frame->new_mv_count; | 206 section->new_mv_count += frame->new_mv_count; |
| 220 section->count += frame->count; | 207 section->count += frame->count; |
| 221 section->duration += frame->duration; | 208 section->duration += frame->duration; |
| 222 } | 209 } |
| 223 | 210 |
| 224 static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) { | 211 static void subtract_stats(FIRSTPASS_STATS *section, |
| 212 const FIRSTPASS_STATS *frame) { |
| 225 section->frame -= frame->frame; | 213 section->frame -= frame->frame; |
| 226 section->intra_error -= frame->intra_error; | 214 section->intra_error -= frame->intra_error; |
| 227 section->coded_error -= frame->coded_error; | 215 section->coded_error -= frame->coded_error; |
| 228 section->sr_coded_error -= frame->sr_coded_error; | 216 section->sr_coded_error -= frame->sr_coded_error; |
| 229 section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err; | 217 section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err; |
| 230 section->pcnt_inter -= frame->pcnt_inter; | 218 section->pcnt_inter -= frame->pcnt_inter; |
| 231 section->pcnt_motion -= frame->pcnt_motion; | 219 section->pcnt_motion -= frame->pcnt_motion; |
| 232 section->pcnt_second_ref -= frame->pcnt_second_ref; | 220 section->pcnt_second_ref -= frame->pcnt_second_ref; |
| 233 section->pcnt_neutral -= frame->pcnt_neutral; | 221 section->pcnt_neutral -= frame->pcnt_neutral; |
| 234 section->MVr -= frame->MVr; | 222 section->MVr -= frame->MVr; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 262 section->MVrv /= section->count; | 250 section->MVrv /= section->count; |
| 263 section->MVcv /= section->count; | 251 section->MVcv /= section->count; |
| 264 section->mv_in_out_count /= section->count; | 252 section->mv_in_out_count /= section->count; |
| 265 section->duration /= section->count; | 253 section->duration /= section->count; |
| 266 } | 254 } |
| 267 | 255 |
| 268 // Calculate a modified Error used in distributing bits between easier and | 256 // Calculate a modified Error used in distributing bits between easier and |
| 269 // harder frames. | 257 // harder frames. |
| 270 static double calculate_modified_err(const VP9_COMP *cpi, | 258 static double calculate_modified_err(const VP9_COMP *cpi, |
| 271 const FIRSTPASS_STATS *this_frame) { | 259 const FIRSTPASS_STATS *this_frame) { |
| 272 const struct twopass_rc *const twopass = &cpi->twopass; | 260 const struct twopass_rc *twopass = &cpi->twopass; |
| 273 const FIRSTPASS_STATS *const stats = &twopass->total_stats; | 261 const SVC *const svc = &cpi->svc; |
| 274 const double av_err = stats->ssim_weighted_pred_err / stats->count; | 262 const FIRSTPASS_STATS *stats; |
| 275 double modified_error = av_err * pow(this_frame->ssim_weighted_pred_err / | 263 double av_err; |
| 276 DOUBLE_DIVIDE_CHECK(av_err), | 264 double modified_error; |
| 277 cpi->oxcf.two_pass_vbrbias / 100.0); | 265 |
| 266 if (svc->number_spatial_layers > 1 && |
| 267 svc->number_temporal_layers == 1) { |
| 268 twopass = &svc->layer_context[svc->spatial_layer_id].twopass; |
| 269 } |
| 270 |
| 271 stats = &twopass->total_stats; |
| 272 av_err = stats->ssim_weighted_pred_err / stats->count; |
| 273 modified_error = av_err * pow(this_frame->ssim_weighted_pred_err / |
| 274 DOUBLE_DIVIDE_CHECK(av_err), |
| 275 cpi->oxcf.two_pass_vbrbias / 100.0); |
| 278 | 276 |
| 279 return fclamp(modified_error, | 277 return fclamp(modified_error, |
| 280 twopass->modified_error_min, twopass->modified_error_max); | 278 twopass->modified_error_min, twopass->modified_error_max); |
| 281 } | 279 } |
| 282 | 280 |
| 283 static const double weight_table[256] = { | 281 static const double weight_table[256] = { |
| 284 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | 282 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
| 285 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | 283 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
| 286 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | 284 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
| 287 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, | 285 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 const uint8_t *pixel = row; | 329 const uint8_t *pixel = row; |
| 332 for (j = 0; j < w; ++j) | 330 for (j = 0; j < w; ++j) |
| 333 sum += weight_table[*pixel++]; | 331 sum += weight_table[*pixel++]; |
| 334 row += buf->y_stride; | 332 row += buf->y_stride; |
| 335 } | 333 } |
| 336 | 334 |
| 337 return MAX(0.1, sum / (w * h)); | 335 return MAX(0.1, sum / (w * h)); |
| 338 } | 336 } |
| 339 | 337 |
| 340 // This function returns the maximum target rate per frame. | 338 // This function returns the maximum target rate per frame. |
| 341 static int frame_max_bits(const VP9_COMP *cpi) { | 339 static int frame_max_bits(const RATE_CONTROL *rc, const VP9_CONFIG *oxcf) { |
| 342 int64_t max_bits = | 340 int64_t max_bits = ((int64_t)rc->av_per_frame_bandwidth * |
| 343 ((int64_t)cpi->rc.av_per_frame_bandwidth * | 341 (int64_t)oxcf->two_pass_vbrmax_section) / 100; |
| 344 (int64_t)cpi->oxcf.two_pass_vbrmax_section) / 100; | |
| 345 | |
| 346 if (max_bits < 0) | 342 if (max_bits < 0) |
| 347 max_bits = 0; | 343 max_bits = 0; |
| 348 else if (max_bits > cpi->rc.max_frame_bandwidth) | 344 else if (max_bits > rc->max_frame_bandwidth) |
| 349 max_bits = cpi->rc.max_frame_bandwidth; | 345 max_bits = rc->max_frame_bandwidth; |
| 350 | 346 |
| 351 return (int)max_bits; | 347 return (int)max_bits; |
| 352 } | 348 } |
| 353 | 349 |
| 354 void vp9_init_first_pass(VP9_COMP *cpi) { | 350 void vp9_init_first_pass(VP9_COMP *cpi) { |
| 355 zero_stats(&cpi->twopass.total_stats); | 351 zero_stats(&cpi->twopass.total_stats); |
| 356 } | 352 } |
| 357 | 353 |
| 358 void vp9_end_first_pass(VP9_COMP *cpi) { | 354 void vp9_end_first_pass(VP9_COMP *cpi) { |
| 359 output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list); | 355 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { |
| 356 int i; |
| 357 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) { |
| 358 output_stats(&cpi->svc.layer_context[i].twopass.total_stats, |
| 359 cpi->output_pkt_list); |
| 360 } |
| 361 } else { |
| 362 output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list); |
| 363 } |
| 360 } | 364 } |
| 361 | 365 |
| 362 static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) { | 366 static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) { |
| 363 switch (bsize) { | 367 switch (bsize) { |
| 364 case BLOCK_8X8: | 368 case BLOCK_8X8: |
| 365 return vp9_mse8x8; | 369 return vp9_mse8x8; |
| 366 case BLOCK_16X8: | 370 case BLOCK_16X8: |
| 367 return vp9_mse16x8; | 371 return vp9_mse16x8; |
| 368 case BLOCK_8X16: | 372 case BLOCK_8X16: |
| 369 return vp9_mse8x16; | 373 return vp9_mse8x16; |
| 370 default: | 374 default: |
| 371 return vp9_mse16x16; | 375 return vp9_mse16x16; |
| 372 } | 376 } |
| 373 } | 377 } |
| 374 | 378 |
| 375 static unsigned int zz_motion_search(const MACROBLOCK *x) { | 379 static unsigned int zz_motion_search(const MACROBLOCK *x) { |
| 376 const MACROBLOCKD *const xd = &x->e_mbd; | 380 const MACROBLOCKD *const xd = &x->e_mbd; |
| 377 const uint8_t *const src = x->plane[0].src.buf; | 381 const uint8_t *const src = x->plane[0].src.buf; |
| 378 const int src_stride = x->plane[0].src.stride; | 382 const int src_stride = x->plane[0].src.stride; |
| 379 const uint8_t *const ref = xd->plane[0].pre[0].buf; | 383 const uint8_t *const ref = xd->plane[0].pre[0].buf; |
| 380 const int ref_stride = xd->plane[0].pre[0].stride; | 384 const int ref_stride = xd->plane[0].pre[0].stride; |
| 381 unsigned int sse; | 385 unsigned int sse; |
| 382 vp9_variance_fn_t fn = get_block_variance_fn(xd->mi_8x8[0]->mbmi.sb_type); | 386 vp9_variance_fn_t fn = get_block_variance_fn(xd->mi[0]->mbmi.sb_type); |
| 383 fn(src, src_stride, ref, ref_stride, &sse); | 387 fn(src, src_stride, ref, ref_stride, &sse); |
| 384 return sse; | 388 return sse; |
| 385 } | 389 } |
| 386 | 390 |
| 387 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, | 391 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
| 388 const MV *ref_mv, MV *best_mv, | 392 const MV *ref_mv, MV *best_mv, |
| 389 int *best_motion_err) { | 393 int *best_motion_err) { |
| 390 MACROBLOCKD *const xd = &x->e_mbd; | 394 MACROBLOCKD *const xd = &x->e_mbd; |
| 391 MV tmp_mv = {0, 0}; | 395 MV tmp_mv = {0, 0}; |
| 392 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3}; | 396 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3}; |
| 393 int num00, tmp_err, n, sr = 0; | 397 int num00, tmp_err, n, sr = 0; |
| 394 int step_param = 3; | 398 int step_param = 3; |
| 395 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; | 399 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; |
| 396 const BLOCK_SIZE bsize = xd->mi_8x8[0]->mbmi.sb_type; | 400 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; |
| 397 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize]; | 401 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize]; |
| 398 int new_mv_mode_penalty = 256; | 402 int new_mv_mode_penalty = 256; |
| 399 const int quart_frm = MIN(cpi->common.width, cpi->common.height); | 403 const int quart_frm = MIN(cpi->common.width, cpi->common.height); |
| 400 | 404 |
| 401 // Refine the motion search range according to the frame dimension | 405 // Refine the motion search range according to the frame dimension |
| 402 // for first pass test. | 406 // for first pass test. |
| 403 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) | 407 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) |
| 404 ++sr; | 408 ++sr; |
| 405 | 409 |
| 406 step_param += sr; | 410 step_param += sr; |
| 407 further_steps -= sr; | 411 further_steps -= sr; |
| 408 | 412 |
| 409 // Override the default variance function to use MSE. | 413 // Override the default variance function to use MSE. |
| 410 v_fn_ptr.vf = get_block_variance_fn(bsize); | 414 v_fn_ptr.vf = get_block_variance_fn(bsize); |
| 411 | 415 |
| 412 // Center the initial step/diamond search on best mv. | 416 // Center the initial step/diamond search on best mv. |
| 413 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, | 417 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, |
| 414 step_param, | 418 step_param, |
| 415 x->sadperbit16, &num00, &v_fn_ptr, | 419 x->sadperbit16, &num00, &v_fn_ptr, |
| 416 x->nmvjointcost, | 420 x->nmvjointcost, |
| 417 x->mvcost, ref_mv); | 421 x->mvcost, ref_mv); |
| 422 if (tmp_err < INT_MAX) |
| 423 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1); |
| 418 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 424 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 419 tmp_err += new_mv_mode_penalty; | 425 tmp_err += new_mv_mode_penalty; |
| 420 | 426 |
| 421 if (tmp_err < *best_motion_err) { | 427 if (tmp_err < *best_motion_err) { |
| 422 *best_motion_err = tmp_err; | 428 *best_motion_err = tmp_err; |
| 423 best_mv->row = tmp_mv.row; | 429 best_mv->row = tmp_mv.row; |
| 424 best_mv->col = tmp_mv.col; | 430 best_mv->col = tmp_mv.col; |
| 425 } | 431 } |
| 426 | 432 |
| 427 // Carry out further step/diamond searches as necessary. | 433 // Carry out further step/diamond searches as necessary. |
| 428 n = num00; | 434 n = num00; |
| 429 num00 = 0; | 435 num00 = 0; |
| 430 | 436 |
| 431 while (n < further_steps) { | 437 while (n < further_steps) { |
| 432 ++n; | 438 ++n; |
| 433 | 439 |
| 434 if (num00) { | 440 if (num00) { |
| 435 --num00; | 441 --num00; |
| 436 } else { | 442 } else { |
| 437 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, | 443 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, |
| 438 step_param + n, x->sadperbit16, | 444 step_param + n, x->sadperbit16, |
| 439 &num00, &v_fn_ptr, | 445 &num00, &v_fn_ptr, |
| 440 x->nmvjointcost, | 446 x->nmvjointcost, |
| 441 x->mvcost, ref_mv); | 447 x->mvcost, ref_mv); |
| 448 if (tmp_err < INT_MAX) |
| 449 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1); |
| 442 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 450 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 443 tmp_err += new_mv_mode_penalty; | 451 tmp_err += new_mv_mode_penalty; |
| 444 | 452 |
| 445 if (tmp_err < *best_motion_err) { | 453 if (tmp_err < *best_motion_err) { |
| 446 *best_motion_err = tmp_err; | 454 *best_motion_err = tmp_err; |
| 447 best_mv->row = tmp_mv.row; | 455 best_mv->row = tmp_mv.row; |
| 448 best_mv->col = tmp_mv.col; | 456 best_mv->col = tmp_mv.col; |
| 449 } | 457 } |
| 450 } | 458 } |
| 451 } | 459 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 467 VP9_COMMON *const cm = &cpi->common; | 475 VP9_COMMON *const cm = &cpi->common; |
| 468 MACROBLOCKD *const xd = &x->e_mbd; | 476 MACROBLOCKD *const xd = &x->e_mbd; |
| 469 TileInfo tile; | 477 TileInfo tile; |
| 470 struct macroblock_plane *const p = x->plane; | 478 struct macroblock_plane *const p = x->plane; |
| 471 struct macroblockd_plane *const pd = xd->plane; | 479 struct macroblockd_plane *const pd = xd->plane; |
| 472 const PICK_MODE_CONTEXT *ctx = &x->sb64_context; | 480 const PICK_MODE_CONTEXT *ctx = &x->sb64_context; |
| 473 int i; | 481 int i; |
| 474 | 482 |
| 475 int recon_yoffset, recon_uvoffset; | 483 int recon_yoffset, recon_uvoffset; |
| 476 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); | 484 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 477 YV12_BUFFER_CONFIG *const gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); | 485 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 478 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); | 486 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); |
| 479 const int recon_y_stride = lst_yv12->y_stride; | 487 int recon_y_stride = lst_yv12->y_stride; |
| 480 const int recon_uv_stride = lst_yv12->uv_stride; | 488 int recon_uv_stride = lst_yv12->uv_stride; |
| 481 const int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height); | 489 int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height); |
| 482 int64_t intra_error = 0; | 490 int64_t intra_error = 0; |
| 483 int64_t coded_error = 0; | 491 int64_t coded_error = 0; |
| 484 int64_t sr_coded_error = 0; | 492 int64_t sr_coded_error = 0; |
| 485 | 493 |
| 486 int sum_mvr = 0, sum_mvc = 0; | 494 int sum_mvr = 0, sum_mvc = 0; |
| 487 int sum_mvr_abs = 0, sum_mvc_abs = 0; | 495 int sum_mvr_abs = 0, sum_mvc_abs = 0; |
| 488 int64_t sum_mvrs = 0, sum_mvcs = 0; | 496 int64_t sum_mvrs = 0, sum_mvcs = 0; |
| 489 int mvcount = 0; | 497 int mvcount = 0; |
| 490 int intercount = 0; | 498 int intercount = 0; |
| 491 int second_ref_count = 0; | 499 int second_ref_count = 0; |
| 492 int intrapenalty = 256; | 500 int intrapenalty = 256; |
| 493 int neutral_count = 0; | 501 int neutral_count = 0; |
| 494 int new_mv_count = 0; | 502 int new_mv_count = 0; |
| 495 int sum_in_vectors = 0; | 503 int sum_in_vectors = 0; |
| 496 uint32_t lastmv_as_int = 0; | 504 uint32_t lastmv_as_int = 0; |
| 497 struct twopass_rc *const twopass = &cpi->twopass; | 505 struct twopass_rc *twopass = &cpi->twopass; |
| 498 const MV zero_mv = {0, 0}; | 506 const MV zero_mv = {0, 0}; |
| 507 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; |
| 499 | 508 |
| 500 vp9_clear_system_state(); | 509 vp9_clear_system_state(); |
| 501 | 510 |
| 511 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { |
| 512 MV_REFERENCE_FRAME ref_frame = LAST_FRAME; |
| 513 const YV12_BUFFER_CONFIG *scaled_ref_buf = NULL; |
| 514 twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass; |
| 515 |
| 516 vp9_scale_references(cpi); |
| 517 |
| 518 // Use either last frame or alt frame for motion search. |
| 519 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { |
| 520 scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); |
| 521 ref_frame = LAST_FRAME; |
| 522 } else if (cpi->ref_frame_flags & VP9_ALT_FLAG) { |
| 523 scaled_ref_buf = vp9_get_scaled_ref_frame(cpi, ALTREF_FRAME); |
| 524 ref_frame = ALTREF_FRAME; |
| 525 } |
| 526 |
| 527 if (scaled_ref_buf != NULL) { |
| 528 // Update the stride since we are using scaled reference buffer |
| 529 first_ref_buf = scaled_ref_buf; |
| 530 recon_y_stride = first_ref_buf->y_stride; |
| 531 recon_uv_stride = first_ref_buf->uv_stride; |
| 532 uv_mb_height = 16 >> (first_ref_buf->y_height > first_ref_buf->uv_height); |
| 533 } |
| 534 |
| 535 // Disable golden frame for svc first pass for now. |
| 536 gld_yv12 = NULL; |
| 537 set_ref_ptrs(cm, xd, ref_frame, NONE); |
| 538 } |
| 539 |
| 502 vp9_setup_src_planes(x, cpi->Source, 0, 0); | 540 vp9_setup_src_planes(x, cpi->Source, 0, 0); |
| 503 setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL); | 541 vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL); |
| 504 setup_dst_planes(xd, new_yv12, 0, 0); | 542 vp9_setup_dst_planes(xd, new_yv12, 0, 0); |
| 505 | 543 |
| 506 xd->mi_8x8 = cm->mi_grid_visible; | 544 xd->mi = cm->mi_grid_visible; |
| 507 xd->mi_8x8[0] = cm->mi; | 545 xd->mi[0] = cm->mi; |
| 508 | 546 |
| 509 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); | 547 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); |
| 510 | 548 |
| 511 vp9_frame_init_quantizer(cpi); | 549 vp9_frame_init_quantizer(cpi); |
| 512 | 550 |
| 513 for (i = 0; i < MAX_MB_PLANE; ++i) { | 551 for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 514 p[i].coeff = ctx->coeff_pbuf[i][1]; | 552 p[i].coeff = ctx->coeff_pbuf[i][1]; |
| 515 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; | 553 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; |
| 516 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; | 554 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; |
| 517 p[i].eobs = ctx->eobs_pbuf[i][1]; | 555 p[i].eobs = ctx->eobs_pbuf[i][1]; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 545 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); | 583 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
| 546 double error_weight = 1.0; | 584 double error_weight = 1.0; |
| 547 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); | 585 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); |
| 548 | 586 |
| 549 vp9_clear_system_state(); | 587 vp9_clear_system_state(); |
| 550 | 588 |
| 551 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; | 589 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; |
| 552 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; | 590 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; |
| 553 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; | 591 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; |
| 554 xd->left_available = (mb_col != 0); | 592 xd->left_available = (mb_col != 0); |
| 555 xd->mi_8x8[0]->mbmi.sb_type = bsize; | 593 xd->mi[0]->mbmi.sb_type = bsize; |
| 556 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; | 594 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
| 557 set_mi_row_col(xd, &tile, | 595 set_mi_row_col(xd, &tile, |
| 558 mb_row << 1, num_8x8_blocks_high_lookup[bsize], | 596 mb_row << 1, num_8x8_blocks_high_lookup[bsize], |
| 559 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], | 597 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], |
| 560 cm->mi_rows, cm->mi_cols); | 598 cm->mi_rows, cm->mi_cols); |
| 561 | 599 |
| 562 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 600 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 563 const int energy = vp9_block_energy(cpi, x, bsize); | 601 const int energy = vp9_block_energy(cpi, x, bsize); |
| 564 error_weight = vp9_vaq_inv_q_ratio(energy); | 602 error_weight = vp9_vaq_inv_q_ratio(energy); |
| 565 } | 603 } |
| 566 | 604 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 586 // Set up limit values for motion vectors to prevent them extending | 624 // Set up limit values for motion vectors to prevent them extending |
| 587 // outside the UMV borders. | 625 // outside the UMV borders. |
| 588 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); | 626 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
| 589 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; | 627 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; |
| 590 | 628 |
| 591 // Other than for the first frame do a motion search. | 629 // Other than for the first frame do a motion search. |
| 592 if (cm->current_video_frame > 0) { | 630 if (cm->current_video_frame > 0) { |
| 593 int tmp_err, motion_error; | 631 int tmp_err, motion_error; |
| 594 int_mv mv, tmp_mv; | 632 int_mv mv, tmp_mv; |
| 595 | 633 |
| 596 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; | 634 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; |
| 597 motion_error = zz_motion_search(x); | 635 motion_error = zz_motion_search(x); |
| 598 // Assume 0,0 motion with no mv overhead. | 636 // Assume 0,0 motion with no mv overhead. |
| 599 mv.as_int = tmp_mv.as_int = 0; | 637 mv.as_int = tmp_mv.as_int = 0; |
| 600 | 638 |
| 601 // Test last reference frame using the previous best mv as the | 639 // Test last reference frame using the previous best mv as the |
| 602 // starting point (best reference) for the search. | 640 // starting point (best reference) for the search. |
| 603 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, | 641 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, |
| 604 &motion_error); | 642 &motion_error); |
| 605 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 643 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 606 vp9_clear_system_state(); | 644 vp9_clear_system_state(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 618 tmp_err = (int)(tmp_err * error_weight); | 656 tmp_err = (int)(tmp_err * error_weight); |
| 619 } | 657 } |
| 620 | 658 |
| 621 if (tmp_err < motion_error) { | 659 if (tmp_err < motion_error) { |
| 622 motion_error = tmp_err; | 660 motion_error = tmp_err; |
| 623 mv.as_int = tmp_mv.as_int; | 661 mv.as_int = tmp_mv.as_int; |
| 624 } | 662 } |
| 625 } | 663 } |
| 626 | 664 |
| 627 // Search in an older reference frame. | 665 // Search in an older reference frame. |
| 628 if (cm->current_video_frame > 1) { | 666 if (cm->current_video_frame > 1 && gld_yv12 != NULL) { |
| 629 // Assume 0,0 motion with no mv overhead. | 667 // Assume 0,0 motion with no mv overhead. |
| 630 int gf_motion_error; | 668 int gf_motion_error; |
| 631 | 669 |
| 632 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; | 670 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; |
| 633 gf_motion_error = zz_motion_search(x); | 671 gf_motion_error = zz_motion_search(x); |
| 634 | 672 |
| 635 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, | 673 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, |
| 636 &gf_motion_error); | 674 &gf_motion_error); |
| 637 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 675 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 638 vp9_clear_system_state(); | 676 vp9_clear_system_state(); |
| 639 gf_motion_error = (int)(gf_motion_error * error_weight); | 677 gf_motion_error = (int)(gf_motion_error * error_weight); |
| 640 } | 678 } |
| 641 | 679 |
| 642 if (gf_motion_error < motion_error && gf_motion_error < this_error) | 680 if (gf_motion_error < motion_error && gf_motion_error < this_error) |
| 643 ++second_ref_count; | 681 ++second_ref_count; |
| 644 | 682 |
| 645 // Reset to last frame as reference buffer. | 683 // Reset to last frame as reference buffer. |
| 646 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; | 684 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; |
| 647 xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset; | 685 xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset; |
| 648 xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset; | 686 xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset; |
| 649 | 687 |
| 650 // In accumulating a score for the older reference frame take the | 688 // In accumulating a score for the older reference frame take the |
| 651 // best of the motion predicted score and the intra coded error | 689 // best of the motion predicted score and the intra coded error |
| 652 // (just as will be done for) accumulation of "coded_error" for | 690 // (just as will be done for) accumulation of "coded_error" for |
| 653 // the last frame. | 691 // the last frame. |
| 654 if (gf_motion_error < this_error) | 692 if (gf_motion_error < this_error) |
| 655 sr_coded_error += gf_motion_error; | 693 sr_coded_error += gf_motion_error; |
| 656 else | 694 else |
| 657 sr_coded_error += this_error; | 695 sr_coded_error += this_error; |
| 658 } else { | 696 } else { |
| 659 sr_coded_error += motion_error; | 697 sr_coded_error += motion_error; |
| 660 } | 698 } |
| 661 // Start by assuming that intra mode is best. | 699 // Start by assuming that intra mode is best. |
| 662 best_ref_mv.as_int = 0; | 700 best_ref_mv.as_int = 0; |
| 663 | 701 |
| 664 if (motion_error <= this_error) { | 702 if (motion_error <= this_error) { |
| 665 // Keep a count of cases where the inter and intra were very close | 703 // Keep a count of cases where the inter and intra were very close |
| 666 // and very low. This helps with scene cut detection for example in | 704 // and very low. This helps with scene cut detection for example in |
| 667 // cropped clips with black bars at the sides or top and bottom. | 705 // cropped clips with black bars at the sides or top and bottom. |
| 668 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && | 706 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && |
| 669 this_error < 2 * intrapenalty) | 707 this_error < 2 * intrapenalty) |
| 670 ++neutral_count; | 708 ++neutral_count; |
| 671 | 709 |
| 672 mv.as_mv.row *= 8; | 710 mv.as_mv.row *= 8; |
| 673 mv.as_mv.col *= 8; | 711 mv.as_mv.col *= 8; |
| 674 this_error = motion_error; | 712 this_error = motion_error; |
| 675 vp9_set_mbmode_and_mvs(xd, NEWMV, &mv.as_mv); | 713 xd->mi[0]->mbmi.mode = NEWMV; |
| 676 xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; | 714 xd->mi[0]->mbmi.mv[0] = mv; |
| 677 xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME; | 715 xd->mi[0]->mbmi.tx_size = TX_4X4; |
| 678 xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE; | 716 xd->mi[0]->mbmi.ref_frame[0] = LAST_FRAME; |
| 717 xd->mi[0]->mbmi.ref_frame[1] = NONE; |
| 679 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize); | 718 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize); |
| 680 vp9_encode_sby_pass1(x, bsize); | 719 vp9_encode_sby_pass1(x, bsize); |
| 681 sum_mvr += mv.as_mv.row; | 720 sum_mvr += mv.as_mv.row; |
| 682 sum_mvr_abs += abs(mv.as_mv.row); | 721 sum_mvr_abs += abs(mv.as_mv.row); |
| 683 sum_mvc += mv.as_mv.col; | 722 sum_mvc += mv.as_mv.col; |
| 684 sum_mvc_abs += abs(mv.as_mv.col); | 723 sum_mvc_abs += abs(mv.as_mv.col); |
| 685 sum_mvrs += mv.as_mv.row * mv.as_mv.row; | 724 sum_mvrs += mv.as_mv.row * mv.as_mv.row; |
| 686 sum_mvcs += mv.as_mv.col * mv.as_mv.col; | 725 sum_mvcs += mv.as_mv.col * mv.as_mv.col; |
| 687 ++intercount; | 726 ++intercount; |
| 688 | 727 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 uv_mb_height * cm->mb_cols; | 784 uv_mb_height * cm->mb_cols; |
| 746 | 785 |
| 747 vp9_clear_system_state(); | 786 vp9_clear_system_state(); |
| 748 } | 787 } |
| 749 | 788 |
| 750 vp9_clear_system_state(); | 789 vp9_clear_system_state(); |
| 751 { | 790 { |
| 752 FIRSTPASS_STATS fps; | 791 FIRSTPASS_STATS fps; |
| 753 | 792 |
| 754 fps.frame = cm->current_video_frame; | 793 fps.frame = cm->current_video_frame; |
| 794 fps.spatial_layer_id = cpi->svc.spatial_layer_id; |
| 755 fps.intra_error = (double)(intra_error >> 8); | 795 fps.intra_error = (double)(intra_error >> 8); |
| 756 fps.coded_error = (double)(coded_error >> 8); | 796 fps.coded_error = (double)(coded_error >> 8); |
| 757 fps.sr_coded_error = (double)(sr_coded_error >> 8); | 797 fps.sr_coded_error = (double)(sr_coded_error >> 8); |
| 758 fps.ssim_weighted_pred_err = fps.coded_error * simple_weight(cpi->Source); | 798 fps.ssim_weighted_pred_err = fps.coded_error * simple_weight(cpi->Source); |
| 759 fps.count = 1.0; | 799 fps.count = 1.0; |
| 760 fps.pcnt_inter = (double)intercount / cm->MBs; | 800 fps.pcnt_inter = (double)intercount / cm->MBs; |
| 761 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; | 801 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; |
| 762 fps.pcnt_neutral = (double)neutral_count / cm->MBs; | 802 fps.pcnt_neutral = (double)neutral_count / cm->MBs; |
| 763 | 803 |
| 764 if (mvcount > 0) { | 804 if (mvcount > 0) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 794 accumulate_stats(&twopass->total_stats, &fps); | 834 accumulate_stats(&twopass->total_stats, &fps); |
| 795 } | 835 } |
| 796 | 836 |
| 797 // Copy the previous Last Frame back into gf and and arf buffers if | 837 // Copy the previous Last Frame back into gf and and arf buffers if |
| 798 // the prediction is good enough... but also don't allow it to lag too far. | 838 // the prediction is good enough... but also don't allow it to lag too far. |
| 799 if ((twopass->sr_update_lag > 3) || | 839 if ((twopass->sr_update_lag > 3) || |
| 800 ((cm->current_video_frame > 0) && | 840 ((cm->current_video_frame > 0) && |
| 801 (twopass->this_frame_stats.pcnt_inter > 0.20) && | 841 (twopass->this_frame_stats.pcnt_inter > 0.20) && |
| 802 ((twopass->this_frame_stats.intra_error / | 842 ((twopass->this_frame_stats.intra_error / |
| 803 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { | 843 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { |
| 804 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 844 if (gld_yv12 != NULL) { |
| 845 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
| 846 } |
| 805 twopass->sr_update_lag = 1; | 847 twopass->sr_update_lag = 1; |
| 806 } else { | 848 } else { |
| 807 ++twopass->sr_update_lag; | 849 ++twopass->sr_update_lag; |
| 808 } | 850 } |
| 809 // Swap frame pointers so last frame refers to the frame we just compressed. | 851 |
| 810 swap_yv12(lst_yv12, new_yv12); | 852 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { |
| 853 vp9_update_reference_frames(cpi); |
| 854 } else { |
| 855 // Swap frame pointers so last frame refers to the frame we just compressed. |
| 856 swap_yv12(lst_yv12, new_yv12); |
| 857 } |
| 811 | 858 |
| 812 vp9_extend_frame_borders(lst_yv12); | 859 vp9_extend_frame_borders(lst_yv12); |
| 813 | 860 |
| 814 // Special case for the first frame. Copy into the GF buffer as a second | 861 // Special case for the first frame. Copy into the GF buffer as a second |
| 815 // reference. | 862 // reference. |
| 816 if (cm->current_video_frame == 0) | 863 if (cm->current_video_frame == 0 && gld_yv12 != NULL) { |
| 817 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 864 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
| 865 } |
| 818 | 866 |
| 819 // Use this to see what the first pass reconstruction looks like. | 867 // Use this to see what the first pass reconstruction looks like. |
| 820 if (0) { | 868 if (0) { |
| 821 char filename[512]; | 869 char filename[512]; |
| 822 FILE *recon_file; | 870 FILE *recon_file; |
| 823 snprintf(filename, sizeof(filename), "enc%04d.yuv", | 871 snprintf(filename, sizeof(filename), "enc%04d.yuv", |
| 824 (int)cm->current_video_frame); | 872 (int)cm->current_video_frame); |
| 825 | 873 |
| 826 if (cm->current_video_frame == 0) | 874 if (cm->current_video_frame == 0) |
| 827 recon_file = fopen(filename, "wb"); | 875 recon_file = fopen(filename, "wb"); |
| 828 else | 876 else |
| 829 recon_file = fopen(filename, "ab"); | 877 recon_file = fopen(filename, "ab"); |
| 830 | 878 |
| 831 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); | 879 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); |
| 832 fclose(recon_file); | 880 fclose(recon_file); |
| 833 } | 881 } |
| 834 | 882 |
| 835 ++cm->current_video_frame; | 883 ++cm->current_video_frame; |
| 836 } | 884 } |
| 837 | 885 |
| 838 // Estimate a cost per mb attributable to overheads such as the coding of modes | |
| 839 // and motion vectors. This currently makes simplistic assumptions for testing. | |
| 840 static double bitcost(double prob) { | |
| 841 return -(log(prob) / log(2.0)); | |
| 842 } | |
| 843 | |
| 844 static double calc_correction_factor(double err_per_mb, | 886 static double calc_correction_factor(double err_per_mb, |
| 845 double err_divisor, | 887 double err_divisor, |
| 846 double pt_low, | 888 double pt_low, |
| 847 double pt_high, | 889 double pt_high, |
| 848 int q) { | 890 int q) { |
| 849 const double error_term = err_per_mb / err_divisor; | 891 const double error_term = err_per_mb / err_divisor; |
| 850 | 892 |
| 851 // Adjustment based on actual quantizer to power term. | 893 // Adjustment based on actual quantizer to power term. |
| 852 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low, | 894 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low, |
| 853 pt_high); | 895 pt_high); |
| 854 | 896 |
| 855 // Calculate correction factor. | 897 // Calculate correction factor. |
| 856 if (power_term < 1.0) | 898 if (power_term < 1.0) |
| 857 assert(error_term >= 0.0); | 899 assert(error_term >= 0.0); |
| 858 | 900 |
| 859 return fclamp(pow(error_term, power_term), 0.05, 5.0); | 901 return fclamp(pow(error_term, power_term), 0.05, 5.0); |
| 860 } | 902 } |
| 861 | 903 |
| 862 int vp9_twopass_worst_quality(VP9_COMP *cpi, FIRSTPASS_STATS *fpstats, | 904 int vp9_twopass_worst_quality(VP9_COMP *cpi, FIRSTPASS_STATS *fpstats, |
| 863 int section_target_bandwitdh) { | 905 int section_target_bandwitdh) { |
| 864 int q; | 906 int q; |
| 865 const int num_mbs = cpi->common.MBs; | 907 const int num_mbs = cpi->common.MBs; |
| 866 int target_norm_bits_per_mb; | 908 int target_norm_bits_per_mb; |
| 867 const RATE_CONTROL *const rc = &cpi->rc; | 909 const RATE_CONTROL *const rc = &cpi->rc; |
| 868 | 910 |
| 869 const double section_err = fpstats->coded_error / fpstats->count; | 911 const double section_err = fpstats->coded_error / fpstats->count; |
| 870 const double err_per_mb = section_err / num_mbs; | 912 const double err_per_mb = section_err / num_mbs; |
| 913 const double speed_term = 1.0 + ((double)cpi->speed * 0.04); |
| 871 | 914 |
| 872 if (section_target_bandwitdh <= 0) | 915 if (section_target_bandwitdh <= 0) |
| 873 return rc->worst_quality; // Highest value allowed | 916 return rc->worst_quality; // Highest value allowed |
| 874 | 917 |
| 875 target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20) | 918 target_norm_bits_per_mb = |
| 876 ? (512 * section_target_bandwitdh) / num_mbs | 919 ((uint64_t)section_target_bandwitdh << BPER_MB_NORMBITS) / num_mbs; |
| 877 : 512 * (section_target_bandwitdh / num_mbs); | |
| 878 | 920 |
| 879 // Try and pick a max Q that will be high enough to encode the | 921 // Try and pick a max Q that will be high enough to encode the |
| 880 // content at the given rate. | 922 // content at the given rate. |
| 881 for (q = rc->best_quality; q < rc->worst_quality; ++q) { | 923 for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
| 882 const double err_correction_factor = calc_correction_factor(err_per_mb, | 924 const double err_correction_factor = calc_correction_factor(err_per_mb, |
| 883 ERR_DIVISOR, 0.5, 0.90, q); | 925 ERR_DIVISOR, 0.5, 0.90, q); |
| 884 const int bits_per_mb_at_this_q = vp9_rc_bits_per_mb(INTER_FRAME, q, | 926 const int bits_per_mb_at_this_q = |
| 885 err_correction_factor); | 927 vp9_rc_bits_per_mb(INTER_FRAME, q, (err_correction_factor * speed_term)); |
| 886 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) | 928 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 887 break; | 929 break; |
| 888 } | 930 } |
| 889 | 931 |
| 890 // Restriction on active max q for constrained quality mode. | 932 // Restriction on active max q for constrained quality mode. |
| 891 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) | 933 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) |
| 892 q = MAX(q, cpi->cq_target_quality); | 934 q = MAX(q, cpi->cq_target_quality); |
| 893 | 935 |
| 894 return q; | 936 return q; |
| 895 } | 937 } |
| 896 | 938 |
| 897 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); | 939 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); |
| 898 | 940 |
| 899 void vp9_init_second_pass(VP9_COMP *cpi) { | 941 void vp9_init_second_pass(VP9_COMP *cpi) { |
| 942 SVC *const svc = &cpi->svc; |
| 900 FIRSTPASS_STATS this_frame; | 943 FIRSTPASS_STATS this_frame; |
| 901 FIRSTPASS_STATS *start_pos; | 944 const FIRSTPASS_STATS *start_pos; |
| 902 struct twopass_rc *const twopass = &cpi->twopass; | 945 struct twopass_rc *twopass = &cpi->twopass; |
| 903 const VP9_CONFIG *const oxcf = &cpi->oxcf; | 946 const VP9_CONFIG *const oxcf = &cpi->oxcf; |
| 947 const int is_spatial_svc = (svc->number_spatial_layers > 1) && |
| 948 (svc->number_temporal_layers == 1); |
| 949 double frame_rate; |
| 950 |
| 951 if (is_spatial_svc) { |
| 952 twopass = &svc->layer_context[svc->spatial_layer_id].twopass; |
| 953 } |
| 904 | 954 |
| 905 zero_stats(&twopass->total_stats); | 955 zero_stats(&twopass->total_stats); |
| 906 zero_stats(&twopass->total_left_stats); | 956 zero_stats(&twopass->total_left_stats); |
| 907 | 957 |
| 908 if (!twopass->stats_in_end) | 958 if (!twopass->stats_in_end) |
| 909 return; | 959 return; |
| 910 | 960 |
| 911 twopass->total_stats = *twopass->stats_in_end; | 961 twopass->total_stats = *twopass->stats_in_end; |
| 912 twopass->total_left_stats = twopass->total_stats; | 962 twopass->total_left_stats = twopass->total_stats; |
| 913 | 963 |
| 964 frame_rate = 10000000.0 * twopass->total_stats.count / |
| 965 twopass->total_stats.duration; |
| 914 // Each frame can have a different duration, as the frame rate in the source | 966 // Each frame can have a different duration, as the frame rate in the source |
| 915 // isn't guaranteed to be constant. The frame rate prior to the first frame | 967 // isn't guaranteed to be constant. The frame rate prior to the first frame |
| 916 // encoded in the second pass is a guess. However, the sum duration is not. | 968 // encoded in the second pass is a guess. However, the sum duration is not. |
| 917 // It is calculated based on the actual durations of all frames from the | 969 // It is calculated based on the actual durations of all frames from the |
| 918 // first pass. | 970 // first pass. |
| 919 vp9_new_framerate(cpi, 10000000.0 * twopass->total_stats.count / | 971 |
| 920 twopass->total_stats.duration); | 972 if (is_spatial_svc) { |
| 973 vp9_update_spatial_layer_framerate(cpi, frame_rate); |
| 974 twopass->bits_left = |
| 975 (int64_t)(twopass->total_stats.duration * |
| 976 svc->layer_context[svc->spatial_layer_id].target_bandwidth / |
| 977 10000000.0); |
| 978 } else { |
| 979 vp9_new_framerate(cpi, frame_rate); |
| 980 twopass->bits_left = (int64_t)(twopass->total_stats.duration * |
| 981 oxcf->target_bandwidth / 10000000.0); |
| 982 } |
| 921 | 983 |
| 922 cpi->output_framerate = oxcf->framerate; | 984 cpi->output_framerate = oxcf->framerate; |
| 923 twopass->bits_left = (int64_t)(twopass->total_stats.duration * | |
| 924 oxcf->target_bandwidth / 10000000.0); | |
| 925 | 985 |
| 926 // Calculate a minimum intra value to be used in determining the IIratio | 986 // Calculate a minimum intra value to be used in determining the IIratio |
| 927 // scores used in the second pass. We have this minimum to make sure | 987 // scores used in the second pass. We have this minimum to make sure |
| 928 // that clips that are static but "low complexity" in the intra domain | 988 // that clips that are static but "low complexity" in the intra domain |
| 929 // are still boosted appropriately for KF/GF/ARF. | 989 // are still boosted appropriately for KF/GF/ARF. |
| 930 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; | 990 if (!is_spatial_svc) { |
| 931 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; | 991 // We don't know the number of MBs for each layer at this point. |
| 992 // So we will do it later. |
| 993 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
| 994 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
| 995 } |
| 932 | 996 |
| 933 // This variable monitors how far behind the second ref update is lagging. | 997 // This variable monitors how far behind the second ref update is lagging. |
| 934 twopass->sr_update_lag = 1; | 998 twopass->sr_update_lag = 1; |
| 935 | 999 |
| 936 // Scan the first pass file and calculate an average Intra / Inter error score | 1000 // Scan the first pass file and calculate an average Intra / Inter error |
| 937 // ratio for the sequence. | 1001 // score ratio for the sequence. |
| 938 { | 1002 { |
| 939 double sum_iiratio = 0.0; | 1003 double sum_iiratio = 0.0; |
| 940 start_pos = twopass->stats_in; | 1004 start_pos = twopass->stats_in; |
| 941 | 1005 |
| 942 while (input_stats(twopass, &this_frame) != EOF) { | 1006 while (input_stats(twopass, &this_frame) != EOF) { |
| 943 const double iiratio = this_frame.intra_error / | 1007 const double iiratio = this_frame.intra_error / |
| 944 DOUBLE_DIVIDE_CHECK(this_frame.coded_error); | 1008 DOUBLE_DIVIDE_CHECK(this_frame.coded_error); |
| 945 sum_iiratio += fclamp(iiratio, 1.0, 20.0); | 1009 sum_iiratio += fclamp(iiratio, 1.0, 20.0); |
| 946 } | 1010 } |
| 947 | 1011 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 986 const double second_ref_decay = mb_sr_err_diff <= 512.0 | 1050 const double second_ref_decay = mb_sr_err_diff <= 512.0 |
| 987 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) | 1051 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) |
| 988 : 0.85; | 1052 : 0.85; |
| 989 | 1053 |
| 990 return MIN(second_ref_decay, next_frame->pcnt_inter); | 1054 return MIN(second_ref_decay, next_frame->pcnt_inter); |
| 991 } | 1055 } |
| 992 | 1056 |
| 993 // Function to test for a condition where a complex transition is followed | 1057 // Function to test for a condition where a complex transition is followed |
| 994 // by a static section. For example in slide shows where there is a fade | 1058 // by a static section. For example in slide shows where there is a fade |
| 995 // between slides. This is to help with more optimal kf and gf positioning. | 1059 // between slides. This is to help with more optimal kf and gf positioning. |
| 996 static int detect_transition_to_still(VP9_COMP *cpi, int frame_interval, | 1060 static int detect_transition_to_still(struct twopass_rc *twopass, |
| 997 int still_interval, | 1061 int frame_interval, int still_interval, |
| 998 double loop_decay_rate, | 1062 double loop_decay_rate, |
| 999 double last_decay_rate) { | 1063 double last_decay_rate) { |
| 1000 int trans_to_still = 0; | 1064 int trans_to_still = 0; |
| 1001 | 1065 |
| 1002 // Break clause to detect very still sections after motion | 1066 // Break clause to detect very still sections after motion |
| 1003 // For example a static image after a fade or other transition | 1067 // For example a static image after a fade or other transition |
| 1004 // instead of a clean scene cut. | 1068 // instead of a clean scene cut. |
| 1005 if (frame_interval > MIN_GF_INTERVAL && | 1069 if (frame_interval > MIN_GF_INTERVAL && |
| 1006 loop_decay_rate >= 0.999 && | 1070 loop_decay_rate >= 0.999 && |
| 1007 last_decay_rate < 0.9) { | 1071 last_decay_rate < 0.9) { |
| 1008 int j; | 1072 int j; |
| 1009 FIRSTPASS_STATS *position = cpi->twopass.stats_in; | 1073 const FIRSTPASS_STATS *position = twopass->stats_in; |
| 1010 FIRSTPASS_STATS tmp_next_frame; | 1074 FIRSTPASS_STATS tmp_next_frame; |
| 1011 | 1075 |
| 1012 // Look ahead a few frames to see if static condition persists... | 1076 // Look ahead a few frames to see if static condition persists... |
| 1013 for (j = 0; j < still_interval; ++j) { | 1077 for (j = 0; j < still_interval; ++j) { |
| 1014 if (EOF == input_stats(&cpi->twopass, &tmp_next_frame)) | 1078 if (EOF == input_stats(twopass, &tmp_next_frame)) |
| 1015 break; | 1079 break; |
| 1016 | 1080 |
| 1017 if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999) | 1081 if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999) |
| 1018 break; | 1082 break; |
| 1019 } | 1083 } |
| 1020 | 1084 |
| 1021 reset_fpf_position(&cpi->twopass, position); | 1085 reset_fpf_position(twopass, position); |
| 1022 | 1086 |
| 1023 // Only if it does do we signal a transition to still. | 1087 // Only if it does do we signal a transition to still. |
| 1024 if (j == still_interval) | 1088 if (j == still_interval) |
| 1025 trans_to_still = 1; | 1089 trans_to_still = 1; |
| 1026 } | 1090 } |
| 1027 | 1091 |
| 1028 return trans_to_still; | 1092 return trans_to_still; |
| 1029 } | 1093 } |
| 1030 | 1094 |
| 1031 // This function detects a flash through the high relative pcnt_second_ref | 1095 // This function detects a flash through the high relative pcnt_second_ref |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1333 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1397 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1334 printf("%4d ", cpi->arf_weight[i]); | 1398 printf("%4d ", cpi->arf_weight[i]); |
| 1335 } | 1399 } |
| 1336 printf("\n"); | 1400 printf("\n"); |
| 1337 #endif | 1401 #endif |
| 1338 } | 1402 } |
| 1339 #endif | 1403 #endif |
| 1340 | 1404 |
| 1341 // Analyse and define a gf/arf group. | 1405 // Analyse and define a gf/arf group. |
| 1342 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1406 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1407 RATE_CONTROL *const rc = &cpi->rc; |
| 1408 VP9_CONFIG *const oxcf = &cpi->oxcf; |
| 1409 struct twopass_rc *const twopass = &cpi->twopass; |
| 1343 FIRSTPASS_STATS next_frame = { 0 }; | 1410 FIRSTPASS_STATS next_frame = { 0 }; |
| 1344 FIRSTPASS_STATS *start_pos; | 1411 const FIRSTPASS_STATS *start_pos; |
| 1345 struct twopass_rc *const twopass = &cpi->twopass; | |
| 1346 int i; | 1412 int i; |
| 1347 double boost_score = 0.0; | 1413 double boost_score = 0.0; |
| 1348 double old_boost_score = 0.0; | 1414 double old_boost_score = 0.0; |
| 1349 double gf_group_err = 0.0; | 1415 double gf_group_err = 0.0; |
| 1350 double gf_first_frame_err = 0.0; | 1416 double gf_first_frame_err = 0.0; |
| 1351 double mod_frame_err = 0.0; | 1417 double mod_frame_err = 0.0; |
| 1352 | 1418 |
| 1353 double mv_ratio_accumulator = 0.0; | 1419 double mv_ratio_accumulator = 0.0; |
| 1354 double decay_accumulator = 1.0; | 1420 double decay_accumulator = 1.0; |
| 1355 double zero_motion_accumulator = 1.0; | 1421 double zero_motion_accumulator = 1.0; |
| 1356 | 1422 |
| 1357 double loop_decay_rate = 1.00; | 1423 double loop_decay_rate = 1.00; |
| 1358 double last_loop_decay_rate = 1.00; | 1424 double last_loop_decay_rate = 1.00; |
| 1359 | 1425 |
| 1360 double this_frame_mv_in_out = 0.0; | 1426 double this_frame_mv_in_out = 0.0; |
| 1361 double mv_in_out_accumulator = 0.0; | 1427 double mv_in_out_accumulator = 0.0; |
| 1362 double abs_mv_in_out_accumulator = 0.0; | 1428 double abs_mv_in_out_accumulator = 0.0; |
| 1363 double mv_ratio_accumulator_thresh; | 1429 double mv_ratio_accumulator_thresh; |
| 1364 const int max_bits = frame_max_bits(cpi); // Max bits for a single frame. | 1430 // Max bits for a single frame. |
| 1365 | 1431 const int max_bits = frame_max_bits(rc, oxcf); |
| 1366 unsigned int allow_alt_ref = cpi->oxcf.play_alternate && | 1432 unsigned int allow_alt_ref = oxcf->play_alternate && oxcf->lag_in_frames; |
| 1367 cpi->oxcf.lag_in_frames; | |
| 1368 | 1433 |
| 1369 int f_boost = 0; | 1434 int f_boost = 0; |
| 1370 int b_boost = 0; | 1435 int b_boost = 0; |
| 1371 int flash_detected; | 1436 int flash_detected; |
| 1372 int active_max_gf_interval; | 1437 int active_max_gf_interval; |
| 1373 RATE_CONTROL *const rc = &cpi->rc; | |
| 1374 | 1438 |
| 1375 twopass->gf_group_bits = 0; | 1439 twopass->gf_group_bits = 0; |
| 1376 | 1440 |
| 1377 vp9_clear_system_state(); | 1441 vp9_clear_system_state(); |
| 1378 | 1442 |
| 1379 start_pos = twopass->stats_in; | 1443 start_pos = twopass->stats_in; |
| 1380 | 1444 |
| 1381 // Load stats for the current frame. | 1445 // Load stats for the current frame. |
| 1382 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1446 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1383 | 1447 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1400 // At high Q when there are few bits to spare we are better with a longer | 1464 // At high Q when there are few bits to spare we are better with a longer |
| 1401 // interval to spread the cost of the GF. | 1465 // interval to spread the cost of the GF. |
| 1402 // | 1466 // |
| 1403 active_max_gf_interval = | 1467 active_max_gf_interval = |
| 1404 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); | 1468 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); |
| 1405 | 1469 |
| 1406 if (active_max_gf_interval > rc->max_gf_interval) | 1470 if (active_max_gf_interval > rc->max_gf_interval) |
| 1407 active_max_gf_interval = rc->max_gf_interval; | 1471 active_max_gf_interval = rc->max_gf_interval; |
| 1408 | 1472 |
| 1409 i = 0; | 1473 i = 0; |
| 1410 while (i < twopass->static_scene_max_gf_interval && i < rc->frames_to_key) { | 1474 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { |
| 1411 ++i; | 1475 ++i; |
| 1412 | 1476 |
| 1413 // Accumulate error score of frames in this gf group. | 1477 // Accumulate error score of frames in this gf group. |
| 1414 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1478 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1415 gf_group_err += mod_frame_err; | 1479 gf_group_err += mod_frame_err; |
| 1416 | 1480 |
| 1417 if (EOF == input_stats(twopass, &next_frame)) | 1481 if (EOF == input_stats(twopass, &next_frame)) |
| 1418 break; | 1482 break; |
| 1419 | 1483 |
| 1420 // Test for the case where there is a brief flash but the prediction | 1484 // Test for the case where there is a brief flash but the prediction |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1435 | 1499 |
| 1436 // Monitor for static sections. | 1500 // Monitor for static sections. |
| 1437 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 1501 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 1438 zero_motion_accumulator) { | 1502 zero_motion_accumulator) { |
| 1439 zero_motion_accumulator = next_frame.pcnt_inter - | 1503 zero_motion_accumulator = next_frame.pcnt_inter - |
| 1440 next_frame.pcnt_motion; | 1504 next_frame.pcnt_motion; |
| 1441 } | 1505 } |
| 1442 | 1506 |
| 1443 // Break clause to detect very still sections after motion. For example, | 1507 // Break clause to detect very still sections after motion. For example, |
| 1444 // a static image after a fade or other transition. | 1508 // a static image after a fade or other transition. |
| 1445 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate, | 1509 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate, |
| 1446 last_loop_decay_rate)) { | 1510 last_loop_decay_rate)) { |
| 1447 allow_alt_ref = 0; | 1511 allow_alt_ref = 0; |
| 1448 break; | 1512 break; |
| 1449 } | 1513 } |
| 1450 } | 1514 } |
| 1451 | 1515 |
| 1452 // Calculate a boost number for this frame. | 1516 // Calculate a boost number for this frame. |
| 1453 boost_score += (decay_accumulator * | 1517 boost_score += (decay_accumulator * |
| 1454 calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out)); | 1518 calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out)); |
| 1455 | 1519 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1574 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1638 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1575 printf("%4d ", cpi->arf_weight[i]); | 1639 printf("%4d ", cpi->arf_weight[i]); |
| 1576 } | 1640 } |
| 1577 printf("\n"); | 1641 printf("\n"); |
| 1578 } | 1642 } |
| 1579 #endif | 1643 #endif |
| 1580 #endif | 1644 #endif |
| 1581 | 1645 |
| 1582 // Calculate the bits to be allocated to the group as a whole. | 1646 // Calculate the bits to be allocated to the group as a whole. |
| 1583 if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) { | 1647 if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) { |
| 1584 twopass->gf_group_bits = (int64_t)(cpi->twopass.kf_group_bits * | 1648 twopass->gf_group_bits = (int64_t)(twopass->kf_group_bits * |
| 1585 (gf_group_err / cpi->twopass.kf_group_error_left)); | 1649 (gf_group_err / twopass->kf_group_error_left)); |
| 1586 } else { | 1650 } else { |
| 1587 twopass->gf_group_bits = 0; | 1651 twopass->gf_group_bits = 0; |
| 1588 } | 1652 } |
| 1589 twopass->gf_group_bits = (twopass->gf_group_bits < 0) ? | 1653 twopass->gf_group_bits = (twopass->gf_group_bits < 0) ? |
| 1590 0 : (twopass->gf_group_bits > twopass->kf_group_bits) ? | 1654 0 : (twopass->gf_group_bits > twopass->kf_group_bits) ? |
| 1591 twopass->kf_group_bits : twopass->gf_group_bits; | 1655 twopass->kf_group_bits : twopass->gf_group_bits; |
| 1592 | 1656 |
| 1593 // Clip cpi->twopass.gf_group_bits based on user supplied data rate | 1657 // Clip cpi->twopass.gf_group_bits based on user supplied data rate |
| 1594 // variability limit, cpi->oxcf.two_pass_vbrmax_section. | 1658 // variability limit, cpi->oxcf.two_pass_vbrmax_section. |
| 1595 if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) | 1659 if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1664 (!rc->source_alt_ref_pending && | 1728 (!rc->source_alt_ref_pending && |
| 1665 cpi->common.frame_type != KEY_FRAME)) { | 1729 cpi->common.frame_type != KEY_FRAME)) { |
| 1666 // Calculate the per frame bit target for this frame. | 1730 // Calculate the per frame bit target for this frame. |
| 1667 vp9_rc_set_frame_target(cpi, gf_bits); | 1731 vp9_rc_set_frame_target(cpi, gf_bits); |
| 1668 } | 1732 } |
| 1669 } | 1733 } |
| 1670 | 1734 |
| 1671 { | 1735 { |
| 1672 // Adjust KF group bits and error remaining. | 1736 // Adjust KF group bits and error remaining. |
| 1673 twopass->kf_group_error_left -= (int64_t)gf_group_err; | 1737 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1674 twopass->kf_group_bits -= twopass->gf_group_bits; | |
| 1675 | |
| 1676 if (twopass->kf_group_bits < 0) | |
| 1677 twopass->kf_group_bits = 0; | |
| 1678 | 1738 |
| 1679 // If this is an arf update we want to remove the score for the overlay | 1739 // If this is an arf update we want to remove the score for the overlay |
| 1680 // frame at the end which will usually be very cheap to code. | 1740 // frame at the end which will usually be very cheap to code. |
| 1681 // The overlay frame has already, in effect, been coded so we want to spread | 1741 // The overlay frame has already, in effect, been coded so we want to spread |
| 1682 // the remaining bits among the other frames. | 1742 // the remaining bits among the other frames. |
| 1683 // For normal GFs remove the score for the GF itself unless this is | 1743 // For normal GFs remove the score for the GF itself unless this is |
| 1684 // also a key frame in which case it has already been accounted for. | 1744 // also a key frame in which case it has already been accounted for. |
| 1685 if (rc->source_alt_ref_pending) { | 1745 if (rc->source_alt_ref_pending) { |
| 1686 twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err); | 1746 twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err); |
| 1687 } else if (cpi->common.frame_type != KEY_FRAME) { | 1747 } else if (cpi->common.frame_type != KEY_FRAME) { |
| 1688 twopass->gf_group_error_left = (int64_t)(gf_group_err | 1748 twopass->gf_group_error_left = (int64_t)(gf_group_err |
| 1689 - gf_first_frame_err); | 1749 - gf_first_frame_err); |
| 1690 } else { | 1750 } else { |
| 1691 twopass->gf_group_error_left = (int64_t)gf_group_err; | 1751 twopass->gf_group_error_left = (int64_t)gf_group_err; |
| 1692 } | 1752 } |
| 1693 | 1753 |
| 1694 twopass->gf_group_bits -= twopass->gf_bits; | |
| 1695 | |
| 1696 if (twopass->gf_group_bits < 0) | |
| 1697 twopass->gf_group_bits = 0; | |
| 1698 | |
| 1699 // This condition could fail if there are two kfs very close together | 1754 // This condition could fail if there are two kfs very close together |
| 1700 // despite MIN_GF_INTERVAL and would cause a divide by 0 in the | 1755 // despite MIN_GF_INTERVAL and would cause a divide by 0 in the |
| 1701 // calculation of alt_extra_bits. | 1756 // calculation of alt_extra_bits. |
| 1702 if (rc->baseline_gf_interval >= 3) { | 1757 if (rc->baseline_gf_interval >= 3) { |
| 1703 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost; | 1758 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost; |
| 1704 | 1759 |
| 1705 if (boost >= 150) { | 1760 if (boost >= 150) { |
| 1706 const int pct_extra = MIN(20, (boost - 100) / 50); | 1761 const int pct_extra = MIN(20, (boost - 100) / 50); |
| 1707 const int alt_extra_bits = (int)((twopass->gf_group_bits * pct_extra) / | 1762 const int alt_extra_bits = (int)(( |
| 1708 100); | 1763 MAX(twopass->gf_group_bits - twopass->gf_bits, 0) * |
| 1764 pct_extra) / 100); |
| 1709 twopass->gf_group_bits -= alt_extra_bits; | 1765 twopass->gf_group_bits -= alt_extra_bits; |
| 1710 } | 1766 } |
| 1711 } | 1767 } |
| 1712 } | 1768 } |
| 1713 | 1769 |
| 1714 if (cpi->common.frame_type != KEY_FRAME) { | 1770 if (cpi->common.frame_type != KEY_FRAME) { |
| 1715 FIRSTPASS_STATS sectionstats; | 1771 FIRSTPASS_STATS sectionstats; |
| 1716 | 1772 |
| 1717 zero_stats(§ionstats); | 1773 zero_stats(§ionstats); |
| 1718 reset_fpf_position(twopass, start_pos); | 1774 reset_fpf_position(twopass, start_pos); |
| 1719 | 1775 |
| 1720 for (i = 0; i < rc->baseline_gf_interval; ++i) { | 1776 for (i = 0; i < rc->baseline_gf_interval; ++i) { |
| 1721 input_stats(twopass, &next_frame); | 1777 input_stats(twopass, &next_frame); |
| 1722 accumulate_stats(§ionstats, &next_frame); | 1778 accumulate_stats(§ionstats, &next_frame); |
| 1723 } | 1779 } |
| 1724 | 1780 |
| 1725 avg_stats(§ionstats); | 1781 avg_stats(§ionstats); |
| 1726 | 1782 |
| 1727 twopass->section_intra_rating = (int) | 1783 twopass->section_intra_rating = (int) |
| 1728 (sectionstats.intra_error / | 1784 (sectionstats.intra_error / |
| 1729 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | 1785 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
| 1730 | 1786 |
| 1731 reset_fpf_position(twopass, start_pos); | 1787 reset_fpf_position(twopass, start_pos); |
| 1732 } | 1788 } |
| 1733 } | 1789 } |
| 1734 | 1790 |
| 1735 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. | 1791 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. |
| 1736 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1792 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1793 struct twopass_rc *twopass = &cpi->twopass; |
| 1794 // For a single frame. |
| 1795 const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf); |
| 1796 // Calculate modified prediction error used in bit allocation. |
| 1797 const double modified_err = calculate_modified_err(cpi, this_frame); |
| 1737 int target_frame_size; | 1798 int target_frame_size; |
| 1738 double modified_err; | |
| 1739 double err_fraction; | 1799 double err_fraction; |
| 1740 const int max_bits = frame_max_bits(cpi); // Max for a single frame. | |
| 1741 | 1800 |
| 1742 // Calculate modified prediction error used in bit allocation. | 1801 if (twopass->gf_group_error_left > 0) |
| 1743 modified_err = calculate_modified_err(cpi, this_frame); | |
| 1744 | |
| 1745 if (cpi->twopass.gf_group_error_left > 0) | |
| 1746 // What portion of the remaining GF group error is used by this frame. | 1802 // What portion of the remaining GF group error is used by this frame. |
| 1747 err_fraction = modified_err / cpi->twopass.gf_group_error_left; | 1803 err_fraction = modified_err / twopass->gf_group_error_left; |
| 1748 else | 1804 else |
| 1749 err_fraction = 0.0; | 1805 err_fraction = 0.0; |
| 1750 | 1806 |
| 1751 // How many of those bits available for allocation should we give it? | 1807 // How many of those bits available for allocation should we give it? |
| 1752 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); | 1808 target_frame_size = (int)((double)twopass->gf_group_bits * err_fraction); |
| 1753 | 1809 |
| 1754 // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at | 1810 // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at |
| 1755 // the top end. | 1811 // the top end. |
| 1756 target_frame_size = clamp(target_frame_size, 0, | 1812 target_frame_size = clamp(target_frame_size, 0, |
| 1757 MIN(max_bits, (int)cpi->twopass.gf_group_bits)); | 1813 MIN(max_bits, (int)twopass->gf_group_bits)); |
| 1758 | 1814 |
| 1759 // Adjust error and bits remaining. | 1815 // Adjust error and bits remaining. |
| 1760 cpi->twopass.gf_group_error_left -= (int64_t)modified_err; | 1816 twopass->gf_group_error_left -= (int64_t)modified_err; |
| 1761 cpi->twopass.gf_group_bits -= target_frame_size; | |
| 1762 | |
| 1763 if (cpi->twopass.gf_group_bits < 0) | |
| 1764 cpi->twopass.gf_group_bits = 0; | |
| 1765 | 1817 |
| 1766 // Per frame bit target for this frame. | 1818 // Per frame bit target for this frame. |
| 1767 vp9_rc_set_frame_target(cpi, target_frame_size); | 1819 vp9_rc_set_frame_target(cpi, target_frame_size); |
| 1768 } | 1820 } |
| 1769 | 1821 |
| 1770 static int test_candidate_kf(VP9_COMP *cpi, | 1822 static int test_candidate_kf(struct twopass_rc *twopass, |
| 1771 const FIRSTPASS_STATS *last_frame, | 1823 const FIRSTPASS_STATS *last_frame, |
| 1772 const FIRSTPASS_STATS *this_frame, | 1824 const FIRSTPASS_STATS *this_frame, |
| 1773 const FIRSTPASS_STATS *next_frame) { | 1825 const FIRSTPASS_STATS *next_frame) { |
| 1774 int is_viable_kf = 0; | 1826 int is_viable_kf = 0; |
| 1775 | 1827 |
| 1776 // Does the frame satisfy the primary criteria of a key frame? | 1828 // Does the frame satisfy the primary criteria of a key frame? |
| 1777 // If so, then examine how well it predicts subsequent frames. | 1829 // If so, then examine how well it predicts subsequent frames. |
| 1778 if ((this_frame->pcnt_second_ref < 0.10) && | 1830 if ((this_frame->pcnt_second_ref < 0.10) && |
| 1779 (next_frame->pcnt_second_ref < 0.10) && | 1831 (next_frame->pcnt_second_ref < 0.10) && |
| 1780 ((this_frame->pcnt_inter < 0.05) || | 1832 ((this_frame->pcnt_inter < 0.05) || |
| 1781 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) && | 1833 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) && |
| 1782 ((this_frame->intra_error / | 1834 ((this_frame->intra_error / |
| 1783 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && | 1835 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && |
| 1784 ((fabs(last_frame->coded_error - this_frame->coded_error) / | 1836 ((fabs(last_frame->coded_error - this_frame->coded_error) / |
| 1785 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) || | 1837 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) || |
| 1786 (fabs(last_frame->intra_error - this_frame->intra_error) / | 1838 (fabs(last_frame->intra_error - this_frame->intra_error) / |
| 1787 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) || | 1839 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) || |
| 1788 ((next_frame->intra_error / | 1840 ((next_frame->intra_error / |
| 1789 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) { | 1841 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) { |
| 1790 int i; | 1842 int i; |
| 1791 FIRSTPASS_STATS *start_pos; | 1843 const FIRSTPASS_STATS *start_pos = twopass->stats_in; |
| 1792 | 1844 FIRSTPASS_STATS local_next_frame = *next_frame; |
| 1793 FIRSTPASS_STATS local_next_frame; | |
| 1794 | |
| 1795 double boost_score = 0.0; | 1845 double boost_score = 0.0; |
| 1796 double old_boost_score = 0.0; | 1846 double old_boost_score = 0.0; |
| 1797 double decay_accumulator = 1.0; | 1847 double decay_accumulator = 1.0; |
| 1798 | 1848 |
| 1799 local_next_frame = *next_frame; | |
| 1800 | |
| 1801 // Note the starting file position so we can reset to it. | |
| 1802 start_pos = cpi->twopass.stats_in; | |
| 1803 | |
| 1804 // Examine how well the key frame predicts subsequent frames. | 1849 // Examine how well the key frame predicts subsequent frames. |
| 1805 for (i = 0; i < 16; ++i) { | 1850 for (i = 0; i < 16; ++i) { |
| 1806 double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / | 1851 double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / |
| 1807 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)); | 1852 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)); |
| 1808 | 1853 |
| 1809 if (next_iiratio > RMAX) | 1854 if (next_iiratio > RMAX) |
| 1810 next_iiratio = RMAX; | 1855 next_iiratio = RMAX; |
| 1811 | 1856 |
| 1812 // Cumulative effect of decay in prediction quality. | 1857 // Cumulative effect of decay in prediction quality. |
| 1813 if (local_next_frame.pcnt_inter > 0.85) | 1858 if (local_next_frame.pcnt_inter > 0.85) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1825 local_next_frame.pcnt_neutral) < 0.20) && | 1870 local_next_frame.pcnt_neutral) < 0.20) && |
| 1826 (next_iiratio < 3.0)) || | 1871 (next_iiratio < 3.0)) || |
| 1827 ((boost_score - old_boost_score) < 3.0) || | 1872 ((boost_score - old_boost_score) < 3.0) || |
| 1828 (local_next_frame.intra_error < 200)) { | 1873 (local_next_frame.intra_error < 200)) { |
| 1829 break; | 1874 break; |
| 1830 } | 1875 } |
| 1831 | 1876 |
| 1832 old_boost_score = boost_score; | 1877 old_boost_score = boost_score; |
| 1833 | 1878 |
| 1834 // Get the next frame details | 1879 // Get the next frame details |
| 1835 if (EOF == input_stats(&cpi->twopass, &local_next_frame)) | 1880 if (EOF == input_stats(twopass, &local_next_frame)) |
| 1836 break; | 1881 break; |
| 1837 } | 1882 } |
| 1838 | 1883 |
| 1839 // If there is tolerable prediction for at least the next 3 frames then | 1884 // If there is tolerable prediction for at least the next 3 frames then |
| 1840 // break out else discard this potential key frame and move on | 1885 // break out else discard this potential key frame and move on |
| 1841 if (boost_score > 30.0 && (i > 3)) { | 1886 if (boost_score > 30.0 && (i > 3)) { |
| 1842 is_viable_kf = 1; | 1887 is_viable_kf = 1; |
| 1843 } else { | 1888 } else { |
| 1844 // Reset the file position | 1889 // Reset the file position |
| 1845 reset_fpf_position(&cpi->twopass, start_pos); | 1890 reset_fpf_position(twopass, start_pos); |
| 1846 | 1891 |
| 1847 is_viable_kf = 0; | 1892 is_viable_kf = 0; |
| 1848 } | 1893 } |
| 1849 } | 1894 } |
| 1850 | 1895 |
| 1851 return is_viable_kf; | 1896 return is_viable_kf; |
| 1852 } | 1897 } |
| 1853 | 1898 |
| 1854 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1899 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1855 int i, j; | 1900 int i, j; |
| 1901 RATE_CONTROL *const rc = &cpi->rc; |
| 1902 struct twopass_rc *const twopass = &cpi->twopass; |
| 1903 const FIRSTPASS_STATS first_frame = *this_frame; |
| 1904 const FIRSTPASS_STATS *start_position = twopass->stats_in; |
| 1905 FIRSTPASS_STATS next_frame; |
| 1856 FIRSTPASS_STATS last_frame; | 1906 FIRSTPASS_STATS last_frame; |
| 1857 FIRSTPASS_STATS first_frame; | |
| 1858 FIRSTPASS_STATS next_frame; | |
| 1859 FIRSTPASS_STATS *start_position; | |
| 1860 | |
| 1861 double decay_accumulator = 1.0; | 1907 double decay_accumulator = 1.0; |
| 1862 double zero_motion_accumulator = 1.0; | 1908 double zero_motion_accumulator = 1.0; |
| 1863 double boost_score = 0; | 1909 double boost_score = 0.0; |
| 1864 double loop_decay_rate; | |
| 1865 | |
| 1866 double kf_mod_err = 0.0; | 1910 double kf_mod_err = 0.0; |
| 1867 double kf_group_err = 0.0; | 1911 double kf_group_err = 0.0; |
| 1868 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; | 1912 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
| 1869 | 1913 |
| 1870 RATE_CONTROL *const rc = &cpi->rc; | |
| 1871 struct twopass_rc *const twopass = &cpi->twopass; | |
| 1872 | |
| 1873 vp9_zero(next_frame); | 1914 vp9_zero(next_frame); |
| 1874 | 1915 |
| 1875 vp9_clear_system_state(); | |
| 1876 | |
| 1877 start_position = twopass->stats_in; | |
| 1878 cpi->common.frame_type = KEY_FRAME; | 1916 cpi->common.frame_type = KEY_FRAME; |
| 1879 | 1917 |
| 1880 // Is this a forced key frame by interval. | 1918 // Is this a forced key frame by interval. |
| 1881 rc->this_key_frame_forced = rc->next_key_frame_forced; | 1919 rc->this_key_frame_forced = rc->next_key_frame_forced; |
| 1882 | 1920 |
| 1883 // Clear the alt ref active flag as this can never be active on a key frame. | 1921 // Clear the alt ref active flag as this can never be active on a key frame. |
| 1884 rc->source_alt_ref_active = 0; | 1922 rc->source_alt_ref_active = 0; |
| 1885 | 1923 |
| 1886 // KF is always a GF so clear frames till next gf counter. | 1924 // KF is always a GF so clear frames till next gf counter. |
| 1887 rc->frames_till_gf_update_due = 0; | 1925 rc->frames_till_gf_update_due = 0; |
| 1888 | 1926 |
| 1889 rc->frames_to_key = 1; | 1927 rc->frames_to_key = 1; |
| 1890 | 1928 |
| 1891 // Take a copy of the initial frame details. | |
| 1892 first_frame = *this_frame; | |
| 1893 | |
| 1894 twopass->kf_group_bits = 0; // Total bits available to kf group | 1929 twopass->kf_group_bits = 0; // Total bits available to kf group |
| 1895 twopass->kf_group_error_left = 0; // Group modified error score. | 1930 twopass->kf_group_error_left = 0; // Group modified error score. |
| 1896 | 1931 |
| 1897 kf_mod_err = calculate_modified_err(cpi, this_frame); | 1932 kf_mod_err = calculate_modified_err(cpi, this_frame); |
| 1898 | 1933 |
| 1899 // Find the next keyframe. | 1934 // Find the next keyframe. |
| 1900 i = 0; | 1935 i = 0; |
| 1901 while (twopass->stats_in < twopass->stats_in_end) { | 1936 while (twopass->stats_in < twopass->stats_in_end) { |
| 1902 // Accumulate kf group error. | 1937 // Accumulate kf group error. |
| 1903 kf_group_err += calculate_modified_err(cpi, this_frame); | 1938 kf_group_err += calculate_modified_err(cpi, this_frame); |
| 1904 | 1939 |
| 1905 // Load the next frame's stats. | 1940 // Load the next frame's stats. |
| 1906 last_frame = *this_frame; | 1941 last_frame = *this_frame; |
| 1907 input_stats(twopass, this_frame); | 1942 input_stats(twopass, this_frame); |
| 1908 | 1943 |
| 1909 // Provided that we are not at the end of the file... | 1944 // Provided that we are not at the end of the file... |
| 1910 if (cpi->oxcf.auto_key && | 1945 if (cpi->oxcf.auto_key && |
| 1911 lookup_next_frame_stats(twopass, &next_frame) != EOF) { | 1946 lookup_next_frame_stats(twopass, &next_frame) != EOF) { |
| 1947 double loop_decay_rate; |
| 1948 |
| 1912 // Check for a scene cut. | 1949 // Check for a scene cut. |
| 1913 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) | 1950 if (test_candidate_kf(twopass, &last_frame, this_frame, &next_frame)) |
| 1914 break; | 1951 break; |
| 1915 | 1952 |
| 1916 // How fast is the prediction quality decaying? | 1953 // How fast is the prediction quality decaying? |
| 1917 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); | 1954 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 1918 | 1955 |
| 1919 // We want to know something about the recent past... rather than | 1956 // We want to know something about the recent past... rather than |
| 1920 // as used elsewhere where we are concerned with decay in prediction | 1957 // as used elsewhere where we are concerned with decay in prediction |
| 1921 // quality since the last GF or KF. | 1958 // quality since the last GF or KF. |
| 1922 recent_loop_decay[i % 8] = loop_decay_rate; | 1959 recent_loop_decay[i % 8] = loop_decay_rate; |
| 1923 decay_accumulator = 1.0; | 1960 decay_accumulator = 1.0; |
| 1924 for (j = 0; j < 8; ++j) | 1961 for (j = 0; j < 8; ++j) |
| 1925 decay_accumulator *= recent_loop_decay[j]; | 1962 decay_accumulator *= recent_loop_decay[j]; |
| 1926 | 1963 |
| 1927 // Special check for transition or high motion followed by a | 1964 // Special check for transition or high motion followed by a |
| 1928 // static scene. | 1965 // static scene. |
| 1929 if (detect_transition_to_still(cpi, i, cpi->key_frame_frequency - i, | 1966 if (detect_transition_to_still(twopass, i, cpi->key_frame_frequency - i, |
| 1930 loop_decay_rate, decay_accumulator)) | 1967 loop_decay_rate, decay_accumulator)) |
| 1931 break; | 1968 break; |
| 1932 | 1969 |
| 1933 // Step on to the next frame. | 1970 // Step on to the next frame. |
| 1934 ++rc->frames_to_key; | 1971 ++rc->frames_to_key; |
| 1935 | 1972 |
| 1936 // If we don't have a real key frame within the next two | 1973 // If we don't have a real key frame within the next two |
| 1937 // key_frame_frequency intervals then break out of the loop. | 1974 // key_frame_frequency intervals then break out of the loop. |
| 1938 if (rc->frames_to_key >= 2 * (int)cpi->key_frame_frequency) | 1975 if (rc->frames_to_key >= 2 * (int)cpi->key_frame_frequency) |
| 1939 break; | 1976 break; |
| 1940 } else { | 1977 } else { |
| 1941 ++rc->frames_to_key; | 1978 ++rc->frames_to_key; |
| 1942 } | 1979 } |
| 1943 ++i; | 1980 ++i; |
| 1944 } | 1981 } |
| 1945 | 1982 |
| 1946 // If there is a max kf interval set by the user we must obey it. | 1983 // If there is a max kf interval set by the user we must obey it. |
| 1947 // We already breakout of the loop above at 2x max. | 1984 // We already breakout of the loop above at 2x max. |
| 1948 // This code centers the extra kf if the actual natural interval | 1985 // This code centers the extra kf if the actual natural interval |
| 1949 // is between 1x and 2x. | 1986 // is between 1x and 2x. |
| 1950 if (cpi->oxcf.auto_key && | 1987 if (cpi->oxcf.auto_key && |
| 1951 rc->frames_to_key > (int)cpi->key_frame_frequency) { | 1988 rc->frames_to_key > (int)cpi->key_frame_frequency) { |
| 1952 FIRSTPASS_STATS tmp_frame; | 1989 FIRSTPASS_STATS tmp_frame = first_frame; |
| 1953 | 1990 |
| 1954 rc->frames_to_key /= 2; | 1991 rc->frames_to_key /= 2; |
| 1955 | 1992 |
| 1956 // Copy first frame details. | |
| 1957 tmp_frame = first_frame; | |
| 1958 | |
| 1959 // Reset to the start of the group. | 1993 // Reset to the start of the group. |
| 1960 reset_fpf_position(twopass, start_position); | 1994 reset_fpf_position(twopass, start_position); |
| 1961 | 1995 |
| 1962 kf_group_err = 0; | 1996 kf_group_err = 0; |
| 1963 | 1997 |
| 1964 // Rescan to get the correct error data for the forced kf group. | 1998 // Rescan to get the correct error data for the forced kf group. |
| 1965 for (i = 0; i < rc->frames_to_key; ++i) { | 1999 for (i = 0; i < rc->frames_to_key; ++i) { |
| 1966 // Accumulate kf group errors. | |
| 1967 kf_group_err += calculate_modified_err(cpi, &tmp_frame); | 2000 kf_group_err += calculate_modified_err(cpi, &tmp_frame); |
| 1968 | |
| 1969 // Load the next frame's stats. | |
| 1970 input_stats(twopass, &tmp_frame); | 2001 input_stats(twopass, &tmp_frame); |
| 1971 } | 2002 } |
| 1972 rc->next_key_frame_forced = 1; | 2003 rc->next_key_frame_forced = 1; |
| 1973 } else if (twopass->stats_in == twopass->stats_in_end) { | 2004 } else if (twopass->stats_in == twopass->stats_in_end) { |
| 1974 rc->next_key_frame_forced = 1; | 2005 rc->next_key_frame_forced = 1; |
| 1975 } else { | 2006 } else { |
| 1976 rc->next_key_frame_forced = 0; | 2007 rc->next_key_frame_forced = 0; |
| 1977 } | 2008 } |
| 1978 | 2009 |
| 1979 // Special case for the last key frame of the file. | 2010 // Special case for the last key frame of the file. |
| 1980 if (twopass->stats_in >= twopass->stats_in_end) { | 2011 if (twopass->stats_in >= twopass->stats_in_end) { |
| 1981 // Accumulate kf group error. | 2012 // Accumulate kf group error. |
| 1982 kf_group_err += calculate_modified_err(cpi, this_frame); | 2013 kf_group_err += calculate_modified_err(cpi, this_frame); |
| 1983 } | 2014 } |
| 1984 | 2015 |
| 1985 // Calculate the number of bits that should be assigned to the kf group. | 2016 // Calculate the number of bits that should be assigned to the kf group. |
| 1986 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { | 2017 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { |
| 1987 // Maximum number of bits for a single normal frame (not key frame). | 2018 // Maximum number of bits for a single normal frame (not key frame). |
| 1988 int max_bits = frame_max_bits(cpi); | 2019 const int max_bits = frame_max_bits(rc, &cpi->oxcf); |
| 1989 | 2020 |
| 1990 // Maximum number of bits allocated to the key frame group. | 2021 // Maximum number of bits allocated to the key frame group. |
| 1991 int64_t max_grp_bits; | 2022 int64_t max_grp_bits; |
| 1992 | 2023 |
| 1993 // Default allocation based on bits left and relative | 2024 // Default allocation based on bits left and relative |
| 1994 // complexity of the section. | 2025 // complexity of the section. |
| 1995 twopass->kf_group_bits = (int64_t)(twopass->bits_left * | 2026 twopass->kf_group_bits = (int64_t)(twopass->bits_left * |
| 1996 (kf_group_err / twopass->modified_error_left)); | 2027 (kf_group_err / twopass->modified_error_left)); |
| 1997 | 2028 |
| 1998 // Clip based on maximum per frame rate defined by the user. | 2029 // Clip based on maximum per frame rate defined by the user. |
| 1999 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; | 2030 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; |
| 2000 if (twopass->kf_group_bits > max_grp_bits) | 2031 if (twopass->kf_group_bits > max_grp_bits) |
| 2001 twopass->kf_group_bits = max_grp_bits; | 2032 twopass->kf_group_bits = max_grp_bits; |
| 2002 } else { | 2033 } else { |
| 2003 twopass->kf_group_bits = 0; | 2034 twopass->kf_group_bits = 0; |
| 2004 } | 2035 } |
| 2005 // Reset the first pass file position. | 2036 // Reset the first pass file position. |
| 2006 reset_fpf_position(twopass, start_position); | 2037 reset_fpf_position(twopass, start_position); |
| 2007 | 2038 |
| 2008 // Determine how big to make this keyframe based on how well the subsequent | 2039 // Determine how big to make this keyframe based on how well the subsequent |
| 2009 // frames use inter blocks. | 2040 // frames use inter blocks. |
| 2010 decay_accumulator = 1.0; | 2041 decay_accumulator = 1.0; |
| 2011 boost_score = 0.0; | 2042 boost_score = 0.0; |
| 2012 | 2043 |
| 2013 // Scan through the kf group collating various stats. | 2044 // Scan through the kf group collating various stats. |
| 2014 for (i = 0; i < rc->frames_to_key; ++i) { | 2045 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2015 double r; | |
| 2016 | |
| 2017 if (EOF == input_stats(twopass, &next_frame)) | 2046 if (EOF == input_stats(twopass, &next_frame)) |
| 2018 break; | 2047 break; |
| 2019 | 2048 |
| 2020 // Monitor for static sections. | 2049 // Monitor for static sections. |
| 2021 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 2050 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 2022 zero_motion_accumulator) { | 2051 zero_motion_accumulator) { |
| 2023 zero_motion_accumulator = | 2052 zero_motion_accumulator = (next_frame.pcnt_inter - |
| 2024 (next_frame.pcnt_inter - next_frame.pcnt_motion); | 2053 next_frame.pcnt_motion); |
| 2025 } | 2054 } |
| 2026 | 2055 |
| 2027 // For the first few frames collect data to decide kf boost. | 2056 // For the first few frames collect data to decide kf boost. |
| 2028 if (i <= (rc->max_gf_interval * 2)) { | 2057 if (i <= (rc->max_gf_interval * 2)) { |
| 2058 double r; |
| 2029 if (next_frame.intra_error > twopass->kf_intra_err_min) | 2059 if (next_frame.intra_error > twopass->kf_intra_err_min) |
| 2030 r = (IIKFACTOR2 * next_frame.intra_error / | 2060 r = (IIKFACTOR2 * next_frame.intra_error / |
| 2031 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2061 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2032 else | 2062 else |
| 2033 r = (IIKFACTOR2 * twopass->kf_intra_err_min / | 2063 r = (IIKFACTOR2 * twopass->kf_intra_err_min / |
| 2034 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2064 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2035 | 2065 |
| 2036 if (r > RMAX) | 2066 if (r > RMAX) |
| 2037 r = RMAX; | 2067 r = RMAX; |
| 2038 | 2068 |
| 2039 // How fast is prediction quality decaying. | 2069 // How fast is prediction quality decaying. |
| 2040 if (!detect_flash(twopass, 0)) { | 2070 if (!detect_flash(twopass, 0)) { |
| 2041 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); | 2071 const double loop_decay_rate = get_prediction_decay_rate(&cpi->common, |
| 2072 &next_frame); |
| 2042 decay_accumulator *= loop_decay_rate; | 2073 decay_accumulator *= loop_decay_rate; |
| 2043 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 2074 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); |
| 2044 ? MIN_DECAY_FACTOR : decay_accumulator; | |
| 2045 } | 2075 } |
| 2046 | 2076 |
| 2047 boost_score += (decay_accumulator * r); | 2077 boost_score += (decay_accumulator * r); |
| 2048 } | 2078 } |
| 2049 } | 2079 } |
| 2050 | 2080 |
| 2051 { | 2081 { |
| 2052 FIRSTPASS_STATS sectionstats; | 2082 FIRSTPASS_STATS sectionstats; |
| 2053 | 2083 |
| 2054 zero_stats(§ionstats); | 2084 zero_stats(§ionstats); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 2065 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | 2095 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
| 2066 } | 2096 } |
| 2067 | 2097 |
| 2068 // Reset the first pass file position. | 2098 // Reset the first pass file position. |
| 2069 reset_fpf_position(twopass, start_position); | 2099 reset_fpf_position(twopass, start_position); |
| 2070 | 2100 |
| 2071 // Work out how many bits to allocate for the key frame itself. | 2101 // Work out how many bits to allocate for the key frame itself. |
| 2072 if (1) { | 2102 if (1) { |
| 2073 int kf_boost = (int)boost_score; | 2103 int kf_boost = (int)boost_score; |
| 2074 int allocation_chunks; | 2104 int allocation_chunks; |
| 2075 int alt_kf_bits; | |
| 2076 | 2105 |
| 2077 if (kf_boost < (rc->frames_to_key * 3)) | 2106 if (kf_boost < (rc->frames_to_key * 3)) |
| 2078 kf_boost = (rc->frames_to_key * 3); | 2107 kf_boost = (rc->frames_to_key * 3); |
| 2079 | 2108 |
| 2080 if (kf_boost < MIN_KF_BOOST) | 2109 if (kf_boost < MIN_KF_BOOST) |
| 2081 kf_boost = MIN_KF_BOOST; | 2110 kf_boost = MIN_KF_BOOST; |
| 2082 | 2111 |
| 2083 // Make a note of baseline boost and the zero motion | 2112 // Make a note of baseline boost and the zero motion |
| 2084 // accumulator value for use elsewhere. | 2113 // accumulator value for use elsewhere. |
| 2085 rc->kf_boost = kf_boost; | 2114 rc->kf_boost = kf_boost; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2099 // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is | 2128 // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is |
| 2100 // taken care of by kf_boost. | 2129 // taken care of by kf_boost. |
| 2101 if (zero_motion_accumulator >= 0.99) { | 2130 if (zero_motion_accumulator >= 0.99) { |
| 2102 allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost; | 2131 allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost; |
| 2103 } else { | 2132 } else { |
| 2104 allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost; | 2133 allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost; |
| 2105 } | 2134 } |
| 2106 | 2135 |
| 2107 // Prevent overflow. | 2136 // Prevent overflow. |
| 2108 if (kf_boost > 1028) { | 2137 if (kf_boost > 1028) { |
| 2109 int divisor = kf_boost >> 10; | 2138 const int divisor = kf_boost >> 10; |
| 2110 kf_boost /= divisor; | 2139 kf_boost /= divisor; |
| 2111 allocation_chunks /= divisor; | 2140 allocation_chunks /= divisor; |
| 2112 } | 2141 } |
| 2113 | 2142 |
| 2114 twopass->kf_group_bits = (twopass->kf_group_bits < 0) ? 0 | 2143 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits); |
| 2115 : twopass->kf_group_bits; | |
| 2116 | |
| 2117 // Calculate the number of bits to be spent on the key frame. | 2144 // Calculate the number of bits to be spent on the key frame. |
| 2118 twopass->kf_bits = (int)((double)kf_boost * | 2145 twopass->kf_bits = (int)((double)kf_boost * |
| 2119 ((double)twopass->kf_group_bits / allocation_chunks)); | 2146 ((double)twopass->kf_group_bits / allocation_chunks)); |
| 2120 | 2147 |
| 2121 // If the key frame is actually easier than the average for the | 2148 // If the key frame is actually easier than the average for the |
| 2122 // kf group (which does sometimes happen, e.g. a blank intro frame) | 2149 // kf group (which does sometimes happen, e.g. a blank intro frame) |
| 2123 // then use an alternate calculation based on the kf error score | 2150 // then use an alternate calculation based on the kf error score |
| 2124 // which should give a smaller key frame. | 2151 // which should give a smaller key frame. |
| 2125 if (kf_mod_err < kf_group_err / rc->frames_to_key) { | 2152 if (kf_mod_err < kf_group_err / rc->frames_to_key) { |
| 2126 double alt_kf_grp_bits = ((double)twopass->bits_left * | 2153 double alt_kf_grp_bits = ((double)twopass->bits_left * |
| 2127 (kf_mod_err * (double)rc->frames_to_key) / | 2154 (kf_mod_err * (double)rc->frames_to_key) / |
| 2128 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)); | 2155 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)); |
| 2129 | 2156 |
| 2130 alt_kf_bits = (int)((double)kf_boost * | 2157 const int alt_kf_bits = (int)((double)kf_boost * |
| 2131 (alt_kf_grp_bits / (double)allocation_chunks)); | 2158 (alt_kf_grp_bits / (double)allocation_chunks)); |
| 2132 | 2159 |
| 2133 if (twopass->kf_bits > alt_kf_bits) | 2160 if (twopass->kf_bits > alt_kf_bits) |
| 2134 twopass->kf_bits = alt_kf_bits; | 2161 twopass->kf_bits = alt_kf_bits; |
| 2135 } else { | 2162 } else { |
| 2136 // Else if it is much harder than other frames in the group make sure | 2163 // Else if it is much harder than other frames in the group make sure |
| 2137 // it at least receives an allocation in keeping with its relative | 2164 // it at least receives an allocation in keeping with its relative |
| 2138 // error score. | 2165 // error score. |
| 2139 alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err / | 2166 const int alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err / |
| 2140 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left))); | 2167 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left))); |
| 2141 | 2168 |
| 2142 if (alt_kf_bits > twopass->kf_bits) { | 2169 if (alt_kf_bits > twopass->kf_bits) |
| 2143 twopass->kf_bits = alt_kf_bits; | 2170 twopass->kf_bits = alt_kf_bits; |
| 2144 } | |
| 2145 } | 2171 } |
| 2146 twopass->kf_group_bits -= twopass->kf_bits; | 2172 twopass->kf_group_bits -= twopass->kf_bits; |
| 2147 // Per frame bit target for this frame. | 2173 // Per frame bit target for this frame. |
| 2148 vp9_rc_set_frame_target(cpi, twopass->kf_bits); | 2174 vp9_rc_set_frame_target(cpi, twopass->kf_bits); |
| 2149 } | 2175 } |
| 2150 | 2176 |
| 2151 // Note the total error score of the kf group minus the key frame itself. | 2177 // Note the total error score of the kf group minus the key frame itself. |
| 2152 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2178 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2153 | 2179 |
| 2154 // Adjust the count of total modified error left. | 2180 // Adjust the count of total modified error left. |
| 2155 // The count of bits left is adjusted elsewhere based on real coded frame | 2181 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2156 // sizes. | 2182 // sizes. |
| 2157 twopass->modified_error_left -= kf_group_err; | 2183 twopass->modified_error_left -= kf_group_err; |
| 2158 } | 2184 } |
| 2159 | 2185 |
| 2160 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) { | 2186 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) { |
| 2161 VP9_COMMON *const cm = &cpi->common; | 2187 VP9_COMMON *const cm = &cpi->common; |
| 2162 if (!cpi->refresh_alt_ref_frame && | 2188 if (!cpi->refresh_alt_ref_frame && |
| 2163 (cm->current_video_frame == 0 || | 2189 (cm->current_video_frame == 0 || |
| 2164 cm->frame_flags & FRAMEFLAGS_KEY)) { | 2190 (cm->frame_flags & FRAMEFLAGS_KEY))) { |
| 2165 cm->frame_type = KEY_FRAME; | 2191 cm->frame_type = KEY_FRAME; |
| 2166 } else { | 2192 } else { |
| 2167 cm->frame_type = INTER_FRAME; | 2193 cm->frame_type = INTER_FRAME; |
| 2168 } | 2194 } |
| 2169 // Do not use periodic key frames. | 2195 // Do not use periodic key frames. |
| 2170 cpi->rc.frames_to_key = INT_MAX; | 2196 cpi->rc.frames_to_key = INT_MAX; |
| 2171 } | 2197 } |
| 2172 | 2198 |
| 2173 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { | 2199 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2174 VP9_COMMON *const cm = &cpi->common; | 2200 VP9_COMMON *const cm = &cpi->common; |
| 2175 RATE_CONTROL *const rc = &cpi->rc; | 2201 RATE_CONTROL *const rc = &cpi->rc; |
| 2176 struct twopass_rc *const twopass = &cpi->twopass; | 2202 struct twopass_rc *const twopass = &cpi->twopass; |
| 2177 const int frames_left = (int)(twopass->total_stats.count - | 2203 int frames_left; |
| 2178 cm->current_video_frame); | |
| 2179 FIRSTPASS_STATS this_frame; | 2204 FIRSTPASS_STATS this_frame; |
| 2180 FIRSTPASS_STATS this_frame_copy; | 2205 FIRSTPASS_STATS this_frame_copy; |
| 2181 | 2206 |
| 2182 double this_frame_intra_error; | 2207 double this_frame_intra_error; |
| 2183 double this_frame_coded_error; | 2208 double this_frame_coded_error; |
| 2184 int target; | 2209 int target; |
| 2210 LAYER_CONTEXT *lc = NULL; |
| 2211 int is_spatial_svc = (cpi->use_svc && cpi->svc.number_temporal_layers == 1); |
| 2212 |
| 2213 if (is_spatial_svc) { |
| 2214 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 2215 frames_left = (int)(twopass->total_stats.count - |
| 2216 lc->current_video_frame_in_layer); |
| 2217 } else { |
| 2218 frames_left = (int)(twopass->total_stats.count - |
| 2219 cm->current_video_frame); |
| 2220 } |
| 2185 | 2221 |
| 2186 if (!twopass->stats_in) | 2222 if (!twopass->stats_in) |
| 2187 return; | 2223 return; |
| 2188 | 2224 |
| 2189 if (cpi->refresh_alt_ref_frame) { | 2225 if (cpi->refresh_alt_ref_frame) { |
| 2190 cm->frame_type = INTER_FRAME; | 2226 cm->frame_type = INTER_FRAME; |
| 2191 vp9_rc_set_frame_target(cpi, twopass->gf_bits); | 2227 vp9_rc_set_frame_target(cpi, twopass->gf_bits); |
| 2192 return; | 2228 return; |
| 2193 } | 2229 } |
| 2194 | 2230 |
| 2195 vp9_clear_system_state(); | 2231 vp9_clear_system_state(); |
| 2196 | 2232 |
| 2233 if (is_spatial_svc && twopass->kf_intra_err_min == 0) { |
| 2234 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
| 2235 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
| 2236 } |
| 2237 |
| 2197 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | 2238 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
| 2198 twopass->active_worst_quality = cpi->oxcf.cq_level; | 2239 twopass->active_worst_quality = cpi->oxcf.cq_level; |
| 2199 } else if (cm->current_video_frame == 0) { | 2240 } else if (cm->current_video_frame == 0 || |
| 2241 (is_spatial_svc && lc->current_video_frame_in_layer == 0)) { |
| 2200 // Special case code for first frame. | 2242 // Special case code for first frame. |
| 2201 const int section_target_bandwidth = (int)(twopass->bits_left / | 2243 const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2202 frames_left); | 2244 frames_left); |
| 2203 const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats, | 2245 const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats, |
| 2204 section_target_bandwidth); | 2246 section_target_bandwidth); |
| 2205 twopass->active_worst_quality = tmp_q; | 2247 twopass->active_worst_quality = tmp_q; |
| 2206 rc->ni_av_qi = tmp_q; | 2248 rc->ni_av_qi = tmp_q; |
| 2207 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); | 2249 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); |
| 2208 } | 2250 } |
| 2209 vp9_zero(this_frame); | 2251 vp9_zero(this_frame); |
| 2210 if (EOF == input_stats(twopass, &this_frame)) | 2252 if (EOF == input_stats(twopass, &this_frame)) |
| 2211 return; | 2253 return; |
| 2212 | 2254 |
| 2213 this_frame_intra_error = this_frame.intra_error; | 2255 this_frame_intra_error = this_frame.intra_error; |
| 2214 this_frame_coded_error = this_frame.coded_error; | 2256 this_frame_coded_error = this_frame.coded_error; |
| 2215 | 2257 |
| 2216 // Keyframe and section processing. | 2258 // Keyframe and section processing. |
| 2217 if (rc->frames_to_key == 0 || | 2259 if (rc->frames_to_key == 0 || |
| 2218 (cm->frame_flags & FRAMEFLAGS_KEY)) { | 2260 (cm->frame_flags & FRAMEFLAGS_KEY)) { |
| 2219 // Define next KF group and assign bits to it. | 2261 // Define next KF group and assign bits to it. |
| 2220 this_frame_copy = this_frame; | 2262 this_frame_copy = this_frame; |
| 2221 find_next_key_frame(cpi, &this_frame_copy); | 2263 find_next_key_frame(cpi, &this_frame_copy); |
| 2264 // Don't place key frame in any enhancement layers in spatial svc |
| 2265 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1 && |
| 2266 cpi->svc.spatial_layer_id > 0) { |
| 2267 cm->frame_type = INTER_FRAME; |
| 2268 } |
| 2222 } else { | 2269 } else { |
| 2223 cm->frame_type = INTER_FRAME; | 2270 cm->frame_type = INTER_FRAME; |
| 2224 } | 2271 } |
| 2225 | 2272 |
| 2226 // Is this frame a GF / ARF? (Note: a key frame is always also a GF). | 2273 // Is this frame a GF / ARF? (Note: a key frame is always also a GF). |
| 2227 if (rc->frames_till_gf_update_due == 0) { | 2274 if (rc->frames_till_gf_update_due == 0) { |
| 2228 // Define next gf group and assign bits to it. | 2275 // Define next gf group and assign bits to it. |
| 2229 this_frame_copy = this_frame; | 2276 this_frame_copy = this_frame; |
| 2230 | 2277 |
| 2231 #if CONFIG_MULTIPLE_ARF | 2278 #if CONFIG_MULTIPLE_ARF |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2271 if (cpi->common.frame_type == KEY_FRAME) | 2318 if (cpi->common.frame_type == KEY_FRAME) |
| 2272 target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target); | 2319 target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target); |
| 2273 else | 2320 else |
| 2274 target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target); | 2321 target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target); |
| 2275 vp9_rc_set_frame_target(cpi, target); | 2322 vp9_rc_set_frame_target(cpi, target); |
| 2276 | 2323 |
| 2277 // Update the total stats remaining structure. | 2324 // Update the total stats remaining structure. |
| 2278 subtract_stats(&twopass->total_left_stats, &this_frame); | 2325 subtract_stats(&twopass->total_left_stats, &this_frame); |
| 2279 } | 2326 } |
| 2280 | 2327 |
| 2281 void vp9_twopass_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { | 2328 void vp9_twopass_postencode_update(VP9_COMP *cpi) { |
| 2282 #ifdef DISABLE_RC_LONG_TERM_MEM | 2329 #ifdef DISABLE_RC_LONG_TERM_MEM |
| 2283 cpi->twopass.bits_left -= cpi->rc.this_frame_target; | 2330 const uint64_t bits_used = cpi->rc.this_frame_target; |
| 2284 #else | 2331 #else |
| 2285 cpi->twopass.bits_left -= 8 * bytes_used; | 2332 const uint64_t bits_used = cpi->rc.projected_frame_size; |
| 2333 #endif |
| 2334 cpi->twopass.bits_left -= bits_used; |
| 2335 cpi->twopass.bits_left = MAX(cpi->twopass.bits_left, 0); |
| 2286 // Update bits left to the kf and gf groups to account for overshoot or | 2336 // Update bits left to the kf and gf groups to account for overshoot or |
| 2287 // undershoot on these frames. | 2337 // undershoot on these frames. |
| 2288 if (cm->frame_type == KEY_FRAME) { | 2338 if (cpi->common.frame_type == KEY_FRAME) { |
| 2289 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - | 2339 // For key frames kf_group_bits already had the target bits subtracted out. |
| 2290 cpi->rc.projected_frame_size; | 2340 // So now update to the correct value based on the actual bits used. |
| 2291 | 2341 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - bits_used; |
| 2292 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); | 2342 } else { |
| 2293 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { | 2343 cpi->twopass.kf_group_bits -= bits_used; |
| 2294 cpi->twopass.gf_group_bits += cpi->rc.this_frame_target - | 2344 cpi->twopass.gf_group_bits -= bits_used; |
| 2295 cpi->rc.projected_frame_size; | |
| 2296 | |
| 2297 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); | 2345 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); |
| 2298 } | 2346 } |
| 2299 #endif | 2347 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); |
| 2300 } | 2348 } |
| OLD | NEW |