Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| 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 | |
| 11 | #include "api/video/video_bitrate_allocation.h" |
| 12 | |
| 13 | #include <limits> |
| 14 | |
| 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/numerics/safe_conversions.h" |
| 17 | #include "rtc_base/strings/string_builder.h" |
| 18 | #include "rtc_base/stringutils.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | VideoBitrateAllocation::VideoBitrateAllocation() : sum_(0) {} |
| 23 | |
| 24 | bool VideoBitrateAllocation::SetBitrate(size_t spatial_index, |
| 25 | size_t temporal_index, |
| 26 | uint32_t bitrate_bps) { |
| 27 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 28 | RTC_CHECK_LT(temporal_index, kMaxTemporalStreams); |
| 29 | int64_t new_bitrate_sum_bps = sum_; |
| 30 | rtc::Optional<uint32_t>& layer_bitrate = |
| 31 | bitrates_[spatial_index][temporal_index]; |
| 32 | if (layer_bitrate) { |
| 33 | RTC_DCHECK_LE(*layer_bitrate, sum_); |
| 34 | new_bitrate_sum_bps -= *layer_bitrate; |
| 35 | } |
| 36 | new_bitrate_sum_bps += bitrate_bps; |
| 37 | if (new_bitrate_sum_bps > kMaxBitrateBps) |
| 38 | return false; |
| 39 | |
| 40 | layer_bitrate = bitrate_bps; |
| 41 | sum_ = rtc::dchecked_cast<uint32_t>(new_bitrate_sum_bps); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool VideoBitrateAllocation::HasBitrate(size_t spatial_index, |
| 46 | size_t temporal_index) const { |
| 47 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 48 | RTC_CHECK_LT(temporal_index, kMaxTemporalStreams); |
| 49 | return bitrates_[spatial_index][temporal_index].has_value(); |
| 50 | } |
| 51 | |
| 52 | uint32_t VideoBitrateAllocation::GetBitrate(size_t spatial_index, |
| 53 | size_t temporal_index) const { |
| 54 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 55 | RTC_CHECK_LT(temporal_index, kMaxTemporalStreams); |
| 56 | return bitrates_[spatial_index][temporal_index].value_or(0); |
| 57 | } |
| 58 | |
| 59 | // Whether the specific spatial layers has the bitrate set in any of its |
| 60 | // temporal layers. |
| 61 | bool VideoBitrateAllocation::IsSpatialLayerUsed(size_t spatial_index) const { |
| 62 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 63 | for (size_t i = 0; i < kMaxTemporalStreams; ++i) { |
| 64 | if (bitrates_[spatial_index][i].has_value()) |
| 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Get the sum of all the temporal layer for a specific spatial layer. |
| 71 | uint32_t VideoBitrateAllocation::GetSpatialLayerSum( |
| 72 | size_t spatial_index) const { |
| 73 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 74 | return GetTemporalLayerSum(spatial_index, kMaxTemporalStreams - 1); |
| 75 | } |
| 76 | |
| 77 | uint32_t VideoBitrateAllocation::GetTemporalLayerSum( |
| 78 | size_t spatial_index, |
| 79 | size_t temporal_index) const { |
| 80 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 81 | RTC_CHECK_LT(temporal_index, kMaxTemporalStreams); |
| 82 | uint32_t sum = 0; |
| 83 | for (size_t i = 0; i <= temporal_index; ++i) { |
| 84 | sum += bitrates_[spatial_index][i].value_or(0); |
| 85 | } |
| 86 | return sum; |
| 87 | } |
| 88 | |
| 89 | std::vector<uint32_t> VideoBitrateAllocation::GetTemporalLayerAllocation( |
| 90 | size_t spatial_index) const { |
| 91 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 92 | std::vector<uint32_t> temporal_rates; |
| 93 | |
| 94 | // Find the highest temporal layer with a defined bitrate in order to |
| 95 | // determine the size of the temporal layer allocation. |
| 96 | for (size_t i = kMaxTemporalStreams; i > 0; --i) { |
| 97 | if (bitrates_[spatial_index][i - 1].has_value()) { |
| 98 | temporal_rates.resize(i); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | for (size_t i = 0; i < temporal_rates.size(); ++i) { |
| 104 | temporal_rates[i] = bitrates_[spatial_index][i].value_or(0); |
| 105 | } |
| 106 | |
| 107 | return temporal_rates; |
| 108 | } |
| 109 | |
| 110 | bool VideoBitrateAllocation::operator==( |
| 111 | const VideoBitrateAllocation& other) const { |
| 112 | for (size_t si = 0; si < kMaxSpatialLayers; ++si) { |
| 113 | for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) { |
| 114 | if (bitrates_[si][ti] != other.bitrates_[si][ti]) |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | std::string VideoBitrateAllocation::ToString() const { |
| 122 | if (sum_ == 0) |
| 123 | return "VideoBitrateAllocation [ [] ]"; |
| 124 | |
| 125 | // Max string length in practice is 260, but let's have some overhead and |
| 126 | // round up to nearest power of two. |
| 127 | char string_buf[512]; |
| 128 | rtc::SimpleStringBuilder ssb(string_buf); |
| 129 | |
| 130 | ssb << "VideoBitrateAllocation ["; |
| 131 | uint32_t spatial_cumulator = 0; |
| 132 | for (size_t si = 0; si < kMaxSpatialLayers; ++si) { |
| 133 | RTC_DCHECK_LE(spatial_cumulator, sum_); |
| 134 | if (spatial_cumulator == sum_) |
| 135 | break; |
| 136 | |
| 137 | const uint32_t layer_sum = GetSpatialLayerSum(si); |
| 138 | if (layer_sum == sum_) { |
| 139 | ssb << " ["; |
| 140 | } else { |
| 141 | if (si > 0) |
| 142 | ssb << ","; |
| 143 | ssb << '\n' << " ["; |
| 144 | } |
| 145 | spatial_cumulator += layer_sum; |
| 146 | |
| 147 | uint32_t temporal_cumulator = 0; |
| 148 | for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) { |
| 149 | RTC_DCHECK_LE(temporal_cumulator, layer_sum); |
| 150 | if (temporal_cumulator == layer_sum) |
| 151 | break; |
| 152 | |
| 153 | if (ti > 0) |
| 154 | ssb << ", "; |
| 155 | |
| 156 | uint32_t bitrate = bitrates_[si][ti].value_or(0); |
| 157 | ssb << bitrate; |
| 158 | temporal_cumulator += bitrate; |
| 159 | } |
| 160 | ssb << "]"; |
| 161 | } |
| 162 | |
| 163 | RTC_DCHECK_EQ(spatial_cumulator, sum_); |
| 164 | ssb << " ]"; |
| 165 | return ssb.str(); |
| 166 | } |
| 167 | |
| 168 | } // namespace webrtc |