| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 return vp9_ac_quant(qindex, 0) / 4.0; | 97 return vp9_ac_quant(qindex, 0) / 4.0; |
| 98 } | 98 } |
| 99 | 99 |
| 100 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, | 100 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
| 101 double correction_factor) { | 101 double correction_factor) { |
| 102 const double q = vp9_convert_qindex_to_q(qindex); | 102 const double q = vp9_convert_qindex_to_q(qindex); |
| 103 int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; | 103 int enumerator = frame_type == KEY_FRAME ? 3300000 : 2250000; |
| 104 | 104 |
| 105 // q based adjustment to baseline enumerator | 105 // q based adjustment to baseline enumerator |
| 106 enumerator += (int)(enumerator * q) >> 12; | 106 enumerator += (int)(enumerator * q) >> 12; |
| 107 return (int)(0.5 + (enumerator * correction_factor / q)); | 107 return (int)(enumerator * correction_factor / q); |
| 108 } | 108 } |
| 109 | 109 |
| 110 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, | 110 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, |
| 111 double correction_factor) { | 111 double correction_factor) { |
| 112 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor)); | 112 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor)); |
| 113 return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS; | 113 return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS; |
| 114 } | 114 } |
| 115 | 115 |
| 116 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { | 116 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { |
| 117 const RATE_CONTROL *rc = &cpi->rc; | 117 const RATE_CONTROL *rc = &cpi->rc; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 } | 270 } |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 | 274 |
| 275 static double get_rate_correction_factor(const VP9_COMP *cpi) { | 275 static double get_rate_correction_factor(const VP9_COMP *cpi) { |
| 276 const RATE_CONTROL *const rc = &cpi->rc; | 276 const RATE_CONTROL *const rc = &cpi->rc; |
| 277 | 277 |
| 278 if (cpi->common.frame_type == KEY_FRAME) { | 278 if (cpi->common.frame_type == KEY_FRAME) { |
| 279 return rc->rate_correction_factors[KF_STD]; | 279 return rc->rate_correction_factors[KF_STD]; |
| 280 } else if (cpi->pass == 2) { | 280 } else if (cpi->oxcf.pass == 2) { |
| 281 RATE_FACTOR_LEVEL rf_lvl = | 281 RATE_FACTOR_LEVEL rf_lvl = |
| 282 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; | 282 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
| 283 return rc->rate_correction_factors[rf_lvl]; | 283 return rc->rate_correction_factors[rf_lvl]; |
| 284 } else { | 284 } else { |
| 285 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 285 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
| 286 !rc->is_src_frame_alt_ref && | 286 !rc->is_src_frame_alt_ref && |
| 287 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) | 287 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) |
| 288 return rc->rate_correction_factors[GF_ARF_STD]; | 288 return rc->rate_correction_factors[GF_ARF_STD]; |
| 289 else | 289 else |
| 290 return rc->rate_correction_factors[INTER_NORMAL]; | 290 return rc->rate_correction_factors[INTER_NORMAL]; |
| 291 } | 291 } |
| 292 } | 292 } |
| 293 | 293 |
| 294 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { | 294 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { |
| 295 RATE_CONTROL *const rc = &cpi->rc; | 295 RATE_CONTROL *const rc = &cpi->rc; |
| 296 | 296 |
| 297 if (cpi->common.frame_type == KEY_FRAME) { | 297 if (cpi->common.frame_type == KEY_FRAME) { |
| 298 rc->rate_correction_factors[KF_STD] = factor; | 298 rc->rate_correction_factors[KF_STD] = factor; |
| 299 } else if (cpi->pass == 2) { | 299 } else if (cpi->oxcf.pass == 2) { |
| 300 RATE_FACTOR_LEVEL rf_lvl = | 300 RATE_FACTOR_LEVEL rf_lvl = |
| 301 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; | 301 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
| 302 rc->rate_correction_factors[rf_lvl] = factor; | 302 rc->rate_correction_factors[rf_lvl] = factor; |
| 303 } else { | 303 } else { |
| 304 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 304 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
| 305 !rc->is_src_frame_alt_ref && | 305 !rc->is_src_frame_alt_ref && |
| 306 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) | 306 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) |
| 307 rc->rate_correction_factors[GF_ARF_STD] = factor; | 307 rc->rate_correction_factors[GF_ARF_STD] = factor; |
| 308 else | 308 else |
| 309 rc->rate_correction_factors[INTER_NORMAL] = factor; | 309 rc->rate_correction_factors[INTER_NORMAL] = factor; |
| (...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 916 *top_index >= rc->best_quality); | 916 *top_index >= rc->best_quality); |
| 917 assert(*bottom_index <= rc->worst_quality && | 917 assert(*bottom_index <= rc->worst_quality && |
| 918 *bottom_index >= rc->best_quality); | 918 *bottom_index >= rc->best_quality); |
| 919 assert(q <= rc->worst_quality && q >= rc->best_quality); | 919 assert(q <= rc->worst_quality && q >= rc->best_quality); |
| 920 return q; | 920 return q; |
| 921 } | 921 } |
| 922 | 922 |
| 923 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, | 923 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, |
| 924 int *bottom_index, int *top_index) { | 924 int *bottom_index, int *top_index) { |
| 925 int q; | 925 int q; |
| 926 if (cpi->pass == 0) { | 926 if (cpi->oxcf.pass == 0) { |
| 927 if (cpi->oxcf.rc_mode == VPX_CBR) | 927 if (cpi->oxcf.rc_mode == VPX_CBR) |
| 928 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index); | 928 q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index); |
| 929 else | 929 else |
| 930 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index); | 930 q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index); |
| 931 } else { | 931 } else { |
| 932 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index); | 932 q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index); |
| 933 } | 933 } |
| 934 if (cpi->sf.use_nonrd_pick_mode) { | 934 if (cpi->sf.use_nonrd_pick_mode) { |
| 935 if (cpi->sf.force_frame_boost == 1) | 935 if (cpi->sf.force_frame_boost == 1) |
| 936 q -= cpi->sf.max_delta_qindex; | 936 q -= cpi->sf.max_delta_qindex; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 984 } | 984 } |
| 985 | 985 |
| 986 static void update_golden_frame_stats(VP9_COMP *cpi) { | 986 static void update_golden_frame_stats(VP9_COMP *cpi) { |
| 987 RATE_CONTROL *const rc = &cpi->rc; | 987 RATE_CONTROL *const rc = &cpi->rc; |
| 988 | 988 |
| 989 // Update the Golden frame usage counts. | 989 // Update the Golden frame usage counts. |
| 990 if (cpi->refresh_golden_frame) { | 990 if (cpi->refresh_golden_frame) { |
| 991 // this frame refreshes means next frames don't unless specified by user | 991 // this frame refreshes means next frames don't unless specified by user |
| 992 rc->frames_since_golden = 0; | 992 rc->frames_since_golden = 0; |
| 993 | 993 |
| 994 if (cpi->pass == 2) { | 994 if (cpi->oxcf.pass == 2) { |
| 995 if (!rc->source_alt_ref_pending && | 995 if (!rc->source_alt_ref_pending && |
| 996 cpi->twopass.gf_group.rf_level[0] == GF_ARF_STD) | 996 cpi->twopass.gf_group.rf_level[0] == GF_ARF_STD) |
| 997 rc->source_alt_ref_active = 0; | 997 rc->source_alt_ref_active = 0; |
| 998 } else if (!rc->source_alt_ref_pending) { | 998 } else if (!rc->source_alt_ref_pending) { |
| 999 rc->source_alt_ref_active = 0; | 999 rc->source_alt_ref_active = 0; |
| 1000 } | 1000 } |
| 1001 | 1001 |
| 1002 // Decrement count down till next gf | 1002 // Decrement count down till next gf |
| 1003 if (rc->frames_till_gf_update_due > 0) | 1003 if (rc->frames_till_gf_update_due > 0) |
| 1004 rc->frames_till_gf_update_due--; | 1004 rc->frames_till_gf_update_due--; |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1201 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { | 1201 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { |
| 1202 const RATE_CONTROL *rc = &cpi->rc; | 1202 const RATE_CONTROL *rc = &cpi->rc; |
| 1203 const VP9EncoderConfig *oxcf = &cpi->oxcf; | 1203 const VP9EncoderConfig *oxcf = &cpi->oxcf; |
| 1204 const SVC *const svc = &cpi->svc; | 1204 const SVC *const svc = &cpi->svc; |
| 1205 int target; | 1205 int target; |
| 1206 if (cpi->common.current_video_frame == 0) { | 1206 if (cpi->common.current_video_frame == 0) { |
| 1207 target = ((rc->starting_buffer_level / 2) > INT_MAX) | 1207 target = ((rc->starting_buffer_level / 2) > INT_MAX) |
| 1208 ? INT_MAX : (int)(rc->starting_buffer_level / 2); | 1208 ? INT_MAX : (int)(rc->starting_buffer_level / 2); |
| 1209 } else { | 1209 } else { |
| 1210 int kf_boost = 32; | 1210 int kf_boost = 32; |
| 1211 double framerate = oxcf->framerate; | 1211 double framerate = cpi->framerate; |
| 1212 if (svc->number_temporal_layers > 1 && | 1212 if (svc->number_temporal_layers > 1 && |
| 1213 oxcf->rc_mode == VPX_CBR) { | 1213 oxcf->rc_mode == VPX_CBR) { |
| 1214 // Use the layer framerate for temporal layers CBR mode. | 1214 // Use the layer framerate for temporal layers CBR mode. |
| 1215 const LAYER_CONTEXT *lc = &svc->layer_context[svc->temporal_layer_id]; | 1215 const LAYER_CONTEXT *lc = &svc->layer_context[svc->temporal_layer_id]; |
| 1216 framerate = lc->framerate; | 1216 framerate = lc->framerate; |
| 1217 } | 1217 } |
| 1218 kf_boost = MAX(kf_boost, (int)(2 * framerate - 16)); | 1218 kf_boost = MAX(kf_boost, (int)(2 * framerate - 16)); |
| 1219 if (rc->frames_since_key < framerate / 2) { | 1219 if (rc->frames_since_key < framerate / 2) { |
| 1220 kf_boost = (int)(kf_boost * rc->frames_since_key / | 1220 kf_boost = (int)(kf_boost * rc->frames_since_key / |
| 1221 (framerate / 2)); | 1221 (framerate / 2)); |
| 1222 } | 1222 } |
| 1223 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; | 1223 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; |
| 1224 } | 1224 } |
| 1225 return vp9_rc_clamp_iframe_target_size(cpi, target); | 1225 return vp9_rc_clamp_iframe_target_size(cpi, target); |
| 1226 } | 1226 } |
| 1227 | 1227 |
| 1228 void vp9_rc_get_svc_params(VP9_COMP *cpi) { | 1228 void vp9_rc_get_svc_params(VP9_COMP *cpi) { |
| 1229 VP9_COMMON *const cm = &cpi->common; | 1229 VP9_COMMON *const cm = &cpi->common; |
| 1230 RATE_CONTROL *const rc = &cpi->rc; | 1230 RATE_CONTROL *const rc = &cpi->rc; |
| 1231 int target = rc->avg_frame_bandwidth; | 1231 int target = rc->avg_frame_bandwidth; |
| 1232 if ((cm->current_video_frame == 0) || | 1232 if ((cm->current_video_frame == 0) || |
| 1233 (cpi->frame_flags & FRAMEFLAGS_KEY) || | 1233 (cpi->frame_flags & FRAMEFLAGS_KEY) || |
| 1234 (cpi->oxcf.auto_key && (rc->frames_since_key % | 1234 (cpi->oxcf.auto_key && (rc->frames_since_key % |
| 1235 cpi->oxcf.key_freq == 0))) { | 1235 cpi->oxcf.key_freq == 0))) { |
| 1236 cm->frame_type = KEY_FRAME; | 1236 cm->frame_type = KEY_FRAME; |
| 1237 rc->source_alt_ref_active = 0; | 1237 rc->source_alt_ref_active = 0; |
| 1238 | 1238 |
| 1239 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 1239 if (is_spatial_svc(cpi)) { |
| 1240 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame = 1; | 1240 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame = 1; |
| 1241 cpi->ref_frame_flags &= |
| 1242 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 1241 } | 1243 } |
| 1242 | 1244 |
| 1243 if (cpi->pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) { | 1245 if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 1244 target = calc_iframe_target_size_one_pass_cbr(cpi); | 1246 target = calc_iframe_target_size_one_pass_cbr(cpi); |
| 1245 } | 1247 } |
| 1246 } else { | 1248 } else { |
| 1247 cm->frame_type = INTER_FRAME; | 1249 cm->frame_type = INTER_FRAME; |
| 1248 | 1250 |
| 1249 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 1251 if (is_spatial_svc(cpi)) { |
| 1250 LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 1252 LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 1251 if (cpi->svc.spatial_layer_id == 0) { | 1253 if (cpi->svc.spatial_layer_id == 0) { |
| 1252 lc->is_key_frame = 0; | 1254 lc->is_key_frame = 0; |
| 1253 } else { | 1255 } else { |
| 1254 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 1256 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
| 1257 if (lc->is_key_frame) |
| 1258 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 1255 } | 1259 } |
| 1260 cpi->ref_frame_flags &= (~VP9_ALT_FLAG); |
| 1256 } | 1261 } |
| 1257 | 1262 |
| 1258 if (cpi->pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) { | 1263 if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) { |
| 1259 target = calc_pframe_target_size_one_pass_cbr(cpi); | 1264 target = calc_pframe_target_size_one_pass_cbr(cpi); |
| 1260 } | 1265 } |
| 1261 } | 1266 } |
| 1262 vp9_rc_set_frame_target(cpi, target); | 1267 vp9_rc_set_frame_target(cpi, target); |
| 1263 rc->frames_till_gf_update_due = INT_MAX; | 1268 rc->frames_till_gf_update_due = INT_MAX; |
| 1264 rc->baseline_gf_interval = INT_MAX; | 1269 rc->baseline_gf_interval = INT_MAX; |
| 1265 } | 1270 } |
| 1266 | 1271 |
| 1267 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { | 1272 void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { |
| 1268 VP9_COMMON *const cm = &cpi->common; | 1273 VP9_COMMON *const cm = &cpi->common; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1352 if (rc->max_gf_interval > rc->static_scene_max_gf_interval) | 1357 if (rc->max_gf_interval > rc->static_scene_max_gf_interval) |
| 1353 rc->max_gf_interval = rc->static_scene_max_gf_interval; | 1358 rc->max_gf_interval = rc->static_scene_max_gf_interval; |
| 1354 } | 1359 } |
| 1355 | 1360 |
| 1356 void vp9_rc_update_framerate(VP9_COMP *cpi) { | 1361 void vp9_rc_update_framerate(VP9_COMP *cpi) { |
| 1357 const VP9_COMMON *const cm = &cpi->common; | 1362 const VP9_COMMON *const cm = &cpi->common; |
| 1358 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1363 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1359 RATE_CONTROL *const rc = &cpi->rc; | 1364 RATE_CONTROL *const rc = &cpi->rc; |
| 1360 int vbr_max_bits; | 1365 int vbr_max_bits; |
| 1361 | 1366 |
| 1362 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / oxcf->framerate); | 1367 rc->avg_frame_bandwidth = (int)(oxcf->target_bandwidth / cpi->framerate); |
| 1363 rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth * | 1368 rc->min_frame_bandwidth = (int)(rc->avg_frame_bandwidth * |
| 1364 oxcf->two_pass_vbrmin_section / 100); | 1369 oxcf->two_pass_vbrmin_section / 100); |
| 1365 | 1370 |
| 1366 rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS); | 1371 rc->min_frame_bandwidth = MAX(rc->min_frame_bandwidth, FRAME_OVERHEAD_BITS); |
| 1367 | 1372 |
| 1368 // A maximum bitrate for a frame is defined. | 1373 // A maximum bitrate for a frame is defined. |
| 1369 // The baseline for this aligns with HW implementations that | 1374 // The baseline for this aligns with HW implementations that |
| 1370 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits | 1375 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits |
| 1371 // per 16x16 MB (averaged over a frame). However this limit is extended if | 1376 // per 16x16 MB (averaged over a frame). However this limit is extended if |
| 1372 // a very high rate is given on the command line or the the rate cannnot | 1377 // a very high rate is given on the command line or the the rate cannnot |
| 1373 // be acheived because of a user specificed max q (e.g. when the user | 1378 // be acheived because of a user specificed max q (e.g. when the user |
| 1374 // specifies lossless encode. | 1379 // specifies lossless encode. |
| 1375 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * | 1380 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * |
| 1376 oxcf->two_pass_vbrmax_section) / 100); | 1381 oxcf->two_pass_vbrmax_section) / 100); |
| 1377 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), | 1382 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), |
| 1378 vbr_max_bits); | 1383 vbr_max_bits); |
| 1379 | 1384 |
| 1380 vp9_rc_set_gf_max_interval(cpi, rc); | 1385 vp9_rc_set_gf_max_interval(cpi, rc); |
| 1381 } | 1386 } |
| OLD | NEW |