| 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 if (oxcf->rc_max_intra_bitrate_pct) { | 227 if (oxcf->rc_max_intra_bitrate_pct) { |
| 228 const int max_rate = rc->avg_frame_bandwidth * | 228 const int max_rate = rc->avg_frame_bandwidth * |
| 229 oxcf->rc_max_intra_bitrate_pct / 100; | 229 oxcf->rc_max_intra_bitrate_pct / 100; |
| 230 target = MIN(target, max_rate); | 230 target = MIN(target, max_rate); |
| 231 } | 231 } |
| 232 if (target > rc->max_frame_bandwidth) | 232 if (target > rc->max_frame_bandwidth) |
| 233 target = rc->max_frame_bandwidth; | 233 target = rc->max_frame_bandwidth; |
| 234 return target; | 234 return target; |
| 235 } | 235 } |
| 236 | 236 |
| 237 // Update the buffer level for higher layers, given the encoded current layer. | 237 // Update the buffer level for higher temporal layers, given the encoded current |
| 238 // temporal layer. |
| 238 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) { | 239 static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) { |
| 239 int temporal_layer = 0; | 240 int i = 0; |
| 240 int current_temporal_layer = svc->temporal_layer_id; | 241 int current_temporal_layer = svc->temporal_layer_id; |
| 241 for (temporal_layer = current_temporal_layer + 1; | 242 for (i = current_temporal_layer + 1; |
| 242 temporal_layer < svc->number_temporal_layers; ++temporal_layer) { | 243 i < svc->number_temporal_layers; ++i) { |
| 243 LAYER_CONTEXT *lc = &svc->layer_context[temporal_layer]; | 244 const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, i, |
| 245 svc->number_temporal_layers); |
| 246 LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
| 244 RATE_CONTROL *lrc = &lc->rc; | 247 RATE_CONTROL *lrc = &lc->rc; |
| 245 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate - | 248 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate - |
| 246 encoded_frame_size); | 249 encoded_frame_size); |
| 247 lrc->bits_off_target += bits_off_for_this_layer; | 250 lrc->bits_off_target += bits_off_for_this_layer; |
| 248 | 251 |
| 249 // Clip buffer level to maximum buffer size for the layer. | 252 // Clip buffer level to maximum buffer size for the layer. |
| 250 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size); | 253 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size); |
| 251 lrc->buffer_level = lrc->bits_off_target; | 254 lrc->buffer_level = lrc->bits_off_target; |
| 252 } | 255 } |
| 253 } | 256 } |
| 254 | 257 |
| 255 // Update the buffer level: leaky bucket model. | 258 // Update the buffer level: leaky bucket model. |
| 256 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { | 259 static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { |
| 257 const VP9_COMMON *const cm = &cpi->common; | 260 const VP9_COMMON *const cm = &cpi->common; |
| 258 RATE_CONTROL *const rc = &cpi->rc; | 261 RATE_CONTROL *const rc = &cpi->rc; |
| 259 | 262 |
| 260 // Non-viewable frames are a special case and are treated as pure overhead. | 263 // Non-viewable frames are a special case and are treated as pure overhead. |
| 261 if (!cm->show_frame) { | 264 if (!cm->show_frame) { |
| 262 rc->bits_off_target -= encoded_frame_size; | 265 rc->bits_off_target -= encoded_frame_size; |
| 263 } else { | 266 } else { |
| 264 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size; | 267 rc->bits_off_target += rc->avg_frame_bandwidth - encoded_frame_size; |
| 265 } | 268 } |
| 266 | 269 |
| 267 // Clip the buffer level to the maximum specified buffer size. | 270 // Clip the buffer level to the maximum specified buffer size. |
| 268 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size); | 271 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size); |
| 269 rc->buffer_level = rc->bits_off_target; | 272 rc->buffer_level = rc->bits_off_target; |
| 270 | 273 |
| 271 if (cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR) { | 274 if (is_one_pass_cbr_svc(cpi)) { |
| 272 update_layer_buffer_level(&cpi->svc, encoded_frame_size); | 275 update_layer_buffer_level(&cpi->svc, encoded_frame_size); |
| 273 } | 276 } |
| 274 } | 277 } |
| 275 | 278 |
| 276 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { | 279 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { |
| 277 int i; | 280 int i; |
| 278 | 281 |
| 279 if (pass == 0 && oxcf->rc_mode == VPX_CBR) { | 282 if (pass == 0 && oxcf->rc_mode == VPX_CBR) { |
| 280 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; | 283 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; |
| 281 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; | 284 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
| (...skipping 1129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1411 if (oxcf->gf_cbr_boost_pct) { | 1414 if (oxcf->gf_cbr_boost_pct) { |
| 1412 const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100; | 1415 const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100; |
| 1413 target = cpi->refresh_golden_frame ? | 1416 target = cpi->refresh_golden_frame ? |
| 1414 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) / | 1417 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) / |
| 1415 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) : | 1418 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) : |
| 1416 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) / | 1419 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) / |
| 1417 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100); | 1420 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100); |
| 1418 } else { | 1421 } else { |
| 1419 target = rc->avg_frame_bandwidth; | 1422 target = rc->avg_frame_bandwidth; |
| 1420 } | 1423 } |
| 1421 if (svc->number_temporal_layers > 1 && | 1424 if (is_one_pass_cbr_svc(cpi)) { |
| 1422 oxcf->rc_mode == VPX_CBR) { | |
| 1423 // Note that for layers, avg_frame_bandwidth is the cumulative | 1425 // Note that for layers, avg_frame_bandwidth is the cumulative |
| 1424 // per-frame-bandwidth. For the target size of this frame, use the | 1426 // per-frame-bandwidth. For the target size of this frame, use the |
| 1425 // layer average frame size (i.e., non-cumulative per-frame-bw). | 1427 // layer average frame size (i.e., non-cumulative per-frame-bw). |
| 1426 int current_temporal_layer = svc->temporal_layer_id; | 1428 int layer = |
| 1427 const LAYER_CONTEXT *lc = &svc->layer_context[current_temporal_layer]; | 1429 LAYER_IDS_TO_IDX(svc->spatial_layer_id, |
| 1430 svc->temporal_layer_id, svc->number_temporal_layers); |
| 1431 const LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
| 1428 target = lc->avg_frame_size; | 1432 target = lc->avg_frame_size; |
| 1429 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); | 1433 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); |
| 1430 } | 1434 } |
| 1431 if (diff > 0) { | 1435 if (diff > 0) { |
| 1432 // Lower the target bandwidth for this frame. | 1436 // Lower the target bandwidth for this frame. |
| 1433 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct); | 1437 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct); |
| 1434 target -= (target * pct_low) / 200; | 1438 target -= (target * pct_low) / 200; |
| 1435 } else if (diff < 0) { | 1439 } else if (diff < 0) { |
| 1436 // Increase the target bandwidth for this frame. | 1440 // Increase the target bandwidth for this frame. |
| 1437 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); | 1441 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1452 int target; | 1456 int target; |
| 1453 if (cpi->common.current_video_frame == 0) { | 1457 if (cpi->common.current_video_frame == 0) { |
| 1454 target = ((rc->starting_buffer_level / 2) > INT_MAX) | 1458 target = ((rc->starting_buffer_level / 2) > INT_MAX) |
| 1455 ? INT_MAX : (int)(rc->starting_buffer_level / 2); | 1459 ? INT_MAX : (int)(rc->starting_buffer_level / 2); |
| 1456 } else { | 1460 } else { |
| 1457 int kf_boost = 32; | 1461 int kf_boost = 32; |
| 1458 double framerate = cpi->framerate; | 1462 double framerate = cpi->framerate; |
| 1459 if (svc->number_temporal_layers > 1 && | 1463 if (svc->number_temporal_layers > 1 && |
| 1460 oxcf->rc_mode == VPX_CBR) { | 1464 oxcf->rc_mode == VPX_CBR) { |
| 1461 // Use the layer framerate for temporal layers CBR mode. | 1465 // Use the layer framerate for temporal layers CBR mode. |
| 1462 const LAYER_CONTEXT *lc = &svc->layer_context[svc->temporal_layer_id]; | 1466 const int layer = LAYER_IDS_TO_IDX(svc->spatial_layer_id, |
| 1467 svc->temporal_layer_id, svc->number_temporal_layers); |
| 1468 const LAYER_CONTEXT *lc = &svc->layer_context[layer]; |
| 1463 framerate = lc->framerate; | 1469 framerate = lc->framerate; |
| 1464 } | 1470 } |
| 1465 kf_boost = MAX(kf_boost, (int)(2 * framerate - 16)); | 1471 kf_boost = MAX(kf_boost, (int)(2 * framerate - 16)); |
| 1466 if (rc->frames_since_key < framerate / 2) { | 1472 if (rc->frames_since_key < framerate / 2) { |
| 1467 kf_boost = (int)(kf_boost * rc->frames_since_key / | 1473 kf_boost = (int)(kf_boost * rc->frames_since_key / |
| 1468 (framerate / 2)); | 1474 (framerate / 2)); |
| 1469 } | 1475 } |
| 1470 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; | 1476 target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4; |
| 1471 } | 1477 } |
| 1472 return vp9_rc_clamp_iframe_target_size(cpi, target); | 1478 return vp9_rc_clamp_iframe_target_size(cpi, target); |
| 1473 } | 1479 } |
| 1474 | 1480 |
| 1481 // Reset information needed to set proper reference frames and buffer updates |
| 1482 // for temporal layering. This is called when a key frame is encoded. |
| 1483 static void reset_temporal_layer_to_zero(VP9_COMP *cpi) { |
| 1484 int sl; |
| 1485 LAYER_CONTEXT *lc = NULL; |
| 1486 cpi->svc.temporal_layer_id = 0; |
| 1487 |
| 1488 for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) { |
| 1489 lc = &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers]; |
| 1490 lc->current_video_frame_in_layer = 0; |
| 1491 lc->frames_from_key_frame = 0; |
| 1492 } |
| 1493 } |
| 1494 |
| 1475 void vp9_rc_get_svc_params(VP9_COMP *cpi) { | 1495 void vp9_rc_get_svc_params(VP9_COMP *cpi) { |
| 1476 VP9_COMMON *const cm = &cpi->common; | 1496 VP9_COMMON *const cm = &cpi->common; |
| 1477 RATE_CONTROL *const rc = &cpi->rc; | 1497 RATE_CONTROL *const rc = &cpi->rc; |
| 1478 int target = rc->avg_frame_bandwidth; | 1498 int target = rc->avg_frame_bandwidth; |
| 1499 const int layer = LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, |
| 1500 cpi->svc.temporal_layer_id, cpi->svc.number_temporal_layers); |
| 1501 |
| 1479 if ((cm->current_video_frame == 0) || | 1502 if ((cm->current_video_frame == 0) || |
| 1480 (cpi->frame_flags & FRAMEFLAGS_KEY) || | 1503 (cpi->frame_flags & FRAMEFLAGS_KEY) || |
| 1481 (cpi->oxcf.auto_key && (rc->frames_since_key % | 1504 (cpi->oxcf.auto_key && (rc->frames_since_key % |
| 1482 cpi->oxcf.key_freq == 0))) { | 1505 cpi->oxcf.key_freq == 0))) { |
| 1483 cm->frame_type = KEY_FRAME; | 1506 cm->frame_type = KEY_FRAME; |
| 1484 rc->source_alt_ref_active = 0; | 1507 rc->source_alt_ref_active = 0; |
| 1485 | 1508 |
| 1486 if (is_two_pass_svc(cpi)) { | 1509 if (is_two_pass_svc(cpi)) { |
| 1487 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame = 1; | 1510 cpi->svc.layer_context[layer].is_key_frame = 1; |
| 1488 cpi->ref_frame_flags &= | 1511 cpi->ref_frame_flags &= |
| 1489 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); | 1512 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 1490 } | 1513 } else if (is_one_pass_cbr_svc(cpi)) { |
| 1491 | 1514 cpi->svc.layer_context[layer].is_key_frame = 1; |
| 1492 if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) { | 1515 reset_temporal_layer_to_zero(cpi); |
| 1516 cpi->ref_frame_flags &= |
| 1517 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 1518 // Assumption here is that LAST_FRAME is being updated for a keyframe. |
| 1519 // Thus no change in update flags. |
| 1493 target = calc_iframe_target_size_one_pass_cbr(cpi); | 1520 target = calc_iframe_target_size_one_pass_cbr(cpi); |
| 1494 } | 1521 } |
| 1495 } else { | 1522 } else { |
| 1496 cm->frame_type = INTER_FRAME; | 1523 cm->frame_type = INTER_FRAME; |
| 1497 | |
| 1498 if (is_two_pass_svc(cpi)) { | 1524 if (is_two_pass_svc(cpi)) { |
| 1499 LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 1525 LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer]; |
| 1500 if (cpi->svc.spatial_layer_id == 0) { | 1526 if (cpi->svc.spatial_layer_id == 0) { |
| 1501 lc->is_key_frame = 0; | 1527 lc->is_key_frame = 0; |
| 1502 } else { | 1528 } else { |
| 1503 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 1529 lc->is_key_frame = |
| 1530 cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame; |
| 1504 if (lc->is_key_frame) | 1531 if (lc->is_key_frame) |
| 1505 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); | 1532 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 1506 } | 1533 } |
| 1507 cpi->ref_frame_flags &= (~VP9_ALT_FLAG); | 1534 cpi->ref_frame_flags &= (~VP9_ALT_FLAG); |
| 1508 } | 1535 } else if (is_one_pass_cbr_svc(cpi)) { |
| 1509 | 1536 LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer]; |
| 1510 if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR) { | 1537 if (cpi->svc.spatial_layer_id == 0) { |
| 1538 lc->is_key_frame = 0; |
| 1539 } else { |
| 1540 lc->is_key_frame = |
| 1541 cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame; |
| 1542 } |
| 1511 target = calc_pframe_target_size_one_pass_cbr(cpi); | 1543 target = calc_pframe_target_size_one_pass_cbr(cpi); |
| 1512 } | 1544 } |
| 1513 } | 1545 } |
| 1514 | 1546 |
| 1515 // Any update/change of global cyclic refresh parameters (amount/delta-qp) | 1547 // Any update/change of global cyclic refresh parameters (amount/delta-qp) |
| 1516 // should be done here, before the frame qp is selected. | 1548 // should be done here, before the frame qp is selected. |
| 1517 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) | 1549 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
| 1518 vp9_cyclic_refresh_update_parameters(cpi); | 1550 vp9_cyclic_refresh_update_parameters(cpi); |
| 1519 | 1551 |
| 1520 vp9_rc_set_frame_target(cpi, target); | 1552 vp9_rc_set_frame_target(cpi, target); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1717 | 1749 |
| 1718 void vp9_set_target_rate(VP9_COMP *cpi) { | 1750 void vp9_set_target_rate(VP9_COMP *cpi) { |
| 1719 RATE_CONTROL *const rc = &cpi->rc; | 1751 RATE_CONTROL *const rc = &cpi->rc; |
| 1720 int target_rate = rc->base_frame_target; | 1752 int target_rate = rc->base_frame_target; |
| 1721 | 1753 |
| 1722 // Correction to rate target based on prior over or under shoot. | 1754 // Correction to rate target based on prior over or under shoot. |
| 1723 if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ) | 1755 if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ) |
| 1724 vbr_rate_correction(cpi, &target_rate); | 1756 vbr_rate_correction(cpi, &target_rate); |
| 1725 vp9_rc_set_frame_target(cpi, target_rate); | 1757 vp9_rc_set_frame_target(cpi, target_rate); |
| 1726 } | 1758 } |
| OLD | NEW |