[email protected] | ac2d27d | 2015-02-26 13:59:22 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 7120742 | 2017-09-15 11:58:09 | [diff] [blame] | 11 | #include "common_types.h" // NOLINT(build/include) |
[email protected] | ac2d27d | 2015-02-26 13:59:22 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
eladalon | d0244c2 | 2017-06-08 11:19:13 | [diff] [blame] | 14 | #include <algorithm> |
danilchap | ef8d773 | 2017-04-19 09:59:48 | [diff] [blame] | 15 | #include <limits> |
| 16 | #include <type_traits> |
[email protected] | ac2d27d | 2015-02-26 13:59:22 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/stringutils.h" |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 20 | |
[email protected] | ac2d27d | 2015-02-26 13:59:22 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
Niels Möller | def1ef5 | 2018-03-19 12:48:44 | [diff] [blame^] | 23 | bool VideoCodecVP8::operator==(const VideoCodecVP8& other) const { |
| 24 | // Doesn't compare the tl_factory pointers, which are constructed |
| 25 | // based on other members. |
| 26 | return (complexity == other.complexity && |
| 27 | resilience == other.resilience && |
| 28 | numberOfTemporalLayers == other.numberOfTemporalLayers && |
| 29 | denoisingOn == other.denoisingOn && |
| 30 | automaticResizeOn == other.automaticResizeOn && |
| 31 | frameDroppingOn == other.frameDroppingOn && |
| 32 | keyFrameInterval == other.keyFrameInterval); |
| 33 | } |
| 34 | |
| 35 | bool VideoCodecVP9::operator==(const VideoCodecVP9& other) const { |
| 36 | return (complexity == other.complexity && |
| 37 | resilienceOn == other.resilienceOn && |
| 38 | numberOfTemporalLayers == other.numberOfTemporalLayers && |
| 39 | denoisingOn == other.denoisingOn && |
| 40 | frameDroppingOn == other.frameDroppingOn && |
| 41 | keyFrameInterval == other.keyFrameInterval && |
| 42 | adaptiveQpMode == other.adaptiveQpMode && |
| 43 | automaticResizeOn == other.automaticResizeOn && |
| 44 | numberOfSpatialLayers == other.numberOfSpatialLayers && |
| 45 | flexibleMode == other.flexibleMode); |
| 46 | } |
| 47 | |
| 48 | bool VideoCodecH264::operator==(const VideoCodecH264& other) const { |
| 49 | return (frameDroppingOn == other.frameDroppingOn && |
| 50 | keyFrameInterval == other.keyFrameInterval && |
| 51 | spsLen == other.spsLen && |
| 52 | ppsLen == other.ppsLen && |
| 53 | profile == other.profile && |
| 54 | (spsLen == 0 || memcmp(spsData, other.spsData, spsLen) == 0) && |
| 55 | (ppsLen == 0 || memcmp(ppsData, other.ppsData, ppsLen) == 0)); |
| 56 | } |
| 57 | |
| 58 | bool SpatialLayer::operator==(const SpatialLayer& other) const { |
| 59 | return (width == other.width && |
| 60 | height == other.height && |
| 61 | numberOfTemporalLayers == other.numberOfTemporalLayers && |
| 62 | maxBitrate == other.maxBitrate && |
| 63 | targetBitrate == other.targetBitrate && |
| 64 | minBitrate == other.minBitrate && |
| 65 | qpMax == other.qpMax && |
| 66 | active == other.active); |
| 67 | } |
| 68 | |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 69 | VideoCodec::VideoCodec() |
| 70 | : codecType(kVideoCodecUnknown), |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 71 | plType(0), |
| 72 | width(0), |
| 73 | height(0), |
| 74 | startBitrate(0), |
| 75 | maxBitrate(0), |
| 76 | minBitrate(0), |
| 77 | targetBitrate(0), |
| 78 | maxFramerate(0), |
Seth Hampson | f6464c9 | 2018-01-17 21:55:14 | [diff] [blame] | 79 | active(true), |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 80 | qpMax(0), |
| 81 | numberOfSimulcastStreams(0), |
| 82 | simulcastStream(), |
| 83 | spatialLayers(), |
| 84 | mode(kRealtimeVideo), |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 85 | expect_encode_from_texture(false), |
ilnik | 04f4d12 | 2017-06-19 14:18:55 | [diff] [blame] | 86 | timing_frame_thresholds({0, 0}), |
hta | 527d347 | 2016-11-17 07:23:04 | [diff] [blame] | 87 | codec_specific_() {} |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 88 | |
| 89 | VideoCodecVP8* VideoCodec::VP8() { |
| 90 | RTC_DCHECK_EQ(codecType, kVideoCodecVP8); |
hta | 527d347 | 2016-11-17 07:23:04 | [diff] [blame] | 91 | return &codec_specific_.VP8; |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | const VideoCodecVP8& VideoCodec::VP8() const { |
| 95 | RTC_DCHECK_EQ(codecType, kVideoCodecVP8); |
hta | 527d347 | 2016-11-17 07:23:04 | [diff] [blame] | 96 | return codec_specific_.VP8; |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | VideoCodecVP9* VideoCodec::VP9() { |
| 100 | RTC_DCHECK_EQ(codecType, kVideoCodecVP9); |
hta | 527d347 | 2016-11-17 07:23:04 | [diff] [blame] | 101 | return &codec_specific_.VP9; |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const VideoCodecVP9& VideoCodec::VP9() const { |
| 105 | RTC_DCHECK_EQ(codecType, kVideoCodecVP9); |
hta | 527d347 | 2016-11-17 07:23:04 | [diff] [blame] | 106 | return codec_specific_.VP9; |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | VideoCodecH264* VideoCodec::H264() { |
| 110 | RTC_DCHECK_EQ(codecType, kVideoCodecH264); |
hta | 527d347 | 2016-11-17 07:23:04 | [diff] [blame] | 111 | return &codec_specific_.H264; |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | const VideoCodecH264& VideoCodec::H264() const { |
| 115 | RTC_DCHECK_EQ(codecType, kVideoCodecH264); |
hta | 527d347 | 2016-11-17 07:23:04 | [diff] [blame] | 116 | return codec_specific_.H264; |
hta | 257dc39 | 2016-10-25 16:05:06 | [diff] [blame] | 117 | } |
| 118 | |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 119 | static const char* kPayloadNameVp8 = "VP8"; |
| 120 | static const char* kPayloadNameVp9 = "VP9"; |
| 121 | static const char* kPayloadNameH264 = "H264"; |
| 122 | static const char* kPayloadNameI420 = "I420"; |
| 123 | static const char* kPayloadNameRED = "RED"; |
| 124 | static const char* kPayloadNameULPFEC = "ULPFEC"; |
Danil Chapovalov | 3c70697 | 2018-01-30 10:11:08 | [diff] [blame] | 125 | static const char* kPayloadNameFlexfec = "flexfec-03"; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 126 | static const char* kPayloadNameGeneric = "Generic"; |
Emircan Uysaler | d7ae3c3 | 2018-01-25 21:01:09 | [diff] [blame] | 127 | static const char* kPayloadNameMultiplex = "Multiplex"; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 128 | |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 129 | static bool CodecNamesEq(const char* name1, const char* name2) { |
| 130 | return _stricmp(name1, name2) == 0; |
| 131 | } |
| 132 | |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 133 | const char* CodecTypeToPayloadString(VideoCodecType type) { |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 134 | switch (type) { |
| 135 | case kVideoCodecVP8: |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 136 | return kPayloadNameVp8; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 137 | case kVideoCodecVP9: |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 138 | return kPayloadNameVp9; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 139 | case kVideoCodecH264: |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 140 | return kPayloadNameH264; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 141 | case kVideoCodecI420: |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 142 | return kPayloadNameI420; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 143 | case kVideoCodecRED: |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 144 | return kPayloadNameRED; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 145 | case kVideoCodecULPFEC: |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 146 | return kPayloadNameULPFEC; |
Danil Chapovalov | 3c70697 | 2018-01-30 10:11:08 | [diff] [blame] | 147 | case kVideoCodecFlexfec: |
| 148 | return kPayloadNameFlexfec; |
Emircan Uysaler | 0a37547 | 2017-12-11 06:51:02 | [diff] [blame] | 149 | // Other codecs default to generic. |
Emircan Uysaler | d7ae3c3 | 2018-01-25 21:01:09 | [diff] [blame] | 150 | case kVideoCodecMultiplex: |
Emircan Uysaler | 0a37547 | 2017-12-11 06:51:02 | [diff] [blame] | 151 | case kVideoCodecGeneric: |
| 152 | case kVideoCodecUnknown: |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 153 | return kPayloadNameGeneric; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 154 | } |
Emircan Uysaler | 0a37547 | 2017-12-11 06:51:02 | [diff] [blame] | 155 | return kPayloadNameGeneric; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 156 | } |
| 157 | |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 158 | VideoCodecType PayloadStringToCodecType(const std::string& name) { |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 159 | if (CodecNamesEq(name.c_str(), kPayloadNameVp8)) |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 160 | return kVideoCodecVP8; |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 161 | if (CodecNamesEq(name.c_str(), kPayloadNameVp9)) |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 162 | return kVideoCodecVP9; |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 163 | if (CodecNamesEq(name.c_str(), kPayloadNameH264)) |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 164 | return kVideoCodecH264; |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 165 | if (CodecNamesEq(name.c_str(), kPayloadNameI420)) |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 166 | return kVideoCodecI420; |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 167 | if (CodecNamesEq(name.c_str(), kPayloadNameRED)) |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 168 | return kVideoCodecRED; |
magjed | 10165ab | 2016-11-22 18:16:57 | [diff] [blame] | 169 | if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC)) |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 170 | return kVideoCodecULPFEC; |
Danil Chapovalov | 3c70697 | 2018-01-30 10:11:08 | [diff] [blame] | 171 | if (CodecNamesEq(name.c_str(), kPayloadNameFlexfec)) |
| 172 | return kVideoCodecFlexfec; |
Emircan Uysaler | d7ae3c3 | 2018-01-25 21:01:09 | [diff] [blame] | 173 | if (CodecNamesEq(name.c_str(), kPayloadNameMultiplex)) |
| 174 | return kVideoCodecMultiplex; |
kthelgason | 1cdddc9 | 2017-08-24 10:52:48 | [diff] [blame] | 175 | return kVideoCodecGeneric; |
| 176 | } |
| 177 | |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 178 | const uint32_t BitrateAllocation::kMaxBitrateBps = |
| 179 | std::numeric_limits<uint32_t>::max(); |
| 180 | |
[email protected] | 01f2ec3 | 2017-11-15 13:58:23 | [diff] [blame] | 181 | BitrateAllocation::BitrateAllocation() : sum_(0), bitrates_{}, has_bitrate_{} {} |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 182 | |
| 183 | bool BitrateAllocation::SetBitrate(size_t spatial_index, |
| 184 | size_t temporal_index, |
| 185 | uint32_t bitrate_bps) { |
sprang | 6d314c7 | 2016-12-06 14:08:53 | [diff] [blame] | 186 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 187 | RTC_CHECK_LT(temporal_index, kMaxTemporalStreams); |
| 188 | RTC_CHECK_LE(bitrates_[spatial_index][temporal_index], sum_); |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 189 | uint64_t new_bitrate_sum_bps = sum_; |
| 190 | new_bitrate_sum_bps -= bitrates_[spatial_index][temporal_index]; |
| 191 | new_bitrate_sum_bps += bitrate_bps; |
| 192 | if (new_bitrate_sum_bps > kMaxBitrateBps) |
| 193 | return false; |
| 194 | |
| 195 | bitrates_[spatial_index][temporal_index] = bitrate_bps; |
[email protected] | 01f2ec3 | 2017-11-15 13:58:23 | [diff] [blame] | 196 | has_bitrate_[spatial_index][temporal_index] = true; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 197 | sum_ = static_cast<uint32_t>(new_bitrate_sum_bps); |
| 198 | return true; |
| 199 | } |
| 200 | |
[email protected] | 01f2ec3 | 2017-11-15 13:58:23 | [diff] [blame] | 201 | bool BitrateAllocation::HasBitrate(size_t spatial_index, |
| 202 | size_t temporal_index) const { |
| 203 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 204 | RTC_CHECK_LT(temporal_index, kMaxTemporalStreams); |
| 205 | return has_bitrate_[spatial_index][temporal_index]; |
| 206 | } |
| 207 | |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 208 | uint32_t BitrateAllocation::GetBitrate(size_t spatial_index, |
| 209 | size_t temporal_index) const { |
sprang | 6d314c7 | 2016-12-06 14:08:53 | [diff] [blame] | 210 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 211 | RTC_CHECK_LT(temporal_index, kMaxTemporalStreams); |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 212 | return bitrates_[spatial_index][temporal_index]; |
| 213 | } |
| 214 | |
[email protected] | 01f2ec3 | 2017-11-15 13:58:23 | [diff] [blame] | 215 | // Whether the specific spatial layers has the bitrate set in any of its |
| 216 | // temporal layers. |
| 217 | bool BitrateAllocation::IsSpatialLayerUsed(size_t spatial_index) const { |
| 218 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
| 219 | for (int i = 0; i < kMaxTemporalStreams; ++i) { |
| 220 | if (has_bitrate_[spatial_index][i]) |
| 221 | return true; |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 226 | // Get the sum of all the temporal layer for a specific spatial layer. |
| 227 | uint32_t BitrateAllocation::GetSpatialLayerSum(size_t spatial_index) const { |
sprang | 6d314c7 | 2016-12-06 14:08:53 | [diff] [blame] | 228 | RTC_CHECK_LT(spatial_index, kMaxSpatialLayers); |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 229 | uint32_t sum = 0; |
| 230 | for (int i = 0; i < kMaxTemporalStreams; ++i) |
| 231 | sum += bitrates_[spatial_index][i]; |
| 232 | return sum; |
| 233 | } |
| 234 | |
sprang | d0fc37a | 2017-06-22 12:40:25 | [diff] [blame] | 235 | std::string BitrateAllocation::ToString() const { |
| 236 | if (sum_ == 0) |
| 237 | return "BitrateAllocation [ [] ]"; |
| 238 | |
| 239 | // TODO(sprang): Replace this stringstream with something cheaper. |
| 240 | std::ostringstream oss; |
| 241 | oss << "BitrateAllocation ["; |
| 242 | uint32_t spatial_cumulator = 0; |
| 243 | for (int si = 0; si < kMaxSpatialLayers; ++si) { |
| 244 | RTC_DCHECK_LE(spatial_cumulator, sum_); |
| 245 | if (spatial_cumulator == sum_) |
| 246 | break; |
| 247 | |
| 248 | const uint32_t layer_sum = GetSpatialLayerSum(si); |
| 249 | if (layer_sum == sum_) { |
| 250 | oss << " ["; |
| 251 | } else { |
| 252 | if (si > 0) |
| 253 | oss << ","; |
| 254 | oss << std::endl << " ["; |
| 255 | } |
| 256 | spatial_cumulator += layer_sum; |
| 257 | |
| 258 | uint32_t temporal_cumulator = 0; |
| 259 | for (int ti = 0; ti < kMaxTemporalStreams; ++ti) { |
| 260 | RTC_DCHECK_LE(temporal_cumulator, layer_sum); |
| 261 | if (temporal_cumulator == layer_sum) |
| 262 | break; |
| 263 | |
| 264 | if (ti > 0) |
| 265 | oss << ", "; |
| 266 | |
| 267 | uint32_t bitrate = bitrates_[si][ti]; |
| 268 | oss << bitrate; |
| 269 | temporal_cumulator += bitrate; |
| 270 | } |
| 271 | oss << "]"; |
| 272 | } |
| 273 | |
| 274 | RTC_DCHECK_EQ(spatial_cumulator, sum_); |
| 275 | oss << " ]"; |
| 276 | return oss.str(); |
| 277 | } |
| 278 | |
| 279 | std::ostream& BitrateAllocation::operator<<(std::ostream& os) const { |
| 280 | os << ToString(); |
| 281 | return os; |
| 282 | } |
| 283 | |
[email protected] | ac2d27d | 2015-02-26 13:59:22 | [diff] [blame] | 284 | } // namespace webrtc |