blob: d81358d4928929807c43d8c626edacc5cbd33ca9 [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
Anders Carlssondd8c1652018-01-30 10:32:13 +010013#include "api/video_codecs/video_encoder.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020014#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "common_video/include/video_bitrate_allocator.h"
16#include "modules/video_coding/codecs/vp8/screenshare_layers.h"
17#include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
18#include "modules/video_coding/codecs/vp8/temporal_layers.h"
19#include "modules/video_coding/include/video_coding_defines.h"
20#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
21#include "rtc_base/basictypes.h"
22#include "rtc_base/logging.h"
23#include "system_wrappers/include/clock.h"
Erik Språng08127a92016-11-16 16:41:30 +010024
25namespace webrtc {
emircanbbcc3562017-08-18 00:28:40 -070026namespace {
27bool TemporalLayersConfigured(const std::vector<VideoStream>& streams) {
28 for (const VideoStream& stream : streams) {
29 if (stream.temporal_layer_thresholds_bps.size() > 0)
30 return true;
31 }
32 return false;
33}
34} // namespace
Erik Språng08127a92016-11-16 16:41:30 +010035
36bool VideoCodecInitializer::SetupCodec(
37 const VideoEncoderConfig& config,
38 const VideoSendStream::Config::EncoderSettings settings,
39 const std::vector<VideoStream>& streams,
asapersson5f7226f2016-11-25 04:37:00 -080040 bool nack_enabled,
Erik Språng08127a92016-11-16 16:41:30 +010041 VideoCodec* codec,
42 std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator) {
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080043 if (PayloadStringToCodecType(settings.payload_name) == kVideoCodecMultiplex) {
Emircan Uysaler0a375472017-12-11 12:21:02 +053044 VideoSendStream::Config::EncoderSettings associated_codec_settings =
45 settings;
46 associated_codec_settings.payload_name =
47 CodecTypeToPayloadString(kVideoCodecVP9);
48 if (!SetupCodec(config, associated_codec_settings, streams, nack_enabled,
49 codec, bitrate_allocator)) {
50 RTC_LOG(LS_ERROR) << "Failed to create stereo encoder configuration.";
51 return false;
52 }
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080053 codec->codecType = kVideoCodecMultiplex;
Qiang Chena9329db2017-12-14 14:05:43 -080054 strncpy(codec->plName, settings.payload_name.c_str(),
55 sizeof(codec->plName));
Emircan Uysaler0a375472017-12-11 12:21:02 +053056 return true;
57 }
58
asapersson5f7226f2016-11-25 04:37:00 -080059 *codec =
60 VideoEncoderConfigToVideoCodec(config, streams, settings.payload_name,
61 settings.payload_type, nack_enabled);
Erik Språng08127a92016-11-16 16:41:30 +010062
63 std::unique_ptr<TemporalLayersFactory> tl_factory;
64 switch (codec->codecType) {
65 case kVideoCodecVP8: {
66 if (!codec->VP8()->tl_factory) {
67 if (codec->mode == kScreensharing &&
sprang429600d2017-01-26 06:12:26 -080068 (codec->numberOfSimulcastStreams > 1 ||
69 (codec->numberOfSimulcastStreams == 1 &&
70 codec->VP8()->numberOfTemporalLayers == 2))) {
Erik Språng08127a92016-11-16 16:41:30 +010071 // Conference mode temporal layering for screen content.
72 tl_factory.reset(new ScreenshareTemporalLayersFactory());
73 } else {
74 // Standard video temporal layers.
75 tl_factory.reset(new TemporalLayersFactory());
76 }
77 codec->VP8()->tl_factory = tl_factory.get();
78 }
79 break;
80 }
81 default: {
82 // TODO(sprang): Warn, once we have specific allocators for all supported
83 // codec types.
84 break;
85 }
86 }
87 *bitrate_allocator = CreateBitrateAllocator(*codec, std::move(tl_factory));
88
89 return true;
90}
91
92std::unique_ptr<VideoBitrateAllocator>
93VideoCodecInitializer::CreateBitrateAllocator(
94 const VideoCodec& codec,
95 std::unique_ptr<TemporalLayersFactory> tl_factory) {
96 std::unique_ptr<VideoBitrateAllocator> rate_allocator;
97
98 switch (codec.codecType) {
99 case kVideoCodecVP8: {
100 // Set up default VP8 temporal layer factory, if not provided.
101 rate_allocator.reset(
102 new SimulcastRateAllocator(codec, std::move(tl_factory)));
103 } break;
104 default:
105 rate_allocator.reset(new DefaultVideoBitrateAllocator(codec));
106 }
107
108 return rate_allocator;
109}
110
111// TODO(sprang): Split this up and separate the codec specific parts.
112VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
113 const VideoEncoderConfig& config,
114 const std::vector<VideoStream>& streams,
115 const std::string& payload_name,
asapersson5f7226f2016-11-25 04:37:00 -0800116 int payload_type,
117 bool nack_enabled) {
Erik Språng08127a92016-11-16 16:41:30 +0100118 static const int kEncoderMinBitrateKbps = 30;
119 RTC_DCHECK(!streams.empty());
120 RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0);
121
122 VideoCodec video_codec;
123 memset(&video_codec, 0, sizeof(video_codec));
kthelgason1cdddc92017-08-24 03:52:48 -0700124 video_codec.codecType = PayloadStringToCodecType(payload_name);
Erik Språng08127a92016-11-16 16:41:30 +0100125
126 switch (config.content_type) {
127 case VideoEncoderConfig::ContentType::kRealtimeVideo:
128 video_codec.mode = kRealtimeVideo;
129 break;
130 case VideoEncoderConfig::ContentType::kScreen:
131 video_codec.mode = kScreensharing;
aleloi47037412017-01-26 07:57:15 -0800132 if (!streams.empty() &&
Erik Språng08127a92016-11-16 16:41:30 +0100133 streams[0].temporal_layer_thresholds_bps.size() == 1) {
134 video_codec.targetBitrate =
135 streams[0].temporal_layer_thresholds_bps[0] / 1000;
136 }
137 break;
138 }
139
140 if (config.encoder_specific_settings)
141 config.encoder_specific_settings->FillEncoderSpecificSettings(&video_codec);
142
143 switch (video_codec.codecType) {
144 case kVideoCodecVP8: {
145 if (!config.encoder_specific_settings)
146 *video_codec.VP8() = VideoEncoder::GetDefaultVp8Settings();
147 video_codec.VP8()->numberOfTemporalLayers = static_cast<unsigned char>(
148 streams.back().temporal_layer_thresholds_bps.size() + 1);
emircanbbcc3562017-08-18 00:28:40 -0700149
150 if (nack_enabled && !TemporalLayersConfigured(streams)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(LS_INFO)
152 << "No temporal layers and nack enabled -> resilience off";
asapersson5f7226f2016-11-25 04:37:00 -0800153 video_codec.VP8()->resilience = kResilienceOff;
154 }
Erik Språng08127a92016-11-16 16:41:30 +0100155 break;
156 }
157 case kVideoCodecVP9: {
158 if (!config.encoder_specific_settings)
159 *video_codec.VP9() = VideoEncoder::GetDefaultVp9Settings();
160 if (video_codec.mode == kScreensharing &&
161 config.encoder_specific_settings) {
162 video_codec.VP9()->flexibleMode = true;
163 // For now VP9 screensharing use 1 temporal and 2 spatial layers.
164 RTC_DCHECK_EQ(1, video_codec.VP9()->numberOfTemporalLayers);
165 RTC_DCHECK_EQ(2, video_codec.VP9()->numberOfSpatialLayers);
166 }
167 video_codec.VP9()->numberOfTemporalLayers = static_cast<unsigned char>(
168 streams.back().temporal_layer_thresholds_bps.size() + 1);
emircanbbcc3562017-08-18 00:28:40 -0700169
170 if (nack_enabled && !TemporalLayersConfigured(streams) &&
171 video_codec.VP9()->numberOfSpatialLayers == 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100172 RTC_LOG(LS_INFO) << "No temporal or spatial layers and nack enabled -> "
173 << "resilience off";
emircanbbcc3562017-08-18 00:28:40 -0700174 video_codec.VP9()->resilienceOn = false;
175 }
Erik Språng08127a92016-11-16 16:41:30 +0100176 break;
177 }
178 case kVideoCodecH264: {
179 if (!config.encoder_specific_settings)
180 *video_codec.H264() = VideoEncoder::GetDefaultH264Settings();
181 break;
182 }
183 default:
184 // TODO(pbos): Support encoder_settings codec-agnostically.
185 RTC_DCHECK(!config.encoder_specific_settings)
186 << "Encoder-specific settings for codec type not wired up.";
187 break;
188 }
189
190 strncpy(video_codec.plName, payload_name.c_str(), kPayloadNameSize - 1);
191 video_codec.plName[kPayloadNameSize - 1] = '\0';
192 video_codec.plType = payload_type;
193 video_codec.numberOfSimulcastStreams =
194 static_cast<unsigned char>(streams.size());
195 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
Seth Hampson46e31ba2018-01-18 10:39:54 -0800196 bool codec_active = false;
197 for (const VideoStream& stream : streams) {
198 if (stream.active) {
199 codec_active = true;
200 break;
201 }
202 }
203 // Set active for the entire video codec for the non simulcast case.
204 video_codec.active = codec_active;
Erik Språng08127a92016-11-16 16:41:30 +0100205 if (video_codec.minBitrate < kEncoderMinBitrateKbps)
206 video_codec.minBitrate = kEncoderMinBitrateKbps;
ilnik04f4d122017-06-19 07:18:55 -0700207 video_codec.timing_frame_thresholds = {kDefaultTimingFramesDelayMs,
208 kDefaultOutlierFrameSizePercent};
kwiberg352444f2016-11-28 15:58:53 -0800209 RTC_DCHECK_LE(streams.size(), kMaxSimulcastStreams);
Erik Språng08127a92016-11-16 16:41:30 +0100210 if (video_codec.codecType == kVideoCodecVP9) {
211 // If the vector is empty, bitrates will be configured automatically.
212 RTC_DCHECK(config.spatial_layers.empty() ||
213 config.spatial_layers.size() ==
214 video_codec.VP9()->numberOfSpatialLayers);
215 RTC_DCHECK_LE(video_codec.VP9()->numberOfSpatialLayers,
216 kMaxSimulcastStreams);
217 for (size_t i = 0; i < config.spatial_layers.size(); ++i)
218 video_codec.spatialLayers[i] = config.spatial_layers[i];
219 }
220 for (size_t i = 0; i < streams.size(); ++i) {
221 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
kwibergaf476c72016-11-28 15:21:39 -0800222 RTC_DCHECK_GT(streams[i].width, 0);
223 RTC_DCHECK_GT(streams[i].height, 0);
Erik Språng08127a92016-11-16 16:41:30 +0100224 RTC_DCHECK_GT(streams[i].max_framerate, 0);
sprang429600d2017-01-26 06:12:26 -0800225 // Different framerates not supported per stream at the moment, unless it's
226 // screenshare where there is an exception and a simulcast encoder adapter,
227 // which supports different framerates, is used instead.
228 if (config.content_type != VideoEncoderConfig::ContentType::kScreen) {
229 RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate);
230 }
Erik Språng08127a92016-11-16 16:41:30 +0100231 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0);
232 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps);
233 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps);
234 RTC_DCHECK_GE(streams[i].max_qp, 0);
235
236 sim_stream->width = static_cast<uint16_t>(streams[i].width);
237 sim_stream->height = static_cast<uint16_t>(streams[i].height);
238 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
239 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
240 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
241 sim_stream->qpMax = streams[i].max_qp;
242 sim_stream->numberOfTemporalLayers = static_cast<unsigned char>(
243 streams[i].temporal_layer_thresholds_bps.size() + 1);
Seth Hampson46e31ba2018-01-18 10:39:54 -0800244 sim_stream->active = streams[i].active;
Erik Språng08127a92016-11-16 16:41:30 +0100245
246 video_codec.width =
247 std::max(video_codec.width, static_cast<uint16_t>(streams[i].width));
248 video_codec.height =
249 std::max(video_codec.height, static_cast<uint16_t>(streams[i].height));
250 video_codec.minBitrate =
251 std::min(static_cast<uint16_t>(video_codec.minBitrate),
252 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000));
253 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
254 video_codec.qpMax = std::max(video_codec.qpMax,
255 static_cast<unsigned int>(streams[i].max_qp));
256 }
257
258 if (video_codec.maxBitrate == 0) {
259 // Unset max bitrate -> cap to one bit per pixel.
260 video_codec.maxBitrate =
261 (video_codec.width * video_codec.height * video_codec.maxFramerate) /
262 1000;
263 }
264 if (video_codec.maxBitrate < kEncoderMinBitrateKbps)
265 video_codec.maxBitrate = kEncoderMinBitrateKbps;
266
267 RTC_DCHECK_GT(streams[0].max_framerate, 0);
268 video_codec.maxFramerate = streams[0].max_framerate;
269 return video_codec;
270}
271
272} // namespace webrtc