niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/media_optimization.h" |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <algorithm> |
Stefan Holmer | 144475b | 2017-03-10 15:08:26 +0100 | [diff] [blame] | 15 | #include <limits> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/video_coding/utility/frame_dropper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 21 | namespace media_optimization { |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 22 | namespace { |
| 23 | const int64_t kFrameHistoryWinMs = 2000; |
| 24 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 26 | MediaOptimization::MediaOptimization(Clock* clock) |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 27 | : clock_(clock), |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 28 | max_bit_rate_(0), |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 29 | max_frame_rate_(0), |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 30 | frame_dropper_(), |
Åsa Persson | 0122e84 | 2017-10-16 12:19:23 +0200 | [diff] [blame] | 31 | incoming_frame_rate_(0) { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 32 | memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_)); |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 33 | frame_dropper_.SetRates(0, 0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 36 | MediaOptimization::~MediaOptimization() = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 38 | void MediaOptimization::SetEncodingData(int32_t max_bit_rate, |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 39 | uint32_t target_bitrate, |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 40 | uint32_t max_frame_rate) { |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 41 | rtc::CritScope lock(&crit_sect_); |
Åsa Persson | eb45007 | 2017-11-13 10:24:22 +0100 | [diff] [blame] | 42 | // Everything codec specific should be reset here since the codec has changed. |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 43 | max_bit_rate_ = max_bit_rate; |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 44 | max_frame_rate_ = static_cast<float>(max_frame_rate); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 45 | float target_bitrate_kbps = static_cast<float>(target_bitrate) / 1000.0f; |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 46 | frame_dropper_.Reset(); |
| 47 | frame_dropper_.SetRates(target_bitrate_kbps, max_frame_rate_); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 48 | } |
| 49 | |
asapersson | 9abd275 | 2016-12-08 02:19:40 -0800 | [diff] [blame] | 50 | uint32_t MediaOptimization::SetTargetRates(uint32_t target_bitrate) { |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 51 | rtc::CritScope lock(&crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 53 | // Cap target video bitrate to codec maximum. |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 54 | int video_target_bitrate = target_bitrate; |
| 55 | if (max_bit_rate_ > 0 && video_target_bitrate > max_bit_rate_) { |
| 56 | video_target_bitrate = max_bit_rate_; |
pbos | 7367463 | 2015-10-29 15:45:00 -0700 | [diff] [blame] | 57 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 58 | float target_video_bitrate_kbps = |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 59 | static_cast<float>(video_target_bitrate) / 1000.0f; |
Åsa Persson | eb45007 | 2017-11-13 10:24:22 +0100 | [diff] [blame] | 60 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 61 | float framerate = incoming_frame_rate_; |
| 62 | if (framerate == 0.0) { |
| 63 | // No framerate estimate available, use configured max framerate instead. |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 64 | framerate = max_frame_rate_; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 67 | frame_dropper_.SetRates(target_video_bitrate_kbps, framerate); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 68 | |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 69 | return video_target_bitrate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 72 | uint32_t MediaOptimization::InputFrameRate() { |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 73 | rtc::CritScope lock(&crit_sect_); |
wuchengli@chromium.org | ae7cfd7 | 2014-06-30 08:01:47 +0000 | [diff] [blame] | 74 | return InputFrameRateInternal(); |
| 75 | } |
| 76 | |
| 77 | uint32_t MediaOptimization::InputFrameRateInternal() { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 78 | ProcessIncomingFrameRate(clock_->TimeInMilliseconds()); |
Stefan Holmer | 144475b | 2017-03-10 15:08:26 +0100 | [diff] [blame] | 79 | uint32_t framerate = static_cast<uint32_t>(std::min<float>( |
| 80 | std::numeric_limits<uint32_t>::max(), incoming_frame_rate_ + 0.5f)); |
| 81 | return framerate; |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Åsa Persson | b52a4d9 | 2017-11-08 11:33:37 +0100 | [diff] [blame] | 84 | void MediaOptimization::UpdateWithEncodedData( |
Ying Wang | 0dd1b0a | 2018-02-20 12:50:27 +0100 | [diff] [blame] | 85 | const size_t encoded_image_length, |
| 86 | const FrameType encoded_image_frametype) { |
| 87 | size_t encoded_length = encoded_image_length; |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 88 | rtc::CritScope lock(&crit_sect_); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 89 | if (encoded_length > 0) { |
Ying Wang | 0dd1b0a | 2018-02-20 12:50:27 +0100 | [diff] [blame] | 90 | const bool delta_frame = encoded_image_frametype != kVideoFrameKey; |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 91 | frame_dropper_.Fill(encoded_length, delta_frame); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 92 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 95 | void MediaOptimization::EnableFrameDropper(bool enable) { |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 96 | rtc::CritScope lock(&crit_sect_); |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 97 | frame_dropper_.Enable(enable); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool MediaOptimization::DropFrame() { |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 101 | rtc::CritScope lock(&crit_sect_); |
andresp@webrtc.org | e682aa5 | 2013-12-19 10:59:48 +0000 | [diff] [blame] | 102 | UpdateIncomingFrameRate(); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 103 | // Leak appropriate number of bytes. |
Rasmus Brandt | 3613fef | 2018-09-03 16:19:29 +0200 | [diff] [blame] | 104 | frame_dropper_.Leak(static_cast<uint32_t>(InputFrameRateInternal() + 0.5f)); |
| 105 | return frame_dropper_.DropFrame(); |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 108 | void MediaOptimization::UpdateIncomingFrameRate() { |
| 109 | int64_t now = clock_->TimeInMilliseconds(); |
| 110 | if (incoming_frame_times_[0] == 0) { |
| 111 | // No shifting if this is the first time. |
| 112 | } else { |
| 113 | // Shift all times one step. |
| 114 | for (int32_t i = (kFrameCountHistorySize - 2); i >= 0; i--) { |
| 115 | incoming_frame_times_[i + 1] = incoming_frame_times_[i]; |
| 116 | } |
| 117 | } |
| 118 | incoming_frame_times_[0] = now; |
| 119 | ProcessIncomingFrameRate(now); |
| 120 | } |
| 121 | |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 122 | // Allowing VCM to keep track of incoming frame rate. |
| 123 | void MediaOptimization::ProcessIncomingFrameRate(int64_t now) { |
| 124 | int32_t num = 0; |
| 125 | int32_t nr_of_frames = 0; |
| 126 | for (num = 1; num < (kFrameCountHistorySize - 1); ++num) { |
| 127 | if (incoming_frame_times_[num] <= 0 || |
asapersson | 60dfbdb | 2017-08-07 00:03:03 -0700 | [diff] [blame] | 128 | // Don't use data older than 2 s. |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 129 | now - incoming_frame_times_[num] > kFrameHistoryWinMs) { |
| 130 | break; |
| 131 | } else { |
| 132 | nr_of_frames++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 134 | } |
| 135 | if (num > 1) { |
Erik Språng | 66a641a | 2015-06-11 14:20:07 +0200 | [diff] [blame] | 136 | const int64_t diff = |
| 137 | incoming_frame_times_[0] - incoming_frame_times_[num - 1]; |
| 138 | incoming_frame_rate_ = 0.0; // No frame rate estimate available. |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 139 | if (diff > 0) { |
| 140 | incoming_frame_rate_ = nr_of_frames * 1000.0f / static_cast<float>(diff); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | } |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 142 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | } |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 144 | } // namespace media_optimization |
| 145 | } // namespace webrtc |