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