| 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 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "vp9/encoder/vp9_extend.h" | 31 #include "vp9/encoder/vp9_extend.h" |
| 32 #include "vp9/encoder/vp9_firstpass.h" | 32 #include "vp9/encoder/vp9_firstpass.h" |
| 33 #include "vp9/encoder/vp9_mcomp.h" | 33 #include "vp9/encoder/vp9_mcomp.h" |
| 34 #include "vp9/encoder/vp9_quantize.h" | 34 #include "vp9/encoder/vp9_quantize.h" |
| 35 #include "vp9/encoder/vp9_rd.h" | 35 #include "vp9/encoder/vp9_rd.h" |
| 36 #include "vp9/encoder/vp9_variance.h" | 36 #include "vp9/encoder/vp9_variance.h" |
| 37 | 37 |
| 38 #define OUTPUT_FPF 0 | 38 #define OUTPUT_FPF 0 |
| 39 #define ARF_STATS_OUTPUT 0 | 39 #define ARF_STATS_OUTPUT 0 |
| 40 | 40 |
| 41 #define GROUP_ADAPTIVE_MAXQ 0 |
| 42 |
| 41 #define BOOST_BREAKOUT 12.5 | 43 #define BOOST_BREAKOUT 12.5 |
| 42 #define BOOST_FACTOR 12.5 | 44 #define BOOST_FACTOR 12.5 |
| 43 #define ERR_DIVISOR 128.0 | 45 #define ERR_DIVISOR 128.0 |
| 44 #define FACTOR_PT_LOW 0.70 | 46 #define FACTOR_PT_LOW 0.70 |
| 45 #define FACTOR_PT_HIGH 0.90 | 47 #define FACTOR_PT_HIGH 0.90 |
| 46 #define FIRST_PASS_Q 10.0 | 48 #define FIRST_PASS_Q 10.0 |
| 47 #define GF_MAX_BOOST 96.0 | 49 #define GF_MAX_BOOST 96.0 |
| 48 #define INTRA_MODE_PENALTY 1024 | 50 #define INTRA_MODE_PENALTY 1024 |
| 49 #define KF_MAX_BOOST 128.0 | 51 #define KF_MAX_BOOST 128.0 |
| 50 #define MIN_ARF_GF_BOOST 240 | 52 #define MIN_ARF_GF_BOOST 240 |
| 51 #define MIN_DECAY_FACTOR 0.01 | 53 #define MIN_DECAY_FACTOR 0.01 |
| 52 #define MIN_GF_INTERVAL 4 | 54 #define MIN_GF_INTERVAL 4 |
| 53 #define MIN_KF_BOOST 300 | 55 #define MIN_KF_BOOST 300 |
| 54 #define NEW_MV_MODE_PENALTY 32 | 56 #define NEW_MV_MODE_PENALTY 32 |
| 55 #define SVC_FACTOR_PT_LOW 0.45 | 57 #define SVC_FACTOR_PT_LOW 0.45 |
| 56 #define DARK_THRESH 64 | 58 #define DARK_THRESH 64 |
| 59 #define DEFAULT_GRP_WEIGHT 1.0 |
| 57 | 60 |
| 58 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 61 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 59 | 62 |
| 60 #if ARF_STATS_OUTPUT | 63 #if ARF_STATS_OUTPUT |
| 61 unsigned int arf_count = 0; | 64 unsigned int arf_count = 0; |
| 62 #endif | 65 #endif |
| 63 | 66 |
| 64 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | 67 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { |
| 65 YV12_BUFFER_CONFIG temp = *a; | 68 YV12_BUFFER_CONFIG temp = *a; |
| 66 *a = *b; | 69 *a = *b; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 pkt.data.twopass_stats.buf = stats; | 103 pkt.data.twopass_stats.buf = stats; |
| 101 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); | 104 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); |
| 102 vpx_codec_pkt_list_add(pktlist, &pkt); | 105 vpx_codec_pkt_list_add(pktlist, &pkt); |
| 103 | 106 |
| 104 // TEMP debug code | 107 // TEMP debug code |
| 105 #if OUTPUT_FPF | 108 #if OUTPUT_FPF |
| 106 { | 109 { |
| 107 FILE *fpfile; | 110 FILE *fpfile; |
| 108 fpfile = fopen("firstpass.stt", "a"); | 111 fpfile = fopen("firstpass.stt", "a"); |
| 109 | 112 |
| 110 fprintf(fpfile, "%12.0f %12.4f %12.0f %12.0f %12.0f %12.4f %12.4f" | 113 fprintf(fpfile, "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf" |
| 111 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" | 114 "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf" |
| 112 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n", | 115 "%12.4lf %12.0lf %12.0lf %12.0lf %12.4lf\n", |
| 113 stats->frame, | 116 stats->frame, |
| 114 stats->weight, | 117 stats->weight, |
| 115 stats->intra_error, | 118 stats->intra_error, |
| 116 stats->coded_error, | 119 stats->coded_error, |
| 117 stats->sr_coded_error, | 120 stats->sr_coded_error, |
| 118 stats->pcnt_inter, | 121 stats->pcnt_inter, |
| 119 stats->pcnt_motion, | 122 stats->pcnt_motion, |
| 120 stats->pcnt_second_ref, | 123 stats->pcnt_second_ref, |
| 121 stats->pcnt_neutral, | 124 stats->pcnt_neutral, |
| 122 stats->MVr, | 125 stats->MVr, |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 vp9_scale_references(cpi); | 528 vp9_scale_references(cpi); |
| 526 | 529 |
| 527 // Use either last frame or alt frame for motion search. | 530 // Use either last frame or alt frame for motion search. |
| 528 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { | 531 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { |
| 529 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); | 532 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); |
| 530 if (first_ref_buf == NULL) | 533 if (first_ref_buf == NULL) |
| 531 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); | 534 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 532 } | 535 } |
| 533 | 536 |
| 534 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { | 537 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { |
| 538 BufferPool *const pool = cm->buffer_pool; |
| 535 const int ref_idx = | 539 const int ref_idx = |
| 536 cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)]; | 540 cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)]; |
| 537 const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1]; | 541 const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1]; |
| 538 | 542 |
| 539 gld_yv12 = (scaled_idx != ref_idx) ? &cm->frame_bufs[scaled_idx].buf : | 543 gld_yv12 = (scaled_idx != ref_idx) ? &pool->frame_bufs[scaled_idx].buf : |
| 540 get_ref_frame_buffer(cpi, GOLDEN_FRAME); | 544 get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 541 } else { | 545 } else { |
| 542 gld_yv12 = NULL; | 546 gld_yv12 = NULL; |
| 543 } | 547 } |
| 544 | 548 |
| 545 recon_y_stride = new_yv12->y_stride; | 549 recon_y_stride = new_yv12->y_stride; |
| 546 recon_uv_stride = new_yv12->uv_stride; | 550 recon_uv_stride = new_yv12->uv_stride; |
| 547 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); | 551 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); |
| 548 | 552 |
| 549 set_ref_ptrs(cm, xd, | 553 set_ref_ptrs(cm, xd, |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1075 | 1079 |
| 1076 return fclamp(pow(error_term, power_term), 0.05, 5.0); | 1080 return fclamp(pow(error_term, power_term), 0.05, 5.0); |
| 1077 } | 1081 } |
| 1078 | 1082 |
| 1079 // Larger image formats are expected to be a little harder to code relatively | 1083 // Larger image formats are expected to be a little harder to code relatively |
| 1080 // given the same prediction error score. This in part at least relates to the | 1084 // given the same prediction error score. This in part at least relates to the |
| 1081 // increased size and hence coding cost of motion vectors. | 1085 // increased size and hence coding cost of motion vectors. |
| 1082 #define EDIV_SIZE_FACTOR 800 | 1086 #define EDIV_SIZE_FACTOR 800 |
| 1083 | 1087 |
| 1084 static int get_twopass_worst_quality(const VP9_COMP *cpi, | 1088 static int get_twopass_worst_quality(const VP9_COMP *cpi, |
| 1085 const FIRSTPASS_STATS *stats, | 1089 const double section_err, |
| 1086 int section_target_bandwidth) { | 1090 int section_target_bandwidth, |
| 1091 double group_weight_factor) { |
| 1087 const RATE_CONTROL *const rc = &cpi->rc; | 1092 const RATE_CONTROL *const rc = &cpi->rc; |
| 1088 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1093 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1089 | 1094 |
| 1090 if (section_target_bandwidth <= 0) { | 1095 if (section_target_bandwidth <= 0) { |
| 1091 return rc->worst_quality; // Highest value allowed | 1096 return rc->worst_quality; // Highest value allowed |
| 1092 } else { | 1097 } else { |
| 1093 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) | 1098 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) |
| 1094 ? cpi->initial_mbs : cpi->common.MBs; | 1099 ? cpi->initial_mbs : cpi->common.MBs; |
| 1095 const double section_err = stats->coded_error / stats->count; | |
| 1096 const double err_per_mb = section_err / num_mbs; | 1100 const double err_per_mb = section_err / num_mbs; |
| 1097 const double speed_term = 1.0 + 0.04 * oxcf->speed; | 1101 const double speed_term = 1.0 + 0.04 * oxcf->speed; |
| 1098 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR; | 1102 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR; |
| 1099 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << | 1103 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << |
| 1100 BPER_MB_NORMBITS) / num_mbs; | 1104 BPER_MB_NORMBITS) / num_mbs; |
| 1101 | 1105 |
| 1102 int q; | 1106 int q; |
| 1103 int is_svc_upper_layer = 0; | 1107 int is_svc_upper_layer = 0; |
| 1108 |
| 1104 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) | 1109 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) |
| 1105 is_svc_upper_layer = 1; | 1110 is_svc_upper_layer = 1; |
| 1106 | 1111 |
| 1112 |
| 1107 // Try and pick a max Q that will be high enough to encode the | 1113 // Try and pick a max Q that will be high enough to encode the |
| 1108 // content at the given rate. | 1114 // content at the given rate. |
| 1109 for (q = rc->best_quality; q < rc->worst_quality; ++q) { | 1115 for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
| 1110 const double factor = | 1116 const double factor = |
| 1111 calc_correction_factor(err_per_mb, | 1117 calc_correction_factor(err_per_mb, |
| 1112 ERR_DIVISOR - ediv_size_correction, | 1118 ERR_DIVISOR - ediv_size_correction, |
| 1113 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : | 1119 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : |
| 1114 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, | 1120 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, |
| 1115 cpi->common.bit_depth); | 1121 cpi->common.bit_depth); |
| 1116 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, | 1122 const int bits_per_mb = |
| 1117 factor * speed_term, | 1123 vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 1118 cpi->common.bit_depth); | 1124 factor * speed_term * group_weight_factor, |
| 1125 cpi->common.bit_depth); |
| 1119 if (bits_per_mb <= target_norm_bits_per_mb) | 1126 if (bits_per_mb <= target_norm_bits_per_mb) |
| 1120 break; | 1127 break; |
| 1121 } | 1128 } |
| 1122 | 1129 |
| 1123 // Restriction on active max q for constrained quality mode. | 1130 // Restriction on active max q for constrained quality mode. |
| 1124 if (cpi->oxcf.rc_mode == VPX_CQ) | 1131 if (cpi->oxcf.rc_mode == VPX_CQ) |
| 1125 q = MAX(q, oxcf->cq_level); | 1132 q = MAX(q, oxcf->cq_level); |
| 1126 return q; | 1133 return q; |
| 1127 } | 1134 } |
| 1128 } | 1135 } |
| 1129 | 1136 |
| 1130 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); | 1137 static void setup_rf_level_maxq(VP9_COMP *cpi) { |
| 1138 int i; |
| 1139 RATE_CONTROL *const rc = &cpi->rc; |
| 1140 for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) { |
| 1141 int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality); |
| 1142 rc->rf_level_maxq[i] = MAX(rc->worst_quality + qdelta, rc->best_quality); |
| 1143 } |
| 1144 } |
| 1145 |
| 1146 void vp9_init_subsampling(VP9_COMP *cpi) { |
| 1147 const VP9_COMMON *const cm = &cpi->common; |
| 1148 RATE_CONTROL *const rc = &cpi->rc; |
| 1149 const int w = cm->width; |
| 1150 const int h = cm->height; |
| 1151 int i; |
| 1152 |
| 1153 for (i = 0; i < FRAME_SCALE_STEPS; ++i) { |
| 1154 // Note: Frames with odd-sized dimensions may result from this scaling. |
| 1155 rc->frame_width[i] = (w * 16) / frame_scale_factor[i]; |
| 1156 rc->frame_height[i] = (h * 16) / frame_scale_factor[i]; |
| 1157 } |
| 1158 |
| 1159 setup_rf_level_maxq(cpi); |
| 1160 } |
| 1161 |
| 1162 void calculate_coded_size(VP9_COMP *cpi, |
| 1163 int *scaled_frame_width, |
| 1164 int *scaled_frame_height) { |
| 1165 RATE_CONTROL *const rc = &cpi->rc; |
| 1166 *scaled_frame_width = rc->frame_width[rc->frame_size_selector]; |
| 1167 *scaled_frame_height = rc->frame_height[rc->frame_size_selector]; |
| 1168 } |
| 1131 | 1169 |
| 1132 void vp9_init_second_pass(VP9_COMP *cpi) { | 1170 void vp9_init_second_pass(VP9_COMP *cpi) { |
| 1133 SVC *const svc = &cpi->svc; | 1171 SVC *const svc = &cpi->svc; |
| 1134 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1172 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1135 const int is_two_pass_svc = (svc->number_spatial_layers > 1) || | 1173 const int is_two_pass_svc = (svc->number_spatial_layers > 1) || |
| 1136 (svc->number_temporal_layers > 1); | 1174 (svc->number_temporal_layers > 1); |
| 1137 TWO_PASS *const twopass = is_two_pass_svc ? | 1175 TWO_PASS *const twopass = is_two_pass_svc ? |
| 1138 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass; | 1176 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass; |
| 1139 double frame_rate; | 1177 double frame_rate; |
| 1140 FIRSTPASS_STATS *stats; | 1178 FIRSTPASS_STATS *stats; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1190 } | 1228 } |
| 1191 | 1229 |
| 1192 // Reset the vbr bits off target counter | 1230 // Reset the vbr bits off target counter |
| 1193 cpi->rc.vbr_bits_off_target = 0; | 1231 cpi->rc.vbr_bits_off_target = 0; |
| 1194 | 1232 |
| 1195 cpi->rc.rate_error_estimate = 0; | 1233 cpi->rc.rate_error_estimate = 0; |
| 1196 | 1234 |
| 1197 // Static sequence monitor variables. | 1235 // Static sequence monitor variables. |
| 1198 twopass->kf_zeromotion_pct = 100; | 1236 twopass->kf_zeromotion_pct = 100; |
| 1199 twopass->last_kfgroup_zeromotion_pct = 100; | 1237 twopass->last_kfgroup_zeromotion_pct = 100; |
| 1238 |
| 1239 if (oxcf->resize_mode != RESIZE_NONE) { |
| 1240 vp9_init_subsampling(cpi); |
| 1241 } |
| 1200 } | 1242 } |
| 1201 | 1243 |
| 1202 #define SR_DIFF_PART 0.0015 | 1244 #define SR_DIFF_PART 0.0015 |
| 1203 #define MOTION_AMP_PART 0.003 | 1245 #define MOTION_AMP_PART 0.003 |
| 1204 #define INTRA_PART 0.005 | 1246 #define INTRA_PART 0.005 |
| 1205 #define DEFAULT_DECAY_LIMIT 0.75 | 1247 #define DEFAULT_DECAY_LIMIT 0.75 |
| 1206 #define LOW_SR_DIFF_TRHESH 0.1 | 1248 #define LOW_SR_DIFF_TRHESH 0.1 |
| 1207 #define SR_DIFF_MAX 128.0 | 1249 #define SR_DIFF_MAX 128.0 |
| 1208 | 1250 |
| 1209 static double get_sr_decay_rate(const VP9_COMP *cpi, | 1251 static double get_sr_decay_rate(const VP9_COMP *cpi, |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1682 gf_group->update_type[frame_index] = GF_UPDATE; | 1724 gf_group->update_type[frame_index] = GF_UPDATE; |
| 1683 gf_group->rf_level[frame_index] = GF_ARF_STD; | 1725 gf_group->rf_level[frame_index] = GF_ARF_STD; |
| 1684 } | 1726 } |
| 1685 | 1727 |
| 1686 // Note whether multi-arf was enabled this group for next time. | 1728 // Note whether multi-arf was enabled this group for next time. |
| 1687 cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled; | 1729 cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled; |
| 1688 } | 1730 } |
| 1689 | 1731 |
| 1690 // Analyse and define a gf/arf group. | 1732 // Analyse and define a gf/arf group. |
| 1691 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1733 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1734 VP9_COMMON *const cm = &cpi->common; |
| 1692 RATE_CONTROL *const rc = &cpi->rc; | 1735 RATE_CONTROL *const rc = &cpi->rc; |
| 1693 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1736 VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1694 TWO_PASS *const twopass = &cpi->twopass; | 1737 TWO_PASS *const twopass = &cpi->twopass; |
| 1695 FIRSTPASS_STATS next_frame; | 1738 FIRSTPASS_STATS next_frame; |
| 1696 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; | 1739 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; |
| 1697 int i; | 1740 int i; |
| 1698 | 1741 |
| 1699 double boost_score = 0.0; | 1742 double boost_score = 0.0; |
| 1700 double old_boost_score = 0.0; | 1743 double old_boost_score = 0.0; |
| 1701 double gf_group_err = 0.0; | 1744 double gf_group_err = 0.0; |
| 1745 #if GROUP_ADAPTIVE_MAXQ |
| 1746 double gf_group_raw_error = 0.0; |
| 1747 #endif |
| 1702 double gf_first_frame_err = 0.0; | 1748 double gf_first_frame_err = 0.0; |
| 1703 double mod_frame_err = 0.0; | 1749 double mod_frame_err = 0.0; |
| 1704 | 1750 |
| 1705 double mv_ratio_accumulator = 0.0; | 1751 double mv_ratio_accumulator = 0.0; |
| 1706 double decay_accumulator = 1.0; | 1752 double decay_accumulator = 1.0; |
| 1707 double zero_motion_accumulator = 1.0; | 1753 double zero_motion_accumulator = 1.0; |
| 1708 | 1754 |
| 1709 double loop_decay_rate = 1.00; | 1755 double loop_decay_rate = 1.00; |
| 1710 double last_loop_decay_rate = 1.00; | 1756 double last_loop_decay_rate = 1.00; |
| 1711 | 1757 |
| 1712 double this_frame_mv_in_out = 0.0; | 1758 double this_frame_mv_in_out = 0.0; |
| 1713 double mv_in_out_accumulator = 0.0; | 1759 double mv_in_out_accumulator = 0.0; |
| 1714 double abs_mv_in_out_accumulator = 0.0; | 1760 double abs_mv_in_out_accumulator = 0.0; |
| 1715 double mv_ratio_accumulator_thresh; | 1761 double mv_ratio_accumulator_thresh; |
| 1716 unsigned int allow_alt_ref = is_altref_enabled(cpi); | 1762 unsigned int allow_alt_ref = is_altref_enabled(cpi); |
| 1717 | 1763 |
| 1718 int f_boost = 0; | 1764 int f_boost = 0; |
| 1719 int b_boost = 0; | 1765 int b_boost = 0; |
| 1720 int flash_detected; | 1766 int flash_detected; |
| 1721 int active_max_gf_interval; | 1767 int active_max_gf_interval; |
| 1722 int active_min_gf_interval; | 1768 int active_min_gf_interval; |
| 1723 int64_t gf_group_bits; | 1769 int64_t gf_group_bits; |
| 1724 double gf_group_error_left; | 1770 double gf_group_error_left; |
| 1725 int gf_arf_bits; | 1771 int gf_arf_bits; |
| 1772 int is_key_frame = frame_is_intra_only(cm); |
| 1726 | 1773 |
| 1727 // Reset the GF group data structures unless this is a key | 1774 // Reset the GF group data structures unless this is a key |
| 1728 // frame in which case it will already have been done. | 1775 // frame in which case it will already have been done. |
| 1729 if (cpi->common.frame_type != KEY_FRAME) { | 1776 if (is_key_frame == 0) { |
| 1730 vp9_zero(twopass->gf_group); | 1777 vp9_zero(twopass->gf_group); |
| 1731 } | 1778 } |
| 1732 | 1779 |
| 1733 vp9_clear_system_state(); | 1780 vp9_clear_system_state(); |
| 1734 vp9_zero(next_frame); | 1781 vp9_zero(next_frame); |
| 1735 | 1782 |
| 1736 // Load stats for the current frame. | 1783 // Load stats for the current frame. |
| 1737 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); | 1784 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1738 | 1785 |
| 1739 // Note the error of the frame at the start of the group. This will be | 1786 // Note the error of the frame at the start of the group. This will be |
| 1740 // the GF frame error if we code a normal gf. | 1787 // the GF frame error if we code a normal gf. |
| 1741 gf_first_frame_err = mod_frame_err; | 1788 gf_first_frame_err = mod_frame_err; |
| 1742 | 1789 |
| 1743 // If this is a key frame or the overlay from a previous arf then | 1790 // If this is a key frame or the overlay from a previous arf then |
| 1744 // the error score / cost of this frame has already been accounted for. | 1791 // the error score / cost of this frame has already been accounted for. |
| 1745 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) | 1792 if (is_key_frame || rc->source_alt_ref_active) { |
| 1746 gf_group_err -= gf_first_frame_err; | 1793 gf_group_err -= gf_first_frame_err; |
| 1794 #if GROUP_ADAPTIVE_MAXQ |
| 1795 gf_group_raw_error -= this_frame->coded_error; |
| 1796 #endif |
| 1797 } |
| 1747 | 1798 |
| 1748 // Motion breakout threshold for loop below depends on image size. | 1799 // Motion breakout threshold for loop below depends on image size. |
| 1749 mv_ratio_accumulator_thresh = | 1800 mv_ratio_accumulator_thresh = |
| 1750 (cpi->initial_height + cpi->initial_width) / 4.0; | 1801 (cpi->initial_height + cpi->initial_width) / 4.0; |
| 1751 | 1802 |
| 1752 // Set a maximum and minimum interval for the GF group. | 1803 // Set a maximum and minimum interval for the GF group. |
| 1753 // If the image appears almost completely static we can extend beyond this. | 1804 // If the image appears almost completely static we can extend beyond this. |
| 1754 { | 1805 { |
| 1755 int int_max_q = | 1806 int int_max_q = |
| 1756 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, | 1807 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1775 } | 1826 } |
| 1776 } | 1827 } |
| 1777 | 1828 |
| 1778 i = 0; | 1829 i = 0; |
| 1779 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { | 1830 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { |
| 1780 ++i; | 1831 ++i; |
| 1781 | 1832 |
| 1782 // Accumulate error score of frames in this gf group. | 1833 // Accumulate error score of frames in this gf group. |
| 1783 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); | 1834 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1784 gf_group_err += mod_frame_err; | 1835 gf_group_err += mod_frame_err; |
| 1836 #if GROUP_ADAPTIVE_MAXQ |
| 1837 gf_group_raw_error += this_frame->coded_error; |
| 1838 #endif |
| 1785 | 1839 |
| 1786 if (EOF == input_stats(twopass, &next_frame)) | 1840 if (EOF == input_stats(twopass, &next_frame)) |
| 1787 break; | 1841 break; |
| 1788 | 1842 |
| 1789 // Test for the case where there is a brief flash but the prediction | 1843 // Test for the case where there is a brief flash but the prediction |
| 1790 // quality back to an earlier frame is then restored. | 1844 // quality back to an earlier frame is then restored. |
| 1791 flash_detected = detect_flash(twopass, 0); | 1845 flash_detected = detect_flash(twopass, 0); |
| 1792 | 1846 |
| 1793 // Update the motion related elements to the boost calculation. | 1847 // Update the motion related elements to the boost calculation. |
| 1794 accumulate_frame_motion_stats(&next_frame, | 1848 accumulate_frame_motion_stats(&next_frame, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1836 boost_score = old_boost_score; | 1890 boost_score = old_boost_score; |
| 1837 break; | 1891 break; |
| 1838 } | 1892 } |
| 1839 | 1893 |
| 1840 *this_frame = next_frame; | 1894 *this_frame = next_frame; |
| 1841 old_boost_score = boost_score; | 1895 old_boost_score = boost_score; |
| 1842 } | 1896 } |
| 1843 | 1897 |
| 1844 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); | 1898 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); |
| 1845 | 1899 |
| 1900 // Was the group length constrained by the requirement for a new KF? |
| 1901 rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0; |
| 1902 |
| 1846 // Set the interval until the next gf. | 1903 // Set the interval until the next gf. |
| 1847 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) | 1904 if (is_key_frame || rc->source_alt_ref_active) |
| 1848 rc->baseline_gf_interval = i - 1; | 1905 rc->baseline_gf_interval = i - 1; |
| 1849 else | 1906 else |
| 1850 rc->baseline_gf_interval = i; | 1907 rc->baseline_gf_interval = i; |
| 1851 | 1908 |
| 1852 // Only encode alt reference frame in temporal base layer. So | 1909 // Only encode alt reference frame in temporal base layer. So |
| 1853 // baseline_gf_interval should be multiple of a temporal layer group | 1910 // baseline_gf_interval should be multiple of a temporal layer group |
| 1854 // (typically the frame distance between two base layer frames) | 1911 // (typically the frame distance between two base layer frames) |
| 1855 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { | 1912 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { |
| 1856 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; | 1913 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; |
| 1857 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count); | 1914 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count); |
| 1858 int j; | 1915 int j; |
| 1859 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) { | 1916 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) { |
| 1860 if (EOF == input_stats(twopass, this_frame)) | 1917 if (EOF == input_stats(twopass, this_frame)) |
| 1861 break; | 1918 break; |
| 1862 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame); | 1919 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame); |
| 1920 #if GROUP_ADAPTIVE_MAXQ |
| 1921 gf_group_raw_error += this_frame->coded_error; |
| 1922 #endif |
| 1863 } | 1923 } |
| 1864 rc->baseline_gf_interval = new_gf_interval; | 1924 rc->baseline_gf_interval = new_gf_interval; |
| 1865 } | 1925 } |
| 1866 | 1926 |
| 1867 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 1927 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 1868 | 1928 |
| 1869 // Should we use the alternate reference frame. | 1929 // Should we use the alternate reference frame. |
| 1870 if (allow_alt_ref && | 1930 if (allow_alt_ref && |
| 1871 (i < cpi->oxcf.lag_in_frames) && | 1931 (i < cpi->oxcf.lag_in_frames) && |
| 1872 (i >= MIN_GF_INTERVAL)) { | 1932 (i >= MIN_GF_INTERVAL)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1883 rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST); | 1943 rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST); |
| 1884 rc->source_alt_ref_pending = 0; | 1944 rc->source_alt_ref_pending = 0; |
| 1885 } | 1945 } |
| 1886 | 1946 |
| 1887 // Reset the file position. | 1947 // Reset the file position. |
| 1888 reset_fpf_position(twopass, start_pos); | 1948 reset_fpf_position(twopass, start_pos); |
| 1889 | 1949 |
| 1890 // Calculate the bits to be allocated to the gf/arf group as a whole | 1950 // Calculate the bits to be allocated to the gf/arf group as a whole |
| 1891 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); | 1951 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); |
| 1892 | 1952 |
| 1953 #if GROUP_ADAPTIVE_MAXQ |
| 1954 // Calculate an estimate of the maxq needed for the group. |
| 1955 // We are more agressive about correcting for sections |
| 1956 // where there could be significant overshoot than for easier |
| 1957 // sections where we do not wish to risk creating an overshoot |
| 1958 // of the allocated bit budget. |
| 1959 if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) { |
| 1960 const int vbr_group_bits_per_frame = |
| 1961 (int)(gf_group_bits / rc->baseline_gf_interval); |
| 1962 const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval; |
| 1963 const int tmp_q = |
| 1964 get_twopass_worst_quality(cpi, group_av_err, vbr_group_bits_per_frame, |
| 1965 twopass->kfgroup_inter_fraction); |
| 1966 |
| 1967 if (tmp_q < twopass->baseline_active_worst_quality) { |
| 1968 twopass->active_worst_quality = |
| 1969 (tmp_q + twopass->baseline_active_worst_quality + 1) / 2; |
| 1970 } else { |
| 1971 twopass->active_worst_quality = tmp_q; |
| 1972 } |
| 1973 } |
| 1974 #endif |
| 1975 |
| 1893 // Calculate the extra bits to be used for boosted frame(s) | 1976 // Calculate the extra bits to be used for boosted frame(s) |
| 1894 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, | 1977 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, |
| 1895 rc->gfu_boost, gf_group_bits); | 1978 rc->gfu_boost, gf_group_bits); |
| 1896 | 1979 |
| 1897 // Adjust KF group bits and error remaining. | 1980 // Adjust KF group bits and error remaining. |
| 1898 twopass->kf_group_error_left -= (int64_t)gf_group_err; | 1981 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1899 | 1982 |
| 1900 // If this is an arf update we want to remove the score for the overlay | 1983 // If this is an arf update we want to remove the score for the overlay |
| 1901 // frame at the end which will usually be very cheap to code. | 1984 // frame at the end which will usually be very cheap to code. |
| 1902 // The overlay frame has already, in effect, been coded so we want to spread | 1985 // The overlay frame has already, in effect, been coded so we want to spread |
| 1903 // the remaining bits among the other frames. | 1986 // the remaining bits among the other frames. |
| 1904 // For normal GFs remove the score for the GF itself unless this is | 1987 // For normal GFs remove the score for the GF itself unless this is |
| 1905 // also a key frame in which case it has already been accounted for. | 1988 // also a key frame in which case it has already been accounted for. |
| 1906 if (rc->source_alt_ref_pending) { | 1989 if (rc->source_alt_ref_pending) { |
| 1907 gf_group_error_left = gf_group_err - mod_frame_err; | 1990 gf_group_error_left = gf_group_err - mod_frame_err; |
| 1908 } else if (cpi->common.frame_type != KEY_FRAME) { | 1991 } else if (is_key_frame == 0) { |
| 1909 gf_group_error_left = gf_group_err - gf_first_frame_err; | 1992 gf_group_error_left = gf_group_err - gf_first_frame_err; |
| 1910 } else { | 1993 } else { |
| 1911 gf_group_error_left = gf_group_err; | 1994 gf_group_error_left = gf_group_err; |
| 1912 } | 1995 } |
| 1913 | 1996 |
| 1914 // Allocate bits to each of the frames in the GF group. | 1997 // Allocate bits to each of the frames in the GF group. |
| 1915 allocate_gf_group_bits(cpi, gf_group_bits, gf_group_error_left, gf_arf_bits); | 1998 allocate_gf_group_bits(cpi, gf_group_bits, gf_group_error_left, gf_arf_bits); |
| 1916 | 1999 |
| 1917 // Reset the file position. | 2000 // Reset the file position. |
| 1918 reset_fpf_position(twopass, start_pos); | 2001 reset_fpf_position(twopass, start_pos); |
| 1919 | 2002 |
| 1920 // Calculate a section intra ratio used in setting max loop filter. | 2003 // Calculate a section intra ratio used in setting max loop filter. |
| 1921 if (cpi->common.frame_type != KEY_FRAME) { | 2004 if (cpi->common.frame_type != KEY_FRAME) { |
| 1922 twopass->section_intra_rating = | 2005 twopass->section_intra_rating = |
| 1923 calculate_section_intra_ratio(start_pos, twopass->stats_in_end, | 2006 calculate_section_intra_ratio(start_pos, twopass->stats_in_end, |
| 1924 rc->baseline_gf_interval); | 2007 rc->baseline_gf_interval); |
| 1925 } | 2008 } |
| 2009 |
| 2010 if (oxcf->resize_mode == RESIZE_DYNAMIC) { |
| 2011 // Default to starting GF groups at normal frame size. |
| 2012 cpi->rc.next_frame_size_selector = UNSCALED; |
| 2013 } |
| 1926 } | 2014 } |
| 1927 | 2015 |
| 1928 // TODO(PGW) Re-examine the use of II ration in this code in the light of# | 2016 // TODO(PGW) Re-examine the use of II ration in this code in the light of# |
| 1929 // changes elsewhere | 2017 // changes elsewhere |
| 1930 #define KF_II_MAX 128.0 | 2018 #define KF_II_MAX 128.0 |
| 1931 static int test_candidate_kf(TWO_PASS *twopass, | 2019 static int test_candidate_kf(TWO_PASS *twopass, |
| 1932 const FIRSTPASS_STATS *last_frame, | 2020 const FIRSTPASS_STATS *last_frame, |
| 1933 const FIRSTPASS_STATS *this_frame, | 2021 const FIRSTPASS_STATS *this_frame, |
| 1934 const FIRSTPASS_STATS *next_frame) { | 2022 const FIRSTPASS_STATS *next_frame) { |
| 1935 int is_viable_kf = 0; | 2023 int is_viable_kf = 0; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2106 // is between 1x and 2x. | 2194 // is between 1x and 2x. |
| 2107 if (cpi->oxcf.auto_key && | 2195 if (cpi->oxcf.auto_key && |
| 2108 rc->frames_to_key > cpi->oxcf.key_freq) { | 2196 rc->frames_to_key > cpi->oxcf.key_freq) { |
| 2109 FIRSTPASS_STATS tmp_frame = first_frame; | 2197 FIRSTPASS_STATS tmp_frame = first_frame; |
| 2110 | 2198 |
| 2111 rc->frames_to_key /= 2; | 2199 rc->frames_to_key /= 2; |
| 2112 | 2200 |
| 2113 // Reset to the start of the group. | 2201 // Reset to the start of the group. |
| 2114 reset_fpf_position(twopass, start_position); | 2202 reset_fpf_position(twopass, start_position); |
| 2115 | 2203 |
| 2116 kf_group_err = 0; | 2204 kf_group_err = 0.0; |
| 2117 | 2205 |
| 2118 // Rescan to get the correct error data for the forced kf group. | 2206 // Rescan to get the correct error data for the forced kf group. |
| 2119 for (i = 0; i < rc->frames_to_key; ++i) { | 2207 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2120 kf_group_err += calculate_modified_err(twopass, oxcf, &tmp_frame); | 2208 kf_group_err += calculate_modified_err(twopass, oxcf, &tmp_frame); |
| 2121 input_stats(twopass, &tmp_frame); | 2209 input_stats(twopass, &tmp_frame); |
| 2122 } | 2210 } |
| 2123 rc->next_key_frame_forced = 1; | 2211 rc->next_key_frame_forced = 1; |
| 2124 } else if (twopass->stats_in == twopass->stats_in_end || | 2212 } else if (twopass->stats_in == twopass->stats_in_end || |
| 2125 rc->frames_to_key >= cpi->oxcf.key_freq) { | 2213 rc->frames_to_key >= cpi->oxcf.key_freq) { |
| 2126 rc->next_key_frame_forced = 1; | 2214 rc->next_key_frame_forced = 1; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2216 | 2304 |
| 2217 // Apply various clamps for min and max boost | 2305 // Apply various clamps for min and max boost |
| 2218 rc->kf_boost = (int)(av_decay_accumulator * boost_score); | 2306 rc->kf_boost = (int)(av_decay_accumulator * boost_score); |
| 2219 rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3)); | 2307 rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3)); |
| 2220 rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST); | 2308 rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST); |
| 2221 | 2309 |
| 2222 // Work out how many bits to allocate for the key frame itself. | 2310 // Work out how many bits to allocate for the key frame itself. |
| 2223 kf_bits = calculate_boost_bits((rc->frames_to_key - 1), | 2311 kf_bits = calculate_boost_bits((rc->frames_to_key - 1), |
| 2224 rc->kf_boost, twopass->kf_group_bits); | 2312 rc->kf_boost, twopass->kf_group_bits); |
| 2225 | 2313 |
| 2314 // Work out the fraction of the kf group bits reserved for the inter frames |
| 2315 // within the group after discounting the bits for the kf itself. |
| 2316 if (twopass->kf_group_bits) { |
| 2317 twopass->kfgroup_inter_fraction = |
| 2318 (double)(twopass->kf_group_bits - kf_bits) / |
| 2319 (double)twopass->kf_group_bits; |
| 2320 } else { |
| 2321 twopass->kfgroup_inter_fraction = 1.0; |
| 2322 } |
| 2323 |
| 2226 twopass->kf_group_bits -= kf_bits; | 2324 twopass->kf_group_bits -= kf_bits; |
| 2227 | 2325 |
| 2228 // Save the bits to spend on the key frame. | 2326 // Save the bits to spend on the key frame. |
| 2229 gf_group->bit_allocation[0] = kf_bits; | 2327 gf_group->bit_allocation[0] = kf_bits; |
| 2230 gf_group->update_type[0] = KF_UPDATE; | 2328 gf_group->update_type[0] = KF_UPDATE; |
| 2231 gf_group->rf_level[0] = KF_STD; | 2329 gf_group->rf_level[0] = KF_STD; |
| 2232 | 2330 |
| 2233 // Note the total error score of the kf group minus the key frame itself. | 2331 // Note the total error score of the kf group minus the key frame itself. |
| 2234 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2332 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2235 | 2333 |
| 2236 // Adjust the count of total modified error left. | 2334 // Adjust the count of total modified error left. |
| 2237 // The count of bits left is adjusted elsewhere based on real coded frame | 2335 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2238 // sizes. | 2336 // sizes. |
| 2239 twopass->modified_error_left -= kf_group_err; | 2337 twopass->modified_error_left -= kf_group_err; |
| 2338 |
| 2339 if (oxcf->resize_mode == RESIZE_DYNAMIC) { |
| 2340 // Default to normal-sized frame on keyframes. |
| 2341 cpi->rc.next_frame_size_selector = UNSCALED; |
| 2342 } |
| 2240 } | 2343 } |
| 2241 | 2344 |
| 2242 // Define the reference buffers that will be updated post encode. | 2345 // Define the reference buffers that will be updated post encode. |
| 2243 void configure_buffer_updates(VP9_COMP *cpi) { | 2346 void configure_buffer_updates(VP9_COMP *cpi) { |
| 2244 TWO_PASS *const twopass = &cpi->twopass; | 2347 TWO_PASS *const twopass = &cpi->twopass; |
| 2245 | 2348 |
| 2246 cpi->rc.is_src_frame_alt_ref = 0; | 2349 cpi->rc.is_src_frame_alt_ref = 0; |
| 2247 switch (twopass->gf_group.update_type[twopass->gf_group.index]) { | 2350 switch (twopass->gf_group.update_type[twopass->gf_group.index]) { |
| 2248 case KF_UPDATE: | 2351 case KF_UPDATE: |
| 2249 cpi->refresh_last_frame = 1; | 2352 cpi->refresh_last_frame = 1; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2306 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1); | 2409 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1); |
| 2307 } | 2410 } |
| 2308 | 2411 |
| 2309 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { | 2412 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2310 VP9_COMMON *const cm = &cpi->common; | 2413 VP9_COMMON *const cm = &cpi->common; |
| 2311 RATE_CONTROL *const rc = &cpi->rc; | 2414 RATE_CONTROL *const rc = &cpi->rc; |
| 2312 TWO_PASS *const twopass = &cpi->twopass; | 2415 TWO_PASS *const twopass = &cpi->twopass; |
| 2313 GF_GROUP *const gf_group = &twopass->gf_group; | 2416 GF_GROUP *const gf_group = &twopass->gf_group; |
| 2314 int frames_left; | 2417 int frames_left; |
| 2315 FIRSTPASS_STATS this_frame; | 2418 FIRSTPASS_STATS this_frame; |
| 2316 FIRSTPASS_STATS this_frame_copy; | |
| 2317 | 2419 |
| 2318 int target_rate; | 2420 int target_rate; |
| 2319 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? | 2421 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? |
| 2320 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0; | 2422 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0; |
| 2321 | 2423 |
| 2322 if (lc != NULL) { | 2424 if (lc != NULL) { |
| 2323 frames_left = (int)(twopass->total_stats.count - | 2425 frames_left = (int)(twopass->total_stats.count - |
| 2324 lc->current_video_frame_in_layer); | 2426 lc->current_video_frame_in_layer); |
| 2325 } else { | 2427 } else { |
| 2326 frames_left = (int)(twopass->total_stats.count - | 2428 frames_left = (int)(twopass->total_stats.count - |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2364 | 2466 |
| 2365 vp9_clear_system_state(); | 2467 vp9_clear_system_state(); |
| 2366 | 2468 |
| 2367 if (cpi->oxcf.rc_mode == VPX_Q) { | 2469 if (cpi->oxcf.rc_mode == VPX_Q) { |
| 2368 twopass->active_worst_quality = cpi->oxcf.cq_level; | 2470 twopass->active_worst_quality = cpi->oxcf.cq_level; |
| 2369 } else if (cm->current_video_frame == 0 || | 2471 } else if (cm->current_video_frame == 0 || |
| 2370 (lc != NULL && lc->current_video_frame_in_layer == 0)) { | 2472 (lc != NULL && lc->current_video_frame_in_layer == 0)) { |
| 2371 // Special case code for first frame. | 2473 // Special case code for first frame. |
| 2372 const int section_target_bandwidth = (int)(twopass->bits_left / | 2474 const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2373 frames_left); | 2475 frames_left); |
| 2374 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, | 2476 const double section_error = |
| 2375 section_target_bandwidth); | 2477 twopass->total_left_stats.coded_error / twopass->total_left_stats.count; |
| 2478 const int tmp_q = |
| 2479 get_twopass_worst_quality(cpi, section_error, |
| 2480 section_target_bandwidth, DEFAULT_GRP_WEIGHT); |
| 2481 |
| 2376 twopass->active_worst_quality = tmp_q; | 2482 twopass->active_worst_quality = tmp_q; |
| 2483 twopass->baseline_active_worst_quality = tmp_q; |
| 2377 rc->ni_av_qi = tmp_q; | 2484 rc->ni_av_qi = tmp_q; |
| 2378 rc->last_q[INTER_FRAME] = tmp_q; | 2485 rc->last_q[INTER_FRAME] = tmp_q; |
| 2379 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); | 2486 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); |
| 2380 rc->avg_frame_qindex[INTER_FRAME] = tmp_q; | 2487 rc->avg_frame_qindex[INTER_FRAME] = tmp_q; |
| 2381 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2; | 2488 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2; |
| 2382 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; | 2489 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; |
| 2383 } | 2490 } |
| 2384 vp9_zero(this_frame); | 2491 vp9_zero(this_frame); |
| 2385 if (EOF == input_stats(twopass, &this_frame)) | 2492 if (EOF == input_stats(twopass, &this_frame)) |
| 2386 return; | 2493 return; |
| 2387 | 2494 |
| 2388 // Local copy of the current frame's first pass stats. | |
| 2389 this_frame_copy = this_frame; | |
| 2390 | |
| 2391 // Keyframe and section processing. | 2495 // Keyframe and section processing. |
| 2392 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) { | 2496 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) { |
| 2497 FIRSTPASS_STATS this_frame_copy; |
| 2498 this_frame_copy = this_frame; |
| 2393 // Define next KF group and assign bits to it. | 2499 // Define next KF group and assign bits to it. |
| 2394 find_next_key_frame(cpi, &this_frame_copy); | 2500 find_next_key_frame(cpi, &this_frame); |
| 2501 this_frame = this_frame_copy; |
| 2395 } else { | 2502 } else { |
| 2396 cm->frame_type = INTER_FRAME; | 2503 cm->frame_type = INTER_FRAME; |
| 2397 } | 2504 } |
| 2398 | 2505 |
| 2399 if (lc != NULL) { | 2506 if (lc != NULL) { |
| 2400 if (cpi->svc.spatial_layer_id == 0) { | 2507 if (cpi->svc.spatial_layer_id == 0) { |
| 2401 lc->is_key_frame = (cm->frame_type == KEY_FRAME); | 2508 lc->is_key_frame = (cm->frame_type == KEY_FRAME); |
| 2402 if (lc->is_key_frame) { | 2509 if (lc->is_key_frame) { |
| 2403 cpi->ref_frame_flags &= | 2510 cpi->ref_frame_flags &= |
| 2404 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); | 2511 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 2405 lc->frames_from_key_frame = 0; | 2512 lc->frames_from_key_frame = 0; |
| 2406 // Reset the empty frame resolution since we have a key frame. | 2513 // Reset the empty frame resolution since we have a key frame. |
| 2407 cpi->svc.empty_frame_width = cm->width; | 2514 cpi->svc.empty_frame_width = cm->width; |
| 2408 cpi->svc.empty_frame_height = cm->height; | 2515 cpi->svc.empty_frame_height = cm->height; |
| 2409 } | 2516 } |
| 2410 } else { | 2517 } else { |
| 2411 cm->frame_type = INTER_FRAME; | 2518 cm->frame_type = INTER_FRAME; |
| 2412 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 2519 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
| 2413 | 2520 |
| 2414 if (lc->is_key_frame) { | 2521 if (lc->is_key_frame) { |
| 2415 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); | 2522 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 2416 lc->frames_from_key_frame = 0; | 2523 lc->frames_from_key_frame = 0; |
| 2417 } | 2524 } |
| 2418 } | 2525 } |
| 2419 } | 2526 } |
| 2420 | 2527 |
| 2421 // Define a new GF/ARF group. (Should always enter here for key frames). | 2528 // Define a new GF/ARF group. (Should always enter here for key frames). |
| 2422 if (rc->frames_till_gf_update_due == 0) { | 2529 if (rc->frames_till_gf_update_due == 0) { |
| 2423 define_gf_group(cpi, &this_frame_copy); | 2530 define_gf_group(cpi, &this_frame); |
| 2424 | 2531 |
| 2425 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 2532 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 2426 if (lc != NULL) | 2533 if (lc != NULL) |
| 2427 cpi->refresh_golden_frame = 1; | 2534 cpi->refresh_golden_frame = 1; |
| 2428 | 2535 |
| 2429 #if ARF_STATS_OUTPUT | 2536 #if ARF_STATS_OUTPUT |
| 2430 { | 2537 { |
| 2431 FILE *fpfile; | 2538 FILE *fpfile; |
| 2432 fpfile = fopen("arf.stt", "a"); | 2539 fpfile = fopen("arf.stt", "a"); |
| 2433 ++arf_count; | 2540 ++arf_count; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2530 // Unwind undershoot or overshoot adjustment. | 2637 // Unwind undershoot or overshoot adjustment. |
| 2531 if (rc->rolling_target_bits < rc->rolling_actual_bits) | 2638 if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2532 --twopass->extend_minq; | 2639 --twopass->extend_minq; |
| 2533 else if (rc->rolling_target_bits > rc->rolling_actual_bits) | 2640 else if (rc->rolling_target_bits > rc->rolling_actual_bits) |
| 2534 --twopass->extend_maxq; | 2641 --twopass->extend_maxq; |
| 2535 } | 2642 } |
| 2536 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); | 2643 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); |
| 2537 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); | 2644 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); |
| 2538 } | 2645 } |
| 2539 } | 2646 } |
| OLD | NEW |