Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: 0b262a6ab953a15d2599bb9025c51168749efc80 [file] [log] [blame]
[email protected]ac2d27d2015-02-26 13:59:221/*
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 Bonadei71207422017-09-15 11:58:0911#include "common_types.h" // NOLINT(build/include)
[email protected]ac2d27d2015-02-26 13:59:2212
13#include <string.h>
eladalond0244c22017-06-08 11:19:1314#include <algorithm>
danilchapef8d7732017-04-19 09:59:4815#include <limits>
16#include <type_traits>
[email protected]ac2d27d2015-02-26 13:59:2217
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
19#include "rtc_base/stringutils.h"
magjed10165ab2016-11-22 18:16:5720
[email protected]ac2d27d2015-02-26 13:59:2221namespace webrtc {
22
Niels Möllerdef1ef52018-03-19 12:48:4423bool 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
35bool 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
48bool 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
58bool 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
hta257dc392016-10-25 16:05:0669VideoCodec::VideoCodec()
70 : codecType(kVideoCodecUnknown),
hta257dc392016-10-25 16:05:0671 plType(0),
72 width(0),
73 height(0),
74 startBitrate(0),
75 maxBitrate(0),
76 minBitrate(0),
77 targetBitrate(0),
78 maxFramerate(0),
Seth Hampsonf6464c92018-01-17 21:55:1479 active(true),
hta257dc392016-10-25 16:05:0680 qpMax(0),
81 numberOfSimulcastStreams(0),
82 simulcastStream(),
83 spatialLayers(),
84 mode(kRealtimeVideo),
Erik Språng08127a92016-11-16 15:41:3085 expect_encode_from_texture(false),
ilnik04f4d122017-06-19 14:18:5586 timing_frame_thresholds({0, 0}),
hta527d3472016-11-17 07:23:0487 codec_specific_() {}
hta257dc392016-10-25 16:05:0688
89VideoCodecVP8* VideoCodec::VP8() {
90 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-17 07:23:0491 return &codec_specific_.VP8;
hta257dc392016-10-25 16:05:0692}
93
94const VideoCodecVP8& VideoCodec::VP8() const {
95 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-17 07:23:0496 return codec_specific_.VP8;
hta257dc392016-10-25 16:05:0697}
98
99VideoCodecVP9* VideoCodec::VP9() {
100 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-17 07:23:04101 return &codec_specific_.VP9;
hta257dc392016-10-25 16:05:06102}
103
104const VideoCodecVP9& VideoCodec::VP9() const {
105 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-17 07:23:04106 return codec_specific_.VP9;
hta257dc392016-10-25 16:05:06107}
108
109VideoCodecH264* VideoCodec::H264() {
110 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-17 07:23:04111 return &codec_specific_.H264;
hta257dc392016-10-25 16:05:06112}
113
114const VideoCodecH264& VideoCodec::H264() const {
115 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-17 07:23:04116 return codec_specific_.H264;
hta257dc392016-10-25 16:05:06117}
118
Erik Språng08127a92016-11-16 15:41:30119static const char* kPayloadNameVp8 = "VP8";
120static const char* kPayloadNameVp9 = "VP9";
121static const char* kPayloadNameH264 = "H264";
122static const char* kPayloadNameI420 = "I420";
123static const char* kPayloadNameRED = "RED";
124static const char* kPayloadNameULPFEC = "ULPFEC";
Danil Chapovalov3c706972018-01-30 10:11:08125static const char* kPayloadNameFlexfec = "flexfec-03";
Erik Språng08127a92016-11-16 15:41:30126static const char* kPayloadNameGeneric = "Generic";
Emircan Uysalerd7ae3c32018-01-25 21:01:09127static const char* kPayloadNameMultiplex = "Multiplex";
Erik Språng08127a92016-11-16 15:41:30128
magjed10165ab2016-11-22 18:16:57129static bool CodecNamesEq(const char* name1, const char* name2) {
130 return _stricmp(name1, name2) == 0;
131}
132
kthelgason1cdddc92017-08-24 10:52:48133const char* CodecTypeToPayloadString(VideoCodecType type) {
Erik Språng08127a92016-11-16 15:41:30134 switch (type) {
135 case kVideoCodecVP8:
kthelgason1cdddc92017-08-24 10:52:48136 return kPayloadNameVp8;
Erik Språng08127a92016-11-16 15:41:30137 case kVideoCodecVP9:
kthelgason1cdddc92017-08-24 10:52:48138 return kPayloadNameVp9;
Erik Språng08127a92016-11-16 15:41:30139 case kVideoCodecH264:
kthelgason1cdddc92017-08-24 10:52:48140 return kPayloadNameH264;
Erik Språng08127a92016-11-16 15:41:30141 case kVideoCodecI420:
kthelgason1cdddc92017-08-24 10:52:48142 return kPayloadNameI420;
Erik Språng08127a92016-11-16 15:41:30143 case kVideoCodecRED:
kthelgason1cdddc92017-08-24 10:52:48144 return kPayloadNameRED;
Erik Språng08127a92016-11-16 15:41:30145 case kVideoCodecULPFEC:
kthelgason1cdddc92017-08-24 10:52:48146 return kPayloadNameULPFEC;
Danil Chapovalov3c706972018-01-30 10:11:08147 case kVideoCodecFlexfec:
148 return kPayloadNameFlexfec;
Emircan Uysaler0a375472017-12-11 06:51:02149 // Other codecs default to generic.
Emircan Uysalerd7ae3c32018-01-25 21:01:09150 case kVideoCodecMultiplex:
Emircan Uysaler0a375472017-12-11 06:51:02151 case kVideoCodecGeneric:
152 case kVideoCodecUnknown:
kthelgason1cdddc92017-08-24 10:52:48153 return kPayloadNameGeneric;
Erik Språng08127a92016-11-16 15:41:30154 }
Emircan Uysaler0a375472017-12-11 06:51:02155 return kPayloadNameGeneric;
Erik Språng08127a92016-11-16 15:41:30156}
157
kthelgason1cdddc92017-08-24 10:52:48158VideoCodecType PayloadStringToCodecType(const std::string& name) {
magjed10165ab2016-11-22 18:16:57159 if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
kthelgason1cdddc92017-08-24 10:52:48160 return kVideoCodecVP8;
magjed10165ab2016-11-22 18:16:57161 if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
kthelgason1cdddc92017-08-24 10:52:48162 return kVideoCodecVP9;
magjed10165ab2016-11-22 18:16:57163 if (CodecNamesEq(name.c_str(), kPayloadNameH264))
kthelgason1cdddc92017-08-24 10:52:48164 return kVideoCodecH264;
magjed10165ab2016-11-22 18:16:57165 if (CodecNamesEq(name.c_str(), kPayloadNameI420))
kthelgason1cdddc92017-08-24 10:52:48166 return kVideoCodecI420;
magjed10165ab2016-11-22 18:16:57167 if (CodecNamesEq(name.c_str(), kPayloadNameRED))
kthelgason1cdddc92017-08-24 10:52:48168 return kVideoCodecRED;
magjed10165ab2016-11-22 18:16:57169 if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
kthelgason1cdddc92017-08-24 10:52:48170 return kVideoCodecULPFEC;
Danil Chapovalov3c706972018-01-30 10:11:08171 if (CodecNamesEq(name.c_str(), kPayloadNameFlexfec))
172 return kVideoCodecFlexfec;
Emircan Uysalerd7ae3c32018-01-25 21:01:09173 if (CodecNamesEq(name.c_str(), kPayloadNameMultiplex))
174 return kVideoCodecMultiplex;
kthelgason1cdddc92017-08-24 10:52:48175 return kVideoCodecGeneric;
176}
177
Erik Språng08127a92016-11-16 15:41:30178const uint32_t BitrateAllocation::kMaxBitrateBps =
179 std::numeric_limits<uint32_t>::max();
180
[email protected]01f2ec32017-11-15 13:58:23181BitrateAllocation::BitrateAllocation() : sum_(0), bitrates_{}, has_bitrate_{} {}
Erik Språng08127a92016-11-16 15:41:30182
183bool BitrateAllocation::SetBitrate(size_t spatial_index,
184 size_t temporal_index,
185 uint32_t bitrate_bps) {
sprang6d314c72016-12-06 14:08:53186 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ång08127a92016-11-16 15:41:30189 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]01f2ec32017-11-15 13:58:23196 has_bitrate_[spatial_index][temporal_index] = true;
Erik Språng08127a92016-11-16 15:41:30197 sum_ = static_cast<uint32_t>(new_bitrate_sum_bps);
198 return true;
199}
200
[email protected]01f2ec32017-11-15 13:58:23201bool 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ång08127a92016-11-16 15:41:30208uint32_t BitrateAllocation::GetBitrate(size_t spatial_index,
209 size_t temporal_index) const {
sprang6d314c72016-12-06 14:08:53210 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
211 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
Erik Språng08127a92016-11-16 15:41:30212 return bitrates_[spatial_index][temporal_index];
213}
214
[email protected]01f2ec32017-11-15 13:58:23215// Whether the specific spatial layers has the bitrate set in any of its
216// temporal layers.
217bool 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ång08127a92016-11-16 15:41:30226// Get the sum of all the temporal layer for a specific spatial layer.
227uint32_t BitrateAllocation::GetSpatialLayerSum(size_t spatial_index) const {
sprang6d314c72016-12-06 14:08:53228 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
Erik Språng08127a92016-11-16 15:41:30229 uint32_t sum = 0;
230 for (int i = 0; i < kMaxTemporalStreams; ++i)
231 sum += bitrates_[spatial_index][i];
232 return sum;
233}
234
sprangd0fc37a2017-06-22 12:40:25235std::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
279std::ostream& BitrateAllocation::operator<<(std::ostream& os) const {
280 os << ToString();
281 return os;
282}
283
[email protected]ac2d27d2015-02-26 13:59:22284} // namespace webrtc