| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 #define MIN_KF_BOOST 300 | 55 #define MIN_KF_BOOST 300 |
| 56 | 56 |
| 57 #if CONFIG_MULTIPLE_ARF | 57 #if CONFIG_MULTIPLE_ARF |
| 58 // Set MIN_GF_INTERVAL to 1 for the full decomposition. | 58 // Set MIN_GF_INTERVAL to 1 for the full decomposition. |
| 59 #define MIN_GF_INTERVAL 2 | 59 #define MIN_GF_INTERVAL 2 |
| 60 #else | 60 #else |
| 61 #define MIN_GF_INTERVAL 4 | 61 #define MIN_GF_INTERVAL 4 |
| 62 #endif | 62 #endif |
| 63 | 63 |
| 64 |
| 64 // #define LONG_TERM_VBR_CORRECTION | 65 // #define LONG_TERM_VBR_CORRECTION |
| 65 | 66 |
| 66 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) { |
| 67 YV12_BUFFER_CONFIG temp = *a; | 68 YV12_BUFFER_CONFIG temp = *a; |
| 68 *a = *b; | 69 *a = *b; |
| 69 *b = temp; | 70 *b = temp; |
| 70 } | 71 } |
| 71 | 72 |
| 72 static int gfboost_qadjust(int qindex) { | 73 static int gfboost_qadjust(int qindex) { |
| 73 const double q = vp9_convert_qindex_to_q(qindex); | 74 const double q = vp9_convert_qindex_to_q(qindex); |
| (...skipping 1336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1410 printf("\n"); | 1411 printf("\n"); |
| 1411 printf("Weight: "); | 1412 printf("Weight: "); |
| 1412 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1413 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1413 printf("%4d ", cpi->arf_weight[i]); | 1414 printf("%4d ", cpi->arf_weight[i]); |
| 1414 } | 1415 } |
| 1415 printf("\n"); | 1416 printf("\n"); |
| 1416 #endif | 1417 #endif |
| 1417 } | 1418 } |
| 1418 #endif | 1419 #endif |
| 1419 | 1420 |
| 1421 // Calculate a section intra ratio used in setting max loop filter. |
| 1422 static void calculate_section_intra_ratio(struct twopass_rc *twopass, |
| 1423 const FIRSTPASS_STATS *start_pos, |
| 1424 int section_length) { |
| 1425 FIRSTPASS_STATS next_frame; |
| 1426 FIRSTPASS_STATS sectionstats; |
| 1427 int i; |
| 1428 |
| 1429 vp9_zero(next_frame); |
| 1430 vp9_zero(sectionstats); |
| 1431 |
| 1432 reset_fpf_position(twopass, start_pos); |
| 1433 |
| 1434 for (i = 0; i < section_length; ++i) { |
| 1435 input_stats(twopass, &next_frame); |
| 1436 accumulate_stats(§ionstats, &next_frame); |
| 1437 } |
| 1438 |
| 1439 avg_stats(§ionstats); |
| 1440 |
| 1441 twopass->section_intra_rating = |
| 1442 (int)(sectionstats.intra_error / |
| 1443 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
| 1444 |
| 1445 reset_fpf_position(twopass, start_pos); |
| 1446 } |
| 1447 |
| 1448 // Calculate the total bits to allocate in this GF/ARF group. |
| 1449 static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi, |
| 1450 double gf_group_err) { |
| 1451 const RATE_CONTROL *const rc = &cpi->rc; |
| 1452 const struct twopass_rc *const twopass = &cpi->twopass; |
| 1453 const int max_bits = frame_max_bits(rc, &cpi->oxcf); |
| 1454 int64_t total_group_bits; |
| 1455 |
| 1456 // Calculate the bits to be allocated to the group as a whole. |
| 1457 if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) { |
| 1458 total_group_bits = (int64_t)(twopass->kf_group_bits * |
| 1459 (gf_group_err / twopass->kf_group_error_left)); |
| 1460 } else { |
| 1461 total_group_bits = 0; |
| 1462 } |
| 1463 |
| 1464 // Clamp odd edge cases. |
| 1465 total_group_bits = (total_group_bits < 0) ? |
| 1466 0 : (total_group_bits > twopass->kf_group_bits) ? |
| 1467 twopass->kf_group_bits : total_group_bits; |
| 1468 |
| 1469 // Clip based on user supplied data rate variability limit. |
| 1470 if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) |
| 1471 total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval; |
| 1472 |
| 1473 return total_group_bits; |
| 1474 } |
| 1475 |
| 1476 // Calculate the number bits extra to assign to boosted frames in a group. |
| 1477 static int calculate_boost_bits(int frame_count, |
| 1478 int boost, int64_t total_group_bits) { |
| 1479 int allocation_chunks; |
| 1480 |
| 1481 // return 0 for invalid inputs (could arise e.g. through rounding errors) |
| 1482 if (!boost || (total_group_bits <= 0) || (frame_count <= 0) ) |
| 1483 return 0; |
| 1484 |
| 1485 allocation_chunks = (frame_count * 100) + boost; |
| 1486 |
| 1487 // Prevent overflow. |
| 1488 if (boost > 1023) { |
| 1489 int divisor = boost >> 10; |
| 1490 boost /= divisor; |
| 1491 allocation_chunks /= divisor; |
| 1492 } |
| 1493 |
| 1494 // Calculate the number of extra bits for use in the boosted frame or frames. |
| 1495 return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0); |
| 1496 } |
| 1497 |
| 1498 |
| 1420 // Analyse and define a gf/arf group. | 1499 // Analyse and define a gf/arf group. |
| 1421 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1500 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1422 RATE_CONTROL *const rc = &cpi->rc; | 1501 RATE_CONTROL *const rc = &cpi->rc; |
| 1423 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1502 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1424 struct twopass_rc *const twopass = &cpi->twopass; | 1503 struct twopass_rc *const twopass = &cpi->twopass; |
| 1425 FIRSTPASS_STATS next_frame = { 0 }; | 1504 FIRSTPASS_STATS next_frame; |
| 1426 const FIRSTPASS_STATS *start_pos; | 1505 const FIRSTPASS_STATS *start_pos; |
| 1427 int i; | 1506 int i; |
| 1428 double boost_score = 0.0; | 1507 double boost_score = 0.0; |
| 1429 double old_boost_score = 0.0; | 1508 double old_boost_score = 0.0; |
| 1430 double gf_group_err = 0.0; | 1509 double gf_group_err = 0.0; |
| 1431 double gf_first_frame_err = 0.0; | 1510 double gf_first_frame_err = 0.0; |
| 1432 double mod_frame_err = 0.0; | 1511 double mod_frame_err = 0.0; |
| 1433 | 1512 |
| 1434 double mv_ratio_accumulator = 0.0; | 1513 double mv_ratio_accumulator = 0.0; |
| 1435 double decay_accumulator = 1.0; | 1514 double decay_accumulator = 1.0; |
| 1436 double zero_motion_accumulator = 1.0; | 1515 double zero_motion_accumulator = 1.0; |
| 1437 | 1516 |
| 1438 double loop_decay_rate = 1.00; | 1517 double loop_decay_rate = 1.00; |
| 1439 double last_loop_decay_rate = 1.00; | 1518 double last_loop_decay_rate = 1.00; |
| 1440 | 1519 |
| 1441 double this_frame_mv_in_out = 0.0; | 1520 double this_frame_mv_in_out = 0.0; |
| 1442 double mv_in_out_accumulator = 0.0; | 1521 double mv_in_out_accumulator = 0.0; |
| 1443 double abs_mv_in_out_accumulator = 0.0; | 1522 double abs_mv_in_out_accumulator = 0.0; |
| 1444 double mv_ratio_accumulator_thresh; | 1523 double mv_ratio_accumulator_thresh; |
| 1445 // Max bits for a single frame. | |
| 1446 const int max_bits = frame_max_bits(rc, oxcf); | |
| 1447 unsigned int allow_alt_ref = oxcf->play_alternate && oxcf->lag_in_frames; | 1524 unsigned int allow_alt_ref = oxcf->play_alternate && oxcf->lag_in_frames; |
| 1448 | 1525 |
| 1449 int f_boost = 0; | 1526 int f_boost = 0; |
| 1450 int b_boost = 0; | 1527 int b_boost = 0; |
| 1451 int flash_detected; | 1528 int flash_detected; |
| 1452 int active_max_gf_interval; | 1529 int active_max_gf_interval; |
| 1453 | 1530 |
| 1531 vp9_clear_system_state(); |
| 1532 vp9_zero(next_frame); |
| 1533 |
| 1454 twopass->gf_group_bits = 0; | 1534 twopass->gf_group_bits = 0; |
| 1455 | |
| 1456 vp9_clear_system_state(); | |
| 1457 | |
| 1458 start_pos = twopass->stats_in; | 1535 start_pos = twopass->stats_in; |
| 1459 | 1536 |
| 1460 // Load stats for the current frame. | 1537 // Load stats for the current frame. |
| 1461 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1538 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1462 | 1539 |
| 1463 // Note the error of the frame at the start of the group. This will be | 1540 // Note the error of the frame at the start of the group. This will be |
| 1464 // the GF frame error if we code a normal gf. | 1541 // the GF frame error if we code a normal gf. |
| 1465 gf_first_frame_err = mod_frame_err; | 1542 gf_first_frame_err = mod_frame_err; |
| 1466 | 1543 |
| 1467 // If this is a key frame or the overlay from a previous arf then | 1544 // If this is a key frame or the overlay from a previous arf then |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1650 } | 1727 } |
| 1651 printf("\n"); | 1728 printf("\n"); |
| 1652 printf("Weight: "); | 1729 printf("Weight: "); |
| 1653 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1730 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1654 printf("%4d ", cpi->arf_weight[i]); | 1731 printf("%4d ", cpi->arf_weight[i]); |
| 1655 } | 1732 } |
| 1656 printf("\n"); | 1733 printf("\n"); |
| 1657 } | 1734 } |
| 1658 #endif | 1735 #endif |
| 1659 #endif | 1736 #endif |
| 1660 | |
| 1661 // Calculate the bits to be allocated to the group as a whole. | |
| 1662 if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) { | |
| 1663 twopass->gf_group_bits = (int64_t)(twopass->kf_group_bits * | |
| 1664 (gf_group_err / twopass->kf_group_error_left)); | |
| 1665 } else { | |
| 1666 twopass->gf_group_bits = 0; | |
| 1667 } | |
| 1668 twopass->gf_group_bits = (twopass->gf_group_bits < 0) ? | |
| 1669 0 : (twopass->gf_group_bits > twopass->kf_group_bits) ? | |
| 1670 twopass->kf_group_bits : twopass->gf_group_bits; | |
| 1671 | |
| 1672 // Clip cpi->twopass.gf_group_bits based on user supplied data rate | |
| 1673 // variability limit, cpi->oxcf.two_pass_vbrmax_section. | |
| 1674 if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) | |
| 1675 twopass->gf_group_bits = (int64_t)max_bits * rc->baseline_gf_interval; | |
| 1676 | |
| 1677 // Reset the file position. | 1737 // Reset the file position. |
| 1678 reset_fpf_position(twopass, start_pos); | 1738 reset_fpf_position(twopass, start_pos); |
| 1679 | 1739 |
| 1680 // Assign bits to the arf or gf. | 1740 // Calculate the bits to be allocated to the gf/arf group as a whole |
| 1681 for (i = 0; i <= (rc->source_alt_ref_pending && | 1741 twopass->gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); |
| 1682 cpi->common.frame_type != KEY_FRAME); ++i) { | 1742 |
| 1683 int allocation_chunks; | 1743 // Calculate the extra bits to be used for boosted frame(s) |
| 1744 { |
| 1684 int q = rc->last_q[INTER_FRAME]; | 1745 int q = rc->last_q[INTER_FRAME]; |
| 1685 int gf_bits; | |
| 1686 | |
| 1687 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; | 1746 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; |
| 1688 | 1747 |
| 1689 // Set max and minimum boost and hence minimum allocation. | 1748 // Set max and minimum boost and hence minimum allocation. |
| 1690 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); | 1749 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); |
| 1691 | 1750 |
| 1692 if (rc->source_alt_ref_pending && i == 0) | 1751 // Calculate the extra bits to be used for boosted frame(s) |
| 1693 allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost; | 1752 twopass->gf_bits = calculate_boost_bits(rc->baseline_gf_interval, |
| 1694 else | 1753 boost, twopass->gf_group_bits); |
| 1695 allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100); | |
| 1696 | 1754 |
| 1697 // Prevent overflow. | |
| 1698 if (boost > 1023) { | |
| 1699 int divisor = boost >> 10; | |
| 1700 boost /= divisor; | |
| 1701 allocation_chunks /= divisor; | |
| 1702 } | |
| 1703 | 1755 |
| 1704 // Calculate the number of bits to be spent on the gf or arf based on | 1756 // For key frames the frame target rate is set already. |
| 1705 // the boost number. | 1757 // NOTE: We dont bother to check for the special case of ARF overlay |
| 1706 gf_bits = (int)((double)boost * (twopass->gf_group_bits / | 1758 // frames here, as there is clamping code for this in the function |
| 1707 (double)allocation_chunks)); | 1759 // vp9_rc_clamp_pframe_target_size(), which applies to one and two pass |
| 1708 | 1760 // encodes. |
| 1709 // If the frame that is to be boosted is simpler than the average for | 1761 if (cpi->common.frame_type != KEY_FRAME && |
| 1710 // the gf/arf group then use an alternative calculation | 1762 !vp9_is_upper_layer_key_frame(cpi)) { |
| 1711 // based on the error score of the frame itself. | 1763 vp9_rc_set_frame_target(cpi, twopass->gf_bits); |
| 1712 if (rc->baseline_gf_interval < 1 || | |
| 1713 mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) { | |
| 1714 double alt_gf_grp_bits = (double)twopass->kf_group_bits * | |
| 1715 (mod_frame_err * (double)rc->baseline_gf_interval) / | |
| 1716 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left); | |
| 1717 | |
| 1718 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / | |
| 1719 (double)allocation_chunks)); | |
| 1720 | |
| 1721 if (gf_bits > alt_gf_bits) | |
| 1722 gf_bits = alt_gf_bits; | |
| 1723 } else { | |
| 1724 // If it is harder than other frames in the group make sure it at | |
| 1725 // least receives an allocation in keeping with its relative error | |
| 1726 // score, otherwise it may be worse off than an "un-boosted" frame. | |
| 1727 int alt_gf_bits = (int)((double)twopass->kf_group_bits * | |
| 1728 mod_frame_err / | |
| 1729 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left)); | |
| 1730 | |
| 1731 if (alt_gf_bits > gf_bits) | |
| 1732 gf_bits = alt_gf_bits; | |
| 1733 } | |
| 1734 | |
| 1735 // Don't allow a negative value for gf_bits. | |
| 1736 if (gf_bits < 0) | |
| 1737 gf_bits = 0; | |
| 1738 | |
| 1739 if (i == 0) { | |
| 1740 twopass->gf_bits = gf_bits; | |
| 1741 } | |
| 1742 if (i == 1 || | |
| 1743 (!rc->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME && | |
| 1744 !vp9_is_upper_layer_key_frame(cpi))) { | |
| 1745 // Calculate the per frame bit target for this frame. | |
| 1746 vp9_rc_set_frame_target(cpi, gf_bits); | |
| 1747 } | 1764 } |
| 1748 } | 1765 } |
| 1749 | 1766 |
| 1750 { | 1767 // Adjust KF group bits and error remaining. |
| 1751 // Adjust KF group bits and error remaining. | 1768 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1752 twopass->kf_group_error_left -= (int64_t)gf_group_err; | |
| 1753 | 1769 |
| 1754 // If this is an arf update we want to remove the score for the overlay | 1770 // If this is an arf update we want to remove the score for the overlay |
| 1755 // frame at the end which will usually be very cheap to code. | 1771 // frame at the end which will usually be very cheap to code. |
| 1756 // The overlay frame has already, in effect, been coded so we want to spread | 1772 // The overlay frame has already, in effect, been coded so we want to spread |
| 1757 // the remaining bits among the other frames. | 1773 // the remaining bits among the other frames. |
| 1758 // For normal GFs remove the score for the GF itself unless this is | 1774 // For normal GFs remove the score for the GF itself unless this is |
| 1759 // also a key frame in which case it has already been accounted for. | 1775 // also a key frame in which case it has already been accounted for. |
| 1760 if (rc->source_alt_ref_pending) { | 1776 if (rc->source_alt_ref_pending) { |
| 1761 twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err); | 1777 twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err); |
| 1762 } else if (cpi->common.frame_type != KEY_FRAME) { | 1778 } else if (cpi->common.frame_type != KEY_FRAME) { |
| 1763 twopass->gf_group_error_left = (int64_t)(gf_group_err | 1779 twopass->gf_group_error_left = (int64_t)(gf_group_err |
| 1764 - gf_first_frame_err); | 1780 - gf_first_frame_err); |
| 1765 } else { | 1781 } else { |
| 1766 twopass->gf_group_error_left = (int64_t)gf_group_err; | 1782 twopass->gf_group_error_left = (int64_t)gf_group_err; |
| 1767 } | |
| 1768 | |
| 1769 // This condition could fail if there are two kfs very close together | |
| 1770 // despite MIN_GF_INTERVAL and would cause a divide by 0 in the | |
| 1771 // calculation of alt_extra_bits. | |
| 1772 if (rc->baseline_gf_interval >= 3) { | |
| 1773 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost; | |
| 1774 | |
| 1775 if (boost >= 150) { | |
| 1776 const int pct_extra = MIN(20, (boost - 100) / 50); | |
| 1777 const int alt_extra_bits = (int)(( | |
| 1778 MAX(twopass->gf_group_bits - twopass->gf_bits, 0) * | |
| 1779 pct_extra) / 100); | |
| 1780 twopass->gf_group_bits -= alt_extra_bits; | |
| 1781 } | |
| 1782 } | |
| 1783 } | 1783 } |
| 1784 | 1784 |
| 1785 // Calculate a section intra ratio used in setting max loop filter. |
| 1785 if (cpi->common.frame_type != KEY_FRAME) { | 1786 if (cpi->common.frame_type != KEY_FRAME) { |
| 1786 FIRSTPASS_STATS sectionstats; | 1787 calculate_section_intra_ratio(twopass, start_pos, rc->baseline_gf_interval); |
| 1787 | |
| 1788 zero_stats(§ionstats); | |
| 1789 reset_fpf_position(twopass, start_pos); | |
| 1790 | |
| 1791 for (i = 0; i < rc->baseline_gf_interval; ++i) { | |
| 1792 input_stats(twopass, &next_frame); | |
| 1793 accumulate_stats(§ionstats, &next_frame); | |
| 1794 } | |
| 1795 | |
| 1796 avg_stats(§ionstats); | |
| 1797 | |
| 1798 twopass->section_intra_rating = (int) | |
| 1799 (sectionstats.intra_error / | |
| 1800 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | |
| 1801 | |
| 1802 reset_fpf_position(twopass, start_pos); | |
| 1803 } | 1788 } |
| 1804 } | 1789 } |
| 1805 | 1790 |
| 1806 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. | 1791 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. |
| 1807 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1792 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1808 struct twopass_rc *twopass = &cpi->twopass; | 1793 struct twopass_rc *twopass = &cpi->twopass; |
| 1809 // For a single frame. | 1794 // For a single frame. |
| 1810 const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf); | 1795 const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf); |
| 1811 // Calculate modified prediction error used in bit allocation. | 1796 // Calculate modified prediction error used in bit allocation. |
| 1812 const double modified_err = calculate_modified_err(cpi, this_frame); | 1797 const double modified_err = calculate_modified_err(cpi, this_frame); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2043 twopass->kf_group_bits = (int64_t)(twopass->bits_left * | 2028 twopass->kf_group_bits = (int64_t)(twopass->bits_left * |
| 2044 (kf_group_err / twopass->modified_error_left)); | 2029 (kf_group_err / twopass->modified_error_left)); |
| 2045 | 2030 |
| 2046 // Clip based on maximum per frame rate defined by the user. | 2031 // Clip based on maximum per frame rate defined by the user. |
| 2047 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; | 2032 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; |
| 2048 if (twopass->kf_group_bits > max_grp_bits) | 2033 if (twopass->kf_group_bits > max_grp_bits) |
| 2049 twopass->kf_group_bits = max_grp_bits; | 2034 twopass->kf_group_bits = max_grp_bits; |
| 2050 } else { | 2035 } else { |
| 2051 twopass->kf_group_bits = 0; | 2036 twopass->kf_group_bits = 0; |
| 2052 } | 2037 } |
| 2038 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits); |
| 2039 |
| 2053 // Reset the first pass file position. | 2040 // Reset the first pass file position. |
| 2054 reset_fpf_position(twopass, start_position); | 2041 reset_fpf_position(twopass, start_position); |
| 2055 | 2042 |
| 2056 // Determine how big to make this keyframe based on how well the subsequent | 2043 // Scan through the kf group collating various stats used to deteermine |
| 2057 // frames use inter blocks. | 2044 // how many bits to spend on it. |
| 2058 decay_accumulator = 1.0; | 2045 decay_accumulator = 1.0; |
| 2059 boost_score = 0.0; | 2046 boost_score = 0.0; |
| 2060 | |
| 2061 // Scan through the kf group collating various stats. | |
| 2062 for (i = 0; i < rc->frames_to_key; ++i) { | 2047 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2063 if (EOF == input_stats(twopass, &next_frame)) | 2048 if (EOF == input_stats(twopass, &next_frame)) |
| 2064 break; | 2049 break; |
| 2065 | 2050 |
| 2066 // Monitor for static sections. | 2051 // Monitor for static sections. |
| 2067 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 2052 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 2068 zero_motion_accumulator) { | 2053 zero_motion_accumulator) { |
| 2069 zero_motion_accumulator = (next_frame.pcnt_inter - | 2054 zero_motion_accumulator = (next_frame.pcnt_inter - |
| 2070 next_frame.pcnt_motion); | 2055 next_frame.pcnt_motion); |
| 2071 } | 2056 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 2088 const double loop_decay_rate = get_prediction_decay_rate(&cpi->common, | 2073 const double loop_decay_rate = get_prediction_decay_rate(&cpi->common, |
| 2089 &next_frame); | 2074 &next_frame); |
| 2090 decay_accumulator *= loop_decay_rate; | 2075 decay_accumulator *= loop_decay_rate; |
| 2091 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); | 2076 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); |
| 2092 } | 2077 } |
| 2093 | 2078 |
| 2094 boost_score += (decay_accumulator * r); | 2079 boost_score += (decay_accumulator * r); |
| 2095 } | 2080 } |
| 2096 } | 2081 } |
| 2097 | 2082 |
| 2098 { | 2083 // Store the zero motion percentage |
| 2099 FIRSTPASS_STATS sectionstats; | 2084 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
| 2100 | 2085 |
| 2101 zero_stats(§ionstats); | 2086 // Calculate a section intra ratio used in setting max loop filter. |
| 2102 reset_fpf_position(twopass, start_position); | 2087 calculate_section_intra_ratio(twopass, start_position, rc->frames_to_key); |
| 2103 | |
| 2104 for (i = 0; i < rc->frames_to_key; ++i) { | |
| 2105 input_stats(twopass, &next_frame); | |
| 2106 accumulate_stats(§ionstats, &next_frame); | |
| 2107 } | |
| 2108 | |
| 2109 avg_stats(§ionstats); | |
| 2110 | |
| 2111 twopass->section_intra_rating = (int) (sectionstats.intra_error / | |
| 2112 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | |
| 2113 } | |
| 2114 | |
| 2115 // Reset the first pass file position. | |
| 2116 reset_fpf_position(twopass, start_position); | |
| 2117 | 2088 |
| 2118 // Work out how many bits to allocate for the key frame itself. | 2089 // Work out how many bits to allocate for the key frame itself. |
| 2119 if (1) { | 2090 rc->kf_boost = (int)boost_score; |
| 2120 int kf_boost = (int)boost_score; | |
| 2121 int allocation_chunks; | |
| 2122 | 2091 |
| 2123 if (kf_boost < (rc->frames_to_key * 3)) | 2092 if (rc->kf_boost < (rc->frames_to_key * 3)) |
| 2124 kf_boost = (rc->frames_to_key * 3); | 2093 rc->kf_boost = (rc->frames_to_key * 3); |
| 2094 if (rc->kf_boost < MIN_KF_BOOST) |
| 2095 rc->kf_boost = MIN_KF_BOOST; |
| 2125 | 2096 |
| 2126 if (kf_boost < MIN_KF_BOOST) | 2097 twopass->kf_bits = calculate_boost_bits((rc->frames_to_key - 1), |
| 2127 kf_boost = MIN_KF_BOOST; | 2098 rc->kf_boost, twopass->kf_group_bits); |
| 2128 | 2099 |
| 2129 // Make a note of baseline boost and the zero motion | 2100 twopass->kf_group_bits -= twopass->kf_bits; |
| 2130 // accumulator value for use elsewhere. | |
| 2131 rc->kf_boost = kf_boost; | |
| 2132 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); | |
| 2133 | 2101 |
| 2134 // Key frame size depends on: | 2102 // Per frame bit target for this frame. |
| 2135 // (1) the error score for the whole key frame group, | 2103 vp9_rc_set_frame_target(cpi, twopass->kf_bits); |
| 2136 // (2) the key frames' own error if this is smaller than the | |
| 2137 // average for the group (optional), | |
| 2138 // (3) insuring that the frame receives at least the allocation it would | |
| 2139 // have received based on its own error score vs the error score | |
| 2140 // remaining. | |
| 2141 // Special case: | |
| 2142 // If the sequence appears almost totally static we want to spend almost | |
| 2143 // all of the bits on the key frame. | |
| 2144 // | |
| 2145 // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is | |
| 2146 // taken care of by kf_boost. | |
| 2147 if (zero_motion_accumulator >= 0.99) { | |
| 2148 allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost; | |
| 2149 } else { | |
| 2150 allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost; | |
| 2151 } | |
| 2152 | |
| 2153 // Prevent overflow. | |
| 2154 if (kf_boost > 1028) { | |
| 2155 const int divisor = kf_boost >> 10; | |
| 2156 kf_boost /= divisor; | |
| 2157 allocation_chunks /= divisor; | |
| 2158 } | |
| 2159 | |
| 2160 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits); | |
| 2161 // Calculate the number of bits to be spent on the key frame. | |
| 2162 twopass->kf_bits = (int)((double)kf_boost * | |
| 2163 ((double)twopass->kf_group_bits / allocation_chunks)); | |
| 2164 | |
| 2165 // If the key frame is actually easier than the average for the | |
| 2166 // kf group (which does sometimes happen, e.g. a blank intro frame) | |
| 2167 // then use an alternate calculation based on the kf error score | |
| 2168 // which should give a smaller key frame. | |
| 2169 if (kf_mod_err < kf_group_err / rc->frames_to_key) { | |
| 2170 double alt_kf_grp_bits = ((double)twopass->bits_left * | |
| 2171 (kf_mod_err * (double)rc->frames_to_key) / | |
| 2172 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)); | |
| 2173 | |
| 2174 const int alt_kf_bits = (int)((double)kf_boost * | |
| 2175 (alt_kf_grp_bits / (double)allocation_chunks)); | |
| 2176 | |
| 2177 if (twopass->kf_bits > alt_kf_bits) | |
| 2178 twopass->kf_bits = alt_kf_bits; | |
| 2179 } else { | |
| 2180 // Else if it is much harder than other frames in the group make sure | |
| 2181 // it at least receives an allocation in keeping with its relative | |
| 2182 // error score. | |
| 2183 const int alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err / | |
| 2184 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left))); | |
| 2185 | |
| 2186 if (alt_kf_bits > twopass->kf_bits) | |
| 2187 twopass->kf_bits = alt_kf_bits; | |
| 2188 } | |
| 2189 twopass->kf_group_bits -= twopass->kf_bits; | |
| 2190 // Per frame bit target for this frame. | |
| 2191 vp9_rc_set_frame_target(cpi, twopass->kf_bits); | |
| 2192 } | |
| 2193 | 2104 |
| 2194 // Note the total error score of the kf group minus the key frame itself. | 2105 // Note the total error score of the kf group minus the key frame itself. |
| 2195 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2106 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2196 | 2107 |
| 2197 // Adjust the count of total modified error left. | 2108 // Adjust the count of total modified error left. |
| 2198 // The count of bits left is adjusted elsewhere based on real coded frame | 2109 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2199 // sizes. | 2110 // sizes. |
| 2200 twopass->modified_error_left -= kf_group_err; | 2111 twopass->modified_error_left -= kf_group_err; |
| 2201 } | 2112 } |
| 2202 | 2113 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2235 RATE_CONTROL *const rc = &cpi->rc; | 2146 RATE_CONTROL *const rc = &cpi->rc; |
| 2236 struct twopass_rc *const twopass = &cpi->twopass; | 2147 struct twopass_rc *const twopass = &cpi->twopass; |
| 2237 int frames_left; | 2148 int frames_left; |
| 2238 FIRSTPASS_STATS this_frame; | 2149 FIRSTPASS_STATS this_frame; |
| 2239 FIRSTPASS_STATS this_frame_copy; | 2150 FIRSTPASS_STATS this_frame_copy; |
| 2240 | 2151 |
| 2241 double this_frame_intra_error; | 2152 double this_frame_intra_error; |
| 2242 double this_frame_coded_error; | 2153 double this_frame_coded_error; |
| 2243 int target; | 2154 int target; |
| 2244 LAYER_CONTEXT *lc = NULL; | 2155 LAYER_CONTEXT *lc = NULL; |
| 2245 int is_spatial_svc = (cpi->use_svc && cpi->svc.number_temporal_layers == 1); | 2156 const int is_spatial_svc = (cpi->use_svc && |
| 2246 | 2157 cpi->svc.number_temporal_layers == 1); |
| 2247 if (is_spatial_svc) { | 2158 if (is_spatial_svc) { |
| 2248 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 2159 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 2249 frames_left = (int)(twopass->total_stats.count - | 2160 frames_left = (int)(twopass->total_stats.count - |
| 2250 lc->current_video_frame_in_layer); | 2161 lc->current_video_frame_in_layer); |
| 2251 } else { | 2162 } else { |
| 2252 frames_left = (int)(twopass->total_stats.count - | 2163 frames_left = (int)(twopass->total_stats.count - |
| 2253 cm->current_video_frame); | 2164 cm->current_video_frame); |
| 2254 } | 2165 } |
| 2255 | 2166 |
| 2256 if (!twopass->stats_in) | 2167 if (!twopass->stats_in) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2296 this_frame_intra_error = this_frame.intra_error; | 2207 this_frame_intra_error = this_frame.intra_error; |
| 2297 this_frame_coded_error = this_frame.coded_error; | 2208 this_frame_coded_error = this_frame.coded_error; |
| 2298 | 2209 |
| 2299 // Keyframe and section processing. | 2210 // Keyframe and section processing. |
| 2300 if (rc->frames_to_key == 0 || | 2211 if (rc->frames_to_key == 0 || |
| 2301 (cpi->frame_flags & FRAMEFLAGS_KEY)) { | 2212 (cpi->frame_flags & FRAMEFLAGS_KEY)) { |
| 2302 // Define next KF group and assign bits to it. | 2213 // Define next KF group and assign bits to it. |
| 2303 this_frame_copy = this_frame; | 2214 this_frame_copy = this_frame; |
| 2304 find_next_key_frame(cpi, &this_frame_copy); | 2215 find_next_key_frame(cpi, &this_frame_copy); |
| 2305 // Don't place key frame in any enhancement layers in spatial svc | 2216 // Don't place key frame in any enhancement layers in spatial svc |
| 2306 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 2217 if (is_spatial_svc) { |
| 2307 lc->is_key_frame = 1; | 2218 lc->is_key_frame = 1; |
| 2308 if (cpi->svc.spatial_layer_id > 0) { | 2219 if (cpi->svc.spatial_layer_id > 0) { |
| 2309 cm->frame_type = INTER_FRAME; | 2220 cm->frame_type = INTER_FRAME; |
| 2310 } | 2221 } |
| 2311 } | 2222 } |
| 2312 } else { | 2223 } else { |
| 2313 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 2224 if (is_spatial_svc) { |
| 2314 lc->is_key_frame = 0; | 2225 lc->is_key_frame = 0; |
| 2315 } | 2226 } |
| 2316 cm->frame_type = INTER_FRAME; | 2227 cm->frame_type = INTER_FRAME; |
| 2317 } | 2228 } |
| 2318 | 2229 |
| 2319 // Is this frame a GF / ARF? (Note: a key frame is always also a GF). | 2230 // Is this frame a GF / ARF? (Note: a key frame is always also a GF). |
| 2320 if (rc->frames_till_gf_update_due == 0) { | 2231 if (rc->frames_till_gf_update_due == 0) { |
| 2321 // Define next gf group and assign bits to it. | 2232 // Define next gf group and assign bits to it. |
| 2322 this_frame_copy = this_frame; | 2233 this_frame_copy = this_frame; |
| 2323 | 2234 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2419 // So now update to the correct value based on the actual bits used. | 2330 // So now update to the correct value based on the actual bits used. |
| 2420 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - bits_used; | 2331 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - bits_used; |
| 2421 } else { | 2332 } else { |
| 2422 #endif | 2333 #endif |
| 2423 cpi->twopass.kf_group_bits -= bits_used; | 2334 cpi->twopass.kf_group_bits -= bits_used; |
| 2424 cpi->twopass.gf_group_bits -= bits_used; | 2335 cpi->twopass.gf_group_bits -= bits_used; |
| 2425 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); | 2336 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); |
| 2426 } | 2337 } |
| 2427 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); | 2338 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); |
| 2428 } | 2339 } |
| OLD | NEW |