Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_firstpass.c

Issue 1302353004: libvpx: Pull from upstream (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libvpx.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_fastssim.c ('k') | source/libvpx/vp9/encoder/vp9_lookahead.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_dsp_rtcd.h" 15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h" 16 #include "./vpx_scale_rtcd.h"
17 17
18 #include "vpx_mem/vpx_mem.h" 18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_ports/mem.h" 19 #include "vpx_ports/mem.h"
20 #include "vpx_ports/system_state.h"
20 #include "vpx_scale/vpx_scale.h" 21 #include "vpx_scale/vpx_scale.h"
21 #include "vpx_scale/yv12config.h" 22 #include "vpx_scale/yv12config.h"
22 23
23 #include "vp9/common/vp9_entropymv.h" 24 #include "vp9/common/vp9_entropymv.h"
24 #include "vp9/common/vp9_quant_common.h" 25 #include "vp9/common/vp9_quant_common.h"
25 #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes() 26 #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes()
26 #include "vp9/common/vp9_systemdependent.h"
27 #include "vp9/encoder/vp9_aq_variance.h" 27 #include "vp9/encoder/vp9_aq_variance.h"
28 #include "vp9/encoder/vp9_block.h" 28 #include "vp9/encoder/vp9_block.h"
29 #include "vp9/encoder/vp9_encodeframe.h" 29 #include "vp9/encoder/vp9_encodeframe.h"
30 #include "vp9/encoder/vp9_encodemb.h" 30 #include "vp9/encoder/vp9_encodemb.h"
31 #include "vp9/encoder/vp9_encodemv.h" 31 #include "vp9/encoder/vp9_encodemv.h"
32 #include "vp9/encoder/vp9_encoder.h" 32 #include "vp9/encoder/vp9_encoder.h"
33 #include "vp9/encoder/vp9_extend.h" 33 #include "vp9/encoder/vp9_extend.h"
34 #include "vp9/encoder/vp9_firstpass.h" 34 #include "vp9/encoder/vp9_firstpass.h"
35 #include "vp9/encoder/vp9_mcomp.h" 35 #include "vp9/encoder/vp9_mcomp.h"
36 #include "vp9/encoder/vp9_quantize.h" 36 #include "vp9/encoder/vp9_quantize.h"
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 section->new_mv_count -= frame->new_mv_count; 230 section->new_mv_count -= frame->new_mv_count;
231 section->count -= frame->count; 231 section->count -= frame->count;
232 section->duration -= frame->duration; 232 section->duration -= frame->duration;
233 } 233 }
234 234
235 // Calculate an active area of the image that discounts formatting 235 // Calculate an active area of the image that discounts formatting
236 // bars and partially discounts other 0 energy areas. 236 // bars and partially discounts other 0 energy areas.
237 #define MIN_ACTIVE_AREA 0.5 237 #define MIN_ACTIVE_AREA 0.5
238 #define MAX_ACTIVE_AREA 1.0 238 #define MAX_ACTIVE_AREA 1.0
239 static double calculate_active_area(const VP9_COMP *cpi, 239 static double calculate_active_area(const VP9_COMP *cpi,
240 const FIRSTPASS_STATS *this_frame) 240 const FIRSTPASS_STATS *this_frame) {
241 {
242 double active_pct; 241 double active_pct;
243 242
244 active_pct = 1.0 - 243 active_pct = 1.0 -
245 ((this_frame->intra_skip_pct / 2) + 244 ((this_frame->intra_skip_pct / 2) +
246 ((this_frame->inactive_zone_rows * 2) / (double)cpi->common.mb_rows)); 245 ((this_frame->inactive_zone_rows * 2) / (double)cpi->common.mb_rows));
247 return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA); 246 return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA);
248 } 247 }
249 248
250 // Calculate a modified Error used in distributing bits between easier and 249 // Calculate a modified Error used in distributing bits between easier and
251 // harder frames. 250 // harder frames.
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 const vpx_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd); 374 const vpx_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
376 fn(src->buf, src->stride, ref->buf, ref->stride, &sse); 375 fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
377 return sse; 376 return sse;
378 } 377 }
379 #endif // CONFIG_VP9_HIGHBITDEPTH 378 #endif // CONFIG_VP9_HIGHBITDEPTH
380 379
381 // Refine the motion search range according to the frame dimension 380 // Refine the motion search range according to the frame dimension
382 // for first pass test. 381 // for first pass test.
383 static int get_search_range(const VP9_COMP *cpi) { 382 static int get_search_range(const VP9_COMP *cpi) {
384 int sr = 0; 383 int sr = 0;
385 const int dim = MIN(cpi->initial_width, cpi->initial_height); 384 const int dim = VPXMIN(cpi->initial_width, cpi->initial_height);
386 385
387 while ((dim << sr) < MAX_FULL_PEL_VAL) 386 while ((dim << sr) < MAX_FULL_PEL_VAL)
388 ++sr; 387 ++sr;
389 return sr; 388 return sr;
390 } 389 }
391 390
392 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, 391 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
393 const MV *ref_mv, MV *best_mv, 392 const MV *ref_mv, MV *best_mv,
394 int *best_motion_err) { 393 int *best_motion_err) {
395 MACROBLOCKD *const xd = &x->e_mbd; 394 MACROBLOCKD *const xd = &x->e_mbd;
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 // First pass code requires valid last and new frame buffers. 538 // First pass code requires valid last and new frame buffers.
540 assert(new_yv12 != NULL); 539 assert(new_yv12 != NULL);
541 assert((lc != NULL) || frame_is_intra_only(cm) || (lst_yv12 != NULL)); 540 assert((lc != NULL) || frame_is_intra_only(cm) || (lst_yv12 != NULL));
542 541
543 #if CONFIG_FP_MB_STATS 542 #if CONFIG_FP_MB_STATS
544 if (cpi->use_fp_mb_stats) { 543 if (cpi->use_fp_mb_stats) {
545 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs); 544 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs);
546 } 545 }
547 #endif 546 #endif
548 547
549 vp9_clear_system_state(); 548 vpx_clear_system_state();
550 549
551 intra_factor = 0.0; 550 intra_factor = 0.0;
552 brightness_factor = 0.0; 551 brightness_factor = 0.0;
553 neutral_count = 0.0; 552 neutral_count = 0.0;
554 553
555 set_first_pass_params(cpi); 554 set_first_pass_params(cpi);
556 vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth)); 555 vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
557 556
558 if (lc != NULL) { 557 if (lc != NULL) {
559 twopass = &lc->twopass; 558 twopass = &lc->twopass;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 int this_error; 649 int this_error;
651 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); 650 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
652 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); 651 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
653 double log_intra; 652 double log_intra;
654 int level_sample; 653 int level_sample;
655 654
656 #if CONFIG_FP_MB_STATS 655 #if CONFIG_FP_MB_STATS
657 const int mb_index = mb_row * cm->mb_cols + mb_col; 656 const int mb_index = mb_row * cm->mb_cols + mb_col;
658 #endif 657 #endif
659 658
660 vp9_clear_system_state(); 659 vpx_clear_system_state();
661 660
662 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; 661 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
663 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; 662 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
664 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; 663 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
665 xd->left_available = (mb_col != 0); 664 xd->left_available = (mb_col != 0);
666 xd->mi[0]->mbmi.sb_type = bsize; 665 xd->mi[0]->mbmi.sb_type = bsize;
667 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; 666 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME;
668 set_mi_row_col(xd, &tile, 667 set_mi_row_col(xd, &tile,
669 mb_row << 1, num_8x8_blocks_high_lookup[bsize], 668 mb_row << 1, num_8x8_blocks_high_lookup[bsize],
670 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], 669 mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
(...skipping 30 matching lines...) Expand all
701 this_error >>= 8; 700 this_error >>= 8;
702 break; 701 break;
703 default: 702 default:
704 assert(0 && "cm->bit_depth should be VPX_BITS_8, " 703 assert(0 && "cm->bit_depth should be VPX_BITS_8, "
705 "VPX_BITS_10 or VPX_BITS_12"); 704 "VPX_BITS_10 or VPX_BITS_12");
706 return; 705 return;
707 } 706 }
708 } 707 }
709 #endif // CONFIG_VP9_HIGHBITDEPTH 708 #endif // CONFIG_VP9_HIGHBITDEPTH
710 709
711 vp9_clear_system_state(); 710 vpx_clear_system_state();
712 log_intra = log(this_error + 1.0); 711 log_intra = log(this_error + 1.0);
713 if (log_intra < 10.0) 712 if (log_intra < 10.0)
714 intra_factor += 1.0 + ((10.0 - log_intra) * 0.05); 713 intra_factor += 1.0 + ((10.0 - log_intra) * 0.05);
715 else 714 else
716 intra_factor += 1.0; 715 intra_factor += 1.0;
717 716
718 #if CONFIG_VP9_HIGHBITDEPTH 717 #if CONFIG_VP9_HIGHBITDEPTH
719 if (cm->use_highbitdepth) 718 if (cm->use_highbitdepth)
720 level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0]; 719 level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0];
721 else 720 else
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK; 870 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
872 if (this_error > FPMB_ERROR_LARGE_TH) { 871 if (this_error > FPMB_ERROR_LARGE_TH) {
873 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK; 872 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
874 } else if (this_error < FPMB_ERROR_SMALL_TH) { 873 } else if (this_error < FPMB_ERROR_SMALL_TH) {
875 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK; 874 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
876 } 875 }
877 } 876 }
878 #endif 877 #endif
879 878
880 if (motion_error <= this_error) { 879 if (motion_error <= this_error) {
881 vp9_clear_system_state(); 880 vpx_clear_system_state();
882 881
883 // Keep a count of cases where the inter and intra were very close 882 // Keep a count of cases where the inter and intra were very close
884 // and very low. This helps with scene cut detection for example in 883 // and very low. This helps with scene cut detection for example in
885 // cropped clips with black bars at the sides or top and bottom. 884 // cropped clips with black bars at the sides or top and bottom.
886 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && 885 if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
887 (this_error < (2 * intrapenalty))) { 886 (this_error < (2 * intrapenalty))) {
888 neutral_count += 1.0; 887 neutral_count += 1.0;
889 // Also track cases where the intra is not much worse than the inter 888 // Also track cases where the intra is not much worse than the inter
890 // and use this in limiting the GF/arf group length. 889 // and use this in limiting the GF/arf group length.
891 } else if ((this_error > NCOUNT_INTRA_THRESH) && 890 } else if ((this_error > NCOUNT_INTRA_THRESH) &&
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 recon_uvoffset += uv_mb_height; 1005 recon_uvoffset += uv_mb_height;
1007 } 1006 }
1008 1007
1009 // Adjust to the next row of MBs. 1008 // Adjust to the next row of MBs.
1010 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols; 1009 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
1011 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride - 1010 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
1012 uv_mb_height * cm->mb_cols; 1011 uv_mb_height * cm->mb_cols;
1013 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride - 1012 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
1014 uv_mb_height * cm->mb_cols; 1013 uv_mb_height * cm->mb_cols;
1015 1014
1016 vp9_clear_system_state(); 1015 vpx_clear_system_state();
1017 } 1016 }
1018 1017
1019 // Clamp the image start to rows/2. This number of rows is discarded top 1018 // Clamp the image start to rows/2. This number of rows is discarded top
1020 // and bottom as dead data so rows / 2 means the frame is blank. 1019 // and bottom as dead data so rows / 2 means the frame is blank.
1021 if ((image_data_start_row > cm->mb_rows / 2) || 1020 if ((image_data_start_row > cm->mb_rows / 2) ||
1022 (image_data_start_row == INVALID_ROW)) { 1021 (image_data_start_row == INVALID_ROW)) {
1023 image_data_start_row = cm->mb_rows / 2; 1022 image_data_start_row = cm->mb_rows / 2;
1024 } 1023 }
1025 // Exclude any image dead zone 1024 // Exclude any image dead zone
1026 if (image_data_start_row > 0) { 1025 if (image_data_start_row > 0) {
1027 intra_skip_count = 1026 intra_skip_count =
1028 MAX(0, intra_skip_count - (image_data_start_row * cm->mb_cols * 2)); 1027 VPXMAX(0, intra_skip_count - (image_data_start_row * cm->mb_cols * 2));
1029 } 1028 }
1030 1029
1031 { 1030 {
1032 FIRSTPASS_STATS fps; 1031 FIRSTPASS_STATS fps;
1033 // The minimum error here insures some bit allocation to frames even 1032 // The minimum error here insures some bit allocation to frames even
1034 // in static regions. The allocation per MB declines for larger formats 1033 // in static regions. The allocation per MB declines for larger formats
1035 // where the typical "real" energy per MB also falls. 1034 // where the typical "real" energy per MB also falls.
1036 // Initial estimate here uses sqrt(mbs) to define the min_err, where the 1035 // Initial estimate here uses sqrt(mbs) to define the min_err, where the
1037 // number of mbs is proportional to the image area. 1036 // number of mbs is proportional to the image area.
1038 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) 1037 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { 1105 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
1107 if (gld_yv12 != NULL) { 1106 if (gld_yv12 != NULL) {
1108 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx], 1107 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
1109 cm->ref_frame_map[cpi->lst_fb_idx]); 1108 cm->ref_frame_map[cpi->lst_fb_idx]);
1110 } 1109 }
1111 twopass->sr_update_lag = 1; 1110 twopass->sr_update_lag = 1;
1112 } else { 1111 } else {
1113 ++twopass->sr_update_lag; 1112 ++twopass->sr_update_lag;
1114 } 1113 }
1115 1114
1116 vp9_extend_frame_borders(new_yv12); 1115 vpx_extend_frame_borders(new_yv12);
1117 1116
1118 if (lc != NULL) { 1117 if (lc != NULL) {
1119 vp9_update_reference_frames(cpi); 1118 vp9_update_reference_frames(cpi);
1120 } else { 1119 } else {
1121 // The frame we just compressed now becomes the last frame. 1120 // The frame we just compressed now becomes the last frame.
1122 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx], 1121 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
1123 cm->new_fb_idx); 1122 cm->new_fb_idx);
1124 } 1123 }
1125 1124
1126 // Special case for the first frame. Copy into the GF buffer as a second 1125 // Special case for the first frame. Copy into the GF buffer as a second
(...skipping 28 matching lines...) Expand all
1155 static double calc_correction_factor(double err_per_mb, 1154 static double calc_correction_factor(double err_per_mb,
1156 double err_divisor, 1155 double err_divisor,
1157 double pt_low, 1156 double pt_low,
1158 double pt_high, 1157 double pt_high,
1159 int q, 1158 int q,
1160 vpx_bit_depth_t bit_depth) { 1159 vpx_bit_depth_t bit_depth) {
1161 const double error_term = err_per_mb / err_divisor; 1160 const double error_term = err_per_mb / err_divisor;
1162 1161
1163 // Adjustment based on actual quantizer to power term. 1162 // Adjustment based on actual quantizer to power term.
1164 const double power_term = 1163 const double power_term =
1165 MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high); 1164 VPXMIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high);
1166 1165
1167 // Calculate correction factor. 1166 // Calculate correction factor.
1168 if (power_term < 1.0) 1167 if (power_term < 1.0)
1169 assert(error_term >= 0.0); 1168 assert(error_term >= 0.0);
1170 1169
1171 return fclamp(pow(error_term, power_term), 0.05, 5.0); 1170 return fclamp(pow(error_term, power_term), 0.05, 5.0);
1172 } 1171 }
1173 1172
1174 // Larger image formats are expected to be a little harder to code relatively 1173 // Larger image formats are expected to be a little harder to code relatively
1175 // given the same prediction error score. This in part at least relates to the 1174 // given the same prediction error score. This in part at least relates to the
1176 // increased size and hence coding cost of motion vectors. 1175 // increased size and hence coding cost of motion vectors.
1177 #define EDIV_SIZE_FACTOR 800 1176 #define EDIV_SIZE_FACTOR 800
1178 1177
1179 static int get_twopass_worst_quality(const VP9_COMP *cpi, 1178 static int get_twopass_worst_quality(const VP9_COMP *cpi,
1180 const double section_err, 1179 const double section_err,
1181 double inactive_zone, 1180 double inactive_zone,
1182 int section_target_bandwidth, 1181 int section_target_bandwidth,
1183 double group_weight_factor) { 1182 double group_weight_factor) {
1184 const RATE_CONTROL *const rc = &cpi->rc; 1183 const RATE_CONTROL *const rc = &cpi->rc;
1185 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1184 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1186 1185
1187 inactive_zone = fclamp(inactive_zone, 0.0, 1.0); 1186 inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
1188 1187
1189 if (section_target_bandwidth <= 0) { 1188 if (section_target_bandwidth <= 0) {
1190 return rc->worst_quality; // Highest value allowed 1189 return rc->worst_quality; // Highest value allowed
1191 } else { 1190 } else {
1192 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) 1191 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1193 ? cpi->initial_mbs : cpi->common.MBs; 1192 ? cpi->initial_mbs : cpi->common.MBs;
1194 const int active_mbs = MAX(1, num_mbs - (int)(num_mbs * inactive_zone)); 1193 const int active_mbs = VPXMAX(1, num_mbs - (int)(num_mbs * inactive_zone));
1195 const double av_err_per_mb = section_err / active_mbs; 1194 const double av_err_per_mb = section_err / active_mbs;
1196 const double speed_term = 1.0 + 0.04 * oxcf->speed; 1195 const double speed_term = 1.0 + 0.04 * oxcf->speed;
1197 const double ediv_size_correction = (double)num_mbs / EDIV_SIZE_FACTOR; 1196 const double ediv_size_correction = (double)num_mbs / EDIV_SIZE_FACTOR;
1198 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << 1197 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth <<
1199 BPER_MB_NORMBITS) / active_mbs; 1198 BPER_MB_NORMBITS) / active_mbs;
1200 1199
1201 int q; 1200 int q;
1202 int is_svc_upper_layer = 0; 1201 int is_svc_upper_layer = 0;
1203 1202
1204 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) 1203 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
(...skipping 12 matching lines...) Expand all
1217 const int bits_per_mb = 1216 const int bits_per_mb =
1218 vp9_rc_bits_per_mb(INTER_FRAME, q, 1217 vp9_rc_bits_per_mb(INTER_FRAME, q,
1219 factor * speed_term * group_weight_factor, 1218 factor * speed_term * group_weight_factor,
1220 cpi->common.bit_depth); 1219 cpi->common.bit_depth);
1221 if (bits_per_mb <= target_norm_bits_per_mb) 1220 if (bits_per_mb <= target_norm_bits_per_mb)
1222 break; 1221 break;
1223 } 1222 }
1224 1223
1225 // Restriction on active max q for constrained quality mode. 1224 // Restriction on active max q for constrained quality mode.
1226 if (cpi->oxcf.rc_mode == VPX_CQ) 1225 if (cpi->oxcf.rc_mode == VPX_CQ)
1227 q = MAX(q, oxcf->cq_level); 1226 q = VPXMAX(q, oxcf->cq_level);
1228 return q; 1227 return q;
1229 } 1228 }
1230 } 1229 }
1231 1230
1232 static void setup_rf_level_maxq(VP9_COMP *cpi) { 1231 static void setup_rf_level_maxq(VP9_COMP *cpi) {
1233 int i; 1232 int i;
1234 RATE_CONTROL *const rc = &cpi->rc; 1233 RATE_CONTROL *const rc = &cpi->rc;
1235 for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) { 1234 for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) {
1236 int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality); 1235 int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality);
1237 rc->rf_level_maxq[i] = MAX(rc->worst_quality + qdelta, rc->best_quality); 1236 rc->rf_level_maxq[i] = VPXMAX(rc->worst_quality + qdelta, rc->best_quality);
1238 } 1237 }
1239 } 1238 }
1240 1239
1241 void vp9_init_subsampling(VP9_COMP *cpi) { 1240 void vp9_init_subsampling(VP9_COMP *cpi) {
1242 const VP9_COMMON *const cm = &cpi->common; 1241 const VP9_COMMON *const cm = &cpi->common;
1243 RATE_CONTROL *const rc = &cpi->rc; 1242 RATE_CONTROL *const rc = &cpi->rc;
1244 const int w = cm->width; 1243 const int w = cm->width;
1245 const int h = cm->height; 1244 const int h = cm->height;
1246 int i; 1245 int i;
1247 1246
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
1358 1357
1359 modified_pct_inter = frame->pcnt_inter; 1358 modified_pct_inter = frame->pcnt_inter;
1360 if ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) < 1359 if ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) <
1361 (double)NCOUNT_FRAME_II_THRESH) { 1360 (double)NCOUNT_FRAME_II_THRESH) {
1362 modified_pct_inter = frame->pcnt_inter - frame->pcnt_neutral; 1361 modified_pct_inter = frame->pcnt_inter - frame->pcnt_neutral;
1363 } 1362 }
1364 modified_pcnt_intra = 100 * (1.0 - modified_pct_inter); 1363 modified_pcnt_intra = 100 * (1.0 - modified_pct_inter);
1365 1364
1366 1365
1367 if ((sr_diff > LOW_SR_DIFF_TRHESH)) { 1366 if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1368 sr_diff = MIN(sr_diff, SR_DIFF_MAX); 1367 sr_diff = VPXMIN(sr_diff, SR_DIFF_MAX);
1369 sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) - 1368 sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) -
1370 (MOTION_AMP_PART * motion_amplitude_factor) - 1369 (MOTION_AMP_PART * motion_amplitude_factor) -
1371 (INTRA_PART * modified_pcnt_intra); 1370 (INTRA_PART * modified_pcnt_intra);
1372 } 1371 }
1373 return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, modified_pct_inter)); 1372 return VPXMAX(sr_decay, VPXMIN(DEFAULT_DECAY_LIMIT, modified_pct_inter));
1374 } 1373 }
1375 1374
1376 // This function gives an estimate of how badly we believe the prediction 1375 // This function gives an estimate of how badly we believe the prediction
1377 // quality is decaying from frame to frame. 1376 // quality is decaying from frame to frame.
1378 static double get_zero_motion_factor(const VP9_COMP *cpi, 1377 static double get_zero_motion_factor(const VP9_COMP *cpi,
1379 const FIRSTPASS_STATS *frame) { 1378 const FIRSTPASS_STATS *frame) {
1380 const double zero_motion_pct = frame->pcnt_inter - 1379 const double zero_motion_pct = frame->pcnt_inter -
1381 frame->pcnt_motion; 1380 frame->pcnt_motion;
1382 double sr_decay = get_sr_decay_rate(cpi, frame); 1381 double sr_decay = get_sr_decay_rate(cpi, frame);
1383 return MIN(sr_decay, zero_motion_pct); 1382 return VPXMIN(sr_decay, zero_motion_pct);
1384 } 1383 }
1385 1384
1386 #define ZM_POWER_FACTOR 0.75 1385 #define ZM_POWER_FACTOR 0.75
1387 1386
1388 static double get_prediction_decay_rate(const VP9_COMP *cpi, 1387 static double get_prediction_decay_rate(const VP9_COMP *cpi,
1389 const FIRSTPASS_STATS *next_frame) { 1388 const FIRSTPASS_STATS *next_frame) {
1390 const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame); 1389 const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame);
1391 const double zero_motion_factor = 1390 const double zero_motion_factor =
1392 (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion), 1391 (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
1393 ZM_POWER_FACTOR)); 1392 ZM_POWER_FACTOR));
1394 1393
1395 return MAX(zero_motion_factor, 1394 return VPXMAX(zero_motion_factor,
1396 (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor))); 1395 (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1397 } 1396 }
1398 1397
1399 // Function to test for a condition where a complex transition is followed 1398 // Function to test for a condition where a complex transition is followed
1400 // by a static section. For example in slide shows where there is a fade 1399 // by a static section. For example in slide shows where there is a fade
1401 // between slides. This is to help with more optimal kf and gf positioning. 1400 // between slides. This is to help with more optimal kf and gf positioning.
1402 static int detect_transition_to_still(VP9_COMP *cpi, 1401 static int detect_transition_to_still(VP9_COMP *cpi,
1403 int frame_interval, int still_interval, 1402 int frame_interval, int still_interval,
1404 double loop_decay_rate, 1403 double loop_decay_rate,
1405 double last_decay_rate) { 1404 double last_decay_rate) {
1406 TWO_PASS *const twopass = &cpi->twopass; 1405 TWO_PASS *const twopass = &cpi->twopass;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1477 1476
1478 #define BASELINE_ERR_PER_MB 1000.0 1477 #define BASELINE_ERR_PER_MB 1000.0
1479 static double calc_frame_boost(VP9_COMP *cpi, 1478 static double calc_frame_boost(VP9_COMP *cpi,
1480 const FIRSTPASS_STATS *this_frame, 1479 const FIRSTPASS_STATS *this_frame,
1481 double this_frame_mv_in_out, 1480 double this_frame_mv_in_out,
1482 double max_boost) { 1481 double max_boost) {
1483 double frame_boost; 1482 double frame_boost;
1484 const double lq = 1483 const double lq =
1485 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME], 1484 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME],
1486 cpi->common.bit_depth); 1485 cpi->common.bit_depth);
1487 const double boost_q_correction = MIN((0.5 + (lq * 0.015)), 1.5); 1486 const double boost_q_correction = VPXMIN((0.5 + (lq * 0.015)), 1.5);
1488 int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) 1487 int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1489 ? cpi->initial_mbs : cpi->common.MBs; 1488 ? cpi->initial_mbs : cpi->common.MBs;
1490 1489
1491 // Correct for any inactive region in the image 1490 // Correct for any inactive region in the image
1492 num_mbs = (int)MAX(1, num_mbs * calculate_active_area(cpi, this_frame)); 1491 num_mbs = (int)VPXMAX(1, num_mbs * calculate_active_area(cpi, this_frame));
1493 1492
1494 // Underlying boost factor is based on inter error ratio. 1493 // Underlying boost factor is based on inter error ratio.
1495 frame_boost = (BASELINE_ERR_PER_MB * num_mbs) / 1494 frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
1496 DOUBLE_DIVIDE_CHECK(this_frame->coded_error); 1495 DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1497 frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction; 1496 frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction;
1498 1497
1499 // Increase boost for frames where new data coming into frame (e.g. zoom out). 1498 // Increase boost for frames where new data coming into frame (e.g. zoom out).
1500 // Slightly reduce boost if there is a net balance of motion out of the frame 1499 // Slightly reduce boost if there is a net balance of motion out of the frame
1501 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. 1500 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1502 if (this_frame_mv_in_out > 0.0) 1501 if (this_frame_mv_in_out > 0.0)
1503 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); 1502 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1504 // In the extreme case the boost is halved. 1503 // In the extreme case the boost is halved.
1505 else 1504 else
1506 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); 1505 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1507 1506
1508 return MIN(frame_boost, max_boost * boost_q_correction); 1507 return VPXMIN(frame_boost, max_boost * boost_q_correction);
1509 } 1508 }
1510 1509
1511 static int calc_arf_boost(VP9_COMP *cpi, int offset, 1510 static int calc_arf_boost(VP9_COMP *cpi, int offset,
1512 int f_frames, int b_frames, 1511 int f_frames, int b_frames,
1513 int *f_boost, int *b_boost) { 1512 int *f_boost, int *b_boost) {
1514 TWO_PASS *const twopass = &cpi->twopass; 1513 TWO_PASS *const twopass = &cpi->twopass;
1515 int i; 1514 int i;
1516 double boost_score = 0.0; 1515 double boost_score = 0.0;
1517 double mv_ratio_accumulator = 0.0; 1516 double mv_ratio_accumulator = 0.0;
1518 double decay_accumulator = 1.0; 1517 double decay_accumulator = 1.0;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1587 1586
1588 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame, 1587 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1589 this_frame_mv_in_out, 1588 this_frame_mv_in_out,
1590 GF_MAX_BOOST); 1589 GF_MAX_BOOST);
1591 } 1590 }
1592 *b_boost = (int)boost_score; 1591 *b_boost = (int)boost_score;
1593 1592
1594 arf_boost = (*f_boost + *b_boost); 1593 arf_boost = (*f_boost + *b_boost);
1595 if (arf_boost < ((b_frames + f_frames) * 20)) 1594 if (arf_boost < ((b_frames + f_frames) * 20))
1596 arf_boost = ((b_frames + f_frames) * 20); 1595 arf_boost = ((b_frames + f_frames) * 20);
1597 arf_boost = MAX(arf_boost, MIN_ARF_GF_BOOST); 1596 arf_boost = VPXMAX(arf_boost, MIN_ARF_GF_BOOST);
1598 1597
1599 return arf_boost; 1598 return arf_boost;
1600 } 1599 }
1601 1600
1602 // Calculate a section intra ratio used in setting max loop filter. 1601 // Calculate a section intra ratio used in setting max loop filter.
1603 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin, 1602 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
1604 const FIRSTPASS_STATS *end, 1603 const FIRSTPASS_STATS *end,
1605 int section_length) { 1604 int section_length) {
1606 const FIRSTPASS_STATS *s = begin; 1605 const FIRSTPASS_STATS *s = begin;
1607 double intra_error = 0.0; 1606 double intra_error = 0.0;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1658 allocation_chunks = (frame_count * 100) + boost; 1657 allocation_chunks = (frame_count * 100) + boost;
1659 1658
1660 // Prevent overflow. 1659 // Prevent overflow.
1661 if (boost > 1023) { 1660 if (boost > 1023) {
1662 int divisor = boost >> 10; 1661 int divisor = boost >> 10;
1663 boost /= divisor; 1662 boost /= divisor;
1664 allocation_chunks /= divisor; 1663 allocation_chunks /= divisor;
1665 } 1664 }
1666 1665
1667 // Calculate the number of extra bits for use in the boosted frame or frames. 1666 // Calculate the number of extra bits for use in the boosted frame or frames.
1668 return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0); 1667 return VPXMAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks),
1668 0);
1669 } 1669 }
1670 1670
1671 // Current limit on maximum number of active arfs in a GF/ARF group. 1671 // Current limit on maximum number of active arfs in a GF/ARF group.
1672 #define MAX_ACTIVE_ARFS 2 1672 #define MAX_ACTIVE_ARFS 2
1673 #define ARF_SLOT1 2 1673 #define ARF_SLOT1 2
1674 #define ARF_SLOT2 3 1674 #define ARF_SLOT2 3
1675 // This function indirects the choice of buffers for arfs. 1675 // This function indirects the choice of buffers for arfs.
1676 // At the moment the values are fixed but this may change as part of 1676 // At the moment the values are fixed but this may change as part of
1677 // the integration process with other codec features that swap buffers around. 1677 // the integration process with other codec features that swap buffers around.
1678 static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) { 1678 static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
1797 mid_boost_bits += (target_frame_size >> 4); 1797 mid_boost_bits += (target_frame_size >> 4);
1798 target_frame_size -= (target_frame_size >> 4); 1798 target_frame_size -= (target_frame_size >> 4);
1799 1799
1800 if (frame_index <= mid_frame_idx) 1800 if (frame_index <= mid_frame_idx)
1801 arf_idx = 1; 1801 arf_idx = 1;
1802 } 1802 }
1803 gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx]; 1803 gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx];
1804 gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx]; 1804 gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx];
1805 1805
1806 target_frame_size = clamp(target_frame_size, 0, 1806 target_frame_size = clamp(target_frame_size, 0,
1807 MIN(max_bits, (int)total_group_bits)); 1807 VPXMIN(max_bits, (int)total_group_bits));
1808 1808
1809 gf_group->update_type[frame_index] = LF_UPDATE; 1809 gf_group->update_type[frame_index] = LF_UPDATE;
1810 gf_group->rf_level[frame_index] = INTER_NORMAL; 1810 gf_group->rf_level[frame_index] = INTER_NORMAL;
1811 1811
1812 gf_group->bit_allocation[frame_index] = target_frame_size; 1812 gf_group->bit_allocation[frame_index] = target_frame_size;
1813 ++frame_index; 1813 ++frame_index;
1814 } 1814 }
1815 1815
1816 // Note: 1816 // Note:
1817 // We need to configure the frame at the end of the sequence + 1 that will be 1817 // We need to configure the frame at the end of the sequence + 1 that will be
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1884 int gf_arf_bits; 1884 int gf_arf_bits;
1885 const int is_key_frame = frame_is_intra_only(cm); 1885 const int is_key_frame = frame_is_intra_only(cm);
1886 const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active; 1886 const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active;
1887 1887
1888 // Reset the GF group data structures unless this is a key 1888 // Reset the GF group data structures unless this is a key
1889 // frame in which case it will already have been done. 1889 // frame in which case it will already have been done.
1890 if (is_key_frame == 0) { 1890 if (is_key_frame == 0) {
1891 vp9_zero(twopass->gf_group); 1891 vp9_zero(twopass->gf_group);
1892 } 1892 }
1893 1893
1894 vp9_clear_system_state(); 1894 vpx_clear_system_state();
1895 vp9_zero(next_frame); 1895 vp9_zero(next_frame);
1896 1896
1897 // Load stats for the current frame. 1897 // Load stats for the current frame.
1898 mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame); 1898 mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
1899 1899
1900 // Note the error of the frame at the start of the group. This will be 1900 // Note the error of the frame at the start of the group. This will be
1901 // the GF frame error if we code a normal gf. 1901 // the GF frame error if we code a normal gf.
1902 gf_first_frame_err = mod_frame_err; 1902 gf_first_frame_err = mod_frame_err;
1903 1903
1904 // If this is a key frame or the overlay from a previous arf then 1904 // If this is a key frame or the overlay from a previous arf then
(...skipping 13 matching lines...) Expand all
1918 1918
1919 // Set a maximum and minimum interval for the GF group. 1919 // Set a maximum and minimum interval for the GF group.
1920 // If the image appears almost completely static we can extend beyond this. 1920 // If the image appears almost completely static we can extend beyond this.
1921 { 1921 {
1922 int int_max_q = 1922 int int_max_q =
1923 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, 1923 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
1924 cpi->common.bit_depth)); 1924 cpi->common.bit_depth));
1925 int int_lbq = 1925 int int_lbq =
1926 (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex, 1926 (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex,
1927 cpi->common.bit_depth)); 1927 cpi->common.bit_depth));
1928 active_min_gf_interval = rc->min_gf_interval + MIN(2, int_max_q / 200); 1928 active_min_gf_interval = rc->min_gf_interval + VPXMIN(2, int_max_q / 200);
1929 if (active_min_gf_interval > rc->max_gf_interval) 1929 if (active_min_gf_interval > rc->max_gf_interval)
1930 active_min_gf_interval = rc->max_gf_interval; 1930 active_min_gf_interval = rc->max_gf_interval;
1931 1931
1932 if (cpi->multi_arf_allowed) { 1932 if (cpi->multi_arf_allowed) {
1933 active_max_gf_interval = rc->max_gf_interval; 1933 active_max_gf_interval = rc->max_gf_interval;
1934 } else { 1934 } else {
1935 // The value chosen depends on the active Q range. At low Q we have 1935 // The value chosen depends on the active Q range. At low Q we have
1936 // bits to spare and are better with a smaller interval and smaller boost. 1936 // bits to spare and are better with a smaller interval and smaller boost.
1937 // At high Q when there are few bits to spare we are better with a longer 1937 // At high Q when there are few bits to spare we are better with a longer
1938 // interval to spread the cost of the GF. 1938 // interval to spread the cost of the GF.
1939 active_max_gf_interval = 12 + MIN(4, (int_lbq / 6)); 1939 active_max_gf_interval = 12 + VPXMIN(4, (int_lbq / 6));
1940 if (active_max_gf_interval < active_min_gf_interval) 1940 if (active_max_gf_interval < active_min_gf_interval)
1941 active_max_gf_interval = active_min_gf_interval; 1941 active_max_gf_interval = active_min_gf_interval;
1942 1942
1943 if (active_max_gf_interval > rc->max_gf_interval) 1943 if (active_max_gf_interval > rc->max_gf_interval)
1944 active_max_gf_interval = rc->max_gf_interval; 1944 active_max_gf_interval = rc->max_gf_interval;
1945 if (active_max_gf_interval < active_min_gf_interval) 1945 if (active_max_gf_interval < active_min_gf_interval)
1946 active_max_gf_interval = active_min_gf_interval; 1946 active_max_gf_interval = active_min_gf_interval;
1947 } 1947 }
1948 } 1948 }
1949 1949
(...skipping 24 matching lines...) Expand all
1974 &mv_ratio_accumulator); 1974 &mv_ratio_accumulator);
1975 1975
1976 // Accumulate the effect of prediction quality decay. 1976 // Accumulate the effect of prediction quality decay.
1977 if (!flash_detected) { 1977 if (!flash_detected) {
1978 last_loop_decay_rate = loop_decay_rate; 1978 last_loop_decay_rate = loop_decay_rate;
1979 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); 1979 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1980 1980
1981 decay_accumulator = decay_accumulator * loop_decay_rate; 1981 decay_accumulator = decay_accumulator * loop_decay_rate;
1982 1982
1983 // Monitor for static sections. 1983 // Monitor for static sections.
1984 zero_motion_accumulator = 1984 zero_motion_accumulator = VPXMIN(
1985 MIN(zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame)); 1985 zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
1986 1986
1987 // Break clause to detect very still sections after motion. For example, 1987 // Break clause to detect very still sections after motion. For example,
1988 // a static image after a fade or other transition. 1988 // a static image after a fade or other transition.
1989 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate, 1989 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
1990 last_loop_decay_rate)) { 1990 last_loop_decay_rate)) {
1991 allow_alt_ref = 0; 1991 allow_alt_ref = 0;
1992 break; 1992 break;
1993 } 1993 }
1994 } 1994 }
1995 1995
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2031 // Calculate the boost for alt ref. 2031 // Calculate the boost for alt ref.
2032 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, 2032 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
2033 &b_boost); 2033 &b_boost);
2034 rc->source_alt_ref_pending = 1; 2034 rc->source_alt_ref_pending = 1;
2035 2035
2036 // Test to see if multi arf is appropriate. 2036 // Test to see if multi arf is appropriate.
2037 cpi->multi_arf_enabled = 2037 cpi->multi_arf_enabled =
2038 (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) && 2038 (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) &&
2039 (zero_motion_accumulator < 0.995)) ? 1 : 0; 2039 (zero_motion_accumulator < 0.995)) ? 1 : 0;
2040 } else { 2040 } else {
2041 rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST); 2041 rc->gfu_boost = VPXMAX((int)boost_score, MIN_ARF_GF_BOOST);
2042 rc->source_alt_ref_pending = 0; 2042 rc->source_alt_ref_pending = 0;
2043 } 2043 }
2044 2044
2045 // Set the interval until the next gf. 2045 // Set the interval until the next gf.
2046 rc->baseline_gf_interval = i - (is_key_frame || rc->source_alt_ref_pending); 2046 rc->baseline_gf_interval = i - (is_key_frame || rc->source_alt_ref_pending);
2047 2047
2048 // Only encode alt reference frame in temporal base layer. So 2048 // Only encode alt reference frame in temporal base layer. So
2049 // baseline_gf_interval should be multiple of a temporal layer group 2049 // baseline_gf_interval should be multiple of a temporal layer group
2050 // (typically the frame distance between two base layer frames) 2050 // (typically the frame distance between two base layer frames)
2051 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { 2051 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
2086 const double group_av_skip_pct = 2086 const double group_av_skip_pct =
2087 gf_group_skip_pct / rc->baseline_gf_interval; 2087 gf_group_skip_pct / rc->baseline_gf_interval;
2088 const double group_av_inactive_zone = 2088 const double group_av_inactive_zone =
2089 ((gf_group_inactive_zone_rows * 2) / 2089 ((gf_group_inactive_zone_rows * 2) /
2090 (rc->baseline_gf_interval * (double)cm->mb_rows)); 2090 (rc->baseline_gf_interval * (double)cm->mb_rows));
2091 2091
2092 int tmp_q; 2092 int tmp_q;
2093 // rc factor is a weight factor that corrects for local rate control drift. 2093 // rc factor is a weight factor that corrects for local rate control drift.
2094 double rc_factor = 1.0; 2094 double rc_factor = 1.0;
2095 if (rc->rate_error_estimate > 0) { 2095 if (rc->rate_error_estimate > 0) {
2096 rc_factor = MAX(RC_FACTOR_MIN, 2096 rc_factor = VPXMAX(RC_FACTOR_MIN,
2097 (double)(100 - rc->rate_error_estimate) / 100.0); 2097 (double)(100 - rc->rate_error_estimate) / 100.0);
2098 } else { 2098 } else {
2099 rc_factor = MIN(RC_FACTOR_MAX, 2099 rc_factor = VPXMIN(RC_FACTOR_MAX,
2100 (double)(100 - rc->rate_error_estimate) / 100.0); 2100 (double)(100 - rc->rate_error_estimate) / 100.0);
2101 } 2101 }
2102 tmp_q = 2102 tmp_q =
2103 get_twopass_worst_quality(cpi, group_av_err, 2103 get_twopass_worst_quality(cpi, group_av_err,
2104 (group_av_skip_pct + group_av_inactive_zone), 2104 (group_av_skip_pct + group_av_inactive_zone),
2105 vbr_group_bits_per_frame, 2105 vbr_group_bits_per_frame,
2106 twopass->kfgroup_inter_fraction * rc_factor); 2106 twopass->kfgroup_inter_fraction * rc_factor);
2107 twopass->active_worst_quality = 2107 twopass->active_worst_quality =
2108 MAX(tmp_q, twopass->active_worst_quality >> 1); 2108 VPXMAX(tmp_q, twopass->active_worst_quality >> 1);
2109 } 2109 }
2110 #endif 2110 #endif
2111 2111
2112 // Calculate the extra bits to be used for boosted frame(s) 2112 // Calculate the extra bits to be used for boosted frame(s)
2113 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, 2113 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval,
2114 rc->gfu_boost, gf_group_bits); 2114 rc->gfu_boost, gf_group_bits);
2115 2115
2116 // Adjust KF group bits and error remaining. 2116 // Adjust KF group bits and error remaining.
2117 twopass->kf_group_error_left -= (int64_t)gf_group_err; 2117 twopass->kf_group_error_left -= (int64_t)gf_group_err;
2118 2118
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
2415 twopass->kf_group_bits = (int64_t)(twopass->bits_left * 2415 twopass->kf_group_bits = (int64_t)(twopass->bits_left *
2416 (kf_group_err / twopass->modified_error_left)); 2416 (kf_group_err / twopass->modified_error_left));
2417 2417
2418 // Clip based on maximum per frame rate defined by the user. 2418 // Clip based on maximum per frame rate defined by the user.
2419 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; 2419 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
2420 if (twopass->kf_group_bits > max_grp_bits) 2420 if (twopass->kf_group_bits > max_grp_bits)
2421 twopass->kf_group_bits = max_grp_bits; 2421 twopass->kf_group_bits = max_grp_bits;
2422 } else { 2422 } else {
2423 twopass->kf_group_bits = 0; 2423 twopass->kf_group_bits = 0;
2424 } 2424 }
2425 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits); 2425 twopass->kf_group_bits = VPXMAX(0, twopass->kf_group_bits);
2426 2426
2427 // Reset the first pass file position. 2427 // Reset the first pass file position.
2428 reset_fpf_position(twopass, start_position); 2428 reset_fpf_position(twopass, start_position);
2429 2429
2430 // Scan through the kf group collating various stats used to determine 2430 // Scan through the kf group collating various stats used to determine
2431 // how many bits to spend on it. 2431 // how many bits to spend on it.
2432 decay_accumulator = 1.0; 2432 decay_accumulator = 1.0;
2433 boost_score = 0.0; 2433 boost_score = 0.0;
2434 for (i = 0; i < (rc->frames_to_key - 1); ++i) { 2434 for (i = 0; i < (rc->frames_to_key - 1); ++i) {
2435 if (EOF == input_stats(twopass, &next_frame)) 2435 if (EOF == input_stats(twopass, &next_frame))
2436 break; 2436 break;
2437 2437
2438 // Monitor for static sections. 2438 // Monitor for static sections.
2439 zero_motion_accumulator = 2439 zero_motion_accumulator = VPXMIN(
2440 MIN(zero_motion_accumulator, 2440 zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
2441 get_zero_motion_factor(cpi, &next_frame));
2442 2441
2443 // Not all frames in the group are necessarily used in calculating boost. 2442 // Not all frames in the group are necessarily used in calculating boost.
2444 if ((i <= rc->max_gf_interval) || 2443 if ((i <= rc->max_gf_interval) ||
2445 ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) { 2444 ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) {
2446 const double frame_boost = 2445 const double frame_boost =
2447 calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST); 2446 calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST);
2448 2447
2449 // How fast is prediction quality decaying. 2448 // How fast is prediction quality decaying.
2450 if (!detect_flash(twopass, 0)) { 2449 if (!detect_flash(twopass, 0)) {
2451 const double loop_decay_rate = 2450 const double loop_decay_rate =
2452 get_prediction_decay_rate(cpi, &next_frame); 2451 get_prediction_decay_rate(cpi, &next_frame);
2453 decay_accumulator *= loop_decay_rate; 2452 decay_accumulator *= loop_decay_rate;
2454 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); 2453 decay_accumulator = VPXMAX(decay_accumulator, MIN_DECAY_FACTOR);
2455 av_decay_accumulator += decay_accumulator; 2454 av_decay_accumulator += decay_accumulator;
2456 ++loop_decay_counter; 2455 ++loop_decay_counter;
2457 } 2456 }
2458 boost_score += (decay_accumulator * frame_boost); 2457 boost_score += (decay_accumulator * frame_boost);
2459 } 2458 }
2460 } 2459 }
2461 av_decay_accumulator /= (double)loop_decay_counter; 2460 av_decay_accumulator /= (double)loop_decay_counter;
2462 2461
2463 reset_fpf_position(twopass, start_position); 2462 reset_fpf_position(twopass, start_position);
2464 2463
2465 // Store the zero motion percentage 2464 // Store the zero motion percentage
2466 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); 2465 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2467 2466
2468 // Calculate a section intra ratio used in setting max loop filter. 2467 // Calculate a section intra ratio used in setting max loop filter.
2469 twopass->section_intra_rating = 2468 twopass->section_intra_rating =
2470 calculate_section_intra_ratio(start_position, twopass->stats_in_end, 2469 calculate_section_intra_ratio(start_position, twopass->stats_in_end,
2471 rc->frames_to_key); 2470 rc->frames_to_key);
2472 2471
2473 // Apply various clamps for min and max boost 2472 // Apply various clamps for min and max boost
2474 rc->kf_boost = (int)(av_decay_accumulator * boost_score); 2473 rc->kf_boost = (int)(av_decay_accumulator * boost_score);
2475 rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3)); 2474 rc->kf_boost = VPXMAX(rc->kf_boost, (rc->frames_to_key * 3));
2476 rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST); 2475 rc->kf_boost = VPXMAX(rc->kf_boost, MIN_KF_BOOST);
2477 2476
2478 // Work out how many bits to allocate for the key frame itself. 2477 // Work out how many bits to allocate for the key frame itself.
2479 kf_bits = calculate_boost_bits((rc->frames_to_key - 1), 2478 kf_bits = calculate_boost_bits((rc->frames_to_key - 1),
2480 rc->kf_boost, twopass->kf_group_bits); 2479 rc->kf_boost, twopass->kf_group_bits);
2481 2480
2482 // Work out the fraction of the kf group bits reserved for the inter frames 2481 // Work out the fraction of the kf group bits reserved for the inter frames
2483 // within the group after discounting the bits for the kf itself. 2482 // within the group after discounting the bits for the kf itself.
2484 if (twopass->kf_group_bits) { 2483 if (twopass->kf_group_bits) {
2485 twopass->kfgroup_inter_fraction = 2484 twopass->kfgroup_inter_fraction =
2486 (double)(twopass->kf_group_bits - kf_bits) / 2485 (double)(twopass->kf_group_bits - kf_bits) /
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
2625 // Do the firstpass stats indicate that this frame is skippable for the 2624 // Do the firstpass stats indicate that this frame is skippable for the
2626 // partition search? 2625 // partition search?
2627 if (cpi->sf.allow_partition_search_skip && 2626 if (cpi->sf.allow_partition_search_skip &&
2628 cpi->oxcf.pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) { 2627 cpi->oxcf.pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
2629 cpi->partition_search_skippable_frame = is_skippable_frame(cpi); 2628 cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
2630 } 2629 }
2631 2630
2632 return; 2631 return;
2633 } 2632 }
2634 2633
2635 vp9_clear_system_state(); 2634 vpx_clear_system_state();
2636 2635
2637 if (cpi->oxcf.rc_mode == VPX_Q) { 2636 if (cpi->oxcf.rc_mode == VPX_Q) {
2638 twopass->active_worst_quality = cpi->oxcf.cq_level; 2637 twopass->active_worst_quality = cpi->oxcf.cq_level;
2639 } else if (cm->current_video_frame == 0 || 2638 } else if (cm->current_video_frame == 0 ||
2640 (lc != NULL && lc->current_video_frame_in_layer == 0)) { 2639 (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2641 // Special case code for first frame. 2640 // Special case code for first frame.
2642 const int section_target_bandwidth = (int)(twopass->bits_left / 2641 const int section_target_bandwidth = (int)(twopass->bits_left /
2643 frames_left); 2642 frames_left);
2644 const double section_length = twopass->total_left_stats.count; 2643 const double section_length = twopass->total_left_stats.count;
2645 const double section_error = 2644 const double section_error =
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
2764 TWO_PASS *const twopass = &cpi->twopass; 2763 TWO_PASS *const twopass = &cpi->twopass;
2765 RATE_CONTROL *const rc = &cpi->rc; 2764 RATE_CONTROL *const rc = &cpi->rc;
2766 const int bits_used = rc->base_frame_target; 2765 const int bits_used = rc->base_frame_target;
2767 2766
2768 // VBR correction is done through rc->vbr_bits_off_target. Based on the 2767 // VBR correction is done through rc->vbr_bits_off_target. Based on the
2769 // sign of this value, a limited % adjustment is made to the target rate 2768 // sign of this value, a limited % adjustment is made to the target rate
2770 // of subsequent frames, to try and push it back towards 0. This method 2769 // of subsequent frames, to try and push it back towards 0. This method
2771 // is designed to prevent extreme behaviour at the end of a clip 2770 // is designed to prevent extreme behaviour at the end of a clip
2772 // or group of frames. 2771 // or group of frames.
2773 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; 2772 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
2774 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); 2773 twopass->bits_left = VPXMAX(twopass->bits_left - bits_used, 0);
2775 2774
2776 // Calculate the pct rc error. 2775 // Calculate the pct rc error.
2777 if (rc->total_actual_bits) { 2776 if (rc->total_actual_bits) {
2778 rc->rate_error_estimate = 2777 rc->rate_error_estimate =
2779 (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits); 2778 (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
2780 rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100); 2779 rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
2781 } else { 2780 } else {
2782 rc->rate_error_estimate = 0; 2781 rc->rate_error_estimate = 0;
2783 } 2782 }
2784 2783
2785 if (cpi->common.frame_type != KEY_FRAME && 2784 if (cpi->common.frame_type != KEY_FRAME &&
2786 !vp9_is_upper_layer_key_frame(cpi)) { 2785 !vp9_is_upper_layer_key_frame(cpi)) {
2787 twopass->kf_group_bits -= bits_used; 2786 twopass->kf_group_bits -= bits_used;
2788 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct; 2787 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
2789 } 2788 }
2790 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); 2789 twopass->kf_group_bits = VPXMAX(twopass->kf_group_bits, 0);
2791 2790
2792 // Increment the gf group index ready for the next frame. 2791 // Increment the gf group index ready for the next frame.
2793 ++twopass->gf_group.index; 2792 ++twopass->gf_group.index;
2794 2793
2795 // If the rate control is drifting consider adjustment to min or maxq. 2794 // If the rate control is drifting consider adjustment to min or maxq.
2796 if ((cpi->oxcf.rc_mode != VPX_Q) && 2795 if ((cpi->oxcf.rc_mode != VPX_Q) &&
2797 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) && 2796 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) &&
2798 !cpi->rc.is_src_frame_alt_ref) { 2797 !cpi->rc.is_src_frame_alt_ref) {
2799 const int maxq_adj_limit = 2798 const int maxq_adj_limit =
2800 rc->worst_quality - twopass->active_worst_quality; 2799 rc->worst_quality - twopass->active_worst_quality;
(...skipping 29 matching lines...) Expand all
2830 // If there is a big and undexpected undershoot then feed the extra 2829 // If there is a big and undexpected undershoot then feed the extra
2831 // bits back in quickly. One situation where this may happen is if a 2830 // bits back in quickly. One situation where this may happen is if a
2832 // frame is unexpectedly almost perfectly predicted by the ARF or GF 2831 // frame is unexpectedly almost perfectly predicted by the ARF or GF
2833 // but not very well predcited by the previous frame. 2832 // but not very well predcited by the previous frame.
2834 if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) { 2833 if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) {
2835 int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO; 2834 int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO;
2836 if (rc->projected_frame_size < fast_extra_thresh) { 2835 if (rc->projected_frame_size < fast_extra_thresh) {
2837 rc->vbr_bits_off_target_fast += 2836 rc->vbr_bits_off_target_fast +=
2838 fast_extra_thresh - rc->projected_frame_size; 2837 fast_extra_thresh - rc->projected_frame_size;
2839 rc->vbr_bits_off_target_fast = 2838 rc->vbr_bits_off_target_fast =
2840 MIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth)); 2839 VPXMIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth));
2841 2840
2842 // Fast adaptation of minQ if necessary to use up the extra bits. 2841 // Fast adaptation of minQ if necessary to use up the extra bits.
2843 if (rc->avg_frame_bandwidth) { 2842 if (rc->avg_frame_bandwidth) {
2844 twopass->extend_minq_fast = 2843 twopass->extend_minq_fast =
2845 (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth); 2844 (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth);
2846 } 2845 }
2847 twopass->extend_minq_fast = MIN(twopass->extend_minq_fast, 2846 twopass->extend_minq_fast = VPXMIN(
2848 minq_adj_limit - twopass->extend_minq); 2847 twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
2849 } else if (rc->vbr_bits_off_target_fast) { 2848 } else if (rc->vbr_bits_off_target_fast) {
2850 twopass->extend_minq_fast = MIN(twopass->extend_minq_fast, 2849 twopass->extend_minq_fast = VPXMIN(
2851 minq_adj_limit - twopass->extend_minq); 2850 twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
2852 } else { 2851 } else {
2853 twopass->extend_minq_fast = 0; 2852 twopass->extend_minq_fast = 0;
2854 } 2853 }
2855 } 2854 }
2856 } 2855 }
2857 } 2856 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_fastssim.c ('k') | source/libvpx/vp9/encoder/vp9_lookahead.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698