blob: 24c9e7cd1740a8f5dcbf03f46e1083490ea50d7a [file] [log] [blame]
Erik Språng08127a92016-11-16 16:41:30 +01001/*
2 * Copyright (c) 2016 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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/include/video_codec_initializer.h"
Erik Språng08127a92016-11-16 16:41:30 +010012
Mirko Bonadei71207422017-09-15 13:58:09 +020013#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "common_video/include/video_bitrate_allocator.h"
15#include "modules/video_coding/codecs/vp8/screenshare_layers.h"
16#include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
17#include "modules/video_coding/codecs/vp8/temporal_layers.h"
18#include "modules/video_coding/include/video_coding_defines.h"
19#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
20#include "rtc_base/basictypes.h"
21#include "rtc_base/logging.h"
22#include "system_wrappers/include/clock.h"
Erik Språng08127a92016-11-16 16:41:30 +010023
24namespace webrtc {
emircanbbcc3562017-08-18 00:28:40 -070025namespace {
26bool TemporalLayersConfigured(const std::vector<VideoStream>& streams) {
27 for (const VideoStream& stream : streams) {
28 if (stream.temporal_layer_thresholds_bps.size() > 0)
29 return true;
30 }
31 return false;
32}
33} // namespace
Erik Språng08127a92016-11-16 16:41:30 +010034
35bool VideoCodecInitializer::SetupCodec(
36 const VideoEncoderConfig& config,
37 const VideoSendStream::Config::EncoderSettings settings,
38 const std::vector<VideoStream>& streams,
asapersson5f7226f2016-11-25 04:37:00 -080039 bool nack_enabled,
Erik Språng08127a92016-11-16 16:41:30 +010040 VideoCodec* codec,
41 std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator) {
Ivo Creusen6bc7bb62018-01-26 12:24:52 +000042 if (PayloadStringToCodecType(settings.payload_name) == kVideoCodecStereo) {
Emircan Uysaler0a375472017-12-11 12:21:02 +053043 VideoSendStream::Config::EncoderSettings associated_codec_settings =
44 settings;
45 associated_codec_settings.payload_name =
46 CodecTypeToPayloadString(kVideoCodecVP9);
47 if (!SetupCodec(config, associated_codec_settings, streams, nack_enabled,
48 codec, bitrate_allocator)) {
49 RTC_LOG(LS_ERROR) << "Failed to create stereo encoder configuration.";
50 return false;
51 }
Ivo Creusen6bc7bb62018-01-26 12:24:52 +000052 codec->codecType = kVideoCodecStereo;
Qiang Chena9329db2017-12-14 14:05:43 -080053 strncpy(codec->plName, settings.payload_name.c_str(),
54 sizeof(codec->plName));
Emircan Uysaler0a375472017-12-11 12:21:02 +053055 return true;
56 }
57
asapersson5f7226f2016-11-25 04:37:00 -080058 *codec =
59 VideoEncoderConfigToVideoCodec(config, streams, settings.payload_name,
60 settings.payload_type, nack_enabled);
Erik Språng08127a92016-11-16 16:41:30 +010061
62 std::unique_ptr<TemporalLayersFactory> tl_factory;
63 switch (codec->codecType) {
64 case kVideoCodecVP8: {
65 if (!codec->VP8()->tl_factory) {
66 if (codec->mode == kScreensharing &&
sprang429600d2017-01-26 06:12:26 -080067 (codec->numberOfSimulcastStreams > 1 ||
68 (codec->numberOfSimulcastStreams == 1 &&
69 codec->VP8()->numberOfTemporalLayers == 2))) {
Erik Språng08127a92016-11-16 16:41:30 +010070 // Conference mode temporal layering for screen content.
71 tl_factory.reset(new ScreenshareTemporalLayersFactory());
72 } else {
73 // Standard video temporal layers.
74 tl_factory.reset(new TemporalLayersFactory());
75 }
76 codec->VP8()->tl_factory = tl_factory.get();
77 }
78 break;
79 }
80 default: {
81 // TODO(sprang): Warn, once we have specific allocators for all supported
82 // codec types.
83 break;
84 }
85 }
86 *bitrate_allocator = CreateBitrateAllocator(*codec, std::move(tl_factory));
87
88 return true;
89}
90
91std::unique_ptr<VideoBitrateAllocator>
92VideoCodecInitializer::CreateBitrateAllocator(
93 const VideoCodec& codec,
94 std::unique_ptr<TemporalLayersFactory> tl_factory) {
95 std::unique_ptr<VideoBitrateAllocator> rate_allocator;
96
97 switch (codec.codecType) {
98 case kVideoCodecVP8: {
99 // Set up default VP8 temporal layer factory, if not provided.
100 rate_allocator.reset(
101 new SimulcastRateAllocator(codec, std::move(tl_factory)));
102 } break;
103 default:
104 rate_allocator.reset(new DefaultVideoBitrateAllocator(codec));
105 }
106
107 return rate_allocator;
108}
109
110// TODO(sprang): Split this up and separate the codec specific parts.
111VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
112 const VideoEncoderConfig& config,
113 const std::vector<VideoStream>& streams,
114 const std::string& payload_name,
asapersson5f7226f2016-11-25 04:37:00 -0800115 int payload_type,
116 bool nack_enabled) {
Erik Språng08127a92016-11-16 16:41:30 +0100117 static const int kEncoderMinBitrateKbps = 30;
118 RTC_DCHECK(!streams.empty());
119 RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0);
120
121 VideoCodec video_codec;
122 memset(&video_codec, 0, sizeof(video_codec));
kthelgason1cdddc92017-08-24 03:52:48 -0700123 video_codec.codecType = PayloadStringToCodecType(payload_name);
Erik Språng08127a92016-11-16 16:41:30 +0100124
125 switch (config.content_type) {
126 case VideoEncoderConfig::ContentType::kRealtimeVideo:
127 video_codec.mode = kRealtimeVideo;
128 break;
129 case VideoEncoderConfig::ContentType::kScreen:
130 video_codec.mode = kScreensharing;
aleloi47037412017-01-26 07:57:15 -0800131 if (!streams.empty() &&
Erik Språng08127a92016-11-16 16:41:30 +0100132 streams[0].temporal_layer_thresholds_bps.size() == 1) {
133 video_codec.targetBitrate =
134 streams[0].temporal_layer_thresholds_bps[0] / 1000;
135 }
136 break;
137 }
138
139 if (config.encoder_specific_settings)
140 config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec);
141
142 switch (video_codec.codecType) {
143 case kVideoCodecVP8: {
144 if (!config.encoder_specific_settings)
145 *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings();
146 video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>(
147 streams.back().temporal_layer_thresholds_bps.size() + 1);
emircanbbcc3562017-08-18 00:28:40 -0700148
149 if (nack_enabled && !TemporalLayersConfigured(streams)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100150 RTC_LOG(LS_INFO)
151 << "No temporal layers and nack enabled -> resilience off";
asapersson5f7226f2016-11-25 04:37:00 -0800152 video_codec.VP8()->resilience = kResilienceOff;
153 }
Erik Språng08127a92016-11-16 16:41:30 +0100154 break;
155 }
156 case kVideoCodecVP9: {
157 if (!config.encoder_specific_settings)
158 *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings();
159 if (video_codec.mode == kScreensharing &&
160 config.encoder_specific_settings) {
161 video_codec.VP9()->flexibleMode = true;
162 // For now VP9 screensharing use 1 temporal and 2 spatial layers.
163 RTC_DCHECK_EQ(1, video_codec.VP9()->numberOfTemporalLayers);
164 RTC_DCHECK_EQ(2, video_codec.VP9()->numberOfSpatialLayers);
165 }
166 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
167 streams.back().temporal_layer_thresholds_bps.size() + 1);
emircanbbcc3562017-08-18 00:28:40 -0700168
169 if (nack_enabled && !TemporalLayersConfigured(streams) &&
170 video_codec.VP9()->numberOfSpatialLayers == 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100171 RTC_LOG(LS_INFO) << "No temporal or spatial layers and nack enabled -> "
172 << "resilience off";
emircanbbcc3562017-08-18 00:28:40 -0700173 video_codec.VP9()->resilienceOn = false;
174 }
Erik Språng08127a92016-11-16 16:41:30 +0100175 break;
176 }
177 case kVideoCodecH264: {
178 if (!config.encoder_specific_settings)
179 *video_codec.H264() = VideoEncoder::GetDefaultH264Settings();
180 break;
181 }
182 default:
183 // TODO(pbos): Support encoder_settings codec-agnostically.
184 RTC_DCHECK(!config.encoder_specific_settings)
185 << "Encoder-specific settings for codec type not wired up.";
186 break;
187 }
188
189 strncpy(video_codec.plName, payload_name.c_str(), kPayloadNameSize - 1);
190 video_codec.plName[kPayloadNameSize - 1] = '\0';
191 video_codec.plType = payload_type;
192 video_codec.numberOfSimulcastStreams =
193 static_cast<unsigned char>(streams.size());
194 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800195 bool codec_active = false;
196 for (const VideoStream& stream : streams) {
197 if (stream.active) {
198 codec_active = true;
199 break;
200 }
201 }
202 // Set active for the entire video codec for the non simulcast case.
203 video_codec.active = codec_active;
Erik Språng08127a92016-11-16 16:41:30 +0100204 if (video_codec.minBitrate < kEncoderMinBitrateKbps)
205 video_codec.minBitrate = kEncoderMinBitrateKbps;
ilnik04f4d122017-06-19 07:18:55 -0700206 video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs,
207 kDefaultOutlierFrameSizePercent};
kwiberg352444f2016-11-28 15:58:53 -0800208 RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams);
Erik Språng08127a92016-11-16 16:41:30 +0100209 if (video_codec.codecType == kVideoCodecVP9) {
210 // If the vector is empty, bitrates will be configured automatically.
211 RTC_DCHECK(config.spatial_layers.empty() ||
212 config.spatial_layers.size() ==
213 video_codec.VP9()->numberOfSpatialLayers);
214 RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers,
215 kMaxSimulcastStreams);
216 for (size_t i = 0; i < config.spatial_layers.size(); ++i)
217 video_codec.spatialLayers[i] = config.spatial_layers[i];
218 }
219 for (size_t i = 0; i < streams.size(); ++i) {
220 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
kwibergaf476c72016-11-28 15:21:39 -0800221 RTC_DCHECK_GT(streams[i].width, 0);
222 RTC_DCHECK_GT(streams[i].height, 0);
Erik Språng08127a92016-11-16 16:41:30 +0100223 RTC_DCHECK_GT(streams[i].max_framerate, 0);
sprang429600d2017-01-26 06:12:26 -0800224 // Different framerates not supported per stream at the moment, unless it's
225 // screenshare where there is an exception and a simulcast encoder adapter,
226 // which supports different framerates, is used instead.
227 if (config.content_type != VideoEncoderConfig::ContentType::kScreen) {
228 RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate);
229 }
Erik Språng08127a92016-11-16 16:41:30 +0100230 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0);
231 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
232 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
233 RTC_DCHECK_GE(streams[i].max_qp, 0);
234
235 sim_stream->width = static_cast<uint16_t>(streams[i].width);
236 sim_stream->height = static_cast<uint16_t>(streams[i].height);
237 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
238 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
239 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
240 sim_stream->qpMax = streams[i].max_qp;
241 sim_stream->numberOfTemporalLayers = static_cast<unsigned char>(
242 streams[i].temporal_layer_thresholds_bps.size() + 1);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800243 sim_stream->active = streams[i].active;
Erik Språng08127a92016-11-16 16:41:30 +0100244
245 video_codec.width =
246 std::max(video_codec.width, static_cast<uint16_t>(streams[i].width));
247 video_codec.height =
248 std::max(video_codec.height, static_cast<uint16_t>(streams[i].height));
249 video_codec.minBitrate =
250 std::min(static_cast<uint16_t>(video_codec.minBitrate),
251 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000));
252 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
253 video_codec.qpMax = std::max(video_codec.qpMax,
254 static_cast<unsigned int>(streams[i].max_qp));
255 }
256
257 if (video_codec.maxBitrate == 0) {
258 // Unset max bitrate -> cap to one bit per pixel.
259 video_codec.maxBitrate =
260 (video_codec.width * video_codec.height * video_codec.maxFramerate) /
261 1000;
262 }
263 if (video_codec.maxBitrate < kEncoderMinBitrateKbps)
264 video_codec.maxBitrate = kEncoderMinBitrateKbps;
265
266 RTC_DCHECK_GT(streams[0].max_framerate, 0);
267 video_codec.maxFramerate = streams[0].max_framerate;
268 return video_codec;
269}
270
271} // namespace webrtc