| 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 | |
| 43 #define BOOST_BREAKOUT 12.5 | 41 #define BOOST_BREAKOUT 12.5 |
| 44 #define BOOST_FACTOR 12.5 | 42 #define BOOST_FACTOR 12.5 |
| 45 #define ERR_DIVISOR 128.0 | 43 #define ERR_DIVISOR 128.0 |
| 46 #define FACTOR_PT_LOW 0.70 | 44 #define FACTOR_PT_LOW 0.70 |
| 47 #define FACTOR_PT_HIGH 0.90 | 45 #define FACTOR_PT_HIGH 0.90 |
| 48 #define FIRST_PASS_Q 10.0 | 46 #define FIRST_PASS_Q 10.0 |
| 49 #define GF_MAX_BOOST 96.0 | 47 #define GF_MAX_BOOST 96.0 |
| 50 #define INTRA_MODE_PENALTY 1024 | 48 #define INTRA_MODE_PENALTY 1024 |
| 51 #define KF_MAX_BOOST 128.0 | 49 #define KF_MAX_BOOST 128.0 |
| 52 #define MIN_ARF_GF_BOOST 240 | 50 #define MIN_ARF_GF_BOOST 240 |
| 53 #define MIN_DECAY_FACTOR 0.01 | 51 #define MIN_DECAY_FACTOR 0.01 |
| 54 #define MIN_GF_INTERVAL 4 | 52 #define MIN_GF_INTERVAL 4 |
| 55 #define MIN_KF_BOOST 300 | 53 #define MIN_KF_BOOST 300 |
| 56 #define NEW_MV_MODE_PENALTY 32 | 54 #define NEW_MV_MODE_PENALTY 32 |
| 57 #define SVC_FACTOR_PT_LOW 0.45 | 55 #define SVC_FACTOR_PT_LOW 0.45 |
| 58 #define DARK_THRESH 64 | 56 #define DARK_THRESH 64 |
| 59 #define DEFAULT_GRP_WEIGHT 1.0 | |
| 60 | 57 |
| 61 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 58 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 62 | 59 |
| 63 #if ARF_STATS_OUTPUT | 60 #if ARF_STATS_OUTPUT |
| 64 unsigned int arf_count = 0; | 61 unsigned int arf_count = 0; |
| 65 #endif | 62 #endif |
| 66 | 63 |
| 67 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | 64 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { |
| 68 YV12_BUFFER_CONFIG temp = *a; | 65 YV12_BUFFER_CONFIG temp = *a; |
| 69 *a = *b; | 66 *a = *b; |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 vp9_scale_references(cpi); | 525 vp9_scale_references(cpi); |
| 529 | 526 |
| 530 // Use either last frame or alt frame for motion search. | 527 // Use either last frame or alt frame for motion search. |
| 531 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { | 528 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { |
| 532 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); | 529 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); |
| 533 if (first_ref_buf == NULL) | 530 if (first_ref_buf == NULL) |
| 534 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); | 531 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 535 } | 532 } |
| 536 | 533 |
| 537 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { | 534 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { |
| 538 BufferPool *const pool = cm->buffer_pool; | |
| 539 const int ref_idx = | 535 const int ref_idx = |
| 540 cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)]; | 536 cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)]; |
| 541 const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1]; | 537 const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1]; |
| 542 | 538 |
| 543 gld_yv12 = (scaled_idx != ref_idx) ? &pool->frame_bufs[scaled_idx].buf : | 539 gld_yv12 = (scaled_idx != ref_idx) ? &cm->frame_bufs[scaled_idx].buf : |
| 544 get_ref_frame_buffer(cpi, GOLDEN_FRAME); | 540 get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 545 } else { | 541 } else { |
| 546 gld_yv12 = NULL; | 542 gld_yv12 = NULL; |
| 547 } | 543 } |
| 548 | 544 |
| 549 recon_y_stride = new_yv12->y_stride; | 545 recon_y_stride = new_yv12->y_stride; |
| 550 recon_uv_stride = new_yv12->uv_stride; | 546 recon_uv_stride = new_yv12->uv_stride; |
| 551 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); | 547 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); |
| 552 | 548 |
| 553 set_ref_ptrs(cm, xd, | 549 set_ref_ptrs(cm, xd, |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1079 | 1075 |
| 1080 return fclamp(pow(error_term, power_term), 0.05, 5.0); | 1076 return fclamp(pow(error_term, power_term), 0.05, 5.0); |
| 1081 } | 1077 } |
| 1082 | 1078 |
| 1083 // Larger image formats are expected to be a little harder to code relatively | 1079 // Larger image formats are expected to be a little harder to code relatively |
| 1084 // given the same prediction error score. This in part at least relates to the | 1080 // given the same prediction error score. This in part at least relates to the |
| 1085 // increased size and hence coding cost of motion vectors. | 1081 // increased size and hence coding cost of motion vectors. |
| 1086 #define EDIV_SIZE_FACTOR 800 | 1082 #define EDIV_SIZE_FACTOR 800 |
| 1087 | 1083 |
| 1088 static int get_twopass_worst_quality(const VP9_COMP *cpi, | 1084 static int get_twopass_worst_quality(const VP9_COMP *cpi, |
| 1089 const double section_err, | 1085 const FIRSTPASS_STATS *stats, |
| 1090 int section_target_bandwidth, | 1086 int section_target_bandwidth) { |
| 1091 double group_weight_factor) { | |
| 1092 const RATE_CONTROL *const rc = &cpi->rc; | 1087 const RATE_CONTROL *const rc = &cpi->rc; |
| 1093 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1088 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1094 | 1089 |
| 1095 if (section_target_bandwidth <= 0) { | 1090 if (section_target_bandwidth <= 0) { |
| 1096 return rc->worst_quality; // Highest value allowed | 1091 return rc->worst_quality; // Highest value allowed |
| 1097 } else { | 1092 } else { |
| 1098 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) | 1093 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) |
| 1099 ? cpi->initial_mbs : cpi->common.MBs; | 1094 ? cpi->initial_mbs : cpi->common.MBs; |
| 1095 const double section_err = stats->coded_error / stats->count; |
| 1100 const double err_per_mb = section_err / num_mbs; | 1096 const double err_per_mb = section_err / num_mbs; |
| 1101 const double speed_term = 1.0 + 0.04 * oxcf->speed; | 1097 const double speed_term = 1.0 + 0.04 * oxcf->speed; |
| 1102 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR; | 1098 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR; |
| 1103 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << | 1099 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << |
| 1104 BPER_MB_NORMBITS) / num_mbs; | 1100 BPER_MB_NORMBITS) / num_mbs; |
| 1105 | 1101 |
| 1106 int q; | 1102 int q; |
| 1107 int is_svc_upper_layer = 0; | 1103 int is_svc_upper_layer = 0; |
| 1108 | |
| 1109 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) | 1104 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) |
| 1110 is_svc_upper_layer = 1; | 1105 is_svc_upper_layer = 1; |
| 1111 | 1106 |
| 1112 | |
| 1113 // Try and pick a max Q that will be high enough to encode the | 1107 // Try and pick a max Q that will be high enough to encode the |
| 1114 // content at the given rate. | 1108 // content at the given rate. |
| 1115 for (q = rc->best_quality; q < rc->worst_quality; ++q) { | 1109 for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
| 1116 const double factor = | 1110 const double factor = |
| 1117 calc_correction_factor(err_per_mb, | 1111 calc_correction_factor(err_per_mb, |
| 1118 ERR_DIVISOR - ediv_size_correction, | 1112 ERR_DIVISOR - ediv_size_correction, |
| 1119 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : | 1113 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : |
| 1120 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, | 1114 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, |
| 1121 cpi->common.bit_depth); | 1115 cpi->common.bit_depth); |
| 1122 const int bits_per_mb = | 1116 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 1123 vp9_rc_bits_per_mb(INTER_FRAME, q, | 1117 factor * speed_term, |
| 1124 factor * speed_term * group_weight_factor, | 1118 cpi->common.bit_depth); |
| 1125 cpi->common.bit_depth); | |
| 1126 if (bits_per_mb <= target_norm_bits_per_mb) | 1119 if (bits_per_mb <= target_norm_bits_per_mb) |
| 1127 break; | 1120 break; |
| 1128 } | 1121 } |
| 1129 | 1122 |
| 1130 // Restriction on active max q for constrained quality mode. | 1123 // Restriction on active max q for constrained quality mode. |
| 1131 if (cpi->oxcf.rc_mode == VPX_CQ) | 1124 if (cpi->oxcf.rc_mode == VPX_CQ) |
| 1132 q = MAX(q, oxcf->cq_level); | 1125 q = MAX(q, oxcf->cq_level); |
| 1133 return q; | 1126 return q; |
| 1134 } | 1127 } |
| 1135 } | 1128 } |
| (...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1699 RATE_CONTROL *const rc = &cpi->rc; | 1692 RATE_CONTROL *const rc = &cpi->rc; |
| 1700 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1693 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1701 TWO_PASS *const twopass = &cpi->twopass; | 1694 TWO_PASS *const twopass = &cpi->twopass; |
| 1702 FIRSTPASS_STATS next_frame; | 1695 FIRSTPASS_STATS next_frame; |
| 1703 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; | 1696 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; |
| 1704 int i; | 1697 int i; |
| 1705 | 1698 |
| 1706 double boost_score = 0.0; | 1699 double boost_score = 0.0; |
| 1707 double old_boost_score = 0.0; | 1700 double old_boost_score = 0.0; |
| 1708 double gf_group_err = 0.0; | 1701 double gf_group_err = 0.0; |
| 1709 #if GROUP_ADAPTIVE_MAXQ | |
| 1710 double gf_group_raw_error = 0.0; | |
| 1711 #endif | |
| 1712 double gf_first_frame_err = 0.0; | 1702 double gf_first_frame_err = 0.0; |
| 1713 double mod_frame_err = 0.0; | 1703 double mod_frame_err = 0.0; |
| 1714 | 1704 |
| 1715 double mv_ratio_accumulator = 0.0; | 1705 double mv_ratio_accumulator = 0.0; |
| 1716 double decay_accumulator = 1.0; | 1706 double decay_accumulator = 1.0; |
| 1717 double zero_motion_accumulator = 1.0; | 1707 double zero_motion_accumulator = 1.0; |
| 1718 | 1708 |
| 1719 double loop_decay_rate = 1.00; | 1709 double loop_decay_rate = 1.00; |
| 1720 double last_loop_decay_rate = 1.00; | 1710 double last_loop_decay_rate = 1.00; |
| 1721 | 1711 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1745 | 1735 |
| 1746 // Load stats for the current frame. | 1736 // Load stats for the current frame. |
| 1747 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); | 1737 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1748 | 1738 |
| 1749 // Note the error of the frame at the start of the group. This will be | 1739 // Note the error of the frame at the start of the group. This will be |
| 1750 // the GF frame error if we code a normal gf. | 1740 // the GF frame error if we code a normal gf. |
| 1751 gf_first_frame_err = mod_frame_err; | 1741 gf_first_frame_err = mod_frame_err; |
| 1752 | 1742 |
| 1753 // If this is a key frame or the overlay from a previous arf then | 1743 // If this is a key frame or the overlay from a previous arf then |
| 1754 // the error score / cost of this frame has already been accounted for. | 1744 // the error score / cost of this frame has already been accounted for. |
| 1755 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) { | 1745 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) |
| 1756 gf_group_err -= gf_first_frame_err; | 1746 gf_group_err -= gf_first_frame_err; |
| 1757 #if GROUP_ADAPTIVE_MAXQ | |
| 1758 gf_group_raw_error -= this_frame->coded_error; | |
| 1759 #endif | |
| 1760 } | |
| 1761 | 1747 |
| 1762 // Motion breakout threshold for loop below depends on image size. | 1748 // Motion breakout threshold for loop below depends on image size. |
| 1763 mv_ratio_accumulator_thresh = | 1749 mv_ratio_accumulator_thresh = |
| 1764 (cpi->initial_height + cpi->initial_width) / 4.0; | 1750 (cpi->initial_height + cpi->initial_width) / 4.0; |
| 1765 | 1751 |
| 1766 // Set a maximum and minimum interval for the GF group. | 1752 // Set a maximum and minimum interval for the GF group. |
| 1767 // If the image appears almost completely static we can extend beyond this. | 1753 // If the image appears almost completely static we can extend beyond this. |
| 1768 { | 1754 { |
| 1769 int int_max_q = | 1755 int int_max_q = |
| 1770 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, | 1756 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1789 } | 1775 } |
| 1790 } | 1776 } |
| 1791 | 1777 |
| 1792 i = 0; | 1778 i = 0; |
| 1793 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { | 1779 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { |
| 1794 ++i; | 1780 ++i; |
| 1795 | 1781 |
| 1796 // Accumulate error score of frames in this gf group. | 1782 // Accumulate error score of frames in this gf group. |
| 1797 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); | 1783 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
| 1798 gf_group_err += mod_frame_err; | 1784 gf_group_err += mod_frame_err; |
| 1799 #if GROUP_ADAPTIVE_MAXQ | |
| 1800 gf_group_raw_error += this_frame->coded_error; | |
| 1801 #endif | |
| 1802 | 1785 |
| 1803 if (EOF == input_stats(twopass, &next_frame)) | 1786 if (EOF == input_stats(twopass, &next_frame)) |
| 1804 break; | 1787 break; |
| 1805 | 1788 |
| 1806 // Test for the case where there is a brief flash but the prediction | 1789 // Test for the case where there is a brief flash but the prediction |
| 1807 // quality back to an earlier frame is then restored. | 1790 // quality back to an earlier frame is then restored. |
| 1808 flash_detected = detect_flash(twopass, 0); | 1791 flash_detected = detect_flash(twopass, 0); |
| 1809 | 1792 |
| 1810 // Update the motion related elements to the boost calculation. | 1793 // Update the motion related elements to the boost calculation. |
| 1811 accumulate_frame_motion_stats(&next_frame, | 1794 accumulate_frame_motion_stats(&next_frame, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1853 boost_score = old_boost_score; | 1836 boost_score = old_boost_score; |
| 1854 break; | 1837 break; |
| 1855 } | 1838 } |
| 1856 | 1839 |
| 1857 *this_frame = next_frame; | 1840 *this_frame = next_frame; |
| 1858 old_boost_score = boost_score; | 1841 old_boost_score = boost_score; |
| 1859 } | 1842 } |
| 1860 | 1843 |
| 1861 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); | 1844 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); |
| 1862 | 1845 |
| 1863 // Was the group length constrained by the requirement for a new KF? | |
| 1864 rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0; | |
| 1865 | |
| 1866 // Set the interval until the next gf. | 1846 // Set the interval until the next gf. |
| 1867 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) | 1847 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) |
| 1868 rc->baseline_gf_interval = i - 1; | 1848 rc->baseline_gf_interval = i - 1; |
| 1869 else | 1849 else |
| 1870 rc->baseline_gf_interval = i; | 1850 rc->baseline_gf_interval = i; |
| 1871 | 1851 |
| 1872 // Only encode alt reference frame in temporal base layer. So | 1852 // Only encode alt reference frame in temporal base layer. So |
| 1873 // baseline_gf_interval should be multiple of a temporal layer group | 1853 // baseline_gf_interval should be multiple of a temporal layer group |
| 1874 // (typically the frame distance between two base layer frames) | 1854 // (typically the frame distance between two base layer frames) |
| 1875 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { | 1855 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { |
| 1876 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; | 1856 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; |
| 1877 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count); | 1857 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count); |
| 1878 int j; | 1858 int j; |
| 1879 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) { | 1859 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) { |
| 1880 if (EOF == input_stats(twopass, this_frame)) | 1860 if (EOF == input_stats(twopass, this_frame)) |
| 1881 break; | 1861 break; |
| 1882 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame); | 1862 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame); |
| 1883 #if GROUP_ADAPTIVE_MAXQ | |
| 1884 gf_group_raw_error += this_frame->coded_error; | |
| 1885 #endif | |
| 1886 } | 1863 } |
| 1887 rc->baseline_gf_interval = new_gf_interval; | 1864 rc->baseline_gf_interval = new_gf_interval; |
| 1888 } | 1865 } |
| 1889 | 1866 |
| 1890 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 1867 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 1891 | 1868 |
| 1892 // Should we use the alternate reference frame. | 1869 // Should we use the alternate reference frame. |
| 1893 if (allow_alt_ref && | 1870 if (allow_alt_ref && |
| 1894 (i < cpi->oxcf.lag_in_frames) && | 1871 (i < cpi->oxcf.lag_in_frames) && |
| 1895 (i >= MIN_GF_INTERVAL)) { | 1872 (i >= MIN_GF_INTERVAL)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1906 rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST); | 1883 rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST); |
| 1907 rc->source_alt_ref_pending = 0; | 1884 rc->source_alt_ref_pending = 0; |
| 1908 } | 1885 } |
| 1909 | 1886 |
| 1910 // Reset the file position. | 1887 // Reset the file position. |
| 1911 reset_fpf_position(twopass, start_pos); | 1888 reset_fpf_position(twopass, start_pos); |
| 1912 | 1889 |
| 1913 // Calculate the bits to be allocated to the gf/arf group as a whole | 1890 // Calculate the bits to be allocated to the gf/arf group as a whole |
| 1914 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); | 1891 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); |
| 1915 | 1892 |
| 1916 #if GROUP_ADAPTIVE_MAXQ | |
| 1917 // Calculate an estimate of the maxq needed for the group. | |
| 1918 // We are more agressive about correcting for sections | |
| 1919 // where there could be significant overshoot than for easier | |
| 1920 // sections where we do not wish to risk creating an overshoot | |
| 1921 // of the allocated bit budget. | |
| 1922 if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) { | |
| 1923 const int vbr_group_bits_per_frame = | |
| 1924 (int)(gf_group_bits / rc->baseline_gf_interval); | |
| 1925 const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval; | |
| 1926 const int tmp_q = | |
| 1927 get_twopass_worst_quality(cpi, group_av_err, vbr_group_bits_per_frame, | |
| 1928 twopass->kfgroup_inter_fraction); | |
| 1929 | |
| 1930 if (tmp_q < twopass->baseline_worst_quality) { | |
| 1931 twopass->active_worst_quality = | |
| 1932 (tmp_q + twopass->baseline_worst_quality + 1) / 2; | |
| 1933 } else { | |
| 1934 twopass->active_worst_quality = tmp_q; | |
| 1935 } | |
| 1936 } | |
| 1937 #endif | |
| 1938 | |
| 1939 // Calculate the extra bits to be used for boosted frame(s) | 1893 // Calculate the extra bits to be used for boosted frame(s) |
| 1940 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, | 1894 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, |
| 1941 rc->gfu_boost, gf_group_bits); | 1895 rc->gfu_boost, gf_group_bits); |
| 1942 | 1896 |
| 1943 // Adjust KF group bits and error remaining. | 1897 // Adjust KF group bits and error remaining. |
| 1944 twopass->kf_group_error_left -= (int64_t)gf_group_err; | 1898 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1945 | 1899 |
| 1946 // If this is an arf update we want to remove the score for the overlay | 1900 // If this is an arf update we want to remove the score for the overlay |
| 1947 // frame at the end which will usually be very cheap to code. | 1901 // frame at the end which will usually be very cheap to code. |
| 1948 // The overlay frame has already, in effect, been coded so we want to spread | 1902 // The overlay frame has already, in effect, been coded so we want to spread |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2152 // is between 1x and 2x. | 2106 // is between 1x and 2x. |
| 2153 if (cpi->oxcf.auto_key && | 2107 if (cpi->oxcf.auto_key && |
| 2154 rc->frames_to_key > cpi->oxcf.key_freq) { | 2108 rc->frames_to_key > cpi->oxcf.key_freq) { |
| 2155 FIRSTPASS_STATS tmp_frame = first_frame; | 2109 FIRSTPASS_STATS tmp_frame = first_frame; |
| 2156 | 2110 |
| 2157 rc->frames_to_key /= 2; | 2111 rc->frames_to_key /= 2; |
| 2158 | 2112 |
| 2159 // Reset to the start of the group. | 2113 // Reset to the start of the group. |
| 2160 reset_fpf_position(twopass, start_position); | 2114 reset_fpf_position(twopass, start_position); |
| 2161 | 2115 |
| 2162 kf_group_err = 0.0; | 2116 kf_group_err = 0; |
| 2163 | 2117 |
| 2164 // Rescan to get the correct error data for the forced kf group. | 2118 // Rescan to get the correct error data for the forced kf group. |
| 2165 for (i = 0; i < rc->frames_to_key; ++i) { | 2119 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2166 kf_group_err += calculate_modified_err(twopass, oxcf, &tmp_frame); | 2120 kf_group_err += calculate_modified_err(twopass, oxcf, &tmp_frame); |
| 2167 input_stats(twopass, &tmp_frame); | 2121 input_stats(twopass, &tmp_frame); |
| 2168 } | 2122 } |
| 2169 rc->next_key_frame_forced = 1; | 2123 rc->next_key_frame_forced = 1; |
| 2170 } else if (twopass->stats_in == twopass->stats_in_end || | 2124 } else if (twopass->stats_in == twopass->stats_in_end || |
| 2171 rc->frames_to_key >= cpi->oxcf.key_freq) { | 2125 rc->frames_to_key >= cpi->oxcf.key_freq) { |
| 2172 rc->next_key_frame_forced = 1; | 2126 rc->next_key_frame_forced = 1; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2262 | 2216 |
| 2263 // Apply various clamps for min and max boost | 2217 // Apply various clamps for min and max boost |
| 2264 rc->kf_boost = (int)(av_decay_accumulator * boost_score); | 2218 rc->kf_boost = (int)(av_decay_accumulator * boost_score); |
| 2265 rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3)); | 2219 rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3)); |
| 2266 rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST); | 2220 rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST); |
| 2267 | 2221 |
| 2268 // Work out how many bits to allocate for the key frame itself. | 2222 // Work out how many bits to allocate for the key frame itself. |
| 2269 kf_bits = calculate_boost_bits((rc->frames_to_key - 1), | 2223 kf_bits = calculate_boost_bits((rc->frames_to_key - 1), |
| 2270 rc->kf_boost, twopass->kf_group_bits); | 2224 rc->kf_boost, twopass->kf_group_bits); |
| 2271 | 2225 |
| 2272 // Work out the fraction of the kf group bits reserved for the inter frames | |
| 2273 // within the group after discounting the bits for the kf itself. | |
| 2274 if (twopass->kf_group_bits) { | |
| 2275 twopass->kfgroup_inter_fraction = | |
| 2276 (double)(twopass->kf_group_bits - kf_bits) / | |
| 2277 (double)twopass->kf_group_bits; | |
| 2278 } else { | |
| 2279 twopass->kfgroup_inter_fraction = 1.0; | |
| 2280 } | |
| 2281 | |
| 2282 twopass->kf_group_bits -= kf_bits; | 2226 twopass->kf_group_bits -= kf_bits; |
| 2283 | 2227 |
| 2284 // Save the bits to spend on the key frame. | 2228 // Save the bits to spend on the key frame. |
| 2285 gf_group->bit_allocation[0] = kf_bits; | 2229 gf_group->bit_allocation[0] = kf_bits; |
| 2286 gf_group->update_type[0] = KF_UPDATE; | 2230 gf_group->update_type[0] = KF_UPDATE; |
| 2287 gf_group->rf_level[0] = KF_STD; | 2231 gf_group->rf_level[0] = KF_STD; |
| 2288 | 2232 |
| 2289 // Note the total error score of the kf group minus the key frame itself. | 2233 // Note the total error score of the kf group minus the key frame itself. |
| 2290 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2234 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2291 | 2235 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2362 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1); | 2306 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1); |
| 2363 } | 2307 } |
| 2364 | 2308 |
| 2365 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { | 2309 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2366 VP9_COMMON *const cm = &cpi->common; | 2310 VP9_COMMON *const cm = &cpi->common; |
| 2367 RATE_CONTROL *const rc = &cpi->rc; | 2311 RATE_CONTROL *const rc = &cpi->rc; |
| 2368 TWO_PASS *const twopass = &cpi->twopass; | 2312 TWO_PASS *const twopass = &cpi->twopass; |
| 2369 GF_GROUP *const gf_group = &twopass->gf_group; | 2313 GF_GROUP *const gf_group = &twopass->gf_group; |
| 2370 int frames_left; | 2314 int frames_left; |
| 2371 FIRSTPASS_STATS this_frame; | 2315 FIRSTPASS_STATS this_frame; |
| 2316 FIRSTPASS_STATS this_frame_copy; |
| 2372 | 2317 |
| 2373 int target_rate; | 2318 int target_rate; |
| 2374 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? | 2319 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? |
| 2375 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0; | 2320 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0; |
| 2376 | 2321 |
| 2377 if (lc != NULL) { | 2322 if (lc != NULL) { |
| 2378 frames_left = (int)(twopass->total_stats.count - | 2323 frames_left = (int)(twopass->total_stats.count - |
| 2379 lc->current_video_frame_in_layer); | 2324 lc->current_video_frame_in_layer); |
| 2380 } else { | 2325 } else { |
| 2381 frames_left = (int)(twopass->total_stats.count - | 2326 frames_left = (int)(twopass->total_stats.count - |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2419 | 2364 |
| 2420 vp9_clear_system_state(); | 2365 vp9_clear_system_state(); |
| 2421 | 2366 |
| 2422 if (cpi->oxcf.rc_mode == VPX_Q) { | 2367 if (cpi->oxcf.rc_mode == VPX_Q) { |
| 2423 twopass->active_worst_quality = cpi->oxcf.cq_level; | 2368 twopass->active_worst_quality = cpi->oxcf.cq_level; |
| 2424 } else if (cm->current_video_frame == 0 || | 2369 } else if (cm->current_video_frame == 0 || |
| 2425 (lc != NULL && lc->current_video_frame_in_layer == 0)) { | 2370 (lc != NULL && lc->current_video_frame_in_layer == 0)) { |
| 2426 // Special case code for first frame. | 2371 // Special case code for first frame. |
| 2427 const int section_target_bandwidth = (int)(twopass->bits_left / | 2372 const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2428 frames_left); | 2373 frames_left); |
| 2429 const double section_error = | 2374 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, |
| 2430 twopass->total_left_stats.coded_error / twopass->total_left_stats.count; | 2375 section_target_bandwidth); |
| 2431 const int tmp_q = | |
| 2432 get_twopass_worst_quality(cpi, section_error, | |
| 2433 section_target_bandwidth, DEFAULT_GRP_WEIGHT); | |
| 2434 | |
| 2435 twopass->active_worst_quality = tmp_q; | 2376 twopass->active_worst_quality = tmp_q; |
| 2436 twopass->baseline_worst_quality = tmp_q; | |
| 2437 rc->ni_av_qi = tmp_q; | 2377 rc->ni_av_qi = tmp_q; |
| 2438 rc->last_q[INTER_FRAME] = tmp_q; | 2378 rc->last_q[INTER_FRAME] = tmp_q; |
| 2439 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); | 2379 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); |
| 2440 rc->avg_frame_qindex[INTER_FRAME] = tmp_q; | 2380 rc->avg_frame_qindex[INTER_FRAME] = tmp_q; |
| 2441 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2; | 2381 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2; |
| 2442 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; | 2382 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; |
| 2443 } | 2383 } |
| 2444 vp9_zero(this_frame); | 2384 vp9_zero(this_frame); |
| 2445 if (EOF == input_stats(twopass, &this_frame)) | 2385 if (EOF == input_stats(twopass, &this_frame)) |
| 2446 return; | 2386 return; |
| 2447 | 2387 |
| 2388 // Local copy of the current frame's first pass stats. |
| 2389 this_frame_copy = this_frame; |
| 2390 |
| 2448 // Keyframe and section processing. | 2391 // Keyframe and section processing. |
| 2449 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) { | 2392 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) { |
| 2450 FIRSTPASS_STATS this_frame_copy; | |
| 2451 this_frame_copy = this_frame; | |
| 2452 // Define next KF group and assign bits to it. | 2393 // Define next KF group and assign bits to it. |
| 2453 find_next_key_frame(cpi, &this_frame); | 2394 find_next_key_frame(cpi, &this_frame_copy); |
| 2454 this_frame = this_frame_copy; | |
| 2455 } else { | 2395 } else { |
| 2456 cm->frame_type = INTER_FRAME; | 2396 cm->frame_type = INTER_FRAME; |
| 2457 } | 2397 } |
| 2458 | 2398 |
| 2459 if (lc != NULL) { | 2399 if (lc != NULL) { |
| 2460 if (cpi->svc.spatial_layer_id == 0) { | 2400 if (cpi->svc.spatial_layer_id == 0) { |
| 2461 lc->is_key_frame = (cm->frame_type == KEY_FRAME); | 2401 lc->is_key_frame = (cm->frame_type == KEY_FRAME); |
| 2462 if (lc->is_key_frame) { | 2402 if (lc->is_key_frame) { |
| 2463 cpi->ref_frame_flags &= | 2403 cpi->ref_frame_flags &= |
| 2464 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); | 2404 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 2465 lc->frames_from_key_frame = 0; | 2405 lc->frames_from_key_frame = 0; |
| 2466 // Reset the empty frame resolution since we have a key frame. | 2406 // Reset the empty frame resolution since we have a key frame. |
| 2467 cpi->svc.empty_frame_width = cm->width; | 2407 cpi->svc.empty_frame_width = cm->width; |
| 2468 cpi->svc.empty_frame_height = cm->height; | 2408 cpi->svc.empty_frame_height = cm->height; |
| 2469 } | 2409 } |
| 2470 } else { | 2410 } else { |
| 2471 cm->frame_type = INTER_FRAME; | 2411 cm->frame_type = INTER_FRAME; |
| 2472 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 2412 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
| 2473 | 2413 |
| 2474 if (lc->is_key_frame) { | 2414 if (lc->is_key_frame) { |
| 2475 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); | 2415 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 2476 lc->frames_from_key_frame = 0; | 2416 lc->frames_from_key_frame = 0; |
| 2477 } | 2417 } |
| 2478 } | 2418 } |
| 2479 } | 2419 } |
| 2480 | 2420 |
| 2481 // Define a new GF/ARF group. (Should always enter here for key frames). | 2421 // Define a new GF/ARF group. (Should always enter here for key frames). |
| 2482 if (rc->frames_till_gf_update_due == 0) { | 2422 if (rc->frames_till_gf_update_due == 0) { |
| 2483 define_gf_group(cpi, &this_frame); | 2423 define_gf_group(cpi, &this_frame_copy); |
| 2484 | 2424 |
| 2485 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 2425 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 2486 if (lc != NULL) | 2426 if (lc != NULL) |
| 2487 cpi->refresh_golden_frame = 1; | 2427 cpi->refresh_golden_frame = 1; |
| 2488 | 2428 |
| 2489 #if ARF_STATS_OUTPUT | 2429 #if ARF_STATS_OUTPUT |
| 2490 { | 2430 { |
| 2491 FILE *fpfile; | 2431 FILE *fpfile; |
| 2492 fpfile = fopen("arf.stt", "a"); | 2432 fpfile = fopen("arf.stt", "a"); |
| 2493 ++arf_count; | 2433 ++arf_count; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2590 // Unwind undershoot or overshoot adjustment. | 2530 // Unwind undershoot or overshoot adjustment. |
| 2591 if (rc->rolling_target_bits < rc->rolling_actual_bits) | 2531 if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2592 --twopass->extend_minq; | 2532 --twopass->extend_minq; |
| 2593 else if (rc->rolling_target_bits > rc->rolling_actual_bits) | 2533 else if (rc->rolling_target_bits > rc->rolling_actual_bits) |
| 2594 --twopass->extend_maxq; | 2534 --twopass->extend_maxq; |
| 2595 } | 2535 } |
| 2596 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); | 2536 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); |
| 2597 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); | 2537 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); |
| 2598 } | 2538 } |
| 2599 } | 2539 } |
| OLD | NEW |